drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 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> |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 12 | #include <string.h> |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 13 | #include "sqlite3.h" |
| 14 | |
| 15 | /* |
| 16 | ** The entry point in ossfuzz.c that this routine will be calling |
| 17 | */ |
| 18 | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
| 19 | |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 20 | /* Must match equivalent #defines in ossfuzz.c */ |
| 21 | #define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */ |
| 22 | #define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */ |
| 23 | #define FUZZ_SHOW_ERRORS 0x0004 /* Show SQL errors */ |
| 24 | extern void ossfuzz_set_debug_flags(unsigned); |
| 25 | |
| 26 | |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | ** Read files named on the command-line and invoke the fuzzer for |
| 30 | ** each one. |
| 31 | */ |
| 32 | int main(int argc, char **argv){ |
| 33 | FILE *in; |
| 34 | int i; |
| 35 | int nErr = 0; |
| 36 | uint8_t *zBuf = 0; |
| 37 | size_t sz; |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 38 | unsigned mDebug = 0; |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 39 | |
| 40 | for(i=1; i<argc; i++){ |
| 41 | const char *zFilename = argv[i]; |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 42 | if( zFilename[0]=='-' ){ |
| 43 | if( zFilename[1]=='-' ) zFilename++; |
| 44 | if( strcmp(zFilename, "-show-errors")==0 ){ |
| 45 | mDebug |= FUZZ_SHOW_ERRORS; |
| 46 | ossfuzz_set_debug_flags(mDebug); |
| 47 | }else |
| 48 | if( strcmp(zFilename, "-show-max-delay")==0 ){ |
| 49 | mDebug |= FUZZ_SHOW_MAX_DELAY; |
| 50 | ossfuzz_set_debug_flags(mDebug); |
| 51 | }else |
| 52 | if( strcmp(zFilename, "-sql-trace")==0 ){ |
| 53 | mDebug |= FUZZ_SQL_TRACE; |
| 54 | ossfuzz_set_debug_flags(mDebug); |
| 55 | }else |
| 56 | { |
| 57 | printf("unknown option \"%s\"\n", argv[i]); |
| 58 | printf("should be one of: --show-errors --show-max-delay" |
| 59 | " --sql-trace\n"); |
| 60 | exit(1); |
| 61 | } |
| 62 | continue; |
| 63 | } |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 64 | in = fopen(zFilename, "rb"); |
| 65 | if( in==0 ){ |
| 66 | fprintf(stderr, "cannot open \"%s\"\n", zFilename); |
| 67 | nErr++; |
| 68 | continue; |
| 69 | } |
| 70 | fseek(in, 0, SEEK_END); |
| 71 | sz = ftell(in); |
| 72 | rewind(in); |
| 73 | zBuf = realloc(zBuf, sz); |
| 74 | if( zBuf==0 ){ |
| 75 | fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz); |
| 76 | exit(1); |
| 77 | } |
| 78 | if( fread(zBuf, sz, 1, in)!=1 ){ |
| 79 | fprintf(stderr, "cannot read %d bytes from \"%s\"\n", |
| 80 | (int)sz, zFilename); |
| 81 | nErr++; |
| 82 | }else{ |
| 83 | printf("%s... ", zFilename); |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 84 | if( mDebug ) printf("\n"); |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 85 | fflush(stdout); |
| 86 | (void)LLVMFuzzerTestOneInput(zBuf, sz); |
drh | f53524b | 2017-03-17 14:59:40 +0000 | [diff] [blame] | 87 | if( mDebug ) printf("%s: ", zFilename); |
drh | 55377b4 | 2016-11-14 17:25:57 +0000 | [diff] [blame] | 88 | printf("ok\n"); |
| 89 | } |
| 90 | fclose(in); |
| 91 | } |
| 92 | free(zBuf); |
| 93 | return nErr; |
| 94 | } |