blob: c6ad4cd5c3799dc726f2e086a73769edb35a50ff [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
drh237f41a2020-12-21 12:14:59 +0000458/* Return the current wall-clock time
459**
460** The number of milliseconds since the julian epoch.
461** 1907-01-01 00:00:00 -> 210866716800000
462** 2021-01-01 00:00:00 -> 212476176000000
463*/
drh3b74d032015-05-25 18:48:19 +0000464static sqlite3_int64 timeOfDay(void){
465 static sqlite3_vfs *clockVfs = 0;
466 sqlite3_int64 t;
drh8055a3e2018-11-21 14:27:34 +0000467 if( clockVfs==0 ){
468 clockVfs = sqlite3_vfs_find(0);
469 if( clockVfs==0 ) return 0;
470 }
drh3b74d032015-05-25 18:48:19 +0000471 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
472 clockVfs->xCurrentTimeInt64(clockVfs, &t);
473 }else{
474 double r;
475 clockVfs->xCurrentTime(clockVfs, &r);
476 t = (sqlite3_int64)(r*86400000.0);
477 }
478 return t;
479}
480
drha47e7092019-01-25 04:00:14 +0000481/***************************************************************************
482** Code to process combined database+SQL scripts generated by the
483** dbsqlfuzz fuzzer.
484*/
485
486/* An instance of the following object is passed by pointer as the
487** client data to various callbacks.
488*/
489typedef struct FuzzCtx {
490 sqlite3 *db; /* The database connection */
491 sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
492 sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
493 sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
494 unsigned nCb; /* Number of progress callbacks */
495 unsigned mxCb; /* Maximum number of progress callbacks allowed */
496 unsigned execCnt; /* Number of calls to the sqlite3_exec callback */
497 int timeoutHit; /* True when reaching a timeout */
498} FuzzCtx;
499
500/* Verbosity level for the dbsqlfuzz test runner */
501static int eVerbosity = 0;
502
503/* True to activate PRAGMA vdbe_debug=on */
504static int bVdbeDebug = 0;
505
506/* Timeout for each fuzzing attempt, in milliseconds */
drhed457032019-01-25 17:51:06 +0000507static int giTimeout = 10000; /* Defaults to 10 seconds */
drha47e7092019-01-25 04:00:14 +0000508
509/* Maximum number of progress handler callbacks */
510static unsigned int mxProgressCb = 2000;
511
512/* Maximum string length in SQLite */
513static int lengthLimit = 1000000;
514
drhbe03cc92020-01-20 14:42:09 +0000515/* Maximum expression depth */
516static int depthLimit = 500;
517
drh31999c52019-11-14 17:46:32 +0000518/* Limit on the amount of heap memory that can be used */
drha8781d92020-02-25 20:05:58 +0000519static sqlite3_int64 heapLimit = 100000000;
drh31999c52019-11-14 17:46:32 +0000520
drha47e7092019-01-25 04:00:14 +0000521/* Maximum byte-code program length in SQLite */
522static int vdbeOpLimit = 25000;
523
524/* Maximum size of the in-memory database */
525static sqlite3_int64 maxDbSize = 104857600;
drh39b3bcf2020-03-02 16:31:21 +0000526/* OOM simulation parameters */
527static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
528static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
529static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
530
531/* This routine is called when a simulated OOM occurs. It is broken
532** out as a separate routine to make it easy to set a breakpoint on
533** the OOM
534*/
535void oomFault(void){
536 if( eVerbosity ){
537 printf("Simulated OOM fault\n");
538 }
539 if( oomRepeat>0 ){
540 oomRepeat--;
541 }else{
542 oomCounter--;
543 }
544}
545
546/* This routine is a replacement malloc() that is used to simulate
547** Out-Of-Memory (OOM) errors for testing purposes.
548*/
549static void *oomMalloc(int nByte){
550 if( oomCounter ){
551 if( oomCounter==1 ){
552 oomFault();
553 return 0;
554 }else{
555 oomCounter--;
556 }
557 }
558 return defaultMalloc(nByte);
559}
560
561/* Register the OOM simulator. This must occur before any memory
562** allocations */
563static void registerOomSimulator(void){
564 sqlite3_mem_methods mem;
565 sqlite3_shutdown();
566 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
567 defaultMalloc = mem.xMalloc;
568 mem.xMalloc = oomMalloc;
569 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
570}
571
572/* Turn off any pending OOM simulation */
573static void disableOom(void){
574 oomCounter = 0;
575 oomRepeat = 0;
576}
drha47e7092019-01-25 04:00:14 +0000577
578/*
579** Translate a single byte of Hex into an integer.
580** This routine only works if h really is a valid hexadecimal
581** character: 0..9a..fA..F
582*/
drhed457032019-01-25 17:51:06 +0000583static unsigned char hexToInt(unsigned int h){
drha47e7092019-01-25 04:00:14 +0000584#ifdef SQLITE_EBCDIC
585 h += 9*(1&~(h>>4)); /* EBCDIC */
586#else
587 h += 9*(1&(h>>6)); /* ASCII */
588#endif
589 return h & 0xf;
590}
591
592/*
593** The first character of buffer zIn[0..nIn-1] is a '['. This routine
594** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it
595** does it makes corresponding changes to the *pK value and *pI value
596** and returns true. If the input buffer does not match the patterns,
597** no changes are made to either *pK or *pI and this routine returns false.
598*/
599static int isOffset(
600 const unsigned char *zIn, /* Text input */
601 int nIn, /* Bytes of input */
602 unsigned int *pK, /* half-byte cursor to adjust */
603 unsigned int *pI /* Input index to adjust */
604){
605 int i;
606 unsigned int k = 0;
607 unsigned char c;
608 for(i=1; i<nIn && (c = zIn[i])!=']'; i++){
609 if( !isxdigit(c) ) return 0;
610 k = k*16 + hexToInt(c);
611 }
612 if( i==nIn ) return 0;
613 *pK = 2*k;
614 *pI += i;
615 return 1;
616}
617
618/*
619** Decode the text starting at zIn into a binary database file.
620** The maximum length of zIn is nIn bytes. Compute the binary database
621** file contain in space obtained from sqlite3_malloc().
622**
623** Return the number of bytes of zIn consumed. Or return -1 if there
624** is an error. One potential error is that the recipe specifies a
625** database file larger than MX_FILE_SZ bytes.
626**
627** Abort on an OOM.
628*/
629static int decodeDatabase(
630 const unsigned char *zIn, /* Input text to be decoded */
631 int nIn, /* Bytes of input text */
632 unsigned char **paDecode, /* OUT: decoded database file */
633 int *pnDecode /* OUT: Size of decoded database */
634){
drh672f07c2020-10-20 14:40:53 +0000635 unsigned char *a, *aNew; /* Database under construction */
drha47e7092019-01-25 04:00:14 +0000636 int mx = 0; /* Current size of the database */
637 sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */
638 unsigned int i; /* Next byte of zIn[] to read */
639 unsigned int j; /* Temporary integer */
640 unsigned int k; /* half-byte cursor index for output */
641 unsigned int n; /* Number of bytes of input */
642 unsigned char b = 0;
643 if( nIn<4 ) return -1;
644 n = (unsigned int)nIn;
drhed457032019-01-25 17:51:06 +0000645 a = sqlite3_malloc64( nAlloc );
drha47e7092019-01-25 04:00:14 +0000646 if( a==0 ){
647 fprintf(stderr, "Out of memory!\n");
648 exit(1);
649 }
mistachkin065f3bf2019-03-20 05:45:03 +0000650 memset(a, 0, (size_t)nAlloc);
drha47e7092019-01-25 04:00:14 +0000651 for(i=k=0; i<n; i++){
drhaf638922019-02-07 00:17:36 +0000652 unsigned char c = (unsigned char)zIn[i];
drha47e7092019-01-25 04:00:14 +0000653 if( isxdigit(c) ){
654 k++;
655 if( k & 1 ){
656 b = hexToInt(c)*16;
657 }else{
658 b += hexToInt(c);
659 j = k/2 - 1;
660 if( j>=nAlloc ){
661 sqlite3_uint64 newSize;
662 if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){
663 if( eVerbosity ){
664 fprintf(stderr, "Input database too big: max %d bytes\n",
665 MX_FILE_SZ);
666 }
667 sqlite3_free(a);
668 return -1;
669 }
670 newSize = nAlloc*2;
671 if( newSize<=j ){
672 newSize = (j+4096)&~4095;
673 }
674 if( newSize>MX_FILE_SZ ){
675 if( j>=MX_FILE_SZ ){
676 sqlite3_free(a);
677 return -1;
678 }
679 newSize = MX_FILE_SZ;
680 }
drh672f07c2020-10-20 14:40:53 +0000681 aNew = sqlite3_realloc64( a, newSize );
682 if( aNew==0 ){
683 sqlite3_free(a);
684 return -1;
drha47e7092019-01-25 04:00:14 +0000685 }
drh672f07c2020-10-20 14:40:53 +0000686 a = aNew;
drha47e7092019-01-25 04:00:14 +0000687 assert( newSize > nAlloc );
mistachkin065f3bf2019-03-20 05:45:03 +0000688 memset(a+nAlloc, 0, (size_t)(newSize - nAlloc));
drha47e7092019-01-25 04:00:14 +0000689 nAlloc = newSize;
690 }
691 if( j>=(unsigned)mx ){
692 mx = (j + 4095)&~4095;
693 if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ;
694 }
695 assert( j<nAlloc );
696 a[j] = b;
697 }
698 }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){
699 continue;
700 }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){
701 i += 4;
702 break;
703 }
704 }
705 *pnDecode = mx;
706 *paDecode = a;
707 return i;
708}
709
710/*
711** Progress handler callback.
712**
713** The argument is the cutoff-time after which all processing should
714** stop. So return non-zero if the cut-off time is exceeded.
715*/
716static int progress_handler(void *pClientData) {
717 FuzzCtx *p = (FuzzCtx*)pClientData;
718 sqlite3_int64 iNow = timeOfDay();
719 int rc = iNow>=p->iCutoffTime;
720 sqlite3_int64 iDiff = iNow - p->iLastCb;
drh237f41a2020-12-21 12:14:59 +0000721 /* printf("time-remaining: %lld\n", p->iCutoffTime - iNow); */
drha47e7092019-01-25 04:00:14 +0000722 if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
723 p->nCb++;
724 if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1;
drhdf216592019-01-25 04:43:26 +0000725 if( rc && !p->timeoutHit && eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000726 printf("Timeout on progress callback %d\n", p->nCb);
727 fflush(stdout);
728 p->timeoutHit = 1;
729 }
730 return rc;
731}
732
733/*
734** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
735** "PRAGMA parser_trace" since they can dramatically increase the
736** amount of output without actually testing anything useful.
737**
738** Also block ATTACH and DETACH
739*/
740static int block_troublesome_sql(
741 void *Notused,
742 int eCode,
743 const char *zArg1,
744 const char *zArg2,
745 const char *zArg3,
746 const char *zArg4
747){
748 (void)Notused;
749 (void)zArg2;
750 (void)zArg3;
751 (void)zArg4;
752 if( eCode==SQLITE_PRAGMA ){
753 if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0
754 || sqlite3_stricmp("parser_trace", zArg1)==0
755 || sqlite3_stricmp("temp_store_directory", zArg1)==0
756 ){
757 return SQLITE_DENY;
758 }
drh39b3bcf2020-03-02 16:31:21 +0000759 if( sqlite3_stricmp("oom",zArg1)==0 && zArg2!=0 && zArg2[0]!=0 ){
760 oomCounter = atoi(zArg2);
761 }
drha47e7092019-01-25 04:00:14 +0000762 }else if( (eCode==SQLITE_ATTACH || eCode==SQLITE_DETACH)
763 && zArg1 && zArg1[0] ){
764 return SQLITE_DENY;
765 }
766 return SQLITE_OK;
767}
768
769/*
770** Run the SQL text
771*/
772static int runDbSql(sqlite3 *db, const char *zSql){
773 int rc;
774 sqlite3_stmt *pStmt;
drhaf638922019-02-07 00:17:36 +0000775 while( isspace(zSql[0]&0x7f) ) zSql++;
drha47e7092019-01-25 04:00:14 +0000776 if( zSql[0]==0 ) return SQLITE_OK;
drhdf216592019-01-25 04:43:26 +0000777 if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000778 printf("RUNNING-SQL: [%s]\n", zSql);
779 fflush(stdout);
780 }
781 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
782 if( rc==SQLITE_OK ){
783 while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
drhdf216592019-01-25 04:43:26 +0000784 if( eVerbosity>=5 ){
drha47e7092019-01-25 04:00:14 +0000785 int j;
786 for(j=0; j<sqlite3_column_count(pStmt); j++){
787 if( j ) printf(",");
788 switch( sqlite3_column_type(pStmt, j) ){
789 case SQLITE_NULL: {
790 printf("NULL");
791 break;
792 }
793 case SQLITE_INTEGER:
794 case SQLITE_FLOAT: {
795 printf("%s", sqlite3_column_text(pStmt, j));
796 break;
797 }
798 case SQLITE_BLOB: {
799 int n = sqlite3_column_bytes(pStmt, j);
800 int i;
801 const unsigned char *a;
802 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
803 printf("x'");
804 for(i=0; i<n; i++){
805 printf("%02x", a[i]);
806 }
807 printf("'");
808 break;
809 }
810 case SQLITE_TEXT: {
811 int n = sqlite3_column_bytes(pStmt, j);
812 int i;
813 const unsigned char *a;
814 a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
815 printf("'");
816 for(i=0; i<n; i++){
817 if( a[i]=='\'' ){
818 printf("''");
819 }else{
820 putchar(a[i]);
821 }
822 }
823 printf("'");
824 break;
825 }
826 } /* End switch() */
827 } /* End for() */
828 printf("\n");
829 fflush(stdout);
drhdf216592019-01-25 04:43:26 +0000830 } /* End if( eVerbosity>=5 ) */
drha47e7092019-01-25 04:00:14 +0000831 } /* End while( SQLITE_ROW */
drhdf216592019-01-25 04:43:26 +0000832 if( rc!=SQLITE_DONE && eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000833 printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db));
834 fflush(stdout);
835 }
drhdf216592019-01-25 04:43:26 +0000836 }else if( eVerbosity>=4 ){
drha47e7092019-01-25 04:00:14 +0000837 printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db));
838 fflush(stdout);
839 } /* End if( SQLITE_OK ) */
840 return sqlite3_finalize(pStmt);
841}
842
843/* Invoke this routine to run a single test case */
drh237f41a2020-12-21 12:14:59 +0000844int runCombinedDbSqlInput(const uint8_t *aData, size_t nByte, int iTimeout){
drha47e7092019-01-25 04:00:14 +0000845 int rc; /* SQLite API return value */
846 int iSql; /* Index in aData[] of start of SQL */
847 unsigned char *aDb = 0; /* Decoded database content */
848 int nDb = 0; /* Size of the decoded database */
849 int i; /* Loop counter */
850 int j; /* Start of current SQL statement */
851 char *zSql = 0; /* SQL text to run */
852 int nSql; /* Bytes of SQL text */
853 FuzzCtx cx; /* Fuzzing context */
854
855 if( nByte<10 ) return 0;
856 if( sqlite3_initialize() ) return 0;
857 if( sqlite3_memory_used()!=0 ){
858 int nAlloc = 0;
859 int nNotUsed = 0;
860 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
drh672f07c2020-10-20 14:40:53 +0000861 fprintf(stderr,"memory leak prior to test start:"
862 " %lld bytes in %d allocations\n",
drha47e7092019-01-25 04:00:14 +0000863 sqlite3_memory_used(), nAlloc);
864 exit(1);
865 }
866 memset(&cx, 0, sizeof(cx));
867 iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb);
868 if( iSql<0 ) return 0;
drhed457032019-01-25 17:51:06 +0000869 nSql = (int)(nByte - iSql);
drhdf216592019-01-25 04:43:26 +0000870 if( eVerbosity>=3 ){
drha47e7092019-01-25 04:00:14 +0000871 printf(
872 "****** %d-byte input, %d-byte database, %d-byte script "
873 "******\n", (int)nByte, nDb, nSql);
874 fflush(stdout);
875 }
876 rc = sqlite3_open(0, &cx.db);
drh672f07c2020-10-20 14:40:53 +0000877 if( rc ){
878 sqlite3_free(aDb);
879 return 1;
880 }
drha47e7092019-01-25 04:00:14 +0000881 if( bVdbeDebug ){
882 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
883 }
884
885 /* Invoke the progress handler frequently to check to see if we
886 ** are taking too long. The progress handler will return true
drhed457032019-01-25 17:51:06 +0000887 ** (which will block further processing) if more than giTimeout seconds have
drha47e7092019-01-25 04:00:14 +0000888 ** elapsed since the start of the test.
889 */
890 cx.iLastCb = timeOfDay();
drh237f41a2020-12-21 12:14:59 +0000891 cx.iCutoffTime = cx.iLastCb + (iTimeout<giTimeout ? iTimeout : giTimeout);
drha47e7092019-01-25 04:00:14 +0000892 cx.mxCb = mxProgressCb;
893#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
894 sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
895#endif
896
897 /* Set a limit on the maximum size of a prepared statement, and the
898 ** maximum length of a string or blob */
899 if( vdbeOpLimit>0 ){
900 sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit);
901 }
902 if( lengthLimit>0 ){
903 sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit);
904 }
drhbe03cc92020-01-20 14:42:09 +0000905 if( depthLimit>0 ){
906 sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit);
907 }
drh4b3282d2020-04-07 15:07:11 +0000908 sqlite3_limit(cx.db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 100);
drh31999c52019-11-14 17:46:32 +0000909 sqlite3_hard_heap_limit64(heapLimit);
drha47e7092019-01-25 04:00:14 +0000910
911 if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){
912 aDb[18] = aDb[19] = 1;
913 }
914 rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb,
915 SQLITE_DESERIALIZE_RESIZEABLE |
916 SQLITE_DESERIALIZE_FREEONCLOSE);
917 if( rc ){
918 fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc);
919 goto testrun_finished;
920 }
921 if( maxDbSize>0 ){
922 sqlite3_int64 x = maxDbSize;
923 sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x);
924 }
925
drh725a9c72019-01-25 13:03:38 +0000926 /* For high debugging levels, turn on debug mode */
927 if( eVerbosity>=5 ){
928 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0);
929 }
930
drha47e7092019-01-25 04:00:14 +0000931 /* Block debug pragmas and ATTACH/DETACH. But wait until after
932 ** deserialize to do this because deserialize depends on ATTACH */
933 sqlite3_set_authorizer(cx.db, block_troublesome_sql, 0);
934
935 /* Consistent PRNG seed */
936 sqlite3_randomness(0,0);
937
938 zSql = sqlite3_malloc( nSql + 1 );
939 if( zSql==0 ){
940 fprintf(stderr, "Out of memory!\n");
941 }else{
942 memcpy(zSql, aData+iSql, nSql);
943 zSql[nSql] = 0;
944 for(i=j=0; zSql[i]; i++){
945 if( zSql[i]==';' ){
946 char cSaved = zSql[i+1];
947 zSql[i+1] = 0;
948 if( sqlite3_complete(zSql+j) ){
949 rc = runDbSql(cx.db, zSql+j);
950 j = i+1;
951 }
952 zSql[i+1] = cSaved;
953 if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){
954 goto testrun_finished;
955 }
956 }
957 }
958 if( j<i ){
959 runDbSql(cx.db, zSql+j);
960 }
961 }
962testrun_finished:
963 sqlite3_free(zSql);
964 rc = sqlite3_close(cx.db);
965 if( rc!=SQLITE_OK ){
966 fprintf(stdout, "sqlite3_close() returns %d\n", rc);
967 }
drhdf216592019-01-25 04:43:26 +0000968 if( eVerbosity>=2 ){
drha47e7092019-01-25 04:00:14 +0000969 fprintf(stdout, "Peak memory usages: %f MB\n",
970 sqlite3_memory_highwater(1) / 1000000.0);
971 }
972 if( sqlite3_memory_used()!=0 ){
973 int nAlloc = 0;
974 int nNotUsed = 0;
975 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
976 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n",
977 sqlite3_memory_used(), nAlloc);
978 exit(1);
979 }
980 return 0;
981}
982
983/*
984** END of the dbsqlfuzz code
985***************************************************************************/
986
987/* Look at a SQL text and try to determine if it begins with a database
988** description, such as would be found in a dbsqlfuzz test case. Return
989** true if this does appear to be a dbsqlfuzz test case and false otherwise.
990*/
991static int isDbSql(unsigned char *a, int n){
drhdf216592019-01-25 04:43:26 +0000992 unsigned char buf[12];
993 int i;
drha47e7092019-01-25 04:00:14 +0000994 if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1;
995 while( n>0 && isspace(a[0]) ){ a++; n--; }
drhdf216592019-01-25 04:43:26 +0000996 for(i=0; n>0 && i<8; n--, a++){
997 if( isxdigit(a[0]) ) buf[i++] = a[0];
998 }
999 if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1;
drha47e7092019-01-25 04:00:14 +00001000 return 0;
1001}
1002
drhe5da9352019-01-27 01:11:40 +00001003/* Implementation of the isdbsql(TEXT) SQL function.
1004*/
1005static void isDbSqlFunc(
1006 sqlite3_context *context,
1007 int argc,
1008 sqlite3_value **argv
1009){
1010 int n = sqlite3_value_bytes(argv[0]);
1011 unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]);
1012 sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n));
1013}
drha47e7092019-01-25 04:00:14 +00001014
drh3b74d032015-05-25 18:48:19 +00001015/* Methods for the VHandle object
1016*/
1017static int inmemClose(sqlite3_file *pFile){
1018 VHandle *p = (VHandle*)pFile;
1019 VFile *pVFile = p->pVFile;
1020 pVFile->nRef--;
1021 if( pVFile->nRef==0 && pVFile->zFilename==0 ){
1022 pVFile->sz = -1;
1023 free(pVFile->a);
1024 pVFile->a = 0;
1025 }
1026 return SQLITE_OK;
1027}
1028static int inmemRead(
1029 sqlite3_file *pFile, /* Read from this open file */
1030 void *pData, /* Store content in this buffer */
1031 int iAmt, /* Bytes of content */
1032 sqlite3_int64 iOfst /* Start reading here */
1033){
1034 VHandle *pHandle = (VHandle*)pFile;
1035 VFile *pVFile = pHandle->pVFile;
1036 if( iOfst<0 || iOfst>=pVFile->sz ){
1037 memset(pData, 0, iAmt);
1038 return SQLITE_IOERR_SHORT_READ;
1039 }
1040 if( iOfst+iAmt>pVFile->sz ){
1041 memset(pData, 0, iAmt);
drh1573dc32015-05-25 22:29:26 +00001042 iAmt = (int)(pVFile->sz - iOfst);
drhe45985b2018-12-14 02:29:56 +00001043 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001044 return SQLITE_IOERR_SHORT_READ;
1045 }
drhaca7ea12015-05-25 23:14:37 +00001046 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +00001047 return SQLITE_OK;
1048}
1049static int inmemWrite(
1050 sqlite3_file *pFile, /* Write to this file */
1051 const void *pData, /* Content to write */
1052 int iAmt, /* bytes to write */
1053 sqlite3_int64 iOfst /* Start writing here */
1054){
1055 VHandle *pHandle = (VHandle*)pFile;
1056 VFile *pVFile = pHandle->pVFile;
1057 if( iOfst+iAmt > pVFile->sz ){
drha9542b12015-05-25 19:35:42 +00001058 if( iOfst+iAmt >= MX_FILE_SZ ){
1059 return SQLITE_FULL;
1060 }
drh1573dc32015-05-25 22:29:26 +00001061 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt));
drh908aced2015-05-26 16:12:45 +00001062 if( iOfst > pVFile->sz ){
1063 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz));
1064 }
drh1573dc32015-05-25 22:29:26 +00001065 pVFile->sz = (int)(iOfst + iAmt);
drh3b74d032015-05-25 18:48:19 +00001066 }
1067 memcpy(pVFile->a + iOfst, pData, iAmt);
1068 return SQLITE_OK;
1069}
1070static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){
1071 VHandle *pHandle = (VHandle*)pFile;
1072 VFile *pVFile = pHandle->pVFile;
drh1573dc32015-05-25 22:29:26 +00001073 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize;
drh3b74d032015-05-25 18:48:19 +00001074 return SQLITE_OK;
1075}
1076static int inmemSync(sqlite3_file *pFile, int flags){
1077 return SQLITE_OK;
1078}
1079static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
1080 *pSize = ((VHandle*)pFile)->pVFile->sz;
1081 return SQLITE_OK;
1082}
1083static int inmemLock(sqlite3_file *pFile, int type){
1084 return SQLITE_OK;
1085}
1086static int inmemUnlock(sqlite3_file *pFile, int type){
1087 return SQLITE_OK;
1088}
1089static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){
1090 *pOut = 0;
1091 return SQLITE_OK;
1092}
1093static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){
1094 return SQLITE_NOTFOUND;
1095}
1096static int inmemSectorSize(sqlite3_file *pFile){
1097 return 512;
1098}
1099static int inmemDeviceCharacteristics(sqlite3_file *pFile){
1100 return
1101 SQLITE_IOCAP_SAFE_APPEND |
1102 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
1103 SQLITE_IOCAP_POWERSAFE_OVERWRITE;
1104}
1105
1106
1107/* Method table for VHandle
1108*/
1109static sqlite3_io_methods VHandleMethods = {
1110 /* iVersion */ 1,
1111 /* xClose */ inmemClose,
1112 /* xRead */ inmemRead,
1113 /* xWrite */ inmemWrite,
1114 /* xTruncate */ inmemTruncate,
1115 /* xSync */ inmemSync,
1116 /* xFileSize */ inmemFileSize,
1117 /* xLock */ inmemLock,
1118 /* xUnlock */ inmemUnlock,
1119 /* xCheck... */ inmemCheckReservedLock,
1120 /* xFileCtrl */ inmemFileControl,
1121 /* xSectorSz */ inmemSectorSize,
1122 /* xDevchar */ inmemDeviceCharacteristics,
1123 /* xShmMap */ 0,
1124 /* xShmLock */ 0,
1125 /* xShmBarrier */ 0,
1126 /* xShmUnmap */ 0,
1127 /* xFetch */ 0,
1128 /* xUnfetch */ 0
1129};
1130
1131/*
1132** Open a new file in the inmem VFS. All files are anonymous and are
1133** delete-on-close.
1134*/
1135static int inmemOpen(
1136 sqlite3_vfs *pVfs,
1137 const char *zFilename,
1138 sqlite3_file *pFile,
1139 int openFlags,
1140 int *pOutFlags
1141){
1142 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)"");
1143 VHandle *pHandle = (VHandle*)pFile;
drha9542b12015-05-25 19:35:42 +00001144 if( pVFile==0 ){
1145 return SQLITE_FULL;
1146 }
drh3b74d032015-05-25 18:48:19 +00001147 pHandle->pVFile = pVFile;
1148 pVFile->nRef++;
1149 pFile->pMethods = &VHandleMethods;
1150 if( pOutFlags ) *pOutFlags = openFlags;
1151 return SQLITE_OK;
1152}
1153
1154/*
1155** Delete a file by name
1156*/
1157static int inmemDelete(
1158 sqlite3_vfs *pVfs,
1159 const char *zFilename,
1160 int syncdir
1161){
1162 VFile *pVFile = findVFile(zFilename);
1163 if( pVFile==0 ) return SQLITE_OK;
1164 if( pVFile->nRef==0 ){
1165 free(pVFile->zFilename);
1166 pVFile->zFilename = 0;
1167 pVFile->sz = -1;
1168 free(pVFile->a);
1169 pVFile->a = 0;
1170 return SQLITE_OK;
1171 }
1172 return SQLITE_IOERR_DELETE;
1173}
1174
1175/* Check for the existance of a file
1176*/
1177static int inmemAccess(
1178 sqlite3_vfs *pVfs,
1179 const char *zFilename,
1180 int flags,
1181 int *pResOut
1182){
1183 VFile *pVFile = findVFile(zFilename);
1184 *pResOut = pVFile!=0;
1185 return SQLITE_OK;
1186}
1187
1188/* Get the canonical pathname for a file
1189*/
1190static int inmemFullPathname(
1191 sqlite3_vfs *pVfs,
1192 const char *zFilename,
1193 int nOut,
1194 char *zOut
1195){
1196 sqlite3_snprintf(nOut, zOut, "%s", zFilename);
1197 return SQLITE_OK;
1198}
1199
drhbeaf5142016-12-26 00:15:56 +00001200/* Always use the same random see, for repeatability.
1201*/
1202static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
1203 memset(zBuf, 0, nBuf);
1204 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom));
1205 return nBuf;
1206}
1207
drh3b74d032015-05-25 18:48:19 +00001208/*
1209** Register the VFS that reads from the g.aFile[] set of files.
1210*/
drhbeaf5142016-12-26 00:15:56 +00001211static void inmemVfsRegister(int makeDefault){
drh3b74d032015-05-25 18:48:19 +00001212 static sqlite3_vfs inmemVfs;
1213 sqlite3_vfs *pDefault = sqlite3_vfs_find(0);
drh5337dac2015-11-25 15:15:03 +00001214 inmemVfs.iVersion = 3;
drh3b74d032015-05-25 18:48:19 +00001215 inmemVfs.szOsFile = sizeof(VHandle);
1216 inmemVfs.mxPathname = 200;
1217 inmemVfs.zName = "inmem";
1218 inmemVfs.xOpen = inmemOpen;
1219 inmemVfs.xDelete = inmemDelete;
1220 inmemVfs.xAccess = inmemAccess;
1221 inmemVfs.xFullPathname = inmemFullPathname;
drhbeaf5142016-12-26 00:15:56 +00001222 inmemVfs.xRandomness = inmemRandomness;
drh3b74d032015-05-25 18:48:19 +00001223 inmemVfs.xSleep = pDefault->xSleep;
drh5337dac2015-11-25 15:15:03 +00001224 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64;
drhbeaf5142016-12-26 00:15:56 +00001225 sqlite3_vfs_register(&inmemVfs, makeDefault);
drh3b74d032015-05-25 18:48:19 +00001226};
1227
drh3b74d032015-05-25 18:48:19 +00001228/*
drhe5c5f2c2015-05-26 00:28:08 +00001229** Allowed values for the runFlags parameter to runSql()
1230*/
1231#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
1232#define SQL_OUTPUT 0x0002 /* Show the SQL output */
1233
1234/*
drh3b74d032015-05-25 18:48:19 +00001235** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
1236** stop if an error is encountered.
1237*/
drhe5c5f2c2015-05-26 00:28:08 +00001238static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
drh3b74d032015-05-25 18:48:19 +00001239 const char *zMore;
1240 sqlite3_stmt *pStmt;
1241
1242 while( zSql && zSql[0] ){
1243 zMore = 0;
1244 pStmt = 0;
1245 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore);
drh4ab31472015-05-25 22:17:06 +00001246 if( zMore==zSql ) break;
drhe5c5f2c2015-05-26 00:28:08 +00001247 if( runFlags & SQL_TRACE ){
drh4ab31472015-05-25 22:17:06 +00001248 const char *z = zSql;
1249 int n;
drhc56fac72015-10-29 13:48:15 +00001250 while( z<zMore && ISSPACE(z[0]) ) z++;
drh4ab31472015-05-25 22:17:06 +00001251 n = (int)(zMore - z);
drhc56fac72015-10-29 13:48:15 +00001252 while( n>0 && ISSPACE(z[n-1]) ) n--;
drh4ab31472015-05-25 22:17:06 +00001253 if( n==0 ) break;
1254 if( pStmt==0 ){
1255 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
1256 }else{
1257 printf("TRACE: %.*s\n", n, z);
1258 }
1259 }
drh3b74d032015-05-25 18:48:19 +00001260 zSql = zMore;
1261 if( pStmt ){
drhe5c5f2c2015-05-26 00:28:08 +00001262 if( (runFlags & SQL_OUTPUT)==0 ){
1263 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
1264 }else{
1265 int nCol = -1;
1266 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1267 int i;
1268 if( nCol<0 ){
1269 nCol = sqlite3_column_count(pStmt);
1270 }else if( nCol>0 ){
1271 printf("--------------------------------------------\n");
1272 }
1273 for(i=0; i<nCol; i++){
1274 int eType = sqlite3_column_type(pStmt,i);
1275 printf("%s = ", sqlite3_column_name(pStmt,i));
1276 switch( eType ){
1277 case SQLITE_NULL: {
1278 printf("NULL\n");
1279 break;
1280 }
1281 case SQLITE_INTEGER: {
1282 printf("INT %s\n", sqlite3_column_text(pStmt,i));
1283 break;
1284 }
1285 case SQLITE_FLOAT: {
1286 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i));
1287 break;
1288 }
1289 case SQLITE_TEXT: {
1290 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i));
1291 break;
1292 }
1293 case SQLITE_BLOB: {
1294 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i));
1295 break;
1296 }
1297 }
1298 }
1299 }
1300 }
drh3b74d032015-05-25 18:48:19 +00001301 sqlite3_finalize(pStmt);
drh3b74d032015-05-25 18:48:19 +00001302 }
1303 }
1304}
1305
drha9542b12015-05-25 19:35:42 +00001306/*
drh9a645862015-06-24 12:44:42 +00001307** Rebuild the database file.
1308**
1309** (1) Remove duplicate entries
1310** (2) Put all entries in order
1311** (3) Vacuum
1312*/
drhe5da9352019-01-27 01:11:40 +00001313static void rebuild_database(sqlite3 *db, int dbSqlOnly){
drh9a645862015-06-24 12:44:42 +00001314 int rc;
drhe5da9352019-01-27 01:11:40 +00001315 char *zSql;
1316 zSql = sqlite3_mprintf(
drh9a645862015-06-24 12:44:42 +00001317 "BEGIN;\n"
1318 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
1319 "DELETE FROM db;\n"
drh5ecf9032018-05-08 12:49:53 +00001320 "INSERT INTO db(dbid, dbcontent) "
1321 " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001322 "DROP TABLE dbx;\n"
drhe5da9352019-01-27 01:11:40 +00001323 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n"
drh9a645862015-06-24 12:44:42 +00001324 "DELETE FROM xsql;\n"
drh5ecf9032018-05-08 12:49:53 +00001325 "INSERT INTO xsql(sqlid,sqltext) "
1326 " SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
drh9a645862015-06-24 12:44:42 +00001327 "DROP TABLE sx;\n"
1328 "COMMIT;\n"
1329 "PRAGMA page_size=1024;\n"
drhe5da9352019-01-27 01:11:40 +00001330 "VACUUM;\n",
1331 dbSqlOnly ? " WHERE isdbsql(sqltext)" : ""
1332 );
1333 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1334 sqlite3_free(zSql);
drh9a645862015-06-24 12:44:42 +00001335 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db));
1336}
1337
1338/*
drh53e66c32015-07-24 15:49:23 +00001339** Return the value of a hexadecimal digit. Return -1 if the input
1340** is not a hex digit.
1341*/
1342static int hexDigitValue(char c){
1343 if( c>='0' && c<='9' ) return c - '0';
1344 if( c>='a' && c<='f' ) return c - 'a' + 10;
1345 if( c>='A' && c<='F' ) return c - 'A' + 10;
1346 return -1;
1347}
1348
1349/*
1350** Interpret zArg as an integer value, possibly with suffixes.
1351*/
1352static int integerValue(const char *zArg){
1353 sqlite3_int64 v = 0;
1354 static const struct { char *zSuffix; int iMult; } aMult[] = {
1355 { "KiB", 1024 },
1356 { "MiB", 1024*1024 },
1357 { "GiB", 1024*1024*1024 },
1358 { "KB", 1000 },
1359 { "MB", 1000000 },
1360 { "GB", 1000000000 },
1361 { "K", 1000 },
1362 { "M", 1000000 },
1363 { "G", 1000000000 },
1364 };
1365 int i;
1366 int isNeg = 0;
1367 if( zArg[0]=='-' ){
1368 isNeg = 1;
1369 zArg++;
1370 }else if( zArg[0]=='+' ){
1371 zArg++;
1372 }
1373 if( zArg[0]=='0' && zArg[1]=='x' ){
1374 int x;
1375 zArg += 2;
1376 while( (x = hexDigitValue(zArg[0]))>=0 ){
1377 v = (v<<4) + x;
1378 zArg++;
1379 }
1380 }else{
drhc56fac72015-10-29 13:48:15 +00001381 while( ISDIGIT(zArg[0]) ){
drh53e66c32015-07-24 15:49:23 +00001382 v = v*10 + zArg[0] - '0';
1383 zArg++;
1384 }
1385 }
1386 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
1387 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1388 v *= aMult[i].iMult;
1389 break;
1390 }
1391 }
1392 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648");
1393 return (int)(isNeg? -v : v);
1394}
1395
1396/*
drh725a9c72019-01-25 13:03:38 +00001397** Return the number of "v" characters in a string. Return 0 if there
1398** are any characters in the string other than "v".
1399*/
1400static int numberOfVChar(const char *z){
1401 int N = 0;
1402 while( z[0] && z[0]=='v' ){
1403 z++;
1404 N++;
1405 }
1406 return z[0]==0 ? N : 0;
1407}
1408
1409/*
drha9542b12015-05-25 19:35:42 +00001410** Print sketchy documentation for this utility program
1411*/
1412static void showHelp(void){
1413 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
1414 printf(
1415"Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
1416"each database, checking for crashes and memory leaks.\n"
1417"Options:\n"
drha36e01a2016-08-03 13:40:54 +00001418" --cell-size-check Set the PRAGMA cell_size_check=ON\n"
1419" --dbid N Use only the database where dbid=N\n"
1420" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
1421" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
1422" --help Show this help text\n"
drh5180d682018-08-06 01:39:31 +00001423" --info Show information about SOURCE-DB w/o running tests\n"
drh672f07c2020-10-20 14:40:53 +00001424" --limit-depth N Limit expression depth to N. Default: 500\n"
1425" --limit-heap N Limit heap memory to N. Default: 100M\n"
drha36e01a2016-08-03 13:40:54 +00001426" --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
1427" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
drh5ecf9032018-05-08 12:49:53 +00001428" --load-sql ARGS... Load SQL scripts fron files into SOURCE-DB\n"
drha36e01a2016-08-03 13:40:54 +00001429" --load-db ARGS... Load template databases from files into SOURCE_DB\n"
drhe5da9352019-01-27 01:11:40 +00001430" --load-dbsql ARGS.. Load dbsqlfuzz outputs into the xsql table\n"
drha36e01a2016-08-03 13:40:54 +00001431" -m TEXT Add a description to the database\n"
1432" --native-vfs Use the native VFS for initially empty database files\n"
drh174f8552017-03-20 22:58:27 +00001433" --native-malloc Turn off MEMSYS3/5 and Lookaside\n"
drhea432ba2016-11-11 16:33:47 +00001434" --oss-fuzz Enable OSS-FUZZ testing\n"
drhbeaf5142016-12-26 00:15:56 +00001435" --prng-seed N Seed value for the PRGN inside of SQLite\n"
drh5180d682018-08-06 01:39:31 +00001436" -q|--quiet Reduced output\n"
drha36e01a2016-08-03 13:40:54 +00001437" --rebuild Rebuild and vacuum the database file\n"
1438" --result-trace Show the results of each SQL command\n"
drh672f07c2020-10-20 14:40:53 +00001439" --skip N Skip the first N test cases\n"
drhaa0696e2020-04-07 13:08:56 +00001440" --spinner Use a spinner to show progress\n"
drha36e01a2016-08-03 13:40:54 +00001441" --sqlid N Use only SQL where sqlid=N\n"
drh237f41a2020-12-21 12:14:59 +00001442" --timeout N Maximum time for any one test in N millseconds\n"
drha36e01a2016-08-03 13:40:54 +00001443" -v|--verbose Increased output. Repeat for more output.\n"
drh6e1c45e2019-12-18 13:42:04 +00001444" --vdbe-debug Activate VDBE debugging.\n"
drha9542b12015-05-25 19:35:42 +00001445 );
1446}
1447
drh3b74d032015-05-25 18:48:19 +00001448int main(int argc, char **argv){
1449 sqlite3_int64 iBegin; /* Start time of this program */
drh3b74d032015-05-25 18:48:19 +00001450 int quietFlag = 0; /* True if --quiet or -q */
1451 int verboseFlag = 0; /* True if --verbose or -v */
1452 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */
drh5ecf9032018-05-08 12:49:53 +00001453 int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */
drh3b74d032015-05-25 18:48:19 +00001454 sqlite3 *db = 0; /* The open database connection */
drhd9972ef2015-05-26 17:57:56 +00001455 sqlite3_stmt *pStmt; /* A prepared statement */
drh3b74d032015-05-25 18:48:19 +00001456 int rc; /* Result code from SQLite interface calls */
1457 Blob *pSql; /* For looping over SQL scripts */
1458 Blob *pDb; /* For looping over template databases */
1459 int i; /* Loop index for the argv[] loop */
drhe5da9352019-01-27 01:11:40 +00001460 int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */
drha9542b12015-05-25 19:35:42 +00001461 int onlySqlid = -1; /* --sqlid */
1462 int onlyDbid = -1; /* --dbid */
drh15b31282015-05-25 21:59:05 +00001463 int nativeFlag = 0; /* --native-vfs */
drh9a645862015-06-24 12:44:42 +00001464 int rebuildFlag = 0; /* --rebuild */
drhd83e2832015-06-24 14:45:44 +00001465 int vdbeLimitFlag = 0; /* --limit-vdbe */
drh5180d682018-08-06 01:39:31 +00001466 int infoFlag = 0; /* --info */
drh672f07c2020-10-20 14:40:53 +00001467 int nSkip = 0; /* --skip */
drhaa0696e2020-04-07 13:08:56 +00001468 int bSpinner = 0; /* True for --spinner */
drh94701b02015-06-24 13:25:34 +00001469 int timeoutTest = 0; /* undocumented --timeout-test flag */
drhe5c5f2c2015-05-26 00:28:08 +00001470 int runFlags = 0; /* Flags sent to runSql() */
drhd9972ef2015-05-26 17:57:56 +00001471 char *zMsg = 0; /* Add this message */
1472 int nSrcDb = 0; /* Number of source databases */
1473 char **azSrcDb = 0; /* Array of source database names */
1474 int iSrcDb; /* Loop over all source databases */
1475 int nTest = 0; /* Total number of tests performed */
1476 char *zDbName = ""; /* Appreviated name of a source database */
drh5ecf9032018-05-08 12:49:53 +00001477 const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */
drh1421d982015-05-27 03:46:18 +00001478 int cellSzCkFlag = 0; /* --cell-size-check */
drh5ecf9032018-05-08 12:49:53 +00001479 int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */
drh237f41a2020-12-21 12:14:59 +00001480 int iTimeout = 120000; /* Default 120-second timeout */
drh31999c52019-11-14 17:46:32 +00001481 int nMem = 0; /* Memory limit override */
drh362b66f2016-11-14 18:27:41 +00001482 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */
drh40e0e0d2015-09-22 18:51:17 +00001483 char *zExpDb = 0; /* Write Databases to files in this directory */
1484 char *zExpSql = 0; /* Write SQL to files in this directory */
drh6653fbe2015-11-13 20:52:49 +00001485 void *pHeap = 0; /* Heap for use by SQLite */
drhea432ba2016-11-11 16:33:47 +00001486 int ossFuzz = 0; /* enable OSS-FUZZ testing */
drh362b66f2016-11-14 18:27:41 +00001487 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */
drh174f8552017-03-20 22:58:27 +00001488 int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */
drhbeaf5142016-12-26 00:15:56 +00001489 sqlite3_vfs *pDfltVfs; /* The default VFS */
drhf2cf4122018-05-08 13:03:31 +00001490 int openFlags4Data; /* Flags for sqlite3_open_v2() */
drh237f41a2020-12-21 12:14:59 +00001491 int bTimer = 0; /* Show elapse time for each test */
drh725a9c72019-01-25 13:03:38 +00001492 int nV; /* How much to increase verbosity with -vvvv */
drh237f41a2020-12-21 12:14:59 +00001493 sqlite3_int64 tmStart; /* Start of each test */
drh3b74d032015-05-25 18:48:19 +00001494
drh39b3bcf2020-03-02 16:31:21 +00001495 registerOomSimulator();
drh8055a3e2018-11-21 14:27:34 +00001496 sqlite3_initialize();
drh3b74d032015-05-25 18:48:19 +00001497 iBegin = timeOfDay();
drh94701b02015-06-24 13:25:34 +00001498#ifdef __unix__
drha7648f02019-12-18 13:02:18 +00001499 signal(SIGALRM, signalHandler);
1500 signal(SIGSEGV, signalHandler);
1501 signal(SIGABRT, signalHandler);
drh94701b02015-06-24 13:25:34 +00001502#endif
drh3b74d032015-05-25 18:48:19 +00001503 g.zArgv0 = argv[0];
drhf2cf4122018-05-08 13:03:31 +00001504 openFlags4Data = SQLITE_OPEN_READONLY;
drh4d6fda72015-05-26 18:58:32 +00001505 zFailCode = getenv("TEST_FAILURE");
drhbeaf5142016-12-26 00:15:56 +00001506 pDfltVfs = sqlite3_vfs_find(0);
1507 inmemVfsRegister(1);
drh3b74d032015-05-25 18:48:19 +00001508 for(i=1; i<argc; i++){
1509 const char *z = argv[i];
1510 if( z[0]=='-' ){
1511 z++;
1512 if( z[0]=='-' ) z++;
drh1421d982015-05-27 03:46:18 +00001513 if( strcmp(z,"cell-size-check")==0 ){
1514 cellSzCkFlag = 1;
1515 }else
drha9542b12015-05-25 19:35:42 +00001516 if( strcmp(z,"dbid")==0 ){
1517 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001518 onlyDbid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001519 }else
drh40e0e0d2015-09-22 18:51:17 +00001520 if( strcmp(z,"export-db")==0 ){
1521 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1522 zExpDb = argv[++i];
1523 }else
drhe5da9352019-01-27 01:11:40 +00001524 if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){
drh40e0e0d2015-09-22 18:51:17 +00001525 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1526 zExpSql = argv[++i];
1527 }else
drh3b74d032015-05-25 18:48:19 +00001528 if( strcmp(z,"help")==0 ){
1529 showHelp();
1530 return 0;
1531 }else
drh5180d682018-08-06 01:39:31 +00001532 if( strcmp(z,"info")==0 ){
1533 infoFlag = 1;
1534 }else
drhbe03cc92020-01-20 14:42:09 +00001535 if( strcmp(z,"limit-depth")==0 ){
1536 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1537 depthLimit = integerValue(argv[++i]);
1538 }else
drh672f07c2020-10-20 14:40:53 +00001539 if( strcmp(z,"limit-heap")==0 ){
1540 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1541 heapLimit = integerValue(argv[++i]);
1542 }else
drh53e66c32015-07-24 15:49:23 +00001543 if( strcmp(z,"limit-mem")==0 ){
1544 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1545 nMem = integerValue(argv[++i]);
1546 }else
drhd83e2832015-06-24 14:45:44 +00001547 if( strcmp(z,"limit-vdbe")==0 ){
1548 vdbeLimitFlag = 1;
1549 }else
drh3b74d032015-05-25 18:48:19 +00001550 if( strcmp(z,"load-sql")==0 ){
drha8781d92020-02-25 20:05:58 +00001551 zInsSql = "INSERT INTO xsql(sqltext)"
1552 "VALUES(CAST(readtextfile(?1) AS text))";
drh3b74d032015-05-25 18:48:19 +00001553 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001554 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001555 break;
1556 }else
1557 if( strcmp(z,"load-db")==0 ){
1558 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
1559 iFirstInsArg = i+1;
drhf2cf4122018-05-08 13:03:31 +00001560 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drh3b74d032015-05-25 18:48:19 +00001561 break;
1562 }else
drhe5da9352019-01-27 01:11:40 +00001563 if( strcmp(z,"load-dbsql")==0 ){
drha8781d92020-02-25 20:05:58 +00001564 zInsSql = "INSERT INTO xsql(sqltext)"
1565 "VALUES(CAST(readtextfile(?1) AS text))";
drhe5da9352019-01-27 01:11:40 +00001566 iFirstInsArg = i+1;
1567 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
1568 dbSqlOnly = 1;
1569 break;
1570 }else
drhd9972ef2015-05-26 17:57:56 +00001571 if( strcmp(z,"m")==0 ){
1572 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1573 zMsg = argv[++i];
drhf2cf4122018-05-08 13:03:31 +00001574 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
drhd9972ef2015-05-26 17:57:56 +00001575 }else
drh174f8552017-03-20 22:58:27 +00001576 if( strcmp(z,"native-malloc")==0 ){
1577 nativeMalloc = 1;
1578 }else
drh15b31282015-05-25 21:59:05 +00001579 if( strcmp(z,"native-vfs")==0 ){
1580 nativeFlag = 1;
1581 }else
drhea432ba2016-11-11 16:33:47 +00001582 if( strcmp(z,"oss-fuzz")==0 ){
1583 ossFuzz = 1;
1584 }else
drhbeaf5142016-12-26 00:15:56 +00001585 if( strcmp(z,"prng-seed")==0 ){
1586 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1587 g.uRandom = atoi(argv[++i]);
1588 }else
drh3b74d032015-05-25 18:48:19 +00001589 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
1590 quietFlag = 1;
1591 verboseFlag = 0;
drha47e7092019-01-25 04:00:14 +00001592 eVerbosity = 0;
drh3b74d032015-05-25 18:48:19 +00001593 }else
drh9a645862015-06-24 12:44:42 +00001594 if( strcmp(z,"rebuild")==0 ){
1595 rebuildFlag = 1;
drhf2cf4122018-05-08 13:03:31 +00001596 openFlags4Data = SQLITE_OPEN_READWRITE;
drh9a645862015-06-24 12:44:42 +00001597 }else
drhe5c5f2c2015-05-26 00:28:08 +00001598 if( strcmp(z,"result-trace")==0 ){
1599 runFlags |= SQL_OUTPUT;
1600 }else
drh672f07c2020-10-20 14:40:53 +00001601 if( strcmp(z,"skip")==0 ){
1602 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
1603 nSkip = atoi(argv[++i]);
1604 }else
drhaa0696e2020-04-07 13:08:56 +00001605 if( strcmp(z,"spinner")==0 ){
1606 bSpinner = 1;
1607 }else
drh237f41a2020-12-21 12:14:59 +00001608 if( strcmp(z,"timer")==0 ){
1609 bTimer = 1;
1610 }else
drha9542b12015-05-25 19:35:42 +00001611 if( strcmp(z,"sqlid")==0 ){
1612 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001613 onlySqlid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +00001614 }else
drh92298632015-06-24 23:44:30 +00001615 if( strcmp(z,"timeout")==0 ){
1616 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +00001617 iTimeout = integerValue(argv[++i]);
drh92298632015-06-24 23:44:30 +00001618 }else
drh94701b02015-06-24 13:25:34 +00001619 if( strcmp(z,"timeout-test")==0 ){
1620 timeoutTest = 1;
1621#ifndef __unix__
1622 fatalError("timeout is not available on non-unix systems");
1623#endif
1624 }else
drh6e1c45e2019-12-18 13:42:04 +00001625 if( strcmp(z,"vdbe-debug")==0 ){
1626 bVdbeDebug = 1;
1627 }else
drh725a9c72019-01-25 13:03:38 +00001628 if( strcmp(z,"verbose")==0 ){
drh3b74d032015-05-25 18:48:19 +00001629 quietFlag = 0;
drh4c9d2282016-02-18 14:03:15 +00001630 verboseFlag++;
drha47e7092019-01-25 04:00:14 +00001631 eVerbosity++;
drh4c9d2282016-02-18 14:03:15 +00001632 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
drh3b74d032015-05-25 18:48:19 +00001633 }else
drh725a9c72019-01-25 13:03:38 +00001634 if( (nV = numberOfVChar(z))>=1 ){
1635 quietFlag = 0;
1636 verboseFlag += nV;
1637 eVerbosity += nV;
1638 if( verboseFlag>1 ) runFlags |= SQL_TRACE;
1639 }else
drha47e7092019-01-25 04:00:14 +00001640 if( strcmp(z,"version")==0 ){
1641 int ii;
drhed457032019-01-25 17:51:06 +00001642 const char *zz;
drha47e7092019-01-25 04:00:14 +00001643 printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid());
drhed457032019-01-25 17:51:06 +00001644 for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){
1645 printf("%s\n", zz);
drha47e7092019-01-25 04:00:14 +00001646 }
1647 return 0;
1648 }else
drh3b74d032015-05-25 18:48:19 +00001649 {
1650 fatalError("unknown option: %s", argv[i]);
1651 }
1652 }else{
drhd9972ef2015-05-26 17:57:56 +00001653 nSrcDb++;
1654 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0]));
1655 azSrcDb[nSrcDb-1] = argv[i];
drh3b74d032015-05-25 18:48:19 +00001656 }
1657 }
drhd9972ef2015-05-26 17:57:56 +00001658 if( nSrcDb==0 ) fatalError("no source database specified");
1659 if( nSrcDb>1 ){
1660 if( zMsg ){
1661 fatalError("cannot change the description of more than one database");
drh3b74d032015-05-25 18:48:19 +00001662 }
drhd9972ef2015-05-26 17:57:56 +00001663 if( zInsSql ){
1664 fatalError("cannot import into more than one database");
1665 }
drh3b74d032015-05-25 18:48:19 +00001666 }
1667
drhd9972ef2015-05-26 17:57:56 +00001668 /* Process each source database separately */
1669 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
drha7648f02019-12-18 13:02:18 +00001670 g.zDbFile = azSrcDb[iSrcDb];
drhbeaf5142016-12-26 00:15:56 +00001671 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db,
drhf2cf4122018-05-08 13:03:31 +00001672 openFlags4Data, pDfltVfs->zName);
drhd9972ef2015-05-26 17:57:56 +00001673 if( rc ){
1674 fatalError("cannot open source database %s - %s",
1675 azSrcDb[iSrcDb], sqlite3_errmsg(db));
1676 }
drh5180d682018-08-06 01:39:31 +00001677
1678 /* Print the description, if there is one */
1679 if( infoFlag ){
1680 int n;
1681 zDbName = azSrcDb[iSrcDb];
1682 i = (int)strlen(zDbName) - 1;
1683 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1684 zDbName += i;
1685 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1686 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1687 printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0));
1688 }else{
1689 printf("%s: (empty \"readme\")", zDbName);
1690 }
1691 sqlite3_finalize(pStmt);
1692 sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0);
1693 if( pStmt
1694 && sqlite3_step(pStmt)==SQLITE_ROW
1695 && (n = sqlite3_column_int(pStmt,0))>0
1696 ){
1697 printf(" - %d DBs", n);
1698 }
1699 sqlite3_finalize(pStmt);
1700 sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0);
1701 if( pStmt
1702 && sqlite3_step(pStmt)==SQLITE_ROW
1703 && (n = sqlite3_column_int(pStmt,0))>0
1704 ){
1705 printf(" - %d scripts", n);
1706 }
1707 sqlite3_finalize(pStmt);
1708 printf("\n");
1709 sqlite3_close(db);
1710 continue;
1711 }
1712
drh9a645862015-06-24 12:44:42 +00001713 rc = sqlite3_exec(db,
drhd9972ef2015-05-26 17:57:56 +00001714 "CREATE TABLE IF NOT EXISTS db(\n"
1715 " dbid INTEGER PRIMARY KEY, -- database id\n"
1716 " dbcontent BLOB -- database disk file image\n"
1717 ");\n"
1718 "CREATE TABLE IF NOT EXISTS xsql(\n"
1719 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
1720 " sqltext TEXT -- Text of SQL statements to run\n"
1721 ");"
1722 "CREATE TABLE IF NOT EXISTS readme(\n"
1723 " msg TEXT -- Human-readable description of this file\n"
1724 ");", 0, 0, 0);
1725 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db));
1726 if( zMsg ){
1727 char *zSql;
1728 zSql = sqlite3_mprintf(
1729 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg);
1730 rc = sqlite3_exec(db, zSql, 0, 0, 0);
1731 sqlite3_free(zSql);
1732 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db));
1733 }
drh362b66f2016-11-14 18:27:41 +00001734 ossFuzzThisDb = ossFuzz;
1735
1736 /* If the CONFIG(name,value) table exists, read db-specific settings
1737 ** from that table */
1738 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){
drh5ecf9032018-05-08 12:49:53 +00001739 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config",
1740 -1, &pStmt, 0);
drh362b66f2016-11-14 18:27:41 +00001741 if( rc ) fatalError("cannot prepare query of CONFIG table: %s",
1742 sqlite3_errmsg(db));
1743 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1744 const char *zName = (const char *)sqlite3_column_text(pStmt,0);
1745 if( zName==0 ) continue;
1746 if( strcmp(zName, "oss-fuzz")==0 ){
1747 ossFuzzThisDb = sqlite3_column_int(pStmt,1);
1748 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb);
1749 }
drh31999c52019-11-14 17:46:32 +00001750 if( strcmp(zName, "limit-mem")==0 ){
drh362b66f2016-11-14 18:27:41 +00001751 nMemThisDb = sqlite3_column_int(pStmt,1);
1752 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb);
drh362b66f2016-11-14 18:27:41 +00001753 }
1754 }
1755 sqlite3_finalize(pStmt);
1756 }
1757
drhd9972ef2015-05-26 17:57:56 +00001758 if( zInsSql ){
1759 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1760 readfileFunc, 0, 0);
drha8781d92020-02-25 20:05:58 +00001761 sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0,
1762 readtextfileFunc, 0, 0);
drhe5da9352019-01-27 01:11:40 +00001763 sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0,
1764 isDbSqlFunc, 0, 0);
drhd9972ef2015-05-26 17:57:56 +00001765 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
1766 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1767 zInsSql, sqlite3_errmsg(db));
1768 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
1769 if( rc ) fatalError("cannot start a transaction");
1770 for(i=iFirstInsArg; i<argc; i++){
1771 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC);
1772 sqlite3_step(pStmt);
1773 rc = sqlite3_reset(pStmt);
1774 if( rc ) fatalError("insert failed for %s", argv[i]);
drh3b74d032015-05-25 18:48:19 +00001775 }
drhd9972ef2015-05-26 17:57:56 +00001776 sqlite3_finalize(pStmt);
1777 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
drh5ecf9032018-05-08 12:49:53 +00001778 if( rc ) fatalError("cannot commit the transaction: %s",
1779 sqlite3_errmsg(db));
drhe5da9352019-01-27 01:11:40 +00001780 rebuild_database(db, dbSqlOnly);
drh3b74d032015-05-25 18:48:19 +00001781 sqlite3_close(db);
drhd9972ef2015-05-26 17:57:56 +00001782 return 0;
drh3b74d032015-05-25 18:48:19 +00001783 }
drh16f05822017-03-20 20:42:21 +00001784 rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0);
1785 if( rc ) fatalError("cannot set database to query-only");
drh40e0e0d2015-09-22 18:51:17 +00001786 if( zExpDb!=0 || zExpSql!=0 ){
1787 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1788 writefileFunc, 0, 0);
1789 if( zExpDb!=0 ){
1790 const char *zExDb =
1791 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
1792 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
1793 " FROM db WHERE ?2<0 OR dbid=?2;";
1794 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
1795 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1796 zExDb, sqlite3_errmsg(db));
1797 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
1798 SQLITE_STATIC, SQLITE_UTF8);
1799 sqlite3_bind_int(pStmt, 2, onlyDbid);
1800 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1801 printf("write db-%d (%d bytes) into %s\n",
1802 sqlite3_column_int(pStmt,1),
1803 sqlite3_column_int(pStmt,3),
1804 sqlite3_column_text(pStmt,2));
1805 }
1806 sqlite3_finalize(pStmt);
1807 }
1808 if( zExpSql!=0 ){
1809 const char *zExSql =
1810 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
1811 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
1812 " FROM xsql WHERE ?2<0 OR sqlid=?2;";
1813 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
1814 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1815 zExSql, sqlite3_errmsg(db));
1816 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
1817 SQLITE_STATIC, SQLITE_UTF8);
1818 sqlite3_bind_int(pStmt, 2, onlySqlid);
1819 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1820 printf("write sql-%d (%d bytes) into %s\n",
1821 sqlite3_column_int(pStmt,1),
1822 sqlite3_column_int(pStmt,3),
1823 sqlite3_column_text(pStmt,2));
1824 }
1825 sqlite3_finalize(pStmt);
1826 }
1827 sqlite3_close(db);
1828 return 0;
1829 }
drhd9972ef2015-05-26 17:57:56 +00001830
1831 /* Load all SQL script content and all initial database images from the
1832 ** source db
1833 */
1834 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid,
1835 &g.nSql, &g.pFirstSql);
1836 if( g.nSql==0 ) fatalError("need at least one SQL script");
1837 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid,
1838 &g.nDb, &g.pFirstDb);
1839 if( g.nDb==0 ){
1840 g.pFirstDb = safe_realloc(0, sizeof(Blob));
1841 memset(g.pFirstDb, 0, sizeof(Blob));
1842 g.pFirstDb->id = 1;
1843 g.pFirstDb->seq = 0;
1844 g.nDb = 1;
drhd83e2832015-06-24 14:45:44 +00001845 sqlFuzz = 1;
drhd9972ef2015-05-26 17:57:56 +00001846 }
1847
1848 /* Print the description, if there is one */
1849 if( !quietFlag ){
drhd9972ef2015-05-26 17:57:56 +00001850 zDbName = azSrcDb[iSrcDb];
drhe683b892016-02-15 18:47:26 +00001851 i = (int)strlen(zDbName) - 1;
drhd9972ef2015-05-26 17:57:56 +00001852 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1853 zDbName += i;
1854 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1855 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1856 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0));
1857 }
1858 sqlite3_finalize(pStmt);
1859 }
drh9a645862015-06-24 12:44:42 +00001860
1861 /* Rebuild the database, if requested */
1862 if( rebuildFlag ){
1863 if( !quietFlag ){
1864 printf("%s: rebuilding... ", zDbName);
1865 fflush(stdout);
1866 }
drhe5da9352019-01-27 01:11:40 +00001867 rebuild_database(db, 0);
drh9a645862015-06-24 12:44:42 +00001868 if( !quietFlag ) printf("done\n");
1869 }
drhd9972ef2015-05-26 17:57:56 +00001870
1871 /* Close the source database. Verify that no SQLite memory allocations are
1872 ** outstanding.
1873 */
1874 sqlite3_close(db);
1875 if( sqlite3_memory_used()>0 ){
1876 fatalError("SQLite has memory in use before the start of testing");
1877 }
drh53e66c32015-07-24 15:49:23 +00001878
1879 /* Limit available memory, if requested */
drh174f8552017-03-20 22:58:27 +00001880 sqlite3_shutdown();
drh39b3bcf2020-03-02 16:31:21 +00001881
drh31999c52019-11-14 17:46:32 +00001882 if( nMemThisDb>0 && nMem==0 ){
1883 if( !nativeMalloc ){
1884 pHeap = realloc(pHeap, nMemThisDb);
1885 if( pHeap==0 ){
1886 fatalError("failed to allocate %d bytes of heap memory", nMem);
1887 }
1888 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128);
1889 }else{
1890 sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb);
drh53e66c32015-07-24 15:49:23 +00001891 }
drh31999c52019-11-14 17:46:32 +00001892 }else{
1893 sqlite3_hard_heap_limit64(0);
drh53e66c32015-07-24 15:49:23 +00001894 }
drh174f8552017-03-20 22:58:27 +00001895
1896 /* Disable lookaside with the --native-malloc option */
1897 if( nativeMalloc ){
1898 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
1899 }
drhd9972ef2015-05-26 17:57:56 +00001900
drhbeaf5142016-12-26 00:15:56 +00001901 /* Reset the in-memory virtual filesystem */
drhd9972ef2015-05-26 17:57:56 +00001902 formatVfs();
drhd9972ef2015-05-26 17:57:56 +00001903
1904 /* Run a test using each SQL script against each database.
1905 */
drhaa0696e2020-04-07 13:08:56 +00001906 if( !verboseFlag && !quietFlag && !bSpinner ) printf("%s:", zDbName);
drhd9972ef2015-05-26 17:57:56 +00001907 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){
drh237f41a2020-12-21 12:14:59 +00001908 tmStart = timeOfDay();
drha47e7092019-01-25 04:00:14 +00001909 if( isDbSql(pSql->a, pSql->sz) ){
1910 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id);
drhaa0696e2020-04-07 13:08:56 +00001911 if( bSpinner ){
1912 int nTotal =g.nSql;
1913 int idx = pSql->seq;
1914 printf("\r%s: %d/%d ", zDbName, idx, nTotal);
1915 fflush(stdout);
1916 }else if( verboseFlag ){
drha47e7092019-01-25 04:00:14 +00001917 printf("%s\n", g.zTestName);
1918 fflush(stdout);
1919 }else if( !quietFlag ){
1920 static int prevAmt = -1;
1921 int idx = pSql->seq;
1922 int amt = idx*10/(g.nSql);
1923 if( amt!=prevAmt ){
1924 printf(" %d%%", amt*10);
1925 fflush(stdout);
1926 prevAmt = amt;
1927 }
1928 }
drh672f07c2020-10-20 14:40:53 +00001929 if( nSkip>0 ){
1930 nSkip--;
1931 }else{
drh237f41a2020-12-21 12:14:59 +00001932 runCombinedDbSqlInput(pSql->a, pSql->sz, iTimeout);
drh672f07c2020-10-20 14:40:53 +00001933 }
drha47e7092019-01-25 04:00:14 +00001934 nTest++;
drh237f41a2020-12-21 12:14:59 +00001935 if( bTimer ){
1936 sqlite3_int64 tmEnd = timeOfDay();
1937 printf("%lld %s\n", tmEnd - tmStart, g.zTestName);
1938 }
drha47e7092019-01-25 04:00:14 +00001939 g.zTestName[0] = 0;
drh39b3bcf2020-03-02 16:31:21 +00001940 disableOom();
drha47e7092019-01-25 04:00:14 +00001941 continue;
1942 }
drhd9972ef2015-05-26 17:57:56 +00001943 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){
1944 int openFlags;
1945 const char *zVfs = "inmem";
1946 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d",
1947 pSql->id, pDb->id);
drhaa0696e2020-04-07 13:08:56 +00001948 if( bSpinner ){
1949 int nTotal = g.nDb*g.nSql;
1950 int idx = pSql->seq*g.nDb + pDb->id - 1;
1951 printf("\r%s: %d/%d ", zDbName, idx, nTotal);
1952 fflush(stdout);
1953 }else if( verboseFlag ){
drhd9972ef2015-05-26 17:57:56 +00001954 printf("%s\n", g.zTestName);
1955 fflush(stdout);
1956 }else if( !quietFlag ){
1957 static int prevAmt = -1;
1958 int idx = pSql->seq*g.nDb + pDb->id - 1;
1959 int amt = idx*10/(g.nDb*g.nSql);
1960 if( amt!=prevAmt ){
1961 printf(" %d%%", amt*10);
1962 fflush(stdout);
1963 prevAmt = amt;
1964 }
1965 }
drh672f07c2020-10-20 14:40:53 +00001966 if( nSkip>0 ){
1967 nSkip--;
1968 continue;
1969 }
drhd9972ef2015-05-26 17:57:56 +00001970 createVFile("main.db", pDb->sz, pDb->a);
drhbeaf5142016-12-26 00:15:56 +00001971 sqlite3_randomness(0,0);
drh362b66f2016-11-14 18:27:41 +00001972 if( ossFuzzThisDb ){
drhea432ba2016-11-11 16:33:47 +00001973#ifndef SQLITE_OSS_FUZZ
drh5ecf9032018-05-08 12:49:53 +00001974 fatalError("--oss-fuzz not supported: recompile"
1975 " with -DSQLITE_OSS_FUZZ");
drhea432ba2016-11-11 16:33:47 +00001976#else
1977 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t);
1978 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz);
drh78057352015-06-24 23:17:35 +00001979#endif
drhea432ba2016-11-11 16:33:47 +00001980 }else{
1981 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
1982 if( nativeFlag && pDb->sz==0 ){
1983 openFlags |= SQLITE_OPEN_MEMORY;
1984 zVfs = 0;
1985 }
1986 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs);
1987 if( rc ) fatalError("cannot open inmem database");
drhdfcfff62016-12-26 12:25:19 +00001988 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000);
1989 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50);
drhea432ba2016-11-11 16:33:47 +00001990 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags);
drh237f41a2020-12-21 12:14:59 +00001991 setAlarm((iTimeout+999)/1000);
drhea432ba2016-11-11 16:33:47 +00001992#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1993 if( sqlFuzz || vdbeLimitFlag ){
drh5ecf9032018-05-08 12:49:53 +00001994 sqlite3_progress_handler(db, 100000, progressHandler,
1995 &vdbeLimitFlag);
drhea432ba2016-11-11 16:33:47 +00001996 }
1997#endif
drhe6e96b12019-08-02 21:03:24 +00001998#ifdef SQLITE_TESTCTRL_PRNG_SEED
drh2e6d83b2019-08-03 01:39:20 +00001999 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
drhe6e96b12019-08-02 21:03:24 +00002000#endif
drh6e1c45e2019-12-18 13:42:04 +00002001 if( bVdbeDebug ){
2002 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
2003 }
drhea432ba2016-11-11 16:33:47 +00002004 do{
2005 runSql(db, (char*)pSql->a, runFlags);
2006 }while( timeoutTest );
2007 setAlarm(0);
drh174f8552017-03-20 22:58:27 +00002008 sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
drhea432ba2016-11-11 16:33:47 +00002009 sqlite3_close(db);
2010 }
drh174f8552017-03-20 22:58:27 +00002011 if( sqlite3_memory_used()>0 ){
2012 fatalError("memory leak: %lld bytes outstanding",
2013 sqlite3_memory_used());
2014 }
drhd9972ef2015-05-26 17:57:56 +00002015 reformatVfs();
2016 nTest++;
drh237f41a2020-12-21 12:14:59 +00002017 if( bTimer ){
2018 sqlite3_int64 tmEnd = timeOfDay();
2019 printf("%lld %s\n", tmEnd - tmStart, g.zTestName);
2020 }
drhd9972ef2015-05-26 17:57:56 +00002021 g.zTestName[0] = 0;
drh4d6fda72015-05-26 18:58:32 +00002022
2023 /* Simulate an error if the TEST_FAILURE environment variable is "5".
2024 ** This is used to verify that automated test script really do spot
2025 ** errors that occur in this test program.
2026 */
2027 if( zFailCode ){
2028 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
2029 fatalError("simulated failure");
2030 }else if( zFailCode[0]!=0 ){
2031 /* If TEST_FAILURE is something other than 5, just exit the test
2032 ** early */
2033 printf("\nExit early due to TEST_FAILURE being set\n");
2034 iSrcDb = nSrcDb-1;
2035 goto sourcedb_cleanup;
2036 }
2037 }
drhd9972ef2015-05-26 17:57:56 +00002038 }
2039 }
drhaa0696e2020-04-07 13:08:56 +00002040 if( bSpinner ){
2041 printf("\n");
2042 }else if( !quietFlag && !verboseFlag ){
drhd9972ef2015-05-26 17:57:56 +00002043 printf(" 100%% - %d tests\n", g.nDb*g.nSql);
2044 }
2045
2046 /* Clean up at the end of processing a single source database
2047 */
drh4d6fda72015-05-26 18:58:32 +00002048 sourcedb_cleanup:
drhd9972ef2015-05-26 17:57:56 +00002049 blobListFree(g.pFirstSql);
2050 blobListFree(g.pFirstDb);
2051 reformatVfs();
2052
2053 } /* End loop over all source databases */
drh3b74d032015-05-25 18:48:19 +00002054
2055 if( !quietFlag ){
2056 sqlite3_int64 iElapse = timeOfDay() - iBegin;
drhd9972ef2015-05-26 17:57:56 +00002057 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
2058 "SQLite %s %s\n",
2059 nTest, (int)(iElapse/1000), (int)(iElapse%1000),
drh3b74d032015-05-25 18:48:19 +00002060 sqlite3_libversion(), sqlite3_sourceid());
2061 }
drhf74d35b2015-05-27 18:19:50 +00002062 free(azSrcDb);
drh6653fbe2015-11-13 20:52:49 +00002063 free(pHeap);
drh3b74d032015-05-25 18:48:19 +00002064 return 0;
2065}