5242be916c730a8680c8b66fddbc9862af64e699
[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 "winnt.h"
29 #include "wine/winternl.h"
30 #include "winerror.h"
31 #include "winuser.h"
32 #include "wine/exception.h"
33 #include "wine/test.h"
34
35 #define NUM_THREADS 4
36 #define MAPPING_SIZE 0x100000
37
38 static HINSTANCE hkernel32, hntdll;
39 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
40 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
41 static UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
42 static UINT (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
43 static NTSTATUS (WINAPI *pNtAreMappedFilesTheSame)(PVOID,PVOID);
44 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
45 static DWORD (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
46 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
47 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG, PVECTORED_EXCEPTION_HANDLER);
48 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID);
49 static BOOL (WINAPI *pGetProcessDEPPolicy)(HANDLE, LPDWORD, PBOOL);
50 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
51 static NTSTATUS (WINAPI *pNtQuerySection)(HANDLE, int, PVOID, ULONG, PULONG);
52 static NTSTATUS (WINAPI *pNtProtectVirtualMemory)(HANDLE, PVOID *, SIZE_T *, ULONG, ULONG *);
53 static NTSTATUS (WINAPI *pNtAllocateVirtualMemory)(HANDLE, PVOID *, ULONG, SIZE_T *, ULONG, ULONG);
54 static NTSTATUS (WINAPI *pNtFreeVirtualMemory)(HANDLE, PVOID *, SIZE_T *, ULONG);
55
56 /* ############################### */
57
58 static UINT_PTR page_mask = 0xfff;
59 #define ROUND_SIZE(addr,size) \
60 (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask)
61
62 static PIMAGE_NT_HEADERS image_nt_header(HMODULE module)
63 {
64 IMAGE_NT_HEADERS *ret = NULL;
65 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)module;
66
67 if (dos->e_magic == IMAGE_DOS_SIGNATURE)
68 {
69 ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
70 if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
71 }
72 return ret;
73 }
74
75 static HANDLE create_target_process(const char *arg)
76 {
77 char **argv;
78 char cmdline[MAX_PATH];
79 PROCESS_INFORMATION pi;
80 BOOL ret;
81 STARTUPINFOA si = { 0 };
82 si.cb = sizeof(si);
83
84 winetest_get_mainargs( &argv );
85 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
86 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
87 ok(ret, "error: %u\n", GetLastError());
88 ret = CloseHandle(pi.hThread);
89 ok(ret, "error %u\n", GetLastError());
90 return pi.hProcess;
91 }
92
93 static void test_VirtualAllocEx(void)
94 {
95 const unsigned int alloc_size = 1<<15;
96 char *src, *dst;
97 SIZE_T bytes_written = 0, bytes_read = 0, i;
98 void *addr1, *addr2;
99 BOOL b;
100 DWORD old_prot;
101 MEMORY_BASIC_INFORMATION info;
102 HANDLE hProcess;
103
104 /* not exported in all windows-versions */
105 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
106 win_skip("Virtual{Alloc,Free}Ex not available\n");
107 return;
108 }
109
110 hProcess = create_target_process("sleep");
111 ok(hProcess != NULL, "Can't start process\n");
112
113 SetLastError(0xdeadbeef);
114 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
115 PAGE_EXECUTE_READWRITE);
116 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
117 { /* Win9x */
118 win_skip("VirtualAllocEx not implemented\n");
119 TerminateProcess(hProcess, 0);
120 CloseHandle(hProcess);
121 return;
122 }
123
124 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
125 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
126 for (i = 0; i < alloc_size; i++)
127 src[i] = i & 0xff;
128
129 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
130 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
131 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
132 bytes_written);
133 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
134 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
135 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
136
137 /* test invalid source buffers */
138
139 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
140 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
141 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
142 ok( !b, "WriteProcessMemory succeeded\n" );
143 ok( GetLastError() == ERROR_NOACCESS ||
144 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
145 "wrong error %u\n", GetLastError() );
146 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
147 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
148 ok( !b, "ReadProcessMemory succeeded\n" );
149 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
150 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
151
152 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
153 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
154 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
155 ok( !b, "WriteProcessMemory succeeded\n" );
156 ok( GetLastError() == ERROR_NOACCESS ||
157 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
158 "wrong error %u\n", GetLastError() );
159 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
160 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
161 ok( !b, "ReadProcessMemory succeeded\n" );
162 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
163 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
164
165 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
166 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
167
168 VirtualFree( src, 0, MEM_RELEASE );
169 VirtualFree( dst, 0, MEM_RELEASE );
170
171 /*
172 * The following tests parallel those in test_VirtualAlloc()
173 */
174
175 SetLastError(0xdeadbeef);
176 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
177 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
178 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
179 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
180 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
181
182 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
183 ok(addr1 != NULL, "VirtualAllocEx failed\n");
184
185 /* test a not committed memory */
186 memset(&info, 'q', sizeof(info));
187 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
188 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
189 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
190 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
191 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
192 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
193 /* NT reports Protect == 0 for a not committed memory block */
194 ok(info.Protect == 0 /* NT */ ||
195 info.Protect == PAGE_NOACCESS, /* Win9x */
196 "%x != PAGE_NOACCESS\n", info.Protect);
197 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
198
199 SetLastError(0xdeadbeef);
200 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
201 "VirtualProtectEx should fail on a not committed memory\n");
202 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
203 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
204 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
205
206 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
207 ok(addr1 == addr2, "VirtualAllocEx failed\n");
208
209 /* test a committed memory */
210 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
211 "VirtualQueryEx failed\n");
212 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
213 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
214 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
215 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
216 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
217 /* this time NT reports PAGE_NOACCESS as well */
218 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
219 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
220
221 /* this should fail, since not the whole range is committed yet */
222 SetLastError(0xdeadbeef);
223 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
224 "VirtualProtectEx should fail on a not committed memory\n");
225 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
226 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
227 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
228
229 old_prot = 0;
230 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
231 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
232
233 old_prot = 0;
234 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
235 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
236
237 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
238 "VirtualFreeEx should fail with type 0\n");
239 ok(GetLastError() == ERROR_INVALID_PARAMETER,
240 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
241
242 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
243
244 /* if the type is MEM_RELEASE, size must be 0 */
245 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
246 "VirtualFreeEx should fail\n");
247 ok(GetLastError() == ERROR_INVALID_PARAMETER,
248 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
249
250 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
251
252 TerminateProcess(hProcess, 0);
253 CloseHandle(hProcess);
254 }
255
256 static void test_VirtualAlloc(void)
257 {
258 void *addr1, *addr2;
259 DWORD old_prot;
260 MEMORY_BASIC_INFORMATION info;
261 NTSTATUS status;
262 SIZE_T size;
263
264 SetLastError(0xdeadbeef);
265 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
266 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
267 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
268 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
269 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
270
271 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
272 ok(addr1 != NULL, "VirtualAlloc failed\n");
273
274 /* test a not committed memory */
275 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
276 "VirtualQuery failed\n");
277 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
278 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
279 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
280 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
281 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
282 /* NT reports Protect == 0 for a not committed memory block */
283 ok(info.Protect == 0 /* NT */ ||
284 info.Protect == PAGE_NOACCESS, /* Win9x */
285 "%x != PAGE_NOACCESS\n", info.Protect);
286 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
287
288 SetLastError(0xdeadbeef);
289 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
290 "VirtualProtect should fail on a not committed memory\n");
291 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
292 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
293 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
294
295 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
296 ok(addr1 == addr2, "VirtualAlloc failed\n");
297
298 /* test a committed memory */
299 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
300 "VirtualQuery failed\n");
301 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
302 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
303 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
304 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
305 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
306 /* this time NT reports PAGE_NOACCESS as well */
307 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
308 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
309
310 /* this should fail, since not the whole range is committed yet */
311 SetLastError(0xdeadbeef);
312 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
313 "VirtualProtect should fail on a not committed memory\n");
314 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
315 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
316 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
317
318 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
319 ok(old_prot == PAGE_NOACCESS,
320 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
321
322 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
323 ok(old_prot == PAGE_READONLY,
324 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
325
326 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
327 "VirtualQuery failed\n");
328 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
329 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
330 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
331 memset( addr1, 0x55, 20 );
332 ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
333
334 addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
335 ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
336 "VirtualAlloc failed err %u\n", GetLastError() );
337 ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
338 if (addr2)
339 {
340 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
341 "VirtualQuery failed\n");
342 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
343 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
344 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
345
346 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
347 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
348
349 ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
350 "VirtualQuery failed\n");
351 ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
352 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
353 ok(info.Protect == 0, "%x != 0\n", info.Protect);
354
355 addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
356 ok( !addr2, "VirtualAlloc failed\n" );
357 ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
358 }
359
360 /* invalid protection values */
361 SetLastError(0xdeadbeef);
362 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
363 ok(!addr2, "VirtualAlloc succeeded\n");
364 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
365
366 SetLastError(0xdeadbeef);
367 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
368 ok(!addr2, "VirtualAlloc succeeded\n");
369 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
370
371 SetLastError(0xdeadbeef);
372 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
373 ok(!addr2, "VirtualAlloc succeeded\n");
374 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
375
376 SetLastError(0xdeadbeef);
377 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
378 "VirtualProtect succeeded\n");
379 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
380
381 SetLastError(0xdeadbeef);
382 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
383 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
384
385 SetLastError(0xdeadbeef);
386 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
387 ok(GetLastError() == ERROR_INVALID_PARAMETER,
388 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
389
390 SetLastError(0xdeadbeef);
391 ok(!VirtualFree(addr1, 0, MEM_FREE), "VirtualFree should fail with type MEM_FREE\n");
392 ok(GetLastError() == ERROR_INVALID_PARAMETER,
393 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
394
395 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
396
397 /* if the type is MEM_RELEASE, size must be 0 */
398 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
399 ok(GetLastError() == ERROR_INVALID_PARAMETER,
400 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
401
402 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
403
404 /* memory returned by VirtualAlloc should be aligned to 64k */
405 addr1 = VirtualAlloc(0, 0x2000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
406 ok(addr1 != NULL, "VirtualAlloc failed\n");
407 ok(!((ULONG_PTR)addr1 & 0xffff), "returned memory %p is not aligned to 64k\n", addr1);
408 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
409 addr2 = VirtualAlloc(addr1, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
410 ok(addr2 == addr1, "VirtualAlloc returned %p, expected %p\n", addr2, addr1);
411
412 /* allocation conflicts because of 64k align */
413 size = 0x1000;
414 addr2 = (char *)addr1 + 0x1000;
415 status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 0, &size,
416 MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
417 ok(status == STATUS_CONFLICTING_ADDRESSES, "NtAllocateVirtualMemory returned %08x\n", status);
418
419 /* it should conflict, even when zero_bits is explicitly set */
420 size = 0x1000;
421 addr2 = (char *)addr1 + 0x1000;
422 status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 12, &size,
423 MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
424 todo_wine
425 ok(status == STATUS_CONFLICTING_ADDRESSES, "NtAllocateVirtualMemory returned %08x\n", status);
426 if (status == STATUS_SUCCESS) ok(VirtualFree(addr2, 0, MEM_RELEASE), "VirtualFree failed\n");
427
428 /* AT_ROUND_TO_PAGE flag is not supported for VirtualAlloc */
429 SetLastError(0xdeadbeef);
430 addr2 = VirtualAlloc(addr1, 0x1000, MEM_RESERVE | MEM_COMMIT | AT_ROUND_TO_PAGE, PAGE_EXECUTE_READWRITE);
431 ok(!addr2, "VirtualAlloc unexpectedly succeeded\n");
432 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
433
434 /* AT_ROUND_TO_PAGE flag is not supported for NtAllocateVirtualMemory */
435 size = 0x1000;
436 addr2 = (char *)addr1 + 0x1000;
437 status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 0, &size, MEM_RESERVE |
438 MEM_COMMIT | AT_ROUND_TO_PAGE, PAGE_EXECUTE_READWRITE);
439 todo_wine
440 ok(status == STATUS_INVALID_PARAMETER_5, "NtAllocateVirtualMemory returned %08x\n", status);
441
442 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
443 }
444
445 static void test_MapViewOfFile(void)
446 {
447 static const char testfile[] = "testfile.xxx";
448 const char *name;
449 HANDLE file, mapping, map2;
450 void *ptr, *ptr2, *addr;
451 MEMORY_BASIC_INFORMATION info;
452 BOOL ret;
453
454 SetLastError(0xdeadbeef);
455 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
456 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
457 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
458 SetEndOfFile( file );
459
460 /* read/write mapping */
461
462 SetLastError(0xdeadbeef);
463 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
464 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
465
466 SetLastError(0xdeadbeef);
467 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
468 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
469 UnmapViewOfFile( ptr );
470
471 /* this fails on win9x but succeeds on NT */
472 SetLastError(0xdeadbeef);
473 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
474 if (ptr) UnmapViewOfFile( ptr );
475 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
476
477 SetLastError(0xdeadbeef);
478 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
479 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
480 UnmapViewOfFile( ptr );
481
482 SetLastError(0xdeadbeef);
483 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
484 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
485 UnmapViewOfFile( ptr );
486
487 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
488 FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
489 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
490 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
491 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
492 UnmapViewOfFile( ptr );
493 CloseHandle( map2 );
494
495 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
496 FILE_MAP_READ, FALSE, 0 );
497 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
498 SetLastError(0xdeadbeef);
499 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
500 if (!ptr)
501 {
502 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
503 CloseHandle( map2 );
504 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
505 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
506 SetLastError(0xdeadbeef);
507 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
508 ok( !ptr, "MapViewOfFile succeeded\n" );
509 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
510 CloseHandle( map2 );
511 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
512 FILE_MAP_READ, FALSE, 0 );
513 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
514 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
515 ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
516 }
517 else win_skip( "no access checks on win9x\n" );
518
519 UnmapViewOfFile( ptr );
520 CloseHandle( map2 );
521 CloseHandle( mapping );
522
523 /* read-only mapping */
524
525 SetLastError(0xdeadbeef);
526 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
527 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
528
529 SetLastError(0xdeadbeef);
530 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
531 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
532 UnmapViewOfFile( ptr );
533
534 /* this fails on win9x but succeeds on NT */
535 SetLastError(0xdeadbeef);
536 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
537 if (ptr) UnmapViewOfFile( ptr );
538 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
539
540 SetLastError(0xdeadbeef);
541 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
542 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
543 UnmapViewOfFile( ptr );
544
545 SetLastError(0xdeadbeef);
546 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
547 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
548 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
549 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
550 CloseHandle( mapping );
551
552 /* copy-on-write mapping */
553
554 SetLastError(0xdeadbeef);
555 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
556 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
557
558 SetLastError(0xdeadbeef);
559 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
560 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
561 UnmapViewOfFile( ptr );
562
563 SetLastError(0xdeadbeef);
564 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
565 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
566 UnmapViewOfFile( ptr );
567
568 SetLastError(0xdeadbeef);
569 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
570 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
571 UnmapViewOfFile( ptr );
572
573 SetLastError(0xdeadbeef);
574 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
575 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
576 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
577 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
578 CloseHandle( mapping );
579
580 /* no access mapping */
581
582 SetLastError(0xdeadbeef);
583 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
584 /* fails on NT but succeeds on win9x */
585 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
586 else
587 {
588 SetLastError(0xdeadbeef);
589 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
590 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
591 UnmapViewOfFile( ptr );
592
593 SetLastError(0xdeadbeef);
594 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
595 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
596 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
597
598 SetLastError(0xdeadbeef);
599 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
600 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
601 UnmapViewOfFile( ptr );
602
603 SetLastError(0xdeadbeef);
604 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
605 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
606 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
607
608 CloseHandle( mapping );
609 }
610
611 CloseHandle( file );
612
613 /* now try read-only file */
614
615 SetLastError(0xdeadbeef);
616 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
617 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
618
619 SetLastError(0xdeadbeef);
620 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
621 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
622 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
623 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
624
625 SetLastError(0xdeadbeef);
626 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
627 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
628 CloseHandle( mapping );
629
630 SetLastError(0xdeadbeef);
631 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
632 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
633 CloseHandle( mapping );
634 CloseHandle( file );
635
636 /* now try no access file */
637
638 SetLastError(0xdeadbeef);
639 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
640 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
641
642 SetLastError(0xdeadbeef);
643 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
644 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
645 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
646 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
647
648 SetLastError(0xdeadbeef);
649 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
650 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
651 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
652 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
653
654 SetLastError(0xdeadbeef);
655 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
656 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
657 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
658 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
659
660 CloseHandle( file );
661 DeleteFileA( testfile );
662
663 SetLastError(0xdeadbeef);
664 name = "Local\\Foo";
665 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
666 /* nt4 doesn't have Local\\ */
667 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
668 {
669 name = "Foo";
670 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
671 }
672 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
673
674 SetLastError(0xdeadbeef);
675 mapping = OpenFileMappingA( FILE_MAP_READ, FALSE, name );
676 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
677 SetLastError(0xdeadbeef);
678 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
679 if (!ptr)
680 {
681 SIZE_T size;
682 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
683 SetLastError(0xdeadbeef);
684 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
685 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
686 SetLastError(0xdeadbeef);
687 size = VirtualQuery( ptr, &info, sizeof(info) );
688 ok( size == sizeof(info),
689 "VirtualQuery error %u\n", GetLastError() );
690 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
691 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
692 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
693 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
694 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
695 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
696 }
697 else win_skip( "no access checks on win9x\n" );
698 UnmapViewOfFile( ptr );
699 CloseHandle( mapping );
700
701 SetLastError(0xdeadbeef);
702 mapping = OpenFileMappingA( FILE_MAP_WRITE, FALSE, name );
703 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
704 SetLastError(0xdeadbeef);
705 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
706 if (!ptr)
707 {
708 SIZE_T size;
709 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
710 SetLastError(0xdeadbeef);
711 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
712 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
713 SetLastError(0xdeadbeef);
714 size = VirtualQuery( ptr, &info, sizeof(info) );
715 ok( size == sizeof(info),
716 "VirtualQuery error %u\n", GetLastError() );
717 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
718 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
719 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
720 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
721 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
722 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
723 }
724 else win_skip( "no access checks on win9x\n" );
725 UnmapViewOfFile( ptr );
726 CloseHandle( mapping );
727
728 CloseHandle( file );
729
730 /* read/write mapping with SEC_RESERVE */
731 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
732 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
733
734 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
735 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
736
737 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
738 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
739 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
740 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
741
742 ret = VirtualQuery(ptr, &info, sizeof(info));
743 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
744 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
745 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
746 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
747 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
748 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
749 {
750 ok(info.AllocationProtect == PAGE_NOACCESS,
751 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
752 ok(info.Protect == PAGE_NOACCESS,
753 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
754 }
755 else
756 {
757 ok(info.AllocationProtect == PAGE_READWRITE,
758 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
759 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
760 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
761 }
762
763 if (ptr != ptr2)
764 {
765 ret = VirtualQuery(ptr2, &info, sizeof(info));
766 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
767 ok(info.BaseAddress == ptr2,
768 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
769 ok(info.AllocationBase == ptr2,
770 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
771 ok(info.AllocationProtect == PAGE_READWRITE,
772 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
773 ok(info.RegionSize == MAPPING_SIZE,
774 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
775 ok(info.State == MEM_RESERVE,
776 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
777 ok(info.Protect == 0,
778 "Protect should have been 0 instead of 0x%x\n", info.Protect);
779 ok(info.Type == MEM_MAPPED,
780 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
781 }
782
783 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
784 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
785
786 ret = VirtualQuery(ptr, &info, sizeof(info));
787 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
788 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
789 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
790 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
791 ok(info.State == MEM_COMMIT, "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
792 ok(info.Protect == PAGE_READONLY, "Protect should have been PAGE_READONLY instead of 0x%x\n", info.Protect);
793 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
794 {
795 ok(info.AllocationProtect == PAGE_NOACCESS,
796 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
797 }
798 else
799 {
800 ok(info.AllocationProtect == PAGE_READWRITE,
801 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
802 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
803 }
804
805 /* shows that the VirtualAlloc above affects the mapping, not just the
806 * virtual memory in this process - it also affects all other processes
807 * with a view of the mapping, but that isn't tested here */
808 if (ptr != ptr2)
809 {
810 ret = VirtualQuery(ptr2, &info, sizeof(info));
811 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
812 ok(info.BaseAddress == ptr2,
813 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
814 ok(info.AllocationBase == ptr2,
815 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
816 ok(info.AllocationProtect == PAGE_READWRITE,
817 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
818 ok(info.RegionSize == 0x10000,
819 "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
820 ok(info.State == MEM_COMMIT,
821 "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
822 ok(info.Protect == PAGE_READWRITE,
823 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info.Protect);
824 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
825 }
826
827 addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
828 ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
829 "VirtualAlloc failed with error %u\n", GetLastError() );
830
831 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
832 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
833 if (!ret)
834 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
835
836 ret = UnmapViewOfFile(ptr2);
837 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
838 ret = UnmapViewOfFile(ptr);
839 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
840 CloseHandle(mapping);
841
842 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
843 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
844
845 SetLastError(0xdeadbeef);
846 ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
847 ok( GetLastError() == ERROR_INVALID_ADDRESS,
848 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
849 SetLastError(0xdeadbeef);
850 ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
851 ok( GetLastError() == ERROR_INVALID_ADDRESS,
852 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
853 SetLastError(0xdeadbeef);
854 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
855 ok( GetLastError() == ERROR_INVALID_ADDRESS,
856 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
857
858 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
859
860 /* close named mapping handle without unmapping */
861 name = "Foo";
862 SetLastError(0xdeadbeef);
863 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
864 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
865 SetLastError(0xdeadbeef);
866 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
867 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
868 SetLastError(0xdeadbeef);
869 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
870 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
871 SetLastError(0xdeadbeef);
872 ret = CloseHandle(map2);
873 ok(ret, "CloseHandle error %d\n", GetLastError());
874 SetLastError(0xdeadbeef);
875 ret = CloseHandle(mapping);
876 ok(ret, "CloseHandle error %d\n", GetLastError());
877
878 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
879 ok( !ret, "memory is not accessible\n" );
880
881 ret = VirtualQuery(ptr, &info, sizeof(info));
882 ok(ret, "VirtualQuery error %d\n", GetLastError());
883 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
884 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
885 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
886 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
887 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
888 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
889 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
890
891 SetLastError(0xdeadbeef);
892 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
893 todo_wine
894 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
895 todo_wine
896 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
897 if (map2) CloseHandle(map2); /* FIXME: remove once Wine is fixed */
898 SetLastError(0xdeadbeef);
899 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
900 ok( mapping != 0, "CreateFileMappingA failed\n" );
901 todo_wine
902 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
903 SetLastError(0xdeadbeef);
904 ret = CloseHandle(mapping);
905 ok(ret, "CloseHandle error %d\n", GetLastError());
906
907 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
908 ok( !ret, "memory is not accessible\n" );
909
910 ret = VirtualQuery(ptr, &info, sizeof(info));
911 ok(ret, "VirtualQuery error %d\n", GetLastError());
912 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
913 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
914 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
915 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
916 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
917 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
918 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
919
920 SetLastError(0xdeadbeef);
921 ret = UnmapViewOfFile(ptr);
922 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
923
924 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
925 ok( ret, "memory is accessible\n" );
926
927 ret = VirtualQuery(ptr, &info, sizeof(info));
928 ok(ret, "VirtualQuery error %d\n", GetLastError());
929 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
930 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
931 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
932 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
933 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
934 ok(info.Type == 0, "%#x != 0\n", info.Type);
935
936 SetLastError(0xdeadbeef);
937 file = CreateFileA(testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
938 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
939 SetFilePointer(file, 4096, NULL, FILE_BEGIN);
940 SetEndOfFile(file);
941
942 SetLastError(0xdeadbeef);
943 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
944 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
945 SetLastError(0xdeadbeef);
946 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
947 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
948 SetLastError(0xdeadbeef);
949 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
950 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
951 SetLastError(0xdeadbeef);
952 ret = CloseHandle(map2);
953 ok(ret, "CloseHandle error %d\n", GetLastError());
954 SetLastError(0xdeadbeef);
955 ret = CloseHandle(mapping);
956 ok(ret, "CloseHandle error %d\n", GetLastError());
957
958 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
959 ok( !ret, "memory is not accessible\n" );
960
961 ret = VirtualQuery(ptr, &info, sizeof(info));
962 ok(ret, "VirtualQuery error %d\n", GetLastError());
963 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
964 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
965 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
966 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
967 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
968 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
969 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
970
971 SetLastError(0xdeadbeef);
972 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
973 todo_wine
974 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
975 todo_wine
976 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
977 CloseHandle(map2);
978 SetLastError(0xdeadbeef);
979 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
980 ok( mapping != 0, "CreateFileMappingA failed\n" );
981 todo_wine
982 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
983 SetLastError(0xdeadbeef);
984 ret = CloseHandle(mapping);
985 ok(ret, "CloseHandle error %d\n", GetLastError());
986
987 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
988 ok( !ret, "memory is not accessible\n" );
989
990 ret = VirtualQuery(ptr, &info, sizeof(info));
991 ok(ret, "VirtualQuery error %d\n", GetLastError());
992 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
993 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
994 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
995 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
996 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
997 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
998 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
999
1000 SetLastError(0xdeadbeef);
1001 ret = UnmapViewOfFile(ptr);
1002 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
1003
1004 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
1005 ok( ret, "memory is accessible\n" );
1006
1007 ret = VirtualQuery(ptr, &info, sizeof(info));
1008 ok(ret, "VirtualQuery error %d\n", GetLastError());
1009 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
1010 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
1011 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
1012 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
1013 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
1014 ok(info.Type == 0, "%#x != 0\n", info.Type);
1015
1016 CloseHandle(file);
1017 DeleteFileA(testfile);
1018 }
1019
1020 static void test_NtMapViewOfSection(void)
1021 {
1022 HANDLE hProcess;
1023
1024 static const char testfile[] = "testfile.xxx";
1025 static const char data[] = "test data for NtMapViewOfSection";
1026 char buffer[sizeof(data)];
1027 HANDLE file, mapping;
1028 void *ptr, *ptr2;
1029 BOOL is_wow64, ret;
1030 DWORD status, written;
1031 SIZE_T size, result;
1032 LARGE_INTEGER offset;
1033
1034 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
1035 {
1036 win_skip( "NtMapViewOfSection not available\n" );
1037 return;
1038 }
1039
1040 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1041 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
1042 WriteFile( file, data, sizeof(data), &written, NULL );
1043 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1044 SetEndOfFile( file );
1045
1046 /* read/write mapping */
1047
1048 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1049 ok( mapping != 0, "CreateFileMapping failed\n" );
1050
1051 hProcess = create_target_process("sleep");
1052 ok(hProcess != NULL, "Can't start process\n");
1053
1054 ptr = NULL;
1055 size = 0;
1056 offset.QuadPart = 0;
1057 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1058 ok( !status, "NtMapViewOfSection failed status %x\n", status );
1059 ok( !((ULONG_PTR)ptr & 0xffff), "returned memory %p is not aligned to 64k\n", ptr );
1060
1061 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
1062 ok( ret, "ReadProcessMemory failed\n" );
1063 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
1064 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
1065
1066 /* for some unknown reason NtMapViewOfSection fails with STATUS_NO_MEMORY when zero_bits != 0 ? */
1067 ptr2 = NULL;
1068 size = 0;
1069 offset.QuadPart = 0;
1070 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 12, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1071 todo_wine
1072 ok( status == STATUS_NO_MEMORY, "NtMapViewOfSection returned %x\n", status );
1073 if (status == STATUS_SUCCESS)
1074 {
1075 status = pNtUnmapViewOfSection( hProcess, ptr2 );
1076 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1077 }
1078
1079 ptr2 = NULL;
1080 size = 0;
1081 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 16, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1082 todo_wine
1083 ok( status == STATUS_NO_MEMORY, "NtMapViewOfSection returned %x\n", status );
1084 if (status == STATUS_SUCCESS)
1085 {
1086 status = pNtUnmapViewOfSection( hProcess, ptr2 );
1087 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1088 }
1089
1090 /* mapping at the same page conflicts */
1091 ptr2 = ptr;
1092 size = 0;
1093 offset.QuadPart = 0;
1094 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1095 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtMapViewOfSection returned %x\n", status );
1096
1097 /* offset has to be aligned */
1098 ptr2 = ptr;
1099 size = 0;
1100 offset.QuadPart = 1;
1101 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1102 ok( status == STATUS_MAPPED_ALIGNMENT, "NtMapViewOfSection returned %x\n", status );
1103
1104 /* ptr has to be aligned */
1105 ptr2 = (char *)ptr + 42;
1106 size = 0;
1107 offset.QuadPart = 0;
1108 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1109 ok( status == STATUS_MAPPED_ALIGNMENT, "NtMapViewOfSection returned %x\n", status );
1110
1111 /* still not 64k aligned */
1112 ptr2 = (char *)ptr + 0x1000;
1113 size = 0;
1114 offset.QuadPart = 0;
1115 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1116 ok( status == STATUS_MAPPED_ALIGNMENT, "NtMapViewOfSection returned %x\n", status );
1117
1118 /* zero_bits != 0 is not allowed when an address is set */
1119 ptr2 = (char *)ptr + 0x1000;
1120 size = 0;
1121 offset.QuadPart = 0;
1122 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 12, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1123 ok( status == STATUS_INVALID_PARAMETER_4, "NtMapViewOfSection returned %x\n", status );
1124
1125 ptr2 = (char *)ptr + 0x1000;
1126 size = 0;
1127 offset.QuadPart = 0;
1128 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 16, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1129 ok( status == STATUS_INVALID_PARAMETER_4, "NtMapViewOfSection returned %x\n", status );
1130
1131 ptr2 = (char *)ptr + 0x1001;
1132 size = 0;
1133 offset.QuadPart = 0;
1134 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 16, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1135 ok( status == STATUS_INVALID_PARAMETER_4, "NtMapViewOfSection returned %x\n", status );
1136
1137 ptr2 = (char *)ptr + 0x1000;
1138 size = 0;
1139 offset.QuadPart = 1;
1140 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 16, 0, &offset, &size, 1, 0, PAGE_READWRITE );
1141 ok( status == STATUS_INVALID_PARAMETER_4, "NtMapViewOfSection returned %x\n", status );
1142
1143 if (sizeof(void *) == sizeof(int) && (!pIsWow64Process ||
1144 !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64))
1145 {
1146 /* new memory region conflicts with previous mapping */
1147 ptr2 = ptr;
1148 size = 0;
1149 offset.QuadPart = 0;
1150 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1151 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1152 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtMapViewOfSection returned %x\n", status );
1153
1154 ptr2 = (char *)ptr + 42;
1155 size = 0;
1156 offset.QuadPart = 0;
1157 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1158 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1159 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtMapViewOfSection returned %x\n", status );
1160
1161 /* in contrary to regular NtMapViewOfSection, only 4kb align is enforced */
1162 ptr2 = (char *)ptr + 0x1000;
1163 size = 0;
1164 offset.QuadPart = 0;
1165 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1166 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1167 ok( status == STATUS_SUCCESS, "NtMapViewOfSection returned %x\n", status );
1168 ok( (char *)ptr2 == (char *)ptr + 0x1000,
1169 "expected address %p, got %p\n", (char *)ptr + 0x1000, ptr2 );
1170 status = pNtUnmapViewOfSection( hProcess, ptr2 );
1171 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1172
1173 /* the address is rounded down if not on a page boundary */
1174 ptr2 = (char *)ptr + 0x1001;
1175 size = 0;
1176 offset.QuadPart = 0;
1177 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1178 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1179 ok( status == STATUS_SUCCESS, "NtMapViewOfSection returned %x\n", status );
1180 ok( (char *)ptr2 == (char *)ptr + 0x1000,
1181 "expected address %p, got %p\n", (char *)ptr + 0x1000, ptr2 );
1182 status = pNtUnmapViewOfSection( hProcess, ptr2 );
1183 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1184
1185 ptr2 = (char *)ptr + 0x2000;
1186 size = 0;
1187 offset.QuadPart = 0;
1188 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1189 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1190 ok( status == STATUS_SUCCESS, "NtMapViewOfSection returned %x\n", status );
1191 ok( (char *)ptr2 == (char *)ptr + 0x2000,
1192 "expected address %p, got %p\n", (char *)ptr + 0x2000, ptr2 );
1193 status = pNtUnmapViewOfSection( hProcess, ptr2 );
1194 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1195 }
1196 else
1197 {
1198 ptr2 = (char *)ptr + 0x1000;
1199 size = 0;
1200 offset.QuadPart = 0;
1201 status = pNtMapViewOfSection( mapping, hProcess, &ptr2, 0, 0, &offset,
1202 &size, 1, AT_ROUND_TO_PAGE, PAGE_READWRITE );
1203 todo_wine
1204 ok( status == STATUS_INVALID_PARAMETER_9, "NtMapViewOfSection returned %x\n", status );
1205 }
1206
1207 status = pNtUnmapViewOfSection( hProcess, ptr );
1208 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
1209
1210 CloseHandle( mapping );
1211 CloseHandle( file );
1212 DeleteFileA( testfile );
1213
1214 TerminateProcess(hProcess, 0);
1215 CloseHandle(hProcess);
1216 }
1217
1218 static void test_NtAreMappedFilesTheSame(void)
1219 {
1220 static const char testfile[] = "testfile.xxx";
1221 HANDLE file, file2, mapping, map2;
1222 void *ptr, *ptr2;
1223 NTSTATUS status;
1224 char path[MAX_PATH];
1225
1226 if (!pNtAreMappedFilesTheSame)
1227 {
1228 win_skip( "NtAreMappedFilesTheSame not available\n" );
1229 return;
1230 }
1231
1232 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1233 NULL, CREATE_ALWAYS, 0, 0 );
1234 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1235 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1236 SetEndOfFile( file );
1237
1238 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1239 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1240
1241 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1242 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1243
1244 file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1245 NULL, OPEN_EXISTING, 0, 0 );
1246 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1247
1248 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
1249 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1250 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1251 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1252 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1253 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1254 UnmapViewOfFile( ptr2 );
1255
1256 ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1257 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1258 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1259 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1260 UnmapViewOfFile( ptr2 );
1261 CloseHandle( map2 );
1262
1263 map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1264 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1265 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1266 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1267 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1268 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1269 UnmapViewOfFile( ptr2 );
1270 CloseHandle( map2 );
1271 CloseHandle( file2 );
1272
1273 status = pNtAreMappedFilesTheSame( ptr, ptr );
1274 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1275 "NtAreMappedFilesTheSame returned %x\n", status );
1276
1277 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
1278 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1279 "NtAreMappedFilesTheSame returned %x\n", status );
1280
1281 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1282 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1283
1284 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
1285 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
1286 "NtAreMappedFilesTheSame returned %x\n", status );
1287
1288 status = pNtAreMappedFilesTheSame( ptr, NULL );
1289 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1290
1291 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
1292 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1293
1294 status = pNtAreMappedFilesTheSame( NULL, NULL );
1295 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1296
1297 ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
1298 ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
1299 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1300 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1301 VirtualFree( ptr2, 0, MEM_RELEASE );
1302
1303 UnmapViewOfFile( ptr );
1304 CloseHandle( mapping );
1305 CloseHandle( file );
1306
1307 status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1308 GetModuleHandleA("kernel32.dll") );
1309 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1310 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1311 GetModuleHandleA("kernel32.dll") );
1312 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1313 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1314 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1315 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1316
1317 GetSystemDirectoryA( path, MAX_PATH );
1318 strcat( path, "\\kernel32.dll" );
1319 file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1320 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1321
1322 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1323 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1324 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1325 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1326 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1327 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1328 UnmapViewOfFile( ptr );
1329 CloseHandle( mapping );
1330
1331 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1332 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1333 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1334 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1335 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1336 todo_wine
1337 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1338
1339 file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1340 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1341 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1342 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1343 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
1344 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1345 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1346 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1347 UnmapViewOfFile( ptr2 );
1348 CloseHandle( map2 );
1349 CloseHandle( file2 );
1350
1351 UnmapViewOfFile( ptr );
1352 CloseHandle( mapping );
1353
1354 CloseHandle( file );
1355 DeleteFileA( testfile );
1356 }
1357
1358 static void test_CreateFileMapping(void)
1359 {
1360 HANDLE handle, handle2;
1361
1362 /* test case sensitivity */
1363
1364 SetLastError(0xdeadbeef);
1365 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1366 "Wine Test Mapping");
1367 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1368 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1369
1370 SetLastError(0xdeadbeef);
1371 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1372 "Wine Test Mapping");
1373 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1374 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
1375 CloseHandle( handle2 );
1376
1377 SetLastError(0xdeadbeef);
1378 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1379 "WINE TEST MAPPING");
1380 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1381 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1382 CloseHandle( handle2 );
1383
1384 SetLastError(0xdeadbeef);
1385 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1386 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1387 CloseHandle( handle2 );
1388
1389 SetLastError(0xdeadbeef);
1390 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1391 ok( !handle2, "OpenFileMapping succeeded\n");
1392 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1393 "wrong error %u\n", GetLastError());
1394
1395 CloseHandle( handle );
1396 }
1397
1398 static void test_IsBadReadPtr(void)
1399 {
1400 BOOL ret;
1401 void *ptr = (void *)0xdeadbeef;
1402 char stackvar;
1403
1404 ret = IsBadReadPtr(NULL, 0);
1405 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1406
1407 ret = IsBadReadPtr(NULL, 1);
1408 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1409
1410 ret = IsBadReadPtr(ptr, 0);
1411 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1412
1413 ret = IsBadReadPtr(ptr, 1);
1414 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1415
1416 ret = IsBadReadPtr(&stackvar, 0);
1417 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1418
1419 ret = IsBadReadPtr(&stackvar, sizeof(char));
1420 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1421 }
1422
1423 static void test_IsBadWritePtr(void)
1424 {
1425 BOOL ret;
1426 void *ptr = (void *)0xdeadbeef;
1427 char stackval;
1428
1429 ret = IsBadWritePtr(NULL, 0);
1430 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1431
1432 ret = IsBadWritePtr(NULL, 1);
1433 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1434
1435 ret = IsBadWritePtr(ptr, 0);
1436 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1437
1438 ret = IsBadWritePtr(ptr, 1);
1439 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1440
1441 ret = IsBadWritePtr(&stackval, 0);
1442 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1443
1444 ret = IsBadWritePtr(&stackval, sizeof(char));
1445 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1446 }
1447
1448 static void test_IsBadCodePtr(void)
1449 {
1450 BOOL ret;
1451 void *ptr = (void *)0xdeadbeef;
1452 char stackval;
1453
1454 ret = IsBadCodePtr(NULL);
1455 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1456
1457 ret = IsBadCodePtr(ptr);
1458 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1459
1460 ret = IsBadCodePtr((void *)&stackval);
1461 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1462 }
1463
1464 static void test_write_watch(void)
1465 {
1466 static const char pipename[] = "\\\\.\\pipe\\test_write_watch_pipe";
1467 static const char testdata[] = "Hello World";
1468 DWORD ret, size, old_prot, num_bytes;
1469 MEMORY_BASIC_INFORMATION info;
1470 HANDLE readpipe, writepipe;
1471 OVERLAPPED overlapped;
1472 void *results[64];
1473 ULONG_PTR count;
1474 ULONG pagesize;
1475 BOOL success;
1476 char *base;
1477
1478 if (!pGetWriteWatch || !pResetWriteWatch)
1479 {
1480 win_skip( "GetWriteWatch not supported\n" );
1481 return;
1482 }
1483
1484 size = 0x10000;
1485 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1486 if (!base &&
1487 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1488 {
1489 win_skip( "MEM_WRITE_WATCH not supported\n" );
1490 return;
1491 }
1492 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1493 ret = VirtualQuery( base, &info, sizeof(info) );
1494 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1495 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1496 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1497 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1498 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1499 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1500 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1501
1502 count = 64;
1503 SetLastError( 0xdeadbeef );
1504 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1505 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1506 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1507 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1508 "wrong error %u\n", GetLastError() );
1509
1510 SetLastError( 0xdeadbeef );
1511 ret = pGetWriteWatch( 0, GetModuleHandleW(NULL), size, results, &count, &pagesize );
1512 if (ret)
1513 {
1514 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1515 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1516 }
1517 else /* win98 */
1518 {
1519 ok( count == 0, "wrong count %lu\n", count );
1520 }
1521
1522 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1523 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1524 ok( count == 0, "wrong count %lu\n", count );
1525
1526 base[pagesize + 1] = 0x44;
1527
1528 count = 64;
1529 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1530 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1531 ok( count == 1, "wrong count %lu\n", count );
1532 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1533
1534 count = 64;
1535 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1536 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1537 ok( count == 1, "wrong count %lu\n", count );
1538 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1539
1540 count = 64;
1541 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1542 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1543 ok( count == 0, "wrong count %lu\n", count );
1544
1545 base[2*pagesize + 3] = 0x11;
1546 base[4*pagesize + 8] = 0x11;
1547
1548 count = 64;
1549 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1550 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1551 ok( count == 2, "wrong count %lu\n", count );
1552 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1553 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1554
1555 count = 64;
1556 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1557 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1558 ok( count == 1, "wrong count %lu\n", count );
1559 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1560
1561 ret = pResetWriteWatch( base, 3*pagesize );
1562 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1563
1564 count = 64;
1565 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1566 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1567 ok( count == 1, "wrong count %lu\n", count );
1568 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1569
1570 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1571
1572 count = 64;
1573 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1574 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1575 ok( count == 3, "wrong count %lu\n", count );
1576 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1577 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1578 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1579
1580 count = 1;
1581 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1582 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1583 ok( count == 1, "wrong count %lu\n", count );
1584 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1585
1586 count = 64;
1587 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1588 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1589 ok( count == 2, "wrong count %lu\n", count );
1590 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1591 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1592
1593 /* changing protections doesn't affect watches */
1594
1595 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1596 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1597 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1598
1599 ret = VirtualQuery( base, &info, sizeof(info) );
1600 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1601 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1602 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1603 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1604 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1605
1606 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1607 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1608 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1609
1610 count = 64;
1611 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1612 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1613 ok( count == 2, "wrong count %lu\n", count );
1614 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1615 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1616
1617 ret = VirtualQuery( base, &info, sizeof(info) );
1618 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1619 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1620 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1621 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1622 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1623
1624 /* ReadFile should trigger write watches */
1625
1626 memset( &overlapped, 0, sizeof(overlapped) );
1627 overlapped.hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
1628
1629 readpipe = CreateNamedPipeA( pipename, FILE_FLAG_OVERLAPPED | PIPE_ACCESS_INBOUND,
1630 PIPE_TYPE_BYTE | PIPE_WAIT, 1, 1024, 1024,
1631 NMPWAIT_USE_DEFAULT_WAIT, NULL );
1632 ok( readpipe != INVALID_HANDLE_VALUE, "CreateNamedPipeA failed %u\n", GetLastError() );
1633
1634 success = ConnectNamedPipe( readpipe, &overlapped );
1635 ok( !success, "ConnectNamedPipe unexpectedly succeeded\n" );
1636 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
1637
1638 writepipe = CreateFileA( pipename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
1639 ok( writepipe != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError() );
1640
1641 ret = WaitForSingleObject( overlapped.hEvent, 1000 );
1642 ok( ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret );
1643
1644 memset( base, 0, size );
1645
1646 count = 64;
1647 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1648 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1649 ok( count == 16, "wrong count %lu\n", count );
1650
1651 success = ReadFile( readpipe, base, size, NULL, &overlapped );
1652 ok( !success, "ReadFile unexpectedly succeeded\n" );
1653 ok( GetLastError() == ERROR_IO_PENDING, "expected ERROR_IO_PENDING, got %u\n", GetLastError() );
1654
1655 count = 64;
1656 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1657 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1658 ok( count == 16, "wrong count %lu\n", count );
1659
1660 num_bytes = 0;
1661 success = WriteFile( writepipe, testdata, sizeof(testdata), &num_bytes, NULL );
1662 ok( success, "WriteFile failed %u\n", GetLastError() );
1663 ok( num_bytes == sizeof(testdata), "wrong number of bytes written\n" );
1664
1665 num_bytes = 0;
1666 success = GetOverlappedResult( readpipe, &overlapped, &num_bytes, TRUE );
1667 ok( success, "GetOverlappedResult failed %u\n", GetLastError() );
1668 ok( num_bytes == sizeof(testdata), "wrong number of bytes read\n" );
1669 ok( !memcmp( base, testdata, sizeof(testdata)), "didn't receive expected data\n" );
1670
1671 count = 64;
1672 memset( results, 0, sizeof(results) );
1673 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1674 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1675 todo_wine ok( count == 1, "wrong count %lu\n", count );
1676 ok( results[0] == base, "wrong result %p\n", results[0] );
1677
1678 CloseHandle( readpipe );
1679 CloseHandle( writepipe );
1680 CloseHandle( overlapped.hEvent );
1681
1682 /* some invalid parameter tests */
1683
1684 SetLastError( 0xdeadbeef );
1685 count = 0;
1686 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1687 if (ret)
1688 {
1689 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1690 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1691
1692 SetLastError( 0xdeadbeef );
1693 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1694 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1695 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1696
1697 SetLastError( 0xdeadbeef );
1698 count = 64;
1699 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1700 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1701 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1702
1703 SetLastError( 0xdeadbeef );
1704 count = 64;
1705 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1706 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1707 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1708
1709 SetLastError( 0xdeadbeef );
1710 count = 0;
1711 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1712 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1713 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1714
1715 SetLastError( 0xdeadbeef );
1716 count = 64;
1717 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1718 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1719 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1720
1721 SetLastError( 0xdeadbeef );
1722 count = 64;
1723 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1724 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1725 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1726
1727 SetLastError( 0xdeadbeef );
1728 count = 64;
1729 ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1730 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1731 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1732
1733 SetLastError( 0xdeadbeef );
1734 count = 64;
1735 ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1736 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1737 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1738
1739 SetLastError( 0xdeadbeef );
1740 ret = pResetWriteWatch( base, 0 );
1741 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1742 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1743
1744 SetLastError( 0xdeadbeef );
1745 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1746 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1747 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1748 }
1749 else /* win98 is completely different */
1750 {
1751 SetLastError( 0xdeadbeef );
1752 count = 64;
1753 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1754 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1755 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1756
1757 count = 0;
1758 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1759 ok( !ret, "GetWriteWatch failed %u\n", ret );
1760
1761 count = 64;
1762 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1763 ok( !ret, "GetWriteWatch failed %u\n", ret );
1764
1765 count = 64;
1766 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1767 ok( !ret, "GetWriteWatch failed %u\n", ret );
1768
1769 ret = pResetWriteWatch( base, 0 );
1770 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1771
1772 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1773 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1774 }
1775
1776 VirtualFree( base, 0, MEM_RELEASE );
1777
1778 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1779 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1780 VirtualFree( base, 0, MEM_RELEASE );
1781
1782 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1783 ok( !base, "VirtualAlloc succeeded\n" );
1784 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1785
1786 /* initial protect doesn't matter */
1787
1788 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1789 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1790 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1791 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1792
1793 count = 64;
1794 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1795 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1796 ok( count == 0, "wrong count %lu\n", count );
1797
1798 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1799 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1800 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1801
1802 base[5*pagesize + 200] = 3;
1803
1804 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1805 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1806 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1807
1808 count = 64;
1809 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1810 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1811 ok( count == 1, "wrong count %lu\n", count );
1812 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1813
1814 ret = VirtualFree( base, size, MEM_DECOMMIT );
1815 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1816
1817 count = 64;
1818 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1819 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1820 ok( count == 1 || broken(count == 0), /* win98 */
1821 "wrong count %lu\n", count );
1822 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1823
1824 VirtualFree( base, 0, MEM_RELEASE );
1825 }
1826
1827 #ifdef __i386__
1828
1829 static DWORD num_guard_page_calls;
1830
1831 static DWORD guard_page_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1832 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1833 {
1834 trace( "exception: %08x flags:%x addr:%p\n",
1835 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
1836
1837 ok( rec->NumberParameters == 2, "NumberParameters is %d instead of 2\n", rec->NumberParameters );
1838 ok( rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION, "ExceptionCode is %08x instead of %08x\n",
1839 rec->ExceptionCode, STATUS_GUARD_PAGE_VIOLATION );
1840
1841 num_guard_page_calls++;
1842 *(int *)rec->ExceptionInformation[1] += 0x100;
1843
1844 return ExceptionContinueExecution;
1845 }
1846
1847 static void test_guard_page(void)
1848 {
1849 EXCEPTION_REGISTRATION_RECORD frame;
1850 MEMORY_BASIC_INFORMATION info;
1851 DWORD ret, size, old_prot;
1852 int *value, old_value;
1853 void *results[64];
1854 ULONG_PTR count;
1855 ULONG pagesize;
1856 BOOL success;
1857 char *base;
1858
1859 if (!pNtCurrentTeb)
1860 {
1861 win_skip( "NtCurrentTeb not supported\n" );
1862 return;
1863 }
1864
1865 size = 0x1000;
1866 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
1867 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1868 value = (int *)base;
1869
1870 /* verify info structure */
1871 ret = VirtualQuery( base, &info, sizeof(info) );
1872 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1873 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1874 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1875 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1876 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1877 ok( info.Protect == (PAGE_READWRITE | PAGE_GUARD), "wrong Protect 0x%x\n", info.Protect );
1878 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1879
1880 /* put some initial value into the memory */
1881 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
1882 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1883 ok( old_prot == (PAGE_READWRITE | PAGE_GUARD), "wrong old prot %x\n", old_prot );
1884
1885 *value = 1;
1886 *(value + 1) = 2;
1887
1888 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1889 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1890 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1891
1892 /* test behaviour of VirtualLock - first attempt should fail */
1893 SetLastError( 0xdeadbeef );
1894 success = VirtualLock( base, size );
1895 ok( !success, "VirtualLock unexpectedly succeeded\n" );
1896 todo_wine
1897 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION, "wrong error %u\n", GetLastError() );
1898
1899 success = VirtualLock( base, size );
1900 todo_wine
1901 ok( success, "VirtualLock failed %u\n", GetLastError() );
1902 if (success)
1903 {
1904 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
1905 success = VirtualUnlock( base, size );
1906 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
1907 }
1908
1909 /* check info structure again, PAGE_GUARD should be removed now */
1910 ret = VirtualQuery( base, &info, sizeof(info) );
1911 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1912 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1913 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1914 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1915 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1916 todo_wine
1917 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1918 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1919
1920 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1921 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1922 todo_wine
1923 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1924
1925 /* test directly accessing the memory - we need to setup an exception handler first */
1926 frame.Handler = guard_page_handler;
1927 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1928 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1929
1930 num_guard_page_calls = 0;
1931 old_value = *value; /* exception handler increments value by 0x100 */
1932 *value = 2;
1933 ok( old_value == 0x101, "memory block contains wrong value, expected 0x101, got 0x%x\n", old_value );
1934 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1935
1936 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1937
1938 /* check info structure again, PAGE_GUARD should be removed now */
1939 ret = VirtualQuery( base, &info, sizeof(info) );
1940 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1941 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1942
1943 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1944 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1945 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1946
1947 /* test accessing second integer in memory */
1948 frame.Handler = guard_page_handler;
1949 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1950 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1951
1952 num_guard_page_calls = 0;
1953 old_value = *(value + 1);
1954 ok( old_value == 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value );
1955 ok( *value == 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value );
1956 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1957
1958 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1959
1960 success = VirtualLock( base, size );
1961 ok( success, "VirtualLock failed %u\n", GetLastError() );
1962 if (success)
1963 {
1964 ok( *value == 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value );
1965 success = VirtualUnlock( base, size );
1966 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
1967 }
1968
1969 VirtualFree( base, 0, MEM_RELEASE );
1970
1971 /* combined guard page / write watch tests */
1972 if (!pGetWriteWatch || !pResetWriteWatch)
1973 {
1974 win_skip( "GetWriteWatch not supported, skipping combined guard page / write watch tests\n" );
1975 return;
1976 }
1977
1978 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE | PAGE_GUARD );
1979 if (!base && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1980 {
1981 win_skip( "MEM_WRITE_WATCH not supported\n" );
1982 return;
1983 }
1984 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1985 value = (int *)base;
1986
1987 ret = VirtualQuery( base, &info, sizeof(info) );
1988 ok( ret, "VirtualQuery failed %u\n", GetLastError() );
1989 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1990 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1991 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1992 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1993 ok( info.Protect == (PAGE_READWRITE | PAGE_GUARD), "wrong Protect 0x%x\n", info.Protect );
1994 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1995
1996 count = 64;
1997 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1998 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1999 ok( count == 0, "wrong count %lu\n", count );
2000
2001 /* writing to a page should trigger should trigger guard page, even if write watch is set */
2002 frame.Handler = guard_page_handler;
2003 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
2004 pNtCurrentTeb()->Tib.ExceptionList = &frame;
2005
2006 num_guard_page_calls = 0;
2007 *value = 1;
2008 *(value + 1) = 2;
2009 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
2010
2011 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
2012
2013 count = 64;
2014 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2015 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2016 ok( count == 1, "wrong count %lu\n", count );
2017 ok( results[0] == base, "wrong result %p\n", results[0] );
2018
2019 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2020 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2021
2022 /* write watch is triggered from inside of the guard page handler */
2023 frame.Handler = guard_page_handler;
2024 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
2025 pNtCurrentTeb()->Tib.ExceptionList = &frame;
2026
2027 num_guard_page_calls = 0;
2028 old_value = *(value + 1); /* doesn't trigger write watch */
2029 ok( old_value == 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value );
2030 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
2031 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
2032
2033 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
2034
2035 count = 64;
2036 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2037 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2038 ok( count == 1, "wrong count %lu\n", count );
2039 ok( results[0] == base, "wrong result %p\n", results[0] );
2040
2041 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2042 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2043
2044 /* test behaviour of VirtualLock - first attempt should fail without triggering write watches */
2045 SetLastError( 0xdeadbeef );
2046 success = VirtualLock( base, size );
2047 ok( !success, "VirtualLock unexpectedly succeeded\n" );
2048 todo_wine
2049 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION, "wrong error %u\n", GetLastError() );
2050
2051 count = 64;
2052 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
2053 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2054 ok( count == 0, "wrong count %lu\n", count );
2055
2056 success = VirtualLock( base, size );
2057 todo_wine
2058 ok( success, "VirtualLock failed %u\n", GetLastError() );
2059 if (success)
2060 {
2061 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
2062 success = VirtualUnlock( base, size );
2063 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
2064 }
2065
2066 count = 64;
2067 results[0] = (void *)0xdeadbeef;
2068 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2069 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2070 todo_wine
2071 ok( count == 1 || broken(count == 0) /* Windows 8 */, "wrong count %lu\n", count );
2072 todo_wine
2073 ok( results[0] == base || broken(results[0] == (void *)0xdeadbeef) /* Windows 8 */, "wrong result %p\n", results[0] );
2074
2075 VirtualFree( base, 0, MEM_RELEASE );
2076 }
2077
2078 static DWORD WINAPI stack_commit_func( void *arg )
2079 {
2080 volatile char *p = (char *)&p;
2081
2082 /* trigger all guard pages, to ensure that the pages are committed */
2083 while (p >= (char *)pNtCurrentTeb()->DeallocationStack + 3 * 0x1000)
2084 {
2085 p[0] |= 0;
2086 p -= 0x1000;
2087 }
2088
2089 ok( arg == (void *)0xdeadbeef, "expected 0xdeadbeef, got %p\n", arg );
2090 return 42;
2091 }
2092
2093 static void test_stack_commit(void)
2094 {
2095 static const char code_call_on_stack[] = {
2096 0x55, /* pushl %ebp */
2097 0x56, /* pushl %esi */
2098 0x89, 0xe6, /* movl %esp,%esi */
2099 0x8b, 0x4c, 0x24, 0x0c, /* movl 12(%esp),%ecx - func */
2100 0x8b, 0x54, 0x24, 0x10, /* movl 16(%esp),%edx - arg */
2101 0x8b, 0x44, 0x24, 0x14, /* movl 20(%esp),%eax - stack */
2102 0x83, 0xe0, 0xf0, /* andl $~15,%eax */
2103 0x83, 0xe8, 0x0c, /* subl $12,%eax */
2104 0x89, 0xc4, /* movl %eax,%esp */
2105 0x52, /* pushl %edx */
2106 0x31, 0xed, /* xorl %ebp,%ebp */
2107 0xff, 0xd1, /* call *%ecx */
2108 0x89, 0xf4, /* movl %esi,%esp */
2109 0x5e, /* popl %esi */
2110 0x5d, /* popl %ebp */
2111 0xc2, 0x0c, 0x00 }; /* ret $12 */
2112
2113 DWORD (WINAPI *call_on_stack)( DWORD (WINAPI *func)(void *), void *arg, void *stack );
2114 void *old_stack, *old_stack_base, *old_stack_limit;
2115 void *new_stack, *new_stack_base;
2116 DWORD result;
2117
2118 if (!pNtCurrentTeb)
2119 {
2120 win_skip( "NtCurrentTeb not supported\n" );
2121 return;
2122 }
2123
2124 call_on_stack = VirtualAlloc( 0, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE );
2125 ok( call_on_stack != NULL, "VirtualAlloc failed %u\n", GetLastError() );
2126 memcpy( call_on_stack, code_call_on_stack, sizeof(code_call_on_stack) );
2127
2128 /* allocate a new stack, only the first guard page is committed */
2129 new_stack = VirtualAlloc( 0, 0x400000, MEM_RESERVE, PAGE_READWRITE );
2130 ok( new_stack != NULL, "VirtualAlloc failed %u\n", GetLastError() );
2131 new_stack_base = (char *)new_stack + 0x400000;
2132 VirtualAlloc( (char *)new_stack_base - 0x1000, 0x1000, MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
2133
2134 old_stack = pNtCurrentTeb()->DeallocationStack;
2135 old_stack_base = pNtCurrentTeb()->Tib.StackBase;
2136 old_stack_limit = pNtCurrentTeb()->Tib.StackLimit;
2137
2138 pNtCurrentTeb()->DeallocationStack = new_stack;
2139 pNtCurrentTeb()->Tib.StackBase = new_stack_base;
2140 pNtCurrentTeb()->Tib.StackLimit = new_stack_base;
2141
2142 result = call_on_stack( stack_commit_func, (void *)0xdeadbeef, new_stack_base );
2143 ok( result == 42, "expected 42, got %u\n", result );
2144
2145 pNtCurrentTeb()->DeallocationStack = old_stack;
2146 pNtCurrentTeb()->Tib.StackBase = old_stack_base;
2147 pNtCurrentTeb()->Tib.StackLimit = old_stack_limit;
2148
2149 VirtualFree( new_stack, 0, MEM_RELEASE );
2150 VirtualFree( call_on_stack, 0, MEM_RELEASE );
2151 }
2152
2153 DWORD num_execute_fault_calls;
2154
2155 static DWORD execute_fault_seh_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
2156 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
2157 {
2158 ULONG flags = MEM_EXECUTE_OPTION_ENABLE;
2159 DWORD err;
2160
2161 trace( "exception: %08x flags:%x addr:%p info[0]:%ld info[1]:%p\n",
2162 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2163 rec->ExceptionInformation[0], (void *)rec->ExceptionInformation[1] );
2164
2165 ok( rec->NumberParameters == 2, "NumberParameters is %d instead of 2\n", rec->NumberParameters );
2166 ok( rec->ExceptionCode == STATUS_ACCESS_VIOLATION || rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION,
2167 "ExceptionCode is %08x instead of STATUS_ACCESS_VIOLATION or STATUS_GUARD_PAGE_VIOLATION\n", rec->ExceptionCode );
2168
2169 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &flags, sizeof(flags), NULL );
2170
2171 if (rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION)
2172 {
2173
2174 err = IsProcessorFeaturePresent( PF_NX_ENABLED ) ? EXCEPTION_EXECUTE_FAULT : EXCEPTION_READ_FAULT;
2175 ok( rec->ExceptionInformation[0] == err, "ExceptionInformation[0] is %d instead of %d\n",
2176 (DWORD)rec->ExceptionInformation[0], err );
2177
2178 num_guard_page_calls++;
2179 }
2180 else if (rec->ExceptionCode == STATUS_ACCESS_VIOLATION)
2181 {
2182 DWORD old_prot;
2183 BOOL success;
2184
2185 err = (flags & MEM_EXECUTE_OPTION_DISABLE) ? EXCEPTION_EXECUTE_FAULT : EXCEPTION_READ_FAULT;
2186 ok( rec->ExceptionInformation[0] == err, "ExceptionInformation[0] is %d instead of %d\n",
2187 (DWORD)rec->ExceptionInformation[0], err );
2188
2189 success = VirtualProtect( (void *)rec->ExceptionInformation[1], 16, PAGE_EXECUTE_READWRITE, &old_prot );
2190 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2191 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
2192
2193 num_execute_fault_calls++;
2194 }
2195
2196 return ExceptionContinueExecution;
2197 }
2198
2199 static LONG CALLBACK execute_fault_vec_handler( EXCEPTION_POINTERS *ExceptionInfo )
2200 {
2201 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2202 DWORD old_prot;
2203 BOOL success;
2204
2205 trace( "exception: %08x flags:%x addr:%p info[0]:%ld info[1]:%p\n",
2206 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
2207 rec->ExceptionInformation[0], (void *)rec->ExceptionInformation[1] );
2208
2209 ok( rec->NumberParameters == 2, "NumberParameters is %d instead of 2\n", rec->NumberParameters );
2210 ok( rec->ExceptionCode == STATUS_ACCESS_VIOLATION,
2211 "ExceptionCode is %08x instead of STATUS_ACCESS_VIOLATION\n", rec->ExceptionCode );
2212
2213 if (rec->ExceptionCode == STATUS_ACCESS_VIOLATION)
2214 num_execute_fault_calls++;
2215
2216 if (rec->ExceptionInformation[0] == EXCEPTION_READ_FAULT)
2217 return EXCEPTION_CONTINUE_SEARCH;
2218
2219 success = VirtualProtect( (void *)rec->ExceptionInformation[1], 16, PAGE_EXECUTE_READWRITE, &old_prot );
2220 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2221 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
2222
2223 return EXCEPTION_CONTINUE_EXECUTION;
2224 }
2225
2226 static inline DWORD send_message_excpt( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
2227 {
2228 EXCEPTION_REGISTRATION_RECORD frame;
2229 DWORD ret;
2230
2231 frame.Handler = execute_fault_seh_handler;
2232 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
2233 pNtCurrentTeb()->Tib.ExceptionList = &frame;
2234
2235 num_guard_page_calls = num_execute_fault_calls = 0;
2236 ret = SendMessageA( hWnd, uMsg, wParam, lParam );
2237
2238 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
2239
2240 return ret;
2241 }
2242
2243 static inline DWORD call_proc_excpt( DWORD (CALLBACK *code)(void *), void *arg )
2244 {
2245 EXCEPTION_REGISTRATION_RECORD frame;
2246 DWORD ret;
2247
2248 frame.Handler = execute_fault_seh_handler;
2249 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
2250 pNtCurrentTeb()->Tib.ExceptionList = &frame;
2251
2252 num_guard_page_calls = num_execute_fault_calls = 0;
2253 ret = code( arg );
2254
2255 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
2256
2257 return ret;
2258 }
2259
2260 static LRESULT CALLBACK jmp_test_func( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
2261 {
2262 if (uMsg == WM_USER)
2263 return 42;
2264
2265 return DefWindowProcA( hWnd, uMsg, wParam, lParam );
2266 }
2267
2268 static LRESULT CALLBACK atl_test_func( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
2269 {
2270 DWORD arg = (DWORD)hWnd;
2271 if (uMsg == WM_USER)
2272 ok( arg == 0x11223344, "arg is 0x%08x instead of 0x11223344\n", arg );
2273 else
2274 ok( arg != 0x11223344, "arg is unexpectedly 0x11223344\n" );
2275 return 43;
2276 }
2277
2278 static DWORD CALLBACK atl5_test_func( void )
2279 {
2280 return 44;
2281 }
2282
2283 static void test_atl_thunk_emulation( ULONG dep_flags )
2284 {
2285 static const char code_jmp[] = {0xE9, 0x00, 0x00, 0x00, 0x00};
2286 static const char code_atl1[] = {0xC7, 0x44, 0x24, 0x04, 0x44, 0x33, 0x22, 0x11, 0xE9, 0x00, 0x00, 0x00, 0x00};
2287 static const char code_atl2[] = {0xB9, 0x44, 0x33, 0x22, 0x11, 0xE9, 0x00, 0x00, 0x00, 0x00};
2288 static const char code_atl3[] = {0xBA, 0x44, 0x33, 0x22, 0x11, 0xB9, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE1};
2289 static const char code_atl4[] = {0xB9, 0x44, 0x33, 0x22, 0x11, 0xB8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0};
2290 static const char code_atl5[] = {0x59, 0x58, 0x51, 0xFF, 0x60, 0x04};
2291 static const char cls_name[] = "atl_thunk_class";
2292 DWORD ret, size, old_prot;
2293 ULONG old_flags = MEM_EXECUTE_OPTION_ENABLE;
2294 BOOL success, restore_flags = FALSE;
2295 void *results[64];
2296 ULONG_PTR count;
2297 ULONG pagesize;
2298 WNDCLASSEXA wc;
2299 char *base;
2300 HWND hWnd;
2301
2302 if (!pNtCurrentTeb)
2303 {
2304 win_skip( "NtCurrentTeb not supported\n" );
2305 return;
2306 }
2307
2308 trace( "Running DEP tests with ProcessExecuteFlags = %d\n", dep_flags );
2309
2310 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &old_flags, sizeof(old_flags), NULL );
2311 if (old_flags != dep_flags)
2312 {
2313 ret = NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &dep_flags, sizeof(dep_flags) );
2314 if (ret == STATUS_INVALID_INFO_CLASS) /* Windows 2000 */
2315 {
2316 win_skip( "Skipping DEP tests with ProcessExecuteFlags = %d\n", dep_flags );
2317 return;
2318 }
2319 ok( !ret, "NtSetInformationProcess failed with status %08x\n", ret );
2320 restore_flags = TRUE;
2321 }
2322
2323 size = 0x1000;
2324 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
2325 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
2326
2327 /* Check result of GetProcessDEPPolicy */
2328 if (!pGetProcessDEPPolicy)
2329 win_skip( "GetProcessDEPPolicy not supported\n" );
2330 else
2331 {
2332 BOOL (WINAPI *get_dep_policy)(HANDLE, LPDWORD, PBOOL) = (void *)base;
2333 BOOL policy_permanent = 0xdeadbeef;
2334 DWORD policy_flags = 0xdeadbeef;
2335
2336 /* GetProcessDEPPolicy crashes on Windows when a NULL pointer is passed.
2337 * Moreover this function has a bug on Windows 8, which has the effect that
2338 * policy_permanent is set to the content of the CL register instead of 0,
2339 * when the policy is not permanent. To detect that we use an assembler
2340 * wrapper to call the function. */
2341
2342 memcpy( base, code_atl2, sizeof(code_atl2) );
2343 *(DWORD *)(base + 6) = (DWORD_PTR)pGetProcessDEPPolicy - (DWORD_PTR)(base + 10);
2344
2345 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2346 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2347
2348 success = get_dep_policy( GetCurrentProcess(), &policy_flags, &policy_permanent );
2349 ok( success, "GetProcessDEPPolicy failed %u\n", GetLastError() );
2350
2351 ret = 0;
2352 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2353 ret |= PROCESS_DEP_ENABLE;
2354 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION)
2355 ret |= PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION;
2356
2357 ok( policy_flags == ret, "expected policy flags %d, got %d\n", ret, policy_flags );
2358 ok( !policy_permanent || broken(policy_permanent == 0x44),
2359 "expected policy permanent FALSE, got %d\n", policy_permanent );
2360 }
2361
2362 memcpy( base, code_jmp, sizeof(code_jmp) );
2363 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2364
2365 /* On Windows, the ATL Thunk emulation is only enabled while running WndProc functions,
2366 * whereas in Wine such a limitation doesn't exist yet. We want to test in a scenario
2367 * where it is active, so that application which depend on that still work properly.
2368 * We have no exception handler enabled yet, so give proper EXECUTE permissions to
2369 * prevent crashes while creating the window. */
2370
2371 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2372 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2373
2374 memset( &wc, 0, sizeof(wc) );
2375 wc.cbSize = sizeof(wc);
2376 wc.style = CS_VREDRAW | CS_HREDRAW;
2377 wc.hInstance = GetModuleHandleA( 0 );
2378 wc.hCursor = LoadCursorA( NULL, (LPCSTR)IDC_ARROW );
2379 wc.hbrBackground = NULL;
2380 wc.lpszClassName = cls_name;
2381 wc.lpfnWndProc = (WNDPROC)base;
2382 success = RegisterClassExA(&wc) != 0;
2383 ok( success, "RegisterClassExA failed %u\n", GetLastError() );
2384
2385 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2386 ok( hWnd != 0, "CreateWindowExA failed %u\n", GetLastError() );
2387
2388 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2389 ok( ret == 42, "SendMessage returned unexpected result %d\n", ret );
2390
2391 /* At first try with an instruction which is not recognized as proper ATL thunk
2392 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
2393 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
2394
2395 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2396 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2397
2398 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2399 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2400 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2401 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && !IsProcessorFeaturePresent( PF_NX_ENABLED ))
2402 {
2403 trace( "DEP hardware support is not available\n" );
2404 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2405 dep_flags = MEM_EXECUTE_OPTION_ENABLE;
2406 }
2407 else if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2408 {
2409 trace( "DEP hardware support is available\n" );
2410 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2411 }
2412 else
2413 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2414
2415 /* Now a bit more complicated, the page containing the code is protected with
2416 * PAGE_GUARD memory protection. */
2417
2418 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2419 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2420
2421 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2422 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2423 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2424 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2425 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2426 else
2427 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2428
2429 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2430 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2431 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2432 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2433
2434 /* Now test with a proper ATL thunk instruction. */
2435
2436 memcpy( base, code_atl1, sizeof(code_atl1) );
2437 *(DWORD *)(base + 9) = (DWORD_PTR)atl_test_func - (DWORD_PTR)(base + 13);
2438
2439 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2440 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2441
2442 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2443 ok( ret == 43, "SendMessage returned unexpected result %d\n", ret );
2444
2445 /* Try executing with PAGE_READWRITE protection. */
2446
2447 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2448 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2449
2450 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2451 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2452 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2453 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2454 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2455 else
2456 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2457
2458 /* Now a bit more complicated, the page containing the code is protected with
2459 * PAGE_GUARD memory protection. */
2460
2461 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2462 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2463
2464 /* the same, but with PAGE_GUARD set */
2465 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2466 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2467 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2468 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2469 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2470 else
2471 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2472
2473 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2474 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2475 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2476 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2477
2478 /* The following test shows that on Windows, even a vectored exception handler
2479 * cannot intercept internal exceptions thrown by the ATL thunk emulation layer. */
2480
2481 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && !(dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2482 {
2483 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
2484 {
2485 PVOID vectored_handler;
2486
2487 success = VirtualProtect( base, size, PAGE_NOACCESS, &old_prot );
2488 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2489
2490 vectored_handler = pRtlAddVectoredExceptionHandler( TRUE, &execute_fault_vec_handler );
2491 ok( vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n" );
2492
2493 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2494
2495 pRtlRemoveVectoredExceptionHandler( vectored_handler );
2496
2497 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2498 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2499 }
2500 else
2501 win_skip( "RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n" );
2502 }
2503
2504 /* Test alternative ATL thunk instructions. */
2505
2506 memcpy( base, code_atl2, sizeof(code_atl2) );
2507 *(DWORD *)(base + 6) = (DWORD_PTR)atl_test_func - (DWORD_PTR)(base + 10);
2508
2509 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2510 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2511
2512 ret = send_message_excpt( hWnd, WM_USER + 1, 0, 0 );
2513 /* FIXME: we don't check the content of the register ECX yet */
2514 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2515 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2516 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2517 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2518 else
2519 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2520
2521 memcpy( base, code_atl3, sizeof(code_atl3) );
2522 *(DWORD *)(base + 6) = (DWORD_PTR)atl_test_func;
2523
2524 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2525 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2526
2527 ret = send_message_excpt( hWnd, WM_USER + 1, 0, 0 );
2528 /* FIXME: we don't check the content of the registers ECX/EDX yet */
2529 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2530 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2531 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2532 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2533 else
2534 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2535
2536 memcpy( base, code_atl4, sizeof(code_atl4) );
2537 *(DWORD *)(base + 6) = (DWORD_PTR)atl_test_func;
2538
2539 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2540 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2541
2542 ret = send_message_excpt( hWnd, WM_USER + 1, 0, 0 );
2543 /* FIXME: We don't check the content of the registers EAX/ECX yet */
2544 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2545 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2546 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2547 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2548 else if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2549 ok( num_execute_fault_calls == 0 || broken(num_execute_fault_calls == 1) /* Windows XP */,
2550 "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2551 else
2552 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2553
2554 memcpy( base, code_atl5, sizeof(code_atl5) );
2555
2556 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2557 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2558
2559 ret = (DWORD_PTR)atl5_test_func;
2560 ret = call_proc_excpt( (void *)base, &ret - 1 );
2561 /* FIXME: We don't check the content of the registers EAX/ECX yet */
2562 ok( ret == 44, "call returned wrong result, expected 44, got %d\n", ret );
2563 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2564 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2565 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2566 else if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2567 ok( num_execute_fault_calls == 0 || broken(num_execute_fault_calls == 1) /* Windows XP */,
2568 "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2569 else
2570 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2571
2572 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2573
2574 memcpy( base, code_jmp, sizeof(code_jmp) );
2575 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2576
2577 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2578 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2579
2580 DestroyWindow( hWnd );
2581
2582 success = UnregisterClassA( cls_name, GetModuleHandleA(0) );
2583 ok( success, "UnregisterClass failed %u\n", GetLastError() );
2584
2585 VirtualFree( base, 0, MEM_RELEASE );
2586
2587 /* Repeat the tests from above with MEM_WRITE_WATCH protected memory. */
2588
2589 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
2590 if (!base && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
2591 {
2592 win_skip( "MEM_WRITE_WATCH not supported\n" );
2593 goto out;
2594 }
2595 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
2596
2597 count = 64;
2598 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2599 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2600 ok( count == 0, "wrong count %lu\n", count );
2601
2602 memcpy( base, code_jmp, sizeof(code_jmp) );
2603 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2604
2605 count = 64;
2606 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2607 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2608 ok( count == 1, "wrong count %lu\n", count );
2609 ok( results[0] == base, "wrong result %p\n", results[0] );
2610
2611 /* Create a new window class and associcated Window (see above) */
2612
2613 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2614 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2615
2616 memset( &wc, 0, sizeof(wc) );
2617 wc.cbSize = sizeof(wc);
2618 wc.style = CS_VREDRAW | CS_HREDRAW;
2619 wc.hInstance = GetModuleHandleA( 0 );
2620 wc.hCursor = LoadCursorA( NULL, (LPCSTR)IDC_ARROW );
2621 wc.hbrBackground = NULL;
2622 wc.lpszClassName = cls_name;
2623 wc.lpfnWndProc = (WNDPROC)base;
2624 success = RegisterClassExA(&wc) != 0;
2625 ok( success, "RegisterClassExA failed %u\n", GetLastError() );
2626
2627 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2628 ok( hWnd != 0, "CreateWindowExA failed %u\n", GetLastError() );
2629
2630 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2631 ok( ret == 42, "SendMessage returned unexpected result %d\n", ret );
2632
2633 count = 64;
2634 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2635 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2636 ok( count == 0, "wrong count %lu\n", count );
2637
2638 /* At first try with an instruction which is not recognized as proper ATL thunk
2639 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
2640 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
2641
2642 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2643 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2644
2645 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2646 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2647 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2648 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2649 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2650 else
2651 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2652
2653 count = 64;
2654 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2655 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2656 ok( count == 0, "wrong count %lu\n", count );
2657
2658 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2659 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2660 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2661 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2662
2663 /* Now a bit more complicated, the page containing the code is protected with
2664 * PAGE_GUARD memory protection. */
2665
2666 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2667 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2668
2669 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2670 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2671 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2672 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2673 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2674 else
2675 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2676
2677 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2678 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2679 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2680 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2681
2682 count = 64;
2683 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2684 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2685 ok( count == 0 || broken(count == 1) /* Windows 8 */, "wrong count %lu\n", count );
2686
2687 /* Now test with a proper ATL thunk instruction. */
2688
2689 memcpy( base, code_atl1, sizeof(code_atl1) );
2690 *(DWORD *)(base + 9) = (DWORD_PTR)atl_test_func - (DWORD_PTR)(base + 13);
2691
2692 count = 64;
2693 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2694 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2695 ok( count == 1, "wrong count %lu\n", count );
2696 ok( results[0] == base, "wrong result %p\n", results[0] );
2697
2698 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2699 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2700
2701 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2702 ok( ret == 43, "SendMessage returned unexpected result %d\n", ret );
2703
2704 /* Try executing with PAGE_READWRITE protection. */
2705
2706 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2707 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2708
2709 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2710 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2711 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2712 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2713 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2714 else
2715 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2716
2717 count = 64;
2718 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2719 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2720 ok( count == 0, "wrong count %lu\n", count );
2721
2722 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2723 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2724 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2725 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2726 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2727 else
2728 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2729
2730 /* Now a bit more complicated, the page containing the code is protected with
2731 * PAGE_GUARD memory protection. */
2732
2733 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2734 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2735
2736 /* the same, but with PAGE_GUARD set */
2737 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2738 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2739 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2740 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2741 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2742 else
2743 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2744
2745 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2746 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2747 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2748 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2749
2750 count = 64;
2751 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2752 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2753 ok( count == 0 || broken(count == 1) /* Windows 8 */, "wrong count %lu\n", count );
2754
2755 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2756
2757 memcpy( base, code_jmp, sizeof(code_jmp) );
2758 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2759
2760 count = 64;
2761 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2762 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2763 ok( count == 1, "wrong count %lu\n", count );
2764 ok( results[0] == base, "wrong result %p\n", results[0] );
2765
2766 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2767 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2768
2769 DestroyWindow( hWnd );
2770
2771 success = UnregisterClassA( cls_name, GetModuleHandleA(0) );
2772 ok( success, "UnregisterClass failed %u\n", GetLastError() );
2773
2774 VirtualFree( base, 0, MEM_RELEASE );
2775
2776 out:
2777 if (restore_flags)
2778 {
2779 ret = NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &old_flags, sizeof(old_flags) );
2780 ok( !ret, "NtSetInformationProcess failed with status %08x\n", ret );
2781 }
2782 }
2783
2784 #endif /* __i386__ */
2785
2786 static void test_VirtualProtect(void)
2787 {
2788 static const struct test_data
2789 {
2790 DWORD prot_set, prot_get;
2791 } td[] =
2792 {
2793 { 0, 0 }, /* 0x00 */
2794 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
2795 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
2796 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
2797 { PAGE_READWRITE, PAGE_READWRITE }, /* 0x04 */
2798 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
2799 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
2800 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
2801 { PAGE_WRITECOPY, 0 }, /* 0x08 */
2802 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
2803 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
2804 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
2805 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
2806 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
2807 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
2808 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
2809
2810 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
2811 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
2812 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
2813 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
2814 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
2815 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
2816 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
2817 { PAGE_EXECUTE_WRITECOPY, 0 }, /* 0x80 */
2818 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
2819 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
2820 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
2821 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
2822 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
2823 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
2824 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
2825 };
2826 char *base, *ptr;
2827 DWORD ret, old_prot, rw_prot, exec_prot, i, j;
2828 MEMORY_BASIC_INFORMATION info;
2829 SYSTEM_INFO si;
2830 void *addr;
2831 SIZE_T size;
2832 NTSTATUS status;
2833
2834 GetSystemInfo(&si);
2835 trace("system page size %#x\n", si.dwPageSize);
2836
2837 SetLastError(0xdeadbeef);
2838 base = VirtualAlloc(0, si.dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
2839 ok(base != NULL, "VirtualAlloc failed %d\n", GetLastError());
2840
2841 SetLastError(0xdeadbeef);
2842 ret = VirtualProtect(base, si.dwPageSize, PAGE_READONLY, NULL);
2843 ok(!ret, "VirtualProtect should fail\n");
2844 ok(GetLastError() == ERROR_NOACCESS, "expected ERROR_NOACCESS, got %d\n", GetLastError());
2845 old_prot = 0xdeadbeef;
2846 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2847 ok(ret, "VirtualProtect failed %d\n", GetLastError());
2848 ok(old_prot == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", old_prot);
2849
2850 addr = base;
2851 size = si.dwPageSize;
2852 status = pNtProtectVirtualMemory(GetCurrentProcess(), &addr, &size, PAGE_READONLY, NULL);
2853 ok(status == STATUS_ACCESS_VIOLATION, "NtProtectVirtualMemory should fail, got %08x\n", status);
2854 addr = base;
2855 size = si.dwPageSize;
2856 old_prot = 0xdeadbeef;
2857 status = pNtProtectVirtualMemory(GetCurrentProcess(), &addr, &size, PAGE_NOACCESS, &old_prot);
2858 ok(status == STATUS_SUCCESS, "NtProtectVirtualMemory should succeed, got %08x\n", status);
2859 ok(old_prot == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", old_prot);
2860
2861 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
2862 {
2863 SetLastError(0xdeadbeef);
2864 ret = VirtualQuery(base, &info, sizeof(info));
2865 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2866 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2867 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2868 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
2869 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2870 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
2871 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2872 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
2873
2874 old_prot = 0xdeadbeef;
2875 SetLastError(0xdeadbeef);
2876 ret = VirtualProtect(base, si.dwPageSize, td[i].prot_set, &old_prot);
2877 if (td[i].prot_get)
2878 {
2879 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2880 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2881
2882 SetLastError(0xdeadbeef);
2883 ret = VirtualQuery(base, &info, sizeof(info));
2884 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2885 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2886 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2887 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
2888 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2889 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
2890 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2891 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
2892 }
2893 else
2894 {
2895 ok(!ret, "%d: VirtualProtect should fail\n", i);
2896 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2897 }
2898
2899 old_prot = 0xdeadbeef;
2900 SetLastError(0xdeadbeef);
2901 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2902 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2903 if (td[i].prot_get)
2904 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
2905 else
2906 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2907 }
2908
2909 exec_prot = 0;
2910
2911 for (i = 0; i <= 4; i++)
2912 {
2913 rw_prot = 0;
2914
2915 for (j = 0; j <= 4; j++)
2916 {
2917 DWORD prot = exec_prot | rw_prot;
2918
2919 SetLastError(0xdeadbeef);
2920 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, prot);
2921 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
2922 {
2923 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
2924 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2925 }
2926 else
2927 {
2928 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
2929 {
2930 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
2931 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2932 }
2933 else
2934 {
2935 ok(ptr != NULL, "VirtualAlloc(%02x) error %d\n", prot, GetLastError());
2936 ok(ptr == base, "expected %p, got %p\n", base, ptr);
2937 }
2938 }
2939
2940 SetLastError(0xdeadbeef);
2941 ret = VirtualProtect(base, si.dwPageSize, prot, &old_prot);
2942 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
2943 {
2944 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
2945 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2946 }
2947 else
2948 {
2949 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
2950 {
2951 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
2952 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2953 }
2954 else
2955 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
2956 }
2957
2958 rw_prot = 1 << j;
2959 }
2960
2961 exec_prot = 1 << (i + 4);
2962 }
2963
2964 VirtualFree(base, 0, MEM_RELEASE);
2965 }
2966
2967 static BOOL is_mem_writable(DWORD prot)
2968 {
2969 switch (prot & 0xff)
2970 {
2971 case PAGE_READWRITE:
2972 case PAGE_WRITECOPY:
2973 case PAGE_EXECUTE_READWRITE:
2974 case PAGE_EXECUTE_WRITECOPY:
2975 return TRUE;
2976
2977 default:
2978 return FALSE;
2979 }
2980 }
2981
2982 static void test_VirtualAlloc_protection(void)
2983 {
2984 static const struct test_data
2985 {
2986 DWORD prot;
2987 BOOL success;
2988 } td[] =
2989 {
2990 { 0, FALSE }, /* 0x00 */
2991 { PAGE_NOACCESS, TRUE }, /* 0x01 */
2992 { PAGE_READONLY, TRUE }, /* 0x02 */
2993 { PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x03 */
2994 { PAGE_READWRITE, TRUE }, /* 0x04 */
2995 { PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x05 */
2996 { PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x06 */
2997 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x07 */
2998 { PAGE_WRITECOPY, FALSE }, /* 0x08 */
2999 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE }, /* 0x09 */
3000 { PAGE_WRITECOPY | PAGE_READONLY, FALSE }, /* 0x0a */
3001 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE }, /* 0x0b */
3002 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE }, /* 0x0c */
3003 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x0d */
3004 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x0e */
3005 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x0f */
3006
3007 { PAGE_EXECUTE, TRUE }, /* 0x10 */
3008 { PAGE_EXECUTE_READ, TRUE }, /* 0x20 */
3009 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x30 */
3010 { PAGE_EXECUTE_READWRITE, TRUE }, /* 0x40 */
3011 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0x50 */
3012 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0x60 */
3013 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x70 */
3014 { PAGE_EXECUTE_WRITECOPY, FALSE }, /* 0x80 */
3015 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE }, /* 0x90 */
3016 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE }, /* 0xa0 */
3017 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0xb0 */
3018 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE }, /* 0xc0 */
3019 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0xd0 */
3020 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0xe0 */
3021 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE } /* 0xf0 */
3022 };
3023 char *base, *ptr;
3024 DWORD ret, i;
3025 MEMORY_BASIC_INFORMATION info;
3026 SYSTEM_INFO si;
3027
3028 GetSystemInfo(&si);
3029 trace("system page size %#x\n", si.dwPageSize);
3030
3031 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3032 {
3033 SetLastError(0xdeadbeef);
3034 base = VirtualAlloc(0, si.dwPageSize, MEM_COMMIT, td[i].prot);
3035
3036 if (td[i].success)
3037 {
3038 ok(base != NULL, "%d: VirtualAlloc failed %d\n", i, GetLastError());
3039
3040 SetLastError(0xdeadbeef);
3041 ret = VirtualQuery(base, &info, sizeof(info));
3042 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3043 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
3044 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
3045 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
3046 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
3047 ok(info.AllocationProtect == td[i].prot, "%d: %#x != %#x\n", i, info.AllocationProtect, td[i].prot);
3048 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
3049 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
3050
3051 if (is_mem_writable(info.Protect))
3052 {
3053 base[0] = 0xfe;
3054
3055 SetLastError(0xdeadbeef);
3056 ret = VirtualQuery(base, &info, sizeof(info));
3057 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3058 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
3059 }
3060
3061 SetLastError(0xdeadbeef);
3062 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
3063 ok(ptr == base, "%d: VirtualAlloc failed %d\n", i, GetLastError());
3064
3065 VirtualFree(base, 0, MEM_RELEASE);
3066 }
3067 else
3068 {
3069 ok(!base, "%d: VirtualAlloc should fail\n", i);
3070 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3071 }
3072 }
3073 }
3074
3075 static void test_CreateFileMapping_protection(void)
3076 {
3077 static const struct test_data
3078 {
3079 DWORD prot;
3080 BOOL success;
3081 DWORD prot_after_write;
3082 } td[] =
3083 {
3084 { 0, FALSE, 0 }, /* 0x00 */
3085 { PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x01 */
3086 { PAGE_READONLY, TRUE, PAGE_READONLY }, /* 0x02 */
3087 { PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x03 */
3088 { PAGE_READWRITE, TRUE, PAGE_READWRITE }, /* 0x04 */
3089 { PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x05 */
3090 { PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x06 */
3091 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x07 */
3092 { PAGE_WRITECOPY, TRUE, PAGE_READWRITE }, /* 0x08 */
3093 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x09 */
3094 { PAGE_WRITECOPY | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0a */
3095 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0b */
3096 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0x0c */
3097 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0d */
3098 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0e */
3099 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0f */
3100
3101 { PAGE_EXECUTE, FALSE, PAGE_EXECUTE }, /* 0x10 */
3102 { PAGE_EXECUTE_READ, TRUE, PAGE_EXECUTE_READ }, /* 0x20 */
3103 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_EXECUTE_READ }, /* 0x30 */
3104 { PAGE_EXECUTE_READWRITE, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
3105 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x50 */
3106 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0x60 */
3107 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x70 */
3108 { PAGE_EXECUTE_WRITECOPY, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x80 */
3109 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x90 */
3110 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xa0 */
3111 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xb0 */
3112 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0xc0 */
3113 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xd0 */
3114 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xe0 */
3115 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS } /* 0xf0 */
3116 };
3117 char *base, *ptr;
3118 DWORD ret, i, alloc_prot, prot, old_prot;
3119 MEMORY_BASIC_INFORMATION info;
3120 SYSTEM_INFO si;
3121 char temp_path[MAX_PATH];
3122 char file_name[MAX_PATH];
3123 HANDLE hfile, hmap;
3124 BOOL page_exec_supported = TRUE;
3125
3126 GetSystemInfo(&si);
3127 trace("system page size %#x\n", si.dwPageSize);
3128
3129 GetTempPathA(MAX_PATH, temp_path);
3130 GetTempFileNameA(temp_path, "map", 0, file_name);
3131
3132 SetLastError(0xdeadbeef);
3133 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
3134 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
3135 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
3136 SetEndOfFile(hfile);
3137
3138 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3139 {
3140 SetLastError(0xdeadbeef);
3141 hmap = CreateFileMappingW(hfile, NULL, td[i].prot | SEC_COMMIT, 0, si.dwPageSize, NULL);
3142
3143 if (td[i].success)
3144 {
3145 if (!hmap)
3146 {
3147 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, td[i].prot, GetLastError());
3148 /* NT4 and win2k don't support EXEC on file mappings */
3149 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
3150 {
3151 page_exec_supported = FALSE;
3152 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
3153 continue;
3154 }
3155 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3156 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
3157 {
3158 page_exec_supported = FALSE;
3159 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
3160 continue;
3161 }
3162 }
3163 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, td[i].prot, GetLastError());
3164
3165 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
3166 ok(base != NULL, "%d: MapViewOfFile failed %d\n", i, GetLastError());
3167
3168 SetLastError(0xdeadbeef);
3169 ret = VirtualQuery(base, &info, sizeof(info));
3170 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3171 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
3172 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
3173 ok(info.Protect == PAGE_READONLY, "%d: got %#x != expected PAGE_READONLY\n", i, info.Protect);
3174 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
3175 ok(info.AllocationProtect == PAGE_READONLY, "%d: %#x != PAGE_READONLY\n", i, info.AllocationProtect);
3176 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
3177 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
3178
3179 if (is_mem_writable(info.Protect))
3180 {
3181 base[0] = 0xfe;
3182
3183 SetLastError(0xdeadbeef);
3184 ret = VirtualQuery(base, &info, sizeof(info));
3185 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3186 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
3187 }
3188
3189 SetLastError(0xdeadbeef);
3190 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
3191 ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot);
3192 /* FIXME: remove once Wine is fixed */
3193 todo_wine_if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
3194 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
3195
3196 SetLastError(0xdeadbeef);
3197 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
3198 if (td[i].prot == PAGE_READONLY || td[i].prot == PAGE_WRITECOPY)
3199 ok(ret, "%d: VirtualProtect(%02x) error %d\n", i, td[i].prot, GetLastError());
3200 else
3201 {
3202 ok(!ret, "%d: VirtualProtect(%02x) should fail\n", i, td[i].prot);
3203 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3204 }
3205
3206 UnmapViewOfFile(base);
3207 CloseHandle(hmap);
3208 }
3209 else
3210 {
3211 ok(!hmap, "%d: CreateFileMapping should fail\n", i);
3212 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3213 }
3214 }
3215
3216 if (page_exec_supported) alloc_prot = PAGE_EXECUTE_READWRITE;
3217 else alloc_prot = PAGE_READWRITE;
3218 SetLastError(0xdeadbeef);
3219 hmap = CreateFileMappingW(hfile, NULL, alloc_prot, 0, si.dwPageSize, NULL);
3220 ok(hmap != 0, "%d: CreateFileMapping error %d\n", i, GetLastError());
3221
3222 SetLastError(0xdeadbeef);
3223 base = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE | (page_exec_supported ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
3224 ok(base != NULL, "MapViewOfFile failed %d\n", GetLastError());
3225
3226 old_prot = 0xdeadbeef;
3227 SetLastError(0xdeadbeef);
3228 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
3229 ok(ret, "VirtualProtect error %d\n", GetLastError());
3230 ok(old_prot == alloc_prot, "got %#x != expected %#x\n", old_prot, alloc_prot);
3231
3232 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3233 {
3234 SetLastError(0xdeadbeef);
3235 ret = VirtualQuery(base, &info, sizeof(info));
3236 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3237 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
3238 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
3239 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
3240 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
3241 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
3242 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
3243 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
3244
3245 old_prot = 0xdeadbeef;
3246 SetLastError(0xdeadbeef);
3247 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
3248 if (td[i].success || td[i].prot == PAGE_NOACCESS || td[i].prot == PAGE_EXECUTE)
3249 {
3250 if (!ret)
3251 {
3252 /* win2k and XP don't support EXEC on file mappings */
3253 if (td[i].prot == PAGE_EXECUTE)
3254 {
3255 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
3256 continue;
3257 }
3258 /* NT4 and win2k don't support EXEC on file mappings */
3259 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
3260 {
3261 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
3262 continue;
3263 }
3264 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3265 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
3266 {
3267 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
3268 continue;
3269 }
3270 }
3271
3272 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
3273 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
3274
3275 prot = td[i].prot;
3276 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
3277 if (prot == PAGE_EXECUTE_WRITECOPY) prot = PAGE_EXECUTE_READWRITE;
3278
3279 SetLastError(0xdeadbeef);
3280 ret = VirtualQuery(base, &info, sizeof(info));
3281 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3282 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
3283 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
3284 /* FIXME: remove the condition below once Wine is fixed */
3285 todo_wine_if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
3286 ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
3287 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
3288 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
3289 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
3290 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
3291
3292 if (is_mem_writable(info.Protect))
3293 {
3294 base[0] = 0xfe;
3295
3296 SetLastError(0xdeadbeef);
3297 ret = VirtualQuery(base, &info, sizeof(info));
3298 ok(ret, "VirtualQuery failed %d\n", GetLastError());
3299 ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
3300 }
3301 }
3302 else
3303 {
3304 ok(!ret, "%d: VirtualProtect should fail\n", i);
3305 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
3306 continue;
3307 }
3308
3309 old_prot = 0xdeadbeef;
3310 SetLastError(0xdeadbeef);
3311 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
3312 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
3313 ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
3314 }
3315
3316 UnmapViewOfFile(base);
3317 CloseHandle(hmap);
3318
3319 CloseHandle(hfile);
3320 DeleteFileA(file_name);
3321 }
3322
3323 #define ACCESS_READ 0x01
3324 #define ACCESS_WRITE 0x02
3325 #define ACCESS_EXECUTE 0x04
3326 #define ACCESS_WRITECOPY 0x08
3327
3328 static DWORD page_prot_to_access(DWORD prot)
3329 {
3330 switch (prot)
3331 {
3332 case PAGE_READWRITE:
3333 return ACCESS_READ | ACCESS_WRITE;
3334
3335 case PAGE_EXECUTE:
3336 case PAGE_EXECUTE_READ:
3337 return ACCESS_READ | ACCESS_EXECUTE;
3338
3339 case PAGE_EXECUTE_READWRITE:
3340 return ACCESS_READ | ACCESS_WRITE | ACCESS_WRITECOPY | ACCESS_EXECUTE;
3341
3342 case PAGE_EXECUTE_WRITECOPY:
3343 return ACCESS_READ | ACCESS_WRITECOPY | ACCESS_EXECUTE;
3344
3345 case PAGE_READONLY:
3346 return ACCESS_READ;
3347
3348 case PAGE_WRITECOPY:
3349 return ACCESS_READ;
3350
3351 default:
3352 return 0;
3353 }
3354 }
3355
3356 static BOOL is_compatible_protection(DWORD map_prot, DWORD view_prot, DWORD prot)
3357 {
3358 DWORD map_access, view_access, prot_access;
3359
3360 map_access = page_prot_to_access(map_prot);
3361 view_access = page_prot_to_access(view_prot);
3362 prot_access = page_prot_to_access(prot);
3363
3364 if (view_access == prot_access) return TRUE;
3365 if (!view_access) return FALSE;
3366
3367 if ((view_access & prot_access) != prot_access) return FALSE;
3368 if ((map_access & prot_access) == prot_access) return TRUE;
3369
3370 return FALSE;
3371 }
3372
3373 static DWORD map_prot_to_access(DWORD prot)
3374 {
3375 switch (prot)
3376 {
3377 case PAGE_READWRITE:
3378 case PAGE_EXECUTE_READWRITE:
3379 return SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
3380 case PAGE_READONLY:
3381 case PAGE_WRITECOPY:
3382 case PAGE_EXECUTE:
3383 case PAGE_EXECUTE_READ:
3384 case PAGE_EXECUTE_WRITECOPY:
3385 return SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
3386 default:
3387 return 0;
3388 }
3389 }
3390
3391 static BOOL is_compatible_access(DWORD map_prot, DWORD view_prot)
3392 {
3393 DWORD access = map_prot_to_access(map_prot);
3394 if (!view_prot) view_prot = SECTION_MAP_READ;
3395 return (view_prot & access) == view_prot;
3396 }
3397
3398 static void *map_view_of_file(HANDLE handle, DWORD access)
3399 {
3400 NTSTATUS status;
3401 LARGE_INTEGER offset;
3402 SIZE_T count;
3403 ULONG protect;
3404 BOOL exec;
3405 void *addr;
3406
3407 if (!pNtMapViewOfSection) return NULL;
3408
3409 count = 0;
3410 offset.u.LowPart = 0;
3411 offset.u.HighPart = 0;
3412
3413 exec = access & FILE_MAP_EXECUTE;
3414 access &= ~FILE_MAP_EXECUTE;
3415
3416 if (access == FILE_MAP_COPY)
3417 {
3418 if (exec)
3419 protect = PAGE_EXECUTE_WRITECOPY;
3420 else
3421 protect = PAGE_WRITECOPY;
3422 }
3423 else if (access & FILE_MAP_WRITE)
3424 {
3425 if (exec)
3426 protect = PAGE_EXECUTE_READWRITE;
3427 else
3428 protect = PAGE_READWRITE;
3429 }
3430 else if (access & FILE_MAP_READ)
3431 {
3432 if (exec)
3433 protect = PAGE_EXECUTE_READ;
3434 else
3435 protect = PAGE_READONLY;
3436 }
3437 else protect = PAGE_NOACCESS;
3438
3439 addr = NULL;
3440 status = pNtMapViewOfSection(handle, GetCurrentProcess(), &addr, 0, 0, &offset,
3441 &count, 1 /* ViewShare */, 0, protect);
3442 if (status)
3443 {
3444 /* for simplicity */
3445 SetLastError(ERROR_ACCESS_DENIED);
3446 addr = NULL;
3447 }
3448 return addr;
3449 }
3450
3451 static void test_mapping(void)
3452 {
3453 static const DWORD page_prot[] =
3454 {
3455 PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY,
3456 PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY
3457 };
3458 static const struct
3459 {
3460 DWORD access, prot;
3461 } view[] =
3462 {
3463 { 0, PAGE_NOACCESS }, /* 0x00 */
3464 { FILE_MAP_COPY, PAGE_WRITECOPY }, /* 0x01 */
3465 { FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x02 */
3466 { FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x03 */
3467 { FILE_MAP_READ, PAGE_READONLY }, /* 0x04 */
3468 { FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x05 */
3469 { FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x06 */
3470 { FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x07 */
3471 { SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x08 */
3472 { SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x09 */
3473 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0a */
3474 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0b */
3475 { SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_READONLY }, /* 0x0c */
3476 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x0d */
3477 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0e */
3478 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0f */
3479 { FILE_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x20 */
3480 { FILE_MAP_EXECUTE | FILE_MAP_COPY, PAGE_EXECUTE_WRITECOPY }, /* 0x21 */
3481 { FILE_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x22 */
3482 { FILE_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x23 */
3483 { FILE_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x24 */
3484 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x25 */
3485 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x26 */
3486 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x27 */
3487 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x28 */
3488 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x29 */
3489 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2a */
3490 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x2b */
3491 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x2c */
3492 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x2d */
3493 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2e */
3494 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE } /* 0x2f */
3495 };
3496 void *base, *nt_base, *ptr;
3497 DWORD i, j, k, ret, old_prot, prev_prot;
3498 SYSTEM_INFO si;
3499 char temp_path[MAX_PATH];
3500 char file_name[MAX_PATH];
3501 HANDLE hfile, hmap;
3502 MEMORY_BASIC_INFORMATION info, nt_info;
3503
3504 GetSystemInfo(&si);
3505 trace("system page size %#x\n", si.dwPageSize);
3506
3507 GetTempPathA(MAX_PATH, temp_path);
3508 GetTempFileNameA(temp_path, "map", 0, file_name);
3509
3510 SetLastError(0xdeadbeef);
3511 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
3512 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
3513 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
3514 SetEndOfFile(hfile);
3515
3516 for (i = 0; i < sizeof(page_prot)/sizeof(page_prot[0]); i++)
3517 {
3518 SetLastError(0xdeadbeef);
3519 hmap = CreateFileMappingW(hfile, NULL, page_prot[i] | SEC_COMMIT, 0, si.dwPageSize, NULL);
3520
3521 if (page_prot[i] == PAGE_NOACCESS)
3522 {
3523 HANDLE hmap2;
3524
3525 ok(!hmap, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
3526 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3527
3528 /* A trick to create a not accessible mapping */
3529 SetLastError(0xdeadbeef);
3530 hmap = CreateFileMappingW(hfile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, si.dwPageSize, NULL);
3531 ok(hmap != 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
3532 SetLastError(0xdeadbeef);
3533 ret = DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, FALSE, 0);
3534 ok(ret, "DuplicateHandle error %d\n", GetLastError());
3535 CloseHandle(hmap);
3536 hmap = hmap2;
3537 }
3538
3539 if (!hmap)
3540 {
3541 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, page_prot[i], GetLastError());
3542
3543 /* NT4 and win2k don't support EXEC on file mappings */
3544 if (page_prot[i] == PAGE_EXECUTE_READ || page_prot[i] == PAGE_EXECUTE_READWRITE)
3545 {
3546 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
3547 continue;
3548 }
3549 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3550 if (page_prot[i] == PAGE_EXECUTE_WRITECOPY)
3551 {
3552 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
3553 continue;
3554 }
3555 }
3556
3557 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, page_prot[i], GetLastError());
3558
3559 for (j = 0; j < sizeof(view)/sizeof(view[0]); j++)
3560 {
3561 nt_base = map_view_of_file(hmap, view[j].access);
3562 if (nt_base)
3563 {
3564 SetLastError(0xdeadbeef);
3565 ret = VirtualQuery(nt_base, &nt_info, sizeof(nt_info));
3566 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
3567 UnmapViewOfFile(nt_base);
3568 }
3569
3570 SetLastError(0xdeadbeef);
3571 base = MapViewOfFile(hmap, view[j].access, 0, 0, 0);
3572
3573 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
3574 ok(!nt_base == !base ||
3575 broken((view[j].access & FILE_MAP_EXECUTE) && !nt_base != !base),
3576 "%d: (%04x/%04x) NT %p kernel %p\n", j, page_prot[i], view[j].access, nt_base, base);
3577
3578 if (!is_compatible_access(page_prot[i], view[j].access))
3579 {
3580 ok(!base, "%d: MapViewOfFile(%04x/%04x) should fail\n", j, page_prot[i], view[j].access);
3581 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
3582 continue;
3583 }
3584
3585 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
3586 if (!base && (view[j].access & FILE_MAP_EXECUTE))
3587 {
3588 ok(broken(!base), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
3589 continue;
3590 }
3591
3592 ok(base != NULL, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
3593
3594 SetLastError(0xdeadbeef);
3595 ret = VirtualQuery(base, &info, sizeof(info));
3596 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
3597 ok(info.BaseAddress == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.BaseAddress, base);
3598 ok(info.RegionSize == si.dwPageSize, "%d: (%04x) got %#lx != expected %#x\n", j, view[j].access, info.RegionSize, si.dwPageSize);
3599 ok(info.Protect == view[j].prot ||
3600 broken(view[j].prot == PAGE_EXECUTE_READ && info.Protect == PAGE_READONLY) || /* win2k */
3601 broken(view[j].prot == PAGE_EXECUTE_READWRITE && info.Protect == PAGE_READWRITE) || /* win2k */
3602 broken(view[j].prot == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3603 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.Protect, view[j].prot);
3604 ok(info.AllocationBase == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.AllocationBase, base);
3605 ok(info.AllocationProtect == info.Protect, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.AllocationProtect, info.Protect);
3606 ok(info.State == MEM_COMMIT, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j, view[j].access, info.State);
3607 ok(info.Type == MEM_MAPPED, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j, view[j].access, info.Type);
3608
3609 if (nt_base && base)
3610 {
3611 ok(nt_info.RegionSize == info.RegionSize, "%d: (%04x) got %#lx != expected %#lx\n", j, view[j].access, nt_info.RegionSize, info.RegionSize);
3612 ok(nt_info.Protect == info.Protect /* Vista+ */ ||
3613 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3614 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Protect, info.Protect);
3615 ok(nt_info.AllocationProtect == info.AllocationProtect /* Vista+ */ ||
3616 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3617 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.AllocationProtect, info.AllocationProtect);
3618 ok(nt_info.State == info.State, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.State, info.State);
3619 ok(nt_info.Type == info.Type, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Type, info.Type);
3620 }
3621
3622 prev_prot = info.Protect;
3623
3624 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
3625 {
3626 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3627 SetLastError(0xdeadbeef);
3628 old_prot = 0xdeadbeef;
3629 ret = VirtualProtect(base, si.dwPageSize, page_prot[k], &old_prot);
3630 if (is_compatible_protection(page_prot[i], view[j].prot, page_prot[k]))
3631 {
3632 /* win2k and XP don't support EXEC on file mappings */
3633 if (!ret && page_prot[k] == PAGE_EXECUTE)
3634 {
3635 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3636 continue;
3637 }
3638 /* NT4 and win2k don't support EXEC on file mappings */
3639 if (!ret && (page_prot[k] == PAGE_EXECUTE_READ || page_prot[k] == PAGE_EXECUTE_READWRITE))
3640 {
3641 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3642 continue;
3643 }
3644 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3645 if (!ret && page_prot[k] == PAGE_EXECUTE_WRITECOPY)
3646 {
3647 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
3648 continue;
3649 }
3650 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
3651 if (!ret && view[j].prot == PAGE_EXECUTE_WRITECOPY)
3652 {
3653 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
3654 continue;
3655 }
3656
3657 ok(ret, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot[i], view[j].prot, page_prot[k]);
3658 ok(old_prot == prev_prot, "got %#x, expected %#x\n", old_prot, prev_prot);
3659 prev_prot = page_prot[k];
3660 }
3661 else
3662 {
3663 /* NT4 doesn't fail on incompatible map and view */
3664 if (ret)
3665 {
3666 ok(broken(ret), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
3667 skip("Incompatible map and view are not properly handled on this platform\n");
3668 break; /* NT4 won't pass remaining tests */
3669 }
3670
3671 ok(!ret, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
3672 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3673 }
3674 }
3675
3676 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
3677 {
3678 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3679 SetLastError(0xdeadbeef);
3680 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, page_prot[k]);
3681 ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]);
3682 /* FIXME: remove once Wine is fixed */
3683 todo_wine_if (page_prot[k] == PAGE_WRITECOPY || page_prot[k] == PAGE_EXECUTE_WRITECOPY)
3684 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3685 }
3686
3687 UnmapViewOfFile(base);
3688 }
3689
3690 CloseHandle(hmap);
3691 }
3692
3693 CloseHandle(hfile);
3694 DeleteFileA(file_name);
3695 }
3696
3697 static void test_shared_memory(BOOL is_child)
3698 {
3699 HANDLE mapping;
3700 LONG *p;
3701
3702 SetLastError(0xdeadbef);
3703 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_virtual.c");
3704 ok(mapping != 0, "CreateFileMapping error %d\n", GetLastError());
3705 if (is_child)
3706 ok(GetLastError() == ERROR_ALREADY_EXISTS, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3707
3708 SetLastError(0xdeadbef);
3709 p = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
3710 ok(p != NULL, "MapViewOfFile error %d\n", GetLastError());
3711
3712 if (is_child)
3713 {
3714 ok(*p == 0x1a2b3c4d, "expected 0x1a2b3c4d in child, got %#x\n", *p);
3715 }
3716 else
3717 {
3718 char **argv;
3719 char cmdline[MAX_PATH];
3720 PROCESS_INFORMATION pi;
3721 STARTUPINFOA si = { sizeof(si) };
3722 DWORD ret;
3723
3724 *p = 0x1a2b3c4d;
3725
3726 winetest_get_mainargs(&argv);
3727 sprintf(cmdline, "\"%s\" virtual sharedmem", argv[0]);
3728 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3729 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3730 winetest_wait_child_process(pi.hProcess);
3731 CloseHandle(pi.hThread);
3732 CloseHandle(pi.hProcess);
3733 }
3734
3735 UnmapViewOfFile(p);
3736 CloseHandle(mapping);
3737 }
3738
3739 static void test_shared_memory_ro(BOOL is_child, DWORD child_access)
3740 {
3741 HANDLE mapping;
3742 LONG *p;
3743
3744 SetLastError(0xdeadbef);
3745 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_virtual.c_ro");
3746 ok(mapping != 0, "CreateFileMapping error %d\n", GetLastError());
3747 if (is_child)
3748 ok(GetLastError() == ERROR_ALREADY_EXISTS, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3749
3750 SetLastError(0xdeadbef);
3751 p = MapViewOfFile(mapping, is_child ? child_access : FILE_MAP_READ, 0, 0, 4096);
3752 ok(p != NULL, "MapViewOfFile error %d\n", GetLastError());
3753
3754 if (is_child)
3755 {
3756 *p = 0xdeadbeef;
3757 }
3758 else
3759 {
3760 char **argv;
3761 char cmdline[MAX_PATH];
3762 PROCESS_INFORMATION pi;
3763 STARTUPINFOA si = { sizeof(si) };
3764 DWORD ret;
3765
3766 winetest_get_mainargs(&argv);
3767 sprintf(cmdline, "\"%s\" virtual sharedmemro %x", argv[0], child_access);
3768 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3769 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3770 winetest_wait_child_process(pi.hProcess);
3771 CloseHandle(pi.hThread);
3772 CloseHandle(pi.hProcess);
3773
3774 if(child_access & FILE_MAP_WRITE)
3775 ok(*p == 0xdeadbeef, "*p = %x, expected 0xdeadbeef\n", *p);
3776 else
3777 ok(!*p, "*p = %x, expected 0\n", *p);
3778 }
3779
3780 UnmapViewOfFile(p);
3781 CloseHandle(mapping);
3782 }
3783
3784 static void test_NtQuerySection(void)
3785 {
3786 char path[MAX_PATH];
3787 HANDLE file, mapping;
3788 void *p;
3789 NTSTATUS status;
3790 union
3791 {
3792 SECTION_BASIC_INFORMATION basic;
3793 SECTION_IMAGE_INFORMATION image;
3794 char buf[1024];
3795 } info;
3796 IMAGE_NT_HEADERS *nt;
3797 ULONG ret;
3798 SIZE_T fsize, image_size;
3799 SYSTEM_INFO si;
3800
3801 if (!pNtQuerySection)
3802 {
3803 win_skip("NtQuerySection is not available\n");
3804 return;
3805 }
3806
3807 GetSystemInfo(&si);
3808 page_mask = si.dwPageSize - 1;
3809
3810 GetSystemDirectoryA(path, sizeof(path));
3811 strcat(path, "\\kernel32.dll");
3812
3813 SetLastError(0xdeadbef);
3814 file = CreateFileA(path, GENERIC_READ|GENERIC_EXECUTE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
3815 ok(file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError());
3816
3817 fsize = GetFileSize(file, NULL);
3818
3819 SetLastError(0xdeadbef);
3820 mapping = CreateFileMappingA(file, NULL, PAGE_EXECUTE_READ, 0, 0, NULL);
3821 /* NT4 and win2k don't support EXEC on file mappings */
3822 if (!mapping)
3823 mapping = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL);
3824 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
3825
3826 status = pNtQuerySection(mapping, SectionBasicInformation, NULL, sizeof(info), &ret);
3827 ok(status == STATUS_ACCESS_VIOLATION, "expected STATUS_ACCESS_VIOLATION, got %#x\n", status);
3828
3829 status = pNtQuerySection(mapping, SectionBasicInformation, &info, 0, NULL);
3830 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3831
3832 status = pNtQuerySection(mapping, SectionBasicInformation, &info, 0, &ret);
3833 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3834
3835 memset(&info, 0x55, sizeof(info));
3836 ret = 0xdeadbeef;
3837 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3838 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3839 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3840 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3841 todo_wine
3842 ok(info.basic.Attributes == SEC_FILE, "expected SEC_FILE, got %#x\n", info.basic.Attributes);
3843 todo_wine
3844 ok(info.basic.Size.QuadPart == fsize, "expected %#lx, got %#x/%08x\n", fsize, info.basic.Size.HighPart, info.basic.Size.LowPart);
3845
3846 status = pNtQuerySection(mapping, SectionImageInformation, &info, sizeof(info.basic), &ret);
3847 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3848
3849 status = pNtQuerySection(mapping, SectionImageInformation, &info, sizeof(info), &ret);
3850 ok(status == STATUS_SECTION_NOT_IMAGE, "expected STATUS_SECTION_NOT_IMAGE, got %#x\n", status);
3851
3852 SetLastError(0xdeadbef);
3853 p = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
3854 ok(p != NULL, "MapViewOfFile error %u\n", GetLastError());
3855
3856 nt = image_nt_header(p);
3857 image_size = ROUND_SIZE(p, nt->OptionalHeader.SizeOfImage);
3858
3859 memset(&info, 0x55, sizeof(info));
3860 ret = 0xdeadbeef;
3861 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3862 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3863 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3864 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3865 todo_wine
3866 ok(info.basic.Attributes == SEC_FILE, "expected SEC_FILE, got %#x\n", info.basic.Attributes);
3867 todo_wine
3868 ok(info.basic.Size.QuadPart == fsize, "expected %#lx, got %#x/%08x\n", fsize, info.basic.Size.HighPart, info.basic.Size.LowPart);
3869
3870 UnmapViewOfFile(p);
3871 CloseHandle(mapping);
3872
3873 SetLastError(0xdeadbef);
3874 mapping = CreateFileMappingA(file, NULL, PAGE_EXECUTE_READ|SEC_IMAGE, 0, 0, NULL);
3875 /* NT4 and win2k don't support EXEC on file mappings */
3876 if (!mapping)
3877 mapping = CreateFileMappingA(file, NULL, PAGE_READONLY|SEC_IMAGE, 0, 0, NULL);
3878 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
3879
3880 memset(&info, 0x55, sizeof(info));
3881 ret = 0xdeadbeef;
3882 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3883 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3884 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3885 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3886 todo_wine
3887 ok(info.basic.Attributes == (SEC_FILE|SEC_IMAGE), "expected SEC_FILE|SEC_IMAGE, got %#x\n", info.basic.Attributes);
3888 ok(info.basic.Size.QuadPart == image_size, "expected %#lx, got %#x/%08x\n", image_size, info.basic.Size.HighPart, info.basic.Size.LowPart);
3889
3890 status = pNtQuerySection(mapping, SectionImageInformation, NULL, sizeof(info), &ret);
3891 ok(status == STATUS_ACCESS_VIOLATION, "expected STATUS_ACCESS_VIOLATION, got %#x\n", status);
3892
3893 status = pNtQuerySection(mapping, SectionImageInformation, &info, 0, NULL);
3894 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3895
3896 status = pNtQuerySection(mapping, SectionImageInformation, &info, 0, &ret);
3897 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3898
3899 status = pNtQuerySection(mapping, SectionImageInformation, &info, sizeof(info.basic), &ret);
3900 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %#x\n", status);
3901
3902 SetLastError(0xdeadbef);
3903 p = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
3904 ok(p != NULL, "MapViewOfFile error %u\n", GetLastError());
3905
3906 nt = image_nt_header(p);
3907
3908 memset(&info, 0x55, sizeof(info));
3909 ret = 0xdeadbeef;
3910 status = pNtQuerySection(mapping, SectionImageInformation, &info, sizeof(info), &ret);
3911 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3912 ok(ret == sizeof(info.image), "wrong returned size %u\n", ret);
3913 ok((ULONG_PTR)info.image.TransferAddress == nt->OptionalHeader.ImageBase + nt->OptionalHeader.AddressOfEntryPoint,
3914 "expected %#lx, got %p\n", (SIZE_T)(nt->OptionalHeader.ImageBase + nt->OptionalHeader.AddressOfEntryPoint), info.image.TransferAddress);
3915 ok(info.image.ZeroBits == 0, "expected 0, got %#x\n", info.image.ZeroBits);
3916 todo_wine
3917 ok(info.image.MaximumStackSize == nt->OptionalHeader.SizeOfStackReserve, "expected %#lx, got %#lx\n", (SIZE_T)nt->OptionalHeader.SizeOfStackReserve, info.image.MaximumStackSize);
3918 todo_wine
3919 ok(info.image.CommittedStackSize == nt->OptionalHeader.SizeOfStackCommit, "expected %#lx, got %#lx\n", (SIZE_T)nt->OptionalHeader.SizeOfStackCommit, info.image.CommittedStackSize);
3920 ok(info.image.SubSystemType == nt->OptionalHeader.Subsystem, "expected %#x, got %#x\n", nt->OptionalHeader.Subsystem, info.image.SubSystemType);
3921 ok(info.image.SubsystemVersionLow == nt->OptionalHeader.MinorSubsystemVersion, "expected %#x, got %#x\n", nt->OptionalHeader.MinorSubsystemVersion, info.image.SubsystemVersionLow);
3922 ok(info.image.SubsystemVersionHigh == nt->OptionalHeader.MajorSubsystemVersion, "expected %#x, got %#x\n", nt->OptionalHeader.MajorSubsystemVersion, info.image.SubsystemVersionHigh);
3923 ok(info.image.ImageCharacteristics == nt->FileHeader.Characteristics, "expected %#x, got %#x\n", nt->FileHeader.Characteristics, info.image.ImageCharacteristics);
3924 ok(info.image.DllCharacteristics == nt->OptionalHeader.DllCharacteristics, "expected %#x, got %#x\n", nt->OptionalHeader.DllCharacteristics, info.image.DllCharacteristics);
3925 ok(info.image.Machine == nt->FileHeader.Machine, "expected %#x, got %#x\n", nt->FileHeader.Machine, info.image.Machine);
3926 ok(info.image.ImageContainsCode == TRUE, "expected 1, got %#x\n", info.image.ImageContainsCode);
3927
3928 memset(&info, 0x55, sizeof(info));
3929 ret = 0xdeadbeef;
3930 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3931 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3932 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3933 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3934 todo_wine
3935 ok(info.basic.Attributes == (SEC_FILE|SEC_IMAGE), "expected SEC_FILE|SEC_IMAGE, got %#x\n", info.basic.Attributes);
3936 ok(info.basic.Size.QuadPart == image_size, "expected %#lx, got %#x/%08x\n", image_size, info.basic.Size.HighPart, info.basic.Size.LowPart);
3937
3938 UnmapViewOfFile(p);
3939 CloseHandle(mapping);
3940
3941 SetLastError(0xdeadbef);
3942 mapping = CreateFileMappingA(file, NULL, PAGE_READONLY|SEC_COMMIT|SEC_NOCACHE, 0, 0, NULL);
3943 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
3944
3945 memset(&info, 0x55, sizeof(info));
3946 ret = 0xdeadbeef;
3947 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3948 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3949 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3950 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3951 todo_wine
3952 ok(info.basic.Attributes == SEC_FILE, "expected SEC_FILE, got %#x\n", info.basic.Attributes);
3953 todo_wine
3954 ok(info.basic.Size.QuadPart == fsize, "expected %#lx, got %#x/%08x\n", fsize, info.basic.Size.HighPart, info.basic.Size.LowPart);
3955
3956 CloseHandle(mapping);
3957
3958 SetLastError(0xdeadbef);
3959 mapping = CreateFileMappingA(file, NULL, PAGE_READONLY|SEC_RESERVE, 0, 0, NULL);
3960 todo_wine
3961 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
3962 if (!mapping) goto skip1;
3963
3964 memset(&info, 0x55, sizeof(info));
3965 ret = 0xdeadbeef;
3966 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3967 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3968 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3969 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3970 ok(info.basic.Attributes == SEC_FILE, "expected SEC_FILE, got %#x\n", info.basic.Attributes);
3971 ok(info.basic.Size.QuadPart == fsize, "expected %#lx, got %#x/%08x\n", fsize, info.basic.Size.HighPart, info.basic.Size.LowPart);
3972
3973 CloseHandle(mapping);
3974 skip1:
3975 CloseHandle(file);
3976
3977 SetLastError(0xdeadbef);
3978 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, 4096, NULL);
3979 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
3980
3981 memset(&info, 0x55, sizeof(info));
3982 ret = 0xdeadbeef;
3983 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3984 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3985 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3986 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
3987 ok(info.basic.Attributes == SEC_COMMIT, "expected SEC_COMMIT, got %#x\n", info.basic.Attributes);
3988 ok(info.basic.Size.QuadPart == 4096, "expected 4096, got %#x/%08x\n", info.basic.Size.HighPart, info.basic.Size.LowPart);
3989
3990 SetLastError(0xdeadbef);
3991 p = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
3992 ok(p != NULL, "MapViewOfFile error %u\n", GetLastError());
3993
3994 memset(&info, 0x55, sizeof(info));
3995 ret = 0xdeadbeef;
3996 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
3997 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
3998 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
3999 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
4000 ok(info.basic.Attributes == SEC_COMMIT, "expected SEC_COMMIT, got %#x\n", info.basic.Attributes);
4001 ok(info.basic.Size.QuadPart == 4096, "expected 4096, got %#x/%08x\n", info.basic.Size.HighPart, info.basic.Size.LowPart);
4002
4003 UnmapViewOfFile(p);
4004 CloseHandle(mapping);
4005
4006 SetLastError(0xdeadbef);
4007 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY|SEC_RESERVE, 0, 4096, NULL);
4008 ok(mapping != 0, "CreateFileMapping error %u\n", GetLastError());
4009
4010 memset(&info, 0x55, sizeof(info));
4011 ret = 0xdeadbeef;
4012 status = pNtQuerySection(mapping, SectionBasicInformation, &info, sizeof(info), &ret);
4013 ok(status == STATUS_SUCCESS, "NtQuerySection error %#x\n", status);
4014 ok(ret == sizeof(info.basic), "wrong returned size %u\n", ret);
4015 ok(info.basic.BaseAddress == NULL, "expected NULL, got %p\n", info.basic.BaseAddress);
4016 ok(info.basic.Attributes == SEC_RESERVE, "expected SEC_RESERVE, got %#x\n", info.basic.Attributes);
4017 ok(info.basic.Size.QuadPart == 4096, "expected 4096, got %#x/%08x\n", info.basic.Size.HighPart, info.basic.Size.LowPart);
4018
4019 CloseHandle(mapping);
4020 }
4021
4022 START_TEST(virtual)
4023 {
4024 int argc;
4025 char **argv;
4026 argc = winetest_get_mainargs( &argv );
4027
4028 if (argc >= 3)
4029 {
4030 if (!strcmp(argv[2], "sleep"))
4031 {
4032 Sleep(5000); /* spawned process runs for at most 5 seconds */
4033 return;
4034 }
4035 if (!strcmp(argv[2], "sharedmem"))
4036 {
4037 test_shared_memory(TRUE);
4038 return;
4039 }
4040 if (!strcmp(argv[2], "sharedmemro"))
4041 {
4042 if(!winetest_interactive)
4043 {
4044 skip("CORE-8541: Skipping test_shared_memory_ro(TRUE, strtol(argv[3], NULL, 16))\n");
4045 }
4046 else
4047 {
4048 test_shared_memory_ro(TRUE, strtol(argv[3], NULL, 16));
4049 }
4050 return;
4051 }
4052 while (1)
4053 {
4054 void *mem;
4055 BOOL ret;
4056 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
4057 PAGE_EXECUTE_READWRITE);
4058 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
4059 if (mem == NULL) break;
4060 ret = VirtualFree(mem, 0, MEM_RELEASE);
4061 ok(ret, "VirtualFree failed %u\n", GetLastError());
4062 if (!ret) break;
4063 }
4064 return;
4065 }
4066
4067 hkernel32 = GetModuleHandleA("kernel32.dll");
4068 hntdll = GetModuleHandleA("ntdll.dll");
4069
4070 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
4071 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
4072 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
4073 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
4074 pGetProcessDEPPolicy = (void *)GetProcAddress( hkernel32, "GetProcessDEPPolicy" );
4075 pIsWow64Process = (void *)GetProcAddress( hkernel32, "IsWow64Process" );
4076 pNtAreMappedFilesTheSame = (void *)GetProcAddress( hntdll, "NtAreMappedFilesTheSame" );
4077 pNtMapViewOfSection = (void *)GetProcAddress( hntdll, "NtMapViewOfSection" );
4078 pNtUnmapViewOfSection = (void *)GetProcAddress( hntdll, "NtUnmapViewOfSection" );
4079 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
4080 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll, "RtlAddVectoredExceptionHandler" );
4081 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll, "RtlRemoveVectoredExceptionHandler" );
4082 pNtQuerySection = (void *)GetProcAddress( hntdll, "NtQuerySection" );
4083 pNtProtectVirtualMemory = (void *)GetProcAddress( hntdll, "NtProtectVirtualMemory" );
4084 pNtAllocateVirtualMemory = (void *)GetProcAddress( hntdll, "NtAllocateVirtualMemory" );
4085 pNtFreeVirtualMemory = (void *)GetProcAddress( hntdll, "NtFreeVirtualMemory" );
4086
4087 test_shared_memory(FALSE);
4088 test_shared_memory_ro(FALSE, FILE_MAP_READ|FILE_MAP_WRITE);
4089 test_shared_memory_ro(FALSE, FILE_MAP_COPY);
4090 test_shared_memory_ro(FALSE, FILE_MAP_COPY|FILE_MAP_WRITE);
4091 test_mapping();
4092 test_NtQuerySection();
4093 test_CreateFileMapping_protection();
4094 test_VirtualAlloc_protection();
4095 test_VirtualProtect();
4096 test_VirtualAllocEx();
4097 test_VirtualAlloc();
4098 test_MapViewOfFile();
4099 test_NtMapViewOfSection();
4100 test_NtAreMappedFilesTheSame();
4101 test_CreateFileMapping();
4102 test_IsBadReadPtr();
4103 test_IsBadWritePtr();
4104 test_IsBadCodePtr();
4105 test_write_watch();
4106 test_stack_commit();
4107 #ifdef __i386__
4108 if (!winetest_interactive)
4109 {
4110 skip("ROSTESTS-155: Skipping virtual guard page tests due to Mm assertion failure.\n");
4111 }
4112 else
4113 {
4114 test_guard_page();
4115 /* The following tests should be executed as a last step, and in exactly this
4116 * order, since ATL thunk emulation cannot be enabled anymore on Windows. */
4117 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_ENABLE );
4118 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE );
4119 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE | MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION );
4120 }
4121 #endif
4122 }