include: Define more wuapi interfaces to avoid undefined forward declarations.
[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 "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "winerror.h"
30 #include "wine/test.h"
31
32 #define NUM_THREADS 4
33 #define MAPPING_SIZE 0x100000
34
35 static HINSTANCE hkernel32;
36 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
37 static BOOL   (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
38 static UINT   (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
39 static UINT   (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
40 static NTSTATUS (WINAPI *pNtAreMappedFilesTheSame)(PVOID,PVOID);
41
42 /* ############################### */
43
44 static HANDLE create_target_process(const char *arg)
45 {
46     char **argv;
47     char cmdline[MAX_PATH];
48     PROCESS_INFORMATION pi;
49     STARTUPINFO si = { 0 };
50     si.cb = sizeof(si);
51
52     winetest_get_mainargs( &argv );
53     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
54     ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
55                      &si, &pi) != 0, "error: %u\n", GetLastError());
56     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
57     return pi.hProcess;
58 }
59
60 static void test_VirtualAllocEx(void)
61 {
62     const unsigned int alloc_size = 1<<15;
63     char *src, *dst;
64     SIZE_T bytes_written = 0, bytes_read = 0, i;
65     void *addr1, *addr2;
66     BOOL b;
67     DWORD old_prot;
68     MEMORY_BASIC_INFORMATION info;
69     HANDLE hProcess;
70
71     /* not exported in all windows-versions  */
72     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
73         win_skip("Virtual{Alloc,Free}Ex not available\n");
74         return;
75     }
76
77     hProcess = create_target_process("sleep");
78     ok(hProcess != NULL, "Can't start process\n");
79
80     SetLastError(0xdeadbeef);
81     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
82                            PAGE_EXECUTE_READWRITE);
83     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
84     {   /* Win9x */
85         win_skip("VirtualAllocEx not implemented\n");
86         TerminateProcess(hProcess, 0);
87         CloseHandle(hProcess);
88         return;
89     }
90
91     src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
92     dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
93     for (i = 0; i < alloc_size; i++)
94         src[i] = i & 0xff;
95
96     ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
97     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
98     ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
99        bytes_written);
100     b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
101     ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
102     ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
103
104     /* test invalid source buffers */
105
106     b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
107     ok( b, "VirtualProtect failed error %u\n", GetLastError() );
108     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
109     ok( !b, "WriteProcessMemory succeeded\n" );
110     ok( GetLastError() == ERROR_NOACCESS ||
111         GetLastError() == ERROR_PARTIAL_COPY, /* vista */
112         "wrong error %u\n", GetLastError() );
113     ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
114     b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
115     ok( !b, "ReadProcessMemory succeeded\n" );
116     ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
117     ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
118
119     b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
120     ok( b, "VirtualProtect failed error %u\n", GetLastError() );
121     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
122     ok( !b, "WriteProcessMemory succeeded\n" );
123     ok( GetLastError() == ERROR_NOACCESS ||
124         GetLastError() == ERROR_PARTIAL_COPY, /* vista */
125         "wrong error %u\n", GetLastError() );
126     ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
127     b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
128     ok( !b, "ReadProcessMemory succeeded\n" );
129     ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
130     ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
131
132     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
133     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
134
135     VirtualFree( src, 0, MEM_FREE );
136     VirtualFree( dst, 0, MEM_FREE );
137
138     /*
139      * The following tests parallel those in test_VirtualAlloc()
140      */
141
142     SetLastError(0xdeadbeef);
143     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
144     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
145     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
146        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
147         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
148
149     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
150     ok(addr1 != NULL, "VirtualAllocEx failed\n");
151
152     /* test a not committed memory */
153     memset(&info, 'q', sizeof(info));
154     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
155     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
156     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
157     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
158     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
159     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
160     /* NT reports Protect == 0 for a not committed memory block */
161     ok(info.Protect == 0 /* NT */ ||
162        info.Protect == PAGE_NOACCESS, /* Win9x */
163         "%x != PAGE_NOACCESS\n", info.Protect);
164     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
165
166     SetLastError(0xdeadbeef);
167     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
168        "VirtualProtectEx should fail on a not committed memory\n");
169     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
170        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
171         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
172
173     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
174     ok(addr1 == addr2, "VirtualAllocEx failed\n");
175
176     /* test a committed memory */
177     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
178         "VirtualQueryEx failed\n");
179     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
180     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
181     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
182     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
183     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
184     /* this time NT reports PAGE_NOACCESS as well */
185     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
186     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
187
188     /* this should fail, since not the whole range is committed yet */
189     SetLastError(0xdeadbeef);
190     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
191         "VirtualProtectEx should fail on a not committed memory\n");
192     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
193        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
194         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
195
196     old_prot = 0;
197     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
198     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
199
200     old_prot = 0;
201     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
202     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
203
204     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
205        "VirtualFreeEx should fail with type 0\n");
206     ok(GetLastError() == ERROR_INVALID_PARAMETER,
207         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
208
209     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
210
211     /* if the type is MEM_RELEASE, size must be 0 */
212     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
213        "VirtualFreeEx should fail\n");
214     ok(GetLastError() == ERROR_INVALID_PARAMETER,
215         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
216
217     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
218
219     TerminateProcess(hProcess, 0);
220     CloseHandle(hProcess);
221 }
222
223 static void test_VirtualAlloc(void)
224 {
225     void *addr1, *addr2;
226     DWORD old_prot;
227     MEMORY_BASIC_INFORMATION info;
228
229     SetLastError(0xdeadbeef);
230     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
231     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
232     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
233        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
234         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
235
236     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
237     ok(addr1 != NULL, "VirtualAlloc failed\n");
238
239     /* test a not committed memory */
240     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
241         "VirtualQuery failed\n");
242     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
243     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
244     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
245     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
246     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
247     /* NT reports Protect == 0 for a not committed memory block */
248     ok(info.Protect == 0 /* NT */ ||
249        info.Protect == PAGE_NOACCESS, /* Win9x */
250         "%x != PAGE_NOACCESS\n", info.Protect);
251     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
252
253     SetLastError(0xdeadbeef);
254     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
255        "VirtualProtect should fail on a not committed memory\n");
256     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
257        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
258         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
259
260     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
261     ok(addr1 == addr2, "VirtualAlloc failed\n");
262
263     /* test a committed memory */
264     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
265         "VirtualQuery failed\n");
266     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
267     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
268     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
269     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
270     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
271     /* this time NT reports PAGE_NOACCESS as well */
272     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
273     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
274
275     /* this should fail, since not the whole range is committed yet */
276     SetLastError(0xdeadbeef);
277     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
278         "VirtualProtect should fail on a not committed memory\n");
279     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
280        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
281         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
282
283     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
284     ok(old_prot == PAGE_NOACCESS,
285         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
286
287     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
288     ok(old_prot == PAGE_READONLY,
289         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
290
291     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
292         "VirtualQuery failed\n");
293     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
294     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
295     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
296     memset( addr1, 0x55, 20 );
297     ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
298
299     addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
300     ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
301         "VirtualAlloc failed err %u\n", GetLastError() );
302     ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
303     if (addr2)
304     {
305         ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
306            "VirtualQuery failed\n");
307         ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
308         ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
309         ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
310
311         addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
312         ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
313
314         ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
315            "VirtualQuery failed\n");
316         ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
317         ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
318         ok(info.Protect == 0, "%x != 0\n", info.Protect);
319
320         addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
321         ok( !addr2, "VirtualAlloc failed\n" );
322         ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
323     }
324
325     /* invalid protection values */
326     SetLastError(0xdeadbeef);
327     addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
328     ok(!addr2, "VirtualAlloc succeeded\n");
329     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
330
331     SetLastError(0xdeadbeef);
332     addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
333     ok(!addr2, "VirtualAlloc succeeded\n");
334     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
335
336     SetLastError(0xdeadbeef);
337     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
338     ok(!addr2, "VirtualAlloc succeeded\n");
339     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
340
341     SetLastError(0xdeadbeef);
342     ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
343        "VirtualProtect succeeded\n");
344     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
345
346     SetLastError(0xdeadbeef);
347     ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
348     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
349
350     SetLastError(0xdeadbeef);
351     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
352     ok(GetLastError() == ERROR_INVALID_PARAMETER,
353         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
354
355     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
356
357     /* if the type is MEM_RELEASE, size must be 0 */
358     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
359     ok(GetLastError() == ERROR_INVALID_PARAMETER,
360         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
361
362     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
363 }
364
365 static void test_MapViewOfFile(void)
366 {
367     static const char testfile[] = "testfile.xxx";
368     const char *name;
369     HANDLE file, mapping, map2;
370     void *ptr, *ptr2, *addr;
371     MEMORY_BASIC_INFORMATION info;
372     BOOL ret;
373
374     SetLastError(0xdeadbeef);
375     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
376     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
377     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
378     SetEndOfFile( file );
379
380     /* read/write mapping */
381
382     SetLastError(0xdeadbeef);
383     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
384     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
385
386     SetLastError(0xdeadbeef);
387     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
388     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
389     UnmapViewOfFile( ptr );
390
391     /* this fails on win9x but succeeds on NT */
392     SetLastError(0xdeadbeef);
393     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
394     if (ptr) UnmapViewOfFile( ptr );
395     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
396
397     SetLastError(0xdeadbeef);
398     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
399     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
400     UnmapViewOfFile( ptr );
401
402     SetLastError(0xdeadbeef);
403     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
404     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
405     UnmapViewOfFile( ptr );
406
407     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
408                            FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
409     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
410     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
411     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
412     UnmapViewOfFile( ptr );
413     CloseHandle( map2 );
414
415     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
416                            FILE_MAP_READ, FALSE, 0 );
417     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
418     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
419     if (!ptr)
420     {
421         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
422         CloseHandle( map2 );
423         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
424         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
425         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
426         ok( !ptr, "MapViewOfFile succeeded\n" );
427         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
428         CloseHandle( map2 );
429         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
430                                FILE_MAP_READ, FALSE, 0 );
431         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
432         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
433         ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
434     }
435     else win_skip( "no access checks on win9x\n" );
436
437     UnmapViewOfFile( ptr );
438     CloseHandle( map2 );
439     CloseHandle( mapping );
440
441     /* read-only mapping */
442
443     SetLastError(0xdeadbeef);
444     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
445     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
446
447     SetLastError(0xdeadbeef);
448     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
449     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
450     UnmapViewOfFile( ptr );
451
452     /* this fails on win9x but succeeds on NT */
453     SetLastError(0xdeadbeef);
454     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
455     if (ptr) UnmapViewOfFile( ptr );
456     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
457
458     SetLastError(0xdeadbeef);
459     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
460     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
461     UnmapViewOfFile( ptr );
462
463     SetLastError(0xdeadbeef);
464     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
465     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
466     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
467         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
468     CloseHandle( mapping );
469
470     /* copy-on-write mapping */
471
472     SetLastError(0xdeadbeef);
473     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
474     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
475
476     SetLastError(0xdeadbeef);
477     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
478     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
479     UnmapViewOfFile( ptr );
480
481     SetLastError(0xdeadbeef);
482     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
483     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
484     UnmapViewOfFile( ptr );
485
486     SetLastError(0xdeadbeef);
487     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
488     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
489     UnmapViewOfFile( ptr );
490
491     SetLastError(0xdeadbeef);
492     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
493     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
494     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
495         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
496     CloseHandle( mapping );
497
498     /* no access mapping */
499
500     SetLastError(0xdeadbeef);
501     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
502     /* fails on NT but succeeds on win9x */
503     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
504     else
505     {
506         SetLastError(0xdeadbeef);
507         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
508         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
509         UnmapViewOfFile( ptr );
510
511         SetLastError(0xdeadbeef);
512         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
513         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
514         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
515
516         SetLastError(0xdeadbeef);
517         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
518         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
519         UnmapViewOfFile( ptr );
520
521         SetLastError(0xdeadbeef);
522         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
523         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
524         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
525
526         CloseHandle( mapping );
527     }
528
529     CloseHandle( file );
530
531     /* now try read-only file */
532
533     SetLastError(0xdeadbeef);
534     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
535     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
536
537     SetLastError(0xdeadbeef);
538     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
539     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
540     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
541         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
542
543     SetLastError(0xdeadbeef);
544     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
545     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
546     CloseHandle( mapping );
547
548     SetLastError(0xdeadbeef);
549     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
550     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
551     CloseHandle( mapping );
552     CloseHandle( file );
553
554     /* now try no access file */
555
556     SetLastError(0xdeadbeef);
557     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
558     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
559
560     SetLastError(0xdeadbeef);
561     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
562     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
563     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
564         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
565
566     SetLastError(0xdeadbeef);
567     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
568     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
569     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
570         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
571
572     SetLastError(0xdeadbeef);
573     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
574     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
575     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
576         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
577
578     CloseHandle( file );
579     DeleteFileA( testfile );
580
581     SetLastError(0xdeadbeef);
582     name = "Local\\Foo";
583     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
584     /* nt4 doesn't have Local\\ */
585     if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
586     {
587         name = "Foo";
588         file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
589     }
590     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
591
592     SetLastError(0xdeadbeef);
593     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
594     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
595     SetLastError(0xdeadbeef);
596     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
597     if (!ptr)
598     {
599         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
600         SetLastError(0xdeadbeef);
601         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
602         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
603         SetLastError(0xdeadbeef);
604         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
605             "VirtualQuery error %u\n", GetLastError() );
606         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
607         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
608         ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
609         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
610         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
611         ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
612     }
613     else win_skip( "no access checks on win9x\n" );
614     UnmapViewOfFile( ptr );
615     CloseHandle( mapping );
616
617     SetLastError(0xdeadbeef);
618     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
619     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
620     SetLastError(0xdeadbeef);
621     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
622     if (!ptr)
623     {
624         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
625         SetLastError(0xdeadbeef);
626         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
627         ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
628         SetLastError(0xdeadbeef);
629         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
630             "VirtualQuery error %u\n", GetLastError() );
631         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
632         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
633         ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
634         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
635         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
636         ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
637     }
638     else win_skip( "no access checks on win9x\n" );
639     UnmapViewOfFile( ptr );
640     CloseHandle( mapping );
641
642     CloseHandle( file );
643
644     /* read/write mapping with SEC_RESERVE */
645     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
646     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
647
648     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
649     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
650
651     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
652     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
653     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
654     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
655
656     ret = VirtualQuery(ptr, &info, sizeof(info));
657     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
658     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
659     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
660     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
661     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
662     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
663     {
664         ok(info.AllocationProtect == PAGE_NOACCESS,
665            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
666         ok(info.Protect == PAGE_NOACCESS,
667            "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
668     }
669     else
670     {
671         ok(info.AllocationProtect == PAGE_READWRITE,
672            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
673         ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
674         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
675     }
676
677     if (ptr != ptr2)
678     {
679         ret = VirtualQuery(ptr2, &info, sizeof(info));
680         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
681         ok(info.BaseAddress == ptr2,
682            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
683         ok(info.AllocationBase == ptr2,
684            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
685         ok(info.AllocationProtect == PAGE_READWRITE,
686            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
687         ok(info.RegionSize == MAPPING_SIZE,
688            "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
689         ok(info.State == MEM_RESERVE,
690            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
691         ok(info.Protect == 0,
692            "Protect should have been 0 instead of 0x%x\n", info.Protect);
693         ok(info.Type == MEM_MAPPED,
694            "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
695     }
696
697     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
698     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
699
700     ret = VirtualQuery(ptr, &info, sizeof(info));
701     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
702     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
703     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
704     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
705     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
706     ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
707     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
708     {
709         ok(info.AllocationProtect == PAGE_NOACCESS,
710            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
711     }
712     else
713     {
714         ok(info.AllocationProtect == PAGE_READWRITE,
715            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
716         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
717     }
718
719     /* shows that the VirtualAlloc above affects the mapping, not just the
720      * virtual memory in this process - it also affects all other processes
721      * with a view of the mapping, but that isn't tested here */
722     if (ptr != ptr2)
723     {
724         ret = VirtualQuery(ptr2, &info, sizeof(info));
725         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
726         ok(info.BaseAddress == ptr2,
727            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
728         ok(info.AllocationBase == ptr2,
729            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
730         ok(info.AllocationProtect == PAGE_READWRITE,
731            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
732         ok(info.RegionSize == 0x10000,
733            "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
734         ok(info.State == MEM_COMMIT,
735            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
736         ok(info.Protect == PAGE_READWRITE,
737            "Protect should have been 0 instead of 0x%x\n", info.Protect);
738         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
739     }
740
741     addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
742     ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
743         "VirtualAlloc failed with error %u\n", GetLastError() );
744
745     ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
746     ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
747     if (!ret)
748         ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
749
750     ret = UnmapViewOfFile(ptr2);
751     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
752     ret = UnmapViewOfFile(ptr);
753     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
754     CloseHandle(mapping);
755
756     addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
757     ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
758
759     SetLastError(0xdeadbeef);
760     ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
761     ok( GetLastError() == ERROR_INVALID_ADDRESS,
762         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
763     SetLastError(0xdeadbeef);
764     ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
765     ok( GetLastError() == ERROR_INVALID_ADDRESS,
766         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
767     SetLastError(0xdeadbeef);
768     ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
769     ok( GetLastError() == ERROR_INVALID_ADDRESS,
770        "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
771
772     ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
773 }
774
775 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
776                                             ULONG zero_bits, SIZE_T commit_size,
777                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
778                                             ULONG inherit, ULONG alloc_type, ULONG protect );
779 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
780
781 static void test_NtMapViewOfSection(void)
782 {
783     HANDLE hProcess;
784
785     static const char testfile[] = "testfile.xxx";
786     static const char data[] = "test data for NtMapViewOfSection";
787     char buffer[sizeof(data)];
788     HANDLE file, mapping;
789     void *ptr;
790     BOOL ret;
791     DWORD status, written;
792     SIZE_T size, result;
793     LARGE_INTEGER offset;
794
795     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
796     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
797     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
798     {
799         win_skip( "NtMapViewOfSection not available\n" );
800         return;
801     }
802
803     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
804     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
805     WriteFile( file, data, sizeof(data), &written, NULL );
806     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
807     SetEndOfFile( file );
808
809     /* read/write mapping */
810
811     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
812     ok( mapping != 0, "CreateFileMapping failed\n" );
813
814     hProcess = create_target_process("sleep");
815     ok(hProcess != NULL, "Can't start process\n");
816
817     ptr = NULL;
818     size = 0;
819     offset.QuadPart = 0;
820     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
821     ok( !status, "NtMapViewOfSection failed status %x\n", status );
822
823     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
824     ok( ret, "ReadProcessMemory failed\n" );
825     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
826     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
827
828     status = pNtUnmapViewOfSection( hProcess, ptr );
829     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
830
831     CloseHandle( mapping );
832     CloseHandle( file );
833     DeleteFileA( testfile );
834
835     TerminateProcess(hProcess, 0);
836     CloseHandle(hProcess);
837 }
838
839 static void test_NtAreMappedFilesTheSame(void)
840 {
841     static const char testfile[] = "testfile.xxx";
842     HANDLE file, file2, mapping, map2;
843     void *ptr, *ptr2;
844     NTSTATUS status;
845     char path[MAX_PATH];
846
847     if (!pNtAreMappedFilesTheSame)
848     {
849         win_skip( "NtAreMappedFilesTheSame not available\n" );
850         return;
851     }
852
853     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
854                         NULL, CREATE_ALWAYS, 0, 0 );
855     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
856     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
857     SetEndOfFile( file );
858
859     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
860     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
861
862     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
863     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
864
865     file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
866                          NULL, OPEN_EXISTING, 0, 0 );
867     ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
868
869     map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
870     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
871     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
872     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
873     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
874     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
875     UnmapViewOfFile( ptr2 );
876
877     ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
878     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
879     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
880     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
881     UnmapViewOfFile( ptr2 );
882     CloseHandle( map2 );
883
884     map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
885     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
886     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
887     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
888     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
889     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
890     UnmapViewOfFile( ptr2 );
891     CloseHandle( map2 );
892     CloseHandle( file2 );
893
894     status = pNtAreMappedFilesTheSame( ptr, ptr );
895     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
896
897     status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
898     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
899
900     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
901     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
902
903     status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
904     ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
905         "NtAreMappedFilesTheSame returned %x\n", status );
906
907     status = pNtAreMappedFilesTheSame( ptr, NULL );
908     ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
909
910     status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
911     ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
912
913     status = pNtAreMappedFilesTheSame( NULL, NULL );
914     ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
915
916     ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
917     ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
918     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
919     ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
920     VirtualFree( ptr2, 0, MEM_RELEASE );
921
922     UnmapViewOfFile( ptr );
923     CloseHandle( mapping );
924     CloseHandle( file );
925
926     status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
927                                        GetModuleHandleA("kernel32.dll") );
928     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
929     status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
930                                        GetModuleHandleA("kernel32.dll") );
931     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
932     status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
933                                        (char *)GetModuleHandleA("kernel32.dll") + 4096 );
934     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
935
936     GetSystemDirectoryA( path, MAX_PATH );
937     strcat( path, "\\kernel32.dll" );
938     file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
939     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
940
941     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
942     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
943     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
944     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
945     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
946     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
947     UnmapViewOfFile( ptr );
948     CloseHandle( mapping );
949
950     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
951     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
952     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
953     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
954     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
955     todo_wine
956     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
957
958     file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
959     ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
960     map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
961     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
962     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
963     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
964     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
965     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
966     UnmapViewOfFile( ptr2 );
967     CloseHandle( map2 );
968     CloseHandle( file2 );
969
970     UnmapViewOfFile( ptr );
971     CloseHandle( mapping );
972
973     CloseHandle( file );
974     DeleteFileA( testfile );
975 }
976
977 static void test_CreateFileMapping(void)
978 {
979     HANDLE handle, handle2;
980
981     /* test case sensitivity */
982
983     SetLastError(0xdeadbeef);
984     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
985                                  "Wine Test Mapping");
986     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
987     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
988
989     SetLastError(0xdeadbeef);
990     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
991                                   "Wine Test Mapping");
992     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
993     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
994     CloseHandle( handle2 );
995
996     SetLastError(0xdeadbeef);
997     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
998                                  "WINE TEST MAPPING");
999     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1000     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1001     CloseHandle( handle2 );
1002
1003     SetLastError(0xdeadbeef);
1004     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1005     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1006     CloseHandle( handle2 );
1007
1008     SetLastError(0xdeadbeef);
1009     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1010     ok( !handle2, "OpenFileMapping succeeded\n");
1011     ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1012         "wrong error %u\n", GetLastError());
1013
1014     CloseHandle( handle );
1015 }
1016
1017 static void test_IsBadReadPtr(void)
1018 {
1019     BOOL ret;
1020     void *ptr = (void *)0xdeadbeef;
1021     char stackvar;
1022
1023     ret = IsBadReadPtr(NULL, 0);
1024     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1025
1026     ret = IsBadReadPtr(NULL, 1);
1027     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1028
1029     ret = IsBadReadPtr(ptr, 0);
1030     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1031
1032     ret = IsBadReadPtr(ptr, 1);
1033     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1034
1035     ret = IsBadReadPtr(&stackvar, 0);
1036     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1037
1038     ret = IsBadReadPtr(&stackvar, sizeof(char));
1039     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1040 }
1041
1042 static void test_IsBadWritePtr(void)
1043 {
1044     BOOL ret;
1045     void *ptr = (void *)0xdeadbeef;
1046     char stackval;
1047
1048     ret = IsBadWritePtr(NULL, 0);
1049     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1050
1051     ret = IsBadWritePtr(NULL, 1);
1052     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1053
1054     ret = IsBadWritePtr(ptr, 0);
1055     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1056
1057     ret = IsBadWritePtr(ptr, 1);
1058     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1059
1060     ret = IsBadWritePtr(&stackval, 0);
1061     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1062
1063     ret = IsBadWritePtr(&stackval, sizeof(char));
1064     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1065 }
1066
1067 static void test_IsBadCodePtr(void)
1068 {
1069     BOOL ret;
1070     void *ptr = (void *)0xdeadbeef;
1071     char stackval;
1072
1073     ret = IsBadCodePtr(NULL);
1074     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1075
1076     ret = IsBadCodePtr(ptr);
1077     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1078
1079     ret = IsBadCodePtr((void *)&stackval);
1080     ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1081 }
1082
1083 static void test_write_watch(void)
1084 {
1085     char *base;
1086     DWORD ret, size, old_prot;
1087     MEMORY_BASIC_INFORMATION info;
1088     void *results[64];
1089     ULONG_PTR count;
1090     ULONG pagesize;
1091
1092     if (!pGetWriteWatch || !pResetWriteWatch)
1093     {
1094         win_skip( "GetWriteWatch not supported\n" );
1095         return;
1096     }
1097
1098     size = 0x10000;
1099     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1100     if (!base &&
1101         (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1102     {
1103         win_skip( "MEM_WRITE_WATCH not supported\n" );
1104         return;
1105     }
1106     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1107     ret = VirtualQuery( base, &info, sizeof(info) );
1108     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1109     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1110     ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1111     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1112     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1113     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1114     ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1115
1116     count = 64;
1117     SetLastError( 0xdeadbeef );
1118     ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1119     ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1120     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1121         broken( GetLastError() == 0xdeadbeef ), /* win98 */
1122         "wrong error %u\n", GetLastError() );
1123
1124     SetLastError( 0xdeadbeef );
1125     ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
1126     if (ret)
1127     {
1128         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1129         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1130     }
1131     else  /* win98 */
1132     {
1133         ok( count == 0, "wrong count %lu\n", count );
1134     }
1135
1136     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1137     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1138     ok( count == 0, "wrong count %lu\n", count );
1139
1140     base[pagesize + 1] = 0x44;
1141
1142     count = 64;
1143     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1144     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1145     ok( count == 1, "wrong count %lu\n", count );
1146     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1147
1148     count = 64;
1149     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1150     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1151     ok( count == 1, "wrong count %lu\n", count );
1152     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1153
1154     count = 64;
1155     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1156     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1157     ok( count == 0, "wrong count %lu\n", count );
1158
1159     base[2*pagesize + 3] = 0x11;
1160     base[4*pagesize + 8] = 0x11;
1161
1162     count = 64;
1163     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1164     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1165     ok( count == 2, "wrong count %lu\n", count );
1166     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1167     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1168
1169     count = 64;
1170     ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1171     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1172     ok( count == 1, "wrong count %lu\n", count );
1173     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1174
1175     ret = pResetWriteWatch( base, 3*pagesize );
1176     ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1177
1178     count = 64;
1179     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1180     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1181     ok( count == 1, "wrong count %lu\n", count );
1182     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1183
1184     *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1185
1186     count = 64;
1187     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1188     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1189     ok( count == 3, "wrong count %lu\n", count );
1190     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1191     ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1192     ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1193
1194     count = 1;
1195     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1196     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1197     ok( count == 1, "wrong count %lu\n", count );
1198     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1199
1200     count = 64;
1201     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1202     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1203     ok( count == 2, "wrong count %lu\n", count );
1204     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1205     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1206
1207     /* changing protections doesn't affect watches */
1208
1209     ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1210     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1211     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1212
1213     ret = VirtualQuery( base, &info, sizeof(info) );
1214     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1215     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1216     ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1217     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1218     ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1219
1220     ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1221     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1222     ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1223
1224     count = 64;
1225     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1226     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1227     ok( count == 2, "wrong count %lu\n", count );
1228     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1229     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1230
1231     ret = VirtualQuery( base, &info, sizeof(info) );
1232     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1233     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1234     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1235     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1236     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1237
1238     /* some invalid parameter tests */
1239
1240     SetLastError( 0xdeadbeef );
1241     count = 0;
1242     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1243     if (ret)
1244     {
1245         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1246         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1247
1248         SetLastError( 0xdeadbeef );
1249         ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1250         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1251         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1252
1253         SetLastError( 0xdeadbeef );
1254         count = 64;
1255         ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1256         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1257         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1258
1259         SetLastError( 0xdeadbeef );
1260         count = 64;
1261         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1262         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1263         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1264
1265         SetLastError( 0xdeadbeef );
1266         count = 0;
1267         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1268         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1269         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1270
1271         SetLastError( 0xdeadbeef );
1272         count = 64;
1273         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1274         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1275         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1276
1277         SetLastError( 0xdeadbeef );
1278         count = 64;
1279         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1280         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1281         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1282
1283         SetLastError( 0xdeadbeef );
1284         count = 64;
1285         ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1286         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1287         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1288
1289         SetLastError( 0xdeadbeef );
1290         count = 64;
1291         ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1292         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1293         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1294
1295         SetLastError( 0xdeadbeef );
1296         ret = pResetWriteWatch( base, 0 );
1297         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1298         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1299
1300         SetLastError( 0xdeadbeef );
1301         ret = pResetWriteWatch( GetModuleHandle(0), size );
1302         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1303         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1304     }
1305     else  /* win98 is completely different */
1306     {
1307         SetLastError( 0xdeadbeef );
1308         count = 64;
1309         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1310         ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1311         ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1312
1313         count = 0;
1314         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1315         ok( !ret, "GetWriteWatch failed %u\n", ret );
1316
1317         count = 64;
1318         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1319         ok( !ret, "GetWriteWatch failed %u\n", ret );
1320
1321         count = 64;
1322         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1323         ok( !ret, "GetWriteWatch failed %u\n", ret );
1324
1325         ret = pResetWriteWatch( base, 0 );
1326         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1327
1328         ret = pResetWriteWatch( GetModuleHandle(0), size );
1329         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1330     }
1331
1332     VirtualFree( base, 0, MEM_FREE );
1333
1334     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1335     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1336     VirtualFree( base, 0, MEM_FREE );
1337
1338     base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1339     ok( !base, "VirtualAlloc succeeded\n" );
1340     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1341
1342     /* initial protect doesn't matter */
1343
1344     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1345     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1346     base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1347     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1348
1349     count = 64;
1350     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1351     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1352     ok( count == 0, "wrong count %lu\n", count );
1353
1354     ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1355     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1356     ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1357
1358     base[5*pagesize + 200] = 3;
1359
1360     ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1361     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1362     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1363
1364     count = 64;
1365     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1366     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1367     ok( count == 1, "wrong count %lu\n", count );
1368     ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1369
1370     ret = VirtualFree( base, size, MEM_DECOMMIT );
1371     ok( ret, "VirtualFree failed %u\n", GetLastError() );
1372
1373     count = 64;
1374     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1375     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1376     ok( count == 1 || broken(count == 0), /* win98 */
1377         "wrong count %lu\n", count );
1378     if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1379
1380     VirtualFree( base, 0, MEM_FREE );
1381 }
1382
1383 START_TEST(virtual)
1384 {
1385     int argc;
1386     char **argv;
1387     argc = winetest_get_mainargs( &argv );
1388
1389     if (argc >= 3)
1390     {
1391         if (!strcmp(argv[2], "sleep"))
1392         {
1393             Sleep(5000); /* spawned process runs for at most 5 seconds */
1394             return;
1395         }
1396         while (1)
1397         {
1398             void *mem;
1399             BOOL ret;
1400             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1401                                PAGE_EXECUTE_READWRITE);
1402             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1403             if (mem == NULL) break;
1404             ret = VirtualFree(mem, 0, MEM_RELEASE);
1405             ok(ret, "VirtualFree failed %u\n", GetLastError());
1406             if (!ret) break;
1407         }
1408         return;
1409     }
1410
1411     hkernel32 = GetModuleHandleA("kernel32.dll");
1412     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1413     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1414     pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1415     pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1416     pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"),
1417                                                        "NtAreMappedFilesTheSame" );
1418
1419     test_VirtualAllocEx();
1420     test_VirtualAlloc();
1421     test_MapViewOfFile();
1422     test_NtMapViewOfSection();
1423     test_NtAreMappedFilesTheSame();
1424     test_CreateFileMapping();
1425     test_IsBadReadPtr();
1426     test_IsBadWritePtr();
1427     test_IsBadCodePtr();
1428     test_write_watch();
1429 }