drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** This module interfaces SQLite to the Google OSS-Fuzz, fuzzer as a service. |
| 3 | ** (https://github.com/google/oss-fuzz) |
| 4 | */ |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | #include "sqlite3.h" |
| 8 | |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 9 | /* Return the current real-world time in milliseconds since the |
| 10 | ** Julian epoch (-4714-11-24). |
| 11 | */ |
| 12 | static sqlite3_int64 timeOfDay(void){ |
| 13 | static sqlite3_vfs *clockVfs = 0; |
| 14 | sqlite3_int64 t; |
| 15 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| 16 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 17 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 18 | }else{ |
| 19 | double r; |
| 20 | clockVfs->xCurrentTime(clockVfs, &r); |
| 21 | t = (sqlite3_int64)(r*86400000.0); |
| 22 | } |
| 23 | return t; |
| 24 | } |
| 25 | |
drh | e6ce2b6 | 2016-12-26 12:14:44 +0000 | [diff] [blame] | 26 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 27 | /* |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 28 | ** Progress handler callback. |
| 29 | ** |
| 30 | ** The argument is the cutoff-time after which all processing should |
| 31 | ** stop. So return non-zero if the cut-off time is exceeded. |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 32 | */ |
| 33 | static int progress_handler(void *pReturn) { |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 34 | sqlite3_int64 iCutoffTime = *(sqlite3_int64*)pReturn; |
| 35 | return timeOfDay()>=iCutoffTime; |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 36 | } |
drh | e6ce2b6 | 2016-12-26 12:14:44 +0000 | [diff] [blame] | 37 | #endif |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | ** Callback for sqlite3_exec(). |
| 41 | */ |
| 42 | static int exec_handler(void *pCnt, int argc, char **argv, char **namev){ |
| 43 | int i; |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 44 | if( argv ){ |
| 45 | for(i=0; i<argc; i++) sqlite3_free(sqlite3_mprintf("%s", argv[i])); |
| 46 | } |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 47 | return ((*(int*)pCnt)--)<=0; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | ** Main entry point. The fuzzer invokes this function with each |
| 52 | ** fuzzed input. |
| 53 | */ |
| 54 | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 55 | int execCnt = 0; /* Abort row callback when count reaches zero */ |
| 56 | char *zErrMsg = 0; /* Error message returned by sqlite_exec() */ |
| 57 | sqlite3 *db; /* The database connection */ |
| 58 | uint8_t uSelector; /* First byte of input data[] */ |
| 59 | int rc; /* Return code from various interfaces */ |
| 60 | char *zSql; /* Zero-terminated copy of data[] */ |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 61 | sqlite3_int64 iCutoff; /* Cutoff timer */ |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 62 | |
| 63 | if( size<3 ) return 0; /* Early out if unsufficient data */ |
| 64 | |
| 65 | /* Extract the selector byte from the beginning of the input. But only |
| 66 | ** do this if the second byte is a \n. If the second byte is not \n, |
| 67 | ** then use a default selector */ |
| 68 | if( data[1]=='\n' ){ |
| 69 | uSelector = data[0]; data += 2; size -= 2; |
| 70 | }else{ |
| 71 | uSelector = 0xfd; |
| 72 | } |
| 73 | |
| 74 | /* Open the database connection. Only use an in-memory database. */ |
| 75 | rc = sqlite3_open_v2("fuzz.db", &db, |
| 76 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0); |
| 77 | if( rc ) return 0; |
| 78 | |
drh | e6ce2b6 | 2016-12-26 12:14:44 +0000 | [diff] [blame] | 79 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | bbc0177 | 2017-03-13 13:45:29 +0000 | [diff] [blame^] | 80 | /* Invoke the progress handler frequently to check to see if we |
| 81 | ** are taking too long. The progress handler will return true |
| 82 | ** (which will block further processing) if more than 10 seconds have |
| 83 | ** elapsed since the start of the test. |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 84 | */ |
| 85 | iCutoff = timeOfDay() + 10000; /* Now + 10 seconds */ |
drh | bbc0177 | 2017-03-13 13:45:29 +0000 | [diff] [blame^] | 86 | sqlite3_progress_handler(db, 10, progress_handler, (void*)&iCutoff); |
drh | e6ce2b6 | 2016-12-26 12:14:44 +0000 | [diff] [blame] | 87 | #endif |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 88 | |
drh | a6bf20b | 2017-03-10 17:03:11 +0000 | [diff] [blame] | 89 | /* Bit 1 of the selector enables foreign key constraints */ |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 90 | sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc); |
| 91 | uSelector >>= 1; |
| 92 | |
| 93 | /* Remaining bits of the selector determine a limit on the number of |
| 94 | ** output rows */ |
| 95 | execCnt = uSelector + 1; |
| 96 | |
| 97 | /* Run the SQL. The sqlite_exec() interface expects a zero-terminated |
| 98 | ** string, so make a copy. */ |
| 99 | zSql = sqlite3_mprintf("%.*s", (int)size, data); |
| 100 | sqlite3_exec(db, zSql, exec_handler, (void*)&execCnt, &zErrMsg); |
| 101 | |
| 102 | /* Cleanup and return */ |
| 103 | sqlite3_free(zErrMsg); |
| 104 | sqlite3_free(zSql); |
| 105 | sqlite3_close(db); |
| 106 | return 0; |
| 107 | } |