mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 14:37:42 -03:00
fuzz: Abort when calling system time without setting mock time
This commit is contained in:
parent
ff21870e20
commit
a96b84cb1b
3 changed files with 23 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include <test/fuzz/util/check_globals.h>
|
#include <test/fuzz/util/check_globals.h>
|
||||||
|
|
||||||
#include <test/util/random.h>
|
#include <test/util/random.h>
|
||||||
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -16,6 +17,8 @@ struct CheckGlobalsImpl {
|
||||||
{
|
{
|
||||||
g_used_g_prng = false;
|
g_used_g_prng = false;
|
||||||
g_seeded_g_prng_zero = false;
|
g_seeded_g_prng_zero = false;
|
||||||
|
g_used_system_time = false;
|
||||||
|
SetMockTime(0s);
|
||||||
}
|
}
|
||||||
~CheckGlobalsImpl()
|
~CheckGlobalsImpl()
|
||||||
{
|
{
|
||||||
|
@ -34,6 +37,19 @@ struct CheckGlobalsImpl {
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
std::abort(); // Abort, because AFL may try to recover from a std::exit
|
std::abort(); // Abort, because AFL may try to recover from a std::exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (g_used_system_time) {
|
||||||
|
std::cerr << "\n\n"
|
||||||
|
"The current fuzz target accessed system time.\n\n"
|
||||||
|
|
||||||
|
"This is acceptable, but requires the fuzz target to call \n"
|
||||||
|
"SetMockTime() at the beginning of processing the fuzz input.\n\n"
|
||||||
|
|
||||||
|
"Without setting mock time, time-dependent behavior can lead \n"
|
||||||
|
"to non-reproducible bugs or inefficient fuzzing.\n\n"
|
||||||
|
<< std::endl;
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,13 @@
|
||||||
#ifndef BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
|
#ifndef BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
|
||||||
#define BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
|
#define BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
extern std::atomic<bool> g_used_system_time;
|
||||||
|
|
||||||
struct CheckGlobalsImpl;
|
struct CheckGlobalsImpl;
|
||||||
struct CheckGlobals {
|
struct CheckGlobals {
|
||||||
CheckGlobals();
|
CheckGlobals();
|
||||||
|
|
|
@ -20,10 +20,14 @@
|
||||||
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
||||||
|
|
||||||
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
|
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
|
||||||
|
std::atomic<bool> g_used_system_time{false};
|
||||||
|
|
||||||
NodeClock::time_point NodeClock::now() noexcept
|
NodeClock::time_point NodeClock::now() noexcept
|
||||||
{
|
{
|
||||||
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
|
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
|
||||||
|
if (!mocktime.count()) {
|
||||||
|
g_used_system_time = true;
|
||||||
|
}
|
||||||
const auto ret{
|
const auto ret{
|
||||||
mocktime.count() ?
|
mocktime.count() ?
|
||||||
mocktime :
|
mocktime :
|
||||||
|
|
Loading…
Add table
Reference in a new issue