Skip to content

Commit bf2c0fb

Browse files
author
MarcoFalke
committed
Merge bitcoin/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 #22763, which was a rebase of #21496, but also reports the name of the fuzzer and the time taken. Fixes #21461 Top commit has no ACKs. Tree-SHA512: d8d046d4a309652eb13de42116276bf992480bc887ad3535a8ff18b354cb24826bc562b06af63802ec945c637f046563b6a5601d6321b46a5543127daafea09b
2 parents 601bfc4 + f59bee3 commit bf2c0fb

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
@@ -10,7 +10,9 @@
1010
#include <test/util/setup_common.h>
1111
#include <util/check.h>
1212
#include <util/sock.h>
13+
#include <util/time.h>
1314

15+
#include <csignal>
1416
#include <cstdint>
1517
#include <exception>
1618
#include <fstream>
@@ -59,6 +61,7 @@ void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target,
5961
Assert(it_ins.second);
6062
}
6163

64+
static std::string_view g_fuzz_target;
6265
static TypeTestOneInput* g_test_one_input{nullptr};
6366

6467
void initialize()
@@ -92,9 +95,12 @@ void initialize()
9295
should_abort = true;
9396
}
9497
Assert(!should_abort);
95-
std::string_view fuzz_target{Assert(std::getenv("FUZZ"))};
96-
const auto it = FuzzTargets().find(fuzz_target);
97-
Assert(it != FuzzTargets().end());
98+
g_fuzz_target = Assert(std::getenv("FUZZ"));
99+
const auto it = FuzzTargets().find(g_fuzz_target);
100+
if (it == FuzzTargets().end()) {
101+
std::cerr << "No fuzzer for " << g_fuzz_target << "." << std::endl;
102+
std::exit(EXIT_FAILURE);
103+
}
98104
Assert(!g_test_one_input);
99105
g_test_one_input = &std::get<0>(it->second);
100106
std::get<1>(it->second)();
@@ -112,6 +118,35 @@ static bool read_stdin(std::vector<uint8_t>& data)
112118
}
113119
#endif
114120

121+
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
122+
static bool read_file(fs::path p, std::vector<uint8_t>& data)
123+
{
124+
uint8_t buffer[1024];
125+
FILE* f = fsbridge::fopen(p, "rb");
126+
if (f == nullptr) return false;
127+
do {
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);
131+
} while (!feof(f));
132+
fclose(f);
133+
return true;
134+
}
135+
#endif
136+
137+
#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && !defined(__AFL_LOOP)
138+
static fs::path g_input_path;
139+
void signal_handler(int signal)
140+
{
141+
if (signal == SIGABRT) {
142+
std::cerr << "Error processing input " << g_input_path << std::endl;
143+
} else {
144+
std::cerr << "Unexpected signal " << signal << " received\n";
145+
}
146+
std::_Exit(EXIT_FAILURE);
147+
}
148+
#endif
149+
115150
// This function is used by libFuzzer
116151
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117152
{
@@ -151,10 +186,37 @@ int main(int argc, char** argv)
151186
}
152187
#else
153188
std::vector<uint8_t> buffer;
154-
if (!read_stdin(buffer)) {
189+
if (argc <= 1) {
190+
if (!read_stdin(buffer)) {
191+
return 0;
192+
}
193+
test_one_input(buffer);
155194
return 0;
156195
}
157-
test_one_input(buffer);
196+
std::signal(SIGABRT, signal_handler);
197+
int64_t start_time = GetTimeSeconds();
198+
int tested = 0;
199+
for (int i = 1; i < argc; ++i) {
200+
fs::path input_path(*(argv + 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);
207+
++tested;
208+
buffer.clear();
209+
}
210+
} else {
211+
g_input_path = input_path;
212+
Assert(read_file(input_path, buffer));
213+
test_one_input(buffer);
214+
++tested;
215+
buffer.clear();
216+
}
217+
}
218+
int64_t end_time = GetTimeSeconds();
219+
std::cout << g_fuzz_target << ": succeeded against " << tested << " files in " << (end_time - start_time) << "s." << std::endl;
158220
#endif
159221
return 0;
160222
}

0 commit comments

Comments
 (0)