17
17
#include "utils_common.h"
18
18
#include "utils_concurrency.h"
19
19
#include "utils_math.h"
20
+ #include "utils_sanitizers.h"
20
21
21
22
// global base allocator used by all providers and pools
22
23
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT ;
@@ -102,8 +103,15 @@ static void *add_metadata_and_align(void *ptr, size_t size, size_t alignment) {
102
103
assert (ptr_offset_from_original < (1ULL << 32 ));
103
104
104
105
size_t * metadata_loc = (size_t * )((char * )user_ptr - ALLOC_METADATA_SIZE );
106
+
107
+ // mark entire allocation as undefined memory so that we can store metadata
108
+ utils_annotate_memory_undefined (ptr , size );
109
+
105
110
* metadata_loc = size | (ptr_offset_from_original << 32 );
106
111
112
+ // mark the metadata part as inaccessible
113
+ utils_annotate_memory_inaccessible (ptr , ptr_offset_from_original );
114
+
107
115
return user_ptr ;
108
116
}
109
117
@@ -116,9 +124,15 @@ static void *get_original_alloc(void *user_ptr, size_t *total_size,
116
124
117
125
size_t * metadata_loc = (size_t * )((char * )user_ptr - ALLOC_METADATA_SIZE );
118
126
127
+ // mark the metadata as defined to read the size and offset
128
+ utils_annotate_memory_undefined (metadata_loc , ALLOC_METADATA_SIZE );
129
+
119
130
size_t stored_size = * metadata_loc & ((1ULL << 32 ) - 1 );
120
131
size_t ptr_offset_from_original = * metadata_loc >> 32 ;
121
132
133
+ // restore the original access mode
134
+ utils_annotate_memory_inaccessible (metadata_loc , ALLOC_METADATA_SIZE );
135
+
122
136
void * original_ptr =
123
137
(void * )((uintptr_t )user_ptr - ptr_offset_from_original );
124
138
@@ -178,16 +192,20 @@ void umf_ba_global_free(void *ptr) {
178
192
179
193
int ac_index = size_to_idx (total_size );
180
194
if (ac_index >= NUM_ALLOCATION_CLASSES ) {
195
+ utils_annotate_memory_inaccessible (ptr , total_size );
181
196
ba_os_free (ptr , total_size );
182
197
return ;
183
198
}
184
199
185
200
if (!BASE_ALLOC .ac [ac_index ]) {
186
201
// if creating ac failed, memory must have been allocated by os
202
+ utils_annotate_memory_inaccessible (ptr , total_size );
187
203
ba_os_free (ptr , total_size );
188
204
return ;
189
205
}
190
206
207
+ // base_alloc expects the allocation to be undefined memory
208
+ utils_annotate_memory_undefined (ptr , total_size );
191
209
umf_ba_free (BASE_ALLOC .ac [ac_index ], ptr );
192
210
}
193
211
0 commit comments