| 1 | // -*- C++ -*- |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_MMAPWRAPPER_H |
| 19 | #define HL_MMAPWRAPPER_H |
| 20 | |
| 21 | #include "utility/arch.h" |
| 22 | |
| 23 | #if defined(_WIN32) |
| 24 | #include <windows.h> |
| 25 | #else |
| 26 | // UNIX |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
| 31 | #include <sys/mman.h> |
| 32 | #if defined(__linux__) |
| 33 | #include <sys/prctl.h> |
| 34 | #if !defined(PR_SET_VMA) |
| 35 | #define PR_SET_VMA 0x53564d41 |
| 36 | #define PR_SET_VMA_ANON_NAME 0 |
| 37 | #endif |
| 38 | #endif |
| 39 | #include <map> |
| 40 | #endif |
| 41 | |
| 42 | #if defined(__SVR4) |
| 43 | // Not sure why this is needed for Solaris, but there it is. |
| 44 | extern "C" int madvise (caddr_t, size_t, int); |
| 45 | #endif |
| 46 | |
| 47 | #if !defined(HL_MMAP_PROTECTION_MASK) |
| 48 | #if HL_EXECUTABLE_HEAP |
| 49 | #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_EXEC) |
| 50 | #else |
| 51 | #if !defined(PROT_MAX) |
| 52 | #define PROT_MAX(p) 0 |
| 53 | #endif |
| 54 | #define HL_MMAP_PROTECTION_MASK (PROT_READ | PROT_WRITE | PROT_MAX(PROT_READ | PROT_WRITE)) |
| 55 | #endif |
| 56 | #endif |
| 57 | |
| 58 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 59 | #define MAP_ANONYMOUS MAP_ANON |
| 60 | #endif |
| 61 | |
| 62 | namespace HL { |
| 63 | |
| 64 | class MmapWrapper { |
| 65 | public: |
| 66 | |
| 67 | #if defined(_WIN32) |
| 68 | |
| 69 | // Microsoft Windows has 4K pages aligned to a 64K boundary. |
| 70 | enum { Size = 4 * 1024UL }; |
| 71 | enum { Alignment = 64 * 1024UL }; |
| 72 | |
| 73 | #elif defined(__SVR4) |
| 74 | // Solaris aligns 8K pages to a 64K boundary. |
| 75 | enum { Size = 8 * 1024UL }; |
| 76 | enum { Alignment = 64 * 1024UL }; |
| 77 | |
| 78 | #elif defined(HL_APPLE_SILICON) |
| 79 | // macOS on Apple Silicon aligns 16K pages to a 16K boundary. |
| 80 | enum { Size = 16 * 1024UL }; |
| 81 | enum { Alignment = 16 * 1024UL }; |
| 82 | |
| 83 | #else |
| 84 | // Linux and most other operating systems align memory to a 4K boundary. |
| 85 | enum { Size = 4 * 1024UL }; |
| 86 | enum { Alignment = 4 * 1024UL }; |
| 87 | |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | // Release the given range of memory to the OS (without unmapping it). |
| 92 | void release (void * ptr, size_t sz) { |
| 93 | if ((size_t) ptr % Alignment == 0) { |
| 94 | // Extra sanity check in case the superheap's declared alignment is wrong! |
| 95 | #if defined(_WIN32) |
| 96 | VirtualAlloc (ptr, sz, MEM_RESET, PAGE_NOACCESS); |
| 97 | #elif defined(__APPLE__) |
| 98 | madvise (ptr, sz, MADV_DONTNEED); |
| 99 | madvise (ptr, sz, MADV_FREE); |
| 100 | #else |
| 101 | // Assume Unix platform. |
| 102 | madvise ((caddr_t) ptr, sz, MADV_DONTNEED); |
| 103 | #endif |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #if defined(_WIN32) |
| 108 | |
| 109 | static void protect (void * ptr, size_t sz) { |
| 110 | DWORD oldProtection; |
| 111 | VirtualProtect (ptr, sz, PAGE_NOACCESS, &oldProtection); |
| 112 | } |
| 113 | |
| 114 | static void unprotect (void * ptr, size_t sz) { |
| 115 | DWORD oldProtection; |
| 116 | VirtualProtect (ptr, sz, PAGE_READWRITE, &oldProtection); |
| 117 | } |
| 118 | |
| 119 | static void * map (size_t sz) { |
| 120 | void * ptr; |
| 121 | #if HL_EXECUTABLE_HEAP |
| 122 | const int permflags = PAGE_EXECUTE_READWRITE; |
| 123 | #else |
| 124 | const int permflags = PAGE_READWRITE; |
| 125 | #endif |
| 126 | ptr = VirtualAlloc(nullptr, sz, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, permflags); |
| 127 | return ptr; |
| 128 | } |
| 129 | |
| 130 | static void unmap (void * ptr, size_t) { |
| 131 | VirtualFree (ptr, 0, MEM_RELEASE); |
| 132 | } |
| 133 | |
| 134 | #else // UNIX |
| 135 | |
| 136 | static void protect (void * ptr, size_t sz) { |
| 137 | mprotect ((char *) ptr, sz, PROT_NONE); |
| 138 | } |
| 139 | |
| 140 | static void unprotect (void * ptr, size_t sz) { |
| 141 | mprotect ((char *) ptr, sz, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 142 | } |
| 143 | |
| 144 | static void * map (size_t sz) { |
| 145 | |
| 146 | if (sz == 0) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
| 150 | // Round up the size to a page-sized value. |
| 151 | sz = Size * ((sz + Size - 1) / Size); |
| 152 | |
| 153 | void * ptr; |
| 154 | int mapFlag = 0; |
| 155 | char * startAddress = 0; |
| 156 | |
| 157 | #if defined(MAP_ALIGN) && defined(MAP_ANON) |
| 158 | int fd = -1; |
| 159 | startAddress = (char *) Alignment; |
| 160 | mapFlag |= MAP_PRIVATE | MAP_ALIGN | MAP_ANON; |
| 161 | #elif defined(MAP_ALIGNED) |
| 162 | int fd = -1; |
| 163 | // On allocations equal or larger than page size, we align it to the log2 boundary |
| 164 | // in those contexts, sometimes (on NetBSD notably) large mappings tends to fail |
| 165 | // without this flag. |
| 166 | size_t alignment = ilog2(sz); |
| 167 | mapFlag |= MAP_PRIVATE | MAP_ANON; |
| 168 | if (alignment >= 12ul) |
| 169 | mapFlag |= MAP_ALIGNED(alignment); |
| 170 | #elif !defined(MAP_ANONYMOUS) |
| 171 | static int fd = ::open ("/dev/zero", O_RDWR); |
| 172 | mapFlag |= MAP_PRIVATE; |
| 173 | #else |
| 174 | int fd = -1; |
| 175 | // mapFlag |= MAP_ANONYMOUS | MAP_PRIVATE; |
| 176 | mapFlag |= MAP_ANON | MAP_PRIVATE; |
| 177 | #if HL_EXECUTABLE_HEAP |
| 178 | #if defined(MAP_JIT) |
| 179 | mapFlag |= MAP_JIT; |
| 180 | #endif |
| 181 | #endif |
| 182 | #endif |
| 183 | |
| 184 | ptr = mmap(startAddress, sz, HL_MMAP_PROTECTION_MASK, mapFlag, fd, 0); |
| 185 | #ifdef HL_APPLE_SILICON |
| 186 | #include <mach/vm_page_size.h> |
| 187 | assert(sz % vm_page_size == 0); |
| 188 | assert((reinterpret_cast<uintptr_t>(ptr) % vm_page_size) == 0); |
| 189 | #endif |
| 190 | |
| 191 | if (ptr == MAP_FAILED) { |
| 192 | // tprintf::tprintf("MmapWrapper: MAP_FAILED\n"); |
| 193 | return nullptr; |
| 194 | } else { |
| 195 | #if HL_NAMED_HEAP |
| 196 | // For every anonymous map (assuming CONFIG_ANON_VMA_NAME is on), the range appears in /proc/<pid>/maps as |
| 197 | // `560215e4f000-560215e81000 rw-p 00000000 00:00 0 7ffaf89c0000-7ffaf8aa1000 rw-p 00000000 00:00 0 [anon:Heap Layers]` |
| 198 | // |
| 199 | // By default the region name is `Heap Layers` but can be defined in heaplayers.h via the HL_HEAP_NAME constant. |
| 200 | // If the kernel did not optin the `CONFIG_ANON_VMA_NAME` option, or even with old kernels prior to 5.17 |
| 201 | // the following call will be a no-op |
| 202 | (void)prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, |
| 203 | reinterpret_cast<uintptr_t>(ptr), |
| 204 | sz, |
| 205 | reinterpret_cast<uintptr_t>(HL_HEAP_NAME)); |
| 206 | #endif |
| 207 | return ptr; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void unmap (void * ptr, size_t sz) { |
| 212 | // Round up the size to a page-sized value. |
| 213 | sz = Size * ((sz + Size - 1) / Size); |
| 214 | munmap ((caddr_t) ptr, sz); |
| 215 | } |
| 216 | |
| 217 | #endif |
| 218 | |
| 219 | }; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | #endif |