1
1
2
+ #include " hwasan_thread.h"
3
+
2
4
#include " hwasan.h"
5
+ #include " hwasan_interface_internal.h"
3
6
#include " hwasan_mapping.h"
4
- #include " hwasan_thread.h"
5
7
#include " hwasan_poisoning.h"
6
- #include " hwasan_interface_internal.h"
7
-
8
+ #include " sanitizer_common/sanitizer_atomic.h"
8
9
#include " sanitizer_common/sanitizer_file.h"
9
10
#include " sanitizer_common/sanitizer_placement_new.h"
10
11
#include " sanitizer_common/sanitizer_tls_get_addr.h"
11
12
12
-
13
13
namespace __hwasan {
14
14
15
15
static u32 RandomSeed () {
@@ -27,6 +27,7 @@ static u32 RandomSeed() {
27
27
28
28
void Thread::InitRandomState () {
29
29
random_state_ = flags ()->random_tags ? RandomSeed () : unique_id_;
30
+ random_state_inited_ = true ;
30
31
31
32
// Push a random number of zeros onto the ring buffer so that the first stack
32
33
// tag base will be random.
@@ -40,8 +41,9 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
40
41
CHECK_EQ (0 , stack_top_);
41
42
CHECK_EQ (0 , stack_bottom_);
42
43
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
+
45
47
if (auto sz = flags ()->heap_history_size )
46
48
heap_allocations_ = HeapAllocationsRingBuffer::New (sz);
47
49
@@ -123,17 +125,21 @@ static u32 xorshift(u32 state) {
123
125
// Generate a (pseudo-)random non-zero tag.
124
126
tag_t Thread::GenerateRandomTag (uptr num_bits) {
125
127
DCHECK_GT (num_bits, 0 );
126
- if (tagging_disabled_) return 0 ;
128
+ if (tagging_disabled_)
129
+ return 0 ;
127
130
tag_t tag;
128
131
const uptr tag_mask = (1ULL << num_bits) - 1 ;
129
132
do {
130
133
if (flags ()->random_tags ) {
131
- if (!random_buffer_)
134
+ if (!random_buffer_) {
135
+ EnsureRandomStateInited ();
132
136
random_buffer_ = random_state_ = xorshift (random_state_);
137
+ }
133
138
CHECK (random_buffer_);
134
139
tag = random_buffer_ & tag_mask;
135
140
random_buffer_ >>= num_bits;
136
141
} else {
142
+ EnsureRandomStateInited ();
137
143
random_state_ += 1 ;
138
144
tag = random_state_ & tag_mask;
139
145
}
0 commit comments