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