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
25 #define WIN32_NO_STATUS
30 #include "wine/test.h"
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);
42 /* ############################### */
44 static HANDLE create_target_process(const char *arg)
47 char cmdline[MAX_PATH];
48 PROCESS_INFORMATION pi;
49 STARTUPINFO si = { 0 };
52 winetest_get_mainargs( &argv );
53 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
54 ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
55 &si, &pi) != 0, "error: %u\n", GetLastError());
56 ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
60 static void test_VirtualAllocEx(void)
62 const unsigned int alloc_size = 1<<15;
64 SIZE_T bytes_written = 0, bytes_read = 0, i;
68 MEMORY_BASIC_INFORMATION info;
71 /* not exported in all windows-versions */
72 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
73 win_skip("Virtual{Alloc,Free}Ex not available\n");
77 hProcess = create_target_process("sleep");
78 ok(hProcess != NULL, "Can't start process\n");
80 SetLastError(0xdeadbeef);
81 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
82 PAGE_EXECUTE_READWRITE);
83 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
85 win_skip("VirtualAllocEx not implemented\n");
86 TerminateProcess(hProcess, 0);
87 CloseHandle(hProcess);
91 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
92 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
93 for (i = 0; i < alloc_size; i++)
96 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
97 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
98 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
100 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
101 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
102 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
104 /* test invalid source buffers */
106 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
107 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
108 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
109 ok( !b, "WriteProcessMemory succeeded\n" );
110 ok( GetLastError() == ERROR_NOACCESS ||
111 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
112 "wrong error %u\n", GetLastError() );
113 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
114 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
115 ok( !b, "ReadProcessMemory succeeded\n" );
116 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
117 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
119 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
120 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
121 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
122 ok( !b, "WriteProcessMemory succeeded\n" );
123 ok( GetLastError() == ERROR_NOACCESS ||
124 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
125 "wrong error %u\n", GetLastError() );
126 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
127 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
128 ok( !b, "ReadProcessMemory succeeded\n" );
129 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
130 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
132 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
133 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
135 VirtualFree( src, 0, MEM_FREE );
136 VirtualFree( dst, 0, MEM_FREE );
139 * The following tests parallel those in test_VirtualAlloc()
142 SetLastError(0xdeadbeef);
143 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
144 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
145 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
146 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
147 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
149 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
150 ok(addr1 != NULL, "VirtualAllocEx failed\n");
152 /* test a not committed memory */
153 memset(&info, 'q', sizeof(info));
154 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
155 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
156 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
157 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
158 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
159 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
160 /* NT reports Protect == 0 for a not committed memory block */
161 ok(info.Protect == 0 /* NT */ ||
162 info.Protect == PAGE_NOACCESS, /* Win9x */
163 "%x != PAGE_NOACCESS\n", info.Protect);
164 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
166 SetLastError(0xdeadbeef);
167 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
168 "VirtualProtectEx should fail on a not committed memory\n");
169 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
170 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
171 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
173 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
174 ok(addr1 == addr2, "VirtualAllocEx failed\n");
176 /* test a committed memory */
177 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
178 "VirtualQueryEx failed\n");
179 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
180 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
181 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
182 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
183 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
184 /* this time NT reports PAGE_NOACCESS as well */
185 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
186 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
188 /* this should fail, since not the whole range is committed yet */
189 SetLastError(0xdeadbeef);
190 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
191 "VirtualProtectEx should fail on a not committed memory\n");
192 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
193 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
194 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
197 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
198 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
201 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
202 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
204 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
205 "VirtualFreeEx should fail with type 0\n");
206 ok(GetLastError() == ERROR_INVALID_PARAMETER,
207 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
209 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
211 /* if the type is MEM_RELEASE, size must be 0 */
212 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
213 "VirtualFreeEx should fail\n");
214 ok(GetLastError() == ERROR_INVALID_PARAMETER,
215 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
217 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
219 TerminateProcess(hProcess, 0);
220 CloseHandle(hProcess);
223 static void test_VirtualAlloc(void)
227 MEMORY_BASIC_INFORMATION info;
229 SetLastError(0xdeadbeef);
230 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
231 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
232 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
233 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
234 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
236 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
237 ok(addr1 != NULL, "VirtualAlloc failed\n");
239 /* test a not committed memory */
240 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
241 "VirtualQuery failed\n");
242 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
243 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
244 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
245 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
246 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
247 /* NT reports Protect == 0 for a not committed memory block */
248 ok(info.Protect == 0 /* NT */ ||
249 info.Protect == PAGE_NOACCESS, /* Win9x */
250 "%x != PAGE_NOACCESS\n", info.Protect);
251 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
253 SetLastError(0xdeadbeef);
254 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
255 "VirtualProtect should fail on a not committed memory\n");
256 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
257 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
258 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
260 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
261 ok(addr1 == addr2, "VirtualAlloc failed\n");
263 /* test a committed memory */
264 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
265 "VirtualQuery failed\n");
266 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
267 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
268 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
269 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
270 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
271 /* this time NT reports PAGE_NOACCESS as well */
272 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
273 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
275 /* this should fail, since not the whole range is committed yet */
276 SetLastError(0xdeadbeef);
277 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
278 "VirtualProtect should fail on a not committed memory\n");
279 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
280 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
281 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
283 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
284 ok(old_prot == PAGE_NOACCESS,
285 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
287 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
288 ok(old_prot == PAGE_READONLY,
289 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
291 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
292 "VirtualQuery failed\n");
293 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
294 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
295 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
296 memset( addr1, 0x55, 20 );
297 ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
299 addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
300 ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
301 "VirtualAlloc failed err %u\n", GetLastError() );
302 ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
305 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
306 "VirtualQuery failed\n");
307 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
308 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
309 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
311 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
312 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
314 ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
315 "VirtualQuery failed\n");
316 ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
317 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
318 ok(info.Protect == 0, "%x != 0\n", info.Protect);
320 addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
321 ok( !addr2, "VirtualAlloc failed\n" );
322 ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
325 /* invalid protection values */
326 SetLastError(0xdeadbeef);
327 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
328 ok(!addr2, "VirtualAlloc succeeded\n");
329 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
331 SetLastError(0xdeadbeef);
332 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
333 ok(!addr2, "VirtualAlloc succeeded\n");
334 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
336 SetLastError(0xdeadbeef);
337 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
338 ok(!addr2, "VirtualAlloc succeeded\n");
339 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
341 SetLastError(0xdeadbeef);
342 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
343 "VirtualProtect succeeded\n");
344 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
346 SetLastError(0xdeadbeef);
347 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
348 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
350 SetLastError(0xdeadbeef);
351 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER,
353 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
355 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
357 /* if the type is MEM_RELEASE, size must be 0 */
358 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
359 ok(GetLastError() == ERROR_INVALID_PARAMETER,
360 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
362 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
365 static void test_MapViewOfFile(void)
367 static const char testfile[] = "testfile.xxx";
369 HANDLE file, mapping, map2;
370 void *ptr, *ptr2, *addr;
371 MEMORY_BASIC_INFORMATION info;
374 SetLastError(0xdeadbeef);
375 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
376 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
377 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
378 SetEndOfFile( file );
380 /* read/write mapping */
382 SetLastError(0xdeadbeef);
383 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
384 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
386 SetLastError(0xdeadbeef);
387 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
388 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
389 UnmapViewOfFile( ptr );
391 /* this fails on win9x but succeeds on NT */
392 SetLastError(0xdeadbeef);
393 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
394 if (ptr) UnmapViewOfFile( ptr );
395 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
397 SetLastError(0xdeadbeef);
398 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
399 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
400 UnmapViewOfFile( ptr );
402 SetLastError(0xdeadbeef);
403 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
404 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
405 UnmapViewOfFile( ptr );
407 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
408 FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
409 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
410 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
411 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
412 UnmapViewOfFile( ptr );
415 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
416 FILE_MAP_READ, FALSE, 0 );
417 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
418 ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
421 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
423 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
424 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
425 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
426 ok( !ptr, "MapViewOfFile succeeded\n" );
427 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
429 ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
430 FILE_MAP_READ, FALSE, 0 );
431 ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
432 ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
433 ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
435 else win_skip( "no access checks on win9x\n" );
437 UnmapViewOfFile( ptr );
439 CloseHandle( mapping );
441 /* read-only mapping */
443 SetLastError(0xdeadbeef);
444 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
445 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
447 SetLastError(0xdeadbeef);
448 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
449 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
450 UnmapViewOfFile( ptr );
452 /* this fails on win9x but succeeds on NT */
453 SetLastError(0xdeadbeef);
454 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
455 if (ptr) UnmapViewOfFile( ptr );
456 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
458 SetLastError(0xdeadbeef);
459 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
460 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
461 UnmapViewOfFile( ptr );
463 SetLastError(0xdeadbeef);
464 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
465 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
466 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
467 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
468 CloseHandle( mapping );
470 /* copy-on-write mapping */
472 SetLastError(0xdeadbeef);
473 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
474 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
476 SetLastError(0xdeadbeef);
477 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
478 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
479 UnmapViewOfFile( ptr );
481 SetLastError(0xdeadbeef);
482 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
483 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
484 UnmapViewOfFile( ptr );
486 SetLastError(0xdeadbeef);
487 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
488 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
489 UnmapViewOfFile( ptr );
491 SetLastError(0xdeadbeef);
492 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
493 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
494 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
495 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
496 CloseHandle( mapping );
498 /* no access mapping */
500 SetLastError(0xdeadbeef);
501 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
502 /* fails on NT but succeeds on win9x */
503 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
506 SetLastError(0xdeadbeef);
507 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
508 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
509 UnmapViewOfFile( ptr );
511 SetLastError(0xdeadbeef);
512 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
513 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
514 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
516 SetLastError(0xdeadbeef);
517 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
518 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
519 UnmapViewOfFile( ptr );
521 SetLastError(0xdeadbeef);
522 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
523 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
524 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
526 CloseHandle( mapping );
531 /* now try read-only file */
533 SetLastError(0xdeadbeef);
534 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
535 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
537 SetLastError(0xdeadbeef);
538 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
539 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
540 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
541 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
543 SetLastError(0xdeadbeef);
544 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
545 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
546 CloseHandle( mapping );
548 SetLastError(0xdeadbeef);
549 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
550 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
551 CloseHandle( mapping );
554 /* now try no access file */
556 SetLastError(0xdeadbeef);
557 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
558 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
560 SetLastError(0xdeadbeef);
561 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
562 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
563 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
564 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
566 SetLastError(0xdeadbeef);
567 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
568 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY 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_READONLY, 0, 4096, NULL );
574 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
575 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
576 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
579 DeleteFileA( testfile );
581 SetLastError(0xdeadbeef);
583 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
584 /* nt4 doesn't have Local\\ */
585 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
588 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
590 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
592 SetLastError(0xdeadbeef);
593 mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
594 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
595 SetLastError(0xdeadbeef);
596 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
599 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
600 SetLastError(0xdeadbeef);
601 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
602 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
603 SetLastError(0xdeadbeef);
604 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
605 "VirtualQuery error %u\n", GetLastError() );
606 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
607 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
608 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
609 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
610 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
611 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
613 else win_skip( "no access checks on win9x\n" );
614 UnmapViewOfFile( ptr );
615 CloseHandle( mapping );
617 SetLastError(0xdeadbeef);
618 mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
619 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
620 SetLastError(0xdeadbeef);
621 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
624 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
625 SetLastError(0xdeadbeef);
626 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
627 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
628 SetLastError(0xdeadbeef);
629 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
630 "VirtualQuery error %u\n", GetLastError() );
631 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
632 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
633 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
634 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
635 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
636 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
638 else win_skip( "no access checks on win9x\n" );
639 UnmapViewOfFile( ptr );
640 CloseHandle( mapping );
644 /* read/write mapping with SEC_RESERVE */
645 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
646 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
648 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
649 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
651 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
652 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
653 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
654 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
656 ret = VirtualQuery(ptr, &info, sizeof(info));
657 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
658 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
659 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
660 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
661 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
662 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
664 ok(info.AllocationProtect == PAGE_NOACCESS,
665 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
666 ok(info.Protect == PAGE_NOACCESS,
667 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
671 ok(info.AllocationProtect == PAGE_READWRITE,
672 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
673 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
674 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
679 ret = VirtualQuery(ptr2, &info, sizeof(info));
680 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
681 ok(info.BaseAddress == ptr2,
682 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
683 ok(info.AllocationBase == ptr2,
684 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
685 ok(info.AllocationProtect == PAGE_READWRITE,
686 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
687 ok(info.RegionSize == MAPPING_SIZE,
688 "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
689 ok(info.State == MEM_RESERVE,
690 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
691 ok(info.Protect == 0,
692 "Protect should have been 0 instead of 0x%x\n", info.Protect);
693 ok(info.Type == MEM_MAPPED,
694 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
697 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
698 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
700 ret = VirtualQuery(ptr, &info, sizeof(info));
701 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
702 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
703 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
704 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
705 ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
706 ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
707 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
709 ok(info.AllocationProtect == PAGE_NOACCESS,
710 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
714 ok(info.AllocationProtect == PAGE_READWRITE,
715 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
716 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
719 /* shows that the VirtualAlloc above affects the mapping, not just the
720 * virtual memory in this process - it also affects all other processes
721 * with a view of the mapping, but that isn't tested here */
724 ret = VirtualQuery(ptr2, &info, sizeof(info));
725 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
726 ok(info.BaseAddress == ptr2,
727 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
728 ok(info.AllocationBase == ptr2,
729 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
730 ok(info.AllocationProtect == PAGE_READWRITE,
731 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
732 ok(info.RegionSize == 0x10000,
733 "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
734 ok(info.State == MEM_COMMIT,
735 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
736 ok(info.Protect == PAGE_READWRITE,
737 "Protect should have been 0 instead of 0x%x\n", info.Protect);
738 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
741 addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
742 ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
743 "VirtualAlloc failed with error %u\n", GetLastError() );
745 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
746 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
748 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
750 ret = UnmapViewOfFile(ptr2);
751 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
752 ret = UnmapViewOfFile(ptr);
753 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
754 CloseHandle(mapping);
756 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
757 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
759 SetLastError(0xdeadbeef);
760 ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
761 ok( GetLastError() == ERROR_INVALID_ADDRESS,
762 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
763 SetLastError(0xdeadbeef);
764 ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
765 ok( GetLastError() == ERROR_INVALID_ADDRESS,
766 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
767 SetLastError(0xdeadbeef);
768 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
769 ok( GetLastError() == ERROR_INVALID_ADDRESS,
770 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
772 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
775 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
776 ULONG zero_bits, SIZE_T commit_size,
777 const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
778 ULONG inherit, ULONG alloc_type, ULONG protect );
779 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
781 static void test_NtMapViewOfSection(void)
785 static const char testfile[] = "testfile.xxx";
786 static const char data[] = "test data for NtMapViewOfSection";
787 char buffer[sizeof(data)];
788 HANDLE file, mapping;
791 DWORD status, written;
793 LARGE_INTEGER offset;
795 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
796 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
797 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
799 win_skip( "NtMapViewOfSection not available\n" );
803 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
804 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
805 WriteFile( file, data, sizeof(data), &written, NULL );
806 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
807 SetEndOfFile( file );
809 /* read/write mapping */
811 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
812 ok( mapping != 0, "CreateFileMapping failed\n" );
814 hProcess = create_target_process("sleep");
815 ok(hProcess != NULL, "Can't start process\n");
820 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
821 ok( !status, "NtMapViewOfSection failed status %x\n", status );
823 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
824 ok( ret, "ReadProcessMemory failed\n" );
825 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
826 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
828 status = pNtUnmapViewOfSection( hProcess, ptr );
829 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
831 CloseHandle( mapping );
833 DeleteFileA( testfile );
835 TerminateProcess(hProcess, 0);
836 CloseHandle(hProcess);
839 static void test_NtAreMappedFilesTheSame(void)
841 static const char testfile[] = "testfile.xxx";
842 HANDLE file, file2, mapping, map2;
847 if (!pNtAreMappedFilesTheSame)
849 win_skip( "NtAreMappedFilesTheSame not available\n" );
853 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
854 NULL, CREATE_ALWAYS, 0, 0 );
855 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
856 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
857 SetEndOfFile( file );
859 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
860 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
862 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
863 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
865 file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
866 NULL, OPEN_EXISTING, 0, 0 );
867 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
869 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
870 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
871 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
872 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
873 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
874 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
875 UnmapViewOfFile( ptr2 );
877 ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
878 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
879 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
880 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
881 UnmapViewOfFile( ptr2 );
884 map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
885 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
886 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
887 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
888 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
889 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
890 UnmapViewOfFile( ptr2 );
892 CloseHandle( file2 );
894 status = pNtAreMappedFilesTheSame( ptr, ptr );
895 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
897 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
898 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
900 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
901 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
903 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
904 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
905 "NtAreMappedFilesTheSame returned %x\n", status );
907 status = pNtAreMappedFilesTheSame( ptr, NULL );
908 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
910 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
911 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
913 status = pNtAreMappedFilesTheSame( NULL, NULL );
914 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
916 ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
917 ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
918 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
919 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
920 VirtualFree( ptr2, 0, MEM_RELEASE );
922 UnmapViewOfFile( ptr );
923 CloseHandle( mapping );
926 status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
927 GetModuleHandleA("kernel32.dll") );
928 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
929 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
930 GetModuleHandleA("kernel32.dll") );
931 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
932 status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
933 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
934 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
936 GetSystemDirectoryA( path, MAX_PATH );
937 strcat( path, "\\kernel32.dll" );
938 file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
939 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
941 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
942 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
943 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
944 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
945 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
946 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
947 UnmapViewOfFile( ptr );
948 CloseHandle( mapping );
950 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
951 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
952 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
953 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
954 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
956 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
958 file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
959 ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
960 map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
961 ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
962 ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
963 ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
964 status = pNtAreMappedFilesTheSame( ptr, ptr2 );
965 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
966 UnmapViewOfFile( ptr2 );
968 CloseHandle( file2 );
970 UnmapViewOfFile( ptr );
971 CloseHandle( mapping );
974 DeleteFileA( testfile );
977 static void test_CreateFileMapping(void)
979 HANDLE handle, handle2;
981 /* test case sensitivity */
983 SetLastError(0xdeadbeef);
984 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
985 "Wine Test Mapping");
986 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
987 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
989 SetLastError(0xdeadbeef);
990 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
991 "Wine Test Mapping");
992 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
993 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
994 CloseHandle( handle2 );
996 SetLastError(0xdeadbeef);
997 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
998 "WINE TEST MAPPING");
999 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1000 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1001 CloseHandle( handle2 );
1003 SetLastError(0xdeadbeef);
1004 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1005 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1006 CloseHandle( handle2 );
1008 SetLastError(0xdeadbeef);
1009 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1010 ok( !handle2, "OpenFileMapping succeeded\n");
1011 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1012 "wrong error %u\n", GetLastError());
1014 CloseHandle( handle );
1017 static void test_IsBadReadPtr(void)
1020 void *ptr = (void *)0xdeadbeef;
1023 ret = IsBadReadPtr(NULL, 0);
1024 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1026 ret = IsBadReadPtr(NULL, 1);
1027 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1029 ret = IsBadReadPtr(ptr, 0);
1030 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1032 ret = IsBadReadPtr(ptr, 1);
1033 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1035 ret = IsBadReadPtr(&stackvar, 0);
1036 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1038 ret = IsBadReadPtr(&stackvar, sizeof(char));
1039 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1042 static void test_IsBadWritePtr(void)
1045 void *ptr = (void *)0xdeadbeef;
1048 ret = IsBadWritePtr(NULL, 0);
1049 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1051 ret = IsBadWritePtr(NULL, 1);
1052 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1054 ret = IsBadWritePtr(ptr, 0);
1055 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1057 ret = IsBadWritePtr(ptr, 1);
1058 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1060 ret = IsBadWritePtr(&stackval, 0);
1061 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1063 ret = IsBadWritePtr(&stackval, sizeof(char));
1064 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1067 static void test_IsBadCodePtr(void)
1070 void *ptr = (void *)0xdeadbeef;
1073 ret = IsBadCodePtr(NULL);
1074 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1076 ret = IsBadCodePtr(ptr);
1077 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1079 ret = IsBadCodePtr((void *)&stackval);
1080 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1083 static void test_write_watch(void)
1086 DWORD ret, size, old_prot;
1087 MEMORY_BASIC_INFORMATION info;
1092 if (!pGetWriteWatch || !pResetWriteWatch)
1094 win_skip( "GetWriteWatch not supported\n" );
1099 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1101 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1103 win_skip( "MEM_WRITE_WATCH not supported\n" );
1106 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1107 ret = VirtualQuery( base, &info, sizeof(info) );
1108 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1109 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1110 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1111 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1112 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1113 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1114 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1117 SetLastError( 0xdeadbeef );
1118 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1119 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1120 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1121 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1122 "wrong error %u\n", GetLastError() );
1124 SetLastError( 0xdeadbeef );
1125 ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
1128 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1129 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1133 ok( count == 0, "wrong count %lu\n", count );
1136 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1137 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1138 ok( count == 0, "wrong count %lu\n", count );
1140 base[pagesize + 1] = 0x44;
1143 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1144 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1145 ok( count == 1, "wrong count %lu\n", count );
1146 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1149 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1150 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1151 ok( count == 1, "wrong count %lu\n", count );
1152 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1155 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1156 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1157 ok( count == 0, "wrong count %lu\n", count );
1159 base[2*pagesize + 3] = 0x11;
1160 base[4*pagesize + 8] = 0x11;
1163 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1164 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1165 ok( count == 2, "wrong count %lu\n", count );
1166 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1167 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1170 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1171 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1172 ok( count == 1, "wrong count %lu\n", count );
1173 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1175 ret = pResetWriteWatch( base, 3*pagesize );
1176 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1179 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1180 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1181 ok( count == 1, "wrong count %lu\n", count );
1182 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1184 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1187 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1188 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1189 ok( count == 3, "wrong count %lu\n", count );
1190 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1191 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1192 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1195 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1196 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1197 ok( count == 1, "wrong count %lu\n", count );
1198 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1201 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1202 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1203 ok( count == 2, "wrong count %lu\n", count );
1204 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1205 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1207 /* changing protections doesn't affect watches */
1209 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1210 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1211 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1213 ret = VirtualQuery( base, &info, sizeof(info) );
1214 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1215 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1216 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1217 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1218 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1220 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1221 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1222 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1225 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1226 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1227 ok( count == 2, "wrong count %lu\n", count );
1228 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1229 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1231 ret = VirtualQuery( base, &info, sizeof(info) );
1232 ok(ret, "VirtualQuery failed %u\n", GetLastError());
1233 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1234 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1235 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1236 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1238 /* some invalid parameter tests */
1240 SetLastError( 0xdeadbeef );
1242 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1245 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1246 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1248 SetLastError( 0xdeadbeef );
1249 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1250 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1251 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1253 SetLastError( 0xdeadbeef );
1255 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1256 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1257 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1259 SetLastError( 0xdeadbeef );
1261 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1262 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1263 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1265 SetLastError( 0xdeadbeef );
1267 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1268 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1269 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1271 SetLastError( 0xdeadbeef );
1273 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1274 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1275 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1277 SetLastError( 0xdeadbeef );
1279 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1280 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1281 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1283 SetLastError( 0xdeadbeef );
1285 ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1286 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1287 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1289 SetLastError( 0xdeadbeef );
1291 ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1292 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1293 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1295 SetLastError( 0xdeadbeef );
1296 ret = pResetWriteWatch( base, 0 );
1297 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1298 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1300 SetLastError( 0xdeadbeef );
1301 ret = pResetWriteWatch( GetModuleHandle(0), size );
1302 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1303 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1305 else /* win98 is completely different */
1307 SetLastError( 0xdeadbeef );
1309 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1310 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1311 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1314 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1315 ok( !ret, "GetWriteWatch failed %u\n", ret );
1318 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1319 ok( !ret, "GetWriteWatch failed %u\n", ret );
1322 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1323 ok( !ret, "GetWriteWatch failed %u\n", ret );
1325 ret = pResetWriteWatch( base, 0 );
1326 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1328 ret = pResetWriteWatch( GetModuleHandle(0), size );
1329 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1332 VirtualFree( base, 0, MEM_FREE );
1334 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1335 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1336 VirtualFree( base, 0, MEM_FREE );
1338 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1339 ok( !base, "VirtualAlloc succeeded\n" );
1340 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1342 /* initial protect doesn't matter */
1344 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1345 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1346 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1347 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1350 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1351 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1352 ok( count == 0, "wrong count %lu\n", count );
1354 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1355 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1356 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1358 base[5*pagesize + 200] = 3;
1360 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1361 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1362 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1365 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1366 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1367 ok( count == 1, "wrong count %lu\n", count );
1368 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1370 ret = VirtualFree( base, size, MEM_DECOMMIT );
1371 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1374 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1375 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1376 ok( count == 1 || broken(count == 0), /* win98 */
1377 "wrong count %lu\n", count );
1378 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1380 VirtualFree( base, 0, MEM_FREE );
1387 argc = winetest_get_mainargs( &argv );
1391 if (!strcmp(argv[2], "sleep"))
1393 Sleep(5000); /* spawned process runs for at most 5 seconds */
1400 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1401 PAGE_EXECUTE_READWRITE);
1402 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1403 if (mem == NULL) break;
1404 ret = VirtualFree(mem, 0, MEM_RELEASE);
1405 ok(ret, "VirtualFree failed %u\n", GetLastError());
1411 hkernel32 = GetModuleHandleA("kernel32.dll");
1412 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1413 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1414 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1415 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1416 pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"),
1417 "NtAreMappedFilesTheSame" );
1419 test_VirtualAllocEx();
1420 test_VirtualAlloc();
1421 test_MapViewOfFile();
1422 test_NtMapViewOfSection();
1423 test_NtAreMappedFilesTheSame();
1424 test_CreateFileMapping();
1425 test_IsBadReadPtr();
1426 test_IsBadWritePtr();
1427 test_IsBadCodePtr();