drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame^] | 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 |
| 14 | ** on 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 | ** |
| 29 | ** For each database file in the DB table, the SQL text in the XSQL table |
| 30 | ** is run against that database. This program is looking for crashes, |
| 31 | ** assertion faults, and/or memory leaks. No attempt is made to verify |
| 32 | ** the output. The assumption is that either all of the database files |
| 33 | ** or all of the SQL statements are malformed inputs, generated by a fuzzer, |
| 34 | ** that need to be checked to make sure they do not present a security risk. |
| 35 | ** |
| 36 | ** This program also includes some command-line options to help with |
| 37 | ** creation and maintenance of the source content database. |
| 38 | */ |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <stdarg.h> |
| 43 | #include <ctype.h> |
| 44 | #include "sqlite3.h" |
| 45 | |
| 46 | /* |
| 47 | ** Files in the virtual file system. |
| 48 | */ |
| 49 | typedef struct VFile VFile; |
| 50 | struct VFile { |
| 51 | char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ |
| 52 | int sz; /* Size of the file in bytes */ |
| 53 | int nRef; /* Number of references to this file */ |
| 54 | unsigned char *a; /* Content of the file. From malloc() */ |
| 55 | }; |
| 56 | typedef struct VHandle VHandle; |
| 57 | struct VHandle { |
| 58 | sqlite3_file base; /* Base class. Must be first */ |
| 59 | VFile *pVFile; /* The underlying file */ |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | ** The value of a database file template, or of an SQL script |
| 64 | */ |
| 65 | typedef struct Blob Blob; |
| 66 | struct Blob { |
| 67 | Blob *pNext; /* Next in a list */ |
| 68 | int id; /* Id of this Blob */ |
| 69 | int sz; /* Size of this Blob in bytes */ |
| 70 | unsigned char a[1]; /* Blob content. Extra space allocated as needed. */ |
| 71 | }; |
| 72 | |
| 73 | /* |
| 74 | ** Maximum number of files in the in-memory virtual filesystem. |
| 75 | */ |
| 76 | #define MX_FILE 10 |
| 77 | |
| 78 | /* |
| 79 | ** Maximum allowed file size |
| 80 | */ |
| 81 | #define MX_FILE_SZ 10000000 |
| 82 | |
| 83 | /* |
| 84 | ** All global variables are gathered into the "g" singleton. |
| 85 | */ |
| 86 | static struct GlobalVars { |
| 87 | const char *zArgv0; /* Name of program */ |
| 88 | VFile aFile[MX_FILE]; /* The virtual filesystem */ |
| 89 | int nDb; /* Number of template databases */ |
| 90 | Blob *pFirstDb; /* Content of first template database */ |
| 91 | int nSql; /* Number of SQL scripts */ |
| 92 | Blob *pFirstSql; /* First SQL script */ |
| 93 | char zTestName[100]; /* Name of current test */ |
| 94 | } g; |
| 95 | |
| 96 | /* |
| 97 | ** Print an error message and quit. |
| 98 | */ |
| 99 | static void fatalError(const char *zFormat, ...){ |
| 100 | va_list ap; |
| 101 | if( g.zTestName[0] ){ |
| 102 | fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); |
| 103 | }else{ |
| 104 | fprintf(stderr, "%s: ", g.zArgv0); |
| 105 | } |
| 106 | va_start(ap, zFormat); |
| 107 | vfprintf(stderr, zFormat, ap); |
| 108 | va_end(ap); |
| 109 | fprintf(stderr, "\n"); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | ** Reallocate memory. Show and error and quit if unable. |
| 115 | */ |
| 116 | static void *safe_realloc(void *pOld, int szNew){ |
| 117 | void *pNew = realloc(pOld, szNew); |
| 118 | if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); |
| 119 | return pNew; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | ** Initialize the virtual file system. |
| 124 | */ |
| 125 | static void formatVfs(void){ |
| 126 | int i; |
| 127 | for(i=0; i<MX_FILE; i++){ |
| 128 | g.aFile[i].sz = -1; |
| 129 | g.aFile[i].zFilename = 0; |
| 130 | g.aFile[i].a = 0; |
| 131 | g.aFile[i].nRef = 0; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /* |
| 137 | ** Erase all information in the virtual file system. |
| 138 | */ |
| 139 | static void reformatVfs(void){ |
| 140 | int i; |
| 141 | for(i=0; i<MX_FILE; i++){ |
| 142 | if( g.aFile[i].sz<0 ) continue; |
| 143 | if( g.aFile[i].zFilename ){ |
| 144 | free(g.aFile[i].zFilename); |
| 145 | g.aFile[i].zFilename = 0; |
| 146 | } |
| 147 | if( g.aFile[i].nRef>0 ){ |
| 148 | fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef); |
| 149 | } |
| 150 | g.aFile[i].sz = -1; |
| 151 | free(g.aFile[i].a); |
| 152 | g.aFile[i].a = 0; |
| 153 | g.aFile[i].nRef = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | ** Find a VFile by name |
| 159 | */ |
| 160 | static VFile *findVFile(const char *zName){ |
| 161 | int i; |
| 162 | for(i=0; i<MX_FILE; i++){ |
| 163 | if( g.aFile[i].zFilename==0 ) continue; |
| 164 | if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i]; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | ** Find a VFile by name. Create it if it does not already exist and |
| 171 | ** initialize it to the size and content given. |
| 172 | ** |
| 173 | ** Return NULL only if the filesystem is full. |
| 174 | */ |
| 175 | static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ |
| 176 | VFile *pNew = findVFile(zName); |
| 177 | int i; |
| 178 | if( pNew ) return pNew; |
| 179 | for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} |
| 180 | if( i>=MX_FILE ) return 0; |
| 181 | pNew = &g.aFile[i]; |
| 182 | pNew->zFilename = safe_realloc(0, strlen(zName)+1); |
| 183 | memcpy(pNew->zFilename, zName, strlen(zName)+1); |
| 184 | pNew->nRef = 0; |
| 185 | pNew->sz = sz; |
| 186 | pNew->a = safe_realloc(0, sz); |
| 187 | if( sz>0 ) memcpy(pNew->a, pData, sz); |
| 188 | return pNew; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 194 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 195 | ** if the file does not exist or is unreadable. |
| 196 | */ |
| 197 | static void readfileFunc( |
| 198 | sqlite3_context *context, |
| 199 | int argc, |
| 200 | sqlite3_value **argv |
| 201 | ){ |
| 202 | const char *zName; |
| 203 | FILE *in; |
| 204 | long nIn; |
| 205 | void *pBuf; |
| 206 | |
| 207 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 208 | if( zName==0 ) return; |
| 209 | in = fopen(zName, "rb"); |
| 210 | if( in==0 ) return; |
| 211 | fseek(in, 0, SEEK_END); |
| 212 | nIn = ftell(in); |
| 213 | rewind(in); |
| 214 | pBuf = sqlite3_malloc64( nIn ); |
| 215 | if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
| 216 | sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); |
| 217 | }else{ |
| 218 | sqlite3_free(pBuf); |
| 219 | } |
| 220 | fclose(in); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | ** Print sketchy documentation for this utility program |
| 225 | */ |
| 226 | static void showHelp(void){ |
| 227 | printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); |
| 228 | printf( |
| 229 | "Read databases and SQL scripts from SOURCE-DB and execute each script against\n" |
| 230 | "each database, checking for crashes and memory leaks.\n" |
| 231 | "Options:\n" |
| 232 | " --help Show this help text\n" |
| 233 | " -q Reduced output\n" |
| 234 | " --quiet Reduced output\n" |
| 235 | " --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n" |
| 236 | " --load-db ARGS... Load template databases from files into SOURCE_DB\n" |
| 237 | " -v Increased output\n" |
| 238 | " --verbose Increased output\n" |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | ** Load a list of Blob objects from the database |
| 244 | */ |
| 245 | static void blobListLoadFromDb( |
| 246 | sqlite3 *db, /* Read from this database */ |
| 247 | const char *zSql, /* Query used to extract the blobs */ |
| 248 | int *pN, /* OUT: Write number of blobs loaded here */ |
| 249 | Blob **ppList /* OUT: Write the head of the blob list here */ |
| 250 | ){ |
| 251 | Blob head; |
| 252 | Blob *p; |
| 253 | sqlite3_stmt *pStmt; |
| 254 | int n = 0; |
| 255 | int rc; |
| 256 | |
| 257 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 258 | if( rc ) fatalError("%s", sqlite3_errmsg(db)); |
| 259 | head.pNext = 0; |
| 260 | p = &head; |
| 261 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 262 | int sz = sqlite3_column_bytes(pStmt, 1); |
| 263 | Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz ); |
| 264 | pNew->id = sqlite3_column_int(pStmt, 0); |
| 265 | pNew->sz = sz; |
| 266 | pNew->pNext = 0; |
| 267 | memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz); |
| 268 | pNew->a[sz] = 0; |
| 269 | p->pNext = pNew; |
| 270 | p = pNew; |
| 271 | n++; |
| 272 | } |
| 273 | sqlite3_finalize(pStmt); |
| 274 | *pN = n; |
| 275 | *ppList = head.pNext; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | ** Free a list of Blob objects |
| 280 | */ |
| 281 | static void blobListFree(Blob *p){ |
| 282 | Blob *pNext; |
| 283 | while( p ){ |
| 284 | pNext = p->pNext; |
| 285 | free(p); |
| 286 | p = pNext; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /* Return the current wall-clock time */ |
| 292 | static sqlite3_int64 timeOfDay(void){ |
| 293 | static sqlite3_vfs *clockVfs = 0; |
| 294 | sqlite3_int64 t; |
| 295 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 296 | if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 297 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 298 | }else{ |
| 299 | double r; |
| 300 | clockVfs->xCurrentTime(clockVfs, &r); |
| 301 | t = (sqlite3_int64)(r*86400000.0); |
| 302 | } |
| 303 | return t; |
| 304 | } |
| 305 | |
| 306 | /* Methods for the VHandle object |
| 307 | */ |
| 308 | static int inmemClose(sqlite3_file *pFile){ |
| 309 | VHandle *p = (VHandle*)pFile; |
| 310 | VFile *pVFile = p->pVFile; |
| 311 | pVFile->nRef--; |
| 312 | if( pVFile->nRef==0 && pVFile->zFilename==0 ){ |
| 313 | pVFile->sz = -1; |
| 314 | free(pVFile->a); |
| 315 | pVFile->a = 0; |
| 316 | } |
| 317 | return SQLITE_OK; |
| 318 | } |
| 319 | static int inmemRead( |
| 320 | sqlite3_file *pFile, /* Read from this open file */ |
| 321 | void *pData, /* Store content in this buffer */ |
| 322 | int iAmt, /* Bytes of content */ |
| 323 | sqlite3_int64 iOfst /* Start reading here */ |
| 324 | ){ |
| 325 | VHandle *pHandle = (VHandle*)pFile; |
| 326 | VFile *pVFile = pHandle->pVFile; |
| 327 | if( iOfst<0 || iOfst>=pVFile->sz ){ |
| 328 | memset(pData, 0, iAmt); |
| 329 | return SQLITE_IOERR_SHORT_READ; |
| 330 | } |
| 331 | if( iOfst+iAmt>pVFile->sz ){ |
| 332 | memset(pData, 0, iAmt); |
| 333 | iAmt = pVFile->sz - iOfst; |
| 334 | memcpy(pData, pVFile->a, iAmt); |
| 335 | return SQLITE_IOERR_SHORT_READ; |
| 336 | } |
| 337 | memcpy(pData, pVFile->a, iAmt); |
| 338 | return SQLITE_OK; |
| 339 | } |
| 340 | static int inmemWrite( |
| 341 | sqlite3_file *pFile, /* Write to this file */ |
| 342 | const void *pData, /* Content to write */ |
| 343 | int iAmt, /* bytes to write */ |
| 344 | sqlite3_int64 iOfst /* Start writing here */ |
| 345 | ){ |
| 346 | VHandle *pHandle = (VHandle*)pFile; |
| 347 | VFile *pVFile = pHandle->pVFile; |
| 348 | if( iOfst+iAmt > pVFile->sz ){ |
| 349 | if( iOfst+iAmt >= MX_FILE_SZ ) return SQLITE_FULL; |
| 350 | pVFile->a = safe_realloc(pVFile->a, iOfst+iAmt); |
| 351 | memset(pVFile->a + pVFile->sz, 0, iOfst - pVFile->sz); |
| 352 | pVFile->sz = iOfst + iAmt; |
| 353 | } |
| 354 | memcpy(pVFile->a + iOfst, pData, iAmt); |
| 355 | return SQLITE_OK; |
| 356 | } |
| 357 | static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){ |
| 358 | VHandle *pHandle = (VHandle*)pFile; |
| 359 | VFile *pVFile = pHandle->pVFile; |
| 360 | if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = iSize; |
| 361 | return SQLITE_OK; |
| 362 | } |
| 363 | static int inmemSync(sqlite3_file *pFile, int flags){ |
| 364 | return SQLITE_OK; |
| 365 | } |
| 366 | static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ |
| 367 | *pSize = ((VHandle*)pFile)->pVFile->sz; |
| 368 | return SQLITE_OK; |
| 369 | } |
| 370 | static int inmemLock(sqlite3_file *pFile, int type){ |
| 371 | return SQLITE_OK; |
| 372 | } |
| 373 | static int inmemUnlock(sqlite3_file *pFile, int type){ |
| 374 | return SQLITE_OK; |
| 375 | } |
| 376 | static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){ |
| 377 | *pOut = 0; |
| 378 | return SQLITE_OK; |
| 379 | } |
| 380 | static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 381 | return SQLITE_NOTFOUND; |
| 382 | } |
| 383 | static int inmemSectorSize(sqlite3_file *pFile){ |
| 384 | return 512; |
| 385 | } |
| 386 | static int inmemDeviceCharacteristics(sqlite3_file *pFile){ |
| 387 | return |
| 388 | SQLITE_IOCAP_SAFE_APPEND | |
| 389 | SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 390 | SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /* Method table for VHandle |
| 395 | */ |
| 396 | static sqlite3_io_methods VHandleMethods = { |
| 397 | /* iVersion */ 1, |
| 398 | /* xClose */ inmemClose, |
| 399 | /* xRead */ inmemRead, |
| 400 | /* xWrite */ inmemWrite, |
| 401 | /* xTruncate */ inmemTruncate, |
| 402 | /* xSync */ inmemSync, |
| 403 | /* xFileSize */ inmemFileSize, |
| 404 | /* xLock */ inmemLock, |
| 405 | /* xUnlock */ inmemUnlock, |
| 406 | /* xCheck... */ inmemCheckReservedLock, |
| 407 | /* xFileCtrl */ inmemFileControl, |
| 408 | /* xSectorSz */ inmemSectorSize, |
| 409 | /* xDevchar */ inmemDeviceCharacteristics, |
| 410 | /* xShmMap */ 0, |
| 411 | /* xShmLock */ 0, |
| 412 | /* xShmBarrier */ 0, |
| 413 | /* xShmUnmap */ 0, |
| 414 | /* xFetch */ 0, |
| 415 | /* xUnfetch */ 0 |
| 416 | }; |
| 417 | |
| 418 | /* |
| 419 | ** Open a new file in the inmem VFS. All files are anonymous and are |
| 420 | ** delete-on-close. |
| 421 | */ |
| 422 | static int inmemOpen( |
| 423 | sqlite3_vfs *pVfs, |
| 424 | const char *zFilename, |
| 425 | sqlite3_file *pFile, |
| 426 | int openFlags, |
| 427 | int *pOutFlags |
| 428 | ){ |
| 429 | VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)""); |
| 430 | VHandle *pHandle = (VHandle*)pFile; |
| 431 | if( pVFile==0 ) return SQLITE_FULL; |
| 432 | pHandle->pVFile = pVFile; |
| 433 | pVFile->nRef++; |
| 434 | pFile->pMethods = &VHandleMethods; |
| 435 | if( pOutFlags ) *pOutFlags = openFlags; |
| 436 | return SQLITE_OK; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | ** Delete a file by name |
| 441 | */ |
| 442 | static int inmemDelete( |
| 443 | sqlite3_vfs *pVfs, |
| 444 | const char *zFilename, |
| 445 | int syncdir |
| 446 | ){ |
| 447 | VFile *pVFile = findVFile(zFilename); |
| 448 | if( pVFile==0 ) return SQLITE_OK; |
| 449 | if( pVFile->nRef==0 ){ |
| 450 | free(pVFile->zFilename); |
| 451 | pVFile->zFilename = 0; |
| 452 | pVFile->sz = -1; |
| 453 | free(pVFile->a); |
| 454 | pVFile->a = 0; |
| 455 | return SQLITE_OK; |
| 456 | } |
| 457 | return SQLITE_IOERR_DELETE; |
| 458 | } |
| 459 | |
| 460 | /* Check for the existance of a file |
| 461 | */ |
| 462 | static int inmemAccess( |
| 463 | sqlite3_vfs *pVfs, |
| 464 | const char *zFilename, |
| 465 | int flags, |
| 466 | int *pResOut |
| 467 | ){ |
| 468 | VFile *pVFile = findVFile(zFilename); |
| 469 | *pResOut = pVFile!=0; |
| 470 | return SQLITE_OK; |
| 471 | } |
| 472 | |
| 473 | /* Get the canonical pathname for a file |
| 474 | */ |
| 475 | static int inmemFullPathname( |
| 476 | sqlite3_vfs *pVfs, |
| 477 | const char *zFilename, |
| 478 | int nOut, |
| 479 | char *zOut |
| 480 | ){ |
| 481 | sqlite3_snprintf(nOut, zOut, "%s", zFilename); |
| 482 | return SQLITE_OK; |
| 483 | } |
| 484 | |
| 485 | /* GetLastError() is never used */ |
| 486 | static int inmemGetLastError(sqlite3_vfs *pVfs, int n, char *z){ |
| 487 | return SQLITE_OK; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | ** Register the VFS that reads from the g.aFile[] set of files. |
| 492 | */ |
| 493 | static void inmemVfsRegister(void){ |
| 494 | static sqlite3_vfs inmemVfs; |
| 495 | sqlite3_vfs *pDefault = sqlite3_vfs_find(0); |
| 496 | inmemVfs.iVersion = 1; |
| 497 | inmemVfs.szOsFile = sizeof(VHandle); |
| 498 | inmemVfs.mxPathname = 200; |
| 499 | inmemVfs.zName = "inmem"; |
| 500 | inmemVfs.xOpen = inmemOpen; |
| 501 | inmemVfs.xDelete = inmemDelete; |
| 502 | inmemVfs.xAccess = inmemAccess; |
| 503 | inmemVfs.xFullPathname = inmemFullPathname; |
| 504 | inmemVfs.xRandomness = pDefault->xRandomness; |
| 505 | inmemVfs.xSleep = pDefault->xSleep; |
| 506 | inmemVfs.xCurrentTime = pDefault->xCurrentTime; |
| 507 | inmemVfs.xGetLastError = inmemGetLastError; |
| 508 | sqlite3_vfs_register(&inmemVfs, 0); |
| 509 | }; |
| 510 | |
| 511 | #ifndef SQLITE_OMIT_TRACE |
| 512 | /* |
| 513 | ** This callback is invoked by sqlite3_trace() as each SQL statement |
| 514 | ** starts. |
| 515 | */ |
| 516 | static void traceCallback(void *NotUsed, const char *zMsg){ |
| 517 | printf("TRACE: %s\n", zMsg); |
| 518 | fflush(stdout); |
| 519 | } |
| 520 | static void traceNoop(void *NotUsed, const char *zMsg){ |
| 521 | return; |
| 522 | } |
| 523 | #endif |
| 524 | |
| 525 | /* |
| 526 | ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not |
| 527 | ** stop if an error is encountered. |
| 528 | */ |
| 529 | static void runSql(sqlite3 *db, const char *zSql){ |
| 530 | const char *zMore; |
| 531 | sqlite3_stmt *pStmt; |
| 532 | |
| 533 | while( zSql && zSql[0] ){ |
| 534 | zMore = 0; |
| 535 | pStmt = 0; |
| 536 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore); |
| 537 | zSql = zMore; |
| 538 | if( pStmt ){ |
| 539 | while( SQLITE_ROW==sqlite3_step(pStmt) ); |
| 540 | sqlite3_finalize(pStmt); |
| 541 | }else{ |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | int main(int argc, char **argv){ |
| 548 | sqlite3_int64 iBegin; /* Start time of this program */ |
| 549 | const char *zSourceDb = 0; /* Source database filename */ |
| 550 | int quietFlag = 0; /* True if --quiet or -q */ |
| 551 | int verboseFlag = 0; /* True if --verbose or -v */ |
| 552 | char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ |
| 553 | int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sql */ |
| 554 | sqlite3 *db = 0; /* The open database connection */ |
| 555 | int rc; /* Result code from SQLite interface calls */ |
| 556 | Blob *pSql; /* For looping over SQL scripts */ |
| 557 | Blob *pDb; /* For looping over template databases */ |
| 558 | int i; /* Loop index for the argv[] loop */ |
| 559 | |
| 560 | iBegin = timeOfDay(); |
| 561 | g.zArgv0 = argv[0]; |
| 562 | for(i=1; i<argc; i++){ |
| 563 | const char *z = argv[i]; |
| 564 | if( z[0]=='-' ){ |
| 565 | z++; |
| 566 | if( z[0]=='-' ) z++; |
| 567 | if( strcmp(z,"help")==0 ){ |
| 568 | showHelp(); |
| 569 | return 0; |
| 570 | }else |
| 571 | if( strcmp(z,"load-sql")==0 ){ |
| 572 | zInsSql = "INSERT INTO xsql(sqltext) VALUES(readfile(?1))"; |
| 573 | iFirstInsArg = i+1; |
| 574 | break; |
| 575 | }else |
| 576 | if( strcmp(z,"load-db")==0 ){ |
| 577 | zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; |
| 578 | iFirstInsArg = i+1; |
| 579 | break; |
| 580 | }else |
| 581 | if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
| 582 | quietFlag = 1; |
| 583 | verboseFlag = 0; |
| 584 | }else |
| 585 | if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ |
| 586 | quietFlag = 0; |
| 587 | verboseFlag = 1; |
| 588 | }else |
| 589 | { |
| 590 | fatalError("unknown option: %s", argv[i]); |
| 591 | } |
| 592 | }else{ |
| 593 | if( zSourceDb ) fatalError("extra argument: %s", argv[i]); |
| 594 | zSourceDb = argv[i]; |
| 595 | } |
| 596 | } |
| 597 | if( zSourceDb==0 ) fatalError("no source database specified"); |
| 598 | rc = sqlite3_open(zSourceDb, &db); |
| 599 | if( rc ){ |
| 600 | fatalError("cannot open source database %s - %s", |
| 601 | zSourceDb, sqlite3_errmsg(db)); |
| 602 | } |
| 603 | rc = sqlite3_exec(db, |
| 604 | "CREATE TABLE IF NOT EXISTS db(\n" |
| 605 | " dbid INTEGER PRIMARY KEY, -- database id\n" |
| 606 | " dbcontent BLOB -- database disk file image\n" |
| 607 | ");\n" |
| 608 | "CREATE TABLE IF NOT EXISTS xsql(\n" |
| 609 | " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" |
| 610 | " sqltext TEXT -- Text of SQL statements to run\n" |
| 611 | ");", 0, 0, 0); |
| 612 | if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); |
| 613 | if( zInsSql ){ |
| 614 | sqlite3_stmt *pStmt; |
| 615 | sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, |
| 616 | readfileFunc, 0, 0); |
| 617 | rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); |
| 618 | if( rc ) fatalError("cannot prepare statement [%s]: %s", |
| 619 | zInsSql, sqlite3_errmsg(db)); |
| 620 | rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); |
| 621 | if( rc ) fatalError("cannot start a transaction"); |
| 622 | for(i=iFirstInsArg; i<argc; i++){ |
| 623 | sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); |
| 624 | sqlite3_step(pStmt); |
| 625 | rc = sqlite3_reset(pStmt); |
| 626 | if( rc ) fatalError("insert failed for %s", argv[i]); |
| 627 | } |
| 628 | sqlite3_finalize(pStmt); |
| 629 | rc = sqlite3_exec(db, "COMMIT", 0, 0, 0); |
| 630 | if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db)); |
| 631 | sqlite3_close(db); |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* Load all SQL script content and all initial database images from the |
| 636 | ** source db |
| 637 | */ |
| 638 | blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", &g.nSql, &g.pFirstSql); |
| 639 | if( g.nSql==0 ) fatalError("need at least one SQL script"); |
| 640 | blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", &g.nDb, &g.pFirstDb); |
| 641 | if( g.nDb==0 ){ |
| 642 | g.pFirstDb = safe_realloc(0, sizeof(Blob)); |
| 643 | memset(g.pFirstDb, 0, sizeof(Blob)); |
| 644 | g.pFirstDb->id = 1; |
| 645 | g.nDb = 1; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | /* Close the source database. Verify that no SQLite memory allocations are |
| 650 | ** outstanding. |
| 651 | */ |
| 652 | sqlite3_close(db); |
| 653 | if( sqlite3_memory_used()>0 ){ |
| 654 | fatalError("SQLite has memory in use before the start of testing"); |
| 655 | } |
| 656 | |
| 657 | /* Register the in-memory virtual filesystem |
| 658 | */ |
| 659 | formatVfs(); |
| 660 | inmemVfsRegister(); |
| 661 | |
| 662 | /* Run a test using each SQL script against each database. |
| 663 | */ |
| 664 | if( !verboseFlag && !quietFlag ){ |
| 665 | int i; |
| 666 | i = strlen(zSourceDb) - 1; |
| 667 | while( i>0 && zSourceDb[i-1]!='/' && zSourceDb[i-1]!='\\' ){ i--; } |
| 668 | printf("%s:", &zSourceDb[i]); |
| 669 | } |
| 670 | for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ |
| 671 | for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ |
| 672 | sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", |
| 673 | pSql->id, pDb->id); |
| 674 | if( verboseFlag ){ |
| 675 | printf("%s\n", g.zTestName); |
| 676 | fflush(stdout); |
| 677 | }else if( !quietFlag ){ |
| 678 | static int prevAmt = -1; |
| 679 | int idx = (pSql->id-1)*g.nDb + pDb->id - 1; |
| 680 | int amt = idx*10/(g.nDb*g.nSql); |
| 681 | if( amt!=prevAmt ){ |
| 682 | printf(" %d%%", amt*10); |
| 683 | prevAmt = amt; |
| 684 | } |
| 685 | } |
| 686 | createVFile("main.db", pDb->sz, pDb->a); |
| 687 | rc = sqlite3_open_v2("main.db", &db, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE, |
| 688 | "inmem"); |
| 689 | #ifndef SQLITE_OMIT_TRACE |
| 690 | sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0); |
| 691 | #endif |
| 692 | if( rc ) fatalError("cannot open inmem database"); |
| 693 | runSql(db, (char*)pSql->a); |
| 694 | sqlite3_close(db); |
| 695 | if( sqlite3_memory_used()>0 ) fatalError("memory leak"); |
| 696 | reformatVfs(); |
| 697 | g.zTestName[0] = 0; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if( !quietFlag ){ |
| 702 | sqlite3_int64 iElapse = timeOfDay() - iBegin; |
| 703 | if( !verboseFlag ) printf("\n"); |
| 704 | printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\nSQLite %s %s\n", |
| 705 | g.nDb*g.nSql, (int)(iElapse/1000), (int)(iElapse%1000), |
| 706 | sqlite3_libversion(), sqlite3_sourceid()); |
| 707 | } |
| 708 | |
| 709 | /* Clean up and exit. |
| 710 | */ |
| 711 | blobListFree(g.pFirstSql); |
| 712 | blobListFree(g.pFirstDb); |
| 713 | reformatVfs(); |
| 714 | return 0; |
| 715 | } |