36 static std::vector<const char*>
g_args;
38 static void SetArgs(
int argc,
char** argv) {
39 for (
int i = 1; i < argc; ++i) {
42 if (strlen(argv[i]) > 2 && argv[i][0] ==
'-' && argv[i][1] ==
'-') {
52 std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>>&
FuzzTargets()
54 static std::map<std::string_view, std::tuple<TypeTestOneInput, TypeInitialize, TypeHidden>> g_fuzz_targets;
55 return g_fuzz_targets;
60 const auto it_ins =
FuzzTargets().try_emplace(
name, std::move(target), std::move(
init), hidden);
80 bool should_abort{
false};
81 if (std::getenv(
"PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
83 if (std::get<2>(
t.second))
continue;
84 std::cout <<
t.first << std::endl;
88 if (
const char* out_path = std::getenv(
"WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
89 std::cout <<
"Writing all fuzz target names to '" << out_path <<
"'." << std::endl;
90 std::ofstream out_stream{out_path, std::ios::binary};
92 if (std::get<2>(
t.second))
continue;
93 out_stream <<
t.first << std::endl;
101 std::cerr <<
"No fuzzer for " <<
g_fuzz_target <<
"." << std::endl;
102 std::exit(EXIT_FAILURE);
106 std::get<1>(it->second)();
109 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
110 static bool read_stdin(std::vector<uint8_t>& data)
112 uint8_t buffer[1024];
114 while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) {
115 data.insert(data.end(), buffer, buffer + length);
121 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
122 static bool read_file(
fs::path p, std::vector<uint8_t>& data)
124 uint8_t buffer[1024];
126 if (f ==
nullptr)
return false;
128 const size_t length = fread(buffer,
sizeof(uint8_t),
sizeof(buffer), f);
129 if (ferror(f))
return false;
130 data.insert(data.end(), buffer, buffer + length);
137 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
139 void signal_handler(
int signal)
141 if (signal == SIGABRT) {
142 std::cerr <<
"Error processing input " << g_input_path << std::endl;
144 std::cerr <<
"Unexpected signal " << signal <<
" received\n";
146 std::_Exit(EXIT_FAILURE);
154 test_one_input({data, size});
166 #if defined(PROVIDE_FUZZ_MAIN_FUNCTION)
167 int main(
int argc,
char** argv)
180 while (__AFL_LOOP(1000)) {
181 std::vector<uint8_t> buffer;
182 if (!read_stdin(buffer)) {
185 test_one_input(buffer);
188 std::vector<uint8_t> buffer;
190 if (!read_stdin(buffer)) {
193 test_one_input(buffer);
196 std::signal(SIGABRT, signal_handler);
197 const auto start_time{Now<SteadySeconds>()};
199 for (
int i = 1; i < argc; ++i) {
201 if (fs::is_directory(input_path)) {
202 for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {
203 if (!fs::is_regular_file(it->path()))
continue;
204 g_input_path = it->
path();
205 Assert(read_file(it->path(), buffer));
206 test_one_input(buffer);
211 g_input_path = input_path;
212 Assert(read_file(input_path, buffer));
213 test_one_input(buffer);
218 const auto end_time{Now<SteadySeconds>()};
219 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.
A combination of a network address (CNetAddr) and a (TCP) port.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
path(std::filesystem::path path)
std::map< std::string_view, std::tuple< TypeTestOneInput, TypeInitialize, TypeHidden > > & FuzzTargets()
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
static void SetArgs(int argc, char **argv)
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init, TypeHidden hidden)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
int LLVMFuzzerInitialize(int *argc, char ***argv)
static TypeTestOneInput * g_test_one_input
static std::string_view g_fuzz_target
static std::vector< const char * > g_args
A copy of the command line arguments that start with --.
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
std::function< void(FuzzBufferType)> TypeTestOneInput
std::function< void()> TypeInitialize
FILE * fopen(const fs::path &p, const char *mode)
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
constexpr int64_t count_seconds(std::chrono::seconds t)