msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / kernel32 / tests / virtual.c
blobae812cd5d1583e4a4dd5a613245f30662d6497c1
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 "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/test.h"
29 #define NUM_THREADS 4
30 #define MAPPING_SIZE 0x100000
32 static HINSTANCE hkernel32;
33 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
34 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
35 static UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
36 static UINT (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
38 /* ############################### */
40 static HANDLE create_target_process(const char *arg)
42 char **argv;
43 char cmdline[MAX_PATH];
44 PROCESS_INFORMATION pi;
45 STARTUPINFO si = { 0 };
46 si.cb = sizeof(si);
48 winetest_get_mainargs( &argv );
49 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
50 ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
51 &si, &pi) != 0, "error: %u\n", GetLastError());
52 ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
53 return pi.hProcess;
56 static void test_VirtualAllocEx(void)
58 const unsigned int alloc_size = 1<<15;
59 char *src, *dst;
60 SIZE_T bytes_written = 0, bytes_read = 0, i;
61 void *addr1, *addr2;
62 BOOL b;
63 DWORD old_prot;
64 MEMORY_BASIC_INFORMATION info;
65 HANDLE hProcess;
67 /* not exported in all windows-versions */
68 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
69 skip("VirtualAllocEx not found\n");
70 return;
73 hProcess = create_target_process("sleep");
74 ok(hProcess != NULL, "Can't start process\n");
76 SetLastError(0xdeadbeef);
77 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
78 PAGE_EXECUTE_READWRITE);
79 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
80 { /* Win9x */
81 skip("VirtualAllocEx not implemented\n");
82 TerminateProcess(hProcess, 0);
83 CloseHandle(hProcess);
84 return;
87 src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
88 dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
89 for (i = 0; i < alloc_size; i++)
90 src[i] = i & 0xff;
92 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
93 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
94 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
95 bytes_written);
96 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
97 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
98 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
100 /* test invalid source buffers */
102 b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
103 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
104 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
105 ok( !b, "WriteProcessMemory succeeded\n" );
106 ok( GetLastError() == ERROR_NOACCESS ||
107 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
108 "wrong error %u\n", GetLastError() );
109 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
110 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
111 ok( !b, "ReadProcessMemory succeeded\n" );
112 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
113 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
115 b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
116 ok( b, "VirtualProtect failed error %u\n", GetLastError() );
117 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
118 ok( !b, "WriteProcessMemory succeeded\n" );
119 ok( GetLastError() == ERROR_NOACCESS ||
120 GetLastError() == ERROR_PARTIAL_COPY, /* vista */
121 "wrong error %u\n", GetLastError() );
122 ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
123 b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
124 ok( !b, "ReadProcessMemory succeeded\n" );
125 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
126 ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
128 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
129 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
131 VirtualFree( src, 0, MEM_FREE );
132 VirtualFree( dst, 0, MEM_FREE );
135 * The following tests parallel those in test_VirtualAlloc()
138 SetLastError(0xdeadbeef);
139 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
140 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
141 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
142 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
143 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
145 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
146 ok(addr1 != NULL, "VirtualAllocEx failed\n");
148 /* test a not committed memory */
149 memset(&info, 'q', sizeof(info));
150 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
151 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
152 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
153 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
154 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
155 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
156 /* NT reports Protect == 0 for a not committed memory block */
157 ok(info.Protect == 0 /* NT */ ||
158 info.Protect == PAGE_NOACCESS, /* Win9x */
159 "%x != PAGE_NOACCESS\n", info.Protect);
160 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
162 SetLastError(0xdeadbeef);
163 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
164 "VirtualProtectEx should fail on a not committed memory\n");
165 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
166 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
167 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
169 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
170 ok(addr1 == addr2, "VirtualAllocEx failed\n");
172 /* test a committed memory */
173 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
174 "VirtualQueryEx failed\n");
175 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
176 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
177 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
178 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
179 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
180 /* this time NT reports PAGE_NOACCESS as well */
181 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
182 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
184 /* this should fail, since not the whole range is committed yet */
185 SetLastError(0xdeadbeef);
186 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
187 "VirtualProtectEx should fail on a not committed memory\n");
188 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
189 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
190 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
192 old_prot = 0;
193 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
194 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
196 old_prot = 0;
197 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
198 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
200 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
201 "VirtualFreeEx should fail with type 0\n");
202 ok(GetLastError() == ERROR_INVALID_PARAMETER,
203 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
205 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
207 /* if the type is MEM_RELEASE, size must be 0 */
208 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
209 "VirtualFreeEx should fail\n");
210 ok(GetLastError() == ERROR_INVALID_PARAMETER,
211 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
213 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
215 TerminateProcess(hProcess, 0);
216 CloseHandle(hProcess);
219 static void test_VirtualAlloc(void)
221 void *addr1, *addr2;
222 DWORD old_prot;
223 MEMORY_BASIC_INFORMATION info;
225 SetLastError(0xdeadbeef);
226 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
227 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
228 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
229 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
230 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
232 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
233 ok(addr1 != NULL, "VirtualAlloc failed\n");
235 /* test a not committed memory */
236 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
237 "VirtualQuery failed\n");
238 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
239 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
240 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
241 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
242 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
243 /* NT reports Protect == 0 for a not committed memory block */
244 ok(info.Protect == 0 /* NT */ ||
245 info.Protect == PAGE_NOACCESS, /* Win9x */
246 "%x != PAGE_NOACCESS\n", info.Protect);
247 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
249 SetLastError(0xdeadbeef);
250 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
251 "VirtualProtect should fail on a not committed memory\n");
252 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
253 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
254 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
256 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
257 ok(addr1 == addr2, "VirtualAlloc failed\n");
259 /* test a committed memory */
260 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
261 "VirtualQuery failed\n");
262 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
263 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
264 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
265 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
266 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
267 /* this time NT reports PAGE_NOACCESS as well */
268 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
269 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
271 /* this should fail, since not the whole range is committed yet */
272 SetLastError(0xdeadbeef);
273 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
274 "VirtualProtect should fail on a not committed memory\n");
275 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
276 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
277 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
279 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
280 ok(old_prot == PAGE_NOACCESS,
281 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
283 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
284 ok(old_prot == PAGE_READONLY,
285 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
287 /* invalid protection values */
288 SetLastError(0xdeadbeef);
289 addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
290 ok(!addr2, "VirtualAlloc succeeded\n");
291 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
293 SetLastError(0xdeadbeef);
294 addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
295 ok(!addr2, "VirtualAlloc succeeded\n");
296 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
298 SetLastError(0xdeadbeef);
299 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
300 ok(!addr2, "VirtualAlloc succeeded\n");
301 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
303 SetLastError(0xdeadbeef);
304 ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
305 "VirtualProtect succeeded\n");
306 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
308 SetLastError(0xdeadbeef);
309 ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
312 SetLastError(0xdeadbeef);
313 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
314 ok(GetLastError() == ERROR_INVALID_PARAMETER,
315 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
317 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
319 /* if the type is MEM_RELEASE, size must be 0 */
320 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
321 ok(GetLastError() == ERROR_INVALID_PARAMETER,
322 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
324 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
327 static void test_MapViewOfFile(void)
329 static const char testfile[] = "testfile.xxx";
330 const char *name;
331 HANDLE file, mapping;
332 void *ptr, *ptr2;
333 MEMORY_BASIC_INFORMATION info;
334 BOOL ret;
336 SetLastError(0xdeadbeef);
337 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
338 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
339 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
340 SetEndOfFile( file );
342 /* read/write mapping */
344 SetLastError(0xdeadbeef);
345 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
346 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
348 SetLastError(0xdeadbeef);
349 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
350 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
351 UnmapViewOfFile( ptr );
353 /* this fails on win9x but succeeds on NT */
354 SetLastError(0xdeadbeef);
355 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
356 if (ptr) UnmapViewOfFile( ptr );
357 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
359 SetLastError(0xdeadbeef);
360 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
361 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
362 UnmapViewOfFile( ptr );
364 SetLastError(0xdeadbeef);
365 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
366 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
367 UnmapViewOfFile( ptr );
368 CloseHandle( mapping );
370 /* read-only mapping */
372 SetLastError(0xdeadbeef);
373 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
374 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
376 SetLastError(0xdeadbeef);
377 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
378 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
379 UnmapViewOfFile( ptr );
381 /* this fails on win9x but succeeds on NT */
382 SetLastError(0xdeadbeef);
383 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
384 if (ptr) UnmapViewOfFile( ptr );
385 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
387 SetLastError(0xdeadbeef);
388 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
389 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
390 UnmapViewOfFile( ptr );
392 SetLastError(0xdeadbeef);
393 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
394 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
395 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
396 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
397 CloseHandle( mapping );
399 /* copy-on-write mapping */
401 SetLastError(0xdeadbeef);
402 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
403 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
405 SetLastError(0xdeadbeef);
406 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
407 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
408 UnmapViewOfFile( ptr );
410 SetLastError(0xdeadbeef);
411 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
412 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
413 UnmapViewOfFile( ptr );
415 SetLastError(0xdeadbeef);
416 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
417 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
418 UnmapViewOfFile( ptr );
420 SetLastError(0xdeadbeef);
421 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
422 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
423 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
424 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
425 CloseHandle( mapping );
427 /* no access mapping */
429 SetLastError(0xdeadbeef);
430 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
431 /* fails on NT but succeeds on win9x */
432 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
433 else
435 SetLastError(0xdeadbeef);
436 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
437 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
438 UnmapViewOfFile( ptr );
440 SetLastError(0xdeadbeef);
441 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
442 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
443 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
445 SetLastError(0xdeadbeef);
446 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
447 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
448 UnmapViewOfFile( ptr );
450 SetLastError(0xdeadbeef);
451 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
452 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
453 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
455 CloseHandle( mapping );
458 CloseHandle( file );
460 /* now try read-only file */
462 SetLastError(0xdeadbeef);
463 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
464 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
466 SetLastError(0xdeadbeef);
467 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
468 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
469 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
470 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
472 SetLastError(0xdeadbeef);
473 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
474 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
475 CloseHandle( mapping );
477 SetLastError(0xdeadbeef);
478 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
479 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
480 CloseHandle( mapping );
481 CloseHandle( file );
483 /* now try no access file */
485 SetLastError(0xdeadbeef);
486 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
487 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
489 SetLastError(0xdeadbeef);
490 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
491 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
492 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
493 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
495 SetLastError(0xdeadbeef);
496 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
497 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
498 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
499 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
501 SetLastError(0xdeadbeef);
502 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
503 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
504 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
505 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
507 CloseHandle( file );
508 DeleteFileA( testfile );
510 SetLastError(0xdeadbeef);
511 name = "Local\\Foo";
512 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
513 /* nt4 doesn't have Local\\ */
514 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
516 name = "Foo";
517 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
519 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
521 SetLastError(0xdeadbeef);
522 mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
523 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
524 SetLastError(0xdeadbeef);
525 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
526 if (!ptr)
528 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
529 SetLastError(0xdeadbeef);
530 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
531 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
532 SetLastError(0xdeadbeef);
533 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
534 "VirtualQuery error %u\n", GetLastError() );
535 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
536 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
537 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
538 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
539 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
540 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
542 else win_skip( "no access checks on win9x\n" );
543 UnmapViewOfFile( ptr );
544 CloseHandle( mapping );
546 SetLastError(0xdeadbeef);
547 mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
548 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
549 SetLastError(0xdeadbeef);
550 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
551 if (!ptr)
553 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
554 SetLastError(0xdeadbeef);
555 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
556 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
557 SetLastError(0xdeadbeef);
558 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
559 "VirtualQuery error %u\n", GetLastError() );
560 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
561 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
562 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
563 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
564 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
565 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
567 else win_skip( "no access checks on win9x\n" );
568 UnmapViewOfFile( ptr );
569 CloseHandle( mapping );
571 CloseHandle( file );
573 /* read/write mapping with SEC_RESERVE */
574 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
575 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
577 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
578 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
580 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
581 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
582 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
583 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
585 ret = VirtualQuery(ptr, &info, sizeof(info));
586 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
587 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
588 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
589 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
590 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
591 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
593 ok(info.AllocationProtect == PAGE_NOACCESS,
594 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
595 ok(info.Protect == PAGE_NOACCESS,
596 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
598 else
600 ok(info.AllocationProtect == PAGE_READWRITE,
601 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
602 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
603 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
606 if (ptr != ptr2)
608 ret = VirtualQuery(ptr2, &info, sizeof(info));
609 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
610 ok(info.BaseAddress == ptr2,
611 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
612 ok(info.AllocationBase == ptr2,
613 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
614 ok(info.AllocationProtect == PAGE_READWRITE,
615 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
616 ok(info.RegionSize == MAPPING_SIZE,
617 "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
618 ok(info.State == MEM_RESERVE,
619 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
620 ok(info.Protect == 0,
621 "Protect should have been 0 instead of 0x%x\n", info.Protect);
622 ok(info.Type == MEM_MAPPED,
623 "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
626 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
627 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
629 ret = VirtualQuery(ptr, &info, sizeof(info));
630 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
631 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
632 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
633 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
634 ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
635 ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
636 if (info.Type == MEM_PRIVATE) /* win9x is different for uncommitted mappings */
638 ok(info.AllocationProtect == PAGE_NOACCESS,
639 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
641 else
643 ok(info.AllocationProtect == PAGE_READWRITE,
644 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
645 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
648 /* shows that the VirtualAlloc above affects the mapping, not just the
649 * virtual memory in this process - it also affects all other processes
650 * with a view of the mapping, but that isn't tested here */
651 if (ptr != ptr2)
653 ret = VirtualQuery(ptr2, &info, sizeof(info));
654 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
655 ok(info.BaseAddress == ptr2,
656 "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
657 ok(info.AllocationBase == ptr2,
658 "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
659 ok(info.AllocationProtect == PAGE_READWRITE,
660 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
661 ok(info.RegionSize == 0x10000,
662 "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
663 ok(info.State == MEM_COMMIT,
664 "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
665 ok(info.Protect == PAGE_READWRITE,
666 "Protect should have been 0 instead of 0x%x\n", info.Protect);
667 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
670 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
671 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
672 if (!ret)
673 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
675 ret = UnmapViewOfFile(ptr2);
676 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
677 ret = UnmapViewOfFile(ptr);
678 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
679 CloseHandle(mapping);
682 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
683 ULONG zero_bits, SIZE_T commit_size,
684 const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
685 ULONG inherit, ULONG alloc_type, ULONG protect );
686 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
688 static void test_NtMapViewOfSection(void)
690 HANDLE hProcess;
692 static const char testfile[] = "testfile.xxx";
693 static const char data[] = "test data for NtMapViewOfSection";
694 char buffer[sizeof(data)];
695 HANDLE file, mapping;
696 void *ptr;
697 BOOL ret;
698 DWORD status, written;
699 SIZE_T size, result;
700 LARGE_INTEGER offset;
702 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
703 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
704 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
706 skip( "NtMapViewOfSection not found\n" );
707 return;
710 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
711 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
712 WriteFile( file, data, sizeof(data), &written, NULL );
713 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
714 SetEndOfFile( file );
716 /* read/write mapping */
718 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
719 ok( mapping != 0, "CreateFileMapping failed\n" );
721 hProcess = create_target_process("sleep");
722 ok(hProcess != NULL, "Can't start process\n");
724 ptr = NULL;
725 size = 0;
726 offset.QuadPart = 0;
727 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
728 ok( !status, "NtMapViewOfSection failed status %x\n", status );
730 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
731 ok( ret, "ReadProcessMemory failed\n" );
732 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
733 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
735 status = pNtUnmapViewOfSection( hProcess, ptr );
736 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
738 CloseHandle( mapping );
739 CloseHandle( file );
740 DeleteFileA( testfile );
742 TerminateProcess(hProcess, 0);
743 CloseHandle(hProcess);
746 static void test_CreateFileMapping(void)
748 HANDLE handle, handle2;
750 /* test case sensitivity */
752 SetLastError(0xdeadbeef);
753 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
754 "Wine Test Mapping");
755 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
756 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
758 SetLastError(0xdeadbeef);
759 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
760 "Wine Test Mapping");
761 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
762 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
763 CloseHandle( handle2 );
765 SetLastError(0xdeadbeef);
766 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
767 "WINE TEST MAPPING");
768 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
769 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
770 CloseHandle( handle2 );
772 SetLastError(0xdeadbeef);
773 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
774 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
775 CloseHandle( handle2 );
777 SetLastError(0xdeadbeef);
778 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
779 ok( !handle2, "OpenFileMapping succeeded\n");
780 ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
781 "wrong error %u\n", GetLastError());
783 CloseHandle( handle );
786 static void test_BadPtr(void)
788 void *ptr = (void*)1;
789 /* We assume address 1 is not mapped. */
790 ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
791 ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
792 ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
795 static void test_write_watch(void)
797 char *base;
798 DWORD ret, size, old_prot;
799 MEMORY_BASIC_INFORMATION info;
800 void *results[64];
801 ULONG_PTR count;
802 ULONG pagesize;
804 if (!pGetWriteWatch || !pResetWriteWatch)
806 win_skip( "GetWriteWatch not supported\n" );
807 return;
810 size = 0x10000;
811 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
812 if (!base &&
813 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
815 win_skip( "MEM_WRITE_WATCH not supported\n" );
816 return;
818 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
819 ret = VirtualQuery( base, &info, sizeof(info) );
820 ok(ret, "VirtualQuery failed %u\n", GetLastError());
821 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
822 ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
823 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
824 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
825 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
826 ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
828 count = 64;
829 SetLastError( 0xdeadbeef );
830 ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
831 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
832 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
833 broken( GetLastError() == 0xdeadbeef ), /* win98 */
834 "wrong error %u\n", GetLastError() );
836 SetLastError( 0xdeadbeef );
837 ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
838 if (ret)
840 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
841 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
843 else /* win98 */
845 ok( count == 0, "wrong count %lu\n", count );
848 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
849 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
850 ok( count == 0, "wrong count %lu\n", count );
852 base[pagesize + 1] = 0x44;
854 count = 64;
855 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
856 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
857 ok( count == 1, "wrong count %lu\n", count );
858 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
860 count = 64;
861 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
862 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
863 ok( count == 1, "wrong count %lu\n", count );
864 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
866 count = 64;
867 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
868 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
869 ok( count == 0, "wrong count %lu\n", count );
871 base[2*pagesize + 3] = 0x11;
872 base[4*pagesize + 8] = 0x11;
874 count = 64;
875 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
876 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
877 ok( count == 2, "wrong count %lu\n", count );
878 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
879 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
881 count = 64;
882 ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
883 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
884 ok( count == 1, "wrong count %lu\n", count );
885 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
887 ret = pResetWriteWatch( base, 3*pagesize );
888 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
890 count = 64;
891 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
892 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
893 ok( count == 1, "wrong count %lu\n", count );
894 ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
896 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
898 count = 64;
899 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
900 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
901 ok( count == 3, "wrong count %lu\n", count );
902 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
903 ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
904 ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
906 count = 1;
907 ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
908 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
909 ok( count == 1, "wrong count %lu\n", count );
910 ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
912 count = 64;
913 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
914 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
915 ok( count == 2, "wrong count %lu\n", count );
916 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
917 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
919 /* changing protections doesn't affect watches */
921 ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
922 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
923 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
925 ret = VirtualQuery( base, &info, sizeof(info) );
926 ok(ret, "VirtualQuery failed %u\n", GetLastError());
927 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
928 ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
929 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
930 ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
932 ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
933 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
934 ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
936 count = 64;
937 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
938 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
939 ok( count == 2, "wrong count %lu\n", count );
940 ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
941 ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
943 ret = VirtualQuery( base, &info, sizeof(info) );
944 ok(ret, "VirtualQuery failed %u\n", GetLastError());
945 ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
946 ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
947 ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
948 ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
950 /* some invalid parameter tests */
952 SetLastError( 0xdeadbeef );
953 count = 0;
954 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
955 if (ret)
957 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
958 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
960 SetLastError( 0xdeadbeef );
961 ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
962 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
963 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
965 SetLastError( 0xdeadbeef );
966 count = 64;
967 ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
968 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
969 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
971 SetLastError( 0xdeadbeef );
972 count = 64;
973 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
974 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
975 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
977 SetLastError( 0xdeadbeef );
978 count = 0;
979 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
980 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
981 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
983 SetLastError( 0xdeadbeef );
984 count = 64;
985 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
986 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
987 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
989 SetLastError( 0xdeadbeef );
990 count = 64;
991 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
992 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
993 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
995 SetLastError( 0xdeadbeef );
996 count = 64;
997 ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
998 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
999 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1001 SetLastError( 0xdeadbeef );
1002 count = 64;
1003 ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1004 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1005 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1007 SetLastError( 0xdeadbeef );
1008 ret = pResetWriteWatch( base, 0 );
1009 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1010 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1012 SetLastError( 0xdeadbeef );
1013 ret = pResetWriteWatch( GetModuleHandle(0), size );
1014 ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1015 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1017 else /* win98 is completely different */
1019 SetLastError( 0xdeadbeef );
1020 count = 64;
1021 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1022 ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1023 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1025 count = 0;
1026 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1027 ok( !ret, "GetWriteWatch failed %u\n", ret );
1029 count = 64;
1030 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1031 ok( !ret, "GetWriteWatch failed %u\n", ret );
1033 count = 64;
1034 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1035 ok( !ret, "GetWriteWatch failed %u\n", ret );
1037 ret = pResetWriteWatch( base, 0 );
1038 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1040 ret = pResetWriteWatch( GetModuleHandle(0), size );
1041 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1044 VirtualFree( base, 0, MEM_FREE );
1046 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1047 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1048 VirtualFree( base, 0, MEM_FREE );
1050 base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1051 ok( !base, "VirtualAlloc succeeded\n" );
1052 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1054 /* initial protect doesn't matter */
1056 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1057 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1058 base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1059 ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1061 count = 64;
1062 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1063 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1064 ok( count == 0, "wrong count %lu\n", count );
1066 ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1067 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1068 ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1070 base[5*pagesize + 200] = 3;
1072 ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1073 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1074 ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1076 count = 64;
1077 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1078 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1079 ok( count == 1, "wrong count %lu\n", count );
1080 ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1082 ret = VirtualFree( base, size, MEM_DECOMMIT );
1083 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1085 count = 64;
1086 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1087 ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1088 ok( count == 1 || broken(count == 0), /* win98 */
1089 "wrong count %lu\n", count );
1090 if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1092 VirtualFree( base, 0, MEM_FREE );
1095 START_TEST(virtual)
1097 int argc;
1098 char **argv;
1099 argc = winetest_get_mainargs( &argv );
1101 if (argc >= 3)
1103 if (!strcmp(argv[2], "sleep"))
1105 Sleep(5000); /* spawned process runs for at most 5 seconds */
1106 return;
1108 while (1)
1110 void *mem;
1111 BOOL ret;
1112 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1113 PAGE_EXECUTE_READWRITE);
1114 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1115 if (mem == NULL) break;
1116 ret = VirtualFree(mem, 0, MEM_RELEASE);
1117 ok(ret, "VirtualFree failed %u\n", GetLastError());
1118 if (!ret) break;
1120 return;
1123 hkernel32 = GetModuleHandleA("kernel32.dll");
1124 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1125 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1126 pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1127 pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1129 test_VirtualAllocEx();
1130 test_VirtualAlloc();
1131 test_MapViewOfFile();
1132 test_NtMapViewOfSection();
1133 test_CreateFileMapping();
1134 test_BadPtr();
1135 test_write_watch();