2016-11-14 20:25:57 +03:00
|
|
|
/*
|
|
|
|
** This is a test interface for the ossfuzz.c module. The ossfuzz.c module
|
|
|
|
** is an adaptor for OSS-FUZZ. (https://github.com/google/oss-fuzz)
|
|
|
|
**
|
|
|
|
** This program links against ossfuzz.c. It reads files named on the
|
|
|
|
** command line and passes them one by one into ossfuzz.c.
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-03-17 17:59:40 +03:00
|
|
|
#include <string.h>
|
2016-11-14 20:25:57 +03:00
|
|
|
#include "sqlite3.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The entry point in ossfuzz.c that this routine will be calling
|
|
|
|
*/
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
|
|
|
|
|
2017-03-17 17:59:40 +03:00
|
|
|
/* Must match equivalent #defines in ossfuzz.c */
|
|
|
|
#define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */
|
|
|
|
#define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */
|
|
|
|
#define FUZZ_SHOW_ERRORS 0x0004 /* Show SQL errors */
|
|
|
|
extern void ossfuzz_set_debug_flags(unsigned);
|
|
|
|
|
|
|
|
|
2016-11-14 20:25:57 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Read files named on the command-line and invoke the fuzzer for
|
|
|
|
** each one.
|
|
|
|
*/
|
|
|
|
int main(int argc, char **argv){
|
|
|
|
FILE *in;
|
|
|
|
int i;
|
|
|
|
int nErr = 0;
|
|
|
|
uint8_t *zBuf = 0;
|
|
|
|
size_t sz;
|
2017-03-17 17:59:40 +03:00
|
|
|
unsigned mDebug = 0;
|
2016-11-14 20:25:57 +03:00
|
|
|
|
|
|
|
for(i=1; i<argc; i++){
|
|
|
|
const char *zFilename = argv[i];
|
2017-03-17 17:59:40 +03:00
|
|
|
if( zFilename[0]=='-' ){
|
|
|
|
if( zFilename[1]=='-' ) zFilename++;
|
|
|
|
if( strcmp(zFilename, "-show-errors")==0 ){
|
|
|
|
mDebug |= FUZZ_SHOW_ERRORS;
|
|
|
|
ossfuzz_set_debug_flags(mDebug);
|
|
|
|
}else
|
|
|
|
if( strcmp(zFilename, "-show-max-delay")==0 ){
|
|
|
|
mDebug |= FUZZ_SHOW_MAX_DELAY;
|
|
|
|
ossfuzz_set_debug_flags(mDebug);
|
|
|
|
}else
|
|
|
|
if( strcmp(zFilename, "-sql-trace")==0 ){
|
|
|
|
mDebug |= FUZZ_SQL_TRACE;
|
|
|
|
ossfuzz_set_debug_flags(mDebug);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
printf("unknown option \"%s\"\n", argv[i]);
|
|
|
|
printf("should be one of: --show-errors --show-max-delay"
|
|
|
|
" --sql-trace\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-14 20:25:57 +03:00
|
|
|
in = fopen(zFilename, "rb");
|
|
|
|
if( in==0 ){
|
|
|
|
fprintf(stderr, "cannot open \"%s\"\n", zFilename);
|
|
|
|
nErr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fseek(in, 0, SEEK_END);
|
|
|
|
sz = ftell(in);
|
|
|
|
rewind(in);
|
|
|
|
zBuf = realloc(zBuf, sz);
|
|
|
|
if( zBuf==0 ){
|
|
|
|
fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if( fread(zBuf, sz, 1, in)!=1 ){
|
|
|
|
fprintf(stderr, "cannot read %d bytes from \"%s\"\n",
|
|
|
|
(int)sz, zFilename);
|
|
|
|
nErr++;
|
|
|
|
}else{
|
|
|
|
printf("%s... ", zFilename);
|
2017-03-17 17:59:40 +03:00
|
|
|
if( mDebug ) printf("\n");
|
2016-11-14 20:25:57 +03:00
|
|
|
fflush(stdout);
|
|
|
|
(void)LLVMFuzzerTestOneInput(zBuf, sz);
|
2017-03-17 17:59:40 +03:00
|
|
|
if( mDebug ) printf("%s: ", zFilename);
|
2016-11-14 20:25:57 +03:00
|
|
|
printf("ok\n");
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
}
|
|
|
|
free(zBuf);
|
|
|
|
return nErr;
|
|
|
|
}
|