blob: 32690dc46603470c59e7b97b174b076344346c8a [file] [log] [blame]
drh3b74d032015-05-25 18:48:19 +00001/*
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**
drh00452192015-06-17 18:24:40 +000013** This is a utility program designed to aid running regressions tests on
14** the SQLite library using data from an external fuzzer, such as American
drh3b74d032015-05-25 18:48:19 +000015** Fuzzy Lop (AFL) (http://lcamtuf.coredump.cx/afl/).
16**
17** This program reads content from an SQLite database file with the following
18** schema:
19**
20** CREATE TABLE db(
21** dbid INTEGER PRIMARY KEY, -- database id
22** dbcontent BLOB -- database disk file image
23** );
24** CREATE TABLE xsql(
25** sqlid INTEGER PRIMARY KEY, -- SQL script id
26** sqltext TEXT -- Text of SQL statements to run
27** );
drh00452192015-06-17 18:24:40 +000028** CREATE TABLE IF NOT EXISTS readme(
29** msg TEXT -- Human-readable description of this test collection
30** );
drh3b74d032015-05-25 18:48:19 +000031**
32** For each database file in the DB table, the SQL text in the XSQL table
drh00452192015-06-17 18:24:40 +000033** is run against that database. All README.MSG values are printed prior
34** to the start of the test (unless the --quiet option is used). If the
35** DB table is empty, then all entries in XSQL are run against an empty
36** in-memory database.
37**
38** This program is looking for crashes, assertion faults, and/or memory leaks.
39** No attempt is made to verify the output. The assumption is that either all
40** of the database files or all of the SQL statements are malformed inputs,
41** generated by a fuzzer, that need to be checked to make sure they do not
42** present a security risk.
drh3b74d032015-05-25 18:48:19 +000043**
44** This program also includes some command-line options to help with
drh00452192015-06-17 18:24:40 +000045** creation and maintenance of the source content database. The command
46**
47** ./fuzzcheck database.db --load-sql FILE...
48**
49** Loads all FILE... arguments into the XSQL table. The --load-db option
50** works the same but loads the files into the DB table. The -m option can
51** be used to initialize the README table. The "database.db" file is created
52** if it does not previously exist. Example:
53**
54** ./fuzzcheck new.db --load-sql *.sql
55** ./fuzzcheck new.db --load-db *.db
56** ./fuzzcheck new.db -m 'New test cases'
57**
58** The three commands above will create the "new.db" file and initialize all
59** tables. Then do "./fuzzcheck new.db" to run the tests.
60**
61** DEBUGGING HINTS:
62**
63** If fuzzcheck does crash, it can be run in the debugger and the content
64** of the global variable g.zTextName[] will identify the specific XSQL and
65** DB values that were running when the crash occurred.
drh3b74d032015-05-25 18:48:19 +000066*/
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <stdarg.h>
71#include <ctype.h>
72#include "sqlite3.h"
drhc56fac72015-10-29 13:48:15 +000073#define ISSPACE(X) isspace((unsigned char)(X))
74#define ISDIGIT(X) isdigit((unsigned char)(X))
75
drh3b74d032015-05-25 18:48:19 +000076
drh94701b02015-06-24 13:25:34 +000077#ifdef __unix__
78# include <signal.h>
79# include <unistd.h>
80#endif
81
drh3b74d032015-05-25 18:48:19 +000082/*
83** Files in the virtual file system.
84*/
85typedef struct VFile VFile;
86struct VFile {
87 char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */
88 int sz; /* Size of the file in bytes */
89 int nRef; /* Number of references to this file */
90 unsigned char *a; /* Content of the file. From malloc() */
91};
92typedef struct VHandle VHandle;
93struct VHandle {
94 sqlite3_file base; /* Base class. Must be first */
95 VFile *pVFile; /* The underlying file */
96};
97
98/*
99** The value of a database file template, or of an SQL script
100*/
101typedef struct Blob Blob;
102struct Blob {
103 Blob *pNext; /* Next in a list */
104 int id; /* Id of this Blob */
drhe5c5f2c2015-05-26 00:28:08 +0000105 int seq; /* Sequence number */
drh3b74d032015-05-25 18:48:19 +0000106 int sz; /* Size of this Blob in bytes */
107 unsigned char a[1]; /* Blob content. Extra space allocated as needed. */
108};
109
110/*
111** Maximum number of files in the in-memory virtual filesystem.
112*/
113#define MX_FILE 10
114
115/*
116** Maximum allowed file size
117*/
118#define MX_FILE_SZ 10000000
119
120/*
121** All global variables are gathered into the "g" singleton.
122*/
123static struct GlobalVars {
124 const char *zArgv0; /* Name of program */
125 VFile aFile[MX_FILE]; /* The virtual filesystem */
126 int nDb; /* Number of template databases */
127 Blob *pFirstDb; /* Content of first template database */
128 int nSql; /* Number of SQL scripts */
129 Blob *pFirstSql; /* First SQL script */
130 char zTestName[100]; /* Name of current test */
131} g;
132
133/*
134** Print an error message and quit.
135*/
136static void fatalError(const char *zFormat, ...){
137 va_list ap;
138 if( g.zTestName[0] ){
139 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName);
140 }else{
141 fprintf(stderr, "%s: ", g.zArgv0);
142 }
143 va_start(ap, zFormat);
144 vfprintf(stderr, zFormat, ap);
145 va_end(ap);
146 fprintf(stderr, "\n");
147 exit(1);
148}
149
150/*
drh94701b02015-06-24 13:25:34 +0000151** Timeout handler
152*/
153#ifdef __unix__
154static void timeoutHandler(int NotUsed){
155 (void)NotUsed;
156 fatalError("timeout\n");
157}
158#endif
159
160/*
161** Set the an alarm to go off after N seconds. Disable the alarm
162** if N==0
163*/
164static void setAlarm(int N){
165#ifdef __unix__
166 alarm(N);
167#else
168 (void)N;
169#endif
170}
171
drh78057352015-06-24 23:17:35 +0000172#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh94701b02015-06-24 13:25:34 +0000173/*
drhd83e2832015-06-24 14:45:44 +0000174** This an SQL progress handler. After an SQL statement has run for
175** many steps, we want to interrupt it. This guards against infinite
176** loops from recursive common table expressions.
177**
178** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
179** In that case, hitting the progress handler is a fatal error.
180*/
181static int progressHandler(void *pVdbeLimitFlag){
182 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles");
183 return 1;
184}
drh78057352015-06-24 23:17:35 +0000185#endif
drhd83e2832015-06-24 14:45:44 +0000186
187/*
drh3b74d032015-05-25 18:48:19 +0000188** Reallocate memory. Show and error and quit if unable.
189*/
190static void *safe_realloc(void *pOld, int szNew){
191 void *pNew = realloc(pOld, szNew);
192 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew);
193 return pNew;
194}
195
196/*
197** Initialize the virtual file system.
198*/
199static void formatVfs(void){
200 int i;
201 for(i=0; i<MX_FILE; i++){
202 g.aFile[i].sz = -1;
203 g.aFile[i].zFilename = 0;
204 g.aFile[i].a = 0;
205 g.aFile[i].nRef = 0;
206 }
207}
208
209
210/*
211** Erase all information in the virtual file system.
212*/
213static void reformatVfs(void){
214 int i;
215 for(i=0; i<MX_FILE; i++){
216 if( g.aFile[i].sz<0 ) continue;
217 if( g.aFile[i].zFilename ){
218 free(g.aFile[i].zFilename);
219 g.aFile[i].zFilename = 0;
220 }
221 if( g.aFile[i].nRef>0 ){
222 fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef);
223 }
224 g.aFile[i].sz = -1;
225 free(g.aFile[i].a);
226 g.aFile[i].a = 0;
227 g.aFile[i].nRef = 0;
228 }
229}
230
231/*
232** Find a VFile by name
233*/
234static VFile *findVFile(const char *zName){
235 int i;
drha9542b12015-05-25 19:35:42 +0000236 if( zName==0 ) return 0;
drh3b74d032015-05-25 18:48:19 +0000237 for(i=0; i<MX_FILE; i++){
238 if( g.aFile[i].zFilename==0 ) continue;
239 if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i];
240 }
241 return 0;
242}
243
244/*
245** Find a VFile by name. Create it if it does not already exist and
246** initialize it to the size and content given.
247**
248** Return NULL only if the filesystem is full.
249*/
250static VFile *createVFile(const char *zName, int sz, unsigned char *pData){
251 VFile *pNew = findVFile(zName);
252 int i;
253 if( pNew ) return pNew;
254 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){}
255 if( i>=MX_FILE ) return 0;
256 pNew = &g.aFile[i];
drha9542b12015-05-25 19:35:42 +0000257 if( zName ){
258 pNew->zFilename = safe_realloc(0, strlen(zName)+1);
259 memcpy(pNew->zFilename, zName, strlen(zName)+1);
260 }else{
261 pNew->zFilename = 0;
262 }
drh3b74d032015-05-25 18:48:19 +0000263 pNew->nRef = 0;
264 pNew->sz = sz;
265 pNew->a = safe_realloc(0, sz);
266 if( sz>0 ) memcpy(pNew->a, pData, sz);
267 return pNew;
268}
269
270
271/*
272** Implementation of the "readfile(X)" SQL function. The entire content
273** of the file named X is read and returned as a BLOB. NULL is returned
274** if the file does not exist or is unreadable.
275*/
276static void readfileFunc(
277 sqlite3_context *context,
278 int argc,
279 sqlite3_value **argv
280){
281 const char *zName;
282 FILE *in;
283 long nIn;
284 void *pBuf;
285
286 zName = (const char*)sqlite3_value_text(argv[0]);
287 if( zName==0 ) return;
288 in = fopen(zName, "rb");
289 if( in==0 ) return;
290 fseek(in, 0, SEEK_END);
291 nIn = ftell(in);
292 rewind(in);
293 pBuf = sqlite3_malloc64( nIn );
294 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
295 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
296 }else{
297 sqlite3_free(pBuf);
298 }
299 fclose(in);
300}
301
302/*
drh40e0e0d2015-09-22 18:51:17 +0000303** Implementation of the "writefile(X,Y)" SQL function. The argument Y
304** is written into file X. The number of bytes written is returned. Or
305** NULL is returned if something goes wrong, such as being unable to open
306** file X for writing.
307*/
308static void writefileFunc(
309 sqlite3_context *context,
310 int argc,
311 sqlite3_value **argv
312){
313 FILE *out;
314 const char *z;
315 sqlite3_int64 rc;
316 const char *zFile;
317
318 (void)argc;
319 zFile = (const char*)sqlite3_value_text(argv[0]);
320 if( zFile==0 ) return;
321 out = fopen(zFile, "wb");
322 if( out==0 ) return;
323 z = (const char*)sqlite3_value_blob(argv[1]);
324 if( z==0 ){
325 rc = 0;
326 }else{
327 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
328 }
329 fclose(out);
330 sqlite3_result_int64(context, rc);
331}
332
333
334/*
drh3b74d032015-05-25 18:48:19 +0000335** Load a list of Blob objects from the database
336*/
337static void blobListLoadFromDb(
338 sqlite3 *db, /* Read from this database */
339 const char *zSql, /* Query used to extract the blobs */
drha9542b12015-05-25 19:35:42 +0000340 int onlyId, /* Only load where id is this value */
drh3b74d032015-05-25 18:48:19 +0000341 int *pN, /* OUT: Write number of blobs loaded here */
342 Blob **ppList /* OUT: Write the head of the blob list here */
343){
344 Blob head;
345 Blob *p;
346 sqlite3_stmt *pStmt;
347 int n = 0;
348 int rc;
drha9542b12015-05-25 19:35:42 +0000349 char *z2;
drh3b74d032015-05-25 18:48:19 +0000350
drha9542b12015-05-25 19:35:42 +0000351 if( onlyId>0 ){
352 z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId);
353 }else{
354 z2 = sqlite3_mprintf("%s", zSql);
355 }
356 rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0);
357 sqlite3_free(z2);
drh3b74d032015-05-25 18:48:19 +0000358 if( rc ) fatalError("%s", sqlite3_errmsg(db));
359 head.pNext = 0;
360 p = &head;
361 while( SQLITE_ROW==sqlite3_step(pStmt) ){
362 int sz = sqlite3_column_bytes(pStmt, 1);
363 Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz );
364 pNew->id = sqlite3_column_int(pStmt, 0);
365 pNew->sz = sz;
drhe5c5f2c2015-05-26 00:28:08 +0000366 pNew->seq = n++;
drh3b74d032015-05-25 18:48:19 +0000367 pNew->pNext = 0;
368 memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz);
369 pNew->a[sz] = 0;
370 p->pNext = pNew;
371 p = pNew;
drh3b74d032015-05-25 18:48:19 +0000372 }
373 sqlite3_finalize(pStmt);
374 *pN = n;
375 *ppList = head.pNext;
376}
377
378/*
379** Free a list of Blob objects
380*/
381static void blobListFree(Blob *p){
382 Blob *pNext;
383 while( p ){
384 pNext = p->pNext;
385 free(p);
386 p = pNext;
387 }
388}
389
390
391/* Return the current wall-clock time */
392static sqlite3_int64 timeOfDay(void){
393 static sqlite3_vfs *clockVfs = 0;
394 sqlite3_int64 t;
395 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
396 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
397 clockVfs->xCurrentTimeInt64(clockVfs, &t);
398 }else{
399 double r;
400 clockVfs->xCurrentTime(clockVfs, &r);
401 t = (sqlite3_int64)(r*86400000.0);
402 }
403 return t;
404}
405
406/* Methods for the VHandle object
407*/
408static int inmemClose(sqlite3_file *pFile){
409 VHandle *p = (VHandle*)pFile;
410 VFile *pVFile = p->pVFile;
411 pVFile->nRef--;
412 if( pVFile->nRef==0 && pVFile->zFilename==0 ){
413 pVFile->sz = -1;
414 free(pVFile->a);
415 pVFile->a = 0;
416 }
417 return SQLITE_OK;
418}
419static int inmemRead(
420 sqlite3_file *pFile, /* Read from this open file */
421 void *pData, /* Store content in this buffer */
422 int iAmt, /* Bytes of content */
423 sqlite3_int64 iOfst /* Start reading here */
424){
425 VHandle *pHandle = (VHandle*)pFile;
426 VFile *pVFile = pHandle->pVFile;
427 if( iOfst<0 || iOfst>=pVFile->sz ){
428 memset(pData, 0, iAmt);
429 return SQLITE_IOERR_SHORT_READ;
430 }
431 if( iOfst+iAmt>pVFile->sz ){
432 memset(pData, 0, iAmt);
drh1573dc32015-05-25 22:29:26 +0000433 iAmt = (int)(pVFile->sz - iOfst);
drh3b74d032015-05-25 18:48:19 +0000434 memcpy(pData, pVFile->a, iAmt);
435 return SQLITE_IOERR_SHORT_READ;
436 }
drhaca7ea12015-05-25 23:14:37 +0000437 memcpy(pData, pVFile->a + iOfst, iAmt);
drh3b74d032015-05-25 18:48:19 +0000438 return SQLITE_OK;
439}
440static int inmemWrite(
441 sqlite3_file *pFile, /* Write to this file */
442 const void *pData, /* Content to write */
443 int iAmt, /* bytes to write */
444 sqlite3_int64 iOfst /* Start writing here */
445){
446 VHandle *pHandle = (VHandle*)pFile;
447 VFile *pVFile = pHandle->pVFile;
448 if( iOfst+iAmt > pVFile->sz ){
drha9542b12015-05-25 19:35:42 +0000449 if( iOfst+iAmt >= MX_FILE_SZ ){
450 return SQLITE_FULL;
451 }
drh1573dc32015-05-25 22:29:26 +0000452 pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt));
drh908aced2015-05-26 16:12:45 +0000453 if( iOfst > pVFile->sz ){
454 memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz));
455 }
drh1573dc32015-05-25 22:29:26 +0000456 pVFile->sz = (int)(iOfst + iAmt);
drh3b74d032015-05-25 18:48:19 +0000457 }
458 memcpy(pVFile->a + iOfst, pData, iAmt);
459 return SQLITE_OK;
460}
461static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){
462 VHandle *pHandle = (VHandle*)pFile;
463 VFile *pVFile = pHandle->pVFile;
drh1573dc32015-05-25 22:29:26 +0000464 if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize;
drh3b74d032015-05-25 18:48:19 +0000465 return SQLITE_OK;
466}
467static int inmemSync(sqlite3_file *pFile, int flags){
468 return SQLITE_OK;
469}
470static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
471 *pSize = ((VHandle*)pFile)->pVFile->sz;
472 return SQLITE_OK;
473}
474static int inmemLock(sqlite3_file *pFile, int type){
475 return SQLITE_OK;
476}
477static int inmemUnlock(sqlite3_file *pFile, int type){
478 return SQLITE_OK;
479}
480static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){
481 *pOut = 0;
482 return SQLITE_OK;
483}
484static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){
485 return SQLITE_NOTFOUND;
486}
487static int inmemSectorSize(sqlite3_file *pFile){
488 return 512;
489}
490static int inmemDeviceCharacteristics(sqlite3_file *pFile){
491 return
492 SQLITE_IOCAP_SAFE_APPEND |
493 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
494 SQLITE_IOCAP_POWERSAFE_OVERWRITE;
495}
496
497
498/* Method table for VHandle
499*/
500static sqlite3_io_methods VHandleMethods = {
501 /* iVersion */ 1,
502 /* xClose */ inmemClose,
503 /* xRead */ inmemRead,
504 /* xWrite */ inmemWrite,
505 /* xTruncate */ inmemTruncate,
506 /* xSync */ inmemSync,
507 /* xFileSize */ inmemFileSize,
508 /* xLock */ inmemLock,
509 /* xUnlock */ inmemUnlock,
510 /* xCheck... */ inmemCheckReservedLock,
511 /* xFileCtrl */ inmemFileControl,
512 /* xSectorSz */ inmemSectorSize,
513 /* xDevchar */ inmemDeviceCharacteristics,
514 /* xShmMap */ 0,
515 /* xShmLock */ 0,
516 /* xShmBarrier */ 0,
517 /* xShmUnmap */ 0,
518 /* xFetch */ 0,
519 /* xUnfetch */ 0
520};
521
522/*
523** Open a new file in the inmem VFS. All files are anonymous and are
524** delete-on-close.
525*/
526static int inmemOpen(
527 sqlite3_vfs *pVfs,
528 const char *zFilename,
529 sqlite3_file *pFile,
530 int openFlags,
531 int *pOutFlags
532){
533 VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)"");
534 VHandle *pHandle = (VHandle*)pFile;
drha9542b12015-05-25 19:35:42 +0000535 if( pVFile==0 ){
536 return SQLITE_FULL;
537 }
drh3b74d032015-05-25 18:48:19 +0000538 pHandle->pVFile = pVFile;
539 pVFile->nRef++;
540 pFile->pMethods = &VHandleMethods;
541 if( pOutFlags ) *pOutFlags = openFlags;
542 return SQLITE_OK;
543}
544
545/*
546** Delete a file by name
547*/
548static int inmemDelete(
549 sqlite3_vfs *pVfs,
550 const char *zFilename,
551 int syncdir
552){
553 VFile *pVFile = findVFile(zFilename);
554 if( pVFile==0 ) return SQLITE_OK;
555 if( pVFile->nRef==0 ){
556 free(pVFile->zFilename);
557 pVFile->zFilename = 0;
558 pVFile->sz = -1;
559 free(pVFile->a);
560 pVFile->a = 0;
561 return SQLITE_OK;
562 }
563 return SQLITE_IOERR_DELETE;
564}
565
566/* Check for the existance of a file
567*/
568static int inmemAccess(
569 sqlite3_vfs *pVfs,
570 const char *zFilename,
571 int flags,
572 int *pResOut
573){
574 VFile *pVFile = findVFile(zFilename);
575 *pResOut = pVFile!=0;
576 return SQLITE_OK;
577}
578
579/* Get the canonical pathname for a file
580*/
581static int inmemFullPathname(
582 sqlite3_vfs *pVfs,
583 const char *zFilename,
584 int nOut,
585 char *zOut
586){
587 sqlite3_snprintf(nOut, zOut, "%s", zFilename);
588 return SQLITE_OK;
589}
590
591/* GetLastError() is never used */
592static int inmemGetLastError(sqlite3_vfs *pVfs, int n, char *z){
593 return SQLITE_OK;
594}
595
596/*
597** Register the VFS that reads from the g.aFile[] set of files.
598*/
599static void inmemVfsRegister(void){
600 static sqlite3_vfs inmemVfs;
601 sqlite3_vfs *pDefault = sqlite3_vfs_find(0);
602 inmemVfs.iVersion = 1;
603 inmemVfs.szOsFile = sizeof(VHandle);
604 inmemVfs.mxPathname = 200;
605 inmemVfs.zName = "inmem";
606 inmemVfs.xOpen = inmemOpen;
607 inmemVfs.xDelete = inmemDelete;
608 inmemVfs.xAccess = inmemAccess;
609 inmemVfs.xFullPathname = inmemFullPathname;
610 inmemVfs.xRandomness = pDefault->xRandomness;
611 inmemVfs.xSleep = pDefault->xSleep;
612 inmemVfs.xCurrentTime = pDefault->xCurrentTime;
613 inmemVfs.xGetLastError = inmemGetLastError;
614 sqlite3_vfs_register(&inmemVfs, 0);
615};
616
drh3b74d032015-05-25 18:48:19 +0000617/*
drhe5c5f2c2015-05-26 00:28:08 +0000618** Allowed values for the runFlags parameter to runSql()
619*/
620#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
621#define SQL_OUTPUT 0x0002 /* Show the SQL output */
622
623/*
drh3b74d032015-05-25 18:48:19 +0000624** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
625** stop if an error is encountered.
626*/
drhe5c5f2c2015-05-26 00:28:08 +0000627static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
drh3b74d032015-05-25 18:48:19 +0000628 const char *zMore;
629 sqlite3_stmt *pStmt;
630
631 while( zSql && zSql[0] ){
632 zMore = 0;
633 pStmt = 0;
634 sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore);
drh4ab31472015-05-25 22:17:06 +0000635 if( zMore==zSql ) break;
drhe5c5f2c2015-05-26 00:28:08 +0000636 if( runFlags & SQL_TRACE ){
drh4ab31472015-05-25 22:17:06 +0000637 const char *z = zSql;
638 int n;
drhc56fac72015-10-29 13:48:15 +0000639 while( z<zMore && ISSPACE(z[0]) ) z++;
drh4ab31472015-05-25 22:17:06 +0000640 n = (int)(zMore - z);
drhc56fac72015-10-29 13:48:15 +0000641 while( n>0 && ISSPACE(z[n-1]) ) n--;
drh4ab31472015-05-25 22:17:06 +0000642 if( n==0 ) break;
643 if( pStmt==0 ){
644 printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
645 }else{
646 printf("TRACE: %.*s\n", n, z);
647 }
648 }
drh3b74d032015-05-25 18:48:19 +0000649 zSql = zMore;
650 if( pStmt ){
drhe5c5f2c2015-05-26 00:28:08 +0000651 if( (runFlags & SQL_OUTPUT)==0 ){
652 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
653 }else{
654 int nCol = -1;
655 while( SQLITE_ROW==sqlite3_step(pStmt) ){
656 int i;
657 if( nCol<0 ){
658 nCol = sqlite3_column_count(pStmt);
659 }else if( nCol>0 ){
660 printf("--------------------------------------------\n");
661 }
662 for(i=0; i<nCol; i++){
663 int eType = sqlite3_column_type(pStmt,i);
664 printf("%s = ", sqlite3_column_name(pStmt,i));
665 switch( eType ){
666 case SQLITE_NULL: {
667 printf("NULL\n");
668 break;
669 }
670 case SQLITE_INTEGER: {
671 printf("INT %s\n", sqlite3_column_text(pStmt,i));
672 break;
673 }
674 case SQLITE_FLOAT: {
675 printf("FLOAT %s\n", sqlite3_column_text(pStmt,i));
676 break;
677 }
678 case SQLITE_TEXT: {
679 printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i));
680 break;
681 }
682 case SQLITE_BLOB: {
683 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i));
684 break;
685 }
686 }
687 }
688 }
689 }
drh3b74d032015-05-25 18:48:19 +0000690 sqlite3_finalize(pStmt);
drh3b74d032015-05-25 18:48:19 +0000691 }
692 }
693}
694
drha9542b12015-05-25 19:35:42 +0000695/*
drh9a645862015-06-24 12:44:42 +0000696** Rebuild the database file.
697**
698** (1) Remove duplicate entries
699** (2) Put all entries in order
700** (3) Vacuum
701*/
702static void rebuild_database(sqlite3 *db){
703 int rc;
704 rc = sqlite3_exec(db,
705 "BEGIN;\n"
706 "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
707 "DELETE FROM db;\n"
708 "INSERT INTO db(dbid, dbcontent) SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
709 "DROP TABLE dbx;\n"
710 "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql;\n"
711 "DELETE FROM xsql;\n"
712 "INSERT INTO xsql(sqlid,sqltext) SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
713 "DROP TABLE sx;\n"
714 "COMMIT;\n"
715 "PRAGMA page_size=1024;\n"
716 "VACUUM;\n", 0, 0, 0);
717 if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db));
718}
719
720/*
drh53e66c32015-07-24 15:49:23 +0000721** Return the value of a hexadecimal digit. Return -1 if the input
722** is not a hex digit.
723*/
724static int hexDigitValue(char c){
725 if( c>='0' && c<='9' ) return c - '0';
726 if( c>='a' && c<='f' ) return c - 'a' + 10;
727 if( c>='A' && c<='F' ) return c - 'A' + 10;
728 return -1;
729}
730
731/*
732** Interpret zArg as an integer value, possibly with suffixes.
733*/
734static int integerValue(const char *zArg){
735 sqlite3_int64 v = 0;
736 static const struct { char *zSuffix; int iMult; } aMult[] = {
737 { "KiB", 1024 },
738 { "MiB", 1024*1024 },
739 { "GiB", 1024*1024*1024 },
740 { "KB", 1000 },
741 { "MB", 1000000 },
742 { "GB", 1000000000 },
743 { "K", 1000 },
744 { "M", 1000000 },
745 { "G", 1000000000 },
746 };
747 int i;
748 int isNeg = 0;
749 if( zArg[0]=='-' ){
750 isNeg = 1;
751 zArg++;
752 }else if( zArg[0]=='+' ){
753 zArg++;
754 }
755 if( zArg[0]=='0' && zArg[1]=='x' ){
756 int x;
757 zArg += 2;
758 while( (x = hexDigitValue(zArg[0]))>=0 ){
759 v = (v<<4) + x;
760 zArg++;
761 }
762 }else{
drhc56fac72015-10-29 13:48:15 +0000763 while( ISDIGIT(zArg[0]) ){
drh53e66c32015-07-24 15:49:23 +0000764 v = v*10 + zArg[0] - '0';
765 zArg++;
766 }
767 }
768 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
769 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
770 v *= aMult[i].iMult;
771 break;
772 }
773 }
774 if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648");
775 return (int)(isNeg? -v : v);
776}
777
778/*
drha9542b12015-05-25 19:35:42 +0000779** Print sketchy documentation for this utility program
780*/
781static void showHelp(void){
782 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
783 printf(
784"Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
785"each database, checking for crashes and memory leaks.\n"
786"Options:\n"
drh1421d982015-05-27 03:46:18 +0000787" --cell-size-check Set the PRAGMA cell_size_check=ON\n"
drha9542b12015-05-25 19:35:42 +0000788" --dbid N Use only the database where dbid=N\n"
drh40e0e0d2015-09-22 18:51:17 +0000789" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
790" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
drhd83e2832015-06-24 14:45:44 +0000791" --help Show this help text\n"
drha9542b12015-05-25 19:35:42 +0000792" -q Reduced output\n"
793" --quiet Reduced output\n"
drh53e66c32015-07-24 15:49:23 +0000794" --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
drhd83e2832015-06-24 14:45:44 +0000795" --limit-vdbe Panic if an sync SQL runs for more than 100,000 cycles\n"
drha9542b12015-05-25 19:35:42 +0000796" --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n"
797" --load-db ARGS... Load template databases from files into SOURCE_DB\n"
drhd9972ef2015-05-26 17:57:56 +0000798" -m TEXT Add a description to the database\n"
drh15b31282015-05-25 21:59:05 +0000799" --native-vfs Use the native VFS for initially empty database files\n"
drh9a645862015-06-24 12:44:42 +0000800" --rebuild Rebuild and vacuum the database file\n"
drhe5c5f2c2015-05-26 00:28:08 +0000801" --result-trace Show the results of each SQL command\n"
drha9542b12015-05-25 19:35:42 +0000802" --sqlid N Use only SQL where sqlid=N\n"
drh9cdd1022015-09-22 17:46:11 +0000803" --timeout N Abort if any single test case needs more than N seconds\n"
drha9542b12015-05-25 19:35:42 +0000804" -v Increased output\n"
805" --verbose Increased output\n"
806 );
807}
808
drh3b74d032015-05-25 18:48:19 +0000809int main(int argc, char **argv){
810 sqlite3_int64 iBegin; /* Start time of this program */
drh3b74d032015-05-25 18:48:19 +0000811 int quietFlag = 0; /* True if --quiet or -q */
812 int verboseFlag = 0; /* True if --verbose or -v */
813 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */
814 int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sql */
815 sqlite3 *db = 0; /* The open database connection */
drhd9972ef2015-05-26 17:57:56 +0000816 sqlite3_stmt *pStmt; /* A prepared statement */
drh3b74d032015-05-25 18:48:19 +0000817 int rc; /* Result code from SQLite interface calls */
818 Blob *pSql; /* For looping over SQL scripts */
819 Blob *pDb; /* For looping over template databases */
820 int i; /* Loop index for the argv[] loop */
drha9542b12015-05-25 19:35:42 +0000821 int onlySqlid = -1; /* --sqlid */
822 int onlyDbid = -1; /* --dbid */
drh15b31282015-05-25 21:59:05 +0000823 int nativeFlag = 0; /* --native-vfs */
drh9a645862015-06-24 12:44:42 +0000824 int rebuildFlag = 0; /* --rebuild */
drhd83e2832015-06-24 14:45:44 +0000825 int vdbeLimitFlag = 0; /* --limit-vdbe */
drh94701b02015-06-24 13:25:34 +0000826 int timeoutTest = 0; /* undocumented --timeout-test flag */
drhe5c5f2c2015-05-26 00:28:08 +0000827 int runFlags = 0; /* Flags sent to runSql() */
drhd9972ef2015-05-26 17:57:56 +0000828 char *zMsg = 0; /* Add this message */
829 int nSrcDb = 0; /* Number of source databases */
830 char **azSrcDb = 0; /* Array of source database names */
831 int iSrcDb; /* Loop over all source databases */
832 int nTest = 0; /* Total number of tests performed */
833 char *zDbName = ""; /* Appreviated name of a source database */
drh4d6fda72015-05-26 18:58:32 +0000834 const char *zFailCode = 0; /* Value of the TEST_FAILURE environment variable */
drh1421d982015-05-27 03:46:18 +0000835 int cellSzCkFlag = 0; /* --cell-size-check */
drhd83e2832015-06-24 14:45:44 +0000836 int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz */
drhd4ddcbc2015-06-25 02:25:28 +0000837 int iTimeout = 120; /* Default 120-second timeout */
drh53e66c32015-07-24 15:49:23 +0000838 int nMem = 0; /* Memory limit */
drh40e0e0d2015-09-22 18:51:17 +0000839 char *zExpDb = 0; /* Write Databases to files in this directory */
840 char *zExpSql = 0; /* Write SQL to files in this directory */
drh6653fbe2015-11-13 20:52:49 +0000841 void *pHeap = 0; /* Heap for use by SQLite */
drh3b74d032015-05-25 18:48:19 +0000842
843 iBegin = timeOfDay();
drh94701b02015-06-24 13:25:34 +0000844#ifdef __unix__
845 signal(SIGALRM, timeoutHandler);
846#endif
drh3b74d032015-05-25 18:48:19 +0000847 g.zArgv0 = argv[0];
drh4d6fda72015-05-26 18:58:32 +0000848 zFailCode = getenv("TEST_FAILURE");
drh3b74d032015-05-25 18:48:19 +0000849 for(i=1; i<argc; i++){
850 const char *z = argv[i];
851 if( z[0]=='-' ){
852 z++;
853 if( z[0]=='-' ) z++;
drh1421d982015-05-27 03:46:18 +0000854 if( strcmp(z,"cell-size-check")==0 ){
855 cellSzCkFlag = 1;
856 }else
drha9542b12015-05-25 19:35:42 +0000857 if( strcmp(z,"dbid")==0 ){
858 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +0000859 onlyDbid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +0000860 }else
drh40e0e0d2015-09-22 18:51:17 +0000861 if( strcmp(z,"export-db")==0 ){
862 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
863 zExpDb = argv[++i];
864 }else
865 if( strcmp(z,"export-sql")==0 ){
866 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
867 zExpSql = argv[++i];
868 }else
drh3b74d032015-05-25 18:48:19 +0000869 if( strcmp(z,"help")==0 ){
870 showHelp();
871 return 0;
872 }else
drh53e66c32015-07-24 15:49:23 +0000873 if( strcmp(z,"limit-mem")==0 ){
874 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
875 nMem = integerValue(argv[++i]);
876 }else
drhd83e2832015-06-24 14:45:44 +0000877 if( strcmp(z,"limit-vdbe")==0 ){
878 vdbeLimitFlag = 1;
879 }else
drh3b74d032015-05-25 18:48:19 +0000880 if( strcmp(z,"load-sql")==0 ){
drhe5c5f2c2015-05-26 00:28:08 +0000881 zInsSql = "INSERT INTO xsql(sqltext) VALUES(CAST(readfile(?1) AS text))";
drh3b74d032015-05-25 18:48:19 +0000882 iFirstInsArg = i+1;
883 break;
884 }else
885 if( strcmp(z,"load-db")==0 ){
886 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
887 iFirstInsArg = i+1;
888 break;
889 }else
drhd9972ef2015-05-26 17:57:56 +0000890 if( strcmp(z,"m")==0 ){
891 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
892 zMsg = argv[++i];
893 }else
drh15b31282015-05-25 21:59:05 +0000894 if( strcmp(z,"native-vfs")==0 ){
895 nativeFlag = 1;
896 }else
drh3b74d032015-05-25 18:48:19 +0000897 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
898 quietFlag = 1;
899 verboseFlag = 0;
900 }else
drh9a645862015-06-24 12:44:42 +0000901 if( strcmp(z,"rebuild")==0 ){
902 rebuildFlag = 1;
903 }else
drhe5c5f2c2015-05-26 00:28:08 +0000904 if( strcmp(z,"result-trace")==0 ){
905 runFlags |= SQL_OUTPUT;
906 }else
drha9542b12015-05-25 19:35:42 +0000907 if( strcmp(z,"sqlid")==0 ){
908 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +0000909 onlySqlid = integerValue(argv[++i]);
drha9542b12015-05-25 19:35:42 +0000910 }else
drh92298632015-06-24 23:44:30 +0000911 if( strcmp(z,"timeout")==0 ){
912 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
drh53e66c32015-07-24 15:49:23 +0000913 iTimeout = integerValue(argv[++i]);
drh92298632015-06-24 23:44:30 +0000914 }else
drh94701b02015-06-24 13:25:34 +0000915 if( strcmp(z,"timeout-test")==0 ){
916 timeoutTest = 1;
917#ifndef __unix__
918 fatalError("timeout is not available on non-unix systems");
919#endif
920 }else
drh3b74d032015-05-25 18:48:19 +0000921 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){
922 quietFlag = 0;
923 verboseFlag = 1;
drhe5c5f2c2015-05-26 00:28:08 +0000924 runFlags |= SQL_TRACE;
drh3b74d032015-05-25 18:48:19 +0000925 }else
926 {
927 fatalError("unknown option: %s", argv[i]);
928 }
929 }else{
drhd9972ef2015-05-26 17:57:56 +0000930 nSrcDb++;
931 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0]));
932 azSrcDb[nSrcDb-1] = argv[i];
drh3b74d032015-05-25 18:48:19 +0000933 }
934 }
drhd9972ef2015-05-26 17:57:56 +0000935 if( nSrcDb==0 ) fatalError("no source database specified");
936 if( nSrcDb>1 ){
937 if( zMsg ){
938 fatalError("cannot change the description of more than one database");
drh3b74d032015-05-25 18:48:19 +0000939 }
drhd9972ef2015-05-26 17:57:56 +0000940 if( zInsSql ){
941 fatalError("cannot import into more than one database");
942 }
drh3b74d032015-05-25 18:48:19 +0000943 }
944
drhd9972ef2015-05-26 17:57:56 +0000945 /* Process each source database separately */
946 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
947 rc = sqlite3_open(azSrcDb[iSrcDb], &db);
948 if( rc ){
949 fatalError("cannot open source database %s - %s",
950 azSrcDb[iSrcDb], sqlite3_errmsg(db));
951 }
drh9a645862015-06-24 12:44:42 +0000952 rc = sqlite3_exec(db,
drhd9972ef2015-05-26 17:57:56 +0000953 "CREATE TABLE IF NOT EXISTS db(\n"
954 " dbid INTEGER PRIMARY KEY, -- database id\n"
955 " dbcontent BLOB -- database disk file image\n"
956 ");\n"
957 "CREATE TABLE IF NOT EXISTS xsql(\n"
958 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
959 " sqltext TEXT -- Text of SQL statements to run\n"
960 ");"
961 "CREATE TABLE IF NOT EXISTS readme(\n"
962 " msg TEXT -- Human-readable description of this file\n"
963 ");", 0, 0, 0);
964 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db));
965 if( zMsg ){
966 char *zSql;
967 zSql = sqlite3_mprintf(
968 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg);
969 rc = sqlite3_exec(db, zSql, 0, 0, 0);
970 sqlite3_free(zSql);
971 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db));
972 }
973 if( zInsSql ){
974 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
975 readfileFunc, 0, 0);
976 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
977 if( rc ) fatalError("cannot prepare statement [%s]: %s",
978 zInsSql, sqlite3_errmsg(db));
979 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
980 if( rc ) fatalError("cannot start a transaction");
981 for(i=iFirstInsArg; i<argc; i++){
982 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC);
983 sqlite3_step(pStmt);
984 rc = sqlite3_reset(pStmt);
985 if( rc ) fatalError("insert failed for %s", argv[i]);
drh3b74d032015-05-25 18:48:19 +0000986 }
drhd9972ef2015-05-26 17:57:56 +0000987 sqlite3_finalize(pStmt);
988 rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
989 if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db));
drh9a645862015-06-24 12:44:42 +0000990 rebuild_database(db);
drh3b74d032015-05-25 18:48:19 +0000991 sqlite3_close(db);
drhd9972ef2015-05-26 17:57:56 +0000992 return 0;
drh3b74d032015-05-25 18:48:19 +0000993 }
drh40e0e0d2015-09-22 18:51:17 +0000994 if( zExpDb!=0 || zExpSql!=0 ){
995 sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
996 writefileFunc, 0, 0);
997 if( zExpDb!=0 ){
998 const char *zExDb =
999 "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
1000 " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
1001 " FROM db WHERE ?2<0 OR dbid=?2;";
1002 rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
1003 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1004 zExDb, sqlite3_errmsg(db));
1005 sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
1006 SQLITE_STATIC, SQLITE_UTF8);
1007 sqlite3_bind_int(pStmt, 2, onlyDbid);
1008 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1009 printf("write db-%d (%d bytes) into %s\n",
1010 sqlite3_column_int(pStmt,1),
1011 sqlite3_column_int(pStmt,3),
1012 sqlite3_column_text(pStmt,2));
1013 }
1014 sqlite3_finalize(pStmt);
1015 }
1016 if( zExpSql!=0 ){
1017 const char *zExSql =
1018 "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
1019 " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
1020 " FROM xsql WHERE ?2<0 OR sqlid=?2;";
1021 rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
1022 if( rc ) fatalError("cannot prepare statement [%s]: %s",
1023 zExSql, sqlite3_errmsg(db));
1024 sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
1025 SQLITE_STATIC, SQLITE_UTF8);
1026 sqlite3_bind_int(pStmt, 2, onlySqlid);
1027 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1028 printf("write sql-%d (%d bytes) into %s\n",
1029 sqlite3_column_int(pStmt,1),
1030 sqlite3_column_int(pStmt,3),
1031 sqlite3_column_text(pStmt,2));
1032 }
1033 sqlite3_finalize(pStmt);
1034 }
1035 sqlite3_close(db);
1036 return 0;
1037 }
drhd9972ef2015-05-26 17:57:56 +00001038
1039 /* Load all SQL script content and all initial database images from the
1040 ** source db
1041 */
1042 blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid,
1043 &g.nSql, &g.pFirstSql);
1044 if( g.nSql==0 ) fatalError("need at least one SQL script");
1045 blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid,
1046 &g.nDb, &g.pFirstDb);
1047 if( g.nDb==0 ){
1048 g.pFirstDb = safe_realloc(0, sizeof(Blob));
1049 memset(g.pFirstDb, 0, sizeof(Blob));
1050 g.pFirstDb->id = 1;
1051 g.pFirstDb->seq = 0;
1052 g.nDb = 1;
drhd83e2832015-06-24 14:45:44 +00001053 sqlFuzz = 1;
drhd9972ef2015-05-26 17:57:56 +00001054 }
1055
1056 /* Print the description, if there is one */
1057 if( !quietFlag ){
drhd9972ef2015-05-26 17:57:56 +00001058 zDbName = azSrcDb[iSrcDb];
1059 i = strlen(zDbName) - 1;
1060 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
1061 zDbName += i;
1062 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
1063 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1064 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0));
1065 }
1066 sqlite3_finalize(pStmt);
1067 }
drh9a645862015-06-24 12:44:42 +00001068
1069 /* Rebuild the database, if requested */
1070 if( rebuildFlag ){
1071 if( !quietFlag ){
1072 printf("%s: rebuilding... ", zDbName);
1073 fflush(stdout);
1074 }
1075 rebuild_database(db);
1076 if( !quietFlag ) printf("done\n");
1077 }
drhd9972ef2015-05-26 17:57:56 +00001078
1079 /* Close the source database. Verify that no SQLite memory allocations are
1080 ** outstanding.
1081 */
1082 sqlite3_close(db);
1083 if( sqlite3_memory_used()>0 ){
1084 fatalError("SQLite has memory in use before the start of testing");
1085 }
drh53e66c32015-07-24 15:49:23 +00001086
1087 /* Limit available memory, if requested */
1088 if( nMem>0 ){
drh53e66c32015-07-24 15:49:23 +00001089 sqlite3_shutdown();
1090 pHeap = malloc(nMem);
1091 if( pHeap==0 ){
1092 fatalError("failed to allocate %d bytes of heap memory", nMem);
1093 }
1094 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMem, 128);
1095 }
drhd9972ef2015-05-26 17:57:56 +00001096
1097 /* Register the in-memory virtual filesystem
1098 */
1099 formatVfs();
1100 inmemVfsRegister();
1101
1102 /* Run a test using each SQL script against each database.
1103 */
1104 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName);
1105 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){
1106 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){
1107 int openFlags;
1108 const char *zVfs = "inmem";
1109 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d",
1110 pSql->id, pDb->id);
1111 if( verboseFlag ){
1112 printf("%s\n", g.zTestName);
1113 fflush(stdout);
1114 }else if( !quietFlag ){
1115 static int prevAmt = -1;
1116 int idx = pSql->seq*g.nDb + pDb->id - 1;
1117 int amt = idx*10/(g.nDb*g.nSql);
1118 if( amt!=prevAmt ){
1119 printf(" %d%%", amt*10);
1120 fflush(stdout);
1121 prevAmt = amt;
1122 }
1123 }
1124 createVFile("main.db", pDb->sz, pDb->a);
1125 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
1126 if( nativeFlag && pDb->sz==0 ){
1127 openFlags |= SQLITE_OPEN_MEMORY;
1128 zVfs = 0;
1129 }
1130 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs);
1131 if( rc ) fatalError("cannot open inmem database");
drh1421d982015-05-27 03:46:18 +00001132 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags);
drh92298632015-06-24 23:44:30 +00001133 setAlarm(iTimeout);
drh78057352015-06-24 23:17:35 +00001134#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhd83e2832015-06-24 14:45:44 +00001135 if( sqlFuzz || vdbeLimitFlag ){
1136 sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag);
1137 }
drh78057352015-06-24 23:17:35 +00001138#endif
drh94701b02015-06-24 13:25:34 +00001139 do{
1140 runSql(db, (char*)pSql->a, runFlags);
1141 }while( timeoutTest );
1142 setAlarm(0);
drhd9972ef2015-05-26 17:57:56 +00001143 sqlite3_close(db);
1144 if( sqlite3_memory_used()>0 ) fatalError("memory leak");
1145 reformatVfs();
1146 nTest++;
1147 g.zTestName[0] = 0;
drh4d6fda72015-05-26 18:58:32 +00001148
1149 /* Simulate an error if the TEST_FAILURE environment variable is "5".
1150 ** This is used to verify that automated test script really do spot
1151 ** errors that occur in this test program.
1152 */
1153 if( zFailCode ){
1154 if( zFailCode[0]=='5' && zFailCode[1]==0 ){
1155 fatalError("simulated failure");
1156 }else if( zFailCode[0]!=0 ){
1157 /* If TEST_FAILURE is something other than 5, just exit the test
1158 ** early */
1159 printf("\nExit early due to TEST_FAILURE being set\n");
1160 iSrcDb = nSrcDb-1;
1161 goto sourcedb_cleanup;
1162 }
1163 }
drhd9972ef2015-05-26 17:57:56 +00001164 }
1165 }
1166 if( !quietFlag && !verboseFlag ){
1167 printf(" 100%% - %d tests\n", g.nDb*g.nSql);
1168 }
1169
1170 /* Clean up at the end of processing a single source database
1171 */
drh4d6fda72015-05-26 18:58:32 +00001172 sourcedb_cleanup:
drhd9972ef2015-05-26 17:57:56 +00001173 blobListFree(g.pFirstSql);
1174 blobListFree(g.pFirstDb);
1175 reformatVfs();
1176
1177 } /* End loop over all source databases */
drh3b74d032015-05-25 18:48:19 +00001178
1179 if( !quietFlag ){
1180 sqlite3_int64 iElapse = timeOfDay() - iBegin;
drhd9972ef2015-05-26 17:57:56 +00001181 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
1182 "SQLite %s %s\n",
1183 nTest, (int)(iElapse/1000), (int)(iElapse%1000),
drh3b74d032015-05-25 18:48:19 +00001184 sqlite3_libversion(), sqlite3_sourceid());
1185 }
drhf74d35b2015-05-27 18:19:50 +00001186 free(azSrcDb);
drh6653fbe2015-11-13 20:52:49 +00001187 free(pHeap);
drh3b74d032015-05-25 18:48:19 +00001188 return 0;
1189}