Skip to content

Commit b9c504c

Browse files
committed
Merge #18742: miner: Avoid stack-use-after-return in validationinterface
7777f2a miner: Avoid stack-use-after-return in validationinterface (MarcoFalke) fa5ceb2 test: Remove UninterruptibleSleep from test and replace it by SyncWithValidationInterfaceQueue (MarcoFalke) fa770ce validationinterface: Rework documentation, Rename pwalletIn to callbacks (MarcoFalke) fab6d06 test: Add unregister_validation_interface_race test (MarcoFalke) Pull request description: When a validationinterface has itself unregistered in one thread, but is about to get executed in another thread [1], there is a race: * The validationinterface destructing itself * The validationinterface getting dereferenced for execution [1] https://github.com/bitcoin/bitcoin/blob/64139803f1225dab26197a20314109d37fa87d5f/src/validationinterface.cpp#L82-L83 This happens in the miner. More generally it happens everywhere where at least one thread is generating notifications and another one is unregistering a validationinterface. This issue has been fixed in commit ab31b9d, but the fix has not been applied to the miner. Example where this happened in practice: https://travis-ci.org/github/bitcoin/bitcoin/jobs/675322230#L4414 ACKs for top commit: promag: Code review ACK 7777f2a. laanwj: Code review ACK 7777f2a Tree-SHA512: 8087119243c71ba18a823a63515f3730d127162625d8729024278b447af29e2ff206f4840ee3d90bf84f93a2c5ab73b76c7e7044c83aa93b5b51047a166ec3d3
2 parents 04c0955 + 7777f2a commit b9c504c

File tree

5 files changed

+75
-34
lines changed

5 files changed

+75
-34
lines changed

src/rpc/mining.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
874874
return result;
875875
}
876876

877-
class submitblock_StateCatcher : public CValidationInterface
877+
class submitblock_StateCatcher final : public CValidationInterface
878878
{
879879
public:
880880
uint256 hash;
@@ -942,17 +942,17 @@ static UniValue submitblock(const JSONRPCRequest& request)
942942
}
943943

944944
bool new_block;
945-
submitblock_StateCatcher sc(block.GetHash());
946-
RegisterValidationInterface(&sc);
945+
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
946+
RegisterSharedValidationInterface(sc);
947947
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
948-
UnregisterValidationInterface(&sc);
948+
UnregisterSharedValidationInterface(sc);
949949
if (!new_block && accepted) {
950950
return "duplicate";
951951
}
952-
if (!sc.found) {
952+
if (!sc->found) {
953953
return "inconclusive";
954954
}
955-
return BIP22ValidationResult(sc.state);
955+
return BIP22ValidationResult(sc->state);
956956
}
957957

958958
static UniValue submitheader(const JSONRPCRequest& request)

