Skip to content

Commit

Permalink
libc: malloc() variant with execute perms
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 20, 2024
1 parent 9c4716f commit c2d873d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
27 changes: 27 additions & 0 deletions src/libc/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c2d873d

Please sign in to comment.