1 /* 2 ** 2015-05-25 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 regressions tests on 14 ** the SQLite library using data from an external fuzzer, such as American 15 ** Fuzzy Lop (AFL) (http://lcamtuf.coredump.cx/afl/). 16 ** 17 ** This program reads content from an SQLite database file with the following 18 ** schema: 19 ** 20 ** CREATE TABLE db( 21 ** dbid INTEGER PRIMARY KEY, -- database id 22 ** dbcontent BLOB -- database disk file image 23 ** ); 24 ** CREATE TABLE xsql( 25 ** sqlid INTEGER PRIMARY KEY, -- SQL script id 26 ** sqltext TEXT -- Text of SQL statements to run 27 ** ); 28 ** CREATE TABLE IF NOT EXISTS readme( 29 ** msg TEXT -- Human-readable description of this test collection 30 ** ); 31 ** 32 ** For each database file in the DB table, the SQL text in the XSQL table 33 ** is run against that database. All README.MSG values are printed prior 34 ** to the start of the test (unless the --quiet option is used). If the 35 ** DB table is empty, then all entries in XSQL are run against an empty 36 ** in-memory database. 37 ** 38 ** This program is looking for crashes, assertion faults, and/or memory leaks. 39 ** No attempt is made to verify the output. The assumption is that either all 40 ** of the database files or all of the SQL statements are malformed inputs, 41 ** generated by a fuzzer, that need to be checked to make sure they do not 42 ** present a security risk. 43 ** 44 ** This program also includes some command-line options to help with 45 ** creation and maintenance of the source content database. The command 46 ** 47 ** ./fuzzcheck database.db --load-sql FILE... 48 ** 49 ** Loads all FILE... arguments into the XSQL table. The --load-db option 50 ** works the same but loads the files into the DB table. The -m option can 51 ** be used to initialize the README table. The "database.db" file is created 52 ** if it does not previously exist. Example: 53 ** 54 ** ./fuzzcheck new.db --load-sql *.sql 55 ** ./fuzzcheck new.db --load-db *.db 56 ** ./fuzzcheck new.db -m 'New test cases' 57 ** 58 ** The three commands above will create the "new.db" file and initialize all 59 ** tables. Then do "./fuzzcheck new.db" to run the tests. 60 ** 61 ** DEBUGGING HINTS: 62 ** 63 ** If fuzzcheck does crash, it can be run in the debugger and the content 64 ** of the global variable g.zTextName[] will identify the specific XSQL and 65 ** DB values that were running when the crash occurred. 66 */ 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <stdarg.h> 71 #include <ctype.h> 72 #include "sqlite3.h" 73 #define ISSPACE(X) isspace((unsigned char)(X)) 74 #define ISDIGIT(X) isdigit((unsigned char)(X)) 75 76 77 #ifdef __unix__ 78 # include <signal.h> 79 # include <unistd.h> 80 #endif 81 82 #ifdef SQLITE_OSS_FUZZ 83 # include <stddef.h> 84 # if !defined(_MSC_VER) 85 # include <stdint.h> 86 # endif 87 #endif 88 89 #if defined(_MSC_VER) 90 typedef unsigned char uint8_t; 91 #endif 92 93 /* 94 ** Files in the virtual file system. 95 */ 96 typedef struct VFile VFile; 97 struct VFile { 98 char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ 99 int sz; /* Size of the file in bytes */ 100 int nRef; /* Number of references to this file */ 101 unsigned char *a; /* Content of the file. From malloc() */ 102 }; 103 typedef struct VHandle VHandle; 104 struct VHandle { 105 sqlite3_file base; /* Base class. Must be first */ 106 VFile *pVFile; /* The underlying file */ 107 }; 108 109 /* 110 ** The value of a database file template, or of an SQL script 111 */ 112 typedef struct Blob Blob; 113 struct Blob { 114 Blob *pNext; /* Next in a list */ 115 int id; /* Id of this Blob */ 116 int seq; /* Sequence number */ 117 int sz; /* Size of this Blob in bytes */ 118 unsigned char a[1]; /* Blob content. Extra space allocated as needed. */ 119 }; 120 121 /* 122 ** Maximum number of files in the in-memory virtual filesystem. 123 */ 124 #define MX_FILE 10 125 126 /* 127 ** Maximum allowed file size 128 */ 129 #define MX_FILE_SZ 10000000 130 131 /* 132 ** All global variables are gathered into the "g" singleton. 133 */ 134 static struct GlobalVars { 135 const char *zArgv0; /* Name of program */ 136 VFile aFile[MX_FILE]; /* The virtual filesystem */ 137 int nDb; /* Number of template databases */ 138 Blob *pFirstDb; /* Content of first template database */ 139 int nSql; /* Number of SQL scripts */ 140 Blob *pFirstSql; /* First SQL script */ 141 unsigned int uRandom; /* Seed for the SQLite PRNG */ 142 char zTestName[100]; /* Name of current test */ 143 } g; 144 145 /* 146 ** Print an error message and quit. 147 */ 148 static void fatalError(const char *zFormat, ...){ 149 va_list ap; 150 if( g.zTestName[0] ){ 151 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); 152 }else{ 153 fprintf(stderr, "%s: ", g.zArgv0); 154 } 155 va_start(ap, zFormat); 156 vfprintf(stderr, zFormat, ap); 157 va_end(ap); 158 fprintf(stderr, "\n"); 159 exit(1); 160 } 161 162 /* 163 ** Timeout handler 164 */ 165 #ifdef __unix__ 166 static void timeoutHandler(int NotUsed){ 167 (void)NotUsed; 168 fatalError("timeout\n"); 169 } 170 #endif 171 172 /* 173 ** Set the an alarm to go off after N seconds. Disable the alarm 174 ** if N==0 175 */ 176 static void setAlarm(int N){ 177 #ifdef __unix__ 178 alarm(N); 179 #else 180 (void)N; 181 #endif 182 } 183 184 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 185 /* 186 ** This an SQL progress handler. After an SQL statement has run for 187 ** many steps, we want to interrupt it. This guards against infinite 188 ** loops from recursive common table expressions. 189 ** 190 ** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used. 191 ** In that case, hitting the progress handler is a fatal error. 192 */ 193 static int progressHandler(void *pVdbeLimitFlag){ 194 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); 195 return 1; 196 } 197 #endif 198 199 /* 200 ** Reallocate memory. Show and error and quit if unable. 201 */ 202 static void *safe_realloc(void *pOld, int szNew){ 203 void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew); 204 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); 205 return pNew; 206 } 207 208 /* 209 ** Initialize the virtual file system. 210 */ 211 static void formatVfs(void){ 212 int i; 213 for(i=0; i<MX_FILE; i++){ 214 g.aFile[i].sz = -1; 215 g.aFile[i].zFilename = 0; 216 g.aFile[i].a = 0; 217 g.aFile[i].nRef = 0; 218 } 219 } 220 221 222 /* 223 ** Erase all information in the virtual file system. 224 */ 225 static void reformatVfs(void){ 226 int i; 227 for(i=0; i<MX_FILE; i++){ 228 if( g.aFile[i].sz<0 ) continue; 229 if( g.aFile[i].zFilename ){ 230 free(g.aFile[i].zFilename); 231 g.aFile[i].zFilename = 0; 232 } 233 if( g.aFile[i].nRef>0 ){ 234 fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef); 235 } 236 g.aFile[i].sz = -1; 237 free(g.aFile[i].a); 238 g.aFile[i].a = 0; 239 g.aFile[i].nRef = 0; 240 } 241 } 242 243 /* 244 ** Find a VFile by name 245 */ 246 static VFile *findVFile(const char *zName){ 247 int i; 248 if( zName==0 ) return 0; 249 for(i=0; i<MX_FILE; i++){ 250 if( g.aFile[i].zFilename==0 ) continue; 251 if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i]; 252 } 253 return 0; 254 } 255 256 /* 257 ** Find a VFile by name. Create it if it does not already exist and 258 ** initialize it to the size and content given. 259 ** 260 ** Return NULL only if the filesystem is full. 261 */ 262 static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ 263 VFile *pNew = findVFile(zName); 264 int i; 265 if( pNew ) return pNew; 266 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} 267 if( i>=MX_FILE ) return 0; 268 pNew = &g.aFile[i]; 269 if( zName ){ 270 int nName = (int)strlen(zName)+1; 271 pNew->zFilename = safe_realloc(0, nName); 272 memcpy(pNew->zFilename, zName, nName); 273 }else{ 274 pNew->zFilename = 0; 275 } 276 pNew->nRef = 0; 277 pNew->sz = sz; 278 pNew->a = safe_realloc(0, sz); 279 if( sz>0 ) memcpy(pNew->a, pData, sz); 280 return pNew; 281 } 282 283 284 /* 285 ** Implementation of the "readfile(X)" SQL function. The entire content 286 ** of the file named X is read and returned as a BLOB. NULL is returned 287 ** if the file does not exist or is unreadable. 288 */ 289 static void readfileFunc( 290 sqlite3_context *context, 291 int argc, 292 sqlite3_value **argv 293 ){ 294 const char *zName; 295 FILE *in; 296 long nIn; 297 void *pBuf; 298 299 zName = (const char*)sqlite3_value_text(argv[0]); 300 if( zName==0 ) return; 301 in = fopen(zName, "rb"); 302 if( in==0 ) return; 303 fseek(in, 0, SEEK_END); 304 nIn = ftell(in); 305 rewind(in); 306 pBuf = sqlite3_malloc64( nIn ); 307 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ 308 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); 309 }else{ 310 sqlite3_free(pBuf); 311 } 312 fclose(in); 313 } 314 315 /* 316 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y 317 ** is written into file X. The number of bytes written is returned. Or 318 ** NULL is returned if something goes wrong, such as being unable to open 319 ** file X for writing. 320 */ 321 static void writefileFunc( 322 sqlite3_context *context, 323 int argc, 324 sqlite3_value **argv 325 ){ 326 FILE *out; 327 const char *z; 328 sqlite3_int64 rc; 329 const char *zFile; 330 331 (void)argc; 332 zFile = (const char*)sqlite3_value_text(argv[0]); 333 if( zFile==0 ) return; 334 out = fopen(zFile, "wb"); 335 if( out==0 ) return; 336 z = (const char*)sqlite3_value_blob(argv[1]); 337 if( z==0 ){ 338 rc = 0; 339 }else{ 340 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); 341 } 342 fclose(out); 343 sqlite3_result_int64(context, rc); 344 } 345 346 347 /* 348 ** Load a list of Blob objects from the database 349 */ 350 static void blobListLoadFromDb( 351 sqlite3 *db, /* Read from this database */ 352 const char *zSql, /* Query used to extract the blobs */ 353 int onlyId, /* Only load where id is this value */ 354 int *pN, /* OUT: Write number of blobs loaded here */ 355 Blob **ppList /* OUT: Write the head of the blob list here */ 356 ){ 357 Blob head; 358 Blob *p; 359 sqlite3_stmt *pStmt; 360 int n = 0; 361 int rc; 362 char *z2; 363 364 if( onlyId>0 ){ 365 z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId); 366 }else{ 367 z2 = sqlite3_mprintf("%s", zSql); 368 } 369 rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0); 370 sqlite3_free(z2); 371 if( rc ) fatalError("%s", sqlite3_errmsg(db)); 372 head.pNext = 0; 373 p = &head; 374 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 375 int sz = sqlite3_column_bytes(pStmt, 1); 376 Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz ); 377 pNew->id = sqlite3_column_int(pStmt, 0); 378 pNew->sz = sz; 379 pNew->seq = n++; 380 pNew->pNext = 0; 381 memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz); 382 pNew->a[sz] = 0; 383 p->pNext = pNew; 384 p = pNew; 385 } 386 sqlite3_finalize(pStmt); 387 *pN = n; 388 *ppList = head.pNext; 389 } 390 391 /* 392 ** Free a list of Blob objects 393 */ 394 static void blobListFree(Blob *p){ 395 Blob *pNext; 396 while( p ){ 397 pNext = p->pNext; 398 free(p); 399 p = pNext; 400 } 401 } 402 403 404 /* Return the current wall-clock time */ 405 static sqlite3_int64 timeOfDay(void){ 406 static sqlite3_vfs *clockVfs = 0; 407 sqlite3_int64 t; 408 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 409 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ 410 clockVfs->xCurrentTimeInt64(clockVfs, &t); 411 }else{ 412 double r; 413 clockVfs->xCurrentTime(clockVfs, &r); 414 t = (sqlite3_int64)(r*86400000.0); 415 } 416 return t; 417 } 418 419 /* Methods for the VHandle object 420 */ 421 static int inmemClose(sqlite3_file *pFile){ 422 VHandle *p = (VHandle*)pFile; 423 VFile *pVFile = p->pVFile; 424 pVFile->nRef--; 425 if( pVFile->nRef==0 && pVFile->zFilename==0 ){ 426 pVFile->sz = -1; 427 free(pVFile->a); 428 pVFile->a = 0; 429 } 430 return SQLITE_OK; 431 } 432 static int inmemRead( 433 sqlite3_file *pFile, /* Read from this open file */ 434 void *pData, /* Store content in this buffer */ 435 int iAmt, /* Bytes of content */ 436 sqlite3_int64 iOfst /* Start reading here */ 437 ){ 438 VHandle *pHandle = (VHandle*)pFile; 439 VFile *pVFile = pHandle->pVFile; 440 if( iOfst<0 || iOfst>=pVFile->sz ){ 441 memset(pData, 0, iAmt); 442 return SQLITE_IOERR_SHORT_READ; 443 } 444 if( iOfst+iAmt>pVFile->sz ){ 445 memset(pData, 0, iAmt); 446 iAmt = (int)(pVFile->sz - iOfst); 447 memcpy(pData, pVFile->a, iAmt); 448 return SQLITE_IOERR_SHORT_READ; 449 } 450 memcpy(pData, pVFile->a + iOfst, iAmt); 451 return SQLITE_OK; 452 } 453 static int inmemWrite( 454 sqlite3_file *pFile, /* Write to this file */ 455 const void *pData, /* Content to write */ 456 int iAmt, /* bytes to write */ 457 sqlite3_int64 iOfst /* Start writing here */ 458 ){ 459 VHandle *pHandle = (VHandle*)pFile; 460 VFile *pVFile = pHandle->pVFile; 461 if( iOfst+iAmt > pVFile->sz ){ 462 if( iOfst+iAmt >= MX_FILE_SZ ){ 463 return SQLITE_FULL; 464 } 465 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt)); 466 if( iOfst > pVFile->sz ){ 467 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz)); 468 } 469 pVFile->sz = (int)(iOfst + iAmt); 470 } 471 memcpy(pVFile->a + iOfst, pData, iAmt); 472 return SQLITE_OK; 473 } 474 static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){ 475 VHandle *pHandle = (VHandle*)pFile; 476 VFile *pVFile = pHandle->pVFile; 477 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize; 478 return SQLITE_OK; 479 } 480 static int inmemSync(sqlite3_file *pFile, int flags){ 481 return SQLITE_OK; 482 } 483 static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ 484 *pSize = ((VHandle*)pFile)->pVFile->sz; 485 return SQLITE_OK; 486 } 487 static int inmemLock(sqlite3_file *pFile, int type){ 488 return SQLITE_OK; 489 } 490 static int inmemUnlock(sqlite3_file *pFile, int type){ 491 return SQLITE_OK; 492 } 493 static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){ 494 *pOut = 0; 495 return SQLITE_OK; 496 } 497 static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){ 498 return SQLITE_NOTFOUND; 499 } 500 static int inmemSectorSize(sqlite3_file *pFile){ 501 return 512; 502 } 503 static int inmemDeviceCharacteristics(sqlite3_file *pFile){ 504 return 505 SQLITE_IOCAP_SAFE_APPEND | 506 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | 507 SQLITE_IOCAP_POWERSAFE_OVERWRITE; 508 } 509 510 511 /* Method table for VHandle 512 */ 513 static sqlite3_io_methods VHandleMethods = { 514 /* iVersion */ 1, 515 /* xClose */ inmemClose, 516 /* xRead */ inmemRead, 517 /* xWrite */ inmemWrite, 518 /* xTruncate */ inmemTruncate, 519 /* xSync */ inmemSync, 520 /* xFileSize */ inmemFileSize, 521 /* xLock */ inmemLock, 522 /* xUnlock */ inmemUnlock, 523 /* xCheck... */ inmemCheckReservedLock, 524 /* xFileCtrl */ inmemFileControl, 525 /* xSectorSz */ inmemSectorSize, 526 /* xDevchar */ inmemDeviceCharacteristics, 527 /* xShmMap */ 0, 528 /* xShmLock */ 0, 529 /* xShmBarrier */ 0, 530 /* xShmUnmap */ 0, 531 /* xFetch */ 0, 532 /* xUnfetch */ 0 533 }; 534 535 /* 536 ** Open a new file in the inmem VFS. All files are anonymous and are 537 ** delete-on-close. 538 */ 539 static int inmemOpen( 540 sqlite3_vfs *pVfs, 541 const char *zFilename, 542 sqlite3_file *pFile, 543 int openFlags, 544 int *pOutFlags 545 ){ 546 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)""); 547 VHandle *pHandle = (VHandle*)pFile; 548 if( pVFile==0 ){ 549 return SQLITE_FULL; 550 } 551 pHandle->pVFile = pVFile; 552 pVFile->nRef++; 553 pFile->pMethods = &VHandleMethods; 554 if( pOutFlags ) *pOutFlags = openFlags; 555 return SQLITE_OK; 556 } 557 558 /* 559 ** Delete a file by name 560 */ 561 static int inmemDelete( 562 sqlite3_vfs *pVfs, 563 const char *zFilename, 564 int syncdir 565 ){ 566 VFile *pVFile = findVFile(zFilename); 567 if( pVFile==0 ) return SQLITE_OK; 568 if( pVFile->nRef==0 ){ 569 free(pVFile->zFilename); 570 pVFile->zFilename = 0; 571 pVFile->sz = -1; 572 free(pVFile->a); 573 pVFile->a = 0; 574 return SQLITE_OK; 575 } 576 return SQLITE_IOERR_DELETE; 577 } 578 579 /* Check for the existance of a file 580 */ 581 static int inmemAccess( 582 sqlite3_vfs *pVfs, 583 const char *zFilename, 584 int flags, 585 int *pResOut 586 ){ 587 VFile *pVFile = findVFile(zFilename); 588 *pResOut = pVFile!=0; 589 return SQLITE_OK; 590 } 591 592 /* Get the canonical pathname for a file 593 */ 594 static int inmemFullPathname( 595 sqlite3_vfs *pVfs, 596 const char *zFilename, 597 int nOut, 598 char *zOut 599 ){ 600 sqlite3_snprintf(nOut, zOut, "%s", zFilename); 601 return SQLITE_OK; 602 } 603 604 /* Always use the same random see, for repeatability. 605 */ 606 static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ 607 memset(zBuf, 0, nBuf); 608 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom)); 609 return nBuf; 610 } 611 612 /* 613 ** Register the VFS that reads from the g.aFile[] set of files. 614 */ 615 static void inmemVfsRegister(int makeDefault){ 616 static sqlite3_vfs inmemVfs; 617 sqlite3_vfs *pDefault = sqlite3_vfs_find(0); 618 inmemVfs.iVersion = 3; 619 inmemVfs.szOsFile = sizeof(VHandle); 620 inmemVfs.mxPathname = 200; 621 inmemVfs.zName = "inmem"; 622 inmemVfs.xOpen = inmemOpen; 623 inmemVfs.xDelete = inmemDelete; 624 inmemVfs.xAccess = inmemAccess; 625 inmemVfs.xFullPathname = inmemFullPathname; 626 inmemVfs.xRandomness = inmemRandomness; 627 inmemVfs.xSleep = pDefault->xSleep; 628 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; 629 sqlite3_vfs_register(&inmemVfs, makeDefault); 630 }; 631 632 /* 633 ** Allowed values for the runFlags parameter to runSql() 634 */ 635 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ 636 #define SQL_OUTPUT 0x0002 /* Show the SQL output */ 637 638 /* 639 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not 640 ** stop if an error is encountered. 641 */ 642 static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){ 643 const char *zMore; 644 sqlite3_stmt *pStmt; 645 646 while( zSql && zSql[0] ){ 647 zMore = 0; 648 pStmt = 0; 649 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore); 650 if( zMore==zSql ) break; 651 if( runFlags & SQL_TRACE ){ 652 const char *z = zSql; 653 int n; 654 while( z<zMore && ISSPACE(z[0]) ) z++; 655 n = (int)(zMore - z); 656 while( n>0 && ISSPACE(z[n-1]) ) n--; 657 if( n==0 ) break; 658 if( pStmt==0 ){ 659 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db)); 660 }else{ 661 printf("TRACE: %.*s\n", n, z); 662 } 663 } 664 zSql = zMore; 665 if( pStmt ){ 666 if( (runFlags & SQL_OUTPUT)==0 ){ 667 while( SQLITE_ROW==sqlite3_step(pStmt) ){} 668 }else{ 669 int nCol = -1; 670 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 671 int i; 672 if( nCol<0 ){ 673 nCol = sqlite3_column_count(pStmt); 674 }else if( nCol>0 ){ 675 printf("--------------------------------------------\n"); 676 } 677 for(i=0; i<nCol; i++){ 678 int eType = sqlite3_column_type(pStmt,i); 679 printf("%s = ", sqlite3_column_name(pStmt,i)); 680 switch( eType ){ 681 case SQLITE_NULL: { 682 printf("NULL\n"); 683 break; 684 } 685 case SQLITE_INTEGER: { 686 printf("INT %s\n", sqlite3_column_text(pStmt,i)); 687 break; 688 } 689 case SQLITE_FLOAT: { 690 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i)); 691 break; 692 } 693 case SQLITE_TEXT: { 694 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i)); 695 break; 696 } 697 case SQLITE_BLOB: { 698 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i)); 699 break; 700 } 701 } 702 } 703 } 704 } 705 sqlite3_finalize(pStmt); 706 } 707 } 708 } 709 710 /* 711 ** Rebuild the database file. 712 ** 713 ** (1) Remove duplicate entries 714 ** (2) Put all entries in order 715 ** (3) Vacuum 716 */ 717 static void rebuild_database(sqlite3 *db){ 718 int rc; 719 rc = sqlite3_exec(db, 720 "BEGIN;\n" 721 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n" 722 "DELETE FROM db;\n" 723 "INSERT INTO db(dbid, dbcontent) SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n" 724 "DROP TABLE dbx;\n" 725 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql;\n" 726 "DELETE FROM xsql;\n" 727 "INSERT INTO xsql(sqlid,sqltext) SELECT NULL, sqltext FROM sx ORDER BY 2;\n" 728 "DROP TABLE sx;\n" 729 "COMMIT;\n" 730 "PRAGMA page_size=1024;\n" 731 "VACUUM;\n", 0, 0, 0); 732 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db)); 733 } 734 735 /* 736 ** Return the value of a hexadecimal digit. Return -1 if the input 737 ** is not a hex digit. 738 */ 739 static int hexDigitValue(char c){ 740 if( c>='0' && c<='9' ) return c - '0'; 741 if( c>='a' && c<='f' ) return c - 'a' + 10; 742 if( c>='A' && c<='F' ) return c - 'A' + 10; 743 return -1; 744 } 745 746 /* 747 ** Interpret zArg as an integer value, possibly with suffixes. 748 */ 749 static int integerValue(const char *zArg){ 750 sqlite3_int64 v = 0; 751 static const struct { char *zSuffix; int iMult; } aMult[] = { 752 { "KiB", 1024 }, 753 { "MiB", 1024*1024 }, 754 { "GiB", 1024*1024*1024 }, 755 { "KB", 1000 }, 756 { "MB", 1000000 }, 757 { "GB", 1000000000 }, 758 { "K", 1000 }, 759 { "M", 1000000 }, 760 { "G", 1000000000 }, 761 }; 762 int i; 763 int isNeg = 0; 764 if( zArg[0]=='-' ){ 765 isNeg = 1; 766 zArg++; 767 }else if( zArg[0]=='+' ){ 768 zArg++; 769 } 770 if( zArg[0]=='0' && zArg[1]=='x' ){ 771 int x; 772 zArg += 2; 773 while( (x = hexDigitValue(zArg[0]))>=0 ){ 774 v = (v<<4) + x; 775 zArg++; 776 } 777 }else{ 778 while( ISDIGIT(zArg[0]) ){ 779 v = v*10 + zArg[0] - '0'; 780 zArg++; 781 } 782 } 783 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ 784 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 785 v *= aMult[i].iMult; 786 break; 787 } 788 } 789 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648"); 790 return (int)(isNeg? -v : v); 791 } 792 793 /* 794 ** Print sketchy documentation for this utility program 795 */ 796 static void showHelp(void){ 797 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); 798 printf( 799 "Read databases and SQL scripts from SOURCE-DB and execute each script against\n" 800 "each database, checking for crashes and memory leaks.\n" 801 "Options:\n" 802 " --cell-size-check Set the PRAGMA cell_size_check=ON\n" 803 " --dbid N Use only the database where dbid=N\n" 804 " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" 805 " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" 806 " --help Show this help text\n" 807 " -q|--quiet Reduced output\n" 808 " --limit-mem N Limit memory used by test SQLite instance to N bytes\n" 809 " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" 810 " --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n" 811 " --load-db ARGS... Load template databases from files into SOURCE_DB\n" 812 " -m TEXT Add a description to the database\n" 813 " --native-vfs Use the native VFS for initially empty database files\n" 814 " --native-malloc Turn off MEMSYS3/5 and Lookaside\n" 815 " --oss-fuzz Enable OSS-FUZZ testing\n" 816 " --prng-seed N Seed value for the PRGN inside of SQLite\n" 817 " --rebuild Rebuild and vacuum the database file\n" 818 " --result-trace Show the results of each SQL command\n" 819 " --sqlid N Use only SQL where sqlid=N\n" 820 " --timeout N Abort if any single test needs more than N seconds\n" 821 " -v|--verbose Increased output. Repeat for more output.\n" 822 ); 823 } 824 825 int main(int argc, char **argv){ 826 sqlite3_int64 iBegin; /* Start time of this program */ 827 int quietFlag = 0; /* True if --quiet or -q */ 828 int verboseFlag = 0; /* True if --verbose or -v */ 829 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ 830 int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sql */ 831 sqlite3 *db = 0; /* The open database connection */ 832 sqlite3_stmt *pStmt; /* A prepared statement */ 833 int rc; /* Result code from SQLite interface calls */ 834 Blob *pSql; /* For looping over SQL scripts */ 835 Blob *pDb; /* For looping over template databases */ 836 int i; /* Loop index for the argv[] loop */ 837 int onlySqlid = -1; /* --sqlid */ 838 int onlyDbid = -1; /* --dbid */ 839 int nativeFlag = 0; /* --native-vfs */ 840 int rebuildFlag = 0; /* --rebuild */ 841 int vdbeLimitFlag = 0; /* --limit-vdbe */ 842 int timeoutTest = 0; /* undocumented --timeout-test flag */ 843 int runFlags = 0; /* Flags sent to runSql() */ 844 char *zMsg = 0; /* Add this message */ 845 int nSrcDb = 0; /* Number of source databases */ 846 char **azSrcDb = 0; /* Array of source database names */ 847 int iSrcDb; /* Loop over all source databases */ 848 int nTest = 0; /* Total number of tests performed */ 849 char *zDbName = ""; /* Appreviated name of a source database */ 850 const char *zFailCode = 0; /* Value of the TEST_FAILURE environment variable */ 851 int cellSzCkFlag = 0; /* --cell-size-check */ 852 int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz */ 853 int iTimeout = 120; /* Default 120-second timeout */ 854 int nMem = 0; /* Memory limit */ 855 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */ 856 char *zExpDb = 0; /* Write Databases to files in this directory */ 857 char *zExpSql = 0; /* Write SQL to files in this directory */ 858 void *pHeap = 0; /* Heap for use by SQLite */ 859 int ossFuzz = 0; /* enable OSS-FUZZ testing */ 860 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */ 861 int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */ 862 sqlite3_vfs *pDfltVfs; /* The default VFS */ 863 864 iBegin = timeOfDay(); 865 #ifdef __unix__ 866 signal(SIGALRM, timeoutHandler); 867 #endif 868 g.zArgv0 = argv[0]; 869 zFailCode = getenv("TEST_FAILURE"); 870 pDfltVfs = sqlite3_vfs_find(0); 871 inmemVfsRegister(1); 872 for(i=1; i<argc; i++){ 873 const char *z = argv[i]; 874 if( z[0]=='-' ){ 875 z++; 876 if( z[0]=='-' ) z++; 877 if( strcmp(z,"cell-size-check")==0 ){ 878 cellSzCkFlag = 1; 879 }else 880 if( strcmp(z,"dbid")==0 ){ 881 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 882 onlyDbid = integerValue(argv[++i]); 883 }else 884 if( strcmp(z,"export-db")==0 ){ 885 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 886 zExpDb = argv[++i]; 887 }else 888 if( strcmp(z,"export-sql")==0 ){ 889 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 890 zExpSql = argv[++i]; 891 }else 892 if( strcmp(z,"help")==0 ){ 893 showHelp(); 894 return 0; 895 }else 896 if( strcmp(z,"limit-mem")==0 ){ 897 #if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5) 898 fatalError("the %s option requires -DSQLITE_ENABLE_MEMSYS5 or _MEMSYS3", 899 argv[i]); 900 #else 901 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 902 nMem = integerValue(argv[++i]); 903 #endif 904 }else 905 if( strcmp(z,"limit-vdbe")==0 ){ 906 vdbeLimitFlag = 1; 907 }else 908 if( strcmp(z,"load-sql")==0 ){ 909 zInsSql = "INSERT INTO xsql(sqltext) VALUES(CAST(readfile(?1) AS text))"; 910 iFirstInsArg = i+1; 911 break; 912 }else 913 if( strcmp(z,"load-db")==0 ){ 914 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; 915 iFirstInsArg = i+1; 916 break; 917 }else 918 if( strcmp(z,"m")==0 ){ 919 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 920 zMsg = argv[++i]; 921 }else 922 if( strcmp(z,"native-malloc")==0 ){ 923 nativeMalloc = 1; 924 }else 925 if( strcmp(z,"native-vfs")==0 ){ 926 nativeFlag = 1; 927 }else 928 if( strcmp(z,"oss-fuzz")==0 ){ 929 ossFuzz = 1; 930 }else 931 if( strcmp(z,"prng-seed")==0 ){ 932 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 933 g.uRandom = atoi(argv[++i]); 934 }else 935 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 936 quietFlag = 1; 937 verboseFlag = 0; 938 }else 939 if( strcmp(z,"rebuild")==0 ){ 940 rebuildFlag = 1; 941 }else 942 if( strcmp(z,"result-trace")==0 ){ 943 runFlags |= SQL_OUTPUT; 944 }else 945 if( strcmp(z,"sqlid")==0 ){ 946 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 947 onlySqlid = integerValue(argv[++i]); 948 }else 949 if( strcmp(z,"timeout")==0 ){ 950 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 951 iTimeout = integerValue(argv[++i]); 952 }else 953 if( strcmp(z,"timeout-test")==0 ){ 954 timeoutTest = 1; 955 #ifndef __unix__ 956 fatalError("timeout is not available on non-unix systems"); 957 #endif 958 }else 959 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ 960 quietFlag = 0; 961 verboseFlag++; 962 if( verboseFlag>1 ) runFlags |= SQL_TRACE; 963 }else 964 { 965 fatalError("unknown option: %s", argv[i]); 966 } 967 }else{ 968 nSrcDb++; 969 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0])); 970 azSrcDb[nSrcDb-1] = argv[i]; 971 } 972 } 973 if( nSrcDb==0 ) fatalError("no source database specified"); 974 if( nSrcDb>1 ){ 975 if( zMsg ){ 976 fatalError("cannot change the description of more than one database"); 977 } 978 if( zInsSql ){ 979 fatalError("cannot import into more than one database"); 980 } 981 } 982 983 /* Process each source database separately */ 984 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ 985 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db, 986 SQLITE_OPEN_READWRITE, pDfltVfs->zName); 987 if( rc ){ 988 fatalError("cannot open source database %s - %s", 989 azSrcDb[iSrcDb], sqlite3_errmsg(db)); 990 } 991 rc = sqlite3_exec(db, 992 "CREATE TABLE IF NOT EXISTS db(\n" 993 " dbid INTEGER PRIMARY KEY, -- database id\n" 994 " dbcontent BLOB -- database disk file image\n" 995 ");\n" 996 "CREATE TABLE IF NOT EXISTS xsql(\n" 997 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" 998 " sqltext TEXT -- Text of SQL statements to run\n" 999 ");" 1000 "CREATE TABLE IF NOT EXISTS readme(\n" 1001 " msg TEXT -- Human-readable description of this file\n" 1002 ");", 0, 0, 0); 1003 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); 1004 if( zMsg ){ 1005 char *zSql; 1006 zSql = sqlite3_mprintf( 1007 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg); 1008 rc = sqlite3_exec(db, zSql, 0, 0, 0); 1009 sqlite3_free(zSql); 1010 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); 1011 } 1012 ossFuzzThisDb = ossFuzz; 1013 1014 /* If the CONFIG(name,value) table exists, read db-specific settings 1015 ** from that table */ 1016 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){ 1017 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config", -1, &pStmt, 0); 1018 if( rc ) fatalError("cannot prepare query of CONFIG table: %s", 1019 sqlite3_errmsg(db)); 1020 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 1021 const char *zName = (const char *)sqlite3_column_text(pStmt,0); 1022 if( zName==0 ) continue; 1023 if( strcmp(zName, "oss-fuzz")==0 ){ 1024 ossFuzzThisDb = sqlite3_column_int(pStmt,1); 1025 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb); 1026 } 1027 if( strcmp(zName, "limit-mem")==0 && !nativeMalloc ){ 1028 #if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5) 1029 fatalError("the limit-mem option requires -DSQLITE_ENABLE_MEMSYS5" 1030 " or _MEMSYS3"); 1031 #else 1032 nMemThisDb = sqlite3_column_int(pStmt,1); 1033 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb); 1034 #endif 1035 } 1036 } 1037 sqlite3_finalize(pStmt); 1038 } 1039 1040 if( zInsSql ){ 1041 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, 1042 readfileFunc, 0, 0); 1043 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); 1044 if( rc ) fatalError("cannot prepare statement [%s]: %s", 1045 zInsSql, sqlite3_errmsg(db)); 1046 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); 1047 if( rc ) fatalError("cannot start a transaction"); 1048 for(i=iFirstInsArg; i<argc; i++){ 1049 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); 1050 sqlite3_step(pStmt); 1051 rc = sqlite3_reset(pStmt); 1052 if( rc ) fatalError("insert failed for %s", argv[i]); 1053 } 1054 sqlite3_finalize(pStmt); 1055 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0); 1056 if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db)); 1057 rebuild_database(db); 1058 sqlite3_close(db); 1059 return 0; 1060 } 1061 rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0); 1062 if( rc ) fatalError("cannot set database to query-only"); 1063 if( zExpDb!=0 || zExpSql!=0 ){ 1064 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, 1065 writefileFunc, 0, 0); 1066 if( zExpDb!=0 ){ 1067 const char *zExDb = 1068 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent)," 1069 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)" 1070 " FROM db WHERE ?2<0 OR dbid=?2;"; 1071 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0); 1072 if( rc ) fatalError("cannot prepare statement [%s]: %s", 1073 zExDb, sqlite3_errmsg(db)); 1074 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb), 1075 SQLITE_STATIC, SQLITE_UTF8); 1076 sqlite3_bind_int(pStmt, 2, onlyDbid); 1077 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 1078 printf("write db-%d (%d bytes) into %s\n", 1079 sqlite3_column_int(pStmt,1), 1080 sqlite3_column_int(pStmt,3), 1081 sqlite3_column_text(pStmt,2)); 1082 } 1083 sqlite3_finalize(pStmt); 1084 } 1085 if( zExpSql!=0 ){ 1086 const char *zExSql = 1087 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext)," 1088 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)" 1089 " FROM xsql WHERE ?2<0 OR sqlid=?2;"; 1090 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0); 1091 if( rc ) fatalError("cannot prepare statement [%s]: %s", 1092 zExSql, sqlite3_errmsg(db)); 1093 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql), 1094 SQLITE_STATIC, SQLITE_UTF8); 1095 sqlite3_bind_int(pStmt, 2, onlySqlid); 1096 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 1097 printf("write sql-%d (%d bytes) into %s\n", 1098 sqlite3_column_int(pStmt,1), 1099 sqlite3_column_int(pStmt,3), 1100 sqlite3_column_text(pStmt,2)); 1101 } 1102 sqlite3_finalize(pStmt); 1103 } 1104 sqlite3_close(db); 1105 return 0; 1106 } 1107 1108 /* Load all SQL script content and all initial database images from the 1109 ** source db 1110 */ 1111 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid, 1112 &g.nSql, &g.pFirstSql); 1113 if( g.nSql==0 ) fatalError("need at least one SQL script"); 1114 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid, 1115 &g.nDb, &g.pFirstDb); 1116 if( g.nDb==0 ){ 1117 g.pFirstDb = safe_realloc(0, sizeof(Blob)); 1118 memset(g.pFirstDb, 0, sizeof(Blob)); 1119 g.pFirstDb->id = 1; 1120 g.pFirstDb->seq = 0; 1121 g.nDb = 1; 1122 sqlFuzz = 1; 1123 } 1124 1125 /* Print the description, if there is one */ 1126 if( !quietFlag ){ 1127 zDbName = azSrcDb[iSrcDb]; 1128 i = (int)strlen(zDbName) - 1; 1129 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } 1130 zDbName += i; 1131 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); 1132 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 1133 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); 1134 } 1135 sqlite3_finalize(pStmt); 1136 } 1137 1138 /* Rebuild the database, if requested */ 1139 if( rebuildFlag ){ 1140 if( !quietFlag ){ 1141 printf("%s: rebuilding... ", zDbName); 1142 fflush(stdout); 1143 } 1144 rebuild_database(db); 1145 if( !quietFlag ) printf("done\n"); 1146 } 1147 1148 /* Close the source database. Verify that no SQLite memory allocations are 1149 ** outstanding. 1150 */ 1151 sqlite3_close(db); 1152 if( sqlite3_memory_used()>0 ){ 1153 fatalError("SQLite has memory in use before the start of testing"); 1154 } 1155 1156 /* Limit available memory, if requested */ 1157 sqlite3_shutdown(); 1158 if( nMemThisDb>0 && !nativeMalloc ){ 1159 pHeap = realloc(pHeap, nMemThisDb); 1160 if( pHeap==0 ){ 1161 fatalError("failed to allocate %d bytes of heap memory", nMem); 1162 } 1163 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128); 1164 } 1165 1166 /* Disable lookaside with the --native-malloc option */ 1167 if( nativeMalloc ){ 1168 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); 1169 } 1170 1171 /* Reset the in-memory virtual filesystem */ 1172 formatVfs(); 1173 1174 /* Run a test using each SQL script against each database. 1175 */ 1176 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName); 1177 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ 1178 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ 1179 int openFlags; 1180 const char *zVfs = "inmem"; 1181 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", 1182 pSql->id, pDb->id); 1183 if( verboseFlag ){ 1184 printf("%s\n", g.zTestName); 1185 fflush(stdout); 1186 }else if( !quietFlag ){ 1187 static int prevAmt = -1; 1188 int idx = pSql->seq*g.nDb + pDb->id - 1; 1189 int amt = idx*10/(g.nDb*g.nSql); 1190 if( amt!=prevAmt ){ 1191 printf(" %d%%", amt*10); 1192 fflush(stdout); 1193 prevAmt = amt; 1194 } 1195 } 1196 createVFile("main.db", pDb->sz, pDb->a); 1197 sqlite3_randomness(0,0); 1198 if( ossFuzzThisDb ){ 1199 #ifndef SQLITE_OSS_FUZZ 1200 fatalError("--oss-fuzz not supported: recompile with -DSQLITE_OSS_FUZZ"); 1201 #else 1202 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t); 1203 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz); 1204 #endif 1205 }else{ 1206 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; 1207 if( nativeFlag && pDb->sz==0 ){ 1208 openFlags |= SQLITE_OPEN_MEMORY; 1209 zVfs = 0; 1210 } 1211 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); 1212 if( rc ) fatalError("cannot open inmem database"); 1213 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000); 1214 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50); 1215 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); 1216 setAlarm(iTimeout); 1217 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1218 if( sqlFuzz || vdbeLimitFlag ){ 1219 sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag); 1220 } 1221 #endif 1222 do{ 1223 runSql(db, (char*)pSql->a, runFlags); 1224 }while( timeoutTest ); 1225 setAlarm(0); 1226 sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0); 1227 sqlite3_close(db); 1228 } 1229 if( sqlite3_memory_used()>0 ){ 1230 fatalError("memory leak: %lld bytes outstanding", 1231 sqlite3_memory_used()); 1232 } 1233 reformatVfs(); 1234 nTest++; 1235 g.zTestName[0] = 0; 1236 1237 /* Simulate an error if the TEST_FAILURE environment variable is "5". 1238 ** This is used to verify that automated test script really do spot 1239 ** errors that occur in this test program. 1240 */ 1241 if( zFailCode ){ 1242 if( zFailCode[0]=='5' && zFailCode[1]==0 ){ 1243 fatalError("simulated failure"); 1244 }else if( zFailCode[0]!=0 ){ 1245 /* If TEST_FAILURE is something other than 5, just exit the test 1246 ** early */ 1247 printf("\nExit early due to TEST_FAILURE being set\n"); 1248 iSrcDb = nSrcDb-1; 1249 goto sourcedb_cleanup; 1250 } 1251 } 1252 } 1253 } 1254 if( !quietFlag && !verboseFlag ){ 1255 printf(" 100%% - %d tests\n", g.nDb*g.nSql); 1256 } 1257 1258 /* Clean up at the end of processing a single source database 1259 */ 1260 sourcedb_cleanup: 1261 blobListFree(g.pFirstSql); 1262 blobListFree(g.pFirstDb); 1263 reformatVfs(); 1264 1265 } /* End loop over all source databases */ 1266 1267 if( !quietFlag ){ 1268 sqlite3_int64 iElapse = timeOfDay() - iBegin; 1269 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" 1270 "SQLite %s %s\n", 1271 nTest, (int)(iElapse/1000), (int)(iElapse%1000), 1272 sqlite3_libversion(), sqlite3_sourceid()); 1273 } 1274 free(azSrcDb); 1275 free(pHeap); 1276 return 0; 1277 } 1278