add infrastructure for a larger guard size option

This commit is contained in:
Daniel Micay 2019-04-07 06:05:11 -04:00
parent c68de6141d
commit 7a7126e780

View File

@ -4,13 +4,18 @@
#include "pages.h" #include "pages.h"
#include "util.h" #include "util.h"
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);
}
static uintptr_t alignment_ceiling(uintptr_t s, uintptr_t alignment) { static uintptr_t alignment_ceiling(uintptr_t s, uintptr_t alignment) {
return ((s) + (alignment - 1)) & ((~alignment) + 1); return ((s) + (alignment - 1)) & ((~alignment) + 1);
} }
void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) { void *allocate_pages(size_t usable_size, size_t guard_size, bool unprotect, const char *name) {
size_t real_size; size_t real_size;
if (unlikely(__builtin_add_overflow(usable_size, guard_size * 2, &real_size))) { if (unlikely(add_guards(usable_size, guard_size, &real_size))) {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }
@ -41,7 +46,7 @@ void *allocate_pages_aligned(size_t usable_size, size_t alignment, size_t guard_
} }
size_t real_alloc_size; size_t real_alloc_size;
if (unlikely(__builtin_add_overflow(alloc_size, guard_size * 2, &real_alloc_size))) { if (unlikely(add_guards(alloc_size, guard_size, &real_alloc_size))) {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }