From d3e681bc06758fe0686cd96fcfd4a1c4c5af62b4 Mon Sep 17 00:00:00 2001 From: marcofleon Date: Wed, 28 Jan 2026 16:50:34 +0000 Subject: [PATCH] fuzz: Use `__AFL_SHM_ID` for naming test directories Use the AFL++ shared memory ID environment variable to create a deterministic datadir path. This prevents accumulation of stale directories after a fuzz iteration crashes or times out. During long fuzz campaigns, this accumulation has occasionally resulted in running out of disk space. --- src/test/util/setup_common.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 4e8866399e9..bbc1c9e3769 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -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