1 /* 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. 34 ** 35 ** (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: 39 ** 40 ** |****<...>****| 41 ** 42 ** where the "..." is arbitrary text. (Except the "|" should really be "/". 43 ** "|" is used here to avoid compiler errors about nested comments.) 44 ** A separate in-memory SQLite database is created to run each test case. 45 ** This feature allows the "queue" of AFL to be captured into a single big 46 ** 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 51 ** can be run in a single go (for regression testing, for example) by typing: 52 ** 53 ** fuzzershell -f ~/all-queue.txt 54 ** 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. 58 ** 59 ** 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: 61 ** 62 ** fuzzershell -f ~/all-queue.txt --unique-cases ~/unique-cases.txt 63 */ 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <stdarg.h> 68 #include <ctype.h> 69 #include "sqlite3.h" 70 71 /* 72 ** All global variables are gathered into the "g" singleton. 73 */ 74 struct GlobalVars { 75 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() */ 83 char zTestName[100]; /* Name of current test */ 84 } g; 85 86 /* 87 ** Maximum number of iterations for an OOM test 88 */ 89 #ifndef OOM_MAX 90 # define OOM_MAX 625 91 #endif 92 93 /* 94 ** This routine is called when a simulated OOM occurs. It exists as a 95 ** convenient place to set a debugger breakpoint. 96 */ 97 static void oomFault(void){ 98 g.nOomBrkpt++; /* Prevent oomFault() from being optimized out */ 99 } 100 101 102 /* Versions of malloc() and realloc() that simulate OOM conditions */ 103 static 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 } 115 static 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 128 /* 129 ** Print an error message and abort in such a way to indicate to the 130 ** fuzzer that this counts as a crash. 131 */ 132 static void abendError(const char *zFormat, ...){ 133 va_list ap; 134 if( g.zTestName[0] ){ 135 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); 136 }else{ 137 fprintf(stderr, "%s: ", g.zArgv0); 138 } 139 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 */ 149 static void fatalError(const char *zFormat, ...){ 150 va_list ap; 151 if( g.zTestName[0] ){ 152 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); 153 }else{ 154 fprintf(stderr, "%s: ", g.zArgv0); 155 } 156 va_start(ap, zFormat); 157 vfprintf(stderr, zFormat, ap); 158 va_end(ap); 159 fprintf(stderr, "\n"); 160 exit(1); 161 } 162 163 /* 164 ** Evaluate some SQL. Abort if unable. 165 */ 166 static 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 /* 180 ** This callback is invoked by sqlite3_log(). 181 */ 182 static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){ 183 printf("LOG: (%d) %s\n", iErrCode, zMsg); 184 fflush(stdout); 185 } 186 static void shellLogNoop(void *pNotUsed, int iErrCode, const char *zMsg){ 187 return; 188 } 189 190 /* 191 ** This callback is invoked by sqlite3_exec() to return query results. 192 */ 193 static 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 } 205 fflush(stdout); 206 return 0; 207 } 208 static int execNoop(void *NotUsed, int argc, char **argv, char **colv){ 209 return 0; 210 } 211 212 #ifndef SQLITE_OMIT_TRACE 213 /* 214 ** This callback is invoked by sqlite3_trace() as each SQL statement 215 ** starts. 216 */ 217 static void traceCallback(void *NotUsed, const char *zMsg){ 218 printf("TRACE: %s\n", zMsg); 219 fflush(stdout); 220 } 221 static void traceNoop(void *NotUsed, const char *zMsg){ 222 return; 223 } 224 #endif 225 226 /*************************************************************************** 227 ** eval() implementation copied from ../ext/misc/eval.c 228 */ 229 /* 230 ** Structure used to accumulate the output 231 */ 232 struct 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 */ 243 static 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 */ 279 static 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 */ 317 static void showHelp(void){ 318 printf("Usage: %s [options] ?FILE...?\n", g.zArgv0); 319 printf( 320 "Read SQL text from FILE... (or from standard input if FILE... is omitted)\n" 321 "and then evaluate each block of SQL contained therein.\n" 322 "Options:\n" 323 " --autovacuum Enable AUTOVACUUM mode\n" 324 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" 325 " --help Show this help text\n" 326 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" 327 " --oom Run each test multiple times in a simulated OOM loop\n" 328 " --pagesize N Set the page size to N\n" 329 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" 330 " -q Reduced output\n" 331 " --quiet Reduced output\n" 332 " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" 333 " --unique-cases FILE Write all unique test cases to FILE\n" 334 " --utf16be Set text encoding to UTF-16BE\n" 335 " --utf16le Set text encoding to UTF-16LE\n" 336 " -v Increased output\n" 337 " --verbose Increased output\n" 338 ); 339 } 340 341 /* 342 ** Return the value of a hexadecimal digit. Return -1 if the input 343 ** is not a hex digit. 344 */ 345 static int hexDigitValue(char c){ 346 if( c>='0' && c<='9' ) return c - '0'; 347 if( c>='a' && c<='f' ) return c - 'a' + 10; 348 if( c>='A' && c<='F' ) return c - 'A' + 10; 349 return -1; 350 } 351 352 /* 353 ** Interpret zArg as an integer value, possibly with suffixes. 354 */ 355 static int integerValue(const char *zArg){ 356 sqlite3_int64 v = 0; 357 static const struct { char *zSuffix; int iMult; } aMult[] = { 358 { "KiB", 1024 }, 359 { "MiB", 1024*1024 }, 360 { "GiB", 1024*1024*1024 }, 361 { "KB", 1000 }, 362 { "MB", 1000000 }, 363 { "GB", 1000000000 }, 364 { "K", 1000 }, 365 { "M", 1000000 }, 366 { "G", 1000000000 }, 367 }; 368 int i; 369 int isNeg = 0; 370 if( zArg[0]=='-' ){ 371 isNeg = 1; 372 zArg++; 373 }else if( zArg[0]=='+' ){ 374 zArg++; 375 } 376 if( zArg[0]=='0' && zArg[1]=='x' ){ 377 int x; 378 zArg += 2; 379 while( (x = hexDigitValue(zArg[0]))>=0 ){ 380 v = (v<<4) + x; 381 zArg++; 382 } 383 }else{ 384 while( isdigit(zArg[0]) ){ 385 v = v*10 + zArg[0] - '0'; 386 zArg++; 387 } 388 } 389 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ 390 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 391 v *= aMult[i].iMult; 392 break; 393 } 394 } 395 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648"); 396 return (int)(isNeg? -v : v); 397 } 398 399 /* Return the current wall-clock time */ 400 static sqlite3_int64 timeOfDay(void){ 401 static sqlite3_vfs *clockVfs = 0; 402 sqlite3_int64 t; 403 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 404 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ 405 clockVfs->xCurrentTimeInt64(clockVfs, &t); 406 }else{ 407 double r; 408 clockVfs->xCurrentTime(clockVfs, &r); 409 t = (sqlite3_int64)(r*86400000.0); 410 } 411 return t; 412 } 413 414 int main(int argc, char **argv){ 415 char *zIn = 0; /* Input text */ 416 int nAlloc = 0; /* Number of bytes allocated for zIn[] */ 417 int nIn = 0; /* Number of bytes of zIn[] used */ 418 size_t got; /* Bytes read from input */ 419 int rc = SQLITE_OK; /* Result codes from API functions */ 420 int i; /* Loop counter */ 421 int iNext; /* Next block of SQL */ 422 sqlite3 *db; /* Open database */ 423 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */ 424 const char *zEncoding = 0; /* --utf16be or --utf16le */ 425 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ 426 int nLook = 0, szLook = 0; /* --lookaside configuration */ 427 int nPCache = 0, szPCache = 0;/* --pcache configuration */ 428 int nScratch = 0, szScratch=0;/* --scratch configuration */ 429 int pageSize = 0; /* Desired page size. 0 means default */ 430 void *pHeap = 0; /* Allocated heap space */ 431 void *pLook = 0; /* Allocated lookaside space */ 432 void *pPCache = 0; /* Allocated storage for pcache */ 433 void *pScratch = 0; /* Allocated storage for scratch */ 434 int doAutovac = 0; /* True for --autovacuum */ 435 char *zSql; /* SQL to run */ 436 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */ 437 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 */ 442 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 */ 445 int nHeader = 0; /* Bytes of header comment text on input file */ 446 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 */ 450 const char *zPrompt; /* Initial prompt when large-file fuzzing */ 451 int nInFile = 0; /* Number of input files to read */ 452 char **azInFile = 0; /* Array of input file names */ 453 int jj; /* Loop counter for azInFile[] */ 454 sqlite3_int64 iStart, iEnd; /* Start and end-times for a test case */ 455 456 457 zFailCode = getenv("TEST_FAILURE"); 458 g.zArgv0 = argv[0]; 459 zPrompt = "<stdin>"; 460 for(i=1; i<argc; i++){ 461 const char *z = argv[i]; 462 if( z[0]=='-' ){ 463 z++; 464 if( z[0]=='-' ) z++; 465 if( strcmp(z,"autovacuum")==0 ){ 466 doAutovac = 1; 467 }else 468 if( strcmp(z, "f")==0 && i+1<argc ){ 469 i++; 470 goto addNewInFile; 471 }else 472 if( strcmp(z,"heap")==0 ){ 473 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]); 474 nHeap = integerValue(argv[i+1]); 475 mnHeap = integerValue(argv[i+2]); 476 i += 2; 477 }else 478 if( strcmp(z,"help")==0 ){ 479 showHelp(); 480 return 0; 481 }else 482 if( strcmp(z,"lookaside")==0 ){ 483 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 484 nLook = integerValue(argv[i+1]); 485 szLook = integerValue(argv[i+2]); 486 i += 2; 487 }else 488 if( strcmp(z,"oom")==0 ){ 489 oomFlag = 1; 490 }else 491 if( strcmp(z,"pagesize")==0 ){ 492 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]); 493 pageSize = integerValue(argv[++i]); 494 }else 495 if( strcmp(z,"pcache")==0 ){ 496 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 497 nPCache = integerValue(argv[i+1]); 498 szPCache = integerValue(argv[i+2]); 499 i += 2; 500 }else 501 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 502 quietFlag = 1; 503 verboseFlag = 0; 504 }else 505 if( strcmp(z,"scratch")==0 ){ 506 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 507 nScratch = integerValue(argv[i+1]); 508 szScratch = integerValue(argv[i+2]); 509 i += 2; 510 }else 511 if( strcmp(z, "unique-cases")==0 ){ 512 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]); 513 if( zDataOut ) abendError("only one --minimize allowed"); 514 zDataOut = argv[++i]; 515 }else 516 if( strcmp(z,"utf16le")==0 ){ 517 zEncoding = "utf16le"; 518 }else 519 if( strcmp(z,"utf16be")==0 ){ 520 zEncoding = "utf16be"; 521 }else 522 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ 523 quietFlag = 0; 524 verboseFlag = 1; 525 }else 526 { 527 abendError("unknown option: %s", argv[i]); 528 } 529 }else{ 530 addNewInFile: 531 nInFile++; 532 azInFile = realloc(azInFile, sizeof(azInFile[0])*nInFile); 533 if( azInFile==0 ) abendError("out of memory"); 534 azInFile[nInFile-1] = argv[i]; 535 } 536 } 537 538 /* Do global SQLite initialization */ 539 sqlite3_config(SQLITE_CONFIG_LOG, verboseFlag ? shellLog : shellLogNoop, 0); 540 if( nHeap>0 ){ 541 pHeap = malloc( nHeap ); 542 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap); 543 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); 544 if( rc ) abendError("heap configuration failed: %d\n", rc); 545 } 546 if( oomFlag ){ 547 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem); 548 g.sOomMem = g.sOrigMem; 549 g.sOomMem.xMalloc = oomMalloc; 550 g.sOomMem.xRealloc = oomRealloc; 551 sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem); 552 } 553 if( nLook>0 ){ 554 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); 555 if( szLook>0 ){ 556 pLook = malloc( nLook*szLook ); 557 if( pLook==0 ) fatalError("out of memory"); 558 } 559 } 560 if( nScratch>0 && szScratch>0 ){ 561 pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); 562 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch", 563 nScratch*(sqlite3_int64)szScratch); 564 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); 565 if( rc ) abendError("scratch configuration failed: %d\n", rc); 566 } 567 if( nPCache>0 && szPCache>0 ){ 568 pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); 569 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache", 570 nPCache*(sqlite3_int64)szPCache); 571 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); 572 if( rc ) abendError("pcache configuration failed: %d", rc); 573 } 574 575 /* If the --unique-cases option was supplied, open the database that will 576 ** be used to gather unique test cases. 577 */ 578 if( zDataOut ){ 579 rc = sqlite3_open(":memory:", &dataDb); 580 if( rc ) abendError("cannot open :memory: database"); 581 rc = sqlite3_exec(dataDb, 582 "CREATE TABLE testcase(sql BLOB PRIMARY KEY, tm) WITHOUT ROWID;",0,0,0); 583 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 584 rc = sqlite3_prepare_v2(dataDb, 585 "INSERT OR IGNORE INTO testcase(sql,tm)VALUES(?1,?2)", 586 -1, &pStmt, 0); 587 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 588 } 589 590 /* Initialize the input buffer used to hold SQL text */ 591 if( nInFile==0 ) nInFile = 1; 592 nAlloc = 1000; 593 zIn = malloc(nAlloc); 594 if( zIn==0 ) fatalError("out of memory"); 595 596 /* Loop over all input files */ 597 for(jj=0; jj<nInFile; jj++){ 598 599 /* Read the complete content of the next input file into zIn[] */ 600 FILE *in; 601 if( azInFile ){ 602 int j, k; 603 in = fopen(azInFile[jj],"rb"); 604 if( in==0 ){ 605 abendError("cannot open %s for reading", azInFile[jj]); 606 } 607 zPrompt = azInFile[jj]; 608 for(j=k=0; zPrompt[j]; j++) if( zPrompt[j]=='/' ) k = j+1; 609 zPrompt += k; 610 }else{ 611 in = stdin; 612 zPrompt = "<stdin>"; 613 } 614 while( !feof(in) ){ 615 zIn = realloc(zIn, nAlloc); 616 if( zIn==0 ) fatalError("out of memory"); 617 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); 618 nIn += (int)got; 619 zIn[nIn] = 0; 620 if( got==0 ) break; 621 nAlloc += nAlloc+1000; 622 } 623 if( in!=stdin ) fclose(in); 624 lastPct = -1; 625 626 /* Skip initial lines of the input file that begin with "#" */ 627 for(i=0; i<nIn; i=iNext+1){ 628 if( zIn[i]!='#' ) break; 629 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){} 630 } 631 nHeader = i; 632 633 /* Process all test cases contained within the input file. 634 */ 635 for(; i<nIn; i=iNext, nTest++, g.zTestName[0]=0){ 636 char cSaved; 637 if( strncmp(&zIn[i], "/****<",6)==0 ){ 638 char *z = strstr(&zIn[i], ">****/"); 639 if( z ){ 640 z += 6; 641 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s", 642 (int)(z-&zIn[i]) - 12, &zIn[i+6]); 643 if( verboseFlag ){ 644 printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]); 645 fflush(stdout); 646 } 647 i += (int)(z-&zIn[i]); 648 multiTest = 1; 649 } 650 } 651 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){} 652 cSaved = zIn[iNext]; 653 zIn[iNext] = 0; 654 655 656 /* Print out the SQL of the next test case is --verbose is enabled 657 */ 658 zSql = &zIn[i]; 659 if( verboseFlag ){ 660 printf("INPUT (offset: %d, size: %d): [%s]\n", 661 i, (int)strlen(&zIn[i]), &zIn[i]); 662 }else if( multiTest && !quietFlag ){ 663 if( oomFlag ){ 664 printf("%s\n", g.zTestName); 665 }else{ 666 int pct = (10*iNext)/nIn; 667 if( pct!=lastPct ){ 668 if( lastPct<0 ) printf("%s:", zPrompt); 669 printf(" %d%%", pct*10); 670 lastPct = pct; 671 } 672 } 673 } 674 fflush(stdout); 675 676 /* Run the next test case. Run it multiple times in --oom mode 677 */ 678 if( oomFlag ){ 679 oomCnt = g.iOomCntdown = 1; 680 g.nOomFault = 0; 681 g.bOomOnce = 1; 682 if( verboseFlag ){ 683 printf("Once.%d\n", oomCnt); 684 fflush(stdout); 685 } 686 }else{ 687 oomCnt = 0; 688 } 689 do{ 690 rc = sqlite3_open_v2( 691 "main.db", &db, 692 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 693 0); 694 if( rc!=SQLITE_OK ){ 695 abendError("Unable to open the in-memory database"); 696 } 697 if( pLook ){ 698 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLook); 699 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc); 700 } 701 #ifndef SQLITE_OMIT_TRACE 702 sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0); 703 #endif 704 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); 705 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); 706 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); 707 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); 708 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); 709 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); 710 iStart = timeOfDay(); 711 g.bOomEnable = 1; 712 if( verboseFlag ){ 713 zErrMsg = 0; 714 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg); 715 if( zErrMsg ){ 716 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg); 717 zErrMsg = 0; 718 } 719 }else { 720 rc = sqlite3_exec(db, zSql, execNoop, 0, 0); 721 } 722 g.bOomEnable = 0; 723 iEnd = timeOfDay(); 724 rc = sqlite3_close(db); 725 if( rc ){ 726 abendError("sqlite3_close() failed with rc=%d", rc); 727 } 728 if( !zDataOut && sqlite3_memory_used()>0 ){ 729 abendError("memory in use after close: %lld bytes",sqlite3_memory_used()); 730 } 731 if( oomFlag ){ 732 /* Limit the number of iterations of the OOM loop to OOM_MAX. If the 733 ** first pass (single failure) exceeds 2/3rds of OOM_MAX this skip the 734 ** second pass (continuous failure after first) completely. */ 735 if( g.nOomFault==0 || oomCnt>OOM_MAX ){ 736 if( g.bOomOnce && oomCnt<=(OOM_MAX*2/3) ){ 737 oomCnt = g.iOomCntdown = 1; 738 g.bOomOnce = 0; 739 }else{ 740 oomCnt = 0; 741 } 742 }else{ 743 g.iOomCntdown = ++oomCnt; 744 g.nOomFault = 0; 745 } 746 if( oomCnt ){ 747 if( verboseFlag ){ 748 printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt); 749 fflush(stdout); 750 } 751 nTest++; 752 } 753 } 754 }while( oomCnt>0 ); 755 756 /* Store unique test cases in the in the dataDb database if the 757 ** --unique-cases flag is present 758 */ 759 if( zDataOut ){ 760 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC); 761 sqlite3_bind_int64(pStmt, 2, iEnd - iStart); 762 rc = sqlite3_step(pStmt); 763 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb)); 764 sqlite3_reset(pStmt); 765 } 766 767 /* Free the SQL from the current test case 768 */ 769 if( zToFree ){ 770 sqlite3_free(zToFree); 771 zToFree = 0; 772 } 773 zIn[iNext] = cSaved; 774 775 /* Show test-case results in --verbose mode 776 */ 777 if( verboseFlag ){ 778 printf("RESULT-CODE: %d\n", rc); 779 if( zErrMsg ){ 780 printf("ERROR-MSG: [%s]\n", zErrBuf); 781 } 782 fflush(stdout); 783 } 784 785 /* Simulate an error if the TEST_FAILURE environment variable is "5". 786 ** This is used to verify that automated test script really do spot 787 ** errors that occur in this test program. 788 */ 789 if( zFailCode ){ 790 if( zFailCode[0]=='5' && zFailCode[1]==0 ){ 791 abendError("simulated failure"); 792 }else if( zFailCode[0]!=0 ){ 793 /* If TEST_FAILURE is something other than 5, just exit the test 794 ** early */ 795 printf("\nExit early due to TEST_FAILURE being set"); 796 break; 797 } 798 } 799 } 800 if( !verboseFlag && multiTest && !quietFlag && !oomFlag ) printf("\n"); 801 } 802 803 /* Report total number of tests run 804 */ 805 if( nTest>1 && !quietFlag ){ 806 printf("%s: 0 errors out of %d tests\nSQLite %s %s\n", 807 g.zArgv0, nTest, sqlite3_libversion(), sqlite3_sourceid()); 808 } 809 810 /* Write the unique test cases if the --unique-cases flag was used 811 */ 812 if( zDataOut ){ 813 int n = 0; 814 FILE *out = fopen(zDataOut, "wb"); 815 if( out==0 ) abendError("cannot open %s for writing", zDataOut); 816 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out); 817 sqlite3_finalize(pStmt); 818 rc = sqlite3_prepare_v2(dataDb, "SELECT sql, tm FROM testcase ORDER BY tm, sql", 819 -1, &pStmt, 0); 820 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 821 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 822 fprintf(out,"/****<%d:%dms>****/", ++n, sqlite3_column_int(pStmt,1)); 823 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out); 824 } 825 fclose(out); 826 sqlite3_finalize(pStmt); 827 sqlite3_close(dataDb); 828 } 829 830 /* Clean up and exit. 831 */ 832 free(azInFile); 833 free(zIn); 834 free(pHeap); 835 free(pLook); 836 free(pScratch); 837 free(pPCache); 838 return 0; 839 } 840