Added exception handling wrapper to a number of server requests.
[wine] / server / mapping.c
1 /*
2  * Server-side file mapping management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "config.h"
13 #include "winnt.h"
14 #include "winbase.h"
15
16 #include "handle.h"
17 #include "thread.h"
18 #include "request.h"
19
20 struct mapping
21 {
22     struct object  obj;             /* object header */
23     int            size_high;       /* mapping size */
24     int            size_low;        /* mapping size */
25     int            protect;         /* protection flags */
26     struct file   *file;            /* file mapped */
27     int            header_size;     /* size of headers (for PE image mapping) */
28     void          *base;            /* default base addr (for PE image mapping) */
29     struct file   *shared_file;     /* temp file for shared PE mapping */
30     int            shared_size;     /* shared mapping total size */
31 };
32
33 static void mapping_dump( struct object *obj, int verbose );
34 static void mapping_destroy( struct object *obj );
35
36 static const struct object_ops mapping_ops =
37 {
38     sizeof(struct mapping),      /* size */
39     mapping_dump,                /* dump */
40     no_add_queue,                /* add_queue */
41     NULL,                        /* remove_queue */
42     NULL,                        /* signaled */
43     NULL,                        /* satisfied */
44     NULL,                        /* get_poll_events */
45     NULL,                        /* poll_event */
46     no_read_fd,                  /* get_read_fd */
47     no_write_fd,                 /* get_write_fd */
48     no_flush,                    /* flush */
49     no_get_file_info,            /* get_file_info */
50     mapping_destroy              /* destroy */
51 };
52
53 #ifdef __i386__
54
55 /* These are always the same on an i386, and it will be faster this way */
56 # define page_mask  0xfff
57 # define page_shift 12
58 # define init_page_size() /* nothing */
59
60 #else  /* __i386__ */
61
62 static int page_shift, page_mask;
63
64 static void init_page_size(void)
65 {
66     int page_size;
67 # ifdef HAVE_GETPAGESIZE
68     page_size = getpagesize();
69 # else
70 #  ifdef __svr4__
71     page_size = sysconf(_SC_PAGESIZE);
72 #  else
73 #   error Cannot get the page size on this platform
74 #  endif
75 # endif
76     page_mask = page_size - 1;
77     /* Make sure we have a power of 2 */
78     assert( !(page_size & page_mask) );
79     page_shift = 0;
80     while ((1 << page_shift) != page_size) page_shift++;
81 }
82 #endif  /* __i386__ */
83
84 #define ROUND_ADDR(addr) \
85    ((int)(addr) & ~page_mask)
86
87 #define ROUND_SIZE(addr,size) \
88    (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
89
90
91 /* allocate and fill the temp file for a shared PE image mapping */
92 static int build_shared_mapping( struct mapping *mapping, int fd,
93                                  IMAGE_SECTION_HEADER *sec, int nb_sec )
94 {
95     int i, max_size, total_size, pos;
96     char *buffer = NULL; 
97     int shared_fd = -1;
98
99     /* compute the total size of the shared mapping */
100
101     total_size = max_size = 0;
102     for (i = 0; i < nb_sec; i++)
103     {
104         if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
105             (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
106         {
107             int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
108             if (size > max_size) max_size = size;
109             total_size += size;
110         }
111     }
112     if (!(mapping->shared_size = total_size)) return 1;  /* nothing to do */
113
114     /* create a temp file for the mapping */
115
116     if (!(mapping->shared_file = create_temp_file( GENERIC_READ|GENERIC_WRITE ))) goto error;
117     if (!grow_file( mapping->shared_file, 0, total_size )) goto error;
118     if ((shared_fd = file_get_mmap_fd( mapping->shared_file )) == -1) goto error;
119
120     if (!(buffer = malloc( max_size ))) goto error;
121
122     /* copy the shared sections data into the temp file */
123
124     for (i = pos = 0; i < nb_sec; i++)
125     {
126         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
127         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
128         if (lseek( shared_fd, pos, SEEK_SET ) != pos) goto error;
129         pos += ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
130         if (sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
131         if (!sec->PointerToRawData || !sec->SizeOfRawData) continue;
132         if (lseek( fd, sec[i].PointerToRawData, SEEK_SET ) != sec[i].PointerToRawData) goto error;
133         if (read( fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
134         if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
135     }
136     close( shared_fd );
137     free( buffer );
138     return 1;
139
140  error:
141     if (shared_fd != -1) close( shared_fd );
142     if (buffer) free( buffer );
143     return 0;
144 }
145
146 /* retrieve the mapping parameters for an executable (PE) image */
147 static int get_image_params( struct mapping *mapping )
148 {
149     IMAGE_DOS_HEADER dos;
150     IMAGE_NT_HEADERS nt;
151     IMAGE_SECTION_HEADER *sec = NULL;
152     int fd, filepos, size;
153
154     /* load the headers */
155
156     if ((fd = file_get_mmap_fd( mapping->file )) == -1) return 0;
157     filepos = lseek( fd, 0, SEEK_SET );
158     if (read( fd, &dos, sizeof(dos) ) != sizeof(dos)) goto error;
159     if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
160     if (lseek( fd, dos.e_lfanew, SEEK_SET ) == -1) goto error;
161     if (read( fd, &nt, sizeof(nt) ) != sizeof(nt)) goto error;
162     if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
163
164     /* load the section headers */
165
166     size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
167     if (!(sec = malloc( size ))) goto error;
168     if (read( fd, sec, size ) != size) goto error;
169
170     if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
171
172     mapping->size_low    = ROUND_SIZE( 0, nt.OptionalHeader.SizeOfImage );
173     mapping->size_high   = 0;
174     mapping->base        = (void *)nt.OptionalHeader.ImageBase;
175     mapping->header_size = ROUND_SIZE( mapping->base, nt.OptionalHeader.SizeOfHeaders );
176     mapping->protect     = VPROT_IMAGE;
177
178     /* sanity check */
179     if (mapping->header_size > mapping->size_low) goto error;
180
181     lseek( fd, filepos, SEEK_SET );
182     close( fd );
183     free( sec );
184     return 1;
185
186  error:
187     lseek( fd, filepos, SEEK_SET );
188     close( fd );
189     if (sec) free( sec );
190     set_error( STATUS_INVALID_FILE_FOR_SECTION );
191     return 0;
192 }
193
194
195 static struct object *create_mapping( int size_high, int size_low, int protect,
196                                       int handle, const WCHAR *name, size_t len )
197 {
198     struct mapping *mapping;
199     int access = 0;
200
201     if (!page_mask) init_page_size();
202
203     if (!(mapping = create_named_object( &mapping_ops, name, len )))
204         return NULL;
205     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
206         return &mapping->obj;  /* Nothing else to do */
207
208     mapping->header_size = 0;
209     mapping->base        = NULL;
210     mapping->shared_file = NULL;
211     mapping->shared_size = 0;
212
213     if (protect & VPROT_READ) access |= GENERIC_READ;
214     if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
215
216     if (handle != -1)
217     {
218         if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
219         if (protect & VPROT_IMAGE)
220         {
221             if (!get_image_params( mapping )) goto error;
222             return &mapping->obj;
223         }
224         if (!size_high && !size_low)
225         {
226             struct get_file_info_request req;
227             struct object *obj = (struct object *)mapping->file;
228             obj->ops->get_file_info( obj, &req );
229             size_high = req.size_high;
230             size_low  = ROUND_SIZE( 0, req.size_low );
231         }
232         else if (!grow_file( mapping->file, size_high, size_low )) goto error;
233     }
234     else  /* Anonymous mapping (no associated file) */
235     {
236         if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
237         {
238             set_error( STATUS_INVALID_PARAMETER );
239             mapping->file = NULL;
240             goto error;
241         }
242         if (!(mapping->file = create_temp_file( access ))) goto error;
243         if (!grow_file( mapping->file, size_high, size_low )) goto error;
244     }
245     mapping->size_high = size_high;
246     mapping->size_low  = ROUND_SIZE( 0, size_low );
247     mapping->protect   = protect;
248     return &mapping->obj;
249
250  error:
251     release_object( mapping );
252     return NULL;
253 }
254
255 static void mapping_dump( struct object *obj, int verbose )
256 {
257     struct mapping *mapping = (struct mapping *)obj;
258     assert( obj->ops == &mapping_ops );
259     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
260              "shared_file=%p shared_size=%08x ",
261              mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
262              mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
263     dump_object_name( &mapping->obj );
264     fputc( '\n', stderr );
265 }
266
267 static void mapping_destroy( struct object *obj )
268 {
269     struct mapping *mapping = (struct mapping *)obj;
270     assert( obj->ops == &mapping_ops );
271     if (mapping->file) release_object( mapping->file );
272     if (mapping->shared_file) release_object( mapping->shared_file );
273 }
274
275 int get_page_size(void)
276 {
277     if (!page_mask) init_page_size();
278     return page_mask + 1;
279 }
280
281 /* create a file mapping */
282 DECL_HANDLER(create_mapping)
283 {
284     struct object *obj;
285
286     req->handle = -1;
287     if ((obj = create_mapping( req->size_high, req->size_low,
288                                req->protect, req->file_handle,
289                                get_req_data(req), get_req_data_size(req) )))
290     {
291         int access = FILE_MAP_ALL_ACCESS;
292         if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
293         req->handle = alloc_handle( current->process, obj, access, req->inherit );
294         release_object( obj );
295     }
296 }
297
298 /* open a handle to a mapping */
299 DECL_HANDLER(open_mapping)
300 {
301     req->handle = open_object( get_req_data(req), get_req_data_size(req),
302                                &mapping_ops, req->access, req->inherit );
303 }
304
305 /* get a mapping information */
306 DECL_HANDLER(get_mapping_info)
307 {
308     struct mapping *mapping;
309
310     if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
311                                                      0, &mapping_ops )))
312     {
313         req->size_high   = mapping->size_high;
314         req->size_low    = mapping->size_low;
315         req->protect     = mapping->protect;
316         req->header_size = mapping->header_size;
317         req->base        = mapping->base;
318         req->shared_file = -1;
319         req->shared_size = mapping->shared_size;
320         if (mapping->shared_file)
321             req->shared_file = alloc_handle( current->process, mapping->shared_file,
322                                              GENERIC_READ|GENERIC_WRITE, 0 );
323         if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
324         release_object( mapping );
325     }
326 }
327