blob: 0c2b1320e4253dae31a3bf88f91ff686680ec7df [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>
drhf53524b2017-03-17 14:59:40 +00007#include <stdio.h>
8#include <string.h>
drhea432ba2016-11-11 16:33:47 +00009#include "sqlite3.h"
10
drhf53524b2017-03-17 14:59:40 +000011/* Global debugging settings. OSS-Fuzz will have all debugging turned
12** off. But if LLVMFuzzerTestOneInput() is called interactively from
13** the ossshell utility program, then these flags might be set.
14*/
15static unsigned mDebug = 0;
16#define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */
17#define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */
18#define FUZZ_SHOW_ERRORS 0x0004 /* Print error messages from SQLite */
19
20/* The ossshell utility program invokes this interface to see the
21** debugging flags. Unused by OSS-Fuzz.
22*/
23void ossfuzz_set_debug_flags(unsigned x){
24 mDebug = x;
25}
26
drha6bf20b2017-03-10 17:03:11 +000027/* Return the current real-world time in milliseconds since the
28** Julian epoch (-4714-11-24).
29*/
30static sqlite3_int64 timeOfDay(void){
31 static sqlite3_vfs *clockVfs = 0;
32 sqlite3_int64 t;
33 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
34 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
35 clockVfs->xCurrentTimeInt64(clockVfs, &t);
36 }else{
37 double r;
38 clockVfs->xCurrentTime(clockVfs, &r);
39 t = (sqlite3_int64)(r*86400000.0);
40 }
41 return t;
42}
43
drhf53524b2017-03-17 14:59:40 +000044/* An instance of the following object is passed by pointer as the
45** client data to various callbacks.
46*/
47typedef struct FuzzCtx {
48 sqlite3 *db; /* The database connection */
49 sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
50 sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
51 sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
52 unsigned nCb; /* Number of progress callbacks */
53} FuzzCtx;
54
drhe6ce2b62016-12-26 12:14:44 +000055#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhea432ba2016-11-11 16:33:47 +000056/*
drha6bf20b2017-03-10 17:03:11 +000057** Progress handler callback.
58**
59** The argument is the cutoff-time after which all processing should
60** stop. So return non-zero if the cut-off time is exceeded.
drhea432ba2016-11-11 16:33:47 +000061*/
drhf53524b2017-03-17 14:59:40 +000062static int progress_handler(void *pClientData) {
63 FuzzCtx *p = (FuzzCtx*)pClientData;
64 sqlite3_int64 iNow = timeOfDay();
65 int rc = iNow>=p->iCutoffTime;
66 sqlite3_int64 iDiff = iNow - p->iLastCb;
67 if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
68 p->nCb++;
69 return rc;
drhea432ba2016-11-11 16:33:47 +000070}
drhe6ce2b62016-12-26 12:14:44 +000071#endif
drhea432ba2016-11-11 16:33:47 +000072
73/*
74** Callback for sqlite3_exec().
75*/
76static int exec_handler(void *pCnt, int argc, char **argv, char **namev){
77 int i;
drh55377b42016-11-14 17:25:57 +000078 if( argv ){
79 for(i=0; i<argc; i++) sqlite3_free(sqlite3_mprintf("%s", argv[i]));
80 }
drhea432ba2016-11-11 16:33:47 +000081 return ((*(int*)pCnt)--)<=0;
82}
83
84/*
85** Main entry point. The fuzzer invokes this function with each
86** fuzzed input.
87*/
88int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
drhea432ba2016-11-11 16:33:47 +000089 int execCnt = 0; /* Abort row callback when count reaches zero */
90 char *zErrMsg = 0; /* Error message returned by sqlite_exec() */
drhea432ba2016-11-11 16:33:47 +000091 uint8_t uSelector; /* First byte of input data[] */
92 int rc; /* Return code from various interfaces */
93 char *zSql; /* Zero-terminated copy of data[] */
drhf53524b2017-03-17 14:59:40 +000094 FuzzCtx cx; /* Fuzzing context */
drhea432ba2016-11-11 16:33:47 +000095
drhf53524b2017-03-17 14:59:40 +000096 memset(&cx, 0, sizeof(cx));
drhea432ba2016-11-11 16:33:47 +000097 if( size<3 ) return 0; /* Early out if unsufficient data */
98
99 /* Extract the selector byte from the beginning of the input. But only
100 ** do this if the second byte is a \n. If the second byte is not \n,
101 ** then use a default selector */
102 if( data[1]=='\n' ){
103 uSelector = data[0]; data += 2; size -= 2;
104 }else{
105 uSelector = 0xfd;
106 }
107
108 /* Open the database connection. Only use an in-memory database. */
drhf53524b2017-03-17 14:59:40 +0000109 rc = sqlite3_open_v2("fuzz.db", &cx.db,
drhea432ba2016-11-11 16:33:47 +0000110 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0);
111 if( rc ) return 0;
112
drhe6ce2b62016-12-26 12:14:44 +0000113#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhbbc01772017-03-13 13:45:29 +0000114 /* Invoke the progress handler frequently to check to see if we
115 ** are taking too long. The progress handler will return true
116 ** (which will block further processing) if more than 10 seconds have
117 ** elapsed since the start of the test.
drha6bf20b2017-03-10 17:03:11 +0000118 */
drhf53524b2017-03-17 14:59:40 +0000119 cx.iLastCb = timeOfDay();
120 cx.iCutoffTime = cx.iLastCb + 10000; /* Now + 10 seconds */
121 sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
drhe6ce2b62016-12-26 12:14:44 +0000122#endif
drhea432ba2016-11-11 16:33:47 +0000123
drh544cab72017-03-17 22:51:28 +0000124 /* Set a limit on the maximum size of a prepared statement */
125 sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, 25000);
126
drha6bf20b2017-03-10 17:03:11 +0000127 /* Bit 1 of the selector enables foreign key constraints */
drhf53524b2017-03-17 14:59:40 +0000128 sqlite3_db_config(cx.db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc);
drhea432ba2016-11-11 16:33:47 +0000129 uSelector >>= 1;
130
131 /* Remaining bits of the selector determine a limit on the number of
132 ** output rows */
133 execCnt = uSelector + 1;
134
135 /* Run the SQL. The sqlite_exec() interface expects a zero-terminated
136 ** string, so make a copy. */
137 zSql = sqlite3_mprintf("%.*s", (int)size, data);
drhf53524b2017-03-17 14:59:40 +0000138 sqlite3_exec(cx.db, zSql, exec_handler, (void*)&execCnt, &zErrMsg);
139
140 /* Show any errors */
141 if( (mDebug & FUZZ_SHOW_ERRORS)!=0 && zErrMsg ){
142 printf("Error: %s\n", zErrMsg);
143 }
drhea432ba2016-11-11 16:33:47 +0000144
145 /* Cleanup and return */
146 sqlite3_free(zErrMsg);
147 sqlite3_free(zSql);
drhf53524b2017-03-17 14:59:40 +0000148 sqlite3_close(cx.db);
149
150 if( mDebug & FUZZ_SHOW_MAX_DELAY ){
151 printf("Progress callback count....... %d\n", cx.nCb);
152 printf("Max time between callbacks.... %d ms\n", (int)cx.mxInterval);
153 }
drhea432ba2016-11-11 16:33:47 +0000154 return 0;
155}