d2d1: Trim rasterization area when drawing Bézier curves.
[wine.git] / server / mapping.c
blob6990a1913d76db758021f6262adf9eecf57176bd
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 ranges = 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 builtin_signature[] = "Wine builtin DLL";
562 static const char fakedll_signature[] = "Wine placeholder DLL";
564 IMAGE_COR20_HEADER clr;
565 IMAGE_SECTION_HEADER sec[96];
566 struct
568 IMAGE_DOS_HEADER dos;
569 char buffer[32];
570 } mz;
571 struct
573 DWORD Signature;
574 IMAGE_FILE_HEADER FileHeader;
575 union
577 IMAGE_OPTIONAL_HEADER32 hdr32;
578 IMAGE_OPTIONAL_HEADER64 hdr64;
579 } opt;
580 } nt;
581 off_t pos;
582 int size;
583 size_t mz_size, clr_va, clr_size;
584 unsigned int i, cpu_mask = get_supported_cpu_mask();
586 /* load the headers */
588 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
589 size = pread( unix_fd, &mz, sizeof(mz), 0 );
590 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
591 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
592 mz_size = size;
593 pos = mz.dos.e_lfanew;
595 size = pread( unix_fd, &nt, sizeof(nt), pos );
596 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
597 /* zero out Optional header in the case it's not present or partial */
598 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader );
599 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
600 if (nt.Signature != IMAGE_NT_SIGNATURE)
602 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
603 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
604 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
605 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
606 return STATUS_INVALID_IMAGE_NE_FORMAT;
609 switch (nt.opt.hdr32.Magic)
611 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
612 switch (nt.FileHeader.Machine)
614 case IMAGE_FILE_MACHINE_I386:
615 mapping->image.cpu = CPU_x86;
616 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
617 return STATUS_INVALID_IMAGE_FORMAT;
618 case IMAGE_FILE_MACHINE_ARM:
619 case IMAGE_FILE_MACHINE_THUMB:
620 case IMAGE_FILE_MACHINE_ARMNT:
621 mapping->image.cpu = CPU_ARM;
622 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
623 return STATUS_INVALID_IMAGE_FORMAT;
624 case IMAGE_FILE_MACHINE_POWERPC:
625 mapping->image.cpu = CPU_POWERPC;
626 if (cpu_mask & CPU_FLAG(CPU_POWERPC)) break;
627 return STATUS_INVALID_IMAGE_FORMAT;
628 default:
629 return STATUS_INVALID_IMAGE_FORMAT;
631 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
632 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
634 mapping->image.base = nt.opt.hdr32.ImageBase;
635 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
636 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
637 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
638 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
639 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
640 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
641 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
642 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
643 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
644 nt.opt.hdr32.AddressOfEntryPoint ||
645 nt.opt.hdr32.SectionAlignment & page_mask);
646 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
647 mapping->image.checksum = nt.opt.hdr32.CheckSum;
648 mapping->image.image_flags = 0;
649 if (nt.opt.hdr32.SectionAlignment & page_mask)
650 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
651 if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
652 mapping->image.contains_code && !(clr_va && clr_size))
653 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
654 break;
656 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
657 if (!(cpu_mask & CPU_64BIT_MASK)) return STATUS_INVALID_IMAGE_WIN_64;
658 switch (nt.FileHeader.Machine)
660 case IMAGE_FILE_MACHINE_AMD64:
661 mapping->image.cpu = CPU_x86_64;
662 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
663 return STATUS_INVALID_IMAGE_FORMAT;
664 case IMAGE_FILE_MACHINE_ARM64:
665 mapping->image.cpu = CPU_ARM64;
666 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
667 return STATUS_INVALID_IMAGE_FORMAT;
668 default:
669 return STATUS_INVALID_IMAGE_FORMAT;
671 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
672 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
674 mapping->image.base = nt.opt.hdr64.ImageBase;
675 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
676 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
677 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
678 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
679 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
680 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
681 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
682 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
683 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
684 nt.opt.hdr64.AddressOfEntryPoint ||
685 nt.opt.hdr64.SectionAlignment & page_mask);
686 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
687 mapping->image.checksum = nt.opt.hdr64.CheckSum;
688 mapping->image.image_flags = 0;
689 if (nt.opt.hdr64.SectionAlignment & page_mask)
690 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
691 if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
692 mapping->image.contains_code && !(clr_va && clr_size))
693 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
694 break;
696 default:
697 return STATUS_INVALID_IMAGE_FORMAT;
700 mapping->image.image_charact = nt.FileHeader.Characteristics;
701 mapping->image.machine = nt.FileHeader.Machine;
702 mapping->image.zerobits = 0; /* FIXME */
703 mapping->image.gp = 0; /* FIXME */
704 mapping->image.file_size = file_size;
705 mapping->image.loader_flags = clr_va && clr_size;
706 mapping->image.__pad = 0;
707 if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ))
708 mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin;
709 else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
710 mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
712 /* load the section headers */
714 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
715 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
716 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
717 if (!mapping->size) mapping->size = mapping->image.map_size;
718 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
719 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
720 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
721 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
723 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
724 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
726 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
727 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
729 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
730 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
731 !(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
733 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
734 if (cpu_mask & CPU_FLAG(CPU_x86_64)) mapping->image.cpu = CPU_x86_64;
735 else if (cpu_mask & CPU_FLAG(CPU_ARM64)) mapping->image.cpu = CPU_ARM64;
739 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
740 return STATUS_INVALID_FILE_FOR_SECTION;
742 return STATUS_SUCCESS;
745 static struct ranges *create_ranges(void)
747 struct ranges *ranges = alloc_object( &ranges_ops );
749 if (!ranges) return NULL;
750 ranges->count = 0;
751 ranges->max = 8;
752 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
754 release_object( ranges );
755 return NULL;
757 return ranges;
760 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
762 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
764 case SEC_IMAGE:
765 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
766 if (handle) return SEC_FILE | SEC_IMAGE;
767 set_error( STATUS_INVALID_FILE_FOR_SECTION );
768 return 0;
769 case SEC_COMMIT:
770 if (!handle) return flags;
771 /* fall through */
772 case SEC_RESERVE:
773 if (flags & SEC_LARGE_PAGES) break;
774 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
775 return flags;
777 set_error( STATUS_INVALID_PARAMETER );
778 return 0;
782 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
783 unsigned int attr, mem_size_t size, unsigned int flags,
784 obj_handle_t handle, unsigned int file_access,
785 const struct security_descriptor *sd )
787 struct mapping *mapping;
788 struct file *file;
789 struct fd *fd;
790 int unix_fd;
791 struct stat st;
793 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
795 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
796 return NULL;
797 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
798 return &mapping->obj; /* Nothing else to do */
800 mapping->size = size;
801 mapping->fd = NULL;
802 mapping->shared = NULL;
803 mapping->committed = NULL;
805 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
807 if (handle)
809 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
810 unsigned int mapping_access = FILE_MAPPING_ACCESS;
812 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
813 fd = get_obj_fd( (struct object *)file );
815 /* file sharing rules for mappings are different so we use magic the access rights */
816 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
817 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
819 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
821 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
822 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
824 release_object( file );
825 release_object( fd );
826 if (!mapping->fd) goto error;
828 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
829 if (fstat( unix_fd, &st ) == -1)
831 file_set_error();
832 goto error;
834 if (flags & SEC_IMAGE)
836 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
837 if (!err) return &mapping->obj;
838 set_error( err );
839 goto error;
841 if (!mapping->size)
843 if (!(mapping->size = st.st_size))
845 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
846 goto error;
849 else if (st.st_size < mapping->size)
851 if (!(file_access & FILE_WRITE_DATA))
853 set_error( STATUS_SECTION_TOO_BIG );
854 goto error;
856 if (!grow_file( unix_fd, mapping->size )) goto error;
859 else /* Anonymous mapping (no associated file) */
861 if (!mapping->size)
863 set_error( STATUS_INVALID_PARAMETER );
864 goto error;
866 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
867 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
868 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
869 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
870 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
871 allow_fd_caching( mapping->fd );
873 return &mapping->obj;
875 error:
876 release_object( mapping );
877 return NULL;
880 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
882 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
885 /* open a new file for the file descriptor backing the mapping */
886 struct file *get_mapping_file( struct process *process, client_ptr_t base,
887 unsigned int access, unsigned int sharing )
889 struct memory_view *view = find_mapped_view( process, base );
891 if (!view || !view->fd) return NULL;
892 return create_file_for_fd_obj( view->fd, access, sharing );
895 static void mapping_dump( struct object *obj, int verbose )
897 struct mapping *mapping = (struct mapping *)obj;
898 assert( obj->ops == &mapping_ops );
899 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
900 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
901 mapping->flags, mapping->fd, mapping->shared );
904 static struct object_type *mapping_get_type( struct object *obj )
906 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
907 static const struct unicode_str str = { name, sizeof(name) };
908 return get_object_type( &str );
911 static struct fd *mapping_get_fd( struct object *obj )
913 struct mapping *mapping = (struct mapping *)obj;
914 return (struct fd *)grab_object( mapping->fd );
917 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
919 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
920 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
921 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
922 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
923 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
926 static void mapping_destroy( struct object *obj )
928 struct mapping *mapping = (struct mapping *)obj;
929 assert( obj->ops == &mapping_ops );
930 if (mapping->fd) release_object( mapping->fd );
931 if (mapping->committed) release_object( mapping->committed );
932 if (mapping->shared) release_object( mapping->shared );
935 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
937 return FD_TYPE_FILE;
940 int get_page_size(void)
942 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
943 return page_mask + 1;
946 /* create a file mapping */
947 DECL_HANDLER(create_mapping)
949 struct object *root, *obj;
950 struct unicode_str name;
951 const struct security_descriptor *sd;
952 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
954 if (!objattr) return;
956 if ((obj = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
957 req->file_handle, req->file_access, sd )))
959 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
960 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
961 else
962 reply->handle = alloc_handle_no_access_check( current->process, obj,
963 req->access, objattr->attributes );
964 release_object( obj );
967 if (root) release_object( root );
970 /* open a handle to a mapping */
971 DECL_HANDLER(open_mapping)
973 struct unicode_str name = get_req_unicode_str();
975 reply->handle = open_object( current->process, req->rootdir, req->access,
976 &mapping_ops, &name, req->attributes );
979 /* get a mapping information */
980 DECL_HANDLER(get_mapping_info)
982 struct mapping *mapping;
984 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
986 reply->size = mapping->size;
987 reply->flags = mapping->flags;
989 if (mapping->flags & SEC_IMAGE)
990 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
992 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
994 release_object( mapping );
995 return;
998 if (mapping->shared)
999 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1000 GENERIC_READ|GENERIC_WRITE, 0 );
1001 release_object( mapping );
1004 /* add a memory view in the current process */
1005 DECL_HANDLER(map_view)
1007 struct mapping *mapping = NULL;
1008 struct memory_view *view;
1010 if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
1012 set_error( STATUS_INVALID_PARAMETER );
1013 return;
1016 /* make sure we don't already have an overlapping view */
1017 LIST_FOR_EACH_ENTRY( view, &current->process->views, struct memory_view, entry )
1019 if (view->base + view->size <= req->base) continue;
1020 if (view->base >= req->base + req->size) continue;
1021 set_error( STATUS_INVALID_PARAMETER );
1022 return;
1025 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1027 if (mapping->flags & SEC_IMAGE)
1029 if (req->start || req->size > mapping->image.map_size)
1031 set_error( STATUS_INVALID_PARAMETER );
1032 goto done;
1035 else if (req->start >= mapping->size ||
1036 req->start + req->size < req->start ||
1037 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1039 set_error( STATUS_INVALID_PARAMETER );
1040 goto done;
1043 if ((view = mem_alloc( sizeof(*view) )))
1045 view->base = req->base;
1046 view->size = req->size;
1047 view->start = req->start;
1048 view->flags = mapping->flags;
1049 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1050 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1051 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1052 list_add_tail( &current->process->views, &view->entry );
1055 done:
1056 release_object( mapping );
1059 /* unmap a memory view from the current process */
1060 DECL_HANDLER(unmap_view)
1062 struct memory_view *view = find_mapped_view( current->process, req->base );
1064 if (view) free_memory_view( view );
1067 /* get a range of committed pages in a file mapping */
1068 DECL_HANDLER(get_mapping_committed_range)
1070 struct memory_view *view = find_mapped_view( current->process, req->base );
1072 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1075 /* add a range to the committed pages in a file mapping */
1076 DECL_HANDLER(add_mapping_committed_range)
1078 struct memory_view *view = find_mapped_view( current->process, req->base );
1080 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1083 /* check if two memory maps are for the same file */
1084 DECL_HANDLER(is_same_mapping)
1086 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1087 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1089 if (!view1 || !view2) return;
1090 if (!view1->fd || !view2->fd ||
1091 !(view1->flags & SEC_IMAGE) || !(view2->flags & SEC_IMAGE) ||
1092 !is_same_file_fd( view1->fd, view2->fd ))
1093 set_error( STATUS_NOT_SAME_DEVICE );