Skip to content

Commit

Permalink
preserve PROT_MTE when releasing memory
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Oct 12, 2024
1 parent 9739cb4 commit e035792
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 11 additions & 2 deletions h_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ static void *memory_map_tagged(size_t size) {
return memory_map(size);
}

static bool memory_map_fixed_tagged(void *ptr, size_t size) {
#ifdef HAS_ARM_MTE
if (likely51(is_memtag_enabled())) {
return memory_map_fixed_mte(ptr, size);
}
#endif
return memory_map_fixed(ptr, size);
}

#define SLAB_METADATA_COUNT

struct slab_metadata {
Expand Down Expand Up @@ -899,7 +908,7 @@ static inline void deallocate_small(void *p, const size_t *expected_size) {

if (c->empty_slabs_total + slab_size > max_empty_slabs_total) {
int saved_errno = errno;
if (!memory_map_fixed(slab, slab_size)) {
if (!memory_map_fixed_tagged(slab, slab_size)) {
label_slab(slab, slab_size, class);
stats_slab_deallocate(c, slab_size);
enqueue_free_slab(c, metadata);
Expand Down Expand Up @@ -1896,7 +1905,7 @@ EXPORT int h_malloc_trim(UNUSED size_t pad) {
struct slab_metadata *iterator = c->empty_slabs;
while (iterator) {
void *slab = get_slab(c, slab_size, iterator);
if (memory_map_fixed(slab, slab_size)) {
if (memory_map_fixed_tagged(slab, slab_size)) {
break;
}
label_slab(slab, slab_size, class);
Expand Down
15 changes: 13 additions & 2 deletions memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@ void *memory_map_mte(size_t size) {
}
#endif

bool memory_map_fixed(void *ptr, size_t size) {
void *p = mmap(ptr, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
static bool memory_map_fixed_prot(void *ptr, size_t size, int prot) {
void *p = mmap(ptr, size, prot, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
bool ret = p == MAP_FAILED;
if (unlikely(ret) && errno != ENOMEM) {
fatal_error("non-ENOMEM MAP_FIXED mmap failure");
}
return ret;
}

bool memory_map_fixed(void *ptr, size_t size) {
return memory_map_fixed_prot(ptr, size, PROT_NONE);
}

#ifdef HAS_ARM_MTE
// Note that PROT_MTE can't be cleared via mprotect
bool memory_map_fixed_mte(void *ptr, size_t size) {
return memory_map_fixed_prot(ptr, size, PROT_MTE);
}
#endif

bool memory_unmap(void *ptr, size_t size) {
bool ret = munmap(ptr, size);
if (unlikely(ret) && errno != ENOMEM) {
Expand Down
3 changes: 3 additions & 0 deletions memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ void *memory_map(size_t size);
void *memory_map_mte(size_t size);
#endif
bool memory_map_fixed(void *ptr, size_t size);
#ifdef HAS_ARM_MTE
bool memory_map_fixed_mte(void *ptr, size_t size);
#endif
bool memory_unmap(void *ptr, size_t size);
bool memory_protect_ro(void *ptr, size_t size);
bool memory_protect_rw(void *ptr, size_t size);
Expand Down

0 comments on commit e035792

Please sign in to comment.