Branch data Line data Source code
1 : : // Copyright (c) 2024-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <test/fuzz/util/check_globals.h>
6 : :
7 : : #include <test/util/random.h>
8 : : #include <util/time.h>
9 : :
10 : : #include <iostream>
11 : : #include <memory>
12 : : #include <optional>
13 : : #include <string>
14 : :
15 : : struct CheckGlobalsImpl {
16 : 211205 : CheckGlobalsImpl()
17 : : {
18 : 211205 : g_used_g_prng = false;
19 : 211205 : g_seeded_g_prng_zero = false;
20 : 211205 : g_used_system_time = false;
21 : 211205 : SetMockTime(0s);
22 : 211205 : MockableSteadyClock::ClearMockTime();
23 : 211205 : }
24 : 211204 : ~CheckGlobalsImpl()
25 : : {
26 [ + + - + ]: 211204 : if (g_used_g_prng && !g_seeded_g_prng_zero) {
27 : 0 : std::cerr << "\n\n"
28 : : "The current fuzz target used the global random state.\n\n"
29 : :
30 : : "This is acceptable, but requires the fuzz target to call \n"
31 : : "SeedRandomStateForTest(SeedRand::ZEROS) in the first line \n"
32 : : "of the FUZZ_TARGET function.\n\n"
33 : :
34 : : "An alternative solution would be to avoid any use of globals.\n\n"
35 : :
36 : : "Without a solution, fuzz instability and non-determinism can lead \n"
37 : 0 : "to non-reproducible bugs or inefficient fuzzing.\n\n"
38 : 0 : << std::endl;
39 : 0 : std::abort(); // Abort, because AFL may try to recover from a std::exit
40 : : }
41 : :
42 [ - + ]: 211204 : if (g_used_system_time) {
43 : 0 : std::cerr << "\n\n"
44 : : "The current fuzz target accessed system time.\n\n"
45 : :
46 : : "This is acceptable, but requires the fuzz target to use \n"
47 : : "a FakeNodeClock, FakeSteadyClock or call \n"
48 : : "SetMockTime() at the \n" "beginning of processing the \n"
49 : : "fuzz input.\n\n"
50 : :
51 : : "Without setting mock time, time-dependent behavior can lead \n"
52 : 0 : "to non-reproducible bugs or inefficient fuzzing.\n\n"
53 : 0 : << std::endl;
54 : 0 : std::abort();
55 : : }
56 : 211204 : }
57 : : };
58 : :
59 : 211205 : CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {}
60 : 211204 : CheckGlobals::~CheckGlobals() = default;
|