2 * file-mmap-posix.c: File mmap internal calls
7 * Copyright 2014 Xamarin Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #ifdef HAVE_SYS_STAT_H
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
34 #include <mono/metadata/object.h>
35 #include <mono/metadata/file-io.h>
36 #include <mono/metadata/file-mmap.h>
37 #include <mono/utils/atomic.h>
38 #include <mono/utils/mono-memory-model.h>
39 #include <mono/utils/mono-mmap.h>
40 #include <mono/utils/mono-coop-mutex.h>
41 #include <mono/utils/mono-threads.h>
58 BAD_CAPACITY_FOR_FILE_BACKED = 1,
59 CAPACITY_SMALLER_THAN_FILE_SIZE,
64 CAPACITY_MUST_BE_POSITIVE,
70 FILE_MODE_CREATE_NEW = 1,
73 FILE_MODE_OPEN_OR_CREATE = 4,
74 FILE_MODE_TRUNCATE = 5,
79 MMAP_FILE_ACCESS_READ_WRITE = 0,
80 MMAP_FILE_ACCESS_READ = 1,
81 MMAP_FILE_ACCESS_WRITE = 2,
82 MMAP_FILE_ACCESS_COPY_ON_WRITE = 3,
83 MMAP_FILE_ACCESS_READ_EXECUTE = 4,
84 MMAP_FILE_ACCESS_READ_WRITE_EXECUTE = 5,
88 #define DEFAULT_FILEMODE DEFFILEMODE
90 #define DEFAULT_FILEMODE 0666
93 static int mmap_init_state;
94 static MonoCoopMutex named_regions_mutex;
95 static GHashTable *named_regions;
99 align_up_to_page_size (gint64 size)
101 gint64 page_size = mono_pagesize ();
102 return (size + page_size - 1) & ~(page_size - 1);
106 align_down_to_page_size (gint64 size)
108 gint64 page_size = mono_pagesize ();
109 return size & ~(page_size - 1);
113 file_mmap_init (void)
116 switch (mmap_init_state) {
118 if (InterlockedCompareExchange (&mmap_init_state, 1, 0) != 0)
120 named_regions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
121 mono_coop_mutex_init (&named_regions_mutex);
123 mono_atomic_store_release (&mmap_init_state, 2);
128 mono_thread_info_sleep (1, NULL); /* Been init'd by other threads, this is very rare. */
129 } while (mmap_init_state != 2);
134 g_error ("Invalid init state %d", mmap_init_state);
139 named_regions_lock (void)
142 mono_coop_mutex_lock (&named_regions_mutex);
146 named_regions_unlock (void)
148 mono_coop_mutex_unlock (&named_regions_mutex);
153 file_mode_to_unix (int mode)
156 case FILE_MODE_CREATE_NEW:
157 return O_CREAT | O_EXCL;
158 case FILE_MODE_CREATE:
159 return O_CREAT | O_TRUNC;
162 case FILE_MODE_OPEN_OR_CREATE:
164 case FILE_MODE_TRUNCATE:
166 case FILE_MODE_APPEND:
169 g_error ("unknown FileMode %d", mode);
174 access_mode_to_unix (int access)
177 case MMAP_FILE_ACCESS_READ_WRITE:
178 case MMAP_FILE_ACCESS_COPY_ON_WRITE:
179 case MMAP_FILE_ACCESS_READ_WRITE_EXECUTE:
181 case MMAP_FILE_ACCESS_READ:
182 case MMAP_FILE_ACCESS_READ_EXECUTE:
184 case MMAP_FILE_ACCESS_WRITE:
187 g_error ("unknown MemoryMappedFileAccess %d", access);
192 acess_to_mmap_flags (int access)
195 case MMAP_FILE_ACCESS_READ_WRITE:
196 return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_SHARED;
198 case MMAP_FILE_ACCESS_WRITE:
199 return MONO_MMAP_WRITE | MONO_MMAP_SHARED;
201 case MMAP_FILE_ACCESS_COPY_ON_WRITE:
202 return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_PRIVATE;
204 case MMAP_FILE_ACCESS_READ_EXECUTE:
205 return MONO_MMAP_EXEC | MONO_MMAP_PRIVATE | MONO_MMAP_SHARED;
207 case MMAP_FILE_ACCESS_READ_WRITE_EXECUTE:
208 return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_EXEC | MONO_MMAP_SHARED;
210 case MMAP_FILE_ACCESS_READ:
211 return MONO_MMAP_READ | MONO_MMAP_SHARED;
213 g_error ("unknown MemoryMappedFileAccess %d", access);
218 This allow us to special case zero size files that can be arbitrarily mapped.
221 is_special_zero_size_file (struct stat *buf)
223 return buf->st_size == 0 && (buf->st_mode & (S_IFCHR | S_IFBLK | S_IFIFO | S_IFSOCK)) != 0;
227 XXX implement options
230 open_file_map (MonoString *path, int input_fd, int mode, gint64 *capacity, int access, int options, int *ioerror)
235 MmapHandle *handle = NULL;
239 c_path = mono_string_to_utf8_checked (path, &error);
240 mono_error_raise_exception (&error); /* FIXME don't raise here */
244 result = stat (c_path, &buf);
246 result = fstat (input_fd, &buf);
248 if (mode == FILE_MODE_TRUNCATE || mode == FILE_MODE_APPEND || mode == FILE_MODE_OPEN) {
249 if (result == -1) { //XXX translate errno?
250 *ioerror = FILE_NOT_FOUND;
255 if (mode == FILE_MODE_CREATE_NEW && result == 0) {
256 *ioerror = FILE_ALREADY_EXISTS;
261 if (*capacity == 0) {
263 * Special files such as FIFOs, sockets, and devices can have a size of 0. Specifying a capacity for these
264 * also makes little sense, so don't do the check if th file is one of these.
266 if (buf.st_size == 0 && !is_special_zero_size_file (&buf)) {
267 *ioerror = CAPACITY_SMALLER_THAN_FILE_SIZE;
270 *capacity = buf.st_size;
271 } else if (*capacity < buf.st_size) {
272 *ioerror = CAPACITY_SMALLER_THAN_FILE_SIZE;
276 if (mode == FILE_MODE_CREATE_NEW && *capacity == 0) {
277 *ioerror = CAPACITY_SMALLER_THAN_FILE_SIZE;
282 if (path) //FIXME use io portability?
283 fd = open (c_path, file_mode_to_unix (mode) | access_mode_to_unix (access), DEFAULT_FILEMODE);
287 if (fd == -1) { //XXX translate errno?
288 *ioerror = COULD_NOT_OPEN;
292 if (result != 0 || *capacity > buf.st_size) {
293 int unused G_GNUC_UNUSED = ftruncate (fd, (off_t)*capacity);
296 handle = g_new0 (MmapHandle, 1);
297 handle->ref_count = 1;
298 handle->capacity = *capacity;
303 return (void*)handle;
306 #define MONO_ANON_FILE_TEMPLATE "/mono.anonmap.XXXXXXXXX"
308 open_memory_map (MonoString *mapName, int mode, gint64 *capacity, int access, int options, int *ioerror)
313 if (*capacity <= 1) {
314 *ioerror = CAPACITY_MUST_BE_POSITIVE;
318 if (!(mode == FILE_MODE_CREATE_NEW || mode == FILE_MODE_OPEN_OR_CREATE || mode == FILE_MODE_OPEN)) {
319 *ioerror = INVALID_FILE_MODE;
323 c_mapName = mono_string_to_utf8_checked (mapName, &error);
324 mono_error_raise_exception (&error); /* FIXME don't raise here */
326 named_regions_lock ();
327 handle = (MmapHandle*)g_hash_table_lookup (named_regions, c_mapName);
329 if (mode == FILE_MODE_CREATE_NEW) {
330 *ioerror = FILE_ALREADY_EXISTS;
335 //XXX should we ftruncate if the file is smaller than capacity?
340 int unused G_GNUC_UNUSED, alloc_size;
342 if (mode == FILE_MODE_OPEN) {
343 *ioerror = FILE_NOT_FOUND;
346 *capacity = align_up_to_page_size (*capacity);
348 tmp_dir = g_get_tmp_dir ();
349 alloc_size = strlen (tmp_dir) + strlen (MONO_ANON_FILE_TEMPLATE) + 1;
350 if (alloc_size > 1024) {//rather fail that stack overflow
351 *ioerror = COULD_NOT_MAP_MEMORY;
354 file_name = (char *)alloca (alloc_size);
355 strcpy (file_name, tmp_dir);
356 strcat (file_name, MONO_ANON_FILE_TEMPLATE);
358 fd = mkstemp (file_name);
360 *ioerror = COULD_NOT_MAP_MEMORY;
365 unused = ftruncate (fd, (off_t)*capacity);
367 handle = g_new0 (MmapHandle, 1);
368 handle->ref_count = 1;
369 handle->capacity = *capacity;
371 handle->name = g_strdup (c_mapName);
373 g_hash_table_insert (named_regions, handle->name, handle);
378 named_regions_unlock ();
386 mono_mmap_open_file (MonoString *path, int mode, MonoString *mapName, gint64 *capacity, int access, int options, int *ioerror)
389 g_assert (path || mapName);
392 return open_file_map (path, -1, mode, capacity, access, options, ioerror);
396 char *c_mapName = mono_string_to_utf8_checked (mapName, &error);
397 mono_error_raise_exception (&error); /* FIXME don't raise here */
399 named_regions_lock ();
400 handle = (MmapHandle*)g_hash_table_lookup (named_regions, c_mapName);
402 *ioerror = FILE_ALREADY_EXISTS;
405 handle = (MmapHandle *)open_file_map (path, -1, mode, capacity, access, options, ioerror);
407 handle->name = g_strdup (c_mapName);
408 g_hash_table_insert (named_regions, handle->name, handle);
411 named_regions_unlock ();
417 return open_memory_map (mapName, mode, capacity, access, options, ioerror);
421 mono_mmap_open_handle (void *input_fd, MonoString *mapName, gint64 *capacity, int access, int options, int *ioerror)
426 handle = (MmapHandle *)open_file_map (NULL, GPOINTER_TO_INT (input_fd), FILE_MODE_OPEN, capacity, access, options, ioerror);
428 char *c_mapName = mono_string_to_utf8_checked (mapName, &error);
429 mono_error_raise_exception (&error); /* FIXME don't raise here */
431 named_regions_lock ();
432 handle = (MmapHandle*)g_hash_table_lookup (named_regions, c_mapName);
434 *ioerror = FILE_ALREADY_EXISTS;
437 //XXX we're exploiting wapi HANDLE == FD equivalence. THIS IS FRAGILE, create a _wapi_handle_to_fd call
438 handle = (MmapHandle *)open_file_map (NULL, GPOINTER_TO_INT (input_fd), FILE_MODE_OPEN, capacity, access, options, ioerror);
439 handle->name = g_strdup (c_mapName);
440 g_hash_table_insert (named_regions, handle->name, handle);
442 named_regions_unlock ();
450 mono_mmap_close (void *mmap_handle)
452 MmapHandle *handle = (MmapHandle *)mmap_handle;
454 named_regions_lock ();
456 if (handle->ref_count == 0) {
458 g_hash_table_remove (named_regions, handle->name);
460 g_free (handle->name);
464 named_regions_unlock ();
468 mono_mmap_configure_inheritability (void *mmap_handle, gboolean inheritability)
470 MmapHandle *h = (MmapHandle *)mmap_handle;
474 flags = fcntl (fd, F_GETFD, 0);
476 flags &= ~FD_CLOEXEC;
479 fcntl (fd, F_SETFD, flags);
483 mono_mmap_flush (void *mmap_handle)
485 MmapInstance *h = (MmapInstance *)mmap_handle;
488 msync (h->address, h->length, MS_SYNC);
492 mono_mmap_map (void *handle, gint64 offset, gint64 *size, int access, void **mmap_handle, void **base_address)
494 gint64 mmap_offset = 0;
495 MmapHandle *fh = (MmapHandle *)handle;
496 MmapInstance res = { 0 };
497 size_t eff_size = *size;
498 struct stat buf = { 0 };
499 fstat (fh->fd, &buf); //FIXME error handling
501 if (offset > buf.st_size || ((eff_size + offset) > buf.st_size && !is_special_zero_size_file (&buf)))
504 * We use the file size if one of the following conditions is true:
505 * -input size is zero
506 * -input size is bigger than the file and the file is not a magical zero size file such as /dev/mem.
509 eff_size = align_up_to_page_size (buf.st_size) - offset;
512 mmap_offset = align_down_to_page_size (offset);
513 eff_size += (offset - mmap_offset);
514 //FIXME translate some interesting errno values
515 res.address = mono_file_map ((size_t)eff_size, acess_to_mmap_flags (access), fh->fd, mmap_offset, &res.free_handle);
516 res.length = eff_size;
519 *mmap_handle = g_memdup (&res, sizeof (MmapInstance));
520 *base_address = (char*)res.address + (offset - mmap_offset);
526 *base_address = NULL;
527 return COULD_NOT_MAP_MEMORY;
531 mono_mmap_unmap (void *mmap_handle)
534 MmapInstance *h = (MmapInstance *)mmap_handle;
536 res = mono_file_unmap (h->address, h->free_handle);