kernel32/tests: Don't compare the result of llseek or hwrite against HFILE_ERROR.
[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     /* invalid protection values */
259     SetLastError(0xdeadbeef);
260     addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
261     ok(!addr2, "VirtualAlloc succeeded\n");
262     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
263
264     SetLastError(0xdeadbeef);
265     addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
266     ok(!addr2, "VirtualAlloc succeeded\n");
267     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
268
269     SetLastError(0xdeadbeef);
270     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
271     ok(!addr2, "VirtualAlloc succeeded\n");
272     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
273
274     SetLastError(0xdeadbeef);
275     ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
276        "VirtualProtect succeeded\n");
277     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
278
279     SetLastError(0xdeadbeef);
280     ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
281     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
282
283     SetLastError(0xdeadbeef);
284     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
285     ok(GetLastError() == ERROR_INVALID_PARAMETER,
286         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
287
288     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
289
290     /* if the type is MEM_RELEASE, size must be 0 */
291     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
292     ok(GetLastError() == ERROR_INVALID_PARAMETER,
293         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
294
295     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
296 }
297
298 static void test_MapViewOfFile(void)
299 {
300     static const char testfile[] = "testfile.xxx";
301     const char *name;
302     HANDLE file, mapping;
303     void *ptr, *ptr2;
304     MEMORY_BASIC_INFORMATION info;
305     BOOL ret;
306
307     SetLastError(0xdeadbeef);
308     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
309     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
310     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
311     SetEndOfFile( file );
312
313     /* read/write mapping */
314
315     SetLastError(0xdeadbeef);
316     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
317     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
318
319     SetLastError(0xdeadbeef);
320     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
321     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
322     UnmapViewOfFile( ptr );
323
324     /* this fails on win9x but succeeds on NT */
325     SetLastError(0xdeadbeef);
326     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
327     if (ptr) UnmapViewOfFile( ptr );
328     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
329
330     SetLastError(0xdeadbeef);
331     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
332     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
333     UnmapViewOfFile( ptr );
334
335     SetLastError(0xdeadbeef);
336     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
337     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
338     UnmapViewOfFile( ptr );
339     CloseHandle( mapping );
340
341     /* read-only mapping */
342
343     SetLastError(0xdeadbeef);
344     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
345     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
346
347     SetLastError(0xdeadbeef);
348     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
349     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
350     UnmapViewOfFile( ptr );
351
352     /* this fails on win9x but succeeds on NT */
353     SetLastError(0xdeadbeef);
354     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
355     if (ptr) UnmapViewOfFile( ptr );
356     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
357
358     SetLastError(0xdeadbeef);
359     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
360     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
361     UnmapViewOfFile( ptr );
362
363     SetLastError(0xdeadbeef);
364     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
365     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
366     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
367         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
368     CloseHandle( mapping );
369
370     /* copy-on-write mapping */
371
372     SetLastError(0xdeadbeef);
373     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
374     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
375
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 );
380
381     SetLastError(0xdeadbeef);
382     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
383     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
384     UnmapViewOfFile( ptr );
385
386     SetLastError(0xdeadbeef);
387     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
388     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
389     UnmapViewOfFile( ptr );
390
391     SetLastError(0xdeadbeef);
392     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
393     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
394     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
395         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
396     CloseHandle( mapping );
397
398     /* no access mapping */
399
400     SetLastError(0xdeadbeef);
401     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
402     /* fails on NT but succeeds on win9x */
403     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
404     else
405     {
406         SetLastError(0xdeadbeef);
407         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
408         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
409         UnmapViewOfFile( ptr );
410
411         SetLastError(0xdeadbeef);
412         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
413         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
414         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
415
416         SetLastError(0xdeadbeef);
417         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
418         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
419         UnmapViewOfFile( ptr );
420
421         SetLastError(0xdeadbeef);
422         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
423         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
424         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
425
426         CloseHandle( mapping );
427     }
428
429     CloseHandle( file );
430
431     /* now try read-only file */
432
433     SetLastError(0xdeadbeef);
434     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
435     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
436
437     SetLastError(0xdeadbeef);
438     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
439     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
440     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
441         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
442
443     SetLastError(0xdeadbeef);
444     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
445     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
446     CloseHandle( mapping );
447
448     SetLastError(0xdeadbeef);
449     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
450     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
451     CloseHandle( mapping );
452     CloseHandle( file );
453
454     /* now try no access file */
455
456     SetLastError(0xdeadbeef);
457     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
458     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
459
460     SetLastError(0xdeadbeef);
461     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
462     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
463     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
464         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
465
466     SetLastError(0xdeadbeef);
467     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
468     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
469     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
470         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
471
472     SetLastError(0xdeadbeef);
473     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
474     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
475     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
476         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
477
478     CloseHandle( file );
479     DeleteFileA( testfile );
480
481     SetLastError(0xdeadbeef);
482     name = "Local\\Foo";
483     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
484     /* nt4 doesn't have Local\\ */
485     if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
486     {
487         name = "Foo";
488         file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
489     }
490     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
491
492     SetLastError(0xdeadbeef);
493     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
494     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
495     SetLastError(0xdeadbeef);
496     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
497     if (!ptr)
498     {
499         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
500         SetLastError(0xdeadbeef);
501         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
502         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
503         SetLastError(0xdeadbeef);
504         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
505             "VirtualQuery error %u\n", GetLastError() );
506         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
507         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
508         ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
509         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
510         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
511         ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
512     }
513     else win_skip( "no access checks on win9x\n" );
514     UnmapViewOfFile( ptr );
515     CloseHandle( mapping );
516
517     SetLastError(0xdeadbeef);
518     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
519     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
520     SetLastError(0xdeadbeef);
521     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
522     if (!ptr)
523     {
524         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
525         SetLastError(0xdeadbeef);
526         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
527         ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
528         SetLastError(0xdeadbeef);
529         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
530             "VirtualQuery error %u\n", GetLastError() );
531         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
532         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
533         ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
534         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
535         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
536         ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
537     }
538     else win_skip( "no access checks on win9x\n" );
539     UnmapViewOfFile( ptr );
540     CloseHandle( mapping );
541
542     CloseHandle( file );
543
544     /* read/write mapping with SEC_RESERVE */
545     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
546     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
547
548     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
549     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
550
551     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
552     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
553     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
554     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
555
556     ret = VirtualQuery(ptr, &info, sizeof(info));
557     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
558     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
559     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
560     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
561     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
562     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
563     {
564         ok(info.AllocationProtect == PAGE_NOACCESS,
565            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
566         ok(info.Protect == PAGE_NOACCESS,
567            "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
568     }
569     else
570     {
571         ok(info.AllocationProtect == PAGE_READWRITE,
572            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
573         ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
574         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
575     }
576
577     if (ptr != ptr2)
578     {
579         ret = VirtualQuery(ptr2, &info, sizeof(info));
580         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
581         ok(info.BaseAddress == ptr2,
582            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
583         ok(info.AllocationBase == ptr2,
584            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
585         ok(info.AllocationProtect == PAGE_READWRITE,
586            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
587         ok(info.RegionSize == MAPPING_SIZE,
588            "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
589         ok(info.State == MEM_RESERVE,
590            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
591         ok(info.Protect == 0,
592            "Protect should have been 0 instead of 0x%x\n", info.Protect);
593         ok(info.Type == MEM_MAPPED,
594            "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
595     }
596
597     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
598     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
599
600     ret = VirtualQuery(ptr, &info, sizeof(info));
601     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
602     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
603     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
604     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
605     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
606     ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
607     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
608     {
609         ok(info.AllocationProtect == PAGE_NOACCESS,
610            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
611     }
612     else
613     {
614         ok(info.AllocationProtect == PAGE_READWRITE,
615            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
616         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
617     }
618
619     /* shows that the VirtualAlloc above affects the mapping, not just the
620      * virtual memory in this process - it also affects all other processes
621      * with a view of the mapping, but that isn't tested here */
622     if (ptr != ptr2)
623     {
624         ret = VirtualQuery(ptr2, &info, sizeof(info));
625         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
626         ok(info.BaseAddress == ptr2,
627            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
628         ok(info.AllocationBase == ptr2,
629            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
630         ok(info.AllocationProtect == PAGE_READWRITE,
631            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
632         ok(info.RegionSize == 0x10000,
633            "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
634         ok(info.State == MEM_COMMIT,
635            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
636         ok(info.Protect == PAGE_READWRITE,
637            "Protect should have been 0 instead of 0x%x\n", info.Protect);
638         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
639     }
640
641     ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
642     ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
643     if (!ret)
644         ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
645
646     ret = UnmapViewOfFile(ptr2);
647     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
648     ret = UnmapViewOfFile(ptr);
649     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
650     CloseHandle(mapping);
651 }
652
653 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
654                                             ULONG zero_bits, SIZE_T commit_size,
655                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
656                                             ULONG inherit, ULONG alloc_type, ULONG protect );
657 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
658
659 static void test_NtMapViewOfSection(void)
660 {
661     HANDLE hProcess;
662
663     static const char testfile[] = "testfile.xxx";
664     static const char data[] = "test data for NtMapViewOfSection";
665     char buffer[sizeof(data)];
666     HANDLE file, mapping;
667     void *ptr;
668     BOOL ret;
669     DWORD status, written;
670     SIZE_T size, result;
671     LARGE_INTEGER offset;
672
673     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
674     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
675     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
676     {
677         skip( "NtMapViewOfSection not found\n" );
678         return;
679     }
680
681     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
682     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
683     WriteFile( file, data, sizeof(data), &written, NULL );
684     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
685     SetEndOfFile( file );
686
687     /* read/write mapping */
688
689     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
690     ok( mapping != 0, "CreateFileMapping failed\n" );
691
692     hProcess = create_target_process("sleep");
693     ok(hProcess != NULL, "Can't start process\n");
694
695     ptr = NULL;
696     size = 0;
697     offset.QuadPart = 0;
698     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
699     ok( !status, "NtMapViewOfSection failed status %x\n", status );
700
701     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
702     ok( ret, "ReadProcessMemory failed\n" );
703     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
704     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
705
706     status = pNtUnmapViewOfSection( hProcess, ptr );
707     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
708
709     CloseHandle( mapping );
710     CloseHandle( file );
711     DeleteFileA( testfile );
712
713     TerminateProcess(hProcess, 0);
714     CloseHandle(hProcess);
715 }
716
717 static void test_CreateFileMapping(void)
718 {
719     HANDLE handle, handle2;
720
721     /* test case sensitivity */
722
723     SetLastError(0xdeadbeef);
724     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
725                                  "Wine Test Mapping");
726     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
727     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
728
729     SetLastError(0xdeadbeef);
730     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
731                                   "Wine Test Mapping");
732     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
733     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
734     CloseHandle( handle2 );
735
736     SetLastError(0xdeadbeef);
737     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
738                                  "WINE TEST MAPPING");
739     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
740     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
741     CloseHandle( handle2 );
742
743     SetLastError(0xdeadbeef);
744     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
745     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
746     CloseHandle( handle2 );
747
748     SetLastError(0xdeadbeef);
749     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
750     ok( !handle2, "OpenFileMapping succeeded\n");
751     ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
752         "wrong error %u\n", GetLastError());
753
754     CloseHandle( handle );
755 }
756
757 static void test_BadPtr(void)
758 {
759     void *ptr = (void*)1;
760     /* We assume address 1 is not mapped. */
761     ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
762     ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
763     ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
764 }
765
766 static void test_write_watch(void)
767 {
768     char *base;
769     DWORD ret, size, old_prot;
770     MEMORY_BASIC_INFORMATION info;
771     void *results[64];
772     ULONG_PTR count;
773     ULONG pagesize;
774
775     if (!pGetWriteWatch || !pResetWriteWatch)
776     {
777         win_skip( "GetWriteWatch not supported\n" );
778         return;
779     }
780
781     size = 0x10000;
782     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
783     if (!base &&
784         (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
785     {
786         win_skip( "MEM_WRITE_WATCH not supported\n" );
787         return;
788     }
789     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
790     ret = VirtualQuery( base, &info, sizeof(info) );
791     ok(ret, "VirtualQuery failed %u\n", GetLastError());
792     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
793     ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
794     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
795     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
796     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
797     ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
798
799     count = 64;
800     SetLastError( 0xdeadbeef );
801     ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
802     ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
803     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
804         broken( GetLastError() == 0xdeadbeef ), /* win98 */
805         "wrong error %u\n", GetLastError() );
806
807     SetLastError( 0xdeadbeef );
808     ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
809     if (ret)
810     {
811         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
812         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
813     }
814     else  /* win98 */
815     {
816         ok( count == 0, "wrong count %lu\n", count );
817     }
818
819     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
820     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
821     ok( count == 0, "wrong count %lu\n", count );
822
823     base[pagesize + 1] = 0x44;
824
825     count = 64;
826     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
827     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
828     ok( count == 1, "wrong count %lu\n", count );
829     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
830
831     count = 64;
832     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
833     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
834     ok( count == 1, "wrong count %lu\n", count );
835     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
836
837     count = 64;
838     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
839     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
840     ok( count == 0, "wrong count %lu\n", count );
841
842     base[2*pagesize + 3] = 0x11;
843     base[4*pagesize + 8] = 0x11;
844
845     count = 64;
846     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
847     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
848     ok( count == 2, "wrong count %lu\n", count );
849     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
850     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
851
852     count = 64;
853     ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
854     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
855     ok( count == 1, "wrong count %lu\n", count );
856     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
857
858     ret = pResetWriteWatch( base, 3*pagesize );
859     ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
860
861     count = 64;
862     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
863     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
864     ok( count == 1, "wrong count %lu\n", count );
865     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
866
867     *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
868
869     count = 64;
870     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
871     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
872     ok( count == 3, "wrong count %lu\n", count );
873     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
874     ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
875     ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
876
877     count = 1;
878     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
879     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
880     ok( count == 1, "wrong count %lu\n", count );
881     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
882
883     count = 64;
884     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
885     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
886     ok( count == 2, "wrong count %lu\n", count );
887     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
888     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
889
890     /* changing protections doesn't affect watches */
891
892     ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
893     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
894     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
895
896     ret = VirtualQuery( base, &info, sizeof(info) );
897     ok(ret, "VirtualQuery failed %u\n", GetLastError());
898     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
899     ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
900     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
901     ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
902
903     ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
904     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
905     ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
906
907     count = 64;
908     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
909     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
910     ok( count == 2, "wrong count %lu\n", count );
911     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
912     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
913
914     ret = VirtualQuery( base, &info, sizeof(info) );
915     ok(ret, "VirtualQuery failed %u\n", GetLastError());
916     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
917     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
918     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
919     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
920
921     /* some invalid parameter tests */
922
923     SetLastError( 0xdeadbeef );
924     count = 0;
925     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
926     if (ret)
927     {
928         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
929         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
930
931         SetLastError( 0xdeadbeef );
932         ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
933         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
934         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
935
936         SetLastError( 0xdeadbeef );
937         count = 64;
938         ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
939         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
940         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
941
942         SetLastError( 0xdeadbeef );
943         count = 64;
944         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
945         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
946         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
947
948         SetLastError( 0xdeadbeef );
949         count = 0;
950         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
951         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
952         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
953
954         SetLastError( 0xdeadbeef );
955         count = 64;
956         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
957         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
958         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
959
960         SetLastError( 0xdeadbeef );
961         count = 64;
962         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
963         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
964         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
965
966         SetLastError( 0xdeadbeef );
967         count = 64;
968         ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
969         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
970         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
971
972         SetLastError( 0xdeadbeef );
973         count = 64;
974         ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
975         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
976         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
977
978         SetLastError( 0xdeadbeef );
979         ret = pResetWriteWatch( base, 0 );
980         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
981         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
982
983         SetLastError( 0xdeadbeef );
984         ret = pResetWriteWatch( GetModuleHandle(0), size );
985         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
986         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
987     }
988     else  /* win98 is completely different */
989     {
990         SetLastError( 0xdeadbeef );
991         count = 64;
992         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
993         ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
994         ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
995
996         count = 0;
997         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
998         ok( !ret, "GetWriteWatch failed %u\n", ret );
999
1000         count = 64;
1001         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1002         ok( !ret, "GetWriteWatch failed %u\n", ret );
1003
1004         count = 64;
1005         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1006         ok( !ret, "GetWriteWatch failed %u\n", ret );
1007
1008         ret = pResetWriteWatch( base, 0 );
1009         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1010
1011         ret = pResetWriteWatch( GetModuleHandle(0), size );
1012         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1013     }
1014
1015     VirtualFree( base, 0, MEM_FREE );
1016
1017     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1018     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1019     VirtualFree( base, 0, MEM_FREE );
1020
1021     base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1022     ok( !base, "VirtualAlloc succeeded\n" );
1023     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1024
1025     /* initial protect doesn't matter */
1026
1027     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1028     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1029     base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1030     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1031
1032     count = 64;
1033     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1034     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1035     ok( count == 0, "wrong count %lu\n", count );
1036
1037     ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1038     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1039     ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1040
1041     base[5*pagesize + 200] = 3;
1042
1043     ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1044     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1045     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1046
1047     count = 64;
1048     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1049     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1050     ok( count == 1, "wrong count %lu\n", count );
1051     ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1052
1053     ret = VirtualFree( base, size, MEM_DECOMMIT );
1054     ok( ret, "VirtualFree failed %u\n", GetLastError() );
1055
1056     count = 64;
1057     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1058     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1059     ok( count == 1 || broken(count == 0), /* win98 */
1060         "wrong count %lu\n", count );
1061     if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1062
1063     VirtualFree( base, 0, MEM_FREE );
1064 }
1065
1066 START_TEST(virtual)
1067 {
1068     int argc;
1069     char **argv;
1070     argc = winetest_get_mainargs( &argv );
1071
1072     if (argc >= 3)
1073     {
1074         if (!strcmp(argv[2], "sleep"))
1075         {
1076             Sleep(5000); /* spawned process runs for at most 5 seconds */
1077             return;
1078         }
1079         while (1)
1080         {
1081             void *mem;
1082             BOOL ret;
1083             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1084                                PAGE_EXECUTE_READWRITE);
1085             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1086             if (mem == NULL) break;
1087             ret = VirtualFree(mem, 0, MEM_RELEASE);
1088             ok(ret, "VirtualFree failed %u\n", GetLastError());
1089             if (!ret) break;
1090         }
1091         return;
1092     }
1093
1094     hkernel32 = GetModuleHandleA("kernel32.dll");
1095     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1096     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1097     pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1098     pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1099
1100     test_VirtualAllocEx();
1101     test_VirtualAlloc();
1102     test_MapViewOfFile();
1103     test_NtMapViewOfSection();
1104     test_CreateFileMapping();
1105     test_BadPtr();
1106     test_write_watch();
1107 }