From d2320a69dfc88e713ab357267be8dfad8275e53b Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 1 Dec 2022 14:35:20 -0800 Subject: [PATCH] On aarch64, use PROT_MTE on arena mapping if MTE is enabled. This automatically uses MTE-enabled mappings (consistent with the rest of the system) if MTE is enabled via a libc-specific environment variable: glibc: GLIBC_TUNABLES=glibc.mem.tagging=1 bionic: MEMTAG_OPTIONS=async --- arena.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arena.c b/arena.c index f1933ed..e8fc3a4 100644 --- a/arena.c +++ b/arena.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -232,15 +233,22 @@ static void check_thp_state(void) { void *alloc_arena_mmap(size_t page_size, bool use_thp, size_t arena_size, int fd) { void *arena; size_t pagemask = page_size - 1; + int prot = PROT_READ | PROT_WRITE; int flags; +#ifdef __aarch64__ + int tac = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + if (tac != -1 && (tac & PR_MTE_TCF_MASK)) + prot |= PROT_MTE; +#endif + if (fd == -1) flags = MAP_PRIVATE | MAP_ANONYMOUS | get_page_size_flags(page_size); else flags = MAP_SHARED; arena_size = (arena_size + pagemask) & ~pagemask; - arena = mmap(0, arena_size, PROT_READ | PROT_WRITE, flags, fd, 0); + arena = mmap(0, arena_size, prot, flags, fd, 0); if (arena == MAP_FAILED) { perror("mmap"); exit(1);