4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This is a utility program designed to aid running regressions tests on
14 ** the SQLite library using data from external fuzzers.
16 ** This program reads content from an SQLite database file with the following
20 ** dbid INTEGER PRIMARY KEY, -- database id
21 ** dbcontent BLOB -- database disk file image
24 ** sqlid INTEGER PRIMARY KEY, -- SQL script id
25 ** sqltext TEXT -- Text of SQL statements to run
27 ** CREATE TABLE IF NOT EXISTS readme(
28 ** msg TEXT -- Human-readable description of this test collection
31 ** For each database file in the DB table, the SQL text in the XSQL table
32 ** 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.
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.
43 ** This program also includes some command-line options to help with
44 ** creation and maintenance of the source content database. The command
46 ** ./fuzzcheck database.db --load-sql FILE...
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:
53 ** ./fuzzcheck new.db --load-sql *.sql
54 ** ./fuzzcheck new.db --load-db *.db
55 ** ./fuzzcheck new.db -m 'New test cases'
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.
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.
66 ** DBSQLFUZZ: (Added 2020-02-25)
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
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.
88 #define ISSPACE(X) isspace((unsigned char)(X))
89 #define ISDIGIT(X) isdigit((unsigned char)(X))
98 #if !defined(_MSC_VER)
102 #if defined(_MSC_VER)
103 typedef unsigned char uint8_t;
107 ** Files in the virtual file system.
109 typedef struct VFile 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() */
116 typedef struct VHandle VHandle
;
118 sqlite3_file base
; /* Base class. Must be first */
119 VFile
*pVFile
; /* The underlying file */
123 ** The value of a database file template, or of an SQL script
125 typedef struct Blob Blob
;
127 Blob
*pNext
; /* Next in a list */
128 int id
; /* Id of this Blob */
129 int seq
; /* Sequence number */
130 int sz
; /* Size of this Blob in bytes */
131 unsigned char a
[1]; /* Blob content. Extra space allocated as needed. */
135 ** Maximum number of files in the in-memory virtual filesystem.
140 ** Maximum allowed file size
142 #define MX_FILE_SZ 10000000
145 ** All global variables are gathered into the "g" singleton.
147 static struct GlobalVars
{
148 const char *zArgv0
; /* Name of program */
149 const char *zDbFile
; /* Name of database file */
150 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 */
155 unsigned int uRandom
; /* Seed for the SQLite PRNG */
156 char zTestName
[100]; /* Name of current test */
160 ** Include the external vt02.c module, if requested by compile-time
168 ** Print an error message and quit.
170 static void fatalError(const char *zFormat
, ...){
172 fprintf(stderr
, "%s", g
.zArgv0
);
173 if( g
.zDbFile
) fprintf(stderr
, " %s", g
.zDbFile
);
174 if( g
.zTestName
[0] ) fprintf(stderr
, " (%s)", g
.zTestName
);
175 fprintf(stderr
, ": ");
176 va_start(ap
, zFormat
);
177 vfprintf(stderr
, zFormat
, ap
);
179 fprintf(stderr
, "\n");
187 static void signalHandler(int signum
){
189 if( signum
==SIGABRT
){
191 }else if( signum
==SIGALRM
){
193 }else if( signum
==SIGSEGV
){
203 ** Set the an alarm to go off after N seconds. Disable the alarm
206 static void setAlarm(int N
){
214 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
216 ** This an SQL progress handler. After an SQL statement has run for
217 ** many steps, we want to interrupt it. This guards against infinite
218 ** loops from recursive common table expressions.
220 ** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
221 ** In that case, hitting the progress handler is a fatal error.
223 static int progressHandler(void *pVdbeLimitFlag
){
224 if( *(int*)pVdbeLimitFlag
) fatalError("too many VDBE cycles");
230 ** Reallocate memory. Show an error and quit if unable.
232 static void *safe_realloc(void *pOld
, int szNew
){
233 void *pNew
= realloc(pOld
, szNew
<=0 ? 1 : szNew
);
234 if( pNew
==0 ) fatalError("unable to realloc for %d bytes", szNew
);
239 ** Initialize the virtual file system.
241 static void formatVfs(void){
243 for(i
=0; i
<MX_FILE
; i
++){
245 g
.aFile
[i
].zFilename
= 0;
253 ** Erase all information in the virtual file system.
255 static void reformatVfs(void){
257 for(i
=0; i
<MX_FILE
; i
++){
258 if( g
.aFile
[i
].sz
<0 ) continue;
259 if( g
.aFile
[i
].zFilename
){
260 free(g
.aFile
[i
].zFilename
);
261 g
.aFile
[i
].zFilename
= 0;
263 if( g
.aFile
[i
].nRef
>0 ){
264 fatalError("file %d still open. nRef=%d", i
, g
.aFile
[i
].nRef
);
274 ** Find a VFile by name
276 static VFile
*findVFile(const char *zName
){
278 if( zName
==0 ) return 0;
279 for(i
=0; i
<MX_FILE
; i
++){
280 if( g
.aFile
[i
].zFilename
==0 ) continue;
281 if( strcmp(g
.aFile
[i
].zFilename
, zName
)==0 ) return &g
.aFile
[i
];
287 ** Find a VFile by name. Create it if it does not already exist and
288 ** initialize it to the size and content given.
290 ** Return NULL only if the filesystem is full.
292 static VFile
*createVFile(const char *zName
, int sz
, unsigned char *pData
){
293 VFile
*pNew
= findVFile(zName
);
295 if( pNew
) return pNew
;
296 for(i
=0; i
<MX_FILE
&& g
.aFile
[i
].sz
>=0; i
++){}
297 if( i
>=MX_FILE
) return 0;
300 int nName
= (int)strlen(zName
)+1;
301 pNew
->zFilename
= safe_realloc(0, nName
);
302 memcpy(pNew
->zFilename
, zName
, nName
);
308 pNew
->a
= safe_realloc(0, sz
);
309 if( sz
>0 ) memcpy(pNew
->a
, pData
, sz
);
313 /* Return true if the line is all zeros */
314 static int allZero(unsigned char *aLine
){
316 for(i
=0; i
<16 && aLine
[i
]==0; i
++){}
321 ** Render a database and query as text that can be input into
324 static void renderDbSqlForCLI(
325 FILE *out
, /* Write to this file */
326 const char *zFile
, /* Name of the database file */
327 unsigned char *aDb
, /* Database content */
328 int nDb
, /* Number of bytes in aDb[] */
329 unsigned char *zSql
, /* SQL content */
330 int nSql
/* Bytes of SQL */
332 fprintf(out
, ".print ******* %s *******\n", zFile
);
334 int i
, j
; /* Loop counters */
335 int pgsz
; /* Size of each page */
336 int lastPage
= 0; /* Last page number shown */
337 int iPage
; /* Current page number */
338 unsigned char *aLine
; /* Single line to display */
339 unsigned char buf
[16]; /* Fake line */
340 unsigned char bShow
[256]; /* Characters ok to display */
342 memset(bShow
, '.', sizeof(bShow
));
343 for(i
=' '; i
<='~'; i
++){
344 if( i
!='{' && i
!='}' && i
!='"' && i
!='\\' ) bShow
[i
] = i
;
346 pgsz
= (aDb
[16]<<8) | aDb
[17];
347 if( pgsz
==0 ) pgsz
= 65536;
348 if( pgsz
<512 || (pgsz
&(pgsz
-1))!=0 ) pgsz
= 4096;
349 fprintf(out
,".open --hexdb\n");
350 fprintf(out
,"| size %d pagesize %d filename %s\n",nDb
,pgsz
,zFile
);
351 for(i
=0; i
<nDb
; i
+= 16){
353 memset(buf
, 0, sizeof(buf
));
354 memcpy(buf
, aDb
+i
, nDb
-i
);
359 if( allZero(aLine
) ) continue;
361 if( lastPage
!=iPage
){
362 fprintf(out
,"| page %d offset %d\n", iPage
, (iPage
-1)*pgsz
);
365 fprintf(out
,"| %5d:", i
-(iPage
-1)*pgsz
);
366 for(j
=0; j
<16; j
++) fprintf(out
," %02x", aLine
[j
]);
369 unsigned char c
= (unsigned char)aLine
[j
];
370 fputc( bShow
[c
], stdout
);
374 fprintf(out
,"| end %s\n", zFile
);
376 fprintf(out
,".open :memory:\n");
378 fprintf(out
,".testctrl prng_seed 1 db\n");
379 fprintf(out
,".testctrl internal_functions\n");
380 fprintf(out
,"%.*s", nSql
, zSql
);
381 if( nSql
>0 && zSql
[nSql
-1]!='\n' ) fprintf(out
, "\n");
385 ** Read the complete content of a file into memory. Add a 0x00 terminator
386 ** and return a pointer to the result.
388 ** The file content is held in memory obtained from sqlite_malloc64() which
389 ** should be freed by the caller.
391 static char *readFile(const char *zFilename
, long *sz
){
397 if( zFilename
==0 ) return 0;
398 in
= fopen(zFilename
, "rb");
399 if( in
==0 ) return 0;
400 fseek(in
, 0, SEEK_END
);
401 *sz
= nIn
= ftell(in
);
403 pBuf
= sqlite3_malloc64( nIn
+1 );
404 if( pBuf
&& 1==fread(pBuf
, nIn
, 1, in
) ){
417 ** Implementation of the "readfile(X)" SQL function. The entire content
418 ** of the file named X is read and returned as a BLOB. NULL is returned
419 ** if the file does not exist or is unreadable.
421 static void readfileFunc(
422 sqlite3_context
*context
,
428 const char *zName
= (const char*)sqlite3_value_text(argv
[0]);
430 if( zName
==0 ) return;
431 pBuf
= readFile(zName
, &nIn
);
433 sqlite3_result_blob(context
, pBuf
, nIn
, sqlite3_free
);
438 ** Implementation of the "readtextfile(X)" SQL function. The text content
439 ** of the file named X through the end of the file or to the first \000
440 ** character, whichever comes first, is read and returned as TEXT. NULL
441 ** is returned if the file does not exist or is unreadable.
443 static void readtextfileFunc(
444 sqlite3_context
*context
,
453 zName
= (const char*)sqlite3_value_text(argv
[0]);
454 if( zName
==0 ) return;
455 in
= fopen(zName
, "rb");
457 fseek(in
, 0, SEEK_END
);
460 pBuf
= sqlite3_malloc64( nIn
+1 );
461 if( pBuf
&& 1==fread(pBuf
, nIn
, 1, in
) ){
463 sqlite3_result_text(context
, pBuf
, -1, sqlite3_free
);
471 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y
472 ** is written into file X. The number of bytes written is returned. Or
473 ** NULL is returned if something goes wrong, such as being unable to open
474 ** file X for writing.
476 static void writefileFunc(
477 sqlite3_context
*context
,
487 zFile
= (const char*)sqlite3_value_text(argv
[0]);
488 if( zFile
==0 ) return;
489 out
= fopen(zFile
, "wb");
491 z
= (const char*)sqlite3_value_blob(argv
[1]);
495 rc
= fwrite(z
, 1, sqlite3_value_bytes(argv
[1]), out
);
498 sqlite3_result_int64(context
, rc
);
503 ** Load a list of Blob objects from the database
505 static void blobListLoadFromDb(
506 sqlite3
*db
, /* Read from this database */
507 const char *zSql
, /* Query used to extract the blobs */
508 int onlyId
, /* Only load where id is this value */
509 int *pN
, /* OUT: Write number of blobs loaded here */
510 Blob
**ppList
/* OUT: Write the head of the blob list here */
520 z2
= sqlite3_mprintf("%s WHERE rowid=%d", zSql
, onlyId
);
522 z2
= sqlite3_mprintf("%s", zSql
);
524 rc
= sqlite3_prepare_v2(db
, z2
, -1, &pStmt
, 0);
526 if( rc
) fatalError("%s", sqlite3_errmsg(db
));
529 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
530 int sz
= sqlite3_column_bytes(pStmt
, 1);
531 Blob
*pNew
= safe_realloc(0, sizeof(*pNew
)+sz
);
532 pNew
->id
= sqlite3_column_int(pStmt
, 0);
536 memcpy(pNew
->a
, sqlite3_column_blob(pStmt
,1), sz
);
541 sqlite3_finalize(pStmt
);
543 *ppList
= head
.pNext
;
547 ** Free a list of Blob objects
549 static void blobListFree(Blob
*p
){
558 /* Return the current wall-clock time
560 ** The number of milliseconds since the julian epoch.
561 ** 1907-01-01 00:00:00 -> 210866716800000
562 ** 2021-01-01 00:00:00 -> 212476176000000
564 static sqlite3_int64
timeOfDay(void){
565 static sqlite3_vfs
*clockVfs
= 0;
568 clockVfs
= sqlite3_vfs_find(0);
569 if( clockVfs
==0 ) return 0;
571 if( clockVfs
->iVersion
>=1 && clockVfs
->xCurrentTimeInt64
!=0 ){
572 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
575 clockVfs
->xCurrentTime(clockVfs
, &r
);
576 t
= (sqlite3_int64
)(r
*86400000.0);
581 /***************************************************************************
582 ** Code to process combined database+SQL scripts generated by the
586 /* An instance of the following object is passed by pointer as the
587 ** client data to various callbacks.
589 typedef struct FuzzCtx
{
590 sqlite3
*db
; /* The database connection */
591 sqlite3_int64 iCutoffTime
; /* Stop processing at this time. */
592 sqlite3_int64 iLastCb
; /* Time recorded for previous progress callback */
593 sqlite3_int64 mxInterval
; /* Longest interval between two progress calls */
594 unsigned nCb
; /* Number of progress callbacks */
595 unsigned mxCb
; /* Maximum number of progress callbacks allowed */
596 unsigned execCnt
; /* Number of calls to the sqlite3_exec callback */
597 int timeoutHit
; /* True when reaching a timeout */
600 /* Verbosity level for the dbsqlfuzz test runner */
601 static int eVerbosity
= 0;
603 /* True to activate PRAGMA vdbe_debug=on */
604 static int bVdbeDebug
= 0;
606 /* Timeout for each fuzzing attempt, in milliseconds */
607 static int giTimeout
= 10000; /* Defaults to 10 seconds */
609 /* Maximum number of progress handler callbacks */
610 static unsigned int mxProgressCb
= 2000;
612 /* Maximum string length in SQLite */
613 static int lengthLimit
= 1000000;
615 /* Maximum expression depth */
616 static int depthLimit
= 500;
618 /* Limit on the amount of heap memory that can be used */
619 static sqlite3_int64 heapLimit
= 100000000;
621 /* Maximum byte-code program length in SQLite */
622 static int vdbeOpLimit
= 25000;
624 /* Maximum size of the in-memory database */
625 static sqlite3_int64 maxDbSize
= 104857600;
626 /* OOM simulation parameters */
627 static unsigned int oomCounter
= 0; /* Simulate OOM when equals 1 */
628 static unsigned int oomRepeat
= 0; /* Number of OOMs in a row */
629 static void*(*defaultMalloc
)(int) = 0; /* The low-level malloc routine */
631 /* This routine is called when a simulated OOM occurs. It is broken
632 ** out as a separate routine to make it easy to set a breakpoint on
637 printf("Simulated OOM fault\n");
646 /* This routine is a replacement malloc() that is used to simulate
647 ** Out-Of-Memory (OOM) errors for testing purposes.
649 static void *oomMalloc(int nByte
){
658 return defaultMalloc(nByte
);
661 /* Register the OOM simulator. This must occur before any memory
663 static void registerOomSimulator(void){
664 sqlite3_mem_methods mem
;
666 sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &mem
);
667 defaultMalloc
= mem
.xMalloc
;
668 mem
.xMalloc
= oomMalloc
;
669 sqlite3_config(SQLITE_CONFIG_MALLOC
, &mem
);
672 /* Turn off any pending OOM simulation */
673 static void disableOom(void){
679 ** Translate a single byte of Hex into an integer.
680 ** This routine only works if h really is a valid hexadecimal
681 ** character: 0..9a..fA..F
683 static unsigned char hexToInt(unsigned int h
){
685 h
+= 9*(1&~(h
>>4)); /* EBCDIC */
687 h
+= 9*(1&(h
>>6)); /* ASCII */
693 ** The first character of buffer zIn[0..nIn-1] is a '['. This routine
694 ** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it
695 ** does it makes corresponding changes to the *pK value and *pI value
696 ** and returns true. If the input buffer does not match the patterns,
697 ** no changes are made to either *pK or *pI and this routine returns false.
700 const unsigned char *zIn
, /* Text input */
701 int nIn
, /* Bytes of input */
702 unsigned int *pK
, /* half-byte cursor to adjust */
703 unsigned int *pI
/* Input index to adjust */
708 for(i
=1; i
<nIn
&& (c
= zIn
[i
])!=']'; i
++){
709 if( !isxdigit(c
) ) return 0;
710 k
= k
*16 + hexToInt(c
);
712 if( i
==nIn
) return 0;
719 ** Decode the text starting at zIn into a binary database file.
720 ** The maximum length of zIn is nIn bytes. Store the binary database
721 ** file in space obtained from sqlite3_malloc().
723 ** Return the number of bytes of zIn consumed. Or return -1 if there
724 ** is an error. One potential error is that the recipe specifies a
725 ** database file larger than MX_FILE_SZ bytes.
729 static int decodeDatabase(
730 const unsigned char *zIn
, /* Input text to be decoded */
731 int nIn
, /* Bytes of input text */
732 unsigned char **paDecode
, /* OUT: decoded database file */
733 int *pnDecode
/* OUT: Size of decoded database */
735 unsigned char *a
, *aNew
; /* Database under construction */
736 int mx
= 0; /* Current size of the database */
737 sqlite3_uint64 nAlloc
= 4096; /* Space allocated in a[] */
738 unsigned int i
; /* Next byte of zIn[] to read */
739 unsigned int j
; /* Temporary integer */
740 unsigned int k
; /* half-byte cursor index for output */
741 unsigned int n
; /* Number of bytes of input */
743 if( nIn
<4 ) return -1;
744 n
= (unsigned int)nIn
;
745 a
= sqlite3_malloc64( nAlloc
);
747 fprintf(stderr
, "Out of memory!\n");
750 memset(a
, 0, (size_t)nAlloc
);
751 for(i
=k
=0; i
<n
; i
++){
752 unsigned char c
= (unsigned char)zIn
[i
];
761 sqlite3_uint64 newSize
;
762 if( nAlloc
==MX_FILE_SZ
|| j
>=MX_FILE_SZ
){
764 fprintf(stderr
, "Input database too big: max %d bytes\n",
772 newSize
= (j
+4096)&~4095;
774 if( newSize
>MX_FILE_SZ
){
779 newSize
= MX_FILE_SZ
;
781 aNew
= sqlite3_realloc64( a
, newSize
);
787 assert( newSize
> nAlloc
);
788 memset(a
+nAlloc
, 0, (size_t)(newSize
- nAlloc
));
791 if( j
>=(unsigned)mx
){
792 mx
= (j
+ 4095)&~4095;
793 if( mx
>MX_FILE_SZ
) mx
= MX_FILE_SZ
;
798 }else if( zIn
[i
]=='[' && i
<n
-3 && isOffset(zIn
+i
, nIn
-i
, &k
, &i
) ){
800 }else if( zIn
[i
]=='\n' && i
<n
-4 && memcmp(zIn
+i
,"\n--\n",4)==0 ){
811 ** Progress handler callback.
813 ** The argument is the cutoff-time after which all processing should
814 ** stop. So return non-zero if the cut-off time is exceeded.
816 static int progress_handler(void *pClientData
) {
817 FuzzCtx
*p
= (FuzzCtx
*)pClientData
;
818 sqlite3_int64 iNow
= timeOfDay();
819 int rc
= iNow
>=p
->iCutoffTime
;
820 sqlite3_int64 iDiff
= iNow
- p
->iLastCb
;
821 /* printf("time-remaining: %lld\n", p->iCutoffTime - iNow); */
822 if( iDiff
> p
->mxInterval
) p
->mxInterval
= iDiff
;
824 if( rc
==0 && p
->mxCb
>0 && p
->mxCb
<=p
->nCb
) rc
= 1;
825 if( rc
&& !p
->timeoutHit
&& eVerbosity
>=2 ){
826 printf("Timeout on progress callback %d\n", p
->nCb
);
834 ** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
835 ** "PRAGMA parser_trace" since they can dramatically increase the
836 ** amount of output without actually testing anything useful.
838 ** Also block ATTACH if attaching a file from the filesystem.
840 static int block_troublesome_sql(
852 if( eCode
==SQLITE_PRAGMA
){
853 if( sqlite3_stricmp("busy_timeout",zArg1
)==0
854 && (zArg2
==0 || strtoll(zArg2
,0,0)>100 || strtoll(zArg2
,0,10)>100)
857 }else if( eVerbosity
==0 ){
858 if( sqlite3_strnicmp("vdbe_", zArg1
, 5)==0
859 || sqlite3_stricmp("parser_trace", zArg1
)==0
860 || sqlite3_stricmp("temp_store_directory", zArg1
)==0
864 }else if( sqlite3_stricmp("oom",zArg1
)==0
865 && zArg2
!=0 && zArg2
[0]!=0 ){
866 oomCounter
= atoi(zArg2
);
868 }else if( eCode
==SQLITE_ATTACH
){
869 /* Deny the ATTACH if it is attaching anything other than an in-memory
871 if( zArg1
==0 ) return SQLITE_DENY
;
872 if( strcmp(zArg1
,":memory:")==0 ) return SQLITE_OK
;
873 if( sqlite3_strglob("file:*[?]vfs=memdb", zArg1
)==0
874 && sqlite3_strglob("file:*[^/a-zA-Z0-9_.]*[?]vfs=memdb", zArg1
)!=0
886 static int runDbSql(sqlite3
*db
, const char *zSql
){
889 while( isspace(zSql
[0]&0x7f) ) zSql
++;
890 if( zSql
[0]==0 ) return SQLITE_OK
;
892 printf("RUNNING-SQL: [%s]\n", zSql
);
895 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
897 while( (rc
= sqlite3_step(pStmt
))==SQLITE_ROW
){
900 for(j
=0; j
<sqlite3_column_count(pStmt
); j
++){
902 switch( sqlite3_column_type(pStmt
, j
) ){
909 printf("%s", sqlite3_column_text(pStmt
, j
));
913 int n
= sqlite3_column_bytes(pStmt
, j
);
915 const unsigned char *a
;
916 a
= (const unsigned char*)sqlite3_column_blob(pStmt
, j
);
919 printf("%02x", a
[i
]);
925 int n
= sqlite3_column_bytes(pStmt
, j
);
927 const unsigned char *a
;
928 a
= (const unsigned char*)sqlite3_column_blob(pStmt
, j
);
944 } /* End if( eVerbosity>=5 ) */
945 } /* End while( SQLITE_ROW */
946 if( rc
!=SQLITE_DONE
&& eVerbosity
>=4 ){
947 printf("SQL-ERROR: (%d) %s\n", rc
, sqlite3_errmsg(db
));
950 }else if( eVerbosity
>=4 ){
951 printf("SQL-ERROR (%d): %s\n", rc
, sqlite3_errmsg(db
));
953 } /* End if( SQLITE_OK ) */
954 return sqlite3_finalize(pStmt
);
957 /* Invoke this routine to run a single test case */
958 int runCombinedDbSqlInput(
959 const uint8_t *aData
, /* Combined DB+SQL content */
960 size_t nByte
, /* Size of aData in bytes */
961 int iTimeout
, /* Use this timeout */
962 int bScript
, /* If true, just render CLI output */
963 int iSqlId
/* SQL identifier */
965 int rc
; /* SQLite API return value */
966 int iSql
; /* Index in aData[] of start of SQL */
967 unsigned char *aDb
= 0; /* Decoded database content */
968 int nDb
= 0; /* Size of the decoded database */
969 int i
; /* Loop counter */
970 int j
; /* Start of current SQL statement */
971 char *zSql
= 0; /* SQL text to run */
972 int nSql
; /* Bytes of SQL text */
973 FuzzCtx cx
; /* Fuzzing context */
975 if( nByte
<10 ) return 0;
976 if( sqlite3_initialize() ) return 0;
977 if( sqlite3_memory_used()!=0 ){
980 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT
, &nAlloc
, &nNotUsed
, 0);
981 fprintf(stderr
,"memory leak prior to test start:"
982 " %lld bytes in %d allocations\n",
983 sqlite3_memory_used(), nAlloc
);
986 memset(&cx
, 0, sizeof(cx
));
987 iSql
= decodeDatabase((unsigned char*)aData
, (int)nByte
, &aDb
, &nDb
);
988 if( iSql
<0 ) return 0;
989 nSql
= (int)(nByte
- iSql
);
992 sqlite3_snprintf(sizeof(zName
),zName
,"dbsql%06d.db",iSqlId
);
993 renderDbSqlForCLI(stdout
, zName
, aDb
, nDb
,
994 (unsigned char*)(aData
+iSql
), nSql
);
1000 "****** %d-byte input, %d-byte database, %d-byte script "
1001 "******\n", (int)nByte
, nDb
, nSql
);
1004 rc
= sqlite3_open(0, &cx
.db
);
1010 sqlite3_exec(cx
.db
, "PRAGMA vdbe_debug=ON", 0, 0, 0);
1013 /* Invoke the progress handler frequently to check to see if we
1014 ** are taking too long. The progress handler will return true
1015 ** (which will block further processing) if more than giTimeout seconds have
1016 ** elapsed since the start of the test.
1018 cx
.iLastCb
= timeOfDay();
1019 cx
.iCutoffTime
= cx
.iLastCb
+ (iTimeout
<giTimeout
? iTimeout
: giTimeout
);
1020 cx
.mxCb
= mxProgressCb
;
1021 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1022 sqlite3_progress_handler(cx
.db
, 10, progress_handler
, (void*)&cx
);
1025 /* Set a limit on the maximum size of a prepared statement, and the
1026 ** maximum length of a string or blob */
1027 if( vdbeOpLimit
>0 ){
1028 sqlite3_limit(cx
.db
, SQLITE_LIMIT_VDBE_OP
, vdbeOpLimit
);
1030 if( lengthLimit
>0 ){
1031 sqlite3_limit(cx
.db
, SQLITE_LIMIT_LENGTH
, lengthLimit
);
1034 sqlite3_limit(cx
.db
, SQLITE_LIMIT_EXPR_DEPTH
, depthLimit
);
1036 sqlite3_limit(cx
.db
, SQLITE_LIMIT_LIKE_PATTERN_LENGTH
, 100);
1037 sqlite3_hard_heap_limit64(heapLimit
);
1039 if( nDb
>=20 && aDb
[18]==2 && aDb
[19]==2 ){
1040 aDb
[18] = aDb
[19] = 1;
1042 rc
= sqlite3_deserialize(cx
.db
, "main", aDb
, nDb
, nDb
,
1043 SQLITE_DESERIALIZE_RESIZEABLE
|
1044 SQLITE_DESERIALIZE_FREEONCLOSE
);
1046 fprintf(stderr
, "sqlite3_deserialize() failed with %d\n", rc
);
1047 goto testrun_finished
;
1050 sqlite3_int64 x
= maxDbSize
;
1051 sqlite3_file_control(cx
.db
, "main", SQLITE_FCNTL_SIZE_LIMIT
, &x
);
1054 /* For high debugging levels, turn on debug mode */
1055 if( eVerbosity
>=5 ){
1056 sqlite3_exec(cx
.db
, "PRAGMA vdbe_debug=ON;", 0, 0, 0);
1059 /* Block debug pragmas and ATTACH/DETACH. But wait until after
1060 ** deserialize to do this because deserialize depends on ATTACH */
1061 sqlite3_set_authorizer(cx
.db
, block_troublesome_sql
, 0);
1064 sqlite3_vt02_init(cx
.db
, 0, 0);
1067 /* Consistent PRNG seed */
1068 #ifdef SQLITE_TESTCTRL_PRNG_SEED
1069 sqlite3_table_column_metadata(cx
.db
, 0, "x", 0, 0, 0, 0, 0, 0);
1070 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED
, 1, cx
.db
);
1072 sqlite3_randomness(0,0);
1075 zSql
= sqlite3_malloc( nSql
+ 1 );
1077 fprintf(stderr
, "Out of memory!\n");
1079 memcpy(zSql
, aData
+iSql
, nSql
);
1081 for(i
=j
=0; zSql
[i
]; i
++){
1083 char cSaved
= zSql
[i
+1];
1085 if( sqlite3_complete(zSql
+j
) ){
1086 rc
= runDbSql(cx
.db
, zSql
+j
);
1090 if( rc
==SQLITE_INTERRUPT
|| progress_handler(&cx
) ){
1091 goto testrun_finished
;
1096 runDbSql(cx
.db
, zSql
+j
);
1101 rc
= sqlite3_close(cx
.db
);
1102 if( rc
!=SQLITE_OK
){
1103 fprintf(stdout
, "sqlite3_close() returns %d\n", rc
);
1105 if( eVerbosity
>=2 && !bScript
){
1106 fprintf(stdout
, "Peak memory usages: %f MB\n",
1107 sqlite3_memory_highwater(1) / 1000000.0);
1109 if( sqlite3_memory_used()!=0 ){
1112 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT
, &nAlloc
, &nNotUsed
, 0);
1113 fprintf(stderr
,"Memory leak: %lld bytes in %d allocations\n",
1114 sqlite3_memory_used(), nAlloc
);
1117 sqlite3_hard_heap_limit64(0);
1118 sqlite3_soft_heap_limit64(0);
1123 ** END of the dbsqlfuzz code
1124 ***************************************************************************/
1126 /* Look at a SQL text and try to determine if it begins with a database
1127 ** description, such as would be found in a dbsqlfuzz test case. Return
1128 ** true if this does appear to be a dbsqlfuzz test case and false otherwise.
1130 static int isDbSql(unsigned char *a
, int n
){
1131 unsigned char buf
[12];
1133 if( n
>4 && memcmp(a
,"\n--\n",4)==0 ) return 1;
1134 while( n
>0 && isspace(a
[0]) ){ a
++; n
--; }
1135 for(i
=0; n
>0 && i
<8; n
--, a
++){
1136 if( isxdigit(a
[0]) ) buf
[i
++] = a
[0];
1138 if( i
==8 && memcmp(buf
,"53514c69",8)==0 ) return 1;
1142 /* Implementation of the isdbsql(TEXT) SQL function.
1144 static void isDbSqlFunc(
1145 sqlite3_context
*context
,
1147 sqlite3_value
**argv
1149 int n
= sqlite3_value_bytes(argv
[0]);
1150 unsigned char *a
= (unsigned char*)sqlite3_value_blob(argv
[0]);
1151 sqlite3_result_int(context
, a
!=0 && n
>0 && isDbSql(a
,n
));
1154 /* Methods for the VHandle object
1156 static int inmemClose(sqlite3_file
*pFile
){
1157 VHandle
*p
= (VHandle
*)pFile
;
1158 VFile
*pVFile
= p
->pVFile
;
1160 if( pVFile
->nRef
==0 && pVFile
->zFilename
==0 ){
1167 static int inmemRead(
1168 sqlite3_file
*pFile
, /* Read from this open file */
1169 void *pData
, /* Store content in this buffer */
1170 int iAmt
, /* Bytes of content */
1171 sqlite3_int64 iOfst
/* Start reading here */
1173 VHandle
*pHandle
= (VHandle
*)pFile
;
1174 VFile
*pVFile
= pHandle
->pVFile
;
1175 if( iOfst
<0 || iOfst
>=pVFile
->sz
){
1176 memset(pData
, 0, iAmt
);
1177 return SQLITE_IOERR_SHORT_READ
;
1179 if( iOfst
+iAmt
>pVFile
->sz
){
1180 memset(pData
, 0, iAmt
);
1181 iAmt
= (int)(pVFile
->sz
- iOfst
);
1182 memcpy(pData
, pVFile
->a
+ iOfst
, iAmt
);
1183 return SQLITE_IOERR_SHORT_READ
;
1185 memcpy(pData
, pVFile
->a
+ iOfst
, iAmt
);
1188 static int inmemWrite(
1189 sqlite3_file
*pFile
, /* Write to this file */
1190 const void *pData
, /* Content to write */
1191 int iAmt
, /* bytes to write */
1192 sqlite3_int64 iOfst
/* Start writing here */
1194 VHandle
*pHandle
= (VHandle
*)pFile
;
1195 VFile
*pVFile
= pHandle
->pVFile
;
1196 if( iOfst
+iAmt
> pVFile
->sz
){
1197 if( iOfst
+iAmt
>= MX_FILE_SZ
){
1200 pVFile
->a
= safe_realloc(pVFile
->a
, (int)(iOfst
+iAmt
));
1201 if( iOfst
> pVFile
->sz
){
1202 memset(pVFile
->a
+ pVFile
->sz
, 0, (int)(iOfst
- pVFile
->sz
));
1204 pVFile
->sz
= (int)(iOfst
+ iAmt
);
1206 memcpy(pVFile
->a
+ iOfst
, pData
, iAmt
);
1209 static int inmemTruncate(sqlite3_file
*pFile
, sqlite3_int64 iSize
){
1210 VHandle
*pHandle
= (VHandle
*)pFile
;
1211 VFile
*pVFile
= pHandle
->pVFile
;
1212 if( pVFile
->sz
>iSize
&& iSize
>=0 ) pVFile
->sz
= (int)iSize
;
1215 static int inmemSync(sqlite3_file
*pFile
, int flags
){
1218 static int inmemFileSize(sqlite3_file
*pFile
, sqlite3_int64
*pSize
){
1219 *pSize
= ((VHandle
*)pFile
)->pVFile
->sz
;
1222 static int inmemLock(sqlite3_file
*pFile
, int type
){
1225 static int inmemUnlock(sqlite3_file
*pFile
, int type
){
1228 static int inmemCheckReservedLock(sqlite3_file
*pFile
, int *pOut
){
1232 static int inmemFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
1233 return SQLITE_NOTFOUND
;
1235 static int inmemSectorSize(sqlite3_file
*pFile
){
1238 static int inmemDeviceCharacteristics(sqlite3_file
*pFile
){
1240 SQLITE_IOCAP_SAFE_APPEND
|
1241 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
|
1242 SQLITE_IOCAP_POWERSAFE_OVERWRITE
;
1246 /* Method table for VHandle
1248 static sqlite3_io_methods VHandleMethods
= {
1250 /* xClose */ inmemClose
,
1251 /* xRead */ inmemRead
,
1252 /* xWrite */ inmemWrite
,
1253 /* xTruncate */ inmemTruncate
,
1254 /* xSync */ inmemSync
,
1255 /* xFileSize */ inmemFileSize
,
1256 /* xLock */ inmemLock
,
1257 /* xUnlock */ inmemUnlock
,
1258 /* xCheck... */ inmemCheckReservedLock
,
1259 /* xFileCtrl */ inmemFileControl
,
1260 /* xSectorSz */ inmemSectorSize
,
1261 /* xDevchar */ inmemDeviceCharacteristics
,
1264 /* xShmBarrier */ 0,
1271 ** Open a new file in the inmem VFS. All files are anonymous and are
1274 static int inmemOpen(
1276 const char *zFilename
,
1277 sqlite3_file
*pFile
,
1281 VFile
*pVFile
= createVFile(zFilename
, 0, (unsigned char*)"");
1282 VHandle
*pHandle
= (VHandle
*)pFile
;
1286 pHandle
->pVFile
= pVFile
;
1288 pFile
->pMethods
= &VHandleMethods
;
1289 if( pOutFlags
) *pOutFlags
= openFlags
;
1294 ** Delete a file by name
1296 static int inmemDelete(
1298 const char *zFilename
,
1301 VFile
*pVFile
= findVFile(zFilename
);
1302 if( pVFile
==0 ) return SQLITE_OK
;
1303 if( pVFile
->nRef
==0 ){
1304 free(pVFile
->zFilename
);
1305 pVFile
->zFilename
= 0;
1311 return SQLITE_IOERR_DELETE
;
1314 /* Check for the existance of a file
1316 static int inmemAccess(
1318 const char *zFilename
,
1322 VFile
*pVFile
= findVFile(zFilename
);
1323 *pResOut
= pVFile
!=0;
1327 /* Get the canonical pathname for a file
1329 static int inmemFullPathname(
1331 const char *zFilename
,
1335 sqlite3_snprintf(nOut
, zOut
, "%s", zFilename
);
1339 /* Always use the same random see, for repeatability.
1341 static int inmemRandomness(sqlite3_vfs
*NotUsed
, int nBuf
, char *zBuf
){
1342 memset(zBuf
, 0, nBuf
);
1343 memcpy(zBuf
, &g
.uRandom
, nBuf
<sizeof(g
.uRandom
) ? nBuf
: sizeof(g
.uRandom
));
1348 ** Register the VFS that reads from the g.aFile[] set of files.
1350 static void inmemVfsRegister(int makeDefault
){
1351 static sqlite3_vfs inmemVfs
;
1352 sqlite3_vfs
*pDefault
= sqlite3_vfs_find(0);
1353 inmemVfs
.iVersion
= 3;
1354 inmemVfs
.szOsFile
= sizeof(VHandle
);
1355 inmemVfs
.mxPathname
= 200;
1356 inmemVfs
.zName
= "inmem";
1357 inmemVfs
.xOpen
= inmemOpen
;
1358 inmemVfs
.xDelete
= inmemDelete
;
1359 inmemVfs
.xAccess
= inmemAccess
;
1360 inmemVfs
.xFullPathname
= inmemFullPathname
;
1361 inmemVfs
.xRandomness
= inmemRandomness
;
1362 inmemVfs
.xSleep
= pDefault
->xSleep
;
1363 inmemVfs
.xCurrentTimeInt64
= pDefault
->xCurrentTimeInt64
;
1364 sqlite3_vfs_register(&inmemVfs
, makeDefault
);
1368 ** Allowed values for the runFlags parameter to runSql()
1370 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
1371 #define SQL_OUTPUT 0x0002 /* Show the SQL output */
1374 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
1375 ** stop if an error is encountered.
1377 static void runSql(sqlite3
*db
, const char *zSql
, unsigned runFlags
){
1379 sqlite3_stmt
*pStmt
;
1381 while( zSql
&& zSql
[0] ){
1384 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zMore
);
1385 if( zMore
==zSql
) break;
1386 if( runFlags
& SQL_TRACE
){
1387 const char *z
= zSql
;
1389 while( z
<zMore
&& ISSPACE(z
[0]) ) z
++;
1390 n
= (int)(zMore
- z
);
1391 while( n
>0 && ISSPACE(z
[n
-1]) ) n
--;
1394 printf("TRACE: %.*s (error: %s)\n", n
, z
, sqlite3_errmsg(db
));
1396 printf("TRACE: %.*s\n", n
, z
);
1401 if( (runFlags
& SQL_OUTPUT
)==0 ){
1402 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){}
1405 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
1408 nCol
= sqlite3_column_count(pStmt
);
1410 printf("--------------------------------------------\n");
1412 for(i
=0; i
<nCol
; i
++){
1413 int eType
= sqlite3_column_type(pStmt
,i
);
1414 printf("%s = ", sqlite3_column_name(pStmt
,i
));
1420 case SQLITE_INTEGER
: {
1421 printf("INT %s\n", sqlite3_column_text(pStmt
,i
));
1424 case SQLITE_FLOAT
: {
1425 printf("FLOAT %s\n", sqlite3_column_text(pStmt
,i
));
1429 printf("TEXT [%s]\n", sqlite3_column_text(pStmt
,i
));
1433 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt
,i
));
1440 sqlite3_finalize(pStmt
);
1446 ** Rebuild the database file.
1448 ** (1) Remove duplicate entries
1449 ** (2) Put all entries in order
1452 static void rebuild_database(sqlite3
*db
, int dbSqlOnly
){
1455 zSql
= sqlite3_mprintf(
1457 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
1459 "INSERT INTO db(dbid, dbcontent) "
1460 " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
1462 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n"
1463 "DELETE FROM xsql;\n"
1464 "INSERT INTO xsql(sqlid,sqltext) "
1465 " SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
1468 "PRAGMA page_size=1024;\n"
1470 dbSqlOnly
? " WHERE isdbsql(sqltext)" : ""
1472 rc
= sqlite3_exec(db
, zSql
, 0, 0, 0);
1474 if( rc
) fatalError("cannot rebuild: %s", sqlite3_errmsg(db
));
1478 ** Return the value of a hexadecimal digit. Return -1 if the input
1479 ** is not a hex digit.
1481 static int hexDigitValue(char c
){
1482 if( c
>='0' && c
<='9' ) return c
- '0';
1483 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
1484 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
1489 ** Interpret zArg as an integer value, possibly with suffixes.
1491 static int integerValue(const char *zArg
){
1492 sqlite3_int64 v
= 0;
1493 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
1495 { "MiB", 1024*1024 },
1496 { "GiB", 1024*1024*1024 },
1499 { "GB", 1000000000 },
1502 { "G", 1000000000 },
1509 }else if( zArg
[0]=='+' ){
1512 if( zArg
[0]=='0' && zArg
[1]=='x' ){
1515 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
1520 while( ISDIGIT(zArg
[0]) ){
1521 v
= v
*10 + zArg
[0] - '0';
1525 for(i
=0; i
<sizeof(aMult
)/sizeof(aMult
[0]); i
++){
1526 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
1527 v
*= aMult
[i
].iMult
;
1531 if( v
>0x7fffffff ) fatalError("parameter too large - max 2147483648");
1532 return (int)(isNeg
? -v
: v
);
1536 ** Return the number of "v" characters in a string. Return 0 if there
1537 ** are any characters in the string other than "v".
1539 static int numberOfVChar(const char *z
){
1541 while( z
[0] && z
[0]=='v' ){
1545 return z
[0]==0 ? N
: 0;
1549 ** Print sketchy documentation for this utility program
1551 static void showHelp(void){
1552 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g
.zArgv0
);
1554 "Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
1555 "each database, checking for crashes and memory leaks.\n"
1557 " --cell-size-check Set the PRAGMA cell_size_check=ON\n"
1558 " --dbid N Use only the database where dbid=N\n"
1559 " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
1560 " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
1561 " --help Show this help text\n"
1562 " --info Show information about SOURCE-DB w/o running tests\n"
1563 " --limit-depth N Limit expression depth to N. Default: 500\n"
1564 " --limit-heap N Limit heap memory to N. Default: 100M\n"
1565 " --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
1566 " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
1567 " --load-sql FILE.. Load SQL scripts fron files into SOURCE-DB\n"
1568 " --load-db FILE.. Load template databases from files into SOURCE_DB\n"
1569 " --load-dbsql FILE.. Load dbsqlfuzz outputs into the xsql table\n"
1570 " ^^^^------ Use \"-\" for FILE to read filenames from stdin\n"
1571 " -m TEXT Add a description to the database\n"
1572 " --native-vfs Use the native VFS for initially empty database files\n"
1573 " --native-malloc Turn off MEMSYS3/5 and Lookaside\n"
1574 " --oss-fuzz Enable OSS-FUZZ testing\n"
1575 " --prng-seed N Seed value for the PRGN inside of SQLite\n"
1576 " -q|--quiet Reduced output\n"
1577 " --rebuild Rebuild and vacuum the database file\n"
1578 " --result-trace Show the results of each SQL command\n"
1579 " --script Output CLI script instead of running tests\n"
1580 " --skip N Skip the first N test cases\n"
1581 " --spinner Use a spinner to show progress\n"
1582 " --sqlid N Use only SQL where sqlid=N\n"
1583 " --timeout N Maximum time for any one test in N millseconds\n"
1584 " -v|--verbose Increased output. Repeat for more output.\n"
1585 " --vdbe-debug Activate VDBE debugging.\n"
1589 int main(int argc
, char **argv
){
1590 sqlite3_int64 iBegin
; /* Start time of this program */
1591 int quietFlag
= 0; /* True if --quiet or -q */
1592 int verboseFlag
= 0; /* True if --verbose or -v */
1593 char *zInsSql
= 0; /* SQL statement for --load-db or --load-sql */
1594 int iFirstInsArg
= 0; /* First argv[] for --load-db or --load-sql */
1595 sqlite3
*db
= 0; /* The open database connection */
1596 sqlite3_stmt
*pStmt
; /* A prepared statement */
1597 int rc
; /* Result code from SQLite interface calls */
1598 Blob
*pSql
; /* For looping over SQL scripts */
1599 Blob
*pDb
; /* For looping over template databases */
1600 int i
; /* Loop index for the argv[] loop */
1601 int dbSqlOnly
= 0; /* Only use scripts that are dbsqlfuzz */
1602 int onlySqlid
= -1; /* --sqlid */
1603 int onlyDbid
= -1; /* --dbid */
1604 int nativeFlag
= 0; /* --native-vfs */
1605 int rebuildFlag
= 0; /* --rebuild */
1606 int vdbeLimitFlag
= 0; /* --limit-vdbe */
1607 int infoFlag
= 0; /* --info */
1608 int nSkip
= 0; /* --skip */
1609 int bScript
= 0; /* --script */
1610 int bSpinner
= 0; /* True for --spinner */
1611 int timeoutTest
= 0; /* undocumented --timeout-test flag */
1612 int runFlags
= 0; /* Flags sent to runSql() */
1613 char *zMsg
= 0; /* Add this message */
1614 int nSrcDb
= 0; /* Number of source databases */
1615 char **azSrcDb
= 0; /* Array of source database names */
1616 int iSrcDb
; /* Loop over all source databases */
1617 int nTest
= 0; /* Total number of tests performed */
1618 char *zDbName
= ""; /* Appreviated name of a source database */
1619 const char *zFailCode
= 0; /* Value of the TEST_FAILURE env variable */
1620 int cellSzCkFlag
= 0; /* --cell-size-check */
1621 int sqlFuzz
= 0; /* True for SQL fuzz. False for DB fuzz */
1622 int iTimeout
= 120000; /* Default 120-second timeout */
1623 int nMem
= 0; /* Memory limit override */
1624 int nMemThisDb
= 0; /* Memory limit set by the CONFIG table */
1625 char *zExpDb
= 0; /* Write Databases to files in this directory */
1626 char *zExpSql
= 0; /* Write SQL to files in this directory */
1627 void *pHeap
= 0; /* Heap for use by SQLite */
1628 int ossFuzz
= 0; /* enable OSS-FUZZ testing */
1629 int ossFuzzThisDb
= 0; /* ossFuzz value for this particular database */
1630 int nativeMalloc
= 0; /* Turn off MEMSYS3/5 and lookaside if true */
1631 sqlite3_vfs
*pDfltVfs
; /* The default VFS */
1632 int openFlags4Data
; /* Flags for sqlite3_open_v2() */
1633 int bTimer
= 0; /* Show elapse time for each test */
1634 int nV
; /* How much to increase verbosity with -vvvv */
1635 sqlite3_int64 tmStart
; /* Start of each test */
1637 sqlite3_config(SQLITE_CONFIG_URI
,1);
1638 registerOomSimulator();
1639 sqlite3_initialize();
1640 iBegin
= timeOfDay();
1642 signal(SIGALRM
, signalHandler
);
1643 signal(SIGSEGV
, signalHandler
);
1644 signal(SIGABRT
, signalHandler
);
1647 openFlags4Data
= SQLITE_OPEN_READONLY
;
1648 zFailCode
= getenv("TEST_FAILURE");
1649 pDfltVfs
= sqlite3_vfs_find(0);
1650 inmemVfsRegister(1);
1651 for(i
=1; i
<argc
; i
++){
1652 const char *z
= argv
[i
];
1655 if( z
[0]=='-' ) z
++;
1656 if( strcmp(z
,"cell-size-check")==0 ){
1659 if( strcmp(z
,"dbid")==0 ){
1660 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1661 onlyDbid
= integerValue(argv
[++i
]);
1663 if( strcmp(z
,"export-db")==0 ){
1664 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1667 if( strcmp(z
,"export-sql")==0 || strcmp(z
,"export-dbsql")==0 ){
1668 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1669 zExpSql
= argv
[++i
];
1671 if( strcmp(z
,"help")==0 ){
1675 if( strcmp(z
,"info")==0 ){
1678 if( strcmp(z
,"limit-depth")==0 ){
1679 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1680 depthLimit
= integerValue(argv
[++i
]);
1682 if( strcmp(z
,"limit-heap")==0 ){
1683 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1684 heapLimit
= integerValue(argv
[++i
]);
1686 if( strcmp(z
,"limit-mem")==0 ){
1687 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1688 nMem
= integerValue(argv
[++i
]);
1690 if( strcmp(z
,"limit-vdbe")==0 ){
1693 if( strcmp(z
,"load-sql")==0 ){
1694 zInsSql
= "INSERT INTO xsql(sqltext)"
1695 "VALUES(CAST(readtextfile(?1) AS text))";
1697 openFlags4Data
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
1700 if( strcmp(z
,"load-db")==0 ){
1701 zInsSql
= "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
1703 openFlags4Data
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
1706 if( strcmp(z
,"load-dbsql")==0 ){
1707 zInsSql
= "INSERT INTO xsql(sqltext)"
1708 "VALUES(readfile(?1))";
1710 openFlags4Data
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
1714 if( strcmp(z
,"m")==0 ){
1715 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1717 openFlags4Data
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
1719 if( strcmp(z
,"native-malloc")==0 ){
1722 if( strcmp(z
,"native-vfs")==0 ){
1725 if( strcmp(z
,"oss-fuzz")==0 ){
1728 if( strcmp(z
,"prng-seed")==0 ){
1729 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1730 g
.uRandom
= atoi(argv
[++i
]);
1732 if( strcmp(z
,"quiet")==0 || strcmp(z
,"q")==0 ){
1737 if( strcmp(z
,"rebuild")==0 ){
1739 openFlags4Data
= SQLITE_OPEN_READWRITE
;
1741 if( strcmp(z
,"result-trace")==0 ){
1742 runFlags
|= SQL_OUTPUT
;
1744 if( strcmp(z
,"script")==0 ){
1747 if( strcmp(z
,"skip")==0 ){
1748 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1749 nSkip
= atoi(argv
[++i
]);
1751 if( strcmp(z
,"spinner")==0 ){
1754 if( strcmp(z
,"timer")==0 ){
1757 if( strcmp(z
,"sqlid")==0 ){
1758 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1759 onlySqlid
= integerValue(argv
[++i
]);
1761 if( strcmp(z
,"timeout")==0 ){
1762 if( i
>=argc
-1 ) fatalError("missing arguments on %s", argv
[i
]);
1763 iTimeout
= integerValue(argv
[++i
]);
1765 if( strcmp(z
,"timeout-test")==0 ){
1768 fatalError("timeout is not available on non-unix systems");
1771 if( strcmp(z
,"vdbe-debug")==0 ){
1774 if( strcmp(z
,"verbose")==0 ){
1778 if( verboseFlag
>1 ) runFlags
|= SQL_TRACE
;
1780 if( (nV
= numberOfVChar(z
))>=1 ){
1784 if( verboseFlag
>1 ) runFlags
|= SQL_TRACE
;
1786 if( strcmp(z
,"version")==0 ){
1789 printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid());
1790 for(ii
=0; (zz
= sqlite3_compileoption_get(ii
))!=0; ii
++){
1795 if( strcmp(z
,"is-dbsql")==0 ){
1797 for(i
++; i
<argc
; i
++){
1799 char *aData
= readFile(argv
[i
], &nData
);
1800 printf("%d %s\n", isDbSql((unsigned char*)aData
,nData
), argv
[i
]);
1801 sqlite3_free(aData
);
1806 fatalError("unknown option: %s", argv
[i
]);
1810 azSrcDb
= safe_realloc(azSrcDb
, nSrcDb
*sizeof(azSrcDb
[0]));
1811 azSrcDb
[nSrcDb
-1] = argv
[i
];
1814 if( nSrcDb
==0 ) fatalError("no source database specified");
1817 fatalError("cannot change the description of more than one database");
1820 fatalError("cannot import into more than one database");
1824 /* Process each source database separately */
1825 for(iSrcDb
=0; iSrcDb
<nSrcDb
; iSrcDb
++){
1828 g
.zDbFile
= azSrcDb
[iSrcDb
];
1829 rc
= sqlite3_open_v2(azSrcDb
[iSrcDb
], &db
,
1830 openFlags4Data
, pDfltVfs
->zName
);
1831 if( rc
==SQLITE_OK
){
1832 rc
= sqlite3_exec(db
, "SELECT count(*) FROM sqlite_schema", 0, 0, 0);
1836 zRawData
= readFile(azSrcDb
[iSrcDb
], &nRawData
);
1838 fatalError("input file \"%s\" is not recognized\n", azSrcDb
[iSrcDb
]);
1840 sqlite3_open(":memory:", &db
);
1843 /* Print the description, if there is one */
1846 zDbName
= azSrcDb
[iSrcDb
];
1847 i
= (int)strlen(zDbName
) - 1;
1848 while( i
>0 && zDbName
[i
-1]!='/' && zDbName
[i
-1]!='\\' ){ i
--; }
1850 sqlite3_prepare_v2(db
, "SELECT msg FROM readme", -1, &pStmt
, 0);
1851 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
1852 printf("%s: %s", zDbName
, sqlite3_column_text(pStmt
,0));
1854 printf("%s: (empty \"readme\")", zDbName
);
1856 sqlite3_finalize(pStmt
);
1857 sqlite3_prepare_v2(db
, "SELECT count(*) FROM db", -1, &pStmt
, 0);
1859 && sqlite3_step(pStmt
)==SQLITE_ROW
1860 && (n
= sqlite3_column_int(pStmt
,0))>0
1862 printf(" - %d DBs", n
);
1864 sqlite3_finalize(pStmt
);
1865 sqlite3_prepare_v2(db
, "SELECT count(*) FROM xsql", -1, &pStmt
, 0);
1867 && sqlite3_step(pStmt
)==SQLITE_ROW
1868 && (n
= sqlite3_column_int(pStmt
,0))>0
1870 printf(" - %d scripts", n
);
1872 sqlite3_finalize(pStmt
);
1875 sqlite3_free(zRawData
);
1879 rc
= sqlite3_exec(db
,
1880 "CREATE TABLE IF NOT EXISTS db(\n"
1881 " dbid INTEGER PRIMARY KEY, -- database id\n"
1882 " dbcontent BLOB -- database disk file image\n"
1884 "CREATE TABLE IF NOT EXISTS xsql(\n"
1885 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
1886 " sqltext TEXT -- Text of SQL statements to run\n"
1888 "CREATE TABLE IF NOT EXISTS readme(\n"
1889 " msg TEXT -- Human-readable description of this file\n"
1891 if( rc
) fatalError("cannot create schema: %s", sqlite3_errmsg(db
));
1894 zSql
= sqlite3_mprintf(
1895 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg
);
1896 rc
= sqlite3_exec(db
, zSql
, 0, 0, 0);
1898 if( rc
) fatalError("cannot change description: %s", sqlite3_errmsg(db
));
1901 zInsSql
= "INSERT INTO xsql(sqltext) VALUES(?1)";
1902 rc
= sqlite3_prepare_v2(db
, zInsSql
, -1, &pStmt
, 0);
1903 if( rc
) fatalError("cannot prepare statement [%s]: %s",
1904 zInsSql
, sqlite3_errmsg(db
));
1905 sqlite3_bind_text(pStmt
, 1, zRawData
, nRawData
, SQLITE_STATIC
);
1906 sqlite3_step(pStmt
);
1907 rc
= sqlite3_reset(pStmt
);
1908 if( rc
) fatalError("insert failed for %s", argv
[i
]);
1909 sqlite3_finalize(pStmt
);
1910 rebuild_database(db
, dbSqlOnly
);
1912 sqlite3_free(zRawData
);
1915 ossFuzzThisDb
= ossFuzz
;
1917 /* If the CONFIG(name,value) table exists, read db-specific settings
1918 ** from that table */
1919 if( sqlite3_table_column_metadata(db
,0,"config",0,0,0,0,0,0)==SQLITE_OK
){
1920 rc
= sqlite3_prepare_v2(db
, "SELECT name, value FROM config",
1922 if( rc
) fatalError("cannot prepare query of CONFIG table: %s",
1923 sqlite3_errmsg(db
));
1924 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
1925 const char *zName
= (const char *)sqlite3_column_text(pStmt
,0);
1926 if( zName
==0 ) continue;
1927 if( strcmp(zName
, "oss-fuzz")==0 ){
1928 ossFuzzThisDb
= sqlite3_column_int(pStmt
,1);
1929 if( verboseFlag
) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb
);
1931 if( strcmp(zName
, "limit-mem")==0 ){
1932 nMemThisDb
= sqlite3_column_int(pStmt
,1);
1933 if( verboseFlag
) printf("Config: limit-mem=%d\n", nMemThisDb
);
1936 sqlite3_finalize(pStmt
);
1940 sqlite3_create_function(db
, "readfile", 1, SQLITE_UTF8
, 0,
1941 readfileFunc
, 0, 0);
1942 sqlite3_create_function(db
, "readtextfile", 1, SQLITE_UTF8
, 0,
1943 readtextfileFunc
, 0, 0);
1944 sqlite3_create_function(db
, "isdbsql", 1, SQLITE_UTF8
, 0,
1946 rc
= sqlite3_prepare_v2(db
, zInsSql
, -1, &pStmt
, 0);
1947 if( rc
) fatalError("cannot prepare statement [%s]: %s",
1948 zInsSql
, sqlite3_errmsg(db
));
1949 rc
= sqlite3_exec(db
, "BEGIN", 0, 0, 0);
1950 if( rc
) fatalError("cannot start a transaction");
1951 for(i
=iFirstInsArg
; i
<argc
; i
++){
1952 if( strcmp(argv
[i
],"-")==0 ){
1953 /* A filename of "-" means read multiple filenames from stdin */
1955 while( rc
==0 && fgets(zLine
,sizeof(zLine
),stdin
)!=0 ){
1956 size_t kk
= strlen(zLine
);
1957 while( kk
>0 && zLine
[kk
-1]<=' ' ) kk
--;
1958 sqlite3_bind_text(pStmt
, 1, zLine
, (int)kk
, SQLITE_STATIC
);
1959 if( verboseFlag
) printf("loading %.*s\n", (int)kk
, zLine
);
1960 sqlite3_step(pStmt
);
1961 rc
= sqlite3_reset(pStmt
);
1962 if( rc
) fatalError("insert failed for %s", zLine
);
1965 sqlite3_bind_text(pStmt
, 1, argv
[i
], -1, SQLITE_STATIC
);
1966 if( verboseFlag
) printf("loading %s\n", argv
[i
]);
1967 sqlite3_step(pStmt
);
1968 rc
= sqlite3_reset(pStmt
);
1969 if( rc
) fatalError("insert failed for %s", argv
[i
]);
1972 sqlite3_finalize(pStmt
);
1973 rc
= sqlite3_exec(db
, "COMMIT", 0, 0, 0);
1974 if( rc
) fatalError("cannot commit the transaction: %s",
1975 sqlite3_errmsg(db
));
1976 rebuild_database(db
, dbSqlOnly
);
1980 rc
= sqlite3_exec(db
, "PRAGMA query_only=1;", 0, 0, 0);
1981 if( rc
) fatalError("cannot set database to query-only");
1982 if( zExpDb
!=0 || zExpSql
!=0 ){
1983 sqlite3_create_function(db
, "writefile", 2, SQLITE_UTF8
, 0,
1984 writefileFunc
, 0, 0);
1987 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
1988 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
1989 " FROM db WHERE ?2<0 OR dbid=?2;";
1990 rc
= sqlite3_prepare_v2(db
, zExDb
, -1, &pStmt
, 0);
1991 if( rc
) fatalError("cannot prepare statement [%s]: %s",
1992 zExDb
, sqlite3_errmsg(db
));
1993 sqlite3_bind_text64(pStmt
, 1, zExpDb
, strlen(zExpDb
),
1994 SQLITE_STATIC
, SQLITE_UTF8
);
1995 sqlite3_bind_int(pStmt
, 2, onlyDbid
);
1996 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
1997 printf("write db-%d (%d bytes) into %s\n",
1998 sqlite3_column_int(pStmt
,1),
1999 sqlite3_column_int(pStmt
,3),
2000 sqlite3_column_text(pStmt
,2));
2002 sqlite3_finalize(pStmt
);
2005 const char *zExSql
=
2006 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
2007 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
2008 " FROM xsql WHERE ?2<0 OR sqlid=?2;";
2009 rc
= sqlite3_prepare_v2(db
, zExSql
, -1, &pStmt
, 0);
2010 if( rc
) fatalError("cannot prepare statement [%s]: %s",
2011 zExSql
, sqlite3_errmsg(db
));
2012 sqlite3_bind_text64(pStmt
, 1, zExpSql
, strlen(zExpSql
),
2013 SQLITE_STATIC
, SQLITE_UTF8
);
2014 sqlite3_bind_int(pStmt
, 2, onlySqlid
);
2015 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
2016 printf("write sql-%d (%d bytes) into %s\n",
2017 sqlite3_column_int(pStmt
,1),
2018 sqlite3_column_int(pStmt
,3),
2019 sqlite3_column_text(pStmt
,2));
2021 sqlite3_finalize(pStmt
);
2027 /* Load all SQL script content and all initial database images from the
2030 blobListLoadFromDb(db
, "SELECT sqlid, sqltext FROM xsql", onlySqlid
,
2031 &g
.nSql
, &g
.pFirstSql
);
2032 if( g
.nSql
==0 ) fatalError("need at least one SQL script");
2033 blobListLoadFromDb(db
, "SELECT dbid, dbcontent FROM db", onlyDbid
,
2034 &g
.nDb
, &g
.pFirstDb
);
2036 g
.pFirstDb
= safe_realloc(0, sizeof(Blob
));
2037 memset(g
.pFirstDb
, 0, sizeof(Blob
));
2039 g
.pFirstDb
->seq
= 0;
2044 /* Print the description, if there is one */
2045 if( !quietFlag
&& !bScript
){
2046 zDbName
= azSrcDb
[iSrcDb
];
2047 i
= (int)strlen(zDbName
) - 1;
2048 while( i
>0 && zDbName
[i
-1]!='/' && zDbName
[i
-1]!='\\' ){ i
--; }
2050 sqlite3_prepare_v2(db
, "SELECT msg FROM readme", -1, &pStmt
, 0);
2051 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
2052 printf("%s: %s\n", zDbName
, sqlite3_column_text(pStmt
,0));
2054 sqlite3_finalize(pStmt
);
2057 /* Rebuild the database, if requested */
2060 printf("%s: rebuilding... ", zDbName
);
2063 rebuild_database(db
, 0);
2064 if( !quietFlag
) printf("done\n");
2067 /* Close the source database. Verify that no SQLite memory allocations are
2071 if( sqlite3_memory_used()>0 ){
2072 fatalError("SQLite has memory in use before the start of testing");
2075 /* Limit available memory, if requested */
2078 if( nMemThisDb
>0 && nMem
==0 ){
2079 if( !nativeMalloc
){
2080 pHeap
= realloc(pHeap
, nMemThisDb
);
2082 fatalError("failed to allocate %d bytes of heap memory", nMem
);
2084 sqlite3_config(SQLITE_CONFIG_HEAP
, pHeap
, nMemThisDb
, 128);
2086 sqlite3_hard_heap_limit64((sqlite3_int64
)nMemThisDb
);
2089 sqlite3_hard_heap_limit64(0);
2092 /* Disable lookaside with the --native-malloc option */
2094 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, 0, 0);
2097 /* Reset the in-memory virtual filesystem */
2100 /* Run a test using each SQL script against each database.
2102 if( !verboseFlag
&& !quietFlag
&& !bSpinner
&& !bScript
){
2103 printf("%s:", zDbName
);
2105 for(pSql
=g
.pFirstSql
; pSql
; pSql
=pSql
->pNext
){
2106 tmStart
= timeOfDay();
2107 if( isDbSql(pSql
->a
, pSql
->sz
) ){
2108 sqlite3_snprintf(sizeof(g
.zTestName
), g
.zTestName
, "sqlid=%d",pSql
->id
);
2110 /* No progress output */
2111 }else if( bSpinner
){
2113 int idx
= pSql
->seq
;
2114 printf("\r%s: %d/%d ", zDbName
, idx
, nTotal
);
2116 }else if( verboseFlag
){
2117 printf("%s\n", g
.zTestName
);
2119 }else if( !quietFlag
){
2120 static int prevAmt
= -1;
2121 int idx
= pSql
->seq
;
2122 int amt
= idx
*10/(g
.nSql
);
2124 printf(" %d%%", amt
*10);
2132 runCombinedDbSqlInput(pSql
->a
, pSql
->sz
, iTimeout
, bScript
, pSql
->id
);
2135 if( bTimer
&& !bScript
){
2136 sqlite3_int64 tmEnd
= timeOfDay();
2137 printf("%lld %s\n", tmEnd
- tmStart
, g
.zTestName
);
2143 for(pDb
=g
.pFirstDb
; pDb
; pDb
=pDb
->pNext
){
2145 const char *zVfs
= "inmem";
2146 sqlite3_snprintf(sizeof(g
.zTestName
), g
.zTestName
, "sqlid=%d,dbid=%d",
2149 /* No progress output */
2150 }else if( bSpinner
){
2151 int nTotal
= g
.nDb
*g
.nSql
;
2152 int idx
= pSql
->seq
*g
.nDb
+ pDb
->id
- 1;
2153 printf("\r%s: %d/%d ", zDbName
, idx
, nTotal
);
2155 }else if( verboseFlag
){
2156 printf("%s\n", g
.zTestName
);
2158 }else if( !quietFlag
){
2159 static int prevAmt
= -1;
2160 int idx
= pSql
->seq
*g
.nDb
+ pDb
->id
- 1;
2161 int amt
= idx
*10/(g
.nDb
*g
.nSql
);
2163 printf(" %d%%", amt
*10);
2174 sqlite3_snprintf(sizeof(zName
), zName
, "db%06d.db",
2175 pDb
->id
>1 ? pDb
->id
: pSql
->id
);
2176 renderDbSqlForCLI(stdout
, zName
,
2177 pDb
->a
, pDb
->sz
, pSql
->a
, pSql
->sz
);
2180 createVFile("main.db", pDb
->sz
, pDb
->a
);
2181 sqlite3_randomness(0,0);
2182 if( ossFuzzThisDb
){
2183 #ifndef SQLITE_OSS_FUZZ
2184 fatalError("--oss-fuzz not supported: recompile"
2185 " with -DSQLITE_OSS_FUZZ");
2187 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t);
2188 LLVMFuzzerTestOneInput((const uint8_t*)pSql
->a
, (size_t)pSql
->sz
);
2191 openFlags
= SQLITE_OPEN_CREATE
| SQLITE_OPEN_READWRITE
;
2192 if( nativeFlag
&& pDb
->sz
==0 ){
2193 openFlags
|= SQLITE_OPEN_MEMORY
;
2196 rc
= sqlite3_open_v2("main.db", &db
, openFlags
, zVfs
);
2197 if( rc
) fatalError("cannot open inmem database");
2198 sqlite3_limit(db
, SQLITE_LIMIT_LENGTH
, 100000000);
2199 sqlite3_limit(db
, SQLITE_LIMIT_LIKE_PATTERN_LENGTH
, 50);
2200 if( cellSzCkFlag
) runSql(db
, "PRAGMA cell_size_check=ON", runFlags
);
2201 setAlarm((iTimeout
+999)/1000);
2202 /* Enable test functions */
2203 sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
, db
);
2204 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2205 if( sqlFuzz
|| vdbeLimitFlag
){
2206 sqlite3_progress_handler(db
, 100000, progressHandler
,
2210 #ifdef SQLITE_TESTCTRL_PRNG_SEED
2211 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED
, 1, db
);
2214 sqlite3_exec(db
, "PRAGMA vdbe_debug=ON", 0, 0, 0);
2217 runSql(db
, (char*)pSql
->a
, runFlags
);
2218 }while( timeoutTest
);
2220 sqlite3_exec(db
, "PRAGMA temp_store_directory=''", 0, 0, 0);
2223 if( sqlite3_memory_used()>0 ){
2224 fatalError("memory leak: %lld bytes outstanding",
2225 sqlite3_memory_used());
2230 sqlite3_int64 tmEnd
= timeOfDay();
2231 printf("%lld %s\n", tmEnd
- tmStart
, g
.zTestName
);
2235 /* Simulate an error if the TEST_FAILURE environment variable is "5".
2236 ** This is used to verify that automated test script really do spot
2237 ** errors that occur in this test program.
2240 if( zFailCode
[0]=='5' && zFailCode
[1]==0 ){
2241 fatalError("simulated failure");
2242 }else if( zFailCode
[0]!=0 ){
2243 /* If TEST_FAILURE is something other than 5, just exit the test
2245 printf("\nExit early due to TEST_FAILURE being set\n");
2247 goto sourcedb_cleanup
;
2253 /* No progress output */
2254 }else if( bSpinner
){
2255 int nTotal
= g
.nDb
*g
.nSql
;
2256 printf("\r%s: %d/%d \n", zDbName
, nTotal
, nTotal
);
2257 }else if( !quietFlag
&& !verboseFlag
){
2258 printf(" 100%% - %d tests\n", g
.nDb
*g
.nSql
);
2261 /* Clean up at the end of processing a single source database
2264 blobListFree(g
.pFirstSql
);
2265 blobListFree(g
.pFirstDb
);
2268 } /* End loop over all source databases */
2270 if( !quietFlag
&& !bScript
){
2271 sqlite3_int64 iElapse
= timeOfDay() - iBegin
;
2272 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
2274 nTest
, (int)(iElapse
/1000), (int)(iElapse
%1000),
2275 sqlite3_libversion(), sqlite3_sourceid());