[KERNEL32_WINETEST]
[reactos.git] / rostests / winetests / kernel32 / virtual.c
1 /*
2 * Unit test suite for Virtual* family of APIs.
3 *
4 * Copyright 2004 Dmitry Timoshkov
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
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "winerror.h"
30 #include "wine/test.h"
31
32 #define NUM_THREADS 4
33 #define MAPPING_SIZE 0x100000
34
35 static HINSTANCE hkernel32;
36 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
37 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
38 static UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
39 static UINT (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
40 static NTSTATUS (WINAPI *pNtAreMappedFilesTheSame)(PVOID,PVOID);
41 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
42 static DWORD (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
43
44 /* ############################### */
45
46 static HANDLE create_target_process(const char *arg)
47 {
48 char **argv;
49 char cmdline[MAX_PATH];
50 PROCESS_INFORMATION pi;
51 BOOL ret;
52 STARTUPINFOA si = { 0 };
53 si.cb = sizeof(si);
54
55 winetest_get_mainargs( &argv );
56 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
57 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
58 ok(ret, "error: %u\n", GetLastError());
59 ret = CloseHandle(pi.hThread);
60 ok(ret, "error %u\n", GetLastError());
61 return pi.hProcess;
62 }
63
64 static void test_VirtualAllocEx(void)
65 {
66 const unsigned int alloc_size = 1<<15;
67 char *src, *dst;
68 SIZE_T bytes_written = 0, bytes_read = 0, i;
69 void *addr1, *addr2;
70 BOOL b;
71 DWORD old_prot;
72 MEMORY_BASIC_INFORMATION info;
73 HANDLE hProcess;
74
75 /* not exported in all windows-versions */
76 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
77 win_skip("Virtual{Alloc,Free}Ex not available\n");
78 return;
79 }
80
81 hProcess = create_target_process("sleep");
82 ok(hProcess != NULL, "Can't start process\n");
83
84 SetLastError(0xdeadbeef);
85 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
86 PAGE_EXECUTE_READWRITE);
87 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
88 { /* Win9x */
89 win_skip("VirtualAllocEx not implemented\n");
90 TerminateProcess(hProcess, 0);
91 CloseHandle(hProcess);
92 return;
93 }
94
95 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
96 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
97 for (i = 0; i < alloc_size; i++)
98 src[i] = i & 0xff;
99
100 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
101 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
102 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
103 bytes_written);
104 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
105 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
106 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
107
108 /* test invalid source buffers */
109
110 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
111 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
112 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
113 ok( !b, "WriteProcessMemory succeeded\n" );
114 ok( GetLastError() == ERROR_NOACCESS ||
115 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
116 "wrong error %u\n", GetLastError() );
117 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
118 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
119 ok( !b, "ReadProcessMemory succeeded\n" );
120 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
121 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
122
123 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
124 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
125 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
126 ok( !b, "WriteProcessMemory succeeded\n" );
127 ok( GetLastError() == ERROR_NOACCESS ||
128 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
129 "wrong error %u\n", GetLastError() );
130 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
131 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
132 ok( !b, "ReadProcessMemory succeeded\n" );
133 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
134 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
135
136 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
137 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
138
139 VirtualFree( src, 0, MEM_FREE );
140 VirtualFree( dst, 0, MEM_FREE );
141
142 /*
143 * The following tests parallel those in test_VirtualAlloc()
144 */
145
146 SetLastError(0xdeadbeef);
147 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
148 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
149 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
150 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
151 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
152
153 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
154 ok(addr1 != NULL, "VirtualAllocEx failed\n");
155
156 /* test a not committed memory */
157 memset(&info, 'q', sizeof(info));
158 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
159 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
160 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
161 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
162 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
163 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
164 /* NT reports Protect == 0 for a not committed memory block */
165 ok(info.Protect == 0 /* NT */ ||
166 info.Protect == PAGE_NOACCESS, /* Win9x */
167 "%x != PAGE_NOACCESS\n", info.Protect);
168 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
169
170 SetLastError(0xdeadbeef);
171 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
172 "VirtualProtectEx should fail on a not committed memory\n");
173 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
174 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
175 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
176
177 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
178 ok(addr1 == addr2, "VirtualAllocEx failed\n");
179
180 /* test a committed memory */
181 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
182 "VirtualQueryEx failed\n");
183 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
184 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
185 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
186 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
187 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
188 /* this time NT reports PAGE_NOACCESS as well */
189 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
190 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
191
192 /* this should fail, since not the whole range is committed yet */
193 SetLastError(0xdeadbeef);
194 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
195 "VirtualProtectEx should fail on a not committed memory\n");
196 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
197 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
198 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
199
200 old_prot = 0;
201 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
202 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
203
204 old_prot = 0;
205 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
206 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
207
208 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
209 "VirtualFreeEx should fail with type 0\n");
210 ok(GetLastError() == ERROR_INVALID_PARAMETER,
211 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
212
213 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
214
215 /* if the type is MEM_RELEASE, size must be 0 */
216 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
217 "VirtualFreeEx should fail\n");
218 ok(GetLastError() == ERROR_INVALID_PARAMETER,
219 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
220
221 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
222
223 TerminateProcess(hProcess, 0);
224 CloseHandle(hProcess);
225 }
226
227 static void test_VirtualAlloc(void)
228 {
229 void *addr1, *addr2;
230 DWORD old_prot;
231 MEMORY_BASIC_INFORMATION info;
232
233 SetLastError(0xdeadbeef);
234 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
235 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
236 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
237 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
238 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
239
240 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
241 ok(addr1 != NULL, "VirtualAlloc failed\n");
242
243 /* test a not committed memory */
244 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
245 "VirtualQuery failed\n");
246 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
247 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
248 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
249 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
250 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
251 /* NT reports Protect == 0 for a not committed memory block */
252 ok(info.Protect == 0 /* NT */ ||
253 info.Protect == PAGE_NOACCESS, /* Win9x */
254 "%x != PAGE_NOACCESS\n", info.Protect);
255 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
256
257 SetLastError(0xdeadbeef);
258 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
259 "VirtualProtect should fail on a not committed memory\n");
260 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
261 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
262 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
263
264 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
265 ok(addr1 == addr2, "VirtualAlloc failed\n");
266
267 /* test a committed memory */
268 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
269 "VirtualQuery failed\n");
270 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
271 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
272 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
273 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
274 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
275 /* this time NT reports PAGE_NOACCESS as well */
276 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
277 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
278
279 /* this should fail, since not the whole range is committed yet */
280 SetLastError(0xdeadbeef);
281 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
282 "VirtualProtect should fail on a not committed memory\n");
283 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
284 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
285 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
286
287 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
288 ok(old_prot == PAGE_NOACCESS,
289 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
290
291 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
292 ok(old_prot == PAGE_READONLY,
293 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
294
295 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
296 "VirtualQuery failed\n");
297 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
298 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
299 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
300 memset( addr1, 0x55, 20 );
301 ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
302
303 addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
304 ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
305 "VirtualAlloc failed err %u\n", GetLastError() );
306 ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
307 if (addr2)
308 {
309 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
310 "VirtualQuery failed\n");
311 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
312 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
313 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
314
315 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
316 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
317
318 ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
319 "VirtualQuery failed\n");
320 ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
321 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
322 ok(info.Protect == 0, "%x != 0\n", info.Protect);
323
324 addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
325 ok( !addr2, "VirtualAlloc failed\n" );
326 ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
327 }
328
329 /* invalid protection values */
330 SetLastError(0xdeadbeef);
331 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
332 ok(!addr2, "VirtualAlloc succeeded\n");
333 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
334
335 SetLastError(0xdeadbeef);
336 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
337 ok(!addr2, "VirtualAlloc succeeded\n");
338 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
339
340 SetLastError(0xdeadbeef);
341 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
342 ok(!addr2, "VirtualAlloc succeeded\n");
343 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
344
345 SetLastError(0xdeadbeef);
346 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
347 "VirtualProtect succeeded\n");
348 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
349
350 SetLastError(0xdeadbeef);
351 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
353
354 SetLastError(0xdeadbeef);
355 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER,
357 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
358
359 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
360
361 /* if the type is MEM_RELEASE, size must be 0 */
362 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
363 ok(GetLastError() == ERROR_INVALID_PARAMETER,
364 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
365
366 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
367 }
368
369 static void test_MapViewOfFile(void)
370 {
371 static const char testfile[] = "testfile.xxx";
372 const char *name;
373 HANDLE file, mapping, map2;
374 void *ptr, *ptr2, *addr;
375 MEMORY_BASIC_INFORMATION info;
376 BOOL ret;
377
378 SetLastError(0xdeadbeef);
379 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
380 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
381 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
382 SetEndOfFile( file );
383
384 /* read/write mapping */
385
386 SetLastError(0xdeadbeef);
387 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
388 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
389
390 SetLastError(0xdeadbeef);
391 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
392 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
393 UnmapViewOfFile( ptr );
394
395 /* this fails on win9x but succeeds on NT */
396 SetLastError(0xdeadbeef);
397 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
398 if (ptr) UnmapViewOfFile( ptr );
399 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
400
401 SetLastError(0xdeadbeef);
402 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
403 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
404 UnmapViewOfFile( ptr );
405
406 SetLastError(0xdeadbeef);
407 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
408 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
409 UnmapViewOfFile( ptr );
410
411 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
412 FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
413 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
414 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
415 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
416 UnmapViewOfFile( ptr );
417 CloseHandle( map2 );
418
419 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
420 FILE_MAP_READ, FALSE, 0 );
421 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
422 SetLastError(0xdeadbeef);
423 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
424 if (!ptr)
425 {
426 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
427 CloseHandle( map2 );
428 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
429 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
430 SetLastError(0xdeadbeef);
431 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
432 ok( !ptr, "MapViewOfFile succeeded\n" );
433 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
434 CloseHandle( map2 );
435 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
436 FILE_MAP_READ, FALSE, 0 );
437 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
438 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
439 ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
440 }
441 else win_skip( "no access checks on win9x\n" );
442
443 UnmapViewOfFile( ptr );
444 CloseHandle( map2 );
445 CloseHandle( mapping );
446
447 /* read-only mapping */
448
449 SetLastError(0xdeadbeef);
450 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
451 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
452
453 SetLastError(0xdeadbeef);
454 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
455 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
456 UnmapViewOfFile( ptr );
457
458 /* this fails on win9x but succeeds on NT */
459 SetLastError(0xdeadbeef);
460 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
461 if (ptr) UnmapViewOfFile( ptr );
462 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
463
464 SetLastError(0xdeadbeef);
465 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
466 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
467 UnmapViewOfFile( ptr );
468
469 SetLastError(0xdeadbeef);
470 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
471 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
472 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
473 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
474 CloseHandle( mapping );
475
476 /* copy-on-write mapping */
477
478 SetLastError(0xdeadbeef);
479 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
480 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
481
482 SetLastError(0xdeadbeef);
483 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
484 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
485 UnmapViewOfFile( ptr );
486
487 SetLastError(0xdeadbeef);
488 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
489 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
490 UnmapViewOfFile( ptr );
491
492 SetLastError(0xdeadbeef);
493 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
494 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
495 UnmapViewOfFile( ptr );
496
497 SetLastError(0xdeadbeef);
498 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
499 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
500 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
501 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
502 CloseHandle( mapping );
503
504 /* no access mapping */
505
506 SetLastError(0xdeadbeef);
507 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
508 /* fails on NT but succeeds on win9x */
509 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
510 else
511 {
512 SetLastError(0xdeadbeef);
513 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
514 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
515 UnmapViewOfFile( ptr );
516
517 SetLastError(0xdeadbeef);
518 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
519 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
520 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
521
522 SetLastError(0xdeadbeef);
523 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
524 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
525 UnmapViewOfFile( ptr );
526
527 SetLastError(0xdeadbeef);
528 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
529 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
530 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
531
532 CloseHandle( mapping );
533 }
534
535 CloseHandle( file );
536
537 /* now try read-only file */
538
539 SetLastError(0xdeadbeef);
540 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
541 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
542
543 SetLastError(0xdeadbeef);
544 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
545 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
546 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
547 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
548
549 SetLastError(0xdeadbeef);
550 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
551 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
552 CloseHandle( mapping );
553
554 SetLastError(0xdeadbeef);
555 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
556 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
557 CloseHandle( mapping );
558 CloseHandle( file );
559
560 /* now try no access file */
561
562 SetLastError(0xdeadbeef);
563 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
564 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
565
566 SetLastError(0xdeadbeef);
567 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
568 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
569 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
570 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
571
572 SetLastError(0xdeadbeef);
573 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
574 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
575 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
576 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
577
578 SetLastError(0xdeadbeef);
579 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
580 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
581 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
582 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
583
584 CloseHandle( file );
585 DeleteFileA( testfile );
586
587 SetLastError(0xdeadbeef);
588 name = "Local\\Foo";
589 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
590 /* nt4 doesn't have Local\\ */
591 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
592 {
593 name = "Foo";
594 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
595 }
596 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
597
598 SetLastError(0xdeadbeef);
599 mapping = OpenFileMappingA( FILE_MAP_READ, FALSE, name );
600 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
601 SetLastError(0xdeadbeef);
602 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
603 if (!ptr)
604 {
605 SIZE_T size;
606 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
607 SetLastError(0xdeadbeef);
608 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
609 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
610 SetLastError(0xdeadbeef);
611 size = VirtualQuery( ptr, &info, sizeof(info) );
612 ok( size == sizeof(info),
613 "VirtualQuery error %u\n", GetLastError() );
614 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
615 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
616 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
617 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
618 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
619 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
620 }
621 else win_skip( "no access checks on win9x\n" );
622 UnmapViewOfFile( ptr );
623 CloseHandle( mapping );
624
625 SetLastError(0xdeadbeef);
626 mapping = OpenFileMappingA( FILE_MAP_WRITE, FALSE, name );
627 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
628 SetLastError(0xdeadbeef);
629 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
630 if (!ptr)
631 {
632 SIZE_T size;
633 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
634 SetLastError(0xdeadbeef);
635 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
636 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
637 SetLastError(0xdeadbeef);
638 size = VirtualQuery( ptr, &info, sizeof(info) );
639 ok( size == sizeof(info),
640 "VirtualQuery error %u\n", GetLastError() );
641 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
642 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
643 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
644 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
645 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
646 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
647 }
648 else win_skip( "no access checks on win9x\n" );
649 UnmapViewOfFile( ptr );
650 CloseHandle( mapping );
651
652 CloseHandle( file );
653
654 /* read/write mapping with SEC_RESERVE */
655 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
656 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
657
658 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
659 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
660
661 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
662 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
663 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
664 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
665
666 ret = VirtualQuery(ptr, &info, sizeof(info));
667 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
668 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
669 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
670 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
671 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
672 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
673 {
674 ok(info.AllocationProtect == PAGE_NOACCESS,
675 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
676 ok(info.Protect == PAGE_NOACCESS,
677 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
678 }
679 else
680 {
681 ok(info.AllocationProtect == PAGE_READWRITE,
682 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
683 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
684 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
685 }
686
687 if (ptr != ptr2)
688 {
689 ret = VirtualQuery(ptr2, &info, sizeof(info));
690 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
691 ok(info.BaseAddress == ptr2,
692 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
693 ok(info.AllocationBase == ptr2,
694 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
695 ok(info.AllocationProtect == PAGE_READWRITE,
696 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
697 ok(info.RegionSize == MAPPING_SIZE,
698 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
699 ok(info.State == MEM_RESERVE,
700 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
701 ok(info.Protect == 0,
702 "Protect should have been 0 instead of 0x%x\n", info.Protect);
703 ok(info.Type == MEM_MAPPED,
704 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
705 }
706
707 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
708 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
709
710 ret = VirtualQuery(ptr, &info, sizeof(info));
711 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
712 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
713 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
714 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
715 ok(info.State == MEM_COMMIT, "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
716 ok(info.Protect == PAGE_READONLY, "Protect should have been PAGE_READONLY instead of 0x%x\n", info.Protect);
717 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
718 {
719 ok(info.AllocationProtect == PAGE_NOACCESS,
720 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
721 }
722 else
723 {
724 ok(info.AllocationProtect == PAGE_READWRITE,
725 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
726 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
727 }
728
729 /* shows that the VirtualAlloc above affects the mapping, not just the
730 * virtual memory in this process - it also affects all other processes
731 * with a view of the mapping, but that isn't tested here */
732 if (ptr != ptr2)
733 {
734 ret = VirtualQuery(ptr2, &info, sizeof(info));
735 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
736 ok(info.BaseAddress == ptr2,
737 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
738 ok(info.AllocationBase == ptr2,
739 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
740 ok(info.AllocationProtect == PAGE_READWRITE,
741 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
742 ok(info.RegionSize == 0x10000,
743 "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
744 ok(info.State == MEM_COMMIT,
745 "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
746 ok(info.Protect == PAGE_READWRITE,
747 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info.Protect);
748 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
749 }
750
751 addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
752 ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
753 "VirtualAlloc failed with error %u\n", GetLastError() );
754
755 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
756 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
757 if (!ret)
758 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
759
760 ret = UnmapViewOfFile(ptr2);
761 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
762 ret = UnmapViewOfFile(ptr);
763 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
764 CloseHandle(mapping);
765
766 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
767 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
768
769 SetLastError(0xdeadbeef);
770 ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
771 ok( GetLastError() == ERROR_INVALID_ADDRESS,
772 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
773 SetLastError(0xdeadbeef);
774 ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
775 ok( GetLastError() == ERROR_INVALID_ADDRESS,
776 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
777 SetLastError(0xdeadbeef);
778 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779 ok( GetLastError() == ERROR_INVALID_ADDRESS,
780 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
781
782 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
783
784 /* close named mapping handle without unmapping */
785 name = "Foo";
786 SetLastError(0xdeadbeef);
787 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
788 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
789 SetLastError(0xdeadbeef);
790 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
791 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
792 SetLastError(0xdeadbeef);
793 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
794 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
795 SetLastError(0xdeadbeef);
796 ret = CloseHandle(map2);
797 ok(ret, "CloseHandle error %d\n", GetLastError());
798 SetLastError(0xdeadbeef);
799 ret = CloseHandle(mapping);
800 ok(ret, "CloseHandle error %d\n", GetLastError());
801
802 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
803 ok( !ret, "memory is not accessible\n" );
804
805 ret = VirtualQuery(ptr, &info, sizeof(info));
806 ok(ret, "VirtualQuery error %d\n", GetLastError());
807 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
808 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
809 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
810 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
811 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
812 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
813 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
814
815 SetLastError(0xdeadbeef);
816 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
817 todo_wine
818 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
819 todo_wine
820 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
821 if (map2) CloseHandle(map2); /* FIXME: remove once Wine is fixed */
822 SetLastError(0xdeadbeef);
823 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
824 ok( mapping != 0, "CreateFileMappingA failed\n" );
825 todo_wine
826 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
827 SetLastError(0xdeadbeef);
828 ret = CloseHandle(mapping);
829 ok(ret, "CloseHandle error %d\n", GetLastError());
830
831 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
832 ok( !ret, "memory is not accessible\n" );
833
834 ret = VirtualQuery(ptr, &info, sizeof(info));
835 ok(ret, "VirtualQuery error %d\n", GetLastError());
836 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
837 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
838 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
839 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
840 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
841 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
842 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
843
844 SetLastError(0xdeadbeef);
845 ret = UnmapViewOfFile(ptr);
846 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
847
848 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
849 ok( ret, "memory is accessible\n" );
850
851 ret = VirtualQuery(ptr, &info, sizeof(info));
852 ok(ret, "VirtualQuery error %d\n", GetLastError());
853 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
854 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
855 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
856 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
857 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
858 ok(info.Type == 0, "%#x != 0\n", info.Type);
859
860 SetLastError(0xdeadbeef);
861 file = CreateFileA(testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
862 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
863 SetFilePointer(file, 4096, NULL, FILE_BEGIN);
864 SetEndOfFile(file);
865
866 SetLastError(0xdeadbeef);
867 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
868 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
869 SetLastError(0xdeadbeef);
870 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
871 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
872 SetLastError(0xdeadbeef);
873 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
874 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
875 SetLastError(0xdeadbeef);
876 ret = CloseHandle(map2);
877 ok(ret, "CloseHandle error %d\n", GetLastError());
878 SetLastError(0xdeadbeef);
879 ret = CloseHandle(mapping);
880 ok(ret, "CloseHandle error %d\n", GetLastError());
881
882 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
883 ok( !ret, "memory is not accessible\n" );
884
885 ret = VirtualQuery(ptr, &info, sizeof(info));
886 ok(ret, "VirtualQuery error %d\n", GetLastError());
887 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
888 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
889 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
890 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
891 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
892 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
893 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
894
895 SetLastError(0xdeadbeef);
896 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
897 todo_wine
898 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
899 todo_wine
900 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
901 CloseHandle(map2);
902 SetLastError(0xdeadbeef);
903 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
904 ok( mapping != 0, "CreateFileMappingA failed\n" );
905 todo_wine
906 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
907 SetLastError(0xdeadbeef);
908 ret = CloseHandle(mapping);
909 ok(ret, "CloseHandle error %d\n", GetLastError());
910
911 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
912 ok( !ret, "memory is not accessible\n" );
913
914 ret = VirtualQuery(ptr, &info, sizeof(info));
915 ok(ret, "VirtualQuery error %d\n", GetLastError());
916 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
917 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
918 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
919 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
920 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
921 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
922 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
923
924 SetLastError(0xdeadbeef);
925 ret = UnmapViewOfFile(ptr);
926 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
927
928 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
929 ok( ret, "memory is accessible\n" );
930
931 ret = VirtualQuery(ptr, &info, sizeof(info));
932 ok(ret, "VirtualQuery error %d\n", GetLastError());
933 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
934 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
935 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
936 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
937 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
938 ok(info.Type == 0, "%#x != 0\n", info.Type);
939
940 CloseHandle(file);
941 DeleteFileA(testfile);
942 }
943
944 static void test_NtMapViewOfSection(void)
945 {
946 HANDLE hProcess;
947
948 static const char testfile[] = "testfile.xxx";
949 static const char data[] = "test data for NtMapViewOfSection";
950 char buffer[sizeof(data)];
951 HANDLE file, mapping;
952 void *ptr;
953 BOOL ret;
954 DWORD status, written;
955 SIZE_T size, result;
956 LARGE_INTEGER offset;
957
958 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
959 {
960 win_skip( "NtMapViewOfSection not available\n" );
961 return;
962 }
963
964 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
965 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
966 WriteFile( file, data, sizeof(data), &written, NULL );
967 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
968 SetEndOfFile( file );
969
970 /* read/write mapping */
971
972 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
973 ok( mapping != 0, "CreateFileMapping failed\n" );
974
975 hProcess = create_target_process("sleep");
976 ok(hProcess != NULL, "Can't start process\n");
977
978 ptr = NULL;
979 size = 0;
980 offset.QuadPart = 0;
981 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
982 ok( !status, "NtMapViewOfSection failed status %x\n", status );
983
984 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
985 ok( ret, "ReadProcessMemory failed\n" );
986 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
987 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
988
989 status = pNtUnmapViewOfSection( hProcess, ptr );
990 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
991
992 CloseHandle( mapping );
993 CloseHandle( file );
994 DeleteFileA( testfile );
995
996 TerminateProcess(hProcess, 0);
997 CloseHandle(hProcess);
998 }
999
1000 static void test_NtAreMappedFilesTheSame(void)
1001 {
1002 static const char testfile[] = "testfile.xxx";
1003 HANDLE file, file2, mapping, map2;
1004 void *ptr, *ptr2;
1005 NTSTATUS status;
1006 char path[MAX_PATH];
1007
1008 if (!pNtAreMappedFilesTheSame)
1009 {
1010 win_skip( "NtAreMappedFilesTheSame not available\n" );
1011 return;
1012 }
1013
1014 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1015 NULL, CREATE_ALWAYS, 0, 0 );
1016 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1017 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1018 SetEndOfFile( file );
1019
1020 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1021 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1022
1023 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1024 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1025
1026 file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1027 NULL, OPEN_EXISTING, 0, 0 );
1028 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1029
1030 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
1031 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1032 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1033 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1034 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1035 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1036 UnmapViewOfFile( ptr2 );
1037
1038 ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1039 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1040 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1041 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1042 UnmapViewOfFile( ptr2 );
1043 CloseHandle( map2 );
1044
1045 map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1046 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1047 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1048 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1049 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1050 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1051 UnmapViewOfFile( ptr2 );
1052 CloseHandle( map2 );
1053 CloseHandle( file2 );
1054
1055 status = pNtAreMappedFilesTheSame( ptr, ptr );
1056 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1057 "NtAreMappedFilesTheSame returned %x\n", status );
1058
1059 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
1060 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1061 "NtAreMappedFilesTheSame returned %x\n", status );
1062
1063 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1064 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1065
1066 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
1067 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
1068 "NtAreMappedFilesTheSame returned %x\n", status );
1069
1070 status = pNtAreMappedFilesTheSame( ptr, NULL );
1071 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1072
1073 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
1074 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1075
1076 status = pNtAreMappedFilesTheSame( NULL, NULL );
1077 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1078
1079 ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
1080 ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
1081 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1082 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1083 VirtualFree( ptr2, 0, MEM_RELEASE );
1084
1085 UnmapViewOfFile( ptr );
1086 CloseHandle( mapping );
1087 CloseHandle( file );
1088
1089 status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1090 GetModuleHandleA("kernel32.dll") );
1091 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1092 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1093 GetModuleHandleA("kernel32.dll") );
1094 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1095 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1096 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1097 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1098
1099 GetSystemDirectoryA( path, MAX_PATH );
1100 strcat( path, "\\kernel32.dll" );
1101 file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1102 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1103
1104 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1105 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1106 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1107 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1108 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1109 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1110 UnmapViewOfFile( ptr );
1111 CloseHandle( mapping );
1112
1113 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1114 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1115 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1116 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1117 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1118 todo_wine
1119 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1120
1121 file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1122 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1123 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1124 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1125 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
1126 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1127 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1128 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1129 UnmapViewOfFile( ptr2 );
1130 CloseHandle( map2 );
1131 CloseHandle( file2 );
1132
1133 UnmapViewOfFile( ptr );
1134 CloseHandle( mapping );
1135
1136 CloseHandle( file );
1137 DeleteFileA( testfile );
1138 }
1139
1140 static void test_CreateFileMapping(void)
1141 {
1142 HANDLE handle, handle2;
1143
1144 /* test case sensitivity */
1145
1146 SetLastError(0xdeadbeef);
1147 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1148 "Wine Test Mapping");
1149 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1150 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1151
1152 SetLastError(0xdeadbeef);
1153 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1154 "Wine Test Mapping");
1155 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1156 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
1157 CloseHandle( handle2 );
1158
1159 SetLastError(0xdeadbeef);
1160 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1161 "WINE TEST MAPPING");
1162 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1163 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1164 CloseHandle( handle2 );
1165
1166 SetLastError(0xdeadbeef);
1167 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1168 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1169 CloseHandle( handle2 );
1170
1171 SetLastError(0xdeadbeef);
1172 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1173 ok( !handle2, "OpenFileMapping succeeded\n");
1174 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1175 "wrong error %u\n", GetLastError());
1176
1177 CloseHandle( handle );
1178 }
1179
1180 static void test_IsBadReadPtr(void)
1181 {
1182 BOOL ret;
1183 void *ptr = (void *)0xdeadbeef;
1184 char stackvar;
1185
1186 ret = IsBadReadPtr(NULL, 0);
1187 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1188
1189 ret = IsBadReadPtr(NULL, 1);
1190 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1191
1192 ret = IsBadReadPtr(ptr, 0);
1193 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1194
1195 ret = IsBadReadPtr(ptr, 1);
1196 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1197
1198 ret = IsBadReadPtr(&stackvar, 0);
1199 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1200
1201 ret = IsBadReadPtr(&stackvar, sizeof(char));
1202 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1203 }
1204
1205 static void test_IsBadWritePtr(void)
1206 {
1207 BOOL ret;
1208 void *ptr = (void *)0xdeadbeef;
1209 char stackval;
1210
1211 ret = IsBadWritePtr(NULL, 0);
1212 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1213
1214 ret = IsBadWritePtr(NULL, 1);
1215 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1216
1217 ret = IsBadWritePtr(ptr, 0);
1218 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1219
1220 ret = IsBadWritePtr(ptr, 1);
1221 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1222
1223 ret = IsBadWritePtr(&stackval, 0);
1224 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1225
1226 ret = IsBadWritePtr(&stackval, sizeof(char));
1227 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1228 }
1229
1230 static void test_IsBadCodePtr(void)
1231 {
1232 BOOL ret;
1233 void *ptr = (void *)0xdeadbeef;
1234 char stackval;
1235
1236 ret = IsBadCodePtr(NULL);
1237 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1238
1239 ret = IsBadCodePtr(ptr);
1240 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1241
1242 ret = IsBadCodePtr((void *)&stackval);
1243 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1244 }
1245
1246 static void test_write_watch(void)
1247 {
1248 char *base;
1249 DWORD ret, size, old_prot;
1250 MEMORY_BASIC_INFORMATION info;
1251 void *results[64];
1252 ULONG_PTR count;
1253 ULONG pagesize;
1254
1255 if (!pGetWriteWatch || !pResetWriteWatch)
1256 {
1257 win_skip( "GetWriteWatch not supported\n" );
1258 return;
1259 }
1260
1261 size = 0x10000;
1262 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1263 if (!base &&
1264 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1265 {
1266 win_skip( "MEM_WRITE_WATCH not supported\n" );
1267 return;
1268 }
1269 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1270 ret = VirtualQuery( base, &info, sizeof(info) );
1271 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1272 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1273 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1274 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1275 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1276 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1277 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1278
1279 count = 64;
1280 SetLastError( 0xdeadbeef );
1281 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1282 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1283 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1284 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1285 "wrong error %u\n", GetLastError() );
1286
1287 SetLastError( 0xdeadbeef );
1288 ret = pGetWriteWatch( 0, GetModuleHandleW(NULL), size, results, &count, &pagesize );
1289 if (ret)
1290 {
1291 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1292 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1293 }
1294 else /* win98 */
1295 {
1296 ok( count == 0, "wrong count %lu\n", count );
1297 }
1298
1299 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1300 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1301 ok( count == 0, "wrong count %lu\n", count );
1302
1303 base[pagesize + 1] = 0x44;
1304
1305 count = 64;
1306 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1307 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1308 ok( count == 1, "wrong count %lu\n", count );
1309 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1310
1311 count = 64;
1312 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1313 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1314 ok( count == 1, "wrong count %lu\n", count );
1315 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1316
1317 count = 64;
1318 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1319 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1320 ok( count == 0, "wrong count %lu\n", count );
1321
1322 base[2*pagesize + 3] = 0x11;
1323 base[4*pagesize + 8] = 0x11;
1324
1325 count = 64;
1326 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1327 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1328 ok( count == 2, "wrong count %lu\n", count );
1329 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1330 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1331
1332 count = 64;
1333 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1334 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1335 ok( count == 1, "wrong count %lu\n", count );
1336 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1337
1338 ret = pResetWriteWatch( base, 3*pagesize );
1339 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1340
1341 count = 64;
1342 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1343 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1344 ok( count == 1, "wrong count %lu\n", count );
1345 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1346
1347 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1348
1349 count = 64;
1350 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1351 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1352 ok( count == 3, "wrong count %lu\n", count );
1353 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1354 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1355 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1356
1357 count = 1;
1358 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1359 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1360 ok( count == 1, "wrong count %lu\n", count );
1361 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1362
1363 count = 64;
1364 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1365 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1366 ok( count == 2, "wrong count %lu\n", count );
1367 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1368 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1369
1370 /* changing protections doesn't affect watches */
1371
1372 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1373 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1374 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1375
1376 ret = VirtualQuery( base, &info, sizeof(info) );
1377 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1378 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1379 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1380 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1381 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1382
1383 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1384 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1385 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1386
1387 count = 64;
1388 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1389 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1390 ok( count == 2, "wrong count %lu\n", count );
1391 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1392 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1393
1394 ret = VirtualQuery( base, &info, sizeof(info) );
1395 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1396 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1397 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1398 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1399 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1400
1401 /* some invalid parameter tests */
1402
1403 SetLastError( 0xdeadbeef );
1404 count = 0;
1405 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1406 if (ret)
1407 {
1408 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1409 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1410
1411 SetLastError( 0xdeadbeef );
1412 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1413 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1414 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1415
1416 SetLastError( 0xdeadbeef );
1417 count = 64;
1418 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1419 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1420 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1421
1422 SetLastError( 0xdeadbeef );
1423 count = 64;
1424 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1425 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1426 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1427
1428 SetLastError( 0xdeadbeef );
1429 count = 0;
1430 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1431 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1432 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1433
1434 SetLastError( 0xdeadbeef );
1435 count = 64;
1436 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1437 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1438 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1439
1440 SetLastError( 0xdeadbeef );
1441 count = 64;
1442 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1443 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1444 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1445
1446 SetLastError( 0xdeadbeef );
1447 count = 64;
1448 ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1449 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1450 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1451
1452 SetLastError( 0xdeadbeef );
1453 count = 64;
1454 ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1455 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1456 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1457
1458 SetLastError( 0xdeadbeef );
1459 ret = pResetWriteWatch( base, 0 );
1460 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1461 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1462
1463 SetLastError( 0xdeadbeef );
1464 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1465 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1466 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1467 }
1468 else /* win98 is completely different */
1469 {
1470 SetLastError( 0xdeadbeef );
1471 count = 64;
1472 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1473 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1474 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1475
1476 count = 0;
1477 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1478 ok( !ret, "GetWriteWatch failed %u\n", ret );
1479
1480 count = 64;
1481 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1482 ok( !ret, "GetWriteWatch failed %u\n", ret );
1483
1484 count = 64;
1485 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1486 ok( !ret, "GetWriteWatch failed %u\n", ret );
1487
1488 ret = pResetWriteWatch( base, 0 );
1489 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1490
1491 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1492 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1493 }
1494
1495 VirtualFree( base, 0, MEM_FREE );
1496
1497 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1498 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1499 VirtualFree( base, 0, MEM_FREE );
1500
1501 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1502 ok( !base, "VirtualAlloc succeeded\n" );
1503 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1504
1505 /* initial protect doesn't matter */
1506
1507 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1508 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1509 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1510 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1511
1512 count = 64;
1513 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1514 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1515 ok( count == 0, "wrong count %lu\n", count );
1516
1517 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1518 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1519 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1520
1521 base[5*pagesize + 200] = 3;
1522
1523 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1524 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1525 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1526
1527 count = 64;
1528 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1529 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1530 ok( count == 1, "wrong count %lu\n", count );
1531 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1532
1533 ret = VirtualFree( base, size, MEM_DECOMMIT );
1534 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1535
1536 count = 64;
1537 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1538 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1539 ok( count == 1 || broken(count == 0), /* win98 */
1540 "wrong count %lu\n", count );
1541 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1542
1543 VirtualFree( base, 0, MEM_FREE );
1544 }
1545
1546 static void test_VirtualProtect(void)
1547 {
1548 static const struct test_data
1549 {
1550 DWORD prot_set, prot_get;
1551 } td[] =
1552 {
1553 { 0, 0 }, /* 0x00 */
1554 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
1555 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
1556 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
1557 { PAGE_READWRITE, PAGE_READWRITE }, /* 0x04 */
1558 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
1559 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
1560 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
1561 { PAGE_WRITECOPY, 0 }, /* 0x08 */
1562 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
1563 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
1564 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
1565 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
1566 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
1567 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
1568 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
1569
1570 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
1571 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
1572 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
1573 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1574 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
1575 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
1576 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
1577 { PAGE_EXECUTE_WRITECOPY, 0 }, /* 0x80 */
1578 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
1579 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
1580 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
1581 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
1582 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
1583 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
1584 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
1585 };
1586 char *base, *ptr;
1587 DWORD ret, old_prot, rw_prot, exec_prot, i, j;
1588 MEMORY_BASIC_INFORMATION info;
1589 SYSTEM_INFO si;
1590
1591 GetSystemInfo(&si);
1592 trace("system page size %#x\n", si.dwPageSize);
1593
1594 SetLastError(0xdeadbeef);
1595 base = VirtualAlloc(0, si.dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
1596 ok(base != NULL, "VirtualAlloc failed %d\n", GetLastError());
1597
1598 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1599 {
1600 SetLastError(0xdeadbeef);
1601 ret = VirtualQuery(base, &info, sizeof(info));
1602 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1603 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1604 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1605 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1606 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1607 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1608 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1609 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1610
1611 old_prot = 0xdeadbeef;
1612 SetLastError(0xdeadbeef);
1613 ret = VirtualProtect(base, si.dwPageSize, td[i].prot_set, &old_prot);
1614 if (td[i].prot_get)
1615 {
1616 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1617 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1618
1619 SetLastError(0xdeadbeef);
1620 ret = VirtualQuery(base, &info, sizeof(info));
1621 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1622 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1623 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1624 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
1625 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1626 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1627 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1628 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1629 }
1630 else
1631 {
1632 ok(!ret, "%d: VirtualProtect should fail\n", i);
1633 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1634 }
1635
1636 old_prot = 0xdeadbeef;
1637 SetLastError(0xdeadbeef);
1638 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1639 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1640 if (td[i].prot_get)
1641 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
1642 else
1643 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1644 }
1645
1646 exec_prot = 0;
1647
1648 for (i = 0; i <= 4; i++)
1649 {
1650 rw_prot = 0;
1651
1652 for (j = 0; j <= 4; j++)
1653 {
1654 DWORD prot = exec_prot | rw_prot;
1655
1656 SetLastError(0xdeadbeef);
1657 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, prot);
1658 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1659 {
1660 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
1661 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1662 }
1663 else
1664 {
1665 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
1666 {
1667 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
1668 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1669 }
1670 else
1671 {
1672 ok(ptr != NULL, "VirtualAlloc(%02x) error %d\n", prot, GetLastError());
1673 ok(ptr == base, "expected %p, got %p\n", base, ptr);
1674 }
1675 }
1676
1677 SetLastError(0xdeadbeef);
1678 ret = VirtualProtect(base, si.dwPageSize, prot, &old_prot);
1679 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1680 {
1681 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1682 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1683 }
1684 else
1685 {
1686 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
1687 {
1688 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1689 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1690 }
1691 else
1692 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
1693 }
1694
1695 rw_prot = 1 << j;
1696 }
1697
1698 exec_prot = 1 << (i + 4);
1699 }
1700
1701 VirtualFree(base, 0, MEM_FREE);
1702 }
1703
1704 static BOOL is_mem_writable(DWORD prot)
1705 {
1706 switch (prot & 0xff)
1707 {
1708 case PAGE_READWRITE:
1709 case PAGE_WRITECOPY:
1710 case PAGE_EXECUTE_READWRITE:
1711 case PAGE_EXECUTE_WRITECOPY:
1712 return TRUE;
1713
1714 default:
1715 return FALSE;
1716 }
1717 }
1718
1719 static void test_VirtualAlloc_protection(void)
1720 {
1721 static const struct test_data
1722 {
1723 DWORD prot;
1724 BOOL success;
1725 } td[] =
1726 {
1727 { 0, FALSE }, /* 0x00 */
1728 { PAGE_NOACCESS, TRUE }, /* 0x01 */
1729 { PAGE_READONLY, TRUE }, /* 0x02 */
1730 { PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x03 */
1731 { PAGE_READWRITE, TRUE }, /* 0x04 */
1732 { PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x05 */
1733 { PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x06 */
1734 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x07 */
1735 { PAGE_WRITECOPY, FALSE }, /* 0x08 */
1736 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE }, /* 0x09 */
1737 { PAGE_WRITECOPY | PAGE_READONLY, FALSE }, /* 0x0a */
1738 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE }, /* 0x0b */
1739 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE }, /* 0x0c */
1740 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x0d */
1741 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x0e */
1742 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x0f */
1743
1744 { PAGE_EXECUTE, TRUE }, /* 0x10 */
1745 { PAGE_EXECUTE_READ, TRUE }, /* 0x20 */
1746 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x30 */
1747 { PAGE_EXECUTE_READWRITE, TRUE }, /* 0x40 */
1748 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0x50 */
1749 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0x60 */
1750 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x70 */
1751 { PAGE_EXECUTE_WRITECOPY, FALSE }, /* 0x80 */
1752 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE }, /* 0x90 */
1753 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE }, /* 0xa0 */
1754 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0xb0 */
1755 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE }, /* 0xc0 */
1756 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0xd0 */
1757 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0xe0 */
1758 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE } /* 0xf0 */
1759 };
1760 char *base, *ptr;
1761 DWORD ret, i;
1762 MEMORY_BASIC_INFORMATION info;
1763 SYSTEM_INFO si;
1764
1765 GetSystemInfo(&si);
1766 trace("system page size %#x\n", si.dwPageSize);
1767
1768 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1769 {
1770 SetLastError(0xdeadbeef);
1771 base = VirtualAlloc(0, si.dwPageSize, MEM_COMMIT, td[i].prot);
1772
1773 if (td[i].success)
1774 {
1775 ok(base != NULL, "%d: VirtualAlloc failed %d\n", i, GetLastError());
1776
1777 SetLastError(0xdeadbeef);
1778 ret = VirtualQuery(base, &info, sizeof(info));
1779 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1780 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1781 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1782 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1783 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1784 ok(info.AllocationProtect == td[i].prot, "%d: %#x != %#x\n", i, info.AllocationProtect, td[i].prot);
1785 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1786 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1787
1788 if (is_mem_writable(info.Protect))
1789 {
1790 base[0] = 0xfe;
1791
1792 SetLastError(0xdeadbeef);
1793 ret = VirtualQuery(base, &info, sizeof(info));
1794 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1795 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1796 }
1797
1798 SetLastError(0xdeadbeef);
1799 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
1800 ok(ptr == base, "%d: VirtualAlloc failed %d\n", i, GetLastError());
1801
1802 VirtualFree(base, 0, MEM_FREE);
1803 }
1804 else
1805 {
1806 ok(!base, "%d: VirtualAlloc should fail\n", i);
1807 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1808 }
1809 }
1810 }
1811
1812 static void test_CreateFileMapping_protection(void)
1813 {
1814 static const struct test_data
1815 {
1816 DWORD prot;
1817 BOOL success;
1818 DWORD prot_after_write;
1819 } td[] =
1820 {
1821 { 0, FALSE, 0 }, /* 0x00 */
1822 { PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x01 */
1823 { PAGE_READONLY, TRUE, PAGE_READONLY }, /* 0x02 */
1824 { PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x03 */
1825 { PAGE_READWRITE, TRUE, PAGE_READWRITE }, /* 0x04 */
1826 { PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x05 */
1827 { PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x06 */
1828 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x07 */
1829 { PAGE_WRITECOPY, TRUE, PAGE_READWRITE }, /* 0x08 */
1830 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x09 */
1831 { PAGE_WRITECOPY | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0a */
1832 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0b */
1833 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0x0c */
1834 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0d */
1835 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0e */
1836 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0f */
1837
1838 { PAGE_EXECUTE, FALSE, PAGE_EXECUTE }, /* 0x10 */
1839 { PAGE_EXECUTE_READ, TRUE, PAGE_EXECUTE_READ }, /* 0x20 */
1840 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_EXECUTE_READ }, /* 0x30 */
1841 { PAGE_EXECUTE_READWRITE, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1842 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x50 */
1843 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0x60 */
1844 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x70 */
1845 { PAGE_EXECUTE_WRITECOPY, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x80 */
1846 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x90 */
1847 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xa0 */
1848 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xb0 */
1849 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0xc0 */
1850 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xd0 */
1851 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xe0 */
1852 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS } /* 0xf0 */
1853 };
1854 char *base, *ptr;
1855 DWORD ret, i, alloc_prot, prot, old_prot;
1856 MEMORY_BASIC_INFORMATION info;
1857 SYSTEM_INFO si;
1858 char temp_path[MAX_PATH];
1859 char file_name[MAX_PATH];
1860 HANDLE hfile, hmap;
1861 BOOL page_exec_supported = TRUE;
1862
1863 GetSystemInfo(&si);
1864 trace("system page size %#x\n", si.dwPageSize);
1865
1866 GetTempPathA(MAX_PATH, temp_path);
1867 GetTempFileNameA(temp_path, "map", 0, file_name);
1868
1869 SetLastError(0xdeadbeef);
1870 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
1871 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
1872 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
1873 SetEndOfFile(hfile);
1874
1875 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1876 {
1877 SetLastError(0xdeadbeef);
1878 hmap = CreateFileMappingW(hfile, NULL, td[i].prot | SEC_COMMIT, 0, si.dwPageSize, NULL);
1879
1880 if (td[i].success)
1881 {
1882 if (!hmap)
1883 {
1884 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, td[i].prot, GetLastError());
1885 /* NT4 and win2k don't support EXEC on file mappings */
1886 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
1887 {
1888 page_exec_supported = FALSE;
1889 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
1890 continue;
1891 }
1892 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1893 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1894 {
1895 page_exec_supported = FALSE;
1896 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
1897 continue;
1898 }
1899 }
1900 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, td[i].prot, GetLastError());
1901
1902 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
1903 ok(base != NULL, "%d: MapViewOfFile failed %d\n", i, GetLastError());
1904
1905 SetLastError(0xdeadbeef);
1906 ret = VirtualQuery(base, &info, sizeof(info));
1907 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1908 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1909 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1910 ok(info.Protect == PAGE_READONLY, "%d: got %#x != expected PAGE_READONLY\n", i, info.Protect);
1911 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1912 ok(info.AllocationProtect == PAGE_READONLY, "%d: %#x != PAGE_READONLY\n", i, info.AllocationProtect);
1913 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1914 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1915
1916 if (is_mem_writable(info.Protect))
1917 {
1918 base[0] = 0xfe;
1919
1920 SetLastError(0xdeadbeef);
1921 ret = VirtualQuery(base, &info, sizeof(info));
1922 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1923 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1924 }
1925
1926 SetLastError(0xdeadbeef);
1927 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
1928 ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot);
1929 /* FIXME: remove once Wine is fixed */
1930 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
1931 todo_wine
1932 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
1933 else
1934 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
1935
1936 SetLastError(0xdeadbeef);
1937 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
1938 if (td[i].prot == PAGE_READONLY || td[i].prot == PAGE_WRITECOPY)
1939 ok(ret, "%d: VirtualProtect(%02x) error %d\n", i, td[i].prot, GetLastError());
1940 else
1941 {
1942 ok(!ret, "%d: VirtualProtect(%02x) should fail\n", i, td[i].prot);
1943 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1944 }
1945
1946 UnmapViewOfFile(base);
1947 CloseHandle(hmap);
1948 }
1949 else
1950 {
1951 ok(!hmap, "%d: CreateFileMapping should fail\n", i);
1952 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1953 }
1954 }
1955
1956 if (page_exec_supported) alloc_prot = PAGE_EXECUTE_READWRITE;
1957 else alloc_prot = PAGE_READWRITE;
1958 SetLastError(0xdeadbeef);
1959 hmap = CreateFileMappingW(hfile, NULL, alloc_prot, 0, si.dwPageSize, NULL);
1960 ok(hmap != 0, "%d: CreateFileMapping error %d\n", i, GetLastError());
1961
1962 SetLastError(0xdeadbeef);
1963 base = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE | (page_exec_supported ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
1964 ok(base != NULL, "MapViewOfFile failed %d\n", GetLastError());
1965
1966 old_prot = 0xdeadbeef;
1967 SetLastError(0xdeadbeef);
1968 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1969 ok(ret, "VirtualProtect error %d\n", GetLastError());
1970 ok(old_prot == alloc_prot, "got %#x != expected %#x\n", old_prot, alloc_prot);
1971
1972 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1973 {
1974 SetLastError(0xdeadbeef);
1975 ret = VirtualQuery(base, &info, sizeof(info));
1976 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1977 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1978 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1979 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1980 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1981 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
1982 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1983 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1984
1985 old_prot = 0xdeadbeef;
1986 SetLastError(0xdeadbeef);
1987 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
1988 if (td[i].success || td[i].prot == PAGE_NOACCESS || td[i].prot == PAGE_EXECUTE)
1989 {
1990 if (!ret)
1991 {
1992 /* win2k and XP don't support EXEC on file mappings */
1993 if (td[i].prot == PAGE_EXECUTE)
1994 {
1995 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
1996 continue;
1997 }
1998 /* NT4 and win2k don't support EXEC on file mappings */
1999 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
2000 {
2001 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
2002 continue;
2003 }
2004 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2005 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
2006 {
2007 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2008 continue;
2009 }
2010 }
2011
2012 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2013 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2014
2015 prot = td[i].prot;
2016 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
2017 if (prot == PAGE_EXECUTE_WRITECOPY) prot = PAGE_EXECUTE_READWRITE;
2018
2019 SetLastError(0xdeadbeef);
2020 ret = VirtualQuery(base, &info, sizeof(info));
2021 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2022 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2023 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2024 /* FIXME: remove the condition below once Wine is fixed */
2025 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
2026 todo_wine ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
2027 else
2028 ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
2029 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2030 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
2031 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2032 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
2033
2034 if (is_mem_writable(info.Protect))
2035 {
2036 base[0] = 0xfe;
2037
2038 SetLastError(0xdeadbeef);
2039 ret = VirtualQuery(base, &info, sizeof(info));
2040 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2041 /* FIXME: remove the condition below once Wine is fixed */
2042 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2043 todo_wine ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2044 else
2045 ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2046 }
2047 }
2048 else
2049 {
2050 ok(!ret, "%d: VirtualProtect should fail\n", i);
2051 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2052 continue;
2053 }
2054
2055 old_prot = 0xdeadbeef;
2056 SetLastError(0xdeadbeef);
2057 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2058 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2059 /* FIXME: remove the condition below once Wine is fixed */
2060 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2061 todo_wine ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2062 else
2063 ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2064 }
2065
2066 UnmapViewOfFile(base);
2067 CloseHandle(hmap);
2068
2069 CloseHandle(hfile);
2070 DeleteFileA(file_name);
2071 }
2072
2073 #define ACCESS_READ 0x01
2074 #define ACCESS_WRITE 0x02
2075 #define ACCESS_EXECUTE 0x04
2076 #define ACCESS_WRITECOPY 0x08
2077
2078 static DWORD page_prot_to_access(DWORD prot)
2079 {
2080 switch (prot)
2081 {
2082 case PAGE_READWRITE:
2083 return ACCESS_READ | ACCESS_WRITE;
2084
2085 case PAGE_EXECUTE:
2086 case PAGE_EXECUTE_READ:
2087 return ACCESS_READ | ACCESS_EXECUTE;
2088
2089 case PAGE_EXECUTE_READWRITE:
2090 return ACCESS_READ | ACCESS_WRITE | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2091
2092 case PAGE_EXECUTE_WRITECOPY:
2093 return ACCESS_READ | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2094
2095 case PAGE_READONLY:
2096 return ACCESS_READ;
2097
2098 case PAGE_WRITECOPY:
2099 return ACCESS_READ;
2100
2101 default:
2102 return 0;
2103 }
2104 }
2105
2106 static BOOL is_compatible_protection(DWORD map_prot, DWORD view_prot, DWORD prot)
2107 {
2108 DWORD map_access, view_access, prot_access;
2109
2110 map_access = page_prot_to_access(map_prot);
2111 view_access = page_prot_to_access(view_prot);
2112 prot_access = page_prot_to_access(prot);
2113
2114 if (view_access == prot_access) return TRUE;
2115 if (!view_access) return FALSE;
2116
2117 if ((view_access & prot_access) != prot_access) return FALSE;
2118 if ((map_access & prot_access) == prot_access) return TRUE;
2119
2120 return FALSE;
2121 }
2122
2123 static DWORD map_prot_to_access(DWORD prot)
2124 {
2125 switch (prot)
2126 {
2127 case PAGE_READWRITE:
2128 case PAGE_EXECUTE_READWRITE:
2129 return SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2130 case PAGE_READONLY:
2131 case PAGE_WRITECOPY:
2132 case PAGE_EXECUTE:
2133 case PAGE_EXECUTE_READ:
2134 case PAGE_EXECUTE_WRITECOPY:
2135 return SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2136 default:
2137 return 0;
2138 }
2139 }
2140
2141 static BOOL is_compatible_access(DWORD map_prot, DWORD view_prot)
2142 {
2143 DWORD access = map_prot_to_access(map_prot);
2144 if (!view_prot) view_prot = SECTION_MAP_READ;
2145 return (view_prot & access) == view_prot;
2146 }
2147
2148 static void *map_view_of_file(HANDLE handle, DWORD access)
2149 {
2150 NTSTATUS status;
2151 LARGE_INTEGER offset;
2152 SIZE_T count;
2153 ULONG protect;
2154 BOOL exec;
2155 void *addr;
2156
2157 if (!pNtMapViewOfSection) return NULL;
2158
2159 count = 0;
2160 offset.u.LowPart = 0;
2161 offset.u.HighPart = 0;
2162
2163 exec = access & FILE_MAP_EXECUTE;
2164 access &= ~FILE_MAP_EXECUTE;
2165
2166 if (access == FILE_MAP_COPY)
2167 {
2168 if (exec)
2169 protect = PAGE_EXECUTE_WRITECOPY;
2170 else
2171 protect = PAGE_WRITECOPY;
2172 }
2173 else if (access & FILE_MAP_WRITE)
2174 {
2175 if (exec)
2176 protect = PAGE_EXECUTE_READWRITE;
2177 else
2178 protect = PAGE_READWRITE;
2179 }
2180 else if (access & FILE_MAP_READ)
2181 {
2182 if (exec)
2183 protect = PAGE_EXECUTE_READ;
2184 else
2185 protect = PAGE_READONLY;
2186 }
2187 else protect = PAGE_NOACCESS;
2188
2189 addr = NULL;
2190 status = pNtMapViewOfSection(handle, GetCurrentProcess(), &addr, 0, 0, &offset,
2191 &count, 1 /* ViewShare */, 0, protect);
2192 if (status)
2193 {
2194 /* for simplicity */
2195 SetLastError(ERROR_ACCESS_DENIED);
2196 addr = NULL;
2197 }
2198 return addr;
2199 }
2200
2201 static void test_mapping(void)
2202 {
2203 static const DWORD page_prot[] =
2204 {
2205 PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY,
2206 PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY
2207 };
2208 static const struct
2209 {
2210 DWORD access, prot;
2211 } view[] =
2212 {
2213 { 0, PAGE_NOACCESS }, /* 0x00 */
2214 { FILE_MAP_COPY, PAGE_WRITECOPY }, /* 0x01 */
2215 { FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x02 */
2216 { FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x03 */
2217 { FILE_MAP_READ, PAGE_READONLY }, /* 0x04 */
2218 { FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x05 */
2219 { FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x06 */
2220 { FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x07 */
2221 { SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x08 */
2222 { SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x09 */
2223 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0a */
2224 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0b */
2225 { SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_READONLY }, /* 0x0c */
2226 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x0d */
2227 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0e */
2228 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0f */
2229 { FILE_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x20 */
2230 { FILE_MAP_EXECUTE | FILE_MAP_COPY, PAGE_EXECUTE_WRITECOPY }, /* 0x21 */
2231 { FILE_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x22 */
2232 { FILE_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x23 */
2233 { FILE_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x24 */
2234 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x25 */
2235 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x26 */
2236 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x27 */
2237 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x28 */
2238 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x29 */
2239 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2a */
2240 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x2b */
2241 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x2c */
2242 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x2d */
2243 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2e */
2244 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE } /* 0x2f */
2245 };
2246 void *base, *nt_base, *ptr;
2247 DWORD i, j, k, ret, old_prot, prev_prot;
2248 SYSTEM_INFO si;
2249 char temp_path[MAX_PATH];
2250 char file_name[MAX_PATH];
2251 HANDLE hfile, hmap;
2252 MEMORY_BASIC_INFORMATION info, nt_info;
2253
2254 GetSystemInfo(&si);
2255 trace("system page size %#x\n", si.dwPageSize);
2256
2257 GetTempPathA(MAX_PATH, temp_path);
2258 GetTempFileNameA(temp_path, "map", 0, file_name);
2259
2260 SetLastError(0xdeadbeef);
2261 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
2262 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
2263 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
2264 SetEndOfFile(hfile);
2265
2266 for (i = 0; i < sizeof(page_prot)/sizeof(page_prot[0]); i++)
2267 {
2268 SetLastError(0xdeadbeef);
2269 hmap = CreateFileMappingW(hfile, NULL, page_prot[i] | SEC_COMMIT, 0, si.dwPageSize, NULL);
2270
2271 if (page_prot[i] == PAGE_NOACCESS)
2272 {
2273 HANDLE hmap2;
2274
2275 ok(!hmap, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2276 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2277
2278 /* A trick to create a not accessible mapping */
2279 SetLastError(0xdeadbeef);
2280 hmap = CreateFileMappingW(hfile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, si.dwPageSize, NULL);
2281 ok(hmap != 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2282 SetLastError(0xdeadbeef);
2283 ret = DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, FALSE, 0);
2284 ok(ret, "DuplicateHandle error %d\n", GetLastError());
2285 CloseHandle(hmap);
2286 hmap = hmap2;
2287 }
2288
2289 if (!hmap)
2290 {
2291 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, page_prot[i], GetLastError());
2292
2293 /* NT4 and win2k don't support EXEC on file mappings */
2294 if (page_prot[i] == PAGE_EXECUTE_READ || page_prot[i] == PAGE_EXECUTE_READWRITE)
2295 {
2296 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
2297 continue;
2298 }
2299 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2300 if (page_prot[i] == PAGE_EXECUTE_WRITECOPY)
2301 {
2302 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2303 continue;
2304 }
2305 }
2306
2307 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, page_prot[i], GetLastError());
2308
2309 for (j = 0; j < sizeof(view)/sizeof(view[0]); j++)
2310 {
2311 nt_base = map_view_of_file(hmap, view[j].access);
2312 if (nt_base)
2313 {
2314 SetLastError(0xdeadbeef);
2315 ret = VirtualQuery(nt_base, &nt_info, sizeof(nt_info));
2316 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2317 UnmapViewOfFile(nt_base);
2318 }
2319
2320 SetLastError(0xdeadbeef);
2321 base = MapViewOfFile(hmap, view[j].access, 0, 0, 0);
2322
2323 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
2324 ok(!nt_base == !base ||
2325 broken((view[j].access & FILE_MAP_EXECUTE) && !nt_base != !base),
2326 "%d: (%04x/%04x) NT %p kernel %p\n", j, page_prot[i], view[j].access, nt_base, base);
2327
2328 if (!is_compatible_access(page_prot[i], view[j].access))
2329 {
2330 ok(!base, "%d: MapViewOfFile(%04x/%04x) should fail\n", j, page_prot[i], view[j].access);
2331 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
2332 continue;
2333 }
2334
2335 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
2336 if (!base && (view[j].access & FILE_MAP_EXECUTE))
2337 {
2338 ok(broken(!base), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2339 continue;
2340 }
2341
2342 ok(base != NULL, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2343
2344 SetLastError(0xdeadbeef);
2345 ret = VirtualQuery(base, &info, sizeof(info));
2346 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2347 ok(info.BaseAddress == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.BaseAddress, base);
2348 ok(info.RegionSize == si.dwPageSize, "%d: (%04x) got %#lx != expected %#x\n", j, view[j].access, info.RegionSize, si.dwPageSize);
2349 ok(info.Protect == view[j].prot ||
2350 broken(view[j].prot == PAGE_EXECUTE_READ && info.Protect == PAGE_READONLY) || /* win2k */
2351 broken(view[j].prot == PAGE_EXECUTE_READWRITE && info.Protect == PAGE_READWRITE) || /* win2k */
2352 broken(view[j].prot == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2353 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.Protect, view[j].prot);
2354 ok(info.AllocationBase == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.AllocationBase, base);
2355 ok(info.AllocationProtect == info.Protect, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.AllocationProtect, info.Protect);
2356 ok(info.State == MEM_COMMIT, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j, view[j].access, info.State);
2357 ok(info.Type == MEM_MAPPED, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j, view[j].access, info.Type);
2358
2359 if (nt_base && base)
2360 {
2361 ok(nt_info.RegionSize == info.RegionSize, "%d: (%04x) got %#lx != expected %#lx\n", j, view[j].access, nt_info.RegionSize, info.RegionSize);
2362 ok(nt_info.Protect == info.Protect /* Vista+ */ ||
2363 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2364 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Protect, info.Protect);
2365 ok(nt_info.AllocationProtect == info.AllocationProtect /* Vista+ */ ||
2366 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2367 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.AllocationProtect, info.AllocationProtect);
2368 ok(nt_info.State == info.State, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.State, info.State);
2369 ok(nt_info.Type == info.Type, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Type, info.Type);
2370 }
2371
2372 prev_prot = info.Protect;
2373
2374 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
2375 {
2376 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2377 SetLastError(0xdeadbeef);
2378 old_prot = 0xdeadbeef;
2379 ret = VirtualProtect(base, si.dwPageSize, page_prot[k], &old_prot);
2380 if (is_compatible_protection(page_prot[i], view[j].prot, page_prot[k]))
2381 {
2382 /* win2k and XP don't support EXEC on file mappings */
2383 if (!ret && page_prot[k] == PAGE_EXECUTE)
2384 {
2385 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2386 continue;
2387 }
2388 /* NT4 and win2k don't support EXEC on file mappings */
2389 if (!ret && (page_prot[k] == PAGE_EXECUTE_READ || page_prot[k] == PAGE_EXECUTE_READWRITE))
2390 {
2391 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2392 continue;
2393 }
2394 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2395 if (!ret && page_prot[k] == PAGE_EXECUTE_WRITECOPY)
2396 {
2397 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
2398 continue;
2399 }
2400 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
2401 if (!ret && view[j].prot == PAGE_EXECUTE_WRITECOPY)
2402 {
2403 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
2404 continue;
2405 }
2406
2407 ok(ret, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot[i], view[j].prot, page_prot[k]);
2408 ok(old_prot == prev_prot, "got %#x, expected %#x\n", old_prot, prev_prot);
2409 prev_prot = page_prot[k];
2410 }
2411 else
2412 {
2413 /* NT4 doesn't fail on incompatible map and view */
2414 if (ret)
2415 {
2416 ok(broken(ret), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2417 skip("Incompatible map and view are not properly handled on this platform\n");
2418 break; /* NT4 won't pass remaining tests */
2419 }
2420
2421 ok(!ret, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2422 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2423 }
2424 }
2425
2426 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
2427 {
2428 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2429 SetLastError(0xdeadbeef);
2430 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, page_prot[k]);
2431 ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]);
2432 /* FIXME: remove once Wine is fixed */
2433 if (page_prot[k] == PAGE_WRITECOPY || page_prot[k] == PAGE_EXECUTE_WRITECOPY)
2434 todo_wine
2435 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2436 else
2437 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2438 }
2439
2440 UnmapViewOfFile(base);
2441 }
2442
2443 CloseHandle(hmap);
2444 }
2445
2446 CloseHandle(hfile);
2447 DeleteFileA(file_name);
2448 }
2449
2450 static void test_shared_memory(int is_child)
2451 {
2452 HANDLE mapping;
2453 LONG *p;
2454
2455 SetLastError(0xdeadbef);
2456 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_virtual.c");
2457 ok(mapping != 0, "CreateFileMapping error %d\n", GetLastError());
2458 if (is_child)
2459 ok(GetLastError() == ERROR_ALREADY_EXISTS, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
2460
2461 SetLastError(0xdeadbef);
2462 p = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
2463 ok(p != NULL, "MapViewOfFile error %d\n", GetLastError());
2464
2465 if (is_child)
2466 {
2467 ok(*p == 0x1a2b3c4d, "expected 0x1a2b3c4d in child, got %#x\n", *p);
2468 }
2469 else
2470 {
2471 char **argv;
2472 char cmdline[MAX_PATH];
2473 PROCESS_INFORMATION pi;
2474 STARTUPINFOA si = { sizeof(si) };
2475 DWORD ret;
2476
2477 *p = 0x1a2b3c4d;
2478
2479 winetest_get_mainargs(&argv);
2480 sprintf(cmdline, "\"%s\" virtual sharedmem", argv[0]);
2481 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2482 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2483 winetest_wait_child_process(pi.hProcess);
2484 CloseHandle(pi.hThread);
2485 CloseHandle(pi.hProcess);
2486 }
2487
2488 UnmapViewOfFile(p);
2489 CloseHandle(mapping);
2490 }
2491
2492 START_TEST(virtual)
2493 {
2494 int argc;
2495 char **argv;
2496 argc = winetest_get_mainargs( &argv );
2497
2498 if (argc >= 3)
2499 {
2500 if (!strcmp(argv[2], "sleep"))
2501 {
2502 Sleep(5000); /* spawned process runs for at most 5 seconds */
2503 return;
2504 }
2505 if (!strcmp(argv[2], "sharedmem"))
2506 {
2507 test_shared_memory(1);
2508 return;
2509 }
2510 while (1)
2511 {
2512 void *mem;
2513 BOOL ret;
2514 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
2515 PAGE_EXECUTE_READWRITE);
2516 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
2517 if (mem == NULL) break;
2518 ret = VirtualFree(mem, 0, MEM_RELEASE);
2519 ok(ret, "VirtualFree failed %u\n", GetLastError());
2520 if (!ret) break;
2521 }
2522 return;
2523 }
2524
2525 hkernel32 = GetModuleHandleA("kernel32.dll");
2526 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
2527 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
2528 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
2529 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
2530 pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"),
2531 "NtAreMappedFilesTheSame" );
2532 pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtMapViewOfSection");
2533 pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection");
2534
2535 test_shared_memory(0);
2536 test_mapping();
2537 test_CreateFileMapping_protection();
2538 test_VirtualAlloc_protection();
2539 test_VirtualProtect();
2540 test_VirtualAllocEx();
2541 test_VirtualAlloc();
2542 test_MapViewOfFile();
2543 test_NtMapViewOfSection();
2544 test_NtAreMappedFilesTheSame();
2545 test_CreateFileMapping();
2546 test_IsBadReadPtr();
2547 test_IsBadWritePtr();
2548 test_IsBadCodePtr();
2549 test_write_watch();
2550 }