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 | ** |
| 35 | ** 2015-04-20: The input text can be divided into separate SQL chunks using |
| 36 | ** lines of the form: |
| 37 | ** |
| 38 | ** |****<...>****| |
| 39 | ** |
| 40 | ** where the "..." is arbitrary text, except the "|" should really be "/". |
| 41 | ** ("|" is used here to avoid compiler warnings about nested comments.) |
| 42 | ** Each such SQL comment is printed as it is encountered. A separate |
| 43 | ** in-memory SQLite database is created to run each chunk of SQL. This |
| 44 | ** feature allows the "queue" of AFL to be captured into a single big |
| 45 | ** file using a command like this: |
| 46 | ** |
| 47 | ** (for i in id:*; do echo '|****<'$i'>****|'; cat $i; done) >~/all-queue.txt |
| 48 | ** |
| 49 | ** (Once again, change the "|" to "/") Then all elements of the AFL queue |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 50 | ** 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] | 51 | ** |
| 52 | ** fuzzershell -f ~/all-queue.txt >out.txt |
| 53 | ** |
| 54 | ** After running each chunk of SQL, the database connection is closed. The |
| 55 | ** program aborts if the close fails or if there is any unfreed memory after |
| 56 | ** the close. |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 57 | */ |
| 58 | #include <stdio.h> |
| 59 | #include <stdlib.h> |
| 60 | #include <string.h> |
| 61 | #include <stdarg.h> |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 62 | #include <ctype.h> |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 63 | #include "sqlite3.h" |
| 64 | |
| 65 | /* |
| 66 | ** All global variables are gathered into the "g" singleton. |
| 67 | */ |
| 68 | struct GlobalVars { |
| 69 | const char *zArgv0; /* Name of program */ |
| 70 | } g; |
| 71 | |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | ** Print an error message and abort in such a way to indicate to the |
| 76 | ** fuzzer that this counts as a crash. |
| 77 | */ |
| 78 | static void abendError(const char *zFormat, ...){ |
| 79 | va_list ap; |
| 80 | fprintf(stderr, "%s: ", g.zArgv0); |
| 81 | va_start(ap, zFormat); |
| 82 | vfprintf(stderr, zFormat, ap); |
| 83 | va_end(ap); |
| 84 | fprintf(stderr, "\n"); |
| 85 | abort(); |
| 86 | } |
| 87 | /* |
| 88 | ** Print an error message and quit, but not in a way that would look |
| 89 | ** like a crash. |
| 90 | */ |
| 91 | static void fatalError(const char *zFormat, ...){ |
| 92 | va_list ap; |
| 93 | fprintf(stderr, "%s: ", g.zArgv0); |
| 94 | va_start(ap, zFormat); |
| 95 | vfprintf(stderr, zFormat, ap); |
| 96 | va_end(ap); |
| 97 | fprintf(stderr, "\n"); |
| 98 | exit(1); |
| 99 | } |
| 100 | |
| 101 | /* |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 102 | ** Evaluate some SQL. Abort if unable. |
| 103 | */ |
| 104 | static void sqlexec(sqlite3 *db, const char *zFormat, ...){ |
| 105 | va_list ap; |
| 106 | char *zSql; |
| 107 | char *zErrMsg = 0; |
| 108 | int rc; |
| 109 | va_start(ap, zFormat); |
| 110 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 111 | va_end(ap); |
| 112 | rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg); |
| 113 | if( rc ) abendError("failed sql [%s]: %s", zSql, zErrMsg); |
| 114 | sqlite3_free(zSql); |
| 115 | } |
| 116 | |
| 117 | /* |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 118 | ** This callback is invoked by sqlite3_log(). |
| 119 | */ |
| 120 | static void shellLog(void *pNotUsed, int iErrCode, const char *zMsg){ |
| 121 | printf("LOG: (%d) %s\n", iErrCode, zMsg); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | ** This callback is invoked by sqlite3_exec() to return query results. |
| 126 | */ |
| 127 | static int execCallback(void *NotUsed, int argc, char **argv, char **colv){ |
| 128 | int i; |
| 129 | static unsigned cnt = 0; |
| 130 | printf("ROW #%u:\n", ++cnt); |
| 131 | for(i=0; i<argc; i++){ |
| 132 | printf(" %s=", colv[i]); |
| 133 | if( argv[i] ){ |
| 134 | printf("[%s]\n", argv[i]); |
| 135 | }else{ |
| 136 | printf("NULL\n"); |
| 137 | } |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | ** This callback is invoked by sqlite3_trace() as each SQL statement |
| 144 | ** starts. |
| 145 | */ |
| 146 | static void traceCallback(void *NotUsed, const char *zMsg){ |
| 147 | printf("TRACE: %s\n", zMsg); |
| 148 | } |
| 149 | |
| 150 | /*************************************************************************** |
| 151 | ** eval() implementation copied from ../ext/misc/eval.c |
| 152 | */ |
| 153 | /* |
| 154 | ** Structure used to accumulate the output |
| 155 | */ |
| 156 | struct EvalResult { |
| 157 | char *z; /* Accumulated output */ |
| 158 | const char *zSep; /* Separator */ |
| 159 | int szSep; /* Size of the separator string */ |
| 160 | sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */ |
| 161 | sqlite3_int64 nUsed; /* Number of bytes of z[] actually used */ |
| 162 | }; |
| 163 | |
| 164 | /* |
| 165 | ** Callback from sqlite_exec() for the eval() function. |
| 166 | */ |
| 167 | static int callback(void *pCtx, int argc, char **argv, char **colnames){ |
| 168 | struct EvalResult *p = (struct EvalResult*)pCtx; |
| 169 | int i; |
| 170 | for(i=0; i<argc; i++){ |
| 171 | const char *z = argv[i] ? argv[i] : ""; |
| 172 | size_t sz = strlen(z); |
| 173 | if( (sqlite3_int64)sz+p->nUsed+p->szSep+1 > p->nAlloc ){ |
| 174 | char *zNew; |
| 175 | p->nAlloc = p->nAlloc*2 + sz + p->szSep + 1; |
| 176 | /* Using sqlite3_realloc64() would be better, but it is a recent |
| 177 | ** addition and will cause a segfault if loaded by an older version |
| 178 | ** of SQLite. */ |
| 179 | zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0; |
| 180 | if( zNew==0 ){ |
| 181 | sqlite3_free(p->z); |
| 182 | memset(p, 0, sizeof(*p)); |
| 183 | return 1; |
| 184 | } |
| 185 | p->z = zNew; |
| 186 | } |
| 187 | if( p->nUsed>0 ){ |
| 188 | memcpy(&p->z[p->nUsed], p->zSep, p->szSep); |
| 189 | p->nUsed += p->szSep; |
| 190 | } |
| 191 | memcpy(&p->z[p->nUsed], z, sz); |
| 192 | p->nUsed += sz; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | ** Implementation of the eval(X) and eval(X,Y) SQL functions. |
| 199 | ** |
| 200 | ** Evaluate the SQL text in X. Return the results, using string |
| 201 | ** Y as the separator. If Y is omitted, use a single space character. |
| 202 | */ |
| 203 | static void sqlEvalFunc( |
| 204 | sqlite3_context *context, |
| 205 | int argc, |
| 206 | sqlite3_value **argv |
| 207 | ){ |
| 208 | const char *zSql; |
| 209 | sqlite3 *db; |
| 210 | char *zErr = 0; |
| 211 | int rc; |
| 212 | struct EvalResult x; |
| 213 | |
| 214 | memset(&x, 0, sizeof(x)); |
| 215 | x.zSep = " "; |
| 216 | zSql = (const char*)sqlite3_value_text(argv[0]); |
| 217 | if( zSql==0 ) return; |
| 218 | if( argc>1 ){ |
| 219 | x.zSep = (const char*)sqlite3_value_text(argv[1]); |
| 220 | if( x.zSep==0 ) return; |
| 221 | } |
| 222 | x.szSep = (int)strlen(x.zSep); |
| 223 | db = sqlite3_context_db_handle(context); |
| 224 | rc = sqlite3_exec(db, zSql, callback, &x, &zErr); |
| 225 | if( rc!=SQLITE_OK ){ |
| 226 | sqlite3_result_error(context, zErr, -1); |
| 227 | sqlite3_free(zErr); |
| 228 | }else if( x.zSep==0 ){ |
| 229 | sqlite3_result_error_nomem(context); |
| 230 | sqlite3_free(x.z); |
| 231 | }else{ |
| 232 | sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free); |
| 233 | } |
| 234 | } |
| 235 | /* End of the eval() implementation |
| 236 | ******************************************************************************/ |
| 237 | |
| 238 | /* |
| 239 | ** Print sketchy documentation for this utility program |
| 240 | */ |
| 241 | static void showHelp(void){ |
| 242 | printf("Usage: %s [options]\n", g.zArgv0); |
| 243 | printf( |
| 244 | "Read SQL text from standard input and evaluate it.\n" |
| 245 | "Options:\n" |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 246 | " --autovacuum Enable AUTOVACUUM mode\n" |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 247 | " -f FILE Read SQL text from FILE instead of standard input\n" |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 248 | " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 249 | " --help Show this help text\n" |
| 250 | " --initdb DBFILE Initialize the in-memory database using template DBFILE\n" |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 251 | " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" |
| 252 | " --pagesize N Set the page size to N\n" |
| 253 | " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" |
| 254 | " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" |
| 255 | " --utf16be Set text encoding to UTF-16BE\n" |
| 256 | " --utf16le Set text encoding to UTF-16LE\n" |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 257 | ); |
| 258 | } |
| 259 | |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 260 | /* |
| 261 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 262 | ** is not a hex digit. |
| 263 | */ |
| 264 | static int hexDigitValue(char c){ |
| 265 | if( c>='0' && c<='9' ) return c - '0'; |
| 266 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 267 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 273 | */ |
| 274 | static int integerValue(const char *zArg){ |
| 275 | sqlite3_int64 v = 0; |
| 276 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 277 | { "KiB", 1024 }, |
| 278 | { "MiB", 1024*1024 }, |
| 279 | { "GiB", 1024*1024*1024 }, |
| 280 | { "KB", 1000 }, |
| 281 | { "MB", 1000000 }, |
| 282 | { "GB", 1000000000 }, |
| 283 | { "K", 1000 }, |
| 284 | { "M", 1000000 }, |
| 285 | { "G", 1000000000 }, |
| 286 | }; |
| 287 | int i; |
| 288 | int isNeg = 0; |
| 289 | if( zArg[0]=='-' ){ |
| 290 | isNeg = 1; |
| 291 | zArg++; |
| 292 | }else if( zArg[0]=='+' ){ |
| 293 | zArg++; |
| 294 | } |
| 295 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 296 | int x; |
| 297 | zArg += 2; |
| 298 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 299 | v = (v<<4) + x; |
| 300 | zArg++; |
| 301 | } |
| 302 | }else{ |
| 303 | while( isdigit(zArg[0]) ){ |
| 304 | v = v*10 + zArg[0] - '0'; |
| 305 | zArg++; |
| 306 | } |
| 307 | } |
| 308 | for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ |
| 309 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 310 | v *= aMult[i].iMult; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | if( v>0x7fffffff ) abendError("parameter too large - max 2147483648"); |
| 315 | return (int)(isNeg? -v : v); |
| 316 | } |
| 317 | |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 318 | |
| 319 | int main(int argc, char **argv){ |
| 320 | char *zIn = 0; /* Input text */ |
| 321 | int nAlloc = 0; /* Number of bytes allocated for zIn[] */ |
| 322 | int nIn = 0; /* Number of bytes of zIn[] used */ |
| 323 | size_t got; /* Bytes read from input */ |
| 324 | FILE *in = stdin; /* Where to read SQL text from */ |
| 325 | int rc = SQLITE_OK; /* Result codes from API functions */ |
| 326 | int i; /* Loop counter */ |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 327 | int iNext; /* Next block of SQL */ |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 328 | sqlite3 *db; /* Open database */ |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 329 | sqlite3 *dbInit = 0; /* On-disk database used to initialize the in-memory db */ |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 330 | const char *zInitDb = 0;/* Name of the initialization database file */ |
| 331 | char *zErrMsg = 0; /* Error message returned from sqlite3_exec() */ |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 332 | const char *zEncoding = 0; /* --utf16be or --utf16le */ |
| 333 | int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ |
| 334 | int nLook = 0, szLook = 0; /* --lookaside configuration */ |
| 335 | int nPCache = 0, szPCache = 0;/* --pcache configuration */ |
| 336 | int nScratch = 0, szScratch=0;/* --scratch configuration */ |
| 337 | int pageSize = 0; /* Desired page size. 0 means default */ |
| 338 | void *pHeap = 0; /* Allocated heap space */ |
| 339 | void *pLook = 0; /* Allocated lookaside space */ |
| 340 | void *pPCache = 0; /* Allocated storage for pcache */ |
| 341 | void *pScratch = 0; /* Allocated storage for scratch */ |
| 342 | int doAutovac = 0; /* True for --autovacuum */ |
| 343 | |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 344 | |
| 345 | g.zArgv0 = argv[0]; |
| 346 | for(i=1; i<argc; i++){ |
| 347 | const char *z = argv[i]; |
| 348 | if( z[0]=='-' ){ |
| 349 | z++; |
| 350 | if( z[0]=='-' ) z++; |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 351 | if( strcmp(z,"autovacuum")==0 ){ |
| 352 | doAutovac = 1; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 353 | }else |
| 354 | if( strcmp(z, "f")==0 && i+1<argc ){ |
| 355 | if( in!=stdin ) abendError("only one -f allowed"); |
| 356 | in = fopen(argv[++i],"rb"); |
| 357 | if( in==0 ) abendError("cannot open input file \"%s\"", argv[i]); |
| 358 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 359 | if( strcmp(z,"heap")==0 ){ |
| 360 | if( i>=argc-2 ) abendError("missing arguments on %s\n", argv[i]); |
| 361 | nHeap = integerValue(argv[i+1]); |
| 362 | mnHeap = integerValue(argv[i+2]); |
| 363 | i += 2; |
| 364 | }else |
| 365 | if( strcmp(z,"help")==0 ){ |
| 366 | showHelp(); |
| 367 | return 0; |
| 368 | }else |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 369 | if( strcmp(z, "initdb")==0 && i+1<argc ){ |
| 370 | if( zInitDb!=0 ) abendError("only one --initdb allowed"); |
| 371 | zInitDb = argv[++i]; |
| 372 | }else |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 373 | if( strcmp(z,"lookaside")==0 ){ |
| 374 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 375 | nLook = integerValue(argv[i+1]); |
| 376 | szLook = integerValue(argv[i+2]); |
| 377 | i += 2; |
| 378 | }else |
| 379 | if( strcmp(z,"pagesize")==0 ){ |
| 380 | if( i>=argc-1 ) abendError("missing argument on %s", argv[i]); |
| 381 | pageSize = integerValue(argv[++i]); |
| 382 | }else |
| 383 | if( strcmp(z,"pcache")==0 ){ |
| 384 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 385 | nPCache = integerValue(argv[i+1]); |
| 386 | szPCache = integerValue(argv[i+2]); |
| 387 | i += 2; |
| 388 | }else |
| 389 | if( strcmp(z,"scratch")==0 ){ |
| 390 | if( i>=argc-2 ) abendError("missing arguments on %s", argv[i]); |
| 391 | nScratch = integerValue(argv[i+1]); |
| 392 | szScratch = integerValue(argv[i+2]); |
| 393 | i += 2; |
| 394 | }else |
| 395 | if( strcmp(z,"utf16le")==0 ){ |
| 396 | zEncoding = "utf16le"; |
| 397 | }else |
| 398 | if( strcmp(z,"utf16be")==0 ){ |
| 399 | zEncoding = "utf16be"; |
| 400 | }else |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 401 | { |
| 402 | abendError("unknown option: %s", argv[i]); |
| 403 | } |
| 404 | }else{ |
| 405 | abendError("unknown argument: %s", argv[i]); |
| 406 | } |
| 407 | } |
| 408 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, 0); |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 409 | if( nHeap>0 ){ |
| 410 | pHeap = malloc( nHeap ); |
| 411 | if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap); |
| 412 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); |
| 413 | if( rc ) abendError("heap configuration failed: %d\n", rc); |
| 414 | } |
| 415 | if( nLook>0 ){ |
| 416 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); |
| 417 | if( szLook>0 ){ |
| 418 | pLook = malloc( nLook*szLook ); |
| 419 | if( pLook==0 ) fatalError("out of memory"); |
| 420 | } |
| 421 | } |
| 422 | if( nScratch>0 && szScratch>0 ){ |
| 423 | pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); |
| 424 | if( pScratch==0 ) fatalError("cannot allocate %lld-byte scratch", |
| 425 | nScratch*(sqlite3_int64)szScratch); |
| 426 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); |
| 427 | if( rc ) abendError("scratch configuration failed: %d\n", rc); |
| 428 | } |
| 429 | if( nPCache>0 && szPCache>0 ){ |
| 430 | pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); |
| 431 | if( pPCache==0 ) fatalError("cannot allocate %lld-byte pcache", |
| 432 | nPCache*(sqlite3_int64)szPCache); |
| 433 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); |
| 434 | if( rc ) abendError("pcache configuration failed: %d", rc); |
| 435 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 436 | while( !feof(in) ){ |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 437 | nAlloc += nAlloc+1000; |
| 438 | zIn = realloc(zIn, nAlloc); |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 439 | if( zIn==0 ) fatalError("out of memory"); |
| 440 | got = fread(zIn+nIn, 1, nAlloc-nIn-1, in); |
| 441 | nIn += (int)got; |
| 442 | zIn[nIn] = 0; |
| 443 | if( got==0 ) break; |
| 444 | } |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 445 | if( zInitDb ){ |
| 446 | rc = sqlite3_open_v2(zInitDb, &dbInit, SQLITE_OPEN_READONLY, 0); |
| 447 | if( rc!=SQLITE_OK ){ |
| 448 | abendError("unable to open initialization database \"%s\"", zInitDb); |
| 449 | } |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 450 | } |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 451 | for(i=0; i<nIn; i=iNext){ |
| 452 | char cSaved; |
| 453 | if( strncmp(&zIn[i], "/****<",6)==0 ){ |
| 454 | char *z = strstr(&zIn[i], ">****/"); |
| 455 | if( z ){ |
| 456 | z += 6; |
| 457 | printf("%.*s\n", (int)(z-&zIn[i]), &zIn[i]); |
| 458 | i += (int)(z-&zIn[i]); |
| 459 | } |
| 460 | } |
| 461 | for(iNext=i; iNext<nIn && strncmp(&zIn[iNext],"/****<",6)!=0; iNext++){} |
| 462 | |
| 463 | rc = sqlite3_open_v2( |
| 464 | "main.db", &db, |
| 465 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, |
| 466 | 0); |
| 467 | if( rc!=SQLITE_OK ){ |
| 468 | abendError("Unable to open the in-memory database"); |
| 469 | } |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 470 | if( pLook ){ |
| 471 | rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook, nLook); |
| 472 | if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc); |
| 473 | } |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 474 | if( zInitDb ){ |
| 475 | sqlite3_backup *pBackup; |
| 476 | pBackup = sqlite3_backup_init(db, "main", dbInit, "main"); |
| 477 | rc = sqlite3_backup_step(pBackup, -1); |
| 478 | if( rc!=SQLITE_DONE ){ |
| 479 | abendError("attempt to initialize the in-memory database failed (rc=%d)", |
| 480 | rc); |
| 481 | } |
| 482 | sqlite3_backup_finish(pBackup); |
| 483 | } |
| 484 | sqlite3_trace(db, traceCallback, 0); |
| 485 | sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); |
| 486 | sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); |
| 487 | sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 488 | if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); |
| 489 | if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); |
| 490 | if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 491 | cSaved = zIn[iNext]; |
| 492 | zIn[iNext] = 0; |
| 493 | printf("INPUT (offset: %d, size: %d): [%s]\n", |
| 494 | i, (int)strlen(&zIn[i]), &zIn[i]); |
| 495 | rc = sqlite3_exec(db, &zIn[i], execCallback, 0, &zErrMsg); |
| 496 | zIn[iNext] = cSaved; |
| 497 | |
| 498 | printf("RESULT-CODE: %d\n", rc); |
| 499 | if( zErrMsg ){ |
| 500 | printf("ERROR-MSG: [%s]\n", zErrMsg); |
| 501 | sqlite3_free(zErrMsg); |
| 502 | } |
| 503 | rc = sqlite3_close(db); |
| 504 | if( rc ){ |
| 505 | abendError("sqlite3_close() failed with rc=%d", rc); |
| 506 | } |
| 507 | if( sqlite3_memory_used()>0 ){ |
| 508 | abendError("memory in use after close: %lld bytes", sqlite3_memory_used()); |
| 509 | } |
| 510 | } |
| 511 | free(zIn); |
drh | 4a74d07 | 2015-04-20 18:58:38 +0000 | [diff] [blame^] | 512 | free(pHeap); |
| 513 | free(pLook); |
| 514 | free(pScratch); |
| 515 | free(pPCache); |
drh | f34e9aa | 2015-04-20 12:50:13 +0000 | [diff] [blame] | 516 | return 0; |
drh | 268e72f | 2015-04-17 14:30:49 +0000 | [diff] [blame] | 517 | } |