blob: d4a5acfbe3919f82b884d51a3583b5ba1b7958a5 [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**
42** where the "..." is arbitrary text, except the "|" should really be "/".
drhbe5248f2015-04-25 11:35:48 +000043** ("|" is used here to avoid compiler errors about nested comments.)
44** 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
63**
drh268e72f2015-04-17 14:30:49 +000064*/
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <stdarg.h>
drh4a74d072015-04-20 18:58:38 +000069#include <ctype.h>
drh268e72f2015-04-17 14:30:49 +000070#include "sqlite3.h"
71
72/*
73** All global variables are gathered into the "g" singleton.
74*/
75struct GlobalVars {
drh048810b2015-04-24 23:45:23 +000076 const char *zArgv0; /* Name of program */
77 sqlite3_mem_methods sOrigMem; /* Original memory methods */
78 sqlite3_mem_methods sOomMem; /* Memory methods with OOM simulator */
79 int iOomCntdown; /* Memory fails on 1 to 0 transition */
80 int nOomFault; /* Increments for each OOM fault */
81 int bOomOnce; /* Fail just once if true */
82 int bOomEnable; /* True to enable OOM simulation */
83 int nOomBrkpt; /* Number of calls to oomFault() */
drh0ee751f2015-04-25 11:19:51 +000084 char zTestName[100]; /* Name of current test */
drh268e72f2015-04-17 14:30:49 +000085} g;
86
drh048810b2015-04-24 23:45:23 +000087/*
88** This routine is called when a simulated OOM occurs. It exists as a
89** convenient place to set a debugger breakpoint.
90*/
91static void oomFault(void){
drhbe5248f2015-04-25 11:35:48 +000092 g.nOomBrkpt++; /* Prevent oomFault() from being optimized out */
drh048810b2015-04-24 23:45:23 +000093}
drh268e72f2015-04-17 14:30:49 +000094
95
drh048810b2015-04-24 23:45:23 +000096/* Versions of malloc() and realloc() that simulate OOM conditions */
97static void *oomMalloc(int nByte){
98 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
99 g.iOomCntdown--;
100 if( g.iOomCntdown==0 ){
101 if( g.nOomFault==0 ) oomFault();
102 g.nOomFault++;
103 if( !g.bOomOnce ) g.iOomCntdown = 1;
104 return 0;
105 }
106 }
107 return g.sOrigMem.xMalloc(nByte);
108}
109static void *oomRealloc(void *pOld, int nByte){
110 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
111 g.iOomCntdown--;
112 if( g.iOomCntdown==0 ){
113 if( g.nOomFault==0 ) oomFault();
114 g.nOomFault++;
115 if( !g.bOomOnce ) g.iOomCntdown = 1;
116 return 0;
117 }
118 }
119 return g.sOrigMem.xRealloc(pOld, nByte);
120}
121
drh268e72f2015-04-17 14:30:49 +0000122/*
123** Print an error message and abort in such a way to indicate to the
124** fuzzer that this counts as a crash.
125*/
126static void abendError(const char *zFormat, ...){
127 va_list ap;
drhbe5248f2015-04-25 11:35:48 +0000128 if( g.zTestName[0] ){
129 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
130 }else{
131 fprintf(stderr, "%s: ", g.zArgv0);
132 }
drh268e72f2015-04-17 14:30:49 +0000133 va_start(ap, zFormat);
134 vfprintf(stderr, zFormat, ap);
135 va_end(ap);
136 fprintf(stderr, "\n");
137 abort();
138}
139/*
140** Print an error message and quit, but not in a way that would look
141** like a crash.
142*/
143static void fatalError(const char *zFormat, ...){
144 va_list ap;
drhbe5248f2015-04-25 11:35:48 +0000145 if( g.zTestName[0] ){
146 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
147 }else{
148 fprintf(stderr, "%s: ", g.zArgv0);
149 }
drh268e72f2015-04-17 14:30:49 +0000150 va_start(ap, zFormat);
151 vfprintf(stderr, zFormat, ap);
152 va_end(ap);
153 fprintf(stderr, "\n");
154 exit(1);
155}
156
157/*
drh4a74d072015-04-20 18:58:38 +0000158** Evaluate some SQL. Abort if unable.
159*/
160static void sqlexec(sqlite3 *db, const char *zFormat, ...){
161 va_list ap;
162 char *zSql;
163 char *zErrMsg = 0;
164 int rc;
165 va_start(ap, zFormat);
166 zSql = sqlite3_vmprintf(zFormat, ap);
167 va_end(ap);
168 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
169 if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg);
170 sqlite3_free(zSql);
171}
172
173/*
drh268e72f2015-04-17 14:30:49 +0000174** This callback is invoked by sqlite3_log().
175*/
176static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){
177 printf("LOG: (%d) %s\n", iErrCode, zMsg);
drh9f18f742015-04-25 00:20:15 +0000178 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000179}
drh0ee751f2015-04-25 11:19:51 +0000180static void shellLogNoop(void *pNotUsed, int iErrCode, const char *zMsg){
181 return;
182}
drh268e72f2015-04-17 14:30:49 +0000183
184/*
185** This callback is invoked by sqlite3_exec() to return query results.
186*/
187static int execCallback(void *NotUsed, int argc, char **argv, char **colv){
188 int i;
189 static unsigned cnt = 0;
190 printf("ROW #%u:\n", ++cnt);
191 for(i=0; i<argc; i++){
192 printf(" %s=", colv[i]);
193 if( argv[i] ){
194 printf("[%s]\n", argv[i]);
195 }else{
196 printf("NULL\n");
197 }
198 }
drh9f18f742015-04-25 00:20:15 +0000199 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000200 return 0;
201}
drh1cbb7fa2015-04-24 13:00:59 +0000202static int execNoop(void *NotUsed, int argc, char **argv, char **colv){
203 return 0;
204}
drh268e72f2015-04-17 14:30:49 +0000205
drh61a0d6b2015-04-24 18:31:12 +0000206#ifndef SQLITE_OMIT_TRACE
drh268e72f2015-04-17 14:30:49 +0000207/*
208** This callback is invoked by sqlite3_trace() as each SQL statement
209** starts.
210*/
211static void traceCallback(void *NotUsed, const char *zMsg){
212 printf("TRACE: %s\n", zMsg);
drh9f18f742015-04-25 00:20:15 +0000213 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000214}
drh0ee751f2015-04-25 11:19:51 +0000215static void traceNoop(void *NotUsed, const char *zMsg){
216 return;
217}
drh61a0d6b2015-04-24 18:31:12 +0000218#endif
drh268e72f2015-04-17 14:30:49 +0000219
220/***************************************************************************
221** eval() implementation copied from ../ext/misc/eval.c
222*/
223/*
224** Structure used to accumulate the output
225*/
226struct EvalResult {
227 char *z; /* Accumulated output */
228 const char *zSep; /* Separator */
229 int szSep; /* Size of the separator string */
230 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */
231 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */
232};
233
234/*
235** Callback from sqlite_exec() for the eval() function.
236*/
237static int callback(void *pCtx, int argc, char **argv, char **colnames){
238 struct EvalResult *p = (struct EvalResult*)pCtx;
239 int i;
240 for(i=0; i<argc; i++){
241 const char *z = argv[i] ? argv[i] : "";
242 size_t sz = strlen(z);
243 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
244 char *zNew;
245 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
246 /* Using sqlite3_realloc64() would be better, but it is a recent
247 ** addition and will cause a segfault if loaded by an older version
248 ** of SQLite. */
249 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
250 if( zNew==0 ){
251 sqlite3_free(p->z);
252 memset(p, 0, sizeof(*p));
253 return 1;
254 }
255 p->z = zNew;
256 }
257 if( p->nUsed>0 ){
258 memcpy(&p->z[p->nUsed], p->zSep, p->szSep);
259 p->nUsed += p->szSep;
260 }
261 memcpy(&p->z[p->nUsed], z, sz);
262 p->nUsed += sz;
263 }
264 return 0;
265}
266
267/*
268** Implementation of the eval(X) and eval(X,Y) SQL functions.
269**
270** Evaluate the SQL text in X. Return the results, using string
271** Y as the separator. If Y is omitted, use a single space character.
272*/
273static void sqlEvalFunc(
274 sqlite3_context *context,
275 int argc,
276 sqlite3_value **argv
277){
278 const char *zSql;
279 sqlite3 *db;
280 char *zErr = 0;
281 int rc;
282 struct EvalResult x;
283
284 memset(&x, 0, sizeof(x));
285 x.zSep = " ";
286 zSql = (const char*)sqlite3_value_text(argv[0]);
287 if( zSql==0 ) return;
288 if( argc>1 ){
289 x.zSep = (const char*)sqlite3_value_text(argv[1]);
290 if( x.zSep==0 ) return;
291 }
292 x.szSep = (int)strlen(x.zSep);
293 db = sqlite3_context_db_handle(context);
294 rc = sqlite3_exec(db, zSql, callback, &x, &zErr);
295 if( rc!=SQLITE_OK ){
296 sqlite3_result_error(context, zErr, -1);
297 sqlite3_free(zErr);
298 }else if( x.zSep==0 ){
299 sqlite3_result_error_nomem(context);
300 sqlite3_free(x.z);
301 }else{
302 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
303 }
304}
305/* End of the eval() implementation
306******************************************************************************/
307
308/*
309** Print sketchy documentation for this utility program
310*/
311static void showHelp(void){
312 printf("Usage: %s [options]\n", g.zArgv0);
313 printf(
314"Read SQL text from standard input and evaluate it.\n"
315"Options:\n"
drh875bafa2015-04-24 14:47:59 +0000316" --autovacuum Enable AUTOVACUUM mode\n"
317" -f FILE Read SQL text from FILE instead of standard input\n"
318" --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
319" --help Show this help text\n"
320" --initdb DBFILE Initialize the in-memory database using template DBFILE\n"
321" --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
drh048810b2015-04-24 23:45:23 +0000322" --oom Run each test multiple times in a simulated OOM loop\n"
drh875bafa2015-04-24 14:47:59 +0000323" --pagesize N Set the page size to N\n"
324" --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
325" -q Reduced output\n"
326" --quiet Reduced output\n"
327" --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
328" --unique-cases FILE Write all unique test cases to FILE\n"
329" --utf16be Set text encoding to UTF-16BE\n"
330" --utf16le Set text encoding to UTF-16LE\n"
331" -v Increased output\n"
332" --verbose Increased output\n"
drh268e72f2015-04-17 14:30:49 +0000333 );
334}
335
drh4a74d072015-04-20 18:58:38 +0000336/*
337** Return the value of a hexadecimal digit. Return -1 if the input
338** is not a hex digit.
339*/
340static int hexDigitValue(char c){
341 if( c>='0' && c<='9' ) return c - '0';
342 if( c>='a' && c<='f' ) return c - 'a' + 10;
343 if( c>='A' && c<='F' ) return c - 'A' + 10;
344 return -1;
345}
346
347/*
348** Interpret zArg as an integer value, possibly with suffixes.
349*/
350static int integerValue(const char *zArg){
351 sqlite3_int64 v = 0;
352 static const struct { char *zSuffix; int iMult; } aMult[] = {
353 { "KiB", 1024 },
354 { "MiB", 1024*1024 },
355 { "GiB", 1024*1024*1024 },
356 { "KB", 1000 },
357 { "MB", 1000000 },
358 { "GB", 1000000000 },
359 { "K", 1000 },
360 { "M", 1000000 },
361 { "G", 1000000000 },
362 };
363 int i;
364 int isNeg = 0;
365 if( zArg[0]=='-' ){
366 isNeg = 1;
367 zArg++;
368 }else if( zArg[0]=='+' ){
369 zArg++;
370 }
371 if( zArg[0]=='0' && zArg[1]=='x' ){
372 int x;
373 zArg += 2;
374 while( (x = hexDigitValue(zArg[0]))>=0 ){
375 v = (v<<4) + x;
376 zArg++;
377 }
378 }else{
379 while( isdigit(zArg[0]) ){
380 v = v*10 + zArg[0] - '0';
381 zArg++;
382 }
383 }
384 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
385 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
386 v *= aMult[i].iMult;
387 break;
388 }
389 }
390 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
391 return (int)(isNeg? -v : v);
392}
393
drh9985dab2015-04-20 22:36:49 +0000394/*
395** Various operating modes
396*/
397#define FZMODE_Generic 1
398#define FZMODE_Strftime 2
399#define FZMODE_Printf 3
400#define FZMODE_Glob 4
401
drh268e72f2015-04-17 14:30:49 +0000402
403int main(int argc, char **argv){
404 char *zIn = 0; /* Input text */
405 int nAlloc = 0; /* Number of bytes allocated for zIn[] */
406 int nIn = 0; /* Number of bytes of zIn[] used */
407 size_t got; /* Bytes read from input */
408 FILE *in = stdin; /* Where to read SQL text from */
409 int rc = SQLITE_OK; /* Result codes from API functions */
410 int i; /* Loop counter */
drhf34e9aa2015-04-20 12:50:13 +0000411 int iNext; /* Next block of SQL */
drh268e72f2015-04-17 14:30:49 +0000412 sqlite3 *db; /* Open database */
drhf34e9aa2015-04-20 12:50:13 +0000413 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */
drh268e72f2015-04-17 14:30:49 +0000414 const char *zInitDb = 0;/* Name of the initialization database file */
415 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */
drh4a74d072015-04-20 18:58:38 +0000416 const char *zEncoding = 0; /* --utf16be or --utf16le */
417 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
418 int nLook = 0, szLook = 0; /* --lookaside configuration */
419 int nPCache = 0, szPCache = 0;/* --pcache configuration */
420 int nScratch = 0, szScratch=0;/* --scratch configuration */
421 int pageSize = 0; /* Desired page size. 0 means default */
422 void *pHeap = 0; /* Allocated heap space */
423 void *pLook = 0; /* Allocated lookaside space */
424 void *pPCache = 0; /* Allocated storage for pcache */
425 void *pScratch = 0; /* Allocated storage for scratch */
426 int doAutovac = 0; /* True for --autovacuum */
drh9985dab2015-04-20 22:36:49 +0000427 char *zSql; /* SQL to run */
428 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */
429 int iMode = FZMODE_Generic; /* Operating mode */
drh0ba51082015-04-22 13:16:46 +0000430 const char *zCkGlob = 0; /* Inputs must match this glob */
drh1cbb7fa2015-04-24 13:00:59 +0000431 int verboseFlag = 0; /* --verbose or -v flag */
432 int quietFlag = 0; /* --quiet or -q flag */
433 int nTest = 0; /* Number of test cases run */
434 int multiTest = 0; /* True if there will be multiple test cases */
435 int lastPct = -1; /* Previous percentage done output */
drh875bafa2015-04-24 14:47:59 +0000436 sqlite3 *dataDb = 0; /* Database holding compacted input data */
437 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */
438 const char *zDataOut = 0; /* Write compacted data to this output file */
drhe1a71a52015-04-24 16:09:12 +0000439 int nHeader = 0; /* Bytes of header comment text on input file */
drh048810b2015-04-24 23:45:23 +0000440 int oomFlag = 0; /* --oom */
441 int oomCnt = 0; /* Counter for the OOM loop */
442 char zErrBuf[200]; /* Space for the error message */
443 const char *zFailCode; /* Value of the TEST_FAILURE environment var */
drh4a74d072015-04-20 18:58:38 +0000444
drh268e72f2015-04-17 14:30:49 +0000445
drh048810b2015-04-24 23:45:23 +0000446 zFailCode = getenv("TEST_FAILURE");
drh268e72f2015-04-17 14:30:49 +0000447 g.zArgv0 = argv[0];
448 for(i=1; i<argc; i++){
449 const char *z = argv[i];
450 if( z[0]=='-' ){
451 z++;
452 if( z[0]=='-' ) z++;
drh4a74d072015-04-20 18:58:38 +0000453 if( strcmp(z,"autovacuum")==0 ){
454 doAutovac = 1;
drh268e72f2015-04-17 14:30:49 +0000455 }else
456 if( strcmp(z, "f")==0 && i+1<argc ){
457 if( in!=stdin ) abendError("only one -f allowed");
458 in = fopen(argv[++i],"rb");
459 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]);
460 }else
drh4a74d072015-04-20 18:58:38 +0000461 if( strcmp(z,"heap")==0 ){
462 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
463 nHeap = integerValue(argv[i+1]);
464 mnHeap = integerValue(argv[i+2]);
465 i += 2;
466 }else
467 if( strcmp(z,"help")==0 ){
468 showHelp();
469 return 0;
470 }else
drh268e72f2015-04-17 14:30:49 +0000471 if( strcmp(z, "initdb")==0 && i+1<argc ){
472 if( zInitDb!=0 ) abendError("only one --initdb allowed");
473 zInitDb = argv[++i];
474 }else
drh4a74d072015-04-20 18:58:38 +0000475 if( strcmp(z,"lookaside")==0 ){
476 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
477 nLook = integerValue(argv[i+1]);
478 szLook = integerValue(argv[i+2]);
479 i += 2;
480 }else
drh9985dab2015-04-20 22:36:49 +0000481 if( strcmp(z,"mode")==0 ){
482 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
483 z = argv[++i];
484 if( strcmp(z,"generic")==0 ){
485 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000486 zCkGlob = 0;
drh9985dab2015-04-20 22:36:49 +0000487 }else if( strcmp(z, "glob")==0 ){
488 iMode = FZMODE_Glob;
drh0ba51082015-04-22 13:16:46 +0000489 zCkGlob = "'*','*'";
drh9985dab2015-04-20 22:36:49 +0000490 }else if( strcmp(z, "printf")==0 ){
491 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000492 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000493 }else if( strcmp(z, "strftime")==0 ){
494 iMode = FZMODE_Strftime;
drh0ba51082015-04-22 13:16:46 +0000495 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000496 }else{
497 abendError("unknown --mode: %s", z);
498 }
499 }else
drh048810b2015-04-24 23:45:23 +0000500 if( strcmp(z,"oom")==0 ){
501 oomFlag = 1;
502 }else
drh4a74d072015-04-20 18:58:38 +0000503 if( strcmp(z,"pagesize")==0 ){
504 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
505 pageSize = integerValue(argv[++i]);
506 }else
507 if( strcmp(z,"pcache")==0 ){
508 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
509 nPCache = integerValue(argv[i+1]);
510 szPCache = integerValue(argv[i+2]);
511 i += 2;
512 }else
drh1cbb7fa2015-04-24 13:00:59 +0000513 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
514 quietFlag = 1;
515 verboseFlag = 0;
516 }else
drh4a74d072015-04-20 18:58:38 +0000517 if( strcmp(z,"scratch")==0 ){
518 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
519 nScratch = integerValue(argv[i+1]);
520 szScratch = integerValue(argv[i+2]);
521 i += 2;
522 }else
drh875bafa2015-04-24 14:47:59 +0000523 if( strcmp(z, "unique-cases")==0 ){
524 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
525 if( zDataOut ) abendError("only one --minimize allowed");
526 zDataOut = argv[++i];
527 }else
drh4a74d072015-04-20 18:58:38 +0000528 if( strcmp(z,"utf16le")==0 ){
529 zEncoding = "utf16le";
530 }else
531 if( strcmp(z,"utf16be")==0 ){
532 zEncoding = "utf16be";
533 }else
drh1cbb7fa2015-04-24 13:00:59 +0000534 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
535 quietFlag = 0;
536 verboseFlag = 1;
537 }else
drh268e72f2015-04-17 14:30:49 +0000538 {
539 abendError("unknown option: %s", argv[i]);
540 }
541 }else{
542 abendError("unknown argument: %s", argv[i]);
543 }
544 }
drh0ee751f2015-04-25 11:19:51 +0000545 sqlite3_config(SQLITE_CONFIG_LOG, verboseFlag ? shellLog : shellLogNoop, 0);
drh4a74d072015-04-20 18:58:38 +0000546 if( nHeap>0 ){
547 pHeap = malloc( nHeap );
548 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
549 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
550 if( rc ) abendError("heap configuration failed: %d\n", rc);
551 }
drh048810b2015-04-24 23:45:23 +0000552 if( oomFlag ){
553 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem);
554 g.sOomMem = g.sOrigMem;
555 g.sOomMem.xMalloc = oomMalloc;
556 g.sOomMem.xRealloc = oomRealloc;
557 sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem);
558 }
drh4a74d072015-04-20 18:58:38 +0000559 if( nLook>0 ){
560 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
561 if( szLook>0 ){
562 pLook = malloc( nLook*szLook );
563 if( pLook==0 ) fatalError("out of memory");
564 }
565 }
566 if( nScratch>0 && szScratch>0 ){
567 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
568 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch",
569 nScratch*(sqlite3_int64)szScratch);
570 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
571 if( rc ) abendError("scratch configuration failed: %d\n", rc);
572 }
573 if( nPCache>0 && szPCache>0 ){
574 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
575 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
576 nPCache*(sqlite3_int64)szPCache);
577 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
578 if( rc ) abendError("pcache configuration failed: %d", rc);
579 }
drh268e72f2015-04-17 14:30:49 +0000580 while( !feof(in) ){
drhf34e9aa2015-04-20 12:50:13 +0000581 nAlloc += nAlloc+1000;
582 zIn = realloc(zIn, nAlloc);
drh268e72f2015-04-17 14:30:49 +0000583 if( zIn==0 ) fatalError("out of memory");
584 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in);
585 nIn += (int)got;
586 zIn[nIn] = 0;
587 if( got==0 ) break;
588 }
drh875bafa2015-04-24 14:47:59 +0000589 if( in!=stdin ) fclose(in);
590 if( zDataOut ){
591 rc = sqlite3_open(":memory:", &dataDb);
592 if( rc ) abendError("cannot open :memory: database");
593 rc = sqlite3_exec(dataDb,
594 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0);
595 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
596 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)",
597 -1, &pStmt, 0);
598 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
599 }
drhf34e9aa2015-04-20 12:50:13 +0000600 if( zInitDb ){
601 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
602 if( rc!=SQLITE_OK ){
603 abendError("unable to open initialization database \"%s\"", zInitDb);
604 }
drh268e72f2015-04-17 14:30:49 +0000605 }
drhe1a71a52015-04-24 16:09:12 +0000606 for(i=0; i<nIn; i=iNext+1){ /* Skip initial lines beginning with '#' */
607 if( zIn[i]!='#' ) break;
608 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){}
609 }
610 nHeader = i;
drhbe5248f2015-04-25 11:35:48 +0000611 for(nTest=0; i<nIn; i=iNext, nTest++, g.zTestName[0]=0){
drhf34e9aa2015-04-20 12:50:13 +0000612 char cSaved;
613 if( strncmp(&zIn[i], "/****<",6)==0 ){
614 char *z = strstr(&zIn[i], ">****/");
615 if( z ){
616 z += 6;
drhbe5248f2015-04-25 11:35:48 +0000617 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s",
drh0ee751f2015-04-25 11:19:51 +0000618 (int)(z-&zIn[i]), &zIn[i]);
drh9f18f742015-04-25 00:20:15 +0000619 if( verboseFlag ){
620 printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
621 fflush(stdout);
622 }
drhf34e9aa2015-04-20 12:50:13 +0000623 i += (int)(z-&zIn[i]);
drh1cbb7fa2015-04-24 13:00:59 +0000624 multiTest = 1;
drhf34e9aa2015-04-20 12:50:13 +0000625 }
626 }
627 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
drh875bafa2015-04-24 14:47:59 +0000628 if( zDataOut ){
629 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
630 rc = sqlite3_step(pStmt);
631 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
632 sqlite3_reset(pStmt);
633 continue;
634 }
drh3fb2cc12015-04-22 11:16:34 +0000635 cSaved = zIn[iNext];
636 zIn[iNext] = 0;
drh0ba51082015-04-22 13:16:46 +0000637 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
drh3fb2cc12015-04-22 11:16:34 +0000638 zIn[iNext] = cSaved;
639 continue;
640 }
drh9985dab2015-04-20 22:36:49 +0000641 zSql = &zIn[i];
drh1cbb7fa2015-04-24 13:00:59 +0000642 if( verboseFlag ){
643 printf("INPUT (offset: %d, size: %d): [%s]\n",
644 i, (int)strlen(&zIn[i]), &zIn[i]);
drh9f18f742015-04-25 00:20:15 +0000645 fflush(stdout);
drh1cbb7fa2015-04-24 13:00:59 +0000646 }else if( multiTest && !quietFlag ){
drh048810b2015-04-24 23:45:23 +0000647 int pct = oomFlag ? 100*iNext/nIn : ((10*iNext)/nIn)*10;
drh1cbb7fa2015-04-24 13:00:59 +0000648 if( pct!=lastPct ){
drhe1a71a52015-04-24 16:09:12 +0000649 if( lastPct<0 ) printf("fuzz test:");
drh048810b2015-04-24 23:45:23 +0000650 printf(" %d%%", pct);
drh1cbb7fa2015-04-24 13:00:59 +0000651 fflush(stdout);
652 lastPct = pct;
653 }
654 }
drh9985dab2015-04-20 22:36:49 +0000655 switch( iMode ){
656 case FZMODE_Glob:
657 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
658 break;
659 case FZMODE_Printf:
660 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
661 break;
662 case FZMODE_Strftime:
663 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
664 break;
665 }
drh048810b2015-04-24 23:45:23 +0000666 if( oomFlag ){
667 oomCnt = g.iOomCntdown = 1;
668 g.nOomFault = 0;
669 g.bOomOnce = 1;
drh9f18f742015-04-25 00:20:15 +0000670 if( verboseFlag ){
671 printf("Once.%d\n", oomCnt);
672 fflush(stdout);
673 }
drh048810b2015-04-24 23:45:23 +0000674 }else{
675 oomCnt = 0;
676 }
677 do{
678 rc = sqlite3_open_v2(
679 "main.db", &db,
680 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
681 0);
682 if( rc!=SQLITE_OK ){
683 abendError("Unable to open the in-memory database");
684 }
685 if( pLook ){
686 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
687 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
688 }
689 if( zInitDb ){
690 sqlite3_backup *pBackup;
691 pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
692 rc = sqlite3_backup_step(pBackup, -1);
693 if( rc!=SQLITE_DONE ){
694 abendError("attempt to initialize the in-memory database failed (rc=%d)",
695 rc);
696 }
697 sqlite3_backup_finish(pBackup);
698 }
699 #ifndef SQLITE_OMIT_TRACE
drh0ee751f2015-04-25 11:19:51 +0000700 sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0);
drh048810b2015-04-24 23:45:23 +0000701 #endif
702 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
703 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
704 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
705 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
706 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
707 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
708 g.bOomEnable = 1;
709 if( verboseFlag ){
710 zErrMsg = 0;
711 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg);
712 if( zErrMsg ){
713 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg);
714 zErrMsg = 0;
715 }
716 }else {
717 rc = sqlite3_exec(db, zSql, execNoop, 0, 0);
718 }
719 g.bOomEnable = 0;
720 rc = sqlite3_close(db);
721 if( rc ){
722 abendError("sqlite3_close() failed with rc=%d", rc);
723 }
724 if( sqlite3_memory_used()>0 ){
725 abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
726 }
727 if( oomFlag ){
728 if( g.nOomFault==0 || oomCnt>2000 ){
729 if( g.bOomOnce ){
730 oomCnt = g.iOomCntdown = 1;
731 g.bOomOnce = 0;
732 }else{
733 oomCnt = 0;
734 }
735 }else{
736 g.iOomCntdown = ++oomCnt;
737 g.nOomFault = 0;
738 }
739 if( oomCnt ){
740 if( verboseFlag ){
741 printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt);
drh9f18f742015-04-25 00:20:15 +0000742 fflush(stdout);
drh048810b2015-04-24 23:45:23 +0000743 }
744 nTest++;
745 }
746 }
747 }while( oomCnt>0 );
drh9985dab2015-04-20 22:36:49 +0000748 if( zToFree ){
749 sqlite3_free(zToFree);
750 zToFree = 0;
751 }
drhf34e9aa2015-04-20 12:50:13 +0000752 zIn[iNext] = cSaved;
drh1cbb7fa2015-04-24 13:00:59 +0000753 if( verboseFlag ){
754 printf("RESULT-CODE: %d\n", rc);
755 if( zErrMsg ){
drh048810b2015-04-24 23:45:23 +0000756 printf("ERROR-MSG: [%s]\n", zErrBuf);
drh1cbb7fa2015-04-24 13:00:59 +0000757 }
drh9f18f742015-04-25 00:20:15 +0000758 fflush(stdout);
drhf34e9aa2015-04-20 12:50:13 +0000759 }
drh048810b2015-04-24 23:45:23 +0000760 /* Simulate an error if the TEST_FAILURE environment variable is "5" */
761 if( zFailCode ){
762 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
763 abendError("simulated failure");
764 }else if( zFailCode[0]!=0 ){
765 /* If TEST_FAILURE is something other than 5, just exit the test
766 ** early */
767 printf("\nExit early due to TEST_FAILURE being set");
768 break;
drhe1a71a52015-04-24 16:09:12 +0000769 }
770 }
drhf34e9aa2015-04-20 12:50:13 +0000771 }
drhe1a71a52015-04-24 16:09:12 +0000772 if( !verboseFlag && multiTest && !quietFlag ) printf("\n");
drh1cbb7fa2015-04-24 13:00:59 +0000773 if( nTest>1 && !quietFlag ){
drhe1a71a52015-04-24 16:09:12 +0000774 printf("%d fuzz tests with no errors\nSQLite %s %s\n",
drh875bafa2015-04-24 14:47:59 +0000775 nTest, sqlite3_libversion(), sqlite3_sourceid());
776 }
777 if( zDataOut ){
drh875bafa2015-04-24 14:47:59 +0000778 int n = 0;
drhe1a71a52015-04-24 16:09:12 +0000779 FILE *out = fopen(zDataOut, "wb");
drh875bafa2015-04-24 14:47:59 +0000780 if( out==0 ) abendError("cannot open %s for writing", zDataOut);
drhe1a71a52015-04-24 16:09:12 +0000781 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out);
drh875bafa2015-04-24 14:47:59 +0000782 sqlite3_finalize(pStmt);
783 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0);
784 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
785 while( sqlite3_step(pStmt)==SQLITE_ROW ){
786 fprintf(out,"/****<%d>****/", ++n);
787 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
788 }
789 fclose(out);
790 sqlite3_finalize(pStmt);
791 sqlite3_close(dataDb);
drh1cbb7fa2015-04-24 13:00:59 +0000792 }
drhf34e9aa2015-04-20 12:50:13 +0000793 free(zIn);
drh4a74d072015-04-20 18:58:38 +0000794 free(pHeap);
795 free(pLook);
796 free(pScratch);
797 free(pPCache);
drhf34e9aa2015-04-20 12:50:13 +0000798 return 0;
drh268e72f2015-04-17 14:30:49 +0000799}