blob: f2f493059df2f074e8a5ad30799b258d7158cce0 [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**
drhbe5248f2015-04-25 11:35:48 +000035** (5) An error is raised if there is a memory leak.
36**
37** The input text can be divided into separate test cases using comments
38** of the form:
drhf34e9aa2015-04-20 12:50:13 +000039**
40** |****<...>****|
41**
drhf3320712015-04-25 13:39:29 +000042** where the "..." is arbitrary text. (Except the "|" should really be "/".
43** "|" is used here to avoid compiler errors about nested comments.)
drhbe5248f2015-04-25 11:35:48 +000044** A separate in-memory SQLite database is created to run each test case.
drh875bafa2015-04-24 14:47:59 +000045** This feature allows the "queue" of AFL to be captured into a single big
drhf34e9aa2015-04-20 12:50:13 +000046** file using a command like this:
47**
48** (for i in id:*; do echo '|****<'$i'>****|'; cat $i; done) >~/all-queue.txt
49**
50** (Once again, change the "|" to "/") Then all elements of the AFL queue
drh4a74d072015-04-20 18:58:38 +000051** can be run in a single go (for regression testing, for example) by typing:
drhf34e9aa2015-04-20 12:50:13 +000052**
drh875bafa2015-04-24 14:47:59 +000053** fuzzershell -f ~/all-queue.txt
drhf34e9aa2015-04-20 12:50:13 +000054**
55** After running each chunk of SQL, the database connection is closed. The
56** program aborts if the close fails or if there is any unfreed memory after
57** the close.
drh875bafa2015-04-24 14:47:59 +000058**
drhbe5248f2015-04-25 11:35:48 +000059** New test cases can be appended to all-queue.txt at any time. If redundant
60** test cases are added, they can be eliminated by running:
drh875bafa2015-04-24 14:47:59 +000061**
62** fuzzershell -f ~/all-queue.txt --unique-cases ~/unique-cases.txt
drh268e72f2015-04-17 14:30:49 +000063*/
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <stdarg.h>
drh4a74d072015-04-20 18:58:38 +000068#include <ctype.h>
drh268e72f2015-04-17 14:30:49 +000069#include "sqlite3.h"
70
71/*
72** All global variables are gathered into the "g" singleton.
73*/
74struct GlobalVars {
drh048810b2015-04-24 23:45:23 +000075 const char *zArgv0; /* Name of program */
76 sqlite3_mem_methods sOrigMem; /* Original memory methods */
77 sqlite3_mem_methods sOomMem; /* Memory methods with OOM simulator */
78 int iOomCntdown; /* Memory fails on 1 to 0 transition */
79 int nOomFault; /* Increments for each OOM fault */
80 int bOomOnce; /* Fail just once if true */
81 int bOomEnable; /* True to enable OOM simulation */
82 int nOomBrkpt; /* Number of calls to oomFault() */
drh0ee751f2015-04-25 11:19:51 +000083 char zTestName[100]; /* Name of current test */
drh268e72f2015-04-17 14:30:49 +000084} g;
85
drh048810b2015-04-24 23:45:23 +000086/*
drhf3320712015-04-25 13:39:29 +000087** Maximum number of iterations for an OOM test
88*/
89#ifndef OOM_MAX
90# define OOM_MAX 1000
91#endif
92
93/*
drh048810b2015-04-24 23:45:23 +000094** This routine is called when a simulated OOM occurs. It exists as a
95** convenient place to set a debugger breakpoint.
96*/
97static void oomFault(void){
drhbe5248f2015-04-25 11:35:48 +000098 g.nOomBrkpt++; /* Prevent oomFault() from being optimized out */
drh048810b2015-04-24 23:45:23 +000099}
drh268e72f2015-04-17 14:30:49 +0000100
101
drh048810b2015-04-24 23:45:23 +0000102/* Versions of malloc() and realloc() that simulate OOM conditions */
103static void *oomMalloc(int nByte){
104 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
105 g.iOomCntdown--;
106 if( g.iOomCntdown==0 ){
107 if( g.nOomFault==0 ) oomFault();
108 g.nOomFault++;
109 if( !g.bOomOnce ) g.iOomCntdown = 1;
110 return 0;
111 }
112 }
113 return g.sOrigMem.xMalloc(nByte);
114}
115static void *oomRealloc(void *pOld, int nByte){
116 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
117 g.iOomCntdown--;
118 if( g.iOomCntdown==0 ){
119 if( g.nOomFault==0 ) oomFault();
120 g.nOomFault++;
121 if( !g.bOomOnce ) g.iOomCntdown = 1;
122 return 0;
123 }
124 }
125 return g.sOrigMem.xRealloc(pOld, nByte);
126}
127
drh268e72f2015-04-17 14:30:49 +0000128/*
129** Print an error message and abort in such a way to indicate to the
130** fuzzer that this counts as a crash.
131*/
132static void abendError(const char *zFormat, ...){
133 va_list ap;
drhbe5248f2015-04-25 11:35:48 +0000134 if( g.zTestName[0] ){
135 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
136 }else{
137 fprintf(stderr, "%s: ", g.zArgv0);
138 }
drh268e72f2015-04-17 14:30:49 +0000139 va_start(ap, zFormat);
140 vfprintf(stderr, zFormat, ap);
141 va_end(ap);
142 fprintf(stderr, "\n");
143 abort();
144}
145/*
146** Print an error message and quit, but not in a way that would look
147** like a crash.
148*/
149static void fatalError(const char *zFormat, ...){
150 va_list ap;
drhbe5248f2015-04-25 11:35:48 +0000151 if( g.zTestName[0] ){
152 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
153 }else{
154 fprintf(stderr, "%s: ", g.zArgv0);
155 }
drh268e72f2015-04-17 14:30:49 +0000156 va_start(ap, zFormat);
157 vfprintf(stderr, zFormat, ap);
158 va_end(ap);
159 fprintf(stderr, "\n");
160 exit(1);
161}
162
163/*
drh4a74d072015-04-20 18:58:38 +0000164** Evaluate some SQL. Abort if unable.
165*/
166static void sqlexec(sqlite3 *db, const char *zFormat, ...){
167 va_list ap;
168 char *zSql;
169 char *zErrMsg = 0;
170 int rc;
171 va_start(ap, zFormat);
172 zSql = sqlite3_vmprintf(zFormat, ap);
173 va_end(ap);
174 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
175 if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg);
176 sqlite3_free(zSql);
177}
178
179/*
drh268e72f2015-04-17 14:30:49 +0000180** This callback is invoked by sqlite3_log().
181*/
182static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){
183 printf("LOG: (%d) %s\n", iErrCode, zMsg);
drh9f18f742015-04-25 00:20:15 +0000184 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000185}
drh0ee751f2015-04-25 11:19:51 +0000186static void shellLogNoop(void *pNotUsed, int iErrCode, const char *zMsg){
187 return;
188}
drh268e72f2015-04-17 14:30:49 +0000189
190/*
191** This callback is invoked by sqlite3_exec() to return query results.
192*/
193static int execCallback(void *NotUsed, int argc, char **argv, char **colv){
194 int i;
195 static unsigned cnt = 0;
196 printf("ROW #%u:\n", ++cnt);
197 for(i=0; i<argc; i++){
198 printf(" %s=", colv[i]);
199 if( argv[i] ){
200 printf("[%s]\n", argv[i]);
201 }else{
202 printf("NULL\n");
203 }
204 }
drh9f18f742015-04-25 00:20:15 +0000205 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000206 return 0;
207}
drh1cbb7fa2015-04-24 13:00:59 +0000208static int execNoop(void *NotUsed, int argc, char **argv, char **colv){
209 return 0;
210}
drh268e72f2015-04-17 14:30:49 +0000211
drh61a0d6b2015-04-24 18:31:12 +0000212#ifndef SQLITE_OMIT_TRACE
drh268e72f2015-04-17 14:30:49 +0000213/*
214** This callback is invoked by sqlite3_trace() as each SQL statement
215** starts.
216*/
217static void traceCallback(void *NotUsed, const char *zMsg){
218 printf("TRACE: %s\n", zMsg);
drh9f18f742015-04-25 00:20:15 +0000219 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000220}
drh0ee751f2015-04-25 11:19:51 +0000221static void traceNoop(void *NotUsed, const char *zMsg){
222 return;
223}
drh61a0d6b2015-04-24 18:31:12 +0000224#endif
drh268e72f2015-04-17 14:30:49 +0000225
226/***************************************************************************
227** eval() implementation copied from ../ext/misc/eval.c
228*/
229/*
230** Structure used to accumulate the output
231*/
232struct EvalResult {
233 char *z; /* Accumulated output */
234 const char *zSep; /* Separator */
235 int szSep; /* Size of the separator string */
236 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */
237 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */
238};
239
240/*
241** Callback from sqlite_exec() for the eval() function.
242*/
243static int callback(void *pCtx, int argc, char **argv, char **colnames){
244 struct EvalResult *p = (struct EvalResult*)pCtx;
245 int i;
246 for(i=0; i<argc; i++){
247 const char *z = argv[i] ? argv[i] : "";
248 size_t sz = strlen(z);
249 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
250 char *zNew;
251 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
252 /* Using sqlite3_realloc64() would be better, but it is a recent
253 ** addition and will cause a segfault if loaded by an older version
254 ** of SQLite. */
255 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
256 if( zNew==0 ){
257 sqlite3_free(p->z);
258 memset(p, 0, sizeof(*p));
259 return 1;
260 }
261 p->z = zNew;
262 }
263 if( p->nUsed>0 ){
264 memcpy(&p->z[p->nUsed], p->zSep, p->szSep);
265 p->nUsed += p->szSep;
266 }
267 memcpy(&p->z[p->nUsed], z, sz);
268 p->nUsed += sz;
269 }
270 return 0;
271}
272
273/*
274** Implementation of the eval(X) and eval(X,Y) SQL functions.
275**
276** Evaluate the SQL text in X. Return the results, using string
277** Y as the separator. If Y is omitted, use a single space character.
278*/
279static void sqlEvalFunc(
280 sqlite3_context *context,
281 int argc,
282 sqlite3_value **argv
283){
284 const char *zSql;
285 sqlite3 *db;
286 char *zErr = 0;
287 int rc;
288 struct EvalResult x;
289
290 memset(&x, 0, sizeof(x));
291 x.zSep = " ";
292 zSql = (const char*)sqlite3_value_text(argv[0]);
293 if( zSql==0 ) return;
294 if( argc>1 ){
295 x.zSep = (const char*)sqlite3_value_text(argv[1]);
296 if( x.zSep==0 ) return;
297 }
298 x.szSep = (int)strlen(x.zSep);
299 db = sqlite3_context_db_handle(context);
300 rc = sqlite3_exec(db, zSql, callback, &x, &zErr);
301 if( rc!=SQLITE_OK ){
302 sqlite3_result_error(context, zErr, -1);
303 sqlite3_free(zErr);
304 }else if( x.zSep==0 ){
305 sqlite3_result_error_nomem(context);
306 sqlite3_free(x.z);
307 }else{
308 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
309 }
310}
311/* End of the eval() implementation
312******************************************************************************/
313
314/*
315** Print sketchy documentation for this utility program
316*/
317static void showHelp(void){
318 printf("Usage: %s [options]\n", g.zArgv0);
319 printf(
320"Read SQL text from standard input and evaluate it.\n"
321"Options:\n"
drh875bafa2015-04-24 14:47:59 +0000322" --autovacuum Enable AUTOVACUUM mode\n"
323" -f FILE Read SQL text from FILE instead of standard input\n"
324" --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
325" --help Show this help text\n"
326" --initdb DBFILE Initialize the in-memory database using template DBFILE\n"
327" --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
drh048810b2015-04-24 23:45:23 +0000328" --oom Run each test multiple times in a simulated OOM loop\n"
drh875bafa2015-04-24 14:47:59 +0000329" --pagesize N Set the page size to N\n"
330" --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
331" -q Reduced output\n"
332" --quiet Reduced output\n"
333" --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
334" --unique-cases FILE Write all unique test cases to FILE\n"
335" --utf16be Set text encoding to UTF-16BE\n"
336" --utf16le Set text encoding to UTF-16LE\n"
337" -v Increased output\n"
338" --verbose Increased output\n"
drh268e72f2015-04-17 14:30:49 +0000339 );
340}
341
drh4a74d072015-04-20 18:58:38 +0000342/*
343** Return the value of a hexadecimal digit. Return -1 if the input
344** is not a hex digit.
345*/
346static int hexDigitValue(char c){
347 if( c>='0' && c<='9' ) return c - '0';
348 if( c>='a' && c<='f' ) return c - 'a' + 10;
349 if( c>='A' && c<='F' ) return c - 'A' + 10;
350 return -1;
351}
352
353/*
354** Interpret zArg as an integer value, possibly with suffixes.
355*/
356static int integerValue(const char *zArg){
357 sqlite3_int64 v = 0;
358 static const struct { char *zSuffix; int iMult; } aMult[] = {
359 { "KiB", 1024 },
360 { "MiB", 1024*1024 },
361 { "GiB", 1024*1024*1024 },
362 { "KB", 1000 },
363 { "MB", 1000000 },
364 { "GB", 1000000000 },
365 { "K", 1000 },
366 { "M", 1000000 },
367 { "G", 1000000000 },
368 };
369 int i;
370 int isNeg = 0;
371 if( zArg[0]=='-' ){
372 isNeg = 1;
373 zArg++;
374 }else if( zArg[0]=='+' ){
375 zArg++;
376 }
377 if( zArg[0]=='0' && zArg[1]=='x' ){
378 int x;
379 zArg += 2;
380 while( (x = hexDigitValue(zArg[0]))>=0 ){
381 v = (v<<4) + x;
382 zArg++;
383 }
384 }else{
385 while( isdigit(zArg[0]) ){
386 v = v*10 + zArg[0] - '0';
387 zArg++;
388 }
389 }
390 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
391 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
392 v *= aMult[i].iMult;
393 break;
394 }
395 }
396 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
397 return (int)(isNeg? -v : v);
398}
399
drh9985dab2015-04-20 22:36:49 +0000400/*
401** Various operating modes
402*/
403#define FZMODE_Generic 1
404#define FZMODE_Strftime 2
405#define FZMODE_Printf 3
406#define FZMODE_Glob 4
407
drh268e72f2015-04-17 14:30:49 +0000408
409int main(int argc, char **argv){
410 char *zIn = 0; /* Input text */
411 int nAlloc = 0; /* Number of bytes allocated for zIn[] */
412 int nIn = 0; /* Number of bytes of zIn[] used */
413 size_t got; /* Bytes read from input */
414 FILE *in = stdin; /* Where to read SQL text from */
415 int rc = SQLITE_OK; /* Result codes from API functions */
416 int i; /* Loop counter */
drhf34e9aa2015-04-20 12:50:13 +0000417 int iNext; /* Next block of SQL */
drh268e72f2015-04-17 14:30:49 +0000418 sqlite3 *db; /* Open database */
drhf34e9aa2015-04-20 12:50:13 +0000419 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */
drh268e72f2015-04-17 14:30:49 +0000420 const char *zInitDb = 0;/* Name of the initialization database file */
421 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */
drh4a74d072015-04-20 18:58:38 +0000422 const char *zEncoding = 0; /* --utf16be or --utf16le */
423 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
424 int nLook = 0, szLook = 0; /* --lookaside configuration */
425 int nPCache = 0, szPCache = 0;/* --pcache configuration */
426 int nScratch = 0, szScratch=0;/* --scratch configuration */
427 int pageSize = 0; /* Desired page size. 0 means default */
428 void *pHeap = 0; /* Allocated heap space */
429 void *pLook = 0; /* Allocated lookaside space */
430 void *pPCache = 0; /* Allocated storage for pcache */
431 void *pScratch = 0; /* Allocated storage for scratch */
432 int doAutovac = 0; /* True for --autovacuum */
drh9985dab2015-04-20 22:36:49 +0000433 char *zSql; /* SQL to run */
434 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */
435 int iMode = FZMODE_Generic; /* Operating mode */
drh0ba51082015-04-22 13:16:46 +0000436 const char *zCkGlob = 0; /* Inputs must match this glob */
drh1cbb7fa2015-04-24 13:00:59 +0000437 int verboseFlag = 0; /* --verbose or -v flag */
438 int quietFlag = 0; /* --quiet or -q flag */
439 int nTest = 0; /* Number of test cases run */
440 int multiTest = 0; /* True if there will be multiple test cases */
441 int lastPct = -1; /* Previous percentage done output */
drh875bafa2015-04-24 14:47:59 +0000442 sqlite3 *dataDb = 0; /* Database holding compacted input data */
443 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */
444 const char *zDataOut = 0; /* Write compacted data to this output file */
drhe1a71a52015-04-24 16:09:12 +0000445 int nHeader = 0; /* Bytes of header comment text on input file */
drh048810b2015-04-24 23:45:23 +0000446 int oomFlag = 0; /* --oom */
447 int oomCnt = 0; /* Counter for the OOM loop */
448 char zErrBuf[200]; /* Space for the error message */
449 const char *zFailCode; /* Value of the TEST_FAILURE environment var */
drh4a74d072015-04-20 18:58:38 +0000450
drh268e72f2015-04-17 14:30:49 +0000451
drh048810b2015-04-24 23:45:23 +0000452 zFailCode = getenv("TEST_FAILURE");
drh268e72f2015-04-17 14:30:49 +0000453 g.zArgv0 = argv[0];
454 for(i=1; i<argc; i++){
455 const char *z = argv[i];
456 if( z[0]=='-' ){
457 z++;
458 if( z[0]=='-' ) z++;
drh4a74d072015-04-20 18:58:38 +0000459 if( strcmp(z,"autovacuum")==0 ){
460 doAutovac = 1;
drh268e72f2015-04-17 14:30:49 +0000461 }else
462 if( strcmp(z, "f")==0 && i+1<argc ){
463 if( in!=stdin ) abendError("only one -f allowed");
464 in = fopen(argv[++i],"rb");
465 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]);
466 }else
drh4a74d072015-04-20 18:58:38 +0000467 if( strcmp(z,"heap")==0 ){
468 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
469 nHeap = integerValue(argv[i+1]);
470 mnHeap = integerValue(argv[i+2]);
471 i += 2;
472 }else
473 if( strcmp(z,"help")==0 ){
474 showHelp();
475 return 0;
476 }else
drh268e72f2015-04-17 14:30:49 +0000477 if( strcmp(z, "initdb")==0 && i+1<argc ){
478 if( zInitDb!=0 ) abendError("only one --initdb allowed");
479 zInitDb = argv[++i];
480 }else
drh4a74d072015-04-20 18:58:38 +0000481 if( strcmp(z,"lookaside")==0 ){
482 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
483 nLook = integerValue(argv[i+1]);
484 szLook = integerValue(argv[i+2]);
485 i += 2;
486 }else
drh9985dab2015-04-20 22:36:49 +0000487 if( strcmp(z,"mode")==0 ){
488 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
489 z = argv[++i];
490 if( strcmp(z,"generic")==0 ){
491 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000492 zCkGlob = 0;
drh9985dab2015-04-20 22:36:49 +0000493 }else if( strcmp(z, "glob")==0 ){
494 iMode = FZMODE_Glob;
drh0ba51082015-04-22 13:16:46 +0000495 zCkGlob = "'*','*'";
drh9985dab2015-04-20 22:36:49 +0000496 }else if( strcmp(z, "printf")==0 ){
497 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000498 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000499 }else if( strcmp(z, "strftime")==0 ){
500 iMode = FZMODE_Strftime;
drh0ba51082015-04-22 13:16:46 +0000501 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000502 }else{
503 abendError("unknown --mode: %s", z);
504 }
505 }else
drh048810b2015-04-24 23:45:23 +0000506 if( strcmp(z,"oom")==0 ){
507 oomFlag = 1;
508 }else
drh4a74d072015-04-20 18:58:38 +0000509 if( strcmp(z,"pagesize")==0 ){
510 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
511 pageSize = integerValue(argv[++i]);
512 }else
513 if( strcmp(z,"pcache")==0 ){
514 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
515 nPCache = integerValue(argv[i+1]);
516 szPCache = integerValue(argv[i+2]);
517 i += 2;
518 }else
drh1cbb7fa2015-04-24 13:00:59 +0000519 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
520 quietFlag = 1;
521 verboseFlag = 0;
522 }else
drh4a74d072015-04-20 18:58:38 +0000523 if( strcmp(z,"scratch")==0 ){
524 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
525 nScratch = integerValue(argv[i+1]);
526 szScratch = integerValue(argv[i+2]);
527 i += 2;
528 }else
drh875bafa2015-04-24 14:47:59 +0000529 if( strcmp(z, "unique-cases")==0 ){
530 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
531 if( zDataOut ) abendError("only one --minimize allowed");
532 zDataOut = argv[++i];
533 }else
drh4a74d072015-04-20 18:58:38 +0000534 if( strcmp(z,"utf16le")==0 ){
535 zEncoding = "utf16le";
536 }else
537 if( strcmp(z,"utf16be")==0 ){
538 zEncoding = "utf16be";
539 }else
drh1cbb7fa2015-04-24 13:00:59 +0000540 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
541 quietFlag = 0;
542 verboseFlag = 1;
543 }else
drh268e72f2015-04-17 14:30:49 +0000544 {
545 abendError("unknown option: %s", argv[i]);
546 }
547 }else{
548 abendError("unknown argument: %s", argv[i]);
549 }
550 }
drh0ee751f2015-04-25 11:19:51 +0000551 sqlite3_config(SQLITE_CONFIG_LOG, verboseFlag ? shellLog : shellLogNoop, 0);
drh4a74d072015-04-20 18:58:38 +0000552 if( nHeap>0 ){
553 pHeap = malloc( nHeap );
554 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
555 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
556 if( rc ) abendError("heap configuration failed: %d\n", rc);
557 }
drh048810b2015-04-24 23:45:23 +0000558 if( oomFlag ){
559 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem);
560 g.sOomMem = g.sOrigMem;
561 g.sOomMem.xMalloc = oomMalloc;
562 g.sOomMem.xRealloc = oomRealloc;
563 sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem);
564 }
drh4a74d072015-04-20 18:58:38 +0000565 if( nLook>0 ){
566 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
567 if( szLook>0 ){
568 pLook = malloc( nLook*szLook );
569 if( pLook==0 ) fatalError("out of memory");
570 }
571 }
572 if( nScratch>0 && szScratch>0 ){
573 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
574 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch",
575 nScratch*(sqlite3_int64)szScratch);
576 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
577 if( rc ) abendError("scratch configuration failed: %d\n", rc);
578 }
579 if( nPCache>0 && szPCache>0 ){
580 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
581 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
582 nPCache*(sqlite3_int64)szPCache);
583 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
584 if( rc ) abendError("pcache configuration failed: %d", rc);
585 }
drh268e72f2015-04-17 14:30:49 +0000586 while( !feof(in) ){
drhf34e9aa2015-04-20 12:50:13 +0000587 nAlloc += nAlloc+1000;
588 zIn = realloc(zIn, nAlloc);
drh268e72f2015-04-17 14:30:49 +0000589 if( zIn==0 ) fatalError("out of memory");
590 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in);
591 nIn += (int)got;
592 zIn[nIn] = 0;
593 if( got==0 ) break;
594 }
drh875bafa2015-04-24 14:47:59 +0000595 if( in!=stdin ) fclose(in);
596 if( zDataOut ){
597 rc = sqlite3_open(":memory:", &dataDb);
598 if( rc ) abendError("cannot open :memory: database");
599 rc = sqlite3_exec(dataDb,
600 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0);
601 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
602 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)",
603 -1, &pStmt, 0);
604 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
605 }
drhf34e9aa2015-04-20 12:50:13 +0000606 if( zInitDb ){
607 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
608 if( rc!=SQLITE_OK ){
609 abendError("unable to open initialization database \"%s\"", zInitDb);
610 }
drh268e72f2015-04-17 14:30:49 +0000611 }
drhe1a71a52015-04-24 16:09:12 +0000612 for(i=0; i<nIn; i=iNext+1){ /* Skip initial lines beginning with '#' */
613 if( zIn[i]!='#' ) break;
614 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){}
615 }
616 nHeader = i;
drhbe5248f2015-04-25 11:35:48 +0000617 for(nTest=0; i<nIn; i=iNext, nTest++, g.zTestName[0]=0){
drhf34e9aa2015-04-20 12:50:13 +0000618 char cSaved;
619 if( strncmp(&zIn[i], "/****<",6)==0 ){
620 char *z = strstr(&zIn[i], ">****/");
621 if( z ){
622 z += 6;
drhbe5248f2015-04-25 11:35:48 +0000623 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s",
drhf3320712015-04-25 13:39:29 +0000624 (int)(z-&zIn[i]) - 12, &zIn[i+6]);
drh9f18f742015-04-25 00:20:15 +0000625 if( verboseFlag ){
626 printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
627 fflush(stdout);
628 }
drhf34e9aa2015-04-20 12:50:13 +0000629 i += (int)(z-&zIn[i]);
drh1cbb7fa2015-04-24 13:00:59 +0000630 multiTest = 1;
drhf34e9aa2015-04-20 12:50:13 +0000631 }
632 }
633 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
drh875bafa2015-04-24 14:47:59 +0000634 if( zDataOut ){
635 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
636 rc = sqlite3_step(pStmt);
637 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
638 sqlite3_reset(pStmt);
639 continue;
640 }
drh3fb2cc12015-04-22 11:16:34 +0000641 cSaved = zIn[iNext];
642 zIn[iNext] = 0;
drh0ba51082015-04-22 13:16:46 +0000643 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
drh3fb2cc12015-04-22 11:16:34 +0000644 zIn[iNext] = cSaved;
645 continue;
646 }
drh9985dab2015-04-20 22:36:49 +0000647 zSql = &zIn[i];
drh1cbb7fa2015-04-24 13:00:59 +0000648 if( verboseFlag ){
649 printf("INPUT (offset: %d, size: %d): [%s]\n",
650 i, (int)strlen(&zIn[i]), &zIn[i]);
651 }else if( multiTest && !quietFlag ){
drhf3320712015-04-25 13:39:29 +0000652 if( oomFlag ){
653 printf("%s\n", g.zTestName);
654 }else{
655 int pct = (10*iNext)/nIn;
656 if( pct!=lastPct ){
657 if( lastPct<0 ) printf("fuzz test:");
658 printf(" %d%%", pct*10);
659 lastPct = pct;
660 }
drh1cbb7fa2015-04-24 13:00:59 +0000661 }
662 }
drhf3320712015-04-25 13:39:29 +0000663 fflush(stdout);
drh9985dab2015-04-20 22:36:49 +0000664 switch( iMode ){
665 case FZMODE_Glob:
666 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
667 break;
668 case FZMODE_Printf:
669 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
670 break;
671 case FZMODE_Strftime:
672 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
673 break;
674 }
drh048810b2015-04-24 23:45:23 +0000675 if( oomFlag ){
676 oomCnt = g.iOomCntdown = 1;
677 g.nOomFault = 0;
678 g.bOomOnce = 1;
drh9f18f742015-04-25 00:20:15 +0000679 if( verboseFlag ){
680 printf("Once.%d\n", oomCnt);
681 fflush(stdout);
682 }
drh048810b2015-04-24 23:45:23 +0000683 }else{
684 oomCnt = 0;
685 }
686 do{
687 rc = sqlite3_open_v2(
688 "main.db", &db,
689 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
690 0);
691 if( rc!=SQLITE_OK ){
692 abendError("Unable to open the in-memory database");
693 }
694 if( pLook ){
695 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
696 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
697 }
698 if( zInitDb ){
699 sqlite3_backup *pBackup;
700 pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
701 rc = sqlite3_backup_step(pBackup, -1);
702 if( rc!=SQLITE_DONE ){
703 abendError("attempt to initialize the in-memory database failed (rc=%d)",
704 rc);
705 }
706 sqlite3_backup_finish(pBackup);
707 }
708 #ifndef SQLITE_OMIT_TRACE
drh0ee751f2015-04-25 11:19:51 +0000709 sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0);
drh048810b2015-04-24 23:45:23 +0000710 #endif
711 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
712 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
713 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
714 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
715 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
716 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
717 g.bOomEnable = 1;
718 if( verboseFlag ){
719 zErrMsg = 0;
720 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg);
721 if( zErrMsg ){
722 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg);
723 zErrMsg = 0;
724 }
725 }else {
726 rc = sqlite3_exec(db, zSql, execNoop, 0, 0);
727 }
728 g.bOomEnable = 0;
729 rc = sqlite3_close(db);
730 if( rc ){
731 abendError("sqlite3_close() failed with rc=%d", rc);
732 }
733 if( sqlite3_memory_used()>0 ){
734 abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
735 }
736 if( oomFlag ){
drhf3320712015-04-25 13:39:29 +0000737 if( g.nOomFault==0 || oomCnt>OOM_MAX ){
drh048810b2015-04-24 23:45:23 +0000738 if( g.bOomOnce ){
739 oomCnt = g.iOomCntdown = 1;
740 g.bOomOnce = 0;
741 }else{
742 oomCnt = 0;
743 }
744 }else{
745 g.iOomCntdown = ++oomCnt;
746 g.nOomFault = 0;
747 }
748 if( oomCnt ){
749 if( verboseFlag ){
750 printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt);
drh9f18f742015-04-25 00:20:15 +0000751 fflush(stdout);
drh048810b2015-04-24 23:45:23 +0000752 }
753 nTest++;
754 }
755 }
756 }while( oomCnt>0 );
drh9985dab2015-04-20 22:36:49 +0000757 if( zToFree ){
758 sqlite3_free(zToFree);
759 zToFree = 0;
760 }
drhf34e9aa2015-04-20 12:50:13 +0000761 zIn[iNext] = cSaved;
drh1cbb7fa2015-04-24 13:00:59 +0000762 if( verboseFlag ){
763 printf("RESULT-CODE: %d\n", rc);
764 if( zErrMsg ){
drh048810b2015-04-24 23:45:23 +0000765 printf("ERROR-MSG: [%s]\n", zErrBuf);
drh1cbb7fa2015-04-24 13:00:59 +0000766 }
drh9f18f742015-04-25 00:20:15 +0000767 fflush(stdout);
drhf34e9aa2015-04-20 12:50:13 +0000768 }
drh048810b2015-04-24 23:45:23 +0000769 /* Simulate an error if the TEST_FAILURE environment variable is "5" */
770 if( zFailCode ){
771 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
772 abendError("simulated failure");
773 }else if( zFailCode[0]!=0 ){
774 /* If TEST_FAILURE is something other than 5, just exit the test
775 ** early */
776 printf("\nExit early due to TEST_FAILURE being set");
777 break;
drhe1a71a52015-04-24 16:09:12 +0000778 }
779 }
drhf34e9aa2015-04-20 12:50:13 +0000780 }
drhf3320712015-04-25 13:39:29 +0000781 if( !verboseFlag && multiTest && !quietFlag && !oomFlag ) printf("\n");
drh1cbb7fa2015-04-24 13:00:59 +0000782 if( nTest>1 && !quietFlag ){
drhe1a71a52015-04-24 16:09:12 +0000783 printf("%d fuzz tests with no errors\nSQLite %s %s\n",
drh875bafa2015-04-24 14:47:59 +0000784 nTest, sqlite3_libversion(), sqlite3_sourceid());
785 }
786 if( zDataOut ){
drh875bafa2015-04-24 14:47:59 +0000787 int n = 0;
drhe1a71a52015-04-24 16:09:12 +0000788 FILE *out = fopen(zDataOut, "wb");
drh875bafa2015-04-24 14:47:59 +0000789 if( out==0 ) abendError("cannot open %s for writing", zDataOut);
drhe1a71a52015-04-24 16:09:12 +0000790 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out);
drh875bafa2015-04-24 14:47:59 +0000791 sqlite3_finalize(pStmt);
792 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0);
793 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
794 while( sqlite3_step(pStmt)==SQLITE_ROW ){
795 fprintf(out,"/****<%d>****/", ++n);
796 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
797 }
798 fclose(out);
799 sqlite3_finalize(pStmt);
800 sqlite3_close(dataDb);
drh1cbb7fa2015-04-24 13:00:59 +0000801 }
drhf34e9aa2015-04-20 12:50:13 +0000802 free(zIn);
drh4a74d072015-04-20 18:58:38 +0000803 free(pHeap);
804 free(pLook);
805 free(pScratch);
806 free(pPCache);
drhf34e9aa2015-04-20 12:50:13 +0000807 return 0;
drh268e72f2015-04-17 14:30:49 +0000808}