mapping.c 47 KB

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