blob: 3e277c9d346bd6065293e399b25dc572fc64d3c1 [file] [log] [blame]
drh65da2852018-10-27 00:47:33 +00001/*
2** 2018-10-26
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 program is designed for fuzz-testing SQLite database files using
14** the -fsanitize=fuzzer option of clang.
15**
16** The -fsanitize=fuzzer option causes a main() to be inserted automatically.
17** That main() invokes LLVMFuzzerTestOneInput(D,S) to be invoked repeatedly.
18** Each D is a fuzzed database file. The code in this file runs various
19** SQL statements against that database, trying to provoke a failure.
20**
21** For best results the seed database files should have these tables:
22**
23** Table "t1" with columns "a" and "b"
24** Tables "t2" and "t3 with the same number of compatible columns
25** "t3" should have a column names "x"
26** Table "t4" with a column "x" that is compatible with t3.x.
27**
28** Any of these tables can be virtual tables, for example FTS or RTree tables.
29**
30** To run this test:
31**
32** mkdir dir
33** cp dbfuzz2-seed*.db dir
34** clang-6.0 -I. -g -O1 -fsanitize=fuzzer \
35** -DTHREADSAFE=0 -DSQLITE_ENABLE_DESERIALIZE \
36** -DSQLITE_ENABLE_DBSTAT_VTAB dbfuzz2.c sqlite3.c -ldl
37** ./a.out dir
38*/
39#include <assert.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <stdarg.h>
44#include <ctype.h>
45#include <stdint.h>
drh7e85e902019-02-20 19:06:16 +000046#ifndef _WIN32
drh8ed07d12019-01-20 00:03:59 +000047#include <sys/time.h>
48#include <sys/resource.h>
drh7e85e902019-02-20 19:06:16 +000049#endif
drh65da2852018-10-27 00:47:33 +000050#include "sqlite3.h"
51
52/*
53** This is the is the SQL that is run against the database.
54*/
55static const char *azSql[] = {
56 "PRAGMA integrity_check;",
57 "SELECT * FROM sqlite_master;",
58 "SELECT sum(length(name)) FROM dbstat;",
59 "UPDATE t1 SET b=a, a=b WHERE a<b;",
drhd811d842018-10-27 21:06:44 +000060 "ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s;",
drh65da2852018-10-27 00:47:33 +000061 "INSERT INTO t3 SELECT * FROM t2;",
62 "DELETE FROM t3 WHERE x IN (SELECT x FROM t4);",
drhd811d842018-10-27 21:06:44 +000063 "REINDEX;",
drh65da2852018-10-27 00:47:33 +000064 "DROP TABLE t3;",
65 "VACUUM;",
66};
67
drhd811d842018-10-27 21:06:44 +000068/* Output verbosity level. 0 means complete silence */
69int eVerbosity = 0;
70
drh1972c8c2019-01-11 14:38:47 +000071/* True to activate PRAGMA vdbe_debug=on */
72static int bVdbeDebug = 0;
73
drh178edcd2019-01-22 16:11:31 +000074/* Maximum size of the in-memory database file */
75static sqlite3_int64 szMax = 104857600;
76
drhfb556712019-03-22 11:38:14 +000077/* Progress handler callback data */
78static int nCb = 0; /* Number of callbacks seen so far */
79static int mxCb = 250000; /* Maximum allowed callbacks */
80
drha7908822019-02-04 19:50:44 +000081/***** Copy/paste from ext/misc/memtrace.c ***************************/
82/* The original memory allocation routines */
83static sqlite3_mem_methods memtraceBase;
84static FILE *memtraceOut;
85
86/* Methods that trace memory allocations */
87static void *memtraceMalloc(int n){
88 if( memtraceOut ){
89 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
90 memtraceBase.xRoundup(n));
91 }
92 return memtraceBase.xMalloc(n);
93}
94static void memtraceFree(void *p){
95 if( p==0 ) return;
96 if( memtraceOut ){
97 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
98 }
99 memtraceBase.xFree(p);
100}
101static void *memtraceRealloc(void *p, int n){
102 if( p==0 ) return memtraceMalloc(n);
103 if( n==0 ){
104 memtraceFree(p);
105 return 0;
106 }
107 if( memtraceOut ){
108 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
109 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
110 }
111 return memtraceBase.xRealloc(p, n);
112}
113static int memtraceSize(void *p){
114 return memtraceBase.xSize(p);
115}
116static int memtraceRoundup(int n){
117 return memtraceBase.xRoundup(n);
118}
119static int memtraceInit(void *p){
120 return memtraceBase.xInit(p);
121}
122static void memtraceShutdown(void *p){
123 memtraceBase.xShutdown(p);
124}
125
126/* The substitute memory allocator */
127static sqlite3_mem_methods ersaztMethods = {
128 memtraceMalloc,
129 memtraceFree,
130 memtraceRealloc,
131 memtraceSize,
132 memtraceRoundup,
133 memtraceInit,
134 memtraceShutdown
135};
136
137/* Begin tracing memory allocations to out. */
138int sqlite3MemTraceActivate(FILE *out){
139 int rc = SQLITE_OK;
140 if( memtraceBase.xMalloc==0 ){
141 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
142 if( rc==SQLITE_OK ){
143 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
144 }
145 }
146 memtraceOut = out;
147 return rc;
148}
149
150/* Deactivate memory tracing */
151int sqlite3MemTraceDeactivate(void){
152 int rc = SQLITE_OK;
153 if( memtraceBase.xMalloc!=0 ){
154 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
155 if( rc==SQLITE_OK ){
156 memset(&memtraceBase, 0, sizeof(memtraceBase));
157 }
158 }
159 memtraceOut = 0;
160 return rc;
161}
162/***** End copy/paste from ext/misc/memtrace.c ***************************/
163
drhfb556712019-03-22 11:38:14 +0000164/*
165** Progress handler callback
166**
167** Count the number of callbacks and cause an abort once the limit is
168** reached.
169*/
170static int progress_handler(void *pNotUsed){
171 nCb++;
172 if( nCb<mxCb ) return 0;
173 if( eVerbosity>=1 ){
174 printf("-- Progress limit of %d reached\n", mxCb);
175 }
176 return 1;
177}
178
drhd811d842018-10-27 21:06:44 +0000179/* libFuzzer invokes this routine with fuzzed database files (in aData).
180** This routine run SQLite against the malformed database to see if it
181** can provoke a failure or malfunction.
182*/
drh65da2852018-10-27 00:47:33 +0000183int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){
184 unsigned char *a;
185 sqlite3 *db;
186 int rc;
187 int i;
drh178edcd2019-01-22 16:11:31 +0000188 sqlite3_int64 x;
drh88862d42019-02-04 19:45:26 +0000189 char *zErr = 0;
drh65da2852018-10-27 00:47:33 +0000190
drhd811d842018-10-27 21:06:44 +0000191 if( eVerbosity>=1 ){
192 printf("************** nByte=%d ***************\n", (int)nByte);
193 fflush(stdout);
194 }
drh62a88292018-12-07 03:01:07 +0000195 if( sqlite3_initialize() ) return 0;
drhad9bfa52018-10-30 15:20:35 +0000196 rc = sqlite3_open(0, &db);
drh65da2852018-10-27 00:47:33 +0000197 if( rc ) return 1;
drhad9bfa52018-10-30 15:20:35 +0000198 a = sqlite3_malloc64(nByte+1);
drh65da2852018-10-27 00:47:33 +0000199 if( a==0 ) return 1;
200 memcpy(a, aData, nByte);
201 sqlite3_deserialize(db, "main", a, nByte, nByte,
202 SQLITE_DESERIALIZE_RESIZEABLE |
203 SQLITE_DESERIALIZE_FREEONCLOSE);
drh178edcd2019-01-22 16:11:31 +0000204 x = szMax;
drhddc28c22019-02-26 18:21:08 +0000205#ifdef SQLITE_FCNTL_SIZE_LIMIT
drh178edcd2019-01-22 16:11:31 +0000206 sqlite3_file_control(db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x);
drhddc28c22019-02-26 18:21:08 +0000207#endif
drh1972c8c2019-01-11 14:38:47 +0000208 if( bVdbeDebug ){
209 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
210 }
drhfb556712019-03-22 11:38:14 +0000211 if( mxCb>0 ){
212 sqlite3_progress_handler(db, 10, progress_handler, 0);
213 }
drh65da2852018-10-27 00:47:33 +0000214 for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){
drhd811d842018-10-27 21:06:44 +0000215 if( eVerbosity>=1 ){
216 printf("%s\n", azSql[i]);
217 fflush(stdout);
218 }
drh88862d42019-02-04 19:45:26 +0000219 zErr = 0;
drhfb556712019-03-22 11:38:14 +0000220 nCb = 0;
drh88862d42019-02-04 19:45:26 +0000221 rc = sqlite3_exec(db, azSql[i], 0, 0, &zErr);
222 if( rc && eVerbosity>=1 ){
223 printf("-- rc=%d zErr=%s\n", rc, zErr);
224 }
225 sqlite3_free(zErr);
drh65da2852018-10-27 00:47:33 +0000226 }
drhad9bfa52018-10-30 15:20:35 +0000227 rc = sqlite3_close(db);
228 if( rc!=SQLITE_OK ){
229 fprintf(stdout, "sqlite3_close() returns %d\n", rc);
230 }
drhd811d842018-10-27 21:06:44 +0000231 if( sqlite3_memory_used()!=0 ){
drhad9bfa52018-10-30 15:20:35 +0000232 int nAlloc = 0;
233 int nNotUsed = 0;
234 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
235 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n",
236 sqlite3_memory_used(), nAlloc);
drhd811d842018-10-27 21:06:44 +0000237 exit(1);
238 }
239 return 0;
240}
241
drh1972c8c2019-01-11 14:38:47 +0000242/*
243** Return the number of "v" characters in a string. Return 0 if there
244** are any characters in the string other than "v".
245*/
246static int numberOfVChar(const char *z){
247 int N = 0;
248 while( z[0] && z[0]=='v' ){
249 z++;
250 N++;
251 }
252 return z[0]==0 ? N : 0;
253}
254
drhd811d842018-10-27 21:06:44 +0000255/* libFuzzer invokes this routine once when the executable starts, to
256** process the command-line arguments.
257*/
258int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){
drh1972c8c2019-01-11 14:38:47 +0000259 int i, j, n;
drhd811d842018-10-27 21:06:44 +0000260 int argc = *pArgc;
drhd811d842018-10-27 21:06:44 +0000261 char **argv = *pArgv;
drhd811d842018-10-27 21:06:44 +0000262 for(i=j=1; i<argc; i++){
263 char *z = argv[i];
264 if( z[0]=='-' ){
265 z++;
266 if( z[0]=='-' ) z++;
drh1972c8c2019-01-11 14:38:47 +0000267 if( z[0]=='v' && (n = numberOfVChar(z))>0 ){
268 eVerbosity += n;
269 continue;
270 }
271 if( strcmp(z,"vdbe-debug")==0 ){
272 bVdbeDebug = 1;
drhd811d842018-10-27 21:06:44 +0000273 continue;
274 }
drhfb556712019-03-22 11:38:14 +0000275 if( strcmp(z,"limit")==0 ){
276 if( i+1==argc ){
277 fprintf(stderr, "missing argument to %s\n", argv[i]);
278 exit(1);
279 }
280 mxCb = strtol(argv[++i], 0, 0);
281 continue;
282 }
drha7908822019-02-04 19:50:44 +0000283 if( strcmp(z,"memtrace")==0 ){
284 sqlite3MemTraceActivate(stdout);
285 continue;
286 }
287 if( strcmp(z,"mem")==0 ){
288 bVdbeDebug = 1;
289 continue;
290 }
drh178edcd2019-01-22 16:11:31 +0000291 if( strcmp(z,"max-db-size")==0 ){
292 if( i+1==argc ){
293 fprintf(stderr, "missing argument to %s\n", argv[i]);
294 exit(1);
295 }
296 szMax = strtol(argv[++i], 0, 0);
297 continue;
298 }
drh7e85e902019-02-20 19:06:16 +0000299#ifndef _WIN32
drh59765522019-01-21 13:47:55 +0000300 if( strcmp(z,"max-stack")==0
301 || strcmp(z,"max-data")==0
302 || strcmp(z,"max-as")==0
303 ){
drh8ed07d12019-01-20 00:03:59 +0000304 struct rlimit x,y;
drh59765522019-01-21 13:47:55 +0000305 int resource = RLIMIT_STACK;
306 char *zType = "RLIMIT_STACK";
drh8ed07d12019-01-20 00:03:59 +0000307 if( i+1==argc ){
308 fprintf(stderr, "missing argument to %s\n", argv[i]);
309 exit(1);
310 }
drh59765522019-01-21 13:47:55 +0000311 if( z[4]=='d' ){
312 resource = RLIMIT_DATA;
313 zType = "RLIMIT_DATA";
314 }
315 if( z[4]=='a' ){
316 resource = RLIMIT_AS;
317 zType = "RLIMIT_AS";
318 }
drh8ed07d12019-01-20 00:03:59 +0000319 memset(&x,0,sizeof(x));
drh59765522019-01-21 13:47:55 +0000320 getrlimit(resource, &x);
drh8ed07d12019-01-20 00:03:59 +0000321 y.rlim_cur = atoi(argv[++i]);
322 y.rlim_max = x.rlim_cur;
drh59765522019-01-21 13:47:55 +0000323 setrlimit(resource, &y);
drh8ed07d12019-01-20 00:03:59 +0000324 memset(&y,0,sizeof(y));
drh59765522019-01-21 13:47:55 +0000325 getrlimit(resource, &y);
326 printf("%s changed from %d to %d\n",
327 zType, (int)x.rlim_cur, (int)y.rlim_cur);
drh8ed07d12019-01-20 00:03:59 +0000328 continue;
329 }
drh7e85e902019-02-20 19:06:16 +0000330#endif /* _WIN32 */
drhd811d842018-10-27 21:06:44 +0000331 }
drhb10a50e2019-01-13 20:23:34 +0000332 argv[j++] = argv[i];
drhd811d842018-10-27 21:06:44 +0000333 }
drhb10a50e2019-01-13 20:23:34 +0000334 argv[j] = 0;
drhd811d842018-10-27 21:06:44 +0000335 *pArgc = j;
drh65da2852018-10-27 00:47:33 +0000336 return 0;
337}
drhe65b9c62019-01-11 13:03:06 +0000338
339#ifdef STANDALONE
340/*
341** Read an entire file into memory. Space to hold the file comes
342** from malloc().
343*/
344static unsigned char *readFile(const char *zName, int *pnByte){
345 FILE *in = fopen(zName, "rb");
346 long nIn;
347 size_t nRead;
348 unsigned char *pBuf;
349 if( in==0 ) return 0;
350 fseek(in, 0, SEEK_END);
351 nIn = ftell(in);
352 rewind(in);
353 pBuf = malloc( nIn+1 );
354 if( pBuf==0 ){ fclose(in); return 0; }
355 nRead = fread(pBuf, nIn, 1, in);
356 fclose(in);
357 if( nRead!=1 ){
358 free(pBuf);
359 return 0;
360 }
361 pBuf[nIn] = 0;
362 if( pnByte ) *pnByte = nIn;
363 return pBuf;
364}
365#endif /* STANDALONE */
366
367#ifdef STANDALONE
368int main(int argc, char **argv){
369 int i;
drhe65b9c62019-01-11 13:03:06 +0000370 LLVMFuzzerInitialize(&argc, &argv);
371 for(i=1; i<argc; i++){
372 unsigned char *pIn;
373 int nIn;
374 pIn = readFile(argv[i], &nIn);
375 if( pIn ){
376 LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn);
377 free(pIn);
378 }
379 }
drhb10a50e2019-01-13 20:23:34 +0000380 if( eVerbosity>0 ){
drh59765522019-01-21 13:47:55 +0000381 struct rusage x;
drhb10a50e2019-01-13 20:23:34 +0000382 printf("SQLite %s\n", sqlite3_sourceid());
drh59765522019-01-21 13:47:55 +0000383 memset(&x, 0, sizeof(x));
384 if( getrusage(RUSAGE_SELF, &x)==0 ){
385 printf("Maximum RSS = %ld KB\n", x.ru_maxrss);
386 }
drhb10a50e2019-01-13 20:23:34 +0000387 }
drhe65b9c62019-01-11 13:03:06 +0000388 return 0;
389}
390#endif /*STANDALONE*/