winegstreamer: Implement IWMStreamConfig::GetStreamType().
[wine.git] / server / mapping.c
blob93dae94b7c447694808fbb3b1f5413dbbe29a0c4
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"
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
33 #include <unistd.h>
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winternl.h"
39 #include "ddk/wdm.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "thread.h"
44 #include "process.h"
45 #include "request.h"
46 #include "security.h"
48 /* list of memory ranges, used to store committed info */
49 struct ranges
51 struct object obj; /* object header */
52 unsigned int count; /* number of used ranges */
53 unsigned int max; /* number of allocated ranges */
54 struct range
56 file_pos_t start;
57 file_pos_t end;
58 } *ranges;
61 static void ranges_dump( struct object *obj, int verbose );
62 static void ranges_destroy( struct object *obj );
64 static const struct object_ops ranges_ops =
66 sizeof(struct ranges), /* size */
67 &no_type, /* type */
68 ranges_dump, /* dump */
69 no_add_queue, /* add_queue */
70 NULL, /* remove_queue */
71 NULL, /* signaled */
72 NULL, /* satisfied */
73 no_signal, /* signal */
74 no_get_fd, /* get_fd */
75 default_map_access, /* map_access */
76 default_get_sd, /* get_sd */
77 default_set_sd, /* set_sd */
78 no_get_full_name, /* get_full_name */
79 no_lookup_name, /* lookup_name */
80 no_link_name, /* link_name */
81 NULL, /* unlink_name */
82 no_open_file, /* open_file */
83 no_kernel_obj_list, /* get_kernel_obj_list */
84 no_close_handle, /* close_handle */
85 ranges_destroy /* destroy */
88 /* file backing the shared sections of a PE image mapping */
89 struct shared_map
91 struct object obj; /* object header */
92 struct fd *fd; /* file descriptor of the mapped PE file */
93 struct file *file; /* temp file holding the shared data */
94 struct list entry; /* entry in global shared maps list */
97 static void shared_map_dump( struct object *obj, int verbose );
98 static void shared_map_destroy( struct object *obj );
100 static const struct object_ops shared_map_ops =
102 sizeof(struct shared_map), /* size */
103 &no_type, /* type */
104 shared_map_dump, /* dump */
105 no_add_queue, /* add_queue */
106 NULL, /* remove_queue */
107 NULL, /* signaled */
108 NULL, /* satisfied */
109 no_signal, /* signal */
110 no_get_fd, /* get_fd */
111 default_map_access, /* map_access */
112 default_get_sd, /* get_sd */
113 default_set_sd, /* set_sd */
114 no_get_full_name, /* get_full_name */
115 no_lookup_name, /* lookup_name */
116 no_link_name, /* link_name */
117 NULL, /* unlink_name */
118 no_open_file, /* open_file */
119 no_kernel_obj_list, /* get_kernel_obj_list */
120 no_close_handle, /* close_handle */
121 shared_map_destroy /* destroy */
124 static struct list shared_map_list = LIST_INIT( shared_map_list );
126 /* memory view mapped in client address space */
127 struct memory_view
129 struct list entry; /* entry in per-process view list */
130 struct fd *fd; /* fd for mapped file */
131 struct ranges *committed; /* list of committed ranges in this mapping */
132 struct shared_map *shared; /* temp file for shared PE mapping */
133 pe_image_info_t image; /* image info (for PE image mapping) */
134 unsigned int flags; /* SEC_* flags */
135 client_ptr_t base; /* view base address (in process addr space) */
136 mem_size_t size; /* view size */
137 file_pos_t start; /* start offset in mapping */
138 data_size_t namelen;
139 WCHAR name[1]; /* filename for .so dll image views */
143 static const WCHAR mapping_name[] = {'S','e','c','t','i','o','n'};
145 struct type_descr mapping_type =
147 { mapping_name, sizeof(mapping_name) }, /* name */
148 SECTION_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
149 { /* mapping */
150 STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ,
151 STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
152 STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
153 SECTION_ALL_ACCESS
157 struct mapping
159 struct object obj; /* object header */
160 mem_size_t size; /* mapping size */
161 unsigned int flags; /* SEC_* flags */
162 struct fd *fd; /* fd for mapped file */
163 pe_image_info_t image; /* image info (for PE image mapping) */
164 struct ranges *committed; /* list of committed ranges in this mapping */
165 struct shared_map *shared; /* temp file for shared PE mapping */
168 static void mapping_dump( struct object *obj, int verbose );
169 static struct fd *mapping_get_fd( struct object *obj );
170 static void mapping_destroy( struct object *obj );
171 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
173 static const struct object_ops mapping_ops =
175 sizeof(struct mapping), /* size */
176 &mapping_type, /* type */
177 mapping_dump, /* dump */
178 no_add_queue, /* add_queue */
179 NULL, /* remove_queue */
180 NULL, /* signaled */
181 NULL, /* satisfied */
182 no_signal, /* signal */
183 mapping_get_fd, /* get_fd */
184 default_map_access, /* map_access */
185 default_get_sd, /* get_sd */
186 default_set_sd, /* set_sd */
187 default_get_full_name, /* get_full_name */
188 no_lookup_name, /* lookup_name */
189 directory_link_name, /* link_name */
190 default_unlink_name, /* unlink_name */
191 no_open_file, /* open_file */
192 no_kernel_obj_list, /* get_kernel_obj_list */
193 no_close_handle, /* close_handle */
194 mapping_destroy /* destroy */
197 static const struct fd_ops mapping_fd_ops =
199 default_fd_get_poll_events, /* get_poll_events */
200 default_poll_event, /* poll_event */
201 mapping_get_fd_type, /* get_fd_type */
202 no_fd_read, /* read */
203 no_fd_write, /* write */
204 no_fd_flush, /* flush */
205 no_fd_get_file_info, /* get_file_info */
206 no_fd_get_volume_info, /* get_volume_info */
207 no_fd_ioctl, /* ioctl */
208 default_fd_cancel_async, /* cancel_async */
209 no_fd_queue_async, /* queue_async */
210 default_fd_reselect_async /* reselect_async */
213 static size_t page_mask;
215 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
218 static void ranges_dump( struct object *obj, int verbose )
220 struct ranges *ranges = (struct ranges *)obj;
221 fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
224 static void ranges_destroy( struct object *obj )
226 struct ranges *ranges = (struct ranges *)obj;
227 free( ranges->ranges );
230 static void shared_map_dump( struct object *obj, int verbose )
232 struct shared_map *shared = (struct shared_map *)obj;
233 fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
236 static void shared_map_destroy( struct object *obj )
238 struct shared_map *shared = (struct shared_map *)obj;
240 release_object( shared->fd );
241 release_object( shared->file );
242 list_remove( &shared->entry );
245 /* extend a file beyond the current end of file */
246 int grow_file( int unix_fd, file_pos_t new_size )
248 static const char zero;
249 off_t size = new_size;
251 if (sizeof(new_size) > sizeof(size) && size != new_size)
253 set_error( STATUS_INVALID_PARAMETER );
254 return 0;
256 /* extend the file one byte beyond the requested size and then truncate it */
257 /* this should work around ftruncate implementations that can't extend files */
258 if (pwrite( unix_fd, &zero, 1, size ) != -1)
260 ftruncate( unix_fd, size );
261 return 1;
263 file_set_error();
264 return 0;
267 /* simplified version of mkstemps() */
268 static int make_temp_file( char name[16] )
270 static unsigned int value;
271 int i, fd = -1;
273 value += (current_time >> 16) + current_time;
274 for (i = 0; i < 0x8000 && fd < 0; i++, value += 7777)
276 sprintf( name, "tmpmap-%08x", value );
277 fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
279 return fd;
282 /* check if the current directory allows exec mappings */
283 static int check_current_dir_for_exec(void)
285 int fd;
286 char tmpfn[16];
287 void *ret = MAP_FAILED;
289 fd = make_temp_file( tmpfn );
290 if (fd == -1) return 0;
291 if (grow_file( fd, 1 ))
293 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
294 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
296 close( fd );
297 unlink( tmpfn );
298 return (ret != MAP_FAILED);
301 /* create a temp file for anonymous mappings */
302 static int create_temp_file( file_pos_t size )
304 static int temp_dir_fd = -1;
305 char tmpfn[16];
306 int fd;
308 if (temp_dir_fd == -1)
310 temp_dir_fd = server_dir_fd;
311 if (!check_current_dir_for_exec())
313 /* the server dir is noexec, try the config dir instead */
314 fchdir( config_dir_fd );
315 if (check_current_dir_for_exec())
316 temp_dir_fd = config_dir_fd;
317 else /* neither works, fall back to server dir */
318 fchdir( server_dir_fd );
321 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
323 fd = make_temp_file( tmpfn );
324 if (fd != -1)
326 if (!grow_file( fd, size ))
328 close( fd );
329 fd = -1;
331 unlink( tmpfn );
333 else file_set_error();
335 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
336 return fd;
339 /* find a memory view from its base address */
340 struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
342 struct memory_view *view;
344 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
345 if (view->base == base) return view;
347 set_error( STATUS_NOT_MAPPED_VIEW );
348 return NULL;
351 /* find a memory view from any address inside it */
352 static struct memory_view *find_mapped_addr( struct process *process, client_ptr_t addr )
354 struct memory_view *view;
356 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
357 if (addr >= view->base && addr < view->base + view->size) return view;
359 set_error( STATUS_NOT_MAPPED_VIEW );
360 return NULL;
363 /* get the main exe memory view */
364 struct memory_view *get_exe_view( struct process *process )
366 return LIST_ENTRY( list_head( &process->views ), struct memory_view, entry );
369 static void set_process_machine( struct process *process, struct memory_view *view )
371 unsigned short machine = view->image.machine;
373 if (machine == IMAGE_FILE_MACHINE_I386 && (view->image.image_flags & IMAGE_FLAGS_ComPlusNativeReady))
375 if (is_machine_supported( IMAGE_FILE_MACHINE_AMD64 )) machine = IMAGE_FILE_MACHINE_AMD64;
376 else if (is_machine_supported( IMAGE_FILE_MACHINE_ARM64 )) machine = IMAGE_FILE_MACHINE_ARM64;
378 process->machine = machine;
381 static int generate_dll_event( struct thread *thread, int code, struct memory_view *view )
383 unsigned short process_machine = thread->process->machine;
385 if (!(view->flags & SEC_IMAGE)) return 0;
386 if (process_machine != native_machine && process_machine != view->image.machine) return 0;
387 generate_debug_event( thread, code, view );
388 return 1;
391 /* add a view to the process list */
392 static void add_process_view( struct thread *thread, struct memory_view *view )
394 struct process *process = thread->process;
395 struct unicode_str name;
397 if (view->flags & SEC_IMAGE)
399 if (is_process_init_done( process ))
401 generate_dll_event( thread, DbgLoadDllStateChange, view );
403 else if (!(view->image.image_charact & IMAGE_FILE_DLL))
405 /* main exe */
406 set_process_machine( process, view );
407 list_add_head( &process->views, &view->entry );
409 free( process->image );
410 process->image = NULL;
411 if (get_view_nt_name( view, &name ) && (process->image = memdup( name.str, name.len )))
412 process->imagelen = name.len;
413 return;
416 list_add_tail( &process->views, &view->entry );
419 static void free_memory_view( struct memory_view *view )
421 if (view->fd) release_object( view->fd );
422 if (view->committed) release_object( view->committed );
423 if (view->shared) release_object( view->shared );
424 list_remove( &view->entry );
425 free( view );
428 /* free all mapped views at process exit */
429 void free_mapped_views( struct process *process )
431 struct list *ptr;
433 while ((ptr = list_head( &process->views )))
434 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
437 /* find the shared PE mapping for a given mapping */
438 static struct shared_map *get_shared_file( struct fd *fd )
440 struct shared_map *ptr;
442 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
443 if (is_same_file_fd( ptr->fd, fd ))
444 return (struct shared_map *)grab_object( ptr );
445 return NULL;
448 /* return the size of the memory mapping and file range of a given section */
449 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
450 off_t *file_start, size_t *file_size )
452 static const unsigned int sector_align = 0x1ff;
454 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
455 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
457 *file_start = sec->PointerToRawData & ~sector_align;
458 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
459 if (*file_size > *map_size) *file_size = *map_size;
462 /* add a range to the committed list */
463 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
465 unsigned int i, j;
466 struct ranges *committed = view->committed;
467 struct range *ranges;
469 if ((start & page_mask) || (end & page_mask) ||
470 start >= view->size || end >= view->size ||
471 start >= end)
473 set_error( STATUS_INVALID_PARAMETER );
474 return;
477 if (!committed) return; /* everything committed already */
479 start += view->start;
480 end += view->start;
482 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
484 if (ranges[i].start > end) break;
485 if (ranges[i].end < start) continue;
486 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
487 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
489 for (j = i + 1; j < committed->count; j++)
491 if (ranges[j].start > end) break;
492 if (ranges[j].end > end) end = ranges[j].end;
494 if (j > i + 1)
496 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
497 committed->count -= j - (i + 1);
499 ranges[i].end = end;
501 return;
504 /* now add a new range */
506 if (committed->count == committed->max)
508 unsigned int new_size = committed->max * 2;
509 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
510 if (!new_ptr) return;
511 committed->max = new_size;
512 ranges = committed->ranges = new_ptr;
514 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
515 ranges[i].start = start;
516 ranges[i].end = end;
517 committed->count++;
520 /* find the range containing start and return whether it's committed */
521 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
523 unsigned int i;
524 struct ranges *committed = view->committed;
525 struct range *ranges;
527 if ((start & page_mask) || start >= view->size)
529 set_error( STATUS_INVALID_PARAMETER );
530 return 0;
532 if (!committed) /* everything is committed */
534 *size = view->size - start;
535 return 1;
537 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
539 if (ranges[i].start > view->start + start)
541 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
542 return 0;
544 if (ranges[i].end > view->start + start)
546 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
547 return 1;
550 *size = view->size - start;
551 return 0;
554 /* allocate and fill the temp file for a shared PE image mapping */
555 static int build_shared_mapping( struct mapping *mapping, int fd,
556 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
558 struct shared_map *shared;
559 struct file *file;
560 unsigned int i;
561 mem_size_t total_size;
562 size_t file_size, map_size, max_size;
563 off_t shared_pos, read_pos, write_pos;
564 char *buffer = NULL;
565 int shared_fd;
566 long toread;
568 /* compute the total size of the shared mapping */
570 total_size = max_size = 0;
571 for (i = 0; i < nb_sec; i++)
573 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
574 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
576 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
577 if (file_size > max_size) max_size = file_size;
578 total_size += map_size;
581 if (!total_size) return 1; /* nothing to do */
583 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
585 /* create a temp file for the mapping */
587 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
588 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
590 if (!(buffer = malloc( max_size ))) goto error;
592 /* copy the shared sections data into the temp file */
594 shared_pos = 0;
595 for (i = 0; i < nb_sec; i++)
597 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
598 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
599 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
600 write_pos = shared_pos;
601 shared_pos += map_size;
602 if (!sec[i].PointerToRawData || !file_size) continue;
603 toread = file_size;
604 while (toread)
606 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
607 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
609 file_size -= toread;
610 break;
612 if (res <= 0) goto error;
613 toread -= res;
614 read_pos += res;
616 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
619 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
620 shared->fd = (struct fd *)grab_object( mapping->fd );
621 shared->file = file;
622 list_add_head( &shared_map_list, &shared->entry );
623 mapping->shared = shared;
624 free( buffer );
625 return 1;
627 error:
628 release_object( file );
629 free( buffer );
630 return 0;
633 /* load the CLR header from its section */
634 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
635 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
637 ssize_t ret;
638 size_t map_size, file_size;
639 off_t file_start;
640 unsigned int i;
642 if (!va || !size) return 0;
644 for (i = 0; i < nb_sec; i++)
646 if (va < sec[i].VirtualAddress) continue;
647 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
648 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
649 if (size >= map_size) continue;
650 if (va - sec[i].VirtualAddress >= map_size - size) continue;
651 file_size = min( file_size, map_size );
652 size = min( size, sizeof(*hdr) );
653 ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
654 if (ret <= 0) break;
655 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
656 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
657 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
658 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
660 return 0;
663 /* retrieve the mapping parameters for an executable (PE) image */
664 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
666 static const char builtin_signature[] = "Wine builtin DLL";
667 static const char fakedll_signature[] = "Wine placeholder DLL";
669 IMAGE_COR20_HEADER clr;
670 IMAGE_SECTION_HEADER sec[96];
671 struct
673 IMAGE_DOS_HEADER dos;
674 char buffer[32];
675 } mz;
676 struct
678 DWORD Signature;
679 IMAGE_FILE_HEADER FileHeader;
680 union
682 IMAGE_OPTIONAL_HEADER32 hdr32;
683 IMAGE_OPTIONAL_HEADER64 hdr64;
684 } opt;
685 } nt;
686 off_t pos;
687 int size, opt_size;
688 size_t mz_size, clr_va, clr_size;
689 unsigned int i;
691 /* load the headers */
693 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
694 size = pread( unix_fd, &mz, sizeof(mz), 0 );
695 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
696 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
697 mz_size = size;
698 pos = mz.dos.e_lfanew;
700 size = pread( unix_fd, &nt, sizeof(nt), pos );
701 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
702 /* zero out Optional header in the case it's not present or partial */
703 opt_size = max( nt.FileHeader.SizeOfOptionalHeader, offsetof( IMAGE_OPTIONAL_HEADER32, CheckSum ));
704 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + opt_size );
705 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
706 if (nt.Signature != IMAGE_NT_SIGNATURE)
708 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
709 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
710 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
711 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
712 return STATUS_INVALID_IMAGE_NE_FORMAT;
715 switch (nt.opt.hdr32.Magic)
717 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
718 if (!is_machine_32bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
719 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
721 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
722 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
724 mapping->image.base = nt.opt.hdr32.ImageBase;
725 mapping->image.entry_point = nt.opt.hdr32.AddressOfEntryPoint;
726 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
727 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
728 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
729 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
730 mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
731 mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
732 mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
733 mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
734 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
735 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
736 nt.opt.hdr32.AddressOfEntryPoint ||
737 nt.opt.hdr32.SectionAlignment & page_mask);
738 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
739 mapping->image.checksum = nt.opt.hdr32.CheckSum;
740 mapping->image.image_flags = 0;
741 if (nt.opt.hdr32.SectionAlignment & page_mask)
742 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
743 if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
744 mapping->image.contains_code && !(clr_va && clr_size))
745 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
746 break;
748 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
749 if (!is_machine_64bit( native_machine )) return STATUS_INVALID_IMAGE_WIN_64;
750 if (!is_machine_64bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
751 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
753 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
754 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
756 mapping->image.base = nt.opt.hdr64.ImageBase;
757 mapping->image.entry_point = nt.opt.hdr64.AddressOfEntryPoint;
758 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
759 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
760 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
761 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
762 mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
763 mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
764 mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
765 mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
766 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
767 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
768 nt.opt.hdr64.AddressOfEntryPoint ||
769 nt.opt.hdr64.SectionAlignment & page_mask);
770 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
771 mapping->image.checksum = nt.opt.hdr64.CheckSum;
772 mapping->image.image_flags = 0;
773 if (nt.opt.hdr64.SectionAlignment & page_mask)
774 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
775 if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
776 mapping->image.contains_code && !(clr_va && clr_size))
777 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
778 break;
780 default:
781 return STATUS_INVALID_IMAGE_FORMAT;
784 mapping->image.image_charact = nt.FileHeader.Characteristics;
785 mapping->image.machine = nt.FileHeader.Machine;
786 mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
787 mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
788 mapping->image.zerobits = 0; /* FIXME */
789 mapping->image.file_size = file_size;
790 mapping->image.loader_flags = clr_va && clr_size;
791 if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ))
792 mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin;
793 else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
794 mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
796 /* load the section headers */
798 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
799 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
800 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
801 if (!mapping->size) mapping->size = mapping->image.map_size;
802 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
803 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
804 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
805 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
807 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
808 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
810 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
811 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
813 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
814 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
816 if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
817 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
818 if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
819 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
823 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
824 return STATUS_INVALID_FILE_FOR_SECTION;
826 return STATUS_SUCCESS;
829 static struct ranges *create_ranges(void)
831 struct ranges *ranges = alloc_object( &ranges_ops );
833 if (!ranges) return NULL;
834 ranges->count = 0;
835 ranges->max = 8;
836 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
838 release_object( ranges );
839 return NULL;
841 return ranges;
844 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
846 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
848 case SEC_IMAGE:
849 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
850 if (handle) return SEC_FILE | SEC_IMAGE;
851 set_error( STATUS_INVALID_FILE_FOR_SECTION );
852 return 0;
853 case SEC_COMMIT:
854 if (!handle) return flags;
855 /* fall through */
856 case SEC_RESERVE:
857 if (flags & SEC_LARGE_PAGES) break;
858 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
859 return flags;
861 set_error( STATUS_INVALID_PARAMETER );
862 return 0;
866 static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
867 unsigned int attr, mem_size_t size, unsigned int flags,
868 obj_handle_t handle, unsigned int file_access,
869 const struct security_descriptor *sd )
871 struct mapping *mapping;
872 struct file *file;
873 struct fd *fd;
874 int unix_fd;
875 struct stat st;
877 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
879 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
880 return NULL;
881 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
882 return mapping; /* Nothing else to do */
884 mapping->size = size;
885 mapping->fd = NULL;
886 mapping->shared = NULL;
887 mapping->committed = NULL;
889 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
891 if (handle)
893 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
894 unsigned int mapping_access = FILE_MAPPING_ACCESS;
896 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
897 fd = get_obj_fd( (struct object *)file );
899 /* file sharing rules for mappings are different so we use magic the access rights */
900 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
901 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
903 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
905 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
906 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
908 release_object( file );
909 release_object( fd );
910 if (!mapping->fd) goto error;
912 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
913 if (fstat( unix_fd, &st ) == -1)
915 file_set_error();
916 goto error;
918 if (flags & SEC_IMAGE)
920 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
921 if (!err) return mapping;
922 set_error( err );
923 goto error;
925 if (!mapping->size)
927 if (!(mapping->size = st.st_size))
929 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
930 goto error;
933 else if (st.st_size < mapping->size)
935 if (!(file_access & FILE_WRITE_DATA))
937 set_error( STATUS_SECTION_TOO_BIG );
938 goto error;
940 if (!grow_file( unix_fd, mapping->size )) goto error;
943 else /* Anonymous mapping (no associated file) */
945 if (!mapping->size)
947 set_error( STATUS_INVALID_PARAMETER );
948 goto error;
950 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
951 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
952 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
953 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
954 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
955 allow_fd_caching( mapping->fd );
957 return mapping;
959 error:
960 release_object( mapping );
961 return NULL;
964 /* create a read-only file mapping for the specified fd */
965 struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
966 struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
968 struct mapping *mapping;
969 int unix_fd;
970 struct stat st;
972 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
973 if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
975 mapping->shared = NULL;
976 mapping->committed = NULL;
977 mapping->flags = SEC_FILE;
978 mapping->fd = (struct fd *)grab_object( fd );
979 set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
981 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
982 if (fstat( unix_fd, &st ) == -1)
984 file_set_error();
985 goto error;
987 if (!(mapping->size = st.st_size))
989 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
990 goto error;
992 return mapping;
994 error:
995 release_object( mapping );
996 return NULL;
999 static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
1001 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
1004 /* open a new file for the file descriptor backing the view */
1005 struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
1007 if (!view->fd) return NULL;
1008 return create_file_for_fd_obj( view->fd, access, sharing );
1011 /* get the image info for a SEC_IMAGE mapped view */
1012 const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
1014 if (!(view->flags & SEC_IMAGE)) return NULL;
1015 *base = view->base;
1016 return &view->image;
1019 /* get the file name for a mapped view */
1020 int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
1022 if (view->namelen) /* .so builtin */
1024 name->str = view->name;
1025 name->len = view->namelen;
1026 return 1;
1028 if (!view->fd) return 0;
1029 get_nt_name( view->fd, name );
1030 return 1;
1033 /* generate all startup events of a given process */
1034 void generate_startup_debug_events( struct process *process )
1036 struct memory_view *view;
1037 struct list *ptr = list_head( &process->views );
1038 struct thread *thread, *first_thread = get_process_first_thread( process );
1040 if (!ptr) return;
1041 view = LIST_ENTRY( ptr, struct memory_view, entry );
1042 generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
1044 /* generate ntdll.dll load event */
1045 while (ptr && (ptr = list_next( &process->views, ptr )))
1047 view = LIST_ENTRY( ptr, struct memory_view, entry );
1048 if (generate_dll_event( first_thread, DbgLoadDllStateChange, view )) break;
1051 /* generate creation events */
1052 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1054 if (thread != first_thread)
1055 generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
1058 /* generate dll events (in loading order) */
1059 while (ptr && (ptr = list_next( &process->views, ptr )))
1061 view = LIST_ENTRY( ptr, struct memory_view, entry );
1062 generate_dll_event( first_thread, DbgLoadDllStateChange, view );
1066 static void mapping_dump( struct object *obj, int verbose )
1068 struct mapping *mapping = (struct mapping *)obj;
1069 assert( obj->ops == &mapping_ops );
1070 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
1071 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
1072 mapping->flags, mapping->fd, mapping->shared );
1075 static struct fd *mapping_get_fd( struct object *obj )
1077 struct mapping *mapping = (struct mapping *)obj;
1078 return (struct fd *)grab_object( mapping->fd );
1081 static void mapping_destroy( struct object *obj )
1083 struct mapping *mapping = (struct mapping *)obj;
1084 assert( obj->ops == &mapping_ops );
1085 if (mapping->fd) release_object( mapping->fd );
1086 if (mapping->committed) release_object( mapping->committed );
1087 if (mapping->shared) release_object( mapping->shared );
1090 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
1092 return FD_TYPE_FILE;
1095 int get_page_size(void)
1097 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
1098 return page_mask + 1;
1101 struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
1102 unsigned int attr, const struct security_descriptor *sd )
1104 void *ptr;
1105 struct mapping *mapping;
1107 if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
1108 SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
1109 ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
1110 if (ptr != MAP_FAILED)
1112 user_shared_data = ptr;
1113 user_shared_data->SystemCall = 1;
1115 return &mapping->obj;
1118 /* create a file mapping */
1119 DECL_HANDLER(create_mapping)
1121 struct object *root;
1122 struct mapping *mapping;
1123 struct unicode_str name;
1124 const struct security_descriptor *sd;
1125 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1127 if (!objattr) return;
1129 if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
1130 req->file_handle, req->file_access, sd )))
1132 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1133 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
1134 else
1135 reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
1136 req->access, objattr->attributes );
1137 release_object( mapping );
1140 if (root) release_object( root );
1143 /* open a handle to a mapping */
1144 DECL_HANDLER(open_mapping)
1146 struct unicode_str name = get_req_unicode_str();
1148 reply->handle = open_object( current->process, req->rootdir, req->access,
1149 &mapping_ops, &name, req->attributes );
1152 /* get a mapping information */
1153 DECL_HANDLER(get_mapping_info)
1155 struct mapping *mapping;
1157 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
1159 reply->size = mapping->size;
1160 reply->flags = mapping->flags;
1162 if (mapping->flags & SEC_IMAGE)
1164 struct unicode_str name = { NULL, 0 };
1165 data_size_t size;
1166 void *data;
1168 if (mapping->fd) get_nt_name( mapping->fd, &name );
1169 size = min( sizeof(pe_image_info_t) + name.len, get_reply_max_size() );
1170 if ((data = set_reply_data_size( size )))
1172 memcpy( data, &mapping->image, min( sizeof(pe_image_info_t), size ));
1173 if (size > sizeof(pe_image_info_t))
1174 memcpy( (pe_image_info_t *)data + 1, name.str, size - sizeof(pe_image_info_t) );
1176 reply->total = sizeof(pe_image_info_t) + name.len;
1179 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
1181 release_object( mapping );
1182 return;
1185 if (mapping->shared)
1186 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1187 GENERIC_READ|GENERIC_WRITE, 0 );
1188 release_object( mapping );
1191 /* add a memory view in the current process */
1192 DECL_HANDLER(map_view)
1194 struct mapping *mapping = NULL;
1195 struct memory_view *view;
1196 data_size_t namelen = 0;
1198 if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
1200 set_error( STATUS_INVALID_PARAMETER );
1201 return;
1204 /* make sure we don't already have an overlapping view */
1205 LIST_FOR_EACH_ENTRY( view, &current->process->views, struct memory_view, entry )
1207 if (view->base + view->size <= req->base) continue;
1208 if (view->base >= req->base + req->size) continue;
1209 set_error( STATUS_INVALID_PARAMETER );
1210 return;
1213 if (!req->mapping) /* image mapping for a .so dll */
1215 if (get_req_data_size() > sizeof(view->image)) namelen = get_req_data_size() - sizeof(view->image);
1216 if (!(view = mem_alloc( offsetof( struct memory_view, name[namelen] )))) return;
1217 memset( view, 0, sizeof(*view) );
1218 view->base = req->base;
1219 view->size = req->size;
1220 view->start = req->start;
1221 view->flags = SEC_IMAGE;
1222 view->namelen = namelen;
1223 memcpy( &view->image, get_req_data(), min( sizeof(view->image), get_req_data_size() ));
1224 memcpy( view->name, (pe_image_info_t *)get_req_data() + 1, namelen );
1225 add_process_view( current, view );
1226 return;
1229 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1231 if (mapping->flags & SEC_IMAGE)
1233 if (req->start || req->size > mapping->image.map_size)
1235 set_error( STATUS_INVALID_PARAMETER );
1236 goto done;
1239 else if (req->start >= mapping->size ||
1240 req->start + req->size < req->start ||
1241 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1243 set_error( STATUS_INVALID_PARAMETER );
1244 goto done;
1247 if ((view = mem_alloc( offsetof( struct memory_view, name[namelen] ))))
1249 view->base = req->base;
1250 view->size = req->size;
1251 view->start = req->start;
1252 view->flags = mapping->flags;
1253 view->namelen = namelen;
1254 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1255 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1256 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1257 if (view->flags & SEC_IMAGE) view->image = mapping->image;
1258 add_process_view( current, view );
1259 if (view->flags & SEC_IMAGE && view->base != mapping->image.base)
1260 set_error( STATUS_IMAGE_NOT_AT_BASE );
1263 done:
1264 release_object( mapping );
1267 /* unmap a memory view from the current process */
1268 DECL_HANDLER(unmap_view)
1270 struct memory_view *view = find_mapped_view( current->process, req->base );
1272 if (!view) return;
1273 generate_dll_event( current, DbgUnloadDllStateChange, view );
1274 free_memory_view( view );
1277 /* get a range of committed pages in a file mapping */
1278 DECL_HANDLER(get_mapping_committed_range)
1280 struct memory_view *view = find_mapped_view( current->process, req->base );
1282 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1285 /* add a range to the committed pages in a file mapping */
1286 DECL_HANDLER(add_mapping_committed_range)
1288 struct memory_view *view = find_mapped_view( current->process, req->base );
1290 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1293 /* check if two memory maps are for the same file */
1294 DECL_HANDLER(is_same_mapping)
1296 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1297 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1299 if (!view1 || !view2) return;
1300 if (!view1->fd || !view2->fd || !(view1->flags & SEC_IMAGE) || !is_same_file_fd( view1->fd, view2->fd ))
1301 set_error( STATUS_NOT_SAME_DEVICE );
1304 /* get the filename of a mapping */
1305 DECL_HANDLER(get_mapping_filename)
1307 struct process *process;
1308 struct memory_view *view;
1309 struct unicode_str name;
1311 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1313 if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
1315 reply->len = name.len;
1316 if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
1317 else if (!name.len) set_error( STATUS_FILE_INVALID );
1318 else set_reply_data( name.str, name.len );
1320 else set_error( STATUS_INVALID_ADDRESS );
1322 release_object( process );