[PATCH] D74135: [GWP-ASan] Fix PRNG to use IE TLS.

Mitch Phillips via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 08:41:44 PST 2020


hctim created this revision.
hctim added reviewers: pcc, cferris.
Herald added projects: Sanitizers, LLVM.
Herald added subscribers: llvm-commits, Sanitizers.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74135

Files:
  compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp
  compiler-rt/lib/gwp_asan/random.cpp
  compiler-rt/lib/gwp_asan/random.h


Index: compiler-rt/lib/gwp_asan/random.h
===================================================================
--- compiler-rt/lib/gwp_asan/random.h
+++ compiler-rt/lib/gwp_asan/random.h
@@ -12,6 +12,9 @@
 #include <stdint.h>
 
 namespace gwp_asan {
+// Initialise the PRNG, using time and thread ID as the seed.
+void initPRNG();
+
 // xorshift (32-bit output), extremely fast PRNG that uses arithmetic operations
 // only. Seeded using walltime.
 uint32_t getRandomUnsigned32();
Index: compiler-rt/lib/gwp_asan/random.cpp
===================================================================
--- compiler-rt/lib/gwp_asan/random.cpp
+++ compiler-rt/lib/gwp_asan/random.cpp
@@ -11,9 +11,18 @@
 
 #include <time.h>
 
+// Initialised to UINT32_MAX - 1 so that an uninitialised GWP-ASan results in a
+// sample counter of ((UINT32_MAX - 1) % UINT32_MAX) + 1 (i.e. UINT32_MAX). This
+// means the sample counter is regenerated as infrequently as possible if
+// GWP-ASan is disabled.
+GWP_ASAN_TLS_INITIAL_EXEC uint32_t RandomState = UINT32_MAX - 1;
+
 namespace gwp_asan {
+void initPRNG() {
+  RandomState = time(nullptr) + getThreadID();
+}
+
 uint32_t getRandomUnsigned32() {
-  thread_local uint32_t RandomState = time(nullptr) + getThreadID();
   RandomState ^= RandomState << 13;
   RandomState ^= RandomState >> 17;
   RandomState ^= RandomState << 5;
Index: compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp
===================================================================
--- compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp
+++ compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp
@@ -99,6 +99,7 @@
   else
     AdjustedSampleRatePlusOne = 2;
 
+  initPRNG();
   ThreadLocals.NextSampleCounter =
       (getRandomUnsigned32() % (AdjustedSampleRatePlusOne - 1)) + 1;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74135.242917.patch
Type: text/x-patch
Size: 1778 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200206/234ef4af/attachment.bin>


More information about the llvm-commits mailing list