LCOV - code coverage report
Current view: top level - src/test/fuzz - threadpool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 96.2 % 52 50
Test Date: 2026-11-21 05:38:30 Functions: 100.0 % 6 6
Branches: 63.6 % 44 28

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 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 <util/threadpool.h>
       6                 :             : 
       7                 :             : #include <logging.h>
       8                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       9                 :             : #include <test/fuzz/fuzz.h>
      10                 :             : #include <util/check.h>
      11                 :             : 
      12                 :             : #include <atomic>
      13                 :             : #include <future>
      14                 :             : #include <queue>
      15                 :             : 
      16                 :             : struct ExpectedException : std::runtime_error {
      17         [ +  - ]:        9601 :     explicit ExpectedException(const std::string& msg) : std::runtime_error(msg) {}
      18                 :             : };
      19                 :             : 
      20                 :             : struct ThrowTask {
      21         [ +  - ]:       19202 :     void operator()() const { throw ExpectedException("fail"); }
      22                 :             : };
      23                 :             : 
      24                 :             : struct CounterTask {
      25                 :             :     std::atomic_uint32_t& m_counter;
      26                 :       45763 :     explicit CounterTask(std::atomic_uint32_t& counter) : m_counter{counter} {}
      27                 :       45763 :     void operator()() const { m_counter.fetch_add(1, std::memory_order_relaxed); }
      28                 :             : };
      29                 :             : 
      30                 :             : // Waits for a future to complete. Increments 'fail_counter' if the expected exception is thrown.
      31                 :       55364 : static void GetFuture(std::future<void>& future, uint32_t& fail_counter)
      32                 :             : {
      33                 :       55364 :     try {
      34         [ +  + ]:       55364 :         future.get();
      35         [ +  - ]:        9601 :     } catch (const ExpectedException&) {
      36                 :        9601 :         fail_counter++;
      37                 :        9601 :     } catch (...) {
      38                 :           0 :         AssertUnreachable(); // Unexpected exception type
      39                 :           0 :     }
      40                 :       55364 : }
      41                 :             : 
      42                 :             : // Global thread pool for fuzzing. Persisting it across iterations prevents
      43                 :             : // the excessive thread creation/destruction overhead that can lead to
      44                 :             : // instability in the fuzzing environment.
      45                 :             : // This is also how we use it in the app's lifecycle.
      46                 :             : ThreadPool g_pool{"fuzz"};
      47                 :             : // Global to verify we always have the same number of threads.
      48                 :             : size_t g_num_workers = 3;
      49                 :             : 
      50                 :         101 : static void StartPoolIfNeeded()
      51                 :             : {
      52         [ +  + ]:         101 :     if (g_pool.WorkersCount() == g_num_workers) return;
      53                 :           1 :     g_pool.Start(g_num_workers);
      54                 :             : }
      55                 :             : 
      56                 :           1 : static void setup_threadpool_test()
      57                 :             : {
      58                 :             :     // Disable logging entirely. It seems to cause memory leaks.
      59                 :           1 :     LogInstance().DisableLogging();
      60                 :           1 : }
      61                 :             : 
      62         [ +  - ]:         577 : FUZZ_TARGET(threadpool, .init = setup_threadpool_test)
      63                 :             : {
      64                 :             :     // Because LibAFL calls fork() after calling the init setup function,
      65                 :             :     // the child processes end up having one thread active and no workers.
      66                 :             :     // To work around this limitation, start thread pool inside the first runner.
      67                 :         101 :     StartPoolIfNeeded();
      68                 :             : 
      69                 :         101 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      70                 :             : 
      71                 :         101 :     const uint32_t num_tasks = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, 1024);
      72         [ -  + ]:         101 :     assert(g_pool.WorkersCount() == g_num_workers);
      73         [ -  + ]:         101 :     assert(g_pool.WorkQueueSize() == 0);
      74                 :             : 
      75                 :             :     // Counters
      76                 :         101 :     std::atomic_uint32_t task_counter{0};
      77                 :         101 :     uint32_t fail_counter{0};
      78                 :         101 :     uint32_t expected_task_counter{0};
      79                 :         101 :     uint32_t expected_fail_tasks{0};
      80                 :             : 
      81                 :         101 :     std::queue<std::future<void>> futures;
      82         [ +  + ]:       55465 :     for (uint32_t i = 0; i < num_tasks; ++i) {
      83                 :       55364 :         const bool will_throw = fuzzed_data_provider.ConsumeBool();
      84                 :       55364 :         const bool wait_immediately = fuzzed_data_provider.ConsumeBool();
      85                 :             : 
      86                 :       55364 :         std::future<void> fut;
      87         [ +  + ]:       55364 :         if (will_throw) {
      88                 :        9601 :             expected_fail_tasks++;
      89         [ +  - ]:        9601 :             fut = *Assert(g_pool.Submit(ThrowTask{}));
      90                 :             :         } else {
      91                 :       45763 :             expected_task_counter++;
      92         [ +  - ]:       45763 :             fut = *Assert(g_pool.Submit(CounterTask{task_counter}));
      93                 :             :         }
      94                 :             : 
      95                 :             :         // If caller wants to wait immediately, consume the future here (safe).
      96         [ +  + ]:       55364 :         if (wait_immediately) {
      97                 :             :             // Waits for this task to complete immediately; prior queued tasks may also complete
      98                 :             :             // as they were queued earlier.
      99         [ +  - ]:       10216 :             GetFuture(fut, fail_counter);
     100                 :             :         } else {
     101                 :             :             // Store task for a posterior check
     102   [ +  -  -  + ]:       55364 :             futures.emplace(std::move(fut));
     103                 :             :         }
     104                 :       55364 :     }
     105                 :             : 
     106                 :             :     // Drain remaining futures
     107         [ +  + ]:       45249 :     while (!futures.empty()) {
     108                 :       45148 :         auto fut = std::move(futures.front());
     109                 :       45148 :         futures.pop();
     110         [ +  - ]:       45148 :         GetFuture(fut, fail_counter);
     111                 :       45148 :     }
     112                 :             : 
     113   [ +  -  -  + ]:         101 :     assert(g_pool.WorkQueueSize() == 0);
     114         [ -  + ]:         101 :     assert(task_counter.load() == expected_task_counter);
     115         [ -  + ]:         101 :     assert(fail_counter == expected_fail_tasks);
     116                 :         101 : }
        

Generated by: LCOV version 2.5.0-full