xref: /sqlite-3.40.0/test/ossshell.c (revision fb32c44e)
1 /*
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>
9 #if !defined(_MSC_VER)
10 # include <stdint.h>
11 #endif
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "sqlite3.h"
16 
17 #if defined(_MSC_VER)
18 typedef unsigned char uint8_t;
19 #endif
20 
21 /*
22 ** The entry point in ossfuzz.c that this routine will be calling
23 */
24 int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
25 
26 /* 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 */
30 extern void ossfuzz_set_debug_flags(unsigned);
31 
32 
33 
34 /*
35 ** Read files named on the command-line and invoke the fuzzer for
36 ** each one.
37 */
38 int main(int argc, char **argv){
39   FILE *in;
40   int i;
41   int nErr = 0;
42   uint8_t *zBuf = 0;
43   size_t sz;
44   unsigned mDebug = 0;
45 
46   for(i=1; i<argc; i++){
47     const char *zFilename = argv[i];
48     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     }
70     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);
90       if( mDebug ) printf("\n");
91       fflush(stdout);
92       (void)LLVMFuzzerTestOneInput(zBuf, sz);
93       if( mDebug ) printf("%s: ", zFilename);
94       printf("ok\n");
95     }
96     fclose(in);
97   }
98   free(zBuf);
99   return nErr;
100 }
101