2018-09-02 02:03:27 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "pages.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2019-04-07 06:05:11 -04:00
|
|
|
static bool add_guards(size_t size, size_t guard_size, size_t *total_size) {
|
|
|
|
return __builtin_add_overflow(size, guard_size, total_size) ||
|
|
|
|
__builtin_add_overflow(*total_size, guard_size, total_size);
|
|
|
|
}
|
|
|
|
|
2019-03-22 23:17:26 -04:00
|
|
|
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) {
|
2018-09-02 02:03:27 -04:00
|
|
|
size_t real_size;
|
2019-04-07 06:05:11 -04:00
|
|
|
if (unlikely(add_guards(usable_size, guard_size, &real_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void *real = memory_map(real_size);
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unlikely(real == NULL)) {
|
2018-09-02 02:03:27 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-03-22 23:17:26 -04:00
|
|
|
memory_set_name(real, real_size, name);
|
2018-09-02 02:03:27 -04:00
|
|
|
void *usable = (char *)real + guard_size;
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unprotect && unlikely(memory_protect_rw(usable, usable_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
memory_unmap(real, real_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return usable;
|
|
|
|
}
|
|
|
|
|
2019-03-23 22:29:04 -04:00
|
|
|
void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_size, const char *name) {
|
2022-01-16 14:41:46 -05:00
|
|
|
usable_size = page_align(usable_size);
|
2018-09-02 02:03:27 -04:00
|
|
|
if (unlikely(!usable_size)) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t alloc_size;
|
|
|
|
if (unlikely(__builtin_add_overflow(usable_size, alignment - PAGE_SIZE, &alloc_size))) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t real_alloc_size;
|
2019-04-07 06:05:11 -04:00
|
|
|
if (unlikely(add_guards(alloc_size, guard_size, &real_alloc_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *real = memory_map(real_alloc_size);
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unlikely(real == NULL)) {
|
2018-09-02 02:03:27 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-03-23 22:29:04 -04:00
|
|
|
memory_set_name(real, real_alloc_size, name);
|
2018-09-02 02:03:27 -04:00
|
|
|
|
|
|
|
void *usable = (char *)real + guard_size;
|
|
|
|
|
2022-01-16 14:41:46 -05:00
|
|
|
size_t lead_size = align((uintptr_t)usable, alignment) - (uintptr_t)usable;
|
2018-09-02 02:03:27 -04:00
|
|
|
size_t trail_size = alloc_size - lead_size - usable_size;
|
|
|
|
void *base = (char *)usable + lead_size;
|
|
|
|
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unlikely(memory_protect_rw(base, usable_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
memory_unmap(real, real_alloc_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lead_size) {
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unlikely(memory_unmap(real, lead_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
memory_unmap(real, real_alloc_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trail_size) {
|
2018-09-02 02:08:44 -04:00
|
|
|
if (unlikely(memory_unmap((char *)base + usable_size + guard_size, trail_size))) {
|
2018-09-02 02:03:27 -04:00
|
|
|
memory_unmap(real, real_alloc_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
2019-03-23 22:29:04 -04:00
|
|
|
|
|
|
|
void deallocate_pages(void *usable, size_t usable_size, size_t guard_size) {
|
2021-05-12 01:05:39 -04:00
|
|
|
if (unlikely(memory_unmap((char *)usable - guard_size, usable_size + guard_size * 2))) {
|
|
|
|
memory_purge(usable, usable_size);
|
|
|
|
}
|
2019-03-23 22:29:04 -04:00
|
|
|
}
|