2018-08-29 00:53:12 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
2018-12-11 12:37:37 -05:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
|
|
|
#ifndef PR_SET_VMA
|
|
|
|
#define PR_SET_VMA 0x53564d41
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PR_SET_VMA_ANON_NAME
|
|
|
|
#define PR_SET_VMA_ANON_NAME 0
|
|
|
|
#endif
|
2018-08-29 00:53:12 -04:00
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
void *memory_map(size_t size) {
|
|
|
|
void *p = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
|
|
|
|
if (unlikely(p == MAP_FAILED)) {
|
|
|
|
if (errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mmap failure");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_map_fixed(void *ptr, size_t size) {
|
2018-08-29 13:04:52 -04:00
|
|
|
void *p = mmap(ptr, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED, -1, 0);
|
2021-03-22 13:59:16 -04:00
|
|
|
bool ret = p == MAP_FAILED;
|
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM MAP_FIXED mmap failure");
|
2018-08-29 13:04:52 -04:00
|
|
|
}
|
2021-03-22 13:59:16 -04:00
|
|
|
return ret;
|
2018-08-29 13:04:52 -04:00
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_unmap(void *ptr, size_t size) {
|
|
|
|
bool ret = munmap(ptr, size);
|
2018-08-29 00:53:12 -04:00
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM munmap failure");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
static bool memory_protect_prot(void *ptr, size_t size, int prot, UNUSED int pkey) {
|
2018-10-19 21:29:40 -04:00
|
|
|
#ifdef USE_PKEY
|
2021-03-22 13:59:16 -04:00
|
|
|
bool ret = pkey_mprotect(ptr, size, prot, pkey);
|
2018-10-19 21:29:40 -04:00
|
|
|
#else
|
2021-03-22 13:59:16 -04:00
|
|
|
bool ret = mprotect(ptr, size, prot);
|
2018-10-19 21:29:40 -04:00
|
|
|
#endif
|
2018-08-29 00:53:12 -04:00
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mprotect failure");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-29 10:43:54 -04:00
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_protect_ro(void *ptr, size_t size) {
|
2018-10-19 21:29:40 -04:00
|
|
|
return memory_protect_prot(ptr, size, PROT_READ, -1);
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_protect_rw(void *ptr, size_t size) {
|
2018-10-19 21:29:40 -04:00
|
|
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, -1);
|
2018-08-29 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_protect_rw_metadata(void *ptr, size_t size) {
|
2018-10-19 21:29:40 -04:00
|
|
|
return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
|
2018-08-29 10:52:10 -04:00
|
|
|
}
|
|
|
|
|
2019-07-05 21:59:44 -04:00
|
|
|
#ifdef HAVE_COMPATIBLE_MREMAP
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_remap(void *old, size_t old_size, size_t new_size) {
|
2018-10-06 10:40:55 -04:00
|
|
|
void *ptr = mremap(old, old_size, new_size, 0);
|
2021-03-22 13:59:16 -04:00
|
|
|
bool ret = ptr == MAP_FAILED;
|
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM mremap failure");
|
2018-10-06 10:40:55 -04:00
|
|
|
}
|
2021-03-22 13:59:16 -04:00
|
|
|
return ret;
|
2018-10-06 10:40:55 -04:00
|
|
|
}
|
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) {
|
2018-08-29 10:43:54 -04:00
|
|
|
void *ptr = mremap(old, old_size, new_size, MREMAP_MAYMOVE|MREMAP_FIXED, new);
|
2021-03-22 13:59:16 -04:00
|
|
|
bool ret = ptr == MAP_FAILED;
|
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM MREMAP_FIXED mremap failure");
|
2018-08-29 10:43:54 -04:00
|
|
|
}
|
2021-03-22 13:59:16 -04:00
|
|
|
return ret;
|
2018-08-29 10:43:54 -04:00
|
|
|
}
|
2019-07-05 21:59:44 -04:00
|
|
|
#endif
|
2018-12-11 12:37:37 -05:00
|
|
|
|
2021-03-22 13:59:16 -04:00
|
|
|
bool memory_purge(void *ptr, size_t size) {
|
2021-03-22 12:24:26 -04:00
|
|
|
int ret = madvise(ptr, size, MADV_DONTNEED);
|
|
|
|
if (unlikely(ret) && errno != ENOMEM) {
|
|
|
|
fatal_error("non-ENOMEM MADV_DONTNEED madvise failure");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-22 14:19:02 -04:00
|
|
|
bool memory_set_name(UNUSED void *ptr, UNUSED size_t size, UNUSED const char *name) {
|
2018-12-11 12:37:37 -05:00
|
|
|
#ifdef LABEL_MEMORY
|
2021-03-22 14:19:02 -04:00
|
|
|
return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, name);
|
|
|
|
#else
|
|
|
|
return false;
|
2018-12-11 12:37:37 -05:00
|
|
|
#endif
|
|
|
|
}
|