1414
1515#include < csignal>
1616#include < cstdint>
17+ #include < cstdio>
18+ #include < cstdlib>
19+ #include < cstring>
1720#include < exception>
1821#include < fstream>
1922#include < functional>
23+ #include < iostream>
2024#include < map>
2125#include < memory>
2226#include < string>
2327#include < tuple>
2428#include < unistd.h>
29+ #include < utility>
2530#include < vector>
2631
2732const std::function<void (const std::string&)> G_TEST_LOG_FUN{};
@@ -77,13 +82,13 @@ void initialize()
7782 return WrappedGetAddrInfo (name, false );
7883 };
7984
80- bool should_abort {false };
85+ bool should_exit {false };
8186 if (std::getenv (" PRINT_ALL_FUZZ_TARGETS_AND_ABORT" )) {
8287 for (const auto & t : FuzzTargets ()) {
8388 if (std::get<2 >(t.second )) continue ;
8489 std::cout << t.first << std::endl;
8590 }
86- should_abort = true ;
91+ should_exit = true ;
8792 }
8893 if (const char * out_path = std::getenv (" WRITE_ALL_FUZZ_TARGETS_AND_ABORT" )) {
8994 std::cout << " Writing all fuzz target names to '" << out_path << " '." << std::endl;
@@ -92,13 +97,23 @@ void initialize()
9297 if (std::get<2 >(t.second )) continue ;
9398 out_stream << t.first << std::endl;
9499 }
95- should_abort = true ;
100+ should_exit= true ;
101+ }
102+ if (should_exit){
103+ std::exit (EXIT_SUCCESS);
104+ }
105+ if (const auto * env_fuzz{std::getenv (" FUZZ" )}) {
106+ // To allow for easier fuzz executable binary modification,
107+ static std::string g_copy{env_fuzz}; // create copy to avoid compiler optimizations, and
108+ g_fuzz_target = g_copy.c_str (); // strip string after the first null-char.
109+ } else {
110+ std::cerr << " Must select fuzz target with the FUZZ env var." << std::endl;
111+ std::cerr << " Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
112+ std::exit (EXIT_FAILURE);
96113 }
97- Assert (!should_abort);
98- g_fuzz_target = Assert (std::getenv (" FUZZ" ));
99114 const auto it = FuzzTargets ().find (g_fuzz_target);
100115 if (it == FuzzTargets ().end ()) {
101- std::cerr << " No fuzzer for " << g_fuzz_target << " ." << std::endl;
116+ std::cerr << " No fuzz target compiled for " << g_fuzz_target << " ." << std::endl;
102117 std::exit (EXIT_FAILURE);
103118 }
104119 Assert (!g_test_one_input);
0 commit comments