| 1 |
/*===- StandaloneFuzzTargetMain.c - standalone main() for fuzz targets. ---===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// This main() function can be linked to a fuzz target (i.e. a library |
| 9 |
// that exports LLVMFuzzerTestOneInput() and possibly LLVMFuzzerInitialize()) |
| 10 |
// instead of libFuzzer. This main() function will not perform any fuzzing |
| 11 |
// but will simply feed all input files one by one to the fuzz target. |
| 12 |
// |
| 13 |
// Use this file to provide reproducers for bugs when linking against libFuzzer |
| 14 |
// or other fuzzing engine is undesirable. |
| 15 |
//===----------------------------------------------------------------------===*/ |
| 16 |
#include <assert.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); |
| 21 |
__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 22 |
int main(int argc, char **argv) { |
| 23 |
fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1); |
| 24 |
if (LLVMFuzzerInitialize) |
| 25 |
LLVMFuzzerInitialize(&argc, &argv); |
| 26 |
for (int i = 1; i < argc; i++) { |
| 27 |
fprintf(stderr, "Running: %s\n", argv[i]); |
| 28 |
FILE *f = fopen(argv[i], "r"); |
| 29 |
assert(f); |
| 30 |
fseek(f, 0, SEEK_END); |
| 31 |
size_t len = ftell(f); |
| 32 |
fseek(f, 0, SEEK_SET); |
| 33 |
unsigned char *buf = (unsigned char*)malloc(len); |
| 34 |
size_t n_read = fread(buf, 1, len, f); |
| 35 |
fclose(f); |
| 36 |
assert(n_read == len); |
| 37 |
LLVMFuzzerTestOneInput(buf, len); |
| 38 |
free(buf); |
| 39 |
fprintf(stderr, "Done: %s: (%zd bytes)\n", argv[i], n_read); |
| 40 |
} |
| 41 |
} |