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