1 /* 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> 46 #ifndef _WIN32 47 #include <sys/time.h> 48 #include <sys/resource.h> 49 #endif 50 #include "sqlite3.h" 51 52 /* 53 ** This is the is the SQL that is run against the database. 54 */ 55 static 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;", 60 "ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s;", 61 "INSERT INTO t3 SELECT * FROM t2;", 62 "DELETE FROM t3 WHERE x IN (SELECT x FROM t4);", 63 "REINDEX;", 64 "DROP TABLE t3;", 65 "VACUUM;", 66 }; 67 68 /* Output verbosity level. 0 means complete silence */ 69 int eVerbosity = 0; 70 71 /* True to activate PRAGMA vdbe_debug=on */ 72 static int bVdbeDebug = 0; 73 74 /* Maximum size of the in-memory database file */ 75 static sqlite3_int64 szMax = 104857600; 76 77 /***** Copy/paste from ext/misc/memtrace.c ***************************/ 78 /* The original memory allocation routines */ 79 static sqlite3_mem_methods memtraceBase; 80 static FILE *memtraceOut; 81 82 /* Methods that trace memory allocations */ 83 static void *memtraceMalloc(int n){ 84 if( memtraceOut ){ 85 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 86 memtraceBase.xRoundup(n)); 87 } 88 return memtraceBase.xMalloc(n); 89 } 90 static void memtraceFree(void *p){ 91 if( p==0 ) return; 92 if( memtraceOut ){ 93 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); 94 } 95 memtraceBase.xFree(p); 96 } 97 static void *memtraceRealloc(void *p, int n){ 98 if( p==0 ) return memtraceMalloc(n); 99 if( n==0 ){ 100 memtraceFree(p); 101 return 0; 102 } 103 if( memtraceOut ){ 104 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", 105 memtraceBase.xSize(p), memtraceBase.xRoundup(n)); 106 } 107 return memtraceBase.xRealloc(p, n); 108 } 109 static int memtraceSize(void *p){ 110 return memtraceBase.xSize(p); 111 } 112 static int memtraceRoundup(int n){ 113 return memtraceBase.xRoundup(n); 114 } 115 static int memtraceInit(void *p){ 116 return memtraceBase.xInit(p); 117 } 118 static void memtraceShutdown(void *p){ 119 memtraceBase.xShutdown(p); 120 } 121 122 /* The substitute memory allocator */ 123 static sqlite3_mem_methods ersaztMethods = { 124 memtraceMalloc, 125 memtraceFree, 126 memtraceRealloc, 127 memtraceSize, 128 memtraceRoundup, 129 memtraceInit, 130 memtraceShutdown 131 }; 132 133 /* Begin tracing memory allocations to out. */ 134 int sqlite3MemTraceActivate(FILE *out){ 135 int rc = SQLITE_OK; 136 if( memtraceBase.xMalloc==0 ){ 137 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); 138 if( rc==SQLITE_OK ){ 139 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); 140 } 141 } 142 memtraceOut = out; 143 return rc; 144 } 145 146 /* Deactivate memory tracing */ 147 int sqlite3MemTraceDeactivate(void){ 148 int rc = SQLITE_OK; 149 if( memtraceBase.xMalloc!=0 ){ 150 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); 151 if( rc==SQLITE_OK ){ 152 memset(&memtraceBase, 0, sizeof(memtraceBase)); 153 } 154 } 155 memtraceOut = 0; 156 return rc; 157 } 158 /***** End copy/paste from ext/misc/memtrace.c ***************************/ 159 160 /* libFuzzer invokes this routine with fuzzed database files (in aData). 161 ** This routine run SQLite against the malformed database to see if it 162 ** can provoke a failure or malfunction. 163 */ 164 int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){ 165 unsigned char *a; 166 sqlite3 *db; 167 int rc; 168 int i; 169 sqlite3_int64 x; 170 char *zErr = 0; 171 172 if( eVerbosity>=1 ){ 173 printf("************** nByte=%d ***************\n", (int)nByte); 174 fflush(stdout); 175 } 176 if( sqlite3_initialize() ) return 0; 177 rc = sqlite3_open(0, &db); 178 if( rc ) return 1; 179 a = sqlite3_malloc64(nByte+1); 180 if( a==0 ) return 1; 181 memcpy(a, aData, nByte); 182 sqlite3_deserialize(db, "main", a, nByte, nByte, 183 SQLITE_DESERIALIZE_RESIZEABLE | 184 SQLITE_DESERIALIZE_FREEONCLOSE); 185 x = szMax; 186 sqlite3_file_control(db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); 187 if( bVdbeDebug ){ 188 sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); 189 } 190 for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){ 191 if( eVerbosity>=1 ){ 192 printf("%s\n", azSql[i]); 193 fflush(stdout); 194 } 195 zErr = 0; 196 rc = sqlite3_exec(db, azSql[i], 0, 0, &zErr); 197 if( rc && eVerbosity>=1 ){ 198 printf("-- rc=%d zErr=%s\n", rc, zErr); 199 } 200 sqlite3_free(zErr); 201 } 202 rc = sqlite3_close(db); 203 if( rc!=SQLITE_OK ){ 204 fprintf(stdout, "sqlite3_close() returns %d\n", rc); 205 } 206 if( sqlite3_memory_used()!=0 ){ 207 int nAlloc = 0; 208 int nNotUsed = 0; 209 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); 210 fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", 211 sqlite3_memory_used(), nAlloc); 212 exit(1); 213 } 214 return 0; 215 } 216 217 /* 218 ** Return the number of "v" characters in a string. Return 0 if there 219 ** are any characters in the string other than "v". 220 */ 221 static int numberOfVChar(const char *z){ 222 int N = 0; 223 while( z[0] && z[0]=='v' ){ 224 z++; 225 N++; 226 } 227 return z[0]==0 ? N : 0; 228 } 229 230 /* libFuzzer invokes this routine once when the executable starts, to 231 ** process the command-line arguments. 232 */ 233 int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){ 234 int i, j, n; 235 int argc = *pArgc; 236 char **argv = *pArgv; 237 for(i=j=1; i<argc; i++){ 238 char *z = argv[i]; 239 if( z[0]=='-' ){ 240 z++; 241 if( z[0]=='-' ) z++; 242 if( z[0]=='v' && (n = numberOfVChar(z))>0 ){ 243 eVerbosity += n; 244 continue; 245 } 246 if( strcmp(z,"vdbe-debug")==0 ){ 247 bVdbeDebug = 1; 248 continue; 249 } 250 if( strcmp(z,"memtrace")==0 ){ 251 sqlite3MemTraceActivate(stdout); 252 continue; 253 } 254 if( strcmp(z,"mem")==0 ){ 255 bVdbeDebug = 1; 256 continue; 257 } 258 if( strcmp(z,"max-db-size")==0 ){ 259 if( i+1==argc ){ 260 fprintf(stderr, "missing argument to %s\n", argv[i]); 261 exit(1); 262 } 263 szMax = strtol(argv[++i], 0, 0); 264 continue; 265 } 266 #ifndef _WIN32 267 if( strcmp(z,"max-stack")==0 268 || strcmp(z,"max-data")==0 269 || strcmp(z,"max-as")==0 270 ){ 271 struct rlimit x,y; 272 int resource = RLIMIT_STACK; 273 char *zType = "RLIMIT_STACK"; 274 if( i+1==argc ){ 275 fprintf(stderr, "missing argument to %s\n", argv[i]); 276 exit(1); 277 } 278 if( z[4]=='d' ){ 279 resource = RLIMIT_DATA; 280 zType = "RLIMIT_DATA"; 281 } 282 if( z[4]=='a' ){ 283 resource = RLIMIT_AS; 284 zType = "RLIMIT_AS"; 285 } 286 memset(&x,0,sizeof(x)); 287 getrlimit(resource, &x); 288 y.rlim_cur = atoi(argv[++i]); 289 y.rlim_max = x.rlim_cur; 290 setrlimit(resource, &y); 291 memset(&y,0,sizeof(y)); 292 getrlimit(resource, &y); 293 printf("%s changed from %d to %d\n", 294 zType, (int)x.rlim_cur, (int)y.rlim_cur); 295 continue; 296 } 297 #endif /* _WIN32 */ 298 } 299 argv[j++] = argv[i]; 300 } 301 argv[j] = 0; 302 *pArgc = j; 303 return 0; 304 } 305 306 #ifdef STANDALONE 307 /* 308 ** Read an entire file into memory. Space to hold the file comes 309 ** from malloc(). 310 */ 311 static unsigned char *readFile(const char *zName, int *pnByte){ 312 FILE *in = fopen(zName, "rb"); 313 long nIn; 314 size_t nRead; 315 unsigned char *pBuf; 316 if( in==0 ) return 0; 317 fseek(in, 0, SEEK_END); 318 nIn = ftell(in); 319 rewind(in); 320 pBuf = malloc( nIn+1 ); 321 if( pBuf==0 ){ fclose(in); return 0; } 322 nRead = fread(pBuf, nIn, 1, in); 323 fclose(in); 324 if( nRead!=1 ){ 325 free(pBuf); 326 return 0; 327 } 328 pBuf[nIn] = 0; 329 if( pnByte ) *pnByte = nIn; 330 return pBuf; 331 } 332 #endif /* STANDALONE */ 333 334 #ifdef STANDALONE 335 int main(int argc, char **argv){ 336 int i; 337 LLVMFuzzerInitialize(&argc, &argv); 338 for(i=1; i<argc; i++){ 339 unsigned char *pIn; 340 int nIn; 341 pIn = readFile(argv[i], &nIn); 342 if( pIn ){ 343 LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn); 344 free(pIn); 345 } 346 } 347 if( eVerbosity>0 ){ 348 struct rusage x; 349 printf("SQLite %s\n", sqlite3_sourceid()); 350 memset(&x, 0, sizeof(x)); 351 if( getrusage(RUSAGE_SELF, &x)==0 ){ 352 printf("Maximum RSS = %ld KB\n", x.ru_maxrss); 353 } 354 } 355 return 0; 356 } 357 #endif /*STANDALONE*/ 358