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 ** 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.) 42 ** 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 44 ** 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 49 ** can be run in a single go (for regression testing, for example) by typing: 50 ** 51 ** fuzzershell -f ~/all-queue.txt 52 ** 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. 56 ** 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 ** 62 */ 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <stdarg.h> 67 #include <ctype.h> 68 #include "sqlite3.h" 69 70 /* 71 ** All global variables are gathered into the "g" singleton. 72 */ 73 struct 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 */ 83 static 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 */ 96 static 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 /* 107 ** Evaluate some SQL. Abort if unable. 108 */ 109 static 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 /* 123 ** This callback is invoked by sqlite3_log(). 124 */ 125 static 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 */ 132 static 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 } 146 static int execNoop(void *NotUsed, int argc, char **argv, char **colv){ 147 return 0; 148 } 149 150 #ifndef SQLITE_OMIT_TRACE 151 /* 152 ** This callback is invoked by sqlite3_trace() as each SQL statement 153 ** starts. 154 */ 155 static void traceCallback(void *NotUsed, const char *zMsg){ 156 printf("TRACE: %s\n", zMsg); 157 } 158 #endif 159 160 /*************************************************************************** 161 ** eval() implementation copied from ../ext/misc/eval.c 162 */ 163 /* 164 ** Structure used to accumulate the output 165 */ 166 struct EvalResult { 167 char *z; /* Accumulated output */ 168 const char *zSep; /* Separator */ 169 int szSep; /* Size of the separator string */ 170 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */ 171 sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */ 172 }; 173 174 /* 175 ** Callback from sqlite_exec() for the eval() function. 176 */ 177 static int callback(void *pCtx, int argc, char **argv, char **colnames){ 178 struct EvalResult *p = (struct EvalResult*)pCtx; 179 int i; 180 for(i=0; i<argc; i++){ 181 const char *z = argv[i] ? argv[i] : ""; 182 size_t sz = strlen(z); 183 if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){ 184 char *zNew; 185 p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1; 186 /* Using sqlite3_realloc64() would be better, but it is a recent 187 ** addition and will cause a segfault if loaded by an older version 188 ** of SQLite. */ 189 zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0; 190 if( zNew==0 ){ 191 sqlite3_free(p->z); 192 memset(p, 0, sizeof(*p)); 193 return 1; 194 } 195 p->z = zNew; 196 } 197 if( p->nUsed>0 ){ 198 memcpy(&p->z[p->nUsed], p->zSep, p->szSep); 199 p->nUsed += p->szSep; 200 } 201 memcpy(&p->z[p->nUsed], z, sz); 202 p->nUsed += sz; 203 } 204 return 0; 205 } 206 207 /* 208 ** Implementation of the eval(X) and eval(X,Y) SQL functions. 209 ** 210 ** Evaluate the SQL text in X. Return the results, using string 211 ** Y as the separator. If Y is omitted, use a single space character. 212 */ 213 static void sqlEvalFunc( 214 sqlite3_context *context, 215 int argc, 216 sqlite3_value **argv 217 ){ 218 const char *zSql; 219 sqlite3 *db; 220 char *zErr = 0; 221 int rc; 222 struct EvalResult x; 223 224 memset(&x, 0, sizeof(x)); 225 x.zSep = " "; 226 zSql = (const char*)sqlite3_value_text(argv[0]); 227 if( zSql==0 ) return; 228 if( argc>1 ){ 229 x.zSep = (const char*)sqlite3_value_text(argv[1]); 230 if( x.zSep==0 ) return; 231 } 232 x.szSep = (int)strlen(x.zSep); 233 db = sqlite3_context_db_handle(context); 234 rc = sqlite3_exec(db, zSql, callback, &x, &zErr); 235 if( rc!=SQLITE_OK ){ 236 sqlite3_result_error(context, zErr, -1); 237 sqlite3_free(zErr); 238 }else if( x.zSep==0 ){ 239 sqlite3_result_error_nomem(context); 240 sqlite3_free(x.z); 241 }else{ 242 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free); 243 } 244 } 245 /* End of the eval() implementation 246 ******************************************************************************/ 247 248 /* 249 ** Print sketchy documentation for this utility program 250 */ 251 static void showHelp(void){ 252 printf("Usage: %s [options]\n", g.zArgv0); 253 printf( 254 "Read SQL text from standard input and evaluate it.\n" 255 "Options:\n" 256 " --autovacuum Enable AUTOVACUUM mode\n" 257 " -f FILE Read SQL text from FILE instead of standard input\n" 258 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" 259 " --help Show this help text\n" 260 " --initdb DBFILE Initialize the in-memory database using template DBFILE\n" 261 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" 262 " --pagesize N Set the page size to N\n" 263 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" 264 " -q Reduced output\n" 265 " --quiet Reduced output\n" 266 " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" 267 " --unique-cases FILE Write all unique test cases to FILE\n" 268 " --utf16be Set text encoding to UTF-16BE\n" 269 " --utf16le Set text encoding to UTF-16LE\n" 270 " -v Increased output\n" 271 " --verbose Increased output\n" 272 ); 273 } 274 275 /* 276 ** Return the value of a hexadecimal digit. Return -1 if the input 277 ** is not a hex digit. 278 */ 279 static int hexDigitValue(char c){ 280 if( c>='0' && c<='9' ) return c - '0'; 281 if( c>='a' && c<='f' ) return c - 'a' + 10; 282 if( c>='A' && c<='F' ) return c - 'A' + 10; 283 return -1; 284 } 285 286 /* 287 ** Interpret zArg as an integer value, possibly with suffixes. 288 */ 289 static int integerValue(const char *zArg){ 290 sqlite3_int64 v = 0; 291 static const struct { char *zSuffix; int iMult; } aMult[] = { 292 { "KiB", 1024 }, 293 { "MiB", 1024*1024 }, 294 { "GiB", 1024*1024*1024 }, 295 { "KB", 1000 }, 296 { "MB", 1000000 }, 297 { "GB", 1000000000 }, 298 { "K", 1000 }, 299 { "M", 1000000 }, 300 { "G", 1000000000 }, 301 }; 302 int i; 303 int isNeg = 0; 304 if( zArg[0]=='-' ){ 305 isNeg = 1; 306 zArg++; 307 }else if( zArg[0]=='+' ){ 308 zArg++; 309 } 310 if( zArg[0]=='0' && zArg[1]=='x' ){ 311 int x; 312 zArg += 2; 313 while( (x = hexDigitValue(zArg[0]))>=0 ){ 314 v = (v<<4) + x; 315 zArg++; 316 } 317 }else{ 318 while( isdigit(zArg[0]) ){ 319 v = v*10 + zArg[0] - '0'; 320 zArg++; 321 } 322 } 323 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ 324 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 325 v *= aMult[i].iMult; 326 break; 327 } 328 } 329 if( v>0x7fffffff ) abendError("parameter too large - max 2147483648"); 330 return (int)(isNeg? -v : v); 331 } 332 333 /* 334 ** Various operating modes 335 */ 336 #define FZMODE_Generic 1 337 #define FZMODE_Strftime 2 338 #define FZMODE_Printf 3 339 #define FZMODE_Glob 4 340 341 342 int main(int argc, char **argv){ 343 char *zIn = 0; /* Input text */ 344 int nAlloc = 0; /* Number of bytes allocated for zIn[] */ 345 int nIn = 0; /* Number of bytes of zIn[] used */ 346 size_t got; /* Bytes read from input */ 347 FILE *in = stdin; /* Where to read SQL text from */ 348 int rc = SQLITE_OK; /* Result codes from API functions */ 349 int i; /* Loop counter */ 350 int iNext; /* Next block of SQL */ 351 sqlite3 *db; /* Open database */ 352 sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */ 353 const char *zInitDb = 0;/* Name of the initialization database file */ 354 char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */ 355 const char *zEncoding = 0; /* --utf16be or --utf16le */ 356 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ 357 int nLook = 0, szLook = 0; /* --lookaside configuration */ 358 int nPCache = 0, szPCache = 0;/* --pcache configuration */ 359 int nScratch = 0, szScratch=0;/* --scratch configuration */ 360 int pageSize = 0; /* Desired page size. 0 means default */ 361 void *pHeap = 0; /* Allocated heap space */ 362 void *pLook = 0; /* Allocated lookaside space */ 363 void *pPCache = 0; /* Allocated storage for pcache */ 364 void *pScratch = 0; /* Allocated storage for scratch */ 365 int doAutovac = 0; /* True for --autovacuum */ 366 char *zSql; /* SQL to run */ 367 char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */ 368 int iMode = FZMODE_Generic; /* Operating mode */ 369 const char *zCkGlob = 0; /* Inputs must match this glob */ 370 int verboseFlag = 0; /* --verbose or -v flag */ 371 int quietFlag = 0; /* --quiet or -q flag */ 372 int nTest = 0; /* Number of test cases run */ 373 int multiTest = 0; /* True if there will be multiple test cases */ 374 int lastPct = -1; /* Previous percentage done output */ 375 sqlite3 *dataDb = 0; /* Database holding compacted input data */ 376 sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */ 377 const char *zDataOut = 0; /* Write compacted data to this output file */ 378 int nHeader = 0; /* Bytes of header comment text on input file */ 379 380 381 g.zArgv0 = argv[0]; 382 for(i=1; i<argc; i++){ 383 const char *z = argv[i]; 384 if( z[0]=='-' ){ 385 z++; 386 if( z[0]=='-' ) z++; 387 if( strcmp(z,"autovacuum")==0 ){ 388 doAutovac = 1; 389 }else 390 if( strcmp(z, "f")==0 && i+1<argc ){ 391 if( in!=stdin ) abendError("only one -f allowed"); 392 in = fopen(argv[++i],"rb"); 393 if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]); 394 }else 395 if( strcmp(z,"heap")==0 ){ 396 if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]); 397 nHeap = integerValue(argv[i+1]); 398 mnHeap = integerValue(argv[i+2]); 399 i += 2; 400 }else 401 if( strcmp(z,"help")==0 ){ 402 showHelp(); 403 return 0; 404 }else 405 if( strcmp(z, "initdb")==0 && i+1<argc ){ 406 if( zInitDb!=0 ) abendError("only one --initdb allowed"); 407 zInitDb = argv[++i]; 408 }else 409 if( strcmp(z,"lookaside")==0 ){ 410 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 411 nLook = integerValue(argv[i+1]); 412 szLook = integerValue(argv[i+2]); 413 i += 2; 414 }else 415 if( strcmp(z,"mode")==0 ){ 416 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]); 417 z = argv[++i]; 418 if( strcmp(z,"generic")==0 ){ 419 iMode = FZMODE_Printf; 420 zCkGlob = 0; 421 }else if( strcmp(z, "glob")==0 ){ 422 iMode = FZMODE_Glob; 423 zCkGlob = "'*','*'"; 424 }else if( strcmp(z, "printf")==0 ){ 425 iMode = FZMODE_Printf; 426 zCkGlob = "'*',*"; 427 }else if( strcmp(z, "strftime")==0 ){ 428 iMode = FZMODE_Strftime; 429 zCkGlob = "'*',*"; 430 }else{ 431 abendError("unknown --mode: %s", z); 432 } 433 }else 434 if( strcmp(z,"pagesize")==0 ){ 435 if( i>=argc-1 ) abendError("missing argument on %s", argv[i]); 436 pageSize = integerValue(argv[++i]); 437 }else 438 if( strcmp(z,"pcache")==0 ){ 439 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 440 nPCache = integerValue(argv[i+1]); 441 szPCache = integerValue(argv[i+2]); 442 i += 2; 443 }else 444 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 445 quietFlag = 1; 446 verboseFlag = 0; 447 }else 448 if( strcmp(z,"scratch")==0 ){ 449 if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); 450 nScratch = integerValue(argv[i+1]); 451 szScratch = integerValue(argv[i+2]); 452 i += 2; 453 }else 454 if( strcmp(z, "unique-cases")==0 ){ 455 if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]); 456 if( zDataOut ) abendError("only one --minimize allowed"); 457 zDataOut = argv[++i]; 458 }else 459 if( strcmp(z,"utf16le")==0 ){ 460 zEncoding = "utf16le"; 461 }else 462 if( strcmp(z,"utf16be")==0 ){ 463 zEncoding = "utf16be"; 464 }else 465 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ 466 quietFlag = 0; 467 verboseFlag = 1; 468 }else 469 { 470 abendError("unknown option: %s", argv[i]); 471 } 472 }else{ 473 abendError("unknown argument: %s", argv[i]); 474 } 475 } 476 if( verboseFlag ) sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0); 477 if( nHeap>0 ){ 478 pHeap = malloc( nHeap ); 479 if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap); 480 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); 481 if( rc ) abendError("heap configuration failed: %d\n", rc); 482 } 483 if( nLook>0 ){ 484 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); 485 if( szLook>0 ){ 486 pLook = malloc( nLook*szLook ); 487 if( pLook==0 ) fatalError("out of memory"); 488 } 489 } 490 if( nScratch>0 && szScratch>0 ){ 491 pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); 492 if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch", 493 nScratch*(sqlite3_int64)szScratch); 494 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); 495 if( rc ) abendError("scratch configuration failed: %d\n", rc); 496 } 497 if( nPCache>0 && szPCache>0 ){ 498 pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); 499 if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache", 500 nPCache*(sqlite3_int64)szPCache); 501 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); 502 if( rc ) abendError("pcache configuration failed: %d", rc); 503 } 504 while( !feof(in) ){ 505 nAlloc += nAlloc+1000; 506 zIn = realloc(zIn, nAlloc); 507 if( zIn==0 ) fatalError("out of memory"); 508 got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); 509 nIn += (int)got; 510 zIn[nIn] = 0; 511 if( got==0 ) break; 512 } 513 if( in!=stdin ) fclose(in); 514 if( zDataOut ){ 515 rc = sqlite3_open(":memory:", &dataDb); 516 if( rc ) abendError("cannot open :memory: database"); 517 rc = sqlite3_exec(dataDb, 518 "CREATE TABLE testcase(sql BLOB PRIMARY KEY) WITHOUT ROWID;",0,0,0); 519 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 520 rc = sqlite3_prepare_v2(dataDb, "INSERT OR IGNORE INTO testcase(sql)VALUES(?1)", 521 -1, &pStmt, 0); 522 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 523 } 524 if( zInitDb ){ 525 rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0); 526 if( rc!=SQLITE_OK ){ 527 abendError("unable to open initialization database \"%s\"", zInitDb); 528 } 529 } 530 for(i=0; i<nIn; i=iNext+1){ /* Skip initial lines beginning with '#' */ 531 if( zIn[i]!='#' ) break; 532 for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){} 533 } 534 nHeader = i; 535 for(nTest=0; i<nIn; i=iNext, nTest++){ 536 char cSaved; 537 if( strncmp(&zIn[i], "/****<",6)==0 ){ 538 char *z = strstr(&zIn[i], ">****/"); 539 if( z ){ 540 z += 6; 541 if( verboseFlag ) printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]); 542 i += (int)(z-&zIn[i]); 543 multiTest = 1; 544 } 545 } 546 for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){} 547 if( zDataOut ){ 548 sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC); 549 rc = sqlite3_step(pStmt); 550 if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb)); 551 sqlite3_reset(pStmt); 552 continue; 553 } 554 cSaved = zIn[iNext]; 555 zIn[iNext] = 0; 556 if( zCkGlob && sqlite3_strglob(zCkGlob,&zIn[i])!=0 ){ 557 zIn[iNext] = cSaved; 558 continue; 559 } 560 rc = sqlite3_open_v2( 561 "main.db", &db, 562 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 563 0); 564 if( rc!=SQLITE_OK ){ 565 abendError("Unable to open the in-memory database"); 566 } 567 if( pLook ){ 568 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook); 569 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc); 570 } 571 if( zInitDb ){ 572 sqlite3_backup *pBackup; 573 pBackup = sqlite3_backup_init(db, "main", dbInit, "main"); 574 rc = sqlite3_backup_step(pBackup, -1); 575 if( rc!=SQLITE_DONE ){ 576 abendError("attempt to initialize the in-memory database failed (rc=%d)", 577 rc); 578 } 579 sqlite3_backup_finish(pBackup); 580 } 581 #ifndef SQLITE_OMIT_TRACE 582 if( verboseFlag ) sqlite3_trace(db, traceCallback, 0); 583 #endif 584 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); 585 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); 586 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); 587 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); 588 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); 589 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); 590 zSql = &zIn[i]; 591 if( verboseFlag ){ 592 printf("INPUT (offset: %d, size: %d): [%s]\n", 593 i, (int)strlen(&zIn[i]), &zIn[i]); 594 }else if( multiTest && !quietFlag ){ 595 int pct = 10*iNext/nIn; 596 if( pct!=lastPct ){ 597 if( lastPct<0 ) printf("fuzz test:"); 598 printf(" %d%%", pct*10); 599 fflush(stdout); 600 lastPct = pct; 601 } 602 } 603 switch( iMode ){ 604 case FZMODE_Glob: 605 zSql = zToFree = sqlite3_mprintf("SELECT glob(%s);", zSql); 606 break; 607 case FZMODE_Printf: 608 zSql = zToFree = sqlite3_mprintf("SELECT printf(%s);", zSql); 609 break; 610 case FZMODE_Strftime: 611 zSql = zToFree = sqlite3_mprintf("SELECT strftime(%s);", zSql); 612 break; 613 } 614 zErrMsg = 0; 615 rc = sqlite3_exec(db, zSql, verboseFlag ? execCallback : execNoop, 0, &zErrMsg); 616 if( zToFree ){ 617 sqlite3_free(zToFree); 618 zToFree = 0; 619 } 620 zIn[iNext] = cSaved; 621 if( verboseFlag ){ 622 printf("RESULT-CODE: %d\n", rc); 623 if( zErrMsg ){ 624 printf("ERROR-MSG: [%s]\n", zErrMsg); 625 } 626 } 627 sqlite3_free(zErrMsg); 628 rc = sqlite3_close(db); 629 if( rc ){ 630 abendError("sqlite3_close() failed with rc=%d", rc); 631 } 632 if( sqlite3_memory_used()>0 ){ 633 abendError("memory in use after close: %lld bytes", sqlite3_memory_used()); 634 } 635 if( nTest==1 ){ 636 /* Simulate an error if the TEST_FAILURE environment variable is "5" */ 637 char *zFailCode = getenv("TEST_FAILURE"); 638 if( zFailCode ){ 639 if( zFailCode[0]=='5' && zFailCode[1]==0 ){ 640 abendError("simulated failure"); 641 }else if( zFailCode[0]!=0 ){ 642 /* If TEST_FAILURE is something other than 5, just exit the test 643 ** early */ 644 printf("\nExit early due to TEST_FAILURE being set"); 645 break; 646 } 647 } 648 } 649 } 650 if( !verboseFlag && multiTest && !quietFlag ) printf("\n"); 651 if( nTest>1 && !quietFlag ){ 652 printf("%d fuzz tests with no errors\nSQLite %s %s\n", 653 nTest, sqlite3_libversion(), sqlite3_sourceid()); 654 } 655 if( zDataOut ){ 656 int n = 0; 657 FILE *out = fopen(zDataOut, "wb"); 658 if( out==0 ) abendError("cannot open %s for writing", zDataOut); 659 if( nHeader>0 ) fwrite(zIn, nHeader, 1, out); 660 sqlite3_finalize(pStmt); 661 rc = sqlite3_prepare_v2(dataDb, "SELECT sql FROM testcase", -1, &pStmt, 0); 662 if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); 663 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 664 fprintf(out,"/****<%d>****/", ++n); 665 fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out); 666 } 667 fclose(out); 668 sqlite3_finalize(pStmt); 669 sqlite3_close(dataDb); 670 } 671 free(zIn); 672 free(pHeap); 673 free(pLook); 674 free(pScratch); 675 free(pPCache); 676 return 0; 677 } 678