Skip to content

Commit a96b84c

Browse files
committed
fuzz: Abort when calling system time without setting mock time
1 parent ff21870 commit a96b84c

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/test/fuzz/util/check_globals.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/fuzz/util/check_globals.h>
66

77
#include <test/util/random.h>
8+
#include <util/time.h>
89

910
#include <iostream>
1011
#include <memory>
@@ -16,6 +17,8 @@ struct CheckGlobalsImpl {
1617
{
1718
g_used_g_prng = false;
1819
g_seeded_g_prng_zero = false;
20+
g_used_system_time = false;
21+
SetMockTime(0s);
1922
}
2023
~CheckGlobalsImpl()
2124
{
@@ -34,6 +37,19 @@ struct CheckGlobalsImpl {
3437
<< std::endl;
3538
std::abort(); // Abort, because AFL may try to recover from a std::exit
3639
}
40+
41+
if (g_used_system_time) {
42+
std::cerr << "\n\n"
43+
"The current fuzz target accessed system time.\n\n"
44+
45+
"This is acceptable, but requires the fuzz target to call \n"
46+
"SetMockTime() at the beginning of processing the fuzz input.\n\n"
47+
48+
"Without setting mock time, time-dependent behavior can lead \n"
49+
"to non-reproducible bugs or inefficient fuzzing.\n\n"
50+
<< std::endl;
51+
std::abort();
52+
}
3753
}
3854
};
3955

src/test/fuzz/util/check_globals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
#ifndef BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
66
#define BITCOIN_TEST_FUZZ_UTIL_CHECK_GLOBALS_H
77

8+
#include <atomic>
89
#include <memory>
910
#include <optional>
1011
#include <string>
1112

13+
extern std::atomic<bool> g_used_system_time;
14+
1215
struct CheckGlobalsImpl;
1316
struct CheckGlobals {
1417
CheckGlobals();

src/util/time.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
2121

2222
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
23+
std::atomic<bool> g_used_system_time{false};
2324

2425
NodeClock::time_point NodeClock::now() noexcept
2526
{
2627
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
28+
if (!mocktime.count()) {
29+
g_used_system_time = true;
30+
}
2731
const auto ret{
2832
mocktime.count() ?
2933
mocktime :

0 commit comments

Comments
 (0)