4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This program is designed for fuzz-testing SQLite database files.
15 ** This program reads fuzzed database files from the disk files named
16 ** on the command-line. Each database is loaded into an in-memory
17 ** filesystem so that the original database file is unmolested.
19 ** The fuzzed database is then opened, and series of SQL statements
20 ** are run against the database to ensure that SQLite can safely handle
21 ** the fuzzed database.
29 #define ISSPACE(X) isspace((unsigned char)(X))
30 #define ISDIGIT(X) isdigit((unsigned char)(X))
38 ** Print sketchy documentation for this utility program
40 static void showHelp(const char *zArgv0
){
41 printf("Usage: %s [options] DATABASE ...\n", zArgv0
);
43 "Read databases into an in-memory filesystem. Run test SQL as specified\n"
44 "by command-line arguments or from\n"
46 " SELECT group_concat(sql) FROM autoexec;\n"
49 " --help Show this help text\n"
50 " -q|--quiet Reduced output\n"
51 " --limit-mem N Limit memory used by test SQLite instances to N bytes\n"
52 " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
53 " --no-lookaside Disable the lookaside memory allocator\n"
54 " --timeout N Timeout after N seconds.\n"
55 " --trace Show the results of each SQL command\n"
56 " -v|--verbose Increased output. Repeat for more output.\n"
62 ** Print an error message and quit.
64 static void fatalError(const char *zFormat
, ...){
66 va_start(ap
, zFormat
);
67 vfprintf(stderr
, zFormat
, ap
);
69 fprintf(stderr
, "\n");
74 ** Files in the virtual file system.
76 typedef struct VFile VFile
;
77 typedef struct VHandle VHandle
;
79 char *zFilename
; /* Filename. NULL for delete-on-close. From malloc() */
80 int sz
; /* Size of the file in bytes */
81 int nRef
; /* Number of references to this file */
82 unsigned char *a
; /* Content of the file. From malloc() */
85 sqlite3_file base
; /* Base class. Must be first */
86 VFile
*pVFile
; /* The underlying file */
90 ** Maximum number of files in the in-memory virtual filesystem.
95 ** Maximum allowed file size
97 #define MX_FILE_SZ 1000000
100 ** All global variables are gathered into the "g" singleton.
102 static struct GlobalVars
{
103 VFile aFile
[MX_FILE
]; /* The virtual filesystem */
108 ** Initialize the virtual file system.
110 static void formatVfs(void){
112 for(i
=0; i
<MX_FILE
; i
++){
114 g
.aFile
[i
].zFilename
= 0;
122 ** Erase all information in the virtual file system.
124 static void reformatVfs(void){
126 for(i
=0; i
<MX_FILE
; i
++){
127 if( g
.aFile
[i
].sz
<0 ) continue;
128 if( g
.aFile
[i
].zFilename
){
129 free(g
.aFile
[i
].zFilename
);
130 g
.aFile
[i
].zFilename
= 0;
132 if( g
.aFile
[i
].nRef
>0 ){
133 fatalError("file %d still open. nRef=%d", i
, g
.aFile
[i
].nRef
);
143 ** Find a VFile by name
145 static VFile
*findVFile(const char *zName
){
147 if( zName
==0 ) return 0;
148 for(i
=0; i
<MX_FILE
; i
++){
149 if( g
.aFile
[i
].zFilename
==0 ) continue;
150 if( strcmp(g
.aFile
[i
].zFilename
, zName
)==0 ) return &g
.aFile
[i
];
156 ** Find a VFile called zName. Initialize it to the content of
157 ** disk file zDiskFile.
159 ** Return NULL if the filesystem is full.
161 static VFile
*createVFile(const char *zName
, const char *zDiskFile
){
162 VFile
*pNew
= findVFile(zName
);
167 if( pNew
) return pNew
;
168 for(i
=0; i
<MX_FILE
&& g
.aFile
[i
].sz
>=0; i
++){}
169 if( i
>=MX_FILE
) return 0;
171 in
= fopen(zDiskFile
, "rb");
172 if( in
==0 ) fatalError("no such file: \"%s\"", zDiskFile
);
173 fseek(in
, 0, SEEK_END
);
179 int nName
= (int)strlen(zName
)+1;
180 pNew
->zFilename
= malloc(nName
);
181 if( pNew
->zFilename
==0 ){
185 memcpy(pNew
->zFilename
, zName
, nName
);
191 pNew
->a
= malloc(sz
);
193 if( pNew
->a
==0 || fread(pNew
->a
, sz
, 1, in
)<1 ){
194 free(pNew
->zFilename
);
206 /* Methods for the VHandle object
208 static int inmemClose(sqlite3_file
*pFile
){
209 VHandle
*p
= (VHandle
*)pFile
;
210 VFile
*pVFile
= p
->pVFile
;
212 if( pVFile
->nRef
==0 && pVFile
->zFilename
==0 ){
219 static int inmemRead(
220 sqlite3_file
*pFile
, /* Read from this open file */
221 void *pData
, /* Store content in this buffer */
222 int iAmt
, /* Bytes of content */
223 sqlite3_int64 iOfst
/* Start reading here */
225 VHandle
*pHandle
= (VHandle
*)pFile
;
226 VFile
*pVFile
= pHandle
->pVFile
;
227 if( iOfst
<0 || iOfst
>=pVFile
->sz
){
228 memset(pData
, 0, iAmt
);
229 return SQLITE_IOERR_SHORT_READ
;
231 if( iOfst
+iAmt
>pVFile
->sz
){
232 memset(pData
, 0, iAmt
);
233 iAmt
= (int)(pVFile
->sz
- iOfst
);
234 memcpy(pData
, pVFile
->a
, iAmt
);
235 return SQLITE_IOERR_SHORT_READ
;
237 memcpy(pData
, pVFile
->a
+ iOfst
, iAmt
);
240 static int inmemWrite(
241 sqlite3_file
*pFile
, /* Write to this file */
242 const void *pData
, /* Content to write */
243 int iAmt
, /* bytes to write */
244 sqlite3_int64 iOfst
/* Start writing here */
246 VHandle
*pHandle
= (VHandle
*)pFile
;
247 VFile
*pVFile
= pHandle
->pVFile
;
248 if( iOfst
+iAmt
> pVFile
->sz
){
250 if( iOfst
+iAmt
>= MX_FILE_SZ
){
253 aNew
= realloc(pVFile
->a
, (int)(iOfst
+iAmt
));
258 if( iOfst
> pVFile
->sz
){
259 memset(pVFile
->a
+ pVFile
->sz
, 0, (int)(iOfst
- pVFile
->sz
));
261 pVFile
->sz
= (int)(iOfst
+ iAmt
);
263 memcpy(pVFile
->a
+ iOfst
, pData
, iAmt
);
266 static int inmemTruncate(sqlite3_file
*pFile
, sqlite3_int64 iSize
){
267 VHandle
*pHandle
= (VHandle
*)pFile
;
268 VFile
*pVFile
= pHandle
->pVFile
;
269 if( pVFile
->sz
>iSize
&& iSize
>=0 ) pVFile
->sz
= (int)iSize
;
272 static int inmemSync(sqlite3_file
*pFile
, int flags
){
275 static int inmemFileSize(sqlite3_file
*pFile
, sqlite3_int64
*pSize
){
276 *pSize
= ((VHandle
*)pFile
)->pVFile
->sz
;
279 static int inmemLock(sqlite3_file
*pFile
, int type
){
282 static int inmemUnlock(sqlite3_file
*pFile
, int type
){
285 static int inmemCheckReservedLock(sqlite3_file
*pFile
, int *pOut
){
289 static int inmemFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
290 return SQLITE_NOTFOUND
;
292 static int inmemSectorSize(sqlite3_file
*pFile
){
295 static int inmemDeviceCharacteristics(sqlite3_file
*pFile
){
297 SQLITE_IOCAP_SAFE_APPEND
|
298 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
|
299 SQLITE_IOCAP_POWERSAFE_OVERWRITE
;
303 /* Method table for VHandle
305 static sqlite3_io_methods VHandleMethods
= {
307 /* xClose */ inmemClose
,
308 /* xRead */ inmemRead
,
309 /* xWrite */ inmemWrite
,
310 /* xTruncate */ inmemTruncate
,
311 /* xSync */ inmemSync
,
312 /* xFileSize */ inmemFileSize
,
313 /* xLock */ inmemLock
,
314 /* xUnlock */ inmemUnlock
,
315 /* xCheck... */ inmemCheckReservedLock
,
316 /* xFileCtrl */ inmemFileControl
,
317 /* xSectorSz */ inmemSectorSize
,
318 /* xDevchar */ inmemDeviceCharacteristics
,
328 ** Open a new file in the inmem VFS. All files are anonymous and are
331 static int inmemOpen(
333 const char *zFilename
,
338 VFile
*pVFile
= createVFile(zFilename
, 0);
339 VHandle
*pHandle
= (VHandle
*)pFile
;
343 pHandle
->pVFile
= pVFile
;
345 pFile
->pMethods
= &VHandleMethods
;
346 if( pOutFlags
) *pOutFlags
= openFlags
;
351 ** Delete a file by name
353 static int inmemDelete(
355 const char *zFilename
,
358 VFile
*pVFile
= findVFile(zFilename
);
359 if( pVFile
==0 ) return SQLITE_OK
;
360 if( pVFile
->nRef
==0 ){
361 free(pVFile
->zFilename
);
362 pVFile
->zFilename
= 0;
368 return SQLITE_IOERR_DELETE
;
371 /* Check for the existance of a file
373 static int inmemAccess(
375 const char *zFilename
,
379 VFile
*pVFile
= findVFile(zFilename
);
380 *pResOut
= pVFile
!=0;
384 /* Get the canonical pathname for a file
386 static int inmemFullPathname(
388 const char *zFilename
,
392 sqlite3_snprintf(nOut
, zOut
, "%s", zFilename
);
397 ** Register the VFS that reads from the g.aFile[] set of files.
399 static void inmemVfsRegister(void){
400 static sqlite3_vfs inmemVfs
;
401 sqlite3_vfs
*pDefault
= sqlite3_vfs_find(0);
402 inmemVfs
.iVersion
= 3;
403 inmemVfs
.szOsFile
= sizeof(VHandle
);
404 inmemVfs
.mxPathname
= 200;
405 inmemVfs
.zName
= "inmem";
406 inmemVfs
.xOpen
= inmemOpen
;
407 inmemVfs
.xDelete
= inmemDelete
;
408 inmemVfs
.xAccess
= inmemAccess
;
409 inmemVfs
.xFullPathname
= inmemFullPathname
;
410 inmemVfs
.xRandomness
= pDefault
->xRandomness
;
411 inmemVfs
.xSleep
= pDefault
->xSleep
;
412 inmemVfs
.xCurrentTimeInt64
= pDefault
->xCurrentTimeInt64
;
413 sqlite3_vfs_register(&inmemVfs
, 0);
420 static void timeoutHandler(int NotUsed
){
422 fatalError("timeout\n");
427 ** Set the an alarm to go off after N seconds. Disable the alarm
430 static void setAlarm(int N
){
437 /***************************************************************************
438 ** String accumulator object
440 typedef struct Str Str
;
442 char *z
; /* The string. Memory from malloc() */
443 sqlite3_uint64 n
; /* Bytes of input used */
444 sqlite3_uint64 nAlloc
; /* Bytes allocated to z[] */
445 int oomErr
; /* OOM error has been seen */
448 /* Initialize a Str object */
449 static void StrInit(Str
*p
){
450 memset(p
, 0, sizeof(*p
));
453 /* Append text to the end of a Str object */
454 static void StrAppend(Str
*p
, const char *z
){
455 sqlite3_uint64 n
= strlen(z
);
456 if( p
->n
+ n
>= p
->nAlloc
){
459 if( p
->oomErr
) return;
460 nNew
= p
->nAlloc
*2 + 100 + n
;
461 zNew
= sqlite3_realloc64(p
->z
, nNew
);
464 memset(p
, 0, sizeof(*p
));
471 memcpy(p
->z
+ p
->n
, z
, (int)n
);
476 /* Return the current string content */
477 static char *StrStr(Str
*p
){
481 /* Free the string */
482 static void StrFree(Str
*p
){
488 ** Return the value of a hexadecimal digit. Return -1 if the input
489 ** is not a hex digit.
491 static int hexDigitValue(char c
){
492 if( c
>='0' && c
<='9' ) return c
- '0';
493 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
494 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
499 ** Interpret zArg as an integer value, possibly with suffixes.
501 static int integerValue(const char *zArg
){
503 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
505 { "MiB", 1024*1024 },
506 { "GiB", 1024*1024*1024 },
509 { "GB", 1000000000 },
519 }else if( zArg
[0]=='+' ){
522 if( zArg
[0]=='0' && zArg
[1]=='x' ){
525 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
530 while( ISDIGIT(zArg
[0]) ){
531 v
= v
*10 + zArg
[0] - '0';
535 for(i
=0; i
<sizeof(aMult
)/sizeof(aMult
[0]); i
++){
536 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
541 if( v
>0x7fffffff ) fatalError("parameter too large - max 2147483648");
542 return (int)(isNeg
? -v
: v
);
546 ** This callback is invoked by sqlite3_log().
548 static void sqlLog(void *pNotUsed
, int iErrCode
, const char *zMsg
){
549 printf("LOG: (%d) %s\n", iErrCode
, zMsg
);
553 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
555 ** This an SQL progress handler. After an SQL statement has run for
556 ** many steps, we want to interrupt it. This guards against infinite
557 ** loops from recursive common table expressions.
559 ** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
560 ** In that case, hitting the progress handler is a fatal error.
562 static int progressHandler(void *pVdbeLimitFlag
){
563 if( *(int*)pVdbeLimitFlag
) fatalError("too many VDBE cycles");
569 ** Allowed values for the runFlags parameter to runSql()
571 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
572 #define SQL_OUTPUT 0x0002 /* Show the SQL output */
575 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
576 ** stop if an error is encountered.
578 static void runSql(sqlite3
*db
, const char *zSql
, unsigned runFlags
){
580 const char *zEnd
= &zSql
[strlen(zSql
)];
583 while( zSql
&& zSql
[0] ){
586 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zMore
);
587 assert( zMore
<=zEnd
);
588 if( zMore
==zSql
) break;
589 if( runFlags
& SQL_TRACE
){
590 const char *z
= zSql
;
592 while( z
<zMore
&& ISSPACE(z
[0]) ) z
++;
593 n
= (int)(zMore
- z
);
594 while( n
>0 && ISSPACE(z
[n
-1]) ) n
--;
597 printf("TRACE: %.*s (error: %s)\n", n
, z
, sqlite3_errmsg(db
));
599 printf("TRACE: %.*s\n", n
, z
);
604 if( (runFlags
& SQL_OUTPUT
)==0 ){
605 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){}
609 for(nRow
=0; SQLITE_ROW
==sqlite3_step(pStmt
); nRow
++){
612 nCol
= sqlite3_column_count(pStmt
);
614 for(i
=0; i
<nCol
; i
++){
615 int eType
= sqlite3_column_type(pStmt
,i
);
616 printf("ROW[%d].%s = ", nRow
, sqlite3_column_name(pStmt
,i
));
622 case SQLITE_INTEGER
: {
623 printf("INT %s\n", sqlite3_column_text(pStmt
,i
));
627 printf("FLOAT %s\n", sqlite3_column_text(pStmt
,i
));
631 printf("TEXT [%s]\n", sqlite3_column_text(pStmt
,i
));
635 printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt
,i
));
642 sqlite3_finalize(pStmt
);
647 int main(int argc
, char **argv
){
648 int i
; /* Loop counter */
649 int nDb
= 0; /* Number of databases to fuzz */
650 char **azDb
= 0; /* Names of the databases (limit: 20) */
651 int verboseFlag
= 0; /* True for extra output */
652 int noLookaside
= 0; /* Disable lookaside if true */
653 int vdbeLimitFlag
= 0; /* Stop after 100,000 VDBE ops */
654 int nHeap
= 0; /* True for fixed heap size */
655 int iTimeout
= 0; /* Timeout delay in seconds */
656 int rc
; /* Result code from SQLite3 API calls */
657 sqlite3
*db
; /* The database connection */
658 sqlite3_stmt
*pStmt
; /* A single SQL statement */
659 Str sql
; /* SQL to run */
660 unsigned runFlags
= 0; /* Flags passed to runSql */
662 for(i
=1; i
<argc
; i
++){
665 azDb
= realloc(azDb
, sizeof(azDb
[0])*(nDb
+1));
666 if( azDb
==0 ) fatalError("out of memory");
672 if( strcmp(z
, "help")==0 ){
674 }else if( strcmp(z
, "limit-mem")==0 ){
675 if( i
==argc
-1 ) fatalError("missing argument to %s", argv
[i
]);
676 nHeap
= integerValue(argv
[++i
]);
677 }else if( strcmp(z
, "no-lookaside")==0 ){
679 }else if( strcmp(z
, "timeout")==0 ){
680 if( i
==argc
-1 ) fatalError("missing argument to %s", argv
[i
]);
681 iTimeout
= integerValue(argv
[++i
]);
682 }else if( strcmp(z
, "trace")==0 ){
683 runFlags
|= SQL_OUTPUT
|SQL_TRACE
;
684 }else if( strcmp(z
, "limit-vdbe")==0 ){
686 }else if( strcmp(z
, "v")==0 || strcmp(z
, "verbose")==0 ){
688 runFlags
|= SQL_TRACE
;
690 fatalError("unknown command-line option: \"%s\"\n", argv
[i
]);
697 sqlite3_config(SQLITE_CONFIG_LOG
, sqlLog
);
700 void *pHeap
= malloc( nHeap
);
701 if( pHeap
==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap
);
702 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, pHeap
, nHeap
, 32);
703 if( rc
) fatalError("heap configuration failed: %d\n", rc
);
706 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, 0, 0);
712 signal(SIGALRM
, timeoutHandler
);
714 for(i
=0; i
<nDb
; i
++){
715 if( verboseFlag
&& nDb
>1 ){
716 printf("DATABASE-FILE: %s\n", azDb
[i
]);
719 if( iTimeout
) setAlarm(iTimeout
);
720 createVFile("test.db", azDb
[i
]);
721 rc
= sqlite3_open_v2("test.db", &db
, SQLITE_OPEN_READWRITE
, "inmem");
723 printf("cannot open test.db for \"%s\"\n", azDb
[i
]);
727 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
729 sqlite3_progress_handler(db
, 100000, progressHandler
, &vdbeLimitFlag
);
732 rc
= sqlite3_prepare_v2(db
, "SELECT sql FROM autoexec", -1, &pStmt
, 0);
734 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
735 StrAppend(&sql
, (const char*)sqlite3_column_text(pStmt
, 0));
736 StrAppend(&sql
, "\n");
739 sqlite3_finalize(pStmt
);
740 StrAppend(&sql
, "PRAGMA integrity_check;\n");
741 runSql(db
, StrStr(&sql
), runFlags
);
745 if( sqlite3_memory_used()>0 ){
748 fatalError("memory leak of %lld bytes", sqlite3_memory_used());