blob: 992b225d36ba7fed91fb789f9779fe5aa1c5b450 [file] [log] [blame]
drh3b74d032015-05-25 18:48:19 +00001/*
2** 2015-05-25
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
drh00452192015-06-17 18:24:40 +000013** This is a utility program designed to aid running regressions tests on
drha8781d92020-02-25 20:05:58 +000014** the SQLite library using data from external fuzzers.
drh3b74d032015-05-25 18:48:19 +000015**
16** This program reads content from an SQLite database file with the following
17** schema:
18**
19** CREATE TABLE db(
20** dbid INTEGER PRIMARY KEY, -- database id
21** dbcontent BLOB -- database disk file image
22** );
23** CREATE TABLE xsql(
24** sqlid INTEGER PRIMARY KEY, -- SQL script id
25** sqltext TEXT -- Text of SQL statements to run
26** );
drh00452192015-06-17 18:24:40 +000027** CREATE TABLE IF NOT EXISTS readme(
28** msg TEXT -- Human-readable description of this test collection
29** );
drh3b74d032015-05-25 18:48:19 +000030**
31** For each database file in the DB table, the SQL text in the XSQL table
drh00452192015-06-17 18:24:40 +000032** is run against that database. All README.MSG values are printed prior
33** to the start of the test (unless the --quiet option is used). If the
34** DB table is empty, then all entries in XSQL are run against an empty
35** in-memory database.
36**
37** This program is looking for crashes, assertion faults, and/or memory leaks.
38** No attempt is made to verify the output. The assumption is that either all
39** of the database files or all of the SQL statements are malformed inputs,
40** generated by a fuzzer, that need to be checked to make sure they do not
41** present a security risk.
drh3b74d032015-05-25 18:48:19 +000042**
43** This program also includes some command-line options to help with
drh00452192015-06-17 18:24:40 +000044** creation and maintenance of the source content database. The command
45**
46** ./fuzzcheck database.db --load-sql FILE...
47**
48** Loads all FILE... arguments into the XSQL table. The --load-db option
49** works the same but loads the files into the DB table. The -m option can
50** be used to initialize the README table. The "database.db" file is created
51** if it does not previously exist. Example:
52**
53** ./fuzzcheck new.db --load-sql *.sql
54** ./fuzzcheck new.db --load-db *.db
55** ./fuzzcheck new.db -m 'New test cases'
56**
57** The three commands above will create the "new.db" file and initialize all
58** tables. Then do "./fuzzcheck new.db" to run the tests.
59**
60** DEBUGGING HINTS:
61**
62** If fuzzcheck does crash, it can be run in the debugger and the content
63** of the global variable g.zTextName[] will identify the specific XSQL and
64** DB values that were running when the crash occurred.
drha8781d92020-02-25 20:05:58 +000065**
66** DBSQLFUZZ:
67**
68** The dbsqlfuzz fuzzer includes both a database file and SQL to run against
69** that database in its input. This utility can now process dbsqlfuzz
70** input files. Load such files using the "--load-dbsql FILE ..." command-line
71** option.
72**
73** Dbsqlfuzz inputs are ordinary text. The first part of the file is text
74** that describes the content of the database (using a lot of hexadecimal),
75** then there is a divider line followed by the SQL to run against the
76** database. Because they are ordinary text, dbsqlfuzz inputs are stored
77** in the XSQL table, as if they were ordinary SQL inputs. The isDbSql()
78** function can look at a text string and determine whether or not it is
79** a valid dbsqlfuzz input.
drh3b74d032015-05-25 18:48:19 +000080*/
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <stdarg.h>
85#include <ctype.h>
drha47e7092019-01-25 04:00:14 +000086#include <assert.h>
drh3b74d032015-05-25 18:48:19 +000087#include "sqlite3.h"
drhc56fac72015-10-29 13:48:15 +000088#define ISSPACE(X) isspace((unsigned char)(X))
89#define ISDIGIT(X) isdigit((unsigned char)(X))
90
drh3b74d032015-05-25 18:48:19 +000091
drh94701b02015-06-24 13:25:34 +000092#ifdef __unix__
93# include <signal.h>
94# include <unistd.h>
95#endif
96
drhf0a21722020-03-19 17:27:52 +000097#include <stddef.h>
98#if !defined(_MSC_VER)
99# include <stdint.h>
mistachkinac8ba262018-03-07 14:42:17 +0000100#endif
101
102#if defined(_MSC_VER)
103typedef unsigned char uint8_t;
drhea432ba2016-11-11 16:33:47 +0000104#endif
105
drh3b74d032015-05-25 18:48:19 +0000106/*
107** Files in the virtual file system.
108*/
109typedef struct VFile VFile;
110struct VFile {
111 char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */
112 int sz; /* Size of the file in bytes */
113 int nRef; /* Number of references to this file */
114 unsigned char *a; /* Content of the file. From malloc() */
115};
116typedef struct VHandle VHandle;
117struct VHandle {
118 sqlite3_file base; /* Base class. Must be first */
119 VFile *pVFile; /* The underlying file */
120};
121
122/*
123** The value of a database file template, or of an SQL script
124*/
125typedef struct Blob Blob;
126struct Blob {
127 Blob *pNext; /* Next in a list */
128 int id; /* Id of this Blob */
drhe5c5f2c2015-05-26 00:28:08 +0000129 int seq; /* Sequence number */
drh3b74d032015-05-25 18:48:19 +0000130 int sz; /* Size of this Blob in bytes */
131 unsigned char a[1]; /* Blob content. Extra space allocated as needed. */
132};
133
134/*
135** Maximum number of files in the in-memory virtual filesystem.
136*/
137#define MX_FILE 10
138
139/*
140** Maximum allowed file size
141*/
142#define MX_FILE_SZ 10000000
143
144/*
145** All global variables are gathered into the "g" singleton.
146*/
147static struct GlobalVars {
148 const char *zArgv0; /* Name of program */
drha7648f02019-12-18 13:02:18 +0000149 const char *zDbFile; /* Name of database file */
drh3b74d032015-05-25 18:48:19 +0000150 VFile aFile[MX_FILE]; /* The virtual filesystem */
151 int nDb; /* Number of template databases */
152 Blob *pFirstDb; /* Content of first template database */
153 int nSql; /* Number of SQL scripts */
154 Blob *pFirstSql; /* First SQL script */
drhbeaf5142016-12-26 00:15:56 +0000155 unsigned int uRandom; /* Seed for the SQLite PRNG */
drh3b74d032015-05-25 18:48:19 +0000156 char zTestName[100]; /* Name of current test */
157} g;
158
159/*
160** Print an error message and quit.
161*/
162static void fatalError(const char *zFormat, ...){
163 va_list ap;
drha7648f02019-12-18 13:02:18 +0000164 fprintf(stderr, "%s", g.zArgv0);
165 if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile);
166 if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName);
167 fprintf(stderr, ": ");
drh3b74d032015-05-25 18:48:19 +0000168 va_start(ap, zFormat);
169 vfprintf(stderr, zFormat, ap);
170 va_end(ap);
171 fprintf(stderr, "\n");
172 exit(1);
173}
174
175/*
drha7648f02019-12-18 13:02:18 +0000176** signal handler
drh94701b02015-06-24 13:25:34 +0000177*/
178#ifdef __unix__
drha7648f02019-12-18 13:02:18 +0000179static void signalHandler(int signum){
180 const char *zSig;
181 if( signum==SIGABRT ){
182 zSig = "abort";
183 }else if( signum==SIGALRM ){
184 zSig = "timeout";
185 }else if( signum==SIGSEGV ){
186 zSig = "segfault";
187 }else{
188 zSig = "signal";
189 }
190 fatalError(zSig);
drh94701b02015-06-24 13:25:34 +0000191}
192#endif
193
194/*
195** Set the an alarm to go off after N seconds. Disable the alarm
196** if N==0
197*/
198static void setAlarm(int N){
199#ifdef __unix__
200 alarm(N);
201#else
202 (void)N;
203#endif
204}
205
drh78057352015-06-24 23:17:35 +0000206#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh94701b02015-06-24 13:25:34 +0000207/*
drhd83e2832015-06-24 14:45:44 +0000208** This an SQL progress handler. After an SQL statement has run for
209** many steps, we want to interrupt it. This guards against infinite
210** loops from recursive common table expressions.
211**
212** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
213** In that case, hitting the progress handler is a fatal error.
214*/
215static int progressHandler(void *pVdbeLimitFlag){
216 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles");
217 return 1;
218}
drh78057352015-06-24 23:17:35 +0000219#endif
drhd83e2832015-06-24 14:45:44 +0000220
221/*
drh3b74d032015-05-25 18:48:19 +0000222** Reallocate memory. Show and error and quit if unable.
223*/
224static void *safe_realloc(void *pOld, int szNew){
drhc5412d52016-03-23 17:54:19 +0000225 void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew);
drh3b74d032015-05-25 18:48:19 +0000226 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew);
227 return pNew;
228}
229
230/*
231** Initialize the virtual file system.
232*/
233static void formatVfs(void){
234 int i;
235 for(i=0; i<MX_FILE; i++){
236 g.aFile[i].sz = -1;
237 g.aFile[i].zFilename = 0;
238 g.aFile[i].a = 0;
239 g.aFile[i].nRef = 0;
240 }
241}
242
243
244/*
245** Erase all information in the virtual file system.
246*/
247static void reformatVfs(void){
248 int i;
249 for(i=0; i<MX_FILE; i++){
250 if( g.aFile[i].sz<0 ) continue;
251 if( g.aFile[i].zFilename ){
252 free(g.aFile[i].zFilename);
253 g.aFile[i].zFilename = 0;
254 }
255 if( g.aFile[i].nRef>0 ){
256 fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef);
257 }
258 g.aFile[i].sz = -1;
259 free(g.aFile[i].a);
260 g.aFile[i].a = 0;
261 g.aFile[i].nRef = 0;
262 }
263}
264
265/*
266** Find a VFile by name
267*/
268static VFile *findVFile(const char *zName){
269 int i;
drha9542b12015-05-25 19:35:42 +0000270 if( zName==0 ) return 0;
drh3b74d032015-05-25 18:48:19 +0000271 for(i=0; i<MX_FILE; i++){
272 if( g.aFile[i].zFilename==0 ) continue;
273 if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i];
274 }
275 return 0;
276}
277
278/*
279** Find a VFile by name. Create it if it does not already exist and
280** initialize it to the size and content given.
281**
282** Return NULL only if the filesystem is full.
283*/
284static VFile *createVFile(const char *zName, int sz, unsigned char *pData){
285 VFile *pNew = findVFile(zName);
286 int i;
287 if( pNew ) return pNew;
288 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){}
289 if( i>=MX_FILE ) return 0;
290 pNew = &g.aFile[i];
drha9542b12015-05-25 19:35:42 +0000291 if( zName ){
drhe683b892016-02-15 18:47:26 +0000292 int nName = (int)strlen(zName)+1;
293 pNew->zFilename = safe_realloc(0, nName);
294 memcpy(pNew->zFilename, zName, nName);
drha9542b12015-05-25 19:35:42 +0000295 }else{
296 pNew->zFilename = 0;
297 }
drh3b74d032015-05-25 18:48:19 +0000298 pNew->nRef = 0;
299 pNew->sz = sz;
300 pNew->a = safe_realloc(0, sz);
301 if( sz>0 ) memcpy(pNew->a, pData, sz);
302 return pNew;
303}
304
305
306/*
307** Implementation of the "readfile(X)" SQL function. The entire content
308** of the file named X is read and returned as a BLOB. NULL is returned
309** if the file does not exist or is unreadable.
310*/
311static void readfileFunc(
312 sqlite3_context *context,
313 int argc,
314 sqlite3_value **argv
315){
316 const char *zName;
317 FILE *in;
318 long nIn;
319 void *pBuf;
320
321 zName = (const char*)sqlite3_value_text(argv[0]);
322 if( zName==0 ) return;
323 in = fopen(zName, "rb");
324 if( in==0 ) return;
325 fseek(in, 0, SEEK_END);
326 nIn = ftell(in);
327 rewind(in);
328 pBuf = sqlite3_malloc64( nIn );
329 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
330 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
331 }else{
332 sqlite3_free(pBuf);
333 }
334 fclose(in);
335}
336
337/*
drha8781d92020-02-25 20:05:58 +0000338** Implementation of the "readtextfile(X)" SQL function. The text content
339** of the file named X through the end of the file or to the first \000
340** character, whichever comes first, is read and returned as TEXT. NULL
341** is returned if the file does not exist or is unreadable.
342*/
343static void readtextfileFunc(
344 sqlite3_context *context,
345 int argc,
346 sqlite3_value **argv
347){
348 const char *zName;
349 FILE *in;
350 long nIn;
351 char *pBuf;
352
353 zName = (const char*)sqlite3_value_text(argv[0]);
354 if( zName==0 ) return;
355 in = fopen(zName, "rb");
356 if( in==0 ) return;
357 fseek(in, 0, SEEK_END);
358 nIn = ftell(in);
359 rewind(in);
360 pBuf = sqlite3_malloc64( nIn+1 );
361 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
362 pBuf[nIn] = 0;
363 sqlite3_result_text(context, pBuf, -1, sqlite3_free);
364 }else{
365 sqlite3_free(pBuf);
366 }
367 fclose(in);
368}
369
370/*
drh40e0e0d2015-09-22 18:51:17 +0000371** Implementation of the "writefile(X,Y)" SQL function. The argument Y
372** is written into file X. The number of bytes written is returned. Or
373** NULL is returned if something goes wrong, such as being unable to open
374** file X for writing.
375*/
376static void writefileFunc(
377 sqlite3_context *context,
378 int argc,
379 sqlite3_value **argv
380){
381 FILE *out;
382 const char *z;
383 sqlite3_int64 rc;
384 const char *zFile;
385
386 (void)argc;
387 zFile = (const char*)sqlite3_value_text(argv[0]);
388 if( zFile==0 ) return;
389 out = fopen(zFile, "wb");
390 if( out==0 ) return;
391 z = (const char*)sqlite3_value_blob(argv[1]);
392 if( z==0 ){
393 rc = 0;
394 }else{
395 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
396 }
397 fclose(out);
398 sqlite3_result_int64(context, rc);
399}
400
401
402/*
drh3b74d032015-05-25 18:48:19 +0000403** Load a list of Blob objects from the database
404*/
405static void blobListLoadFromDb(
406 sqlite3 *db, /* Read from this database */
407 const char *zSql, /* Query used to extract the blobs */
drha9542b12015-05-25 19:35:42 +0000408 int onlyId, /* Only load where id is this value */
drh3b74d032015-05-25 18:48:19 +0000409 int *pN, /* OUT: Write number of blobs loaded here */
410 Blob **ppList /* OUT: Write the head of the blob list here */
411){
412 Blob head;
413 Blob *p;
414 sqlite3_stmt *pStmt;
415 int n = 0;
416 int rc;
drha9542b12015-05-25 19:35:42 +0000417 char *z2;
drh3b74d032015-05-25 18:48:19 +0000418
drha9542b12015-05-25 19:35:42 +0000419 if( onlyId>0 ){
420 z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId);
421 }else{
422 z2 = sqlite3_mprintf("%s", zSql);
423 }
424 rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0);
425 sqlite3_free(z2);
drh3b74d032015-05-25 18:48:19 +0000426 if( rc ) fatalError("%s", sqlite3_errmsg(db));
427 head.pNext = 0;
428 p = &head;
429 while( SQLITE_ROW==sqlite3_step(pStmt) ){
430 int sz = sqlite3_column_bytes(pStmt, 1);
431 Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz );
432 pNew->id = sqlite3_column_int(pStmt, 0);
433 pNew->sz = sz;
drhe5c5f2c2015-05-26 00:28:08 +0000434 pNew->seq = n++;
drh3b74d032015-05-25 18:48:19 +0000435 pNew->pNext = 0;
436 memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz);
437 pNew->a[sz] = 0;
438 p->pNext = pNew;
439 p = pNew;
drh3b74d032015-05-25 18:48:19 +0000440 }
441 sqlite3_finalize(pStmt);
442 *pN = n;
443 *ppList = head.pNext;
444}
445
446/*
447** Free a list of Blob objects
448*/
449static void blobListFree(Blob *p){
450 Blob *pNext;
451 while( p ){
452 pNext = p->pNext;
453 free(p);
454 p = pNext;
455 }
456}
457
drh3b74d032015-05-25 18:48:19 +0000458/* Return the current wall-clock time */
459static sqlite3_int64 timeOfDay(void){
460 static sqlite3_vfs *clockVfs = 0;
461 sqlite3_int64 t;
drh8055a3e2018-11-21 14:27:34 +0000462 if( clockVfs==0 ){
463 clockVfs = sqlite3_vfs_find(0);
464 if( clockVfs==0 ) return 0;
465 }
drh3b74d032015-05-25 18:48:19 +0000466 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
467 clockVfs->xCurrentTimeInt64(clockVfs, &t);
468 }else{
469 double r;
470 clockVfs->xCurrentTime(clockVfs, &r);
471 t = (sqlite3_int64)(r*86400000.0);
472 }
473 return t;
474}
475
drha47e7092019-01-25 04:00:14 +0000476/***************************************************************************
477** Code to process combined database+SQL scripts generated by the
478** dbsqlfuzz fuzzer.
479*/
480
481/* An instance of the following object is passed by pointer as the
482** client data to various callbacks.
483*/
484typedef struct FuzzCtx {
485 sqlite3 *db; /* The database connection */
486 sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
487 sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
488 sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
489 unsigned nCb; /* Number of progress callbacks */
490 unsigned mxCb; /* Maximum number of progress callbacks allowed */
491 unsigned execCnt; /* Number of calls to the sqlite3_exec callback */
492 int timeoutHit; /* True when reaching a timeout */
493} FuzzCtx;
494
495/* Verbosity level for the dbsqlfuzz test runner */
496static int eVerbosity = 0;
497
498/* True to activate PRAGMA vdbe_debug=on */
499static int bVdbeDebug = 0;
500
501/* Timeout for each fuzzing attempt, in milliseconds */
drhed457032019-01-25 17:51:06 +0000502static int giTimeout = 10000; /* Defaults to 10 seconds */
drha47e7092019-01-25 04:00:14 +0000503
504/* Maximum number of progress handler callbacks */
505static unsigned int mxProgressCb = 2000;
506
507/* Maximum string length in SQLite */
508static int lengthLimit = 1000000;
509
drhbe03cc92020-01-20 14:42:09 +0000510/* Maximum expression depth */
511static int depthLimit = 500;
512
drh31999c52019-11-14 17:46:32 +0000513/* Limit on the amount of heap memory that can be used */
drha8781d92020-02-25 20:05:58 +0000514static sqlite3_int64 heapLimit = 100000000;
drh31999c52019-11-14 17:46:32 +0000515
drha47e7092019-01-25 04:00:14 +0000516/* Maximum byte-code program length in SQLite */
517static int vdbeOpLimit = 25000;
518
519/* Maximum size of the in-memory database */
520static sqlite3_int64 maxDbSize = 104857600;
drh39b3bcf2020-03-02 16:31:21 +0000521/* OOM simulation parameters */
522static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
523static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
524static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
525
526/* This routine is called when a simulated OOM occurs. It is broken
527** out as a separate routine to make it easy to set a breakpoint on
528** the OOM
529*/
530void oomFault(void){
531 if( eVerbosity ){
532 printf("Simulated OOM fault\n");
533 }
534 if( oomRepeat>0 ){
535 oomRepeat--;
536 }else{
537 oomCounter--;
538 }
539}
540
541/* This routine is a replacement malloc() that is used to simulate
542** Out-Of-Memory (OOM) errors for testing purposes.
543*/
544static void *oomMalloc(int nByte){
545 if( oomCounter ){
546 if( oomCounter==1 ){
547 oomFault();
548 return 0;
549 }else{
550 oomCounter--;
551 }
552 }
553 return defaultMalloc(nByte);
554}
555
556/* Register the OOM simulator. This must occur before any memory
557** allocations */
558static void registerOomSimulator(void){
559 sqlite3_mem_methods mem;
560 sqlite3_shutdown();
561 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
562 defaultMalloc = mem.xMalloc;
563 mem.xMalloc = oomMalloc;
564 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
565}
566
567/* Turn off any pending OOM simulation */
568static void disableOom(void){
569 oomCounter = 0;
570 oomRepeat = 0;
571}
drha47e7092019-01-25 04:00:14 +0000572
573/*
574** Translate a single byte of Hex into an integer.
575** This routine only works if h really is a valid hexadecimal
576** character: 0..9a..fA..F
577*/
drhed457032019-01-25 17:51:06 +0000578static unsigned char hexToInt(unsigned int h){
drha47e7092019-01-25 04:00:14 +0000579#ifdef SQLITE_EBCDIC
580 h += 9*(1&~(h>>4)); /* EBCDIC */
581#else
582 h += 9*(1&(h>>6)); /* ASCII */
583#endif
584 return h & 0xf;
585}
586
587/*
588** The first character of buffer zIn[0..nIn-1] is a '['. This routine
589** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it
590** does it makes corresponding changes to the *pK value and *pI value
591** and returns true. If the input buffer does not match the patterns,
592** no changes are made to either *pK or *pI and this routine returns false.
593*/
594static int isOffset(
595 const unsigned char *zIn, /* Text input */
596 int nIn, /* Bytes of input */
597 unsigned int *pK, /* half-byte cursor to adjust */
598 unsigned int *pI /* Input index to adjust */
599){
600 int i;
601 unsigned int k = 0;
602 unsigned char c;
603 for(i=1; i<nIn && (c = zIn[i])!=']'; i++){
604 if( !isxdigit(c) ) return 0;
605 k = k*16 + hexToInt(c);
606 }
607 if( i==nIn ) return 0;
608 *pK = 2*k;
609 *pI += i;
610 return 1;
611}
612
613/*
614** Decode the text starting at zIn into a binary database file.
615** The maximum length of zIn is nIn bytes. Compute the binary database
616** file contain in space obtained from sqlite3_malloc().
617**
618** Return the number of bytes of zIn consumed. Or return -1 if there
619** is an error. One potential error is that the recipe specifies a
620** database file larger than MX_FILE_SZ bytes.
621**
622** Abort on an OOM.
623*/
624static int decodeDatabase(
625 const unsigned char *zIn, /* Input text to be decoded */
626 int nIn, /* Bytes of input text */
627 unsigned char **paDecode, /* OUT: decoded database file */
628 int *pnDecode /* OUT: Size of decoded database */
629){
630 unsigned char *a; /* Database under construction */
631 int mx = 0; /* Current size of the database */
632 sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */
633 unsigned int i; /* Next byte of zIn[] to read */
634 unsigned int j; /* Temporary integer */
635 unsigned int k; /* half-byte cursor index for output */
636 unsigned int n; /* Number of bytes of input */
637 unsigned char b = 0;
638 if( nIn<4 ) return -1;
639 n = (unsigned int)nIn;
drhed457032019-01-25 17:51:06 +0000640 a = sqlite3_malloc64( nAlloc );
drha47e7092019-01-25 04:00:14 +0000641 if( a==0 ){
642 fprintf(stderr, "Out of memory!\n");
643 exit(1);
644 }
mistachkin065f3bf2019-03-20 05:45:03 +0000645 memset(a, 0, (size_t)nAlloc);
drha47e7092019-01-25 04:00:14 +0000646 for(i=k=0; i<n; i++){
drhaf638922019-02-07 00:17:36 +0000647 unsigned char c = (unsigned char)zIn[i];
drha47e7092019-01-25 04:00:14 +0000648 if( isxdigit(c) ){
649 k++;
650 if( k & 1 ){
651 b = hexToInt(c)*16;
652 }else{
653 b += hexToInt(c);
654 j = k/2 - 1;
655 if( j>=nAlloc ){
656 sqlite3_uint64 newSize;
657 if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){
658 if( eVerbosity ){
659 fprintf(stderr, "Input database too big: max %d bytes\n",
660 MX_FILE_SZ);
661 }
662 sqlite3_free(a);
663 return -1;
664 }
665 newSize = nAlloc*2;
666 if( newSize<=j ){
667 newSize = (j+4096)&~4095;
668 }
669 if( newSize>MX_FILE_SZ ){
670 if( j>=MX_FILE_SZ ){
671 sqlite3_free(a);
672 return -1;
673 }
674 newSize = MX_FILE_SZ;
675 }
drhed457032019-01-25 17:51:06 +0000676 a = sqlite3_realloc64( a, newSize );
drha47e7092019-01-25 04:00:14 +0000677 if( a==0 ){
678 fprintf(stderr, "Out of memory!\n");
679 exit(1);
680 }
681 assert( newSize > nAlloc );
mistachkin065f3bf2019-03-20 05:45:03 +0000682 memset(a+nAlloc, 0, (size_t)(newSize - nAlloc));
drha47e7092019-01-25 04:00:14 +0000683 nAlloc = newSize;
684 }
685 if( j>=(unsigned)mx ){
686 mx = (j + 4095)&~4095;
687 if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ;
688 }
689 assert( j<nAlloc );
690 a[j] = b;
691 }
692 }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){
693 continue;
694 }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){
695 i += 4;
696 break;
697 }
698 }
699 *pnDecode = mx;
700 *paDecode = a;
701 return i;
702}
703
704/*
705** Progress handler callback.
706**
707** The argument is the cutoff-time after which all processing should
708** stop. So return non-zero if the cut-off time is exceeded.
709*/
710static int progress_handler(void *pClientData) {
711 FuzzCtx *p = (FuzzCtx*)pClientData;
712 sqlite3_int64 iNow = timeOfDay();
713 int rc = iNow>=p->iCutoffTime;
714 sqlite3_int64 iDiff = iNow - p->iLastCb;
715 if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
716 p->nCb++;
717 if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1;
drhdf216592019-01-25 04:43:26 +0000718 if( rc && !p->timeoutHit && eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000719 printf("Timeout on progress callback %d\n", p->nCb);
720 fflush(stdout);
721 p->timeoutHit = 1;
722 }
723 return rc;
724}
725
726/*
727** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
728** "PRAGMA parser_trace" since they can dramatically increase the
729** amount of output without actually testing anything useful.
730**
731** Also block ATTACH and DETACH
732*/
733static int block_troublesome_sql(
734 void *Notused,
735 int eCode,
736 const char *zArg1,
737 const char *zArg2,
738 const char *zArg3,
739 const char *zArg4
740){
741 (void)Notused;
742 (void)zArg2;
743 (void)zArg3;
744 (void)zArg4;
745 if( eCode==SQLITE_PRAGMA ){
746 if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0
747 || sqlite3_stricmp("parser_trace", zArg1)==0
748 || sqlite3_stricmp("temp_store_directory", zArg1)==0
749 ){
750 return SQLITE_DENY;
751 }
drh39b3bcf2020-03-02 16:31:21 +0000752 if( sqlite3_stricmp("oom",zArg1)==0 && zArg2!=0 && zArg2[0]!=0 ){
753 oomCounter = atoi(zArg2);
754 }
drha47e7092019-01-25 04:00:14 +0000755 }else if( (eCode==SQLITE_ATTACH || eCode==SQLITE_DETACH)
756 && zArg1 && zArg1[0] ){
757 return SQLITE_DENY;
758 }
759 return SQLITE_OK;
760}
761
762/*
763** Run the SQL text
764*/
765static int runDbSql(sqlite3 *db, const char *zSql){
766 int rc;
767 sqlite3_stmt *pStmt;
drhaf638922019-02-07 00:17:36 +0000768 while( isspace(zSql[0]&0x7f) ) zSql++;
drha47e7092019-01-25 04:00:14 +0000769 if( zSql[0]==0 ) return SQLITE_OK;
drhdf216592019-01-25 04:43:26 +0000770 if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000771 printf("RUNNING-SQL: [%s]\n", zSql);
772 fflush(stdout);
773 }
774 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
775 if( rc==SQLITE_OK ){
776 while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
drhdf216592019-01-25 04:43:26 +0000777 if( eVerbosity>=5 ){
drha47e7092019-01-25 04:00:14 +0000778 int j;
779 for(j=0; j<sqlite3_column_count(pStmt); j++){
780 if( j ) printf(",");
781 switch( sqlite3_column_type(pStmt, j) ){
782 case SQLITE_NULL: {
783 printf("NULL");
784 break;
785 }
786 case SQLITE_INTEGER:
787 case SQLITE_FLOAT: {
788 printf("%s", sqlite3_column_text(pStmt, j));
789 break;
790 }
791 case SQLITE_BLOB: {
792 int n = sqlite3_column_bytes(pStmt, j);
793 int i;
794 const unsigned char *a;
795 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
796 printf("x'");
797 for(i=0; i<n; i++){
798 printf("%02x", a[i]);
799 }
800 printf("'");
801 break;
802 }
803 case SQLITE_TEXT: {
804 int n = sqlite3_column_bytes(pStmt, j);
805 int i;
806 const unsigned char *a;
807 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
808 printf("'");
809 for(i=0; i<n; i++){
810 if( a[i]=='\'' ){
811 printf("''");
812 }else{
813 putchar(a[i]);
814 }
815 }
816 printf("'");
817 break;
818 }
819 } /* End switch() */
820 } /* End for() */
821 printf("\n");
822 fflush(stdout);
drhdf216592019-01-25 04:43:26 +0000823 } /* End if( eVerbosity>=5 ) */
drha47e7092019-01-25 04:00:14 +0000824 } /* End while( SQLITE_ROW */
drhdf216592019-01-25 04:43:26 +0000825 if( rc!=SQLITE_DONE && eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000826 printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db));
827 fflush(stdout);
828 }
drhdf216592019-01-25 04:43:26 +0000829 }else if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000830 printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db));
831 fflush(stdout);
832 } /* End if( SQLITE_OK ) */
833 return sqlite3_finalize(pStmt);
834}
835
836/* Invoke this routine to run a single test case */
837int runCombinedDbSqlInput(const uint8_t *aData, size_t nByte){
838 int rc; /* SQLite API return value */
839 int iSql; /* Index in aData[] of start of SQL */
840 unsigned char *aDb = 0; /* Decoded database content */
841 int nDb = 0; /* Size of the decoded database */
842 int i; /* Loop counter */
843 int j; /* Start of current SQL statement */
844 char *zSql = 0; /* SQL text to run */
845 int nSql; /* Bytes of SQL text */
846 FuzzCtx cx; /* Fuzzing context */
847
848 if( nByte<10 ) return 0;
849 if( sqlite3_initialize() ) return 0;
850 if( sqlite3_memory_used()!=0 ){
851 int nAlloc = 0;
852 int nNotUsed = 0;
853 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
854 fprintf(stderr,"Memory leak in mutator: %lld bytes in %d allocations\n",
855 sqlite3_memory_used(), nAlloc);
856 exit(1);
857 }
858 memset(&cx, 0, sizeof(cx));
859 iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb);
860 if( iSql<0 ) return 0;
drhed457032019-01-25 17:51:06 +0000861 nSql = (int)(nByte - iSql);
drhdf216592019-01-25 04:43:26 +0000862 if( eVerbosity>=3 ){
drha47e7092019-01-25 04:00:14 +0000863 printf(
864 "****** %d-byte input, %d-byte database, %d-byte script "
865 "******\n", (int)nByte, nDb, nSql);
866 fflush(stdout);
867 }
868 rc = sqlite3_open(0, &cx.db);
869 if( rc ) return 1;
870 if( bVdbeDebug ){
871 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
872 }
873
874 /* Invoke the progress handler frequently to check to see if we
875 ** are taking too long. The progress handler will return true
drhed457032019-01-25 17:51:06 +0000876 ** (which will block further processing) if more than giTimeout seconds have
drha47e7092019-01-25 04:00:14 +0000877 ** elapsed since the start of the test.
878 */
879 cx.iLastCb = timeOfDay();
drhed457032019-01-25 17:51:06 +0000880 cx.iCutoffTime = cx.iLastCb + giTimeout; /* Now + giTimeout seconds */
drha47e7092019-01-25 04:00:14 +0000881 cx.mxCb = mxProgressCb;
882#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
883 sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
884#endif
885
886 /* Set a limit on the maximum size of a prepared statement, and the
887 ** maximum length of a string or blob */
888 if( vdbeOpLimit>0 ){
889 sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit);
890 }
891 if( lengthLimit>0 ){
892 sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit);
893 }
drhbe03cc92020-01-20 14:42:09 +0000894 if( depthLimit>0 ){
895 sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit);
896 }
drh31999c52019-11-14 17:46:32 +0000897 sqlite3_hard_heap_limit64(heapLimit);
drha47e7092019-01-25 04:00:14 +0000898
899 if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){
900 aDb[18] = aDb[19] = 1;
901 }
902 rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb,
903 SQLITE_DESERIALIZE_RESIZEABLE |
904 SQLITE_DESERIALIZE_FREEONCLOSE);
905 if( rc ){
906 fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc);
907 goto testrun_finished;
908 }
909 if( maxDbSize>0 ){
910 sqlite3_int64 x = maxDbSize;
911 sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x);
912 }
913
drh725a9c72019-01-25 13:03:38 +0000914 /* For high debugging levels, turn on debug mode */
915 if( eVerbosity>=5 ){
916 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0);
917 }
918
drha47e7092019-01-25 04:00:14 +0000919 /* Block debug pragmas and ATTACH/DETACH. But wait until after
920 ** deserialize to do this because deserialize depends on ATTACH */
921 sqlite3_set_authorizer(cx.db, block_troublesome_sql, 0);
922
923 /* Consistent PRNG seed */
924 sqlite3_randomness(0,0);
925
926 zSql = sqlite3_malloc( nSql + 1 );
927 if( zSql==0 ){
928 fprintf(stderr, "Out of memory!\n");
929 }else{
930 memcpy(zSql, aData+iSql, nSql);
931 zSql[nSql] = 0;
932 for(i=j=0; zSql[i]; i++){
933 if( zSql[i]==';' ){
934 char cSaved = zSql[i+1];
935 zSql[i+1] = 0;
936 if( sqlite3_complete(zSql+j) ){
937 rc = runDbSql(cx.db, zSql+j);
938 j = i+1;
939 }
940 zSql[i+1] = cSaved;
941 if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){
942 goto testrun_finished;
943 }
944 }
945 }
946 if( j<i ){
947 runDbSql(cx.db, zSql+j);
948 }
949 }
950testrun_finished:
951 sqlite3_free(zSql);
952 rc = sqlite3_close(cx.db);
953 if( rc!=SQLITE_OK ){
954 fprintf(stdout, "sqlite3_close() returns %d\n", rc);
955 }
drhdf216592019-01-25 04:43:26 +0000956 if( eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000957 fprintf(stdout, "Peak memory usages: %f MB\n",
958 sqlite3_memory_highwater(1) / 1000000.0);
959 }
960 if( sqlite3_memory_used()!=0 ){
961 int nAlloc = 0;
962 int nNotUsed = 0;
963 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
964 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n",
965 sqlite3_memory_used(), nAlloc);
966 exit(1);
967 }
968 return 0;
969}
970
971/*
972** END of the dbsqlfuzz code
973***************************************************************************/
974
975/* Look at a SQL text and try to determine if it begins with a database
976** description, such as would be found in a dbsqlfuzz test case. Return
977** true if this does appear to be a dbsqlfuzz test case and false otherwise.
978*/
979static int isDbSql(unsigned char *a, int n){
drhdf216592019-01-25 04:43:26 +0000980 unsigned char buf[12];
981 int i;
drha47e7092019-01-25 04:00:14 +0000982 if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1;
983 while( n>0 && isspace(a[0]) ){ a++; n--; }
drhdf216592019-01-25 04:43:26 +0000984 for(i=0; n>0 && i<8; n--, a++){
985 if( isxdigit(a[0]) ) buf[i++] = a[0];
986 }
987 if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1;
drha47e7092019-01-25 04:00:14 +0000988 return 0;
989}
990
drhe5da9352019-01-27 01:11:40 +0000991/* Implementation of the isdbsql(TEXT) SQL function.
992*/
993static void isDbSqlFunc(
994 sqlite3_context *context,
995 int argc,
996 sqlite3_value **argv
997){
998 int n = sqlite3_value_bytes(argv[0]);
999 unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]);
1000 sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n));
1001}
drha47e7092019-01-25 04:00:14 +00001002
drh3b74d032015-05-25 18:48:19 +00001003/* Methods for the VHandle object
1004*/
1005static int inmemClose(sqlite3_file *pFile){
1006 VHandle *p = (VHandle*)pFile;
1007 VFile *pVFile = p->pVFile;
1008 pVFile->nRef--;
1009 if( pVFile->nRef==0 && pVFile->zFilename==0 ){
1010 pVFile->sz = -1;
1011 free(pVFile->a);
1012 pVFile->a = 0;
1013 }
1014 return SQLITE_OK;
1015}
1016static int inmemRead(
1017 sqlite3_file *pFile, /* Read from this open file */
1018 void *pData, /* Store content in this buffer */
1019 int iAmt, /* Bytes of content */
1020 sqlite3_int64 iOfst /* Start reading here */
1021){
1022 VHandle *pHandle = (VHandle*)pFile;
1023 VFile *pVFile = pHandle->pVFile;
1024 if( iOfst<0 || iOfst>=pVFile->sz ){
1025 memset(pData, 0, iAmt);
1026 return SQLITE_IOERR_SHORT_READ;
1027 }
1028 if( iOfst+iAmt>pVFile->sz ){
1029 memset(pData, 0, iAmt);
drh1573dc32015-05-25 22:29:26 +00001030 iAmt = (int)(pVFile->sz - iOfst);
drhe45985b2018-12-14 02:29:56 +00001031 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001032 return SQLITE_IOERR_SHORT_READ;
1033 }
drhaca7ea12015-05-25 23:14:37 +00001034 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001035 return SQLITE_OK;
1036}
1037static int inmemWrite(
1038 sqlite3_file *pFile, /* Write to this file */
1039 const void *pData, /* Content to write */
1040 int iAmt, /* bytes to write */
1041 sqlite3_int64 iOfst /* Start writing here */
1042){
1043 VHandle *pHandle = (VHandle*)pFile;
1044 VFile *pVFile = pHandle->pVFile;
1045 if( iOfst+iAmt > pVFile->sz ){
drha9542b12015-05-25 19:35:42 +00001046 if( iOfst+iAmt >= MX_FILE_SZ ){
1047 return SQLITE_FULL;
1048 }
drh1573dc32015-05-25 22:29:26 +00001049 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt));
drh908aced2015-05-26 16:12:45 +00001050 if( iOfst > pVFile->sz ){
1051 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz));
1052 }
drh1573dc32015-05-25 22:29:26 +00001053 pVFile->sz = (int)(iOfst + iAmt);
drh3b74d032015-05-25 18:48:19 +00001054 }
1055 memcpy(pVFile->a + iOfst, pData, iAmt);
1056 return SQLITE_OK;
1057}
1058static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){
1059 VHandle *pHandle = (VHandle*)pFile;
1060 VFile *pVFile = pHandle->pVFile;
drh1573dc32015-05-25 22:29:26 +00001061 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize;
drh3b74d032015-05-25 18:48:19 +00001062 return SQLITE_OK;
1063}
1064static int inmemSync(sqlite3_file *pFile, int flags){
1065 return SQLITE_OK;
1066}
1067static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
1068 *pSize = ((VHandle*)pFile)->pVFile->sz;
1069 return SQLITE_OK;
1070}
1071static int inmemLock(sqlite3_file *pFile, int type){
1072 return SQLITE_OK;
1073}
1074static int inmemUnlock(sqlite3_file *pFile, int type){
1075 return SQLITE_OK;
1076}
1077static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){
1078 *pOut = 0;
1079 return SQLITE_OK;
1080}
1081static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){
1082 return SQLITE_NOTFOUND;
1083}
1084static int inmemSectorSize(sqlite3_file *pFile){
1085 return 512;
1086}
1087static int inmemDeviceCharacteristics(sqlite3_file *pFile){
1088 return
1089 SQLITE_IOCAP_SAFE_APPEND |
1090 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
1091 SQLITE_IOCAP_POWERSAFE_OVERWRITE;
1092}
1093
1094
1095/* Method table for VHandle
1096*/
1097static sqlite3_io_methods VHandleMethods = {
1098 /* iVersion */ 1,
1099 /* xClose */ inmemClose,
1100 /* xRead */ inmemRead,
1101 /* xWrite */ inmemWrite,
1102 /* xTruncate */ inmemTruncate,
1103 /* xSync */ inmemSync,
1104 /* xFileSize */ inmemFileSize,
1105 /* xLock */ inmemLock,
1106 /* xUnlock */ inmemUnlock,
1107 /* xCheck... */ inmemCheckReservedLock,
1108 /* xFileCtrl */ inmemFileControl,
1109 /* xSectorSz */ inmemSectorSize,
1110 /* xDevchar */ inmemDeviceCharacteristics,
1111 /* xShmMap */ 0,
1112 /* xShmLock */ 0,
1113 /* xShmBarrier */ 0,
1114 /* xShmUnmap */ 0,
1115 /* xFetch */ 0,
1116 /* xUnfetch */ 0
1117};
1118
1119/*
1120** Open a new file in the inmem VFS. All files are anonymous and are
1121** delete-on-close.
1122*/
1123static int inmemOpen(
1124 sqlite3_vfs *pVfs,
1125 const char *zFilename,
1126 sqlite3_file *pFile,
1127 int openFlags,
1128 int *pOutFlags
1129){
1130 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)"");
1131 VHandle *pHandle = (VHandle*)pFile;
drha9542b12015-05-25 19:35:42 +00001132 if( pVFile==0 ){
1133 return SQLITE_FULL;
1134 }
drh3b74d032015-05-25 18:48:19 +00001135 pHandle->pVFile = pVFile;
1136 pVFile->nRef++;
1137 pFile->pMethods = &VHandleMethods;
1138 if( pOutFlags ) *pOutFlags = openFlags;
1139 return SQLITE_OK;
1140}
1141
1142/*
1143** Delete a file by name
1144*/
1145static int inmemDelete(
1146 sqlite3_vfs *pVfs,
1147 const char *zFilename,
1148 int syncdir
1149){
1150 VFile *pVFile = findVFile(zFilename);
1151 if( pVFile==0 ) return SQLITE_OK;
1152 if( pVFile->nRef==0 ){
1153 free(pVFile->zFilename);
1154 pVFile->zFilename = 0;
1155 pVFile->sz = -1;
1156 free(pVFile->a);
1157 pVFile->a = 0;
1158 return SQLITE_OK;
1159 }
1160 return SQLITE_IOERR_DELETE;
1161}
1162
1163/* Check for the existance of a file
1164*/
1165static int inmemAccess(
1166 sqlite3_vfs *pVfs,
1167 const char *zFilename,
1168 int flags,
1169 int *pResOut
1170){
1171 VFile *pVFile = findVFile(zFilename);
1172 *pResOut = pVFile!=0;
1173 return SQLITE_OK;
1174}
1175
1176/* Get the canonical pathname for a file
1177*/
1178static int inmemFullPathname(
1179 sqlite3_vfs *pVfs,
1180 const char *zFilename,
1181 int nOut,
1182 char *zOut
1183){
1184 sqlite3_snprintf(nOut, zOut, "%s", zFilename);
1185 return SQLITE_OK;
1186}
1187
drhbeaf5142016-12-26 00:15:56 +00001188/* Always use the same random see, for repeatability.
1189*/
1190static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
1191 memset(zBuf, 0, nBuf);
1192 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom));
1193 return nBuf;
1194}
1195
drh3b74d032015-05-25 18:48:19 +00001196/*
1197** Register the VFS that reads from the g.aFile[] set of files.
1198*/
drhbeaf5142016-12-26 00:15:56 +00001199static void inmemVfsRegister(int makeDefault){
drh3b74d032015-05-25 18:48:19 +00001200 static sqlite3_vfs inmemVfs;
1201 sqlite3_vfs *pDefault = sqlite3_vfs_find(0);
drh5337dac2015-11-25 15:15:03 +00001202 inmemVfs.iVersion = 3;
drh3b74d032015-05-25 18:48:19 +00001203 inmemVfs.szOsFile = sizeof(VHandle);
1204 inmemVfs.mxPathname = 200;
1205 inmemVfs.zName = "inmem";
1206 inmemVfs.xOpen = inmemOpen;
1207 inmemVfs.xDelete = inmemDelete;
1208 inmemVfs.xAccess = inmemAccess;
1209 inmemVfs.xFullPathname = inmemFullPathname;
drhbeaf5142016-12-26 00:15:56 +00001210 inmemVfs.xRandomness = inmemRandomness;
drh3b74d032015-05-25 18:48:19 +00001211 inmemVfs.xSleep = pDefault->xSleep;
drh5337dac2015-11-25 15:15:03 +00001212 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64;
drhbeaf5142016-12-26 00:15:56 +00001213 sqlite3_vfs_register(&inmemVfs, makeDefault);
drh3b74d032015-05-25 18:48:19 +00001214};
1215
drh3b74d032015-05-25 18:48:19 +00001216/*
drhe5c5f2c2015-05-26 00:28:08 +00001217** Allowed values for the runFlags parameter to runSql()
1218*/
1219#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
1220#define SQL_OUTPUT 0x0002 /* Show the SQL output */
1221
1222/*
drh3b74d032015-05-25 18:48:19 +00001223** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
1224** stop if an error is encountered.
1225*/
drhe5c5f2c2015-05-26 00:28:08 +00001226static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
drh3b74d032015-05-25 18:48:19 +00001227 const char *zMore;
1228 sqlite3_stmt *pStmt;
1229
1230 while( zSql && zSql[0] ){
1231 zMore = 0;
1232 pStmt = 0;
1233 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore);
drh4ab31472015-05-25 22:17:06 +00001234 if( zMore==zSql ) break;
drhe5c5f2c2015-05-26 00:28:08 +00001235 if( runFlags & SQL_TRACE ){
drh4ab31472015-05-25 22:17:06 +00001236 const char *z = zSql;
1237 int n;
drhc56fac72015-10-29 13:48:15 +00001238 while( z<zMore && ISSPACE(z[0]) ) z++;
drh4ab31472015-05-25 22:17:06 +00001239 n = (int)(zMore - z);
drhc56fac72015-10-29 13:48:15 +00001240 while( n>0 && ISSPACE(z[n-1]) ) n--;
drh4ab31472015-05-25 22:17:06 +00001241 if( n==0 ) break;
1242 if( pStmt==0 ){
1243 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
1244 }else{
1245 printf("TRACE: %.*s\n", n, z);
1246 }
1247 }
drh3b74d032015-05-25 18:48:19 +00001248 zSql = zMore;
1249 if( pStmt ){
drhe5c5f2c2015-05-26 00:28:08 +00001250 if( (runFlags & SQL_OUTPUT)==0 ){
1251 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
1252 }else{
1253 int nCol = -1;
1254 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1255 int i;
1256 if( nCol<0 ){
1257 nCol = sqlite3_column_count(pStmt);
1258 }else if( nCol>0 ){
1259 printf("--------------------------------------------\n");
1260 }
1261 for(i=0; i<nCol; i++){
1262 int eType = sqlite3_column_type(pStmt,i);
1263 printf("%s = ", sqlite3_column_name(pStmt,i));
1264 switch( eType ){
1265 case SQLITE_NULL: {
1266 printf("NULL\n");
1267 break;
1268 }
1269 case SQLITE_INTEGER: {
1270 printf("INT %s\n", sqlite3_column_text(pStmt,i));
1271 break;
1272 }
1273 case SQLITE_FLOAT: {
1274 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i));
1275 break;
1276 }
1277 case SQLITE_TEXT: {
1278 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i));
1279 break;
1280 }
1281 case SQLITE_BLOB: {
1282 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i));
1283 break;
1284 }
1285 }
1286 }
1287 }
1288 }
drh3b74d032015-05-25 18:48:19 +00001289 sqlite3_finalize(pStmt);
drh3b74d032015-05-25 18:48:19 +00001290 }
1291 }
1292}
1293
drha9542b12015-05-25 19:35:42 +00001294/*
drh9a645862015-06-24 12:44:42 +00001295** Rebuild the database file.
1296**
1297** (1) Remove duplicate entries
1298** (2) Put all entries in order
1299** (3) Vacuum
1300*/
drhe5da9352019-01-27 01:11:40 +00001301static void rebuild_database(sqlite3 *db, int dbSqlOnly){
drh9a645862015-06-24 12:44:42 +00001302 int rc;
drhe5da9352019-01-27 01:11:40 +00001303 char *zSql;
1304 zSql = sqlite3_mprintf(
drh9a645862015-06-24 12:44:42 +00001305 "BEGIN;\n"
1306 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
1307 "DELETE FROM db;\n"
drh5ecf9032018-05-08 12:49:53 +00001308 "INSERT INTO db(dbid, dbcontent) "
1309 " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001310 "DROP TABLE dbx;\n"
drhe5da9352019-01-27 01:11:40 +00001311 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n"
drh9a645862015-06-24 12:44:42 +00001312 "DELETE FROM xsql;\n"
drh5ecf9032018-05-08 12:49:53 +00001313 "INSERT INTO xsql(sqlid,sqltext) "
1314 " SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001315 "DROP TABLE sx;\n"
1316 "COMMIT;\n"
1317 "PRAGMA page_size=1024;\n"
drhe5da9352019-01-27 01:11:40 +00001318 "VACUUM;\n",
1319 dbSqlOnly ? " WHERE isdbsql(sqltext)" : ""
1320 );
1321 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1322 sqlite3_free(zSql);
drh9a645862015-06-24 12:44:42 +00001323 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db));
1324}
1325
1326/*
drh53e66c32015-07-24 15:49:23 +00001327** Return the value of a hexadecimal digit. Return -1 if the input
1328** is not a hex digit.
1329*/
1330static int hexDigitValue(char c){
1331 if( c>='0' && c<='9' ) return c - '0';
1332 if( c>='a' && c<='f' ) return c - 'a' + 10;
1333 if( c>='A' && c<='F' ) return c - 'A' + 10;
1334 return -1;
1335}
1336
1337/*
1338** Interpret zArg as an integer value, possibly with suffixes.
1339*/
1340static int integerValue(const char *zArg){
1341 sqlite3_int64 v = 0;
1342 static const struct { char *zSuffix; int iMult; } aMult[] = {
1343 { "KiB", 1024 },
1344 { "MiB", 1024*1024 },
1345 { "GiB", 1024*1024*1024 },
1346 { "KB", 1000 },
1347 { "MB", 1000000 },
1348 { "GB", 1000000000 },
1349 { "K", 1000 },
1350 { "M", 1000000 },
1351 { "G", 1000000000 },
1352 };
1353 int i;
1354 int isNeg = 0;
1355 if( zArg[0]=='-' ){
1356 isNeg = 1;
1357 zArg++;
1358 }else if( zArg[0]=='+' ){
1359 zArg++;
1360 }
1361 if( zArg[0]=='0' && zArg[1]=='x' ){
1362 int x;
1363 zArg += 2;
1364 while( (x = hexDigitValue(zArg[0]))>=0 ){
1365 v = (v<<4) + x;
1366 zArg++;
1367 }
1368 }else{
drhc56fac72015-10-29 13:48:15 +00001369 while( ISDIGIT(zArg[0]) ){
drh53e66c32015-07-24 15:49:23 +00001370 v = v*10 + zArg[0] - '0';
1371 zArg++;
1372 }
1373 }
1374 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
1375 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1376 v *= aMult[i].iMult;
1377 break;
1378 }
1379 }
1380 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648");
1381 return (int)(isNeg? -v : v);
1382}
1383
1384/*
drh725a9c72019-01-25 13:03:38 +00001385** Return the number of "v" characters in a string. Return 0 if there
1386** are any characters in the string other than "v".
1387*/
1388static int numberOfVChar(const char *z){
1389 int N = 0;
1390 while( z[0] && z[0]=='v' ){
1391 z++;
1392 N++;
1393 }
1394 return z[0]==0 ? N : 0;
1395}
1396
1397/*
drha9542b12015-05-25 19:35:42 +00001398** Print sketchy documentation for this utility program
1399*/
1400static void showHelp(void){
1401 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
1402 printf(
1403"Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
1404"each database, checking for crashes and memory leaks.\n"
1405"Options:\n"
drha36e01a2016-08-03 13:40:54 +00001406" --cell-size-check Set the PRAGMA cell_size_check=ON\n"
1407" --dbid N Use only the database where dbid=N\n"
1408" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
1409" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
1410" --help Show this help text\n"
drh5180d682018-08-06 01:39:31 +00001411" --info Show information about SOURCE-DB w/o running tests\n"
drhbe03cc92020-01-20 14:42:09 +00001412" --limit-depth N Limit expression depth to N\n"
drha36e01a2016-08-03 13:40:54 +00001413" --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
1414" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
drh5ecf9032018-05-08 12:49:53 +00001415" --load-sql ARGS... Load SQL scripts fron files into SOURCE-DB\n"
drha36e01a2016-08-03 13:40:54 +00001416" --load-db ARGS... Load template databases from files into SOURCE_DB\n"
drhe5da9352019-01-27 01:11:40 +00001417" --load-dbsql ARGS.. Load dbsqlfuzz outputs into the xsql table\n"
drha36e01a2016-08-03 13:40:54 +00001418" -m TEXT Add a description to the database\n"
1419" --native-vfs Use the native VFS for initially empty database files\n"
drh174f8552017-03-20 22:58:27 +00001420" --native-malloc Turn off MEMSYS3/5 and Lookaside\n"
drhea432ba2016-11-11 16:33:47 +00001421" --oss-fuzz Enable OSS-FUZZ testing\n"
drhbeaf5142016-12-26 00:15:56 +00001422" --prng-seed N Seed value for the PRGN inside of SQLite\n"
drh5180d682018-08-06 01:39:31 +00001423" -q|--quiet Reduced output\n"
drha36e01a2016-08-03 13:40:54 +00001424" --rebuild Rebuild and vacuum the database file\n"
1425" --result-trace Show the results of each SQL command\n"
1426" --sqlid N Use only SQL where sqlid=N\n"
1427" --timeout N Abort if any single test needs more than N seconds\n"
1428" -v|--verbose Increased output. Repeat for more output.\n"
drh6e1c45e2019-12-18 13:42:04 +00001429" --vdbe-debug Activate VDBE debugging.\n"
drha9542b12015-05-25 19:35:42 +00001430 );
1431}
1432
drh3b74d032015-05-25 18:48:19 +00001433int main(int argc, char **argv){
1434 sqlite3_int64 iBegin; /* Start time of this program */
drh3b74d032015-05-25 18:48:19 +00001435 int quietFlag = 0; /* True if --quiet or -q */
1436 int verboseFlag = 0; /* True if --verbose or -v */
1437 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */
drh5ecf9032018-05-08 12:49:53 +00001438 int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */
drh3b74d032015-05-25 18:48:19 +00001439 sqlite3 *db = 0; /* The open database connection */
drhd9972ef2015-05-26 17:57:56 +00001440 sqlite3_stmt *pStmt; /* A prepared statement */
drh3b74d032015-05-25 18:48:19 +00001441 int rc; /* Result code from SQLite interface calls */
1442 Blob *pSql; /* For looping over SQL scripts */
1443 Blob *pDb; /* For looping over template databases */
1444 int i; /* Loop index for the argv[] loop */
drhe5da9352019-01-27 01:11:40 +00001445 int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */
drha9542b12015-05-25 19:35:42 +00001446 int onlySqlid = -1; /* --sqlid */
1447 int onlyDbid = -1; /* --dbid */
drh15b31282015-05-25 21:59:05 +00001448 int nativeFlag = 0; /* --native-vfs */
drh9a645862015-06-24 12:44:42 +00001449 int rebuildFlag = 0; /* --rebuild */
drhd83e2832015-06-24 14:45:44 +00001450 int vdbeLimitFlag = 0; /* --limit-vdbe */
drh5180d682018-08-06 01:39:31 +00001451 int infoFlag = 0; /* --info */
drh94701b02015-06-24 13:25:34 +00001452 int timeoutTest = 0; /* undocumented --timeout-test flag */
drhe5c5f2c2015-05-26 00:28:08 +00001453 int runFlags = 0; /* Flags sent to runSql() */
drhd9972ef2015-05-26 17:57:56 +00001454 char *zMsg = 0; /* Add this message */
1455 int nSrcDb = 0; /* Number of source databases */
1456 char **azSrcDb = 0; /* Array of source database names */
1457 int iSrcDb; /* Loop over all source databases */
1458 int nTest = 0; /* Total number of tests performed */
1459 char *zDbName = ""; /* Appreviated name of a source database */
drh5ecf9032018-05-08 12:49:53 +00001460 const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */
drh1421d982015-05-27 03:46:18 +00001461 int cellSzCkFlag = 0; /* --cell-size-check */
drh5ecf9032018-05-08 12:49:53 +00001462 int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */
drhd4ddcbc2015-06-25 02:25:28 +00001463 int iTimeout = 120; /* Default 120-second timeout */
drh31999c52019-11-14 17:46:32 +00001464 int nMem = 0; /* Memory limit override */
drh362b66f2016-11-14 18:27:41 +00001465 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */
drh40e0e0d2015-09-22 18:51:17 +00001466 char *zExpDb = 0; /* Write Databases to files in this directory */
1467 char *zExpSql = 0; /* Write SQL to files in this directory */
drh6653fbe2015-11-13 20:52:49 +00001468 void *pHeap = 0; /* Heap for use by SQLite */
drhea432ba2016-11-11 16:33:47 +00001469 int ossFuzz = 0; /* enable OSS-FUZZ testing */
drh362b66f2016-11-14 18:27:41 +00001470 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */
drh174f8552017-03-20 22:58:27 +00001471 int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */
drhbeaf5142016-12-26 00:15:56 +00001472 sqlite3_vfs *pDfltVfs; /* The default VFS */
drhf2cf4122018-05-08 13:03:31 +00001473 int openFlags4Data; /* Flags for sqlite3_open_v2() */
drh725a9c72019-01-25 13:03:38 +00001474 int nV; /* How much to increase verbosity with -vvvv */
drh3b74d032015-05-25 18:48:19 +00001475
drh39b3bcf2020-03-02 16:31:21 +00001476 registerOomSimulator();
drh8055a3e2018-11-21 14:27:34 +00001477 sqlite3_initialize();
drh3b74d032015-05-25 18:48:19 +00001478 iBegin = timeOfDay();
drh94701b02015-06-24 13:25:34 +00001479#ifdef __unix__
drha7648f02019-12-18 13:02:18 +00001480 signal(SIGALRM, signalHandler);
1481 signal(SIGSEGV, signalHandler);
1482 signal(SIGABRT, signalHandler);
drh94701b02015-06-24 13:25:34 +00001483#endif
drh3b74d032015-05-25 18:48:19 +00001484 g.zArgv0 = argv[0];
drhf2cf4122018-05-08 13:03:31 +00001485 openFlags4Data = SQLITE_OPEN_READONLY;
drh4d6fda72015-05-26 18:58:32 +00001486 zFailCode = getenv("TEST_FAILURE");
drhbeaf5142016-12-26 00:15:56 +00001487 pDfltVfs = sqlite3_vfs_find(0);
1488 inmemVfsRegister(1);
drh3b74d032015-05-25 18:48:19 +00001489 for(i=1; i<argc; i++){
1490 const char *z = argv[i];
1491 if( z[0]=='-' ){
1492 z++;
1493 if( z[0]=='-' ) z++;
drh1421d982015-05-27 03:46:18 +00001494 if( strcmp(z,"cell-size-check")==0 ){
1495 cellSzCkFlag = 1;
1496 }else
drha9542b12015-05-25 19:35:42 +00001497 if( strcmp(z,"dbid")==0 ){
1498 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001499 onlyDbid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001500 }else
drh40e0e0d2015-09-22 18:51:17 +00001501 if( strcmp(z,"export-db")==0 ){
1502 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1503 zExpDb = argv[++i];
1504 }else
drhe5da9352019-01-27 01:11:40 +00001505 if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){
drh40e0e0d2015-09-22 18:51:17 +00001506 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1507 zExpSql = argv[++i];
1508 }else
drh3b74d032015-05-25 18:48:19 +00001509 if( strcmp(z,"help")==0 ){
1510 showHelp();
1511 return 0;
1512 }else
drh5180d682018-08-06 01:39:31 +00001513 if( strcmp(z,"info")==0 ){
1514 infoFlag = 1;
1515 }else
drhbe03cc92020-01-20 14:42:09 +00001516 if( strcmp(z,"limit-depth")==0 ){
1517 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1518 depthLimit = integerValue(argv[++i]);
1519 }else
drh53e66c32015-07-24 15:49:23 +00001520 if( strcmp(z,"limit-mem")==0 ){
1521 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1522 nMem = integerValue(argv[++i]);
1523 }else
drhd83e2832015-06-24 14:45:44 +00001524 if( strcmp(z,"limit-vdbe")==0 ){
1525 vdbeLimitFlag = 1;
1526 }else
drh3b74d032015-05-25 18:48:19 +00001527 if( strcmp(z,"load-sql")==0 ){
drha8781d92020-02-25 20:05:58 +00001528 zInsSql = "INSERT INTO xsql(sqltext)"
1529 "VALUES(CAST(readtextfile(?1) AS text))";
drh3b74d032015-05-25 18:48:19 +00001530 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001531 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001532 break;
1533 }else
1534 if( strcmp(z,"load-db")==0 ){
1535 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
1536 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001537 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001538 break;
1539 }else
drhe5da9352019-01-27 01:11:40 +00001540 if( strcmp(z,"load-dbsql")==0 ){
drha8781d92020-02-25 20:05:58 +00001541 zInsSql = "INSERT INTO xsql(sqltext)"
1542 "VALUES(CAST(readtextfile(?1) AS text))";
drhe5da9352019-01-27 01:11:40 +00001543 iFirstInsArg = i+1;
1544 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
1545 dbSqlOnly = 1;
1546 break;
1547 }else
drhd9972ef2015-05-26 17:57:56 +00001548 if( strcmp(z,"m")==0 ){
1549 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1550 zMsg = argv[++i];
drhf2cf4122018-05-08 13:03:31 +00001551 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drhd9972ef2015-05-26 17:57:56 +00001552 }else
drh174f8552017-03-20 22:58:27 +00001553 if( strcmp(z,"native-malloc")==0 ){
1554 nativeMalloc = 1;
1555 }else
drh15b31282015-05-25 21:59:05 +00001556 if( strcmp(z,"native-vfs")==0 ){
1557 nativeFlag = 1;
1558 }else
drhea432ba2016-11-11 16:33:47 +00001559 if( strcmp(z,"oss-fuzz")==0 ){
1560 ossFuzz = 1;
1561 }else
drhbeaf5142016-12-26 00:15:56 +00001562 if( strcmp(z,"prng-seed")==0 ){
1563 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1564 g.uRandom = atoi(argv[++i]);
1565 }else
drh3b74d032015-05-25 18:48:19 +00001566 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
1567 quietFlag = 1;
1568 verboseFlag = 0;
drha47e7092019-01-25 04:00:14 +00001569 eVerbosity = 0;
drh3b74d032015-05-25 18:48:19 +00001570 }else
drh9a645862015-06-24 12:44:42 +00001571 if( strcmp(z,"rebuild")==0 ){
1572 rebuildFlag = 1;
drhf2cf4122018-05-08 13:03:31 +00001573 openFlags4Data = SQLITE_OPEN_READWRITE;
drh9a645862015-06-24 12:44:42 +00001574 }else
drhe5c5f2c2015-05-26 00:28:08 +00001575 if( strcmp(z,"result-trace")==0 ){
1576 runFlags |= SQL_OUTPUT;
1577 }else
drha9542b12015-05-25 19:35:42 +00001578 if( strcmp(z,"sqlid")==0 ){
1579 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001580 onlySqlid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001581 }else
drh92298632015-06-24 23:44:30 +00001582 if( strcmp(z,"timeout")==0 ){
1583 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001584 iTimeout = integerValue(argv[++i]);
drh92298632015-06-24 23:44:30 +00001585 }else
drh94701b02015-06-24 13:25:34 +00001586 if( strcmp(z,"timeout-test")==0 ){
1587 timeoutTest = 1;
1588#ifndef __unix__
1589 fatalError("timeout is not available on non-unix systems");
1590#endif
1591 }else
drh6e1c45e2019-12-18 13:42:04 +00001592 if( strcmp(z,"vdbe-debug")==0 ){
1593 bVdbeDebug = 1;
1594 }else
drh725a9c72019-01-25 13:03:38 +00001595 if( strcmp(z,"verbose")==0 ){
drh3b74d032015-05-25 18:48:19 +00001596 quietFlag = 0;
drh4c9d2282016-02-18 14:03:15 +00001597 verboseFlag++;
drha47e7092019-01-25 04:00:14 +00001598 eVerbosity++;
drh4c9d2282016-02-18 14:03:15 +00001599 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
drh3b74d032015-05-25 18:48:19 +00001600 }else
drh725a9c72019-01-25 13:03:38 +00001601 if( (nV = numberOfVChar(z))>=1 ){
1602 quietFlag = 0;
1603 verboseFlag += nV;
1604 eVerbosity += nV;
1605 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
1606 }else
drha47e7092019-01-25 04:00:14 +00001607 if( strcmp(z,"version")==0 ){
1608 int ii;
drhed457032019-01-25 17:51:06 +00001609 const char *zz;
drha47e7092019-01-25 04:00:14 +00001610 printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid());
drhed457032019-01-25 17:51:06 +00001611 for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){
1612 printf("%s\n", zz);
drha47e7092019-01-25 04:00:14 +00001613 }
1614 return 0;
1615 }else
drh3b74d032015-05-25 18:48:19 +00001616 {
1617 fatalError("unknown option: %s", argv[i]);
1618 }
1619 }else{
drhd9972ef2015-05-26 17:57:56 +00001620 nSrcDb++;
1621 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0]));
1622 azSrcDb[nSrcDb-1] = argv[i];
drh3b74d032015-05-25 18:48:19 +00001623 }
1624 }
drhd9972ef2015-05-26 17:57:56 +00001625 if( nSrcDb==0 ) fatalError("no source database specified");
1626 if( nSrcDb>1 ){
1627 if( zMsg ){
1628 fatalError("cannot change the description of more than one database");
drh3b74d032015-05-25 18:48:19 +00001629 }
drhd9972ef2015-05-26 17:57:56 +00001630 if( zInsSql ){
1631 fatalError("cannot import into more than one database");
1632 }
drh3b74d032015-05-25 18:48:19 +00001633 }
1634
drhd9972ef2015-05-26 17:57:56 +00001635 /* Process each source database separately */
1636 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
drha7648f02019-12-18 13:02:18 +00001637 g.zDbFile = azSrcDb[iSrcDb];
drhbeaf5142016-12-26 00:15:56 +00001638 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db,
drhf2cf4122018-05-08 13:03:31 +00001639 openFlags4Data, pDfltVfs->zName);
drhd9972ef2015-05-26 17:57:56 +00001640 if( rc ){
1641 fatalError("cannot open source database %s - %s",
1642 azSrcDb[iSrcDb], sqlite3_errmsg(db));
1643 }
drh5180d682018-08-06 01:39:31 +00001644
1645 /* Print the description, if there is one */
1646 if( infoFlag ){
1647 int n;
1648 zDbName = azSrcDb[iSrcDb];
1649 i = (int)strlen(zDbName) - 1;
1650 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1651 zDbName += i;
1652 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1653 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1654 printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0));
1655 }else{
1656 printf("%s: (empty \"readme\")", zDbName);
1657 }
1658 sqlite3_finalize(pStmt);
1659 sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0);
1660 if( pStmt
1661 && sqlite3_step(pStmt)==SQLITE_ROW
1662 && (n = sqlite3_column_int(pStmt,0))>0
1663 ){
1664 printf(" - %d DBs", n);
1665 }
1666 sqlite3_finalize(pStmt);
1667 sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0);
1668 if( pStmt
1669 && sqlite3_step(pStmt)==SQLITE_ROW
1670 && (n = sqlite3_column_int(pStmt,0))>0
1671 ){
1672 printf(" - %d scripts", n);
1673 }
1674 sqlite3_finalize(pStmt);
1675 printf("\n");
1676 sqlite3_close(db);
1677 continue;
1678 }
1679
drh9a645862015-06-24 12:44:42 +00001680 rc = sqlite3_exec(db,
drhd9972ef2015-05-26 17:57:56 +00001681 "CREATE TABLE IF NOT EXISTS db(\n"
1682 " dbid INTEGER PRIMARY KEY, -- database id\n"
1683 " dbcontent BLOB -- database disk file image\n"
1684 ");\n"
1685 "CREATE TABLE IF NOT EXISTS xsql(\n"
1686 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
1687 " sqltext TEXT -- Text of SQL statements to run\n"
1688 ");"
1689 "CREATE TABLE IF NOT EXISTS readme(\n"
1690 " msg TEXT -- Human-readable description of this file\n"
1691 ");", 0, 0, 0);
1692 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db));
1693 if( zMsg ){
1694 char *zSql;
1695 zSql = sqlite3_mprintf(
1696 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg);
1697 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1698 sqlite3_free(zSql);
1699 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db));
1700 }
drh362b66f2016-11-14 18:27:41 +00001701 ossFuzzThisDb = ossFuzz;
1702
1703 /* If the CONFIG(name,value) table exists, read db-specific settings
1704 ** from that table */
1705 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){
drh5ecf9032018-05-08 12:49:53 +00001706 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config",
1707 -1, &pStmt, 0);
drh362b66f2016-11-14 18:27:41 +00001708 if( rc ) fatalError("cannot prepare query of CONFIG table: %s",
1709 sqlite3_errmsg(db));
1710 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1711 const char *zName = (const char *)sqlite3_column_text(pStmt,0);
1712 if( zName==0 ) continue;
1713 if( strcmp(zName, "oss-fuzz")==0 ){
1714 ossFuzzThisDb = sqlite3_column_int(pStmt,1);
1715 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb);
1716 }
drh31999c52019-11-14 17:46:32 +00001717 if( strcmp(zName, "limit-mem")==0 ){
drh362b66f2016-11-14 18:27:41 +00001718 nMemThisDb = sqlite3_column_int(pStmt,1);
1719 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb);
drh362b66f2016-11-14 18:27:41 +00001720 }
1721 }
1722 sqlite3_finalize(pStmt);
1723 }
1724
drhd9972ef2015-05-26 17:57:56 +00001725 if( zInsSql ){
1726 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1727 readfileFunc, 0, 0);
drha8781d92020-02-25 20:05:58 +00001728 sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0,
1729 readtextfileFunc, 0, 0);
drhe5da9352019-01-27 01:11:40 +00001730 sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0,
1731 isDbSqlFunc, 0, 0);
drhd9972ef2015-05-26 17:57:56 +00001732 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
1733 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1734 zInsSql, sqlite3_errmsg(db));
1735 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
1736 if( rc ) fatalError("cannot start a transaction");
1737 for(i=iFirstInsArg; i<argc; i++){
1738 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC);
1739 sqlite3_step(pStmt);
1740 rc = sqlite3_reset(pStmt);
1741 if( rc ) fatalError("insert failed for %s", argv[i]);
drh3b74d032015-05-25 18:48:19 +00001742 }
drhd9972ef2015-05-26 17:57:56 +00001743 sqlite3_finalize(pStmt);
1744 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
drh5ecf9032018-05-08 12:49:53 +00001745 if( rc ) fatalError("cannot commit the transaction: %s",
1746 sqlite3_errmsg(db));
drhe5da9352019-01-27 01:11:40 +00001747 rebuild_database(db, dbSqlOnly);
drh3b74d032015-05-25 18:48:19 +00001748 sqlite3_close(db);
drhd9972ef2015-05-26 17:57:56 +00001749 return 0;
drh3b74d032015-05-25 18:48:19 +00001750 }
drh16f05822017-03-20 20:42:21 +00001751 rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0);
1752 if( rc ) fatalError("cannot set database to query-only");
drh40e0e0d2015-09-22 18:51:17 +00001753 if( zExpDb!=0 || zExpSql!=0 ){
1754 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1755 writefileFunc, 0, 0);
1756 if( zExpDb!=0 ){
1757 const char *zExDb =
1758 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
1759 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
1760 " FROM db WHERE ?2<0 OR dbid=?2;";
1761 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
1762 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1763 zExDb, sqlite3_errmsg(db));
1764 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
1765 SQLITE_STATIC, SQLITE_UTF8);
1766 sqlite3_bind_int(pStmt, 2, onlyDbid);
1767 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1768 printf("write db-%d (%d bytes) into %s\n",
1769 sqlite3_column_int(pStmt,1),
1770 sqlite3_column_int(pStmt,3),
1771 sqlite3_column_text(pStmt,2));
1772 }
1773 sqlite3_finalize(pStmt);
1774 }
1775 if( zExpSql!=0 ){
1776 const char *zExSql =
1777 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
1778 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
1779 " FROM xsql WHERE ?2<0 OR sqlid=?2;";
1780 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
1781 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1782 zExSql, sqlite3_errmsg(db));
1783 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
1784 SQLITE_STATIC, SQLITE_UTF8);
1785 sqlite3_bind_int(pStmt, 2, onlySqlid);
1786 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1787 printf("write sql-%d (%d bytes) into %s\n",
1788 sqlite3_column_int(pStmt,1),
1789 sqlite3_column_int(pStmt,3),
1790 sqlite3_column_text(pStmt,2));
1791 }
1792 sqlite3_finalize(pStmt);
1793 }
1794 sqlite3_close(db);
1795 return 0;
1796 }
drhd9972ef2015-05-26 17:57:56 +00001797
1798 /* Load all SQL script content and all initial database images from the
1799 ** source db
1800 */
1801 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid,
1802 &g.nSql, &g.pFirstSql);
1803 if( g.nSql==0 ) fatalError("need at least one SQL script");
1804 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid,
1805 &g.nDb, &g.pFirstDb);
1806 if( g.nDb==0 ){
1807 g.pFirstDb = safe_realloc(0, sizeof(Blob));
1808 memset(g.pFirstDb, 0, sizeof(Blob));
1809 g.pFirstDb->id = 1;
1810 g.pFirstDb->seq = 0;
1811 g.nDb = 1;
drhd83e2832015-06-24 14:45:44 +00001812 sqlFuzz = 1;
drhd9972ef2015-05-26 17:57:56 +00001813 }
1814
1815 /* Print the description, if there is one */
1816 if( !quietFlag ){
drhd9972ef2015-05-26 17:57:56 +00001817 zDbName = azSrcDb[iSrcDb];
drhe683b892016-02-15 18:47:26 +00001818 i = (int)strlen(zDbName) - 1;
drhd9972ef2015-05-26 17:57:56 +00001819 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1820 zDbName += i;
1821 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1822 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1823 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0));
1824 }
1825 sqlite3_finalize(pStmt);
1826 }
drh9a645862015-06-24 12:44:42 +00001827
1828 /* Rebuild the database, if requested */
1829 if( rebuildFlag ){
1830 if( !quietFlag ){
1831 printf("%s: rebuilding... ", zDbName);
1832 fflush(stdout);
1833 }
drhe5da9352019-01-27 01:11:40 +00001834 rebuild_database(db, 0);
drh9a645862015-06-24 12:44:42 +00001835 if( !quietFlag ) printf("done\n");
1836 }
drhd9972ef2015-05-26 17:57:56 +00001837
1838 /* Close the source database. Verify that no SQLite memory allocations are
1839 ** outstanding.
1840 */
1841 sqlite3_close(db);
1842 if( sqlite3_memory_used()>0 ){
1843 fatalError("SQLite has memory in use before the start of testing");
1844 }
drh53e66c32015-07-24 15:49:23 +00001845
1846 /* Limit available memory, if requested */
drh174f8552017-03-20 22:58:27 +00001847 sqlite3_shutdown();
drh39b3bcf2020-03-02 16:31:21 +00001848
drh31999c52019-11-14 17:46:32 +00001849 if( nMemThisDb>0 && nMem==0 ){
1850 if( !nativeMalloc ){
1851 pHeap = realloc(pHeap, nMemThisDb);
1852 if( pHeap==0 ){
1853 fatalError("failed to allocate %d bytes of heap memory", nMem);
1854 }
1855 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128);
1856 }else{
1857 sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb);
drh53e66c32015-07-24 15:49:23 +00001858 }
drh31999c52019-11-14 17:46:32 +00001859 }else{
1860 sqlite3_hard_heap_limit64(0);
drh53e66c32015-07-24 15:49:23 +00001861 }
drh174f8552017-03-20 22:58:27 +00001862
1863 /* Disable lookaside with the --native-malloc option */
1864 if( nativeMalloc ){
1865 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
1866 }
drhd9972ef2015-05-26 17:57:56 +00001867
drhbeaf5142016-12-26 00:15:56 +00001868 /* Reset the in-memory virtual filesystem */
drhd9972ef2015-05-26 17:57:56 +00001869 formatVfs();
drhd9972ef2015-05-26 17:57:56 +00001870
1871 /* Run a test using each SQL script against each database.
1872 */
1873 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName);
1874 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){
drha47e7092019-01-25 04:00:14 +00001875 if( isDbSql(pSql->a, pSql->sz) ){
1876 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id);
1877 if( verboseFlag ){
1878 printf("%s\n", g.zTestName);
1879 fflush(stdout);
1880 }else if( !quietFlag ){
1881 static int prevAmt = -1;
1882 int idx = pSql->seq;
1883 int amt = idx*10/(g.nSql);
1884 if( amt!=prevAmt ){
1885 printf(" %d%%", amt*10);
1886 fflush(stdout);
1887 prevAmt = amt;
1888 }
1889 }
1890 runCombinedDbSqlInput(pSql->a, pSql->sz);
1891 nTest++;
1892 g.zTestName[0] = 0;
drh39b3bcf2020-03-02 16:31:21 +00001893 disableOom();
drha47e7092019-01-25 04:00:14 +00001894 continue;
1895 }
drhd9972ef2015-05-26 17:57:56 +00001896 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){
1897 int openFlags;
1898 const char *zVfs = "inmem";
1899 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d",
1900 pSql->id, pDb->id);
1901 if( verboseFlag ){
1902 printf("%s\n", g.zTestName);
1903 fflush(stdout);
1904 }else if( !quietFlag ){
1905 static int prevAmt = -1;
1906 int idx = pSql->seq*g.nDb + pDb->id - 1;
1907 int amt = idx*10/(g.nDb*g.nSql);
1908 if( amt!=prevAmt ){
1909 printf(" %d%%", amt*10);
1910 fflush(stdout);
1911 prevAmt = amt;
1912 }
1913 }
1914 createVFile("main.db", pDb->sz, pDb->a);
drhbeaf5142016-12-26 00:15:56 +00001915 sqlite3_randomness(0,0);
drh362b66f2016-11-14 18:27:41 +00001916 if( ossFuzzThisDb ){
drhea432ba2016-11-11 16:33:47 +00001917#ifndef SQLITE_OSS_FUZZ
drh5ecf9032018-05-08 12:49:53 +00001918 fatalError("--oss-fuzz not supported: recompile"
1919 " with -DSQLITE_OSS_FUZZ");
drhea432ba2016-11-11 16:33:47 +00001920#else
1921 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t);
1922 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz);
drh78057352015-06-24 23:17:35 +00001923#endif
drhea432ba2016-11-11 16:33:47 +00001924 }else{
1925 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
1926 if( nativeFlag && pDb->sz==0 ){
1927 openFlags |= SQLITE_OPEN_MEMORY;
1928 zVfs = 0;
1929 }
1930 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs);
1931 if( rc ) fatalError("cannot open inmem database");
drhdfcfff62016-12-26 12:25:19 +00001932 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000);
1933 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50);
drhea432ba2016-11-11 16:33:47 +00001934 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags);
1935 setAlarm(iTimeout);
1936#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1937 if( sqlFuzz || vdbeLimitFlag ){
drh5ecf9032018-05-08 12:49:53 +00001938 sqlite3_progress_handler(db, 100000, progressHandler,
1939 &vdbeLimitFlag);
drhea432ba2016-11-11 16:33:47 +00001940 }
1941#endif
drhe6e96b12019-08-02 21:03:24 +00001942#ifdef SQLITE_TESTCTRL_PRNG_SEED
drh2e6d83b2019-08-03 01:39:20 +00001943 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
drhe6e96b12019-08-02 21:03:24 +00001944#endif
drh6e1c45e2019-12-18 13:42:04 +00001945 if( bVdbeDebug ){
1946 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
1947 }
drhea432ba2016-11-11 16:33:47 +00001948 do{
1949 runSql(db, (char*)pSql->a, runFlags);
1950 }while( timeoutTest );
1951 setAlarm(0);
drh174f8552017-03-20 22:58:27 +00001952 sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
drhea432ba2016-11-11 16:33:47 +00001953 sqlite3_close(db);
1954 }
drh174f8552017-03-20 22:58:27 +00001955 if( sqlite3_memory_used()>0 ){
1956 fatalError("memory leak: %lld bytes outstanding",
1957 sqlite3_memory_used());
1958 }
drhd9972ef2015-05-26 17:57:56 +00001959 reformatVfs();
1960 nTest++;
1961 g.zTestName[0] = 0;
drh4d6fda72015-05-26 18:58:32 +00001962
1963 /* Simulate an error if the TEST_FAILURE environment variable is "5".
1964 ** This is used to verify that automated test script really do spot
1965 ** errors that occur in this test program.
1966 */
1967 if( zFailCode ){
1968 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
1969 fatalError("simulated failure");
1970 }else if( zFailCode[0]!=0 ){
1971 /* If TEST_FAILURE is something other than 5, just exit the test
1972 ** early */
1973 printf("\nExit early due to TEST_FAILURE being set\n");
1974 iSrcDb = nSrcDb-1;
1975 goto sourcedb_cleanup;
1976 }
1977 }
drhd9972ef2015-05-26 17:57:56 +00001978 }
1979 }
1980 if( !quietFlag && !verboseFlag ){
1981 printf(" 100%% - %d tests\n", g.nDb*g.nSql);
1982 }
1983
1984 /* Clean up at the end of processing a single source database
1985 */
drh4d6fda72015-05-26 18:58:32 +00001986 sourcedb_cleanup:
drhd9972ef2015-05-26 17:57:56 +00001987 blobListFree(g.pFirstSql);
1988 blobListFree(g.pFirstDb);
1989 reformatVfs();
1990
1991 } /* End loop over all source databases */
drh3b74d032015-05-25 18:48:19 +00001992
1993 if( !quietFlag ){
1994 sqlite3_int64 iElapse = timeOfDay() - iBegin;
drhd9972ef2015-05-26 17:57:56 +00001995 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
1996 "SQLite %s %s\n",
1997 nTest, (int)(iElapse/1000), (int)(iElapse%1000),
drh3b74d032015-05-25 18:48:19 +00001998 sqlite3_libversion(), sqlite3_sourceid());
1999 }
drhf74d35b2015-05-27 18:19:50 +00002000 free(azSrcDb);
drh6653fbe2015-11-13 20:52:49 +00002001 free(pHeap);
drh3b74d032015-05-25 18:48:19 +00002002 return 0;
2003}