Skip to content

Commit 6c7cfc8

Browse files
author
MarcoFalke
committed
Merge #13126: util: Add Clang thread safety annotations for variables guarded by cs_args
1e29379 Fix potential deadlock (practicalswift) d58dc9f Add lock annotations (cs_args) (practicalswift) db5e9d3 Add missing locks (cs_args) (practicalswift) Pull request description: * Add missing `cs_args` locks * Add Clang thread safety annotations for variables guarded by `cs_args` Tree-SHA512: bc562dbddf24a287bcf9bf902bc930f942f260a94e5c8ec4d190f7f2ddac448ed3d52acadaf9fc1c81a5cbff2c171c52c18ba0804eeb03f699d70394e1c977c5
2 parents be301a5 + 1e29379 commit 6c7cfc8

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/util.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class ArgsManagerHelper {
216216

217217
/** Determine whether to use config settings in the default section,
218218
* See also comments around ArgsManager::ArgsManager() below. */
219-
static inline bool UseDefaultSection(const ArgsManager& am, const std::string& arg)
219+
static inline bool UseDefaultSection(const ArgsManager& am, const std::string& arg) EXCLUSIVE_LOCKS_REQUIRED(am.cs_args)
220220
{
221221
return (am.m_network == CBaseChainParams::MAIN || am.m_network_only_args.count(arg) == 0);
222222
}
@@ -295,7 +295,7 @@ class ArgsManagerHelper {
295295
/* Special test for -testnet and -regtest args, because we
296296
* don't want to be confused by craziness like "[regtest] testnet=1"
297297
*/
298-
static inline bool GetNetBoolArg(const ArgsManager &am, const std::string& net_arg)
298+
static inline bool GetNetBoolArg(const ArgsManager &am, const std::string& net_arg) EXCLUSIVE_LOCKS_REQUIRED(am.cs_args)
299299
{
300300
std::pair<bool,std::string> found_result(false,std::string());
301301
found_result = GetArgHelper(am.m_override_args, net_arg, true);
@@ -372,6 +372,8 @@ ArgsManager::ArgsManager() :
372372

373373
void ArgsManager::WarnForSectionOnlyArgs()
374374
{
375+
LOCK(cs_args);
376+
375377
// if there's no section selected, don't worry
376378
if (m_network.empty()) return;
377379

@@ -400,6 +402,7 @@ void ArgsManager::WarnForSectionOnlyArgs()
400402

401403
void ArgsManager::SelectConfigNetwork(const std::string& network)
402404
{
405+
LOCK(cs_args);
403406
m_network = network;
404407
}
405408

@@ -468,6 +471,7 @@ bool ArgsManager::IsArgKnown(const std::string& key) const
468471
arg_no_net = std::string("-") + key.substr(option_index + 1, std::string::npos);
469472
}
470473

474+
LOCK(cs_args);
471475
for (const auto& arg_map : m_available_args) {
472476
if (arg_map.second.count(arg_no_net)) return true;
473477
}
@@ -571,6 +575,7 @@ void ArgsManager::AddArg(const std::string& name, const std::string& help, const
571575
eq_index = name.size();
572576
}
573577

578+
LOCK(cs_args);
574579
std::map<std::string, Arg>& arg_map = m_available_args[cat];
575580
auto ret = arg_map.emplace(name.substr(0, eq_index), Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only));
576581
assert(ret.second); // Make sure an insertion actually happened
@@ -588,6 +593,7 @@ std::string ArgsManager::GetHelpMessage() const
588593
const bool show_debug = gArgs.GetBoolArg("-help-debug", false);
589594

590595
std::string usage = "";
596+
LOCK(cs_args);
591597
for (const auto& arg_map : m_available_args) {
592598
switch(arg_map.first) {
593599
case OptionsCategory::OPTIONS:
@@ -880,7 +886,12 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
880886
}
881887
// if there is an -includeconf in the override args, but it is empty, that means the user
882888
// passed '-noincludeconf' on the command line, in which case we should not include anything
883-
if (m_override_args.count("-includeconf") == 0) {
889+
bool emptyIncludeConf;
890+
{
891+
LOCK(cs_args);
892+
emptyIncludeConf = m_override_args.count("-includeconf") == 0;
893+
}
894+
if (emptyIncludeConf) {
884895
std::string chain_id = GetChainName();
885896
std::vector<std::string> includeconf(GetArgs("-includeconf"));
886897
{
@@ -940,6 +951,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
940951

941952
std::string ArgsManager::GetChainName() const
942953
{
954+
LOCK(cs_args);
943955
bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest");
944956
bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet");
945957

src/util.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ class ArgsManager
142142
};
143143

144144
mutable CCriticalSection cs_args;
145-
std::map<std::string, std::vector<std::string>> m_override_args;
146-
std::map<std::string, std::vector<std::string>> m_config_args;
147-
std::string m_network;
148-
std::set<std::string> m_network_only_args;
149-
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args;
145+
std::map<std::string, std::vector<std::string>> m_override_args GUARDED_BY(cs_args);
146+
std::map<std::string, std::vector<std::string>> m_config_args GUARDED_BY(cs_args);
147+
std::string m_network GUARDED_BY(cs_args);
148+
std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
149+
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
150150

151151
bool ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys = false);
152152

@@ -262,7 +262,10 @@ class ArgsManager
262262
/**
263263
* Clear available arguments
264264
*/
265-
void ClearArgs() { m_available_args.clear(); }
265+
void ClearArgs() {
266+
LOCK(cs_args);
267+
m_available_args.clear();
268+
}
266269

267270
/**
268271
* Get the help string

0 commit comments

Comments
 (0)