1ea432ba2Sdrh /*
2ea432ba2Sdrh ** This module interfaces SQLite to the Google OSS-Fuzz, fuzzer as a service.
3ea432ba2Sdrh ** (https://github.com/google/oss-fuzz)
4ea432ba2Sdrh */
5ea432ba2Sdrh #include <stddef.h>
6ac8ba26eSmistachkin #if !defined(_MSC_VER)
7ea432ba2Sdrh # include <stdint.h>
8ac8ba26eSmistachkin #endif
9f53524b4Sdrh #include <stdio.h>
10f53524b4Sdrh #include <string.h>
11ea432ba2Sdrh #include "sqlite3.h"
12ea432ba2Sdrh
13ac8ba26eSmistachkin #if defined(_MSC_VER)
14ac8ba26eSmistachkin typedef unsigned char uint8_t;
15ac8ba26eSmistachkin #endif
16ac8ba26eSmistachkin
17f53524b4Sdrh /* Global debugging settings. OSS-Fuzz will have all debugging turned
18f53524b4Sdrh ** off. But if LLVMFuzzerTestOneInput() is called interactively from
19f53524b4Sdrh ** the ossshell utility program, then these flags might be set.
20f53524b4Sdrh */
21f53524b4Sdrh static unsigned mDebug = 0;
22f53524b4Sdrh #define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */
23f53524b4Sdrh #define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */
24f53524b4Sdrh #define FUZZ_SHOW_ERRORS 0x0004 /* Print error messages from SQLite */
25f53524b4Sdrh
26f53524b4Sdrh /* The ossshell utility program invokes this interface to see the
27f53524b4Sdrh ** debugging flags. Unused by OSS-Fuzz.
28f53524b4Sdrh */
ossfuzz_set_debug_flags(unsigned x)29f53524b4Sdrh void ossfuzz_set_debug_flags(unsigned x){
30f53524b4Sdrh mDebug = x;
31f53524b4Sdrh }
32f53524b4Sdrh
33a6bf20b5Sdrh /* Return the current real-world time in milliseconds since the
34a6bf20b5Sdrh ** Julian epoch (-4714-11-24).
35a6bf20b5Sdrh */
timeOfDay(void)36a6bf20b5Sdrh static sqlite3_int64 timeOfDay(void){
37a6bf20b5Sdrh static sqlite3_vfs *clockVfs = 0;
38a6bf20b5Sdrh sqlite3_int64 t;
398055a3eaSdrh if( clockVfs==0 ){
408055a3eaSdrh clockVfs = sqlite3_vfs_find(0);
418055a3eaSdrh if( clockVfs==0 ) return 0;
428055a3eaSdrh }
43a6bf20b5Sdrh if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
44a6bf20b5Sdrh clockVfs->xCurrentTimeInt64(clockVfs, &t);
45a6bf20b5Sdrh }else{
46a6bf20b5Sdrh double r;
47a6bf20b5Sdrh clockVfs->xCurrentTime(clockVfs, &r);
48a6bf20b5Sdrh t = (sqlite3_int64)(r*86400000.0);
49a6bf20b5Sdrh }
50a6bf20b5Sdrh return t;
51a6bf20b5Sdrh }
52a6bf20b5Sdrh
53f53524b4Sdrh /* An instance of the following object is passed by pointer as the
54f53524b4Sdrh ** client data to various callbacks.
55f53524b4Sdrh */
56f53524b4Sdrh typedef struct FuzzCtx {
57f53524b4Sdrh sqlite3 *db; /* The database connection */
58f53524b4Sdrh sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
59f53524b4Sdrh sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
60f53524b4Sdrh sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
61f53524b4Sdrh unsigned nCb; /* Number of progress callbacks */
628055a3eaSdrh unsigned execCnt; /* Number of calls to the sqlite3_exec callback */
63f53524b4Sdrh } FuzzCtx;
64f53524b4Sdrh
65ea432ba2Sdrh /*
66a6bf20b5Sdrh ** Progress handler callback.
67a6bf20b5Sdrh **
68a6bf20b5Sdrh ** The argument is the cutoff-time after which all processing should
69a6bf20b5Sdrh ** stop. So return non-zero if the cut-off time is exceeded.
70ea432ba2Sdrh */
progress_handler(void * pClientData)71f53524b4Sdrh static int progress_handler(void *pClientData) {
72f53524b4Sdrh FuzzCtx *p = (FuzzCtx*)pClientData;
73f53524b4Sdrh sqlite3_int64 iNow = timeOfDay();
74f53524b4Sdrh int rc = iNow>=p->iCutoffTime;
75f53524b4Sdrh sqlite3_int64 iDiff = iNow - p->iLastCb;
76f53524b4Sdrh if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
77f53524b4Sdrh p->nCb++;
78f53524b4Sdrh return rc;
79ea432ba2Sdrh }
80ea432ba2Sdrh
81ea432ba2Sdrh /*
8293bbfbe5Sdrh ** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
8393bbfbe5Sdrh ** "PRAGMA parser_trace" since they can dramatically increase the
8493bbfbe5Sdrh ** amount of output without actually testing anything useful.
8593bbfbe5Sdrh */
block_debug_pragmas(void * Notused,int eCode,const char * zArg1,const char * zArg2,const char * zArg3,const char * zArg4)8693bbfbe5Sdrh static int block_debug_pragmas(
8793bbfbe5Sdrh void *Notused,
8893bbfbe5Sdrh int eCode,
8993bbfbe5Sdrh const char *zArg1,
9093bbfbe5Sdrh const char *zArg2,
9193bbfbe5Sdrh const char *zArg3,
9293bbfbe5Sdrh const char *zArg4
9393bbfbe5Sdrh ){
9493bbfbe5Sdrh if( eCode==SQLITE_PRAGMA
9593bbfbe5Sdrh && (sqlite3_strnicmp("vdbe_", zArg1, 5)==0
9693bbfbe5Sdrh || sqlite3_stricmp("parser_trace", zArg1)==0)
9793bbfbe5Sdrh ){
9893bbfbe5Sdrh return SQLITE_DENY;
9993bbfbe5Sdrh }
10093bbfbe5Sdrh return SQLITE_OK;
10193bbfbe5Sdrh }
10293bbfbe5Sdrh
10393bbfbe5Sdrh /*
104ea432ba2Sdrh ** Callback for sqlite3_exec().
105ea432ba2Sdrh */
exec_handler(void * pClientData,int argc,char ** argv,char ** namev)1068055a3eaSdrh static int exec_handler(void *pClientData, int argc, char **argv, char **namev){
1078055a3eaSdrh FuzzCtx *p = (FuzzCtx*)pClientData;
108ea432ba2Sdrh int i;
10955377b47Sdrh if( argv ){
110ea432ba2Sdrh for(i=0; i<argc; i++) sqlite3_free(sqlite3_mprintf("%s", argv[i]));
11155377b47Sdrh }
1128055a3eaSdrh return (p->execCnt--)<=0 || progress_handler(pClientData);
113ea432ba2Sdrh }
114ea432ba2Sdrh
115ea432ba2Sdrh /*
116ea432ba2Sdrh ** Main entry point. The fuzzer invokes this function with each
117ea432ba2Sdrh ** fuzzed input.
118ea432ba2Sdrh */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)119ea432ba2Sdrh int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
120ea432ba2Sdrh char *zErrMsg = 0; /* Error message returned by sqlite_exec() */
121ea432ba2Sdrh uint8_t uSelector; /* First byte of input data[] */
122ea432ba2Sdrh int rc; /* Return code from various interfaces */
123ea432ba2Sdrh char *zSql; /* Zero-terminated copy of data[] */
124f53524b4Sdrh FuzzCtx cx; /* Fuzzing context */
125ea432ba2Sdrh
126f53524b4Sdrh memset(&cx, 0, sizeof(cx));
127ea432ba2Sdrh if( size<3 ) return 0; /* Early out if unsufficient data */
128ea432ba2Sdrh
129ea432ba2Sdrh /* Extract the selector byte from the beginning of the input. But only
130ea432ba2Sdrh ** do this if the second byte is a \n. If the second byte is not \n,
131ea432ba2Sdrh ** then use a default selector */
132ea432ba2Sdrh if( data[1]=='\n' ){
133ea432ba2Sdrh uSelector = data[0]; data += 2; size -= 2;
134ea432ba2Sdrh }else{
135ea432ba2Sdrh uSelector = 0xfd;
136ea432ba2Sdrh }
137ea432ba2Sdrh
138ea432ba2Sdrh /* Open the database connection. Only use an in-memory database. */
1398055a3eaSdrh if( sqlite3_initialize() ) return 0;
140f53524b4Sdrh rc = sqlite3_open_v2("fuzz.db", &cx.db,
141ea432ba2Sdrh SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0);
142ea432ba2Sdrh if( rc ) return 0;
143ea432ba2Sdrh
144bbc01774Sdrh /* Invoke the progress handler frequently to check to see if we
145bbc01774Sdrh ** are taking too long. The progress handler will return true
146bbc01774Sdrh ** (which will block further processing) if more than 10 seconds have
147bbc01774Sdrh ** elapsed since the start of the test.
148a6bf20b5Sdrh */
149f53524b4Sdrh cx.iLastCb = timeOfDay();
150f53524b4Sdrh cx.iCutoffTime = cx.iLastCb + 10000; /* Now + 10 seconds */
1518055a3eaSdrh #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
152f53524b4Sdrh sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
153e6ce2b67Sdrh #endif
154ea432ba2Sdrh
155544cab76Sdrh /* Set a limit on the maximum size of a prepared statement */
156544cab76Sdrh sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, 25000);
157544cab76Sdrh
158*31999c5cSdrh /* Limit total memory available to SQLite to 20MB */
159*31999c5cSdrh sqlite3_hard_heap_limit64(20000000);
160*31999c5cSdrh
1614a7e9a25Sdrh /* Set a limit on the maximum length of a string or BLOB. Without this
1624a7e9a25Sdrh ** limit, fuzzers will invoke randomblob(N) for a large N, and the process
1634a7e9a25Sdrh ** will timeout trying to generate the huge blob */
1644a7e9a25Sdrh sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, 50000);
1654a7e9a25Sdrh
166a6bf20b5Sdrh /* Bit 1 of the selector enables foreign key constraints */
167f53524b4Sdrh sqlite3_db_config(cx.db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc);
168ea432ba2Sdrh uSelector >>= 1;
169ea432ba2Sdrh
17093bbfbe5Sdrh /* Do not allow debugging pragma statements that might cause excess output */
17193bbfbe5Sdrh sqlite3_set_authorizer(cx.db, block_debug_pragmas, 0);
17293bbfbe5Sdrh
173ea432ba2Sdrh /* Remaining bits of the selector determine a limit on the number of
174ea432ba2Sdrh ** output rows */
1758055a3eaSdrh cx.execCnt = uSelector + 1;
176ea432ba2Sdrh
177ea432ba2Sdrh /* Run the SQL. The sqlite_exec() interface expects a zero-terminated
178ea432ba2Sdrh ** string, so make a copy. */
179ea432ba2Sdrh zSql = sqlite3_mprintf("%.*s", (int)size, data);
18056f17746Sdrh #ifndef SQLITE_OMIT_COMPLETE
1815347f3c1Sdrh sqlite3_complete(zSql);
18256f17746Sdrh #endif
1838055a3eaSdrh sqlite3_exec(cx.db, zSql, exec_handler, (void*)&cx, &zErrMsg);
184f53524b4Sdrh
185f53524b4Sdrh /* Show any errors */
186f53524b4Sdrh if( (mDebug & FUZZ_SHOW_ERRORS)!=0 && zErrMsg ){
187f53524b4Sdrh printf("Error: %s\n", zErrMsg);
188f53524b4Sdrh }
189ea432ba2Sdrh
190ea432ba2Sdrh /* Cleanup and return */
191ea432ba2Sdrh sqlite3_free(zErrMsg);
192ea432ba2Sdrh sqlite3_free(zSql);
193174f8553Sdrh sqlite3_exec(cx.db, "PRAGMA temp_store_directory=''", 0, 0, 0);
194f53524b4Sdrh sqlite3_close(cx.db);
195f53524b4Sdrh
196f53524b4Sdrh if( mDebug & FUZZ_SHOW_MAX_DELAY ){
197f53524b4Sdrh printf("Progress callback count....... %d\n", cx.nCb);
198f53524b4Sdrh printf("Max time between callbacks.... %d ms\n", (int)cx.mxInterval);
199f53524b4Sdrh }
200ea432ba2Sdrh return 0;
201ea432ba2Sdrh }
202