windowscodecs: Fix DdsFrameDecode_Dds_CopyBlocks().
[wine.git] / server / mapping.c
blobdb0debe0af578e47a835ce8e630c24c5e5a829ba
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"
38 #include "ddk/wdm.h"
40 #include "file.h"
41 #include "handle.h"
42 #include "thread.h"
43 #include "process.h"
44 #include "request.h"
45 #include "security.h"
47 /* list of memory ranges, used to store committed info */
48 struct ranges
50 struct object obj; /* object header */
51 unsigned int count; /* number of used ranges */
52 unsigned int max; /* number of allocated ranges */
53 struct range
55 file_pos_t start;
56 file_pos_t end;
57 } *ranges;
60 static void ranges_dump( struct object *obj, int verbose );
61 static void ranges_destroy( struct object *obj );
63 static const struct object_ops ranges_ops =
65 sizeof(struct ranges), /* size */
66 ranges_dump, /* dump */
67 no_get_type, /* get_type */
68 no_add_queue, /* add_queue */
69 NULL, /* remove_queue */
70 NULL, /* signaled */
71 NULL, /* satisfied */
72 no_signal, /* signal */
73 no_get_fd, /* get_fd */
74 no_map_access, /* map_access */
75 default_get_sd, /* get_sd */
76 default_set_sd, /* set_sd */
77 no_lookup_name, /* lookup_name */
78 no_link_name, /* link_name */
79 NULL, /* unlink_name */
80 no_open_file, /* open_file */
81 no_kernel_obj_list, /* get_kernel_obj_list */
82 no_close_handle, /* close_handle */
83 ranges_destroy /* destroy */
86 /* file backing the shared sections of a PE image mapping */
87 struct shared_map
89 struct object obj; /* object header */
90 struct fd *fd; /* file descriptor of the mapped PE file */
91 struct file *file; /* temp file holding the shared data */
92 struct list entry; /* entry in global shared maps list */
95 static void shared_map_dump( struct object *obj, int verbose );
96 static void shared_map_destroy( struct object *obj );
98 static const struct object_ops shared_map_ops =
100 sizeof(struct shared_map), /* size */
101 shared_map_dump, /* dump */
102 no_get_type, /* get_type */
103 no_add_queue, /* add_queue */
104 NULL, /* remove_queue */
105 NULL, /* signaled */
106 NULL, /* satisfied */
107 no_signal, /* signal */
108 no_get_fd, /* get_fd */
109 no_map_access, /* map_access */
110 default_get_sd, /* get_sd */
111 default_set_sd, /* set_sd */
112 no_lookup_name, /* lookup_name */
113 no_link_name, /* link_name */
114 NULL, /* unlink_name */
115 no_open_file, /* open_file */
116 no_kernel_obj_list, /* get_kernel_obj_list */
117 no_close_handle, /* close_handle */
118 shared_map_destroy /* destroy */
121 static struct list shared_map_list = LIST_INIT( shared_map_list );
123 /* memory view mapped in client address space */
124 struct memory_view
126 struct list entry; /* entry in per-process view list */
127 struct fd *fd; /* fd for mapped file */
128 struct ranges *committed; /* list of committed ranges in this mapping */
129 struct shared_map *shared; /* temp file for shared PE mapping */
130 pe_image_info_t image; /* image info (for PE image mapping) */
131 unsigned int flags; /* SEC_* flags */
132 client_ptr_t base; /* view base address (in process addr space) */
133 mem_size_t size; /* view size */
134 file_pos_t start; /* start offset in mapping */
137 struct mapping
139 struct object obj; /* object header */
140 mem_size_t size; /* mapping size */
141 unsigned int flags; /* SEC_* flags */
142 struct fd *fd; /* fd for mapped file */
143 pe_image_info_t image; /* image info (for PE image mapping) */
144 struct ranges *committed; /* list of committed ranges in this mapping */
145 struct shared_map *shared; /* temp file for shared PE mapping */
148 static void mapping_dump( struct object *obj, int verbose );
149 static struct object_type *mapping_get_type( struct object *obj );
150 static struct fd *mapping_get_fd( struct object *obj );
151 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
152 static void mapping_destroy( struct object *obj );
153 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
155 static const struct object_ops mapping_ops =
157 sizeof(struct mapping), /* size */
158 mapping_dump, /* dump */
159 mapping_get_type, /* get_type */
160 no_add_queue, /* add_queue */
161 NULL, /* remove_queue */
162 NULL, /* signaled */
163 NULL, /* satisfied */
164 no_signal, /* signal */
165 mapping_get_fd, /* get_fd */
166 mapping_map_access, /* map_access */
167 default_get_sd, /* get_sd */
168 default_set_sd, /* set_sd */
169 no_lookup_name, /* lookup_name */
170 directory_link_name, /* link_name */
171 default_unlink_name, /* unlink_name */
172 no_open_file, /* open_file */
173 no_kernel_obj_list, /* get_kernel_obj_list */
174 fd_close_handle, /* close_handle */
175 mapping_destroy /* destroy */
178 static const struct fd_ops mapping_fd_ops =
180 default_fd_get_poll_events, /* get_poll_events */
181 default_poll_event, /* poll_event */
182 mapping_get_fd_type, /* get_fd_type */
183 no_fd_read, /* read */
184 no_fd_write, /* write */
185 no_fd_flush, /* flush */
186 no_fd_get_file_info, /* get_file_info */
187 no_fd_get_volume_info, /* get_volume_info */
188 no_fd_ioctl, /* ioctl */
189 no_fd_queue_async, /* queue_async */
190 default_fd_reselect_async /* reselect_async */
193 static size_t page_mask;
195 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
198 static void ranges_dump( struct object *obj, int verbose )
200 struct ranges *ranges = (struct ranges *)obj;
201 fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
204 static void ranges_destroy( struct object *obj )
206 struct ranges *ranges = (struct ranges *)obj;
207 free( ranges->ranges );
210 static void shared_map_dump( struct object *obj, int verbose )
212 struct shared_map *shared = (struct shared_map *)obj;
213 fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
216 static void shared_map_destroy( struct object *obj )
218 struct shared_map *shared = (struct shared_map *)obj;
220 release_object( shared->fd );
221 release_object( shared->file );
222 list_remove( &shared->entry );
225 /* extend a file beyond the current end of file */
226 static int grow_file( int unix_fd, file_pos_t new_size )
228 static const char zero;
229 off_t size = new_size;
231 if (sizeof(new_size) > sizeof(size) && size != new_size)
233 set_error( STATUS_INVALID_PARAMETER );
234 return 0;
236 /* extend the file one byte beyond the requested size and then truncate it */
237 /* this should work around ftruncate implementations that can't extend files */
238 if (pwrite( unix_fd, &zero, 1, size ) != -1)
240 ftruncate( unix_fd, size );
241 return 1;
243 file_set_error();
244 return 0;
247 /* check if the current directory allows exec mappings */
248 static int check_current_dir_for_exec(void)
250 int fd;
251 char tmpfn[] = "anonmap.XXXXXX";
252 void *ret = MAP_FAILED;
254 fd = mkstemps( tmpfn, 0 );
255 if (fd == -1) return 0;
256 if (grow_file( fd, 1 ))
258 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
259 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
261 close( fd );
262 unlink( tmpfn );
263 return (ret != MAP_FAILED);
266 /* create a temp file for anonymous mappings */
267 static int create_temp_file( file_pos_t size )
269 static int temp_dir_fd = -1;
270 char tmpfn[] = "anonmap.XXXXXX";
271 int fd;
273 if (temp_dir_fd == -1)
275 temp_dir_fd = server_dir_fd;
276 if (!check_current_dir_for_exec())
278 /* the server dir is noexec, try the config dir instead */
279 fchdir( config_dir_fd );
280 if (check_current_dir_for_exec())
281 temp_dir_fd = config_dir_fd;
282 else /* neither works, fall back to server dir */
283 fchdir( server_dir_fd );
286 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
288 fd = mkstemps( tmpfn, 0 );
289 if (fd != -1)
291 if (!grow_file( fd, size ))
293 close( fd );
294 fd = -1;
296 unlink( tmpfn );
298 else file_set_error();
300 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
301 return fd;
304 /* find a memory view from its base address */
305 static struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
307 struct memory_view *view;
309 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
310 if (view->base == base) return view;
312 set_error( STATUS_NOT_MAPPED_VIEW );
313 return NULL;
316 static void free_memory_view( struct memory_view *view )
318 if (view->fd) release_object( view->fd );
319 if (view->committed) release_object( view->committed );
320 if (view->shared) release_object( view->shared );
321 list_remove( &view->entry );
322 free( view );
325 /* free all mapped views at process exit */
326 void free_mapped_views( struct process *process )
328 struct list *ptr;
330 while ((ptr = list_head( &process->views )))
331 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
334 /* find the shared PE mapping for a given mapping */
335 static struct shared_map *get_shared_file( struct fd *fd )
337 struct shared_map *ptr;
339 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
340 if (is_same_file_fd( ptr->fd, fd ))
341 return (struct shared_map *)grab_object( ptr );
342 return NULL;
345 /* return the size of the memory mapping and file range of a given section */
346 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
347 off_t *file_start, size_t *file_size )
349 static const unsigned int sector_align = 0x1ff;
351 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
352 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
354 *file_start = sec->PointerToRawData & ~sector_align;
355 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
356 if (*file_size > *map_size) *file_size = *map_size;
359 /* add a range to the committed list */
360 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
362 unsigned int i, j;
363 struct ranges *committed = view->committed;
364 struct range *ranges;
366 if ((start & page_mask) || (end & page_mask) ||
367 start >= view->size || end >= view->size ||
368 start >= end)
370 set_error( STATUS_INVALID_PARAMETER );
371 return;
374 if (!committed) return; /* everything committed already */
376 start += view->start;
377 end += view->start;
379 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
381 if (ranges[i].start > end) break;
382 if (ranges[i].end < start) continue;
383 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
384 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
386 for (j = i + 1; j < committed->count; j++)
388 if (ranges[j].start > end) break;
389 if (ranges[j].end > end) end = ranges[j].end;
391 if (j > i + 1)
393 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
394 committed->count -= j - (i + 1);
396 ranges[i].end = end;
398 return;
401 /* now add a new range */
403 if (committed->count == committed->max)
405 unsigned int new_size = committed->max * 2;
406 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
407 if (!new_ptr) return;
408 committed->max = new_size;
409 ranges = committed->ranges = new_ptr;
411 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
412 ranges[i].start = start;
413 ranges[i].end = end;
414 committed->count++;
417 /* find the range containing start and return whether it's committed */
418 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
420 unsigned int i;
421 struct ranges *committed = view->committed;
422 struct range *ranges;
424 if ((start & page_mask) || start >= view->size)
426 set_error( STATUS_INVALID_PARAMETER );
427 return 0;
429 if (!committed) /* everything is committed */
431 *size = view->size - start;
432 return 1;
434 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
436 if (ranges[i].start > view->start + start)
438 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
439 return 0;
441 if (ranges[i].end > view->start + start)
443 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
444 return 1;
447 *size = view->size - start;
448 return 0;
451 /* allocate and fill the temp file for a shared PE image mapping */
452 static int build_shared_mapping( struct mapping *mapping, int fd,
453 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
455 struct shared_map *shared;
456 struct file *file;
457 unsigned int i;
458 mem_size_t total_size;
459 size_t file_size, map_size, max_size;
460 off_t shared_pos, read_pos, write_pos;
461 char *buffer = NULL;
462 int shared_fd;
463 long toread;
465 /* compute the total size of the shared mapping */
467 total_size = max_size = 0;
468 for (i = 0; i < nb_sec; i++)
470 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
471 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
473 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
474 if (file_size > max_size) max_size = file_size;
475 total_size += map_size;
478 if (!total_size) return 1; /* nothing to do */
480 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
482 /* create a temp file for the mapping */
484 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
485 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
487 if (!(buffer = malloc( max_size ))) goto error;
489 /* copy the shared sections data into the temp file */
491 shared_pos = 0;
492 for (i = 0; i < nb_sec; i++)
494 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
495 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
496 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
497 write_pos = shared_pos;
498 shared_pos += map_size;
499 if (!sec[i].PointerToRawData || !file_size) continue;
500 toread = file_size;
501 while (toread)
503 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
504 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
506 file_size -= toread;
507 break;
509 if (res <= 0) goto error;
510 toread -= res;
511 read_pos += res;
513 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
516 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
517 shared->fd = (struct fd *)grab_object( mapping->fd );
518 shared->file = file;
519 list_add_head( &shared_map_list, &shared->entry );
520 mapping->shared = shared;
521 free( buffer );
522 return 1;
524 error:
525 release_object( file );
526 free( buffer );
527 return 0;
530 /* load the CLR header from its section */
531 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
532 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
534 ssize_t ret;
535 size_t map_size, file_size;
536 off_t file_start;
537 unsigned int i;
539 if (!va || !size) return 0;
541 for (i = 0; i < nb_sec; i++)
543 if (va < sec[i].VirtualAddress) continue;
544 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
545 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
546 if (size >= map_size) continue;
547 if (va - sec[i].VirtualAddress >= map_size - size) continue;
548 file_size = min( file_size, map_size );
549 size = min( size, sizeof(*hdr) );
550 ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
551 if (ret <= 0) break;
552 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
553 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
554 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
555 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
557 return 0;
560 /* retrieve the mapping parameters for an executable (PE) image */
561 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
563 static const char builtin_signature[] = "Wine builtin DLL";
564 static const char fakedll_signature[] = "Wine placeholder DLL";
566 IMAGE_COR20_HEADER clr;
567 IMAGE_SECTION_HEADER sec[96];
568 struct
570 IMAGE_DOS_HEADER dos;
571 char buffer[32];
572 } mz;
573 struct
575 DWORD Signature;
576 IMAGE_FILE_HEADER FileHeader;
577 union
579 IMAGE_OPTIONAL_HEADER32 hdr32;
580 IMAGE_OPTIONAL_HEADER64 hdr64;
581 } opt;
582 } nt;
583 off_t pos;
584 int size, opt_size;
585 size_t mz_size, clr_va, clr_size;
586 unsigned int i, cpu_mask = get_supported_cpu_mask();
588 /* load the headers */
590 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
591 size = pread( unix_fd, &mz, sizeof(mz), 0 );
592 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
593 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
594 mz_size = size;
595 pos = mz.dos.e_lfanew;
597 size = pread( unix_fd, &nt, sizeof(nt), pos );
598 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
599 /* zero out Optional header in the case it's not present or partial */
600 opt_size = max( nt.FileHeader.SizeOfOptionalHeader, offsetof( IMAGE_OPTIONAL_HEADER32, CheckSum ));
601 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + opt_size );
602 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
603 if (nt.Signature != IMAGE_NT_SIGNATURE)
605 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
606 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
607 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
608 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
609 return STATUS_INVALID_IMAGE_NE_FORMAT;
612 switch (nt.opt.hdr32.Magic)
614 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
615 switch (nt.FileHeader.Machine)
617 case IMAGE_FILE_MACHINE_I386:
618 mapping->image.cpu = CPU_x86;
619 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
620 return STATUS_INVALID_IMAGE_FORMAT;
621 case IMAGE_FILE_MACHINE_ARM:
622 case IMAGE_FILE_MACHINE_THUMB:
623 case IMAGE_FILE_MACHINE_ARMNT:
624 mapping->image.cpu = CPU_ARM;
625 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
626 return STATUS_INVALID_IMAGE_FORMAT;
627 case IMAGE_FILE_MACHINE_POWERPC:
628 mapping->image.cpu = CPU_POWERPC;
629 if (cpu_mask & CPU_FLAG(CPU_POWERPC)) break;
630 return STATUS_INVALID_IMAGE_FORMAT;
631 default:
632 return STATUS_INVALID_IMAGE_FORMAT;
634 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
635 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
637 mapping->image.base = nt.opt.hdr32.ImageBase;
638 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
639 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
640 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
641 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
642 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
643 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
644 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
645 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
646 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
647 nt.opt.hdr32.AddressOfEntryPoint ||
648 nt.opt.hdr32.SectionAlignment & page_mask);
649 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
650 mapping->image.checksum = nt.opt.hdr32.CheckSum;
651 mapping->image.image_flags = 0;
652 if (nt.opt.hdr32.SectionAlignment & page_mask)
653 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
654 if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
655 mapping->image.contains_code && !(clr_va && clr_size))
656 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
657 break;
659 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
660 if (!(cpu_mask & CPU_64BIT_MASK)) return STATUS_INVALID_IMAGE_WIN_64;
661 switch (nt.FileHeader.Machine)
663 case IMAGE_FILE_MACHINE_AMD64:
664 mapping->image.cpu = CPU_x86_64;
665 if (cpu_mask & (CPU_FLAG(CPU_x86) | CPU_FLAG(CPU_x86_64))) break;
666 return STATUS_INVALID_IMAGE_FORMAT;
667 case IMAGE_FILE_MACHINE_ARM64:
668 mapping->image.cpu = CPU_ARM64;
669 if (cpu_mask & (CPU_FLAG(CPU_ARM) | CPU_FLAG(CPU_ARM64))) break;
670 return STATUS_INVALID_IMAGE_FORMAT;
671 default:
672 return STATUS_INVALID_IMAGE_FORMAT;
674 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
675 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
677 mapping->image.base = nt.opt.hdr64.ImageBase;
678 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
679 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
680 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
681 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
682 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
683 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
684 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
685 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
686 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
687 nt.opt.hdr64.AddressOfEntryPoint ||
688 nt.opt.hdr64.SectionAlignment & page_mask);
689 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
690 mapping->image.checksum = nt.opt.hdr64.CheckSum;
691 mapping->image.image_flags = 0;
692 if (nt.opt.hdr64.SectionAlignment & page_mask)
693 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
694 if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
695 mapping->image.contains_code && !(clr_va && clr_size))
696 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
697 break;
699 default:
700 return STATUS_INVALID_IMAGE_FORMAT;
703 mapping->image.image_charact = nt.FileHeader.Characteristics;
704 mapping->image.machine = nt.FileHeader.Machine;
705 mapping->image.zerobits = 0; /* FIXME */
706 mapping->image.gp = 0; /* FIXME */
707 mapping->image.file_size = file_size;
708 mapping->image.loader_flags = clr_va && clr_size;
709 mapping->image.__pad = 0;
710 if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ))
711 mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin;
712 else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
713 mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
715 /* load the section headers */
717 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
718 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
719 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
720 if (!mapping->size) mapping->size = mapping->image.map_size;
721 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
722 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
723 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
724 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
726 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
727 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
729 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
730 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
732 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
733 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
734 !(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
736 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
737 if (cpu_mask & CPU_FLAG(CPU_x86_64)) mapping->image.cpu = CPU_x86_64;
738 else if (cpu_mask & CPU_FLAG(CPU_ARM64)) mapping->image.cpu = CPU_ARM64;
742 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
743 return STATUS_INVALID_FILE_FOR_SECTION;
745 return STATUS_SUCCESS;
748 static struct ranges *create_ranges(void)
750 struct ranges *ranges = alloc_object( &ranges_ops );
752 if (!ranges) return NULL;
753 ranges->count = 0;
754 ranges->max = 8;
755 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
757 release_object( ranges );
758 return NULL;
760 return ranges;
763 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
765 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
767 case SEC_IMAGE:
768 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
769 if (handle) return SEC_FILE | SEC_IMAGE;
770 set_error( STATUS_INVALID_FILE_FOR_SECTION );
771 return 0;
772 case SEC_COMMIT:
773 if (!handle) return flags;
774 /* fall through */
775 case SEC_RESERVE:
776 if (flags & SEC_LARGE_PAGES) break;
777 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
778 return flags;
780 set_error( STATUS_INVALID_PARAMETER );
781 return 0;
785 static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
786 unsigned int attr, mem_size_t size, unsigned int flags,
787 obj_handle_t handle, unsigned int file_access,
788 const struct security_descriptor *sd )
790 struct mapping *mapping;
791 struct file *file;
792 struct fd *fd;
793 int unix_fd;
794 struct stat st;
796 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
798 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
799 return NULL;
800 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
801 return mapping; /* Nothing else to do */
803 mapping->size = size;
804 mapping->fd = NULL;
805 mapping->shared = NULL;
806 mapping->committed = NULL;
808 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
810 if (handle)
812 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
813 unsigned int mapping_access = FILE_MAPPING_ACCESS;
815 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
816 fd = get_obj_fd( (struct object *)file );
818 /* file sharing rules for mappings are different so we use magic the access rights */
819 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
820 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
822 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
824 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
825 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
827 release_object( file );
828 release_object( fd );
829 if (!mapping->fd) goto error;
831 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
832 if (fstat( unix_fd, &st ) == -1)
834 file_set_error();
835 goto error;
837 if (flags & SEC_IMAGE)
839 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
840 if (!err) return mapping;
841 set_error( err );
842 goto error;
844 if (!mapping->size)
846 if (!(mapping->size = st.st_size))
848 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
849 goto error;
852 else if (st.st_size < mapping->size)
854 if (!(file_access & FILE_WRITE_DATA))
856 set_error( STATUS_SECTION_TOO_BIG );
857 goto error;
859 if (!grow_file( unix_fd, mapping->size )) goto error;
862 else /* Anonymous mapping (no associated file) */
864 if (!mapping->size)
866 set_error( STATUS_INVALID_PARAMETER );
867 goto error;
869 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
870 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
871 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
872 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
873 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
874 allow_fd_caching( mapping->fd );
876 return mapping;
878 error:
879 release_object( mapping );
880 return NULL;
883 static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
885 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
888 /* open a new file for the file descriptor backing the mapping */
889 struct file *get_mapping_file( struct process *process, client_ptr_t base,
890 unsigned int access, unsigned int sharing )
892 struct memory_view *view = find_mapped_view( process, base );
894 if (!view || !view->fd) return NULL;
895 return create_file_for_fd_obj( view->fd, access, sharing );
898 /* get the image info for a SEC_IMAGE mapping */
899 const pe_image_info_t *get_mapping_image_info( struct process *process, client_ptr_t base )
901 struct memory_view *view = find_mapped_view( process, base );
903 if (!view || !(view->flags & SEC_IMAGE)) return NULL;
904 return &view->image;
907 static void mapping_dump( struct object *obj, int verbose )
909 struct mapping *mapping = (struct mapping *)obj;
910 assert( obj->ops == &mapping_ops );
911 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
912 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
913 mapping->flags, mapping->fd, mapping->shared );
916 static struct object_type *mapping_get_type( struct object *obj )
918 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
919 static const struct unicode_str str = { name, sizeof(name) };
920 return get_object_type( &str );
923 static struct fd *mapping_get_fd( struct object *obj )
925 struct mapping *mapping = (struct mapping *)obj;
926 return (struct fd *)grab_object( mapping->fd );
929 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
931 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
932 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
933 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
934 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
935 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
938 static void mapping_destroy( struct object *obj )
940 struct mapping *mapping = (struct mapping *)obj;
941 assert( obj->ops == &mapping_ops );
942 if (mapping->fd) release_object( mapping->fd );
943 if (mapping->committed) release_object( mapping->committed );
944 if (mapping->shared) release_object( mapping->shared );
947 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
949 return FD_TYPE_FILE;
952 int get_page_size(void)
954 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
955 return page_mask + 1;
958 struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
959 unsigned int attr, const struct security_descriptor *sd )
961 void *ptr;
962 struct mapping *mapping;
964 if (!(mapping = create_mapping( root, name, OBJ_OPENIF, sizeof(KSHARED_USER_DATA),
965 SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, NULL ))) return NULL;
966 ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
967 if (ptr != MAP_FAILED)
969 user_shared_data = ptr;
970 user_shared_data->SystemCall = 1;
972 return &mapping->obj;
975 /* create a file mapping */
976 DECL_HANDLER(create_mapping)
978 struct object *root;
979 struct mapping *mapping;
980 struct unicode_str name;
981 const struct security_descriptor *sd;
982 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
984 if (!objattr) return;
986 if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
987 req->file_handle, req->file_access, sd )))
989 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
990 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
991 else
992 reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
993 req->access, objattr->attributes );
994 release_object( mapping );
997 if (root) release_object( root );
1000 /* open a handle to a mapping */
1001 DECL_HANDLER(open_mapping)
1003 struct unicode_str name = get_req_unicode_str();
1005 reply->handle = open_object( current->process, req->rootdir, req->access,
1006 &mapping_ops, &name, req->attributes );
1009 /* get a mapping information */
1010 DECL_HANDLER(get_mapping_info)
1012 struct mapping *mapping;
1014 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
1016 reply->size = mapping->size;
1017 reply->flags = mapping->flags;
1019 if (mapping->flags & SEC_IMAGE)
1020 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
1022 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
1024 release_object( mapping );
1025 return;
1028 if (mapping->shared)
1029 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1030 GENERIC_READ|GENERIC_WRITE, 0 );
1031 release_object( mapping );
1034 /* add a memory view in the current process */
1035 DECL_HANDLER(map_view)
1037 struct mapping *mapping = NULL;
1038 struct memory_view *view;
1040 if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
1042 set_error( STATUS_INVALID_PARAMETER );
1043 return;
1046 /* make sure we don't already have an overlapping view */
1047 LIST_FOR_EACH_ENTRY( view, &current->process->views, struct memory_view, entry )
1049 if (view->base + view->size <= req->base) continue;
1050 if (view->base >= req->base + req->size) continue;
1051 set_error( STATUS_INVALID_PARAMETER );
1052 return;
1055 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1057 if (mapping->flags & SEC_IMAGE)
1059 if (req->start || req->size > mapping->image.map_size)
1061 set_error( STATUS_INVALID_PARAMETER );
1062 goto done;
1065 else if (req->start >= mapping->size ||
1066 req->start + req->size < req->start ||
1067 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1069 set_error( STATUS_INVALID_PARAMETER );
1070 goto done;
1073 if ((view = mem_alloc( sizeof(*view) )))
1075 view->base = req->base;
1076 view->size = req->size;
1077 view->start = req->start;
1078 view->flags = mapping->flags;
1079 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1080 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1081 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1082 if (mapping->flags & SEC_IMAGE)
1084 view->image = mapping->image;
1085 if (view->base != mapping->image.base) set_error( STATUS_IMAGE_NOT_AT_BASE );
1087 list_add_tail( &current->process->views, &view->entry );
1090 done:
1091 release_object( mapping );
1094 /* unmap a memory view from the current process */
1095 DECL_HANDLER(unmap_view)
1097 struct memory_view *view = find_mapped_view( current->process, req->base );
1099 if (view) free_memory_view( view );
1102 /* get a range of committed pages in a file mapping */
1103 DECL_HANDLER(get_mapping_committed_range)
1105 struct memory_view *view = find_mapped_view( current->process, req->base );
1107 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1110 /* add a range to the committed pages in a file mapping */
1111 DECL_HANDLER(add_mapping_committed_range)
1113 struct memory_view *view = find_mapped_view( current->process, req->base );
1115 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1118 /* check if two memory maps are for the same file */
1119 DECL_HANDLER(is_same_mapping)
1121 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1122 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1124 if (!view1 || !view2) return;
1125 if (!view1->fd || !view2->fd ||
1126 !(view1->flags & SEC_IMAGE) || !(view2->flags & SEC_IMAGE) ||
1127 !is_same_file_fd( view1->fd, view2->fd ))
1128 set_error( STATUS_NOT_SAME_DEVICE );