xref: /sqlite-3.40.0/test/ossshell.c (revision 87f500ce)
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 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "sqlite3.h"
13 
14 /*
15 ** The entry point in ossfuzz.c that this routine will be calling
16 */
17 int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
18 
19 
20 /*
21 ** Read files named on the command-line and invoke the fuzzer for
22 ** each one.
23 */
24 int main(int argc, char **argv){
25   FILE *in;
26   int i;
27   int nErr = 0;
28   uint8_t *zBuf = 0;
29   size_t sz;
30 
31   for(i=1; i<argc; i++){
32     const char *zFilename = argv[i];
33     in = fopen(zFilename, "rb");
34     if( in==0 ){
35       fprintf(stderr, "cannot open \"%s\"\n", zFilename);
36       nErr++;
37       continue;
38     }
39     fseek(in, 0, SEEK_END);
40     sz = ftell(in);
41     rewind(in);
42     zBuf = realloc(zBuf, sz);
43     if( zBuf==0 ){
44       fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz);
45       exit(1);
46     }
47     if( fread(zBuf, sz, 1, in)!=1 ){
48       fprintf(stderr, "cannot read %d bytes from \"%s\"\n",
49                        (int)sz, zFilename);
50       nErr++;
51     }else{
52       printf("%s... ", zFilename);
53       fflush(stdout);
54       (void)LLVMFuzzerTestOneInput(zBuf, sz);
55       printf("ok\n");
56     }
57     fclose(in);
58   }
59   free(zBuf);
60   return nErr;
61 }
62