2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2013 JP Ikaheimonen <jp_ikaheimonen@mentor.com>
5 * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com>
7 * These sources are based on ftp://g.oswego.edu/pub/misc/malloc.c
8 * file by Doug Lea, released to the public domain.
11 #define BT_LOG_OUTPUT_LEVEL (mapping->log_level)
12 #define BT_LOG_TAG "COMPAT/MMAN"
13 #include "logging/log.h"
15 #include "common/macros.h"
16 #include "common/common.h"
20 * On macOS, we need a dummy symbol so that the linker won't
21 * complain of an empty table of contents.
24 int bt_mman_dummy_symbol
;
25 #endif /* __APPLE__ */
36 #include "compat/mman.h"
41 /* The duplicated handle. */
43 /* Handle returned by CreateFileMapping. */
48 GHashTable
*mmap_mappings
= NULL
;
51 * This mutex protects the hashtable of memory mappings.
53 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
56 struct mmap_mapping
*mapping_create(int log_level
)
58 struct mmap_mapping
*mapping
;
60 mapping
= malloc(sizeof(struct mmap_mapping
));
62 mapping
->file_handle
= NULL
;
63 mapping
->map_handle
= NULL
;
64 mapping
->log_level
= log_level
;
71 void mapping_clean(struct mmap_mapping
*mapping
)
74 if (!CloseHandle(mapping
->map_handle
)) {
75 BT_LOGF_STR("Failed to close mmap map_handle.");
78 if (!CloseHandle(mapping
->file_handle
)) {
79 BT_LOGF_STR("Failed to close mmap file_handle.");
88 void addr_clean(void *addr
)
90 /* Cleanup of handles should never fail. */
91 if (!UnmapViewOfFile(addr
)) {
93 * FIXME: We don't have access to the mapping's log
94 * level here, so force a FATAL level.
96 BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL
, BT_LOG_FATAL
, BT_LOG_TAG
,
97 "Failed to unmap mmap mapping.");
103 void mmap_lock(int log_level
)
105 if (pthread_mutex_lock(&mmap_mutex
)) {
106 BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL
, log_level
, BT_LOG_TAG
, "Failed to acquire mmap_mutex.");
112 void mmap_unlock(int log_level
)
114 if (pthread_mutex_unlock(&mmap_mutex
)) {
115 BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL
, log_level
, BT_LOG_TAG
, "Failed to release mmap_mutex.");
121 * Convert mmap memory protection flags to CreateFileMapping page protection
122 * flag and MapViewOfFile desired access flag.
125 DWORD
map_prot_flags(int prot
, DWORD
*dwDesiredAccess
)
127 if (prot
& PROT_READ
) {
128 if (prot
& PROT_WRITE
) {
129 *dwDesiredAccess
= FILE_MAP_WRITE
;
130 if (prot
& PROT_EXEC
) {
131 return PAGE_EXECUTE_READWRITE
;
133 return PAGE_READWRITE
;
135 if (prot
& PROT_EXEC
) {
136 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
137 return PAGE_EXECUTE_READ
;
139 *dwDesiredAccess
= FILE_MAP_READ
;
140 return PAGE_READONLY
;
142 if (prot
& PROT_WRITE
) {
143 *dwDesiredAccess
= FILE_MAP_COPY
;
144 return PAGE_WRITECOPY
;
146 if (prot
& PROT_EXEC
) {
147 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
148 return PAGE_EXECUTE_READ
;
151 /* Mapping failed. */
152 *dwDesiredAccess
= 0;
157 void *bt_mmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
158 off_t offset
, int log_level
)
160 struct mmap_mapping
*mapping
= NULL
;
162 DWORD dwDesiredAccess
;
166 /* Check for a valid fd. */
172 /* We don't support this at the moment. */
173 if (flags
== MAP_FIXED
) {
178 /* Map mmap flags to those of the Windows API. */
179 flProtect
= map_prot_flags(prot
, &dwDesiredAccess
);
180 if (flProtect
== 0) {
185 /* Allocate the mapping struct. */
186 mapping
= mapping_create(log_level
);
188 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR
, log_level
, BT_LOG_TAG
,
189 "Failed to allocate mmap mapping.");
194 /* Get a handle from the fd. */
195 handle
= (HANDLE
) _get_osfhandle(fd
);
197 /* Duplicate the handle and store it in 'mapping.file_handle'. */
198 if (!DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(),
199 &mapping
->file_handle
, 0, FALSE
,
200 DUPLICATE_SAME_ACCESS
)) {
206 * Create a file mapping object with a maximum size
207 * of 'offset' + 'length'.
209 mapping
->map_handle
= CreateFileMapping(mapping
->file_handle
, NULL
,
210 flProtect
, 0, offset
+ length
, NULL
);
211 if (mapping
->map_handle
== 0) {
216 /* Map the requested block starting at 'offset' for 'length' bytes. */
217 mapping_addr
= MapViewOfFile(mapping
->map_handle
, dwDesiredAccess
, 0,
219 if (mapping_addr
== 0) {
220 DWORD dwLastErr
= GetLastError();
221 if (dwLastErr
== ERROR_MAPPED_ALIGNMENT
) {
229 mmap_lock(log_level
);
231 /* If we have never done any mappings, allocate the hashtable. */
232 if (!mmap_mappings
) {
233 mmap_mappings
= g_hash_table_new_full(g_direct_hash
,
234 g_direct_equal
, (GDestroyNotify
) addr_clean
,
235 (GDestroyNotify
) mapping_clean
);
236 if (!mmap_mappings
) {
237 BT_LOGE_STR("Failed to allocate mmap hashtable.");
239 goto error_mutex_unlock
;
243 /* Add the new mapping to the hashtable. */
244 g_hash_table_insert(mmap_mappings
, mapping_addr
, mapping
);
246 mmap_unlock(log_level
);
251 mmap_unlock(log_level
);
253 mapping_clean(mapping
);
258 int bt_munmap(void *addr
, size_t length
)
261 struct mmap_mapping
*mapping
= addr
;
265 log_level
= mapping
->log_level
;
266 mmap_lock(log_level
);
268 /* Check if the mapping exists in the hashtable. */
269 if (!g_hash_table_lookup(mmap_mappings
, addr
)) {
276 if (!g_hash_table_remove(mmap_mappings
, addr
)) {
277 BT_LOGF_STR("Failed to remove mapping from hashtable.");
282 mmap_unlock(log_level
);
287 size_t bt_mmap_get_offset_align_size(int log_level
)
291 GetNativeSystemInfo(&sysinfo
);
292 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, log_level
, BT_LOG_TAG
,
293 "Allocator granularity is %lu.",
294 sysinfo
.dwAllocationGranularity
);
296 return sysinfo
.dwAllocationGranularity
;