1//===-- FuzzerCLI.h - Common logic for CLIs of fuzzers ----------*- C++ -*-===//
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//
9// Common logic needed to implement LLVM's fuzz targets' CLIs - including LLVM
10// concepts like cl::opt and libFuzzer concepts like -ignore_remaining_args=1.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_FUZZMUTATE_FUZZERCLI_H
15#define LLVM_FUZZMUTATE_FUZZERCLI_H
16
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/DataTypes.h"
19#include <stddef.h>
20
21namespace llvm {
22
23class StringRef;
24
25/// Parse cl::opts from a fuzz target commandline.
26///
27/// This handles all arguments after -ignore_remaining_args=1 as cl::opts.
28LLVM_ABI void parseFuzzerCLOpts(int ArgC, char *ArgV[]);
29
30/// Handle backend options that are encoded in the executable name.
31///
32/// Parses some common backend options out of a specially crafted executable
33/// name (argv[0]). For example, a name like llvm-foo-fuzzer--aarch64-gisel
34/// might set up an AArch64 triple and the Global ISel selector. This should be
35/// called *before* parseFuzzerCLOpts if calling both.
36///
37/// This is meant to be used for environments like OSS-Fuzz that aren't capable
38/// of passing in command line arguments in the normal way.
39LLVM_ABI void handleExecNameEncodedBEOpts(StringRef ExecName);
40
41/// Handle optimizer options which are encoded in the executable name.
42/// Same semantics as in 'handleExecNameEncodedBEOpts'.
43LLVM_ABI void handleExecNameEncodedOptimizerOpts(StringRef ExecName);
44
45using FuzzerTestFun = int (*)(const uint8_t *Data, size_t Size);
46using FuzzerInitFun = int (*)(int *argc, char ***argv);
47
48/// Runs a fuzz target on the inputs specified on the command line.
49///
50/// Useful for testing fuzz targets without linking to libFuzzer. Finds inputs
51/// in the argument list in a libFuzzer compatible way.
52LLVM_ABI int runFuzzerOnInputs(
53 int ArgC, char *ArgV[], FuzzerTestFun TestOne,
54 FuzzerInitFun Init = [](int *, char ***) { return 0; });
55
56} // namespace llvm
57
58#endif // LLVM_FUZZMUTATE_FUZZERCLI_H
59