ntdll: Only check for ATL thunk if allowed by execute option flags.
[wine.git] / dlls / kernel32 / tests / virtual.c
blob434d880d4835b70801b4d15eec91a6b4a04e37f4
1 /*
2 * Unit test suite for Virtual* family of APIs.
4 * Copyright 2004 Dmitry Timoshkov
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winternl.h"
30 #include "winerror.h"
31 #include "winuser.h"
32 #include "excpt.h"
33 #include "wine/test.h"
35 #define NUM_THREADS 4
36 #define MAPPING_SIZE 0x100000
38 static HINSTANCE hkernel32;
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);
48 /* ############################### */
50 static HANDLE create_target_process(const char *arg)
52 char **argv;
53 char cmdline[MAX_PATH];
54 PROCESS_INFORMATION pi;
55 BOOL ret;
56 STARTUPINFOA si = { 0 };
57 si.cb = sizeof(si);
59 winetest_get_mainargs( &argv );
60 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
61 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
62 ok(ret, "error: %u\n", GetLastError());
63 ret = CloseHandle(pi.hThread);
64 ok(ret, "error %u\n", GetLastError());
65 return pi.hProcess;
68 static void test_VirtualAllocEx(void)
70 const unsigned int alloc_size = 1<<15;
71 char *src, *dst;
72 SIZE_T bytes_written = 0, bytes_read = 0, i;
73 void *addr1, *addr2;
74 BOOL b;
75 DWORD old_prot;
76 MEMORY_BASIC_INFORMATION info;
77 HANDLE hProcess;
79 /* not exported in all windows-versions */
80 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
81 win_skip("Virtual{Alloc,Free}Ex not available\n");
82 return;
85 hProcess = create_target_process("sleep");
86 ok(hProcess != NULL, "Can't start process\n");
88 SetLastError(0xdeadbeef);
89 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
90 PAGE_EXECUTE_READWRITE);
91 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
92 { /* Win9x */
93 win_skip("VirtualAllocEx not implemented\n");
94 TerminateProcess(hProcess, 0);
95 CloseHandle(hProcess);
96 return;
99 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
100 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
101 for (i = 0; i < alloc_size; i++)
102 src[i] = i & 0xff;
104 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
105 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
106 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
107 bytes_written);
108 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
109 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
110 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
112 /* test invalid source buffers */
114 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
115 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
116 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
117 ok( !b, "WriteProcessMemory succeeded\n" );
118 ok( GetLastError() == ERROR_NOACCESS ||
119 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
120 "wrong error %u\n", GetLastError() );
121 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
122 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
123 ok( !b, "ReadProcessMemory succeeded\n" );
124 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
125 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
127 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
128 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
129 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
130 ok( !b, "WriteProcessMemory succeeded\n" );
131 ok( GetLastError() == ERROR_NOACCESS ||
132 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
133 "wrong error %u\n", GetLastError() );
134 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
135 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
136 ok( !b, "ReadProcessMemory succeeded\n" );
137 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
138 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
140 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
141 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
143 VirtualFree( src, 0, MEM_FREE );
144 VirtualFree( dst, 0, MEM_FREE );
147 * The following tests parallel those in test_VirtualAlloc()
150 SetLastError(0xdeadbeef);
151 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
152 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
153 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
154 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
155 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
157 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
158 ok(addr1 != NULL, "VirtualAllocEx failed\n");
160 /* test a not committed memory */
161 memset(&info, 'q', sizeof(info));
162 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
163 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
164 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
165 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
166 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
167 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
168 /* NT reports Protect == 0 for a not committed memory block */
169 ok(info.Protect == 0 /* NT */ ||
170 info.Protect == PAGE_NOACCESS, /* Win9x */
171 "%x != PAGE_NOACCESS\n", info.Protect);
172 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
174 SetLastError(0xdeadbeef);
175 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
176 "VirtualProtectEx should fail on a not committed memory\n");
177 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
178 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
179 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
181 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
182 ok(addr1 == addr2, "VirtualAllocEx failed\n");
184 /* test a committed memory */
185 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
186 "VirtualQueryEx failed\n");
187 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
188 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
189 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
190 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
191 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
192 /* this time NT reports PAGE_NOACCESS as well */
193 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
194 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
196 /* this should fail, since not the whole range is committed yet */
197 SetLastError(0xdeadbeef);
198 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
199 "VirtualProtectEx should fail on a not committed memory\n");
200 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
201 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
202 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
204 old_prot = 0;
205 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
206 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
208 old_prot = 0;
209 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
210 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
212 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
213 "VirtualFreeEx should fail with type 0\n");
214 ok(GetLastError() == ERROR_INVALID_PARAMETER,
215 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
217 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
219 /* if the type is MEM_RELEASE, size must be 0 */
220 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
221 "VirtualFreeEx should fail\n");
222 ok(GetLastError() == ERROR_INVALID_PARAMETER,
223 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
225 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
227 TerminateProcess(hProcess, 0);
228 CloseHandle(hProcess);
231 static void test_VirtualAlloc(void)
233 void *addr1, *addr2;
234 DWORD old_prot;
235 MEMORY_BASIC_INFORMATION info;
237 SetLastError(0xdeadbeef);
238 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
239 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
240 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
241 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
242 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
244 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
245 ok(addr1 != NULL, "VirtualAlloc failed\n");
247 /* test a not committed memory */
248 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
249 "VirtualQuery failed\n");
250 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
251 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
252 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
253 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
254 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
255 /* NT reports Protect == 0 for a not committed memory block */
256 ok(info.Protect == 0 /* NT */ ||
257 info.Protect == PAGE_NOACCESS, /* Win9x */
258 "%x != PAGE_NOACCESS\n", info.Protect);
259 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
261 SetLastError(0xdeadbeef);
262 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
263 "VirtualProtect should fail on a not committed memory\n");
264 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
265 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
266 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
268 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
269 ok(addr1 == addr2, "VirtualAlloc failed\n");
271 /* test a committed memory */
272 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
273 "VirtualQuery failed\n");
274 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
275 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
276 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
277 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
278 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
279 /* this time NT reports PAGE_NOACCESS as well */
280 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
281 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
283 /* this should fail, since not the whole range is committed yet */
284 SetLastError(0xdeadbeef);
285 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
286 "VirtualProtect should fail on a not committed memory\n");
287 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
288 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
289 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
291 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
292 ok(old_prot == PAGE_NOACCESS,
293 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
295 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
296 ok(old_prot == PAGE_READONLY,
297 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
299 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
300 "VirtualQuery failed\n");
301 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
302 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
303 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
304 memset( addr1, 0x55, 20 );
305 ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
307 addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
308 ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
309 "VirtualAlloc failed err %u\n", GetLastError() );
310 ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
311 if (addr2)
313 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
314 "VirtualQuery failed\n");
315 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
316 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
317 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
319 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
320 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
322 ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
323 "VirtualQuery failed\n");
324 ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
325 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
326 ok(info.Protect == 0, "%x != 0\n", info.Protect);
328 addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
329 ok( !addr2, "VirtualAlloc failed\n" );
330 ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
333 /* invalid protection values */
334 SetLastError(0xdeadbeef);
335 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
336 ok(!addr2, "VirtualAlloc succeeded\n");
337 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
339 SetLastError(0xdeadbeef);
340 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
341 ok(!addr2, "VirtualAlloc succeeded\n");
342 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
344 SetLastError(0xdeadbeef);
345 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
346 ok(!addr2, "VirtualAlloc succeeded\n");
347 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
349 SetLastError(0xdeadbeef);
350 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
351 "VirtualProtect succeeded\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
358 SetLastError(0xdeadbeef);
359 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
360 ok(GetLastError() == ERROR_INVALID_PARAMETER,
361 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
363 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
365 /* if the type is MEM_RELEASE, size must be 0 */
366 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
367 ok(GetLastError() == ERROR_INVALID_PARAMETER,
368 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
370 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
373 static void test_MapViewOfFile(void)
375 static const char testfile[] = "testfile.xxx";
376 const char *name;
377 HANDLE file, mapping, map2;
378 void *ptr, *ptr2, *addr;
379 MEMORY_BASIC_INFORMATION info;
380 BOOL ret;
382 SetLastError(0xdeadbeef);
383 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
384 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
385 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
386 SetEndOfFile( file );
388 /* read/write mapping */
390 SetLastError(0xdeadbeef);
391 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
392 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
394 SetLastError(0xdeadbeef);
395 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
396 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
397 UnmapViewOfFile( ptr );
399 /* this fails on win9x but succeeds on NT */
400 SetLastError(0xdeadbeef);
401 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
402 if (ptr) UnmapViewOfFile( ptr );
403 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
405 SetLastError(0xdeadbeef);
406 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
407 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
408 UnmapViewOfFile( ptr );
410 SetLastError(0xdeadbeef);
411 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
412 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
413 UnmapViewOfFile( ptr );
415 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
416 FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
417 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
418 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
419 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
420 UnmapViewOfFile( ptr );
421 CloseHandle( map2 );
423 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
424 FILE_MAP_READ, FALSE, 0 );
425 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
426 SetLastError(0xdeadbeef);
427 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
428 if (!ptr)
430 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
431 CloseHandle( map2 );
432 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
433 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
434 SetLastError(0xdeadbeef);
435 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
436 ok( !ptr, "MapViewOfFile succeeded\n" );
437 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
438 CloseHandle( map2 );
439 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
440 FILE_MAP_READ, FALSE, 0 );
441 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
442 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
443 ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
445 else win_skip( "no access checks on win9x\n" );
447 UnmapViewOfFile( ptr );
448 CloseHandle( map2 );
449 CloseHandle( mapping );
451 /* read-only mapping */
453 SetLastError(0xdeadbeef);
454 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
455 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
457 SetLastError(0xdeadbeef);
458 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
459 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
460 UnmapViewOfFile( ptr );
462 /* this fails on win9x but succeeds on NT */
463 SetLastError(0xdeadbeef);
464 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
465 if (ptr) UnmapViewOfFile( ptr );
466 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
468 SetLastError(0xdeadbeef);
469 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
470 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
471 UnmapViewOfFile( ptr );
473 SetLastError(0xdeadbeef);
474 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
475 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
476 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
477 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
478 CloseHandle( mapping );
480 /* copy-on-write mapping */
482 SetLastError(0xdeadbeef);
483 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
484 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
486 SetLastError(0xdeadbeef);
487 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
488 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
489 UnmapViewOfFile( ptr );
491 SetLastError(0xdeadbeef);
492 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
493 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
494 UnmapViewOfFile( ptr );
496 SetLastError(0xdeadbeef);
497 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
498 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
499 UnmapViewOfFile( ptr );
501 SetLastError(0xdeadbeef);
502 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
503 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
504 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
505 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
506 CloseHandle( mapping );
508 /* no access mapping */
510 SetLastError(0xdeadbeef);
511 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
512 /* fails on NT but succeeds on win9x */
513 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
514 else
516 SetLastError(0xdeadbeef);
517 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
518 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
519 UnmapViewOfFile( ptr );
521 SetLastError(0xdeadbeef);
522 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
523 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
524 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
526 SetLastError(0xdeadbeef);
527 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
528 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
529 UnmapViewOfFile( ptr );
531 SetLastError(0xdeadbeef);
532 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
533 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
534 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
536 CloseHandle( mapping );
539 CloseHandle( file );
541 /* now try read-only file */
543 SetLastError(0xdeadbeef);
544 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
545 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
547 SetLastError(0xdeadbeef);
548 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
549 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
550 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
551 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
553 SetLastError(0xdeadbeef);
554 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
555 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
556 CloseHandle( mapping );
558 SetLastError(0xdeadbeef);
559 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
560 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
561 CloseHandle( mapping );
562 CloseHandle( file );
564 /* now try no access file */
566 SetLastError(0xdeadbeef);
567 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
568 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
570 SetLastError(0xdeadbeef);
571 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
572 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
573 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
574 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
576 SetLastError(0xdeadbeef);
577 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
578 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
579 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
580 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
582 SetLastError(0xdeadbeef);
583 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
584 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
585 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
586 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
588 CloseHandle( file );
589 DeleteFileA( testfile );
591 SetLastError(0xdeadbeef);
592 name = "Local\\Foo";
593 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
594 /* nt4 doesn't have Local\\ */
595 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
597 name = "Foo";
598 file = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
600 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
602 SetLastError(0xdeadbeef);
603 mapping = OpenFileMappingA( FILE_MAP_READ, FALSE, name );
604 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
605 SetLastError(0xdeadbeef);
606 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
607 if (!ptr)
609 SIZE_T size;
610 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
611 SetLastError(0xdeadbeef);
612 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
613 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
614 SetLastError(0xdeadbeef);
615 size = VirtualQuery( ptr, &info, sizeof(info) );
616 ok( size == sizeof(info),
617 "VirtualQuery error %u\n", GetLastError() );
618 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
619 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
620 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
621 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
622 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
623 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
625 else win_skip( "no access checks on win9x\n" );
626 UnmapViewOfFile( ptr );
627 CloseHandle( mapping );
629 SetLastError(0xdeadbeef);
630 mapping = OpenFileMappingA( FILE_MAP_WRITE, FALSE, name );
631 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
632 SetLastError(0xdeadbeef);
633 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
634 if (!ptr)
636 SIZE_T size;
637 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
638 SetLastError(0xdeadbeef);
639 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
640 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
641 SetLastError(0xdeadbeef);
642 size = VirtualQuery( ptr, &info, sizeof(info) );
643 ok( size == sizeof(info),
644 "VirtualQuery error %u\n", GetLastError() );
645 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
646 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
647 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
648 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
649 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
650 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
652 else win_skip( "no access checks on win9x\n" );
653 UnmapViewOfFile( ptr );
654 CloseHandle( mapping );
656 CloseHandle( file );
658 /* read/write mapping with SEC_RESERVE */
659 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
660 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
662 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
663 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
665 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
666 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
667 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
668 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
670 ret = VirtualQuery(ptr, &info, sizeof(info));
671 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
672 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
673 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
674 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
675 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
676 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
678 ok(info.AllocationProtect == PAGE_NOACCESS,
679 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
680 ok(info.Protect == PAGE_NOACCESS,
681 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
683 else
685 ok(info.AllocationProtect == PAGE_READWRITE,
686 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
687 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
688 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
691 if (ptr != ptr2)
693 ret = VirtualQuery(ptr2, &info, sizeof(info));
694 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
695 ok(info.BaseAddress == ptr2,
696 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
697 ok(info.AllocationBase == ptr2,
698 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
699 ok(info.AllocationProtect == PAGE_READWRITE,
700 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
701 ok(info.RegionSize == MAPPING_SIZE,
702 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
703 ok(info.State == MEM_RESERVE,
704 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
705 ok(info.Protect == 0,
706 "Protect should have been 0 instead of 0x%x\n", info.Protect);
707 ok(info.Type == MEM_MAPPED,
708 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
711 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
712 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
714 ret = VirtualQuery(ptr, &info, sizeof(info));
715 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
716 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
717 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
718 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
719 ok(info.State == MEM_COMMIT, "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
720 ok(info.Protect == PAGE_READONLY, "Protect should have been PAGE_READONLY instead of 0x%x\n", info.Protect);
721 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
723 ok(info.AllocationProtect == PAGE_NOACCESS,
724 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
726 else
728 ok(info.AllocationProtect == PAGE_READWRITE,
729 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
730 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
733 /* shows that the VirtualAlloc above affects the mapping, not just the
734 * virtual memory in this process - it also affects all other processes
735 * with a view of the mapping, but that isn't tested here */
736 if (ptr != ptr2)
738 ret = VirtualQuery(ptr2, &info, sizeof(info));
739 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
740 ok(info.BaseAddress == ptr2,
741 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
742 ok(info.AllocationBase == ptr2,
743 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
744 ok(info.AllocationProtect == PAGE_READWRITE,
745 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
746 ok(info.RegionSize == 0x10000,
747 "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
748 ok(info.State == MEM_COMMIT,
749 "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
750 ok(info.Protect == PAGE_READWRITE,
751 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info.Protect);
752 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
755 addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
756 ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
757 "VirtualAlloc failed with error %u\n", GetLastError() );
759 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
760 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
761 if (!ret)
762 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
764 ret = UnmapViewOfFile(ptr2);
765 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
766 ret = UnmapViewOfFile(ptr);
767 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
768 CloseHandle(mapping);
770 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
771 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
773 SetLastError(0xdeadbeef);
774 ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
775 ok( GetLastError() == ERROR_INVALID_ADDRESS,
776 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
777 SetLastError(0xdeadbeef);
778 ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779 ok( GetLastError() == ERROR_INVALID_ADDRESS,
780 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
781 SetLastError(0xdeadbeef);
782 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
783 ok( GetLastError() == ERROR_INVALID_ADDRESS,
784 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
786 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
788 /* close named mapping handle without unmapping */
789 name = "Foo";
790 SetLastError(0xdeadbeef);
791 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
792 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
793 SetLastError(0xdeadbeef);
794 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
795 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
796 SetLastError(0xdeadbeef);
797 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
798 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
799 SetLastError(0xdeadbeef);
800 ret = CloseHandle(map2);
801 ok(ret, "CloseHandle error %d\n", GetLastError());
802 SetLastError(0xdeadbeef);
803 ret = CloseHandle(mapping);
804 ok(ret, "CloseHandle error %d\n", GetLastError());
806 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
807 ok( !ret, "memory is not accessible\n" );
809 ret = VirtualQuery(ptr, &info, sizeof(info));
810 ok(ret, "VirtualQuery error %d\n", GetLastError());
811 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
812 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
813 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
814 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
815 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
816 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
817 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
819 SetLastError(0xdeadbeef);
820 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
821 todo_wine
822 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
823 todo_wine
824 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
825 if (map2) CloseHandle(map2); /* FIXME: remove once Wine is fixed */
826 SetLastError(0xdeadbeef);
827 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
828 ok( mapping != 0, "CreateFileMappingA failed\n" );
829 todo_wine
830 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
831 SetLastError(0xdeadbeef);
832 ret = CloseHandle(mapping);
833 ok(ret, "CloseHandle error %d\n", GetLastError());
835 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
836 ok( !ret, "memory is not accessible\n" );
838 ret = VirtualQuery(ptr, &info, sizeof(info));
839 ok(ret, "VirtualQuery error %d\n", GetLastError());
840 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
841 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
842 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
843 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
844 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
845 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
846 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
848 SetLastError(0xdeadbeef);
849 ret = UnmapViewOfFile(ptr);
850 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
852 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
853 ok( ret, "memory is accessible\n" );
855 ret = VirtualQuery(ptr, &info, sizeof(info));
856 ok(ret, "VirtualQuery error %d\n", GetLastError());
857 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
858 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
859 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
860 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
861 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
862 ok(info.Type == 0, "%#x != 0\n", info.Type);
864 SetLastError(0xdeadbeef);
865 file = CreateFileA(testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
866 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
867 SetFilePointer(file, 4096, NULL, FILE_BEGIN);
868 SetEndOfFile(file);
870 SetLastError(0xdeadbeef);
871 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
872 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
873 SetLastError(0xdeadbeef);
874 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
875 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
876 SetLastError(0xdeadbeef);
877 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
878 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
879 SetLastError(0xdeadbeef);
880 ret = CloseHandle(map2);
881 ok(ret, "CloseHandle error %d\n", GetLastError());
882 SetLastError(0xdeadbeef);
883 ret = CloseHandle(mapping);
884 ok(ret, "CloseHandle error %d\n", GetLastError());
886 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
887 ok( !ret, "memory is not accessible\n" );
889 ret = VirtualQuery(ptr, &info, sizeof(info));
890 ok(ret, "VirtualQuery error %d\n", GetLastError());
891 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
892 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
893 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
894 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
895 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
896 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
897 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
899 SetLastError(0xdeadbeef);
900 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
901 todo_wine
902 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
903 todo_wine
904 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
905 CloseHandle(map2);
906 SetLastError(0xdeadbeef);
907 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
908 ok( mapping != 0, "CreateFileMappingA failed\n" );
909 todo_wine
910 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
911 SetLastError(0xdeadbeef);
912 ret = CloseHandle(mapping);
913 ok(ret, "CloseHandle error %d\n", GetLastError());
915 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
916 ok( !ret, "memory is not accessible\n" );
918 ret = VirtualQuery(ptr, &info, sizeof(info));
919 ok(ret, "VirtualQuery error %d\n", GetLastError());
920 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
921 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
922 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
923 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
924 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
925 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
926 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
928 SetLastError(0xdeadbeef);
929 ret = UnmapViewOfFile(ptr);
930 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
932 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
933 ok( ret, "memory is accessible\n" );
935 ret = VirtualQuery(ptr, &info, sizeof(info));
936 ok(ret, "VirtualQuery error %d\n", GetLastError());
937 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
938 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
939 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
940 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
941 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
942 ok(info.Type == 0, "%#x != 0\n", info.Type);
944 CloseHandle(file);
945 DeleteFileA(testfile);
948 static void test_NtMapViewOfSection(void)
950 HANDLE hProcess;
952 static const char testfile[] = "testfile.xxx";
953 static const char data[] = "test data for NtMapViewOfSection";
954 char buffer[sizeof(data)];
955 HANDLE file, mapping;
956 void *ptr;
957 BOOL ret;
958 DWORD status, written;
959 SIZE_T size, result;
960 LARGE_INTEGER offset;
962 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
964 win_skip( "NtMapViewOfSection not available\n" );
965 return;
968 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
969 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
970 WriteFile( file, data, sizeof(data), &written, NULL );
971 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
972 SetEndOfFile( file );
974 /* read/write mapping */
976 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
977 ok( mapping != 0, "CreateFileMapping failed\n" );
979 hProcess = create_target_process("sleep");
980 ok(hProcess != NULL, "Can't start process\n");
982 ptr = NULL;
983 size = 0;
984 offset.QuadPart = 0;
985 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
986 ok( !status, "NtMapViewOfSection failed status %x\n", status );
988 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
989 ok( ret, "ReadProcessMemory failed\n" );
990 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
991 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
993 status = pNtUnmapViewOfSection( hProcess, ptr );
994 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
996 CloseHandle( mapping );
997 CloseHandle( file );
998 DeleteFileA( testfile );
1000 TerminateProcess(hProcess, 0);
1001 CloseHandle(hProcess);
1004 static void test_NtAreMappedFilesTheSame(void)
1006 static const char testfile[] = "testfile.xxx";
1007 HANDLE file, file2, mapping, map2;
1008 void *ptr, *ptr2;
1009 NTSTATUS status;
1010 char path[MAX_PATH];
1012 if (!pNtAreMappedFilesTheSame)
1014 win_skip( "NtAreMappedFilesTheSame not available\n" );
1015 return;
1018 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1019 NULL, CREATE_ALWAYS, 0, 0 );
1020 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1021 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1022 SetEndOfFile( file );
1024 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1025 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1027 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1028 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1030 file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1031 NULL, OPEN_EXISTING, 0, 0 );
1032 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1034 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
1035 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1036 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1037 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1038 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1039 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1040 UnmapViewOfFile( ptr2 );
1042 ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1043 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1044 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1045 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1046 UnmapViewOfFile( ptr2 );
1047 CloseHandle( map2 );
1049 map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1050 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1051 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1052 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1053 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1054 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1055 UnmapViewOfFile( ptr2 );
1056 CloseHandle( map2 );
1057 CloseHandle( file2 );
1059 status = pNtAreMappedFilesTheSame( ptr, ptr );
1060 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1061 "NtAreMappedFilesTheSame returned %x\n", status );
1063 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
1064 ok( status == STATUS_SUCCESS || broken(status == STATUS_NOT_SAME_DEVICE),
1065 "NtAreMappedFilesTheSame returned %x\n", status );
1067 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1068 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1070 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
1071 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
1072 "NtAreMappedFilesTheSame returned %x\n", status );
1074 status = pNtAreMappedFilesTheSame( ptr, NULL );
1075 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1077 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
1078 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1080 status = pNtAreMappedFilesTheSame( NULL, NULL );
1081 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1083 ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
1084 ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
1085 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1086 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1087 VirtualFree( ptr2, 0, MEM_RELEASE );
1089 UnmapViewOfFile( ptr );
1090 CloseHandle( mapping );
1091 CloseHandle( file );
1093 status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1094 GetModuleHandleA("kernel32.dll") );
1095 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1096 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1097 GetModuleHandleA("kernel32.dll") );
1098 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1099 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1100 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1101 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1103 GetSystemDirectoryA( path, MAX_PATH );
1104 strcat( path, "\\kernel32.dll" );
1105 file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1106 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1108 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1109 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1110 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1111 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1112 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1113 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1114 UnmapViewOfFile( ptr );
1115 CloseHandle( mapping );
1117 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1118 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1119 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1120 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1121 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1122 todo_wine
1123 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1125 file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1126 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1127 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1128 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1129 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
1130 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1131 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1132 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1133 UnmapViewOfFile( ptr2 );
1134 CloseHandle( map2 );
1135 CloseHandle( file2 );
1137 UnmapViewOfFile( ptr );
1138 CloseHandle( mapping );
1140 CloseHandle( file );
1141 DeleteFileA( testfile );
1144 static void test_CreateFileMapping(void)
1146 HANDLE handle, handle2;
1148 /* test case sensitivity */
1150 SetLastError(0xdeadbeef);
1151 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1152 "Wine Test Mapping");
1153 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1154 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1156 SetLastError(0xdeadbeef);
1157 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1158 "Wine Test Mapping");
1159 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1160 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
1161 CloseHandle( handle2 );
1163 SetLastError(0xdeadbeef);
1164 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1165 "WINE TEST MAPPING");
1166 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1167 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1168 CloseHandle( handle2 );
1170 SetLastError(0xdeadbeef);
1171 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1172 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1173 CloseHandle( handle2 );
1175 SetLastError(0xdeadbeef);
1176 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1177 ok( !handle2, "OpenFileMapping succeeded\n");
1178 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1179 "wrong error %u\n", GetLastError());
1181 CloseHandle( handle );
1184 static void test_IsBadReadPtr(void)
1186 BOOL ret;
1187 void *ptr = (void *)0xdeadbeef;
1188 char stackvar;
1190 ret = IsBadReadPtr(NULL, 0);
1191 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1193 ret = IsBadReadPtr(NULL, 1);
1194 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1196 ret = IsBadReadPtr(ptr, 0);
1197 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1199 ret = IsBadReadPtr(ptr, 1);
1200 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1202 ret = IsBadReadPtr(&stackvar, 0);
1203 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1205 ret = IsBadReadPtr(&stackvar, sizeof(char));
1206 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1209 static void test_IsBadWritePtr(void)
1211 BOOL ret;
1212 void *ptr = (void *)0xdeadbeef;
1213 char stackval;
1215 ret = IsBadWritePtr(NULL, 0);
1216 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1218 ret = IsBadWritePtr(NULL, 1);
1219 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1221 ret = IsBadWritePtr(ptr, 0);
1222 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1224 ret = IsBadWritePtr(ptr, 1);
1225 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1227 ret = IsBadWritePtr(&stackval, 0);
1228 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1230 ret = IsBadWritePtr(&stackval, sizeof(char));
1231 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1234 static void test_IsBadCodePtr(void)
1236 BOOL ret;
1237 void *ptr = (void *)0xdeadbeef;
1238 char stackval;
1240 ret = IsBadCodePtr(NULL);
1241 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1243 ret = IsBadCodePtr(ptr);
1244 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1246 ret = IsBadCodePtr((void *)&stackval);
1247 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1250 static void test_write_watch(void)
1252 char *base;
1253 DWORD ret, size, old_prot;
1254 MEMORY_BASIC_INFORMATION info;
1255 void *results[64];
1256 ULONG_PTR count;
1257 ULONG pagesize;
1259 if (!pGetWriteWatch || !pResetWriteWatch)
1261 win_skip( "GetWriteWatch not supported\n" );
1262 return;
1265 size = 0x10000;
1266 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1267 if (!base &&
1268 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1270 win_skip( "MEM_WRITE_WATCH not supported\n" );
1271 return;
1273 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1274 ret = VirtualQuery( base, &info, sizeof(info) );
1275 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1276 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1277 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1278 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1279 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1280 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1281 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1283 count = 64;
1284 SetLastError( 0xdeadbeef );
1285 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1286 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1287 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1288 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1289 "wrong error %u\n", GetLastError() );
1291 SetLastError( 0xdeadbeef );
1292 ret = pGetWriteWatch( 0, GetModuleHandleW(NULL), size, results, &count, &pagesize );
1293 if (ret)
1295 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1296 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1298 else /* win98 */
1300 ok( count == 0, "wrong count %lu\n", count );
1303 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1304 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1305 ok( count == 0, "wrong count %lu\n", count );
1307 base[pagesize + 1] = 0x44;
1309 count = 64;
1310 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1311 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1312 ok( count == 1, "wrong count %lu\n", count );
1313 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1315 count = 64;
1316 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1317 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1318 ok( count == 1, "wrong count %lu\n", count );
1319 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1321 count = 64;
1322 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1323 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1324 ok( count == 0, "wrong count %lu\n", count );
1326 base[2*pagesize + 3] = 0x11;
1327 base[4*pagesize + 8] = 0x11;
1329 count = 64;
1330 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1331 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1332 ok( count == 2, "wrong count %lu\n", count );
1333 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1334 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1336 count = 64;
1337 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1338 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1339 ok( count == 1, "wrong count %lu\n", count );
1340 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1342 ret = pResetWriteWatch( base, 3*pagesize );
1343 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1345 count = 64;
1346 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1347 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1348 ok( count == 1, "wrong count %lu\n", count );
1349 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1351 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1353 count = 64;
1354 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1355 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1356 ok( count == 3, "wrong count %lu\n", count );
1357 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1358 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1359 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1361 count = 1;
1362 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1363 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1364 ok( count == 1, "wrong count %lu\n", count );
1365 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1367 count = 64;
1368 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1369 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1370 ok( count == 2, "wrong count %lu\n", count );
1371 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1372 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1374 /* changing protections doesn't affect watches */
1376 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1377 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1378 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1380 ret = VirtualQuery( base, &info, sizeof(info) );
1381 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1382 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1383 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1384 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1385 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1387 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1388 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1389 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1391 count = 64;
1392 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1393 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1394 ok( count == 2, "wrong count %lu\n", count );
1395 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1396 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1398 ret = VirtualQuery( base, &info, sizeof(info) );
1399 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1400 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1401 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1402 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1403 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1405 /* some invalid parameter tests */
1407 SetLastError( 0xdeadbeef );
1408 count = 0;
1409 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1410 if (ret)
1412 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1413 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1415 SetLastError( 0xdeadbeef );
1416 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1417 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1418 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1420 SetLastError( 0xdeadbeef );
1421 count = 64;
1422 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1423 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1424 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1426 SetLastError( 0xdeadbeef );
1427 count = 64;
1428 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1429 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1430 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1432 SetLastError( 0xdeadbeef );
1433 count = 0;
1434 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1435 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1436 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1438 SetLastError( 0xdeadbeef );
1439 count = 64;
1440 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1441 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1442 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1444 SetLastError( 0xdeadbeef );
1445 count = 64;
1446 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1447 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1448 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1450 SetLastError( 0xdeadbeef );
1451 count = 64;
1452 ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1453 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1454 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1456 SetLastError( 0xdeadbeef );
1457 count = 64;
1458 ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1459 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1460 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1462 SetLastError( 0xdeadbeef );
1463 ret = pResetWriteWatch( base, 0 );
1464 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1465 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1467 SetLastError( 0xdeadbeef );
1468 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1469 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1470 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1472 else /* win98 is completely different */
1474 SetLastError( 0xdeadbeef );
1475 count = 64;
1476 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1477 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1478 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1480 count = 0;
1481 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1482 ok( !ret, "GetWriteWatch failed %u\n", ret );
1484 count = 64;
1485 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1486 ok( !ret, "GetWriteWatch failed %u\n", ret );
1488 count = 64;
1489 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1490 ok( !ret, "GetWriteWatch failed %u\n", ret );
1492 ret = pResetWriteWatch( base, 0 );
1493 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1495 ret = pResetWriteWatch( GetModuleHandleW(NULL), size );
1496 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1499 VirtualFree( base, 0, MEM_FREE );
1501 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1502 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1503 VirtualFree( base, 0, MEM_FREE );
1505 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1506 ok( !base, "VirtualAlloc succeeded\n" );
1507 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1509 /* initial protect doesn't matter */
1511 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1512 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1513 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1514 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1516 count = 64;
1517 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1518 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1519 ok( count == 0, "wrong count %lu\n", count );
1521 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1522 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1523 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1525 base[5*pagesize + 200] = 3;
1527 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1528 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1529 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1531 count = 64;
1532 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1533 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1534 ok( count == 1, "wrong count %lu\n", count );
1535 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1537 ret = VirtualFree( base, size, MEM_DECOMMIT );
1538 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1540 count = 64;
1541 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1542 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1543 ok( count == 1 || broken(count == 0), /* win98 */
1544 "wrong count %lu\n", count );
1545 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1547 VirtualFree( base, 0, MEM_FREE );
1550 #ifdef __i386__
1552 static DWORD num_guard_page_calls;
1554 static DWORD guard_page_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1555 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1557 trace( "exception: %08x flags:%x addr:%p\n",
1558 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
1560 ok( rec->NumberParameters == 2, "NumberParameters is %d instead of 2\n", rec->NumberParameters );
1561 ok( rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION, "ExceptionCode is %08x instead of %08x\n",
1562 rec->ExceptionCode, STATUS_GUARD_PAGE_VIOLATION );
1564 num_guard_page_calls++;
1565 *(int *)rec->ExceptionInformation[1] += 0x100;
1567 return ExceptionContinueExecution;
1570 static void test_guard_page(void)
1572 EXCEPTION_REGISTRATION_RECORD frame;
1573 MEMORY_BASIC_INFORMATION info;
1574 DWORD ret, size, old_prot;
1575 int *value, old_value;
1576 void *results[64];
1577 ULONG_PTR count;
1578 ULONG pagesize;
1579 BOOL success;
1580 char *base;
1582 if (!pNtCurrentTeb)
1584 win_skip( "NtCurrentTeb not supported\n" );
1585 return;
1588 size = 0x1000;
1589 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
1590 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1591 value = (int *)base;
1593 /* verify info structure */
1594 ret = VirtualQuery( base, &info, sizeof(info) );
1595 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1596 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1597 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1598 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1599 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1600 ok( info.Protect == (PAGE_READWRITE | PAGE_GUARD), "wrong Protect 0x%x\n", info.Protect );
1601 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1603 /* put some initial value into the memory */
1604 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
1605 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1606 ok( old_prot == (PAGE_READWRITE | PAGE_GUARD), "wrong old prot %x\n", old_prot );
1608 *value = 1;
1609 *(value + 1) = 2;
1611 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1612 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1613 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1615 /* test behaviour of VirtualLock - first attempt should fail */
1616 SetLastError( 0xdeadbeef );
1617 success = VirtualLock( base, size );
1618 ok( !success, "VirtualLock unexpectedly succeded\n" );
1619 todo_wine
1620 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION, "wrong error %u\n", GetLastError() );
1622 success = VirtualLock( base, size );
1623 todo_wine
1624 ok( success, "VirtualLock failed %u\n", GetLastError() );
1625 if (success)
1627 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
1628 success = VirtualUnlock( base, size );
1629 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
1632 /* check info structure again, PAGE_GUARD should be removed now */
1633 ret = VirtualQuery( base, &info, sizeof(info) );
1634 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1635 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1636 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1637 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1638 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1639 todo_wine
1640 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1641 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1643 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1644 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1645 todo_wine
1646 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1648 /* test directly accessing the memory - we need to setup an exception handler first */
1649 frame.Handler = guard_page_handler;
1650 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1651 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1653 num_guard_page_calls = 0;
1654 old_value = *value; /* exception handler increments value by 0x100 */
1655 *value = 2;
1656 ok( old_value == 0x101, "memory block contains wrong value, expected 0x101, got 0x%x\n", old_value );
1657 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1659 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1661 /* check info structure again, PAGE_GUARD should be removed now */
1662 ret = VirtualQuery( base, &info, sizeof(info) );
1663 ok( ret, "VirtualQuery failed %u\n", GetLastError());
1664 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1666 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1667 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1668 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1670 /* test accessing second integer in memory */
1671 frame.Handler = guard_page_handler;
1672 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1673 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1675 num_guard_page_calls = 0;
1676 old_value = *(value + 1);
1677 ok( old_value == 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value );
1678 ok( *value == 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value );
1679 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1681 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1683 success = VirtualLock( base, size );
1684 ok( success, "VirtualLock failed %u\n", GetLastError() );
1685 if (success)
1687 ok( *value == 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value );
1688 success = VirtualUnlock( base, size );
1689 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
1692 VirtualFree( base, 0, MEM_FREE );
1694 /* combined guard page / write watch tests */
1695 if (!pGetWriteWatch || !pResetWriteWatch)
1697 win_skip( "GetWriteWatch not supported, skipping combined guard page / write watch tests\n" );
1698 return;
1701 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE | PAGE_GUARD );
1702 if (!base && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1704 win_skip( "MEM_WRITE_WATCH not supported\n" );
1705 return;
1707 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1708 value = (int *)base;
1710 ret = VirtualQuery( base, &info, sizeof(info) );
1711 ok( ret, "VirtualQuery failed %u\n", GetLastError() );
1712 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1713 ok( info.AllocationProtect == (PAGE_READWRITE | PAGE_GUARD), "wrong AllocationProtect %x\n", info.AllocationProtect );
1714 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1715 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1716 ok( info.Protect == (PAGE_READWRITE | PAGE_GUARD), "wrong Protect 0x%x\n", info.Protect );
1717 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1719 count = 64;
1720 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1721 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1722 ok( count == 0, "wrong count %lu\n", count );
1724 /* writing to a page should trigger should trigger guard page, even if write watch is set */
1725 frame.Handler = guard_page_handler;
1726 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1727 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1729 num_guard_page_calls = 0;
1730 *value = 1;
1731 *(value + 1) = 2;
1732 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1734 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1736 count = 64;
1737 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1738 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1739 ok( count == 1, "wrong count %lu\n", count );
1740 ok( results[0] == base, "wrong result %p\n", results[0] );
1742 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1743 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1745 /* write watch is triggered from inside of the guard page handler */
1746 frame.Handler = guard_page_handler;
1747 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1748 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1750 num_guard_page_calls = 0;
1751 old_value = *(value + 1); /* doesn't trigger write watch */
1752 ok( old_value == 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value );
1753 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
1754 ok( num_guard_page_calls == 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls );
1756 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1758 count = 64;
1759 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1760 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1761 ok( count == 1, "wrong count %lu\n", count );
1762 ok( results[0] == base, "wrong result %p\n", results[0] );
1764 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1765 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1767 /* test behaviour of VirtualLock - first attempt should fail without triggering write watches */
1768 SetLastError( 0xdeadbeef );
1769 success = VirtualLock( base, size );
1770 ok( !success, "VirtualLock unexpectedly succeded\n" );
1771 todo_wine
1772 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION, "wrong error %u\n", GetLastError() );
1774 count = 64;
1775 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1776 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1777 ok( count == 0, "wrong count %lu\n", count );
1779 success = VirtualLock( base, size );
1780 todo_wine
1781 ok( success, "VirtualLock failed %u\n", GetLastError() );
1782 if (success)
1784 ok( *value == 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value );
1785 success = VirtualUnlock( base, size );
1786 ok( success, "VirtualUnlock failed %u\n", GetLastError() );
1789 count = 64;
1790 results[0] = (void *)0xdeadbeef;
1791 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1792 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1793 todo_wine
1794 ok( count == 1 || broken(count == 0) /* Windows 8 */, "wrong count %lu\n", count );
1795 todo_wine
1796 ok( results[0] == base || broken(results[0] == (void *)0xdeadbeef) /* Windows 8 */, "wrong result %p\n", results[0] );
1798 VirtualFree( base, 0, MEM_FREE );
1801 DWORD num_execute_fault_calls;
1803 static DWORD execute_fault_seh_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1804 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1806 ULONG flags = MEM_EXECUTE_OPTION_ENABLE;
1808 trace( "exception: %08x flags:%x addr:%p info[0]:%ld info[1]:%p\n",
1809 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
1810 rec->ExceptionInformation[0], (void *)rec->ExceptionInformation[1] );
1812 ok( rec->NumberParameters == 2, "NumberParameters is %d instead of 2\n", rec->NumberParameters );
1813 ok( rec->ExceptionCode == STATUS_ACCESS_VIOLATION || rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION,
1814 "ExceptionCode is %08x instead of STATUS_ACCESS_VIOLATION or STATUS_GUARD_PAGE_VIOLATION\n", rec->ExceptionCode );
1816 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &flags, sizeof(flags), NULL );
1818 if (rec->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION)
1821 ok( rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT ||
1822 broken(!(flags & MEM_EXECUTE_OPTION_DISABLE) && rec->ExceptionInformation[0] == EXCEPTION_READ_FAULT), /* Windows 2000 */
1823 "ExceptionInformation[0] is %d instead of %d\n", (DWORD)rec->ExceptionInformation[0], EXCEPTION_EXECUTE_FAULT );
1825 num_guard_page_calls++;
1827 else if (rec->ExceptionCode == STATUS_ACCESS_VIOLATION)
1829 DWORD err, old_prot;
1830 BOOL success;
1832 err = (flags & MEM_EXECUTE_OPTION_DISABLE) ? EXCEPTION_EXECUTE_FAULT : EXCEPTION_READ_FAULT;
1833 ok( rec->ExceptionInformation[0] == err, "ExceptionInformation[0] is %d instead of %d\n",
1834 (DWORD)rec->ExceptionInformation[0], err );
1836 success = VirtualProtect( (void *)rec->ExceptionInformation[1], 16, PAGE_EXECUTE_READWRITE, &old_prot );
1837 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1838 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1840 num_execute_fault_calls++;
1843 return ExceptionContinueExecution;
1846 static inline DWORD send_message_excpt( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
1848 EXCEPTION_REGISTRATION_RECORD frame;
1849 DWORD ret;
1851 frame.Handler = execute_fault_seh_handler;
1852 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
1853 pNtCurrentTeb()->Tib.ExceptionList = &frame;
1855 num_guard_page_calls = num_execute_fault_calls = 0;
1856 ret = SendMessageA( hWnd, WM_USER, 0, 0 );
1858 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
1860 return ret;
1863 static LRESULT CALLBACK jmp_test_func( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
1865 if (uMsg == WM_USER)
1866 return 42;
1868 return DefWindowProcA( hWnd, uMsg, wParam, lParam );
1871 static LRESULT CALLBACK atl_test_func( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
1873 DWORD arg = (DWORD)hWnd;
1874 ok( arg == 0x11223344, "arg is 0x%08x instead of 0x11223344\n", arg );
1875 return 43;
1878 static void test_atl_thunk_emulation( ULONG dep_flags )
1880 static const char code_jmp[] = {0xE9, 0x00, 0x00, 0x00, 0x00};
1881 static const char code_atl[] = {0xC7, 0x44, 0x24, 0x04, 0x44, 0x33, 0x22, 0x11, 0xE9, 0x00, 0x00, 0x00, 0x00};
1882 static const char cls_name[] = "atl_thunk_class";
1883 DWORD ret, size, old_prot;
1884 ULONG old_flags = MEM_EXECUTE_OPTION_ENABLE;
1885 void *results[64];
1886 ULONG_PTR count;
1887 ULONG pagesize;
1888 WNDCLASSEXA wc;
1889 BOOL success;
1890 char *base;
1891 HWND hWnd;
1893 if (!pNtCurrentTeb)
1895 win_skip( "NtCurrentTeb not supported\n" );
1896 return;
1899 trace( "Running DEP tests with ProcessExecuteFlags = %d\n", dep_flags );
1901 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &old_flags, sizeof(old_flags), NULL );
1902 if (old_flags != dep_flags)
1904 ret = NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &dep_flags, sizeof(dep_flags) );
1905 if (ret == STATUS_INVALID_INFO_CLASS) /* Windows 2000 */
1907 win_skip( "Skipping DEP tests with ProcessExecuteFlags = %d\n", dep_flags );
1908 return;
1910 ok( !ret, "NtSetInformationProcess failed with status %08x\n", ret );
1913 size = 0x1000;
1914 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
1915 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1917 memcpy( base, code_jmp, sizeof(code_jmp) );
1918 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
1920 /* On Windows, the ATL Thunk emulation is only enabled while running WndProc functions,
1921 * whereas in Wine such a limitation doesn't exist yet. We want to test in a scenario
1922 * where it is active, so that application which depend on that still work properly.
1923 * We have no exception handler enabled yet, so give proper EXECUTE permissions to
1924 * prevent crashes while creating the window. */
1926 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
1927 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1929 memset( &wc, 0, sizeof(wc) );
1930 wc.cbSize = sizeof(wc);
1931 wc.style = CS_VREDRAW | CS_HREDRAW;
1932 wc.hInstance = GetModuleHandleA( 0 );
1933 wc.hCursor = LoadCursorA( NULL, (LPCSTR)IDC_ARROW );
1934 wc.hbrBackground = NULL;
1935 wc.lpszClassName = cls_name;
1936 wc.lpfnWndProc = (WNDPROC)base;
1937 success = RegisterClassExA(&wc) != 0;
1938 ok( success, "RegisterClassExA failed %u\n", GetLastError() );
1940 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
1941 ok( hWnd != 0, "CreateWindowExA failed %u\n", GetLastError() );
1943 ret = SendMessageA(hWnd, WM_USER, 0, 0);
1944 ok( ret == 42, "SendMessage returned unexpected result %d\n", ret );
1946 /* At first try with an instruction which is not recognized as proper ATL thunk
1947 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
1948 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
1950 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
1951 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1953 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
1954 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
1955 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
1956 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
1957 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
1958 else
1959 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
1961 /* Now a bit more complicated, the page containing the code is protected with
1962 * PAGE_GUARD memory protection. */
1964 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
1965 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1967 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
1968 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
1969 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
1970 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
1971 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
1972 else
1973 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
1975 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
1976 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
1977 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
1978 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
1980 /* Now test with a proper ATL thunk instruction. */
1982 memcpy( base, code_atl, sizeof(code_atl) );
1983 *(DWORD *)(base + 9) = (DWORD_PTR)atl_test_func - (DWORD_PTR)(base + 13);
1985 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
1986 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1988 ret = SendMessageA(hWnd, WM_USER, 0, 0);
1989 ok( ret == 43, "SendMessage returned unexpected result %d\n", ret );
1991 /* Try executing with PAGE_READWRITE protection. */
1993 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
1994 ok( success, "VirtualProtect failed %u\n", GetLastError() );
1996 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
1997 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
1998 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
1999 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2000 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2001 else
2002 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2004 /* Now a bit more complicated, the page containing the code is protected with
2005 * PAGE_GUARD memory protection. */
2007 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2008 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2010 /* the same, but with PAGE_GUARD set */
2011 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2012 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2013 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2014 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2015 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2016 else
2017 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2019 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2020 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2021 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2022 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2024 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2026 memcpy( base, code_jmp, sizeof(code_jmp) );
2027 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2029 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2030 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2032 DestroyWindow( hWnd );
2034 success = UnregisterClassA( cls_name, GetModuleHandleA(0) );
2035 ok( success, "UnregisterClass failed %u\n", GetLastError() );
2037 VirtualFree( base, 0, MEM_FREE );
2039 /* Repeat the tests from above with MEM_WRITE_WATCH protected memory. */
2041 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
2042 if (!base && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
2044 win_skip( "MEM_WRITE_WATCH not supported\n" );
2045 goto out;
2047 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
2049 count = 64;
2050 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2051 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2052 ok( count == 0, "wrong count %lu\n", count );
2054 memcpy( base, code_jmp, sizeof(code_jmp) );
2055 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2057 count = 64;
2058 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2059 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2060 ok( count == 1, "wrong count %lu\n", count );
2061 ok( results[0] == base, "wrong result %p\n", results[0] );
2063 /* Create a new window class and associcated Window (see above) */
2065 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2066 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2068 memset( &wc, 0, sizeof(wc) );
2069 wc.cbSize = sizeof(wc);
2070 wc.style = CS_VREDRAW | CS_HREDRAW;
2071 wc.hInstance = GetModuleHandleA( 0 );
2072 wc.hCursor = LoadCursorA( NULL, (LPCSTR)IDC_ARROW );
2073 wc.hbrBackground = NULL;
2074 wc.lpszClassName = cls_name;
2075 wc.lpfnWndProc = (WNDPROC)base;
2076 success = RegisterClassExA(&wc) != 0;
2077 ok( success, "RegisterClassExA failed %u\n", GetLastError() );
2079 hWnd = CreateWindowExA(0, cls_name, "Test", WS_TILEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0);
2080 ok( hWnd != 0, "CreateWindowExA failed %u\n", GetLastError() );
2082 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2083 ok( ret == 42, "SendMessage returned unexpected result %d\n", ret );
2085 count = 64;
2086 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2087 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2088 ok( count == 0, "wrong count %lu\n", count );
2090 /* At first try with an instruction which is not recognized as proper ATL thunk
2091 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
2092 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
2094 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2095 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2097 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2098 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2099 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2100 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2101 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2102 else
2103 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2105 count = 64;
2106 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2107 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2108 ok( count == 0, "wrong count %lu\n", count );
2110 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2111 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2112 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2113 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2115 /* Now a bit more complicated, the page containing the code is protected with
2116 * PAGE_GUARD memory protection. */
2118 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2119 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2121 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2122 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2123 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2124 if (dep_flags & MEM_EXECUTE_OPTION_DISABLE)
2125 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2126 else
2127 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2129 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2130 ok( ret == 42, "call returned wrong result, expected 42, got %d\n", ret );
2131 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2132 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2134 count = 64;
2135 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2136 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2137 ok( count == 0 || broken(count == 1) /* Windows 8 */, "wrong count %lu\n", count );
2139 /* Now test with a proper ATL thunk instruction. */
2141 memcpy( base, code_atl, sizeof(code_atl) );
2142 *(DWORD *)(base + 9) = (DWORD_PTR)atl_test_func - (DWORD_PTR)(base + 13);
2144 count = 64;
2145 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2146 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2147 ok( count == 1, "wrong count %lu\n", count );
2148 ok( results[0] == base, "wrong result %p\n", results[0] );
2150 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2151 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2153 ret = SendMessageA(hWnd, WM_USER, 0, 0);
2154 ok( ret == 43, "SendMessage returned unexpected result %d\n", ret );
2156 /* Try executing with PAGE_READWRITE protection. */
2158 success = VirtualProtect( base, size, PAGE_READWRITE, &old_prot );
2159 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2161 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2162 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2163 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2164 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2165 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2166 else
2167 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2169 count = 64;
2170 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2171 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2172 ok( count == 0, "wrong count %lu\n", count );
2174 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2175 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2176 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2177 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2178 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2179 else
2180 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2182 /* Now a bit more complicated, the page containing the code is protected with
2183 * PAGE_GUARD memory protection. */
2185 success = VirtualProtect( base, size, PAGE_READWRITE | PAGE_GUARD, &old_prot );
2186 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2188 /* the same, but with PAGE_GUARD set */
2189 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2190 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2191 ok( num_guard_page_calls == 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2192 if ((dep_flags & MEM_EXECUTE_OPTION_DISABLE) && (dep_flags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION))
2193 ok( num_execute_fault_calls == 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2194 else
2195 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2197 ret = send_message_excpt( hWnd, WM_USER, 0, 0 );
2198 ok( ret == 43, "call returned wrong result, expected 43, got %d\n", ret );
2199 ok( num_guard_page_calls == 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls );
2200 ok( num_execute_fault_calls == 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls );
2202 count = 64;
2203 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2204 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2205 ok( count == 0 || broken(count == 1) /* Windows 8 */, "wrong count %lu\n", count );
2207 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2209 memcpy( base, code_jmp, sizeof(code_jmp) );
2210 *(DWORD *)(base + 1) = (DWORD_PTR)jmp_test_func - (DWORD_PTR)(base + 5);
2212 count = 64;
2213 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
2214 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
2215 ok( count == 1, "wrong count %lu\n", count );
2216 ok( results[0] == base, "wrong result %p\n", results[0] );
2218 success = VirtualProtect( base, size, PAGE_EXECUTE_READWRITE, &old_prot );
2219 ok( success, "VirtualProtect failed %u\n", GetLastError() );
2221 DestroyWindow( hWnd );
2223 success = UnregisterClassA( cls_name, GetModuleHandleA(0) );
2224 ok( success, "UnregisterClass failed %u\n", GetLastError() );
2226 VirtualFree( base, 0, MEM_FREE );
2228 out:
2229 if (old_flags != dep_flags)
2231 ret = NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags, &old_flags, sizeof(old_flags) );
2232 ok( !ret, "NtSetInformationProcess failed with status %08x\n", ret );
2236 #endif /* __i386__ */
2238 static void test_VirtualProtect(void)
2240 static const struct test_data
2242 DWORD prot_set, prot_get;
2243 } td[] =
2245 { 0, 0 }, /* 0x00 */
2246 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
2247 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
2248 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
2249 { PAGE_READWRITE, PAGE_READWRITE }, /* 0x04 */
2250 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
2251 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
2252 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
2253 { PAGE_WRITECOPY, 0 }, /* 0x08 */
2254 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
2255 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
2256 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
2257 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
2258 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
2259 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
2260 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
2262 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
2263 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
2264 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
2265 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
2266 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
2267 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
2268 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
2269 { PAGE_EXECUTE_WRITECOPY, 0 }, /* 0x80 */
2270 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
2271 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
2272 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
2273 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
2274 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
2275 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
2276 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
2278 char *base, *ptr;
2279 DWORD ret, old_prot, rw_prot, exec_prot, i, j;
2280 MEMORY_BASIC_INFORMATION info;
2281 SYSTEM_INFO si;
2283 GetSystemInfo(&si);
2284 trace("system page size %#x\n", si.dwPageSize);
2286 SetLastError(0xdeadbeef);
2287 base = VirtualAlloc(0, si.dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
2288 ok(base != NULL, "VirtualAlloc failed %d\n", GetLastError());
2290 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
2292 SetLastError(0xdeadbeef);
2293 ret = VirtualQuery(base, &info, sizeof(info));
2294 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2295 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2296 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2297 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
2298 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2299 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
2300 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2301 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
2303 old_prot = 0xdeadbeef;
2304 SetLastError(0xdeadbeef);
2305 ret = VirtualProtect(base, si.dwPageSize, td[i].prot_set, &old_prot);
2306 if (td[i].prot_get)
2308 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2309 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2311 SetLastError(0xdeadbeef);
2312 ret = VirtualQuery(base, &info, sizeof(info));
2313 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2314 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2315 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2316 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
2317 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2318 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
2319 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2320 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
2322 else
2324 ok(!ret, "%d: VirtualProtect should fail\n", i);
2325 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2328 old_prot = 0xdeadbeef;
2329 SetLastError(0xdeadbeef);
2330 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2331 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2332 if (td[i].prot_get)
2333 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
2334 else
2335 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2338 exec_prot = 0;
2340 for (i = 0; i <= 4; i++)
2342 rw_prot = 0;
2344 for (j = 0; j <= 4; j++)
2346 DWORD prot = exec_prot | rw_prot;
2348 SetLastError(0xdeadbeef);
2349 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, prot);
2350 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
2352 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
2353 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2355 else
2357 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
2359 ok(!ptr, "VirtualAlloc(%02x) should fail\n", prot);
2360 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2362 else
2364 ok(ptr != NULL, "VirtualAlloc(%02x) error %d\n", prot, GetLastError());
2365 ok(ptr == base, "expected %p, got %p\n", base, ptr);
2369 SetLastError(0xdeadbeef);
2370 ret = VirtualProtect(base, si.dwPageSize, prot, &old_prot);
2371 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
2373 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
2374 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2376 else
2378 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
2380 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
2381 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2383 else
2384 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
2387 rw_prot = 1 << j;
2390 exec_prot = 1 << (i + 4);
2393 VirtualFree(base, 0, MEM_FREE);
2396 static BOOL is_mem_writable(DWORD prot)
2398 switch (prot & 0xff)
2400 case PAGE_READWRITE:
2401 case PAGE_WRITECOPY:
2402 case PAGE_EXECUTE_READWRITE:
2403 case PAGE_EXECUTE_WRITECOPY:
2404 return TRUE;
2406 default:
2407 return FALSE;
2411 static void test_VirtualAlloc_protection(void)
2413 static const struct test_data
2415 DWORD prot;
2416 BOOL success;
2417 } td[] =
2419 { 0, FALSE }, /* 0x00 */
2420 { PAGE_NOACCESS, TRUE }, /* 0x01 */
2421 { PAGE_READONLY, TRUE }, /* 0x02 */
2422 { PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x03 */
2423 { PAGE_READWRITE, TRUE }, /* 0x04 */
2424 { PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x05 */
2425 { PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x06 */
2426 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x07 */
2427 { PAGE_WRITECOPY, FALSE }, /* 0x08 */
2428 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE }, /* 0x09 */
2429 { PAGE_WRITECOPY | PAGE_READONLY, FALSE }, /* 0x0a */
2430 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE }, /* 0x0b */
2431 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE }, /* 0x0c */
2432 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x0d */
2433 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x0e */
2434 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x0f */
2436 { PAGE_EXECUTE, TRUE }, /* 0x10 */
2437 { PAGE_EXECUTE_READ, TRUE }, /* 0x20 */
2438 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x30 */
2439 { PAGE_EXECUTE_READWRITE, TRUE }, /* 0x40 */
2440 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0x50 */
2441 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0x60 */
2442 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x70 */
2443 { PAGE_EXECUTE_WRITECOPY, FALSE }, /* 0x80 */
2444 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE }, /* 0x90 */
2445 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE }, /* 0xa0 */
2446 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0xb0 */
2447 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE }, /* 0xc0 */
2448 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0xd0 */
2449 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0xe0 */
2450 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE } /* 0xf0 */
2452 char *base, *ptr;
2453 DWORD ret, i;
2454 MEMORY_BASIC_INFORMATION info;
2455 SYSTEM_INFO si;
2457 GetSystemInfo(&si);
2458 trace("system page size %#x\n", si.dwPageSize);
2460 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
2462 SetLastError(0xdeadbeef);
2463 base = VirtualAlloc(0, si.dwPageSize, MEM_COMMIT, td[i].prot);
2465 if (td[i].success)
2467 ok(base != NULL, "%d: VirtualAlloc failed %d\n", i, GetLastError());
2469 SetLastError(0xdeadbeef);
2470 ret = VirtualQuery(base, &info, sizeof(info));
2471 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2472 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2473 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2474 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
2475 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2476 ok(info.AllocationProtect == td[i].prot, "%d: %#x != %#x\n", i, info.AllocationProtect, td[i].prot);
2477 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2478 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
2480 if (is_mem_writable(info.Protect))
2482 base[0] = 0xfe;
2484 SetLastError(0xdeadbeef);
2485 ret = VirtualQuery(base, &info, sizeof(info));
2486 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2487 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
2490 SetLastError(0xdeadbeef);
2491 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
2492 ok(ptr == base, "%d: VirtualAlloc failed %d\n", i, GetLastError());
2494 VirtualFree(base, 0, MEM_FREE);
2496 else
2498 ok(!base, "%d: VirtualAlloc should fail\n", i);
2499 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2504 static void test_CreateFileMapping_protection(void)
2506 static const struct test_data
2508 DWORD prot;
2509 BOOL success;
2510 DWORD prot_after_write;
2511 } td[] =
2513 { 0, FALSE, 0 }, /* 0x00 */
2514 { PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x01 */
2515 { PAGE_READONLY, TRUE, PAGE_READONLY }, /* 0x02 */
2516 { PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x03 */
2517 { PAGE_READWRITE, TRUE, PAGE_READWRITE }, /* 0x04 */
2518 { PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x05 */
2519 { PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x06 */
2520 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x07 */
2521 { PAGE_WRITECOPY, TRUE, PAGE_READWRITE }, /* 0x08 */
2522 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x09 */
2523 { PAGE_WRITECOPY | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0a */
2524 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0b */
2525 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0x0c */
2526 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0d */
2527 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0e */
2528 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0f */
2530 { PAGE_EXECUTE, FALSE, PAGE_EXECUTE }, /* 0x10 */
2531 { PAGE_EXECUTE_READ, TRUE, PAGE_EXECUTE_READ }, /* 0x20 */
2532 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_EXECUTE_READ }, /* 0x30 */
2533 { PAGE_EXECUTE_READWRITE, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
2534 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x50 */
2535 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0x60 */
2536 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x70 */
2537 { PAGE_EXECUTE_WRITECOPY, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x80 */
2538 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x90 */
2539 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xa0 */
2540 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xb0 */
2541 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0xc0 */
2542 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xd0 */
2543 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xe0 */
2544 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS } /* 0xf0 */
2546 char *base, *ptr;
2547 DWORD ret, i, alloc_prot, prot, old_prot;
2548 MEMORY_BASIC_INFORMATION info;
2549 SYSTEM_INFO si;
2550 char temp_path[MAX_PATH];
2551 char file_name[MAX_PATH];
2552 HANDLE hfile, hmap;
2553 BOOL page_exec_supported = TRUE;
2555 GetSystemInfo(&si);
2556 trace("system page size %#x\n", si.dwPageSize);
2558 GetTempPathA(MAX_PATH, temp_path);
2559 GetTempFileNameA(temp_path, "map", 0, file_name);
2561 SetLastError(0xdeadbeef);
2562 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
2563 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
2564 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
2565 SetEndOfFile(hfile);
2567 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
2569 SetLastError(0xdeadbeef);
2570 hmap = CreateFileMappingW(hfile, NULL, td[i].prot | SEC_COMMIT, 0, si.dwPageSize, NULL);
2572 if (td[i].success)
2574 if (!hmap)
2576 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, td[i].prot, GetLastError());
2577 /* NT4 and win2k don't support EXEC on file mappings */
2578 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
2580 page_exec_supported = FALSE;
2581 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
2582 continue;
2584 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2585 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
2587 page_exec_supported = FALSE;
2588 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2589 continue;
2592 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, td[i].prot, GetLastError());
2594 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
2595 ok(base != NULL, "%d: MapViewOfFile failed %d\n", i, GetLastError());
2597 SetLastError(0xdeadbeef);
2598 ret = VirtualQuery(base, &info, sizeof(info));
2599 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2600 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2601 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2602 ok(info.Protect == PAGE_READONLY, "%d: got %#x != expected PAGE_READONLY\n", i, info.Protect);
2603 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2604 ok(info.AllocationProtect == PAGE_READONLY, "%d: %#x != PAGE_READONLY\n", i, info.AllocationProtect);
2605 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2606 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
2608 if (is_mem_writable(info.Protect))
2610 base[0] = 0xfe;
2612 SetLastError(0xdeadbeef);
2613 ret = VirtualQuery(base, &info, sizeof(info));
2614 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2615 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
2618 SetLastError(0xdeadbeef);
2619 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot);
2620 ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot);
2621 /* FIXME: remove once Wine is fixed */
2622 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2623 todo_wine
2624 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
2625 else
2626 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
2628 SetLastError(0xdeadbeef);
2629 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
2630 if (td[i].prot == PAGE_READONLY || td[i].prot == PAGE_WRITECOPY)
2631 ok(ret, "%d: VirtualProtect(%02x) error %d\n", i, td[i].prot, GetLastError());
2632 else
2634 ok(!ret, "%d: VirtualProtect(%02x) should fail\n", i, td[i].prot);
2635 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2638 UnmapViewOfFile(base);
2639 CloseHandle(hmap);
2641 else
2643 ok(!hmap, "%d: CreateFileMapping should fail\n", i);
2644 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2648 if (page_exec_supported) alloc_prot = PAGE_EXECUTE_READWRITE;
2649 else alloc_prot = PAGE_READWRITE;
2650 SetLastError(0xdeadbeef);
2651 hmap = CreateFileMappingW(hfile, NULL, alloc_prot, 0, si.dwPageSize, NULL);
2652 ok(hmap != 0, "%d: CreateFileMapping error %d\n", i, GetLastError());
2654 SetLastError(0xdeadbeef);
2655 base = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE | (page_exec_supported ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
2656 ok(base != NULL, "MapViewOfFile failed %d\n", GetLastError());
2658 old_prot = 0xdeadbeef;
2659 SetLastError(0xdeadbeef);
2660 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2661 ok(ret, "VirtualProtect error %d\n", GetLastError());
2662 ok(old_prot == alloc_prot, "got %#x != expected %#x\n", old_prot, alloc_prot);
2664 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
2666 SetLastError(0xdeadbeef);
2667 ret = VirtualQuery(base, &info, sizeof(info));
2668 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2669 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2670 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2671 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
2672 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2673 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
2674 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2675 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
2677 old_prot = 0xdeadbeef;
2678 SetLastError(0xdeadbeef);
2679 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
2680 if (td[i].success || td[i].prot == PAGE_NOACCESS || td[i].prot == PAGE_EXECUTE)
2682 if (!ret)
2684 /* win2k and XP don't support EXEC on file mappings */
2685 if (td[i].prot == PAGE_EXECUTE)
2687 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
2688 continue;
2690 /* NT4 and win2k don't support EXEC on file mappings */
2691 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
2693 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
2694 continue;
2696 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2697 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
2699 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2700 continue;
2704 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2705 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
2707 prot = td[i].prot;
2708 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
2709 if (prot == PAGE_EXECUTE_WRITECOPY) prot = PAGE_EXECUTE_READWRITE;
2711 SetLastError(0xdeadbeef);
2712 ret = VirtualQuery(base, &info, sizeof(info));
2713 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2714 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
2715 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
2716 /* FIXME: remove the condition below once Wine is fixed */
2717 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
2718 todo_wine ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
2719 else
2720 ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
2721 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
2722 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
2723 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
2724 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
2726 if (is_mem_writable(info.Protect))
2728 base[0] = 0xfe;
2730 SetLastError(0xdeadbeef);
2731 ret = VirtualQuery(base, &info, sizeof(info));
2732 ok(ret, "VirtualQuery failed %d\n", GetLastError());
2733 /* FIXME: remove the condition below once Wine is fixed */
2734 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2735 todo_wine ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2736 else
2737 ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2740 else
2742 ok(!ret, "%d: VirtualProtect should fail\n", i);
2743 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2744 continue;
2747 old_prot = 0xdeadbeef;
2748 SetLastError(0xdeadbeef);
2749 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2750 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2751 /* FIXME: remove the condition below once Wine is fixed */
2752 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2753 todo_wine ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2754 else
2755 ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2758 UnmapViewOfFile(base);
2759 CloseHandle(hmap);
2761 CloseHandle(hfile);
2762 DeleteFileA(file_name);
2765 #define ACCESS_READ 0x01
2766 #define ACCESS_WRITE 0x02
2767 #define ACCESS_EXECUTE 0x04
2768 #define ACCESS_WRITECOPY 0x08
2770 static DWORD page_prot_to_access(DWORD prot)
2772 switch (prot)
2774 case PAGE_READWRITE:
2775 return ACCESS_READ | ACCESS_WRITE;
2777 case PAGE_EXECUTE:
2778 case PAGE_EXECUTE_READ:
2779 return ACCESS_READ | ACCESS_EXECUTE;
2781 case PAGE_EXECUTE_READWRITE:
2782 return ACCESS_READ | ACCESS_WRITE | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2784 case PAGE_EXECUTE_WRITECOPY:
2785 return ACCESS_READ | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2787 case PAGE_READONLY:
2788 return ACCESS_READ;
2790 case PAGE_WRITECOPY:
2791 return ACCESS_READ;
2793 default:
2794 return 0;
2798 static BOOL is_compatible_protection(DWORD map_prot, DWORD view_prot, DWORD prot)
2800 DWORD map_access, view_access, prot_access;
2802 map_access = page_prot_to_access(map_prot);
2803 view_access = page_prot_to_access(view_prot);
2804 prot_access = page_prot_to_access(prot);
2806 if (view_access == prot_access) return TRUE;
2807 if (!view_access) return FALSE;
2809 if ((view_access & prot_access) != prot_access) return FALSE;
2810 if ((map_access & prot_access) == prot_access) return TRUE;
2812 return FALSE;
2815 static DWORD map_prot_to_access(DWORD prot)
2817 switch (prot)
2819 case PAGE_READWRITE:
2820 case PAGE_EXECUTE_READWRITE:
2821 return SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2822 case PAGE_READONLY:
2823 case PAGE_WRITECOPY:
2824 case PAGE_EXECUTE:
2825 case PAGE_EXECUTE_READ:
2826 case PAGE_EXECUTE_WRITECOPY:
2827 return SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2828 default:
2829 return 0;
2833 static BOOL is_compatible_access(DWORD map_prot, DWORD view_prot)
2835 DWORD access = map_prot_to_access(map_prot);
2836 if (!view_prot) view_prot = SECTION_MAP_READ;
2837 return (view_prot & access) == view_prot;
2840 static void *map_view_of_file(HANDLE handle, DWORD access)
2842 NTSTATUS status;
2843 LARGE_INTEGER offset;
2844 SIZE_T count;
2845 ULONG protect;
2846 BOOL exec;
2847 void *addr;
2849 if (!pNtMapViewOfSection) return NULL;
2851 count = 0;
2852 offset.u.LowPart = 0;
2853 offset.u.HighPart = 0;
2855 exec = access & FILE_MAP_EXECUTE;
2856 access &= ~FILE_MAP_EXECUTE;
2858 if (access == FILE_MAP_COPY)
2860 if (exec)
2861 protect = PAGE_EXECUTE_WRITECOPY;
2862 else
2863 protect = PAGE_WRITECOPY;
2865 else if (access & FILE_MAP_WRITE)
2867 if (exec)
2868 protect = PAGE_EXECUTE_READWRITE;
2869 else
2870 protect = PAGE_READWRITE;
2872 else if (access & FILE_MAP_READ)
2874 if (exec)
2875 protect = PAGE_EXECUTE_READ;
2876 else
2877 protect = PAGE_READONLY;
2879 else protect = PAGE_NOACCESS;
2881 addr = NULL;
2882 status = pNtMapViewOfSection(handle, GetCurrentProcess(), &addr, 0, 0, &offset,
2883 &count, 1 /* ViewShare */, 0, protect);
2884 if (status)
2886 /* for simplicity */
2887 SetLastError(ERROR_ACCESS_DENIED);
2888 addr = NULL;
2890 return addr;
2893 static void test_mapping(void)
2895 static const DWORD page_prot[] =
2897 PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY,
2898 PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY
2900 static const struct
2902 DWORD access, prot;
2903 } view[] =
2905 { 0, PAGE_NOACCESS }, /* 0x00 */
2906 { FILE_MAP_COPY, PAGE_WRITECOPY }, /* 0x01 */
2907 { FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x02 */
2908 { FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x03 */
2909 { FILE_MAP_READ, PAGE_READONLY }, /* 0x04 */
2910 { FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x05 */
2911 { FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x06 */
2912 { FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x07 */
2913 { SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x08 */
2914 { SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x09 */
2915 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0a */
2916 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0b */
2917 { SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_READONLY }, /* 0x0c */
2918 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x0d */
2919 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0e */
2920 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0f */
2921 { FILE_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x20 */
2922 { FILE_MAP_EXECUTE | FILE_MAP_COPY, PAGE_EXECUTE_WRITECOPY }, /* 0x21 */
2923 { FILE_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x22 */
2924 { FILE_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x23 */
2925 { FILE_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x24 */
2926 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x25 */
2927 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x26 */
2928 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x27 */
2929 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x28 */
2930 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x29 */
2931 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2a */
2932 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x2b */
2933 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x2c */
2934 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x2d */
2935 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2e */
2936 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE } /* 0x2f */
2938 void *base, *nt_base, *ptr;
2939 DWORD i, j, k, ret, old_prot, prev_prot;
2940 SYSTEM_INFO si;
2941 char temp_path[MAX_PATH];
2942 char file_name[MAX_PATH];
2943 HANDLE hfile, hmap;
2944 MEMORY_BASIC_INFORMATION info, nt_info;
2946 GetSystemInfo(&si);
2947 trace("system page size %#x\n", si.dwPageSize);
2949 GetTempPathA(MAX_PATH, temp_path);
2950 GetTempFileNameA(temp_path, "map", 0, file_name);
2952 SetLastError(0xdeadbeef);
2953 hfile = CreateFileA(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
2954 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
2955 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
2956 SetEndOfFile(hfile);
2958 for (i = 0; i < sizeof(page_prot)/sizeof(page_prot[0]); i++)
2960 SetLastError(0xdeadbeef);
2961 hmap = CreateFileMappingW(hfile, NULL, page_prot[i] | SEC_COMMIT, 0, si.dwPageSize, NULL);
2963 if (page_prot[i] == PAGE_NOACCESS)
2965 HANDLE hmap2;
2967 ok(!hmap, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2968 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2970 /* A trick to create a not accessible mapping */
2971 SetLastError(0xdeadbeef);
2972 hmap = CreateFileMappingW(hfile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, si.dwPageSize, NULL);
2973 ok(hmap != 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2974 SetLastError(0xdeadbeef);
2975 ret = DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, FALSE, 0);
2976 ok(ret, "DuplicateHandle error %d\n", GetLastError());
2977 CloseHandle(hmap);
2978 hmap = hmap2;
2981 if (!hmap)
2983 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, page_prot[i], GetLastError());
2985 /* NT4 and win2k don't support EXEC on file mappings */
2986 if (page_prot[i] == PAGE_EXECUTE_READ || page_prot[i] == PAGE_EXECUTE_READWRITE)
2988 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
2989 continue;
2991 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2992 if (page_prot[i] == PAGE_EXECUTE_WRITECOPY)
2994 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2995 continue;
2999 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, page_prot[i], GetLastError());
3001 for (j = 0; j < sizeof(view)/sizeof(view[0]); j++)
3003 nt_base = map_view_of_file(hmap, view[j].access);
3004 if (nt_base)
3006 SetLastError(0xdeadbeef);
3007 ret = VirtualQuery(nt_base, &nt_info, sizeof(nt_info));
3008 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
3009 UnmapViewOfFile(nt_base);
3012 SetLastError(0xdeadbeef);
3013 base = MapViewOfFile(hmap, view[j].access, 0, 0, 0);
3015 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
3016 ok(!nt_base == !base ||
3017 broken((view[j].access & FILE_MAP_EXECUTE) && !nt_base != !base),
3018 "%d: (%04x/%04x) NT %p kernel %p\n", j, page_prot[i], view[j].access, nt_base, base);
3020 if (!is_compatible_access(page_prot[i], view[j].access))
3022 ok(!base, "%d: MapViewOfFile(%04x/%04x) should fail\n", j, page_prot[i], view[j].access);
3023 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
3024 continue;
3027 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
3028 if (!base && (view[j].access & FILE_MAP_EXECUTE))
3030 ok(broken(!base), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
3031 continue;
3034 ok(base != NULL, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
3036 SetLastError(0xdeadbeef);
3037 ret = VirtualQuery(base, &info, sizeof(info));
3038 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
3039 ok(info.BaseAddress == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.BaseAddress, base);
3040 ok(info.RegionSize == si.dwPageSize, "%d: (%04x) got %#lx != expected %#x\n", j, view[j].access, info.RegionSize, si.dwPageSize);
3041 ok(info.Protect == view[j].prot ||
3042 broken(view[j].prot == PAGE_EXECUTE_READ && info.Protect == PAGE_READONLY) || /* win2k */
3043 broken(view[j].prot == PAGE_EXECUTE_READWRITE && info.Protect == PAGE_READWRITE) || /* win2k */
3044 broken(view[j].prot == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3045 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.Protect, view[j].prot);
3046 ok(info.AllocationBase == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.AllocationBase, base);
3047 ok(info.AllocationProtect == info.Protect, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.AllocationProtect, info.Protect);
3048 ok(info.State == MEM_COMMIT, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j, view[j].access, info.State);
3049 ok(info.Type == MEM_MAPPED, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j, view[j].access, info.Type);
3051 if (nt_base && base)
3053 ok(nt_info.RegionSize == info.RegionSize, "%d: (%04x) got %#lx != expected %#lx\n", j, view[j].access, nt_info.RegionSize, info.RegionSize);
3054 ok(nt_info.Protect == info.Protect /* Vista+ */ ||
3055 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3056 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Protect, info.Protect);
3057 ok(nt_info.AllocationProtect == info.AllocationProtect /* Vista+ */ ||
3058 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
3059 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.AllocationProtect, info.AllocationProtect);
3060 ok(nt_info.State == info.State, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.State, info.State);
3061 ok(nt_info.Type == info.Type, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Type, info.Type);
3064 prev_prot = info.Protect;
3066 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
3068 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3069 SetLastError(0xdeadbeef);
3070 old_prot = 0xdeadbeef;
3071 ret = VirtualProtect(base, si.dwPageSize, page_prot[k], &old_prot);
3072 if (is_compatible_protection(page_prot[i], view[j].prot, page_prot[k]))
3074 /* win2k and XP don't support EXEC on file mappings */
3075 if (!ret && page_prot[k] == PAGE_EXECUTE)
3077 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3078 continue;
3080 /* NT4 and win2k don't support EXEC on file mappings */
3081 if (!ret && (page_prot[k] == PAGE_EXECUTE_READ || page_prot[k] == PAGE_EXECUTE_READWRITE))
3083 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3084 continue;
3086 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3087 if (!ret && page_prot[k] == PAGE_EXECUTE_WRITECOPY)
3089 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
3090 continue;
3092 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
3093 if (!ret && view[j].prot == PAGE_EXECUTE_WRITECOPY)
3095 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
3096 continue;
3099 ok(ret, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot[i], view[j].prot, page_prot[k]);
3100 ok(old_prot == prev_prot, "got %#x, expected %#x\n", old_prot, prev_prot);
3101 prev_prot = page_prot[k];
3103 else
3105 /* NT4 doesn't fail on incompatible map and view */
3106 if (ret)
3108 ok(broken(ret), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
3109 skip("Incompatible map and view are not properly handled on this platform\n");
3110 break; /* NT4 won't pass remaining tests */
3113 ok(!ret, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
3114 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3118 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
3120 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3121 SetLastError(0xdeadbeef);
3122 ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, page_prot[k]);
3123 ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]);
3124 /* FIXME: remove once Wine is fixed */
3125 if (page_prot[k] == PAGE_WRITECOPY || page_prot[k] == PAGE_EXECUTE_WRITECOPY)
3126 todo_wine
3127 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3128 else
3129 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3132 UnmapViewOfFile(base);
3135 CloseHandle(hmap);
3138 CloseHandle(hfile);
3139 DeleteFileA(file_name);
3142 static void test_shared_memory(BOOL is_child)
3144 HANDLE mapping;
3145 LONG *p;
3147 SetLastError(0xdeadbef);
3148 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_virtual.c");
3149 ok(mapping != 0, "CreateFileMapping error %d\n", GetLastError());
3150 if (is_child)
3151 ok(GetLastError() == ERROR_ALREADY_EXISTS, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3153 SetLastError(0xdeadbef);
3154 p = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
3155 ok(p != NULL, "MapViewOfFile error %d\n", GetLastError());
3157 if (is_child)
3159 ok(*p == 0x1a2b3c4d, "expected 0x1a2b3c4d in child, got %#x\n", *p);
3161 else
3163 char **argv;
3164 char cmdline[MAX_PATH];
3165 PROCESS_INFORMATION pi;
3166 STARTUPINFOA si = { sizeof(si) };
3167 DWORD ret;
3169 *p = 0x1a2b3c4d;
3171 winetest_get_mainargs(&argv);
3172 sprintf(cmdline, "\"%s\" virtual sharedmem", argv[0]);
3173 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3174 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3175 winetest_wait_child_process(pi.hProcess);
3176 CloseHandle(pi.hThread);
3177 CloseHandle(pi.hProcess);
3180 UnmapViewOfFile(p);
3181 CloseHandle(mapping);
3184 static void test_shared_memory_ro(BOOL is_child, DWORD child_access)
3186 HANDLE mapping;
3187 LONG *p;
3189 SetLastError(0xdeadbef);
3190 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_virtual.c_ro");
3191 ok(mapping != 0, "CreateFileMapping error %d\n", GetLastError());
3192 if (is_child)
3193 ok(GetLastError() == ERROR_ALREADY_EXISTS, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3195 SetLastError(0xdeadbef);
3196 p = MapViewOfFile(mapping, is_child ? child_access : FILE_MAP_READ, 0, 0, 4096);
3197 ok(p != NULL, "MapViewOfFile error %d\n", GetLastError());
3199 if (is_child)
3201 *p = 0xdeadbeef;
3203 else
3205 char **argv;
3206 char cmdline[MAX_PATH];
3207 PROCESS_INFORMATION pi;
3208 STARTUPINFOA si = { sizeof(si) };
3209 DWORD ret;
3211 winetest_get_mainargs(&argv);
3212 sprintf(cmdline, "\"%s\" virtual sharedmemro %x", argv[0], child_access);
3213 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3214 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3215 winetest_wait_child_process(pi.hProcess);
3216 CloseHandle(pi.hThread);
3217 CloseHandle(pi.hProcess);
3219 if(child_access & FILE_MAP_WRITE)
3220 ok(*p == 0xdeadbeef, "*p = %x, expected 0xdeadbeef\n", *p);
3221 else
3222 ok(!*p, "*p = %x, expected 0\n", *p);
3225 UnmapViewOfFile(p);
3226 CloseHandle(mapping);
3229 START_TEST(virtual)
3231 int argc;
3232 char **argv;
3233 argc = winetest_get_mainargs( &argv );
3235 if (argc >= 3)
3237 if (!strcmp(argv[2], "sleep"))
3239 Sleep(5000); /* spawned process runs for at most 5 seconds */
3240 return;
3242 if (!strcmp(argv[2], "sharedmem"))
3244 test_shared_memory(TRUE);
3245 return;
3247 if (!strcmp(argv[2], "sharedmemro"))
3249 test_shared_memory_ro(TRUE, strtol(argv[3], NULL, 16));
3250 return;
3252 while (1)
3254 void *mem;
3255 BOOL ret;
3256 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
3257 PAGE_EXECUTE_READWRITE);
3258 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
3259 if (mem == NULL) break;
3260 ret = VirtualFree(mem, 0, MEM_RELEASE);
3261 ok(ret, "VirtualFree failed %u\n", GetLastError());
3262 if (!ret) break;
3264 return;
3267 hkernel32 = GetModuleHandleA("kernel32.dll");
3268 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
3269 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
3270 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
3271 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
3272 pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"),
3273 "NtAreMappedFilesTheSame" );
3274 pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtMapViewOfSection");
3275 pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection");
3276 pNtCurrentTeb = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtCurrentTeb" );
3278 test_shared_memory(FALSE);
3279 test_shared_memory_ro(FALSE, FILE_MAP_READ|FILE_MAP_WRITE);
3280 test_shared_memory_ro(FALSE, FILE_MAP_COPY);
3281 test_shared_memory_ro(FALSE, FILE_MAP_COPY|FILE_MAP_WRITE);
3282 test_mapping();
3283 test_CreateFileMapping_protection();
3284 test_VirtualAlloc_protection();
3285 test_VirtualProtect();
3286 test_VirtualAllocEx();
3287 test_VirtualAlloc();
3288 test_MapViewOfFile();
3289 test_NtMapViewOfSection();
3290 test_NtAreMappedFilesTheSame();
3291 test_CreateFileMapping();
3292 test_IsBadReadPtr();
3293 test_IsBadWritePtr();
3294 test_IsBadCodePtr();
3295 test_write_watch();
3296 #ifdef __i386__
3297 test_guard_page();
3298 /* The following tests should be executed as a last step, and in exactly this
3299 * order, since ATL thunk emulation cannot be enabled anymore on Windows. */
3300 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_ENABLE );
3301 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE );
3302 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE | MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION );
3303 #endif