Check the attributes of the *current* section, not only the attributes
[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() do { /* nothing */ } while(0)
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     long toread;
99
100     /* compute the total size of the shared mapping */
101
102     total_size = max_size = 0;
103     for (i = 0; i < nb_sec; i++)
104     {
105         if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
106             (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
107         {
108             int size = ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
109             if (size > max_size) max_size = size;
110             total_size += size;
111         }
112     }
113     if (!(mapping->shared_size = total_size)) return 1;  /* nothing to do */
114
115     /* create a temp file for the mapping */
116
117     if (!(mapping->shared_file = create_temp_file( GENERIC_READ|GENERIC_WRITE ))) goto error;
118     if (!grow_file( mapping->shared_file, 0, total_size )) goto error;
119     if ((shared_fd = file_get_mmap_fd( mapping->shared_file )) == -1) goto error;
120
121     if (!(buffer = malloc( max_size ))) goto error;
122
123     /* copy the shared sections data into the temp file */
124
125     for (i = pos = 0; i < nb_sec; i++)
126     {
127         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
128         if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
129         if (lseek( shared_fd, pos, SEEK_SET ) != pos) goto error;
130         pos += ROUND_SIZE( 0, sec[i].Misc.VirtualSize );
131         if (sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) continue;
132         if (!sec[i].PointerToRawData || !sec[i].SizeOfRawData) continue;
133         if (lseek( fd, sec[i].PointerToRawData, SEEK_SET ) != sec[i].PointerToRawData) goto error;
134         toread = sec[i].SizeOfRawData;
135         while (toread)
136         {
137             long res = read( fd, buffer + sec[i].SizeOfRawData - toread, toread );
138             if (res <= 0) goto error;
139             toread -= res;
140         }
141         if (write( shared_fd, buffer, sec[i].SizeOfRawData ) != sec[i].SizeOfRawData) goto error;
142     }
143     close( shared_fd );
144     free( buffer );
145     return 1;
146
147  error:
148     if (shared_fd != -1) close( shared_fd );
149     if (buffer) free( buffer );
150     return 0;
151 }
152
153 /* retrieve the mapping parameters for an executable (PE) image */
154 static int get_image_params( struct mapping *mapping )
155 {
156     IMAGE_DOS_HEADER dos;
157     IMAGE_NT_HEADERS nt;
158     IMAGE_SECTION_HEADER *sec = NULL;
159     int fd, filepos, size;
160
161     /* load the headers */
162
163     if ((fd = file_get_mmap_fd( mapping->file )) == -1) return 0;
164     filepos = lseek( fd, 0, SEEK_SET );
165     if (read( fd, &dos, sizeof(dos) ) != sizeof(dos)) goto error;
166     if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
167     if (lseek( fd, dos.e_lfanew, SEEK_SET ) == -1) goto error;
168     if (read( fd, &nt, sizeof(nt) ) != sizeof(nt)) goto error;
169     if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
170
171     /* load the section headers */
172
173     size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
174     if (!(sec = malloc( size ))) goto error;
175     if (read( fd, sec, size ) != size) goto error;
176
177     if (!build_shared_mapping( mapping, fd, sec, nt.FileHeader.NumberOfSections )) goto error;
178
179     mapping->size_low    = ROUND_SIZE( 0, nt.OptionalHeader.SizeOfImage );
180     mapping->size_high   = 0;
181     mapping->base        = (void *)nt.OptionalHeader.ImageBase;
182     mapping->header_size = ROUND_SIZE( mapping->base, nt.OptionalHeader.SizeOfHeaders );
183     mapping->protect     = VPROT_IMAGE;
184
185     /* sanity check */
186     if (mapping->header_size > mapping->size_low) goto error;
187
188     lseek( fd, filepos, SEEK_SET );
189     close( fd );
190     free( sec );
191     return 1;
192
193  error:
194     lseek( fd, filepos, SEEK_SET );
195     close( fd );
196     if (sec) free( sec );
197     set_error( STATUS_INVALID_FILE_FOR_SECTION );
198     return 0;
199 }
200
201
202 static struct object *create_mapping( int size_high, int size_low, int protect,
203                                       int handle, const WCHAR *name, size_t len )
204 {
205     struct mapping *mapping;
206     int access = 0;
207
208     if (!page_mask) init_page_size();
209
210     if (!(mapping = create_named_object( &mapping_ops, name, len )))
211         return NULL;
212     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
213         return &mapping->obj;  /* Nothing else to do */
214
215     mapping->header_size = 0;
216     mapping->base        = NULL;
217     mapping->shared_file = NULL;
218     mapping->shared_size = 0;
219
220     if (protect & VPROT_READ) access |= GENERIC_READ;
221     if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
222
223     if (handle != -1)
224     {
225         if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
226         if (protect & VPROT_IMAGE)
227         {
228             if (!get_image_params( mapping )) goto error;
229             return &mapping->obj;
230         }
231         if (!size_high && !size_low)
232         {
233             struct get_file_info_request req;
234             struct object *obj = (struct object *)mapping->file;
235             obj->ops->get_file_info( obj, &req );
236             size_high = req.size_high;
237             size_low  = ROUND_SIZE( 0, req.size_low );
238         }
239         else if (!grow_file( mapping->file, size_high, size_low )) goto error;
240     }
241     else  /* Anonymous mapping (no associated file) */
242     {
243         if ((!size_high && !size_low) || (protect & VPROT_IMAGE))
244         {
245             set_error( STATUS_INVALID_PARAMETER );
246             mapping->file = NULL;
247             goto error;
248         }
249         if (!(mapping->file = create_temp_file( access ))) goto error;
250         if (!grow_file( mapping->file, size_high, size_low )) goto error;
251     }
252     mapping->size_high = size_high;
253     mapping->size_low  = ROUND_SIZE( 0, size_low );
254     mapping->protect   = protect;
255     return &mapping->obj;
256
257  error:
258     release_object( mapping );
259     return NULL;
260 }
261
262 static void mapping_dump( struct object *obj, int verbose )
263 {
264     struct mapping *mapping = (struct mapping *)obj;
265     assert( obj->ops == &mapping_ops );
266     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
267              "shared_file=%p shared_size=%08x ",
268              mapping->size_high, mapping->size_low, mapping->protect, mapping->file,
269              mapping->header_size, mapping->base, mapping->shared_file, mapping->shared_size );
270     dump_object_name( &mapping->obj );
271     fputc( '\n', stderr );
272 }
273
274 static void mapping_destroy( struct object *obj )
275 {
276     struct mapping *mapping = (struct mapping *)obj;
277     assert( obj->ops == &mapping_ops );
278     if (mapping->file) release_object( mapping->file );
279     if (mapping->shared_file) release_object( mapping->shared_file );
280 }
281
282 int get_page_size(void)
283 {
284     if (!page_mask) init_page_size();
285     return page_mask + 1;
286 }
287
288 /* create a file mapping */
289 DECL_HANDLER(create_mapping)
290 {
291     struct object *obj;
292
293     req->handle = -1;
294     if ((obj = create_mapping( req->size_high, req->size_low,
295                                req->protect, req->file_handle,
296                                get_req_data(req), get_req_data_size(req) )))
297     {
298         int access = FILE_MAP_ALL_ACCESS;
299         if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
300         req->handle = alloc_handle( current->process, obj, access, req->inherit );
301         release_object( obj );
302     }
303 }
304
305 /* open a handle to a mapping */
306 DECL_HANDLER(open_mapping)
307 {
308     req->handle = open_object( get_req_data(req), get_req_data_size(req),
309                                &mapping_ops, req->access, req->inherit );
310 }
311
312 /* get a mapping information */
313 DECL_HANDLER(get_mapping_info)
314 {
315     struct mapping *mapping;
316
317     if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
318                                                      0, &mapping_ops )))
319     {
320         req->size_high   = mapping->size_high;
321         req->size_low    = mapping->size_low;
322         req->protect     = mapping->protect;
323         req->header_size = mapping->header_size;
324         req->base        = mapping->base;
325         req->shared_file = -1;
326         req->shared_size = mapping->shared_size;
327         if (mapping->shared_file)
328             req->shared_file = alloc_handle( current->process, mapping->shared_file,
329                                              GENERIC_READ|GENERIC_WRITE, 0 );
330         if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
331         release_object( mapping );
332     }
333 }
334