Skip to content

Commit 25de3f9

Browse files
committed
[GWP-ASan] Fix PRNG to use IE TLS.
Summary: GWP-ASan's PRNG didn't use Initial-Exec TLS. Fix that to ensure that we don't have infinite recursion, and also that we don't allocate a DTV on Android when GWP-ASan is touched. Test coverage ensuring that the sample counter is UINT32_MAX for an uninitialised GWP-ASan is provided by gwp_asan/tests/late_init.cpp. Reviewers: pcc, cferris Reviewed By: pcc Subscribers: #sanitizers, llvm-commits, rprichard, eugenis Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D74135
1 parent b8f4e0a commit 25de3f9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void GuardedPoolAllocator::init(const options::Options &Opts) {
9999
else
100100
AdjustedSampleRatePlusOne = 2;
101101

102+
initPRNG();
102103
ThreadLocals.NextSampleCounter =
103104
(getRandomUnsigned32() % (AdjustedSampleRatePlusOne - 1)) + 1;
104105

compiler-rt/lib/gwp_asan/random.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111

1212
#include <time.h>
1313

14+
// Initialised to a magic constant so that an uninitialised GWP-ASan won't
15+
// regenerate its sample counter for as long as possible. The xorshift32()
16+
// algorithm used below results in getRandomUnsigned32(0xff82eb50) ==
17+
// 0xfffffea4.
18+
GWP_ASAN_TLS_INITIAL_EXEC uint32_t RandomState = 0xff82eb50;
19+
1420
namespace gwp_asan {
21+
void initPRNG() {
22+
RandomState = time(nullptr) + getThreadID();
23+
}
24+
1525
uint32_t getRandomUnsigned32() {
16-
thread_local uint32_t RandomState = time(nullptr) + getThreadID();
1726
RandomState ^= RandomState << 13;
1827
RandomState ^= RandomState >> 17;
1928
RandomState ^= RandomState << 5;

compiler-rt/lib/gwp_asan/random.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <stdint.h>
1313

1414
namespace gwp_asan {
15+
// Initialise the PRNG, using time and thread ID as the seed.
16+
void initPRNG();
17+
1518
// xorshift (32-bit output), extremely fast PRNG that uses arithmetic operations
1619
// only. Seeded using walltime.
1720
uint32_t getRandomUnsigned32();

0 commit comments

Comments
 (0)