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 external fuzzers. 15 ** 16 ** This program reads content from an SQLite database file with the following 17 ** schema: 18 ** 19 ** CREATE TABLE db( 20 ** dbid INTEGER PRIMARY KEY, -- database id 21 ** dbcontent BLOB -- database disk file image 22 ** ); 23 ** CREATE TABLE xsql( 24 ** sqlid INTEGER PRIMARY KEY, -- SQL script id 25 ** sqltext TEXT -- Text of SQL statements to run 26 ** ); 27 ** CREATE TABLE IF NOT EXISTS readme( 28 ** msg TEXT -- Human-readable description of this test collection 29 ** ); 30 ** 31 ** For each database file in the DB table, the SQL text in the XSQL table 32 ** is run against that database. All README.MSG values are printed prior 33 ** to the start of the test (unless the --quiet option is used). If the 34 ** DB table is empty, then all entries in XSQL are run against an empty 35 ** in-memory database. 36 ** 37 ** This program is looking for crashes, assertion faults, and/or memory leaks. 38 ** No attempt is made to verify the output. The assumption is that either all 39 ** of the database files or all of the SQL statements are malformed inputs, 40 ** generated by a fuzzer, that need to be checked to make sure they do not 41 ** present a security risk. 42 ** 43 ** This program also includes some command-line options to help with 44 ** creation and maintenance of the source content database. The command 45 ** 46 ** ./fuzzcheck database.db --load-sql FILE... 47 ** 48 ** Loads all FILE... arguments into the XSQL table. The --load-db option 49 ** works the same but loads the files into the DB table. The -m option can 50 ** be used to initialize the README table. The "database.db" file is created 51 ** if it does not previously exist. Example: 52 ** 53 ** ./fuzzcheck new.db --load-sql *.sql 54 ** ./fuzzcheck new.db --load-db *.db 55 ** ./fuzzcheck new.db -m 'New test cases' 56 ** 57 ** The three commands above will create the "new.db" file and initialize all 58 ** tables. Then do "./fuzzcheck new.db" to run the tests. 59 ** 60 ** DEBUGGING HINTS: 61 ** 62 ** If fuzzcheck does crash, it can be run in the debugger and the content 63 ** of the global variable g.zTextName[] will identify the specific XSQL and 64 ** DB values that were running when the crash occurred. 65 ** 66 ** DBSQLFUZZ: (Added 2020-02-25) 67 ** 68 ** The dbsqlfuzz fuzzer includes both a database file and SQL to run against 69 ** that database in its input. This utility can now process dbsqlfuzz 70 ** input files. Load such files using the "--load-dbsql FILE ..." command-line 71 ** option. 72 ** 73 ** Dbsqlfuzz inputs are ordinary text. The first part of the file is text 74 ** that describes the content of the database (using a lot of hexadecimal), 75 ** then there is a divider line followed by the SQL to run against the 76 ** database. Because they are ordinary text, dbsqlfuzz inputs are stored 77 ** in the XSQL table, as if they were ordinary SQL inputs. The isDbSql() 78 ** function can look at a text string and determine whether or not it is 79 ** a valid dbsqlfuzz input. 80 */ 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <string.h> 84 #include <stdarg.h> 85 #include <ctype.h> 86 #include <assert.h> 87 #include "sqlite3.h" 88 #define ISSPACE(X) isspace((unsigned char)(X)) 89 #define ISDIGIT(X) isdigit((unsigned char)(X)) 90 91 92 #ifdef __unix__ 93 # include <signal.h> 94 # include <unistd.h> 95 #endif 96 97 #include <stddef.h> 98 #if !defined(_MSC_VER) 99 # include <stdint.h> 100 #endif 101 102 #if defined(_MSC_VER) 103 typedef unsigned char uint8_t; 104 #endif 105 106 /* 107 ** Files in the virtual file system. 108 */ 109 typedef struct VFile VFile; 110 struct VFile { 111 char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ 112 int sz; /* Size of the file in bytes */ 113 int nRef; /* Number of references to this file */ 114 unsigned char *a; /* Content of the file. From malloc() */ 115 }; 116 typedef struct VHandle VHandle; 117 struct VHandle { 118 sqlite3_file base; /* Base class. Must be first */ 119 VFile *pVFile; /* The underlying file */ 120 }; 121 122 /* 123 ** The value of a database file template, or of an SQL script 124 */ 125 typedef struct Blob Blob; 126 struct Blob { 127 Blob *pNext; /* Next in a list */ 128 int id; /* Id of this Blob */ 129 int seq; /* Sequence number */ 130 int sz; /* Size of this Blob in bytes */ 131 unsigned char a[1]; /* Blob content. Extra space allocated as needed. */ 132 }; 133 134 /* 135 ** Maximum number of files in the in-memory virtual filesystem. 136 */ 137 #define MX_FILE 10 138 139 /* 140 ** Maximum allowed file size 141 */ 142 #define MX_FILE_SZ 10000000 143 144 /* 145 ** All global variables are gathered into the "g" singleton. 146 */ 147 static struct GlobalVars { 148 const char *zArgv0; /* Name of program */ 149 const char *zDbFile; /* Name of database file */ 150 VFile aFile[MX_FILE]; /* The virtual filesystem */ 151 int nDb; /* Number of template databases */ 152 Blob *pFirstDb; /* Content of first template database */ 153 int nSql; /* Number of SQL scripts */ 154 Blob *pFirstSql; /* First SQL script */ 155 unsigned int uRandom; /* Seed for the SQLite PRNG */ 156 unsigned char doInvariantChecks; /* True to run query invariant checks */ 157 unsigned int nInvariant; /* Number of invariant checks run */ 158 char zTestName[100]; /* Name of current test */ 159 } g; 160 161 /* 162 ** Include the external vt02.c module, if requested by compile-time 163 ** options. 164 */ 165 #ifdef VT02_SOURCES 166 # include "vt02.c" 167 #endif 168 169 /* 170 ** Print an error message and quit. 171 */ 172 static void fatalError(const char *zFormat, ...){ 173 va_list ap; 174 fprintf(stderr, "%s", g.zArgv0); 175 if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile); 176 if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName); 177 fprintf(stderr, ": "); 178 va_start(ap, zFormat); 179 vfprintf(stderr, zFormat, ap); 180 va_end(ap); 181 fprintf(stderr, "\n"); 182 exit(1); 183 } 184 185 /* 186 ** signal handler 187 */ 188 #ifdef __unix__ 189 static void signalHandler(int signum){ 190 const char *zSig; 191 if( signum==SIGABRT ){ 192 zSig = "abort"; 193 }else if( signum==SIGALRM ){ 194 zSig = "timeout"; 195 }else if( signum==SIGSEGV ){ 196 zSig = "segfault"; 197 }else{ 198 zSig = "signal"; 199 } 200 fatalError(zSig); 201 } 202 #endif 203 204 /* 205 ** Set the an alarm to go off after N seconds. Disable the alarm 206 ** if N==0 207 */ 208 static void setAlarm(int N){ 209 #ifdef __unix__ 210 alarm(N); 211 #else 212 (void)N; 213 #endif 214 } 215 216 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 217 /* 218 ** This an SQL progress handler. After an SQL statement has run for 219 ** many steps, we want to interrupt it. This guards against infinite 220 ** loops from recursive common table expressions. 221 ** 222 ** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used. 223 ** In that case, hitting the progress handler is a fatal error. 224 */ 225 static int progressHandler(void *pVdbeLimitFlag){ 226 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); 227 return 1; 228 } 229 #endif 230 231 /* 232 ** Reallocate memory. Show an error and quit if unable. 233 */ 234 static void *safe_realloc(void *pOld, int szNew){ 235 void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew); 236 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); 237 return pNew; 238 } 239 240 /* 241 ** Initialize the virtual file system. 242 */ 243 static void formatVfs(void){ 244 int i; 245 for(i=0; i<MX_FILE; i++){ 246 g.aFile[i].sz = -1; 247 g.aFile[i].zFilename = 0; 248 g.aFile[i].a = 0; 249 g.aFile[i].nRef = 0; 250 } 251 } 252 253 254 /* 255 ** Erase all information in the virtual file system. 256 */ 257 static void reformatVfs(void){ 258 int i; 259 for(i=0; i<MX_FILE; i++){ 260 if( g.aFile[i].sz<0 ) continue; 261 if( g.aFile[i].zFilename ){ 262 free(g.aFile[i].zFilename); 263 g.aFile[i].zFilename = 0; 264 } 265 if( g.aFile[i].nRef>0 ){ 266 fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef); 267 } 268 g.aFile[i].sz = -1; 269 free(g.aFile[i].a); 270 g.aFile[i].a = 0; 271 g.aFile[i].nRef = 0; 272 } 273 } 274 275 /* 276 ** Find a VFile by name 277 */ 278 static VFile *findVFile(const char *zName){ 279 int i; 280 if( zName==0 ) return 0; 281 for(i=0; i<MX_FILE; i++){ 282 if( g.aFile[i].zFilename==0 ) continue; 283 if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i]; 284 } 285 return 0; 286 } 287 288 /* 289 ** Find a VFile by name. Create it if it does not already exist and 290 ** initialize it to the size and content given. 291 ** 292 ** Return NULL only if the filesystem is full. 293 */ 294 static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ 295 VFile *pNew = findVFile(zName); 296 int i; 297 if( pNew ) return pNew; 298 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} 299 if( i>=MX_FILE ) return 0; 300 pNew = &g.aFile[i]; 301 if( zName ){ 302 int nName = (int)strlen(zName)+1; 303 pNew->zFilename = safe_realloc(0, nName); 304 memcpy(pNew->zFilename, zName, nName); 305 }else{ 306 pNew->zFilename = 0; 307 } 308 pNew->nRef = 0; 309 pNew->sz = sz; 310 pNew->a = safe_realloc(0, sz); 311 if( sz>0 ) memcpy(pNew->a, pData, sz); 312 return pNew; 313 } 314 315 /* Return true if the line is all zeros */ 316 static int allZero(unsigned char *aLine){ 317 int i; 318 for(i=0; i<16 && aLine[i]==0; i++){} 319 return i==16; 320 } 321 322 /* 323 ** Render a database and query as text that can be input into 324 ** the CLI. 325 */ 326 static void renderDbSqlForCLI( 327 FILE *out, /* Write to this file */ 328 const char *zFile, /* Name of the database file */ 329 unsigned char *aDb, /* Database content */ 330 int nDb, /* Number of bytes in aDb[] */ 331 unsigned char *zSql, /* SQL content */ 332 int nSql /* Bytes of SQL */ 333 ){ 334 fprintf(out, ".print ******* %s *******\n", zFile); 335 if( nDb>100 ){ 336 int i, j; /* Loop counters */ 337 int pgsz; /* Size of each page */ 338 int lastPage = 0; /* Last page number shown */ 339 int iPage; /* Current page number */ 340 unsigned char *aLine; /* Single line to display */ 341 unsigned char buf[16]; /* Fake line */ 342 unsigned char bShow[256]; /* Characters ok to display */ 343 344 memset(bShow, '.', sizeof(bShow)); 345 for(i=' '; i<='~'; i++){ 346 if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = i; 347 } 348 pgsz = (aDb[16]<<8) | aDb[17]; 349 if( pgsz==0 ) pgsz = 65536; 350 if( pgsz<512 || (pgsz&(pgsz-1))!=0 ) pgsz = 4096; 351 fprintf(out,".open --hexdb\n"); 352 fprintf(out,"| size %d pagesize %d filename %s\n",nDb,pgsz,zFile); 353 for(i=0; i<nDb; i += 16){ 354 if( i+16>nDb ){ 355 memset(buf, 0, sizeof(buf)); 356 memcpy(buf, aDb+i, nDb-i); 357 aLine = buf; 358 }else{ 359 aLine = aDb + i; 360 } 361 if( allZero(aLine) ) continue; 362 iPage = i/pgsz + 1; 363 if( lastPage!=iPage ){ 364 fprintf(out,"| page %d offset %d\n", iPage, (iPage-1)*pgsz); 365 lastPage = iPage; 366 } 367 fprintf(out,"| %5d:", i-(iPage-1)*pgsz); 368 for(j=0; j<16; j++) fprintf(out," %02x", aLine[j]); 369 fprintf(out," "); 370 for(j=0; j<16; j++){ 371 unsigned char c = (unsigned char)aLine[j]; 372 fputc( bShow[c], stdout); 373 } 374 fputc('\n', stdout); 375 } 376 fprintf(out,"| end %s\n", zFile); 377 }else{ 378 fprintf(out,".open :memory:\n"); 379 } 380 fprintf(out,".testctrl prng_seed 1 db\n"); 381 fprintf(out,".testctrl internal_functions\n"); 382 fprintf(out,"%.*s", nSql, zSql); 383 if( nSql>0 && zSql[nSql-1]!='\n' ) fprintf(out, "\n"); 384 } 385 386 /* 387 ** Read the complete content of a file into memory. Add a 0x00 terminator 388 ** and return a pointer to the result. 389 ** 390 ** The file content is held in memory obtained from sqlite_malloc64() which 391 ** should be freed by the caller. 392 */ 393 static char *readFile(const char *zFilename, long *sz){ 394 FILE *in; 395 long nIn; 396 unsigned char *pBuf; 397 398 *sz = 0; 399 if( zFilename==0 ) return 0; 400 in = fopen(zFilename, "rb"); 401 if( in==0 ) return 0; 402 fseek(in, 0, SEEK_END); 403 *sz = nIn = ftell(in); 404 rewind(in); 405 pBuf = sqlite3_malloc64( nIn+1 ); 406 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ 407 pBuf[nIn] = 0; 408 fclose(in); 409 return (char*)pBuf; 410 } 411 sqlite3_free(pBuf); 412 *sz = 0; 413 fclose(in); 414 return 0; 415 } 416 417 418 /* 419 ** Implementation of the "readfile(X)" SQL function. The entire content 420 ** of the file named X is read and returned as a BLOB. NULL is returned 421 ** if the file does not exist or is unreadable. 422 */ 423 static void readfileFunc( 424 sqlite3_context *context, 425 int argc, 426 sqlite3_value **argv 427 ){ 428 long nIn; 429 void *pBuf; 430 const char *zName = (const char*)sqlite3_value_text(argv[0]); 431 432 if( zName==0 ) return; 433 pBuf = readFile(zName, &nIn); 434 if( pBuf ){ 435 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); 436 } 437 } 438 439 /* 440 ** Implementation of the "readtextfile(X)" SQL function. The text content 441 ** of the file named X through the end of the file or to the first \000 442 ** character, whichever comes first, is read and returned as TEXT. NULL 443 ** is returned if the file does not exist or is unreadable. 444 */ 445 static void readtextfileFunc( 446 sqlite3_context *context, 447 int argc, 448 sqlite3_value **argv 449 ){ 450 const char *zName; 451 FILE *in; 452 long nIn; 453 char *pBuf; 454 455 zName = (const char*)sqlite3_value_text(argv[0]); 456 if( zName==0 ) return; 457 in = fopen(zName, "rb"); 458 if( in==0 ) return; 459 fseek(in, 0, SEEK_END); 460 nIn = ftell(in); 461 rewind(in); 462 pBuf = sqlite3_malloc64( nIn+1 ); 463 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ 464 pBuf[nIn] = 0; 465 sqlite3_result_text(context, pBuf, -1, sqlite3_free); 466 }else{ 467 sqlite3_free(pBuf); 468 } 469 fclose(in); 470 } 471 472 /* 473 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y 474 ** is written into file X. The number of bytes written is returned. Or 475 ** NULL is returned if something goes wrong, such as being unable to open 476 ** file X for writing. 477 */ 478 static void writefileFunc( 479 sqlite3_context *context, 480 int argc, 481 sqlite3_value **argv 482 ){ 483 FILE *out; 484 const char *z; 485 sqlite3_int64 rc; 486 const char *zFile; 487 488 (void)argc; 489 zFile = (const char*)sqlite3_value_text(argv[0]); 490 if( zFile==0 ) return; 491 out = fopen(zFile, "wb"); 492 if( out==0 ) return; 493 z = (const char*)sqlite3_value_blob(argv[1]); 494 if( z==0 ){ 495 rc = 0; 496 }else{ 497 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); 498 } 499 fclose(out); 500 sqlite3_result_int64(context, rc); 501 } 502 503 504 /* 505 ** Load a list of Blob objects from the database 506 */ 507 static void blobListLoadFromDb( 508 sqlite3 *db, /* Read from this database */ 509 const char *zSql, /* Query used to extract the blobs */ 510 int onlyId, /* Only load where id is this value */ 511 int *pN, /* OUT: Write number of blobs loaded here */ 512 Blob **ppList /* OUT: Write the head of the blob list here */ 513 ){ 514 Blob head; 515 Blob *p; 516 sqlite3_stmt *pStmt; 517 int n = 0; 518 int rc; 519 char *z2; 520 521 if( onlyId>0 ){ 522 z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId); 523 }else{ 524 z2 = sqlite3_mprintf("%s", zSql); 525 } 526 rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0); 527 sqlite3_free(z2); 528 if( rc ) fatalError("%s", sqlite3_errmsg(db)); 529 head.pNext = 0; 530 p = &head; 531 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 532 int sz = sqlite3_column_bytes(pStmt, 1); 533 Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz ); 534 pNew->id = sqlite3_column_int(pStmt, 0); 535 pNew->sz = sz; 536 pNew->seq = n++; 537 pNew->pNext = 0; 538 memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz); 539 pNew->a[sz] = 0; 540 p->pNext = pNew; 541 p = pNew; 542 } 543 sqlite3_finalize(pStmt); 544 *pN = n; 545 *ppList = head.pNext; 546 } 547 548 /* 549 ** Free a list of Blob objects 550 */ 551 static void blobListFree(Blob *p){ 552 Blob *pNext; 553 while( p ){ 554 pNext = p->pNext; 555 free(p); 556 p = pNext; 557 } 558 } 559 560 /* Return the current wall-clock time 561 ** 562 ** The number of milliseconds since the julian epoch. 563 ** 1907-01-01 00:00:00 -> 210866716800000 564 ** 2021-01-01 00:00:00 -> 212476176000000 565 */ 566 static sqlite3_int64 timeOfDay(void){ 567 static sqlite3_vfs *clockVfs = 0; 568 sqlite3_int64 t; 569 if( clockVfs==0 ){ 570 clockVfs = sqlite3_vfs_find(0); 571 if( clockVfs==0 ) return 0; 572 } 573 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ 574 clockVfs->xCurrentTimeInt64(clockVfs, &t); 575 }else{ 576 double r; 577 clockVfs->xCurrentTime(clockVfs, &r); 578 t = (sqlite3_int64)(r*86400000.0); 579 } 580 return t; 581 } 582 583 /*************************************************************************** 584 ** Code to process combined database+SQL scripts generated by the 585 ** dbsqlfuzz fuzzer. 586 */ 587 588 /* An instance of the following object is passed by pointer as the 589 ** client data to various callbacks. 590 */ 591 typedef struct FuzzCtx { 592 sqlite3 *db; /* The database connection */ 593 sqlite3_int64 iCutoffTime; /* Stop processing at this time. */ 594 sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */ 595 sqlite3_int64 mxInterval; /* Longest interval between two progress calls */ 596 unsigned nCb; /* Number of progress callbacks */ 597 unsigned mxCb; /* Maximum number of progress callbacks allowed */ 598 unsigned execCnt; /* Number of calls to the sqlite3_exec callback */ 599 int timeoutHit; /* True when reaching a timeout */ 600 } FuzzCtx; 601 602 /* Verbosity level for the dbsqlfuzz test runner */ 603 static int eVerbosity = 0; 604 605 /* True to activate PRAGMA vdbe_debug=on */ 606 static int bVdbeDebug = 0; 607 608 /* Timeout for each fuzzing attempt, in milliseconds */ 609 static int giTimeout = 10000; /* Defaults to 10 seconds */ 610 611 /* Maximum number of progress handler callbacks */ 612 static unsigned int mxProgressCb = 2000; 613 614 /* Maximum string length in SQLite */ 615 static int lengthLimit = 1000000; 616 617 /* Maximum expression depth */ 618 static int depthLimit = 500; 619 620 /* Limit on the amount of heap memory that can be used */ 621 static sqlite3_int64 heapLimit = 100000000; 622 623 /* Maximum byte-code program length in SQLite */ 624 static int vdbeOpLimit = 25000; 625 626 /* Maximum size of the in-memory database */ 627 static sqlite3_int64 maxDbSize = 104857600; 628 /* OOM simulation parameters */ 629 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ 630 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ 631 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ 632 633 /* This routine is called when a simulated OOM occurs. It is broken 634 ** out as a separate routine to make it easy to set a breakpoint on 635 ** the OOM 636 */ 637 void oomFault(void){ 638 if( eVerbosity ){ 639 printf("Simulated OOM fault\n"); 640 } 641 if( oomRepeat>0 ){ 642 oomRepeat--; 643 }else{ 644 oomCounter--; 645 } 646 } 647 648 /* This routine is a replacement malloc() that is used to simulate 649 ** Out-Of-Memory (OOM) errors for testing purposes. 650 */ 651 static void *oomMalloc(int nByte){ 652 if( oomCounter ){ 653 if( oomCounter==1 ){ 654 oomFault(); 655 return 0; 656 }else{ 657 oomCounter--; 658 } 659 } 660 return defaultMalloc(nByte); 661 } 662 663 /* Register the OOM simulator. This must occur before any memory 664 ** allocations */ 665 static void registerOomSimulator(void){ 666 sqlite3_mem_methods mem; 667 sqlite3_shutdown(); 668 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); 669 defaultMalloc = mem.xMalloc; 670 mem.xMalloc = oomMalloc; 671 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); 672 } 673 674 /* Turn off any pending OOM simulation */ 675 static void disableOom(void){ 676 oomCounter = 0; 677 oomRepeat = 0; 678 } 679 680 /* 681 ** Translate a single byte of Hex into an integer. 682 ** This routine only works if h really is a valid hexadecimal 683 ** character: 0..9a..fA..F 684 */ 685 static unsigned char hexToInt(unsigned int h){ 686 #ifdef SQLITE_EBCDIC 687 h += 9*(1&~(h>>4)); /* EBCDIC */ 688 #else 689 h += 9*(1&(h>>6)); /* ASCII */ 690 #endif 691 return h & 0xf; 692 } 693 694 /* 695 ** The first character of buffer zIn[0..nIn-1] is a '['. This routine 696 ** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it 697 ** does it makes corresponding changes to the *pK value and *pI value 698 ** and returns true. If the input buffer does not match the patterns, 699 ** no changes are made to either *pK or *pI and this routine returns false. 700 */ 701 static int isOffset( 702 const unsigned char *zIn, /* Text input */ 703 int nIn, /* Bytes of input */ 704 unsigned int *pK, /* half-byte cursor to adjust */ 705 unsigned int *pI /* Input index to adjust */ 706 ){ 707 int i; 708 unsigned int k = 0; 709 unsigned char c; 710 for(i=1; i<nIn && (c = zIn[i])!=']'; i++){ 711 if( !isxdigit(c) ) return 0; 712 k = k*16 + hexToInt(c); 713 } 714 if( i==nIn ) return 0; 715 *pK = 2*k; 716 *pI += i; 717 return 1; 718 } 719 720 /* 721 ** Decode the text starting at zIn into a binary database file. 722 ** The maximum length of zIn is nIn bytes. Store the binary database 723 ** file in space obtained from sqlite3_malloc(). 724 ** 725 ** Return the number of bytes of zIn consumed. Or return -1 if there 726 ** is an error. One potential error is that the recipe specifies a 727 ** database file larger than MX_FILE_SZ bytes. 728 ** 729 ** Abort on an OOM. 730 */ 731 static int decodeDatabase( 732 const unsigned char *zIn, /* Input text to be decoded */ 733 int nIn, /* Bytes of input text */ 734 unsigned char **paDecode, /* OUT: decoded database file */ 735 int *pnDecode /* OUT: Size of decoded database */ 736 ){ 737 unsigned char *a, *aNew; /* Database under construction */ 738 int mx = 0; /* Current size of the database */ 739 sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */ 740 unsigned int i; /* Next byte of zIn[] to read */ 741 unsigned int j; /* Temporary integer */ 742 unsigned int k; /* half-byte cursor index for output */ 743 unsigned int n; /* Number of bytes of input */ 744 unsigned char b = 0; 745 if( nIn<4 ) return -1; 746 n = (unsigned int)nIn; 747 a = sqlite3_malloc64( nAlloc ); 748 if( a==0 ){ 749 fprintf(stderr, "Out of memory!\n"); 750 exit(1); 751 } 752 memset(a, 0, (size_t)nAlloc); 753 for(i=k=0; i<n; i++){ 754 unsigned char c = (unsigned char)zIn[i]; 755 if( isxdigit(c) ){ 756 k++; 757 if( k & 1 ){ 758 b = hexToInt(c)*16; 759 }else{ 760 b += hexToInt(c); 761 j = k/2 - 1; 762 if( j>=nAlloc ){ 763 sqlite3_uint64 newSize; 764 if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){ 765 if( eVerbosity ){ 766 fprintf(stderr, "Input database too big: max %d bytes\n", 767 MX_FILE_SZ); 768 } 769 sqlite3_free(a); 770 return -1; 771 } 772 newSize = nAlloc*2; 773 if( newSize<=j ){ 774 newSize = (j+4096)&~4095; 775 } 776 if( newSize>MX_FILE_SZ ){ 777 if( j>=MX_FILE_SZ ){ 778 sqlite3_free(a); 779 return -1; 780 } 781 newSize = MX_FILE_SZ; 782 } 783 aNew = sqlite3_realloc64( a, newSize ); 784 if( aNew==0 ){ 785 sqlite3_free(a); 786 return -1; 787 } 788 a = aNew; 789 assert( newSize > nAlloc ); 790 memset(a+nAlloc, 0, (size_t)(newSize - nAlloc)); 791 nAlloc = newSize; 792 } 793 if( j>=(unsigned)mx ){ 794 mx = (j + 4095)&~4095; 795 if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ; 796 } 797 assert( j<nAlloc ); 798 a[j] = b; 799 } 800 }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){ 801 continue; 802 }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){ 803 i += 4; 804 break; 805 } 806 } 807 *pnDecode = mx; 808 *paDecode = a; 809 return i; 810 } 811 812 /* 813 ** Progress handler callback. 814 ** 815 ** The argument is the cutoff-time after which all processing should 816 ** stop. So return non-zero if the cut-off time is exceeded. 817 */ 818 static int progress_handler(void *pClientData) { 819 FuzzCtx *p = (FuzzCtx*)pClientData; 820 sqlite3_int64 iNow = timeOfDay(); 821 int rc = iNow>=p->iCutoffTime; 822 sqlite3_int64 iDiff = iNow - p->iLastCb; 823 /* printf("time-remaining: %lld\n", p->iCutoffTime - iNow); */ 824 if( iDiff > p->mxInterval ) p->mxInterval = iDiff; 825 p->nCb++; 826 if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1; 827 if( rc && !p->timeoutHit && eVerbosity>=2 ){ 828 printf("Timeout on progress callback %d\n", p->nCb); 829 fflush(stdout); 830 p->timeoutHit = 1; 831 } 832 return rc; 833 } 834 835 /* 836 ** Flag bits set by block_troublesome_sql() 837 */ 838 #define BTS_SELECT 0x000001 839 #define BTS_NONSELECT 0x000002 840 #define BTS_BADFUNC 0x000004 841 842 /* 843 ** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and 844 ** "PRAGMA parser_trace" since they can dramatically increase the 845 ** amount of output without actually testing anything useful. 846 ** 847 ** Also block ATTACH if attaching a file from the filesystem. 848 */ 849 static int block_troublesome_sql( 850 void *pClientData, 851 int eCode, 852 const char *zArg1, 853 const char *zArg2, 854 const char *zArg3, 855 const char *zArg4 856 ){ 857 unsigned int *pFlags = (unsigned int*)pClientData; 858 (void)zArg3; 859 (void)zArg4; 860 switch( eCode ){ 861 case SQLITE_PRAGMA: { 862 if( sqlite3_stricmp("busy_timeout",zArg1)==0 863 && (zArg2==0 || strtoll(zArg2,0,0)>100 || strtoll(zArg2,0,10)>100) 864 ){ 865 return SQLITE_DENY; 866 }else if( eVerbosity==0 ){ 867 if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0 868 || sqlite3_stricmp("parser_trace", zArg1)==0 869 || sqlite3_stricmp("temp_store_directory", zArg1)==0 870 ){ 871 return SQLITE_DENY; 872 } 873 }else if( sqlite3_stricmp("oom",zArg1)==0 874 && zArg2!=0 && zArg2[0]!=0 ){ 875 oomCounter = atoi(zArg2); 876 } 877 *pFlags |= BTS_NONSELECT; 878 break; 879 } 880 case SQLITE_ATTACH: { 881 /* Deny the ATTACH if it is attaching anything other than an in-memory 882 ** database. */ 883 *pFlags |= BTS_NONSELECT; 884 if( zArg1==0 ) return SQLITE_DENY; 885 if( strcmp(zArg1,":memory:")==0 ) return SQLITE_OK; 886 if( sqlite3_strglob("file:*[?]vfs=memdb", zArg1)==0 887 && sqlite3_strglob("file:*[^/a-zA-Z0-9_.]*[?]vfs=memdb", zArg1)!=0 888 ){ 889 return SQLITE_OK; 890 } 891 return SQLITE_DENY; 892 } 893 case SQLITE_SELECT: { 894 *pFlags |= BTS_SELECT; 895 break; 896 } 897 case SQLITE_FUNCTION: { 898 static const char *azBadFuncs[] = { 899 "random", 900 "randomblob", 901 "rtreedepth", 902 }; 903 int i; 904 for(i=0; i<sizeof(azBadFuncs)/sizeof(azBadFuncs[0]); i++){ 905 if( sqlite3_stricmp(azBadFuncs[i], zArg2)==0 ){ 906 *pFlags |= BTS_BADFUNC; 907 break; 908 } 909 } 910 break; 911 } 912 case SQLITE_READ: { 913 /* Benign */ 914 break; 915 } 916 default: { 917 *pFlags |= BTS_NONSELECT; 918 } 919 } 920 return SQLITE_OK; 921 } 922 923 /* Implementation found in fuzzinvariant.c */ 924 int fuzz_invariant( 925 sqlite3 *db, /* The database connection */ 926 sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ 927 int iCnt, /* Invariant sequence number, starting at 0 */ 928 int iRow, /* The row number for pStmt */ 929 int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ 930 int eVerbosity /* How much debugging output */ 931 ); 932 933 /* 934 ** Run the SQL text 935 */ 936 static int runDbSql(sqlite3 *db, const char *zSql, unsigned int *pBtsFlags){ 937 int rc; 938 sqlite3_stmt *pStmt; 939 int bCorrupt = 0; 940 while( isspace(zSql[0]&0x7f) ) zSql++; 941 if( zSql[0]==0 ) return SQLITE_OK; 942 if( eVerbosity>=4 ){ 943 printf("RUNNING-SQL: [%s]\n", zSql); 944 fflush(stdout); 945 } 946 (*pBtsFlags) = 0; 947 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 948 if( rc==SQLITE_OK ){ 949 int nRow = 0; 950 while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ 951 nRow++; 952 if( (*pBtsFlags)==BTS_SELECT && g.doInvariantChecks ){ 953 int iCnt = 0; 954 for(iCnt=0; iCnt<99999; iCnt++){ 955 rc = fuzz_invariant(db, pStmt, iCnt, nRow, &bCorrupt, eVerbosity); 956 if( rc==SQLITE_DONE ) break; 957 g.nInvariant++; 958 if( eVerbosity>0 ){ 959 if( rc==SQLITE_OK ){ 960 printf("invariant-check: ok\n"); 961 }else if( rc==SQLITE_CORRUPT ){ 962 printf("invariant-check: failed due to database corruption\n"); 963 } 964 } 965 } 966 } 967 if( eVerbosity>=5 ){ 968 int j; 969 for(j=0; j<sqlite3_column_count(pStmt); j++){ 970 if( j ) printf(","); 971 switch( sqlite3_column_type(pStmt, j) ){ 972 case SQLITE_NULL: { 973 printf("NULL"); 974 break; 975 } 976 case SQLITE_INTEGER: 977 case SQLITE_FLOAT: { 978 printf("%s", sqlite3_column_text(pStmt, j)); 979 break; 980 } 981 case SQLITE_BLOB: { 982 int n = sqlite3_column_bytes(pStmt, j); 983 int i; 984 const unsigned char *a; 985 a = (const unsigned char*)sqlite3_column_blob(pStmt, j); 986 printf("x'"); 987 for(i=0; i<n; i++){ 988 printf("%02x", a[i]); 989 } 990 printf("'"); 991 break; 992 } 993 case SQLITE_TEXT: { 994 int n = sqlite3_column_bytes(pStmt, j); 995 int i; 996 const unsigned char *a; 997 a = (const unsigned char*)sqlite3_column_blob(pStmt, j); 998 printf("'"); 999 for(i=0; i<n; i++){ 1000 if( a[i]=='\'' ){ 1001 printf("''"); 1002 }else{ 1003 putchar(a[i]); 1004 } 1005 } 1006 printf("'"); 1007 break; 1008 } 1009 } /* End switch() */ 1010 } /* End for() */ 1011 printf("\n"); 1012 fflush(stdout); 1013 } /* End if( eVerbosity>=5 ) */ 1014 } /* End while( SQLITE_ROW */ 1015 if( rc!=SQLITE_DONE && eVerbosity>=4 ){ 1016 printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db)); 1017 fflush(stdout); 1018 } 1019 }else if( eVerbosity>=4 ){ 1020 printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db)); 1021 fflush(stdout); 1022 } /* End if( SQLITE_OK ) */ 1023 return sqlite3_finalize(pStmt); 1024 } 1025 1026 /* Invoke this routine to run a single test case */ 1027 int runCombinedDbSqlInput( 1028 const uint8_t *aData, /* Combined DB+SQL content */ 1029 size_t nByte, /* Size of aData in bytes */ 1030 int iTimeout, /* Use this timeout */ 1031 int bScript, /* If true, just render CLI output */ 1032 int iSqlId /* SQL identifier */ 1033 ){ 1034 int rc; /* SQLite API return value */ 1035 int iSql; /* Index in aData[] of start of SQL */ 1036 unsigned char *aDb = 0; /* Decoded database content */ 1037 int nDb = 0; /* Size of the decoded database */ 1038 int i; /* Loop counter */ 1039 int j; /* Start of current SQL statement */ 1040 char *zSql = 0; /* SQL text to run */ 1041 int nSql; /* Bytes of SQL text */ 1042 FuzzCtx cx; /* Fuzzing context */ 1043 unsigned int btsFlags = 0; /* Parsing flags */ 1044 1045 if( nByte<10 ) return 0; 1046 if( sqlite3_initialize() ) return 0; 1047 if( sqlite3_memory_used()!=0 ){ 1048 int nAlloc = 0; 1049 int nNotUsed = 0; 1050 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); 1051 fprintf(stderr,"memory leak prior to test start:" 1052 " %lld bytes in %d allocations\n", 1053 sqlite3_memory_used(), nAlloc); 1054 exit(1); 1055 } 1056 memset(&cx, 0, sizeof(cx)); 1057 iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb); 1058 if( iSql<0 ) return 0; 1059 nSql = (int)(nByte - iSql); 1060 if( bScript ){ 1061 char zName[100]; 1062 sqlite3_snprintf(sizeof(zName),zName,"dbsql%06d.db",iSqlId); 1063 renderDbSqlForCLI(stdout, zName, aDb, nDb, 1064 (unsigned char*)(aData+iSql), nSql); 1065 sqlite3_free(aDb); 1066 return 0; 1067 } 1068 if( eVerbosity>=3 ){ 1069 printf( 1070 "****** %d-byte input, %d-byte database, %d-byte script " 1071 "******\n", (int)nByte, nDb, nSql); 1072 fflush(stdout); 1073 } 1074 rc = sqlite3_open(0, &cx.db); 1075 if( rc ){ 1076 sqlite3_free(aDb); 1077 return 1; 1078 } 1079 if( bVdbeDebug ){ 1080 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0); 1081 } 1082 1083 /* Invoke the progress handler frequently to check to see if we 1084 ** are taking too long. The progress handler will return true 1085 ** (which will block further processing) if more than giTimeout seconds have 1086 ** elapsed since the start of the test. 1087 */ 1088 cx.iLastCb = timeOfDay(); 1089 cx.iCutoffTime = cx.iLastCb + (iTimeout<giTimeout ? iTimeout : giTimeout); 1090 cx.mxCb = mxProgressCb; 1091 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1092 sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx); 1093 #endif 1094 1095 /* Set a limit on the maximum size of a prepared statement, and the 1096 ** maximum length of a string or blob */ 1097 if( vdbeOpLimit>0 ){ 1098 sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit); 1099 } 1100 if( lengthLimit>0 ){ 1101 sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit); 1102 } 1103 if( depthLimit>0 ){ 1104 sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit); 1105 } 1106 sqlite3_limit(cx.db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 100); 1107 sqlite3_hard_heap_limit64(heapLimit); 1108 1109 if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){ 1110 aDb[18] = aDb[19] = 1; 1111 } 1112 rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb, 1113 SQLITE_DESERIALIZE_RESIZEABLE | 1114 SQLITE_DESERIALIZE_FREEONCLOSE); 1115 if( rc ){ 1116 fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc); 1117 goto testrun_finished; 1118 } 1119 if( maxDbSize>0 ){ 1120 sqlite3_int64 x = maxDbSize; 1121 sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); 1122 } 1123 1124 /* For high debugging levels, turn on debug mode */ 1125 if( eVerbosity>=5 ){ 1126 sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0); 1127 } 1128 1129 /* Block debug pragmas and ATTACH/DETACH. But wait until after 1130 ** deserialize to do this because deserialize depends on ATTACH */ 1131 sqlite3_set_authorizer(cx.db, block_troublesome_sql, &btsFlags); 1132 1133 #ifdef VT02_SOURCES 1134 sqlite3_vt02_init(cx.db, 0, 0); 1135 #endif 1136 1137 /* Consistent PRNG seed */ 1138 #ifdef SQLITE_TESTCTRL_PRNG_SEED 1139 sqlite3_table_column_metadata(cx.db, 0, "x", 0, 0, 0, 0, 0, 0); 1140 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, cx.db); 1141 #else 1142 sqlite3_randomness(0,0); 1143 #endif 1144 1145 zSql = sqlite3_malloc( nSql + 1 ); 1146 if( zSql==0 ){ 1147 fprintf(stderr, "Out of memory!\n"); 1148 }else{ 1149 memcpy(zSql, aData+iSql, nSql); 1150 zSql[nSql] = 0; 1151 for(i=j=0; zSql[i]; i++){ 1152 if( zSql[i]==';' ){ 1153 char cSaved = zSql[i+1]; 1154 zSql[i+1] = 0; 1155 if( sqlite3_complete(zSql+j) ){ 1156 rc = runDbSql(cx.db, zSql+j, &btsFlags); 1157 j = i+1; 1158 } 1159 zSql[i+1] = cSaved; 1160 if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){ 1161 goto testrun_finished; 1162 } 1163 } 1164 } 1165 if( j<i ){ 1166 runDbSql(cx.db, zSql+j, &btsFlags); 1167 } 1168 } 1169 testrun_finished: 1170 sqlite3_free(zSql); 1171 rc = sqlite3_close(cx.db); 1172 if( rc!=SQLITE_OK ){ 1173 fprintf(stdout, "sqlite3_close() returns %d\n", rc); 1174 } 1175 if( eVerbosity>=2 && !bScript ){ 1176 fprintf(stdout, "Peak memory usages: %f MB\n", 1177 sqlite3_memory_highwater(1) / 1000000.0); 1178 } 1179 if( sqlite3_memory_used()!=0 ){ 1180 int nAlloc = 0; 1181 int nNotUsed = 0; 1182 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); 1183 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", 1184 sqlite3_memory_used(), nAlloc); 1185 exit(1); 1186 } 1187 sqlite3_hard_heap_limit64(0); 1188 sqlite3_soft_heap_limit64(0); 1189 return 0; 1190 } 1191 1192 /* 1193 ** END of the dbsqlfuzz code 1194 ***************************************************************************/ 1195 1196 /* Look at a SQL text and try to determine if it begins with a database 1197 ** description, such as would be found in a dbsqlfuzz test case. Return 1198 ** true if this does appear to be a dbsqlfuzz test case and false otherwise. 1199 */ 1200 static int isDbSql(unsigned char *a, int n){ 1201 unsigned char buf[12]; 1202 int i; 1203 if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1; 1204 while( n>0 && isspace(a[0]) ){ a++; n--; } 1205 for(i=0; n>0 && i<8; n--, a++){ 1206 if( isxdigit(a[0]) ) buf[i++] = a[0]; 1207 } 1208 if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1; 1209 return 0; 1210 } 1211 1212 /* Implementation of the isdbsql(TEXT) SQL function. 1213 */ 1214 static void isDbSqlFunc( 1215 sqlite3_context *context, 1216 int argc, 1217 sqlite3_value **argv 1218 ){ 1219 int n = sqlite3_value_bytes(argv[0]); 1220 unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]); 1221 sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n)); 1222 } 1223 1224 /* Methods for the VHandle object 1225 */ 1226 static int inmemClose(sqlite3_file *pFile){ 1227 VHandle *p = (VHandle*)pFile; 1228 VFile *pVFile = p->pVFile; 1229 pVFile->nRef--; 1230 if( pVFile->nRef==0 && pVFile->zFilename==0 ){ 1231 pVFile->sz = -1; 1232 free(pVFile->a); 1233 pVFile->a = 0; 1234 } 1235 return SQLITE_OK; 1236 } 1237 static int inmemRead( 1238 sqlite3_file *pFile, /* Read from this open file */ 1239 void *pData, /* Store content in this buffer */ 1240 int iAmt, /* Bytes of content */ 1241 sqlite3_int64 iOfst /* Start reading here */ 1242 ){ 1243 VHandle *pHandle = (VHandle*)pFile; 1244 VFile *pVFile = pHandle->pVFile; 1245 if( iOfst<0 || iOfst>=pVFile->sz ){ 1246 memset(pData, 0, iAmt); 1247 return SQLITE_IOERR_SHORT_READ; 1248 } 1249 if( iOfst+iAmt>pVFile->sz ){ 1250 memset(pData, 0, iAmt); 1251 iAmt = (int)(pVFile->sz - iOfst); 1252 memcpy(pData, pVFile->a + iOfst, iAmt); 1253 return SQLITE_IOERR_SHORT_READ; 1254 } 1255 memcpy(pData, pVFile->a + iOfst, iAmt); 1256 return SQLITE_OK; 1257 } 1258 static int inmemWrite( 1259 sqlite3_file *pFile, /* Write to this file */ 1260 const void *pData, /* Content to write */ 1261 int iAmt, /* bytes to write */ 1262 sqlite3_int64 iOfst /* Start writing here */ 1263 ){ 1264 VHandle *pHandle = (VHandle*)pFile; 1265 VFile *pVFile = pHandle->pVFile; 1266 if( iOfst+iAmt > pVFile->sz ){ 1267 if( iOfst+iAmt >= MX_FILE_SZ ){ 1268 return SQLITE_FULL; 1269 } 1270 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt)); 1271 if( iOfst > pVFile->sz ){ 1272 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz)); 1273 } 1274 pVFile->sz = (int)(iOfst + iAmt); 1275 } 1276 memcpy(pVFile->a + iOfst, pData, iAmt); 1277 return SQLITE_OK; 1278 } 1279 static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){ 1280 VHandle *pHandle = (VHandle*)pFile; 1281 VFile *pVFile = pHandle->pVFile; 1282 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize; 1283 return SQLITE_OK; 1284 } 1285 static int inmemSync(sqlite3_file *pFile, int flags){ 1286 return SQLITE_OK; 1287 } 1288 static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ 1289 *pSize = ((VHandle*)pFile)->pVFile->sz; 1290 return SQLITE_OK; 1291 } 1292 static int inmemLock(sqlite3_file *pFile, int type){ 1293 return SQLITE_OK; 1294 } 1295 static int inmemUnlock(sqlite3_file *pFile, int type){ 1296 return SQLITE_OK; 1297 } 1298 static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){ 1299 *pOut = 0; 1300 return SQLITE_OK; 1301 } 1302 static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){ 1303 return SQLITE_NOTFOUND; 1304 } 1305 static int inmemSectorSize(sqlite3_file *pFile){ 1306 return 512; 1307 } 1308 static int inmemDeviceCharacteristics(sqlite3_file *pFile){ 1309 return 1310 SQLITE_IOCAP_SAFE_APPEND | 1311 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | 1312 SQLITE_IOCAP_POWERSAFE_OVERWRITE; 1313 } 1314 1315 1316 /* Method table for VHandle 1317 */ 1318 static sqlite3_io_methods VHandleMethods = { 1319 /* iVersion */ 1, 1320 /* xClose */ inmemClose, 1321 /* xRead */ inmemRead, 1322 /* xWrite */ inmemWrite, 1323 /* xTruncate */ inmemTruncate, 1324 /* xSync */ inmemSync, 1325 /* xFileSize */ inmemFileSize, 1326 /* xLock */ inmemLock, 1327 /* xUnlock */ inmemUnlock, 1328 /* xCheck... */ inmemCheckReservedLock, 1329 /* xFileCtrl */ inmemFileControl, 1330 /* xSectorSz */ inmemSectorSize, 1331 /* xDevchar */ inmemDeviceCharacteristics, 1332 /* xShmMap */ 0, 1333 /* xShmLock */ 0, 1334 /* xShmBarrier */ 0, 1335 /* xShmUnmap */ 0, 1336 /* xFetch */ 0, 1337 /* xUnfetch */ 0 1338 }; 1339 1340 /* 1341 ** Open a new file in the inmem VFS. All files are anonymous and are 1342 ** delete-on-close. 1343 */ 1344 static int inmemOpen( 1345 sqlite3_vfs *pVfs, 1346 const char *zFilename, 1347 sqlite3_file *pFile, 1348 int openFlags, 1349 int *pOutFlags 1350 ){ 1351 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)""); 1352 VHandle *pHandle = (VHandle*)pFile; 1353 if( pVFile==0 ){ 1354 return SQLITE_FULL; 1355 } 1356 pHandle->pVFile = pVFile; 1357 pVFile->nRef++; 1358 pFile->pMethods = &VHandleMethods; 1359 if( pOutFlags ) *pOutFlags = openFlags; 1360 return SQLITE_OK; 1361 } 1362 1363 /* 1364 ** Delete a file by name 1365 */ 1366 static int inmemDelete( 1367 sqlite3_vfs *pVfs, 1368 const char *zFilename, 1369 int syncdir 1370 ){ 1371 VFile *pVFile = findVFile(zFilename); 1372 if( pVFile==0 ) return SQLITE_OK; 1373 if( pVFile->nRef==0 ){ 1374 free(pVFile->zFilename); 1375 pVFile->zFilename = 0; 1376 pVFile->sz = -1; 1377 free(pVFile->a); 1378 pVFile->a = 0; 1379 return SQLITE_OK; 1380 } 1381 return SQLITE_IOERR_DELETE; 1382 } 1383 1384 /* Check for the existance of a file 1385 */ 1386 static int inmemAccess( 1387 sqlite3_vfs *pVfs, 1388 const char *zFilename, 1389 int flags, 1390 int *pResOut 1391 ){ 1392 VFile *pVFile = findVFile(zFilename); 1393 *pResOut = pVFile!=0; 1394 return SQLITE_OK; 1395 } 1396 1397 /* Get the canonical pathname for a file 1398 */ 1399 static int inmemFullPathname( 1400 sqlite3_vfs *pVfs, 1401 const char *zFilename, 1402 int nOut, 1403 char *zOut 1404 ){ 1405 sqlite3_snprintf(nOut, zOut, "%s", zFilename); 1406 return SQLITE_OK; 1407 } 1408 1409 /* Always use the same random see, for repeatability. 1410 */ 1411 static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ 1412 memset(zBuf, 0, nBuf); 1413 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom)); 1414 return nBuf; 1415 } 1416 1417 /* 1418 ** Register the VFS that reads from the g.aFile[] set of files. 1419 */ 1420 static void inmemVfsRegister(int makeDefault){ 1421 static sqlite3_vfs inmemVfs; 1422 sqlite3_vfs *pDefault = sqlite3_vfs_find(0); 1423 inmemVfs.iVersion = 3; 1424 inmemVfs.szOsFile = sizeof(VHandle); 1425 inmemVfs.mxPathname = 200; 1426 inmemVfs.zName = "inmem"; 1427 inmemVfs.xOpen = inmemOpen; 1428 inmemVfs.xDelete = inmemDelete; 1429 inmemVfs.xAccess = inmemAccess; 1430 inmemVfs.xFullPathname = inmemFullPathname; 1431 inmemVfs.xRandomness = inmemRandomness; 1432 inmemVfs.xSleep = pDefault->xSleep; 1433 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; 1434 sqlite3_vfs_register(&inmemVfs, makeDefault); 1435 }; 1436 1437 /* 1438 ** Allowed values for the runFlags parameter to runSql() 1439 */ 1440 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ 1441 #define SQL_OUTPUT 0x0002 /* Show the SQL output */ 1442 1443 /* 1444 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not 1445 ** stop if an error is encountered. 1446 */ 1447 static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){ 1448 const char *zMore; 1449 sqlite3_stmt *pStmt; 1450 1451 while( zSql && zSql[0] ){ 1452 zMore = 0; 1453 pStmt = 0; 1454 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore); 1455 if( zMore==zSql ) break; 1456 if( runFlags & SQL_TRACE ){ 1457 const char *z = zSql; 1458 int n; 1459 while( z<zMore && ISSPACE(z[0]) ) z++; 1460 n = (int)(zMore - z); 1461 while( n>0 && ISSPACE(z[n-1]) ) n--; 1462 if( n==0 ) break; 1463 if( pStmt==0 ){ 1464 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db)); 1465 }else{ 1466 printf("TRACE: %.*s\n", n, z); 1467 } 1468 } 1469 zSql = zMore; 1470 if( pStmt ){ 1471 if( (runFlags & SQL_OUTPUT)==0 ){ 1472 while( SQLITE_ROW==sqlite3_step(pStmt) ){} 1473 }else{ 1474 int nCol = -1; 1475 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 1476 int i; 1477 if( nCol<0 ){ 1478 nCol = sqlite3_column_count(pStmt); 1479 }else if( nCol>0 ){ 1480 printf("--------------------------------------------\n"); 1481 } 1482 for(i=0; i<nCol; i++){ 1483 int eType = sqlite3_column_type(pStmt,i); 1484 printf("%s = ", sqlite3_column_name(pStmt,i)); 1485 switch( eType ){ 1486 case SQLITE_NULL: { 1487 printf("NULL\n"); 1488 break; 1489 } 1490 case SQLITE_INTEGER: { 1491 printf("INT %s\n", sqlite3_column_text(pStmt,i)); 1492 break; 1493 } 1494 case SQLITE_FLOAT: { 1495 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i)); 1496 break; 1497 } 1498 case SQLITE_TEXT: { 1499 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i)); 1500 break; 1501 } 1502 case SQLITE_BLOB: { 1503 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i)); 1504 break; 1505 } 1506 } 1507 } 1508 } 1509 } 1510 sqlite3_finalize(pStmt); 1511 } 1512 } 1513 } 1514 1515 /* 1516 ** Rebuild the database file. 1517 ** 1518 ** (1) Remove duplicate entries 1519 ** (2) Put all entries in order 1520 ** (3) Vacuum 1521 */ 1522 static void rebuild_database(sqlite3 *db, int dbSqlOnly){ 1523 int rc; 1524 char *zSql; 1525 zSql = sqlite3_mprintf( 1526 "BEGIN;\n" 1527 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n" 1528 "DELETE FROM db;\n" 1529 "INSERT INTO db(dbid, dbcontent) " 1530 " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n" 1531 "DROP TABLE dbx;\n" 1532 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n" 1533 "DELETE FROM xsql;\n" 1534 "INSERT INTO xsql(sqlid,sqltext) " 1535 " SELECT NULL, sqltext FROM sx ORDER BY 2;\n" 1536 "DROP TABLE sx;\n" 1537 "COMMIT;\n" 1538 "PRAGMA page_size=1024;\n" 1539 "VACUUM;\n", 1540 dbSqlOnly ? " WHERE isdbsql(sqltext)" : "" 1541 ); 1542 rc = sqlite3_exec(db, zSql, 0, 0, 0); 1543 sqlite3_free(zSql); 1544 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db)); 1545 } 1546 1547 /* 1548 ** Return the value of a hexadecimal digit. Return -1 if the input 1549 ** is not a hex digit. 1550 */ 1551 static int hexDigitValue(char c){ 1552 if( c>='0' && c<='9' ) return c - '0'; 1553 if( c>='a' && c<='f' ) return c - 'a' + 10; 1554 if( c>='A' && c<='F' ) return c - 'A' + 10; 1555 return -1; 1556 } 1557 1558 /* 1559 ** Interpret zArg as an integer value, possibly with suffixes. 1560 */ 1561 static int integerValue(const char *zArg){ 1562 sqlite3_int64 v = 0; 1563 static const struct { char *zSuffix; int iMult; } aMult[] = { 1564 { "KiB", 1024 }, 1565 { "MiB", 1024*1024 }, 1566 { "GiB", 1024*1024*1024 }, 1567 { "KB", 1000 }, 1568 { "MB", 1000000 }, 1569 { "GB", 1000000000 }, 1570 { "K", 1000 }, 1571 { "M", 1000000 }, 1572 { "G", 1000000000 }, 1573 }; 1574 int i; 1575 int isNeg = 0; 1576 if( zArg[0]=='-' ){ 1577 isNeg = 1; 1578 zArg++; 1579 }else if( zArg[0]=='+' ){ 1580 zArg++; 1581 } 1582 if( zArg[0]=='0' && zArg[1]=='x' ){ 1583 int x; 1584 zArg += 2; 1585 while( (x = hexDigitValue(zArg[0]))>=0 ){ 1586 v = (v<<4) + x; 1587 zArg++; 1588 } 1589 }else{ 1590 while( ISDIGIT(zArg[0]) ){ 1591 v = v*10 + zArg[0] - '0'; 1592 zArg++; 1593 } 1594 } 1595 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ 1596 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ 1597 v *= aMult[i].iMult; 1598 break; 1599 } 1600 } 1601 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648"); 1602 return (int)(isNeg? -v : v); 1603 } 1604 1605 /* 1606 ** Return the number of "v" characters in a string. Return 0 if there 1607 ** are any characters in the string other than "v". 1608 */ 1609 static int numberOfVChar(const char *z){ 1610 int N = 0; 1611 while( z[0] && z[0]=='v' ){ 1612 z++; 1613 N++; 1614 } 1615 return z[0]==0 ? N : 0; 1616 } 1617 1618 /* 1619 ** Print sketchy documentation for this utility program 1620 */ 1621 static void showHelp(void){ 1622 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); 1623 printf( 1624 "Read databases and SQL scripts from SOURCE-DB and execute each script against\n" 1625 "each database, checking for crashes and memory leaks.\n" 1626 "Options:\n" 1627 " --cell-size-check Set the PRAGMA cell_size_check=ON\n" 1628 " --dbid N Use only the database where dbid=N\n" 1629 " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" 1630 " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" 1631 " --help Show this help text\n" 1632 " --info Show information about SOURCE-DB w/o running tests\n" 1633 " --limit-depth N Limit expression depth to N. Default: 500\n" 1634 " --limit-heap N Limit heap memory to N. Default: 100M\n" 1635 " --limit-mem N Limit memory used by test SQLite instance to N bytes\n" 1636 " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" 1637 " --load-sql FILE.. Load SQL scripts fron files into SOURCE-DB\n" 1638 " --load-db FILE.. Load template databases from files into SOURCE_DB\n" 1639 " --load-dbsql FILE.. Load dbsqlfuzz outputs into the xsql table\n" 1640 " ^^^^------ Use \"-\" for FILE to read filenames from stdin\n" 1641 " -m TEXT Add a description to the database\n" 1642 " --native-vfs Use the native VFS for initially empty database files\n" 1643 " --native-malloc Turn off MEMSYS3/5 and Lookaside\n" 1644 " --oss-fuzz Enable OSS-FUZZ testing\n" 1645 " --prng-seed N Seed value for the PRGN inside of SQLite\n" 1646 " -q|--quiet Reduced output\n" 1647 " --query-invariants Run query invariant checks\n" 1648 " --rebuild Rebuild and vacuum the database file\n" 1649 " --result-trace Show the results of each SQL command\n" 1650 " --script Output CLI script instead of running tests\n" 1651 " --skip N Skip the first N test cases\n" 1652 " --spinner Use a spinner to show progress\n" 1653 " --sqlid N Use only SQL where sqlid=N\n" 1654 " --timeout N Maximum time for any one test in N millseconds\n" 1655 " -v|--verbose Increased output. Repeat for more output.\n" 1656 " --vdbe-debug Activate VDBE debugging.\n" 1657 ); 1658 } 1659 1660 int main(int argc, char **argv){ 1661 sqlite3_int64 iBegin; /* Start time of this program */ 1662 int quietFlag = 0; /* True if --quiet or -q */ 1663 int verboseFlag = 0; /* True if --verbose or -v */ 1664 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ 1665 int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */ 1666 sqlite3 *db = 0; /* The open database connection */ 1667 sqlite3_stmt *pStmt; /* A prepared statement */ 1668 int rc; /* Result code from SQLite interface calls */ 1669 Blob *pSql; /* For looping over SQL scripts */ 1670 Blob *pDb; /* For looping over template databases */ 1671 int i; /* Loop index for the argv[] loop */ 1672 int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */ 1673 int onlySqlid = -1; /* --sqlid */ 1674 int onlyDbid = -1; /* --dbid */ 1675 int nativeFlag = 0; /* --native-vfs */ 1676 int rebuildFlag = 0; /* --rebuild */ 1677 int vdbeLimitFlag = 0; /* --limit-vdbe */ 1678 int infoFlag = 0; /* --info */ 1679 int nSkip = 0; /* --skip */ 1680 int bScript = 0; /* --script */ 1681 int bSpinner = 0; /* True for --spinner */ 1682 int timeoutTest = 0; /* undocumented --timeout-test flag */ 1683 int runFlags = 0; /* Flags sent to runSql() */ 1684 char *zMsg = 0; /* Add this message */ 1685 int nSrcDb = 0; /* Number of source databases */ 1686 char **azSrcDb = 0; /* Array of source database names */ 1687 int iSrcDb; /* Loop over all source databases */ 1688 int nTest = 0; /* Total number of tests performed */ 1689 char *zDbName = ""; /* Appreviated name of a source database */ 1690 const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */ 1691 int cellSzCkFlag = 0; /* --cell-size-check */ 1692 int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */ 1693 int iTimeout = 120000; /* Default 120-second timeout */ 1694 int nMem = 0; /* Memory limit override */ 1695 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */ 1696 char *zExpDb = 0; /* Write Databases to files in this directory */ 1697 char *zExpSql = 0; /* Write SQL to files in this directory */ 1698 void *pHeap = 0; /* Heap for use by SQLite */ 1699 int ossFuzz = 0; /* enable OSS-FUZZ testing */ 1700 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */ 1701 int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */ 1702 sqlite3_vfs *pDfltVfs; /* The default VFS */ 1703 int openFlags4Data; /* Flags for sqlite3_open_v2() */ 1704 int bTimer = 0; /* Show elapse time for each test */ 1705 int nV; /* How much to increase verbosity with -vvvv */ 1706 sqlite3_int64 tmStart; /* Start of each test */ 1707 1708 sqlite3_config(SQLITE_CONFIG_URI,1); 1709 registerOomSimulator(); 1710 sqlite3_initialize(); 1711 iBegin = timeOfDay(); 1712 #ifdef __unix__ 1713 signal(SIGALRM, signalHandler); 1714 signal(SIGSEGV, signalHandler); 1715 signal(SIGABRT, signalHandler); 1716 #endif 1717 g.zArgv0 = argv[0]; 1718 openFlags4Data = SQLITE_OPEN_READONLY; 1719 zFailCode = getenv("TEST_FAILURE"); 1720 pDfltVfs = sqlite3_vfs_find(0); 1721 inmemVfsRegister(1); 1722 for(i=1; i<argc; i++){ 1723 const char *z = argv[i]; 1724 if( z[0]=='-' ){ 1725 z++; 1726 if( z[0]=='-' ) z++; 1727 if( strcmp(z,"cell-size-check")==0 ){ 1728 cellSzCkFlag = 1; 1729 }else 1730 if( strcmp(z,"dbid")==0 ){ 1731 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1732 onlyDbid = integerValue(argv[++i]); 1733 }else 1734 if( strcmp(z,"export-db")==0 ){ 1735 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1736 zExpDb = argv[++i]; 1737 }else 1738 if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){ 1739 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1740 zExpSql = argv[++i]; 1741 }else 1742 if( strcmp(z,"help")==0 ){ 1743 showHelp(); 1744 return 0; 1745 }else 1746 if( strcmp(z,"info")==0 ){ 1747 infoFlag = 1; 1748 }else 1749 if( strcmp(z,"limit-depth")==0 ){ 1750 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1751 depthLimit = integerValue(argv[++i]); 1752 }else 1753 if( strcmp(z,"limit-heap")==0 ){ 1754 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1755 heapLimit = integerValue(argv[++i]); 1756 }else 1757 if( strcmp(z,"limit-mem")==0 ){ 1758 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1759 nMem = integerValue(argv[++i]); 1760 }else 1761 if( strcmp(z,"limit-vdbe")==0 ){ 1762 vdbeLimitFlag = 1; 1763 }else 1764 if( strcmp(z,"load-sql")==0 ){ 1765 zInsSql = "INSERT INTO xsql(sqltext)" 1766 "VALUES(CAST(readtextfile(?1) AS text))"; 1767 iFirstInsArg = i+1; 1768 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 1769 break; 1770 }else 1771 if( strcmp(z,"load-db")==0 ){ 1772 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; 1773 iFirstInsArg = i+1; 1774 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 1775 break; 1776 }else 1777 if( strcmp(z,"load-dbsql")==0 ){ 1778 zInsSql = "INSERT INTO xsql(sqltext)" 1779 "VALUES(readfile(?1))"; 1780 iFirstInsArg = i+1; 1781 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 1782 dbSqlOnly = 1; 1783 break; 1784 }else 1785 if( strcmp(z,"m")==0 ){ 1786 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1787 zMsg = argv[++i]; 1788 openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; 1789 }else 1790 if( strcmp(z,"native-malloc")==0 ){ 1791 nativeMalloc = 1; 1792 }else 1793 if( strcmp(z,"native-vfs")==0 ){ 1794 nativeFlag = 1; 1795 }else 1796 if( strcmp(z,"oss-fuzz")==0 ){ 1797 ossFuzz = 1; 1798 }else 1799 if( strcmp(z,"prng-seed")==0 ){ 1800 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1801 g.uRandom = atoi(argv[++i]); 1802 }else 1803 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ 1804 quietFlag = 1; 1805 verboseFlag = 0; 1806 eVerbosity = 0; 1807 }else 1808 if( strcmp(z,"query-invariants")==0 ){ 1809 g.doInvariantChecks = 1; 1810 }else 1811 if( strcmp(z,"rebuild")==0 ){ 1812 rebuildFlag = 1; 1813 openFlags4Data = SQLITE_OPEN_READWRITE; 1814 }else 1815 if( strcmp(z,"result-trace")==0 ){ 1816 runFlags |= SQL_OUTPUT; 1817 }else 1818 if( strcmp(z,"script")==0 ){ 1819 bScript = 1; 1820 }else 1821 if( strcmp(z,"skip")==0 ){ 1822 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1823 nSkip = atoi(argv[++i]); 1824 }else 1825 if( strcmp(z,"spinner")==0 ){ 1826 bSpinner = 1; 1827 }else 1828 if( strcmp(z,"timer")==0 ){ 1829 bTimer = 1; 1830 }else 1831 if( strcmp(z,"sqlid")==0 ){ 1832 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1833 onlySqlid = integerValue(argv[++i]); 1834 }else 1835 if( strcmp(z,"timeout")==0 ){ 1836 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); 1837 iTimeout = integerValue(argv[++i]); 1838 }else 1839 if( strcmp(z,"timeout-test")==0 ){ 1840 timeoutTest = 1; 1841 #ifndef __unix__ 1842 fatalError("timeout is not available on non-unix systems"); 1843 #endif 1844 }else 1845 if( strcmp(z,"vdbe-debug")==0 ){ 1846 bVdbeDebug = 1; 1847 }else 1848 if( strcmp(z,"verbose")==0 ){ 1849 quietFlag = 0; 1850 verboseFlag++; 1851 eVerbosity++; 1852 if( verboseFlag>1 ) runFlags |= SQL_TRACE; 1853 }else 1854 if( (nV = numberOfVChar(z))>=1 ){ 1855 quietFlag = 0; 1856 verboseFlag += nV; 1857 eVerbosity += nV; 1858 if( verboseFlag>1 ) runFlags |= SQL_TRACE; 1859 }else 1860 if( strcmp(z,"version")==0 ){ 1861 int ii; 1862 const char *zz; 1863 printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid()); 1864 for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){ 1865 printf("%s\n", zz); 1866 } 1867 return 0; 1868 }else 1869 if( strcmp(z,"is-dbsql")==0 ){ 1870 i++; 1871 for(i++; i<argc; i++){ 1872 long nData; 1873 char *aData = readFile(argv[i], &nData); 1874 printf("%d %s\n", isDbSql((unsigned char*)aData,nData), argv[i]); 1875 sqlite3_free(aData); 1876 } 1877 exit(0); 1878 }else 1879 { 1880 fatalError("unknown option: %s", argv[i]); 1881 } 1882 }else{ 1883 nSrcDb++; 1884 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0])); 1885 azSrcDb[nSrcDb-1] = argv[i]; 1886 } 1887 } 1888 if( nSrcDb==0 ) fatalError("no source database specified"); 1889 if( nSrcDb>1 ){ 1890 if( zMsg ){ 1891 fatalError("cannot change the description of more than one database"); 1892 } 1893 if( zInsSql ){ 1894 fatalError("cannot import into more than one database"); 1895 } 1896 } 1897 1898 /* Process each source database separately */ 1899 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ 1900 char *zRawData = 0; 1901 long nRawData = 0; 1902 g.zDbFile = azSrcDb[iSrcDb]; 1903 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db, 1904 openFlags4Data, pDfltVfs->zName); 1905 if( rc==SQLITE_OK ){ 1906 rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_schema", 0, 0, 0); 1907 } 1908 if( rc ){ 1909 sqlite3_close(db); 1910 zRawData = readFile(azSrcDb[iSrcDb], &nRawData); 1911 if( zRawData==0 ){ 1912 fatalError("input file \"%s\" is not recognized\n", azSrcDb[iSrcDb]); 1913 } 1914 sqlite3_open(":memory:", &db); 1915 } 1916 1917 /* Print the description, if there is one */ 1918 if( infoFlag ){ 1919 int n; 1920 zDbName = azSrcDb[iSrcDb]; 1921 i = (int)strlen(zDbName) - 1; 1922 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } 1923 zDbName += i; 1924 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); 1925 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 1926 printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0)); 1927 }else{ 1928 printf("%s: (empty \"readme\")", zDbName); 1929 } 1930 sqlite3_finalize(pStmt); 1931 sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0); 1932 if( pStmt 1933 && sqlite3_step(pStmt)==SQLITE_ROW 1934 && (n = sqlite3_column_int(pStmt,0))>0 1935 ){ 1936 printf(" - %d DBs", n); 1937 } 1938 sqlite3_finalize(pStmt); 1939 sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0); 1940 if( pStmt 1941 && sqlite3_step(pStmt)==SQLITE_ROW 1942 && (n = sqlite3_column_int(pStmt,0))>0 1943 ){ 1944 printf(" - %d scripts", n); 1945 } 1946 sqlite3_finalize(pStmt); 1947 printf("\n"); 1948 sqlite3_close(db); 1949 sqlite3_free(zRawData); 1950 continue; 1951 } 1952 1953 rc = sqlite3_exec(db, 1954 "CREATE TABLE IF NOT EXISTS db(\n" 1955 " dbid INTEGER PRIMARY KEY, -- database id\n" 1956 " dbcontent BLOB -- database disk file image\n" 1957 ");\n" 1958 "CREATE TABLE IF NOT EXISTS xsql(\n" 1959 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" 1960 " sqltext TEXT -- Text of SQL statements to run\n" 1961 ");" 1962 "CREATE TABLE IF NOT EXISTS readme(\n" 1963 " msg TEXT -- Human-readable description of this file\n" 1964 ");", 0, 0, 0); 1965 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); 1966 if( zMsg ){ 1967 char *zSql; 1968 zSql = sqlite3_mprintf( 1969 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg); 1970 rc = sqlite3_exec(db, zSql, 0, 0, 0); 1971 sqlite3_free(zSql); 1972 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); 1973 } 1974 if( zRawData ){ 1975 zInsSql = "INSERT INTO xsql(sqltext) VALUES(?1)"; 1976 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); 1977 if( rc ) fatalError("cannot prepare statement [%s]: %s", 1978 zInsSql, sqlite3_errmsg(db)); 1979 sqlite3_bind_text(pStmt, 1, zRawData, nRawData, SQLITE_STATIC); 1980 sqlite3_step(pStmt); 1981 rc = sqlite3_reset(pStmt); 1982 if( rc ) fatalError("insert failed for %s", argv[i]); 1983 sqlite3_finalize(pStmt); 1984 rebuild_database(db, dbSqlOnly); 1985 zInsSql = 0; 1986 sqlite3_free(zRawData); 1987 zRawData = 0; 1988 } 1989 ossFuzzThisDb = ossFuzz; 1990 1991 /* If the CONFIG(name,value) table exists, read db-specific settings 1992 ** from that table */ 1993 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){ 1994 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config", 1995 -1, &pStmt, 0); 1996 if( rc ) fatalError("cannot prepare query of CONFIG table: %s", 1997 sqlite3_errmsg(db)); 1998 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 1999 const char *zName = (const char *)sqlite3_column_text(pStmt,0); 2000 if( zName==0 ) continue; 2001 if( strcmp(zName, "oss-fuzz")==0 ){ 2002 ossFuzzThisDb = sqlite3_column_int(pStmt,1); 2003 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb); 2004 } 2005 if( strcmp(zName, "limit-mem")==0 ){ 2006 nMemThisDb = sqlite3_column_int(pStmt,1); 2007 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb); 2008 } 2009 } 2010 sqlite3_finalize(pStmt); 2011 } 2012 2013 if( zInsSql ){ 2014 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, 2015 readfileFunc, 0, 0); 2016 sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0, 2017 readtextfileFunc, 0, 0); 2018 sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0, 2019 isDbSqlFunc, 0, 0); 2020 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); 2021 if( rc ) fatalError("cannot prepare statement [%s]: %s", 2022 zInsSql, sqlite3_errmsg(db)); 2023 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); 2024 if( rc ) fatalError("cannot start a transaction"); 2025 for(i=iFirstInsArg; i<argc; i++){ 2026 if( strcmp(argv[i],"-")==0 ){ 2027 /* A filename of "-" means read multiple filenames from stdin */ 2028 char zLine[2000]; 2029 while( rc==0 && fgets(zLine,sizeof(zLine),stdin)!=0 ){ 2030 size_t kk = strlen(zLine); 2031 while( kk>0 && zLine[kk-1]<=' ' ) kk--; 2032 sqlite3_bind_text(pStmt, 1, zLine, (int)kk, SQLITE_STATIC); 2033 if( verboseFlag ) printf("loading %.*s\n", (int)kk, zLine); 2034 sqlite3_step(pStmt); 2035 rc = sqlite3_reset(pStmt); 2036 if( rc ) fatalError("insert failed for %s", zLine); 2037 } 2038 }else{ 2039 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); 2040 if( verboseFlag ) printf("loading %s\n", argv[i]); 2041 sqlite3_step(pStmt); 2042 rc = sqlite3_reset(pStmt); 2043 if( rc ) fatalError("insert failed for %s", argv[i]); 2044 } 2045 } 2046 sqlite3_finalize(pStmt); 2047 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0); 2048 if( rc ) fatalError("cannot commit the transaction: %s", 2049 sqlite3_errmsg(db)); 2050 rebuild_database(db, dbSqlOnly); 2051 sqlite3_close(db); 2052 return 0; 2053 } 2054 rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0); 2055 if( rc ) fatalError("cannot set database to query-only"); 2056 if( zExpDb!=0 || zExpSql!=0 ){ 2057 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, 2058 writefileFunc, 0, 0); 2059 if( zExpDb!=0 ){ 2060 const char *zExDb = 2061 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent)," 2062 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)" 2063 " FROM db WHERE ?2<0 OR dbid=?2;"; 2064 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0); 2065 if( rc ) fatalError("cannot prepare statement [%s]: %s", 2066 zExDb, sqlite3_errmsg(db)); 2067 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb), 2068 SQLITE_STATIC, SQLITE_UTF8); 2069 sqlite3_bind_int(pStmt, 2, onlyDbid); 2070 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 2071 printf("write db-%d (%d bytes) into %s\n", 2072 sqlite3_column_int(pStmt,1), 2073 sqlite3_column_int(pStmt,3), 2074 sqlite3_column_text(pStmt,2)); 2075 } 2076 sqlite3_finalize(pStmt); 2077 } 2078 if( zExpSql!=0 ){ 2079 const char *zExSql = 2080 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext)," 2081 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)" 2082 " FROM xsql WHERE ?2<0 OR sqlid=?2;"; 2083 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0); 2084 if( rc ) fatalError("cannot prepare statement [%s]: %s", 2085 zExSql, sqlite3_errmsg(db)); 2086 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql), 2087 SQLITE_STATIC, SQLITE_UTF8); 2088 sqlite3_bind_int(pStmt, 2, onlySqlid); 2089 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 2090 printf("write sql-%d (%d bytes) into %s\n", 2091 sqlite3_column_int(pStmt,1), 2092 sqlite3_column_int(pStmt,3), 2093 sqlite3_column_text(pStmt,2)); 2094 } 2095 sqlite3_finalize(pStmt); 2096 } 2097 sqlite3_close(db); 2098 return 0; 2099 } 2100 2101 /* Load all SQL script content and all initial database images from the 2102 ** source db 2103 */ 2104 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid, 2105 &g.nSql, &g.pFirstSql); 2106 if( g.nSql==0 ) fatalError("need at least one SQL script"); 2107 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid, 2108 &g.nDb, &g.pFirstDb); 2109 if( g.nDb==0 ){ 2110 g.pFirstDb = safe_realloc(0, sizeof(Blob)); 2111 memset(g.pFirstDb, 0, sizeof(Blob)); 2112 g.pFirstDb->id = 1; 2113 g.pFirstDb->seq = 0; 2114 g.nDb = 1; 2115 sqlFuzz = 1; 2116 } 2117 2118 /* Print the description, if there is one */ 2119 if( !quietFlag && !bScript ){ 2120 zDbName = azSrcDb[iSrcDb]; 2121 i = (int)strlen(zDbName) - 1; 2122 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } 2123 zDbName += i; 2124 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); 2125 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ 2126 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); 2127 } 2128 sqlite3_finalize(pStmt); 2129 } 2130 2131 /* Rebuild the database, if requested */ 2132 if( rebuildFlag ){ 2133 if( !quietFlag ){ 2134 printf("%s: rebuilding... ", zDbName); 2135 fflush(stdout); 2136 } 2137 rebuild_database(db, 0); 2138 if( !quietFlag ) printf("done\n"); 2139 } 2140 2141 /* Close the source database. Verify that no SQLite memory allocations are 2142 ** outstanding. 2143 */ 2144 sqlite3_close(db); 2145 if( sqlite3_memory_used()>0 ){ 2146 fatalError("SQLite has memory in use before the start of testing"); 2147 } 2148 2149 /* Limit available memory, if requested */ 2150 sqlite3_shutdown(); 2151 2152 if( nMemThisDb>0 && nMem==0 ){ 2153 if( !nativeMalloc ){ 2154 pHeap = realloc(pHeap, nMemThisDb); 2155 if( pHeap==0 ){ 2156 fatalError("failed to allocate %d bytes of heap memory", nMem); 2157 } 2158 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128); 2159 }else{ 2160 sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb); 2161 } 2162 }else{ 2163 sqlite3_hard_heap_limit64(0); 2164 } 2165 2166 /* Disable lookaside with the --native-malloc option */ 2167 if( nativeMalloc ){ 2168 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); 2169 } 2170 2171 /* Reset the in-memory virtual filesystem */ 2172 formatVfs(); 2173 2174 /* Run a test using each SQL script against each database. 2175 */ 2176 if( !verboseFlag && !quietFlag && !bSpinner && !bScript ){ 2177 printf("%s:", zDbName); 2178 } 2179 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ 2180 tmStart = timeOfDay(); 2181 if( isDbSql(pSql->a, pSql->sz) ){ 2182 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id); 2183 if( bScript ){ 2184 /* No progress output */ 2185 }else if( bSpinner ){ 2186 int nTotal =g.nSql; 2187 int idx = pSql->seq; 2188 printf("\r%s: %d/%d ", zDbName, idx, nTotal); 2189 fflush(stdout); 2190 }else if( verboseFlag ){ 2191 printf("%s\n", g.zTestName); 2192 fflush(stdout); 2193 }else if( !quietFlag ){ 2194 static int prevAmt = -1; 2195 int idx = pSql->seq; 2196 int amt = idx*10/(g.nSql); 2197 if( amt!=prevAmt ){ 2198 printf(" %d%%", amt*10); 2199 fflush(stdout); 2200 prevAmt = amt; 2201 } 2202 } 2203 if( nSkip>0 ){ 2204 nSkip--; 2205 }else{ 2206 runCombinedDbSqlInput(pSql->a, pSql->sz, iTimeout, bScript, pSql->id); 2207 } 2208 nTest++; 2209 if( bTimer && !bScript ){ 2210 sqlite3_int64 tmEnd = timeOfDay(); 2211 printf("%lld %s\n", tmEnd - tmStart, g.zTestName); 2212 } 2213 g.zTestName[0] = 0; 2214 disableOom(); 2215 continue; 2216 } 2217 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ 2218 int openFlags; 2219 const char *zVfs = "inmem"; 2220 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", 2221 pSql->id, pDb->id); 2222 if( bScript ){ 2223 /* No progress output */ 2224 }else if( bSpinner ){ 2225 int nTotal = g.nDb*g.nSql; 2226 int idx = pSql->seq*g.nDb + pDb->id - 1; 2227 printf("\r%s: %d/%d ", zDbName, idx, nTotal); 2228 fflush(stdout); 2229 }else if( verboseFlag ){ 2230 printf("%s\n", g.zTestName); 2231 fflush(stdout); 2232 }else if( !quietFlag ){ 2233 static int prevAmt = -1; 2234 int idx = pSql->seq*g.nDb + pDb->id - 1; 2235 int amt = idx*10/(g.nDb*g.nSql); 2236 if( amt!=prevAmt ){ 2237 printf(" %d%%", amt*10); 2238 fflush(stdout); 2239 prevAmt = amt; 2240 } 2241 } 2242 if( nSkip>0 ){ 2243 nSkip--; 2244 continue; 2245 } 2246 if( bScript ){ 2247 char zName[100]; 2248 sqlite3_snprintf(sizeof(zName), zName, "db%06d.db", 2249 pDb->id>1 ? pDb->id : pSql->id); 2250 renderDbSqlForCLI(stdout, zName, 2251 pDb->a, pDb->sz, pSql->a, pSql->sz); 2252 continue; 2253 } 2254 createVFile("main.db", pDb->sz, pDb->a); 2255 sqlite3_randomness(0,0); 2256 if( ossFuzzThisDb ){ 2257 #ifndef SQLITE_OSS_FUZZ 2258 fatalError("--oss-fuzz not supported: recompile" 2259 " with -DSQLITE_OSS_FUZZ"); 2260 #else 2261 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t); 2262 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz); 2263 #endif 2264 }else{ 2265 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; 2266 if( nativeFlag && pDb->sz==0 ){ 2267 openFlags |= SQLITE_OPEN_MEMORY; 2268 zVfs = 0; 2269 } 2270 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); 2271 if( rc ) fatalError("cannot open inmem database"); 2272 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000); 2273 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50); 2274 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); 2275 setAlarm((iTimeout+999)/1000); 2276 /* Enable test functions */ 2277 sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db); 2278 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 2279 if( sqlFuzz || vdbeLimitFlag ){ 2280 sqlite3_progress_handler(db, 100000, progressHandler, 2281 &vdbeLimitFlag); 2282 } 2283 #endif 2284 #ifdef SQLITE_TESTCTRL_PRNG_SEED 2285 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db); 2286 #endif 2287 if( bVdbeDebug ){ 2288 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); 2289 } 2290 do{ 2291 runSql(db, (char*)pSql->a, runFlags); 2292 }while( timeoutTest ); 2293 setAlarm(0); 2294 sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0); 2295 sqlite3_close(db); 2296 } 2297 if( sqlite3_memory_used()>0 ){ 2298 fatalError("memory leak: %lld bytes outstanding", 2299 sqlite3_memory_used()); 2300 } 2301 reformatVfs(); 2302 nTest++; 2303 if( bTimer ){ 2304 sqlite3_int64 tmEnd = timeOfDay(); 2305 printf("%lld %s\n", tmEnd - tmStart, g.zTestName); 2306 } 2307 g.zTestName[0] = 0; 2308 2309 /* Simulate an error if the TEST_FAILURE environment variable is "5". 2310 ** This is used to verify that automated test script really do spot 2311 ** errors that occur in this test program. 2312 */ 2313 if( zFailCode ){ 2314 if( zFailCode[0]=='5' && zFailCode[1]==0 ){ 2315 fatalError("simulated failure"); 2316 }else if( zFailCode[0]!=0 ){ 2317 /* If TEST_FAILURE is something other than 5, just exit the test 2318 ** early */ 2319 printf("\nExit early due to TEST_FAILURE being set\n"); 2320 iSrcDb = nSrcDb-1; 2321 goto sourcedb_cleanup; 2322 } 2323 } 2324 } 2325 } 2326 if( bScript ){ 2327 /* No progress output */ 2328 }else if( bSpinner ){ 2329 int nTotal = g.nDb*g.nSql; 2330 printf("\r%s: %d/%d \n", zDbName, nTotal, nTotal); 2331 }else if( !quietFlag && !verboseFlag ){ 2332 printf(" 100%% - %d tests\n", g.nDb*g.nSql); 2333 } 2334 2335 /* Clean up at the end of processing a single source database 2336 */ 2337 sourcedb_cleanup: 2338 blobListFree(g.pFirstSql); 2339 blobListFree(g.pFirstDb); 2340 reformatVfs(); 2341 2342 } /* End loop over all source databases */ 2343 2344 if( !quietFlag && !bScript ){ 2345 sqlite3_int64 iElapse = timeOfDay() - iBegin; 2346 if( g.nInvariant ){ 2347 printf("fuzzcheck: %u query invariants checked\n", g.nInvariant); 2348 } 2349 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" 2350 "SQLite %s %s\n", 2351 nTest, (int)(iElapse/1000), (int)(iElapse%1000), 2352 sqlite3_libversion(), sqlite3_sourceid()); 2353 } 2354 free(azSrcDb); 2355 free(pHeap); 2356 return 0; 2357 } 2358