| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2013 JP Ikaheimonen <jp_ikaheimonen@mentor.com> |
| 5 | * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com> |
| 6 | * |
| 7 | * These sources are based on ftp://g.oswego.edu/pub/misc/malloc.c |
| 8 | * file by Doug Lea, released to the public domain. |
| 9 | */ |
| 10 | |
| 11 | #define BT_LOG_OUTPUT_LEVEL (mapping->log_level) |
| 12 | #define BT_LOG_TAG "COMPAT/MMAN" |
| 13 | #include "logging/log.h" |
| 14 | |
| 15 | #include "common/macros.h" |
| 16 | #include "common/common.h" |
| 17 | |
| 18 | #ifdef __APPLE__ |
| 19 | /* |
| 20 | * On macOS, we need a dummy symbol so that the linker won't |
| 21 | * complain of an empty table of contents. |
| 22 | */ |
| 23 | int bt_mman_dummy_symbol; |
| 24 | #endif /* __APPLE__ */ |
| 25 | |
| 26 | #ifdef __MINGW32__ |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <glib.h> |
| 30 | #include <io.h> |
| 31 | #include <pthread.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <windows.h> |
| 34 | |
| 35 | #include "compat/mman.h" |
| 36 | |
| 37 | struct mmap_mapping { |
| 38 | int log_level; |
| 39 | |
| 40 | /* The duplicated handle. */ |
| 41 | HANDLE file_handle; |
| 42 | /* Handle returned by CreateFileMapping. */ |
| 43 | HANDLE map_handle; |
| 44 | }; |
| 45 | |
| 46 | static |
| 47 | GHashTable *mmap_mappings = NULL; |
| 48 | |
| 49 | /* |
| 50 | * This mutex protects the hashtable of memory mappings. |
| 51 | */ |
| 52 | static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 53 | |
| 54 | static |
| 55 | struct mmap_mapping *mapping_create(int log_level) |
| 56 | { |
| 57 | struct mmap_mapping *mapping; |
| 58 | |
| 59 | mapping = malloc(sizeof(struct mmap_mapping)); |
| 60 | if (mapping) { |
| 61 | mapping->file_handle = NULL; |
| 62 | mapping->map_handle = NULL; |
| 63 | mapping->log_level = log_level; |
| 64 | } |
| 65 | |
| 66 | return mapping; |
| 67 | } |
| 68 | |
| 69 | static |
| 70 | void mapping_clean(struct mmap_mapping *mapping) |
| 71 | { |
| 72 | if (mapping) { |
| 73 | if (!CloseHandle(mapping->map_handle)) { |
| 74 | BT_LOGF_STR("Failed to close mmap map_handle."); |
| 75 | bt_common_abort(); |
| 76 | } |
| 77 | if (!CloseHandle(mapping->file_handle)) { |
| 78 | BT_LOGF_STR("Failed to close mmap file_handle."); |
| 79 | bt_common_abort(); |
| 80 | } |
| 81 | free(mapping); |
| 82 | mapping = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | void addr_clean(void *addr) |
| 88 | { |
| 89 | /* Cleanup of handles should never fail. */ |
| 90 | if (!UnmapViewOfFile(addr)) { |
| 91 | /* |
| 92 | * FIXME: We don't have access to the mapping's log |
| 93 | * level here, so force a FATAL level. |
| 94 | */ |
| 95 | BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, BT_LOG_FATAL, BT_LOG_TAG, |
| 96 | "Failed to unmap mmap mapping."); |
| 97 | bt_common_abort(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static |
| 102 | void mmap_lock(int log_level) |
| 103 | { |
| 104 | if (pthread_mutex_lock(&mmap_mutex)) { |
| 105 | BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, log_level, BT_LOG_TAG, "Failed to acquire mmap_mutex."); |
| 106 | bt_common_abort(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | void mmap_unlock(int log_level) |
| 112 | { |
| 113 | if (pthread_mutex_unlock(&mmap_mutex)) { |
| 114 | BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, log_level, BT_LOG_TAG, "Failed to release mmap_mutex."); |
| 115 | bt_common_abort(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Convert mmap memory protection flags to CreateFileMapping page protection |
| 121 | * flag and MapViewOfFile desired access flag. |
| 122 | */ |
| 123 | static |
| 124 | DWORD map_prot_flags(int prot, DWORD *dwDesiredAccess) |
| 125 | { |
| 126 | if (prot & PROT_READ) { |
| 127 | if (prot & PROT_WRITE) { |
| 128 | *dwDesiredAccess = FILE_MAP_WRITE; |
| 129 | if (prot & PROT_EXEC) { |
| 130 | return PAGE_EXECUTE_READWRITE; |
| 131 | } |
| 132 | return PAGE_READWRITE; |
| 133 | } |
| 134 | if (prot & PROT_EXEC) { |
| 135 | *dwDesiredAccess = FILE_MAP_EXECUTE; |
| 136 | return PAGE_EXECUTE_READ; |
| 137 | } |
| 138 | *dwDesiredAccess = FILE_MAP_READ; |
| 139 | return PAGE_READONLY; |
| 140 | } |
| 141 | if (prot & PROT_WRITE) { |
| 142 | *dwDesiredAccess = FILE_MAP_COPY; |
| 143 | return PAGE_WRITECOPY; |
| 144 | } |
| 145 | if (prot & PROT_EXEC) { |
| 146 | *dwDesiredAccess = FILE_MAP_EXECUTE; |
| 147 | return PAGE_EXECUTE_READ; |
| 148 | } |
| 149 | |
| 150 | /* Mapping failed. */ |
| 151 | *dwDesiredAccess = 0; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | void *bt_mmap(size_t length, int prot, int flags, int fd, off_t offset, |
| 156 | int log_level) |
| 157 | { |
| 158 | struct mmap_mapping *mapping = NULL; |
| 159 | void *mapping_addr; |
| 160 | DWORD dwDesiredAccess; |
| 161 | DWORD flProtect; |
| 162 | HANDLE handle; |
| 163 | |
| 164 | /* Check for a valid fd. */ |
| 165 | if (fd == -1) { |
| 166 | _set_errno(EBADF); |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | /* We don't support this at the moment. */ |
| 171 | if (flags == MAP_FIXED) { |
| 172 | _set_errno(ENOTSUP); |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | /* Map mmap flags to those of the Windows API. */ |
| 177 | flProtect = map_prot_flags(prot, &dwDesiredAccess); |
| 178 | if (flProtect == 0) { |
| 179 | _set_errno(EINVAL); |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | /* Allocate the mapping struct. */ |
| 184 | mapping = mapping_create(log_level); |
| 185 | if (!mapping) { |
| 186 | BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, |
| 187 | "Failed to allocate mmap mapping."); |
| 188 | _set_errno(ENOMEM); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | /* Get a handle from the fd. */ |
| 193 | handle = (HANDLE) _get_osfhandle(fd); |
| 194 | |
| 195 | /* Duplicate the handle and store it in 'mapping.file_handle'. */ |
| 196 | if (!DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), |
| 197 | &mapping->file_handle, 0, FALSE, |
| 198 | DUPLICATE_SAME_ACCESS)) { |
| 199 | _set_errno(ENOMEM); |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Create a file mapping object with a maximum size |
| 205 | * of 'offset' + 'length'. |
| 206 | */ |
| 207 | mapping->map_handle = CreateFileMapping(mapping->file_handle, NULL, |
| 208 | flProtect, 0, offset + length, NULL); |
| 209 | if (mapping->map_handle == 0) { |
| 210 | _set_errno(EACCES); |
| 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | /* Map the requested block starting at 'offset' for 'length' bytes. */ |
| 215 | mapping_addr = MapViewOfFile(mapping->map_handle, dwDesiredAccess, 0, |
| 216 | offset, length); |
| 217 | if (mapping_addr == 0) { |
| 218 | DWORD dwLastErr = GetLastError(); |
| 219 | if (dwLastErr == ERROR_MAPPED_ALIGNMENT) { |
| 220 | _set_errno(EINVAL); |
| 221 | } else { |
| 222 | _set_errno(EACCES); |
| 223 | } |
| 224 | goto error; |
| 225 | } |
| 226 | |
| 227 | mmap_lock(log_level); |
| 228 | |
| 229 | /* If we have never done any mappings, allocate the hashtable. */ |
| 230 | if (!mmap_mappings) { |
| 231 | mmap_mappings = g_hash_table_new_full(g_direct_hash, |
| 232 | g_direct_equal, (GDestroyNotify) addr_clean, |
| 233 | (GDestroyNotify) mapping_clean); |
| 234 | if (!mmap_mappings) { |
| 235 | BT_LOGE_STR("Failed to allocate mmap hashtable."); |
| 236 | _set_errno(ENOMEM); |
| 237 | goto error_mutex_unlock; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* Add the new mapping to the hashtable. */ |
| 242 | g_hash_table_insert(mmap_mappings, mapping_addr, mapping); |
| 243 | |
| 244 | mmap_unlock(log_level); |
| 245 | |
| 246 | return mapping_addr; |
| 247 | |
| 248 | error_mutex_unlock: |
| 249 | mmap_unlock(log_level); |
| 250 | error: |
| 251 | mapping_clean(mapping); |
| 252 | return MAP_FAILED; |
| 253 | } |
| 254 | |
| 255 | int bt_munmap(void *addr, size_t length __attribute__((unused))) |
| 256 | { |
| 257 | int ret = 0; |
| 258 | struct mmap_mapping *mapping = addr; |
| 259 | int log_level; |
| 260 | |
| 261 | BT_ASSERT(mapping); |
| 262 | log_level = mapping->log_level; |
| 263 | mmap_lock(log_level); |
| 264 | |
| 265 | /* Check if the mapping exists in the hashtable. */ |
| 266 | if (!g_hash_table_lookup(mmap_mappings, addr)) { |
| 267 | _set_errno(EINVAL); |
| 268 | ret = -1; |
| 269 | goto end; |
| 270 | } |
| 271 | |
| 272 | /* Remove it. */ |
| 273 | if (!g_hash_table_remove(mmap_mappings, addr)) { |
| 274 | BT_LOGF_STR("Failed to remove mapping from hashtable."); |
| 275 | bt_common_abort(); |
| 276 | } |
| 277 | |
| 278 | end: |
| 279 | mmap_unlock(log_level); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | size_t bt_mmap_get_offset_align_size(int log_level) |
| 284 | { |
| 285 | SYSTEM_INFO sysinfo; |
| 286 | |
| 287 | GetNativeSystemInfo(&sysinfo); |
| 288 | BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_DEBUG, log_level, BT_LOG_TAG, |
| 289 | "Allocator granularity is %lu.", |
| 290 | sysinfo.dwAllocationGranularity); |
| 291 | |
| 292 | return sysinfo.dwAllocationGranularity; |
| 293 | } |
| 294 | |
| 295 | #endif |