Merge bitcoin/bitcoin#23707: fuzz: Fix RPC internal bug detection

fa77f95c2f fuzz: Fix RPC internal bug detection (MarcoFalke)

Pull request description:

  Previously the fuzz test considered any exception which contains the string `Internal bug detected` (magic string) as a bug. This is not true when the user (fuzzer) passes in the magic string from outside.

  Fix that by:

  1. Changing the format the string in `NonFatalCheckError` to start with the magic string.
  2. Only treat exceptions that start with the magic string as internal bugs.

  This should fix the bug because any other exception shouldn't start with the magic string.

  To test:

  ```
  echo 'bG9nZ2luZ1y+bUludGVybmFsIGJ1ZyBkZXRlY3RlZAAXCqNcjqNcjuYjeg==' | base64 --decode > /tmp/a
  FUZZ=rpc ./src/test/fuzz/fuzz /tmp/a
  ```

  Before:
  ```
  fuzz: test/fuzz/rpc.cpp:365: void rpc_fuzz_target(FuzzBufferType): Assertion `error_msg.find("trigger_internal_bug") != std::string::npos' failed.
  ```

  After:
  ```
  Executed /tmp/a in 0 ms

ACKs for top commit:
  shaavan:
    crACK fa77f95c2f

Tree-SHA512: 079bc97b6ce0cbad8603c7b577cc1ac0fd19e884ccbaba317588b91d98b36afeaa8cb398344b52bf12c9fd1737b3fdd8452b4e833a3b06cb3c789651955f78b8
This commit is contained in:
MarcoFalke 2021-12-08 16:50:21 +01:00
commit 926fc2a0d4
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 6 additions and 4 deletions

View file

@ -360,7 +360,9 @@ FUZZ_TARGET_INIT(rpc, initialize_rpc)
rpc_testing_setup->CallRPC(rpc_command, arguments);
} catch (const UniValue& json_rpc_error) {
const std::string error_msg{find_value(json_rpc_error, "message").get_str()};
if (error_msg.find("Internal bug detected") != std::string::npos) {
// Once c++20 is allowed, starts_with can be used.
// if (error_msg.starts_with("Internal bug detected")) {
if (0 == error_msg.rfind("Internal bug detected", 0)) {
// Only allow the intentional internal bug
assert(error_msg.find("trigger_internal_bug") != std::string::npos);
}

View file

@ -33,11 +33,11 @@ class NonFatalCheckError : public std::runtime_error
do { \
if (!(condition)) { \
throw NonFatalCheckError( \
strprintf("%s:%d (%s)\n" \
"Internal bug detected: '%s'\n" \
strprintf("Internal bug detected: '%s'\n" \
"%s:%d (%s)\n" \
"You may report this issue here: %s\n", \
__FILE__, __LINE__, __func__, \
(#condition), \
__FILE__, __LINE__, __func__, \
PACKAGE_BUGREPORT)); \
} \
} while (false)