drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2015-04-17 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This is a utility program designed to aid running the SQLite library |
| 14 | ** against an external fuzzer, such as American Fuzzy Lop (AFL) |
| 15 | ** (http://lcamtuf.coredump.cx/afl/). Basically, this program reads |
| 16 | ** SQL text from standard input and passes it through to SQLite for evaluation, |
| 17 | ** just like the "sqlite3" command-line shell. Differences from the |
| 18 | ** command-line shell: |
| 19 | ** |
| 20 | ** (1) The complex "dot-command" extensions are omitted. This |
| 21 | ** prevents the fuzzer from discovering that it can run things |
| 22 | ** like ".shell rm -rf ~" |
| 23 | ** |
| 24 | ** (2) The database is opened with the SQLITE_OPEN_MEMORY flag so that |
| 25 | ** no disk I/O from the database is permitted. The ATTACH command |
| 26 | ** with a filename still uses an in-memory database. |
| 27 | ** |
| 28 | ** (3) The main in-memory database can be initialized from a template |
| 29 | ** disk database so that the fuzzer starts with a database containing |
| 30 | ** content. |
| 31 | ** |
| 32 | ** (4) The eval() SQL function is added, allowing the fuzzer to do |
| 33 | ** interesting recursive operations. |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 34 | ** |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 35 | ** (5) An error is raised if there is a memory leak. |
| 36 | ** |
| 37 | ** The input text can be divided into separate test cases using comments |
| 38 | ** of the form: |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 39 | ** |
| 40 | ** |****<...>****| |
| 41 | ** |
drh | f332071 | 2015-04-25 13:39:29 +0000 | [diff] [blame] | 42 | ** where the "..." is arbitrary text. (Except the "|" should really be "/". |
| 43 | ** "|" is used here to avoid compiler errors about nested comments.) |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 44 | ** A separate in-memory SQLite database is created to run each test case. |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 45 | ** This feature allows the "queue" of AFL to be captured into a single big |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 46 | ** file using a command like this: |
| 47 | ** |
| 48 | ** (for i in id:*; do echo '|****<'$i'>****|'; cat $i; done) >~/all-queue.txt |
| 49 | ** |
| 50 | ** (Once again, change the "|" to "/") Then all elements of the AFL queue |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 51 | ** can be run in a single go (for regression testing, for example) by typing: |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 52 | ** |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 53 | ** fuzzershell -f ~/all-queue.txt |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 54 | ** |
| 55 | ** After running each chunk of SQL, the database connection is closed. The |
| 56 | ** program aborts if the close fails or if there is any unfreed memory after |
| 57 | ** the close. |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 58 | ** |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 59 | ** New test cases can be appended to all-queue.txt at any time. If redundant |
| 60 | ** test cases are added, they can be eliminated by running: |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 61 | ** |
| 62 | ** fuzzershell -f ~/all-queue.txt --unique-cases ~/unique-cases.txt |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 63 | */ |
| 64 | #include <stdio.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <string.h> |
| 67 | #include <stdarg.h> |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 68 | #include <ctype.h> |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 69 | #include "sqlite3.h" |
| 70 | |
| 71 | /* |
| 72 | ** All global variables are gathered into the "g" singleton. |
| 73 | */ |
| 74 | struct GlobalVars { |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 75 | const char *zArgv0; /* Name of program */ |
| 76 | sqlite3_mem_methods sOrigMem; /* Original memory methods */ |
| 77 | sqlite3_mem_methods sOomMem; /* Memory methods with OOM simulator */ |
| 78 | int iOomCntdown; /* Memory fails on 1 to 0 transition */ |
| 79 | int nOomFault; /* Increments for each OOM fault */ |
| 80 | int bOomOnce; /* Fail just once if true */ |
| 81 | int bOomEnable; /* True to enable OOM simulation */ |
| 82 | int nOomBrkpt; /* Number of calls to oomFault() */ |
drh | 0ee751f | 2015-04-25 11:19:51 +0000 | [diff] [blame] | 83 | char zTestName[100]; /* Name of current test */ |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 84 | } g; |
| 85 | |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 86 | /* |
drh | f332071 | 2015-04-25 13:39:29 +0000 | [diff] [blame] | 87 | ** Maximum number of iterations for an OOM test |
| 88 | */ |
| 89 | #ifndef OOM_MAX |
drh | 7c84c02 | 2015-04-25 16:39:49 +0000 | [diff] [blame] | 90 | # define OOM_MAX 625 |
drh | f332071 | 2015-04-25 13:39:29 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /* |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 94 | ** This routine is called when a simulated OOM occurs. It exists as a |
| 95 | ** convenient place to set a debugger breakpoint. |
| 96 | */ |
| 97 | static void oomFault(void){ |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 98 | g.nOomBrkpt++; /* Prevent oomFault() from being optimized out */ |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 99 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 100 | |
| 101 | |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 102 | /* Versions of malloc() and realloc() that simulate OOM conditions */ |
| 103 | static void *oomMalloc(int nByte){ |
| 104 | if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){ |
| 105 | g.iOomCntdown--; |
| 106 | if( g.iOomCntdown==0 ){ |
| 107 | if( g.nOomFault==0 ) oomFault(); |
| 108 | g.nOomFault++; |
| 109 | if( !g.bOomOnce ) g.iOomCntdown = 1; |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | return g.sOrigMem.xMalloc(nByte); |
| 114 | } |
| 115 | static void *oomRealloc(void *pOld, int nByte){ |
| 116 | if( nByte>0 && g.bOomEnable && g.iOomCntdown>0 ){ |
| 117 | g.iOomCntdown--; |
| 118 | if( g.iOomCntdown==0 ){ |
| 119 | if( g.nOomFault==0 ) oomFault(); |
| 120 | g.nOomFault++; |
| 121 | if( !g.bOomOnce ) g.iOomCntdown = 1; |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | return g.sOrigMem.xRealloc(pOld, nByte); |
| 126 | } |
| 127 | |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 128 | /* |
| 129 | ** Print an error message and abort in such a way to indicate to the |
| 130 | ** fuzzer that this counts as a crash. |
| 131 | */ |
| 132 | static void abendError(const char *zFormat, ...){ |
| 133 | va_list ap; |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 134 | if( g.zTestName[0] ){ |
| 135 | fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); |
| 136 | }else{ |
| 137 | fprintf(stderr, "%s: ", g.zArgv0); |
| 138 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 139 | va_start(ap, zFormat); |
| 140 | vfprintf(stderr, zFormat, ap); |
| 141 | va_end(ap); |
| 142 | fprintf(stderr, "\n"); |
| 143 | abort(); |
| 144 | } |
| 145 | /* |
| 146 | ** Print an error message and quit, but not in a way that would look |
| 147 | ** like a crash. |
| 148 | */ |
| 149 | static void fatalError(const char *zFormat, ...){ |
| 150 | va_list ap; |
drh | be5248f | 2015-04-25 11:35:48 +0000 | [diff] [blame] | 151 | if( g.zTestName[0] ){ |
| 152 | fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); |
| 153 | }else{ |
| 154 | fprintf(stderr, "%s: ", g.zArgv0); |
| 155 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 156 | va_start(ap, zFormat); |
| 157 | vfprintf(stderr, zFormat, ap); |
| 158 | va_end(ap); |
| 159 | fprintf(stderr, "\n"); |
| 160 | exit(1); |
| 161 | } |
| 162 | |
| 163 | /* |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 164 | ** Evaluate some SQL. Abort if unable. |
| 165 | */ |
| 166 | static void sqlexec(sqlite3 *db, const char *zFormat, ...){ |
| 167 | va_list ap; |
| 168 | char *zSql; |
| 169 | char *zErrMsg = 0; |
| 170 | int rc; |
| 171 | va_start(ap, zFormat); |
| 172 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 173 | va_end(ap); |
| 174 | rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg); |
| 175 | if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg); |
| 176 | sqlite3_free(zSql); |
| 177 | } |
| 178 | |
| 179 | /* |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 180 | ** This callback is invoked by sqlite3_log(). |
| 181 | */ |
| 182 | static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){ |
| 183 | printf("LOG: (%d) %s\n", iErrCode, zMsg); |
drh | 9f18f74 | 2015-04-25 00:20:15 +0000 | [diff] [blame] | 184 | fflush(stdout); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 185 | } |
drh | 0ee751f | 2015-04-25 11:19:51 +0000 | [diff] [blame] | 186 | static void shellLogNoop(void *pNotUsed, int iErrCode, const char *zMsg){ |
| 187 | return; |
| 188 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | ** This callback is invoked by sqlite3_exec() to return query results. |
| 192 | */ |
| 193 | static int execCallback(void *NotUsed, int argc, char **argv, char **colv){ |
| 194 | int i; |
| 195 | static unsigned cnt = 0; |
| 196 | printf("ROW #%u:\n", ++cnt); |
| 197 | for(i=0; i<argc; i++){ |
| 198 | printf(" %s=", colv[i]); |
| 199 | if( argv[i] ){ |
| 200 | printf("[%s]\n", argv[i]); |
| 201 | }else{ |
| 202 | printf("NULL\n"); |
| 203 | } |
| 204 | } |
drh | 9f18f74 | 2015-04-25 00:20:15 +0000 | [diff] [blame] | 205 | fflush(stdout); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 206 | return 0; |
| 207 | } |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 208 | static int execNoop(void *NotUsed, int argc, char **argv, char **colv){ |
| 209 | return 0; |
| 210 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 211 | |
drh | 61a0d6b | 2015-04-24 18:31:12 +0000 | [diff] [blame] | 212 | #ifndef SQLITE_OMIT_TRACE |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 213 | /* |
| 214 | ** This callback is invoked by sqlite3_trace() as each SQL statement |
| 215 | ** starts. |
| 216 | */ |
| 217 | static void traceCallback(void *NotUsed, const char *zMsg){ |
| 218 | printf("TRACE: %s\n", zMsg); |
drh | 9f18f74 | 2015-04-25 00:20:15 +0000 | [diff] [blame] | 219 | fflush(stdout); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 220 | } |
drh | 0ee751f | 2015-04-25 11:19:51 +0000 | [diff] [blame] | 221 | static void traceNoop(void *NotUsed, const char *zMsg){ |
| 222 | return; |
| 223 | } |
drh | 61a0d6b | 2015-04-24 18:31:12 +0000 | [diff] [blame] | 224 | #endif |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 225 | |
| 226 | /*************************************************************************** |
| 227 | ** eval() implementation copied from ../ext/misc/eval.c |
| 228 | */ |
| 229 | /* |
| 230 | ** Structure used to accumulate the output |
| 231 | */ |
| 232 | struct EvalResult { |
| 233 | char *z; /* Accumulated output */ |
| 234 | const char *zSep; /* Separator */ |
| 235 | int szSep; /* Size of the separator string */ |
| 236 | sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */ |
| 237 | sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */ |
| 238 | }; |
| 239 | |
| 240 | /* |
| 241 | ** Callback from sqlite_exec() for the eval() function. |
| 242 | */ |
| 243 | static int callback(void *pCtx, int argc, char **argv, char **colnames){ |
| 244 | struct EvalResult *p = (struct EvalResult*)pCtx; |
| 245 | int i; |
| 246 | for(i=0; i<argc; i++){ |
| 247 | const char *z = argv[i] ? argv[i] : ""; |
| 248 | size_t sz = strlen(z); |
| 249 | if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){ |
| 250 | char *zNew; |
| 251 | p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1; |
| 252 | /* Using sqlite3_realloc64() would be better, but it is a recent |
| 253 | ** addition and will cause a segfault if loaded by an older version |
| 254 | ** of SQLite. */ |
| 255 | zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0; |
| 256 | if( zNew==0 ){ |
| 257 | sqlite3_free(p->z); |
| 258 | memset(p, 0, sizeof(*p)); |
| 259 | return 1; |
| 260 | } |
| 261 | p->z = zNew; |
| 262 | } |
| 263 | if( p->nUsed>0 ){ |
| 264 | memcpy(&p->z[p->nUsed], p->zSep, p->szSep); |
| 265 | p->nUsed += p->szSep; |
| 266 | } |
| 267 | memcpy(&p->z[p->nUsed], z, sz); |
| 268 | p->nUsed += sz; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | ** Implementation of the eval(X) and eval(X,Y) SQL functions. |
| 275 | ** |
| 276 | ** Evaluate the SQL text in X. Return the results, using string |
| 277 | ** Y as the separator. If Y is omitted, use a single space character. |
| 278 | */ |
| 279 | static void sqlEvalFunc( |
| 280 | sqlite3_context *context, |
| 281 | int argc, |
| 282 | sqlite3_value **argv |
| 283 | ){ |
| 284 | const char *zSql; |
| 285 | sqlite3 *db; |
| 286 | char *zErr = 0; |
| 287 | int rc; |
| 288 | struct EvalResult x; |
| 289 | |
| 290 | memset(&x, 0, sizeof(x)); |
| 291 | x.zSep = " "; |
| 292 | zSql = (const char*)sqlite3_value_text(argv[0]); |
| 293 | if( zSql==0 ) return; |
| 294 | if( argc>1 ){ |
| 295 | x.zSep = (const char*)sqlite3_value_text(argv[1]); |
| 296 | if( x.zSep==0 ) return; |
| 297 | } |
| 298 | x.szSep = (int)strlen(x.zSep); |
| 299 | db = sqlite3_context_db_handle(context); |
| 300 | rc = sqlite3_exec(db, zSql, callback, &x, &zErr); |
| 301 | if( rc!=SQLITE_OK ){ |
| 302 | sqlite3_result_error(context, zErr, -1); |
| 303 | sqlite3_free(zErr); |
| 304 | }else if( x.zSep==0 ){ |
| 305 | sqlite3_result_error_nomem(context); |
| 306 | sqlite3_free(x.z); |
| 307 | }else{ |
| 308 | sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free); |
| 309 | } |
| 310 | } |
| 311 | /* End of the eval() implementation |
| 312 | ******************************************************************************/ |
| 313 | |
| 314 | /* |
| 315 | ** Print sketchy documentation for this utility program |
| 316 | */ |
| 317 | static void showHelp(void){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 318 | printf("Usage: %s [options] ?FILE...?\n", g.zArgv0); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 319 | printf( |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 320 | "Read SQL text from FILE... (or from standard input if FILE... is omitted)\n" |
| 321 | "and then evaluate each block of SQL contained therein.\n" |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 322 | "Options:\n" |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 323 | " --autovacuum Enable AUTOVACUUM mode\n" |
drh | acd3374 | 2015-05-22 11:38:22 +0000 | [diff] [blame] | 324 | " --database FILE Use database FILE instead of an in-memory database\n" |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 325 | " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" |
| 326 | " --help Show this help text\n" |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 327 | " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 328 | " --oom Run each test multiple times in a simulated OOM loop\n" |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 329 | " --pagesize N Set the page size to N\n" |
| 330 | " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" |
| 331 | " -q Reduced output\n" |
| 332 | " --quiet Reduced output\n" |
| 333 | " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" |
| 334 | " --unique-cases FILE Write all unique test cases to FILE\n" |
| 335 | " --utf16be Set text encoding to UTF-16BE\n" |
| 336 | " --utf16le Set text encoding to UTF-16LE\n" |
| 337 | " -v Increased output\n" |
| 338 | " --verbose Increased output\n" |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 339 | ); |
| 340 | } |
| 341 | |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 342 | /* |
| 343 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 344 | ** is not a hex digit. |
| 345 | */ |
| 346 | static int hexDigitValue(char c){ |
| 347 | if( c>='0' && c<='9' ) return c - '0'; |
| 348 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 349 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 355 | */ |
| 356 | static int integerValue(const char *zArg){ |
| 357 | sqlite3_int64 v = 0; |
| 358 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 359 | { "KiB", 1024 }, |
| 360 | { "MiB", 1024*1024 }, |
| 361 | { "GiB", 1024*1024*1024 }, |
| 362 | { "KB", 1000 }, |
| 363 | { "MB", 1000000 }, |
| 364 | { "GB", 1000000000 }, |
| 365 | { "K", 1000 }, |
| 366 | { "M", 1000000 }, |
| 367 | { "G", 1000000000 }, |
| 368 | }; |
| 369 | int i; |
| 370 | int isNeg = 0; |
| 371 | if( zArg[0]=='-' ){ |
| 372 | isNeg = 1; |
| 373 | zArg++; |
| 374 | }else if( zArg[0]=='+' ){ |
| 375 | zArg++; |
| 376 | } |
| 377 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 378 | int x; |
| 379 | zArg += 2; |
| 380 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 381 | v = (v<<4) + x; |
| 382 | zArg++; |
| 383 | } |
| 384 | }else{ |
| 385 | while( isdigit(zArg[0]) ){ |
| 386 | v = v*10 + zArg[0] - '0'; |
| 387 | zArg++; |
| 388 | } |
| 389 | } |
| 390 | for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ |
| 391 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 392 | v *= aMult[i].iMult; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | if( v>0x7fffffff ) abendError("parameter too large - max 2147483648"); |
| 397 | return (int)(isNeg? -v : v); |
| 398 | } |
| 399 | |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 400 | /* Return the current wall-clock time */ |
| 401 | static sqlite3_int64 timeOfDay(void){ |
| 402 | static sqlite3_vfs *clockVfs = 0; |
| 403 | sqlite3_int64 t; |
| 404 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 405 | if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 406 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 407 | }else{ |
| 408 | double r; |
| 409 | clockVfs->xCurrentTime(clockVfs, &r); |
| 410 | t = (sqlite3_int64)(r*86400000.0); |
| 411 | } |
| 412 | return t; |
| 413 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 414 | |
| 415 | int main(int argc, char **argv){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 416 | char *zIn = 0; /* Input text */ |
| 417 | int nAlloc = 0; /* Number of bytes allocated for zIn[] */ |
| 418 | int nIn = 0; /* Number of bytes of zIn[] used */ |
| 419 | size_t got; /* Bytes read from input */ |
| 420 | int rc = SQLITE_OK; /* Result codes from API functions */ |
| 421 | int i; /* Loop counter */ |
| 422 | int iNext; /* Next block of SQL */ |
| 423 | sqlite3 *db; /* Open database */ |
| 424 | char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */ |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 425 | const char *zEncoding = 0; /* --utf16be or --utf16le */ |
| 426 | int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ |
| 427 | int nLook = 0, szLook = 0; /* --lookaside configuration */ |
| 428 | int nPCache = 0, szPCache = 0;/* --pcache configuration */ |
| 429 | int nScratch = 0, szScratch=0;/* --scratch configuration */ |
| 430 | int pageSize = 0; /* Desired page size. 0 means default */ |
| 431 | void *pHeap = 0; /* Allocated heap space */ |
| 432 | void *pLook = 0; /* Allocated lookaside space */ |
| 433 | void *pPCache = 0; /* Allocated storage for pcache */ |
| 434 | void *pScratch = 0; /* Allocated storage for scratch */ |
| 435 | int doAutovac = 0; /* True for --autovacuum */ |
drh | 9985dab | 2015-04-20 22:36:49 +0000 | [diff] [blame] | 436 | char *zSql; /* SQL to run */ |
| 437 | char *zToFree = 0; /* Call sqlite3_free() on this afte running zSql */ |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 438 | int verboseFlag = 0; /* --verbose or -v flag */ |
| 439 | int quietFlag = 0; /* --quiet or -q flag */ |
| 440 | int nTest = 0; /* Number of test cases run */ |
| 441 | int multiTest = 0; /* True if there will be multiple test cases */ |
| 442 | int lastPct = -1; /* Previous percentage done output */ |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 443 | sqlite3 *dataDb = 0; /* Database holding compacted input data */ |
| 444 | sqlite3_stmt *pStmt = 0; /* Statement to insert testcase into dataDb */ |
| 445 | const char *zDataOut = 0; /* Write compacted data to this output file */ |
drh | e1a71a5 | 2015-04-24 16:09:12 +0000 | [diff] [blame] | 446 | int nHeader = 0; /* Bytes of header comment text on input file */ |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 447 | int oomFlag = 0; /* --oom */ |
| 448 | int oomCnt = 0; /* Counter for the OOM loop */ |
| 449 | char zErrBuf[200]; /* Space for the error message */ |
| 450 | const char *zFailCode; /* Value of the TEST_FAILURE environment var */ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 451 | const char *zPrompt; /* Initial prompt when large-file fuzzing */ |
| 452 | int nInFile = 0; /* Number of input files to read */ |
| 453 | char **azInFile = 0; /* Array of input file names */ |
| 454 | int jj; /* Loop counter for azInFile[] */ |
drh | 1a57c17 | 2015-05-02 19:54:35 +0000 | [diff] [blame] | 455 | sqlite3_int64 iBegin; /* Start time for the whole program */ |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 456 | sqlite3_int64 iStart, iEnd; /* Start and end-times for a test case */ |
drh | f9def06 | 2015-05-22 23:45:56 +0000 | [diff] [blame] | 457 | const char *zDbName = 0; /* Name of an on-disk database file to open */ |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 458 | |
drh | 1a57c17 | 2015-05-02 19:54:35 +0000 | [diff] [blame] | 459 | iBegin = timeOfDay(); |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 460 | zFailCode = getenv("TEST_FAILURE"); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 461 | g.zArgv0 = argv[0]; |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 462 | zPrompt = "<stdin>"; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 463 | for(i=1; i<argc; i++){ |
| 464 | const char *z = argv[i]; |
| 465 | if( z[0]=='-' ){ |
| 466 | z++; |
| 467 | if( z[0]=='-' ) z++; |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 468 | if( strcmp(z,"autovacuum")==0 ){ |
| 469 | doAutovac = 1; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 470 | }else |
drh | acd3374 | 2015-05-22 11:38:22 +0000 | [diff] [blame] | 471 | if( strcmp(z,"database")==0 ){ |
| 472 | if( i>=argc-1 ) abendError("missing argument on %s\n", argv[i]); |
| 473 | zDbName = argv[i+1]; |
| 474 | i += 1; |
| 475 | }else |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 476 | if( strcmp(z, "f")==0 && i+1<argc ){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 477 | i++; |
| 478 | goto addNewInFile; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 479 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 480 | if( strcmp(z,"heap")==0 ){ |
| 481 | if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]); |
| 482 | nHeap = integerValue(argv[i+1]); |
| 483 | mnHeap = integerValue(argv[i+2]); |
| 484 | i += 2; |
| 485 | }else |
| 486 | if( strcmp(z,"help")==0 ){ |
| 487 | showHelp(); |
| 488 | return 0; |
| 489 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 490 | if( strcmp(z,"lookaside")==0 ){ |
| 491 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 492 | nLook = integerValue(argv[i+1]); |
| 493 | szLook = integerValue(argv[i+2]); |
| 494 | i += 2; |
| 495 | }else |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 496 | if( strcmp(z,"oom")==0 ){ |
| 497 | oomFlag = 1; |
| 498 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 499 | if( strcmp(z,"pagesize")==0 ){ |
| 500 | if( i>=argc-1 ) abendError("missing argument on %s", argv[i]); |
| 501 | pageSize = integerValue(argv[++i]); |
| 502 | }else |
| 503 | if( strcmp(z,"pcache")==0 ){ |
| 504 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 505 | nPCache = integerValue(argv[i+1]); |
| 506 | szPCache = integerValue(argv[i+2]); |
| 507 | i += 2; |
| 508 | }else |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 509 | if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
| 510 | quietFlag = 1; |
| 511 | verboseFlag = 0; |
| 512 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 513 | if( strcmp(z,"scratch")==0 ){ |
| 514 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 515 | nScratch = integerValue(argv[i+1]); |
| 516 | szScratch = integerValue(argv[i+2]); |
| 517 | i += 2; |
| 518 | }else |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 519 | if( strcmp(z, "unique-cases")==0 ){ |
| 520 | if( i>=argc-1 ) abendError("missing arguments on %s", argv[i]); |
| 521 | if( zDataOut ) abendError("only one --minimize allowed"); |
| 522 | zDataOut = argv[++i]; |
| 523 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 524 | if( strcmp(z,"utf16le")==0 ){ |
| 525 | zEncoding = "utf16le"; |
| 526 | }else |
| 527 | if( strcmp(z,"utf16be")==0 ){ |
| 528 | zEncoding = "utf16be"; |
| 529 | }else |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 530 | if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ |
| 531 | quietFlag = 0; |
| 532 | verboseFlag = 1; |
| 533 | }else |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 534 | { |
| 535 | abendError("unknown option: %s", argv[i]); |
| 536 | } |
| 537 | }else{ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 538 | addNewInFile: |
| 539 | nInFile++; |
| 540 | azInFile = realloc(azInFile, sizeof(azInFile[0])*nInFile); |
| 541 | if( azInFile==0 ) abendError("out of memory"); |
| 542 | azInFile[nInFile-1] = argv[i]; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 545 | |
| 546 | /* Do global SQLite initialization */ |
drh | 0ee751f | 2015-04-25 11:19:51 +0000 | [diff] [blame] | 547 | sqlite3_config(SQLITE_CONFIG_LOG, verboseFlag ? shellLog : shellLogNoop, 0); |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 548 | if( nHeap>0 ){ |
| 549 | pHeap = malloc( nHeap ); |
| 550 | if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap); |
| 551 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); |
| 552 | if( rc ) abendError("heap configuration failed: %d\n", rc); |
| 553 | } |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 554 | if( oomFlag ){ |
| 555 | sqlite3_config(SQLITE_CONFIG_GETMALLOC, &g.sOrigMem); |
| 556 | g.sOomMem = g.sOrigMem; |
| 557 | g.sOomMem.xMalloc = oomMalloc; |
| 558 | g.sOomMem.xRealloc = oomRealloc; |
| 559 | sqlite3_config(SQLITE_CONFIG_MALLOC, &g.sOomMem); |
| 560 | } |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 561 | if( nLook>0 ){ |
| 562 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); |
| 563 | if( szLook>0 ){ |
| 564 | pLook = malloc( nLook*szLook ); |
| 565 | if( pLook==0 ) fatalError("out of memory"); |
| 566 | } |
| 567 | } |
| 568 | if( nScratch>0 && szScratch>0 ){ |
| 569 | pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); |
| 570 | if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch", |
| 571 | nScratch*(sqlite3_int64)szScratch); |
| 572 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); |
| 573 | if( rc ) abendError("scratch configuration failed: %d\n", rc); |
| 574 | } |
| 575 | if( nPCache>0 && szPCache>0 ){ |
| 576 | pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); |
| 577 | if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache", |
| 578 | nPCache*(sqlite3_int64)szPCache); |
| 579 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); |
| 580 | if( rc ) abendError("pcache configuration failed: %d", rc); |
| 581 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 582 | |
| 583 | /* If the --unique-cases option was supplied, open the database that will |
| 584 | ** be used to gather unique test cases. |
| 585 | */ |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 586 | if( zDataOut ){ |
| 587 | rc = sqlite3_open(":memory:", &dataDb); |
| 588 | if( rc ) abendError("cannot open :memory: database"); |
| 589 | rc = sqlite3_exec(dataDb, |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 590 | "CREATE TABLE testcase(sql BLOB PRIMARY KEY, tm) WITHOUT ROWID;",0,0,0); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 591 | if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 592 | rc = sqlite3_prepare_v2(dataDb, |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 593 | "INSERT OR IGNORE INTO testcase(sql,tm)VALUES(?1,?2)", |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 594 | -1, &pStmt, 0); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 595 | if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); |
| 596 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 597 | |
| 598 | /* Initialize the input buffer used to hold SQL text */ |
| 599 | if( nInFile==0 ) nInFile = 1; |
| 600 | nAlloc = 1000; |
| 601 | zIn = malloc(nAlloc); |
| 602 | if( zIn==0 ) fatalError("out of memory"); |
| 603 | |
| 604 | /* Loop over all input files */ |
| 605 | for(jj=0; jj<nInFile; jj++){ |
| 606 | |
| 607 | /* Read the complete content of the next input file into zIn[] */ |
| 608 | FILE *in; |
| 609 | if( azInFile ){ |
| 610 | int j, k; |
| 611 | in = fopen(azInFile[jj],"rb"); |
| 612 | if( in==0 ){ |
| 613 | abendError("cannot open %s for reading", azInFile[jj]); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 614 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 615 | zPrompt = azInFile[jj]; |
| 616 | for(j=k=0; zPrompt[j]; j++) if( zPrompt[j]=='/' ) k = j+1; |
| 617 | zPrompt += k; |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 618 | }else{ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 619 | in = stdin; |
| 620 | zPrompt = "<stdin>"; |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 621 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 622 | while( !feof(in) ){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 623 | got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); |
| 624 | nIn += (int)got; |
| 625 | zIn[nIn] = 0; |
| 626 | if( got==0 ) break; |
drh | 1a57c17 | 2015-05-02 19:54:35 +0000 | [diff] [blame] | 627 | if( nAlloc - nIn - 1 < 100 ){ |
| 628 | nAlloc += nAlloc+1000; |
| 629 | zIn = realloc(zIn, nAlloc); |
| 630 | if( zIn==0 ) fatalError("out of memory"); |
| 631 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 632 | } |
| 633 | if( in!=stdin ) fclose(in); |
| 634 | lastPct = -1; |
| 635 | |
| 636 | /* Skip initial lines of the input file that begin with "#" */ |
| 637 | for(i=0; i<nIn; i=iNext+1){ |
| 638 | if( zIn[i]!='#' ) break; |
| 639 | for(iNext=i+1; iNext<nIn && zIn[iNext]!='\n'; iNext++){} |
| 640 | } |
| 641 | nHeader = i; |
| 642 | |
| 643 | /* Process all test cases contained within the input file. |
| 644 | */ |
| 645 | for(; i<nIn; i=iNext, nTest++, g.zTestName[0]=0){ |
| 646 | char cSaved; |
| 647 | if( strncmp(&zIn[i], "/****<",6)==0 ){ |
| 648 | char *z = strstr(&zIn[i], ">****/"); |
| 649 | if( z ){ |
| 650 | z += 6; |
| 651 | sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "%.*s", |
| 652 | (int)(z-&zIn[i]) - 12, &zIn[i+6]); |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 653 | if( verboseFlag ){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 654 | printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]); |
drh | 9f18f74 | 2015-04-25 00:20:15 +0000 | [diff] [blame] | 655 | fflush(stdout); |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 656 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 657 | i += (int)(z-&zIn[i]); |
| 658 | multiTest = 1; |
drh | 048810b | 2015-04-24 23:45:23 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 661 | for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){} |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 662 | cSaved = zIn[iNext]; |
| 663 | zIn[iNext] = 0; |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 664 | |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 665 | |
| 666 | /* Print out the SQL of the next test case is --verbose is enabled |
| 667 | */ |
| 668 | zSql = &zIn[i]; |
| 669 | if( verboseFlag ){ |
| 670 | printf("INPUT (offset: %d, size: %d): [%s]\n", |
| 671 | i, (int)strlen(&zIn[i]), &zIn[i]); |
| 672 | }else if( multiTest && !quietFlag ){ |
| 673 | if( oomFlag ){ |
| 674 | printf("%s\n", g.zTestName); |
| 675 | }else{ |
| 676 | int pct = (10*iNext)/nIn; |
| 677 | if( pct!=lastPct ){ |
| 678 | if( lastPct<0 ) printf("%s:", zPrompt); |
| 679 | printf(" %d%%", pct*10); |
| 680 | lastPct = pct; |
| 681 | } |
| 682 | } |
drh | 1a57c17 | 2015-05-02 19:54:35 +0000 | [diff] [blame] | 683 | }else if( nInFile>1 ){ |
| 684 | printf("%s\n", zPrompt); |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 685 | } |
drh | 9f18f74 | 2015-04-25 00:20:15 +0000 | [diff] [blame] | 686 | fflush(stdout); |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 687 | |
| 688 | /* Run the next test case. Run it multiple times in --oom mode |
| 689 | */ |
| 690 | if( oomFlag ){ |
| 691 | oomCnt = g.iOomCntdown = 1; |
| 692 | g.nOomFault = 0; |
| 693 | g.bOomOnce = 1; |
| 694 | if( verboseFlag ){ |
| 695 | printf("Once.%d\n", oomCnt); |
| 696 | fflush(stdout); |
| 697 | } |
| 698 | }else{ |
| 699 | oomCnt = 0; |
| 700 | } |
| 701 | do{ |
drh | acd3374 | 2015-05-22 11:38:22 +0000 | [diff] [blame] | 702 | if( zDbName ){ |
| 703 | rc = sqlite3_open_v2(zDbName, &db, SQLITE_OPEN_READWRITE, 0); |
drh | c19bc9b | 2015-05-22 23:50:19 +0000 | [diff] [blame] | 704 | if( rc!=SQLITE_OK ){ |
| 705 | abendError("Cannot open database file %s", zDbName); |
| 706 | } |
drh | acd3374 | 2015-05-22 11:38:22 +0000 | [diff] [blame] | 707 | }else{ |
| 708 | rc = sqlite3_open_v2( |
| 709 | "main.db", &db, |
| 710 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, |
| 711 | 0); |
drh | c19bc9b | 2015-05-22 23:50:19 +0000 | [diff] [blame] | 712 | if( rc!=SQLITE_OK ){ |
| 713 | abendError("Unable to open the in-memory database"); |
| 714 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 715 | } |
| 716 | if( pLook ){ |
| 717 | rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLook); |
| 718 | if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc); |
| 719 | } |
| 720 | #ifndef SQLITE_OMIT_TRACE |
| 721 | sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0); |
| 722 | #endif |
drh | 5f4a44a | 2015-09-19 14:57:00 +0000 | [diff] [blame] | 723 | #ifdef SQLITE_ENABLE_JSON1 |
| 724 | { |
| 725 | extern int sqlite3_json_init(sqlite3*); |
| 726 | sqlite3_json_init(db); |
| 727 | } |
| 728 | #endif |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 729 | sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); |
| 730 | sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); |
| 731 | sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); |
| 732 | if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); |
| 733 | if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); |
| 734 | if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 735 | iStart = timeOfDay(); |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 736 | g.bOomEnable = 1; |
| 737 | if( verboseFlag ){ |
| 738 | zErrMsg = 0; |
| 739 | rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg); |
| 740 | if( zErrMsg ){ |
| 741 | sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg); |
| 742 | zErrMsg = 0; |
| 743 | } |
| 744 | }else { |
| 745 | rc = sqlite3_exec(db, zSql, execNoop, 0, 0); |
| 746 | } |
| 747 | g.bOomEnable = 0; |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 748 | iEnd = timeOfDay(); |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 749 | rc = sqlite3_close(db); |
| 750 | if( rc ){ |
| 751 | abendError("sqlite3_close() failed with rc=%d", rc); |
| 752 | } |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 753 | if( !zDataOut && sqlite3_memory_used()>0 ){ |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 754 | abendError("memory in use after close: %lld bytes",sqlite3_memory_used()); |
| 755 | } |
| 756 | if( oomFlag ){ |
| 757 | /* Limit the number of iterations of the OOM loop to OOM_MAX. If the |
| 758 | ** first pass (single failure) exceeds 2/3rds of OOM_MAX this skip the |
| 759 | ** second pass (continuous failure after first) completely. */ |
| 760 | if( g.nOomFault==0 || oomCnt>OOM_MAX ){ |
| 761 | if( g.bOomOnce && oomCnt<=(OOM_MAX*2/3) ){ |
| 762 | oomCnt = g.iOomCntdown = 1; |
| 763 | g.bOomOnce = 0; |
| 764 | }else{ |
| 765 | oomCnt = 0; |
| 766 | } |
| 767 | }else{ |
| 768 | g.iOomCntdown = ++oomCnt; |
| 769 | g.nOomFault = 0; |
| 770 | } |
| 771 | if( oomCnt ){ |
| 772 | if( verboseFlag ){ |
| 773 | printf("%s.%d\n", g.bOomOnce ? "Once" : "Multi", oomCnt); |
| 774 | fflush(stdout); |
| 775 | } |
| 776 | nTest++; |
| 777 | } |
| 778 | } |
| 779 | }while( oomCnt>0 ); |
| 780 | |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 781 | /* Store unique test cases in the in the dataDb database if the |
| 782 | ** --unique-cases flag is present |
| 783 | */ |
| 784 | if( zDataOut ){ |
| 785 | sqlite3_bind_blob(pStmt, 1, &zIn[i], iNext-i, SQLITE_STATIC); |
| 786 | sqlite3_bind_int64(pStmt, 2, iEnd - iStart); |
| 787 | rc = sqlite3_step(pStmt); |
| 788 | if( rc!=SQLITE_DONE ) abendError("%s", sqlite3_errmsg(dataDb)); |
| 789 | sqlite3_reset(pStmt); |
| 790 | } |
| 791 | |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 792 | /* Free the SQL from the current test case |
| 793 | */ |
| 794 | if( zToFree ){ |
| 795 | sqlite3_free(zToFree); |
| 796 | zToFree = 0; |
| 797 | } |
| 798 | zIn[iNext] = cSaved; |
| 799 | |
| 800 | /* Show test-case results in --verbose mode |
| 801 | */ |
| 802 | if( verboseFlag ){ |
| 803 | printf("RESULT-CODE: %d\n", rc); |
| 804 | if( zErrMsg ){ |
| 805 | printf("ERROR-MSG: [%s]\n", zErrBuf); |
| 806 | } |
| 807 | fflush(stdout); |
| 808 | } |
| 809 | |
| 810 | /* Simulate an error if the TEST_FAILURE environment variable is "5". |
| 811 | ** This is used to verify that automated test script really do spot |
| 812 | ** errors that occur in this test program. |
| 813 | */ |
| 814 | if( zFailCode ){ |
| 815 | if( zFailCode[0]=='5' && zFailCode[1]==0 ){ |
| 816 | abendError("simulated failure"); |
| 817 | }else if( zFailCode[0]!=0 ){ |
| 818 | /* If TEST_FAILURE is something other than 5, just exit the test |
| 819 | ** early */ |
| 820 | printf("\nExit early due to TEST_FAILURE being set"); |
| 821 | break; |
| 822 | } |
drh | e1a71a5 | 2015-04-24 16:09:12 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 825 | if( !verboseFlag && multiTest && !quietFlag && !oomFlag ) printf("\n"); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 826 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 827 | |
| 828 | /* Report total number of tests run |
| 829 | */ |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 830 | if( nTest>1 && !quietFlag ){ |
drh | 1a57c17 | 2015-05-02 19:54:35 +0000 | [diff] [blame] | 831 | sqlite3_int64 iElapse = timeOfDay() - iBegin; |
| 832 | printf("%s: 0 errors out of %d tests in %d.%03d seconds\nSQLite %s %s\n", |
| 833 | g.zArgv0, nTest, (int)(iElapse/1000), (int)(iElapse%1000), |
| 834 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 835 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 836 | |
| 837 | /* Write the unique test cases if the --unique-cases flag was used |
| 838 | */ |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 839 | if( zDataOut ){ |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 840 | int n = 0; |
drh | e1a71a5 | 2015-04-24 16:09:12 +0000 | [diff] [blame] | 841 | FILE *out = fopen(zDataOut, "wb"); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 842 | if( out==0 ) abendError("cannot open %s for writing", zDataOut); |
drh | e1a71a5 | 2015-04-24 16:09:12 +0000 | [diff] [blame] | 843 | if( nHeader>0 ) fwrite(zIn, nHeader, 1, out); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 844 | sqlite3_finalize(pStmt); |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 845 | rc = sqlite3_prepare_v2(dataDb, "SELECT sql, tm FROM testcase ORDER BY tm, sql", |
| 846 | -1, &pStmt, 0); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 847 | if( rc ) abendError("%s", sqlite3_errmsg(dataDb)); |
| 848 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | c843016 | 2015-05-01 20:34:47 +0000 | [diff] [blame] | 849 | fprintf(out,"/****<%d:%dms>****/", ++n, sqlite3_column_int(pStmt,1)); |
drh | 875bafa | 2015-04-24 14:47:59 +0000 | [diff] [blame] | 850 | fwrite(sqlite3_column_blob(pStmt,0),sqlite3_column_bytes(pStmt,0),1,out); |
| 851 | } |
| 852 | fclose(out); |
| 853 | sqlite3_finalize(pStmt); |
| 854 | sqlite3_close(dataDb); |
drh | 1cbb7fa | 2015-04-24 13:00:59 +0000 | [diff] [blame] | 855 | } |
drh | b3df0c6 | 2015-05-01 19:21:12 +0000 | [diff] [blame] | 856 | |
| 857 | /* Clean up and exit. |
| 858 | */ |
| 859 | free(azInFile); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 860 | free(zIn); |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame] | 861 | free(pHeap); |
| 862 | free(pLook); |
| 863 | free(pScratch); |
| 864 | free(pPCache); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 865 | return 0; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 866 | } |