d3dx8: Add a few tests for MatrixStack.
[wine] / dlls / kernel32 / tests / virtual.c
1 /*
2  * Unit test suite for Virtual* family of APIs.
3  *
4  * Copyright 2004 Dmitry Timoshkov
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/test.h"
28
29 #define NUM_THREADS 4
30 #define MAPPING_SIZE 0x100000
31
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);
37
38 /* ############################### */
39
40 static HANDLE create_target_process(const char *arg)
41 {
42     char **argv;
43     char cmdline[MAX_PATH];
44     PROCESS_INFORMATION pi;
45     STARTUPINFO si = { 0 };
46     si.cb = sizeof(si);
47
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;
54 }
55
56 static void test_VirtualAllocEx(void)
57 {
58     const unsigned int alloc_size = 1<<15;
59     char *src, *dst;
60     unsigned long 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;
66
67     /* not exported in all windows-versions  */
68     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
69         skip("VirtualAllocEx not found\n");
70         return;
71     }
72
73     hProcess = create_target_process("sleep");
74     ok(hProcess != NULL, "Can't start process\n");
75
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;
85     }
86
87     src = HeapAlloc( GetProcessHeap(), 0, alloc_size );
88     dst = HeapAlloc( GetProcessHeap(), 0, alloc_size );
89     for (i = 0; i < alloc_size; i++)
90         src[i] = i & 0xff;
91
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");
99     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
100     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
101
102     HeapFree( GetProcessHeap(), 0, src );
103     HeapFree( GetProcessHeap(), 0, dst );
104
105     /*
106      * The following tests parallel those in test_VirtualAlloc()
107      */
108
109     SetLastError(0xdeadbeef);
110     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
111     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
112     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
113        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
114         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
115
116     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
117     ok(addr1 != NULL, "VirtualAllocEx failed\n");
118
119     /* test a not committed memory */
120     memset(&info, 'q', sizeof(info));
121     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
122     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
123     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
124     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
125     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
126     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
127     /* NT reports Protect == 0 for a not committed memory block */
128     ok(info.Protect == 0 /* NT */ ||
129        info.Protect == PAGE_NOACCESS, /* Win9x */
130         "%x != PAGE_NOACCESS\n", info.Protect);
131     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
132
133     SetLastError(0xdeadbeef);
134     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
135        "VirtualProtectEx should fail on a not committed memory\n");
136     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
137        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
138         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
139
140     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
141     ok(addr1 == addr2, "VirtualAllocEx failed\n");
142
143     /* test a committed memory */
144     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
145         "VirtualQueryEx failed\n");
146     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
147     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
148     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
149     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
150     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
151     /* this time NT reports PAGE_NOACCESS as well */
152     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
153     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
154
155     /* this should fail, since not the whole range is committed yet */
156     SetLastError(0xdeadbeef);
157     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
158         "VirtualProtectEx should fail on a not committed memory\n");
159     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
160        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
161         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
162
163     old_prot = 0;
164     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
165     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
166
167     old_prot = 0;
168     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
169     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
170
171     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
172        "VirtualFreeEx should fail with type 0\n");
173     ok(GetLastError() == ERROR_INVALID_PARAMETER,
174         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
175
176     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
177
178     /* if the type is MEM_RELEASE, size must be 0 */
179     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
180        "VirtualFreeEx should fail\n");
181     ok(GetLastError() == ERROR_INVALID_PARAMETER,
182         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
183
184     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
185
186     TerminateProcess(hProcess, 0);
187     CloseHandle(hProcess);
188 }
189
190 static void test_VirtualAlloc(void)
191 {
192     void *addr1, *addr2;
193     DWORD old_prot;
194     MEMORY_BASIC_INFORMATION info;
195
196     SetLastError(0xdeadbeef);
197     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
198     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
199     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
200        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
201         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
202
203     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
204     ok(addr1 != NULL, "VirtualAlloc failed\n");
205
206     /* test a not committed memory */
207     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
208         "VirtualQuery failed\n");
209     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
210     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
211     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
212     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
213     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
214     /* NT reports Protect == 0 for a not committed memory block */
215     ok(info.Protect == 0 /* NT */ ||
216        info.Protect == PAGE_NOACCESS, /* Win9x */
217         "%x != PAGE_NOACCESS\n", info.Protect);
218     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
219
220     SetLastError(0xdeadbeef);
221     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
222        "VirtualProtect should fail on a not committed memory\n");
223     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
224        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
225         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
226
227     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
228     ok(addr1 == addr2, "VirtualAlloc failed\n");
229
230     /* test a committed memory */
231     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
232         "VirtualQuery failed\n");
233     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
234     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
235     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
236     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
237     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
238     /* this time NT reports PAGE_NOACCESS as well */
239     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
240     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
241
242     /* this should fail, since not the whole range is committed yet */
243     SetLastError(0xdeadbeef);
244     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
245         "VirtualProtect should fail on a not committed memory\n");
246     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
247        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
248         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
249
250     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
251     ok(old_prot == PAGE_NOACCESS,
252         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
253
254     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
255     ok(old_prot == PAGE_READONLY,
256         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
257
258     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
259     ok(GetLastError() == ERROR_INVALID_PARAMETER,
260         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
261
262     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
263
264     /* if the type is MEM_RELEASE, size must be 0 */
265     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
266     ok(GetLastError() == ERROR_INVALID_PARAMETER,
267         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
268
269     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
270 }
271
272 static void test_MapViewOfFile(void)
273 {
274     static const char testfile[] = "testfile.xxx";
275     const char *name;
276     HANDLE file, mapping;
277     void *ptr, *ptr2;
278     MEMORY_BASIC_INFORMATION info;
279     BOOL ret;
280
281     SetLastError(0xdeadbeef);
282     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
283     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
284     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
285     SetEndOfFile( file );
286
287     /* read/write mapping */
288
289     SetLastError(0xdeadbeef);
290     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
291     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
292
293     SetLastError(0xdeadbeef);
294     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
295     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
296     UnmapViewOfFile( ptr );
297
298     /* this fails on win9x but succeeds on NT */
299     SetLastError(0xdeadbeef);
300     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
301     if (ptr) UnmapViewOfFile( ptr );
302     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
303
304     SetLastError(0xdeadbeef);
305     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
306     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
307     UnmapViewOfFile( ptr );
308
309     SetLastError(0xdeadbeef);
310     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
311     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
312     UnmapViewOfFile( ptr );
313     CloseHandle( mapping );
314
315     /* read-only mapping */
316
317     SetLastError(0xdeadbeef);
318     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
319     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
320
321     SetLastError(0xdeadbeef);
322     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
323     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
324     UnmapViewOfFile( ptr );
325
326     /* this fails on win9x but succeeds on NT */
327     SetLastError(0xdeadbeef);
328     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
329     if (ptr) UnmapViewOfFile( ptr );
330     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
331
332     SetLastError(0xdeadbeef);
333     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
334     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
335     UnmapViewOfFile( ptr );
336
337     SetLastError(0xdeadbeef);
338     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
339     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
340     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
341         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
342     CloseHandle( mapping );
343
344     /* copy-on-write mapping */
345
346     SetLastError(0xdeadbeef);
347     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
348     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
349
350     SetLastError(0xdeadbeef);
351     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
352     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
353     UnmapViewOfFile( ptr );
354
355     SetLastError(0xdeadbeef);
356     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
357     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
358     UnmapViewOfFile( ptr );
359
360     SetLastError(0xdeadbeef);
361     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
362     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
363     UnmapViewOfFile( ptr );
364
365     SetLastError(0xdeadbeef);
366     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
367     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
368     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
369         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
370     CloseHandle( mapping );
371
372     /* no access mapping */
373
374     SetLastError(0xdeadbeef);
375     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
376     /* fails on NT but succeeds on win9x */
377     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
378     else
379     {
380         SetLastError(0xdeadbeef);
381         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
382         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
383         UnmapViewOfFile( ptr );
384
385         SetLastError(0xdeadbeef);
386         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
387         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
388         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
389
390         SetLastError(0xdeadbeef);
391         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
392         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
393         UnmapViewOfFile( ptr );
394
395         SetLastError(0xdeadbeef);
396         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
397         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
398         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
399
400         CloseHandle( mapping );
401     }
402
403     CloseHandle( file );
404
405     /* now try read-only file */
406
407     SetLastError(0xdeadbeef);
408     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
409     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
410
411     SetLastError(0xdeadbeef);
412     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
413     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
414     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
415         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
416
417     SetLastError(0xdeadbeef);
418     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
419     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
420     CloseHandle( mapping );
421
422     SetLastError(0xdeadbeef);
423     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
424     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
425     CloseHandle( mapping );
426     CloseHandle( file );
427
428     /* now try no access file */
429
430     SetLastError(0xdeadbeef);
431     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
432     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
433
434     SetLastError(0xdeadbeef);
435     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
436     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
437     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
438         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
439
440     SetLastError(0xdeadbeef);
441     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
442     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
443     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
444         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
445
446     SetLastError(0xdeadbeef);
447     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
448     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
449     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
450         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
451
452     CloseHandle( file );
453     DeleteFileA( testfile );
454
455     SetLastError(0xdeadbeef);
456     name = "Local\\Foo";
457     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
458     /* nt4 doesn't have Local\\ */
459     if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
460     {
461         name = "Foo";
462         file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
463     }
464     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
465
466     SetLastError(0xdeadbeef);
467     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
468     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
469     SetLastError(0xdeadbeef);
470     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
471     if (!ptr)
472     {
473         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
474         SetLastError(0xdeadbeef);
475         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
476         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
477         SetLastError(0xdeadbeef);
478         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
479             "VirtualQuery error %u\n", GetLastError() );
480         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
481         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
482         ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
483         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
484         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
485         ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
486     }
487     else win_skip( "no access checks on win9x\n" );
488     UnmapViewOfFile( ptr );
489     CloseHandle( mapping );
490
491     SetLastError(0xdeadbeef);
492     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
493     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
494     SetLastError(0xdeadbeef);
495     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
496     if (!ptr)
497     {
498         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
499         SetLastError(0xdeadbeef);
500         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
501         ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
502         SetLastError(0xdeadbeef);
503         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
504             "VirtualQuery error %u\n", GetLastError() );
505         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
506         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
507         ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
508         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
509         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
510         ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
511     }
512     else win_skip( "no access checks on win9x\n" );
513     UnmapViewOfFile( ptr );
514     CloseHandle( mapping );
515
516     CloseHandle( file );
517
518     /* read/write mapping with SEC_RESERVE */
519     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
520     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
521
522     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
523     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
524
525     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
526     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
527     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
528     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
529
530     ret = VirtualQuery(ptr, &info, sizeof(info));
531     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
532     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
533     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
534     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
535     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
536     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
537     {
538         ok(info.AllocationProtect == PAGE_NOACCESS,
539            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
540         ok(info.Protect == PAGE_NOACCESS,
541            "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
542     }
543     else
544     {
545         ok(info.AllocationProtect == PAGE_READWRITE,
546            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
547         ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
548         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
549     }
550
551     if (ptr != ptr2)
552     {
553         ret = VirtualQuery(ptr2, &info, sizeof(info));
554         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
555         ok(info.BaseAddress == ptr2,
556            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
557         ok(info.AllocationBase == ptr2,
558            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
559         ok(info.AllocationProtect == PAGE_READWRITE,
560            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
561         ok(info.RegionSize == MAPPING_SIZE,
562            "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
563         ok(info.State == MEM_RESERVE,
564            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
565         ok(info.Protect == 0,
566            "Protect should have been 0 instead of 0x%x\n", info.Protect);
567         ok(info.Type == MEM_MAPPED,
568            "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
569     }
570
571     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
572     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
573
574     ret = VirtualQuery(ptr, &info, sizeof(info));
575     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
576     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
577     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
578     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
579     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
580     ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
581     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
582     {
583         ok(info.AllocationProtect == PAGE_NOACCESS,
584            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
585     }
586     else
587     {
588         ok(info.AllocationProtect == PAGE_READWRITE,
589            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
590         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
591     }
592
593     /* shows that the VirtualAlloc above affects the mapping, not just the
594      * virtual memory in this process - it also affects all other processes
595      * with a view of the mapping, but that isn't tested here */
596     if (ptr != ptr2)
597     {
598         ret = VirtualQuery(ptr2, &info, sizeof(info));
599         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
600         ok(info.BaseAddress == ptr2,
601            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
602         ok(info.AllocationBase == ptr2,
603            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
604         ok(info.AllocationProtect == PAGE_READWRITE,
605            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
606         ok(info.RegionSize == 0x10000,
607            "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
608         ok(info.State == MEM_COMMIT,
609            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
610         ok(info.Protect == PAGE_READWRITE,
611            "Protect should have been 0 instead of 0x%x\n", info.Protect);
612         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
613     }
614
615     ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
616     ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
617     if (!ret)
618         ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
619
620     ret = UnmapViewOfFile(ptr2);
621     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
622     ret = UnmapViewOfFile(ptr);
623     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
624     CloseHandle(mapping);
625 }
626
627 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
628                                             ULONG zero_bits, SIZE_T commit_size,
629                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
630                                             ULONG inherit, ULONG alloc_type, ULONG protect );
631 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
632
633 static void test_NtMapViewOfSection(void)
634 {
635     HANDLE hProcess;
636
637     static const char testfile[] = "testfile.xxx";
638     static const char data[] = "test data for NtMapViewOfSection";
639     char buffer[sizeof(data)];
640     HANDLE file, mapping;
641     void *ptr;
642     BOOL ret;
643     DWORD status, written;
644     SIZE_T size, result;
645     LARGE_INTEGER offset;
646
647     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
648     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
649     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
650     {
651         skip( "NtMapViewOfSection not found\n" );
652         return;
653     }
654
655     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
656     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
657     WriteFile( file, data, sizeof(data), &written, NULL );
658     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
659     SetEndOfFile( file );
660
661     /* read/write mapping */
662
663     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
664     ok( mapping != 0, "CreateFileMapping failed\n" );
665
666     hProcess = create_target_process("sleep");
667     ok(hProcess != NULL, "Can't start process\n");
668
669     ptr = NULL;
670     size = 0;
671     offset.QuadPart = 0;
672     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
673     ok( !status, "NtMapViewOfSection failed status %x\n", status );
674
675     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
676     ok( ret, "ReadProcessMemory failed\n" );
677     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
678     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
679
680     status = pNtUnmapViewOfSection( hProcess, ptr );
681     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
682
683     CloseHandle( mapping );
684     CloseHandle( file );
685     DeleteFileA( testfile );
686
687     TerminateProcess(hProcess, 0);
688     CloseHandle(hProcess);
689 }
690
691 static void test_CreateFileMapping(void)
692 {
693     HANDLE handle, handle2;
694
695     /* test case sensitivity */
696
697     SetLastError(0xdeadbeef);
698     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
699                                  "Wine Test Mapping");
700     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
701     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
702
703     SetLastError(0xdeadbeef);
704     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
705                                   "Wine Test Mapping");
706     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
707     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
708     CloseHandle( handle2 );
709
710     SetLastError(0xdeadbeef);
711     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
712                                  "WINE TEST MAPPING");
713     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
714     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
715     CloseHandle( handle2 );
716
717     SetLastError(0xdeadbeef);
718     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
719     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
720     CloseHandle( handle2 );
721
722     SetLastError(0xdeadbeef);
723     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
724     ok( !handle2, "OpenFileMapping succeeded\n");
725     ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
726         "wrong error %u\n", GetLastError());
727
728     CloseHandle( handle );
729 }
730
731 static void test_BadPtr(void)
732 {
733     void *ptr = (void*)1;
734     /* We assume address 1 is not mapped. */
735     ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
736     ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
737     ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
738 }
739
740 static void test_write_watch(void)
741 {
742     char *base;
743     DWORD ret, size, old_prot;
744     MEMORY_BASIC_INFORMATION info;
745     void *results[64];
746     ULONG_PTR count;
747     ULONG pagesize;
748
749     if (!pGetWriteWatch || !pResetWriteWatch)
750     {
751         win_skip( "GetWriteWatch not supported\n" );
752         return;
753     }
754
755     size = 0x10000;
756     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
757     if (!base &&
758         (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
759     {
760         win_skip( "MEM_WRITE_WATCH not supported\n" );
761         return;
762     }
763     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
764     ret = VirtualQuery( base, &info, sizeof(info) );
765     ok(ret, "VirtualQuery failed %u\n", GetLastError());
766     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
767     ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
768     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
769     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
770     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
771     ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
772
773     count = 64;
774     SetLastError( 0xdeadbeef );
775     ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
776     ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
777     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
778         broken( GetLastError() == 0xdeadbeef ), /* win98 */
779         "wrong error %u\n", GetLastError() );
780
781     SetLastError( 0xdeadbeef );
782     ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
783     if (ret)
784     {
785         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
786         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
787     }
788     else  /* win98 */
789     {
790         ok( count == 0, "wrong count %lu\n", count );
791     }
792
793     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
794     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
795     ok( count == 0, "wrong count %lu\n", count );
796
797     base[pagesize + 1] = 0x44;
798
799     count = 64;
800     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
801     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
802     ok( count == 1, "wrong count %lu\n", count );
803     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
804
805     count = 64;
806     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
807     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
808     ok( count == 1, "wrong count %lu\n", count );
809     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
810
811     count = 64;
812     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
813     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
814     ok( count == 0, "wrong count %lu\n", count );
815
816     base[2*pagesize + 3] = 0x11;
817     base[4*pagesize + 8] = 0x11;
818
819     count = 64;
820     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
821     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
822     ok( count == 2, "wrong count %lu\n", count );
823     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
824     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
825
826     count = 64;
827     ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
828     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
829     ok( count == 1, "wrong count %lu\n", count );
830     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
831
832     ret = pResetWriteWatch( base, 3*pagesize );
833     ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
834
835     count = 64;
836     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
837     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
838     ok( count == 1, "wrong count %lu\n", count );
839     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
840
841     *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
842
843     count = 64;
844     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
845     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
846     ok( count == 3, "wrong count %lu\n", count );
847     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
848     ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
849     ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
850
851     count = 1;
852     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
853     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
854     ok( count == 1, "wrong count %lu\n", count );
855     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
856
857     count = 64;
858     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
859     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
860     ok( count == 2, "wrong count %lu\n", count );
861     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
862     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
863
864     /* changing protections doesn't affect watches */
865
866     ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
867     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
868     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
869
870     ret = VirtualQuery( base, &info, sizeof(info) );
871     ok(ret, "VirtualQuery failed %u\n", GetLastError());
872     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
873     ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
874     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
875     ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
876
877     ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
878     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
879     ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
880
881     count = 64;
882     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
883     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
884     ok( count == 2, "wrong count %lu\n", count );
885     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
886     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
887
888     ret = VirtualQuery( base, &info, sizeof(info) );
889     ok(ret, "VirtualQuery failed %u\n", GetLastError());
890     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
891     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
892     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
893     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
894
895     /* some invalid parameter tests */
896
897     SetLastError( 0xdeadbeef );
898     count = 0;
899     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
900     if (ret)
901     {
902         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
903         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
904
905         SetLastError( 0xdeadbeef );
906         ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
907         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
908         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
909
910         SetLastError( 0xdeadbeef );
911         count = 64;
912         ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
913         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
914         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
915
916         SetLastError( 0xdeadbeef );
917         count = 64;
918         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
919         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
920         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
921
922         SetLastError( 0xdeadbeef );
923         count = 0;
924         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
925         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
926         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
927
928         SetLastError( 0xdeadbeef );
929         count = 64;
930         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
931         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
932         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
933
934         SetLastError( 0xdeadbeef );
935         count = 64;
936         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
937         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
938         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
939
940         SetLastError( 0xdeadbeef );
941         count = 64;
942         ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
943         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
944         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
945
946         SetLastError( 0xdeadbeef );
947         count = 64;
948         ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
949         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
950         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
951
952         SetLastError( 0xdeadbeef );
953         ret = pResetWriteWatch( base, 0 );
954         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
955         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
956
957         SetLastError( 0xdeadbeef );
958         ret = pResetWriteWatch( GetModuleHandle(0), size );
959         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
960         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
961     }
962     else  /* win98 is completely different */
963     {
964         SetLastError( 0xdeadbeef );
965         count = 64;
966         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
967         ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
968         ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
969
970         count = 0;
971         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
972         ok( !ret, "GetWriteWatch failed %u\n", ret );
973
974         count = 64;
975         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
976         ok( !ret, "GetWriteWatch failed %u\n", ret );
977
978         count = 64;
979         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
980         ok( !ret, "GetWriteWatch failed %u\n", ret );
981
982         ret = pResetWriteWatch( base, 0 );
983         ok( !ret, "ResetWriteWatch failed %u\n", ret );
984
985         ret = pResetWriteWatch( GetModuleHandle(0), size );
986         ok( !ret, "ResetWriteWatch failed %u\n", ret );
987     }
988
989     VirtualFree( base, 0, MEM_FREE );
990
991     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
992     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
993     VirtualFree( base, 0, MEM_FREE );
994
995     base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
996     ok( !base, "VirtualAlloc succeeded\n" );
997     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
998
999     /* initial protect doesn't matter */
1000
1001     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1002     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1003     base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1004     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1005
1006     count = 64;
1007     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1008     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1009     ok( count == 0, "wrong count %lu\n", count );
1010
1011     ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1012     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1013     ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1014
1015     base[5*pagesize + 200] = 3;
1016
1017     ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1018     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1019     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1020
1021     count = 64;
1022     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1023     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1024     ok( count == 1, "wrong count %lu\n", count );
1025     ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1026
1027     ret = VirtualFree( base, size, MEM_DECOMMIT );
1028     ok( ret, "VirtualFree failed %u\n", GetLastError() );
1029
1030     count = 64;
1031     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1032     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1033     ok( count == 1 || broken(count == 0), /* win98 */
1034         "wrong count %lu\n", count );
1035     if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1036
1037     VirtualFree( base, 0, MEM_FREE );
1038 }
1039
1040 START_TEST(virtual)
1041 {
1042     int argc;
1043     char **argv;
1044     argc = winetest_get_mainargs( &argv );
1045
1046     if (argc >= 3)
1047     {
1048         if (!strcmp(argv[2], "sleep"))
1049         {
1050             Sleep(5000); /* spawned process runs for at most 5 seconds */
1051             return;
1052         }
1053         while (1)
1054         {
1055             void *mem;
1056             BOOL ret;
1057             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1058                                PAGE_EXECUTE_READWRITE);
1059             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1060             if (mem == NULL) break;
1061             ret = VirtualFree(mem, 0, MEM_RELEASE);
1062             ok(ret, "VirtualFree failed %u\n", GetLastError());
1063             if (!ret) break;
1064         }
1065         return;
1066     }
1067
1068     hkernel32 = GetModuleHandleA("kernel32.dll");
1069     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1070     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1071     pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1072     pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1073
1074     test_VirtualAllocEx();
1075     test_VirtualAlloc();
1076     test_MapViewOfFile();
1077     test_NtMapViewOfSection();
1078     test_CreateFileMapping();
1079     test_BadPtr();
1080     test_write_watch();
1081 }