Skip to content

Commit 3cabce6

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#24472: fuzz: execute each file in dir without fuzz engine
f59bee3 fuzz: execute each file in dir without fuzz engine (Anthony Towns) Pull request description: Phony fuzzing (phuzzing)! Run the fuzz testing code against known inputs to detect errors. Advantage is you can easily test using the existing qa-assets datasets without having to compile with fuzzing enabled; disadvantage is that it doesn't do any actual fuzzing. Example usage: ``` $ for a in ${QA_ASSETS}/fuzz_seed_corpus/*; do echo ${a##*/}; done | xargs -P8 -I {} /bin/sh -c "FUZZ={} test/fuzz/fuzz ${QA_ASSETS}/fuzz_seed_corpus/{}" No fuzzer for address_deserialize. No fuzzer for addrdb. No fuzzer for banentry_deserialize. addition_overflow: succeeded against 848 files in 0s. asmap: succeeded against 981 files in 0s. checkqueue: succeeded against 211 files in 0s. ... ``` (`-P8` says run 8 of the tasks in parallel) If there are failures, the first one will be reported and the program will abort with output like: ``` fuzz: test/fuzz/versionbits.cpp:336: void (anonymous namespace)::versionbits_fuzz_target(FuzzBufferType): Assertion `exp_state != ThresholdState::FAILED' failed. Error processing seed "corpus/versionbits/35345ae8e722234095810b1117a29b63af7621af" ``` Rebase of bitcoin#22763, which was a rebase of bitcoin#21496, but also reports the name of the fuzzer and the time taken. Fixes bitcoin#21461 Top commit has no ACKs. Tree-SHA512: d8d046d4a309652eb13de42116276bf992480bc887ad3535a8ff18b354cb24826bc562b06af63802ec945c637f046563b6a5601d6321b46a5543127daafea09b
1 parent f5116a7 commit 3cabce6

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <test/util/setup_common.h>
1010
#include <util/check.h>
1111
#include <util/sock.h>
12+
#include <util/time.h>
1213

14+
#include <csignal>
1315
#include <cstdint>
1416
#include <string>
1517
#include <exception>
@@ -31,6 +33,7 @@ void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target,
3133
Assert(it_ins.second);
3234
}
3335

36+
static std::string_view g_fuzz_target;
3437
static TypeTestOneInput* g_test_one_input{nullptr};
3538

3639
void initialize()
@@ -64,9 +67,12 @@ void initialize()
6467
should_abort = true;
6568
}
6669
Assert(!should_abort);
67-
std::string_view fuzz_target{Assert(std::getenv("FUZZ"))};
68-
const auto it = FuzzTargets().find(fuzz_target);
69-
Assert(it != FuzzTargets().end());
70+
g_fuzz_target = Assert(std::getenv("FUZZ"));
71+
const auto it = FuzzTargets().find(g_fuzz_target);
72+
if (it == FuzzTargets().end()) {
73+
std::cerr << "No fuzzer for " << g_fuzz_target << "." << std::endl;
74+
std::exit(EXIT_FAILURE);
75+
}
7076
Assert(!g_test_one_input);
7177
g_test_one_input = &std::get<0>(it->second);
7278
std::get<1>(it->second)();
@@ -84,6 +90,35 @@ static bool read_stdin(std::vector<uint8_t>& data)
8490
}
8591
#endif
8692

93+
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
94+
static bool read_file(fs::path p, std::vector<uint8_t>& data)
95+
{
96+
uint8_t buffer[1024];
97+
FILE* f = fsbridge::fopen(p, "rb");
98+
if (f == nullptr) return false;
99+
do {
100+
const size_t length = fread(buffer, sizeof(uint8_t), sizeof(buffer), f);
101+
if (ferror(f)) return false;
102+
data.insert(data.end(), buffer, buffer + length);
103+
} while (!feof(f));
104+
fclose(f);
105+
return true;
106+
}
107+
#endif
108+
109+
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
110+
static fs::path g_input_path;
111+
void signal_handler(int signal)
112+
{
113+
if (signal == SIGABRT) {
114+
std::cerr << "Error processing input " << g_input_path << std::endl;
115+
} else {
116+
std::cerr << "Unexpected signal " << signal << " received\n";
117+
}
118+
std::_Exit(EXIT_FAILURE);
119+
}
120+
#endif
121+
87122
// This function is used by libFuzzer
88123
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89124
{
@@ -122,10 +157,37 @@ int main(int argc, char** argv)
122157
}
123158
#else
124159
std::vector<uint8_t> buffer;
125-
if (!read_stdin(buffer)) {
160+
if (argc <= 1) {
161+
if (!read_stdin(buffer)) {
162+
return 0;
163+
}
164+
test_one_input(buffer);
126165
return 0;
127166
}
128-
test_one_input(buffer);
167+
std::signal(SIGABRT, signal_handler);
168+
int64_t start_time = GetTimeSeconds();
169+
int tested = 0;
170+
for (int i = 1; i < argc; ++i) {
171+
fs::path input_path(*(argv + i));
172+
if (fs::is_directory(input_path)) {
173+
for (fs::directory_iterator it(input_path); it != fs::directory_iterator(); ++it) {
174+
if (!fs::is_regular_file(it->path())) continue;
175+
g_input_path = it->path();
176+
Assert(read_file(it->path(), buffer));
177+
test_one_input(buffer);
178+
++tested;
179+
buffer.clear();
180+
}
181+
} else {
182+
g_input_path = input_path;
183+
Assert(read_file(input_path, buffer));
184+
test_one_input(buffer);
185+
++tested;
186+
buffer.clear();
187+
}
188+
}
189+
int64_t end_time = GetTimeSeconds();
190+
std::cout << g_fuzz_target << ": succeeded against " << tested << " files in " << (end_time - start_time) << "s." << std::endl;
129191
#endif
130192
return 0;
131193
}

0 commit comments

Comments
 (0)