src/test/validation_block_tests.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct MinerTestingSetup : public RegTestingSetup {
3232

3333
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
3434

35-
struct TestSubscriber : public CValidationInterface {
35+
struct TestSubscriber final : public CValidationInterface {
3636
uint256 m_expected_tip;
3737

3838
explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
@@ -175,8 +175,8 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
175175
LOCK(cs_main);
176176
initial_tip = ::ChainActive().Tip();
177177
}
178-
TestSubscriber sub(initial_tip->GetBlockHash());
179-
RegisterValidationInterface(&sub);
178+
auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash());
179+
RegisterSharedValidationInterface(sub);
180180

181181
// create a bunch of threads that repeatedly process a block generated above at random
182182
// this will create parallelism and randomness inside validation - the ValidationInterface
@@ -204,14 +204,12 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
204204
for (auto& t : threads) {
205205
t.join();
206206
}
207-
while (GetMainSignals().CallbacksPending() > 0) {
208-
UninterruptibleSleep(std::chrono::milliseconds{100});
209-
}
207+
SyncWithValidationInterfaceQueue();
210208

211-
UnregisterValidationInterface(&sub);
209+
UnregisterSharedValidationInterface(sub);
212210

213211
LOCK(cs_main);
214-
BOOST_CHECK_EQUAL(sub.m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
212+
BOOST_CHECK_EQUAL(sub->m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
215213
}
216214

217215
/**

src/test/validationinterface_tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@
1212

1313
BOOST_FIXTURE_TEST_SUITE(validationinterface_tests, TestingSetup)
1414

15+
struct TestSubscriberNoop final : public CValidationInterface {
16+
void BlockChecked(const CBlock&, const BlockValidationState&) override {}
17+
};
18+
19+
BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
20+
{
21+
std::atomic<bool> generate{true};
22+
23+
// Start thread to generate notifications
24+
std::thread gen{[&] {
25+
const CBlock block_dummy;
26+
const BlockValidationState state_dummy;
27+
while (generate) {
28+
GetMainSignals().BlockChecked(block_dummy, state_dummy);
29+
}
30+
}};
31+
32+
// Start thread to consume notifications
33+
std::thread sub{[&] {
34+
// keep going for about 1 sec, which is 250k iterations
35+
for (int i = 0; i < 250000; i++) {
36+
auto sub = std::make_shared<TestSubscriberNoop>();
37+
RegisterSharedValidationInterface(sub);
38+
UnregisterSharedValidationInterface(sub);
39+
}
40+
// tell the other thread we are done
41+
generate = false;
42+
}};
43+
44+
gen.join();
45+
sub.join();
46+
BOOST_CHECK(!generate);
47+
}
48+
1549
class TestInterface : public CValidationInterface
1650
{
1751
public:

src/validationinterface.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,26 @@ struct MainSignalsInstance {
8989

9090
static CMainSignals g_signals;
9191

92-
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
92+
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler)
93+
{
9394
assert(!m_internals);
9495
m_internals.reset(new MainSignalsInstance(&scheduler));
9596
}
9697

97-
void CMainSignals::UnregisterBackgroundSignalScheduler() {
98+
void CMainSignals::UnregisterBackgroundSignalScheduler()
99+
{
98100
m_internals.reset(nullptr);
99101
}
100102

101-
void CMainSignals::FlushBackgroundCallbacks() {
103+
void CMainSignals::FlushBackgroundCallbacks()
104+
{
102105
if (m_internals) {
103106
m_internals->m_schedulerClient.EmptyQueue();
104107
}
105108
}
106109

107-
size_t CMainSignals::CallbacksPending() {
110+
size_t CMainSignals::CallbacksPending()
111+
{
108112
if (!m_internals) return 0;
109113
return m_internals->m_schedulerClient.CallbacksPending();
110114
}
@@ -114,10 +118,11 @@ CMainSignals& GetMainSignals()
114118
return g_signals;
115119
}
116120

117-
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> pwalletIn) {
118-
// Each connection captures pwalletIn to ensure that each callback is
119-
// executed before pwalletIn is destroyed. For more details see #18338.
120-
g_signals.m_internals->Register(std::move(pwalletIn));
121+
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
122+
{
123+
// Each connection captures the shared_ptr to ensure that each callback is
124+
// executed before the subscriber is destroyed. For more details see #18338.
125+
g_signals.m_internals->Register(std::move(callbacks));
121126
}
122127

123128
void RegisterValidationInterface(CValidationInterface* callbacks)
@@ -132,24 +137,28 @@ void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> c
132137
UnregisterValidationInterface(callbacks.get());
133138
}
134139

135-
void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
140+
void UnregisterValidationInterface(CValidationInterface* callbacks)
141+
{
136142
if (g_signals.m_internals) {
137-
g_signals.m_internals->Unregister(pwalletIn);
143+
g_signals.m_internals->Unregister(callbacks);
138144
}
139145
}
140146

141-
void UnregisterAllValidationInterfaces() {
147+
void UnregisterAllValidationInterfaces()
148+
{
142149
if (!g_signals.m_internals) {
143150
return;
144151
}
145152
g_signals.m_internals->Clear();
146153
}
147154

148-
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
155+
void CallFunctionInValidationInterfaceQueue(std::function<void()> func)
156+
{
149157
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
150158
}
151159

152-
void SyncWithValidationInterfaceQueue() {
160+
void SyncWithValidationInterfaceQueue()
161+
{
153162
AssertLockNotHeld(cs_main);
154163
// Block until the validation queue drains
155164
std::promise<void> promise;

src/validationinterface.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ class CValidationInterface;
2222
class uint256;
2323
class CScheduler;
2424

25-
// These functions dispatch to one or all registered wallets
26-
27-
/** Register a wallet to receive updates from core */
28-
void RegisterValidationInterface(CValidationInterface* pwalletIn);
29-
/** Unregister a wallet from core */
30-
void UnregisterValidationInterface(CValidationInterface* pwalletIn);
31-
/** Unregister all wallets from core */
25+
/** Register subscriber */
26+
void RegisterValidationInterface(CValidationInterface* callbacks);
27+
/** Unregister subscriber. DEPRECATED. This is not safe to use when the RPC server or main message handler thread is running. */
28+
void UnregisterValidationInterface(CValidationInterface* callbacks);
29+
/** Unregister all subscribers */
3230
void UnregisterAllValidationInterfaces();
3331

3432
// Alternate registration functions that release a shared_ptr after the last
3533
// notification is sent. These are useful for race-free cleanup, since
3634
// unregistration is nonblocking and can return before the last notification is
3735
// processed.
36+
/** Register subscriber */
3837
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
38+
/** Unregister subscriber */
3939
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
4040

4141
/**

0 commit comments

Comments
 (0)