blob: 65c8fb78aca4b13508af3e12b86493d906cb411b [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
150/*
151** This callback is invoked by sqlite3_trace() as each SQL statement
152** starts.
153*/
154static void traceCallback(void *NotUsed, const char *zMsg){
155 printf("TRACE: %s\n", zMsg);
156}
157
158/***************************************************************************
159** eval() implementation copied from ../ext/misc/eval.c
160*/
161/*
162** Structure used to accumulate the output
163*/
164struct EvalResult {
165 char *z; /* Accumulated output */
166 const char *zSep; /* Separator */
167 int szSep; /* Size of the separator string */
168 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */
169 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */
170};
171
172/*
173** Callback from sqlite_exec() for the eval() function.
174*/
175static int callback(void *pCtx, int argc, char **argv, char **colnames){
176 struct EvalResult *p = (struct EvalResult*)pCtx;
177 int i;
178 for(i=0; i<argc; i++){
179 const char *z = argv[i] ? argv[i] : "";
180 size_t sz = strlen(z);
181 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){
182 char *zNew;
183 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1;
184 /* Using sqlite3_realloc64() would be better, but it is a recent
185 ** addition and will cause a segfault if loaded by an older version
186 ** of SQLite. */
187 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0;
188 if( zNew==0 ){
189 sqlite3_free(p->z);
190 memset(p, 0, sizeof(*p));
191 return 1;
192 }
193 p->z = zNew;
194 }
195 if( p->nUsed>0 ){
196 memcpy(&p->z[p->nUsed], p->zSep, p->szSep);
197 p->nUsed += p->szSep;
198 }
199 memcpy(&p->z[p->nUsed], z, sz);
200 p->nUsed += sz;
201 }
202 return 0;
203}
204
205/*
206** Implementation of the eval(X) and eval(X,Y) SQL functions.
207**
208** Evaluate the SQL text in X. Return the results, using string
209** Y as the separator. If Y is omitted, use a single space character.
210*/
211static void sqlEvalFunc(
212 sqlite3_context *context,
213 int argc,
214 sqlite3_value **argv
215){
216 const char *zSql;
217 sqlite3 *db;
218 char *zErr = 0;
219 int rc;
220 struct EvalResult x;
221
222 memset(&x, 0, sizeof(x));
223 x.zSep = " ";
224 zSql = (const char*)sqlite3_value_text(argv[0]);
225 if( zSql==0 ) return;
226 if( argc>1 ){
227 x.zSep = (const char*)sqlite3_value_text(argv[1]);
228 if( x.zSep==0 ) return;
229 }
230 x.szSep = (int)strlen(x.zSep);
231 db = sqlite3_context_db_handle(context);
232 rc = sqlite3_exec(db, zSql, callback, &x, &zErr);
233 if( rc!=SQLITE_OK ){
234 sqlite3_result_error(context, zErr, -1);
235 sqlite3_free(zErr);
236 }else if( x.zSep==0 ){
237 sqlite3_result_error_nomem(context);
238 sqlite3_free(x.z);
239 }else{
240 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free);
241 }
242}
243/* End of the eval() implementation
244******************************************************************************/
245
246/*
247** Print sketchy documentation for this utility program
248*/
249static void showHelp(void){
250 printf("Usage: %s [options]\n", g.zArgv0);
251 printf(
252"Read SQL text from standard input and evaluate it.\n"
253"Options:\n"
drh875bafa2015-04-24 14:47:59 +0000254" --autovacuum Enable AUTOVACUUM mode\n"
255" -f FILE Read SQL text from FILE instead of standard input\n"
256" --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
257" --help Show this help text\n"
258" --initdb DBFILE Initialize the in-memory database using template DBFILE\n"
259" --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
260" --pagesize N Set the page size to N\n"
261" --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
262" -q Reduced output\n"
263" --quiet Reduced output\n"
264" --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
265" --unique-cases FILE Write all unique test cases to FILE\n"
266" --utf16be Set text encoding to UTF-16BE\n"
267" --utf16le Set text encoding to UTF-16LE\n"
268" -v Increased output\n"
269" --verbose Increased output\n"
drh268e72f2015-04-17 14:30:49 +0000270 );
271}
272
drh4a74d072015-04-20 18:58:38 +0000273/*
274** Return the value of a hexadecimal digit. Return -1 if the input
275** is not a hex digit.
276*/
277static int hexDigitValue(char c){
278 if( c>='0' && c<='9' ) return c - '0';
279 if( c>='a' && c<='f' ) return c - 'a' + 10;
280 if( c>='A' && c<='F' ) return c - 'A' + 10;
281 return -1;
282}
283
284/*
285** Interpret zArg as an integer value, possibly with suffixes.
286*/
287static int integerValue(const char *zArg){
288 sqlite3_int64 v = 0;
289 static const struct { char *zSuffix; int iMult; } aMult[] = {
290 { "KiB", 1024 },
291 { "MiB", 1024*1024 },
292 { "GiB", 1024*1024*1024 },
293 { "KB", 1000 },
294 { "MB", 1000000 },
295 { "GB", 1000000000 },
296 { "K", 1000 },
297 { "M", 1000000 },
298 { "G", 1000000000 },
299 };
300 int i;
301 int isNeg = 0;
302 if( zArg[0]=='-' ){
303 isNeg = 1;
304 zArg++;
305 }else if( zArg[0]=='+' ){
306 zArg++;
307 }
308 if( zArg[0]=='0' && zArg[1]=='x' ){
309 int x;
310 zArg += 2;
311 while( (x = hexDigitValue(zArg[0]))>=0 ){
312 v = (v<<4) + x;
313 zArg++;
314 }
315 }else{
316 while( isdigit(zArg[0]) ){
317 v = v*10 + zArg[0] - '0';
318 zArg++;
319 }
320 }
321 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
322 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
323 v *= aMult[i].iMult;
324 break;
325 }
326 }
327 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648");
328 return (int)(isNeg? -v : v);
329}
330
drh9985dab2015-04-20 22:36:49 +0000331/*
332** Various operating modes
333*/
334#define FZMODE_Generic 1
335#define FZMODE_Strftime 2
336#define FZMODE_Printf 3
337#define FZMODE_Glob 4
338
drh268e72f2015-04-17 14:30:49 +0000339
340int main(int argc, char **argv){
341 char *zIn = 0; /* Input text */
342 int nAlloc = 0; /* Number of bytes allocated for zIn[] */
343 int nIn = 0; /* Number of bytes of zIn[] used */
344 size_t got; /* Bytes read from input */
345 FILE *in = stdin; /* Where to read SQL text from */
346 int rc = SQLITE_OK; /* Result codes from API functions */
347 int i; /* Loop counter */
drhf34e9aa2015-04-20 12:50:13 +0000348 int iNext; /* Next block of SQL */
drh268e72f2015-04-17 14:30:49 +0000349 sqlite3 *db; /* Open database */
drhf34e9aa2015-04-20 12:50:13 +0000350 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */
drh268e72f2015-04-17 14:30:49 +0000351 const char *zInitDb = 0;/* Name of the initialization database file */
352 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */
drh4a74d072015-04-20 18:58:38 +0000353 const char *zEncoding = 0; /* --utf16be or --utf16le */
354 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
355 int nLook = 0, szLook = 0; /* --lookaside configuration */
356 int nPCache = 0, szPCache = 0;/* --pcache configuration */
357 int nScratch = 0, szScratch=0;/* --scratch configuration */
358 int pageSize = 0; /* Desired page size. 0 means default */
359 void *pHeap = 0; /* Allocated heap space */
360 void *pLook = 0; /* Allocated lookaside space */
361 void *pPCache = 0; /* Allocated storage for pcache */
362 void *pScratch = 0; /* Allocated storage for scratch */
363 int doAutovac = 0; /* True for --autovacuum */
drh9985dab2015-04-20 22:36:49 +0000364 char *zSql; /* SQL to run */
365 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */
366 int iMode = FZMODE_Generic; /* Operating mode */
drh0ba51082015-04-22 13:16:46 +0000367 const char *zCkGlob = 0; /* Inputs must match this glob */
drh1cbb7fa2015-04-24 13:00:59 +0000368 int verboseFlag = 0; /* --verbose or -v flag */
369 int quietFlag = 0; /* --quiet or -q flag */
370 int nTest = 0; /* Number of test cases run */
371 int multiTest = 0; /* True if there will be multiple test cases */
372 int lastPct = -1; /* Previous percentage done output */
drh875bafa2015-04-24 14:47:59 +0000373 sqlite3 *dataDb = 0; /* Database holding compacted input data */
374 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */
375 const char *zDataOut = 0; /* Write compacted data to this output file */
drh4a74d072015-04-20 18:58:38 +0000376
drh268e72f2015-04-17 14:30:49 +0000377
378 g.zArgv0 = argv[0];
379 for(i=1; i<argc; i++){
380 const char *z = argv[i];
381 if( z[0]=='-' ){
382 z++;
383 if( z[0]=='-' ) z++;
drh4a74d072015-04-20 18:58:38 +0000384 if( strcmp(z,"autovacuum")==0 ){
385 doAutovac = 1;
drh268e72f2015-04-17 14:30:49 +0000386 }else
387 if( strcmp(z, "f")==0 && i+1<argc ){
388 if( in!=stdin ) abendError("only one -f allowed");
389 in = fopen(argv[++i],"rb");
390 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]);
391 }else
drh4a74d072015-04-20 18:58:38 +0000392 if( strcmp(z,"heap")==0 ){
393 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]);
394 nHeap = integerValue(argv[i+1]);
395 mnHeap = integerValue(argv[i+2]);
396 i += 2;
397 }else
398 if( strcmp(z,"help")==0 ){
399 showHelp();
400 return 0;
401 }else
drh268e72f2015-04-17 14:30:49 +0000402 if( strcmp(z, "initdb")==0 && i+1<argc ){
403 if( zInitDb!=0 ) abendError("only one --initdb allowed");
404 zInitDb = argv[++i];
405 }else
drh4a74d072015-04-20 18:58:38 +0000406 if( strcmp(z,"lookaside")==0 ){
407 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
408 nLook = integerValue(argv[i+1]);
409 szLook = integerValue(argv[i+2]);
410 i += 2;
411 }else
drh9985dab2015-04-20 22:36:49 +0000412 if( strcmp(z,"mode")==0 ){
413 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
414 z = argv[++i];
415 if( strcmp(z,"generic")==0 ){
416 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000417 zCkGlob = 0;
drh9985dab2015-04-20 22:36:49 +0000418 }else if( strcmp(z, "glob")==0 ){
419 iMode = FZMODE_Glob;
drh0ba51082015-04-22 13:16:46 +0000420 zCkGlob = "'*','*'";
drh9985dab2015-04-20 22:36:49 +0000421 }else if( strcmp(z, "printf")==0 ){
422 iMode = FZMODE_Printf;
drh0ba51082015-04-22 13:16:46 +0000423 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000424 }else if( strcmp(z, "strftime")==0 ){
425 iMode = FZMODE_Strftime;
drh0ba51082015-04-22 13:16:46 +0000426 zCkGlob = "'*',*";
drh9985dab2015-04-20 22:36:49 +0000427 }else{
428 abendError("unknown --mode: %s", z);
429 }
430 }else
drh4a74d072015-04-20 18:58:38 +0000431 if( strcmp(z,"pagesize")==0 ){
432 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]);
433 pageSize = integerValue(argv[++i]);
434 }else
435 if( strcmp(z,"pcache")==0 ){
436 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
437 nPCache = integerValue(argv[i+1]);
438 szPCache = integerValue(argv[i+2]);
439 i += 2;
440 }else
drh1cbb7fa2015-04-24 13:00:59 +0000441 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
442 quietFlag = 1;
443 verboseFlag = 0;
444 }else
drh4a74d072015-04-20 18:58:38 +0000445 if( strcmp(z,"scratch")==0 ){
446 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]);
447 nScratch = integerValue(argv[i+1]);
448 szScratch = integerValue(argv[i+2]);
449 i += 2;
450 }else
drh875bafa2015-04-24 14:47:59 +0000451 if( strcmp(z, "unique-cases")==0 ){
452 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]);
453 if( zDataOut ) abendError("only one --minimize allowed");
454 zDataOut = argv[++i];
455 }else
drh4a74d072015-04-20 18:58:38 +0000456 if( strcmp(z,"utf16le")==0 ){
457 zEncoding = "utf16le";
458 }else
459 if( strcmp(z,"utf16be")==0 ){
460 zEncoding = "utf16be";
461 }else
drh1cbb7fa2015-04-24 13:00:59 +0000462 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
463 quietFlag = 0;
464 verboseFlag = 1;
465 }else
drh268e72f2015-04-17 14:30:49 +0000466 {
467 abendError("unknown option: %s", argv[i]);
468 }
469 }else{
470 abendError("unknown argument: %s", argv[i]);
471 }
472 }
drh1cbb7fa2015-04-24 13:00:59 +0000473 if( verboseFlag ) sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0);
drh4a74d072015-04-20 18:58:38 +0000474 if( nHeap>0 ){
475 pHeap = malloc( nHeap );
476 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap);
477 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
478 if( rc ) abendError("heap configuration failed: %d\n", rc);
479 }
480 if( nLook>0 ){
481 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
482 if( szLook>0 ){
483 pLook = malloc( nLook*szLook );
484 if( pLook==0 ) fatalError("out of memory");
485 }
486 }
487 if( nScratch>0 && szScratch>0 ){
488 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
489 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch",
490 nScratch*(sqlite3_int64)szScratch);
491 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
492 if( rc ) abendError("scratch configuration failed: %d\n", rc);
493 }
494 if( nPCache>0 && szPCache>0 ){
495 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
496 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache",
497 nPCache*(sqlite3_int64)szPCache);
498 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
499 if( rc ) abendError("pcache configuration failed: %d", rc);
500 }
drh268e72f2015-04-17 14:30:49 +0000501 while( !feof(in) ){
drhf34e9aa2015-04-20 12:50:13 +0000502 nAlloc += nAlloc+1000;
503 zIn = realloc(zIn, nAlloc);
drh268e72f2015-04-17 14:30:49 +0000504 if( zIn==0 ) fatalError("out of memory");
505 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in);
506 nIn += (int)got;
507 zIn[nIn] = 0;
508 if( got==0 ) break;
509 }
drh875bafa2015-04-24 14:47:59 +0000510 if( in!=stdin ) fclose(in);
511 if( zDataOut ){
512 rc = sqlite3_open(":memory:", &dataDb);
513 if( rc ) abendError("cannot open :memory: database");
514 rc = sqlite3_exec(dataDb,
515 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0);
516 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
517 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)",
518 -1, &pStmt, 0);
519 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
520 }
drhf34e9aa2015-04-20 12:50:13 +0000521 if( zInitDb ){
522 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0);
523 if( rc!=SQLITE_OK ){
524 abendError("unable to open initialization database \"%s\"", zInitDb);
525 }
drh268e72f2015-04-17 14:30:49 +0000526 }
drh1cbb7fa2015-04-24 13:00:59 +0000527 for(i=nTest=0; i<nIn; i=iNext, nTest++){
drhf34e9aa2015-04-20 12:50:13 +0000528 char cSaved;
529 if( strncmp(&zIn[i], "/****<",6)==0 ){
530 char *z = strstr(&zIn[i], ">****/");
531 if( z ){
532 z += 6;
drh1cbb7fa2015-04-24 13:00:59 +0000533 if( verboseFlag ) printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]);
drhf34e9aa2015-04-20 12:50:13 +0000534 i += (int)(z-&zIn[i]);
drh1cbb7fa2015-04-24 13:00:59 +0000535 multiTest = 1;
drhf34e9aa2015-04-20 12:50:13 +0000536 }
537 }
538 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){}
drh875bafa2015-04-24 14:47:59 +0000539 if( zDataOut ){
540 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC);
541 rc = sqlite3_step(pStmt);
542 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb));
543 sqlite3_reset(pStmt);
544 continue;
545 }
drh3fb2cc12015-04-22 11:16:34 +0000546 cSaved = zIn[iNext];
547 zIn[iNext] = 0;
drh0ba51082015-04-22 13:16:46 +0000548 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){
drh3fb2cc12015-04-22 11:16:34 +0000549 zIn[iNext] = cSaved;
550 continue;
551 }
drhf34e9aa2015-04-20 12:50:13 +0000552 rc = sqlite3_open_v2(
553 "main.db", &db,
554 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY,
555 0);
556 if( rc!=SQLITE_OK ){
557 abendError("Unable to open the in-memory database");
558 }
drh4a74d072015-04-20 18:58:38 +0000559 if( pLook ){
560 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook);
561 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc);
562 }
drhf34e9aa2015-04-20 12:50:13 +0000563 if( zInitDb ){
564 sqlite3_backup *pBackup;
565 pBackup = sqlite3_backup_init(db, "main", dbInit, "main");
566 rc = sqlite3_backup_step(pBackup, -1);
567 if( rc!=SQLITE_DONE ){
568 abendError("attempt to initialize the in-memory database failed (rc=%d)",
569 rc);
570 }
571 sqlite3_backup_finish(pBackup);
572 }
drh1cbb7fa2015-04-24 13:00:59 +0000573 if( verboseFlag ) sqlite3_trace(db, traceCallback, 0);
drhf34e9aa2015-04-20 12:50:13 +0000574 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
575 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0);
576 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
drh4a74d072015-04-20 18:58:38 +0000577 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding);
578 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize);
579 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL");
drh9985dab2015-04-20 22:36:49 +0000580 zSql = &zIn[i];
drh1cbb7fa2015-04-24 13:00:59 +0000581 if( verboseFlag ){
582 printf("INPUT (offset: %d, size: %d): [%s]\n",
583 i, (int)strlen(&zIn[i]), &zIn[i]);
584 }else if( multiTest && !quietFlag ){
585 int pct = 100*(i+strlen(zSql))/nIn;
586 if( pct!=lastPct ){
587 printf("%d%%\r", pct);
588 fflush(stdout);
589 lastPct = pct;
590 }
591 }
drh9985dab2015-04-20 22:36:49 +0000592 switch( iMode ){
593 case FZMODE_Glob:
594 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql);
595 break;
596 case FZMODE_Printf:
597 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql);
598 break;
599 case FZMODE_Strftime:
600 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql);
601 break;
602 }
drh1cbb7fa2015-04-24 13:00:59 +0000603 zErrMsg = 0;
604 rc = sqlite3_exec(db, zSql, verboseFlag ? execCallback : execNoop, 0, &zErrMsg);
drh9985dab2015-04-20 22:36:49 +0000605 if( zToFree ){
606 sqlite3_free(zToFree);
607 zToFree = 0;
608 }
drhf34e9aa2015-04-20 12:50:13 +0000609 zIn[iNext] = cSaved;
drh1cbb7fa2015-04-24 13:00:59 +0000610 if( verboseFlag ){
611 printf("RESULT-CODE: %d\n", rc);
612 if( zErrMsg ){
613 printf("ERROR-MSG: [%s]\n", zErrMsg);
614 }
drhf34e9aa2015-04-20 12:50:13 +0000615 }
drh1cbb7fa2015-04-24 13:00:59 +0000616 sqlite3_free(zErrMsg);
drhf34e9aa2015-04-20 12:50:13 +0000617 rc = sqlite3_close(db);
618 if( rc ){
619 abendError("sqlite3_close() failed with rc=%d", rc);
620 }
621 if( sqlite3_memory_used()>0 ){
622 abendError("memory in use after close: %lld bytes", sqlite3_memory_used());
623 }
624 }
drh1cbb7fa2015-04-24 13:00:59 +0000625 if( nTest>1 && !quietFlag ){
drh875bafa2015-04-24 14:47:59 +0000626 printf("%d tests with no errors\nSQLite %s %s\n",
627 nTest, sqlite3_libversion(), sqlite3_sourceid());
628 }
629 if( zDataOut ){
630 FILE *out = fopen(zDataOut, "wb");
631 int n = 0;
632 if( out==0 ) abendError("cannot open %s for writing", zDataOut);
633 sqlite3_finalize(pStmt);
634 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0);
635 if( rc ) abendError("%s", sqlite3_errmsg(dataDb));
636 while( sqlite3_step(pStmt)==SQLITE_ROW ){
637 fprintf(out,"/****<%d>****/", ++n);
638 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out);
639 }
640 fclose(out);
641 sqlite3_finalize(pStmt);
642 sqlite3_close(dataDb);
drh1cbb7fa2015-04-24 13:00:59 +0000643 }
drhf34e9aa2015-04-20 12:50:13 +0000644 free(zIn);
drh4a74d072015-04-20 18:58:38 +0000645 free(pHeap);
646 free(pLook);
647 free(pScratch);
648 free(pPCache);
drhf34e9aa2015-04-20 12:50:13 +0000649 return 0;
drh268e72f2015-04-17 14:30:49 +0000650}