1717#include "utils_common.h"
1818#include "utils_concurrency.h"
1919#include "utils_math.h"
20+ #include "utils_sanitizers.h"
2021
2122// global base allocator used by all providers and pools
2223static 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) {
102103 assert (ptr_offset_from_original < (1ULL << 32 ));
103104
104105 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+
105110 * metadata_loc = size | (ptr_offset_from_original << 32 );
106111
112+ // mark the metadata part as inaccessible
113+ utils_annotate_memory_inaccessible (ptr , ptr_offset_from_original );
114+
107115 return user_ptr ;
108116}
109117
@@ -116,9 +124,15 @@ static void *get_original_alloc(void *user_ptr, size_t *total_size,
116124
117125 size_t * metadata_loc = (size_t * )((char * )user_ptr - ALLOC_METADATA_SIZE );
118126
127+ // mark the metadata as defined to read the size and offset
128+ utils_annotate_memory_undefined (metadata_loc , ALLOC_METADATA_SIZE );
129+
119130 size_t stored_size = * metadata_loc & ((1ULL << 32 ) - 1 );
120131 size_t ptr_offset_from_original = * metadata_loc >> 32 ;
121132
133+ // restore the original access mode
134+ utils_annotate_memory_inaccessible (metadata_loc , ALLOC_METADATA_SIZE );
135+
122136 void * original_ptr =
123137 (void * )((uintptr_t )user_ptr - ptr_offset_from_original );
124138
@@ -178,16 +192,20 @@ void umf_ba_global_free(void *ptr) {
178192
179193 int ac_index = size_to_idx (total_size );
180194 if (ac_index >= NUM_ALLOCATION_CLASSES ) {
195+ utils_annotate_memory_inaccessible (ptr , total_size );
181196 ba_os_free (ptr , total_size );
182197 return ;
183198 }
184199
185200 if (!BASE_ALLOC .ac [ac_index ]) {
186201 // if creating ac failed, memory must have been allocated by os
202+ utils_annotate_memory_inaccessible (ptr , total_size );
187203 ba_os_free (ptr , total_size );
188204 return ;
189205 }
190206
207+ // base_alloc expects the allocation to be undefined memory
208+ utils_annotate_memory_undefined (ptr , total_size );
191209 umf_ba_free (BASE_ALLOC .ac [ac_index ], ptr );
192210}
193211
0 commit comments