util/check: Add CHECK_NONFATAL identity function, NONFATAL_UNREACHABLE AND UNREACHABLE macros

This commit is contained in:
Aurèle Oulès 2022-04-09 02:14:02 +02:00
parent e0680bbce8
commit ee02c8bd9a
7 changed files with 49 additions and 39 deletions

View File

@ -37,6 +37,7 @@
#include <txmempool.h> #include <txmempool.h>
#include <undo.h> #include <undo.h>
#include <univalue.h> #include <univalue.h>
#include <util/check.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/translation.h> #include <util/translation.h>
#include <validation.h> #include <validation.h>
@ -786,8 +787,7 @@ static RPCHelpMan pruneblockchain()
} }
PruneBlockFilesManual(active_chainstate, height); PruneBlockFilesManual(active_chainstate, height);
const CBlockIndex* block = active_chain.Tip(); const CBlockIndex* block = CHECK_NONFATAL(active_chain.Tip());
CHECK_NONFATAL(block);
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
block = block->pprev; block = block->pprev;
} }
@ -1201,8 +1201,7 @@ RPCHelpMan getblockchaininfo()
LOCK(cs_main); LOCK(cs_main);
CChainState& active_chainstate = chainman.ActiveChainstate(); CChainState& active_chainstate = chainman.ActiveChainstate();
const CBlockIndex* tip = active_chainstate.m_chain.Tip(); const CBlockIndex* tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
CHECK_NONFATAL(tip);
const int height = tip->nHeight; const int height = tip->nHeight;
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("chain", Params().NetworkIDString()); obj.pushKV("chain", Params().NetworkIDString());
@ -1218,8 +1217,7 @@ RPCHelpMan getblockchaininfo()
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage()); obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
obj.pushKV("pruned", node::fPruneMode); obj.pushKV("pruned", node::fPruneMode);
if (node::fPruneMode) { if (node::fPruneMode) {
const CBlockIndex* block = tip; const CBlockIndex* block = CHECK_NONFATAL(tip);
CHECK_NONFATAL(block);
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
block = block->pprev; block = block->pprev;
} }
@ -1310,8 +1308,7 @@ static RPCHelpMan getdeploymentinfo()
const CBlockIndex* blockindex; const CBlockIndex* blockindex;
if (request.params[0].isNull()) { if (request.params[0].isNull()) {
blockindex = active_chainstate.m_chain.Tip(); blockindex = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
CHECK_NONFATAL(blockindex);
} else { } else {
const uint256 hash(ParseHashV(request.params[0], "blockhash")); const uint256 hash(ParseHashV(request.params[0], "blockhash"));
blockindex = chainman.m_blockman.LookupBlockIndex(hash); blockindex = chainman.m_blockman.LookupBlockIndex(hash);
@ -2132,10 +2129,8 @@ static RPCHelpMan scantxoutset()
LOCK(cs_main); LOCK(cs_main);
CChainState& active_chainstate = chainman.ActiveChainstate(); CChainState& active_chainstate = chainman.ActiveChainstate();
active_chainstate.ForceFlushStateToDisk(); active_chainstate.ForceFlushStateToDisk();
pcursor = active_chainstate.CoinsDB().Cursor(); pcursor = CHECK_NONFATAL(active_chainstate.CoinsDB().Cursor());
CHECK_NONFATAL(pcursor); tip = CHECK_NONFATAL(active_chainstate.m_chain.Tip());
tip = active_chainstate.m_chain.Tip();
CHECK_NONFATAL(tip);
} }
bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins, node.rpc_interruption_point); bool res = FindScriptPubKey(g_scan_progress, g_should_abort_scan, count, pcursor.get(), needles, coins, node.rpc_interruption_point);
result.pushKV("success", res); result.pushKV("success", res);
@ -2337,8 +2332,7 @@ UniValue CreateUTXOSnapshot(
} }
pcursor = chainstate.CoinsDB().Cursor(); pcursor = chainstate.CoinsDB().Cursor();
tip = chainstate.m_blockman.LookupBlockIndex(stats.hashBlock); tip = CHECK_NONFATAL(chainstate.m_blockman.LookupBlockIndex(stats.hashBlock));
CHECK_NONFATAL(tip);
} }
LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)", LOG_TIME_SECONDS(strprintf("writing UTXO snapshot at height %s (%s) to file %s (via %s)",

View File

@ -484,9 +484,8 @@ static RPCHelpMan mockscheduler()
throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)"); throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)");
} }
auto node_context = util::AnyPtr<NodeContext>(request.context); auto node_context = CHECK_NONFATAL(util::AnyPtr<NodeContext>(request.context));
// protect against null pointer dereference // protect against null pointer dereference
CHECK_NONFATAL(node_context);
CHECK_NONFATAL(node_context->scheduler); CHECK_NONFATAL(node_context->scheduler);
node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds)); node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds));

View File

