Merge bitcoin/bitcoin#34445: fuzz: Use __AFL_SHM_ID for naming test directories

d3e681bc06758fe0686cd96fcfd4a1c4c5af62b4 fuzz: Use `__AFL_SHM_ID` for naming test directories (marcofleon)

Pull request description:

  During long multicore fuzzing campaigns with AFL++, stale datadirs can eventually accumulate from time outs, resulting in disk running out of space (see https://github.com/bitcoin/bitcoin/issues/28811). The easiest way to reproduce this is by running our `utxo_total_supply` target using multiple cores with AFL++ and observing the crashes that occur because of all the directories in `/tmp/test_common\ bitcoin/utxo_total_supply/`.

  Fix this by using the AFL++ shared memory ID to name the test dirs and cleaning it up before each setup. This ID is unique per AFL++ instance, so multiple cores can run in parallel without conflicts.

  Fixes https://github.com/bitcoin/bitcoin/issues/28811

ACKs for top commit:
  maflcko:
    lgtm ACK d3e681bc06758fe0686cd96fcfd4a1c4c5af62b4
  dergoegge:
    utACK d3e681bc06758fe0686cd96fcfd4a1c4c5af62b4

Tree-SHA512: 420373e5f8a63c84797303ba2ef6657dfe9dacf9c2f3d818524421c24681a0e984c212ecb706217d93f67c2ec16b146a2d37fddcbd6918b2e5e9f634f5e13c10
This commit is contained in:
merge-script 2026-01-30 09:27:28 +00:00
commit c7cf2b8f3a
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -161,8 +161,17 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
// tests, such as the fuzz tests to run in several processes at the
// same time, add a random element to the path. Keep it small enough to
// avoid a MAX_PATH violation on Windows.
const auto rand{HexStr(g_rng_temp_path.randbytes(10))};
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / rand;
//
// When fuzzing with AFL++, use the shared memory ID for a deterministic
// path. This allows for cleanup of leftover directories from timed-out
// or crashed iterations, preventing accumulation of stale datadirs.
if (const char* shm_id = std::getenv("__AFL_SHM_ID"); shm_id && *shm_id) {
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / shm_id;
fs::remove_all(m_path_root);
} else {
const auto rand{HexStr(g_rng_temp_path.randbytes(10))};
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / test_name / rand;
}
TryCreateDirectories(m_path_root);
} else {
// Custom data directory