Skip to content

Commit 6f2cf18

Browse files
authored
Fix an over-eager determinizer hack breaking overlay fuzzer (#5282)
We have a little hack in Random.cpp that routes to the semi-deterministic global PRNG for drawing random bytes when we're in a fuzzer build. This helps reduce pointless nondeterminism, but we've also in the meantime made the global PRNG main-thread-only (sensibly!) and so this actually hits an assert. The simplest fix -- and I think entirely adequate since this is a fuzzer-only hack anyways -- is to only do the diversion on the main thread, and let non-main threads (of which there are only a small number of callers, mostly those generating random filenames) keep using the normal-build path of accessing true entropy.
2 parents fe3bed5 + 917a32b commit 6f2cf18

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/crypto/Random.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "crypto/Random.h"
66
#include "lib/util/stdrandom.h"
7+
#include "util/GlobalChecks.h"
78
#include "util/Math.h"
89

910
#include <algorithm>
@@ -19,16 +20,27 @@ randomBytes(size_t length)
1920
std::vector<uint8_t> vec(length);
2021

2122
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
22-
stellar::uniform_int_distribution<unsigned short> dist(0, 255);
23-
std::generate(vec.begin(), vec.end(), [&]() {
24-
return static_cast<uint8_t>(dist(stellar::getGlobalRandomEngine()));
25-
});
23+
if (stellar::threadIsMain())
24+
{
25+
// Only do this when threadIsMain because (a) we assert it
26+
// inside the getGlobalRandomEngine() and (b) if we're not
27+
// on main thread then thread scheduling is non-deterministic
28+
// anyways and there's no point in trying to be deterministic.
29+
stellar::uniform_int_distribution<unsigned short> dist(0, 255);
30+
std::generate(vec.begin(), vec.end(), [&]() {
31+
return static_cast<uint8_t>(dist(stellar::getGlobalRandomEngine()));
32+
});
33+
}
34+
else
35+
{
36+
randombytes_buf(vec.data(), length);
37+
}
2638
#else
2739
randombytes_buf(vec.data(), length);
2840
#endif
2941

3042
#ifdef MSAN_ENABLED
31-
__msan_unpoison(out.key.data(), out.key.size());
43+
__msan_unpoison(vec.data(), vec.size());
3244
#endif
3345
return vec;
3446
}

0 commit comments

Comments
 (0)