32 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && defined(__AFL_FUZZ_INIT) 45 static std::vector<const char*>
g_args;
47 static void SetArgs(
int argc,
char** argv) {
48 for (
int i = 1; i < argc; ++i) {
51 if (strlen(argv[i]) > 2 && argv[i][0] ==
'-' && argv[i][1] ==
'-') {
68 static std::map<std::string_view, FuzzTarget> g_fuzz_targets;
69 return g_fuzz_targets;
94 bool should_exit{
false};
95 if (std::getenv(
"PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
97 if (
t.opts.hidden)
continue;
98 std::cout <<
name << std::endl;
102 if (
const char* out_path = std::getenv(
"WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
103 std::cout <<
"Writing all fuzz target names to '" << out_path <<
"'." << std::endl;
104 std::ofstream out_stream{out_path, std::ios::binary};
106 if (
t.opts.hidden)
continue;
107 out_stream <<
name << std::endl;
114 if (
const auto* env_fuzz{std::getenv(
"FUZZ")}) {
116 static std::string g_copy{env_fuzz};
119 std::cerr <<
"Must select fuzz target with the FUZZ env var." << std::endl;
120 std::cerr <<
"Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
121 std::exit(EXIT_FAILURE);
125 std::cerr <<
"No fuzz target compiled for " <<
g_fuzz_target <<
"." << std::endl;
126 std::exit(EXIT_FAILURE);
130 it->second.opts.init();
133 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) 134 static bool read_stdin(std::vector<uint8_t>& data)
136 uint8_t buffer[1024];
138 while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) {
139 data.insert(data.end(), buffer, buffer + length);
145 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP) 146 static bool read_file(
fs::path p, std::vector<uint8_t>& data)
148 uint8_t buffer[1024];
150 if (f ==
nullptr)
return false;
152 const size_t length = fread(buffer,
sizeof(uint8_t),
sizeof(buffer), f);
153 if (ferror(f))
return false;
154 data.insert(data.end(), buffer, buffer + length);
161 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP) 163 void signal_handler(
int signal)
165 if (signal == SIGABRT) {
166 std::cerr <<
"Error processing input " << g_input_path << std::endl;
168 std::cerr <<
"Unexpected signal " << signal <<
" received\n";
170 std::_Exit(EXIT_FAILURE);
178 test_one_input({data, size});
190 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) 191 int main(
int argc,
char** argv)
198 const uint8_t* buffer = __AFL_FUZZ_TESTCASE_BUF;
199 while (__AFL_LOOP(100000)) {
200 size_t buffer_len = __AFL_FUZZ_TESTCASE_LEN;
201 test_one_input({buffer, buffer_len});
204 std::vector<uint8_t> buffer;
206 if (!read_stdin(buffer)) {
209 test_one_input(buffer);
212 std::signal(SIGABRT, signal_handler);
213 const auto start_time{Now<SteadySeconds>()};
215 for (
int i = 1; i < argc; ++i) {
217 if (fs::is_directory(input_path)) {
218 for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {
219 if (!fs::is_regular_file(it->path()))
continue;
220 g_input_path = it->
path();
221 Assert(read_file(it->path(), buffer));
222 test_one_input(buffer);
227 g_input_path = input_path;
228 Assert(read_file(input_path, buffer));
229 test_one_input(buffer);
234 const auto end_time{Now<SteadySeconds>()};
235 std::cout <<
g_fuzz_target <<
": succeeded against " << tested <<
" files in " <<
count_seconds(end_time - start_time) <<
"s." << std::endl;
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
FILE * fopen(const fs::path &p, const char *mode)
int LLVMFuzzerInitialize(int *argc, char ***argv)
static const TypeTestOneInput * g_test_one_input
static void SetArgs(int argc, char **argv)
std::function< void(FuzzBufferType)> TypeTestOneInput
const TypeTestOneInput test_one_input
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
static std::vector< const char * > g_args
A copy of the command line arguments that start with --.
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
A combination of a network address (CNetAddr) and a (TCP) port.
path(std::filesystem::path path)
constexpr int64_t count_seconds(std::chrono::seconds t)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
int main(int argc, char **argv)
static std::string_view g_fuzz_target
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, FuzzTargetOptions opts)
const FuzzTargetOptions opts
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
#define Assert(val)
Identity function.