Coverage Report

Created: 2025-06-06 15:08

/root/bitcoin/src/test/fuzz/util/check_globals.cpp
Line
Count
Source (jump to first uncovered line)
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
    CheckGlobalsImpl()
17
412
    {
18
412
        g_used_g_prng = false;
19
412
        g_seeded_g_prng_zero = false;
20
412
        g_used_system_time = false;
21
412
        SetMockTime(0s);
22
412
    }
23
    ~CheckGlobalsImpl()
24
412
    {
25
412
        if (g_used_g_prng && !g_seeded_g_prng_zero) {
26
0
            std::cerr << "\n\n"
27
0
                         "The current fuzz target used the global random state.\n\n"
28
29
0
                         "This is acceptable, but requires the fuzz target to call \n"
30
0
                         "SeedRandomStateForTest(SeedRand::ZEROS) in the first line \n"
31
0
                         "of the FUZZ_TARGET function.\n\n"
32
33
0
                         "An alternative solution would be to avoid any use of globals.\n\n"
34
35
0
                         "Without a solution, fuzz instability and non-determinism can lead \n"
36
0
                         "to non-reproducible bugs or inefficient fuzzing.\n\n"
37
0
                      << std::endl;
38
0
            std::abort(); // Abort, because AFL may try to recover from a std::exit
39
0
        }
40
41
412
        if (g_used_system_time) {
42
0
            std::cerr << "\n\n"
43
0
                         "The current fuzz target accessed system time.\n\n"
44
45
0
                         "This is acceptable, but requires the fuzz target to call \n"
46
0
                         "SetMockTime() at the beginning of processing the fuzz input.\n\n"
47
48
0
                         "Without setting mock time, time-dependent behavior can lead \n"
49
0
                         "to non-reproducible bugs or inefficient fuzzing.\n\n"
50
0
                      << std::endl;
51
0
            std::abort();
52
0
        }
53
412
    }
54
};
55
56
412
CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {}
57
412
CheckGlobals::~CheckGlobals() = default;