blob: 54849f97f6d8fdff438be2e1250bef38fed3138d [file] [log] [blame]
drh55377b42016-11-14 17:25:57 +00001/*
2** This is a test interface for the ossfuzz.c module. The ossfuzz.c module
3** is an adaptor for OSS-FUZZ. (https://github.com/google/oss-fuzz)
4**
5** This program links against ossfuzz.c. It reads files named on the
6** command line and passes them one by one into ossfuzz.c.
7*/
8#include <stddef.h>
mistachkinac8ba262018-03-07 14:42:17 +00009#if !defined(_MSC_VER)
10# include <stdint.h>
11#endif
drh55377b42016-11-14 17:25:57 +000012#include <stdio.h>
13#include <stdlib.h>
drhf53524b2017-03-17 14:59:40 +000014#include <string.h>
drh55377b42016-11-14 17:25:57 +000015#include "sqlite3.h"
16
mistachkinac8ba262018-03-07 14:42:17 +000017#if defined(_MSC_VER)
18typedef unsigned char uint8_t;
19#endif
20
drh55377b42016-11-14 17:25:57 +000021/*
22** The entry point in ossfuzz.c that this routine will be calling
23*/
24int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
25
drhf53524b2017-03-17 14:59:40 +000026/* Must match equivalent #defines in ossfuzz.c */
27#define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */
28#define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */
29#define FUZZ_SHOW_ERRORS 0x0004 /* Show SQL errors */
30extern void ossfuzz_set_debug_flags(unsigned);
31
32
drh55377b42016-11-14 17:25:57 +000033
34/*
35** Read files named on the command-line and invoke the fuzzer for
36** each one.
37*/
38int main(int argc, char **argv){
39 FILE *in;
40 int i;
41 int nErr = 0;
42 uint8_t *zBuf = 0;
43 size_t sz;
drhf53524b2017-03-17 14:59:40 +000044 unsigned mDebug = 0;
drh55377b42016-11-14 17:25:57 +000045
46 for(i=1; i<argc; i++){
47 const char *zFilename = argv[i];
drhf53524b2017-03-17 14:59:40 +000048 if( zFilename[0]=='-' ){
49 if( zFilename[1]=='-' ) zFilename++;
50 if( strcmp(zFilename, "-show-errors")==0 ){
51 mDebug |= FUZZ_SHOW_ERRORS;
52 ossfuzz_set_debug_flags(mDebug);
53 }else
54 if( strcmp(zFilename, "-show-max-delay")==0 ){
55 mDebug |= FUZZ_SHOW_MAX_DELAY;
56 ossfuzz_set_debug_flags(mDebug);
57 }else
58 if( strcmp(zFilename, "-sql-trace")==0 ){
59 mDebug |= FUZZ_SQL_TRACE;
60 ossfuzz_set_debug_flags(mDebug);
61 }else
62 {
63 printf("unknown option \"%s\"\n", argv[i]);
64 printf("should be one of: --show-errors --show-max-delay"
65 " --sql-trace\n");
66 exit(1);
67 }
68 continue;
69 }
drh55377b42016-11-14 17:25:57 +000070 in = fopen(zFilename, "rb");
71 if( in==0 ){
72 fprintf(stderr, "cannot open \"%s\"\n", zFilename);
73 nErr++;
74 continue;
75 }
76 fseek(in, 0, SEEK_END);
77 sz = ftell(in);
78 rewind(in);
79 zBuf = realloc(zBuf, sz);
80 if( zBuf==0 ){
81 fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz);
82 exit(1);
83 }
84 if( fread(zBuf, sz, 1, in)!=1 ){
85 fprintf(stderr, "cannot read %d bytes from \"%s\"\n",
86 (int)sz, zFilename);
87 nErr++;
88 }else{
89 printf("%s... ", zFilename);
drhf53524b2017-03-17 14:59:40 +000090 if( mDebug ) printf("\n");
drh55377b42016-11-14 17:25:57 +000091 fflush(stdout);
92 (void)LLVMFuzzerTestOneInput(zBuf, sz);
drhf53524b2017-03-17 14:59:40 +000093 if( mDebug ) printf("%s: ", zFilename);
drh55377b42016-11-14 17:25:57 +000094 printf("ok\n");
95 }
96 fclose(in);
97 }
98 free(zBuf);
99 return nErr;
100}