Skip to content

Commit

Permalink
merge bitcoin#27672: Print error message when FUZZ is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Feb 12, 2025
1 parent 1f4c0b5 commit 84d6a7c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 5 additions & 1 deletion ci/test/04_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ if [ "$RUN_FUZZ_TESTS" = "true" ] || [ "$RUN_UNIT_TESTS" = "true" ] || [ "$RUN_U
if [ ! -d "${DIR_QA_ASSETS}" ]; then
DOCKER_EXEC git clone --depth=1 https://github.com/bitcoin-core/qa-assets "${DIR_QA_ASSETS}"
fi

(
DOCKER_EXEC cd "${DIR_QA_ASSETS}"
DOCKER_EXEC echo "Using qa-assets repo from commit ..."
DOCKER_EXEC git log -1
)
export DIR_FUZZ_IN=${DIR_QA_ASSETS}/fuzz_seed_corpus/
fi

Expand Down
2 changes: 1 addition & 1 deletion src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <tinyformat.h>

#include <cstdio>
#include <filesystem>
#include <filesystem> // IWYU pragma: export
#include <iomanip>
#include <ios>
#include <ostream>
Expand Down
27 changes: 21 additions & 6 deletions src/test/fuzz/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <unistd.h>
#include <utility>
#include <vector>

#if defined(PROVIDE_FUZZ_MAIN_FUNCTION) && defined(__AFL_FUZZ_INIT)
Expand Down Expand Up @@ -79,13 +84,13 @@ void initialize()
return WrappedGetAddrInfo(name, false);
};

bool should_abort{false};
bool should_exit{false};
if (std::getenv("PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
for (const auto& t : FuzzTargets()) {
if (std::get<2>(t.second)) continue;
std::cout << t.first << std::endl;
}
should_abort = true;
should_exit = true;
}
if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl;
Expand All @@ -94,13 +99,23 @@ void initialize()
if (std::get<2>(t.second)) continue;
out_stream << t.first << std::endl;
}
should_abort = true;
should_exit= true;
}
if (should_exit){
std::exit(EXIT_SUCCESS);
}
if (const auto* env_fuzz{std::getenv("FUZZ")}) {
// To allow for easier fuzz executable binary modification,
static std::string g_copy{env_fuzz}; // create copy to avoid compiler optimizations, and
g_fuzz_target = g_copy.c_str(); // strip string after the first null-char.
} else {
std::cerr << "Must select fuzz target with the FUZZ env var." << std::endl;
std::cerr << "Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
std::exit(EXIT_FAILURE);
}
Assert(!should_abort);
g_fuzz_target = Assert(std::getenv("FUZZ"));
const auto it = FuzzTargets().find(g_fuzz_target);
if (it == FuzzTargets().end()) {
std::cerr << "No fuzzer for " << g_fuzz_target << "." << std::endl;
std::cerr << "No fuzz target compiled for " << g_fuzz_target << "." << std::endl;
std::exit(EXIT_FAILURE);
}
Assert(!g_test_one_input);
Expand Down

0 comments on commit 84d6a7c

Please sign in to comment.