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