Skip to content

Add missing annotations to base_alloc_global #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "utils_common.h"
#include "utils_concurrency.h"
#include "utils_math.h"
#include "utils_sanitizers.h"

// global base allocator used by all providers and pools
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT;
Expand Down Expand Up @@ -102,8 +103,15 @@ static void *add_metadata_and_align(void *ptr, size_t size, size_t alignment) {
assert(ptr_offset_from_original < (1ULL << 32));

size_t *metadata_loc = (size_t *)((char *)user_ptr - ALLOC_METADATA_SIZE);

// mark entire allocation as undefined memory so that we can store metadata
utils_annotate_memory_undefined(ptr, size);

*metadata_loc = size | (ptr_offset_from_original << 32);

// mark the metadata part as inaccessible
utils_annotate_memory_inaccessible(ptr, ptr_offset_from_original);

return user_ptr;
}

Expand All @@ -116,9 +124,15 @@ static void *get_original_alloc(void *user_ptr, size_t *total_size,

size_t *metadata_loc = (size_t *)((char *)user_ptr - ALLOC_METADATA_SIZE);

// mark the metadata as defined to read the size and offset
utils_annotate_memory_undefined(metadata_loc, ALLOC_METADATA_SIZE);

size_t stored_size = *metadata_loc & ((1ULL << 32) - 1);
size_t ptr_offset_from_original = *metadata_loc >> 32;

// restore the original access mode
utils_annotate_memory_inaccessible(metadata_loc, ALLOC_METADATA_SIZE);

void *original_ptr =
(void *)((uintptr_t)user_ptr - ptr_offset_from_original);

Expand Down Expand Up @@ -178,16 +192,20 @@ void umf_ba_global_free(void *ptr) {

int ac_index = size_to_idx(total_size);
if (ac_index >= NUM_ALLOCATION_CLASSES) {
utils_annotate_memory_inaccessible(ptr, total_size);
ba_os_free(ptr, total_size);
return;
}

if (!BASE_ALLOC.ac[ac_index]) {
// if creating ac failed, memory must have been allocated by os
utils_annotate_memory_inaccessible(ptr, total_size);
ba_os_free(ptr, total_size);
return;
}

// base_alloc expects the allocation to be undefined memory
utils_annotate_memory_undefined(ptr, total_size);
umf_ba_free(BASE_ALLOC.ac[ac_index], ptr);
}

Expand Down