LCOV - code coverage report
Current view: top level - src/test/fuzz/util - check_globals.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 60.0 % 20 12
Test Date: 2025-01-19 04:06:55 Functions: 100.0 % 4 4
Branches: 66.7 % 6 4

             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                 :      178804 :     CheckGlobalsImpl()
      17                 :             :     {
      18                 :      178804 :         g_used_g_prng = false;
      19                 :      178804 :         g_seeded_g_prng_zero = false;
      20                 :      178804 :         g_used_system_time = false;
      21                 :      178804 :         SetMockTime(0s);
      22                 :      178804 :     }
      23                 :      178804 :     ~CheckGlobalsImpl()
      24                 :             :     {
      25   [ +  +  -  + ]:      178804 :         if (g_used_g_prng && !g_seeded_g_prng_zero) {
      26                 :           0 :             std::cerr << "\n\n"
      27                 :             :                          "The current fuzz target used the global random state.\n\n"
      28                 :             : 
      29                 :             :                          "This is acceptable, but requires the fuzz target to call \n"
      30                 :             :                          "SeedRandomStateForTest(SeedRand::ZEROS) in the first line \n"
      31                 :             :                          "of the FUZZ_TARGET function.\n\n"
      32                 :             : 
      33                 :             :                          "An alternative solution would be to avoid any use of globals.\n\n"
      34                 :             : 
      35                 :             :                          "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                 :             :         }
      40                 :             : 
      41         [ -  + ]:      178804 :         if (g_used_system_time) {
      42                 :           0 :             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                 :           0 :                          "to non-reproducible bugs or inefficient fuzzing.\n\n"
      50                 :           0 :                       << std::endl;
      51                 :           0 :             std::abort();
      52                 :             :         }
      53                 :      178804 :     }
      54                 :             : };
      55                 :             : 
      56                 :      178804 : CheckGlobals::CheckGlobals() : m_impl(std::make_unique<CheckGlobalsImpl>()) {}
      57                 :      178804 : CheckGlobals::~CheckGlobals() = default;
        

Generated by: LCOV version 2.0-1