blob: 51f1a636604e6816ce338767b47204d0a42b8662 [file] [log] [blame]
drh268e72f2015-04-17 14:30:49 +00001/*
2** 2015-04-17
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**
13** This is a utility program designed to aid running the SQLite library
14** against an external fuzzer, such as American Fuzzy Lop (AFL)
15** (http://lcamtuf.coredump.cx/afl/). Basically, this program reads
16** SQL text from standard input and passes it through to SQLite for evaluation,
17** just like the "sqlite3" command-line shell. Differences from the
18** command-line shell:
19**
20** (1) The complex "dot-command" extensions are omitted. This
21** prevents the fuzzer from discovering that it can run things
22** like ".shell rm -rf ~"
23**
24** (2) The database is opened with the SQLITE_OPEN_MEMORY flag so that
25** no disk I/O from the database is permitted. The ATTACH command
26** with a filename still uses an in-memory database.
27**
28** (3) The main in-memory database can be initialized from a template
29** disk database so that the fuzzer starts with a database containing
30** content.
31**
32** (4) The eval() SQL function is added, allowing the fuzzer to do
33** interesting recursive operations.
drhf34e9aa2015-04-20 12:50:13 +000034**
35** 2015-04-20: The input text can be divided into separate SQL chunks using
36** lines of the form:
37**
38** |****<...>****|
39**
40** where the "..." is arbitrary text, except the "|" should really be "/".
41** ("|" is used here to avoid compiler warnings about nested comments.)
drh875bafa2015-04-24 14:47:59 +000042** A separate in-memory SQLite database is created to run each chunk of SQL.
43** This feature allows the "queue" of AFL to be captured into a single big
drhf34e9aa2015-04-20 12:50:13 +000044** file using a command like this:
45**
46** (for i in id:*; do echo '|****<'$i'>****|'; cat $i; done) >~/all-queue.txt
47**
48** (Once again, change the "|" to "/") Then all elements of the AFL queue
drh4a74d072015-04-20 18:58:38 +000049** can be run in a single go (for regression testing, for example) by typing:
drhf34e9aa2015-04-20 12:50:13 +000050**
drh875bafa2015-04-24 14:47:59 +000051** fuzzershell -f ~/all-queue.txt
drhf34e9aa2015-04-20 12:50:13 +000052**
53** After running each chunk of SQL, the database connection is closed. The
54** program aborts if the close fails or if there is any unfreed memory after
55** the close.
drh875bafa2015-04-24 14:47:59 +000056**
57** New cases can be appended to all-queue.txt at any time. If redundant cases
58** are added, that can be eliminated by running:
59**
60** fuzzershell -f ~/all-queue.txt --unique-cases ~/unique-cases.txt
61**
drh268e72f2015-04-17 14:30:49 +000062*/
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <stdarg.h>
drh4a74d072015-04-20 18:58:38 +000067#include <ctype.h>
drh268e72f2015-04-17 14:30:49 +000068#include "sqlite3.h"
69
70/*
71** All global variables are gathered into the "g" singleton.
72*/
73struct GlobalVars {
74 const char *zArgv0; /* Name of program */
75} g;
76
77
78
79/*
80** Print an error message and abort in such a way to indicate to the
81** fuzzer that this counts as a crash.
82*/
83static void abendError(const char *zFormat, ...){
84 va_list ap;
85 fprintf(stderr, "%s: ", g.zArgv0);
86 va_start(ap, zFormat);
87 vfprintf(stderr, zFormat, ap);
88 va_end(ap);
89 fprintf(stderr, "\n");
90 abort();
91}
92/*
93** Print an error message and quit, but not in a way that would look
94** like a crash.
95*/
96static void fatalError(const char *zFormat, ...){
97 va_list ap;
98 fprintf(stderr, "%s: ", g.zArgv0);
99 va_start(ap, zFormat);
100 vfprintf(stderr, zFormat, ap);
101 va_end(ap);
102 fprintf(stderr, "\n");
103 exit(1);
104}
105
106/*
drh4a74d072015-04-20 18:58:38 +0000107** Evaluate some SQL. Abort if unable.
108*/
109static void sqlexec(sqlite3 *db, const char *zFormat, ...){
110 va_list ap;
111 char *zSql;
112 char *zErrMsg = 0;
113 int rc;
114 va_start(ap, zFormat);
115 zSql = sqlite3_vmprintf(zFormat, ap);
116 va_end(ap);
117 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
118 if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg);
119 sqlite3_free(zSql);
120}
121
122/*
drh268e72f2015-04-17 14:30:49 +0000123** This callback is invoked by sqlite3_log().
124*/
125static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){
126 printf("LOG: (%d) %s\n", iErrCode, zMsg);
127}
128
129/*
130** This callback is invoked by sqlite3_exec() to return query results.
131*/
132static int execCallback(void *NotUsed, int argc, char **argv, char **colv){
133 int i;
134 static unsigned cnt = 0;
135 printf("ROW #%u:\n", ++cnt);
136 for(i=0; i<argc; i++){
137 printf(" %s=", colv[i]);
138 if( argv[i] ){
139 printf("[%s]\n", argv[i]);
140 }else{
141 printf("NULL\n");
142 }
143 }
144 return 0;
145}
drh1cbb7fa2015-04-24 13:00:59 +0000146static int execNoop(void *NotUsed, int argc, char **argv, char **colv){
147 return 0;
148}
drh268e72f2015-04-17 14:30:49 +0000149
drh61a0d6b2015-04-24 18:31:12 +0000150#ifndef SQLITE_OMIT_TRACE
drh268e72f2015-04-17 14:30:49 +0000151/*
152** This callback is invoked by sqlite3_trace() as each SQL statement
153** starts.
154*/
155static void traceCallback(void *NotUsed, const char *zMsg){
156 printf("TRACE: %s\n", zMsg);
157}
drh61a0d6b2015-04-24 18:31:12 +0000158#endif
drh268e72f2015-04-17 14:30:49 +0000159
160/***************************************************************************
161** eval() implementation copied from ../ext/misc/eval.c
162*/
163/*
164** Structure used to accumulate the output
165*/
166struct EvalResult {
167 char *z; /* Accumulated output */
168 const char *zSep; /* Separator */
169 int szSep; /* Size of the separator string */
170 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */
171 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */
172};
173
174/*
175** Callback from sqlite_exec() for the eval() function.
176*/
177static int callback(void *pCtx, int argc, char **argv, char **colnames){
178 struct EvalResult *p = (struct EvalResult*)pCtx;
179 int i;
180 for(i=0; i<argc; i++){
181 const char *z = argv[i] ? argv[i] : "";
182 size_t sz = strlen(z);
183 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
184 char *zNew;
185 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
186 /* Using sqlite3_realloc64() would be better, but it is a recent
187 ** addition and will cause a segfault if loaded by an older version
188 ** of SQLite. */
189 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
190 if( zNew==0 ){
191 sqlite3_free(p->z);
192 memset(p, 0, sizeof(*p));
193 return 1;
194 }
195 p->z = zNew;
196 }
197 if( p->nUsed>0 ){
198 memcpy(&p->z[p->nUsed], p->zSep, p->szSep);
199 p->nUsed += p->szSep;
200 }
201 memcpy(&p->z[p->nUsed], z, sz);
202 p->nUsed += sz;
203 }
204 return 0;
205}
206
207/*
208** Implementation of the eval(X) and eval(X,Y) SQL functions.
209**
210** Evaluate the SQL text in X. Return the results, using string
211** Y as the separator. If Y is omitted, use a single space character.
212*/
213static void sqlEvalFunc(
214 sqlite3_context *context,
215 int argc,
216 sqlite3_value **argv
217){
218 const char *zSql;
219 sqlite3 *db;
220 char *zErr = 0;
221 int rc;
222 struct EvalResult x;
223
224 memset(&x, 0, sizeof(x));
225 x.zSep = " ";
226 zSql = (const char*)sqlite3_value_text(argv[0]);
227 if( zSql==0 ) return;
228 if( argc>1 ){
229 x.zSep = (const char*)sqlite3_value_text(argv[1]);
230 if( x.zSep==0 ) return;
231 }
232 x.szSep = (int)strlen(x.zSep);
233 db = sqlite3_context_db_handle(context);
234 rc = sqlite3_exec(db, zSql, callback, &x, &zErr);
235 if( rc!=SQLITE_OK ){
236 sqlite3_result_error(context, zErr, -1);
237 sqlite3_free(zErr);
238 }else if( x.zSep==0 ){
239 sqlite3_result_error_nomem(context);
240 sqlite3_free(x.z);
241 }else{
242 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
243 }
244}
245/* End of the eval() implementation
246******************************************************************************/
247
248/*
249** Print sketchy documentation for this utility program
250*/
251static void showHelp(void){
252 printf("Usage: %s [options]\n", g.zArgv0);
253 printf(
254"Read SQL text from standard input and evaluate it.\n"
255"Options:\n"
drh875bafa2015-04-24 14:47:59 +0000256" --autovacuum Enable AUTOVACUUM mode\n"
257" -f FILE Read SQL text from FILE instead of standard input\n"
258" --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
259" --help Show this help text\n"
260" --initdb DBFILE Initialize the in-memory database using template DBFILE\n"
261" --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
262" --pagesize N Set the page size to N\n"
263" --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
264" -q Reduced output\n"
265" --quiet Reduced output\n"
266" --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
267" --unique-cases FILE Write all unique test cases to FILE\n"
268" --utf16be Set text encoding to UTF-16BE\n"
269" --utf16le Set text encoding to UTF-16LE\n"
270" -v Increased output\n"
271" --verbose Increased output\n"
drh268e72f2015-04-17 14:30:49 +0000272 );
273}
274
drh4a74d072015-04-20 18:58:38 +0000275/*
276** Return the value of a hexadecimal digit. Return -1 if the input
277** is not a hex digit.
278*/
279static int hexDigitValue(char c){
280 if( c>='0' && c<='9' ) return c - '0';
281 if( c>='a' && c<='f' ) return c - 'a' + 10;
282 if( c>='A' && c<='F' ) return c - 'A' + 10;
283 return -1;
284}
285
286/*
287** Interpret zArg as an integer value, possibly with suffixes.
288*/
289static int integerValue(const char *zArg){
290 sqlite3_int64 v = 0;
291 static const struct { char *zSuffix; int iMult; } aMult[] = {
292 { "KiB", 1024 },
293 { "MiB", 1024*1024 },
294 { "GiB", 1024*1024*1024 },
295 { "KB", 1000 },
296 { "MB", 1000000 },
297 { "GB", 1000000000 },
298 { "K", 1000 },
299 { "M", 1000000 },
300 { "G", 1000000000 },
301 };
302 int i;
303 int isNeg = 0;
304 if( zArg[0]=='-' ){
305 isNeg = 1;
306 zArg++;
307 }else if( zArg[0]=='+' ){
308 zArg++;
309 }
310 if( zArg[0]=='0' && zArg[1]=='x' ){
311 int x;
312 zArg += 2;
313 while( (x = hexDigitValue(zArg[0]))>=0 ){
314 v = (v<<4) + x;
315 zArg++;
316 }
317 }else{
318 while( isdigit(zArg[0]) ){
319 v = v*10 + zArg[0] - '0';
320 zArg++;
321 }
322 }
323 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
324 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
325 v *= aMult[i].iMult;
326 break;
327 }
328 }
329 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
330 return (int)(isNeg? -v : v);
331}
332
drh9985dab2015-04-20 22:36:49 +0000333/*
334** Various operating modes
335*/
336#define FZMODE_Generic 1
337#define FZMODE_Strftime 2
338#define FZMODE_Printf 3
339#define FZMODE_Glob 4
340
drh268e72f2015-04-17 14:30:49 +0000341
342int main(int argc, char **argv){
343 char *zIn = 0; /* Input text */
344 int nAlloc = 0; /* Number of bytes allocated for zIn[] */
345 int nIn = 0; /* Number of bytes of zIn[] used */
346 size_t got; /* Bytes read from input */
347 FILE *in = stdin; /* Where to read SQL text from */
348 int rc = SQLITE_OK; /* Result codes from API functions */
349 int i; /* Loop counter */
drhf34e9aa2015-04-20 12:50:13 +0000350 int iNext; /* Next block of SQL */
drh268e72f2015-04-17 14:30:49 +0000351 sqlite3 *db; /* Open database */
drhf34e9aa2015-04-20 12:50:13 +0000352 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */
drh268e72f2015-04-17 14:30:49 +0000353 const char *zInitDb = 0;/* Name of the initialization database file */
354 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */
drh4a74d072015-04-20 18:58:38 +0000355 const char *zEncoding = 0; /* --utf16be or --utf16le */
356 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
357 int nLook = 0, szLook = 0; /* --lookaside configuration */
358 int nPCache = 0, szPCache = 0;/* --pcache configuration */
359 int nScratch = 0, szScratch=0;/* --scratch configuration */
360 int pageSize = 0; /* Desired page size. 0 means default */
361 void *pHeap = 0; /* Allocated heap space */
362 void *pLook = 0; /* Allocated lookaside space */
363 void *pPCache = 0; /* Allocated storage for pcache */
364 void *pScratch = 0; /* Allocated storage for scratch */
365 int doAutovac = 0; /* True for --autovacuum */
drh9985dab2015-04-20 22:36:49 +0000366 char *zSql; /* SQL to run */
367 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */
368 int iMode = FZMODE_Generic; /* Operating mode */
drh0ba51082015-04-22 13:16:46 +0000369 const char *zCkGlob = 0; /* Inputs must match this glob */
drh1cbb7fa2015-04-24 13:00:59 +0000370 int verboseFlag = 0; /* --verbose or -v flag */
371 int quietFlag = 0; /* --quiet or -q flag */
372 int nTest = 0; /* Number of test cases run */
373 int multiTest = 0; /* True if there will be multiple test cases */
374 int lastPct = -1; /* Previous percentage done output */
drh875bafa2015-04-24 14:47:59 +0000375 sqlite3 *dataDb = 0; /* Database holding compacted input data */
376 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */
377 const char *zDataOut = 0; /* Write compacted data to this output file */
drhe1a71a52015-04-24 16:09:12 +0000378 int nHeader = 0; /* Bytes of header comment text on input file */
drh4a74d072015-04-20 18:58:38 +0000379
drh268e72f2015-04-17 14:30:49 +0000380
381 g.zArgv0 = argv[0];
382 for(i=1; i<argc; i++){
383 const char *z = argv[i];
384 if( z[0]=='-' ){
385 z++;
386 if( z[0]=='-' ) z++;
drh4a74d072015-04-20 18:58:38 +0000387 if( strcmp(z,"autovacuum")==0 ){
388 doAutovac = 1;
drh268e72f2015-04-17 14:30:49 +0000389 }else
390 if( strcmp(z, "f")==0 && i+1<argc ){
391 if( in!=stdin ) abendError("only one -f allowed");
392 in = fopen(argv[++i],"rb");
393 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]);
394 }else
drh4a74d072015-04-20 18:58:38 +0000395 if( strcmp(z,"heap")==0 ){
396 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
397 nHeap = integerValue(argv[i+1]);
398 mnHeap = integerValue(argv[i+2]);
399 i += 2;
400 }else
401 if( strcmp(z,"help")==0 ){
402 showHelp();
403 return 0;
404 }else
drh268e72f2015-04-17 14:30:49 +0000405 if( strcmp(z, "initdb")==0 && i+1<argc ){
406 if( zInitDb!=0 ) abendError("only one --initdb allowed");
407 zInitDb = argv[++i];
408 }else
drh4a74d072015-04-20 18:58:38 +0000409 if( strcmp(z,"lookaside")==0 ){
410 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
411 nLook = integerValue(argv[i+1]);
412 szLook = integerValue(argv[i+2]);
413 i += 2;
414 }else
drh9985dab2015-04-20 22:36:49 +0000415 if( strcmp(z,"mode")==0 ){
416 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
417 z = argv[++i];
418 if( strcmp(z,"generic")==0 ){
419 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000420 zCkGlob = 0;
drh9985dab2015-04-20 22:36:49 +0000421 }else if( strcmp(z, "glob")==0 ){
422 iMode = FZMODE_Glob;
drh0ba51082015-04-22 13:16:46 +0000423 zCkGlob = "'*','*'";
drh9985dab2015-04-20 22:36:49 +0000424 }else if( strcmp(z, "printf")==0 ){
425 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000426 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000427 }else if( strcmp(z, "strftime")==0 ){
428 iMode = FZMODE_Strftime;
drh0ba51082015-04-22 13:16:46 +0000429 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000430 }else{
431 abendError("unknown --mode: %s", z);
432 }
433 }else
drh4a74d072015-04-20 18:58:38 +0000434 if( strcmp(z,"pagesize")==0 ){
435 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
436 pageSize = integerValue(argv[++i]);
437 }else
438 if( strcmp(z,"pcache")==0 ){
439 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
440 nPCache = integerValue(argv[i+1]);
441 szPCache = integerValue(argv[i+2]);
442 i += 2;
443 }else
drh1cbb7fa2015-04-24 13:00:59 +0000444 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
445 quietFlag = 1;
446 verboseFlag = 0;
447 }else
drh4a74d072015-04-20 18:58:38 +0000448 if( strcmp(z,"scratch")==0 ){
449 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
450 nScratch = integerValue(argv[i+1]);
451 szScratch = integerValue(argv[i+2]);
452 i += 2;
453 }else
drh875bafa2015-04-24 14:47:59 +0000454 if( strcmp(z, "unique-cases")==0 ){
455 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
456 if( zDataOut ) abendError("only one --minimize allowed");
457 zDataOut = argv[++i];
458 }else
drh4a74d072015-04-20 18:58:38 +0000459 if( strcmp(z,"utf16le")==0 ){
460 zEncoding = "utf16le";
461 }else
462 if( strcmp(z,"utf16be")==0 ){
463 zEncoding = "utf16be";
464 }else
drh1cbb7fa2015-04-24 13:00:59 +0000465 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
466 quietFlag = 0;
467 verboseFlag = 1;
468 }else
drh268e72f2015-04-17 14:30:49 +0000469 {
470 abendError("unknown option: %s", argv[i]);
471 }
472 }else{
473 abendError("unknown argument: %s", argv[i]);
474 }
475 }
drh1cbb7fa2015-04-24 13:00:59 +0000476 if( verboseFlag ) sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0);
drh4a74d072015-04-20 18:58:38 +0000477 if( nHeap>0 ){
478 pHeap = malloc( nHeap );
479 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
480 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
481 if( rc ) abendError("heap configuration failed: %d\n", rc);
482 }
483 if( nLook>0 ){
484 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
485 if( szLook>0 ){
486 pLook = malloc( nLook*szLook );
487 if( pLook==0 ) fatalError("out of memory");
488 }
489 }
490 if( nScratch>0 && szScratch>0 ){
491 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
492 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch",
493 nScratch*(sqlite3_int64)szScratch);
494 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
495 if( rc ) abendError("scratch configuration failed: %d\n", rc);
496 }
497 if( nPCache>0 && szPCache>0 ){
498 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
499 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
500 nPCache*(sqlite3_int64)szPCache);
501 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
502 if( rc ) abendError("pcache configuration failed: %d", rc);
503 }
drh268e72f2015-04-17 14:30:49 +0000504 while( !feof(in) ){
drhf34e9aa2015-04-20 12:50:13 +0000505 nAlloc += nAlloc+1000;
506 zIn = realloc(zIn, nAlloc);
drh268e72f2015-04-17 14:30:49 +0000507 if( zIn==0 ) fatalError("out of memory");
508 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in);
509 nIn += (int)got;
510 zIn[nIn] = 0;
511 if( got==0 ) break;
512 }
drh875bafa2015-04-24 14:47:59 +0000513 if( in!=stdin ) fclose(in);
514 if( zDataOut ){
515 rc = sqlite3_open(":memory:", &dataDb);
516 if( rc ) abendError("cannot open :memory: database");
517 rc = sqlite3_exec(dataDb,
518 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0);
519 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
520 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)",
521 -1, &pStmt, 0);
522 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
523 }
drhf34e9aa2015-04-20 12:50:13 +0000524 if( zInitDb ){
525 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
526 if( rc!=SQLITE_OK ){
527 abendError("unable to open initialization database \"%s\"", zInitDb);
528 }
drh268e72f2015-04-17 14:30:49 +0000529 }
drhe1a71a52015-04-24 16:09:12 +0000530 for(i=0; i<nIn; i=iNext+1){ /* Skip initial lines beginning with '#' */
531 if( zIn[i]!='#' ) break;
532 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){}
533 }
534 nHeader = i;
535 for(nTest=0; i<nIn; i=iNext, nTest++){
drhf34e9aa2015-04-20 12:50:13 +0000536 char cSaved;
537 if( strncmp(&zIn[i], "/****<",6)==0 ){
538 char *z = strstr(&zIn[i], ">****/");
539 if( z ){
540 z += 6;
drh1cbb7fa2015-04-24 13:00:59 +0000541 if( verboseFlag ) printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
drhf34e9aa2015-04-20 12:50:13 +0000542 i += (int)(z-&zIn[i]);
drh1cbb7fa2015-04-24 13:00:59 +0000543 multiTest = 1;
drhf34e9aa2015-04-20 12:50:13 +0000544 }
545 }
546 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
drh875bafa2015-04-24 14:47:59 +0000547 if( zDataOut ){
548 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
549 rc = sqlite3_step(pStmt);
550 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
551 sqlite3_reset(pStmt);
552 continue;
553 }
drh3fb2cc12015-04-22 11:16:34 +0000554 cSaved = zIn[iNext];
555 zIn[iNext] = 0;
drh0ba51082015-04-22 13:16:46 +0000556 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
drh3fb2cc12015-04-22 11:16:34 +0000557 zIn[iNext] = cSaved;
558 continue;
559 }
drhf34e9aa2015-04-20 12:50:13 +0000560 rc = sqlite3_open_v2(
561 "main.db", &db,
562 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
563 0);
564 if( rc!=SQLITE_OK ){
565 abendError("Unable to open the in-memory database");
566 }
drh4a74d072015-04-20 18:58:38 +0000567 if( pLook ){
568 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
569 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
570 }
drhf34e9aa2015-04-20 12:50:13 +0000571 if( zInitDb ){
572 sqlite3_backup *pBackup;
573 pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
574 rc = sqlite3_backup_step(pBackup, -1);
575 if( rc!=SQLITE_DONE ){
576 abendError("attempt to initialize the in-memory database failed (rc=%d)",
577 rc);
578 }
579 sqlite3_backup_finish(pBackup);
580 }
drh61a0d6b2015-04-24 18:31:12 +0000581#ifndef SQLITE_OMIT_TRACE
drh1cbb7fa2015-04-24 13:00:59 +0000582 if( verboseFlag ) sqlite3_trace(db, traceCallback, 0);
drh61a0d6b2015-04-24 18:31:12 +0000583#endif
drhf34e9aa2015-04-20 12:50:13 +0000584 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
585 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
586 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
drh4a74d072015-04-20 18:58:38 +0000587 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
588 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
589 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
drh9985dab2015-04-20 22:36:49 +0000590 zSql = &zIn[i];
drh1cbb7fa2015-04-24 13:00:59 +0000591 if( verboseFlag ){
592 printf("INPUT (offset: %d, size: %d): [%s]\n",
593 i, (int)strlen(&zIn[i]), &zIn[i]);
594 }else if( multiTest && !quietFlag ){
drhe1a71a52015-04-24 16:09:12 +0000595 int pct = 10*iNext/nIn;
drh1cbb7fa2015-04-24 13:00:59 +0000596 if( pct!=lastPct ){
drhe1a71a52015-04-24 16:09:12 +0000597 if( lastPct<0 ) printf("fuzz test:");
598 printf(" %d%%", pct*10);
drh1cbb7fa2015-04-24 13:00:59 +0000599 fflush(stdout);
600 lastPct = pct;
601 }
602 }
drh9985dab2015-04-20 22:36:49 +0000603 switch( iMode ){
604 case FZMODE_Glob:
605 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
606 break;
607 case FZMODE_Printf:
608 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
609 break;
610 case FZMODE_Strftime:
611 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
612 break;
613 }
drh1cbb7fa2015-04-24 13:00:59 +0000614 zErrMsg = 0;
615 rc = sqlite3_exec(db, zSql, verboseFlag ? execCallback : execNoop, 0, &zErrMsg);
drh9985dab2015-04-20 22:36:49 +0000616 if( zToFree ){
617 sqlite3_free(zToFree);
618 zToFree = 0;
619 }
drhf34e9aa2015-04-20 12:50:13 +0000620 zIn[iNext] = cSaved;
drh1cbb7fa2015-04-24 13:00:59 +0000621 if( verboseFlag ){
622 printf("RESULT-CODE: %d\n", rc);
623 if( zErrMsg ){
624 printf("ERROR-MSG: [%s]\n", zErrMsg);
625 }
drhf34e9aa2015-04-20 12:50:13 +0000626 }
drh1cbb7fa2015-04-24 13:00:59 +0000627 sqlite3_free(zErrMsg);
drhf34e9aa2015-04-20 12:50:13 +0000628 rc = sqlite3_close(db);
629 if( rc ){
630 abendError("sqlite3_close() failed with rc=%d", rc);
631 }
632 if( sqlite3_memory_used()>0 ){
633 abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
634 }
drhe1a71a52015-04-24 16:09:12 +0000635 if( nTest==1 ){
636 /* Simulate an error if the TEST_FAILURE environment variable is "5" */
637 char *zFailCode = getenv("TEST_FAILURE");
drh8ea5eca2015-04-24 16:53:03 +0000638 if( zFailCode ){
639 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
640 abendError("simulated failure");
641 }else if( zFailCode[0]!=0 ){
642 /* If TEST_FAILURE is something other than 5, just exit the test
643 ** early */
644 printf("\nExit early due to TEST_FAILURE being set");
645 break;
646 }
drhe1a71a52015-04-24 16:09:12 +0000647 }
648 }
drhf34e9aa2015-04-20 12:50:13 +0000649 }
drhe1a71a52015-04-24 16:09:12 +0000650 if( !verboseFlag && multiTest && !quietFlag ) printf("\n");
drh1cbb7fa2015-04-24 13:00:59 +0000651 if( nTest>1 && !quietFlag ){
drhe1a71a52015-04-24 16:09:12 +0000652 printf("%d fuzz tests with no errors\nSQLite %s %s\n",
drh875bafa2015-04-24 14:47:59 +0000653 nTest, sqlite3_libversion(), sqlite3_sourceid());
654 }
655 if( zDataOut ){
drh875bafa2015-04-24 14:47:59 +0000656 int n = 0;
drhe1a71a52015-04-24 16:09:12 +0000657 FILE *out = fopen(zDataOut, "wb");
drh875bafa2015-04-24 14:47:59 +0000658 if( out==0 ) abendError("cannot open %s for writing", zDataOut);
drhe1a71a52015-04-24 16:09:12 +0000659 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out);
drh875bafa2015-04-24 14:47:59 +0000660 sqlite3_finalize(pStmt);
661 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0);
662 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
663 while( sqlite3_step(pStmt)==SQLITE_ROW ){
664 fprintf(out,"/****<%d>****/", ++n);
665 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
666 }
667 fclose(out);
668 sqlite3_finalize(pStmt);
669 sqlite3_close(dataDb);
drh1cbb7fa2015-04-24 13:00:59 +0000670 }
drhf34e9aa2015-04-20 12:50:13 +0000671 free(zIn);
drh4a74d072015-04-20 18:58:38 +0000672 free(pHeap);
673 free(pLook);
674 free(pScratch);
675 free(pPCache);
drhf34e9aa2015-04-20 12:50:13 +0000676 return 0;
drh268e72f2015-04-17 14:30:49 +0000677}