mapping.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * Server-side file mapping management
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/stat.h>
  27. #ifdef HAVE_SYS_MMAN_H
  28. # include <sys/mman.h>
  29. #endif
  30. #include <unistd.h>
  31. #include "ntstatus.h"
  32. #define WIN32_NO_STATUS
  33. #include "windef.h"
  34. #include "winternl.h"
  35. #include "ddk/wdm.h"
  36. #include "file.h"
  37. #include "handle.h"
  38. #include "thread.h"
  39. #include "process.h"
  40. #include "request.h"
  41. #include "security.h"
  42. /* list of memory ranges, used to store committed info */
  43. struct ranges
  44. {
  45. struct object obj; /* object header */
  46. unsigned int count; /* number of used ranges */
  47. unsigned int max; /* number of allocated ranges */
  48. struct range
  49. {
  50. file_pos_t start;
  51. file_pos_t end;
  52. } *ranges;
  53. };
  54. static void ranges_dump( struct object *obj, int verbose );
  55. static void ranges_destroy( struct object *obj );
  56. static const struct object_ops ranges_ops =
  57. {
  58. sizeof(struct ranges), /* size */
  59. &no_type, /* type */
  60. ranges_dump, /* dump */
  61. no_add_queue, /* add_queue */
  62. NULL, /* remove_queue */
  63. NULL, /* signaled */
  64. NULL, /* satisfied */
  65. no_signal, /* signal */
  66. no_get_fd, /* get_fd */
  67. default_map_access, /* map_access */
  68. default_get_sd, /* get_sd */
  69. default_set_sd, /* set_sd */
  70. no_get_full_name, /* get_full_name */
  71. no_lookup_name, /* lookup_name */
  72. no_link_name, /* link_name */
  73. NULL, /* unlink_name */
  74. no_open_file, /* open_file */
  75. no_kernel_obj_list, /* get_kernel_obj_list */
  76. no_close_handle, /* close_handle */
  77. ranges_destroy /* destroy */
  78. };
  79. /* file backing the shared sections of a PE image mapping */
  80. struct shared_map
  81. {
  82. struct object obj; /* object header */
  83. struct fd *fd; /* file descriptor of the mapped PE file */
  84. struct file *file; /* temp file holding the shared data */
  85. struct list entry; /* entry in global shared maps list */
  86. };
  87. static void shared_map_dump( struct object *obj, int verbose );
  88. static void shared_map_destroy( struct object *obj );
  89. static const struct object_ops shared_map_ops =
  90. {
  91. sizeof(struct shared_map), /* size */
  92. &no_type, /* type */
  93. shared_map_dump, /* dump */
  94. no_add_queue, /* add_queue */
  95. NULL, /* remove_queue */
  96. NULL, /* signaled */
  97. NULL, /* satisfied */
  98. no_signal, /* signal */
  99. no_get_fd, /* get_fd */
  100. default_map_access, /* map_access */
  101. default_get_sd, /* get_sd */
  102. default_set_sd, /* set_sd */
  103. no_get_full_name, /* get_full_name */
  104. no_lookup_name, /* lookup_name */
  105. no_link_name, /* link_name */
  106. NULL, /* unlink_name */
  107. no_open_file, /* open_file */
  108. no_kernel_obj_list, /* get_kernel_obj_list */
  109. no_close_handle, /* close_handle */
  110. shared_map_destroy /* destroy */
  111. };
  112. static struct list shared_map_list = LIST_INIT( shared_map_list );
  113. /* memory view mapped in client address space */
  114. struct memory_view
  115. {
  116. struct list entry; /* entry in per-process view list */
  117. struct fd *fd; /* fd for mapped file */
  118. struct ranges *committed; /* list of committed ranges in this mapping */
  119. struct shared_map *shared; /* temp file for shared PE mapping */
  120. pe_image_info_t image; /* image info (for PE image mapping) */
  121. unsigned int flags; /* SEC_* flags */
  122. client_ptr_t base; /* view base address (in process addr space) */
  123. mem_size_t size; /* view size */
  124. file_pos_t start; /* start offset in mapping */
  125. data_size_t namelen;
  126. WCHAR name[1]; /* filename for .so dll image views */
  127. };
  128. static const WCHAR mapping_name[] = {'S','e','c','t','i','o','n'};
  129. struct type_descr mapping_type =
  130. {
  131. { mapping_name, sizeof(mapping_name) }, /* name */
  132. SECTION_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
  133. { /* mapping */
  134. STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ,
  135. STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
  136. STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
  137. SECTION_ALL_ACCESS
  138. },
  139. };
  140. struct mapping
  141. {
  142. struct object obj; /* object header */
  143. mem_size_t size; /* mapping size */
  144. unsigned int flags; /* SEC_* flags */
  145. struct fd *fd; /* fd for mapped file */
  146. pe_image_info_t image; /* image info (for PE image mapping) */
  147. struct ranges *committed; /* list of committed ranges in this mapping */
  148. struct shared_map *shared; /* temp file for shared PE mapping */
  149. };
  150. static void mapping_dump( struct object *obj, int verbose );
  151. static struct fd *mapping_get_fd( struct object *obj );
  152. static void mapping_destroy( struct object *obj );
  153. static enum server_fd_type mapping_get_fd_type( struct fd *fd );
  154. static const struct object_ops mapping_ops =
  155. {
  156. sizeof(struct mapping), /* size */
  157. &mapping_type, /* type */
  158. mapping_dump, /* dump */
  159. no_add_queue, /* add_queue */
  160. NULL, /* remove_queue */
  161. NULL, /* signaled */
  162. NULL, /* satisfied */
  163. no_signal, /* signal */
  164. mapping_get_fd, /* get_fd */
  165. default_map_access, /* map_access */
  166. default_get_sd, /* get_sd */
  167. default_set_sd, /* set_sd */
  168. default_get_full_name, /* get_full_name */
  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. no_close_handle, /* close_handle */
  175. mapping_destroy /* destroy */
  176. };
  177. static const struct fd_ops mapping_fd_ops =
  178. {
  179. default_fd_get_poll_events, /* get_poll_events */
  180. default_poll_event, /* poll_event */
  181. mapping_get_fd_type, /* get_fd_type */
  182. no_fd_read, /* read */
  183. no_fd_write, /* write */
  184. no_fd_flush, /* flush */
  185. no_fd_get_file_info, /* get_file_info */
  186. no_fd_get_volume_info, /* get_volume_info */
  187. no_fd_ioctl, /* ioctl */
  188. no_fd_queue_async, /* queue_async */
  189. default_fd_reselect_async /* reselect_async */
  190. };
  191. static size_t page_mask;
  192. #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
  193. static void ranges_dump( struct object *obj, int verbose )
  194. {
  195. struct ranges *ranges = (struct ranges *)obj;
  196. fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
  197. }
  198. static void ranges_destroy( struct object *obj )
  199. {
  200. struct ranges *ranges = (struct ranges *)obj;
  201. free( ranges->ranges );
  202. }
  203. static void shared_map_dump( struct object *obj, int verbose )
  204. {
  205. struct shared_map *shared = (struct shared_map *)obj;
  206. fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
  207. }
  208. static void shared_map_destroy( struct object *obj )
  209. {
  210. struct shared_map *shared = (struct shared_map *)obj;
  211. release_object( shared->fd );
  212. release_object( shared->file );
  213. list_remove( &shared->entry );
  214. }
  215. /* extend a file beyond the current end of file */
  216. int grow_file( int unix_fd, file_pos_t new_size )
  217. {
  218. static const char zero;
  219. off_t size = new_size;
  220. if (sizeof(new_size) > sizeof(size) && size != new_size)
  221. {
  222. set_error( STATUS_INVALID_PARAMETER );
  223. return 0;
  224. }
  225. /* extend the file one byte beyond the requested size and then truncate it */
  226. /* this should work around ftruncate implementations that can't extend files */
  227. if (pwrite( unix_fd, &zero, 1, size ) != -1)
  228. {
  229. ftruncate( unix_fd, size );
  230. return 1;
  231. }
  232. file_set_error();
  233. return 0;
  234. }
  235. /* check if the current directory allows exec mappings */
  236. static int check_current_dir_for_exec(void)
  237. {
  238. int fd;
  239. char tmpfn[] = "anonmap.XXXXXX";
  240. void *ret = MAP_FAILED;
  241. fd = mkstemps( tmpfn, 0 );
  242. if (fd == -1) return 0;
  243. if (grow_file( fd, 1 ))
  244. {
  245. ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
  246. if (ret != MAP_FAILED) munmap( ret, get_page_size() );
  247. }
  248. close( fd );
  249. unlink( tmpfn );
  250. return (ret != MAP_FAILED);
  251. }
  252. /* create a temp file for anonymous mappings */
  253. static int create_temp_file( file_pos_t size )
  254. {
  255. static int temp_dir_fd = -1;
  256. char tmpfn[] = "anonmap.XXXXXX";
  257. int fd;
  258. if (temp_dir_fd == -1)
  259. {
  260. temp_dir_fd = server_dir_fd;
  261. if (!check_current_dir_for_exec())
  262. {
  263. /* the server dir is noexec, try the config dir instead */
  264. fchdir( config_dir_fd );
  265. if (check_current_dir_for_exec())
  266. temp_dir_fd = config_dir_fd;
  267. else /* neither works, fall back to server dir */
  268. fchdir( server_dir_fd );
  269. }
  270. }
  271. else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
  272. fd = mkstemps( tmpfn, 0 );
  273. if (fd != -1)
  274. {
  275. if (!grow_file( fd, size ))
  276. {
  277. close( fd );
  278. fd = -1;
  279. }
  280. unlink( tmpfn );
  281. }
  282. else file_set_error();
  283. if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
  284. return fd;
  285. }
  286. /* find a memory view from its base address */
  287. struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
  288. {
  289. struct memory_view *view;
  290. LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
  291. if (view->base == base) return view;
  292. set_error( STATUS_NOT_MAPPED_VIEW );
  293. return NULL;
  294. }
  295. /* find a memory view from any address inside it */
  296. static struct memory_view *find_mapped_addr( struct process *process, client_ptr_t addr )
  297. {
  298. struct memory_view *view;
  299. LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
  300. if (addr >= view->base && addr < view->base + view->size) return view;
  301. set_error( STATUS_NOT_MAPPED_VIEW );
  302. return NULL;
  303. }
  304. /* get the main exe memory view */
  305. struct memory_view *get_exe_view( struct process *process )
  306. {
  307. return LIST_ENTRY( list_head( &process->views ), struct memory_view, entry );
  308. }
  309. static void set_process_machine( struct process *process, struct memory_view *view )
  310. {
  311. unsigned short machine = view->image.machine;
  312. if (machine == IMAGE_FILE_MACHINE_I386 && (view->image.image_flags & IMAGE_FLAGS_ComPlusNativeReady))
  313. {
  314. if (is_machine_supported( IMAGE_FILE_MACHINE_AMD64 )) machine = IMAGE_FILE_MACHINE_AMD64;
  315. else if (is_machine_supported( IMAGE_FILE_MACHINE_ARM64 )) machine = IMAGE_FILE_MACHINE_ARM64;
  316. }
  317. process->machine = machine;
  318. }
  319. static int generate_dll_event( struct thread *thread, int code, struct memory_view *view )
  320. {
  321. unsigned short process_machine = thread->process->machine;
  322. if (!(view->flags & SEC_IMAGE)) return 0;
  323. if (process_machine != native_machine && process_machine != view->image.machine) return 0;
  324. generate_debug_event( thread, code, view );
  325. return 1;
  326. }
  327. /* add a view to the process list */
  328. static void add_process_view( struct thread *thread, struct memory_view *view )
  329. {
  330. struct process *process = thread->process;
  331. struct unicode_str name;
  332. if (view->flags & SEC_IMAGE)
  333. {
  334. if (is_process_init_done( process ))
  335. {
  336. generate_dll_event( thread, DbgLoadDllStateChange, view );
  337. }
  338. else if (!(view->image.image_charact & IMAGE_FILE_DLL))
  339. {
  340. /* main exe */
  341. set_process_machine( process, view );
  342. list_add_head( &process->views, &view->entry );
  343. free( process->image );
  344. process->image = NULL;
  345. if (get_view_nt_name( view, &name ) && (process->image = memdup( name.str, name.len )))
  346. process->imagelen = name.len;
  347. return;
  348. }
  349. }
  350. list_add_tail( &process->views, &view->entry );
  351. }
  352. static void free_memory_view( struct memory_view *view )
  353. {
  354. if (view->fd) release_object( view->fd );
  355. if (view->committed) release_object( view->committed );
  356. if (view->shared) release_object( view->shared );
  357. list_remove( &view->entry );
  358. free( view );
  359. }
  360. /* free all mapped views at process exit */
  361. void free_mapped_views( struct process *process )
  362. {
  363. struct list *ptr;
  364. while ((ptr = list_head( &process->views )))
  365. free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
  366. }
  367. /* find the shared PE mapping for a given mapping */
  368. static struct shared_map *get_shared_file( struct fd *fd )
  369. {
  370. struct shared_map *ptr;
  371. LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
  372. if (is_same_file_fd( ptr->fd, fd ))
  373. return (struct shared_map *)grab_object( ptr );
  374. return NULL;
  375. }
  376. /* return the size of the memory mapping and file range of a given section */
  377. static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
  378. off_t *file_start, size_t *file_size )
  379. {
  380. static const unsigned int sector_align = 0x1ff;
  381. if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
  382. else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
  383. *file_start = sec->PointerToRawData & ~sector_align;
  384. *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
  385. if (*file_size > *map_size) *file_size = *map_size;
  386. }
  387. /* add a range to the committed list */
  388. static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
  389. {
  390. unsigned int i, j;
  391. struct ranges *committed = view->committed;
  392. struct range *ranges;
  393. if ((start & page_mask) || (end & page_mask) ||
  394. start >= view->size || end >= view->size ||
  395. start >= end)
  396. {
  397. set_error( STATUS_INVALID_PARAMETER );
  398. return;
  399. }
  400. if (!committed) return; /* everything committed already */
  401. start += view->start;
  402. end += view->start;
  403. for (i = 0, ranges = committed->ranges; i < committed->count; i++)
  404. {
  405. if (ranges[i].start > end) break;
  406. if (ranges[i].end < start) continue;
  407. if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
  408. if (ranges[i].end < end) /* extend upwards and maybe merge with next */
  409. {
  410. for (j = i + 1; j < committed->count; j++)
  411. {
  412. if (ranges[j].start > end) break;
  413. if (ranges[j].end > end) end = ranges[j].end;
  414. }
  415. if (j > i + 1)
  416. {
  417. memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
  418. committed->count -= j - (i + 1);
  419. }
  420. ranges[i].end = end;
  421. }
  422. return;
  423. }
  424. /* now add a new range */
  425. if (committed->count == committed->max)
  426. {
  427. unsigned int new_size = committed->max * 2;
  428. struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
  429. if (!new_ptr) return;
  430. committed->max = new_size;
  431. ranges = committed->ranges = new_ptr;
  432. }
  433. memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
  434. ranges[i].start = start;
  435. ranges[i].end = end;
  436. committed->count++;
  437. }
  438. /* find the range containing start and return whether it's committed */
  439. static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
  440. {
  441. unsigned int i;
  442. struct ranges *committed = view->committed;
  443. struct range *ranges;
  444. if ((start & page_mask) || start >= view->size)
  445. {
  446. set_error( STATUS_INVALID_PARAMETER );
  447. return 0;
  448. }
  449. if (!committed) /* everything is committed */
  450. {
  451. *size = view->size - start;
  452. return 1;
  453. }
  454. for (i = 0, ranges = committed->ranges; i < committed->count; i++)
  455. {
  456. if (ranges[i].start > view->start + start)
  457. {
  458. *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
  459. return 0;
  460. }
  461. if (ranges[i].end > view->start + start)
  462. {
  463. *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
  464. return 1;
  465. }
  466. }
  467. *size = view->size - start;
  468. return 0;
  469. }
  470. /* allocate and fill the temp file for a shared PE image mapping */
  471. static int build_shared_mapping( struct mapping *mapping, int fd,
  472. IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
  473. {
  474. struct shared_map *shared;
  475. struct file *file;
  476. unsigned int i;
  477. mem_size_t total_size;
  478. size_t file_size, map_size, max_size;
  479. off_t shared_pos, read_pos, write_pos;
  480. char *buffer = NULL;
  481. int shared_fd;
  482. long toread;
  483. /* compute the total size of the shared mapping */
  484. total_size = max_size = 0;
  485. for (i = 0; i < nb_sec; i++)
  486. {
  487. if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
  488. (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
  489. {
  490. get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
  491. if (file_size > max_size) max_size = file_size;
  492. total_size += map_size;
  493. }
  494. }
  495. if (!total_size) return 1; /* nothing to do */
  496. if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
  497. /* create a temp file for the mapping */
  498. if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
  499. if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
  500. if (!(buffer = malloc( max_size ))) goto error;
  501. /* copy the shared sections data into the temp file */
  502. shared_pos = 0;
  503. for (i = 0; i < nb_sec; i++)
  504. {
  505. if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
  506. if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
  507. get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
  508. write_pos = shared_pos;
  509. shared_pos += map_size;
  510. if (!sec[i].PointerToRawData || !file_size) continue;
  511. toread = file_size;
  512. while (toread)
  513. {
  514. long res = pread( fd, buffer + file_size - toread, toread, read_pos );
  515. if (!res && toread < 0x200) /* partial sector at EOF is not an error */
  516. {
  517. file_size -= toread;
  518. break;
  519. }
  520. if (res <= 0) goto error;
  521. toread -= res;
  522. read_pos += res;
  523. }
  524. if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
  525. }
  526. if (!(shared = alloc_object( &shared_map_ops ))) goto error;
  527. shared->fd = (struct fd *)grab_object( mapping->fd );
  528. shared->file = file;
  529. list_add_head( &shared_map_list, &shared->entry );
  530. mapping->shared = shared;
  531. free( buffer );
  532. return 1;
  533. error:
  534. release_object( file );
  535. free( buffer );
  536. return 0;
  537. }
  538. /* load the CLR header from its section */
  539. static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
  540. IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
  541. {
  542. ssize_t ret;
  543. size_t map_size, file_size;
  544. off_t file_start;
  545. unsigned int i;
  546. if (!va || !size) return 0;
  547. for (i = 0; i < nb_sec; i++)
  548. {
  549. if (va < sec[i].VirtualAddress) continue;
  550. if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
  551. get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
  552. if (size >= map_size) continue;
  553. if (va - sec[i].VirtualAddress >= map_size - size) continue;
  554. file_size = min( file_size, map_size );
  555. size = min( size, sizeof(*hdr) );
  556. ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
  557. if (ret <= 0) break;
  558. if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
  559. return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
  560. (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
  561. hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
  562. }
  563. return 0;
  564. }
  565. /* retrieve the mapping parameters for an executable (PE) image */
  566. static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
  567. {
  568. static const char builtin_signature[] = "Wine builtin DLL";
  569. static const char fakedll_signature[] = "Wine placeholder DLL";
  570. IMAGE_COR20_HEADER clr;
  571. IMAGE_SECTION_HEADER sec[96];
  572. struct
  573. {
  574. IMAGE_DOS_HEADER dos;
  575. char buffer[32];
  576. } mz;
  577. struct
  578. {
  579. DWORD Signature;
  580. IMAGE_FILE_HEADER FileHeader;
  581. union
  582. {
  583. IMAGE_OPTIONAL_HEADER32 hdr32;
  584. IMAGE_OPTIONAL_HEADER64 hdr64;
  585. } opt;
  586. } nt;
  587. off_t pos;
  588. int size, opt_size;
  589. size_t mz_size, clr_va, clr_size;
  590. unsigned int i;
  591. /* load the headers */
  592. if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
  593. size = pread( unix_fd, &mz, sizeof(mz), 0 );
  594. if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
  595. if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
  596. mz_size = size;
  597. pos = mz.dos.e_lfanew;
  598. size = pread( unix_fd, &nt, sizeof(nt), pos );
  599. if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
  600. /* zero out Optional header in the case it's not present or partial */
  601. opt_size = max( nt.FileHeader.SizeOfOptionalHeader, offsetof( IMAGE_OPTIONAL_HEADER32, CheckSum ));
  602. size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + opt_size );
  603. if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
  604. if (nt.Signature != IMAGE_NT_SIGNATURE)
  605. {
  606. IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
  607. if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
  608. if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
  609. if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
  610. return STATUS_INVALID_IMAGE_NE_FORMAT;
  611. }
  612. switch (nt.opt.hdr32.Magic)
  613. {
  614. case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
  615. if (!is_machine_32bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
  616. if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
  617. clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
  618. clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
  619. mapping->image.base = nt.opt.hdr32.ImageBase;
  620. mapping->image.entry_point = nt.opt.hdr32.AddressOfEntryPoint;
  621. mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
  622. mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
  623. mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
  624. mapping->image.subsystem = nt.opt.hdr32.Subsystem;
  625. mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
  626. mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
  627. mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
  628. mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
  629. mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
  630. mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
  631. nt.opt.hdr32.AddressOfEntryPoint ||
  632. nt.opt.hdr32.SectionAlignment & page_mask);
  633. mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
  634. mapping->image.checksum = nt.opt.hdr32.CheckSum;
  635. mapping->image.image_flags = 0;
  636. if (nt.opt.hdr32.SectionAlignment & page_mask)
  637. mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
  638. if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
  639. mapping->image.contains_code && !(clr_va && clr_size))
  640. mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
  641. break;
  642. case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  643. if (!is_machine_64bit( native_machine )) return STATUS_INVALID_IMAGE_WIN_64;
  644. if (!is_machine_64bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
  645. if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
  646. clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
  647. clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
  648. mapping->image.base = nt.opt.hdr64.ImageBase;
  649. mapping->image.entry_point = nt.opt.hdr64.AddressOfEntryPoint;
  650. mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
  651. mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
  652. mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
  653. mapping->image.subsystem = nt.opt.hdr64.Subsystem;
  654. mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
  655. mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
  656. mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
  657. mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
  658. mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
  659. mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
  660. nt.opt.hdr64.AddressOfEntryPoint ||
  661. nt.opt.hdr64.SectionAlignment & page_mask);
  662. mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
  663. mapping->image.checksum = nt.opt.hdr64.CheckSum;
  664. mapping->image.image_flags = 0;
  665. if (nt.opt.hdr64.SectionAlignment & page_mask)
  666. mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
  667. if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
  668. mapping->image.contains_code && !(clr_va && clr_size))
  669. mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
  670. break;
  671. default:
  672. return STATUS_INVALID_IMAGE_FORMAT;
  673. }
  674. mapping->image.image_charact = nt.FileHeader.Characteristics;
  675. mapping->image.machine = nt.FileHeader.Machine;
  676. mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
  677. mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
  678. mapping->image.zerobits = 0; /* FIXME */
  679. mapping->image.file_size = file_size;
  680. mapping->image.loader_flags = clr_va && clr_size;
  681. if (mz_size == sizeof(mz) && !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ))
  682. mapping->image.image_flags |= IMAGE_FLAGS_WineBuiltin;
  683. else if (mz_size == sizeof(mz) && !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ))
  684. mapping->image.image_flags |= IMAGE_FLAGS_WineFakeDll;
  685. /* load the section headers */
  686. pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
  687. if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
  688. size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
  689. if (!mapping->size) mapping->size = mapping->image.map_size;
  690. else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
  691. if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
  692. if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
  693. if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
  694. for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
  695. if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
  696. if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
  697. (clr.Flags & COMIMAGE_FLAGS_ILONLY))
  698. {
  699. mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
  700. if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
  701. {
  702. if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
  703. mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
  704. if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
  705. mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
  706. }
  707. }
  708. if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
  709. return STATUS_INVALID_FILE_FOR_SECTION;
  710. return STATUS_SUCCESS;
  711. }
  712. static struct ranges *create_ranges(void)
  713. {
  714. struct ranges *ranges = alloc_object( &ranges_ops );
  715. if (!ranges) return NULL;
  716. ranges->count = 0;
  717. ranges->max = 8;
  718. if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
  719. {
  720. release_object( ranges );
  721. return NULL;
  722. }
  723. return ranges;
  724. }
  725. static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
  726. {
  727. switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
  728. {
  729. case SEC_IMAGE:
  730. if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
  731. if (handle) return SEC_FILE | SEC_IMAGE;
  732. set_error( STATUS_INVALID_FILE_FOR_SECTION );
  733. return 0;
  734. case SEC_COMMIT:
  735. if (!handle) return flags;
  736. /* fall through */
  737. case SEC_RESERVE:
  738. if (flags & SEC_LARGE_PAGES) break;
  739. if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
  740. return flags;
  741. }
  742. set_error( STATUS_INVALID_PARAMETER );
  743. return 0;
  744. }
  745. static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
  746. unsigned int attr, mem_size_t size, unsigned int flags,
  747. obj_handle_t handle, unsigned int file_access,
  748. const struct security_descriptor *sd )
  749. {
  750. struct mapping *mapping;
  751. struct file *file;
  752. struct fd *fd;
  753. int unix_fd;
  754. struct stat st;
  755. if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
  756. if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
  757. return NULL;
  758. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  759. return mapping; /* Nothing else to do */
  760. mapping->size = size;
  761. mapping->fd = NULL;
  762. mapping->shared = NULL;
  763. mapping->committed = NULL;
  764. if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
  765. if (handle)
  766. {
  767. const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  768. unsigned int mapping_access = FILE_MAPPING_ACCESS;
  769. if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
  770. fd = get_obj_fd( (struct object *)file );
  771. /* file sharing rules for mappings are different so we use magic the access rights */
  772. if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
  773. else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
  774. if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
  775. {
  776. mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
  777. if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
  778. }
  779. release_object( file );
  780. release_object( fd );
  781. if (!mapping->fd) goto error;
  782. if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
  783. if (fstat( unix_fd, &st ) == -1)
  784. {
  785. file_set_error();
  786. goto error;
  787. }
  788. if (flags & SEC_IMAGE)
  789. {
  790. unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
  791. if (!err) return mapping;
  792. set_error( err );
  793. goto error;
  794. }
  795. if (!mapping->size)
  796. {
  797. if (!(mapping->size = st.st_size))
  798. {
  799. set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
  800. goto error;
  801. }
  802. }
  803. else if (st.st_size < mapping->size)
  804. {
  805. if (!(file_access & FILE_WRITE_DATA))
  806. {
  807. set_error( STATUS_SECTION_TOO_BIG );
  808. goto error;
  809. }
  810. if (!grow_file( unix_fd, mapping->size )) goto error;
  811. }
  812. }
  813. else /* Anonymous mapping (no associated file) */
  814. {
  815. if (!mapping->size)
  816. {
  817. set_error( STATUS_INVALID_PARAMETER );
  818. goto error;
  819. }
  820. if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
  821. mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
  822. if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
  823. if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
  824. FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
  825. allow_fd_caching( mapping->fd );
  826. }
  827. return mapping;
  828. error:
  829. release_object( mapping );
  830. return NULL;
  831. }
  832. /* create a read-only file mapping for the specified fd */
  833. struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
  834. struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
  835. {
  836. struct mapping *mapping;
  837. int unix_fd;
  838. struct stat st;
  839. if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
  840. if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
  841. mapping->shared = NULL;
  842. mapping->committed = NULL;
  843. mapping->flags = SEC_FILE;
  844. mapping->fd = (struct fd *)grab_object( fd );
  845. set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
  846. if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
  847. if (fstat( unix_fd, &st ) == -1)
  848. {
  849. file_set_error();
  850. goto error;
  851. }
  852. if (!(mapping->size = st.st_size))
  853. {
  854. set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
  855. goto error;
  856. }
  857. return mapping;
  858. error:
  859. release_object( mapping );
  860. return NULL;
  861. }
  862. static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
  863. {
  864. return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
  865. }
  866. /* open a new file for the file descriptor backing the view */
  867. struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
  868. {
  869. if (!view->fd) return NULL;
  870. return create_file_for_fd_obj( view->fd, access, sharing );
  871. }
  872. /* get the image info for a SEC_IMAGE mapped view */
  873. const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
  874. {
  875. if (!(view->flags & SEC_IMAGE)) return NULL;
  876. *base = view->base;
  877. return &view->image;
  878. }
  879. /* get the file name for a mapped view */
  880. int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
  881. {
  882. if (view->namelen) /* .so builtin */
  883. {
  884. name->str = view->name;
  885. name->len = view->namelen;
  886. return 1;
  887. }
  888. if (!view->fd) return 0;
  889. get_nt_name( view->fd, name );
  890. return 1;
  891. }
  892. /* generate all startup events of a given process */
  893. void generate_startup_debug_events( struct process *process )
  894. {
  895. struct memory_view *view;
  896. struct list *ptr = list_head( &process->views );
  897. struct thread *thread, *first_thread = get_process_first_thread( process );
  898. if (!ptr) return;
  899. view = LIST_ENTRY( ptr, struct memory_view, entry );
  900. generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
  901. /* generate ntdll.dll load event */
  902. while (ptr && (ptr = list_next( &process->views, ptr )))
  903. {
  904. view = LIST_ENTRY( ptr, struct memory_view, entry );
  905. if (generate_dll_event( first_thread, DbgLoadDllStateChange, view )) break;
  906. }
  907. /* generate creation events */
  908. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  909. {
  910. if (thread != first_thread)
  911. generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
  912. }
  913. /* generate dll events (in loading order) */
  914. while (ptr && (ptr = list_next( &process->views, ptr )))
  915. {
  916. view = LIST_ENTRY( ptr, struct memory_view, entry );
  917. generate_dll_event( first_thread, DbgLoadDllStateChange, view );
  918. }
  919. }
  920. static void mapping_dump( struct object *obj, int verbose )
  921. {
  922. struct mapping *mapping = (struct mapping *)obj;
  923. assert( obj->ops == &mapping_ops );
  924. fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
  925. (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
  926. mapping->flags, mapping->fd, mapping->shared );
  927. }
  928. static struct fd *mapping_get_fd( struct object *obj )
  929. {
  930. struct mapping *mapping = (struct mapping *)obj;
  931. return (struct fd *)grab_object( mapping->fd );
  932. }
  933. static void mapping_destroy( struct object *obj )
  934. {
  935. struct mapping *mapping = (struct mapping *)obj;
  936. assert( obj->ops == &mapping_ops );
  937. if (mapping->fd) release_object( mapping->fd );
  938. if (mapping->committed) release_object( mapping->committed );
  939. if (mapping->shared) release_object( mapping->shared );
  940. }
  941. static enum server_fd_type mapping_get_fd_type( struct fd *fd )
  942. {
  943. return FD_TYPE_FILE;
  944. }
  945. int get_page_size(void)
  946. {
  947. if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
  948. return page_mask + 1;
  949. }
  950. struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
  951. unsigned int attr, const struct security_descriptor *sd )
  952. {
  953. void *ptr;
  954. struct mapping *mapping;
  955. if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
  956. SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
  957. ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
  958. if (ptr != MAP_FAILED)
  959. {
  960. user_shared_data = ptr;
  961. user_shared_data->SystemCall = 1;
  962. }
  963. return &mapping->obj;
  964. }
  965. /* create a file mapping */
  966. DECL_HANDLER(create_mapping)
  967. {
  968. struct object *root;
  969. struct mapping *mapping;
  970. struct unicode_str name;
  971. const struct security_descriptor *sd;
  972. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  973. if (!objattr) return;
  974. if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
  975. req->file_handle, req->file_access, sd )))
  976. {
  977. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  978. reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
  979. else
  980. reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
  981. req->access, objattr->attributes );
  982. release_object( mapping );
  983. }
  984. if (root) release_object( root );
  985. }
  986. /* open a handle to a mapping */
  987. DECL_HANDLER(open_mapping)
  988. {
  989. struct unicode_str name = get_req_unicode_str();
  990. reply->handle = open_object( current->process, req->rootdir, req->access,
  991. &mapping_ops, &name, req->attributes );
  992. }
  993. /* get a mapping information */
  994. DECL_HANDLER(get_mapping_info)
  995. {
  996. struct mapping *mapping;
  997. if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
  998. reply->size = mapping->size;
  999. reply->flags = mapping->flags;
  1000. if (mapping->flags & SEC_IMAGE)
  1001. {
  1002. struct unicode_str name = { NULL, 0 };
  1003. data_size_t size;
  1004. void *data;
  1005. if (mapping->fd) get_nt_name( mapping->fd, &name );
  1006. size = min( sizeof(pe_image_info_t) + name.len, get_reply_max_size() );
  1007. if ((data = set_reply_data_size( size )))
  1008. {
  1009. memcpy( data, &mapping->image, min( sizeof(pe_image_info_t), size ));
  1010. if (size > sizeof(pe_image_info_t))
  1011. memcpy( (pe_image_info_t *)data + 1, name.str, size - sizeof(pe_image_info_t) );
  1012. }
  1013. reply->total = sizeof(pe_image_info_t) + name.len;
  1014. }
  1015. if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
  1016. {
  1017. release_object( mapping );
  1018. return;
  1019. }
  1020. if (mapping->shared)
  1021. reply->shared_file = alloc_handle( current->process, mapping->shared->file,
  1022. GENERIC_READ|GENERIC_WRITE, 0 );
  1023. release_object( mapping );
  1024. }
  1025. /* add a memory view in the current process */
  1026. DECL_HANDLER(map_view)
  1027. {
  1028. struct mapping *mapping = NULL;
  1029. struct memory_view *view;
  1030. data_size_t namelen = 0;
  1031. if (!req->size || (req->base & page_mask) || req->base + req->size < req->base) /* overflow */
  1032. {
  1033. set_error( STATUS_INVALID_PARAMETER );
  1034. return;
  1035. }
  1036. /* make sure we don't already have an overlapping view */
  1037. LIST_FOR_EACH_ENTRY( view, &current->process->views, struct memory_view, entry )
  1038. {
  1039. if (view->base + view->size <= req->base) continue;
  1040. if (view->base >= req->base + req->size) continue;
  1041. set_error( STATUS_INVALID_PARAMETER );
  1042. return;
  1043. }
  1044. if (!req->mapping) /* image mapping for a .so dll */
  1045. {
  1046. if (get_req_data_size() > sizeof(view->image)) namelen = get_req_data_size() - sizeof(view->image);
  1047. if (!(view = mem_alloc( offsetof( struct memory_view, name[namelen] )))) return;
  1048. memset( view, 0, sizeof(*view) );
  1049. view->base = req->base;
  1050. view->size = req->size;
  1051. view->start = req->start;
  1052. view->flags = SEC_IMAGE;
  1053. view->namelen = namelen;
  1054. memcpy( &view->image, get_req_data(), min( sizeof(view->image), get_req_data_size() ));
  1055. memcpy( view->name, (pe_image_info_t *)get_req_data() + 1, namelen );
  1056. add_process_view( current, view );
  1057. return;
  1058. }
  1059. if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
  1060. if (mapping->flags & SEC_IMAGE)
  1061. {
  1062. if (req->start || req->size > mapping->image.map_size)
  1063. {
  1064. set_error( STATUS_INVALID_PARAMETER );
  1065. goto done;
  1066. }
  1067. }
  1068. else if (req->start >= mapping->size ||
  1069. req->start + req->size < req->start ||
  1070. req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
  1071. {
  1072. set_error( STATUS_INVALID_PARAMETER );
  1073. goto done;
  1074. }
  1075. if ((view = mem_alloc( offsetof( struct memory_view, name[namelen] ))))
  1076. {
  1077. view->base = req->base;
  1078. view->size = req->size;
  1079. view->start = req->start;
  1080. view->flags = mapping->flags;
  1081. view->namelen = namelen;
  1082. view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
  1083. view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
  1084. view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
  1085. if (view->flags & SEC_IMAGE) view->image = mapping->image;
  1086. add_process_view( current, view );
  1087. if (view->flags & SEC_IMAGE && view->base != mapping->image.base)
  1088. set_error( STATUS_IMAGE_NOT_AT_BASE );
  1089. }
  1090. done:
  1091. release_object( mapping );
  1092. }
  1093. /* unmap a memory view from the current process */
  1094. DECL_HANDLER(unmap_view)
  1095. {
  1096. struct memory_view *view = find_mapped_view( current->process, req->base );
  1097. if (!view) return;
  1098. generate_dll_event( current, DbgUnloadDllStateChange, view );
  1099. free_memory_view( view );
  1100. }
  1101. /* get a range of committed pages in a file mapping */
  1102. DECL_HANDLER(get_mapping_committed_range)
  1103. {
  1104. struct memory_view *view = find_mapped_view( current->process, req->base );
  1105. if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
  1106. }
  1107. /* add a range to the committed pages in a file mapping */
  1108. DECL_HANDLER(add_mapping_committed_range)
  1109. {
  1110. struct memory_view *view = find_mapped_view( current->process, req->base );
  1111. if (view) add_committed_range( view, req->offset, req->offset + req->size );
  1112. }
  1113. /* check if two memory maps are for the same file */
  1114. DECL_HANDLER(is_same_mapping)
  1115. {
  1116. struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
  1117. struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
  1118. if (!view1 || !view2) return;
  1119. if (!view1->fd || !view2->fd || !(view1->flags & SEC_IMAGE) || !is_same_file_fd( view1->fd, view2->fd ))
  1120. set_error( STATUS_NOT_SAME_DEVICE );
  1121. }
  1122. /* get the filename of a mapping */
  1123. DECL_HANDLER(get_mapping_filename)
  1124. {
  1125. struct process *process;
  1126. struct memory_view *view;
  1127. struct unicode_str name;
  1128. if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
  1129. if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
  1130. {
  1131. reply->len = name.len;
  1132. if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
  1133. else if (!name.len) set_error( STATUS_FILE_INVALID );
  1134. else set_reply_data( name.str, name.len );
  1135. }
  1136. else set_error( STATUS_INVALID_ADDRESS );
  1137. release_object( process );
  1138. }