From c2d873dd2bf7e1d07f1eec340bcdead17e4b3d3a Mon Sep 17 00:00:00 2001 From: jewelcodes Date: Wed, 20 Nov 2024 17:54:21 -0500 Subject: [PATCH] libc: malloc() variant with execute perms --- src/include/stdlib.h | 1 + src/libc/stdlib.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/include/stdlib.h b/src/include/stdlib.h index fcb9d43..47922a7 100644 --- a/src/include/stdlib.h +++ b/src/include/stdlib.h @@ -23,6 +23,7 @@ void *malloc(size_t); void *calloc(size_t, size_t); void *realloc(void *, size_t); void *umalloc(size_t); +void *uxmalloc(size_t); void *ucalloc(size_t, size_t); void *urealloc(void *, size_t); void free(void *); diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c index 103d183..3290762 100644 --- a/src/libc/stdlib.c +++ b/src/libc/stdlib.c @@ -149,6 +149,33 @@ void *umalloc(size_t size) { return (void *)((uintptr_t)ptr + sizeof(struct mallocHeader)); } +void *uxmalloc(size_t size) { + /* again exactly the same umalloc() but with execute permissions, this will + * be used for installing platform-specific signal trampoline code */ + if(!size) return NULL; + size_t pageSize = (size + sizeof(struct mallocHeader) + PAGE_SIZE - 1) / PAGE_SIZE; + + acquireLockBlocking(&lock); + + uintptr_t ptr = vmmAllocate(USER_HEAP_BASE, USER_HEAP_LIMIT, pageSize, VMM_WRITE | VMM_USER | VMM_EXEC); + if(!ptr) { + releaseLock(&lock); + return NULL; + } + + struct mallocHeader *header = (struct mallocHeader *)ptr; + header->byteSize = size; + header->pageSize = pageSize; + + // allocate a guard page as well + uintptr_t guard = ptr + (pageSize*PAGE_SIZE); + platformMapPage(guard, 0, 0); + + releaseLock(&lock); + + return (void *)((uintptr_t)ptr + sizeof(struct mallocHeader)); +} + void *calloc(size_t num, size_t size) { void *ptr = malloc(num * size); if(!ptr) return NULL;