33#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && defined(__AFL_FUZZ_INIT)
46static std::vector<const char*>
g_args;
48static void SetArgs(
int argc,
char** argv) {
49 for (
int i = 1; i < argc; ++i) {
52 if (strlen(argv[i]) > 2 && argv[i][0] ==
'-' && argv[i][1] ==
'-') {
69 static std::map<std::string_view, FuzzTarget> g_fuzz_targets;
70 return g_fuzz_targets;
92#if defined(__clang__) && defined(__linux__)
93extern "C" void __llvm_profile_reset_counters(
void) __attribute__((weak));
94extern "C" void __gcov_reset(
void) __attribute__((weak));
98 if (__llvm_profile_reset_counters) {
99 __llvm_profile_reset_counters();
121 CreateSock = [](int, int, int) -> std::unique_ptr<Sock> { std::terminate(); };
131 bool should_exit{
false};
132 if (std::getenv(
"PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
134 if (
t.opts.hidden)
continue;
135 std::cout <<
name << std::endl;
139 if (
const char* out_path = std::getenv(
"WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
140 std::cout <<
"Writing all fuzz target names to '" << out_path <<
"'." << std::endl;
141 std::ofstream out_stream{out_path, std::ios::binary};
143 if (
t.opts.hidden)
continue;
144 out_stream <<
name << std::endl;
151 if (
const auto* env_fuzz{std::getenv(
"FUZZ")}) {
153 static std::string g_copy{env_fuzz};
156 std::cerr <<
"Must select fuzz target with the FUZZ env var." << std::endl;
157 std::cerr <<
"Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
158 std::exit(EXIT_FAILURE);
162 std::cerr <<
"No fuzz target compiled for " <<
g_fuzz_target <<
"." << std::endl;
163 std::exit(EXIT_FAILURE);
166 std::cerr <<
"Must compile with -DBUILD_FOR_FUZZING=ON to execute a fuzz target." << std::endl;
167 std::exit(EXIT_FAILURE);
171 it->second.opts.init();
176#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
177static bool read_stdin(std::vector<uint8_t>&
data)
179 std::istream::char_type buffer[1024];
180 std::streamsize length;
181 while ((std::cin.read(buffer, 1024), length = std::cin.gcount()) > 0) {
182 data.insert(
data.end(), buffer, buffer + length);
188#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
189static bool read_file(
fs::path p, std::vector<uint8_t>&
data)
191 uint8_t buffer[1024];
193 if (f ==
nullptr)
return false;
195 const size_t length = fread(buffer,
sizeof(uint8_t),
sizeof(buffer), f);
196 if (ferror(f))
return false;
197 data.insert(
data.end(), buffer, buffer + length);
204#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
206void signal_handler(
int signal)
208 if (signal == SIGABRT) {
209 std::cerr <<
"Error processing input " << g_input_path << std::endl;
211 std::cerr <<
"Unexpected signal " << signal <<
" received\n";
213 std::_Exit(EXIT_FAILURE);
232#if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
233int main(
int argc,
char** argv)
239 const uint8_t* buffer = __AFL_FUZZ_TESTCASE_BUF;
240 while (__AFL_LOOP(100000)) {
241 size_t buffer_len = __AFL_FUZZ_TESTCASE_LEN;
245 std::vector<uint8_t> buffer;
247 if (!read_stdin(buffer)) {
253 std::signal(SIGABRT, signal_handler);
254 const auto start_time{Now<SteadySeconds>()};
256 for (
int i = 1; i < argc; ++i) {
258 if (fs::is_directory(input_path)) {
259 for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {
260 if (!fs::is_regular_file(it->path()))
continue;
261 g_input_path = it->
path();
262 Assert(read_file(it->path(), buffer));
268 g_input_path = input_path;
269 Assert(read_file(input_path, buffer));
275 const auto end_time{Now<SteadySeconds>()};
276 std::cout <<
g_fuzz_target <<
": succeeded against " << tested <<
" files in " <<
count_seconds(end_time - start_time) <<
"s." << std::endl;
int main(int argc, char **argv)
#define Assert(val)
Identity function.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
path(std::filesystem::path path)
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts)
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
static const TypeTestOneInput * g_test_one_input
static void SetArgs(int argc, char **argv)
const std::function< std::vector< const char * >()> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
int LLVMFuzzerInitialize(int *argc, char ***argv)
static std::string_view g_fuzz_target
void ResetCoverageCounters()
static std::vector< const char * > g_args
A copy of the command line arguments that start with --.
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
void test_one_input(FuzzBufferType buffer)
std::span< const uint8_t > FuzzBufferType
std::function< void(FuzzBufferType)> TypeTestOneInput
FILE * fopen(const fs::path &p, const char *mode)
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
const TypeTestOneInput test_one_input
const FuzzTargetOptions opts
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.
constexpr int64_t count_seconds(std::chrono::seconds t)