drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 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 |
drh | 8d889af | 2021-05-08 17:18:23 +0000 | [diff] [blame] | 34 | ** clang-6.0 -I. -g -O1 -fsanitize=fuzzer -DTHREADSAFE=0 \ |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 35 | ** -DSQLITE_ENABLE_DBSTAT_VTAB dbfuzz2.c sqlite3.c -ldl |
| 36 | ** ./a.out dir |
| 37 | */ |
| 38 | #include <assert.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <stdarg.h> |
| 43 | #include <ctype.h> |
| 44 | #include <stdint.h> |
drh | 7e85e90 | 2019-02-20 19:06:16 +0000 | [diff] [blame] | 45 | #ifndef _WIN32 |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 46 | #include <sys/time.h> |
| 47 | #include <sys/resource.h> |
drh | 7e85e90 | 2019-02-20 19:06:16 +0000 | [diff] [blame] | 48 | #endif |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 49 | #include "sqlite3.h" |
| 50 | |
| 51 | /* |
| 52 | ** This is the is the SQL that is run against the database. |
| 53 | */ |
| 54 | static const char *azSql[] = { |
| 55 | "PRAGMA integrity_check;", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 56 | "SELECT * FROM sqlite_schema;", |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 57 | "SELECT sum(length(name)) FROM dbstat;", |
| 58 | "UPDATE t1 SET b=a, a=b WHERE a<b;", |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 59 | "ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s;", |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 60 | "INSERT INTO t3 SELECT * FROM t2;", |
| 61 | "DELETE FROM t3 WHERE x IN (SELECT x FROM t4);", |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 62 | "REINDEX;", |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 63 | "DROP TABLE t3;", |
| 64 | "VACUUM;", |
| 65 | }; |
| 66 | |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 67 | /* Output verbosity level. 0 means complete silence */ |
| 68 | int eVerbosity = 0; |
| 69 | |
drh | 1972c8c | 2019-01-11 14:38:47 +0000 | [diff] [blame] | 70 | /* True to activate PRAGMA vdbe_debug=on */ |
| 71 | static int bVdbeDebug = 0; |
| 72 | |
drh | 178edcd | 2019-01-22 16:11:31 +0000 | [diff] [blame] | 73 | /* Maximum size of the in-memory database file */ |
| 74 | static sqlite3_int64 szMax = 104857600; |
| 75 | |
drh | fb55671 | 2019-03-22 11:38:14 +0000 | [diff] [blame] | 76 | /* Progress handler callback data */ |
| 77 | static int nCb = 0; /* Number of callbacks seen so far */ |
| 78 | static int mxCb = 250000; /* Maximum allowed callbacks */ |
| 79 | |
drh | a790882 | 2019-02-04 19:50:44 +0000 | [diff] [blame] | 80 | /***** Copy/paste from ext/misc/memtrace.c ***************************/ |
| 81 | /* The original memory allocation routines */ |
| 82 | static sqlite3_mem_methods memtraceBase; |
| 83 | static FILE *memtraceOut; |
| 84 | |
| 85 | /* Methods that trace memory allocations */ |
| 86 | static void *memtraceMalloc(int n){ |
| 87 | if( memtraceOut ){ |
| 88 | fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", |
| 89 | memtraceBase.xRoundup(n)); |
| 90 | } |
| 91 | return memtraceBase.xMalloc(n); |
| 92 | } |
| 93 | static void memtraceFree(void *p){ |
| 94 | if( p==0 ) return; |
| 95 | if( memtraceOut ){ |
| 96 | fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); |
| 97 | } |
| 98 | memtraceBase.xFree(p); |
| 99 | } |
| 100 | static void *memtraceRealloc(void *p, int n){ |
| 101 | if( p==0 ) return memtraceMalloc(n); |
| 102 | if( n==0 ){ |
| 103 | memtraceFree(p); |
| 104 | return 0; |
| 105 | } |
| 106 | if( memtraceOut ){ |
| 107 | fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", |
| 108 | memtraceBase.xSize(p), memtraceBase.xRoundup(n)); |
| 109 | } |
| 110 | return memtraceBase.xRealloc(p, n); |
| 111 | } |
| 112 | static int memtraceSize(void *p){ |
| 113 | return memtraceBase.xSize(p); |
| 114 | } |
| 115 | static int memtraceRoundup(int n){ |
| 116 | return memtraceBase.xRoundup(n); |
| 117 | } |
| 118 | static int memtraceInit(void *p){ |
| 119 | return memtraceBase.xInit(p); |
| 120 | } |
| 121 | static void memtraceShutdown(void *p){ |
| 122 | memtraceBase.xShutdown(p); |
| 123 | } |
| 124 | |
| 125 | /* The substitute memory allocator */ |
| 126 | static sqlite3_mem_methods ersaztMethods = { |
| 127 | memtraceMalloc, |
| 128 | memtraceFree, |
| 129 | memtraceRealloc, |
| 130 | memtraceSize, |
| 131 | memtraceRoundup, |
| 132 | memtraceInit, |
| 133 | memtraceShutdown |
| 134 | }; |
| 135 | |
| 136 | /* Begin tracing memory allocations to out. */ |
| 137 | int sqlite3MemTraceActivate(FILE *out){ |
| 138 | int rc = SQLITE_OK; |
| 139 | if( memtraceBase.xMalloc==0 ){ |
| 140 | rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); |
| 141 | if( rc==SQLITE_OK ){ |
| 142 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); |
| 143 | } |
| 144 | } |
| 145 | memtraceOut = out; |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | /* Deactivate memory tracing */ |
| 150 | int sqlite3MemTraceDeactivate(void){ |
| 151 | int rc = SQLITE_OK; |
| 152 | if( memtraceBase.xMalloc!=0 ){ |
| 153 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); |
| 154 | if( rc==SQLITE_OK ){ |
| 155 | memset(&memtraceBase, 0, sizeof(memtraceBase)); |
| 156 | } |
| 157 | } |
| 158 | memtraceOut = 0; |
| 159 | return rc; |
| 160 | } |
| 161 | /***** End copy/paste from ext/misc/memtrace.c ***************************/ |
| 162 | |
drh | fb55671 | 2019-03-22 11:38:14 +0000 | [diff] [blame] | 163 | /* |
| 164 | ** Progress handler callback |
| 165 | ** |
| 166 | ** Count the number of callbacks and cause an abort once the limit is |
| 167 | ** reached. |
| 168 | */ |
| 169 | static int progress_handler(void *pNotUsed){ |
| 170 | nCb++; |
| 171 | if( nCb<mxCb ) return 0; |
| 172 | if( eVerbosity>=1 ){ |
| 173 | printf("-- Progress limit of %d reached\n", mxCb); |
| 174 | } |
| 175 | return 1; |
| 176 | } |
| 177 | |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 178 | /* libFuzzer invokes this routine with fuzzed database files (in aData). |
| 179 | ** This routine run SQLite against the malformed database to see if it |
| 180 | ** can provoke a failure or malfunction. |
| 181 | */ |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 182 | int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){ |
| 183 | unsigned char *a; |
| 184 | sqlite3 *db; |
| 185 | int rc; |
| 186 | int i; |
drh | 178edcd | 2019-01-22 16:11:31 +0000 | [diff] [blame] | 187 | sqlite3_int64 x; |
drh | 88862d4 | 2019-02-04 19:45:26 +0000 | [diff] [blame] | 188 | char *zErr = 0; |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 189 | |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 190 | if( eVerbosity>=1 ){ |
| 191 | printf("************** nByte=%d ***************\n", (int)nByte); |
| 192 | fflush(stdout); |
| 193 | } |
drh | 62a8829 | 2018-12-07 03:01:07 +0000 | [diff] [blame] | 194 | if( sqlite3_initialize() ) return 0; |
drh | ad9bfa5 | 2018-10-30 15:20:35 +0000 | [diff] [blame] | 195 | rc = sqlite3_open(0, &db); |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 196 | if( rc ) return 1; |
drh | ad9bfa5 | 2018-10-30 15:20:35 +0000 | [diff] [blame] | 197 | a = sqlite3_malloc64(nByte+1); |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 198 | if( a==0 ) return 1; |
| 199 | memcpy(a, aData, nByte); |
| 200 | sqlite3_deserialize(db, "main", a, nByte, nByte, |
| 201 | SQLITE_DESERIALIZE_RESIZEABLE | |
| 202 | SQLITE_DESERIALIZE_FREEONCLOSE); |
drh | 178edcd | 2019-01-22 16:11:31 +0000 | [diff] [blame] | 203 | x = szMax; |
drh | ddc28c2 | 2019-02-26 18:21:08 +0000 | [diff] [blame] | 204 | #ifdef SQLITE_FCNTL_SIZE_LIMIT |
drh | 178edcd | 2019-01-22 16:11:31 +0000 | [diff] [blame] | 205 | sqlite3_file_control(db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); |
drh | ddc28c2 | 2019-02-26 18:21:08 +0000 | [diff] [blame] | 206 | #endif |
drh | 1972c8c | 2019-01-11 14:38:47 +0000 | [diff] [blame] | 207 | if( bVdbeDebug ){ |
| 208 | sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); |
| 209 | } |
drh | fb55671 | 2019-03-22 11:38:14 +0000 | [diff] [blame] | 210 | if( mxCb>0 ){ |
| 211 | sqlite3_progress_handler(db, 10, progress_handler, 0); |
| 212 | } |
drh | e6e96b1 | 2019-08-02 21:03:24 +0000 | [diff] [blame] | 213 | #ifdef SQLITE_TESTCTRL_PRNG_SEED |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 214 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db); |
drh | e6e96b1 | 2019-08-02 21:03:24 +0000 | [diff] [blame] | 215 | #endif |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 216 | for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){ |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 217 | if( eVerbosity>=1 ){ |
| 218 | printf("%s\n", azSql[i]); |
| 219 | fflush(stdout); |
| 220 | } |
drh | 88862d4 | 2019-02-04 19:45:26 +0000 | [diff] [blame] | 221 | zErr = 0; |
drh | fb55671 | 2019-03-22 11:38:14 +0000 | [diff] [blame] | 222 | nCb = 0; |
drh | 88862d4 | 2019-02-04 19:45:26 +0000 | [diff] [blame] | 223 | rc = sqlite3_exec(db, azSql[i], 0, 0, &zErr); |
| 224 | if( rc && eVerbosity>=1 ){ |
| 225 | printf("-- rc=%d zErr=%s\n", rc, zErr); |
| 226 | } |
| 227 | sqlite3_free(zErr); |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 228 | } |
drh | ad9bfa5 | 2018-10-30 15:20:35 +0000 | [diff] [blame] | 229 | rc = sqlite3_close(db); |
| 230 | if( rc!=SQLITE_OK ){ |
| 231 | fprintf(stdout, "sqlite3_close() returns %d\n", rc); |
| 232 | } |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 233 | if( sqlite3_memory_used()!=0 ){ |
drh | ad9bfa5 | 2018-10-30 15:20:35 +0000 | [diff] [blame] | 234 | int nAlloc = 0; |
| 235 | int nNotUsed = 0; |
| 236 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); |
| 237 | fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", |
| 238 | sqlite3_memory_used(), nAlloc); |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 239 | exit(1); |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
drh | 1972c8c | 2019-01-11 14:38:47 +0000 | [diff] [blame] | 244 | /* |
| 245 | ** Return the number of "v" characters in a string. Return 0 if there |
| 246 | ** are any characters in the string other than "v". |
| 247 | */ |
| 248 | static int numberOfVChar(const char *z){ |
| 249 | int N = 0; |
| 250 | while( z[0] && z[0]=='v' ){ |
| 251 | z++; |
| 252 | N++; |
| 253 | } |
| 254 | return z[0]==0 ? N : 0; |
| 255 | } |
| 256 | |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 257 | /* libFuzzer invokes this routine once when the executable starts, to |
| 258 | ** process the command-line arguments. |
| 259 | */ |
| 260 | int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){ |
drh | 1972c8c | 2019-01-11 14:38:47 +0000 | [diff] [blame] | 261 | int i, j, n; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 262 | int argc = *pArgc; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 263 | char **argv = *pArgv; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 264 | for(i=j=1; i<argc; i++){ |
| 265 | char *z = argv[i]; |
| 266 | if( z[0]=='-' ){ |
| 267 | z++; |
| 268 | if( z[0]=='-' ) z++; |
drh | 1972c8c | 2019-01-11 14:38:47 +0000 | [diff] [blame] | 269 | if( z[0]=='v' && (n = numberOfVChar(z))>0 ){ |
| 270 | eVerbosity += n; |
| 271 | continue; |
| 272 | } |
| 273 | if( strcmp(z,"vdbe-debug")==0 ){ |
| 274 | bVdbeDebug = 1; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 275 | continue; |
| 276 | } |
drh | fb55671 | 2019-03-22 11:38:14 +0000 | [diff] [blame] | 277 | if( strcmp(z,"limit")==0 ){ |
| 278 | if( i+1==argc ){ |
| 279 | fprintf(stderr, "missing argument to %s\n", argv[i]); |
| 280 | exit(1); |
| 281 | } |
| 282 | mxCb = strtol(argv[++i], 0, 0); |
| 283 | continue; |
| 284 | } |
drh | a790882 | 2019-02-04 19:50:44 +0000 | [diff] [blame] | 285 | if( strcmp(z,"memtrace")==0 ){ |
| 286 | sqlite3MemTraceActivate(stdout); |
| 287 | continue; |
| 288 | } |
drh | 178edcd | 2019-01-22 16:11:31 +0000 | [diff] [blame] | 289 | if( strcmp(z,"max-db-size")==0 ){ |
| 290 | if( i+1==argc ){ |
| 291 | fprintf(stderr, "missing argument to %s\n", argv[i]); |
| 292 | exit(1); |
| 293 | } |
| 294 | szMax = strtol(argv[++i], 0, 0); |
| 295 | continue; |
| 296 | } |
drh | f461bab | 2020-12-01 23:18:13 +0000 | [diff] [blame] | 297 | if( strcmp(z, "lookaside")==0 ){ |
| 298 | int sz, nSlot; |
| 299 | if( i+2>=argc ){ |
| 300 | fprintf(stderr, |
| 301 | "--lookaside requires two arguments: slot-size num-slots\n"); |
| 302 | exit(1); |
| 303 | } |
| 304 | sz = atoi(argv[++i]); |
| 305 | nSlot = atoi(argv[++i]); |
| 306 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, nSlot); |
| 307 | continue; |
| 308 | } |
drh | 7e85e90 | 2019-02-20 19:06:16 +0000 | [diff] [blame] | 309 | #ifndef _WIN32 |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 310 | if( strcmp(z,"max-stack")==0 |
| 311 | || strcmp(z,"max-data")==0 |
| 312 | || strcmp(z,"max-as")==0 |
| 313 | ){ |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 314 | struct rlimit x,y; |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 315 | int resource = RLIMIT_STACK; |
| 316 | char *zType = "RLIMIT_STACK"; |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 317 | if( i+1==argc ){ |
| 318 | fprintf(stderr, "missing argument to %s\n", argv[i]); |
| 319 | exit(1); |
| 320 | } |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 321 | if( z[4]=='d' ){ |
| 322 | resource = RLIMIT_DATA; |
| 323 | zType = "RLIMIT_DATA"; |
| 324 | } |
| 325 | if( z[4]=='a' ){ |
| 326 | resource = RLIMIT_AS; |
| 327 | zType = "RLIMIT_AS"; |
| 328 | } |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 329 | memset(&x,0,sizeof(x)); |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 330 | getrlimit(resource, &x); |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 331 | y.rlim_cur = atoi(argv[++i]); |
| 332 | y.rlim_max = x.rlim_cur; |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 333 | setrlimit(resource, &y); |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 334 | memset(&y,0,sizeof(y)); |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 335 | getrlimit(resource, &y); |
| 336 | printf("%s changed from %d to %d\n", |
| 337 | zType, (int)x.rlim_cur, (int)y.rlim_cur); |
drh | 8ed07d1 | 2019-01-20 00:03:59 +0000 | [diff] [blame] | 338 | continue; |
| 339 | } |
drh | 7e85e90 | 2019-02-20 19:06:16 +0000 | [diff] [blame] | 340 | #endif /* _WIN32 */ |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 341 | } |
drh | b10a50e | 2019-01-13 20:23:34 +0000 | [diff] [blame] | 342 | argv[j++] = argv[i]; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 343 | } |
drh | b10a50e | 2019-01-13 20:23:34 +0000 | [diff] [blame] | 344 | argv[j] = 0; |
drh | d811d84 | 2018-10-27 21:06:44 +0000 | [diff] [blame] | 345 | *pArgc = j; |
drh | 65da285 | 2018-10-27 00:47:33 +0000 | [diff] [blame] | 346 | return 0; |
| 347 | } |
drh | e65b9c6 | 2019-01-11 13:03:06 +0000 | [diff] [blame] | 348 | |
| 349 | #ifdef STANDALONE |
| 350 | /* |
| 351 | ** Read an entire file into memory. Space to hold the file comes |
| 352 | ** from malloc(). |
| 353 | */ |
| 354 | static unsigned char *readFile(const char *zName, int *pnByte){ |
| 355 | FILE *in = fopen(zName, "rb"); |
| 356 | long nIn; |
| 357 | size_t nRead; |
| 358 | unsigned char *pBuf; |
| 359 | if( in==0 ) return 0; |
| 360 | fseek(in, 0, SEEK_END); |
| 361 | nIn = ftell(in); |
| 362 | rewind(in); |
| 363 | pBuf = malloc( nIn+1 ); |
| 364 | if( pBuf==0 ){ fclose(in); return 0; } |
| 365 | nRead = fread(pBuf, nIn, 1, in); |
| 366 | fclose(in); |
| 367 | if( nRead!=1 ){ |
| 368 | free(pBuf); |
| 369 | return 0; |
| 370 | } |
| 371 | pBuf[nIn] = 0; |
| 372 | if( pnByte ) *pnByte = nIn; |
| 373 | return pBuf; |
| 374 | } |
| 375 | #endif /* STANDALONE */ |
| 376 | |
| 377 | #ifdef STANDALONE |
| 378 | int main(int argc, char **argv){ |
| 379 | int i; |
drh | e65b9c6 | 2019-01-11 13:03:06 +0000 | [diff] [blame] | 380 | LLVMFuzzerInitialize(&argc, &argv); |
| 381 | for(i=1; i<argc; i++){ |
| 382 | unsigned char *pIn; |
| 383 | int nIn; |
| 384 | pIn = readFile(argv[i], &nIn); |
| 385 | if( pIn ){ |
| 386 | LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn); |
| 387 | free(pIn); |
| 388 | } |
| 389 | } |
drh | 0ba3234 | 2019-07-08 18:35:51 +0000 | [diff] [blame] | 390 | #ifdef RUSAGE_SELF |
drh | b10a50e | 2019-01-13 20:23:34 +0000 | [diff] [blame] | 391 | if( eVerbosity>0 ){ |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 392 | struct rusage x; |
drh | b10a50e | 2019-01-13 20:23:34 +0000 | [diff] [blame] | 393 | printf("SQLite %s\n", sqlite3_sourceid()); |
drh | 5976552 | 2019-01-21 13:47:55 +0000 | [diff] [blame] | 394 | memset(&x, 0, sizeof(x)); |
| 395 | if( getrusage(RUSAGE_SELF, &x)==0 ){ |
| 396 | printf("Maximum RSS = %ld KB\n", x.ru_maxrss); |
| 397 | } |
drh | b10a50e | 2019-01-13 20:23:34 +0000 | [diff] [blame] | 398 | } |
drh | 0ba3234 | 2019-07-08 18:35:51 +0000 | [diff] [blame] | 399 | #endif |
drh | e65b9c6 | 2019-01-11 13:03:06 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | #endif /*STANDALONE*/ |