blob: 5ac2e482ebcd920bad20964df4eb318075248701 [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 {
drh048810b2015-04-24 23:45:23 +000074 const char *zArgv0; /* Name of program */
75 sqlite3_mem_methods sOrigMem; /* Original memory methods */
76 sqlite3_mem_methods sOomMem; /* Memory methods with OOM simulator */
77 int iOomCntdown; /* Memory fails on 1 to 0 transition */
78 int nOomFault; /* Increments for each OOM fault */
79 int bOomOnce; /* Fail just once if true */
80 int bOomEnable; /* True to enable OOM simulation */
81 int nOomBrkpt; /* Number of calls to oomFault() */
drh268e72f2015-04-17 14:30:49 +000082} g;
83
drh048810b2015-04-24 23:45:23 +000084/*
85** This routine is called when a simulated OOM occurs. It exists as a
86** convenient place to set a debugger breakpoint.
87*/
88static void oomFault(void){
89 g.nOomBrkpt++;
90}
drh268e72f2015-04-17 14:30:49 +000091
92
drh048810b2015-04-24 23:45:23 +000093/* Versions of malloc() and realloc() that simulate OOM conditions */
94static void *oomMalloc(int nByte){
95 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
96 g.iOomCntdown--;
97 if( g.iOomCntdown==0 ){
98 if( g.nOomFault==0 ) oomFault();
99 g.nOomFault++;
100 if( !g.bOomOnce ) g.iOomCntdown = 1;
101 return 0;
102 }
103 }
104 return g.sOrigMem.xMalloc(nByte);
105}
106static void *oomRealloc(void *pOld, int nByte){
107 if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){
108 g.iOomCntdown--;
109 if( g.iOomCntdown==0 ){
110 if( g.nOomFault==0 ) oomFault();
111 g.nOomFault++;
112 if( !g.bOomOnce ) g.iOomCntdown = 1;
113 return 0;
114 }
115 }
116 return g.sOrigMem.xRealloc(pOld, nByte);
117}
118
drh268e72f2015-04-17 14:30:49 +0000119/*
120** Print an error message and abort in such a way to indicate to the
121** fuzzer that this counts as a crash.
122*/
123static void abendError(const char *zFormat, ...){
124 va_list ap;
125 fprintf(stderr, "%s: ", g.zArgv0);
126 va_start(ap, zFormat);
127 vfprintf(stderr, zFormat, ap);
128 va_end(ap);
129 fprintf(stderr, "\n");
130 abort();
131}
132/*
133** Print an error message and quit, but not in a way that would look
134** like a crash.
135*/
136static void fatalError(const char *zFormat, ...){
137 va_list ap;
138 fprintf(stderr, "%s: ", g.zArgv0);
139 va_start(ap, zFormat);
140 vfprintf(stderr, zFormat, ap);
141 va_end(ap);
142 fprintf(stderr, "\n");
143 exit(1);
144}
145
146/*
drh4a74d072015-04-20 18:58:38 +0000147** Evaluate some SQL. Abort if unable.
148*/
149static void sqlexec(sqlite3 *db, const char *zFormat, ...){
150 va_list ap;
151 char *zSql;
152 char *zErrMsg = 0;
153 int rc;
154 va_start(ap, zFormat);
155 zSql = sqlite3_vmprintf(zFormat, ap);
156 va_end(ap);
157 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
158 if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg);
159 sqlite3_free(zSql);
160}
161
162/*
drh268e72f2015-04-17 14:30:49 +0000163** This callback is invoked by sqlite3_log().
164*/
165static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){
166 printf("LOG: (%d) %s\n", iErrCode, zMsg);
drh9f18f742015-04-25 00:20:15 +0000167 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000168}
169
170/*
171** This callback is invoked by sqlite3_exec() to return query results.
172*/
173static int execCallback(void *NotUsed, int argc, char **argv, char **colv){
174 int i;
175 static unsigned cnt = 0;
176 printf("ROW #%u:\n", ++cnt);
177 for(i=0; i<argc; i++){
178 printf(" %s=", colv[i]);
179 if( argv[i] ){
180 printf("[%s]\n", argv[i]);
181 }else{
182 printf("NULL\n");
183 }
184 }
drh9f18f742015-04-25 00:20:15 +0000185 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000186 return 0;
187}
drh1cbb7fa2015-04-24 13:00:59 +0000188static int execNoop(void *NotUsed, int argc, char **argv, char **colv){
189 return 0;
190}
drh268e72f2015-04-17 14:30:49 +0000191
drh61a0d6b2015-04-24 18:31:12 +0000192#ifndef SQLITE_OMIT_TRACE
drh268e72f2015-04-17 14:30:49 +0000193/*
194** This callback is invoked by sqlite3_trace() as each SQL statement
195** starts.
196*/
197static void traceCallback(void *NotUsed, const char *zMsg){
198 printf("TRACE: %s\n", zMsg);
drh9f18f742015-04-25 00:20:15 +0000199 fflush(stdout);
drh268e72f2015-04-17 14:30:49 +0000200}
drh61a0d6b2015-04-24 18:31:12 +0000201#endif
drh268e72f2015-04-17 14:30:49 +0000202
203/***************************************************************************
204** eval() implementation copied from ../ext/misc/eval.c
205*/
206/*
207** Structure used to accumulate the output
208*/
209struct EvalResult {
210 char *z; /* Accumulated output */
211 const char *zSep; /* Separator */
212 int szSep; /* Size of the separator string */
213 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */
214 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */
215};
216
217/*
218** Callback from sqlite_exec() for the eval() function.
219*/
220static int callback(void *pCtx, int argc, char **argv, char **colnames){
221 struct EvalResult *p = (struct EvalResult*)pCtx;
222 int i;
223 for(i=0; i<argc; i++){
224 const char *z = argv[i] ? argv[i] : "";
225 size_t sz = strlen(z);
226 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
227 char *zNew;
228 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
229 /* Using sqlite3_realloc64() would be better, but it is a recent
230 ** addition and will cause a segfault if loaded by an older version
231 ** of SQLite. */
232 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
233 if( zNew==0 ){
234 sqlite3_free(p->z);
235 memset(p, 0, sizeof(*p));
236 return 1;
237 }
238 p->z = zNew;
239 }
240 if( p->nUsed>0 ){
241 memcpy(&p->z[p->nUsed], p->zSep, p->szSep);
242 p->nUsed += p->szSep;
243 }
244 memcpy(&p->z[p->nUsed], z, sz);
245 p->nUsed += sz;
246 }
247 return 0;
248}
249
250/*
251** Implementation of the eval(X) and eval(X,Y) SQL functions.
252**
253** Evaluate the SQL text in X. Return the results, using string
254** Y as the separator. If Y is omitted, use a single space character.
255*/
256static void sqlEvalFunc(
257 sqlite3_context *context,
258 int argc,
259 sqlite3_value **argv
260){
261 const char *zSql;
262 sqlite3 *db;
263 char *zErr = 0;
264 int rc;
265 struct EvalResult x;
266
267 memset(&x, 0, sizeof(x));
268 x.zSep = " ";
269 zSql = (const char*)sqlite3_value_text(argv[0]);
270 if( zSql==0 ) return;
271 if( argc>1 ){
272 x.zSep = (const char*)sqlite3_value_text(argv[1]);
273 if( x.zSep==0 ) return;
274 }
275 x.szSep = (int)strlen(x.zSep);
276 db = sqlite3_context_db_handle(context);
277 rc = sqlite3_exec(db, zSql, callback, &x, &zErr);
278 if( rc!=SQLITE_OK ){
279 sqlite3_result_error(context, zErr, -1);
280 sqlite3_free(zErr);
281 }else if( x.zSep==0 ){
282 sqlite3_result_error_nomem(context);
283 sqlite3_free(x.z);
284 }else{
285 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
286 }
287}
288/* End of the eval() implementation
289******************************************************************************/
290
291/*
292** Print sketchy documentation for this utility program
293*/
294static void showHelp(void){
295 printf("Usage: %s [options]\n", g.zArgv0);
296 printf(
297"Read SQL text from standard input and evaluate it.\n"
298"Options:\n"
drh875bafa2015-04-24 14:47:59 +0000299" --autovacuum Enable AUTOVACUUM mode\n"
300" -f FILE Read SQL text from FILE instead of standard input\n"
301" --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
302" --help Show this help text\n"
303" --initdb DBFILE Initialize the in-memory database using template DBFILE\n"
304" --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
drh048810b2015-04-24 23:45:23 +0000305" --oom Run each test multiple times in a simulated OOM loop\n"
drh875bafa2015-04-24 14:47:59 +0000306" --pagesize N Set the page size to N\n"
307" --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
308" -q Reduced output\n"
309" --quiet Reduced output\n"
310" --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
311" --unique-cases FILE Write all unique test cases to FILE\n"
312" --utf16be Set text encoding to UTF-16BE\n"
313" --utf16le Set text encoding to UTF-16LE\n"
314" -v Increased output\n"
315" --verbose Increased output\n"
drh268e72f2015-04-17 14:30:49 +0000316 );
317}
318
drh4a74d072015-04-20 18:58:38 +0000319/*
320** Return the value of a hexadecimal digit. Return -1 if the input
321** is not a hex digit.
322*/
323static int hexDigitValue(char c){
324 if( c>='0' && c<='9' ) return c - '0';
325 if( c>='a' && c<='f' ) return c - 'a' + 10;
326 if( c>='A' && c<='F' ) return c - 'A' + 10;
327 return -1;
328}
329
330/*
331** Interpret zArg as an integer value, possibly with suffixes.
332*/
333static int integerValue(const char *zArg){
334 sqlite3_int64 v = 0;
335 static const struct { char *zSuffix; int iMult; } aMult[] = {
336 { "KiB", 1024 },
337 { "MiB", 1024*1024 },
338 { "GiB", 1024*1024*1024 },
339 { "KB", 1000 },
340 { "MB", 1000000 },
341 { "GB", 1000000000 },
342 { "K", 1000 },
343 { "M", 1000000 },
344 { "G", 1000000000 },
345 };
346 int i;
347 int isNeg = 0;
348 if( zArg[0]=='-' ){
349 isNeg = 1;
350 zArg++;
351 }else if( zArg[0]=='+' ){
352 zArg++;
353 }
354 if( zArg[0]=='0' && zArg[1]=='x' ){
355 int x;
356 zArg += 2;
357 while( (x = hexDigitValue(zArg[0]))>=0 ){
358 v = (v<<4) + x;
359 zArg++;
360 }
361 }else{
362 while( isdigit(zArg[0]) ){
363 v = v*10 + zArg[0] - '0';
364 zArg++;
365 }
366 }
367 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
368 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
369 v *= aMult[i].iMult;
370 break;
371 }
372 }
373 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
374 return (int)(isNeg? -v : v);
375}
376
drh9985dab2015-04-20 22:36:49 +0000377/*
378** Various operating modes
379*/
380#define FZMODE_Generic 1
381#define FZMODE_Strftime 2
382#define FZMODE_Printf 3
383#define FZMODE_Glob 4
384
drh268e72f2015-04-17 14:30:49 +0000385
386int main(int argc, char **argv){
387 char *zIn = 0; /* Input text */
388 int nAlloc = 0; /* Number of bytes allocated for zIn[] */
389 int nIn = 0; /* Number of bytes of zIn[] used */
390 size_t got; /* Bytes read from input */
391 FILE *in = stdin; /* Where to read SQL text from */
392 int rc = SQLITE_OK; /* Result codes from API functions */
393 int i; /* Loop counter */
drhf34e9aa2015-04-20 12:50:13 +0000394 int iNext; /* Next block of SQL */
drh268e72f2015-04-17 14:30:49 +0000395 sqlite3 *db; /* Open database */
drhf34e9aa2015-04-20 12:50:13 +0000396 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */
drh268e72f2015-04-17 14:30:49 +0000397 const char *zInitDb = 0;/* Name of the initialization database file */
398 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */
drh4a74d072015-04-20 18:58:38 +0000399 const char *zEncoding = 0; /* --utf16be or --utf16le */
400 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
401 int nLook = 0, szLook = 0; /* --lookaside configuration */
402 int nPCache = 0, szPCache = 0;/* --pcache configuration */
403 int nScratch = 0, szScratch=0;/* --scratch configuration */
404 int pageSize = 0; /* Desired page size. 0 means default */
405 void *pHeap = 0; /* Allocated heap space */
406 void *pLook = 0; /* Allocated lookaside space */
407 void *pPCache = 0; /* Allocated storage for pcache */
408 void *pScratch = 0; /* Allocated storage for scratch */
409 int doAutovac = 0; /* True for --autovacuum */
drh9985dab2015-04-20 22:36:49 +0000410 char *zSql; /* SQL to run */
411 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */
412 int iMode = FZMODE_Generic; /* Operating mode */
drh0ba51082015-04-22 13:16:46 +0000413 const char *zCkGlob = 0; /* Inputs must match this glob */
drh1cbb7fa2015-04-24 13:00:59 +0000414 int verboseFlag = 0; /* --verbose or -v flag */
415 int quietFlag = 0; /* --quiet or -q flag */
416 int nTest = 0; /* Number of test cases run */
417 int multiTest = 0; /* True if there will be multiple test cases */
418 int lastPct = -1; /* Previous percentage done output */
drh875bafa2015-04-24 14:47:59 +0000419 sqlite3 *dataDb = 0; /* Database holding compacted input data */
420 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */
421 const char *zDataOut = 0; /* Write compacted data to this output file */
drhe1a71a52015-04-24 16:09:12 +0000422 int nHeader = 0; /* Bytes of header comment text on input file */
drh048810b2015-04-24 23:45:23 +0000423 int oomFlag = 0; /* --oom */
424 int oomCnt = 0; /* Counter for the OOM loop */
425 char zErrBuf[200]; /* Space for the error message */
426 const char *zFailCode; /* Value of the TEST_FAILURE environment var */
drh4a74d072015-04-20 18:58:38 +0000427
drh268e72f2015-04-17 14:30:49 +0000428
drh048810b2015-04-24 23:45:23 +0000429 zFailCode = getenv("TEST_FAILURE");
drh268e72f2015-04-17 14:30:49 +0000430 g.zArgv0 = argv[0];
431 for(i=1; i<argc; i++){
432 const char *z = argv[i];
433 if( z[0]=='-' ){
434 z++;
435 if( z[0]=='-' ) z++;
drh4a74d072015-04-20 18:58:38 +0000436 if( strcmp(z,"autovacuum")==0 ){
437 doAutovac = 1;
drh268e72f2015-04-17 14:30:49 +0000438 }else
439 if( strcmp(z, "f")==0 && i+1<argc ){
440 if( in!=stdin ) abendError("only one -f allowed");
441 in = fopen(argv[++i],"rb");
442 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]);
443 }else
drh4a74d072015-04-20 18:58:38 +0000444 if( strcmp(z,"heap")==0 ){
445 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
446 nHeap = integerValue(argv[i+1]);
447 mnHeap = integerValue(argv[i+2]);
448 i += 2;
449 }else
450 if( strcmp(z,"help")==0 ){
451 showHelp();
452 return 0;
453 }else
drh268e72f2015-04-17 14:30:49 +0000454 if( strcmp(z, "initdb")==0 && i+1<argc ){
455 if( zInitDb!=0 ) abendError("only one --initdb allowed");
456 zInitDb = argv[++i];
457 }else
drh4a74d072015-04-20 18:58:38 +0000458 if( strcmp(z,"lookaside")==0 ){
459 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
460 nLook = integerValue(argv[i+1]);
461 szLook = integerValue(argv[i+2]);
462 i += 2;
463 }else
drh9985dab2015-04-20 22:36:49 +0000464 if( strcmp(z,"mode")==0 ){
465 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
466 z = argv[++i];
467 if( strcmp(z,"generic")==0 ){
468 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000469 zCkGlob = 0;
drh9985dab2015-04-20 22:36:49 +0000470 }else if( strcmp(z, "glob")==0 ){
471 iMode = FZMODE_Glob;
drh0ba51082015-04-22 13:16:46 +0000472 zCkGlob = "'*','*'";
drh9985dab2015-04-20 22:36:49 +0000473 }else if( strcmp(z, "printf")==0 ){
474 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000475 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000476 }else if( strcmp(z, "strftime")==0 ){
477 iMode = FZMODE_Strftime;
drh0ba51082015-04-22 13:16:46 +0000478 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000479 }else{
480 abendError("unknown --mode: %s", z);
481 }
482 }else
drh048810b2015-04-24 23:45:23 +0000483 if( strcmp(z,"oom")==0 ){
484 oomFlag = 1;
485 }else
drh4a74d072015-04-20 18:58:38 +0000486 if( strcmp(z,"pagesize")==0 ){
487 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
488 pageSize = integerValue(argv[++i]);
489 }else
490 if( strcmp(z,"pcache")==0 ){
491 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
492 nPCache = integerValue(argv[i+1]);
493 szPCache = integerValue(argv[i+2]);
494 i += 2;
495 }else
drh1cbb7fa2015-04-24 13:00:59 +0000496 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
497 quietFlag = 1;
498 verboseFlag = 0;
499 }else
drh4a74d072015-04-20 18:58:38 +0000500 if( strcmp(z,"scratch")==0 ){
501 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
502 nScratch = integerValue(argv[i+1]);
503 szScratch = integerValue(argv[i+2]);
504 i += 2;
505 }else
drh875bafa2015-04-24 14:47:59 +0000506 if( strcmp(z, "unique-cases")==0 ){
507 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
508 if( zDataOut ) abendError("only one --minimize allowed");
509 zDataOut = argv[++i];
510 }else
drh4a74d072015-04-20 18:58:38 +0000511 if( strcmp(z,"utf16le")==0 ){
512 zEncoding = "utf16le";
513 }else
514 if( strcmp(z,"utf16be")==0 ){
515 zEncoding = "utf16be";
516 }else
drh1cbb7fa2015-04-24 13:00:59 +0000517 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
518 quietFlag = 0;
519 verboseFlag = 1;
520 }else
drh268e72f2015-04-17 14:30:49 +0000521 {
522 abendError("unknown option: %s", argv[i]);
523 }
524 }else{
525 abendError("unknown argument: %s", argv[i]);
526 }
527 }
drh1cbb7fa2015-04-24 13:00:59 +0000528 if( verboseFlag ) sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0);
drh4a74d072015-04-20 18:58:38 +0000529 if( nHeap>0 ){
530 pHeap = malloc( nHeap );
531 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
532 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
533 if( rc ) abendError("heap configuration failed: %d\n", rc);
534 }
drh048810b2015-04-24 23:45:23 +0000535 if( oomFlag ){
536 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem);
537 g.sOomMem = g.sOrigMem;
538 g.sOomMem.xMalloc = oomMalloc;
539 g.sOomMem.xRealloc = oomRealloc;
540 sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem);
541 }
drh4a74d072015-04-20 18:58:38 +0000542 if( nLook>0 ){
543 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
544 if( szLook>0 ){
545 pLook = malloc( nLook*szLook );
546 if( pLook==0 ) fatalError("out of memory");
547 }
548 }
549 if( nScratch>0 && szScratch>0 ){
550 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
551 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch",
552 nScratch*(sqlite3_int64)szScratch);
553 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
554 if( rc ) abendError("scratch configuration failed: %d\n", rc);
555 }
556 if( nPCache>0 && szPCache>0 ){
557 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
558 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
559 nPCache*(sqlite3_int64)szPCache);
560 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
561 if( rc ) abendError("pcache configuration failed: %d", rc);
562 }
drh268e72f2015-04-17 14:30:49 +0000563 while( !feof(in) ){
drhf34e9aa2015-04-20 12:50:13 +0000564 nAlloc += nAlloc+1000;
565 zIn = realloc(zIn, nAlloc);
drh268e72f2015-04-17 14:30:49 +0000566 if( zIn==0 ) fatalError("out of memory");
567 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in);
568 nIn += (int)got;
569 zIn[nIn] = 0;
570 if( got==0 ) break;
571 }
drh875bafa2015-04-24 14:47:59 +0000572 if( in!=stdin ) fclose(in);
573 if( zDataOut ){
574 rc = sqlite3_open(":memory:", &dataDb);
575 if( rc ) abendError("cannot open :memory: database");
576 rc = sqlite3_exec(dataDb,
577 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0);
578 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
579 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)",
580 -1, &pStmt, 0);
581 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
582 }
drhf34e9aa2015-04-20 12:50:13 +0000583 if( zInitDb ){
584 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
585 if( rc!=SQLITE_OK ){
586 abendError("unable to open initialization database \"%s\"", zInitDb);
587 }
drh268e72f2015-04-17 14:30:49 +0000588 }
drhe1a71a52015-04-24 16:09:12 +0000589 for(i=0; i<nIn; i=iNext+1){ /* Skip initial lines beginning with '#' */
590 if( zIn[i]!='#' ) break;
591 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){}
592 }
593 nHeader = i;
594 for(nTest=0; i<nIn; i=iNext, nTest++){
drhf34e9aa2015-04-20 12:50:13 +0000595 char cSaved;
596 if( strncmp(&zIn[i], "/****<",6)==0 ){
597 char *z = strstr(&zIn[i], ">****/");
598 if( z ){
599 z += 6;
drh9f18f742015-04-25 00:20:15 +0000600 if( verboseFlag ){
601 printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
602 fflush(stdout);
603 }
drhf34e9aa2015-04-20 12:50:13 +0000604 i += (int)(z-&zIn[i]);
drh1cbb7fa2015-04-24 13:00:59 +0000605 multiTest = 1;
drhf34e9aa2015-04-20 12:50:13 +0000606 }
607 }
608 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
drh875bafa2015-04-24 14:47:59 +0000609 if( zDataOut ){
610 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
611 rc = sqlite3_step(pStmt);
612 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
613 sqlite3_reset(pStmt);
614 continue;
615 }
drh3fb2cc12015-04-22 11:16:34 +0000616 cSaved = zIn[iNext];
617 zIn[iNext] = 0;
drh0ba51082015-04-22 13:16:46 +0000618 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
drh3fb2cc12015-04-22 11:16:34 +0000619 zIn[iNext] = cSaved;
620 continue;
621 }
drh9985dab2015-04-20 22:36:49 +0000622 zSql = &zIn[i];
drh1cbb7fa2015-04-24 13:00:59 +0000623 if( verboseFlag ){
624 printf("INPUT (offset: %d, size: %d): [%s]\n",
625 i, (int)strlen(&zIn[i]), &zIn[i]);
drh9f18f742015-04-25 00:20:15 +0000626 fflush(stdout);
drh1cbb7fa2015-04-24 13:00:59 +0000627 }else if( multiTest && !quietFlag ){
drh048810b2015-04-24 23:45:23 +0000628 int pct = oomFlag ? 100*iNext/nIn : ((10*iNext)/nIn)*10;
drh1cbb7fa2015-04-24 13:00:59 +0000629 if( pct!=lastPct ){
drhe1a71a52015-04-24 16:09:12 +0000630 if( lastPct<0 ) printf("fuzz test:");
drh048810b2015-04-24 23:45:23 +0000631 printf(" %d%%", pct);
drh1cbb7fa2015-04-24 13:00:59 +0000632 fflush(stdout);
633 lastPct = pct;
634 }
635 }
drh9985dab2015-04-20 22:36:49 +0000636 switch( iMode ){
637 case FZMODE_Glob:
638 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
639 break;
640 case FZMODE_Printf:
641 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
642 break;
643 case FZMODE_Strftime:
644 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
645 break;
646 }
drh048810b2015-04-24 23:45:23 +0000647 if( oomFlag ){
648 oomCnt = g.iOomCntdown = 1;
649 g.nOomFault = 0;
650 g.bOomOnce = 1;
drh9f18f742015-04-25 00:20:15 +0000651 if( verboseFlag ){
652 printf("Once.%d\n", oomCnt);
653 fflush(stdout);
654 }
drh048810b2015-04-24 23:45:23 +0000655 }else{
656 oomCnt = 0;
657 }
658 do{
659 rc = sqlite3_open_v2(
660 "main.db", &db,
661 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
662 0);
663 if( rc!=SQLITE_OK ){
664 abendError("Unable to open the in-memory database");
665 }
666 if( pLook ){
667 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
668 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
669 }
670 if( zInitDb ){
671 sqlite3_backup *pBackup;
672 pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
673 rc = sqlite3_backup_step(pBackup, -1);
674 if( rc!=SQLITE_DONE ){
675 abendError("attempt to initialize the in-memory database failed (rc=%d)",
676 rc);
677 }
678 sqlite3_backup_finish(pBackup);
679 }
680 #ifndef SQLITE_OMIT_TRACE
681 if( verboseFlag ) sqlite3_trace(db, traceCallback, 0);
682 #endif
683 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
684 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
685 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
686 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
687 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
688 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
689 g.bOomEnable = 1;
690 if( verboseFlag ){
691 zErrMsg = 0;
692 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg);
693 if( zErrMsg ){
694 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg);
695 zErrMsg = 0;
696 }
697 }else {
698 rc = sqlite3_exec(db, zSql, execNoop, 0, 0);
699 }
700 g.bOomEnable = 0;
701 rc = sqlite3_close(db);
702 if( rc ){
703 abendError("sqlite3_close() failed with rc=%d", rc);
704 }
705 if( sqlite3_memory_used()>0 ){
706 abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
707 }
708 if( oomFlag ){
709 if( g.nOomFault==0 || oomCnt>2000 ){
710 if( g.bOomOnce ){
711 oomCnt = g.iOomCntdown = 1;
712 g.bOomOnce = 0;
713 }else{
714 oomCnt = 0;
715 }
716 }else{
717 g.iOomCntdown = ++oomCnt;
718 g.nOomFault = 0;
719 }
720 if( oomCnt ){
721 if( verboseFlag ){
722 printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt);
drh9f18f742015-04-25 00:20:15 +0000723 fflush(stdout);
drh048810b2015-04-24 23:45:23 +0000724 }
725 nTest++;
726 }
727 }
728 }while( oomCnt>0 );
drh9985dab2015-04-20 22:36:49 +0000729 if( zToFree ){
730 sqlite3_free(zToFree);
731 zToFree = 0;
732 }
drhf34e9aa2015-04-20 12:50:13 +0000733 zIn[iNext] = cSaved;
drh1cbb7fa2015-04-24 13:00:59 +0000734 if( verboseFlag ){
735 printf("RESULT-CODE: %d\n", rc);
736 if( zErrMsg ){
drh048810b2015-04-24 23:45:23 +0000737 printf("ERROR-MSG: [%s]\n", zErrBuf);
drh1cbb7fa2015-04-24 13:00:59 +0000738 }
drh9f18f742015-04-25 00:20:15 +0000739 fflush(stdout);
drhf34e9aa2015-04-20 12:50:13 +0000740 }
drh048810b2015-04-24 23:45:23 +0000741 /* Simulate an error if the TEST_FAILURE environment variable is "5" */
742 if( zFailCode ){
743 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
744 abendError("simulated failure");
745 }else if( zFailCode[0]!=0 ){
746 /* If TEST_FAILURE is something other than 5, just exit the test
747 ** early */
748 printf("\nExit early due to TEST_FAILURE being set");
749 break;
drhe1a71a52015-04-24 16:09:12 +0000750 }
751 }
drhf34e9aa2015-04-20 12:50:13 +0000752 }
drhe1a71a52015-04-24 16:09:12 +0000753 if( !verboseFlag && multiTest && !quietFlag ) printf("\n");
drh1cbb7fa2015-04-24 13:00:59 +0000754 if( nTest>1 && !quietFlag ){
drhe1a71a52015-04-24 16:09:12 +0000755 printf("%d fuzz tests with no errors\nSQLite %s %s\n",
drh875bafa2015-04-24 14:47:59 +0000756 nTest, sqlite3_libversion(), sqlite3_sourceid());
757 }
758 if( zDataOut ){
drh875bafa2015-04-24 14:47:59 +0000759 int n = 0;
drhe1a71a52015-04-24 16:09:12 +0000760 FILE *out = fopen(zDataOut, "wb");
drh875bafa2015-04-24 14:47:59 +0000761 if( out==0 ) abendError("cannot open %s for writing", zDataOut);
drhe1a71a52015-04-24 16:09:12 +0000762 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out);
drh875bafa2015-04-24 14:47:59 +0000763 sqlite3_finalize(pStmt);
764 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0);
765 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
766 while( sqlite3_step(pStmt)==SQLITE_ROW ){
767 fprintf(out,"/****<%d>****/", ++n);
768 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
769 }
770 fclose(out);
771 sqlite3_finalize(pStmt);
772 sqlite3_close(dataDb);
drh1cbb7fa2015-04-24 13:00:59 +0000773 }
drhf34e9aa2015-04-20 12:50:13 +0000774 free(zIn);
drh4a74d072015-04-20 18:58:38 +0000775 free(pHeap);
776 free(pLook);
777 free(pScratch);
778 free(pPCache);
drhf34e9aa2015-04-20 12:50:13 +0000779 return 0;
drh268e72f2015-04-17 14:30:49 +0000780}