Skip to content

Commit e895fdc

Browse files
author
MarcoFalke
committed
Merge #11634: wallet: Add missing cs_wallet/cs_KeyStore locks to wallet
69e7ee2 Add GUARDED_BY(cs_wallet) for setExternalKeyPool, mapKeyMetadata, m_script_metadata and setLockedCoins (practicalswift) 37b2538 Add GUARDED_BY(cs_wallet) for encrypted_batch, nWalletMaxVersion, m_max_keypool_index and nOrderPosNext (practicalswift) dee4292 wallet: Add Clang thread safety analysis annotations (practicalswift) 1c7e25d wallet: Add missing locks (practicalswift) Pull request description: Add missing wallet locks: * Calling the function `GetConflicts(...)` requires holding the mutex `cs_wallet` * Calling the function `IsSpent(...)` requires holding the mutex `cs_wallet` * Accessing the variables `mapKeys` and `mapCryptedKeys` requires holding the mutex `cs_KeyStore` * Accessing the variable `nTimeFirstKey` requires holding the mutex `cs_wallet` * Accessing the variable `mapWallet` requires holding the mutex `cs_wallet` * Accessing the variable `nTimeFirstKey` requires holding the mutex `cs_wallet` Tree-SHA512: 8a7b9a4e1f2147e77c04b817617a06304a2e2159148d3eb3514a3c09c41d77ef7e773df6e63880ad9acc026e00690f72d0c51f3f86279177f672d477423accca
2 parents 5473e25 + 69e7ee2 commit e895fdc

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

src/interfaces/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static WalletTxStatus MakeWalletTxStatus(const CWalletTx& wtx) EXCLUSIVE_LOCKS_R
103103
}
104104

105105
//! Construct wallet TxOut struct.
106-
static WalletTxOut MakeWalletTxOut(CWallet& wallet, const CWalletTx& wtx, int n, int depth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
106+
static WalletTxOut MakeWalletTxOut(CWallet& wallet, const CWalletTx& wtx, int n, int depth) EXCLUSIVE_LOCKS_REQUIRED(cs_main, wallet.cs_wallet)
107107
{
108108
WalletTxOut result;
109109
result.txout = wtx.tx->vout[n];

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ struct tallyitem
11211121
}
11221122
};
11231123

1124-
static UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1124+
static UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwallet->cs_wallet)
11251125
{
11261126
// Minimum confirmations
11271127
int nMinDepth = 1;

src/wallet/test/psbt_wallet_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ BOOST_FIXTURE_TEST_SUITE(psbt_wallet_tests, WalletTestingSetup)
1717

1818
BOOST_AUTO_TEST_CASE(psbt_updater_test)
1919
{
20+
LOCK(m_wallet.cs_wallet);
21+
2022
// Create prevtxs and add to wallet
2123
CDataStream s_prev_tx1(ParseHex("0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"), SER_NETWORK, PROTOCOL_VERSION);
2224
CTransactionRef prev_tx1;

src/wallet/wallet.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(const std::string& name,
40934093
// Try to top up keypool. No-op if the wallet is locked.
40944094
walletInstance->TopUpKeyPool();
40954095

4096-
LOCK(cs_main);
4096+
LOCK2(cs_main, walletInstance->cs_wallet);
40974097

40984098
CBlockIndex *pindexRescan = chainActive.Genesis();
40994099
if (!gArgs.GetBoolArg("-rescan", false))
@@ -4178,7 +4178,6 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(const std::string& name,
41784178
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
41794179

41804180
{
4181-
LOCK(walletInstance->cs_wallet);
41824181
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
41834182
walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
41844183
walletInstance->WalletLogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size());

src/wallet/wallet.h

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,11 @@ class CWalletTx : public CMerkleTx
460460
CAmount GetDebit(const isminefilter& filter) const;
461461
CAmount GetCredit(const isminefilter& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
462462
CAmount GetImmatureCredit(bool fUseCache=true) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
463-
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
463+
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
464+
// annotation "EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwallet->cs_wallet)". The
465+
// annotation "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid
466+
// having to resolve the issue of member access into incomplete type CWallet.
467+
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const NO_THREAD_SAFETY_ANALYSIS;
464468
CAmount GetImmatureWatchOnlyCredit(const bool fUseCache=true) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
465469
CAmount GetChange() const;
466470

@@ -492,7 +496,13 @@ class CWalletTx : public CMerkleTx
492496
/** Pass this transaction to the mempool. Fails if absolute fee exceeds absurd fee. */
493497
bool AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
494498

495-
std::set<uint256> GetConflicts() const;
499+
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
500+
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The annotation
501+
// "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid having to
502+
// resolve the issue of member access into incomplete type CWallet. Note
503+
// that we still have the runtime check "AssertLockHeld(pwallet->cs_wallet)"
504+
// in place.
505+
std::set<uint256> GetConflicts() const NO_THREAD_SAFETY_ANALYSIS;
496506
};
497507

498508
class COutput
@@ -591,13 +601,13 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
591601
std::mutex mutexScanning;
592602
friend class WalletRescanReserver;
593603

594-
WalletBatch *encrypted_batch = nullptr;
604+
WalletBatch *encrypted_batch GUARDED_BY(cs_wallet) = nullptr;
595605

596606
//! the current wallet version: clients below this version are not able to load the wallet
597607
int nWalletVersion = FEATURE_BASE;
598608

599609
//! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
600-
int nWalletMaxVersion = FEATURE_BASE;
610+
int nWalletMaxVersion GUARDED_BY(cs_wallet) = FEATURE_BASE;
601611

602612
int64_t nNextResend = 0;
603613
int64_t nLastResend = 0;
@@ -609,9 +619,9 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
609619
* mutated transactions where the mutant gets mined).
610620
*/
611621
typedef std::multimap<COutPoint, uint256> TxSpends;
612-
TxSpends mapTxSpends;
613-
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid);
614-
void AddToSpends(const uint256& wtxid);
622+
TxSpends mapTxSpends GUARDED_BY(cs_wallet);
623+
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
624+
void AddToSpends(const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
615625

616626
/**
617627
* Add a transaction to the wallet, or update it. pIndex and posInBlock should
@@ -632,9 +642,9 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
632642
void MarkConflicted(const uint256& hashBlock, const uint256& hashTx);
633643

634644
/* Mark a transaction's inputs dirty, thus forcing the outputs to be recomputed */
635-
void MarkInputsDirty(const CTransactionRef& tx);
645+
void MarkInputsDirty(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
636646

637-
void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
647+
void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
638648

639649
/* Used by TransactionAddedToMemorypool/BlockConnected/Disconnected/ScanForWalletTransactions.
640650
* Should be called with pindexBlock and posInBlock if this is for a transaction that is included in a block. */
@@ -647,13 +657,13 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
647657
void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal = false) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
648658

649659
std::set<int64_t> setInternalKeyPool;
650-
std::set<int64_t> setExternalKeyPool;
660+
std::set<int64_t> setExternalKeyPool GUARDED_BY(cs_wallet);
651661
std::set<int64_t> set_pre_split_keypool;
652-
int64_t m_max_keypool_index = 0;
662+
int64_t m_max_keypool_index GUARDED_BY(cs_wallet) = 0;
653663
std::map<CKeyID, int64_t> m_pool_key_to_index;
654664
std::atomic<uint64_t> m_wallet_flags{0};
655665

656-
int64_t nTimeFirstKey = 0;
666+
int64_t nTimeFirstKey GUARDED_BY(cs_wallet) = 0;
657667

658668
/**
659669
* Private version of AddWatchOnly method which does not accept a
@@ -709,20 +719,20 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
709719
* if they are not ours
710720
*/
711721
bool SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet,
712-
const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params, bool& bnb_used) const;
722+
const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params, bool& bnb_used) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
713723

714724
/** Get a name for this wallet for logging/debugging purposes.
715725
*/
716726
const std::string& GetName() const { return m_name; }
717727

718728
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
719-
void MarkPreSplitKeys();
729+
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
720730

721731
// Map from Key ID to key metadata.
722-
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
732+
std::map<CKeyID, CKeyMetadata> mapKeyMetadata GUARDED_BY(cs_wallet);
723733

724734
// Map from Script ID to key metadata (for watch-only keys).
725-
std::map<CScriptID, CKeyMetadata> m_script_metadata;
735+
std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY(cs_wallet);
726736

727737
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
728738
MasterKeyMap mapMasterKeys;
@@ -739,17 +749,17 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
739749
encrypted_batch = nullptr;
740750
}
741751

