blob: 3785024368702e0f14eec96a0407579264ef60be [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
drhea432ba2016-11-11 16:33:47 +000097#ifdef SQLITE_OSS_FUZZ
98# include <stddef.h>
mistachkinac8ba262018-03-07 14:42:17 +000099# if !defined(_MSC_VER)
100# include <stdint.h>
101# endif
102#endif
103
104#if defined(_MSC_VER)
105typedef unsigned char uint8_t;
drhea432ba2016-11-11 16:33:47 +0000106#endif
107
drh3b74d032015-05-25 18:48:19 +0000108/*
109** Files in the virtual file system.
110*/
111typedef struct VFile VFile;
112struct VFile {
113 char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */
114 int sz; /* Size of the file in bytes */
115 int nRef; /* Number of references to this file */
116 unsigned char *a; /* Content of the file. From malloc() */
117};
118typedef struct VHandle VHandle;
119struct VHandle {
120 sqlite3_file base; /* Base class. Must be first */
121 VFile *pVFile; /* The underlying file */
122};
123
124/*
125** The value of a database file template, or of an SQL script
126*/
127typedef struct Blob Blob;
128struct Blob {
129 Blob *pNext; /* Next in a list */
130 int id; /* Id of this Blob */
drhe5c5f2c2015-05-26 00:28:08 +0000131 int seq; /* Sequence number */
drh3b74d032015-05-25 18:48:19 +0000132 int sz; /* Size of this Blob in bytes */
133 unsigned char a[1]; /* Blob content. Extra space allocated as needed. */
134};
135
136/*
137** Maximum number of files in the in-memory virtual filesystem.
138*/
139#define MX_FILE 10
140
141/*
142** Maximum allowed file size
143*/
144#define MX_FILE_SZ 10000000
145
146/*
147** All global variables are gathered into the "g" singleton.
148*/
149static struct GlobalVars {
150 const char *zArgv0; /* Name of program */
drha7648f02019-12-18 13:02:18 +0000151 const char *zDbFile; /* Name of database file */
drh3b74d032015-05-25 18:48:19 +0000152 VFile aFile[MX_FILE]; /* The virtual filesystem */
153 int nDb; /* Number of template databases */
154 Blob *pFirstDb; /* Content of first template database */
155 int nSql; /* Number of SQL scripts */
156 Blob *pFirstSql; /* First SQL script */
drhbeaf5142016-12-26 00:15:56 +0000157 unsigned int uRandom; /* Seed for the SQLite PRNG */
drh3b74d032015-05-25 18:48:19 +0000158 char zTestName[100]; /* Name of current test */
159} g;
160
161/*
162** Print an error message and quit.
163*/
164static void fatalError(const char *zFormat, ...){
165 va_list ap;
drha7648f02019-12-18 13:02:18 +0000166 fprintf(stderr, "%s", g.zArgv0);
167 if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile);
168 if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName);
169 fprintf(stderr, ": ");
drh3b74d032015-05-25 18:48:19 +0000170 va_start(ap, zFormat);
171 vfprintf(stderr, zFormat, ap);
172 va_end(ap);
173 fprintf(stderr, "\n");
174 exit(1);
175}
176
177/*
drha7648f02019-12-18 13:02:18 +0000178** signal handler
drh94701b02015-06-24 13:25:34 +0000179*/
180#ifdef __unix__
drha7648f02019-12-18 13:02:18 +0000181static void signalHandler(int signum){
182 const char *zSig;
183 if( signum==SIGABRT ){
184 zSig = "abort";
185 }else if( signum==SIGALRM ){
186 zSig = "timeout";
187 }else if( signum==SIGSEGV ){
188 zSig = "segfault";
189 }else{
190 zSig = "signal";
191 }
192 fatalError(zSig);
drh94701b02015-06-24 13:25:34 +0000193}
194#endif
195
196/*
197** Set the an alarm to go off after N seconds. Disable the alarm
198** if N==0
199*/
200static void setAlarm(int N){
201#ifdef __unix__
202 alarm(N);
203#else
204 (void)N;
205#endif
206}
207
drh78057352015-06-24 23:17:35 +0000208#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh94701b02015-06-24 13:25:34 +0000209/*
drhd83e2832015-06-24 14:45:44 +0000210** This an SQL progress handler. After an SQL statement has run for
211** many steps, we want to interrupt it. This guards against infinite
212** loops from recursive common table expressions.
213**
214** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
215** In that case, hitting the progress handler is a fatal error.
216*/
217static int progressHandler(void *pVdbeLimitFlag){
218 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles");
219 return 1;
220}
drh78057352015-06-24 23:17:35 +0000221#endif
drhd83e2832015-06-24 14:45:44 +0000222
223/*
drh3b74d032015-05-25 18:48:19 +0000224** Reallocate memory. Show and error and quit if unable.
225*/
226static void *safe_realloc(void *pOld, int szNew){
drhc5412d52016-03-23 17:54:19 +0000227 void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew);
drh3b74d032015-05-25 18:48:19 +0000228 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew);
229 return pNew;
230}
231
232/*
233** Initialize the virtual file system.
234*/
235static void formatVfs(void){
236 int i;
237 for(i=0; i<MX_FILE; i++){
238 g.aFile[i].sz = -1;
239 g.aFile[i].zFilename = 0;
240 g.aFile[i].a = 0;
241 g.aFile[i].nRef = 0;
242 }
243}
244
245
246/*
247** Erase all information in the virtual file system.
248*/
249static void reformatVfs(void){
250 int i;
251 for(i=0; i<MX_FILE; i++){
252 if( g.aFile[i].sz<0 ) continue;
253 if( g.aFile[i].zFilename ){
254 free(g.aFile[i].zFilename);
255 g.aFile[i].zFilename = 0;
256 }
257 if( g.aFile[i].nRef>0 ){
258 fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef);
259 }
260 g.aFile[i].sz = -1;
261 free(g.aFile[i].a);
262 g.aFile[i].a = 0;
263 g.aFile[i].nRef = 0;
264 }
265}
266
267/*
268** Find a VFile by name
269*/
270static VFile *findVFile(const char *zName){
271 int i;
drha9542b12015-05-25 19:35:42 +0000272 if( zName==0 ) return 0;
drh3b74d032015-05-25 18:48:19 +0000273 for(i=0; i<MX_FILE; i++){
274 if( g.aFile[i].zFilename==0 ) continue;
275 if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i];
276 }
277 return 0;
278}
279
280/*
281** Find a VFile by name. Create it if it does not already exist and
282** initialize it to the size and content given.
283**
284** Return NULL only if the filesystem is full.
285*/
286static VFile *createVFile(const char *zName, int sz, unsigned char *pData){
287 VFile *pNew = findVFile(zName);
288 int i;
289 if( pNew ) return pNew;
290 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){}
291 if( i>=MX_FILE ) return 0;
292 pNew = &g.aFile[i];
drha9542b12015-05-25 19:35:42 +0000293 if( zName ){
drhe683b892016-02-15 18:47:26 +0000294 int nName = (int)strlen(zName)+1;
295 pNew->zFilename = safe_realloc(0, nName);
296 memcpy(pNew->zFilename, zName, nName);
drha9542b12015-05-25 19:35:42 +0000297 }else{
298 pNew->zFilename = 0;
299 }
drh3b74d032015-05-25 18:48:19 +0000300 pNew->nRef = 0;
301 pNew->sz = sz;
302 pNew->a = safe_realloc(0, sz);
303 if( sz>0 ) memcpy(pNew->a, pData, sz);
304 return pNew;
305}
306
307
308/*
309** Implementation of the "readfile(X)" SQL function. The entire content
310** of the file named X is read and returned as a BLOB. NULL is returned
311** if the file does not exist or is unreadable.
312*/
313static void readfileFunc(
314 sqlite3_context *context,
315 int argc,
316 sqlite3_value **argv
317){
318 const char *zName;
319 FILE *in;
320 long nIn;
321 void *pBuf;
322
323 zName = (const char*)sqlite3_value_text(argv[0]);
324 if( zName==0 ) return;
325 in = fopen(zName, "rb");
326 if( in==0 ) return;
327 fseek(in, 0, SEEK_END);
328 nIn = ftell(in);
329 rewind(in);
330 pBuf = sqlite3_malloc64( nIn );
331 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
332 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
333 }else{
334 sqlite3_free(pBuf);
335 }
336 fclose(in);
337}
338
339/*
drha8781d92020-02-25 20:05:58 +0000340** Implementation of the "readtextfile(X)" SQL function. The text content
341** of the file named X through the end of the file or to the first \000
342** character, whichever comes first, is read and returned as TEXT. NULL
343** is returned if the file does not exist or is unreadable.
344*/
345static void readtextfileFunc(
346 sqlite3_context *context,
347 int argc,
348 sqlite3_value **argv
349){
350 const char *zName;
351 FILE *in;
352 long nIn;
353 char *pBuf;
354
355 zName = (const char*)sqlite3_value_text(argv[0]);
356 if( zName==0 ) return;
357 in = fopen(zName, "rb");
358 if( in==0 ) return;
359 fseek(in, 0, SEEK_END);
360 nIn = ftell(in);
361 rewind(in);
362 pBuf = sqlite3_malloc64( nIn+1 );
363 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
364 pBuf[nIn] = 0;
365 sqlite3_result_text(context, pBuf, -1, sqlite3_free);
366 }else{
367 sqlite3_free(pBuf);
368 }
369 fclose(in);
370}
371
372/*
drh40e0e0d2015-09-22 18:51:17 +0000373** Implementation of the "writefile(X,Y)" SQL function. The argument Y
374** is written into file X. The number of bytes written is returned. Or
375** NULL is returned if something goes wrong, such as being unable to open
376** file X for writing.
377*/
378static void writefileFunc(
379 sqlite3_context *context,
380 int argc,
381 sqlite3_value **argv
382){
383 FILE *out;
384 const char *z;
385 sqlite3_int64 rc;
386 const char *zFile;
387
388 (void)argc;
389 zFile = (const char*)sqlite3_value_text(argv[0]);
390 if( zFile==0 ) return;
391 out = fopen(zFile, "wb");
392 if( out==0 ) return;
393 z = (const char*)sqlite3_value_blob(argv[1]);
394 if( z==0 ){
395 rc = 0;
396 }else{
397 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
398 }
399 fclose(out);
400 sqlite3_result_int64(context, rc);
401}
402
403
404/*
drh3b74d032015-05-25 18:48:19 +0000405** Load a list of Blob objects from the database
406*/
407static void blobListLoadFromDb(
408 sqlite3 *db, /* Read from this database */
409 const char *zSql, /* Query used to extract the blobs */
drha9542b12015-05-25 19:35:42 +0000410 int onlyId, /* Only load where id is this value */
drh3b74d032015-05-25 18:48:19 +0000411 int *pN, /* OUT: Write number of blobs loaded here */
412 Blob **ppList /* OUT: Write the head of the blob list here */
413){
414 Blob head;
415 Blob *p;
416 sqlite3_stmt *pStmt;
417 int n = 0;
418 int rc;
drha9542b12015-05-25 19:35:42 +0000419 char *z2;
drh3b74d032015-05-25 18:48:19 +0000420
drha9542b12015-05-25 19:35:42 +0000421 if( onlyId>0 ){
422 z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId);
423 }else{
424 z2 = sqlite3_mprintf("%s", zSql);
425 }
426 rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0);
427 sqlite3_free(z2);
drh3b74d032015-05-25 18:48:19 +0000428 if( rc ) fatalError("%s", sqlite3_errmsg(db));
429 head.pNext = 0;
430 p = &head;
431 while( SQLITE_ROW==sqlite3_step(pStmt) ){
432 int sz = sqlite3_column_bytes(pStmt, 1);
433 Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz );
434 pNew->id = sqlite3_column_int(pStmt, 0);
435 pNew->sz = sz;
drhe5c5f2c2015-05-26 00:28:08 +0000436 pNew->seq = n++;
drh3b74d032015-05-25 18:48:19 +0000437 pNew->pNext = 0;
438 memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz);
439 pNew->a[sz] = 0;
440 p->pNext = pNew;
441 p = pNew;
drh3b74d032015-05-25 18:48:19 +0000442 }
443 sqlite3_finalize(pStmt);
444 *pN = n;
445 *ppList = head.pNext;
446}
447
448/*
449** Free a list of Blob objects
450*/
451static void blobListFree(Blob *p){
452 Blob *pNext;
453 while( p ){
454 pNext = p->pNext;
455 free(p);
456 p = pNext;
457 }
458}
459
drh3b74d032015-05-25 18:48:19 +0000460/* Return the current wall-clock time */
461static sqlite3_int64 timeOfDay(void){
462 static sqlite3_vfs *clockVfs = 0;
463 sqlite3_int64 t;
drh8055a3e2018-11-21 14:27:34 +0000464 if( clockVfs==0 ){
465 clockVfs = sqlite3_vfs_find(0);
466 if( clockVfs==0 ) return 0;
467 }
drh3b74d032015-05-25 18:48:19 +0000468 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
469 clockVfs->xCurrentTimeInt64(clockVfs, &t);
470 }else{
471 double r;
472 clockVfs->xCurrentTime(clockVfs, &r);
473 t = (sqlite3_int64)(r*86400000.0);
474 }
475 return t;
476}
477
drha47e7092019-01-25 04:00:14 +0000478/***************************************************************************
479** Code to process combined database+SQL scripts generated by the
480** dbsqlfuzz fuzzer.
481*/
482
483/* An instance of the following object is passed by pointer as the
484** client data to various callbacks.
485*/
486typedef struct FuzzCtx {
487 sqlite3 *db; /* The database connection */
488 sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
489 sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
490 sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
491 unsigned nCb; /* Number of progress callbacks */
492 unsigned mxCb; /* Maximum number of progress callbacks allowed */
493 unsigned execCnt; /* Number of calls to the sqlite3_exec callback */
494 int timeoutHit; /* True when reaching a timeout */
495} FuzzCtx;
496
497/* Verbosity level for the dbsqlfuzz test runner */
498static int eVerbosity = 0;
499
500/* True to activate PRAGMA vdbe_debug=on */
501static int bVdbeDebug = 0;
502
503/* Timeout for each fuzzing attempt, in milliseconds */
drhed457032019-01-25 17:51:06 +0000504static int giTimeout = 10000; /* Defaults to 10 seconds */
drha47e7092019-01-25 04:00:14 +0000505
506/* Maximum number of progress handler callbacks */
507static unsigned int mxProgressCb = 2000;
508
509/* Maximum string length in SQLite */
510static int lengthLimit = 1000000;
511
drhbe03cc92020-01-20 14:42:09 +0000512/* Maximum expression depth */
513static int depthLimit = 500;
514
drh31999c52019-11-14 17:46:32 +0000515/* Limit on the amount of heap memory that can be used */
drha8781d92020-02-25 20:05:58 +0000516static sqlite3_int64 heapLimit = 100000000;
drh31999c52019-11-14 17:46:32 +0000517
drha47e7092019-01-25 04:00:14 +0000518/* Maximum byte-code program length in SQLite */
519static int vdbeOpLimit = 25000;
520
521/* Maximum size of the in-memory database */
522static sqlite3_int64 maxDbSize = 104857600;
drh39b3bcf2020-03-02 16:31:21 +0000523/* OOM simulation parameters */
524static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
525static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
526static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
527
528/* This routine is called when a simulated OOM occurs. It is broken
529** out as a separate routine to make it easy to set a breakpoint on
530** the OOM
531*/
532void oomFault(void){
533 if( eVerbosity ){
534 printf("Simulated OOM fault\n");
535 }
536 if( oomRepeat>0 ){
537 oomRepeat--;
538 }else{
539 oomCounter--;
540 }
541}
542
543/* This routine is a replacement malloc() that is used to simulate
544** Out-Of-Memory (OOM) errors for testing purposes.
545*/
546static void *oomMalloc(int nByte){
547 if( oomCounter ){
548 if( oomCounter==1 ){
549 oomFault();
550 return 0;
551 }else{
552 oomCounter--;
553 }
554 }
555 return defaultMalloc(nByte);
556}
557
558/* Register the OOM simulator. This must occur before any memory
559** allocations */
560static void registerOomSimulator(void){
561 sqlite3_mem_methods mem;
562 sqlite3_shutdown();
563 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
564 defaultMalloc = mem.xMalloc;
565 mem.xMalloc = oomMalloc;
566 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
567}
568
569/* Turn off any pending OOM simulation */
570static void disableOom(void){
571 oomCounter = 0;
572 oomRepeat = 0;
573}
drha47e7092019-01-25 04:00:14 +0000574
575/*
576** Translate a single byte of Hex into an integer.
577** This routine only works if h really is a valid hexadecimal
578** character: 0..9a..fA..F
579*/
drhed457032019-01-25 17:51:06 +0000580static unsigned char hexToInt(unsigned int h){
drha47e7092019-01-25 04:00:14 +0000581#ifdef SQLITE_EBCDIC
582 h += 9*(1&~(h>>4)); /* EBCDIC */
583#else
584 h += 9*(1&(h>>6)); /* ASCII */
585#endif
586 return h & 0xf;
587}
588
589/*
590** The first character of buffer zIn[0..nIn-1] is a '['. This routine
591** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it
592** does it makes corresponding changes to the *pK value and *pI value
593** and returns true. If the input buffer does not match the patterns,
594** no changes are made to either *pK or *pI and this routine returns false.
595*/
596static int isOffset(
597 const unsigned char *zIn, /* Text input */
598 int nIn, /* Bytes of input */
599 unsigned int *pK, /* half-byte cursor to adjust */
600 unsigned int *pI /* Input index to adjust */
601){
602 int i;
603 unsigned int k = 0;
604 unsigned char c;
605 for(i=1; i<nIn && (c = zIn[i])!=']'; i++){
606 if( !isxdigit(c) ) return 0;
607 k = k*16 + hexToInt(c);
608 }
609 if( i==nIn ) return 0;
610 *pK = 2*k;
611 *pI += i;
612 return 1;
613}
614
615/*
616** Decode the text starting at zIn into a binary database file.
617** The maximum length of zIn is nIn bytes. Compute the binary database
618** file contain in space obtained from sqlite3_malloc().
619**
620** Return the number of bytes of zIn consumed. Or return -1 if there
621** is an error. One potential error is that the recipe specifies a
622** database file larger than MX_FILE_SZ bytes.
623**
624** Abort on an OOM.
625*/
626static int decodeDatabase(
627 const unsigned char *zIn, /* Input text to be decoded */
628 int nIn, /* Bytes of input text */
629 unsigned char **paDecode, /* OUT: decoded database file */
630 int *pnDecode /* OUT: Size of decoded database */
631){
632 unsigned char *a; /* Database under construction */
633 int mx = 0; /* Current size of the database */
634 sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */
635 unsigned int i; /* Next byte of zIn[] to read */
636 unsigned int j; /* Temporary integer */
637 unsigned int k; /* half-byte cursor index for output */
638 unsigned int n; /* Number of bytes of input */
639 unsigned char b = 0;
640 if( nIn<4 ) return -1;
641 n = (unsigned int)nIn;
drhed457032019-01-25 17:51:06 +0000642 a = sqlite3_malloc64( nAlloc );
drha47e7092019-01-25 04:00:14 +0000643 if( a==0 ){
644 fprintf(stderr, "Out of memory!\n");
645 exit(1);
646 }
mistachkin065f3bf2019-03-20 05:45:03 +0000647 memset(a, 0, (size_t)nAlloc);
drha47e7092019-01-25 04:00:14 +0000648 for(i=k=0; i<n; i++){
drhaf638922019-02-07 00:17:36 +0000649 unsigned char c = (unsigned char)zIn[i];
drha47e7092019-01-25 04:00:14 +0000650 if( isxdigit(c) ){
651 k++;
652 if( k & 1 ){
653 b = hexToInt(c)*16;
654 }else{
655 b += hexToInt(c);
656 j = k/2 - 1;
657 if( j>=nAlloc ){
658 sqlite3_uint64 newSize;
659 if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){
660 if( eVerbosity ){
661 fprintf(stderr, "Input database too big: max %d bytes\n",
662 MX_FILE_SZ);
663 }
664 sqlite3_free(a);
665 return -1;
666 }
667 newSize = nAlloc*2;
668 if( newSize<=j ){
669 newSize = (j+4096)&~4095;
670 }
671 if( newSize>MX_FILE_SZ ){
672 if( j>=MX_FILE_SZ ){
673 sqlite3_free(a);
674 return -1;
675 }
676 newSize = MX_FILE_SZ;
677 }
drhed457032019-01-25 17:51:06 +0000678 a = sqlite3_realloc64( a, newSize );
drha47e7092019-01-25 04:00:14 +0000679 if( a==0 ){
680 fprintf(stderr, "Out of memory!\n");
681 exit(1);
682 }
683 assert( newSize > nAlloc );
mistachkin065f3bf2019-03-20 05:45:03 +0000684 memset(a+nAlloc, 0, (size_t)(newSize - nAlloc));
drha47e7092019-01-25 04:00:14 +0000685 nAlloc = newSize;
686 }
687 if( j>=(unsigned)mx ){
688 mx = (j + 4095)&~4095;
689 if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ;
690 }
691 assert( j<nAlloc );
692 a[j] = b;
693 }
694 }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){
695 continue;
696 }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){
697 i += 4;
698 break;
699 }
700 }
701 *pnDecode = mx;
702 *paDecode = a;
703 return i;
704}
705
706/*
707** Progress handler callback.
708**
709** The argument is the cutoff-time after which all processing should
710** stop. So return non-zero if the cut-off time is exceeded.
711*/
712static int progress_handler(void *pClientData) {
713 FuzzCtx *p = (FuzzCtx*)pClientData;
714 sqlite3_int64 iNow = timeOfDay();
715 int rc = iNow>=p->iCutoffTime;
716 sqlite3_int64 iDiff = iNow - p->iLastCb;
717 if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
718 p->nCb++;
719 if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1;
drhdf216592019-01-25 04:43:26 +0000720 if( rc && !p->timeoutHit && eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000721 printf("Timeout on progress callback %d\n", p->nCb);
722 fflush(stdout);
723 p->timeoutHit = 1;
724 }
725 return rc;
726}
727
728/*
729** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
730** "PRAGMA parser_trace" since they can dramatically increase the
731** amount of output without actually testing anything useful.
732**
733** Also block ATTACH and DETACH
734*/
735static int block_troublesome_sql(
736 void *Notused,
737 int eCode,
738 const char *zArg1,
739 const char *zArg2,
740 const char *zArg3,
741 const char *zArg4
742){
743 (void)Notused;
744 (void)zArg2;
745 (void)zArg3;
746 (void)zArg4;
747 if( eCode==SQLITE_PRAGMA ){
748 if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0
749 || sqlite3_stricmp("parser_trace", zArg1)==0
750 || sqlite3_stricmp("temp_store_directory", zArg1)==0
751 ){
752 return SQLITE_DENY;
753 }
drh39b3bcf2020-03-02 16:31:21 +0000754 if( sqlite3_stricmp("oom",zArg1)==0 && zArg2!=0 && zArg2[0]!=0 ){
755 oomCounter = atoi(zArg2);
756 }
drha47e7092019-01-25 04:00:14 +0000757 }else if( (eCode==SQLITE_ATTACH || eCode==SQLITE_DETACH)
758 && zArg1 && zArg1[0] ){
759 return SQLITE_DENY;
760 }
761 return SQLITE_OK;
762}
763
764/*
765** Run the SQL text
766*/
767static int runDbSql(sqlite3 *db, const char *zSql){
768 int rc;
769 sqlite3_stmt *pStmt;
drhaf638922019-02-07 00:17:36 +0000770 while( isspace(zSql[0]&0x7f) ) zSql++;
drha47e7092019-01-25 04:00:14 +0000771 if( zSql[0]==0 ) return SQLITE_OK;
drhdf216592019-01-25 04:43:26 +0000772 if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000773 printf("RUNNING-SQL: [%s]\n", zSql);
774 fflush(stdout);
775 }
776 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
777 if( rc==SQLITE_OK ){
778 while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
drhdf216592019-01-25 04:43:26 +0000779 if( eVerbosity>=5 ){
drha47e7092019-01-25 04:00:14 +0000780 int j;
781 for(j=0; j<sqlite3_column_count(pStmt); j++){
782 if( j ) printf(",");
783 switch( sqlite3_column_type(pStmt, j) ){
784 case SQLITE_NULL: {
785 printf("NULL");
786 break;
787 }
788 case SQLITE_INTEGER:
789 case SQLITE_FLOAT: {
790 printf("%s", sqlite3_column_text(pStmt, j));
791 break;
792 }
793 case SQLITE_BLOB: {
794 int n = sqlite3_column_bytes(pStmt, j);
795 int i;
796 const unsigned char *a;
797 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
798 printf("x'");
799 for(i=0; i<n; i++){
800 printf("%02x", a[i]);
801 }
802 printf("'");
803 break;
804 }
805 case SQLITE_TEXT: {
806 int n = sqlite3_column_bytes(pStmt, j);
807 int i;
808 const unsigned char *a;
809 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
810 printf("'");
811 for(i=0; i<n; i++){
812 if( a[i]=='\'' ){
813 printf("''");
814 }else{
815 putchar(a[i]);
816 }
817 }
818 printf("'");
819 break;
820 }
821 } /* End switch() */
822 } /* End for() */
823 printf("\n");
824 fflush(stdout);
drhdf216592019-01-25 04:43:26 +0000825 } /* End if( eVerbosity>=5 ) */
drha47e7092019-01-25 04:00:14 +0000826 } /* End while( SQLITE_ROW */
drhdf216592019-01-25 04:43:26 +0000827 if( rc!=SQLITE_DONE && eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000828 printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db));
829 fflush(stdout);
830 }
drhdf216592019-01-25 04:43:26 +0000831 }else if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000832 printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db));
833 fflush(stdout);
834 } /* End if( SQLITE_OK ) */
835 return sqlite3_finalize(pStmt);
836}
837
838/* Invoke this routine to run a single test case */
839int runCombinedDbSqlInput(const uint8_t *aData, size_t nByte){
840 int rc; /* SQLite API return value */
841 int iSql; /* Index in aData[] of start of SQL */
842 unsigned char *aDb = 0; /* Decoded database content */
843 int nDb = 0; /* Size of the decoded database */
844 int i; /* Loop counter */
845 int j; /* Start of current SQL statement */
846 char *zSql = 0; /* SQL text to run */
847 int nSql; /* Bytes of SQL text */
848 FuzzCtx cx; /* Fuzzing context */
849
850 if( nByte<10 ) return 0;
851 if( sqlite3_initialize() ) return 0;
852 if( sqlite3_memory_used()!=0 ){
853 int nAlloc = 0;
854 int nNotUsed = 0;
855 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
856 fprintf(stderr,"Memory leak in mutator: %lld bytes in %d allocations\n",
857 sqlite3_memory_used(), nAlloc);
858 exit(1);
859 }
860 memset(&cx, 0, sizeof(cx));
861 iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb);
862 if( iSql<0 ) return 0;
drhed457032019-01-25 17:51:06 +0000863 nSql = (int)(nByte - iSql);
drhdf216592019-01-25 04:43:26 +0000864 if( eVerbosity>=3 ){
drha47e7092019-01-25 04:00:14 +0000865 printf(
866 "****** %d-byte input, %d-byte database, %d-byte script "
867 "******\n", (int)nByte, nDb, nSql);
868 fflush(stdout);
869 }
870 rc = sqlite3_open(0, &cx.db);
871 if( rc ) return 1;
872 if( bVdbeDebug ){
873 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
874 }
875
876 /* Invoke the progress handler frequently to check to see if we
877 ** are taking too long. The progress handler will return true
drhed457032019-01-25 17:51:06 +0000878 ** (which will block further processing) if more than giTimeout seconds have
drha47e7092019-01-25 04:00:14 +0000879 ** elapsed since the start of the test.
880 */
881 cx.iLastCb = timeOfDay();
drhed457032019-01-25 17:51:06 +0000882 cx.iCutoffTime = cx.iLastCb + giTimeout; /* Now + giTimeout seconds */
drha47e7092019-01-25 04:00:14 +0000883 cx.mxCb = mxProgressCb;
884#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
885 sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
886#endif
887
888 /* Set a limit on the maximum size of a prepared statement, and the
889 ** maximum length of a string or blob */
890 if( vdbeOpLimit>0 ){
891 sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit);
892 }
893 if( lengthLimit>0 ){
894 sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit);
895 }
drhbe03cc92020-01-20 14:42:09 +0000896 if( depthLimit>0 ){
897 sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit);
898 }
drh31999c52019-11-14 17:46:32 +0000899 sqlite3_hard_heap_limit64(heapLimit);
drha47e7092019-01-25 04:00:14 +0000900
901 if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){
902 aDb[18] = aDb[19] = 1;
903 }
904 rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb,
905 SQLITE_DESERIALIZE_RESIZEABLE |
906 SQLITE_DESERIALIZE_FREEONCLOSE);
907 if( rc ){
908 fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc);
909 goto testrun_finished;
910 }
911 if( maxDbSize>0 ){
912 sqlite3_int64 x = maxDbSize;
913 sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x);
914 }
915
drh725a9c72019-01-25 13:03:38 +0000916 /* For high debugging levels, turn on debug mode */
917 if( eVerbosity>=5 ){
918 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0);
919 }
920
drha47e7092019-01-25 04:00:14 +0000921 /* Block debug pragmas and ATTACH/DETACH. But wait until after
922 ** deserialize to do this because deserialize depends on ATTACH */
923 sqlite3_set_authorizer(cx.db, block_troublesome_sql, 0);
924
925 /* Consistent PRNG seed */
926 sqlite3_randomness(0,0);
927
928 zSql = sqlite3_malloc( nSql + 1 );
929 if( zSql==0 ){
930 fprintf(stderr, "Out of memory!\n");
931 }else{
932 memcpy(zSql, aData+iSql, nSql);
933 zSql[nSql] = 0;
934 for(i=j=0; zSql[i]; i++){
935 if( zSql[i]==';' ){
936 char cSaved = zSql[i+1];
937 zSql[i+1] = 0;
938 if( sqlite3_complete(zSql+j) ){
939 rc = runDbSql(cx.db, zSql+j);
940 j = i+1;
941 }
942 zSql[i+1] = cSaved;
943 if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){
944 goto testrun_finished;
945 }
946 }
947 }
948 if( j<i ){
949 runDbSql(cx.db, zSql+j);
950 }
951 }
952testrun_finished:
953 sqlite3_free(zSql);
954 rc = sqlite3_close(cx.db);
955 if( rc!=SQLITE_OK ){
956 fprintf(stdout, "sqlite3_close() returns %d\n", rc);
957 }
drhdf216592019-01-25 04:43:26 +0000958 if( eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000959 fprintf(stdout, "Peak memory usages: %f MB\n",
960 sqlite3_memory_highwater(1) / 1000000.0);
961 }
962 if( sqlite3_memory_used()!=0 ){
963 int nAlloc = 0;
964 int nNotUsed = 0;
965 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
966 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n",
967 sqlite3_memory_used(), nAlloc);
968 exit(1);
969 }
970 return 0;
971}
972
973/*
974** END of the dbsqlfuzz code
975***************************************************************************/
976
977/* Look at a SQL text and try to determine if it begins with a database
978** description, such as would be found in a dbsqlfuzz test case. Return
979** true if this does appear to be a dbsqlfuzz test case and false otherwise.
980*/
981static int isDbSql(unsigned char *a, int n){
drhdf216592019-01-25 04:43:26 +0000982 unsigned char buf[12];
983 int i;
drha47e7092019-01-25 04:00:14 +0000984 if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1;
985 while( n>0 && isspace(a[0]) ){ a++; n--; }
drhdf216592019-01-25 04:43:26 +0000986 for(i=0; n>0 && i<8; n--, a++){
987 if( isxdigit(a[0]) ) buf[i++] = a[0];
988 }
989 if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1;
drha47e7092019-01-25 04:00:14 +0000990 return 0;
991}
992
drhe5da9352019-01-27 01:11:40 +0000993/* Implementation of the isdbsql(TEXT) SQL function.
994*/
995static void isDbSqlFunc(
996 sqlite3_context *context,
997 int argc,
998 sqlite3_value **argv
999){
1000 int n = sqlite3_value_bytes(argv[0]);
1001 unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]);
1002 sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n));
1003}
drha47e7092019-01-25 04:00:14 +00001004
drh3b74d032015-05-25 18:48:19 +00001005/* Methods for the VHandle object
1006*/
1007static int inmemClose(sqlite3_file *pFile){
1008 VHandle *p = (VHandle*)pFile;
1009 VFile *pVFile = p->pVFile;
1010 pVFile->nRef--;
1011 if( pVFile->nRef==0 && pVFile->zFilename==0 ){
1012 pVFile->sz = -1;
1013 free(pVFile->a);
1014 pVFile->a = 0;
1015 }
1016 return SQLITE_OK;
1017}
1018static int inmemRead(
1019 sqlite3_file *pFile, /* Read from this open file */
1020 void *pData, /* Store content in this buffer */
1021 int iAmt, /* Bytes of content */
1022 sqlite3_int64 iOfst /* Start reading here */
1023){
1024 VHandle *pHandle = (VHandle*)pFile;
1025 VFile *pVFile = pHandle->pVFile;
1026 if( iOfst<0 || iOfst>=pVFile->sz ){
1027 memset(pData, 0, iAmt);
1028 return SQLITE_IOERR_SHORT_READ;
1029 }
1030 if( iOfst+iAmt>pVFile->sz ){
1031 memset(pData, 0, iAmt);
drh1573dc32015-05-25 22:29:26 +00001032 iAmt = (int)(pVFile->sz - iOfst);
drhe45985b2018-12-14 02:29:56 +00001033 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001034 return SQLITE_IOERR_SHORT_READ;
1035 }
drhaca7ea12015-05-25 23:14:37 +00001036 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001037 return SQLITE_OK;
1038}
1039static int inmemWrite(
1040 sqlite3_file *pFile, /* Write to this file */
1041 const void *pData, /* Content to write */
1042 int iAmt, /* bytes to write */
1043 sqlite3_int64 iOfst /* Start writing here */
1044){
1045 VHandle *pHandle = (VHandle*)pFile;
1046 VFile *pVFile = pHandle->pVFile;
1047 if( iOfst+iAmt > pVFile->sz ){
drha9542b12015-05-25 19:35:42 +00001048 if( iOfst+iAmt >= MX_FILE_SZ ){
1049 return SQLITE_FULL;
1050 }
drh1573dc32015-05-25 22:29:26 +00001051 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt));
drh908aced2015-05-26 16:12:45 +00001052 if( iOfst > pVFile->sz ){
1053 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz));
1054 }
drh1573dc32015-05-25 22:29:26 +00001055 pVFile->sz = (int)(iOfst + iAmt);
drh3b74d032015-05-25 18:48:19 +00001056 }
1057 memcpy(pVFile->a + iOfst, pData, iAmt);
1058 return SQLITE_OK;
1059}
1060static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){
1061 VHandle *pHandle = (VHandle*)pFile;
1062 VFile *pVFile = pHandle->pVFile;
drh1573dc32015-05-25 22:29:26 +00001063 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize;
drh3b74d032015-05-25 18:48:19 +00001064 return SQLITE_OK;
1065}
1066static int inmemSync(sqlite3_file *pFile, int flags){
1067 return SQLITE_OK;
1068}
1069static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
1070 *pSize = ((VHandle*)pFile)->pVFile->sz;
1071 return SQLITE_OK;
1072}
1073static int inmemLock(sqlite3_file *pFile, int type){
1074 return SQLITE_OK;
1075}
1076static int inmemUnlock(sqlite3_file *pFile, int type){
1077 return SQLITE_OK;
1078}
1079static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){
1080 *pOut = 0;
1081 return SQLITE_OK;
1082}
1083static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){
1084 return SQLITE_NOTFOUND;
1085}
1086static int inmemSectorSize(sqlite3_file *pFile){
1087 return 512;
1088}
1089static int inmemDeviceCharacteristics(sqlite3_file *pFile){
1090 return
1091 SQLITE_IOCAP_SAFE_APPEND |
1092 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
1093 SQLITE_IOCAP_POWERSAFE_OVERWRITE;
1094}
1095
1096
1097/* Method table for VHandle
1098*/
1099static sqlite3_io_methods VHandleMethods = {
1100 /* iVersion */ 1,
1101 /* xClose */ inmemClose,
1102 /* xRead */ inmemRead,
1103 /* xWrite */ inmemWrite,
1104 /* xTruncate */ inmemTruncate,
1105 /* xSync */ inmemSync,
1106 /* xFileSize */ inmemFileSize,
1107 /* xLock */ inmemLock,
1108 /* xUnlock */ inmemUnlock,
1109 /* xCheck... */ inmemCheckReservedLock,
1110 /* xFileCtrl */ inmemFileControl,
1111 /* xSectorSz */ inmemSectorSize,
1112 /* xDevchar */ inmemDeviceCharacteristics,
1113 /* xShmMap */ 0,
1114 /* xShmLock */ 0,
1115 /* xShmBarrier */ 0,
1116 /* xShmUnmap */ 0,
1117 /* xFetch */ 0,
1118 /* xUnfetch */ 0
1119};
1120
1121/*
1122** Open a new file in the inmem VFS. All files are anonymous and are
1123** delete-on-close.
1124*/
1125static int inmemOpen(
1126 sqlite3_vfs *pVfs,
1127 const char *zFilename,
1128 sqlite3_file *pFile,
1129 int openFlags,
1130 int *pOutFlags
1131){
1132 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)"");
1133 VHandle *pHandle = (VHandle*)pFile;
drha9542b12015-05-25 19:35:42 +00001134 if( pVFile==0 ){
1135 return SQLITE_FULL;
1136 }
drh3b74d032015-05-25 18:48:19 +00001137 pHandle->pVFile = pVFile;
1138 pVFile->nRef++;
1139 pFile->pMethods = &VHandleMethods;
1140 if( pOutFlags ) *pOutFlags = openFlags;
1141 return SQLITE_OK;
1142}
1143
1144/*
1145** Delete a file by name
1146*/
1147static int inmemDelete(
1148 sqlite3_vfs *pVfs,
1149 const char *zFilename,
1150 int syncdir
1151){
1152 VFile *pVFile = findVFile(zFilename);
1153 if( pVFile==0 ) return SQLITE_OK;
1154 if( pVFile->nRef==0 ){
1155 free(pVFile->zFilename);
1156 pVFile->zFilename = 0;
1157 pVFile->sz = -1;
1158 free(pVFile->a);
1159 pVFile->a = 0;
1160 return SQLITE_OK;
1161 }
1162 return SQLITE_IOERR_DELETE;
1163}
1164
1165/* Check for the existance of a file
1166*/
1167static int inmemAccess(
1168 sqlite3_vfs *pVfs,
1169 const char *zFilename,
1170 int flags,
1171 int *pResOut
1172){
1173 VFile *pVFile = findVFile(zFilename);
1174 *pResOut = pVFile!=0;
1175 return SQLITE_OK;
1176}
1177
1178/* Get the canonical pathname for a file
1179*/
1180static int inmemFullPathname(
1181 sqlite3_vfs *pVfs,
1182 const char *zFilename,
1183 int nOut,
1184 char *zOut
1185){
1186 sqlite3_snprintf(nOut, zOut, "%s", zFilename);
1187 return SQLITE_OK;
1188}
1189
drhbeaf5142016-12-26 00:15:56 +00001190/* Always use the same random see, for repeatability.
1191*/
1192static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
1193 memset(zBuf, 0, nBuf);
1194 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom));
1195 return nBuf;
1196}
1197
drh3b74d032015-05-25 18:48:19 +00001198/*
1199** Register the VFS that reads from the g.aFile[] set of files.
1200*/
drhbeaf5142016-12-26 00:15:56 +00001201static void inmemVfsRegister(int makeDefault){
drh3b74d032015-05-25 18:48:19 +00001202 static sqlite3_vfs inmemVfs;
1203 sqlite3_vfs *pDefault = sqlite3_vfs_find(0);
drh5337dac2015-11-25 15:15:03 +00001204 inmemVfs.iVersion = 3;
drh3b74d032015-05-25 18:48:19 +00001205 inmemVfs.szOsFile = sizeof(VHandle);
1206 inmemVfs.mxPathname = 200;
1207 inmemVfs.zName = "inmem";
1208 inmemVfs.xOpen = inmemOpen;
1209 inmemVfs.xDelete = inmemDelete;
1210 inmemVfs.xAccess = inmemAccess;
1211 inmemVfs.xFullPathname = inmemFullPathname;
drhbeaf5142016-12-26 00:15:56 +00001212 inmemVfs.xRandomness = inmemRandomness;
drh3b74d032015-05-25 18:48:19 +00001213 inmemVfs.xSleep = pDefault->xSleep;
drh5337dac2015-11-25 15:15:03 +00001214 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64;
drhbeaf5142016-12-26 00:15:56 +00001215 sqlite3_vfs_register(&inmemVfs, makeDefault);
drh3b74d032015-05-25 18:48:19 +00001216};
1217
drh3b74d032015-05-25 18:48:19 +00001218/*
drhe5c5f2c2015-05-26 00:28:08 +00001219** Allowed values for the runFlags parameter to runSql()
1220*/
1221#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
1222#define SQL_OUTPUT 0x0002 /* Show the SQL output */
1223
1224/*
drh3b74d032015-05-25 18:48:19 +00001225** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
1226** stop if an error is encountered.
1227*/
drhe5c5f2c2015-05-26 00:28:08 +00001228static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
drh3b74d032015-05-25 18:48:19 +00001229 const char *zMore;
1230 sqlite3_stmt *pStmt;
1231
1232 while( zSql && zSql[0] ){
1233 zMore = 0;
1234 pStmt = 0;
1235 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore);
drh4ab31472015-05-25 22:17:06 +00001236 if( zMore==zSql ) break;
drhe5c5f2c2015-05-26 00:28:08 +00001237 if( runFlags & SQL_TRACE ){
drh4ab31472015-05-25 22:17:06 +00001238 const char *z = zSql;
1239 int n;
drhc56fac72015-10-29 13:48:15 +00001240 while( z<zMore && ISSPACE(z[0]) ) z++;
drh4ab31472015-05-25 22:17:06 +00001241 n = (int)(zMore - z);
drhc56fac72015-10-29 13:48:15 +00001242 while( n>0 && ISSPACE(z[n-1]) ) n--;
drh4ab31472015-05-25 22:17:06 +00001243 if( n==0 ) break;
1244 if( pStmt==0 ){
1245 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
1246 }else{
1247 printf("TRACE: %.*s\n", n, z);
1248 }
1249 }
drh3b74d032015-05-25 18:48:19 +00001250 zSql = zMore;
1251 if( pStmt ){
drhe5c5f2c2015-05-26 00:28:08 +00001252 if( (runFlags & SQL_OUTPUT)==0 ){
1253 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
1254 }else{
1255 int nCol = -1;
1256 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1257 int i;
1258 if( nCol<0 ){
1259 nCol = sqlite3_column_count(pStmt);
1260 }else if( nCol>0 ){
1261 printf("--------------------------------------------\n");
1262 }
1263 for(i=0; i<nCol; i++){
1264 int eType = sqlite3_column_type(pStmt,i);
1265 printf("%s = ", sqlite3_column_name(pStmt,i));
1266 switch( eType ){
1267 case SQLITE_NULL: {
1268 printf("NULL\n");
1269 break;
1270 }
1271 case SQLITE_INTEGER: {
1272 printf("INT %s\n", sqlite3_column_text(pStmt,i));
1273 break;
1274 }
1275 case SQLITE_FLOAT: {
1276 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i));
1277 break;
1278 }
1279 case SQLITE_TEXT: {
1280 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i));
1281 break;
1282 }
1283 case SQLITE_BLOB: {
1284 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i));
1285 break;
1286 }
1287 }
1288 }
1289 }
1290 }
drh3b74d032015-05-25 18:48:19 +00001291 sqlite3_finalize(pStmt);
drh3b74d032015-05-25 18:48:19 +00001292 }
1293 }
1294}
1295
drha9542b12015-05-25 19:35:42 +00001296/*
drh9a645862015-06-24 12:44:42 +00001297** Rebuild the database file.
1298**
1299** (1) Remove duplicate entries
1300** (2) Put all entries in order
1301** (3) Vacuum
1302*/
drhe5da9352019-01-27 01:11:40 +00001303static void rebuild_database(sqlite3 *db, int dbSqlOnly){
drh9a645862015-06-24 12:44:42 +00001304 int rc;
drhe5da9352019-01-27 01:11:40 +00001305 char *zSql;
1306 zSql = sqlite3_mprintf(
drh9a645862015-06-24 12:44:42 +00001307 "BEGIN;\n"
1308 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
1309 "DELETE FROM db;\n"
drh5ecf9032018-05-08 12:49:53 +00001310 "INSERT INTO db(dbid, dbcontent) "
1311 " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001312 "DROP TABLE dbx;\n"
drhe5da9352019-01-27 01:11:40 +00001313 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n"
drh9a645862015-06-24 12:44:42 +00001314 "DELETE FROM xsql;\n"
drh5ecf9032018-05-08 12:49:53 +00001315 "INSERT INTO xsql(sqlid,sqltext) "
1316 " SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001317 "DROP TABLE sx;\n"
1318 "COMMIT;\n"
1319 "PRAGMA page_size=1024;\n"
drhe5da9352019-01-27 01:11:40 +00001320 "VACUUM;\n",
1321 dbSqlOnly ? " WHERE isdbsql(sqltext)" : ""
1322 );
1323 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1324 sqlite3_free(zSql);
drh9a645862015-06-24 12:44:42 +00001325 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db));
1326}
1327
1328/*
drh53e66c32015-07-24 15:49:23 +00001329** Return the value of a hexadecimal digit. Return -1 if the input
1330** is not a hex digit.
1331*/
1332static int hexDigitValue(char c){
1333 if( c>='0' && c<='9' ) return c - '0';
1334 if( c>='a' && c<='f' ) return c - 'a' + 10;
1335 if( c>='A' && c<='F' ) return c - 'A' + 10;
1336 return -1;
1337}
1338
1339/*
1340** Interpret zArg as an integer value, possibly with suffixes.
1341*/
1342static int integerValue(const char *zArg){
1343 sqlite3_int64 v = 0;
1344 static const struct { char *zSuffix; int iMult; } aMult[] = {
1345 { "KiB", 1024 },
1346 { "MiB", 1024*1024 },
1347 { "GiB", 1024*1024*1024 },
1348 { "KB", 1000 },
1349 { "MB", 1000000 },
1350 { "GB", 1000000000 },
1351 { "K", 1000 },
1352 { "M", 1000000 },
1353 { "G", 1000000000 },
1354 };
1355 int i;
1356 int isNeg = 0;
1357 if( zArg[0]=='-' ){
1358 isNeg = 1;
1359 zArg++;
1360 }else if( zArg[0]=='+' ){
1361 zArg++;
1362 }
1363 if( zArg[0]=='0' && zArg[1]=='x' ){
1364 int x;
1365 zArg += 2;
1366 while( (x = hexDigitValue(zArg[0]))>=0 ){
1367 v = (v<<4) + x;
1368 zArg++;
1369 }
1370 }else{
drhc56fac72015-10-29 13:48:15 +00001371 while( ISDIGIT(zArg[0]) ){
drh53e66c32015-07-24 15:49:23 +00001372 v = v*10 + zArg[0] - '0';
1373 zArg++;
1374 }
1375 }
1376 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
1377 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1378 v *= aMult[i].iMult;
1379 break;
1380 }
1381 }
1382 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648");
1383 return (int)(isNeg? -v : v);
1384}
1385
1386/*
drh725a9c72019-01-25 13:03:38 +00001387** Return the number of "v" characters in a string. Return 0 if there
1388** are any characters in the string other than "v".
1389*/
1390static int numberOfVChar(const char *z){
1391 int N = 0;
1392 while( z[0] && z[0]=='v' ){
1393 z++;
1394 N++;
1395 }
1396 return z[0]==0 ? N : 0;
1397}
1398
1399/*
drha9542b12015-05-25 19:35:42 +00001400** Print sketchy documentation for this utility program
1401*/
1402static void showHelp(void){
1403 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
1404 printf(
1405"Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
1406"each database, checking for crashes and memory leaks.\n"
1407"Options:\n"
drha36e01a2016-08-03 13:40:54 +00001408" --cell-size-check Set the PRAGMA cell_size_check=ON\n"
1409" --dbid N Use only the database where dbid=N\n"
1410" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
1411" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
1412" --help Show this help text\n"
drh5180d682018-08-06 01:39:31 +00001413" --info Show information about SOURCE-DB w/o running tests\n"
drhbe03cc92020-01-20 14:42:09 +00001414" --limit-depth N Limit expression depth to N\n"
drha36e01a2016-08-03 13:40:54 +00001415" --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
1416" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
drh5ecf9032018-05-08 12:49:53 +00001417" --load-sql ARGS... Load SQL scripts fron files into SOURCE-DB\n"
drha36e01a2016-08-03 13:40:54 +00001418" --load-db ARGS... Load template databases from files into SOURCE_DB\n"
drhe5da9352019-01-27 01:11:40 +00001419" --load-dbsql ARGS.. Load dbsqlfuzz outputs into the xsql table\n"
drha36e01a2016-08-03 13:40:54 +00001420" -m TEXT Add a description to the database\n"
1421" --native-vfs Use the native VFS for initially empty database files\n"
drh174f8552017-03-20 22:58:27 +00001422" --native-malloc Turn off MEMSYS3/5 and Lookaside\n"
drhea432ba2016-11-11 16:33:47 +00001423" --oss-fuzz Enable OSS-FUZZ testing\n"
drhbeaf5142016-12-26 00:15:56 +00001424" --prng-seed N Seed value for the PRGN inside of SQLite\n"
drh5180d682018-08-06 01:39:31 +00001425" -q|--quiet Reduced output\n"
drha36e01a2016-08-03 13:40:54 +00001426" --rebuild Rebuild and vacuum the database file\n"
1427" --result-trace Show the results of each SQL command\n"
1428" --sqlid N Use only SQL where sqlid=N\n"
1429" --timeout N Abort if any single test needs more than N seconds\n"
1430" -v|--verbose Increased output. Repeat for more output.\n"
drh6e1c45e2019-12-18 13:42:04 +00001431" --vdbe-debug Activate VDBE debugging.\n"
drha9542b12015-05-25 19:35:42 +00001432 );
1433}
1434
drh3b74d032015-05-25 18:48:19 +00001435int main(int argc, char **argv){
1436 sqlite3_int64 iBegin; /* Start time of this program */
drh3b74d032015-05-25 18:48:19 +00001437 int quietFlag = 0; /* True if --quiet or -q */
1438 int verboseFlag = 0; /* True if --verbose or -v */
1439 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */
drh5ecf9032018-05-08 12:49:53 +00001440 int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */
drh3b74d032015-05-25 18:48:19 +00001441 sqlite3 *db = 0; /* The open database connection */
drhd9972ef2015-05-26 17:57:56 +00001442 sqlite3_stmt *pStmt; /* A prepared statement */
drh3b74d032015-05-25 18:48:19 +00001443 int rc; /* Result code from SQLite interface calls */
1444 Blob *pSql; /* For looping over SQL scripts */
1445 Blob *pDb; /* For looping over template databases */
1446 int i; /* Loop index for the argv[] loop */
drhe5da9352019-01-27 01:11:40 +00001447 int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */
drha9542b12015-05-25 19:35:42 +00001448 int onlySqlid = -1; /* --sqlid */
1449 int onlyDbid = -1; /* --dbid */
drh15b31282015-05-25 21:59:05 +00001450 int nativeFlag = 0; /* --native-vfs */
drh9a645862015-06-24 12:44:42 +00001451 int rebuildFlag = 0; /* --rebuild */
drhd83e2832015-06-24 14:45:44 +00001452 int vdbeLimitFlag = 0; /* --limit-vdbe */
drh5180d682018-08-06 01:39:31 +00001453 int infoFlag = 0; /* --info */
drh94701b02015-06-24 13:25:34 +00001454 int timeoutTest = 0; /* undocumented --timeout-test flag */
drhe5c5f2c2015-05-26 00:28:08 +00001455 int runFlags = 0; /* Flags sent to runSql() */
drhd9972ef2015-05-26 17:57:56 +00001456 char *zMsg = 0; /* Add this message */
1457 int nSrcDb = 0; /* Number of source databases */
1458 char **azSrcDb = 0; /* Array of source database names */
1459 int iSrcDb; /* Loop over all source databases */
1460 int nTest = 0; /* Total number of tests performed */
1461 char *zDbName = ""; /* Appreviated name of a source database */
drh5ecf9032018-05-08 12:49:53 +00001462 const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */
drh1421d982015-05-27 03:46:18 +00001463 int cellSzCkFlag = 0; /* --cell-size-check */
drh5ecf9032018-05-08 12:49:53 +00001464 int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */
drhd4ddcbc2015-06-25 02:25:28 +00001465 int iTimeout = 120; /* Default 120-second timeout */
drh31999c52019-11-14 17:46:32 +00001466 int nMem = 0; /* Memory limit override */
drh362b66f2016-11-14 18:27:41 +00001467 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */
drh40e0e0d2015-09-22 18:51:17 +00001468 char *zExpDb = 0; /* Write Databases to files in this directory */
1469 char *zExpSql = 0; /* Write SQL to files in this directory */
drh6653fbe2015-11-13 20:52:49 +00001470 void *pHeap = 0; /* Heap for use by SQLite */
drhea432ba2016-11-11 16:33:47 +00001471 int ossFuzz = 0; /* enable OSS-FUZZ testing */
drh362b66f2016-11-14 18:27:41 +00001472 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */
drh174f8552017-03-20 22:58:27 +00001473 int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */
drhbeaf5142016-12-26 00:15:56 +00001474 sqlite3_vfs *pDfltVfs; /* The default VFS */
drhf2cf4122018-05-08 13:03:31 +00001475 int openFlags4Data; /* Flags for sqlite3_open_v2() */
drh725a9c72019-01-25 13:03:38 +00001476 int nV; /* How much to increase verbosity with -vvvv */
drh3b74d032015-05-25 18:48:19 +00001477
drh39b3bcf2020-03-02 16:31:21 +00001478 registerOomSimulator();
drh8055a3e2018-11-21 14:27:34 +00001479 sqlite3_initialize();
drh3b74d032015-05-25 18:48:19 +00001480 iBegin = timeOfDay();
drh94701b02015-06-24 13:25:34 +00001481#ifdef __unix__
drha7648f02019-12-18 13:02:18 +00001482 signal(SIGALRM, signalHandler);
1483 signal(SIGSEGV, signalHandler);
1484 signal(SIGABRT, signalHandler);
drh94701b02015-06-24 13:25:34 +00001485#endif
drh3b74d032015-05-25 18:48:19 +00001486 g.zArgv0 = argv[0];
drhf2cf4122018-05-08 13:03:31 +00001487 openFlags4Data = SQLITE_OPEN_READONLY;
drh4d6fda72015-05-26 18:58:32 +00001488 zFailCode = getenv("TEST_FAILURE");
drhbeaf5142016-12-26 00:15:56 +00001489 pDfltVfs = sqlite3_vfs_find(0);
1490 inmemVfsRegister(1);
drh3b74d032015-05-25 18:48:19 +00001491 for(i=1; i<argc; i++){
1492 const char *z = argv[i];
1493 if( z[0]=='-' ){
1494 z++;
1495 if( z[0]=='-' ) z++;
drh1421d982015-05-27 03:46:18 +00001496 if( strcmp(z,"cell-size-check")==0 ){
1497 cellSzCkFlag = 1;
1498 }else
drha9542b12015-05-25 19:35:42 +00001499 if( strcmp(z,"dbid")==0 ){
1500 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001501 onlyDbid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001502 }else
drh40e0e0d2015-09-22 18:51:17 +00001503 if( strcmp(z,"export-db")==0 ){
1504 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1505 zExpDb = argv[++i];
1506 }else
drhe5da9352019-01-27 01:11:40 +00001507 if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){
drh40e0e0d2015-09-22 18:51:17 +00001508 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1509 zExpSql = argv[++i];
1510 }else
drh3b74d032015-05-25 18:48:19 +00001511 if( strcmp(z,"help")==0 ){
1512 showHelp();
1513 return 0;
1514 }else
drh5180d682018-08-06 01:39:31 +00001515 if( strcmp(z,"info")==0 ){
1516 infoFlag = 1;
1517 }else
drhbe03cc92020-01-20 14:42:09 +00001518 if( strcmp(z,"limit-depth")==0 ){
1519 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1520 depthLimit = integerValue(argv[++i]);
1521 }else
drh53e66c32015-07-24 15:49:23 +00001522 if( strcmp(z,"limit-mem")==0 ){
1523 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1524 nMem = integerValue(argv[++i]);
1525 }else
drhd83e2832015-06-24 14:45:44 +00001526 if( strcmp(z,"limit-vdbe")==0 ){
1527 vdbeLimitFlag = 1;
1528 }else
drh3b74d032015-05-25 18:48:19 +00001529 if( strcmp(z,"load-sql")==0 ){
drha8781d92020-02-25 20:05:58 +00001530 zInsSql = "INSERT INTO xsql(sqltext)"
1531 "VALUES(CAST(readtextfile(?1) AS text))";
drh3b74d032015-05-25 18:48:19 +00001532 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001533 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001534 break;
1535 }else
1536 if( strcmp(z,"load-db")==0 ){
1537 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
1538 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001539 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001540 break;
1541 }else
drhe5da9352019-01-27 01:11:40 +00001542 if( strcmp(z,"load-dbsql")==0 ){
drha8781d92020-02-25 20:05:58 +00001543 zInsSql = "INSERT INTO xsql(sqltext)"
1544 "VALUES(CAST(readtextfile(?1) AS text))";
drhe5da9352019-01-27 01:11:40 +00001545 iFirstInsArg = i+1;
1546 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
1547 dbSqlOnly = 1;
1548 break;
1549 }else
drhd9972ef2015-05-26 17:57:56 +00001550 if( strcmp(z,"m")==0 ){
1551 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1552 zMsg = argv[++i];
drhf2cf4122018-05-08 13:03:31 +00001553 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drhd9972ef2015-05-26 17:57:56 +00001554 }else
drh174f8552017-03-20 22:58:27 +00001555 if( strcmp(z,"native-malloc")==0 ){
1556 nativeMalloc = 1;
1557 }else
drh15b31282015-05-25 21:59:05 +00001558 if( strcmp(z,"native-vfs")==0 ){
1559 nativeFlag = 1;
1560 }else
drhea432ba2016-11-11 16:33:47 +00001561 if( strcmp(z,"oss-fuzz")==0 ){
1562 ossFuzz = 1;
1563 }else
drhbeaf5142016-12-26 00:15:56 +00001564 if( strcmp(z,"prng-seed")==0 ){
1565 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1566 g.uRandom = atoi(argv[++i]);
1567 }else
drh3b74d032015-05-25 18:48:19 +00001568 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
1569 quietFlag = 1;
1570 verboseFlag = 0;
drha47e7092019-01-25 04:00:14 +00001571 eVerbosity = 0;
drh3b74d032015-05-25 18:48:19 +00001572 }else
drh9a645862015-06-24 12:44:42 +00001573 if( strcmp(z,"rebuild")==0 ){
1574 rebuildFlag = 1;
drhf2cf4122018-05-08 13:03:31 +00001575 openFlags4Data = SQLITE_OPEN_READWRITE;
drh9a645862015-06-24 12:44:42 +00001576 }else
drhe5c5f2c2015-05-26 00:28:08 +00001577 if( strcmp(z,"result-trace")==0 ){
1578 runFlags |= SQL_OUTPUT;
1579 }else
drha9542b12015-05-25 19:35:42 +00001580 if( strcmp(z,"sqlid")==0 ){
1581 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001582 onlySqlid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001583 }else
drh92298632015-06-24 23:44:30 +00001584 if( strcmp(z,"timeout")==0 ){
1585 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001586 iTimeout = integerValue(argv[++i]);
drh92298632015-06-24 23:44:30 +00001587 }else
drh94701b02015-06-24 13:25:34 +00001588 if( strcmp(z,"timeout-test")==0 ){
1589 timeoutTest = 1;
1590#ifndef __unix__
1591 fatalError("timeout is not available on non-unix systems");
1592#endif
1593 }else
drh6e1c45e2019-12-18 13:42:04 +00001594 if( strcmp(z,"vdbe-debug")==0 ){
1595 bVdbeDebug = 1;
1596 }else
drh725a9c72019-01-25 13:03:38 +00001597 if( strcmp(z,"verbose")==0 ){
drh3b74d032015-05-25 18:48:19 +00001598 quietFlag = 0;
drh4c9d2282016-02-18 14:03:15 +00001599 verboseFlag++;
drha47e7092019-01-25 04:00:14 +00001600 eVerbosity++;
drh4c9d2282016-02-18 14:03:15 +00001601 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
drh3b74d032015-05-25 18:48:19 +00001602 }else
drh725a9c72019-01-25 13:03:38 +00001603 if( (nV = numberOfVChar(z))>=1 ){
1604 quietFlag = 0;
1605 verboseFlag += nV;
1606 eVerbosity += nV;
1607 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
1608 }else
drha47e7092019-01-25 04:00:14 +00001609 if( strcmp(z,"version")==0 ){
1610 int ii;
drhed457032019-01-25 17:51:06 +00001611 const char *zz;
drha47e7092019-01-25 04:00:14 +00001612 printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid());
drhed457032019-01-25 17:51:06 +00001613 for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){
1614 printf("%s\n", zz);
drha47e7092019-01-25 04:00:14 +00001615 }
1616 return 0;
1617 }else
drh3b74d032015-05-25 18:48:19 +00001618 {
1619 fatalError("unknown option: %s", argv[i]);
1620 }
1621 }else{
drhd9972ef2015-05-26 17:57:56 +00001622 nSrcDb++;
1623 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0]));
1624 azSrcDb[nSrcDb-1] = argv[i];
drh3b74d032015-05-25 18:48:19 +00001625 }
1626 }
drhd9972ef2015-05-26 17:57:56 +00001627 if( nSrcDb==0 ) fatalError("no source database specified");
1628 if( nSrcDb>1 ){
1629 if( zMsg ){
1630 fatalError("cannot change the description of more than one database");
drh3b74d032015-05-25 18:48:19 +00001631 }
drhd9972ef2015-05-26 17:57:56 +00001632 if( zInsSql ){
1633 fatalError("cannot import into more than one database");
1634 }
drh3b74d032015-05-25 18:48:19 +00001635 }
1636
drhd9972ef2015-05-26 17:57:56 +00001637 /* Process each source database separately */
1638 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
drha7648f02019-12-18 13:02:18 +00001639 g.zDbFile = azSrcDb[iSrcDb];
drhbeaf5142016-12-26 00:15:56 +00001640 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db,
drhf2cf4122018-05-08 13:03:31 +00001641 openFlags4Data, pDfltVfs->zName);
drhd9972ef2015-05-26 17:57:56 +00001642 if( rc ){
1643 fatalError("cannot open source database %s - %s",
1644 azSrcDb[iSrcDb], sqlite3_errmsg(db));
1645 }
drh5180d682018-08-06 01:39:31 +00001646
1647 /* Print the description, if there is one */
1648 if( infoFlag ){
1649 int n;
1650 zDbName = azSrcDb[iSrcDb];
1651 i = (int)strlen(zDbName) - 1;
1652 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1653 zDbName += i;
1654 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1655 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1656 printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0));
1657 }else{
1658 printf("%s: (empty \"readme\")", zDbName);
1659 }
1660 sqlite3_finalize(pStmt);
1661 sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0);
1662 if( pStmt
1663 && sqlite3_step(pStmt)==SQLITE_ROW
1664 && (n = sqlite3_column_int(pStmt,0))>0
1665 ){
1666 printf(" - %d DBs", n);
1667 }
1668 sqlite3_finalize(pStmt);
1669 sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0);
1670 if( pStmt
1671 && sqlite3_step(pStmt)==SQLITE_ROW
1672 && (n = sqlite3_column_int(pStmt,0))>0
1673 ){
1674 printf(" - %d scripts", n);
1675 }
1676 sqlite3_finalize(pStmt);
1677 printf("\n");
1678 sqlite3_close(db);
1679 continue;
1680 }
1681
drh9a645862015-06-24 12:44:42 +00001682 rc = sqlite3_exec(db,
drhd9972ef2015-05-26 17:57:56 +00001683 "CREATE TABLE IF NOT EXISTS db(\n"
1684 " dbid INTEGER PRIMARY KEY, -- database id\n"
1685 " dbcontent BLOB -- database disk file image\n"
1686 ");\n"
1687 "CREATE TABLE IF NOT EXISTS xsql(\n"
1688 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
1689 " sqltext TEXT -- Text of SQL statements to run\n"
1690 ");"
1691 "CREATE TABLE IF NOT EXISTS readme(\n"
1692 " msg TEXT -- Human-readable description of this file\n"
1693 ");", 0, 0, 0);
1694 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db));
1695 if( zMsg ){
1696 char *zSql;
1697 zSql = sqlite3_mprintf(
1698 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg);
1699 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1700 sqlite3_free(zSql);
1701 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db));
1702 }
drh362b66f2016-11-14 18:27:41 +00001703 ossFuzzThisDb = ossFuzz;
1704
1705 /* If the CONFIG(name,value) table exists, read db-specific settings
1706 ** from that table */
1707 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){
drh5ecf9032018-05-08 12:49:53 +00001708 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config",
1709 -1, &pStmt, 0);
drh362b66f2016-11-14 18:27:41 +00001710 if( rc ) fatalError("cannot prepare query of CONFIG table: %s",
1711 sqlite3_errmsg(db));
1712 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1713 const char *zName = (const char *)sqlite3_column_text(pStmt,0);
1714 if( zName==0 ) continue;
1715 if( strcmp(zName, "oss-fuzz")==0 ){
1716 ossFuzzThisDb = sqlite3_column_int(pStmt,1);
1717 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb);
1718 }
drh31999c52019-11-14 17:46:32 +00001719 if( strcmp(zName, "limit-mem")==0 ){
drh362b66f2016-11-14 18:27:41 +00001720 nMemThisDb = sqlite3_column_int(pStmt,1);
1721 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb);
drh362b66f2016-11-14 18:27:41 +00001722 }
1723 }
1724 sqlite3_finalize(pStmt);
1725 }
1726
drhd9972ef2015-05-26 17:57:56 +00001727 if( zInsSql ){
1728 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1729 readfileFunc, 0, 0);
drha8781d92020-02-25 20:05:58 +00001730 sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0,
1731 readtextfileFunc, 0, 0);
drhe5da9352019-01-27 01:11:40 +00001732 sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0,
1733 isDbSqlFunc, 0, 0);
drhd9972ef2015-05-26 17:57:56 +00001734 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
1735 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1736 zInsSql, sqlite3_errmsg(db));
1737 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
1738 if( rc ) fatalError("cannot start a transaction");
1739 for(i=iFirstInsArg; i<argc; i++){
1740 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC);
1741 sqlite3_step(pStmt);
1742 rc = sqlite3_reset(pStmt);
1743 if( rc ) fatalError("insert failed for %s", argv[i]);
drh3b74d032015-05-25 18:48:19 +00001744 }
drhd9972ef2015-05-26 17:57:56 +00001745 sqlite3_finalize(pStmt);
1746 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
drh5ecf9032018-05-08 12:49:53 +00001747 if( rc ) fatalError("cannot commit the transaction: %s",
1748 sqlite3_errmsg(db));
drhe5da9352019-01-27 01:11:40 +00001749 rebuild_database(db, dbSqlOnly);
drh3b74d032015-05-25 18:48:19 +00001750 sqlite3_close(db);
drhd9972ef2015-05-26 17:57:56 +00001751 return 0;
drh3b74d032015-05-25 18:48:19 +00001752 }
drh16f05822017-03-20 20:42:21 +00001753 rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0);
1754 if( rc ) fatalError("cannot set database to query-only");
drh40e0e0d2015-09-22 18:51:17 +00001755 if( zExpDb!=0 || zExpSql!=0 ){
1756 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1757 writefileFunc, 0, 0);
1758 if( zExpDb!=0 ){
1759 const char *zExDb =
1760 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
1761 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
1762 " FROM db WHERE ?2<0 OR dbid=?2;";
1763 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
1764 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1765 zExDb, sqlite3_errmsg(db));
1766 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
1767 SQLITE_STATIC, SQLITE_UTF8);
1768 sqlite3_bind_int(pStmt, 2, onlyDbid);
1769 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1770 printf("write db-%d (%d bytes) into %s\n",
1771 sqlite3_column_int(pStmt,1),
1772 sqlite3_column_int(pStmt,3),
1773 sqlite3_column_text(pStmt,2));
1774 }
1775 sqlite3_finalize(pStmt);
1776 }
1777 if( zExpSql!=0 ){
1778 const char *zExSql =
1779 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
1780 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
1781 " FROM xsql WHERE ?2<0 OR sqlid=?2;";
1782 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
1783 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1784 zExSql, sqlite3_errmsg(db));
1785 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
1786 SQLITE_STATIC, SQLITE_UTF8);
1787 sqlite3_bind_int(pStmt, 2, onlySqlid);
1788 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1789 printf("write sql-%d (%d bytes) into %s\n",
1790 sqlite3_column_int(pStmt,1),
1791 sqlite3_column_int(pStmt,3),
1792 sqlite3_column_text(pStmt,2));
1793 }
1794 sqlite3_finalize(pStmt);
1795 }
1796 sqlite3_close(db);
1797 return 0;
1798 }
drhd9972ef2015-05-26 17:57:56 +00001799
1800 /* Load all SQL script content and all initial database images from the
1801 ** source db
1802 */
1803 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid,
1804 &g.nSql, &g.pFirstSql);
1805 if( g.nSql==0 ) fatalError("need at least one SQL script");
1806 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid,
1807 &g.nDb, &g.pFirstDb);
1808 if( g.nDb==0 ){
1809 g.pFirstDb = safe_realloc(0, sizeof(Blob));
1810 memset(g.pFirstDb, 0, sizeof(Blob));
1811 g.pFirstDb->id = 1;
1812 g.pFirstDb->seq = 0;
1813 g.nDb = 1;
drhd83e2832015-06-24 14:45:44 +00001814 sqlFuzz = 1;
drhd9972ef2015-05-26 17:57:56 +00001815 }
1816
1817 /* Print the description, if there is one */
1818 if( !quietFlag ){
drhd9972ef2015-05-26 17:57:56 +00001819 zDbName = azSrcDb[iSrcDb];
drhe683b892016-02-15 18:47:26 +00001820 i = (int)strlen(zDbName) - 1;
drhd9972ef2015-05-26 17:57:56 +00001821 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1822 zDbName += i;
1823 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1824 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1825 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0));
1826 }
1827 sqlite3_finalize(pStmt);
1828 }
drh9a645862015-06-24 12:44:42 +00001829
1830 /* Rebuild the database, if requested */
1831 if( rebuildFlag ){
1832 if( !quietFlag ){
1833 printf("%s: rebuilding... ", zDbName);
1834 fflush(stdout);
1835 }
drhe5da9352019-01-27 01:11:40 +00001836 rebuild_database(db, 0);
drh9a645862015-06-24 12:44:42 +00001837 if( !quietFlag ) printf("done\n");
1838 }
drhd9972ef2015-05-26 17:57:56 +00001839
1840 /* Close the source database. Verify that no SQLite memory allocations are
1841 ** outstanding.
1842 */
1843 sqlite3_close(db);
1844 if( sqlite3_memory_used()>0 ){
1845 fatalError("SQLite has memory in use before the start of testing");
1846 }
drh53e66c32015-07-24 15:49:23 +00001847
1848 /* Limit available memory, if requested */
drh174f8552017-03-20 22:58:27 +00001849 sqlite3_shutdown();
drh39b3bcf2020-03-02 16:31:21 +00001850
drh31999c52019-11-14 17:46:32 +00001851 if( nMemThisDb>0 && nMem==0 ){
1852 if( !nativeMalloc ){
1853 pHeap = realloc(pHeap, nMemThisDb);
1854 if( pHeap==0 ){
1855 fatalError("failed to allocate %d bytes of heap memory", nMem);
1856 }
1857 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128);
1858 }else{
1859 sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb);
drh53e66c32015-07-24 15:49:23 +00001860 }
drh31999c52019-11-14 17:46:32 +00001861 }else{
1862 sqlite3_hard_heap_limit64(0);
drh53e66c32015-07-24 15:49:23 +00001863 }
drh174f8552017-03-20 22:58:27 +00001864
1865 /* Disable lookaside with the --native-malloc option */
1866 if( nativeMalloc ){
1867 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
1868 }
drhd9972ef2015-05-26 17:57:56 +00001869
drhbeaf5142016-12-26 00:15:56 +00001870 /* Reset the in-memory virtual filesystem */
drhd9972ef2015-05-26 17:57:56 +00001871 formatVfs();
drhd9972ef2015-05-26 17:57:56 +00001872
1873 /* Run a test using each SQL script against each database.
1874 */
1875 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName);
1876 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){
drha47e7092019-01-25 04:00:14 +00001877 if( isDbSql(pSql->a, pSql->sz) ){
1878 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id);
1879 if( verboseFlag ){
1880 printf("%s\n", g.zTestName);
1881 fflush(stdout);
1882 }else if( !quietFlag ){
1883 static int prevAmt = -1;
1884 int idx = pSql->seq;
1885 int amt = idx*10/(g.nSql);
1886 if( amt!=prevAmt ){
1887 printf(" %d%%", amt*10);
1888 fflush(stdout);
1889 prevAmt = amt;
1890 }
1891 }
1892 runCombinedDbSqlInput(pSql->a, pSql->sz);
1893 nTest++;
1894 g.zTestName[0] = 0;
drh39b3bcf2020-03-02 16:31:21 +00001895 disableOom();
drha47e7092019-01-25 04:00:14 +00001896 continue;
1897 }
drhd9972ef2015-05-26 17:57:56 +00001898 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){
1899 int openFlags;
1900 const char *zVfs = "inmem";
1901 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d",
1902 pSql->id, pDb->id);
1903 if( verboseFlag ){
1904 printf("%s\n", g.zTestName);
1905 fflush(stdout);
1906 }else if( !quietFlag ){
1907 static int prevAmt = -1;
1908 int idx = pSql->seq*g.nDb + pDb->id - 1;
1909 int amt = idx*10/(g.nDb*g.nSql);
1910 if( amt!=prevAmt ){
1911 printf(" %d%%", amt*10);
1912 fflush(stdout);
1913 prevAmt = amt;
1914 }
1915 }
1916 createVFile("main.db", pDb->sz, pDb->a);
drhbeaf5142016-12-26 00:15:56 +00001917 sqlite3_randomness(0,0);
drh362b66f2016-11-14 18:27:41 +00001918 if( ossFuzzThisDb ){
drhea432ba2016-11-11 16:33:47 +00001919#ifndef SQLITE_OSS_FUZZ
drh5ecf9032018-05-08 12:49:53 +00001920 fatalError("--oss-fuzz not supported: recompile"
1921 " with -DSQLITE_OSS_FUZZ");
drhea432ba2016-11-11 16:33:47 +00001922#else
1923 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t);
1924 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz);
drh78057352015-06-24 23:17:35 +00001925#endif
drhea432ba2016-11-11 16:33:47 +00001926 }else{
1927 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
1928 if( nativeFlag && pDb->sz==0 ){
1929 openFlags |= SQLITE_OPEN_MEMORY;
1930 zVfs = 0;
1931 }
1932 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs);
1933 if( rc ) fatalError("cannot open inmem database");
drhdfcfff62016-12-26 12:25:19 +00001934 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000);
1935 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50);
drhea432ba2016-11-11 16:33:47 +00001936 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags);
1937 setAlarm(iTimeout);
1938#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1939 if( sqlFuzz || vdbeLimitFlag ){
drh5ecf9032018-05-08 12:49:53 +00001940 sqlite3_progress_handler(db, 100000, progressHandler,
1941 &vdbeLimitFlag);
drhea432ba2016-11-11 16:33:47 +00001942 }
1943#endif
drhe6e96b12019-08-02 21:03:24 +00001944#ifdef SQLITE_TESTCTRL_PRNG_SEED
drh2e6d83b2019-08-03 01:39:20 +00001945 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
drhe6e96b12019-08-02 21:03:24 +00001946#endif
drh6e1c45e2019-12-18 13:42:04 +00001947 if( bVdbeDebug ){
1948 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
1949 }
drhea432ba2016-11-11 16:33:47 +00001950 do{
1951 runSql(db, (char*)pSql->a, runFlags);
1952 }while( timeoutTest );
1953 setAlarm(0);
drh174f8552017-03-20 22:58:27 +00001954 sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
drhea432ba2016-11-11 16:33:47 +00001955 sqlite3_close(db);
1956 }
drh174f8552017-03-20 22:58:27 +00001957 if( sqlite3_memory_used()>0 ){
1958 fatalError("memory leak: %lld bytes outstanding",
1959 sqlite3_memory_used());
1960 }
drhd9972ef2015-05-26 17:57:56 +00001961 reformatVfs();
1962 nTest++;
1963 g.zTestName[0] = 0;
drh4d6fda72015-05-26 18:58:32 +00001964
1965 /* Simulate an error if the TEST_FAILURE environment variable is "5".
1966 ** This is used to verify that automated test script really do spot
1967 ** errors that occur in this test program.
1968 */
1969 if( zFailCode ){
1970 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
1971 fatalError("simulated failure");
1972 }else if( zFailCode[0]!=0 ){
1973 /* If TEST_FAILURE is something other than 5, just exit the test
1974 ** early */
1975 printf("\nExit early due to TEST_FAILURE being set\n");
1976 iSrcDb = nSrcDb-1;
1977 goto sourcedb_cleanup;
1978 }
1979 }
drhd9972ef2015-05-26 17:57:56 +00001980 }
1981 }
1982 if( !quietFlag && !verboseFlag ){
1983 printf(" 100%% - %d tests\n", g.nDb*g.nSql);
1984 }
1985
1986 /* Clean up at the end of processing a single source database
1987 */
drh4d6fda72015-05-26 18:58:32 +00001988 sourcedb_cleanup:
drhd9972ef2015-05-26 17:57:56 +00001989 blobListFree(g.pFirstSql);
1990 blobListFree(g.pFirstDb);
1991 reformatVfs();
1992
1993 } /* End loop over all source databases */
drh3b74d032015-05-25 18:48:19 +00001994
1995 if( !quietFlag ){
1996 sqlite3_int64 iElapse = timeOfDay() - iBegin;
drhd9972ef2015-05-26 17:57:56 +00001997 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
1998 "SQLite %s %s\n",
1999 nTest, (int)(iElapse/1000), (int)(iElapse%1000),
drh3b74d032015-05-25 18:48:19 +00002000 sqlite3_libversion(), sqlite3_sourceid());
2001 }
drhf74d35b2015-05-27 18:19:50 +00002002 free(azSrcDb);
drh6653fbe2015-11-13 20:52:49 +00002003 free(pHeap);
drh3b74d032015-05-25 18:48:19 +00002004 return 0;
2005}