kernel32/tests: Remove a couple of invalid tests.
[wine/multimedia.git] / dlls / kernel32 / tests / virtual.c
blobd02ca341b35fd532354e482ec34cac340f639668
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 "winternl.h"
29 #include "winerror.h"
30 #include "wine/test.h"
32 #define NUM_THREADS 4
33 #define MAPPING_SIZE 0x100000
35 static HINSTANCE hkernel32;
36 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
37 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
38 static UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
39 static UINT (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
40 static NTSTATUS (WINAPI *pNtAreMappedFilesTheSame)(PVOID,PVOID);
41 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
42 static DWORD (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
44 /* ############################### */
46 static HANDLE create_target_process(const char *arg)
48 char **argv;
49 char cmdline[MAX_PATH];
50 PROCESS_INFORMATION pi;
51 BOOL ret;
52 STARTUPINFO si = { 0 };
53 si.cb = sizeof(si);
55 winetest_get_mainargs( &argv );
56 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
57 ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
58 ok(ret, "error: %u\n", GetLastError());
59 ret = CloseHandle(pi.hThread);
60 ok(ret, "error %u\n", GetLastError());
61 return pi.hProcess;
64 static void test_VirtualAllocEx(void)
66 const unsigned int alloc_size = 1<<15;
67 char *src, *dst;
68 SIZE_T bytes_written = 0, bytes_read = 0, i;
69 void *addr1, *addr2;
70 BOOL b;
71 DWORD old_prot;
72 MEMORY_BASIC_INFORMATION info;
73 HANDLE hProcess;
75 /* not exported in all windows-versions */
76 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
77 win_skip("Virtual{Alloc,Free}Ex not available\n");
78 return;
81 hProcess = create_target_process("sleep");
82 ok(hProcess != NULL, "Can't start process\n");
84 SetLastError(0xdeadbeef);
85 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
86 PAGE_EXECUTE_READWRITE);
87 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
88 { /* Win9x */
89 win_skip("VirtualAllocEx not implemented\n");
90 TerminateProcess(hProcess, 0);
91 CloseHandle(hProcess);
92 return;
95 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
96 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
97 for (i = 0; i < alloc_size; i++)
98 src[i] = i & 0xff;
100 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
101 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
102 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
103 bytes_written);
104 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
105 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
106 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
108 /* test invalid source buffers */
110 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
111 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
112 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
113 ok( !b, "WriteProcessMemory succeeded\n" );
114 ok( GetLastError() == ERROR_NOACCESS ||
115 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
116 "wrong error %u\n", GetLastError() );
117 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
118 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
119 ok( !b, "ReadProcessMemory succeeded\n" );
120 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
121 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
123 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
124 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
125 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
126 ok( !b, "WriteProcessMemory succeeded\n" );
127 ok( GetLastError() == ERROR_NOACCESS ||
128 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
129 "wrong error %u\n", GetLastError() );
130 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
131 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
132 ok( !b, "ReadProcessMemory succeeded\n" );
133 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
134 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
136 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
137 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
139 VirtualFree( src, 0, MEM_FREE );
140 VirtualFree( dst, 0, MEM_FREE );
143 * The following tests parallel those in test_VirtualAlloc()
146 SetLastError(0xdeadbeef);
147 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
148 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
149 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
150 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
151 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
153 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
154 ok(addr1 != NULL, "VirtualAllocEx failed\n");
156 /* test a not committed memory */
157 memset(&info, 'q', sizeof(info));
158 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
159 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
160 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
161 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
162 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
163 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
164 /* NT reports Protect == 0 for a not committed memory block */
165 ok(info.Protect == 0 /* NT */ ||
166 info.Protect == PAGE_NOACCESS, /* Win9x */
167 "%x != PAGE_NOACCESS\n", info.Protect);
168 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
170 SetLastError(0xdeadbeef);
171 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
172 "VirtualProtectEx should fail on a not committed memory\n");
173 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
174 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
175 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
177 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
178 ok(addr1 == addr2, "VirtualAllocEx failed\n");
180 /* test a committed memory */
181 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
182 "VirtualQueryEx failed\n");
183 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
184 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
185 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
186 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
187 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
188 /* this time NT reports PAGE_NOACCESS as well */
189 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
190 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
192 /* this should fail, since not the whole range is committed yet */
193 SetLastError(0xdeadbeef);
194 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
195 "VirtualProtectEx should fail on a not committed memory\n");
196 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
197 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
198 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
200 old_prot = 0;
201 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
202 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
204 old_prot = 0;
205 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
206 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
208 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
209 "VirtualFreeEx should fail with type 0\n");
210 ok(GetLastError() == ERROR_INVALID_PARAMETER,
211 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
213 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
215 /* if the type is MEM_RELEASE, size must be 0 */
216 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
217 "VirtualFreeEx should fail\n");
218 ok(GetLastError() == ERROR_INVALID_PARAMETER,
219 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
221 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
223 TerminateProcess(hProcess, 0);
224 CloseHandle(hProcess);
227 static void test_VirtualAlloc(void)
229 void *addr1, *addr2;
230 DWORD old_prot;
231 MEMORY_BASIC_INFORMATION info;
233 SetLastError(0xdeadbeef);
234 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
235 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
236 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
237 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
238 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
240 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
241 ok(addr1 != NULL, "VirtualAlloc failed\n");
243 /* test a not committed memory */
244 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
245 "VirtualQuery failed\n");
246 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
247 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
248 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
249 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
250 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
251 /* NT reports Protect == 0 for a not committed memory block */
252 ok(info.Protect == 0 /* NT */ ||
253 info.Protect == PAGE_NOACCESS, /* Win9x */
254 "%x != PAGE_NOACCESS\n", info.Protect);
255 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
257 SetLastError(0xdeadbeef);
258 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
259 "VirtualProtect should fail on a not committed memory\n");
260 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
261 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
262 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
264 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
265 ok(addr1 == addr2, "VirtualAlloc failed\n");
267 /* test a committed memory */
268 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
269 "VirtualQuery failed\n");
270 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
271 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
272 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
273 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
274 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
275 /* this time NT reports PAGE_NOACCESS as well */
276 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
277 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
279 /* this should fail, since not the whole range is committed yet */
280 SetLastError(0xdeadbeef);
281 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
282 "VirtualProtect should fail on a not committed memory\n");
283 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
284 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
285 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
287 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
288 ok(old_prot == PAGE_NOACCESS,
289 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
291 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
292 ok(old_prot == PAGE_READONLY,
293 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
295 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
296 "VirtualQuery failed\n");
297 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
298 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
299 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
300 memset( addr1, 0x55, 20 );
301 ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
303 addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
304 ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
305 "VirtualAlloc failed err %u\n", GetLastError() );
306 ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
307 if (addr2)
309 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
310 "VirtualQuery failed\n");
311 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
312 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
313 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
315 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
316 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
318 ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
319 "VirtualQuery failed\n");
320 ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
321 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
322 ok(info.Protect == 0, "%x != 0\n", info.Protect);
324 addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
325 ok( !addr2, "VirtualAlloc failed\n" );
326 ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
329 /* invalid protection values */
330 SetLastError(0xdeadbeef);
331 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
332 ok(!addr2, "VirtualAlloc succeeded\n");
333 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
335 SetLastError(0xdeadbeef);
336 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
337 ok(!addr2, "VirtualAlloc succeeded\n");
338 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
340 SetLastError(0xdeadbeef);
341 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
342 ok(!addr2, "VirtualAlloc succeeded\n");
343 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
345 SetLastError(0xdeadbeef);
346 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
347 "VirtualProtect succeeded\n");
348 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
350 SetLastError(0xdeadbeef);
351 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER,
357 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
359 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
361 /* if the type is MEM_RELEASE, size must be 0 */
362 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
363 ok(GetLastError() == ERROR_INVALID_PARAMETER,
364 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
366 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
369 static void test_MapViewOfFile(void)
371 static const char testfile[] = "testfile.xxx";
372 const char *name;
373 HANDLE file, mapping, map2;
374 void *ptr, *ptr2, *addr;
375 MEMORY_BASIC_INFORMATION info;
376 BOOL ret;
378 SetLastError(0xdeadbeef);
379 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
380 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
381 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
382 SetEndOfFile( file );
384 /* read/write mapping */
386 SetLastError(0xdeadbeef);
387 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
388 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
390 SetLastError(0xdeadbeef);
391 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
392 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
393 UnmapViewOfFile( ptr );
395 /* this fails on win9x but succeeds on NT */
396 SetLastError(0xdeadbeef);
397 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
398 if (ptr) UnmapViewOfFile( ptr );
399 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
401 SetLastError(0xdeadbeef);
402 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
403 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
404 UnmapViewOfFile( ptr );
406 SetLastError(0xdeadbeef);
407 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
408 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
409 UnmapViewOfFile( ptr );
411 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
412 FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
413 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
414 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
415 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
416 UnmapViewOfFile( ptr );
417 CloseHandle( map2 );
419 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
420 FILE_MAP_READ, FALSE, 0 );
421 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
422 SetLastError(0xdeadbeef);
423 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
424 if (!ptr)
426 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
427 CloseHandle( map2 );
428 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
429 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
430 SetLastError(0xdeadbeef);
431 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
432 ok( !ptr, "MapViewOfFile succeeded\n" );
433 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
434 CloseHandle( map2 );
435 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
436 FILE_MAP_READ, FALSE, 0 );
437 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
438 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
439 ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
441 else win_skip( "no access checks on win9x\n" );
443 UnmapViewOfFile( ptr );
444 CloseHandle( map2 );
445 CloseHandle( mapping );
447 /* read-only mapping */
449 SetLastError(0xdeadbeef);
450 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
451 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
453 SetLastError(0xdeadbeef);
454 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
455 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
456 UnmapViewOfFile( ptr );
458 /* this fails on win9x but succeeds on NT */
459 SetLastError(0xdeadbeef);
460 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
461 if (ptr) UnmapViewOfFile( ptr );
462 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
464 SetLastError(0xdeadbeef);
465 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
466 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
467 UnmapViewOfFile( ptr );
469 SetLastError(0xdeadbeef);
470 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
471 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
472 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
473 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
474 CloseHandle( mapping );
476 /* copy-on-write mapping */
478 SetLastError(0xdeadbeef);
479 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
480 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
482 SetLastError(0xdeadbeef);
483 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
484 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
485 UnmapViewOfFile( ptr );
487 SetLastError(0xdeadbeef);
488 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
489 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
490 UnmapViewOfFile( ptr );
492 SetLastError(0xdeadbeef);
493 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
494 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
495 UnmapViewOfFile( ptr );
497 SetLastError(0xdeadbeef);
498 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
499 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
500 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
501 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
502 CloseHandle( mapping );
504 /* no access mapping */
506 SetLastError(0xdeadbeef);
507 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
508 /* fails on NT but succeeds on win9x */
509 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
510 else
512 SetLastError(0xdeadbeef);
513 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
514 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
515 UnmapViewOfFile( ptr );
517 SetLastError(0xdeadbeef);
518 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
519 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
520 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
522 SetLastError(0xdeadbeef);
523 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
524 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
525 UnmapViewOfFile( ptr );
527 SetLastError(0xdeadbeef);
528 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
529 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
530 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
532 CloseHandle( mapping );
535 CloseHandle( file );
537 /* now try read-only file */
539 SetLastError(0xdeadbeef);
540 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
541 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
543 SetLastError(0xdeadbeef);
544 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
545 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
546 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
547 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
549 SetLastError(0xdeadbeef);
550 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
551 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
552 CloseHandle( mapping );
554 SetLastError(0xdeadbeef);
555 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
556 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
557 CloseHandle( mapping );
558 CloseHandle( file );
560 /* now try no access file */
562 SetLastError(0xdeadbeef);
563 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
564 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
566 SetLastError(0xdeadbeef);
567 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
568 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
569 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
570 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
572 SetLastError(0xdeadbeef);
573 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
574 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
575 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
576 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
578 SetLastError(0xdeadbeef);
579 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
580 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
581 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
582 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
584 CloseHandle( file );
585 DeleteFileA( testfile );
587 SetLastError(0xdeadbeef);
588 name = "Local\\Foo";
589 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
590 /* nt4 doesn't have Local\\ */
591 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
593 name = "Foo";
594 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
596 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
598 SetLastError(0xdeadbeef);
599 mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
600 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
601 SetLastError(0xdeadbeef);
602 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
603 if (!ptr)
605 SIZE_T size;
606 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
607 SetLastError(0xdeadbeef);
608 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
609 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
610 SetLastError(0xdeadbeef);
611 size = VirtualQuery( ptr, &info, sizeof(info) );
612 ok( size == sizeof(info),
613 "VirtualQuery error %u\n", GetLastError() );
614 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
615 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
616 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
617 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
618 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
619 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
621 else win_skip( "no access checks on win9x\n" );
622 UnmapViewOfFile( ptr );
623 CloseHandle( mapping );
625 SetLastError(0xdeadbeef);
626 mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
627 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
628 SetLastError(0xdeadbeef);
629 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
630 if (!ptr)
632 SIZE_T size;
633 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
634 SetLastError(0xdeadbeef);
635 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
636 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
637 SetLastError(0xdeadbeef);
638 size = VirtualQuery( ptr, &info, sizeof(info) );
639 ok( size == sizeof(info),
640 "VirtualQuery error %u\n", GetLastError() );
641 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
642 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
643 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
644 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
645 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
646 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
648 else win_skip( "no access checks on win9x\n" );
649 UnmapViewOfFile( ptr );
650 CloseHandle( mapping );
652 CloseHandle( file );
654 /* read/write mapping with SEC_RESERVE */
655 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
656 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
658 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
659 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
661 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
662 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
663 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
664 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
666 ret = VirtualQuery(ptr, &info, sizeof(info));
667 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
668 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
669 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
670 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
671 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
672 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
674 ok(info.AllocationProtect == PAGE_NOACCESS,
675 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
676 ok(info.Protect == PAGE_NOACCESS,
677 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
679 else
681 ok(info.AllocationProtect == PAGE_READWRITE,
682 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
683 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
684 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
687 if (ptr != ptr2)
689 ret = VirtualQuery(ptr2, &info, sizeof(info));
690 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
691 ok(info.BaseAddress == ptr2,
692 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
693 ok(info.AllocationBase == ptr2,
694 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
695 ok(info.AllocationProtect == PAGE_READWRITE,
696 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
697 ok(info.RegionSize == MAPPING_SIZE,
698 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
699 ok(info.State == MEM_RESERVE,
700 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
701 ok(info.Protect == 0,
702 "Protect should have been 0 instead of 0x%x\n", info.Protect);
703 ok(info.Type == MEM_MAPPED,
704 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
707 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
708 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
710 ret = VirtualQuery(ptr, &info, sizeof(info));
711 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
712 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
713 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
714 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
715 ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
716 ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
717 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
719 ok(info.AllocationProtect == PAGE_NOACCESS,
720 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
722 else
724 ok(info.AllocationProtect == PAGE_READWRITE,
725 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
726 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
729 /* shows that the VirtualAlloc above affects the mapping, not just the
730 * virtual memory in this process - it also affects all other processes
731 * with a view of the mapping, but that isn't tested here */
732 if (ptr != ptr2)
734 ret = VirtualQuery(ptr2, &info, sizeof(info));
735 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
736 ok(info.BaseAddress == ptr2,
737 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
738 ok(info.AllocationBase == ptr2,
739 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
740 ok(info.AllocationProtect == PAGE_READWRITE,
741 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
742 ok(info.RegionSize == 0x10000,
743 "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
744 ok(info.State == MEM_COMMIT,
745 "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
746 ok(info.Protect == PAGE_READWRITE,
747 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info.Protect);
748 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
751 addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
752 ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
753 "VirtualAlloc failed with error %u\n", GetLastError() );
755 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
756 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
757 if (!ret)
758 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
760 ret = UnmapViewOfFile(ptr2);
761 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
762 ret = UnmapViewOfFile(ptr);
763 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
764 CloseHandle(mapping);
766 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
767 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
769 SetLastError(0xdeadbeef);
770 ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
771 ok( GetLastError() == ERROR_INVALID_ADDRESS,
772 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
773 SetLastError(0xdeadbeef);
774 ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
775 ok( GetLastError() == ERROR_INVALID_ADDRESS,
776 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
777 SetLastError(0xdeadbeef);
778 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779 ok( GetLastError() == ERROR_INVALID_ADDRESS,
780 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
782 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
784 /* close named mapping handle without unmapping */
785 name = "Foo";
786 SetLastError(0xdeadbeef);
787 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
788 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
789 SetLastError(0xdeadbeef);
790 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
791 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
792 SetLastError(0xdeadbeef);
793 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
794 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
795 SetLastError(0xdeadbeef);
796 ret = CloseHandle(map2);
797 ok(ret, "CloseHandle error %d\n", GetLastError());
798 SetLastError(0xdeadbeef);
799 ret = CloseHandle(mapping);
800 ok(ret, "CloseHandle error %d\n", GetLastError());
802 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
803 ok( !ret, "memory is not accessible\n" );
805 ret = VirtualQuery(ptr, &info, sizeof(info));
806 ok(ret, "VirtualQuery error %d\n", GetLastError());
807 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
808 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
809 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
810 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
811 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
812 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
813 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
815 SetLastError(0xdeadbeef);
816 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
817 todo_wine
818 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
819 todo_wine
820 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
821 if (map2) CloseHandle(map2); /* FIXME: remove once Wine is fixed */
822 SetLastError(0xdeadbeef);
823 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
824 ok( mapping != 0, "CreateFileMappingA failed\n" );
825 todo_wine
826 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
827 SetLastError(0xdeadbeef);
828 ret = CloseHandle(mapping);
829 ok(ret, "CloseHandle error %d\n", GetLastError());
831 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
832 ok( !ret, "memory is not accessible\n" );
834 ret = VirtualQuery(ptr, &info, sizeof(info));
835 ok(ret, "VirtualQuery error %d\n", GetLastError());
836 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
837 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
838 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
839 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
840 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
841 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
842 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
844 SetLastError(0xdeadbeef);
845 ret = UnmapViewOfFile(ptr);
846 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
848 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
849 ok( ret, "memory is accessible\n" );
851 ret = VirtualQuery(ptr, &info, sizeof(info));
852 ok(ret, "VirtualQuery error %d\n", GetLastError());
853 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
854 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
855 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
856 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
857 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
858 ok(info.Type == 0, "%#x != 0\n", info.Type);
860 SetLastError(0xdeadbeef);
861 file = CreateFileA(testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
862 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
863 SetFilePointer(file, 4096, NULL, FILE_BEGIN);
864 SetEndOfFile(file);
866 SetLastError(0xdeadbeef);
867 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
868 ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
869 SetLastError(0xdeadbeef);
870 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
871 ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
872 SetLastError(0xdeadbeef);
873 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
874 ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
875 SetLastError(0xdeadbeef);
876 ret = CloseHandle(map2);
877 ok(ret, "CloseHandle error %d\n", GetLastError());
878 SetLastError(0xdeadbeef);
879 ret = CloseHandle(mapping);
880 ok(ret, "CloseHandle error %d\n", GetLastError());
882 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
883 ok( !ret, "memory is not accessible\n" );
885 ret = VirtualQuery(ptr, &info, sizeof(info));
886 ok(ret, "VirtualQuery error %d\n", GetLastError());
887 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
888 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
889 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
890 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
891 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
892 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
893 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
895 SetLastError(0xdeadbeef);
896 map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
897 todo_wine
898 ok( map2 == 0, "OpenFileMappingA succeeded\n" );
899 todo_wine
900 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
901 CloseHandle(map2);
902 SetLastError(0xdeadbeef);
903 mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
904 ok( mapping != 0, "CreateFileMappingA failed\n" );
905 todo_wine
906 ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
907 SetLastError(0xdeadbeef);
908 ret = CloseHandle(mapping);
909 ok(ret, "CloseHandle error %d\n", GetLastError());
911 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
912 ok( !ret, "memory is not accessible\n" );
914 ret = VirtualQuery(ptr, &info, sizeof(info));
915 ok(ret, "VirtualQuery error %d\n", GetLastError());
916 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
917 ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
918 ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
919 ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
920 ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
921 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
922 ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
924 SetLastError(0xdeadbeef);
925 ret = UnmapViewOfFile(ptr);
926 ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
928 ret = IsBadReadPtr(ptr, MAPPING_SIZE);
929 ok( ret, "memory is accessible\n" );
931 ret = VirtualQuery(ptr, &info, sizeof(info));
932 ok(ret, "VirtualQuery error %d\n", GetLastError());
933 ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
934 ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
935 ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
936 ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
937 ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
938 ok(info.Type == 0, "%#x != 0\n", info.Type);
940 CloseHandle(file);
941 DeleteFileA(testfile);
944 static void test_NtMapViewOfSection(void)
946 HANDLE hProcess;
948 static const char testfile[] = "testfile.xxx";
949 static const char data[] = "test data for NtMapViewOfSection";
950 char buffer[sizeof(data)];
951 HANDLE file, mapping;
952 void *ptr;
953 BOOL ret;
954 DWORD status, written;
955 SIZE_T size, result;
956 LARGE_INTEGER offset;
958 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
960 win_skip( "NtMapViewOfSection not available\n" );
961 return;
964 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
965 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
966 WriteFile( file, data, sizeof(data), &written, NULL );
967 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
968 SetEndOfFile( file );
970 /* read/write mapping */
972 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
973 ok( mapping != 0, "CreateFileMapping failed\n" );
975 hProcess = create_target_process("sleep");
976 ok(hProcess != NULL, "Can't start process\n");
978 ptr = NULL;
979 size = 0;
980 offset.QuadPart = 0;
981 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
982 ok( !status, "NtMapViewOfSection failed status %x\n", status );
984 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
985 ok( ret, "ReadProcessMemory failed\n" );
986 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
987 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
989 status = pNtUnmapViewOfSection( hProcess, ptr );
990 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
992 CloseHandle( mapping );
993 CloseHandle( file );
994 DeleteFileA( testfile );
996 TerminateProcess(hProcess, 0);
997 CloseHandle(hProcess);
1000 static void test_NtAreMappedFilesTheSame(void)
1002 static const char testfile[] = "testfile.xxx";
1003 HANDLE file, file2, mapping, map2;
1004 void *ptr, *ptr2;
1005 NTSTATUS status;
1006 char path[MAX_PATH];
1008 if (!pNtAreMappedFilesTheSame)
1010 win_skip( "NtAreMappedFilesTheSame not available\n" );
1011 return;
1014 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1015 NULL, CREATE_ALWAYS, 0, 0 );
1016 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1017 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1018 SetEndOfFile( file );
1020 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1021 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1023 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1024 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1026 file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1027 NULL, OPEN_EXISTING, 0, 0 );
1028 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1030 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
1031 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1032 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1033 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1034 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1035 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1036 UnmapViewOfFile( ptr2 );
1038 ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1039 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1040 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1041 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1042 UnmapViewOfFile( ptr2 );
1043 CloseHandle( map2 );
1045 map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1046 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1047 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1048 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1049 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1050 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1051 UnmapViewOfFile( ptr2 );
1052 CloseHandle( map2 );
1053 CloseHandle( file2 );
1055 status = pNtAreMappedFilesTheSame( ptr, ptr );
1056 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1058 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
1059 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1061 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1062 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1064 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
1065 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
1066 "NtAreMappedFilesTheSame returned %x\n", status );
1068 status = pNtAreMappedFilesTheSame( ptr, NULL );
1069 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1071 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
1072 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1074 status = pNtAreMappedFilesTheSame( NULL, NULL );
1075 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1077 ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
1078 ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
1079 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1080 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1081 VirtualFree( ptr2, 0, MEM_RELEASE );
1083 UnmapViewOfFile( ptr );
1084 CloseHandle( mapping );
1085 CloseHandle( file );
1087 status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1088 GetModuleHandleA("kernel32.dll") );
1089 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1090 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1091 GetModuleHandleA("kernel32.dll") );
1092 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1093 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1094 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1095 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1097 GetSystemDirectoryA( path, MAX_PATH );
1098 strcat( path, "\\kernel32.dll" );
1099 file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1100 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1102 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1103 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1104 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1105 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1106 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1107 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1108 UnmapViewOfFile( ptr );
1109 CloseHandle( mapping );
1111 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1112 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1113 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1114 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1115 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1116 todo_wine
1117 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1119 file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1120 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1121 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1122 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1123 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
1124 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1125 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1126 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1127 UnmapViewOfFile( ptr2 );
1128 CloseHandle( map2 );
1129 CloseHandle( file2 );
1131 UnmapViewOfFile( ptr );
1132 CloseHandle( mapping );
1134 CloseHandle( file );
1135 DeleteFileA( testfile );
1138 static void test_CreateFileMapping(void)
1140 HANDLE handle, handle2;
1142 /* test case sensitivity */
1144 SetLastError(0xdeadbeef);
1145 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1146 "Wine Test Mapping");
1147 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1148 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1150 SetLastError(0xdeadbeef);
1151 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1152 "Wine Test Mapping");
1153 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1154 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
1155 CloseHandle( handle2 );
1157 SetLastError(0xdeadbeef);
1158 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1159 "WINE TEST MAPPING");
1160 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1161 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1162 CloseHandle( handle2 );
1164 SetLastError(0xdeadbeef);
1165 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1166 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1167 CloseHandle( handle2 );
1169 SetLastError(0xdeadbeef);
1170 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1171 ok( !handle2, "OpenFileMapping succeeded\n");
1172 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1173 "wrong error %u\n", GetLastError());
1175 CloseHandle( handle );
1178 static void test_IsBadReadPtr(void)
1180 BOOL ret;
1181 void *ptr = (void *)0xdeadbeef;
1182 char stackvar;
1184 ret = IsBadReadPtr(NULL, 0);
1185 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1187 ret = IsBadReadPtr(NULL, 1);
1188 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1190 ret = IsBadReadPtr(ptr, 0);
1191 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1193 ret = IsBadReadPtr(ptr, 1);
1194 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1196 ret = IsBadReadPtr(&stackvar, 0);
1197 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1199 ret = IsBadReadPtr(&stackvar, sizeof(char));
1200 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1203 static void test_IsBadWritePtr(void)
1205 BOOL ret;
1206 void *ptr = (void *)0xdeadbeef;
1207 char stackval;
1209 ret = IsBadWritePtr(NULL, 0);
1210 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1212 ret = IsBadWritePtr(NULL, 1);
1213 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1215 ret = IsBadWritePtr(ptr, 0);
1216 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1218 ret = IsBadWritePtr(ptr, 1);
1219 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1221 ret = IsBadWritePtr(&stackval, 0);
1222 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1224 ret = IsBadWritePtr(&stackval, sizeof(char));
1225 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1228 static void test_IsBadCodePtr(void)
1230 BOOL ret;
1231 void *ptr = (void *)0xdeadbeef;
1232 char stackval;
1234 ret = IsBadCodePtr(NULL);
1235 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1237 ret = IsBadCodePtr(ptr);
1238 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1240 ret = IsBadCodePtr((void *)&stackval);
1241 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1244 static void test_write_watch(void)
1246 char *base;
1247 DWORD ret, size, old_prot;
1248 MEMORY_BASIC_INFORMATION info;
1249 void *results[64];
1250 ULONG_PTR count;
1251 ULONG pagesize;
1253 if (!pGetWriteWatch || !pResetWriteWatch)
1255 win_skip( "GetWriteWatch not supported\n" );
1256 return;
1259 size = 0x10000;
1260 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1261 if (!base &&
1262 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1264 win_skip( "MEM_WRITE_WATCH not supported\n" );
1265 return;
1267 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1268 ret = VirtualQuery( base, &info, sizeof(info) );
1269 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1270 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1271 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1272 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1273 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1274 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1275 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1277 count = 64;
1278 SetLastError( 0xdeadbeef );
1279 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1280 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1281 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1282 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1283 "wrong error %u\n", GetLastError() );
1285 SetLastError( 0xdeadbeef );
1286 ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
1287 if (ret)
1289 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1290 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1292 else /* win98 */
1294 ok( count == 0, "wrong count %lu\n", count );
1297 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1298 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1299 ok( count == 0, "wrong count %lu\n", count );
1301 base[pagesize + 1] = 0x44;
1303 count = 64;
1304 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1305 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1306 ok( count == 1, "wrong count %lu\n", count );
1307 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1309 count = 64;
1310 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, 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( 0, base, size, results, &count, &pagesize );
1317 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1318 ok( count == 0, "wrong count %lu\n", count );
1320 base[2*pagesize + 3] = 0x11;
1321 base[4*pagesize + 8] = 0x11;
1323 count = 64;
1324 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1325 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1326 ok( count == 2, "wrong count %lu\n", count );
1327 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1328 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1330 count = 64;
1331 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1332 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1333 ok( count == 1, "wrong count %lu\n", count );
1334 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1336 ret = pResetWriteWatch( base, 3*pagesize );
1337 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1339 count = 64;
1340 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1341 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1342 ok( count == 1, "wrong count %lu\n", count );
1343 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1345 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1347 count = 64;
1348 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1349 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1350 ok( count == 3, "wrong count %lu\n", count );
1351 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1352 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1353 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1355 count = 1;
1356 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1357 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1358 ok( count == 1, "wrong count %lu\n", count );
1359 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1361 count = 64;
1362 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1363 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1364 ok( count == 2, "wrong count %lu\n", count );
1365 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1366 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1368 /* changing protections doesn't affect watches */
1370 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1371 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1372 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1374 ret = VirtualQuery( base, &info, sizeof(info) );
1375 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1376 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1377 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1378 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1379 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1381 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1382 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1383 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1385 count = 64;
1386 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1387 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1388 ok( count == 2, "wrong count %lu\n", count );
1389 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1390 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1392 ret = VirtualQuery( base, &info, sizeof(info) );
1393 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1394 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1395 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1396 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1397 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1399 /* some invalid parameter tests */
1401 SetLastError( 0xdeadbeef );
1402 count = 0;
1403 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1404 if (ret)
1406 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1407 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1409 SetLastError( 0xdeadbeef );
1410 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1411 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1412 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1414 SetLastError( 0xdeadbeef );
1415 count = 64;
1416 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
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, NULL, &count, &pagesize );
1423 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1424 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1426 SetLastError( 0xdeadbeef );
1427 count = 0;
1428 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1429 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1430 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1432 SetLastError( 0xdeadbeef );
1433 count = 64;
1434 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &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( 0, base, 0, 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, size * 2, 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 - pagesize, pagesize + 1, 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 ret = pResetWriteWatch( base, 0 );
1458 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1459 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1461 SetLastError( 0xdeadbeef );
1462 ret = pResetWriteWatch( GetModuleHandle(0), size );
1463 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1464 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1466 else /* win98 is completely different */
1468 SetLastError( 0xdeadbeef );
1469 count = 64;
1470 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1471 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1472 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1474 count = 0;
1475 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1476 ok( !ret, "GetWriteWatch failed %u\n", ret );
1478 count = 64;
1479 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1480 ok( !ret, "GetWriteWatch failed %u\n", ret );
1482 count = 64;
1483 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1484 ok( !ret, "GetWriteWatch failed %u\n", ret );
1486 ret = pResetWriteWatch( base, 0 );
1487 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1489 ret = pResetWriteWatch( GetModuleHandle(0), size );
1490 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1493 VirtualFree( base, 0, MEM_FREE );
1495 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1496 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1497 VirtualFree( base, 0, MEM_FREE );
1499 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1500 ok( !base, "VirtualAlloc succeeded\n" );
1501 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1503 /* initial protect doesn't matter */
1505 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1506 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1507 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1508 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1510 count = 64;
1511 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1512 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1513 ok( count == 0, "wrong count %lu\n", count );
1515 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1516 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1517 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1519 base[5*pagesize + 200] = 3;
1521 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1522 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1523 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1525 count = 64;
1526 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1527 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1528 ok( count == 1, "wrong count %lu\n", count );
1529 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1531 ret = VirtualFree( base, size, MEM_DECOMMIT );
1532 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1534 count = 64;
1535 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1536 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1537 ok( count == 1 || broken(count == 0), /* win98 */
1538 "wrong count %lu\n", count );
1539 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1541 VirtualFree( base, 0, MEM_FREE );
1544 static void test_VirtualProtect(void)
1546 static const struct test_data
1548 DWORD prot_set, prot_get;
1549 } td[] =
1551 { 0, 0 }, /* 0x00 */
1552 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
1553 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
1554 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
1555 { PAGE_READWRITE, PAGE_READWRITE }, /* 0x04 */
1556 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
1557 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
1558 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
1559 { PAGE_WRITECOPY, 0 }, /* 0x08 */
1560 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
1561 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
1562 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
1563 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
1564 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
1565 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
1566 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
1568 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
1569 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
1570 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
1571 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1572 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
1573 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
1574 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
1575 { PAGE_EXECUTE_WRITECOPY, 0 }, /* 0x80 */
1576 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
1577 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
1578 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
1579 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
1580 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
1581 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
1582 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
1584 char *base;
1585 DWORD ret, old_prot, rw_prot, exec_prot, i, j;
1586 MEMORY_BASIC_INFORMATION info;
1587 SYSTEM_INFO si;
1589 GetSystemInfo(&si);
1590 trace("system page size %#x\n", si.dwPageSize);
1592 SetLastError(0xdeadbeef);
1593 base = VirtualAlloc(0, si.dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
1594 ok(base != NULL, "VirtualAlloc failed %d\n", GetLastError());
1596 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1598 SetLastError(0xdeadbeef);
1599 ret = VirtualQuery(base, &info, sizeof(info));
1600 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1601 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1602 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1603 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1604 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1605 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1606 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1607 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1609 old_prot = 0xdeadbeef;
1610 SetLastError(0xdeadbeef);
1611 ret = VirtualProtect(base, si.dwPageSize, td[i].prot_set, &old_prot);
1612 if (td[i].prot_get)
1614 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1615 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1617 SetLastError(0xdeadbeef);
1618 ret = VirtualQuery(base, &info, sizeof(info));
1619 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1620 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1621 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1622 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
1623 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1624 ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1625 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1626 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1628 else
1630 ok(!ret, "%d: VirtualProtect should fail\n", i);
1631 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1634 old_prot = 0xdeadbeef;
1635 SetLastError(0xdeadbeef);
1636 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1637 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1638 if (td[i].prot_get)
1639 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
1640 else
1641 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1644 exec_prot = 0;
1646 for (i = 0; i <= 4; i++)
1648 rw_prot = 0;
1650 for (j = 0; j <= 4; j++)
1652 DWORD prot = exec_prot | rw_prot;
1654 SetLastError(0xdeadbeef);
1655 ret = VirtualProtect(base, si.dwPageSize, prot, &old_prot);
1656 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1658 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1659 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1661 else
1663 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
1665 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1666 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1668 else
1669 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
1672 rw_prot = 1 << j;
1675 exec_prot = 1 << (i + 4);
1678 VirtualFree(base, 0, MEM_FREE);
1681 static BOOL is_mem_writable(DWORD prot)
1683 switch (prot & 0xff)
1685 case PAGE_READWRITE:
1686 case PAGE_WRITECOPY:
1687 case PAGE_EXECUTE_READWRITE:
1688 case PAGE_EXECUTE_WRITECOPY:
1689 return TRUE;
1691 default:
1692 return FALSE;
1696 static void test_VirtualAlloc_protection(void)
1698 static const struct test_data
1700 DWORD prot;
1701 BOOL success;
1702 } td[] =
1704 { 0, FALSE }, /* 0x00 */
1705 { PAGE_NOACCESS, TRUE }, /* 0x01 */
1706 { PAGE_READONLY, TRUE }, /* 0x02 */
1707 { PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x03 */
1708 { PAGE_READWRITE, TRUE }, /* 0x04 */
1709 { PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x05 */
1710 { PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x06 */
1711 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x07 */
1712 { PAGE_WRITECOPY, FALSE }, /* 0x08 */
1713 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE }, /* 0x09 */
1714 { PAGE_WRITECOPY | PAGE_READONLY, FALSE }, /* 0x0a */
1715 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE }, /* 0x0b */
1716 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE }, /* 0x0c */
1717 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x0d */
1718 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x0e */
1719 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x0f */
1721 { PAGE_EXECUTE, TRUE }, /* 0x10 */
1722 { PAGE_EXECUTE_READ, TRUE }, /* 0x20 */
1723 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x30 */
1724 { PAGE_EXECUTE_READWRITE, TRUE }, /* 0x40 */
1725 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0x50 */
1726 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0x60 */
1727 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x70 */
1728 { PAGE_EXECUTE_WRITECOPY, FALSE }, /* 0x80 */
1729 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE }, /* 0x90 */
1730 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE }, /* 0xa0 */
1731 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0xb0 */
1732 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE }, /* 0xc0 */
1733 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0xd0 */
1734 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0xe0 */
1735 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE } /* 0xf0 */
1737 char *base;
1738 DWORD ret, i;
1739 MEMORY_BASIC_INFORMATION info;
1740 SYSTEM_INFO si;
1742 GetSystemInfo(&si);
1743 trace("system page size %#x\n", si.dwPageSize);
1745 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1747 SetLastError(0xdeadbeef);
1748 base = VirtualAlloc(0, si.dwPageSize, MEM_COMMIT, td[i].prot);
1750 if (td[i].success)
1752 ok(base != NULL, "%d: VirtualAlloc failed %d\n", i, GetLastError());
1754 SetLastError(0xdeadbeef);
1755 ret = VirtualQuery(base, &info, sizeof(info));
1756 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1757 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1758 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1759 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1760 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1761 ok(info.AllocationProtect == td[i].prot, "%d: %#x != %#x\n", i, info.AllocationProtect, td[i].prot);
1762 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1763 ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1765 if (is_mem_writable(info.Protect))
1767 base[0] = 0xfe;
1769 SetLastError(0xdeadbeef);
1770 ret = VirtualQuery(base, &info, sizeof(info));
1771 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1772 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1775 VirtualFree(base, 0, MEM_FREE);
1777 else
1779 ok(!base, "%d: VirtualAlloc should fail\n", i);
1780 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1785 static void test_CreateFileMapping_protection(void)
1787 static const struct test_data
1789 DWORD prot;
1790 BOOL success;
1791 DWORD prot_after_write;
1792 } td[] =
1794 { 0, FALSE, 0 }, /* 0x00 */
1795 { PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x01 */
1796 { PAGE_READONLY, TRUE, PAGE_READONLY }, /* 0x02 */
1797 { PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x03 */
1798 { PAGE_READWRITE, TRUE, PAGE_READWRITE }, /* 0x04 */
1799 { PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x05 */
1800 { PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x06 */
1801 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x07 */
1802 { PAGE_WRITECOPY, TRUE, PAGE_READWRITE }, /* 0x08 */
1803 { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x09 */
1804 { PAGE_WRITECOPY | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0a */
1805 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0b */
1806 { PAGE_WRITECOPY | PAGE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0x0c */
1807 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0d */
1808 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0e */
1809 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0f */
1811 { PAGE_EXECUTE, FALSE, PAGE_EXECUTE }, /* 0x10 */
1812 { PAGE_EXECUTE_READ, TRUE, PAGE_EXECUTE_READ }, /* 0x20 */
1813 { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_EXECUTE_READ }, /* 0x30 */
1814 { PAGE_EXECUTE_READWRITE, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1815 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x50 */
1816 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0x60 */
1817 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x70 */
1818 { PAGE_EXECUTE_WRITECOPY, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x80 */
1819 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x90 */
1820 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xa0 */
1821 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xb0 */
1822 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0xc0 */
1823 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xd0 */
1824 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xe0 */
1825 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS } /* 0xf0 */
1827 char *base;
1828 DWORD ret, i, alloc_prot, prot, old_prot;
1829 MEMORY_BASIC_INFORMATION info;
1830 SYSTEM_INFO si;
1831 char temp_path[MAX_PATH];
1832 char file_name[MAX_PATH];
1833 HANDLE hfile, hmap;
1834 BOOL page_exec_supported = TRUE;
1836 GetSystemInfo(&si);
1837 trace("system page size %#x\n", si.dwPageSize);
1839 GetTempPath(MAX_PATH, temp_path);
1840 GetTempFileName(temp_path, "map", 0, file_name);
1842 SetLastError(0xdeadbeef);
1843 hfile = CreateFile(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
1844 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
1845 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
1846 SetEndOfFile(hfile);
1848 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1850 SetLastError(0xdeadbeef);
1851 hmap = CreateFileMapping(hfile, NULL, td[i].prot | SEC_COMMIT, 0, si.dwPageSize, NULL);
1853 if (td[i].success)
1855 if (!hmap)
1857 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, td[i].prot, GetLastError());
1858 /* NT4 and win2k don't support EXEC on file mappings */
1859 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
1861 page_exec_supported = FALSE;
1862 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
1863 continue;
1865 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1866 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1868 page_exec_supported = FALSE;
1869 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
1870 continue;
1873 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, td[i].prot, GetLastError());
1875 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
1876 ok(base != NULL, "%d: MapViewOfFile failed %d\n", i, GetLastError());
1878 SetLastError(0xdeadbeef);
1879 ret = VirtualQuery(base, &info, sizeof(info));
1880 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1881 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1882 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1883 ok(info.Protect == PAGE_READONLY, "%d: got %#x != expected PAGE_READONLY\n", i, info.Protect);
1884 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1885 ok(info.AllocationProtect == PAGE_READONLY, "%d: %#x != PAGE_READONLY\n", i, info.AllocationProtect);
1886 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1887 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1889 if (is_mem_writable(info.Protect))
1891 base[0] = 0xfe;
1893 SetLastError(0xdeadbeef);
1894 ret = VirtualQuery(base, &info, sizeof(info));
1895 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1896 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1899 UnmapViewOfFile(base);
1900 CloseHandle(hmap);
1902 else
1904 ok(!hmap, "%d: CreateFileMapping should fail\n", i);
1905 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1909 if (page_exec_supported) alloc_prot = PAGE_EXECUTE_READWRITE;
1910 else alloc_prot = PAGE_READWRITE;
1911 SetLastError(0xdeadbeef);
1912 hmap = CreateFileMapping(hfile, NULL, alloc_prot, 0, si.dwPageSize, NULL);
1913 ok(hmap != 0, "%d: CreateFileMapping error %d\n", i, GetLastError());
1915 SetLastError(0xdeadbeef);
1916 base = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE | (page_exec_supported ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
1917 ok(base != NULL, "MapViewOfFile failed %d\n", GetLastError());
1919 old_prot = 0xdeadbeef;
1920 SetLastError(0xdeadbeef);
1921 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1922 ok(ret, "VirtualProtect error %d\n", GetLastError());
1923 ok(old_prot == alloc_prot, "got %#x != expected %#x\n", old_prot, alloc_prot);
1925 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1927 SetLastError(0xdeadbeef);
1928 ret = VirtualQuery(base, &info, sizeof(info));
1929 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1930 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1931 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1932 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1933 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1934 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
1935 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1936 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1938 old_prot = 0xdeadbeef;
1939 SetLastError(0xdeadbeef);
1940 ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
1941 if (td[i].success || td[i].prot == PAGE_NOACCESS || td[i].prot == PAGE_EXECUTE)
1943 if (!ret)
1945 /* win2k and XP don't support EXEC on file mappings */
1946 if (td[i].prot == PAGE_EXECUTE)
1948 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
1949 continue;
1951 /* NT4 and win2k don't support EXEC on file mappings */
1952 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
1954 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
1955 continue;
1957 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1958 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1960 ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
1961 continue;
1965 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1966 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1968 prot = td[i].prot;
1969 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
1970 if (prot == PAGE_EXECUTE_WRITECOPY) prot = PAGE_EXECUTE_READWRITE;
1972 SetLastError(0xdeadbeef);
1973 ret = VirtualQuery(base, &info, sizeof(info));
1974 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1975 ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1976 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1977 /* FIXME: remove the condition below once Wine is fixed */
1978 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1979 todo_wine ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
1980 else
1981 ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
1982 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1983 ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
1984 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1985 ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1987 if (is_mem_writable(info.Protect))
1989 base[0] = 0xfe;
1991 SetLastError(0xdeadbeef);
1992 ret = VirtualQuery(base, &info, sizeof(info));
1993 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1994 /* FIXME: remove the condition below once Wine is fixed */
1995 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
1996 todo_wine ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
1997 else
1998 ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2001 else
2003 ok(!ret, "%d: VirtualProtect should fail\n", i);
2004 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2005 continue;
2008 old_prot = 0xdeadbeef;
2009 SetLastError(0xdeadbeef);
2010 ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2011 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2012 /* FIXME: remove the condition below once Wine is fixed */
2013 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2014 todo_wine ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2015 else
2016 ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2019 UnmapViewOfFile(base);
2020 CloseHandle(hmap);
2022 CloseHandle(hfile);
2023 DeleteFile(file_name);
2026 #define ACCESS_READ 0x01
2027 #define ACCESS_WRITE 0x02
2028 #define ACCESS_EXECUTE 0x04
2029 #define ACCESS_WRITECOPY 0x08
2031 static DWORD page_prot_to_access(DWORD prot)
2033 switch (prot)
2035 case PAGE_READWRITE:
2036 return ACCESS_READ | ACCESS_WRITE;
2038 case PAGE_EXECUTE:
2039 case PAGE_EXECUTE_READ:
2040 return ACCESS_READ | ACCESS_EXECUTE;
2042 case PAGE_EXECUTE_READWRITE:
2043 return ACCESS_READ | ACCESS_WRITE | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2045 case PAGE_EXECUTE_WRITECOPY:
2046 return ACCESS_READ | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2048 case PAGE_READONLY:
2049 return ACCESS_READ;
2051 case PAGE_WRITECOPY:
2052 return ACCESS_READ;
2054 default:
2055 return 0;
2059 static BOOL is_compatible_protection(DWORD map_prot, DWORD view_prot, DWORD prot)
2061 DWORD map_access, view_access, prot_access;
2063 map_access = page_prot_to_access(map_prot);
2064 view_access = page_prot_to_access(view_prot);
2065 prot_access = page_prot_to_access(prot);
2067 if (view_access == prot_access) return TRUE;
2068 if (!view_access) return FALSE;
2070 if ((view_access & prot_access) != prot_access) return FALSE;
2071 if ((map_access & prot_access) == prot_access) return TRUE;
2073 return FALSE;
2076 static DWORD map_prot_to_access(DWORD prot)
2078 switch (prot)
2080 case PAGE_READWRITE:
2081 case PAGE_EXECUTE_READWRITE:
2082 return SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2083 case PAGE_READONLY:
2084 case PAGE_WRITECOPY:
2085 case PAGE_EXECUTE:
2086 case PAGE_EXECUTE_READ:
2087 case PAGE_EXECUTE_WRITECOPY:
2088 return SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2089 default:
2090 return 0;
2094 static BOOL is_compatible_access(DWORD map_prot, DWORD view_prot)
2096 DWORD access = map_prot_to_access(map_prot);
2097 if (!view_prot) view_prot = SECTION_MAP_READ;
2098 return (view_prot & access) == view_prot;
2101 static void *map_view_of_file(HANDLE handle, DWORD access)
2103 NTSTATUS status;
2104 LARGE_INTEGER offset;
2105 SIZE_T count;
2106 ULONG protect;
2107 BOOL exec;
2108 void *addr;
2110 if (!pNtMapViewOfSection) return NULL;
2112 count = 0;
2113 offset.u.LowPart = 0;
2114 offset.u.HighPart = 0;
2116 exec = access & FILE_MAP_EXECUTE;
2117 access &= ~FILE_MAP_EXECUTE;
2119 if (access == FILE_MAP_COPY)
2121 if (exec)
2122 protect = PAGE_EXECUTE_WRITECOPY;
2123 else
2124 protect = PAGE_WRITECOPY;
2126 else if (access & FILE_MAP_WRITE)
2128 if (exec)
2129 protect = PAGE_EXECUTE_READWRITE;
2130 else
2131 protect = PAGE_READWRITE;
2133 else if (access & FILE_MAP_READ)
2135 if (exec)
2136 protect = PAGE_EXECUTE_READ;
2137 else
2138 protect = PAGE_READONLY;
2140 else protect = PAGE_NOACCESS;
2142 addr = NULL;
2143 status = pNtMapViewOfSection(handle, GetCurrentProcess(), &addr, 0, 0, &offset,
2144 &count, 1 /* ViewShare */, 0, protect);
2145 if (status)
2147 /* for simplicity */
2148 SetLastError(ERROR_ACCESS_DENIED);
2149 addr = NULL;
2151 return addr;
2154 static void test_mapping(void)
2156 static const DWORD page_prot[] =
2158 PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY,
2159 PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY
2161 static const struct
2163 DWORD access, prot;
2164 } view[] =
2166 { 0, PAGE_NOACCESS }, /* 0x00 */
2167 { FILE_MAP_COPY, PAGE_WRITECOPY }, /* 0x01 */
2168 { FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x02 */
2169 { FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x03 */
2170 { FILE_MAP_READ, PAGE_READONLY }, /* 0x04 */
2171 { FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x05 */
2172 { FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x06 */
2173 { FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x07 */
2174 { SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x08 */
2175 { SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x09 */
2176 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0a */
2177 { SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0b */
2178 { SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_READONLY }, /* 0x0c */
2179 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x0d */
2180 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0e */
2181 { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0f */
2182 { FILE_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x20 */
2183 { FILE_MAP_EXECUTE | FILE_MAP_COPY, PAGE_EXECUTE_WRITECOPY }, /* 0x21 */
2184 { FILE_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x22 */
2185 { FILE_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x23 */
2186 { FILE_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x24 */
2187 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x25 */
2188 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x26 */
2189 { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x27 */
2190 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x28 */
2191 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x29 */
2192 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2a */
2193 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x2b */
2194 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x2c */
2195 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x2d */
2196 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2e */
2197 { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE } /* 0x2f */
2199 void *base, *nt_base;
2200 DWORD i, j, k, ret, old_prot, prev_prot;
2201 SYSTEM_INFO si;
2202 char temp_path[MAX_PATH];
2203 char file_name[MAX_PATH];
2204 HANDLE hfile, hmap;
2205 MEMORY_BASIC_INFORMATION info, nt_info;
2207 GetSystemInfo(&si);
2208 trace("system page size %#x\n", si.dwPageSize);
2210 GetTempPath(MAX_PATH, temp_path);
2211 GetTempFileName(temp_path, "map", 0, file_name);
2213 SetLastError(0xdeadbeef);
2214 hfile = CreateFile(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
2215 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
2216 SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
2217 SetEndOfFile(hfile);
2219 for (i = 0; i < sizeof(page_prot)/sizeof(page_prot[0]); i++)
2221 SetLastError(0xdeadbeef);
2222 hmap = CreateFileMapping(hfile, NULL, page_prot[i] | SEC_COMMIT, 0, si.dwPageSize, NULL);
2224 if (page_prot[i] == PAGE_NOACCESS)
2226 HANDLE hmap2;
2228 ok(!hmap, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2229 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2231 /* A trick to create a not accessible mapping */
2232 SetLastError(0xdeadbeef);
2233 hmap = CreateFileMapping(hfile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, si.dwPageSize, NULL);
2234 ok(hmap != 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2235 SetLastError(0xdeadbeef);
2236 ret = DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, FALSE, 0);
2237 ok(ret, "DuplicateHandle error %d\n", GetLastError());
2238 CloseHandle(hmap);
2239 hmap = hmap2;
2242 if (!hmap)
2244 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, page_prot[i], GetLastError());
2246 /* NT4 and win2k don't support EXEC on file mappings */
2247 if (page_prot[i] == PAGE_EXECUTE_READ || page_prot[i] == PAGE_EXECUTE_READWRITE)
2249 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
2250 continue;
2252 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2253 if (page_prot[i] == PAGE_EXECUTE_WRITECOPY)
2255 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2256 continue;
2260 ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, page_prot[i], GetLastError());
2262 for (j = 0; j < sizeof(view)/sizeof(view[0]); j++)
2264 nt_base = map_view_of_file(hmap, view[j].access);
2265 if (nt_base)
2267 SetLastError(0xdeadbeef);
2268 ret = VirtualQuery(nt_base, &nt_info, sizeof(nt_info));
2269 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2270 UnmapViewOfFile(nt_base);
2273 SetLastError(0xdeadbeef);
2274 base = MapViewOfFile(hmap, view[j].access, 0, 0, 0);
2276 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
2277 ok(!nt_base == !base ||
2278 broken((view[j].access & FILE_MAP_EXECUTE) && !nt_base != !base),
2279 "%d: (%04x/%04x) NT %p kernel %p\n", j, page_prot[i], view[j].access, nt_base, base);
2281 if (!is_compatible_access(page_prot[i], view[j].access))
2283 ok(!base, "%d: MapViewOfFile(%04x/%04x) should fail\n", j, page_prot[i], view[j].access);
2284 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
2285 continue;
2288 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
2289 if (!base && (view[j].access & FILE_MAP_EXECUTE))
2291 ok(broken(!base), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2292 continue;
2295 ok(base != NULL, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2297 SetLastError(0xdeadbeef);
2298 ret = VirtualQuery(base, &info, sizeof(info));
2299 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2300 ok(info.BaseAddress == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.BaseAddress, base);
2301 ok(info.RegionSize == si.dwPageSize, "%d: (%04x) got %#lx != expected %#x\n", j, view[j].access, info.RegionSize, si.dwPageSize);
2302 ok(info.Protect == view[j].prot ||
2303 broken(view[j].prot == PAGE_EXECUTE_READ && info.Protect == PAGE_READONLY) || /* win2k */
2304 broken(view[j].prot == PAGE_EXECUTE_READWRITE && info.Protect == PAGE_READWRITE) || /* win2k */
2305 broken(view[j].prot == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2306 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.Protect, view[j].prot);
2307 ok(info.AllocationBase == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.AllocationBase, base);
2308 ok(info.AllocationProtect == info.Protect, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.AllocationProtect, info.Protect);
2309 ok(info.State == MEM_COMMIT, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j, view[j].access, info.State);
2310 ok(info.Type == MEM_MAPPED, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j, view[j].access, info.Type);
2312 if (nt_base && base)
2314 ok(nt_info.RegionSize == info.RegionSize, "%d: (%04x) got %#lx != expected %#lx\n", j, view[j].access, nt_info.RegionSize, info.RegionSize);
2315 ok(nt_info.Protect == info.Protect /* Vista+ */ ||
2316 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2317 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Protect, info.Protect);
2318 ok(nt_info.AllocationProtect == info.AllocationProtect /* Vista+ */ ||
2319 broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2320 "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.AllocationProtect, info.AllocationProtect);
2321 ok(nt_info.State == info.State, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.State, info.State);
2322 ok(nt_info.Type == info.Type, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Type, info.Type);
2325 prev_prot = info.Protect;
2327 for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
2329 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2330 SetLastError(0xdeadbeef);
2331 old_prot = 0xdeadbeef;
2332 ret = VirtualProtect(base, si.dwPageSize, page_prot[k], &old_prot);
2333 if (is_compatible_protection(page_prot[i], view[j].prot, page_prot[k]))
2335 /* win2k and XP don't support EXEC on file mappings */
2336 if (!ret && page_prot[k] == PAGE_EXECUTE)
2338 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2339 continue;
2341 /* NT4 and win2k don't support EXEC on file mappings */
2342 if (!ret && (page_prot[k] == PAGE_EXECUTE_READ || page_prot[k] == PAGE_EXECUTE_READWRITE))
2344 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2345 continue;
2347 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2348 if (!ret && page_prot[k] == PAGE_EXECUTE_WRITECOPY)
2350 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
2351 continue;
2353 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
2354 if (!ret && view[j].prot == PAGE_EXECUTE_WRITECOPY)
2356 ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
2357 continue;
2360 ok(ret, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot[i], view[j].prot, page_prot[k]);
2361 ok(old_prot == prev_prot, "got %#x, expected %#x\n", old_prot, prev_prot);
2362 prev_prot = page_prot[k];
2364 else
2366 /* NT4 doesn't fail on incompatible map and view */
2367 if (ret)
2369 ok(broken(ret), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2370 skip("Incompatible map and view are not properly handled on this platform\n");
2371 break; /* NT4 won't pass remaining tests */
2374 ok(!ret, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2375 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2379 UnmapViewOfFile(base);
2382 CloseHandle(hmap);
2385 CloseHandle(hfile);
2386 DeleteFile(file_name);
2389 START_TEST(virtual)
2391 int argc;
2392 char **argv;
2393 argc = winetest_get_mainargs( &argv );
2395 if (argc >= 3)
2397 if (!strcmp(argv[2], "sleep"))
2399 Sleep(5000); /* spawned process runs for at most 5 seconds */
2400 return;
2402 while (1)
2404 void *mem;
2405 BOOL ret;
2406 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
2407 PAGE_EXECUTE_READWRITE);
2408 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
2409 if (mem == NULL) break;
2410 ret = VirtualFree(mem, 0, MEM_RELEASE);
2411 ok(ret, "VirtualFree failed %u\n", GetLastError());
2412 if (!ret) break;
2414 return;
2417 hkernel32 = GetModuleHandleA("kernel32.dll");
2418 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
2419 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
2420 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
2421 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
2422 pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"),
2423 "NtAreMappedFilesTheSame" );
2424 pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtMapViewOfSection");
2425 pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection");
2427 test_mapping();
2428 test_CreateFileMapping_protection();
2429 test_VirtualAlloc_protection();
2430 test_VirtualProtect();
2431 test_VirtualAllocEx();
2432 test_VirtualAlloc();
2433 test_MapViewOfFile();
2434 test_NtMapViewOfSection();
2435 test_NtAreMappedFilesTheSame();
2436 test_CreateFileMapping();
2437 test_IsBadReadPtr();
2438 test_IsBadWritePtr();
2439 test_IsBadCodePtr();
2440 test_write_watch();