@ -33,6 +33,7 @@
#include <script/standard.h> #include <script/standard.h>
#include <uint256.h> #include <uint256.h>
#include <util/bip32.h> #include <util/bip32.h>
#include <util/check.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/string.h> #include <util/string.h>
#include <util/vector.h> #include <util/vector.h>
@ -466,7 +467,7 @@ static RPCHelpMan decodescript()
// Should not be wrapped // Should not be wrapped
return false; return false;
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
}()}; }()};
if (can_wrap_P2WSH) { if (can_wrap_P2WSH) {
UniValue sr(UniValue::VOBJ); UniValue sr(UniValue::VOBJ);

View File

@ -9,6 +9,7 @@
#include <script/descriptor.h> #include <script/descriptor.h>
#include <script/signingprovider.h> #include <script/signingprovider.h>
#include <tinyformat.h> #include <tinyformat.h>
#include <util/check.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <util/string.h> #include <util/string.h>
#include <util/translation.h> #include <util/translation.h>
@ -542,7 +543,7 @@ RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RP
// Null values are accepted in all arguments // Null values are accepted in all arguments
break; break;
default: default:
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
break; break;
} }
} }
@ -793,7 +794,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
return; return;
} }
case Type::ANY: { case Type::ANY: {
CHECK_NONFATAL(false); // Only for testing NONFATAL_UNREACHABLE(); // Only for testing
} }
case Type::NONE: { case Type::NONE: {
sections.PushSection({indent + "null" + maybe_separator, Description("json null")}); sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
@ -860,7 +861,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
return; return;
} }
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} }
bool RPCResult::MatchesType(const UniValue& result) const bool RPCResult::MatchesType(const UniValue& result) const
@ -938,7 +939,7 @@ bool RPCResult::MatchesType(const UniValue& result) const
return true; return true;
} }
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} }
void RPCResult::CheckInnerDoc() const void RPCResult::CheckInnerDoc() const
@ -984,9 +985,9 @@ std::string RPCArg::ToStringObj(const bool oneline) const
case Type::OBJ: case Type::OBJ:
case Type::OBJ_USER_KEYS: case Type::OBJ_USER_KEYS:
// Currently unused, so avoid writing dead code // Currently unused, so avoid writing dead code
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} }
std::string RPCArg::ToString(const bool oneline) const std::string RPCArg::ToString(const bool oneline) const
@ -1021,7 +1022,7 @@ std::string RPCArg::ToString(const bool oneline) const
return "[" + res + "...]"; return "[" + res + "...]";
} }
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} }
static std::pair<int64_t, int64_t> ParseRange(const UniValue& value) static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)

View File

@ -18,8 +18,24 @@ class NonFatalCheckError : public std::runtime_error
using std::runtime_error::runtime_error; using std::runtime_error::runtime_error;
}; };
#define format_internal_error(msg, file, line, func, report) \
strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", \
msg, file, line, func, report)
/** Helper for CHECK_NONFATAL() */
template <typename T>
T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion)
{
if (!(val)) {
throw NonFatalCheckError(
format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT));
}
return std::forward<T>(val);
}
/** /**
* Throw a NonFatalCheckError when the condition evaluates to false * Identity function. Throw a NonFatalCheckError when the condition evaluates to false
* *
* This should only be used * This should only be used
* - where the condition is assumed to be true, not for error handling or validating user input * - where the condition is assumed to be true, not for error handling or validating user input
@ -29,18 +45,8 @@ class NonFatalCheckError : public std::runtime_error
* asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC * asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
* caller, which can then report the issue to the developers. * caller, which can then report the issue to the developers.
*/ */
#define CHECK_NONFATAL(condition) \ #define CHECK_NONFATAL(condition) \
do { \ inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
if (!(condition)) { \
throw NonFatalCheckError( \
strprintf("Internal bug detected: '%s'\n" \
"%s:%d (%s)\n" \
"You may report this issue here: %s\n", \
(#condition), \
__FILE__, __LINE__, __func__, \
PACKAGE_BUGREPORT)); \
} \
} while (false)
#if defined(NDEBUG) #if defined(NDEBUG)
#error "Cannot compile without assertions!" #error "Cannot compile without assertions!"
@ -80,4 +86,13 @@ T&& inline_assertion_check(T&& val, [[maybe_unused]] const char* file, [[maybe_u
*/ */
#define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val) #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
/**
* NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
* This is used to mark code that is not yet implemented or is not yet reachable.
*/
#define NONFATAL_UNREACHABLE() \
throw NonFatalCheckError( \
format_internal_error("Unreachable code reached (non-fatal)", \
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT))
#endif // BITCOIN_UTIL_CHECK_H #endif // BITCOIN_UTIL_CHECK_H

View File

@ -915,7 +915,7 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
case TxoutType::WITNESS_V1_TAPROOT: case TxoutType::WITNESS_V1_TAPROOT:
return "unrecognized script"; return "unrecognized script";
} // no default case, so the compiler can warn about missing cases } // no default case, so the compiler can warn about missing cases
CHECK_NONFATAL(false); NONFATAL_UNREACHABLE();
} }
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys) static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)

View File

@ -27,7 +27,7 @@ class RpcMiscTest(BitcoinTestFramework):
self.log.info("test CHECK_NONFATAL") self.log.info("test CHECK_NONFATAL")
assert_raises_rpc_error( assert_raises_rpc_error(
-1, -1,
'Internal bug detected: \'request.params[9].get_str() != "trigger_internal_bug"\'', 'Internal bug detected: "request.params[9].get_str() != "trigger_internal_bug""',
lambda: node.echo(arg9='trigger_internal_bug'), lambda: node.echo(arg9='trigger_internal_bug'),
) )