Skip to content

Commit c2ff2e9

Browse files
TheCharlatanfurszy
andcommitted
fuzz: add test case for threadpool
Co-authored-by: furszy <[email protected]>
1 parent 9fc0f61 commit c2ff2e9

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/test/fuzz/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ add_executable(fuzz
119119
string.cpp
120120
strprintf.cpp
121121
system.cpp
122+
threadpool.cpp
122123
timeoffsets.cpp
123124
torcontrol.cpp
124125
transaction.cpp

src/test/fuzz/threadpool.cpp

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

0 commit comments

Comments
 (0)