blob: 97d101e17a0cad11ef1612dee61023c7b80211dc [file] [log] [blame]
drhea432ba2016-11-11 16:33:47 +00001/*
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
drha6bf20b2017-03-10 17:03:11 +00009/* Return the current real-world time in milliseconds since the
10** Julian epoch (-4714-11-24).
11*/
12static 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
drhe6ce2b62016-12-26 12:14:44 +000026#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhea432ba2016-11-11 16:33:47 +000027/*
drha6bf20b2017-03-10 17:03:11 +000028** 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.
drhea432ba2016-11-11 16:33:47 +000032*/
33static int progress_handler(void *pReturn) {
drha6bf20b2017-03-10 17:03:11 +000034 sqlite3_int64 iCutoffTime = *(sqlite3_int64*)pReturn;
35 return timeOfDay()>=iCutoffTime;
drhea432ba2016-11-11 16:33:47 +000036}
drhe6ce2b62016-12-26 12:14:44 +000037#endif
drhea432ba2016-11-11 16:33:47 +000038
39/*
40** Callback for sqlite3_exec().
41*/
42static int exec_handler(void *pCnt, int argc, char **argv, char **namev){
43 int i;
drh55377b42016-11-14 17:25:57 +000044 if( argv ){
45 for(i=0; i<argc; i++) sqlite3_free(sqlite3_mprintf("%s", argv[i]));
46 }
drhea432ba2016-11-11 16:33:47 +000047 return ((*(int*)pCnt)--)<=0;
48}
49
50/*
51** Main entry point. The fuzzer invokes this function with each
52** fuzzed input.
53*/
54int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
drhea432ba2016-11-11 16:33:47 +000055 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[] */
drha6bf20b2017-03-10 17:03:11 +000061 sqlite3_int64 iCutoff; /* Cutoff timer */
drhea432ba2016-11-11 16:33:47 +000062
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
drhe6ce2b62016-12-26 12:14:44 +000079#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhbbc01772017-03-13 13:45:29 +000080 /* 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.
drha6bf20b2017-03-10 17:03:11 +000084 */
85 iCutoff = timeOfDay() + 10000; /* Now + 10 seconds */
drhbbc01772017-03-13 13:45:29 +000086 sqlite3_progress_handler(db, 10, progress_handler, (void*)&iCutoff);
drhe6ce2b62016-12-26 12:14:44 +000087#endif
drhea432ba2016-11-11 16:33:47 +000088
drha6bf20b2017-03-10 17:03:11 +000089 /* Bit 1 of the selector enables foreign key constraints */
drhea432ba2016-11-11 16:33:47 +000090 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}