wined3d: Don't validate the frontbuffer's DRAWABLE location in wined3d_swapchain_resi...
[wine.git] / server / mapping.c
blob5d4df2050e433f8af14ae38d54284a34b4305f1a
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 #include <sys/mman.h>
31 #include <unistd.h>
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winternl.h"
37 #include "ddk/wdm.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 &no_type, /* type */
66 ranges_dump, /* dump */
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 default_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_get_full_name, /* get_full_name */
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 &no_type, /* type */
102 shared_map_dump, /* dump */
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 default_map_access, /* map_access */
110 default_get_sd, /* get_sd */
111 default_set_sd, /* set_sd */
112 no_get_full_name, /* get_full_name */
113 no_lookup_name, /* lookup_name */
114 no_link_name, /* link_name */
115 NULL, /* unlink_name */
116 no_open_file, /* open_file */
117 no_kernel_obj_list, /* get_kernel_obj_list */
118 no_close_handle, /* close_handle */
119 shared_map_destroy /* destroy */
122 static struct list shared_map_list = LIST_INIT( shared_map_list );
124 /* memory view mapped in client address space */
125 struct memory_view
127 struct list entry; /* entry in per-process view list */
128 struct fd *fd; /* fd for mapped file */
129 struct ranges *committed; /* list of committed ranges in this mapping */
130 struct shared_map *shared; /* temp file for shared PE mapping */
131 pe_image_info_t image; /* image info (for PE image mapping) */
132 unsigned int flags; /* SEC_* flags */
133 client_ptr_t base; /* view base address (in process addr space) */
134 mem_size_t size; /* view size */
135 file_pos_t start; /* start offset in mapping */
136 data_size_t namelen;
137 WCHAR name[1]; /* filename for .so dll image views */
141 static const WCHAR mapping_name[] = {'S','e','c','t','i','o','n'};
143 struct type_descr mapping_type =
145 { mapping_name, sizeof(mapping_name) }, /* name */
146 SECTION_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
147 { /* mapping */
148 STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ,
149 STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
150 STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
151 SECTION_ALL_ACCESS
155 struct mapping
157 struct object obj; /* object header */
158 mem_size_t size; /* mapping size */
159 unsigned int flags; /* SEC_* flags */
160 struct fd *fd; /* fd for mapped file */
161 pe_image_info_t image; /* image info (for PE image mapping) */
162 struct ranges *committed; /* list of committed ranges in this mapping */
163 struct shared_map *shared; /* temp file for shared PE mapping */
166 static void mapping_dump( struct object *obj, int verbose );
167 static struct fd *mapping_get_fd( struct object *obj );
168 static void mapping_destroy( struct object *obj );
169 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
171 static const struct object_ops mapping_ops =
173 sizeof(struct mapping), /* size */
174 &mapping_type, /* type */
175 mapping_dump, /* dump */
176 no_add_queue, /* add_queue */
177 NULL, /* remove_queue */
178 NULL, /* signaled */
179 NULL, /* satisfied */
180 no_signal, /* signal */
181 mapping_get_fd, /* get_fd */
182 default_map_access, /* map_access */
183 default_get_sd, /* get_sd */
184 default_set_sd, /* set_sd */
185 default_get_full_name, /* get_full_name */
186 no_lookup_name, /* lookup_name */
187 directory_link_name, /* link_name */
188 default_unlink_name, /* unlink_name */
189 no_open_file, /* open_file */
190 no_kernel_obj_list, /* get_kernel_obj_list */
191 no_close_handle, /* close_handle */
192 mapping_destroy /* destroy */
195 static const struct fd_ops mapping_fd_ops =
197 default_fd_get_poll_events, /* get_poll_events */
198 default_poll_event, /* poll_event */
199 mapping_get_fd_type, /* get_fd_type */
200 no_fd_read, /* read */
201 no_fd_write, /* write */
202 no_fd_flush, /* flush */
203 no_fd_get_file_info, /* get_file_info */
204 no_fd_get_volume_info, /* get_volume_info */
205 no_fd_ioctl, /* ioctl */
206 default_fd_cancel_async, /* cancel_async */
207 no_fd_queue_async, /* queue_async */
208 default_fd_reselect_async /* reselect_async */
211 static size_t page_mask;
213 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
216 static void ranges_dump( struct object *obj, int verbose )
218 struct ranges *ranges = (struct ranges *)obj;
219 fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
222 static void ranges_destroy( struct object *obj )
224 struct ranges *ranges = (struct ranges *)obj;
225 free( ranges->ranges );
228 static void shared_map_dump( struct object *obj, int verbose )
230 struct shared_map *shared = (struct shared_map *)obj;
231 fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
234 static void shared_map_destroy( struct object *obj )
236 struct shared_map *shared = (struct shared_map *)obj;
238 release_object( shared->fd );
239 release_object( shared->file );
240 list_remove( &shared->entry );
243 /* extend a file beyond the current end of file */
244 int grow_file( int unix_fd, file_pos_t new_size )
246 static const char zero;
247 off_t size = new_size;
249 if (sizeof(new_size) > sizeof(size) && size != new_size)
251 set_error( STATUS_INVALID_PARAMETER );
252 return 0;
254 /* extend the file one byte beyond the requested size and then truncate it */
255 /* this should work around ftruncate implementations that can't extend files */
256 if (pwrite( unix_fd, &zero, 1, size ) != -1)
258 ftruncate( unix_fd, size );
259 return 1;
261 file_set_error();
262 return 0;
265 /* simplified version of mkstemps() */
266 static int make_temp_file( char name[16] )
268 static unsigned int value;
269 int i, fd = -1;
271 value += (current_time >> 16) + current_time;
272 for (i = 0; i < 0x8000 && fd < 0; i++, value += 7777)
274 sprintf( name, "tmpmap-%08x", value );
275 fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
277 return fd;
280 /* check if the current directory allows exec mappings */
281 static int check_current_dir_for_exec(void)
283 int fd;
284 char tmpfn[16];
285 void *ret = MAP_FAILED;
287 fd = make_temp_file( tmpfn );
288 if (fd == -1) return 0;
289 if (grow_file( fd, 1 ))
291 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
292 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
294 close( fd );
295 unlink( tmpfn );
296 return (ret != MAP_FAILED);
299 /* create a temp file for anonymous mappings */
300 static int create_temp_file( file_pos_t size )
302 static int temp_dir_fd = -1;
303 char tmpfn[16];
304 int fd;
306 if (temp_dir_fd == -1)
308 temp_dir_fd = server_dir_fd;
309 if (!check_current_dir_for_exec())
311 /* the server dir is noexec, try the config dir instead */
312 fchdir( config_dir_fd );
313 if (check_current_dir_for_exec())
314 temp_dir_fd = config_dir_fd;
315 else /* neither works, fall back to server dir */
316 fchdir( server_dir_fd );
319 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
321 fd = make_temp_file( tmpfn );
322 if (fd != -1)
324 if (!grow_file( fd, size ))
326 close( fd );
327 fd = -1;
329 unlink( tmpfn );
331 else file_set_error();
333 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
334 return fd;
337 /* find a memory view from its base address */
338 struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
340 struct memory_view *view;
342 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
343 if (view->base == base) return view;
345 set_error( STATUS_NOT_MAPPED_VIEW );
346 return NULL;
349 /* find a memory view from any address inside it */
350 static struct memory_view *find_mapped_addr( struct process *process, client_ptr_t addr )
352 struct memory_view *view;
354 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
355 if (addr >= view->base && addr < view->base + view->size) return view;
357 set_error( STATUS_NOT_MAPPED_VIEW );
358 return NULL;
361 /* check if an address range is valid for creating a view */
362 static int is_valid_view_addr( struct process *process, client_ptr_t addr, mem_size_t size )
364 struct memory_view *view;
366 if (!size) return 0;
367 if (addr & page_mask) return 0;
368 if (addr + size < addr) return 0; /* overflow */
370 /* check for overlapping view */
371 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
373 if (view->base + view->size <= addr) continue;
374 if (view->base >= addr + size) continue;
375 return 0;
377 return 1;
380 /* get the main exe memory view */
381 struct memory_view *get_exe_view( struct process *process )
383 return LIST_ENTRY( list_head( &process->views ), struct memory_view, entry );
386 static void set_process_machine( struct process *process, struct memory_view *view )
388 if (view->image.image_flags & IMAGE_FLAGS_ComPlusNativeReady)
389 process->machine = native_machine;
390 else
391 process->machine = view->image.machine;
394 static int generate_dll_event( struct thread *thread, int code, struct memory_view *view )
396 if (!(view->flags & SEC_IMAGE)) return 0;
397 generate_debug_event( thread, code, view );
398 return 1;
401 /* add a view to the process list */
402 static void add_process_view( struct thread *thread, struct memory_view *view )
404 struct process *process = thread->process;
405 struct unicode_str name;
407 if (view->flags & SEC_IMAGE)
409 if (is_process_init_done( process ))
411 generate_dll_event( thread, DbgLoadDllStateChange, view );
413 else if (!(view->image.image_charact & IMAGE_FILE_DLL))
415 /* main exe */
416 set_process_machine( process, view );
417 list_add_head( &process->views, &view->entry );
419 free( process->image );
420 process->image = NULL;
421 if (get_view_nt_name( view, &name ) && (process->image = memdup( name.str, name.len )))
422 process->imagelen = name.len;
423 process->image_info = view->image;
424 return;
427 list_add_tail( &process->views, &view->entry );
430 static void free_memory_view( struct memory_view *view )
432 if (view->fd) release_object( view->fd );
433 if (view->committed) release_object( view->committed );
434 if (view->shared) release_object( view->shared );
435 list_remove( &view->entry );
436 free( view );
439 /* free all mapped views at process exit */
440 void free_mapped_views( struct process *process )
442 struct list *ptr;
444 while ((ptr = list_head( &process->views )))
445 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
448 /* find the shared PE mapping for a given mapping */
449 static struct shared_map *get_shared_file( struct fd *fd )
451 struct shared_map *ptr;
453 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
454 if (is_same_file_fd( ptr->fd, fd ))
455 return (struct shared_map *)grab_object( ptr );
456 return NULL;
459 /* return the size of the memory mapping and file range of a given section */
460 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
461 off_t *file_start, size_t *file_size )
463 static const unsigned int sector_align = 0x1ff;
465 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
466 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
468 *file_start = sec->PointerToRawData & ~sector_align;
469 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
470 if (*file_size > *map_size) *file_size = *map_size;
473 /* add a range to the committed list */
474 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
476 unsigned int i, j;
477 struct ranges *committed = view->committed;
478 struct range *ranges;
480 if ((start & page_mask) || (end & page_mask) ||
481 start >= view->size || end >= view->size ||
482 start >= end)
484 set_error( STATUS_INVALID_PARAMETER );
485 return;
488 if (!committed) return; /* everything committed already */
490 start += view->start;
491 end += view->start;
493 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
495 if (ranges[i].start > end) break;
496 if (ranges[i].end < start) continue;
497 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
498 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
500 for (j = i + 1; j < committed->count; j++)
502 if (ranges[j].start > end) break;
503 if (ranges[j].end > end) end = ranges[j].end;
505 if (j > i + 1)
507 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
508 committed->count -= j - (i + 1);
510 ranges[i].end = end;
512 return;
515 /* now add a new range */
517 if (committed->count == committed->max)
519 unsigned int new_size = committed->max * 2;
520 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
521 if (!new_ptr) return;
522 committed->max = new_size;
523 ranges = committed->ranges = new_ptr;
525 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
526 ranges[i].start = start;
527 ranges[i].end = end;
528 committed->count++;
531 /* find the range containing start and return whether it's committed */
532 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
534 unsigned int i;
535 struct ranges *committed = view->committed;
536 struct range *ranges;
538 if ((start & page_mask) || start >= view->size)
540 set_error( STATUS_INVALID_PARAMETER );
541 return 0;
543 if (!committed) /* everything is committed */
545 *size = view->size - start;
546 return 1;
548 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
550 if (ranges[i].start > view->start + start)
552 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
553 return 0;
555 if (ranges[i].end > view->start + start)
557 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
558 return 1;
561 *size = view->size - start;
562 return 0;
565 /* allocate and fill the temp file for a shared PE image mapping */
566 static int build_shared_mapping( struct mapping *mapping, int fd,
567 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
569 struct shared_map *shared;
570 struct file *file;
571 unsigned int i;
572 mem_size_t total_size;
573 size_t file_size, map_size, max_size;
574 off_t shared_pos, read_pos, write_pos;
575 char *buffer = NULL;
576 int shared_fd;
577 long toread;
579 /* compute the total size of the shared mapping */
581 total_size = max_size = 0;
582 for (i = 0; i < nb_sec; i++)
584 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
585 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
587 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
588 if (file_size > max_size) max_size = file_size;
589 total_size += map_size;
592 if (!total_size) return 1; /* nothing to do */
594 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
596 /* create a temp file for the mapping */
598 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
599 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
601 if (!(buffer = malloc( max_size ))) goto error;
603 /* copy the shared sections data into the temp file */
605 shared_pos = 0;
606 for (i = 0; i < nb_sec; i++)
608 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
609 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
610 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
611 write_pos = shared_pos;
612 shared_pos += map_size;
613 if (!sec[i].PointerToRawData || !file_size) continue;
614 toread = file_size;
615 while (toread)
617 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
618 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
620 file_size -= toread;
621 break;
623 if (res <= 0) goto error;
624 toread -= res;
625 read_pos += res;
627 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
630 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
631 shared->fd = (struct fd *)grab_object( mapping->fd );
632 shared->file = file;
633 list_add_head( &shared_map_list, &shared->entry );
634 mapping->shared = shared;
635 free( buffer );
636 return 1;
638 error:
639 release_object( file );
640 free( buffer );
641 return 0;
644 /* load the CLR header from its section */
645 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
646 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
648 ssize_t ret;
649 size_t map_size, file_size;
650 off_t file_start;
651 unsigned int i;
653 if (!va || !size) return 0;
655 for (i = 0; i < nb_sec; i++)
657 if (va < sec[i].VirtualAddress) continue;
658 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
659 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
660 if (size >= map_size) continue;
661 if (va - sec[i].VirtualAddress >= map_size - size) continue;
662 file_size = min( file_size, map_size );
663 size = min( size, sizeof(*hdr) );
664 ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
665 if (ret <= 0) break;
666 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
667 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
668 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
669 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
671 return 0;
674 /* retrieve the mapping parameters for an executable (PE) image */
675 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
677 static const char builtin_signature[] = "Wine builtin DLL";
678 static const char fakedll_signature[] = "Wine placeholder DLL";
680 IMAGE_COR20_HEADER clr;
681 IMAGE_SECTION_HEADER sec[96];
682 struct
684 IMAGE_DOS_HEADER dos;
685 char buffer[32];
686 } mz;
687 struct
689 DWORD Signature;
690 IMAGE_FILE_HEADER FileHeader;
691 union
693 IMAGE_OPTIONAL_HEADER32 hdr32;
694 IMAGE_OPTIONAL_HEADER64 hdr64;
695 } opt;
696 } nt;
697 off_t pos;
698 int size, opt_size;
699 size_t mz_size, clr_va, clr_size;
700 unsigned int i;
702 /* load the headers */
704 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
705 size = pread( unix_fd, &mz, sizeof(mz), 0 );
706 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
707 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
708 mz_size = size;
709 pos = mz.dos.e_lfanew;
711 size = pread( unix_fd, &nt, sizeof(nt), pos );
712 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
713 /* zero out Optional header in the case it's not present or partial */
714 opt_size = max( nt.FileHeader.SizeOfOptionalHeader, offsetof( IMAGE_OPTIONAL_HEADER32, CheckSum ));
715 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + opt_size );
716 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
717 if (nt.Signature != IMAGE_NT_SIGNATURE)
719 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
720 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
721 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
722 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
723 return STATUS_INVALID_IMAGE_NE_FORMAT;
726 switch (nt.opt.hdr32.Magic)
728 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
729 if (!is_machine_32bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
730 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
732 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
733 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
735 mapping->image.base = nt.opt.hdr32.ImageBase;
736 mapping->image.entry_point = nt.opt.hdr32.AddressOfEntryPoint;
737 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
738 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
739 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
740 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
741 mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
742 mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
743 mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
744 mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
745 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
746 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
747 nt.opt.hdr32.AddressOfEntryPoint ||
748 nt.opt.hdr32.SectionAlignment & page_mask);
749 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
750 mapping->image.checksum = nt.opt.hdr32.CheckSum;
751 mapping->image.image_flags = 0;
752 if (nt.opt.hdr32.SectionAlignment & page_mask)
753 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
754 if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
755 mapping->image.contains_code && !(clr_va && clr_size))
756 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
757 break;
759 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
760 if (!is_machine_64bit( native_machine )) return STATUS_INVALID_IMAGE_WIN_64;
761 if (!is_machine_64bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
762 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
764 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
765 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
767 mapping->image.base = nt.opt.hdr64.ImageBase;
768 mapping->image.entry_point = nt.opt.hdr64.AddressOfEntryPoint;
769 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
770 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
771 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
772 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
773 mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
774 mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
775 mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
776 mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
777 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
778 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
779 nt.opt.hdr64.AddressOfEntryPoint ||
780 nt.opt.hdr64.SectionAlignment & page_mask);
781 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
782 mapping->image.checksum = nt.opt.hdr64.CheckSum;
783 mapping->image.image_flags = 0;
784 if (nt.opt.hdr64.SectionAlignment & page_mask)
785 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
786 if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
787 mapping->image.contains_code && !(clr_va && clr_size))
788 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
789 break;
791 default:
792 return STATUS_INVALID_IMAGE_FORMAT;
795 mapping->image.image_charact = nt.FileHeader.Characteristics;
796 mapping->image.machine = nt.FileHeader.Machine;
797 mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
798 mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
799 mapping->image.zerobits = 0; /* FIXME */
800 mapping->image.file_size = file_size;
801 mapping->image.loader_flags = clr_va && clr_size;
802 mapping->image.wine_builtin = (mz_size == sizeof(mz) &&
803 !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ));
804 mapping->image.wine_fakedll = (mz_size == sizeof(mz) &&
805 !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ));
807 /* load the section headers */
809 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
810 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
811 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
812 if (!mapping->size) mapping->size = mapping->image.map_size;
813 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
814 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
815 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
816 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
818 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
819 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
821 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
822 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
824 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
825 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
827 if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
828 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
829 if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
830 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
834 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
835 return STATUS_INVALID_FILE_FOR_SECTION;
837 return STATUS_SUCCESS;
840 static struct ranges *create_ranges(void)
842 struct ranges *ranges = alloc_object( &ranges_ops );
844 if (!ranges) return NULL;
845 ranges->count = 0;
846 ranges->max = 8;
847 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
849 release_object( ranges );
850 return NULL;
852 return ranges;
855 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
857 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
859 case SEC_IMAGE:
860 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
861 if (handle) return SEC_FILE | SEC_IMAGE;
862 set_error( STATUS_INVALID_FILE_FOR_SECTION );
863 return 0;
864 case SEC_COMMIT:
865 if (!handle) return flags;
866 /* fall through */
867 case SEC_RESERVE:
868 if (flags & SEC_LARGE_PAGES) break;
869 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
870 return flags;
872 set_error( STATUS_INVALID_PARAMETER );
873 return 0;
877 static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
878 unsigned int attr, mem_size_t size, unsigned int flags,
879 obj_handle_t handle, unsigned int file_access,
880 const struct security_descriptor *sd )
882 struct mapping *mapping;
883 struct file *file;
884 struct fd *fd;
885 int unix_fd;
886 struct stat st;
888 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
890 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
891 return NULL;
892 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
893 return mapping; /* Nothing else to do */
895 mapping->size = size;
896 mapping->fd = NULL;
897 mapping->shared = NULL;
898 mapping->committed = NULL;
900 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
902 if (handle)
904 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
905 unsigned int mapping_access = FILE_MAPPING_ACCESS;
907 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
908 fd = get_obj_fd( (struct object *)file );
910 /* file sharing rules for mappings are different so we use magic the access rights */
911 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
912 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
914 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
916 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
917 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
919 release_object( file );
920 release_object( fd );
921 if (!mapping->fd) goto error;
923 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
924 if (fstat( unix_fd, &st ) == -1)
926 file_set_error();
927 goto error;
929 if (flags & SEC_IMAGE)
931 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
932 if (!err) return mapping;
933 set_error( err );
934 goto error;
936 if (!mapping->size)
938 if (!(mapping->size = st.st_size))
940 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
941 goto error;
944 else if (st.st_size < mapping->size)
946 if (!(file_access & FILE_WRITE_DATA))
948 set_error( STATUS_SECTION_TOO_BIG );
949 goto error;
951 if (!grow_file( unix_fd, mapping->size )) goto error;
954 else /* Anonymous mapping (no associated file) */
956 if (!mapping->size)
958 set_error( STATUS_INVALID_PARAMETER );
959 goto error;
961 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
962 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
963 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
964 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
965 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
966 allow_fd_caching( mapping->fd );
968 return mapping;
970 error:
971 release_object( mapping );
972 return NULL;
975 /* create a read-only file mapping for the specified fd */
976 struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
977 struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
979 struct mapping *mapping;
980 int unix_fd;
981 struct stat st;
983 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
984 if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
986 mapping->shared = NULL;
987 mapping->committed = NULL;
988 mapping->flags = SEC_FILE;
989 mapping->fd = (struct fd *)grab_object( fd );
990 set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
992 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
993 if (fstat( unix_fd, &st ) == -1)
995 file_set_error();
996 goto error;
998 if (!(mapping->size = st.st_size))
1000 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
1001 goto error;
1003 return mapping;
1005 error:
1006 release_object( mapping );
1007 return NULL;
1010 static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
1012 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
1015 /* open a new file for the file descriptor backing the view */
1016 struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
1018 if (!view->fd) return NULL;
1019 return create_file_for_fd_obj( view->fd, access, sharing );
1022 /* get the image info for a SEC_IMAGE mapped view */
1023 const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
1025 if (!(view->flags & SEC_IMAGE)) return NULL;
1026 *base = view->base;
1027 return &view->image;
1030 /* get the file name for a mapped view */
1031 int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
1033 if (view->namelen) /* .so builtin */
1035 name->str = view->name;
1036 name->len = view->namelen;
1037 return 1;
1039 if (!view->fd) return 0;
1040 get_nt_name( view->fd, name );
1041 return 1;
1044 /* generate all startup events of a given process */
1045 void generate_startup_debug_events( struct process *process )
1047 struct memory_view *view;
1048 struct list *ptr = list_head( &process->views );
1049 struct thread *thread, *first_thread = get_process_first_thread( process );
1051 if (!ptr) return;
1052 view = LIST_ENTRY( ptr, struct memory_view, entry );
1053 generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
1055 /* generate ntdll.dll load event */
1056 while (ptr && (ptr = list_next( &process->views, ptr )))
1058 view = LIST_ENTRY( ptr, struct memory_view, entry );
1059 if (generate_dll_event( first_thread, DbgLoadDllStateChange, view )) break;
1062 /* generate creation events */
1063 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1065 if (thread != first_thread)
1066 generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
1069 /* generate dll events (in loading order) */
1070 while (ptr && (ptr = list_next( &process->views, ptr )))
1072 view = LIST_ENTRY( ptr, struct memory_view, entry );
1073 generate_dll_event( first_thread, DbgLoadDllStateChange, view );
1077 static void mapping_dump( struct object *obj, int verbose )
1079 struct mapping *mapping = (struct mapping *)obj;
1080 assert( obj->ops == &mapping_ops );
1081 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
1082 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
1083 mapping->flags, mapping->fd, mapping->shared );
1086 static struct fd *mapping_get_fd( struct object *obj )
1088 struct mapping *mapping = (struct mapping *)obj;
1089 return (struct fd *)grab_object( mapping->fd );
1092 static void mapping_destroy( struct object *obj )
1094 struct mapping *mapping = (struct mapping *)obj;
1095 assert( obj->ops == &mapping_ops );
1096 if (mapping->fd) release_object( mapping->fd );
1097 if (mapping->committed) release_object( mapping->committed );
1098 if (mapping->shared) release_object( mapping->shared );
1101 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
1103 return FD_TYPE_FILE;
1106 int get_page_size(void)
1108 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
1109 return page_mask + 1;
1112 struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
1113 unsigned int attr, const struct security_descriptor *sd )
1115 void *ptr;
1116 struct mapping *mapping;
1118 if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
1119 SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
1120 ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
1121 if (ptr != MAP_FAILED)
1123 user_shared_data = ptr;
1124 user_shared_data->SystemCall = 1;
1126 return &mapping->obj;
1129 /* create a file mapping */
1130 DECL_HANDLER(create_mapping)
1132 struct object *root;
1133 struct mapping *mapping;
1134 struct unicode_str name;
1135 const struct security_descriptor *sd;
1136 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1138 if (!objattr) return;
1140 if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
1141 req->file_handle, req->file_access, sd )))
1143 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1144 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
1145 else
1146 reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
1147 req->access, objattr->attributes );
1148 release_object( mapping );
1151 if (root) release_object( root );
1154 /* open a handle to a mapping */
1155 DECL_HANDLER(open_mapping)
1157 struct unicode_str name = get_req_unicode_str();
1159 reply->handle = open_object( current->process, req->rootdir, req->access,
1160 &mapping_ops, &name, req->attributes );
1163 /* get a mapping information */
1164 DECL_HANDLER(get_mapping_info)
1166 struct mapping *mapping;
1168 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
1170 reply->size = mapping->size;
1171 reply->flags = mapping->flags;
1173 if (mapping->flags & SEC_IMAGE)
1175 struct unicode_str name = { NULL, 0 };
1176 data_size_t size;
1177 void *data;
1179 if (mapping->fd) get_nt_name( mapping->fd, &name );
1180 size = min( sizeof(pe_image_info_t) + name.len, get_reply_max_size() );
1181 if ((data = set_reply_data_size( size )))
1183 memcpy( data, &mapping->image, min( sizeof(pe_image_info_t), size ));
1184 if (size > sizeof(pe_image_info_t))
1185 memcpy( (pe_image_info_t *)data + 1, name.str, size - sizeof(pe_image_info_t) );
1187 reply->total = sizeof(pe_image_info_t) + name.len;
1190 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
1192 release_object( mapping );
1193 return;
1196 if (mapping->shared)
1197 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1198 GENERIC_READ|GENERIC_WRITE, 0 );
1199 release_object( mapping );
1202 /* add a memory view in the current process */
1203 DECL_HANDLER(map_view)
1205 struct mapping *mapping;
1206 struct memory_view *view;
1208 if (!is_valid_view_addr( current->process, req->base, req->size ))
1210 set_error( STATUS_INVALID_PARAMETER );
1211 return;
1214 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1216 if ((mapping->flags & SEC_IMAGE) ||
1217 req->start >= mapping->size ||
1218 req->start + req->size < req->start ||
1219 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1221 set_error( STATUS_INVALID_PARAMETER );
1222 goto done;
1225 if ((view = mem_alloc( sizeof(*view) )))
1227 view->base = req->base;
1228 view->size = req->size;
1229 view->start = req->start;
1230 view->flags = mapping->flags;
1231 view->namelen = 0;
1232 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1233 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1234 view->shared = NULL;
1235 add_process_view( current, view );
1238 done:
1239 release_object( mapping );
1242 /* add a memory view for an image mapping in the current process */
1243 DECL_HANDLER(map_image_view)
1245 struct mapping *mapping;
1246 struct memory_view *view;
1248 if (!is_valid_view_addr( current->process, req->base, req->size ))
1250 set_error( STATUS_INVALID_PARAMETER );
1251 return;
1254 if (!(mapping = get_mapping_obj( current->process, req->mapping, SECTION_MAP_READ ))) return;
1256 if (!(mapping->flags & SEC_IMAGE) || req->size > mapping->image.map_size)
1258 set_error( STATUS_INVALID_PARAMETER );
1259 goto done;
1262 if ((view = mem_alloc( sizeof(*view) )))
1264 view->base = req->base;
1265 view->size = req->size;
1266 view->flags = mapping->flags;
1267 view->start = 0;
1268 view->namelen = 0;
1269 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1270 view->committed = NULL;
1271 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1272 view->image = mapping->image;
1273 view->image.machine = req->machine;
1274 view->image.entry_point = req->entry;
1275 add_process_view( current, view );
1277 if (view->base != mapping->image.base) set_error( STATUS_IMAGE_NOT_AT_BASE );
1278 if (view->image.machine != current->process->machine)
1280 /* on 32-bit, the native 64-bit machine is allowed */
1281 if (is_machine_64bit( current->process->machine ) || view->image.machine != native_machine)
1282 set_error( STATUS_IMAGE_MACHINE_TYPE_MISMATCH );
1286 done:
1287 release_object( mapping );
1290 /* add a memory view for a builtin dll in the current process */
1291 DECL_HANDLER(map_builtin_view)
1293 struct memory_view *view;
1294 const pe_image_info_t *image = get_req_data();
1295 data_size_t namelen = get_req_data_size() - sizeof(*image);
1297 if (get_req_data_size() < sizeof(*image) ||
1298 (namelen & (sizeof(WCHAR) - 1)) ||
1299 !is_valid_view_addr( current->process, image->base, image->map_size ))
1301 set_error( STATUS_INVALID_PARAMETER );
1302 return;
1305 if ((view = mem_alloc( sizeof(struct memory_view) + namelen )))
1307 memset( view, 0, sizeof(*view) );
1308 view->base = image->base;
1309 view->size = image->map_size;
1310 view->flags = SEC_IMAGE;
1311 view->image = *image;
1312 view->namelen = namelen;
1313 memcpy( view->name, image + 1, namelen );
1314 add_process_view( current, view );
1318 /* unmap a memory view from the current process */
1319 DECL_HANDLER(unmap_view)
1321 struct memory_view *view = find_mapped_view( current->process, req->base );
1323 if (!view) return;
1324 generate_dll_event( current, DbgUnloadDllStateChange, view );
1325 free_memory_view( view );
1328 /* get information about a mapped image view */
1329 DECL_HANDLER(get_image_view_info)
1331 struct process *process;
1332 struct memory_view *view;
1334 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1336 if ((view = find_mapped_addr( process, req->addr )) && (view->flags & SEC_IMAGE))
1338 reply->base = view->base;
1339 reply->size = view->size;
1342 release_object( process );
1345 /* get a range of committed pages in a file mapping */
1346 DECL_HANDLER(get_mapping_committed_range)
1348 struct memory_view *view = find_mapped_view( current->process, req->base );
1350 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1353 /* add a range to the committed pages in a file mapping */
1354 DECL_HANDLER(add_mapping_committed_range)
1356 struct memory_view *view = find_mapped_view( current->process, req->base );
1358 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1361 /* check if two memory maps are for the same file */
1362 DECL_HANDLER(is_same_mapping)
1364 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1365 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1367 if (!view1 || !view2) return;
1368 if (!view1->fd || !view2->fd || !(view1->flags & SEC_IMAGE) || !is_same_file_fd( view1->fd, view2->fd ))
1369 set_error( STATUS_NOT_SAME_DEVICE );
1372 /* get the filename of a mapping */
1373 DECL_HANDLER(get_mapping_filename)
1375 struct process *process;
1376 struct memory_view *view;
1377 struct unicode_str name;
1379 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1381 if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
1383 reply->len = name.len;
1384 if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
1385 else if (!name.len) set_error( STATUS_FILE_INVALID );
1386 else set_reply_data( name.str, name.len );
1388 else set_error( STATUS_INVALID_ADDRESS );
1390 release_object( process );