Skip to content

Commit 547c648

Browse files
committed
Merge bitcoin/bitcoin#32987: init: [gui] Avoid UB/crash in InitAndLoadChainstate
fac90e5 test: Check that the GUI interactive reindex works (MarcoFalke) faaadda init: [gui] Avoid UB/crash in InitAndLoadChainstate (MarcoFalke) Pull request description: `InitAndLoadChainstate` is problematic, when called twice in the GUI. This can happen when it returns a failure and the user selects an interactive reindex. There are several bugs that have been introduced since the last time this was working correctly: * The first one is a crash (assertion failure), which happens due to a cached tip block in the notifiications from the previous run. See bitcoin/bitcoin#31346 (comment) * The second one is UB (use-after-free), which happens because the block index db in the blockmanager is not reset. See bitcoin/bitcoin#30965 (comment) Fix both bugs by resetting any dirty state in `InitAndLoadChainstate`. Also, add a test, because I don't really want to keep testing this manually every time. (A failing test run can be seen in https://github.com/bitcoin/bitcoin/pull/32979/checks) ACKs for top commit: achow101: ACK fac90e5 TheCharlatan: ACK fac90e5 mzumsande: Tested ACK fac90e5 Tree-SHA512: 9f744d36e7cdd3f5871764386ec5a5cca1ae144f1bacc26c07e60313c2bdacdc5fca351aa185cb51359540eea4534dda17e4fb6073ad90f91ba0a6936faeead8
2 parents e6bfd95 + fac90e5 commit 547c648

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/common/args.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message
709709

710710
const std::vector<std::string> TEST_OPTIONS_DOC{
711711
"addrman (use deterministic addrman)",
712+
"reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
712713
"bip94 (enforce BIP94 consensus rules)",
713714
};
714715

src/init.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,13 +1223,24 @@ static ChainstateLoadResult InitAndLoadChainstate(
12231223
const kernel::CacheSizes& cache_sizes,
12241224
const ArgsManager& args)
12251225
{
1226+
// This function may be called twice, so any dirty state must be reset.
1227+
node.notifications.reset(); // Drop state, such as a cached tip block
1228+
node.mempool.reset();
1229+
node.chainman.reset(); // Drop state, such as an initialized m_block_tree_db
1230+
12261231
const CChainParams& chainparams = Params();
1232+
1233+
Assert(!node.notifications); // Was reset above
1234+
node.notifications = std::make_unique<KernelNotifications>(Assert(node.shutdown_request), node.exit_status, *Assert(node.warnings));
1235+
ReadNotificationArgs(args, *node.notifications);
1236+
12271237
CTxMemPool::Options mempool_opts{
12281238
.check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
12291239
.signals = node.validation_signals.get(),
12301240
};
12311241
Assert(ApplyArgsManOptions(args, chainparams, mempool_opts)); // no error can happen, already checked in AppInitParameterInteraction
12321242
bilingual_str mempool_error;
1243+
Assert(!node.mempool); // Was reset above
12331244
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
12341245
if (!mempool_error.empty()) {
12351246
return {ChainstateLoadStatus::FAILURE_FATAL, mempool_error};
@@ -1260,6 +1271,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
12601271
// Creating the chainstate manager internally creates a BlockManager, opens
12611272
// the blocks tree db, and wipes existing block files in case of a reindex.
12621273
// The coinsdb is opened at a later point on LoadChainstate.
1274+
Assert(!node.chainman); // Was reset above
12631275
try {
12641276
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
12651277
} catch (dbwrapper_error& e) {
@@ -1697,10 +1709,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16971709

16981710
// ********************************************************* Step 7: load block chain
16991711

1700-
node.notifications = std::make_unique<KernelNotifications>(Assert(node.shutdown_request), node.exit_status, *Assert(node.warnings));
1701-
auto& kernel_notifications{*node.notifications};
1702-
ReadNotificationArgs(args, kernel_notifications);
1703-
17041712
// cache size calculations
17051713
const auto [index_cache_sizes, kernel_cache_sizes] = CalculateCacheSizes(args, g_enabled_filter_types.size());
17061714

@@ -1730,10 +1738,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17301738
args);
17311739
if (status == ChainstateLoadStatus::FAILURE && !do_reindex && !ShutdownRequested(node)) {
17321740
// suggest a reindex
1733-
bool do_retry = uiInterface.ThreadSafeQuestion(
1741+
bool do_retry{HasTestOption(args, "reindex_after_failure_noninteractive_yes") ||
1742+
uiInterface.ThreadSafeQuestion(
17341743
error + Untranslated(".\n\n") + _("Do you want to rebuild the databases now?"),
17351744
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
1736-
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1745+
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT)};
17371746
if (!do_retry) {
17381747
return false;
17391748
}
@@ -1760,6 +1769,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17601769
}
17611770

17621771
ChainstateManager& chainman = *Assert(node.chainman);
1772+
auto& kernel_notifications{*Assert(node.notifications)};
17631773

17641774
assert(!node.peerman);
17651775
node.peerman = PeerManager::make(*node.connman, *node.addrman,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test reindex works on init after a db load failure"""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import assert_equal
9+
import os
10+
import shutil
11+
12+
13+
class ReindexInitTest(BitcoinTestFramework):
14+
def set_test_params(self):
15+
self.num_nodes = 1
16+
17+
def run_test(self):
18+
node = self.nodes[0]
19+
self.stop_nodes()
20+
21+
self.log.info("Removing the block index leads to init error")
22+
shutil.rmtree(node.blocks_path / "index")
23+
node.assert_start_raises_init_error(
24+
expected_msg=f": Error initializing block database.{os.linesep}"
25+
"Please restart with -reindex or -reindex-chainstate to recover.",
26+
)
27+
28+
self.log.info("Allowing the reindex should work fine")
29+
self.start_node(0, extra_args=["-test=reindex_after_failure_noninteractive_yes"])
30+
assert_equal(node.getblockcount(), 200)
31+
32+
33+
if __name__ == "__main__":
34+
ReindexInitTest(__file__).main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
'p2p_leak.py',
290290
'wallet_encryption.py',
291291
'feature_dersig.py',
292+
'feature_reindex_init.py',
292293
'feature_cltv.py',
293294
'rpc_uptime.py',
294295
'feature_discover.py',

0 commit comments

Comments
 (0)