shlwapi: SHMapHandle should not set error when NULL is passed as hShared.
[wine.git] / server / mapping.c
blobddc8be890f397119b9939ab329efeb73646b7aad
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
32 #include <unistd.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "security.h"
46 /* list of memory ranges, used to store committed info */
47 struct ranges
49 struct object obj; /* object header */
50 unsigned int count; /* number of used ranges */
51 unsigned int max; /* number of allocated ranges */
52 struct range
54 file_pos_t start;
55 file_pos_t end;
56 } *ranges;
59 static void ranges_dump( struct object *obj, int verbose );
60 static void ranges_destroy( struct object *obj );
62 static const struct object_ops ranges_ops =
64 sizeof(struct ranges), /* size */
65 ranges_dump, /* dump */
66 no_get_type, /* get_type */
67 no_add_queue, /* add_queue */
68 NULL, /* remove_queue */
69 NULL, /* signaled */
70 NULL, /* satisfied */
71 no_signal, /* signal */
72 no_get_fd, /* get_fd */
73 no_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_lookup_name, /* lookup_name */
77 no_link_name, /* link_name */
78 NULL, /* unlink_name */
79 no_open_file, /* open_file */
80 no_kernel_obj_list, /* get_kernel_obj_list */
81 no_close_handle, /* close_handle */
82 ranges_destroy /* destroy */
85 /* file backing the shared sections of a PE image mapping */
86 struct shared_map
88 struct object obj; /* object header */
89 struct fd *fd; /* file descriptor of the mapped PE file */
90 struct file *file; /* temp file holding the shared data */
91 struct list entry; /* entry in global shared maps list */
94 static void shared_map_dump( struct object *obj, int verbose );
95 static void shared_map_destroy( struct object *obj );
97 static const struct object_ops shared_map_ops =
99 sizeof(struct shared_map), /* size */
100 shared_map_dump, /* dump */
101 no_get_type, /* get_type */
102 no_add_queue, /* add_queue */
103 NULL, /* remove_queue */
104 NULL, /* signaled */
105 NULL, /* satisfied */
106 no_signal, /* signal */
107 no_get_fd, /* get_fd */
108 no_map_access, /* map_access */
109 default_get_sd, /* get_sd */
110 default_set_sd, /* set_sd */
111 no_lookup_name, /* lookup_name */
112 no_link_name, /* link_name */
113 NULL, /* unlink_name */
114 no_open_file, /* open_file */
115 no_kernel_obj_list, /* get_kernel_obj_list */
116 no_close_handle, /* close_handle */
117 shared_map_destroy /* destroy */
120 static struct list shared_map_list = LIST_INIT( shared_map_list );
122 /* memory view mapped in client address space */
123 struct memory_view
125 struct list entry; /* entry in per-process view list */
126 struct fd *fd; /* fd for mapped file */
127 struct ranges *committed; /* list of committed ranges in this mapping */
128 struct shared_map *shared; /* temp file for shared PE mapping */
129 unsigned int flags; /* SEC_* flags */
130 client_ptr_t base; /* view base address (in process addr space) */
131 mem_size_t size; /* view size */
132 file_pos_t start; /* start offset in mapping */
135 struct mapping
137 struct object obj; /* object header */
138 mem_size_t size; /* mapping size */
139 unsigned int flags; /* SEC_* flags */
140 struct fd *fd; /* fd for mapped file */
141 pe_image_info_t image; /* image info (for PE image mapping) */
142 struct ranges *committed; /* list of committed ranges in this mapping */
143 struct shared_map *shared; /* temp file for shared PE mapping */
146 static void mapping_dump( struct object *obj, int verbose );
147 static struct object_type *mapping_get_type( struct object *obj );
148 static struct fd *mapping_get_fd( struct object *obj );
149 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
150 static void mapping_destroy( struct object *obj );
151 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
153 static const struct object_ops mapping_ops =
155 sizeof(struct mapping), /* size */
156 mapping_dump, /* dump */
157 mapping_get_type, /* get_type */
158 no_add_queue, /* add_queue */
159 NULL, /* remove_queue */
160 NULL, /* signaled */
161 NULL, /* satisfied */
162 no_signal, /* signal */
163 mapping_get_fd, /* get_fd */
164 mapping_map_access, /* map_access */
165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
167 no_lookup_name, /* lookup_name */
168 directory_link_name, /* link_name */
169 default_unlink_name, /* unlink_name */
170 no_open_file, /* open_file */
171 no_kernel_obj_list, /* get_kernel_obj_list */
172 fd_close_handle, /* close_handle */
173 mapping_destroy /* destroy */
176 static const struct fd_ops mapping_fd_ops =
178 default_fd_get_poll_events, /* get_poll_events */
179 default_poll_event, /* poll_event */
180 mapping_get_fd_type, /* get_fd_type */
181 no_fd_read, /* read */
182 no_fd_write, /* write */
183 no_fd_flush, /* flush */
184 no_fd_get_file_info, /* get_file_info */
185 no_fd_get_volume_info, /* get_volume_info */
186 no_fd_ioctl, /* ioctl */
187 no_fd_queue_async, /* queue_async */
188 default_fd_reselect_async /* reselect_async */
191 static size_t page_mask;
193 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
196 static void ranges_dump( struct object *obj, int verbose )
198 struct ranges *ranges = (struct ranges *)obj;
199 fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
202 static void ranges_destroy( struct object *obj )
204 struct ranges *ranges = (struct ranges *)obj;
205 free( ranges->ranges );
208 static void shared_map_dump( struct object *obj, int verbose )
210 struct shared_map *shared = (struct shared_map *)obj;
211 fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
214 static void shared_map_destroy( struct object *obj )
216 struct shared_map *shared = (struct shared_map *)obj;
218 release_object( shared->fd );
219 release_object( shared->file );
220 list_remove( &shared->entry );
223 /* extend a file beyond the current end of file */
224 static int grow_file( int unix_fd, file_pos_t new_size )
226 static const char zero;
227 off_t size = new_size;
229 if (sizeof(new_size) > sizeof(size) && size != new_size)
231 set_error( STATUS_INVALID_PARAMETER );
232 return 0;
234 /* extend the file one byte beyond the requested size and then truncate it */
235 /* this should work around ftruncate implementations that can't extend files */
236 if (pwrite( unix_fd, &zero, 1, size ) != -1)
238 ftruncate( unix_fd, size );
239 return 1;
241 file_set_error();
242 return 0;
245 /* check if the current directory allows exec mappings */
246 static int check_current_dir_for_exec(void)
248 int fd;
249 char tmpfn[] = "anonmap.XXXXXX";
250 void *ret = MAP_FAILED;
252 fd = mkstemps( tmpfn, 0 );
253 if (fd == -1) return 0;
254 if (grow_file( fd, 1 ))
256 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
257 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
259 close( fd );
260 unlink( tmpfn );
261 return (ret != MAP_FAILED);
264 /* create a temp file for anonymous mappings */
265 static int create_temp_file( file_pos_t size )
267 static int temp_dir_fd = -1;
268 char tmpfn[] = "anonmap.XXXXXX";
269 int fd;
271 if (temp_dir_fd == -1)
273 temp_dir_fd = server_dir_fd;
274 if (!check_current_dir_for_exec())
276 /* the server dir is noexec, try the config dir instead */
277 fchdir( config_dir_fd );
278 if (check_current_dir_for_exec())
279 temp_dir_fd = config_dir_fd;
280 else /* neither works, fall back to server dir */
281 fchdir( server_dir_fd );
284 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
286 fd = mkstemps( tmpfn, 0 );
287 if (fd != -1)
289 if (!grow_file( fd, size ))
291 close( fd );
292 fd = -1;
294 unlink( tmpfn );
296 else file_set_error();
298 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
299 return fd;
302 /* find a memory view from its base address */
303 static struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
305 struct memory_view *view;
307 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
308 if (view->base == base) return view;
310 set_error( STATUS_NOT_MAPPED_VIEW );
311 return NULL;
314 static void free_memory_view( struct memory_view *view )
316 if (view->fd) release_object( view->fd );
317 if (view->committed) release_object( view->committed );
318 if (view->shared) release_object( view->shared );
319 list_remove( &view->entry );
320 free( view );
323 /* free all mapped views at process exit */
324 void free_mapped_views( struct process *process )
326 struct list *ptr;
328 while ((ptr = list_head( &process->views )))
329 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
332 /* find the shared PE mapping for a given mapping */
333 static struct shared_map *get_shared_file( struct fd *fd )
335 struct shared_map *ptr;
337 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
338 if (is_same_file_fd( ptr->fd, fd ))
339 return (struct shared_map *)grab_object( ptr );
340 return NULL;
343 /* return the size of the memory mapping and file range of a given section */
344 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
345 off_t *file_start, size_t *file_size )
347 static const unsigned int sector_align = 0x1ff;
349 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
350 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
352 *file_start = sec->PointerToRawData & ~sector_align;
353 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
354 if (*file_size > *map_size) *file_size = *map_size;
357 /* add a range to the committed list */
358 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
360 unsigned int i, j;
361 struct ranges *committed = view->committed;
362 struct range *ranges;
364 if ((start & page_mask) || (end & page_mask) ||
365 start >= view->size || end >= view->size ||
366 start >= end)
368 set_error( STATUS_INVALID_PARAMETER );
369 return;
372 if (!committed) return; /* everything committed already */
374 start += view->start;
375 end += view->start;
377 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
379 if (ranges[i].start > end) break;
380 if (ranges[i].end < start) continue;
381 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
382 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
384 for (j = i + 1; j < committed->count; j++)
386 if (ranges[j].start > end) break;
387 if (ranges[j].end > end) end = ranges[j].end;
389 if (j > i + 1)
391 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
392 committed->count -= j - (i + 1);
394 ranges[i].end = end;
396 return;
399 /* now add a new range */
401 if (committed->count == committed->max)
403 unsigned int new_size = committed->max * 2;
404 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
405 if (!new_ptr) return;
406 committed->max = new_size;
407 committed->ranges = new_ptr;
409 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
410 ranges[i].start = start;
411 ranges[i].end = end;
412 committed->count++;
415 /* find the range containing start and return whether it's committed */
416 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
418 unsigned int i;
419 struct ranges *committed = view->committed;
420 struct range *ranges;
422 if ((start & page_mask) || start >= view->size)
424 set_error( STATUS_INVALID_PARAMETER );
425 return 0;
427 if (!committed) /* everything is committed */
429 *size = view->size - start;
430 return 1;
432 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
434 if (ranges[i].start > view->start + start)
436 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
437 return 0;
439 if (ranges[i].end > view->start + start)
441 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
442 return 1;
445 *size = view->size - start;
446 return 0;
449 /* allocate and fill the temp file for a shared PE image mapping */
450 static int build_shared_mapping( struct mapping *mapping, int fd,
451 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
453 struct shared_map *shared;
454 struct file *file;
455 unsigned int i;
456 mem_size_t total_size;
457 size_t file_size, map_size, max_size;
458 off_t shared_pos, read_pos, write_pos;
459 char *buffer = NULL;
460 int shared_fd;
461 long toread;
463 /* compute the total size of the shared mapping */
465 total_size = max_size = 0;
466 for (i = 0; i < nb_sec; i++)
468 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
469 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
471 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
472 if (file_size > max_size) max_size = file_size;
473 total_size += map_size;
476 if (!total_size) return 1; /* nothing to do */
478 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
480 /* create a temp file for the mapping */
482 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
483 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
485 if (!(buffer = malloc( max_size ))) goto error;
487 /* copy the shared sections data into the temp file */
489 shared_pos = 0;
490 for (i = 0; i < nb_sec; i++)
492 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
493 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
494 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
495 write_pos = shared_pos;
496 shared_pos += map_size;
497 if (!sec[i].PointerToRawData || !file_size) continue;
498 toread = file_size;
499 while (toread)
501 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
502 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
504 file_size -= toread;
505 break;
507 if (res <= 0) goto error;
508 toread -= res;
509 read_pos += res;
511 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
514 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
515 shared->fd = (struct fd *)grab_object( mapping->fd );
516 shared->file = file;
517 list_add_head( &shared_map_list, &shared->entry );
518 mapping->shared = shared;
519 free( buffer );
520 return 1;
522 error:
523 release_object( file );
524 free( buffer );
525 return 0;
528 /* load the CLR header from its section */
529 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
530 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
532 ssize_t ret;
533 size_t map_size, file_size;
534 off_t file_start;
535 unsigned int i;
537 if (!va || !size) return 0;
539 for (i = 0; i < nb_sec; i++)
541 if (va < sec[i].VirtualAddress) continue;
542 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
543 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
544 if (size >= map_size) continue;
545 if (va - sec[i].VirtualAddress >= map_size - size) continue;
546 file_size = min( file_size, map_size );
547 size = min( size, sizeof(*hdr) );
548 ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
549 if (ret <= 0) break;
550 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
551 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
552 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
553 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
555 return 0;
558 /* retrieve the mapping parameters for an executable (PE) image */
559 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
561 static const char fakedll_signature[] = "Wine placeholder DLL";
563 IMAGE_COR20_HEADER clr;
564 IMAGE_SECTION_HEADER sec[96];
565 struct
567 IMAGE_DOS_HEADER dos;
568 char buffer[sizeof(fakedll_signature)];
569 } mz;
570 struct
572 DWORD Signature;
573 IMAGE_FILE_HEADER FileHeader;
574 union
576 IMAGE_OPTIONAL_HEADER32 hdr32;
577 IMAGE_OPTIONAL_HEADER64 hdr64;
578 } opt;
579 } nt;
580 off_t pos;
581 int size;
582 size_t mz_size, clr_va, clr_size;
583 unsigned int i, cpu_mask = get_supported_cpu_mask();
585 /* load the headers */
587 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
588 size = pread( unix_fd, &mz, sizeof(mz), 0 );
589 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
590 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
591 mz_size = size;
592 pos = mz.dos.e_lfanew;
594 size = pread( unix_fd, &nt, sizeof(nt), pos );
595 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
596 /* zero out Optional header in the case it's not present or partial */
597 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader );
598 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
599 if (nt.Signature != IMAGE_NT_SIGNATURE)
601 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
602 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
603 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
604 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
605 return STATUS_INVALID_IMAGE_NE_FORMAT;
608 switch (nt.opt.hdr32.Magic)
610 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
611 switch (nt.FileHeader.Machine)
613 case IMAGE_FILE_MACHINE_I386:
614 mapping->image.cpu = CPU_x86;
615 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
616 return STATUS_INVALID_IMAGE_FORMAT;
617 case IMAGE_FILE_MACHINE_ARM:
618 case IMAGE_FILE_MACHINE_THUMB:
619 case IMAGE_FILE_MACHINE_ARMNT:
620 mapping->image.cpu = CPU_ARM;
621 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
622 return STATUS_INVALID_IMAGE_FORMAT;
623 case IMAGE_FILE_MACHINE_POWERPC:
624 mapping->image.cpu = CPU_POWERPC;
625 if (cpu_mask & CPU_FLAG(CPU_POWERPC)) break;
626 return STATUS_INVALID_IMAGE_FORMAT;
627 default:
628 return STATUS_INVALID_IMAGE_FORMAT;
630 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
631 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
633 mapping->image.base = nt.opt.hdr32.ImageBase;
634 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
635 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
636 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
637 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
638 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
639 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
640 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
641 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
642 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
643 nt.opt.hdr32.AddressOfEntryPoint ||
644 nt.opt.hdr32.SectionAlignment & page_mask);
645 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
646 mapping->image.checksum = nt.opt.hdr32.CheckSum;
647 mapping->image.image_flags = 0;
648 if (nt.opt.hdr32.SectionAlignment & page_mask)
649 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
650 if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
651 mapping->image.contains_code && !(clr_va && clr_size))
652 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
653 break;
655 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
656 if (!(cpu_mask & CPU_64BIT_MASK)) return STATUS_INVALID_IMAGE_WIN_64;
657 switch (nt.FileHeader.Machine)
659 case IMAGE_FILE_MACHINE_AMD64:
660 mapping->image.cpu = CPU_x86_64;
661 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
662 return STATUS_INVALID_IMAGE_FORMAT;
663 case IMAGE_FILE_MACHINE_ARM64:
664 mapping->image.cpu = CPU_ARM64;
665 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
666 return STATUS_INVALID_IMAGE_FORMAT;
667 default:
668 return STATUS_INVALID_IMAGE_FORMAT;
670 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
671 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
673 mapping->image.base = nt.opt.hdr64.ImageBase;
674 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
675 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
676 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
677 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
678 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
679 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
680 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
681 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
682 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
683 nt.opt.hdr64.AddressOfEntryPoint ||
684 nt.opt.hdr64.SectionAlignment & page_mask);
685 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
686 mapping->image.checksum = nt.opt.hdr64.CheckSum;
687 mapping->image.image_flags = 0;
688 if (nt.opt.hdr64.SectionAlignment & page_mask)
689 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
690 if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
691 mapping->image.contains_code && !(clr_va && clr_size))
692 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
693 break;
695 default:
696 return STATUS_INVALID_IMAGE_FORMAT;
699 mapping->image.image_charact = nt.FileHeader.Characteristics;
700 mapping->image.machine = nt.FileHeader.Machine;
701 mapping->image.zerobits = 0; /* FIXME */
702 mapping->image.gp = 0; /* FIXME */
703 mapping->image.file_size = file_size;
704 mapping->image.loader_flags = clr_va && clr_size;
705 if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
706 mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
708 /* load the section headers */
710 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
711 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
712 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
713 if (!mapping->size) mapping->size = mapping->image.map_size;
714 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
715 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
716 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
717 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
719 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
720 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
722 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
723 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
725 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
726 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
727 !(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
729 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
730 if (cpu_mask & CPU_FLAG(CPU_x86_64)) mapping->image.cpu = CPU_x86_64;
731 else if (cpu_mask & CPU_FLAG(CPU_ARM64)) mapping->image.cpu = CPU_ARM64;
735 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
736 return STATUS_INVALID_FILE_FOR_SECTION;
738 return STATUS_SUCCESS;
741 static struct ranges *create_ranges(void)
743 struct ranges *ranges = alloc_object( &ranges_ops );
745 if (!ranges) return NULL;
746 ranges->count = 0;
747 ranges->max = 8;
748 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
750 release_object( ranges );
751 return NULL;
753 return ranges;
756 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
758 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
760 case SEC_IMAGE:
761 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
762 if (handle) return SEC_FILE | SEC_IMAGE;
763 set_error( STATUS_INVALID_FILE_FOR_SECTION );
764 return 0;
765 case SEC_COMMIT:
766 if (!handle) return flags;
767 /* fall through */
768 case SEC_RESERVE:
769 if (flags & SEC_LARGE_PAGES) break;
770 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
771 return flags;
773 set_error( STATUS_INVALID_PARAMETER );
774 return 0;
778 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
779 unsigned int attr, mem_size_t size, unsigned int flags,
780 obj_handle_t handle, unsigned int file_access,
781 const struct security_descriptor *sd )
783 struct mapping *mapping;
784 struct file *file;
785 struct fd *fd;
786 int unix_fd;
787 struct stat st;
789 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
791 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
792 return NULL;
793 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
794 return &mapping->obj; /* Nothing else to do */
796 mapping->size = size;
797 mapping->fd = NULL;
798 mapping->shared = NULL;
799 mapping->committed = NULL;
801 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
803 if (handle)
805 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
806 unsigned int mapping_access = FILE_MAPPING_ACCESS;
808 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
809 fd = get_obj_fd( (struct object *)file );
811 /* file sharing rules for mappings are different so we use magic the access rights */
812 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
813 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
815 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
817 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
818 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
820 release_object( file );
821 release_object( fd );
822 if (!mapping->fd) goto error;
824 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
825 if (fstat( unix_fd, &st ) == -1)
827 file_set_error();
828 goto error;
830 if (flags & SEC_IMAGE)
832 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
833 if (!err) return &mapping->obj;
834 set_error( err );
835 goto error;
837 if (!mapping->size)
839 if (!(mapping->size = st.st_size))
841 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
842 goto error;
845 else if (st.st_size < mapping->size)
847 if (!(file_access & FILE_WRITE_DATA))
849 set_error( STATUS_SECTION_TOO_BIG );
850 goto error;
852 if (!grow_file( unix_fd, mapping->size )) goto error;
855 else /* Anonymous mapping (no associated file) */
857 if (!mapping->size)
859 set_error( STATUS_INVALID_PARAMETER );
860 goto error;
862 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
863 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
864 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
865 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
866 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
867 allow_fd_caching( mapping->fd );
869 return &mapping->obj;
871 error:
872 release_object( mapping );
873 return NULL;
876 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
878 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
881 /* open a new file for the file descriptor backing the mapping */
882 struct file *get_mapping_file( struct process *process, client_ptr_t base,
883 unsigned int access, unsigned int sharing )
885 struct memory_view *view = find_mapped_view( process, base );
887 if (!view || !view->fd) return NULL;
888 return create_file_for_fd_obj( view->fd, access, sharing );
891 static void mapping_dump( struct object *obj, int verbose )
893 struct mapping *mapping = (struct mapping *)obj;
894 assert( obj->ops == &mapping_ops );
895 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
896 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
897 mapping->flags, mapping->fd, mapping->shared );
900 static struct object_type *mapping_get_type( struct object *obj )
902 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
903 static const struct unicode_str str = { name, sizeof(name) };
904 return get_object_type( &str );
907 static struct fd *mapping_get_fd( struct object *obj )
909 struct mapping *mapping = (struct mapping *)obj;
910 return (struct fd *)grab_object( mapping->fd );
913 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
915 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
916 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
917 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
918 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
919 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
922 static void mapping_destroy( struct object *obj )
924 struct mapping *mapping = (struct mapping *)obj;
925 assert( obj->ops == &mapping_ops );
926 if (mapping->fd) release_object( mapping->fd );
927 if (mapping->committed) release_object( mapping->committed );
928 if (mapping->shared) release_object( mapping->shared );
931 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
933 return FD_TYPE_FILE;
936 int get_page_size(void)
938 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
939 return page_mask + 1;
942 /* create a file mapping */
943 DECL_HANDLER(create_mapping)
945 struct object *root, *obj;
946 struct unicode_str name;
947 const struct security_descriptor *sd;
948 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
950 if (!objattr) return;
952 if ((obj = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
953 req->file_handle, req->file_access, sd )))
955 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
956 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
957 else
958 reply->handle = alloc_handle_no_access_check( current->process, obj,
959 req->access, objattr->attributes );
960 release_object( obj );
963 if (root) release_object( root );
966 /* open a handle to a mapping */
967 DECL_HANDLER(open_mapping)
969 struct unicode_str name = get_req_unicode_str();
971 reply->handle = open_object( current->process, req->rootdir, req->access,
972 &mapping_ops, &name, req->attributes );
975 /* get a mapping information */
976 DECL_HANDLER(get_mapping_info)
978 struct mapping *mapping;
980 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
982 reply->size = mapping->size;
983 reply->flags = mapping->flags;
985 if (mapping->flags & SEC_IMAGE)
986 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
988 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
990 release_object( mapping );
991 return;
994 if (mapping->shared)
995 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
996 GENERIC_READ|GENERIC_WRITE, 0 );
997 release_object( mapping );
1000 /* add a memory view in the current process */
1001 DECL_HANDLER(map_view)
1003 struct mapping *mapping = NULL;
1004 struct memory_view *view;
1006 if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
1008 set_error( STATUS_INVALID_PARAMETER );
1009 return;
1012 /* make sure we don't already have an overlapping view */
1013 LIST_FOR_EACH_ENTRY( view, &current->process->views, struct memory_view, entry )
1015 if (view->base + view->size <= req->base) continue;
1016 if (view->base >= req->base + req->size) continue;
1017 set_error( STATUS_INVALID_PARAMETER );
1018 return;
1021 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1023 if (mapping->flags & SEC_IMAGE)
1025 if (req->start || req->size > mapping->image.map_size)
1027 set_error( STATUS_INVALID_PARAMETER );
1028 goto done;
1031 else if (req->start >= mapping->size ||
1032 req->start + req->size < req->start ||
1033 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1035 set_error( STATUS_INVALID_PARAMETER );
1036 goto done;
1039 if ((view = mem_alloc( sizeof(*view) )))
1041 view->base = req->base;
1042 view->size = req->size;
1043 view->start = req->start;
1044 view->flags = mapping->flags;
1045 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1046 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1047 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1048 list_add_tail( &current->process->views, &view->entry );
1051 done:
1052 release_object( mapping );
1055 /* unmap a memory view from the current process */
1056 DECL_HANDLER(unmap_view)
1058 struct memory_view *view = find_mapped_view( current->process, req->base );
1060 if (view) free_memory_view( view );
1063 /* get a range of committed pages in a file mapping */
1064 DECL_HANDLER(get_mapping_committed_range)
1066 struct memory_view *view = find_mapped_view( current->process, req->base );
1068 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1071 /* add a range to the committed pages in a file mapping */
1072 DECL_HANDLER(add_mapping_committed_range)
1074 struct memory_view *view = find_mapped_view( current->process, req->base );
1076 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1079 /* check if two memory maps are for the same file */
1080 DECL_HANDLER(is_same_mapping)
1082 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1083 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1085 if (!view1 || !view2) return;
1086 if (!view1->fd || !view2->fd ||
1087 !(view1->flags & SEC_IMAGE) || !(view2->flags & SEC_IMAGE) ||
1088 !is_same_file_fd( view1->fd, view2->fd ))
1089 set_error( STATUS_NOT_SAME_DEVICE );