11
2+ #include " hwasan_thread.h"
3+
24#include " hwasan.h"
5+ #include " hwasan_interface_internal.h"
36#include " hwasan_mapping.h"
4- #include " hwasan_thread.h"
57#include " hwasan_poisoning.h"
6- #include " hwasan_interface_internal.h"
7-
8+ #include " sanitizer_common/sanitizer_atomic.h"
89#include " sanitizer_common/sanitizer_file.h"
910#include " sanitizer_common/sanitizer_placement_new.h"
1011#include " sanitizer_common/sanitizer_tls_get_addr.h"
1112
12-
1313namespace __hwasan {
1414
1515static u32 RandomSeed () {
@@ -27,6 +27,7 @@ static u32 RandomSeed() {
2727
2828void Thread::InitRandomState () {
2929 random_state_ = flags ()->random_tags ? RandomSeed () : unique_id_;
30+ random_state_inited_ = true ;
3031
3132 // Push a random number of zeros onto the ring buffer so that the first stack
3233 // tag base will be random.
@@ -40,8 +41,9 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
4041 CHECK_EQ (0 , stack_top_);
4142 CHECK_EQ (0 , stack_bottom_);
4243
43- static u64 unique_id;
44- unique_id_ = unique_id++;
44+ static atomic_uint64_t unique_id;
45+ unique_id_ = atomic_fetch_add (&unique_id, 1 , memory_order_relaxed);
46+
4547 if (auto sz = flags ()->heap_history_size )
4648 heap_allocations_ = HeapAllocationsRingBuffer::New (sz);
4749
@@ -123,17 +125,21 @@ static u32 xorshift(u32 state) {
123125// Generate a (pseudo-)random non-zero tag.
124126tag_t Thread::GenerateRandomTag (uptr num_bits) {
125127 DCHECK_GT (num_bits, 0 );
126- if (tagging_disabled_) return 0 ;
128+ if (tagging_disabled_)
129+ return 0 ;
127130 tag_t tag;
128131 const uptr tag_mask = (1ULL << num_bits) - 1 ;
129132 do {
130133 if (flags ()->random_tags ) {
131- if (!random_buffer_)
134+ if (!random_buffer_) {
135+ EnsureRandomStateInited ();
132136 random_buffer_ = random_state_ = xorshift (random_state_);
137+ }
133138 CHECK (random_buffer_);
134139 tag = random_buffer_ & tag_mask;
135140 random_buffer_ >>= num_bits;
136141 } else {
142+ EnsureRandomStateInited ();
137143 random_state_ += 1 ;
138144 tag = random_state_ & tag_mask;
139145 }
0 commit comments