742-
std::map<uint256, CWalletTx> mapWallet;
752+
std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet);
743753

744754
typedef std::multimap<int64_t, CWalletTx*> TxItems;
745755
TxItems wtxOrdered;
746756

747-
int64_t nOrderPosNext = 0;
757+
int64_t nOrderPosNext GUARDED_BY(cs_wallet) = 0;
748758
uint64_t nAccountingEntryNumber = 0;
749759

750760
std::map<CTxDestination, CAddressBookData> mapAddressBook;
751761

752-
std::set<COutPoint> setLockedCoins;
762+
std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
753763

754764
const CWalletTx* GetWalletTx(const uint256& hash) const;
755765

@@ -769,7 +779,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
769779
/**
770780
* Find non-change parent output.
771781
*/
772-
const CTxOut& FindNonChangeParentOutput(const CTransaction& tx, int output) const;
782+
const CTxOut& FindNonChangeParentOutput(const CTransaction& tx, int output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
773783

774784
/**
775785
* Shuffle and select coins until nTargetValue is reached while avoiding
@@ -780,7 +790,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
780790
bool SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<OutputGroup> groups,
781791
std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CoinSelectionParams& coin_selection_params, bool& bnb_used) const;
782792

783-
bool IsSpent(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
793+
bool IsSpent(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_main, cs_wallet);
784794
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin) const;
785795

786796
bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
@@ -856,7 +866,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
856866

857867
void MarkDirty();
858868
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose=true);
859-
void LoadToWallet(const CWalletTx& wtxIn);
869+
void LoadToWallet(const CWalletTx& wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
860870
void TransactionAddedToMempool(const CTransactionRef& tx) override;
861871
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) override;
862872
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
@@ -1000,7 +1010,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
10001010
int GetVersion() { LOCK(cs_wallet); return nWalletVersion; }
10011011

10021012
//! Get wallet transactions that conflict with given transaction (spend same outputs)
1003-
std::set<uint256> GetConflicts(const uint256& txid) const;
1013+
std::set<uint256> GetConflicts(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10041014

10051015
//! Check if a given transaction has any of its outputs spent by another transaction in the wallet
10061016
bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
@@ -1198,6 +1208,6 @@ class WalletRescanReserver
11981208
// Use DummySignatureCreator, which inserts 71 byte signatures everywhere.
11991209
// NOTE: this requires that all inputs must be in mapWallet (eg the tx should
12001210
// be IsAllFromMe).
1201-
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig = false);
1211+
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, bool use_max_sig = false) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
12021212
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig = false);
12031213
#endif // BITCOIN_WALLET_WALLET_H

0 commit comments

Comments
 (0)