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
20extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
21__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
22int main(int argc, char **argv) {
23 fprintf(stderr, format: "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1);
24 if (LLVMFuzzerInitialize)
25 LLVMFuzzerInitialize(argc: &argc, argv: &argv);
26 for (int i = 1; i < argc; i++) {
27 fprintf(stderr, format: "Running: %s\n", argv[i]);
28 FILE *f = fopen(filename: argv[i], modes: "r");
29 assert(f);
30 fseek(stream: f, off: 0, SEEK_END);
31 size_t len = ftell(stream: f);
32 fseek(stream: f, off: 0, SEEK_SET);
33 unsigned char *buf = (unsigned char*)malloc(size: len);
34 size_t n_read = fread(ptr: buf, size: 1, n: len, stream: f);
35 fclose(stream: f);
36 assert(n_read == len);
37 LLVMFuzzerTestOneInput(data: buf, size: len);
38 free(ptr: buf);
39 fprintf(stderr, format: "Done: %s: (%zd bytes)\n", argv[i], n_read);
40 }
41}
42

source code of compiler-rt/lib/fuzzer/standalone/StandaloneFuzzTargetMain.c