2 * rawbuffer.c: Manages buffers that might have been mmapped or malloced
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
10 #if defined(PLATFORM_WIN32)
11 #define USE_WIN32_API 1
25 #include <sys/types.h>
27 #include "rawbuffer.h"
29 #include <mono/io-layer/io-layer.h>
31 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
32 #define ROUND_UP(VALUE,SIZE) (ROUND_DOWN((VALUE) + (SIZE) - 1, (SIZE)))
33 #if SIZEOF_VOID_P == 8
34 #define UINTPTR_TYPE guint64
36 #define UINTPTR_TYPE guint32
39 static GHashTable *mmap_map = NULL;
40 static size_t alignment = 0;
41 #define mono_mmap_lock() EnterCriticalSection (&mmap_mutex)
42 #define mono_mmap_unlock() LeaveCriticalSection (&mmap_mutex)
43 static CRITICAL_SECTION mmap_mutex;
44 static gboolean make_unreadable = FALSE;
45 static guint32 n_pagefaults = 0;
53 GetSystemInfo (&info);
54 alignment = info.dwAllocationGranularity;
56 alignment = getpagesize ();
61 mono_raw_buffer_load_malloc (int fd, int is_writable, guint32 base, size_t size)
65 ptr = g_malloc (size);
69 if (lseek (fd, base, 0) == (off_t) -1) {
79 mono_raw_buffer_free_malloc (void *base)
85 mono_raw_buffer_init (void)
87 InitializeCriticalSection (&mmap_mutex);
91 mmap_map = g_hash_table_new (NULL, NULL);
95 mono_raw_buffer_cleanup (void)
97 g_hash_table_destroy (mmap_map);
99 DeleteCriticalSection (&mmap_mutex);
103 mono_raw_buffer_load_mmap (int fd, int is_writable, guint32 base, size_t size)
106 /* FileMapping implementation */
111 HANDLE file, mapping;
113 start = ROUND_DOWN (base, alignment);
117 prot = PAGE_WRITECOPY;
118 access = FILE_MAP_COPY;
121 prot = PAGE_READONLY;
122 access = FILE_MAP_READ;
125 file = (HANDLE) _get_osfhandle (fd);
126 mapping = CreateFileMapping (file, NULL, prot, 0, 0, NULL);
130 ptr = MapViewOfFile (mapping, access, 0, start, end - start);
132 CloseHandle (mapping);
137 g_hash_table_insert (mmap_map, ptr, GINT_TO_POINTER (mapping));
140 return ((char *)ptr) + (base - start);
143 /* mmap implementation */
147 int prot = PROT_READ;
151 start = ROUND_DOWN (base, alignment);
152 end = ROUND_UP (base + size, alignment);
161 ptr = mmap (0, end - start, prot, flags, fd, start);
163 if (ptr == (void *) -1)
167 * This seems to prevent segmentation faults on Fedora Linux, no
169 * http://bugzilla.ximian.com/show_bug.cgi?id=49499
172 mprotect (ptr, end - start, prot | PROT_EXEC);
174 if (make_unreadable) {
175 int res = mprotect (ptr, end - start, 0);
180 g_hash_table_insert (mmap_map, ptr, GINT_TO_POINTER (size));
183 return ((char *)ptr) + (base - start);
188 mono_raw_buffer_free_mmap (void *base)
193 value = GPOINTER_TO_INT (g_hash_table_lookup (mmap_map, base));
197 UnmapViewOfFile (base);
198 CloseHandle ((HANDLE) value);
200 munmap (base, value);
205 mono_raw_buffer_update_mmap (void *base, size_t size)
208 FlushViewOfFile (base, size);
210 msync (base, size, MS_SYNC);
215 mono_raw_buffer_load (int fd, int is_writable, guint32 base, size_t size)
219 ptr = mono_raw_buffer_load_mmap (fd, is_writable, base, size);
221 ptr = mono_raw_buffer_load_malloc (fd, is_writable, base, size);
227 mono_raw_buffer_update (void *buffer, size_t size)
232 mmap_base = (gpointer)(ROUND_DOWN ((UINTPTR_TYPE) (buffer), alignment));
235 exists = g_hash_table_lookup (mmap_map, mmap_base) != NULL;
238 mono_raw_buffer_update_mmap (mmap_base, size);
242 mono_raw_buffer_free (void *buffer)
247 mmap_base = (gpointer)(ROUND_DOWN ((UINTPTR_TYPE) (buffer), alignment));
249 exists = g_hash_table_lookup (mmap_map, mmap_base) != NULL;
251 mono_raw_buffer_free_mmap (mmap_base);
253 mono_raw_buffer_free_malloc (buffer);
257 * mono_raw_buffer_set_make_unreadable:
259 * Set whenever to make all mmaped memory unreadable. In conjuction with a
260 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
263 mono_raw_buffer_set_make_unreadable (gboolean unreadable)
265 make_unreadable = unreadable;
274 find_map (void *start, guint32 size, gpointer user_data)
276 FindMapUserData *data = (FindMapUserData*)user_data;
279 if (((guint8*)data->ptr >= (guint8*)start) && ((guint8*)data->ptr < (guint8*)start + size))
284 * mono_raw_buffer_is_pagefault:
286 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
287 * within memory allocated by this module.
290 mono_raw_buffer_is_pagefault (void *ptr)
292 FindMapUserData data;
294 if (!make_unreadable)
301 g_hash_table_foreach (mmap_map, (GHFunc)find_map, &data);
308 * mono_raw_buffer_handle_pagefault:
310 * Handle a pagefault caused by an unreadable page by making it readable again.
313 mono_raw_buffer_handle_pagefault (void *ptr)
315 #ifndef PLATFORM_WIN32
316 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), alignment);
320 res = mprotect (start, alignment, PROT_READ);
329 * mono_raw_buffer_get_n_pagefaults:
331 * Return the number of times handle_pagefault is called.
332 * To count the number of pagefaults caused by a block of code use code like this:
334 * int prev_pagefaults = mono_raw_buffer_get_n_pagefaults ();
336 * int new_pagefaults = mono_raw_buffer_get_n_pagefaults () - prev_pagefaults;
339 mono_raw_buffer_get_n_pagefaults (void)