dmusic: Pass creation parameters to DMUSIC_CreateDirectMusicBufferImpl then allocate...
[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 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
42 static DWORD (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
43
44 /* ############################### */
45
46 static HANDLE create_target_process(const char *arg)
47 {
48     char **argv;
49     char cmdline[MAX_PATH];
50     PROCESS_INFORMATION pi;
51     BOOL ret;
52     STARTUPINFO si = { 0 };
53     si.cb = sizeof(si);
54
55     winetest_get_mainargs( &argv );
56     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
57     ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
58     ok(ret, "error: %u\n", GetLastError());
59     ret = CloseHandle(pi.hThread);
60     ok(ret, "error %u\n", GetLastError());
61     return pi.hProcess;
62 }
63
64 static void test_VirtualAllocEx(void)
65 {
66     const unsigned int alloc_size = 1<<15;
67     char *src, *dst;
68     SIZE_T bytes_written = 0, bytes_read = 0, i;
69     void *addr1, *addr2;
70     BOOL b;
71     DWORD old_prot;
72     MEMORY_BASIC_INFORMATION info;
73     HANDLE hProcess;
74
75     /* not exported in all windows-versions  */
76     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
77         win_skip("Virtual{Alloc,Free}Ex not available\n");
78         return;
79     }
80
81     hProcess = create_target_process("sleep");
82     ok(hProcess != NULL, "Can't start process\n");
83
84     SetLastError(0xdeadbeef);
85     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
86                            PAGE_EXECUTE_READWRITE);
87     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
88     {   /* Win9x */
89         win_skip("VirtualAllocEx not implemented\n");
90         TerminateProcess(hProcess, 0);
91         CloseHandle(hProcess);
92         return;
93     }
94
95     src = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
96     dst = VirtualAlloc( NULL, alloc_size, MEM_COMMIT, PAGE_READWRITE );
97     for (i = 0; i < alloc_size; i++)
98         src[i] = i & 0xff;
99
100     ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
101     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
102     ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
103        bytes_written);
104     b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
105     ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
106     ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
107
108     /* test invalid source buffers */
109
110     b = VirtualProtect( src + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
111     ok( b, "VirtualProtect failed error %u\n", GetLastError() );
112     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
113     ok( !b, "WriteProcessMemory succeeded\n" );
114     ok( GetLastError() == ERROR_NOACCESS ||
115         GetLastError() == ERROR_PARTIAL_COPY, /* vista */
116         "wrong error %u\n", GetLastError() );
117     ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
118     b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
119     ok( !b, "ReadProcessMemory succeeded\n" );
120     ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
121     ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
122
123     b = VirtualProtect( src, 0x2000, PAGE_NOACCESS, &old_prot );
124     ok( b, "VirtualProtect failed error %u\n", GetLastError() );
125     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
126     ok( !b, "WriteProcessMemory succeeded\n" );
127     ok( GetLastError() == ERROR_NOACCESS ||
128         GetLastError() == ERROR_PARTIAL_COPY, /* vista */
129         "wrong error %u\n", GetLastError() );
130     ok( bytes_written == 0, "%lu bytes written\n", bytes_written );
131     b = ReadProcessMemory(hProcess, addr1, src, alloc_size, &bytes_read);
132     ok( !b, "ReadProcessMemory succeeded\n" );
133     ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
134     ok( bytes_read == 0, "%lu bytes written\n", bytes_read );
135
136     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
137     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
138
139     VirtualFree( src, 0, MEM_FREE );
140     VirtualFree( dst, 0, MEM_FREE );
141
142     /*
143      * The following tests parallel those in test_VirtualAlloc()
144      */
145
146     SetLastError(0xdeadbeef);
147     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
148     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
149     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
150        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
151         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
152
153     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
154     ok(addr1 != NULL, "VirtualAllocEx failed\n");
155
156     /* test a not committed memory */
157     memset(&info, 'q', sizeof(info));
158     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
159     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
160     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
161     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
162     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
163     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
164     /* NT reports Protect == 0 for a not committed memory block */
165     ok(info.Protect == 0 /* NT */ ||
166        info.Protect == PAGE_NOACCESS, /* Win9x */
167         "%x != PAGE_NOACCESS\n", info.Protect);
168     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
169
170     SetLastError(0xdeadbeef);
171     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
172        "VirtualProtectEx should fail on a not committed memory\n");
173     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
174        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
175         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
176
177     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
178     ok(addr1 == addr2, "VirtualAllocEx failed\n");
179
180     /* test a committed memory */
181     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
182         "VirtualQueryEx failed\n");
183     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
184     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
185     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
186     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
187     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
188     /* this time NT reports PAGE_NOACCESS as well */
189     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
190     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
191
192     /* this should fail, since not the whole range is committed yet */
193     SetLastError(0xdeadbeef);
194     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
195         "VirtualProtectEx should fail on a not committed memory\n");
196     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
197        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
198         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
199
200     old_prot = 0;
201     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
202     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
203
204     old_prot = 0;
205     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
206     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
207
208     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
209        "VirtualFreeEx should fail with type 0\n");
210     ok(GetLastError() == ERROR_INVALID_PARAMETER,
211         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
212
213     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
214
215     /* if the type is MEM_RELEASE, size must be 0 */
216     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
217        "VirtualFreeEx should fail\n");
218     ok(GetLastError() == ERROR_INVALID_PARAMETER,
219         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
220
221     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
222
223     TerminateProcess(hProcess, 0);
224     CloseHandle(hProcess);
225 }
226
227 static void test_VirtualAlloc(void)
228 {
229     void *addr1, *addr2;
230     DWORD old_prot;
231     MEMORY_BASIC_INFORMATION info;
232
233     SetLastError(0xdeadbeef);
234     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
235     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
236     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
237        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
238         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
239
240     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
241     ok(addr1 != NULL, "VirtualAlloc failed\n");
242
243     /* test a not committed memory */
244     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
245         "VirtualQuery failed\n");
246     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
247     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
248     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
249     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
250     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
251     /* NT reports Protect == 0 for a not committed memory block */
252     ok(info.Protect == 0 /* NT */ ||
253        info.Protect == PAGE_NOACCESS, /* Win9x */
254         "%x != PAGE_NOACCESS\n", info.Protect);
255     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
256
257     SetLastError(0xdeadbeef);
258     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
259        "VirtualProtect should fail on a not committed memory\n");
260     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
261        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
262         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
263
264     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
265     ok(addr1 == addr2, "VirtualAlloc failed\n");
266
267     /* test a committed memory */
268     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
269         "VirtualQuery failed\n");
270     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
271     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
272     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
273     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
274     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
275     /* this time NT reports PAGE_NOACCESS as well */
276     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
277     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
278
279     /* this should fail, since not the whole range is committed yet */
280     SetLastError(0xdeadbeef);
281     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
282         "VirtualProtect should fail on a not committed memory\n");
283     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
284        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
285         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
286
287     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
288     ok(old_prot == PAGE_NOACCESS,
289         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
290
291     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
292     ok(old_prot == PAGE_READONLY,
293         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
294
295     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
296         "VirtualQuery failed\n");
297     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
298     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
299     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
300     memset( addr1, 0x55, 20 );
301     ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
302
303     addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
304     ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
305         "VirtualAlloc failed err %u\n", GetLastError() );
306     ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
307     if (addr2)
308     {
309         ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
310            "VirtualQuery failed\n");
311         ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
312         ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
313         ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
314
315         addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
316         ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
317
318         ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
319            "VirtualQuery failed\n");
320         ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
321         ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
322         ok(info.Protect == 0, "%x != 0\n", info.Protect);
323
324         addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
325         ok( !addr2, "VirtualAlloc failed\n" );
326         ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
327     }
328
329     /* invalid protection values */
330     SetLastError(0xdeadbeef);
331     addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
332     ok(!addr2, "VirtualAlloc succeeded\n");
333     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
334
335     SetLastError(0xdeadbeef);
336     addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
337     ok(!addr2, "VirtualAlloc succeeded\n");
338     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
339
340     SetLastError(0xdeadbeef);
341     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
342     ok(!addr2, "VirtualAlloc succeeded\n");
343     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
344
345     SetLastError(0xdeadbeef);
346     ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
347        "VirtualProtect succeeded\n");
348     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
349
350     SetLastError(0xdeadbeef);
351     ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
352     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
353
354     SetLastError(0xdeadbeef);
355     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
356     ok(GetLastError() == ERROR_INVALID_PARAMETER,
357         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
358
359     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
360
361     /* if the type is MEM_RELEASE, size must be 0 */
362     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
363     ok(GetLastError() == ERROR_INVALID_PARAMETER,
364         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
365
366     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
367 }
368
369 static void test_MapViewOfFile(void)
370 {
371     static const char testfile[] = "testfile.xxx";
372     const char *name;
373     HANDLE file, mapping, map2;
374     void *ptr, *ptr2, *addr;
375     MEMORY_BASIC_INFORMATION info;
376     BOOL ret;
377
378     SetLastError(0xdeadbeef);
379     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
380     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
381     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
382     SetEndOfFile( file );
383
384     /* read/write mapping */
385
386     SetLastError(0xdeadbeef);
387     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
388     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
389
390     SetLastError(0xdeadbeef);
391     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
392     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
393     UnmapViewOfFile( ptr );
394
395     /* this fails on win9x but succeeds on NT */
396     SetLastError(0xdeadbeef);
397     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
398     if (ptr) UnmapViewOfFile( ptr );
399     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
400
401     SetLastError(0xdeadbeef);
402     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
403     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
404     UnmapViewOfFile( ptr );
405
406     SetLastError(0xdeadbeef);
407     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
408     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
409     UnmapViewOfFile( ptr );
410
411     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
412                            FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
413     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
414     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
415     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
416     UnmapViewOfFile( ptr );
417     CloseHandle( map2 );
418
419     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
420                            FILE_MAP_READ, FALSE, 0 );
421     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
422     SetLastError(0xdeadbeef);
423     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
424     if (!ptr)
425     {
426         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
427         CloseHandle( map2 );
428         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
429         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
430         SetLastError(0xdeadbeef);
431         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
432         ok( !ptr, "MapViewOfFile succeeded\n" );
433         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
434         CloseHandle( map2 );
435         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
436                                FILE_MAP_READ, FALSE, 0 );
437         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
438         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
439         ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
440     }
441     else win_skip( "no access checks on win9x\n" );
442
443     UnmapViewOfFile( ptr );
444     CloseHandle( map2 );
445     CloseHandle( mapping );
446
447     /* read-only mapping */
448
449     SetLastError(0xdeadbeef);
450     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
451     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
452
453     SetLastError(0xdeadbeef);
454     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
455     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
456     UnmapViewOfFile( ptr );
457
458     /* this fails on win9x but succeeds on NT */
459     SetLastError(0xdeadbeef);
460     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
461     if (ptr) UnmapViewOfFile( ptr );
462     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
463
464     SetLastError(0xdeadbeef);
465     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
466     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
467     UnmapViewOfFile( ptr );
468
469     SetLastError(0xdeadbeef);
470     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
471     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
472     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
473         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
474     CloseHandle( mapping );
475
476     /* copy-on-write mapping */
477
478     SetLastError(0xdeadbeef);
479     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
480     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
481
482     SetLastError(0xdeadbeef);
483     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
484     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
485     UnmapViewOfFile( ptr );
486
487     SetLastError(0xdeadbeef);
488     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
489     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
490     UnmapViewOfFile( ptr );
491
492     SetLastError(0xdeadbeef);
493     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
494     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
495     UnmapViewOfFile( ptr );
496
497     SetLastError(0xdeadbeef);
498     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
499     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
500     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
501         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
502     CloseHandle( mapping );
503
504     /* no access mapping */
505
506     SetLastError(0xdeadbeef);
507     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
508     /* fails on NT but succeeds on win9x */
509     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
510     else
511     {
512         SetLastError(0xdeadbeef);
513         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
514         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
515         UnmapViewOfFile( ptr );
516
517         SetLastError(0xdeadbeef);
518         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
519         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
520         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
521
522         SetLastError(0xdeadbeef);
523         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
524         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
525         UnmapViewOfFile( ptr );
526
527         SetLastError(0xdeadbeef);
528         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
529         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
530         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
531
532         CloseHandle( mapping );
533     }
534
535     CloseHandle( file );
536
537     /* now try read-only file */
538
539     SetLastError(0xdeadbeef);
540     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
541     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
542
543     SetLastError(0xdeadbeef);
544     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
545     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
546     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
547         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
548
549     SetLastError(0xdeadbeef);
550     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
551     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
552     CloseHandle( mapping );
553
554     SetLastError(0xdeadbeef);
555     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
556     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
557     CloseHandle( mapping );
558     CloseHandle( file );
559
560     /* now try no access file */
561
562     SetLastError(0xdeadbeef);
563     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
564     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
565
566     SetLastError(0xdeadbeef);
567     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
568     ok( !mapping, "CreateFileMapping PAGE_READWRITE 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_WRITECOPY, 0, 4096, NULL );
574     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
575     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
576         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
577
578     SetLastError(0xdeadbeef);
579     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
580     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
581     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
582         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
583
584     CloseHandle( file );
585     DeleteFileA( testfile );
586
587     SetLastError(0xdeadbeef);
588     name = "Local\\Foo";
589     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
590     /* nt4 doesn't have Local\\ */
591     if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
592     {
593         name = "Foo";
594         file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
595     }
596     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
597
598     SetLastError(0xdeadbeef);
599     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
600     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
601     SetLastError(0xdeadbeef);
602     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
603     if (!ptr)
604     {
605         SIZE_T size;
606         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
607         SetLastError(0xdeadbeef);
608         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
609         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
610         SetLastError(0xdeadbeef);
611         size = VirtualQuery( ptr, &info, sizeof(info) );
612         ok( size == sizeof(info),
613             "VirtualQuery error %u\n", GetLastError() );
614         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
615         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
616         ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
617         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
618         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
619         ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
620     }
621     else win_skip( "no access checks on win9x\n" );
622     UnmapViewOfFile( ptr );
623     CloseHandle( mapping );
624
625     SetLastError(0xdeadbeef);
626     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
627     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
628     SetLastError(0xdeadbeef);
629     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
630     if (!ptr)
631     {
632         SIZE_T size;
633         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
634         SetLastError(0xdeadbeef);
635         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
636         ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
637         SetLastError(0xdeadbeef);
638         size = VirtualQuery( ptr, &info, sizeof(info) );
639         ok( size == sizeof(info),
640             "VirtualQuery error %u\n", GetLastError() );
641         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
642         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
643         ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
644         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
645         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
646         ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
647     }
648     else win_skip( "no access checks on win9x\n" );
649     UnmapViewOfFile( ptr );
650     CloseHandle( mapping );
651
652     CloseHandle( file );
653
654     /* read/write mapping with SEC_RESERVE */
655     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
656     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
657
658     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
659     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
660
661     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
662     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
663     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
664     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
665
666     ret = VirtualQuery(ptr, &info, sizeof(info));
667     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
668     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
669     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
670     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
671     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
672     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
673     {
674         ok(info.AllocationProtect == PAGE_NOACCESS,
675            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
676         ok(info.Protect == PAGE_NOACCESS,
677            "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
678     }
679     else
680     {
681         ok(info.AllocationProtect == PAGE_READWRITE,
682            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
683         ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
684         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
685     }
686
687     if (ptr != ptr2)
688     {
689         ret = VirtualQuery(ptr2, &info, sizeof(info));
690         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
691         ok(info.BaseAddress == ptr2,
692            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
693         ok(info.AllocationBase == ptr2,
694            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
695         ok(info.AllocationProtect == PAGE_READWRITE,
696            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
697         ok(info.RegionSize == MAPPING_SIZE,
698            "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE, info.RegionSize);
699         ok(info.State == MEM_RESERVE,
700            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
701         ok(info.Protect == 0,
702            "Protect should have been 0 instead of 0x%x\n", info.Protect);
703         ok(info.Type == MEM_MAPPED,
704            "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
705     }
706
707     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
708     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
709
710     ret = VirtualQuery(ptr, &info, sizeof(info));
711     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
712     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
713     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
714     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
715     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
716     ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
717     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
718     {
719         ok(info.AllocationProtect == PAGE_NOACCESS,
720            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
721     }
722     else
723     {
724         ok(info.AllocationProtect == PAGE_READWRITE,
725            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
726         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
727     }
728
729     /* shows that the VirtualAlloc above affects the mapping, not just the
730      * virtual memory in this process - it also affects all other processes
731      * with a view of the mapping, but that isn't tested here */
732     if (ptr != ptr2)
733     {
734         ret = VirtualQuery(ptr2, &info, sizeof(info));
735         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
736         ok(info.BaseAddress == ptr2,
737            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
738         ok(info.AllocationBase == ptr2,
739            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
740         ok(info.AllocationProtect == PAGE_READWRITE,
741            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
742         ok(info.RegionSize == 0x10000,
743            "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
744         ok(info.State == MEM_COMMIT,
745            "State should have been MEM_COMMIT instead of 0x%x\n", info.State);
746         ok(info.Protect == PAGE_READWRITE,
747            "Protect should have been PAGE_READWRITE instead of 0x%x\n", info.Protect);
748         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
749     }
750
751     addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
752     ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
753         "VirtualAlloc failed with error %u\n", GetLastError() );
754
755     ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
756     ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
757     if (!ret)
758         ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
759
760     ret = UnmapViewOfFile(ptr2);
761     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
762     ret = UnmapViewOfFile(ptr);
763     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
764     CloseHandle(mapping);
765
766     addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
767     ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
768
769     SetLastError(0xdeadbeef);
770     ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
771     ok( GetLastError() == ERROR_INVALID_ADDRESS,
772         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
773     SetLastError(0xdeadbeef);
774     ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
775     ok( GetLastError() == ERROR_INVALID_ADDRESS,
776         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
777     SetLastError(0xdeadbeef);
778     ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779     ok( GetLastError() == ERROR_INVALID_ADDRESS,
780        "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
781
782     ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
783
784     /* close named mapping handle without unmapping */
785     name = "Foo";
786     SetLastError(0xdeadbeef);
787     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
788     ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
789     SetLastError(0xdeadbeef);
790     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
791     ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
792     SetLastError(0xdeadbeef);
793     map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
794     ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
795     SetLastError(0xdeadbeef);
796     ret = CloseHandle(map2);
797     ok(ret, "CloseHandle error %d\n", GetLastError());
798     SetLastError(0xdeadbeef);
799     ret = CloseHandle(mapping);
800     ok(ret, "CloseHandle error %d\n", GetLastError());
801
802     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
803     ok( !ret, "memory is not accessible\n" );
804
805     ret = VirtualQuery(ptr, &info, sizeof(info));
806     ok(ret, "VirtualQuery error %d\n", GetLastError());
807     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
808     ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
809     ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
810     ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
811     ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
812     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
813     ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
814
815     SetLastError(0xdeadbeef);
816     map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
817     todo_wine
818     ok( map2 == 0, "OpenFileMappingA succeeded\n" );
819     todo_wine
820     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
821     if (map2) CloseHandle(map2); /* FIXME: remove once Wine is fixed */
822     SetLastError(0xdeadbeef);
823     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
824     ok( mapping != 0, "CreateFileMappingA failed\n" );
825     todo_wine
826     ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
827     SetLastError(0xdeadbeef);
828     ret = CloseHandle(mapping);
829     ok(ret, "CloseHandle error %d\n", GetLastError());
830
831     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
832     ok( !ret, "memory is not accessible\n" );
833
834     ret = VirtualQuery(ptr, &info, sizeof(info));
835     ok(ret, "VirtualQuery error %d\n", GetLastError());
836     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
837     ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
838     ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
839     ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
840     ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
841     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
842     ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
843
844     SetLastError(0xdeadbeef);
845     ret = UnmapViewOfFile(ptr);
846     ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
847
848     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
849     ok( ret, "memory is accessible\n" );
850
851     ret = VirtualQuery(ptr, &info, sizeof(info));
852     ok(ret, "VirtualQuery error %d\n", GetLastError());
853     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
854     ok(info.RegionSize != MAPPING_SIZE, "got size %#lx\n", info.RegionSize);
855     ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
856     ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
857     ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
858     ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
859     ok(info.Type == 0, "%#x != 0\n", info.Type);
860
861     SetLastError(0xdeadbeef);
862     file = CreateFileA(testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
863     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
864     SetFilePointer(file, 4096, NULL, FILE_BEGIN);
865     SetEndOfFile(file);
866
867     SetLastError(0xdeadbeef);
868     mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
869     ok( mapping != 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
870     SetLastError(0xdeadbeef);
871     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
872     ok( ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError() );
873     SetLastError(0xdeadbeef);
874     map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
875     ok( map2 != 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
876     SetLastError(0xdeadbeef);
877     ret = CloseHandle(map2);
878     ok(ret, "CloseHandle error %d\n", GetLastError());
879     SetLastError(0xdeadbeef);
880     ret = CloseHandle(mapping);
881     ok(ret, "CloseHandle error %d\n", GetLastError());
882
883     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
884     ok( !ret, "memory is not accessible\n" );
885
886     ret = VirtualQuery(ptr, &info, sizeof(info));
887     ok(ret, "VirtualQuery error %d\n", GetLastError());
888     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
889     ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
890     ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
891     ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
892     ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
893     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
894     ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
895
896     SetLastError(0xdeadbeef);
897     map2 = OpenFileMappingA(FILE_MAP_READ, FALSE, name);
898     todo_wine
899     ok( map2 == 0, "OpenFileMappingA succeeded\n" );
900     todo_wine
901     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "OpenFileMappingA set error %d\n", GetLastError() );
902     CloseHandle(map2);
903     SetLastError(0xdeadbeef);
904     mapping = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, MAPPING_SIZE, name);
905     ok( mapping != 0, "CreateFileMappingA failed\n" );
906     todo_wine
907     ok( GetLastError() == ERROR_SUCCESS, "CreateFileMappingA set error %d\n", GetLastError() );
908     SetLastError(0xdeadbeef);
909     ret = CloseHandle(mapping);
910     ok(ret, "CloseHandle error %d\n", GetLastError());
911
912     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
913     ok( !ret, "memory is not accessible\n" );
914
915     ret = VirtualQuery(ptr, &info, sizeof(info));
916     ok(ret, "VirtualQuery error %d\n", GetLastError());
917     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
918     ok(info.RegionSize == MAPPING_SIZE, "got %#lx != expected %#x\n", info.RegionSize, MAPPING_SIZE);
919     ok(info.Protect == PAGE_READWRITE, "got %#x != expected PAGE_READWRITE\n", info.Protect);
920     ok(info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr);
921     ok(info.AllocationProtect == PAGE_READWRITE, "%#x != PAGE_READWRITE\n", info.AllocationProtect);
922     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
923     ok(info.Type == MEM_MAPPED, "%#x != MEM_MAPPED\n", info.Type);
924
925     SetLastError(0xdeadbeef);
926     ret = UnmapViewOfFile(ptr);
927     ok( ret, "UnmapViewOfFile failed with error %d\n", GetLastError() );
928
929     ret = IsBadReadPtr(ptr, MAPPING_SIZE);
930     ok( ret, "memory is accessible\n" );
931
932     ret = VirtualQuery(ptr, &info, sizeof(info));
933     ok(ret, "VirtualQuery error %d\n", GetLastError());
934     ok(info.BaseAddress == ptr, "got %p != expected %p\n", info.BaseAddress, ptr);
935     ok(info.RegionSize != MAPPING_SIZE, "got size %#lx\n", info.RegionSize);
936     ok(info.Protect == PAGE_NOACCESS, "got %#x != expected PAGE_NOACCESS\n", info.Protect);
937     ok(info.AllocationBase == NULL, "%p != NULL\n", info.AllocationBase);
938     ok(info.AllocationProtect == 0, "%#x != 0\n", info.AllocationProtect);
939     ok(info.State == MEM_FREE, "%#x != MEM_FREE\n", info.State);
940     ok(info.Type == 0, "%#x != 0\n", info.Type);
941
942     CloseHandle(file);
943     DeleteFileA(testfile);
944 }
945
946 static void test_NtMapViewOfSection(void)
947 {
948     HANDLE hProcess;
949
950     static const char testfile[] = "testfile.xxx";
951     static const char data[] = "test data for NtMapViewOfSection";
952     char buffer[sizeof(data)];
953     HANDLE file, mapping;
954     void *ptr;
955     BOOL ret;
956     DWORD status, written;
957     SIZE_T size, result;
958     LARGE_INTEGER offset;
959
960     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
961     {
962         win_skip( "NtMapViewOfSection not available\n" );
963         return;
964     }
965
966     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
967     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
968     WriteFile( file, data, sizeof(data), &written, NULL );
969     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
970     SetEndOfFile( file );
971
972     /* read/write mapping */
973
974     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
975     ok( mapping != 0, "CreateFileMapping failed\n" );
976
977     hProcess = create_target_process("sleep");
978     ok(hProcess != NULL, "Can't start process\n");
979
980     ptr = NULL;
981     size = 0;
982     offset.QuadPart = 0;
983     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
984     ok( !status, "NtMapViewOfSection failed status %x\n", status );
985
986     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
987     ok( ret, "ReadProcessMemory failed\n" );
988     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
989     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
990
991     status = pNtUnmapViewOfSection( hProcess, ptr );
992     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
993
994     CloseHandle( mapping );
995     CloseHandle( file );
996     DeleteFileA( testfile );
997
998     TerminateProcess(hProcess, 0);
999     CloseHandle(hProcess);
1000 }
1001
1002 static void test_NtAreMappedFilesTheSame(void)
1003 {
1004     static const char testfile[] = "testfile.xxx";
1005     HANDLE file, file2, mapping, map2;
1006     void *ptr, *ptr2;
1007     NTSTATUS status;
1008     char path[MAX_PATH];
1009
1010     if (!pNtAreMappedFilesTheSame)
1011     {
1012         win_skip( "NtAreMappedFilesTheSame not available\n" );
1013         return;
1014     }
1015
1016     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1017                         NULL, CREATE_ALWAYS, 0, 0 );
1018     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1019     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
1020     SetEndOfFile( file );
1021
1022     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
1023     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1024
1025     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1026     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1027
1028     file2 = CreateFileA( testfile, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
1029                          NULL, OPEN_EXISTING, 0, 0 );
1030     ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1031
1032     map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY, 0, 4096, NULL );
1033     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1034     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1035     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1036     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1037     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1038     UnmapViewOfFile( ptr2 );
1039
1040     ptr2 = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1041     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1042     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1043     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1044     UnmapViewOfFile( ptr2 );
1045     CloseHandle( map2 );
1046
1047     map2 = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1048     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1049     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 4096 );
1050     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1051     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1052     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1053     UnmapViewOfFile( ptr2 );
1054     CloseHandle( map2 );
1055     CloseHandle( file2 );
1056
1057     status = pNtAreMappedFilesTheSame( ptr, ptr );
1058     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1059
1060     status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
1061     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1062
1063     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1064     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1065
1066     status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
1067     ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
1068         "NtAreMappedFilesTheSame returned %x\n", status );
1069
1070     status = pNtAreMappedFilesTheSame( ptr, NULL );
1071     ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1072
1073     status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
1074     ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1075
1076     status = pNtAreMappedFilesTheSame( NULL, NULL );
1077     ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
1078
1079     ptr2 = VirtualAlloc( NULL, 0x10000, MEM_COMMIT, PAGE_READWRITE );
1080     ok( ptr2 != NULL, "VirtualAlloc error %u\n", GetLastError() );
1081     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1082     ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
1083     VirtualFree( ptr2, 0, MEM_RELEASE );
1084
1085     UnmapViewOfFile( ptr );
1086     CloseHandle( mapping );
1087     CloseHandle( file );
1088
1089     status = pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1090                                        GetModuleHandleA("kernel32.dll") );
1091     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1092     status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1093                                        GetModuleHandleA("kernel32.dll") );
1094     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1095     status = pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1096                                        (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1097     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1098
1099     GetSystemDirectoryA( path, MAX_PATH );
1100     strcat( path, "\\kernel32.dll" );
1101     file = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1102     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1103
1104     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
1105     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1106     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
1107     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1108     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1109     ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
1110     UnmapViewOfFile( ptr );
1111     CloseHandle( mapping );
1112
1113     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1114     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
1115     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1116     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1117     status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
1118     todo_wine
1119     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1120
1121     file2 = CreateFileA( path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1122     ok( file2 != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
1123     map2 = CreateFileMappingA( file2, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL );
1124     ok( map2 != 0, "CreateFileMapping error %u\n", GetLastError() );
1125     ptr2 = MapViewOfFile( map2, FILE_MAP_READ, 0, 0, 0 );
1126     ok( ptr2 != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1127     status = pNtAreMappedFilesTheSame( ptr, ptr2 );
1128     ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
1129     UnmapViewOfFile( ptr2 );
1130     CloseHandle( map2 );
1131     CloseHandle( file2 );
1132
1133     UnmapViewOfFile( ptr );
1134     CloseHandle( mapping );
1135
1136     CloseHandle( file );
1137     DeleteFileA( testfile );
1138 }
1139
1140 static void test_CreateFileMapping(void)
1141 {
1142     HANDLE handle, handle2;
1143
1144     /* test case sensitivity */
1145
1146     SetLastError(0xdeadbeef);
1147     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1148                                  "Wine Test Mapping");
1149     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1150     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1151
1152     SetLastError(0xdeadbeef);
1153     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1154                                   "Wine Test Mapping");
1155     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1156     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
1157     CloseHandle( handle2 );
1158
1159     SetLastError(0xdeadbeef);
1160     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
1161                                  "WINE TEST MAPPING");
1162     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
1163     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1164     CloseHandle( handle2 );
1165
1166     SetLastError(0xdeadbeef);
1167     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
1168     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
1169     CloseHandle( handle2 );
1170
1171     SetLastError(0xdeadbeef);
1172     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
1173     ok( !handle2, "OpenFileMapping succeeded\n");
1174     ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
1175         "wrong error %u\n", GetLastError());
1176
1177     CloseHandle( handle );
1178 }
1179
1180 static void test_IsBadReadPtr(void)
1181 {
1182     BOOL ret;
1183     void *ptr = (void *)0xdeadbeef;
1184     char stackvar;
1185
1186     ret = IsBadReadPtr(NULL, 0);
1187     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1188
1189     ret = IsBadReadPtr(NULL, 1);
1190     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1191
1192     ret = IsBadReadPtr(ptr, 0);
1193     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1194
1195     ret = IsBadReadPtr(ptr, 1);
1196     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1197
1198     ret = IsBadReadPtr(&stackvar, 0);
1199     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1200
1201     ret = IsBadReadPtr(&stackvar, sizeof(char));
1202     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1203 }
1204
1205 static void test_IsBadWritePtr(void)
1206 {
1207     BOOL ret;
1208     void *ptr = (void *)0xdeadbeef;
1209     char stackval;
1210
1211     ret = IsBadWritePtr(NULL, 0);
1212     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1213
1214     ret = IsBadWritePtr(NULL, 1);
1215     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1216
1217     ret = IsBadWritePtr(ptr, 0);
1218     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1219
1220     ret = IsBadWritePtr(ptr, 1);
1221     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1222
1223     ret = IsBadWritePtr(&stackval, 0);
1224     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1225
1226     ret = IsBadWritePtr(&stackval, sizeof(char));
1227     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1228 }
1229
1230 static void test_IsBadCodePtr(void)
1231 {
1232     BOOL ret;
1233     void *ptr = (void *)0xdeadbeef;
1234     char stackval;
1235
1236     ret = IsBadCodePtr(NULL);
1237     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1238
1239     ret = IsBadCodePtr(ptr);
1240     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1241
1242     ret = IsBadCodePtr((void *)&stackval);
1243     ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1244 }
1245
1246 static void test_write_watch(void)
1247 {
1248     char *base;
1249     DWORD ret, size, old_prot;
1250     MEMORY_BASIC_INFORMATION info;
1251     void *results[64];
1252     ULONG_PTR count;
1253     ULONG pagesize;
1254
1255     if (!pGetWriteWatch || !pResetWriteWatch)
1256     {
1257         win_skip( "GetWriteWatch not supported\n" );
1258         return;
1259     }
1260
1261     size = 0x10000;
1262     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1263     if (!base &&
1264         (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1265     {
1266         win_skip( "MEM_WRITE_WATCH not supported\n" );
1267         return;
1268     }
1269     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1270     ret = VirtualQuery( base, &info, sizeof(info) );
1271     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1272     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1273     ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
1274     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1275     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1276     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1277     ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
1278
1279     count = 64;
1280     SetLastError( 0xdeadbeef );
1281     ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
1282     ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1283     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
1284         broken( GetLastError() == 0xdeadbeef ), /* win98 */
1285         "wrong error %u\n", GetLastError() );
1286
1287     SetLastError( 0xdeadbeef );
1288     ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
1289     if (ret)
1290     {
1291         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1292         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1293     }
1294     else  /* win98 */
1295     {
1296         ok( count == 0, "wrong count %lu\n", count );
1297     }
1298
1299     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1300     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1301     ok( count == 0, "wrong count %lu\n", count );
1302
1303     base[pagesize + 1] = 0x44;
1304
1305     count = 64;
1306     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1307     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1308     ok( count == 1, "wrong count %lu\n", count );
1309     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1310
1311     count = 64;
1312     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1313     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1314     ok( count == 1, "wrong count %lu\n", count );
1315     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1316
1317     count = 64;
1318     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1319     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1320     ok( count == 0, "wrong count %lu\n", count );
1321
1322     base[2*pagesize + 3] = 0x11;
1323     base[4*pagesize + 8] = 0x11;
1324
1325     count = 64;
1326     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1327     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1328     ok( count == 2, "wrong count %lu\n", count );
1329     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1330     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1331
1332     count = 64;
1333     ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1334     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1335     ok( count == 1, "wrong count %lu\n", count );
1336     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1337
1338     ret = pResetWriteWatch( base, 3*pagesize );
1339     ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1340
1341     count = 64;
1342     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1343     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1344     ok( count == 1, "wrong count %lu\n", count );
1345     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1346
1347     *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1348
1349     count = 64;
1350     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1351     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1352     ok( count == 3, "wrong count %lu\n", count );
1353     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1354     ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1355     ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1356
1357     count = 1;
1358     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1359     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1360     ok( count == 1, "wrong count %lu\n", count );
1361     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1362
1363     count = 64;
1364     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1365     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1366     ok( count == 2, "wrong count %lu\n", count );
1367     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1368     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1369
1370     /* changing protections doesn't affect watches */
1371
1372     ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1373     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1374     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1375
1376     ret = VirtualQuery( base, &info, sizeof(info) );
1377     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1378     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1379     ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1380     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1381     ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1382
1383     ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1384     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1385     ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1386
1387     count = 64;
1388     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1389     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1390     ok( count == 2, "wrong count %lu\n", count );
1391     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1392     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1393
1394     ret = VirtualQuery( base, &info, sizeof(info) );
1395     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1396     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1397     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1398     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1399     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1400
1401     /* some invalid parameter tests */
1402
1403     SetLastError( 0xdeadbeef );
1404     count = 0;
1405     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1406     if (ret)
1407     {
1408         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1409         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1410
1411         SetLastError( 0xdeadbeef );
1412         ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1413         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1414         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1415
1416         SetLastError( 0xdeadbeef );
1417         count = 64;
1418         ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1419         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1420         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1421
1422         SetLastError( 0xdeadbeef );
1423         count = 64;
1424         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1425         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1426         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1427
1428         SetLastError( 0xdeadbeef );
1429         count = 0;
1430         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1431         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1432         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1433
1434         SetLastError( 0xdeadbeef );
1435         count = 64;
1436         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1437         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1438         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1439
1440         SetLastError( 0xdeadbeef );
1441         count = 64;
1442         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1443         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1444         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1445
1446         SetLastError( 0xdeadbeef );
1447         count = 64;
1448         ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1449         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1450         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1451
1452         SetLastError( 0xdeadbeef );
1453         count = 64;
1454         ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1455         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1456         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1457
1458         SetLastError( 0xdeadbeef );
1459         ret = pResetWriteWatch( base, 0 );
1460         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1461         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1462
1463         SetLastError( 0xdeadbeef );
1464         ret = pResetWriteWatch( GetModuleHandle(0), size );
1465         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1466         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1467     }
1468     else  /* win98 is completely different */
1469     {
1470         SetLastError( 0xdeadbeef );
1471         count = 64;
1472         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1473         ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1474         ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1475
1476         count = 0;
1477         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1478         ok( !ret, "GetWriteWatch failed %u\n", ret );
1479
1480         count = 64;
1481         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1482         ok( !ret, "GetWriteWatch failed %u\n", ret );
1483
1484         count = 64;
1485         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1486         ok( !ret, "GetWriteWatch failed %u\n", ret );
1487
1488         ret = pResetWriteWatch( base, 0 );
1489         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1490
1491         ret = pResetWriteWatch( GetModuleHandle(0), size );
1492         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1493     }
1494
1495     VirtualFree( base, 0, MEM_FREE );
1496
1497     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1498     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1499     VirtualFree( base, 0, MEM_FREE );
1500
1501     base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1502     ok( !base, "VirtualAlloc succeeded\n" );
1503     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1504
1505     /* initial protect doesn't matter */
1506
1507     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1508     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1509     base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1510     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1511
1512     count = 64;
1513     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1514     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1515     ok( count == 0, "wrong count %lu\n", count );
1516
1517     ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1518     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1519     ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1520
1521     base[5*pagesize + 200] = 3;
1522
1523     ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1524     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1525     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1526
1527     count = 64;
1528     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1529     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1530     ok( count == 1, "wrong count %lu\n", count );
1531     ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1532
1533     ret = VirtualFree( base, size, MEM_DECOMMIT );
1534     ok( ret, "VirtualFree failed %u\n", GetLastError() );
1535
1536     count = 64;
1537     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1538     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1539     ok( count == 1 || broken(count == 0), /* win98 */
1540         "wrong count %lu\n", count );
1541     if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1542
1543     VirtualFree( base, 0, MEM_FREE );
1544 }
1545
1546 static void test_VirtualProtect(void)
1547 {
1548     static const struct test_data
1549     {
1550         DWORD prot_set, prot_get;
1551     } td[] =
1552     {
1553         { 0, 0 }, /* 0x00 */
1554         { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
1555         { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
1556         { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
1557         { PAGE_READWRITE, PAGE_READWRITE }, /* 0x04 */
1558         { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
1559         { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
1560         { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
1561         { PAGE_WRITECOPY, 0 }, /* 0x08 */
1562         { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
1563         { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
1564         { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
1565         { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
1566         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
1567         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
1568         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
1569
1570         { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
1571         { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
1572         { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
1573         { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1574         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
1575         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
1576         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
1577         { PAGE_EXECUTE_WRITECOPY, 0 }, /* 0x80 */
1578         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
1579         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
1580         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
1581         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
1582         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
1583         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
1584         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
1585     };
1586     char *base;
1587     DWORD ret, old_prot, rw_prot, exec_prot, i, j;
1588     MEMORY_BASIC_INFORMATION info;
1589     SYSTEM_INFO si;
1590
1591     GetSystemInfo(&si);
1592     trace("system page size %#x\n", si.dwPageSize);
1593
1594     SetLastError(0xdeadbeef);
1595     base = VirtualAlloc(0, si.dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
1596     ok(base != NULL, "VirtualAlloc failed %d\n", GetLastError());
1597
1598     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1599     {
1600         SetLastError(0xdeadbeef);
1601         ret = VirtualQuery(base, &info, sizeof(info));
1602         ok(ret, "VirtualQuery failed %d\n", GetLastError());
1603         ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1604         ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1605         ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1606         ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1607         ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1608         ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1609         ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1610
1611         old_prot = 0xdeadbeef;
1612         SetLastError(0xdeadbeef);
1613         ret = VirtualProtect(base, si.dwPageSize, td[i].prot_set, &old_prot);
1614         if (td[i].prot_get)
1615         {
1616             ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1617             ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1618
1619             SetLastError(0xdeadbeef);
1620             ret = VirtualQuery(base, &info, sizeof(info));
1621             ok(ret, "VirtualQuery failed %d\n", GetLastError());
1622             ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1623             ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1624             ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
1625             ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1626             ok(info.AllocationProtect == PAGE_NOACCESS, "%d: %#x != PAGE_NOACCESS\n", i, info.AllocationProtect);
1627             ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1628             ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1629         }
1630         else
1631         {
1632             ok(!ret, "%d: VirtualProtect should fail\n", i);
1633             ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1634         }
1635
1636         old_prot = 0xdeadbeef;
1637         SetLastError(0xdeadbeef);
1638         ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1639         ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1640         if (td[i].prot_get)
1641             ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
1642         else
1643             ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1644     }
1645
1646     exec_prot = 0;
1647
1648     for (i = 0; i <= 4; i++)
1649     {
1650         rw_prot = 0;
1651
1652         for (j = 0; j <= 4; j++)
1653         {
1654             DWORD prot = exec_prot | rw_prot;
1655
1656             SetLastError(0xdeadbeef);
1657             ret = VirtualProtect(base, si.dwPageSize, prot, &old_prot);
1658             if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1659             {
1660                 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1661                 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1662             }
1663             else
1664             {
1665                 if (prot & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY))
1666                 {
1667                     ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1668                     ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1669                 }
1670                 else
1671                     ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
1672             }
1673
1674             rw_prot = 1 << j;
1675         }
1676
1677         exec_prot = 1 << (i + 4);
1678     }
1679
1680     VirtualFree(base, 0, MEM_FREE);
1681 }
1682
1683 static BOOL is_mem_writable(DWORD prot)
1684 {
1685     switch (prot & 0xff)
1686     {
1687         case PAGE_READWRITE:
1688         case PAGE_WRITECOPY:
1689         case PAGE_EXECUTE_READWRITE:
1690         case PAGE_EXECUTE_WRITECOPY:
1691             return TRUE;
1692
1693         default:
1694             return FALSE;
1695     }
1696 }
1697
1698 static void test_VirtualAlloc_protection(void)
1699 {
1700     static const struct test_data
1701     {
1702         DWORD prot;
1703         BOOL success;
1704     } td[] =
1705     {
1706         { 0, FALSE }, /* 0x00 */
1707         { PAGE_NOACCESS, TRUE }, /* 0x01 */
1708         { PAGE_READONLY, TRUE }, /* 0x02 */
1709         { PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x03 */
1710         { PAGE_READWRITE, TRUE }, /* 0x04 */
1711         { PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x05 */
1712         { PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x06 */
1713         { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x07 */
1714         { PAGE_WRITECOPY, FALSE }, /* 0x08 */
1715         { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE }, /* 0x09 */
1716         { PAGE_WRITECOPY | PAGE_READONLY, FALSE }, /* 0x0a */
1717         { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE }, /* 0x0b */
1718         { PAGE_WRITECOPY | PAGE_READWRITE, FALSE }, /* 0x0c */
1719         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE }, /* 0x0d */
1720         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE }, /* 0x0e */
1721         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE }, /* 0x0f */
1722
1723         { PAGE_EXECUTE, TRUE }, /* 0x10 */
1724         { PAGE_EXECUTE_READ, TRUE }, /* 0x20 */
1725         { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x30 */
1726         { PAGE_EXECUTE_READWRITE, TRUE }, /* 0x40 */
1727         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0x50 */
1728         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0x60 */
1729         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0x70 */
1730         { PAGE_EXECUTE_WRITECOPY, FALSE }, /* 0x80 */
1731         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE }, /* 0x90 */
1732         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE }, /* 0xa0 */
1733         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE }, /* 0xb0 */
1734         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE }, /* 0xc0 */
1735         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE }, /* 0xd0 */
1736         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE }, /* 0xe0 */
1737         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE } /* 0xf0 */
1738     };
1739     char *base;
1740     DWORD ret, i;
1741     MEMORY_BASIC_INFORMATION info;
1742     SYSTEM_INFO si;
1743
1744     GetSystemInfo(&si);
1745     trace("system page size %#x\n", si.dwPageSize);
1746
1747     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1748     {
1749         SetLastError(0xdeadbeef);
1750         base = VirtualAlloc(0, si.dwPageSize, MEM_COMMIT, td[i].prot);
1751
1752         if (td[i].success)
1753         {
1754             ok(base != NULL, "%d: VirtualAlloc failed %d\n", i, GetLastError());
1755
1756             SetLastError(0xdeadbeef);
1757             ret = VirtualQuery(base, &info, sizeof(info));
1758             ok(ret, "VirtualQuery failed %d\n", GetLastError());
1759             ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1760             ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1761             ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1762             ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1763             ok(info.AllocationProtect == td[i].prot, "%d: %#x != %#x\n", i, info.AllocationProtect, td[i].prot);
1764             ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1765             ok(info.Type == MEM_PRIVATE, "%d: %#x != MEM_PRIVATE\n", i, info.Type);
1766
1767             if (is_mem_writable(info.Protect))
1768             {
1769                 base[0] = 0xfe;
1770
1771                 SetLastError(0xdeadbeef);
1772                 ret = VirtualQuery(base, &info, sizeof(info));
1773                 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1774                 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1775             }
1776
1777             VirtualFree(base, 0, MEM_FREE);
1778         }
1779         else
1780         {
1781             ok(!base, "%d: VirtualAlloc should fail\n", i);
1782             ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1783         }
1784     }
1785 }
1786
1787 static void test_CreateFileMapping_protection(void)
1788 {
1789     static const struct test_data
1790     {
1791         DWORD prot;
1792         BOOL success;
1793         DWORD prot_after_write;
1794     } td[] =
1795     {
1796         { 0, FALSE, 0 }, /* 0x00 */
1797         { PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x01 */
1798         { PAGE_READONLY, TRUE, PAGE_READONLY }, /* 0x02 */
1799         { PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x03 */
1800         { PAGE_READWRITE, TRUE, PAGE_READWRITE }, /* 0x04 */
1801         { PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x05 */
1802         { PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x06 */
1803         { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x07 */
1804         { PAGE_WRITECOPY, TRUE, PAGE_READWRITE }, /* 0x08 */
1805         { PAGE_WRITECOPY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x09 */
1806         { PAGE_WRITECOPY | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0a */
1807         { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0b */
1808         { PAGE_WRITECOPY | PAGE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0x0c */
1809         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0d */
1810         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, FALSE, PAGE_NOACCESS }, /* 0x0e */
1811         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, FALSE, PAGE_NOACCESS }, /* 0x0f */
1812
1813         { PAGE_EXECUTE, FALSE, PAGE_EXECUTE }, /* 0x10 */
1814         { PAGE_EXECUTE_READ, TRUE, PAGE_EXECUTE_READ }, /* 0x20 */
1815         { PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_EXECUTE_READ }, /* 0x30 */
1816         { PAGE_EXECUTE_READWRITE, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x40 */
1817         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x50 */
1818         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0x60 */
1819         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x70 */
1820         { PAGE_EXECUTE_WRITECOPY, TRUE, PAGE_EXECUTE_READWRITE }, /* 0x80 */
1821         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0x90 */
1822         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xa0 */
1823         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xb0 */
1824         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, FALSE, PAGE_NOACCESS }, /* 0xc0 */
1825         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, FALSE, PAGE_NOACCESS }, /* 0xd0 */
1826         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, FALSE, PAGE_NOACCESS }, /* 0xe0 */
1827         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, FALSE, PAGE_NOACCESS } /* 0xf0 */
1828     };
1829     char *base;
1830     DWORD ret, i, alloc_prot, prot, old_prot;
1831     MEMORY_BASIC_INFORMATION info;
1832     SYSTEM_INFO si;
1833     char temp_path[MAX_PATH];
1834     char file_name[MAX_PATH];
1835     HANDLE hfile, hmap;
1836     BOOL page_exec_supported = TRUE;
1837
1838     GetSystemInfo(&si);
1839     trace("system page size %#x\n", si.dwPageSize);
1840
1841     GetTempPath(MAX_PATH, temp_path);
1842     GetTempFileName(temp_path, "map", 0, file_name);
1843
1844     SetLastError(0xdeadbeef);
1845     hfile = CreateFile(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
1846     ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
1847     SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
1848     SetEndOfFile(hfile);
1849
1850     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1851     {
1852         SetLastError(0xdeadbeef);
1853         hmap = CreateFileMapping(hfile, NULL, td[i].prot | SEC_COMMIT, 0, si.dwPageSize, NULL);
1854
1855         if (td[i].success)
1856         {
1857             if (!hmap)
1858             {
1859                 trace("%d: CreateFileMapping(%04x) failed: %d\n", i, td[i].prot, GetLastError());
1860                 /* NT4 and win2k don't support EXEC on file mappings */
1861                 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
1862                 {
1863                     page_exec_supported = FALSE;
1864                     ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
1865                     continue;
1866                 }
1867                 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1868                 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1869                 {
1870                     page_exec_supported = FALSE;
1871                     ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
1872                     continue;
1873                 }
1874             }
1875             ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, td[i].prot, GetLastError());
1876
1877             base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
1878             ok(base != NULL, "%d: MapViewOfFile failed %d\n", i, GetLastError());
1879
1880             SetLastError(0xdeadbeef);
1881             ret = VirtualQuery(base, &info, sizeof(info));
1882             ok(ret, "VirtualQuery failed %d\n", GetLastError());
1883             ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1884             ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1885             ok(info.Protect == PAGE_READONLY, "%d: got %#x != expected PAGE_READONLY\n", i, info.Protect);
1886             ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1887             ok(info.AllocationProtect == PAGE_READONLY, "%d: %#x != PAGE_READONLY\n", i, info.AllocationProtect);
1888             ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1889             ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1890
1891             if (is_mem_writable(info.Protect))
1892             {
1893                 base[0] = 0xfe;
1894
1895                 SetLastError(0xdeadbeef);
1896                 ret = VirtualQuery(base, &info, sizeof(info));
1897                 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1898                 ok(info.Protect == td[i].prot, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot);
1899             }
1900
1901             UnmapViewOfFile(base);
1902             CloseHandle(hmap);
1903         }
1904         else
1905         {
1906             ok(!hmap, "%d: CreateFileMapping should fail\n", i);
1907             ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1908         }
1909     }
1910
1911     if (page_exec_supported) alloc_prot = PAGE_EXECUTE_READWRITE;
1912     else alloc_prot = PAGE_READWRITE;
1913     SetLastError(0xdeadbeef);
1914     hmap = CreateFileMapping(hfile, NULL, alloc_prot, 0, si.dwPageSize, NULL);
1915     ok(hmap != 0, "%d: CreateFileMapping error %d\n", i, GetLastError());
1916
1917     SetLastError(0xdeadbeef);
1918     base = MapViewOfFile(hmap, FILE_MAP_READ | FILE_MAP_WRITE | (page_exec_supported ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
1919     ok(base != NULL, "MapViewOfFile failed %d\n", GetLastError());
1920
1921     old_prot = 0xdeadbeef;
1922     SetLastError(0xdeadbeef);
1923     ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
1924     ok(ret, "VirtualProtect error %d\n", GetLastError());
1925     ok(old_prot == alloc_prot, "got %#x != expected %#x\n", old_prot, alloc_prot);
1926
1927     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1928     {
1929         SetLastError(0xdeadbeef);
1930         ret = VirtualQuery(base, &info, sizeof(info));
1931         ok(ret, "VirtualQuery failed %d\n", GetLastError());
1932         ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1933         ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1934         ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1935         ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1936         ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
1937         ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1938         ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1939
1940         old_prot = 0xdeadbeef;
1941         SetLastError(0xdeadbeef);
1942         ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
1943         if (td[i].success || td[i].prot == PAGE_NOACCESS || td[i].prot == PAGE_EXECUTE)
1944         {
1945             if (!ret)
1946             {
1947                 /* win2k and XP don't support EXEC on file mappings */
1948                 if (td[i].prot == PAGE_EXECUTE)
1949                 {
1950                     ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
1951                     continue;
1952                 }
1953                 /* NT4 and win2k don't support EXEC on file mappings */
1954                 if (td[i].prot == PAGE_EXECUTE_READ || td[i].prot == PAGE_EXECUTE_READWRITE)
1955                 {
1956                     ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i);
1957                     continue;
1958                 }
1959                 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1960                 if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1961                 {
1962                     ok(broken(!ret), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
1963                     continue;
1964                 }
1965             }
1966
1967             ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1968             ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1969
1970             prot = td[i].prot;
1971             /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
1972             if (prot == PAGE_EXECUTE_WRITECOPY) prot = PAGE_EXECUTE_READWRITE;
1973
1974             SetLastError(0xdeadbeef);
1975             ret = VirtualQuery(base, &info, sizeof(info));
1976             ok(ret, "VirtualQuery failed %d\n", GetLastError());
1977             ok(info.BaseAddress == base, "%d: got %p != expected %p\n", i, info.BaseAddress, base);
1978             ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1979             /* FIXME: remove the condition below once Wine is fixed */
1980             if (td[i].prot == PAGE_EXECUTE_WRITECOPY)
1981                 todo_wine ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
1982             else
1983                 ok(info.Protect == prot, "%d: got %#x != expected %#x\n", i, info.Protect, prot);
1984             ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1985             ok(info.AllocationProtect == alloc_prot, "%d: %#x != %#x\n", i, info.AllocationProtect, alloc_prot);
1986             ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1987             ok(info.Type == MEM_MAPPED, "%d: %#x != MEM_MAPPED\n", i, info.Type);
1988
1989             if (is_mem_writable(info.Protect))
1990             {
1991                 base[0] = 0xfe;
1992
1993                 SetLastError(0xdeadbeef);
1994                 ret = VirtualQuery(base, &info, sizeof(info));
1995                 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1996                 /* FIXME: remove the condition below once Wine is fixed */
1997                 if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
1998                     todo_wine ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
1999                 else
2000                     ok(info.Protect == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_after_write);
2001             }
2002         }
2003         else
2004         {
2005             ok(!ret, "%d: VirtualProtect should fail\n", i);
2006             ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
2007             continue;
2008         }
2009
2010         old_prot = 0xdeadbeef;
2011         SetLastError(0xdeadbeef);
2012         ret = VirtualProtect(base, si.dwPageSize, PAGE_NOACCESS, &old_prot);
2013         ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
2014         /* FIXME: remove the condition below once Wine is fixed */
2015         if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY)
2016             todo_wine ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2017         else
2018             ok(old_prot == td[i].prot_after_write, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_after_write);
2019     }
2020
2021     UnmapViewOfFile(base);
2022     CloseHandle(hmap);
2023
2024     CloseHandle(hfile);
2025     DeleteFile(file_name);
2026 }
2027
2028 #define ACCESS_READ      0x01
2029 #define ACCESS_WRITE     0x02
2030 #define ACCESS_EXECUTE   0x04
2031 #define ACCESS_WRITECOPY 0x08
2032
2033 static DWORD page_prot_to_access(DWORD prot)
2034 {
2035     switch (prot)
2036     {
2037     case PAGE_READWRITE:
2038         return ACCESS_READ | ACCESS_WRITE;
2039
2040     case PAGE_EXECUTE:
2041     case PAGE_EXECUTE_READ:
2042         return ACCESS_READ | ACCESS_EXECUTE;
2043
2044     case PAGE_EXECUTE_READWRITE:
2045         return ACCESS_READ | ACCESS_WRITE | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2046
2047     case PAGE_EXECUTE_WRITECOPY:
2048         return ACCESS_READ | ACCESS_WRITECOPY | ACCESS_EXECUTE;
2049
2050     case PAGE_READONLY:
2051         return ACCESS_READ;
2052
2053     case PAGE_WRITECOPY:
2054         return ACCESS_READ;
2055
2056     default:
2057         return 0;
2058     }
2059 }
2060
2061 static BOOL is_compatible_protection(DWORD map_prot, DWORD view_prot, DWORD prot)
2062 {
2063     DWORD map_access, view_access, prot_access;
2064
2065     map_access = page_prot_to_access(map_prot);
2066     view_access = page_prot_to_access(view_prot);
2067     prot_access = page_prot_to_access(prot);
2068
2069     if (view_access == prot_access) return TRUE;
2070     if (!view_access) return FALSE;
2071
2072     if ((view_access & prot_access) != prot_access) return FALSE;
2073     if ((map_access & prot_access) == prot_access) return TRUE;
2074
2075     return FALSE;
2076 }
2077
2078 static DWORD map_prot_to_access(DWORD prot)
2079 {
2080     switch (prot)
2081     {
2082     case PAGE_READWRITE:
2083     case PAGE_EXECUTE_READWRITE:
2084         return SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2085     case PAGE_READONLY:
2086     case PAGE_WRITECOPY:
2087     case PAGE_EXECUTE:
2088     case PAGE_EXECUTE_READ:
2089     case PAGE_EXECUTE_WRITECOPY:
2090         return SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_MAP_EXECUTE_EXPLICIT | SECTION_QUERY;
2091     default:
2092         return 0;
2093     }
2094 }
2095
2096 static BOOL is_compatible_access(DWORD map_prot, DWORD view_prot)
2097 {
2098     DWORD access = map_prot_to_access(map_prot);
2099     if (!view_prot) view_prot = SECTION_MAP_READ;
2100     return (view_prot & access) == view_prot;
2101 }
2102
2103 static void *map_view_of_file(HANDLE handle, DWORD access)
2104 {
2105     NTSTATUS status;
2106     LARGE_INTEGER offset;
2107     SIZE_T count;
2108     ULONG protect;
2109     BOOL exec;
2110     void *addr;
2111
2112     if (!pNtMapViewOfSection) return NULL;
2113
2114     count = 0;
2115     offset.u.LowPart  = 0;
2116     offset.u.HighPart = 0;
2117
2118     exec = access & FILE_MAP_EXECUTE;
2119     access &= ~FILE_MAP_EXECUTE;
2120
2121     if (access == FILE_MAP_COPY)
2122     {
2123         if (exec)
2124             protect = PAGE_EXECUTE_WRITECOPY;
2125         else
2126             protect = PAGE_WRITECOPY;
2127     }
2128     else if (access & FILE_MAP_WRITE)
2129     {
2130         if (exec)
2131             protect = PAGE_EXECUTE_READWRITE;
2132         else
2133             protect = PAGE_READWRITE;
2134     }
2135     else if (access & FILE_MAP_READ)
2136     {
2137         if (exec)
2138             protect = PAGE_EXECUTE_READ;
2139         else
2140             protect = PAGE_READONLY;
2141     }
2142     else protect = PAGE_NOACCESS;
2143
2144     addr = NULL;
2145     status = pNtMapViewOfSection(handle, GetCurrentProcess(), &addr, 0, 0, &offset,
2146                                  &count, 1 /* ViewShare */, 0, protect);
2147     if (status)
2148     {
2149         /* for simplicity */
2150         SetLastError(ERROR_ACCESS_DENIED);
2151         addr = NULL;
2152     }
2153     return addr;
2154 }
2155
2156 static void test_mapping(void)
2157 {
2158     static const DWORD page_prot[] =
2159     {
2160         PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY,
2161         PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY
2162     };
2163     static const struct
2164     {
2165         DWORD access, prot;
2166     } view[] =
2167     {
2168         { 0, PAGE_NOACCESS }, /* 0x00 */
2169         { FILE_MAP_COPY, PAGE_WRITECOPY }, /* 0x01 */
2170         { FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x02 */
2171         { FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x03 */
2172         { FILE_MAP_READ, PAGE_READONLY }, /* 0x04 */
2173         { FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x05 */
2174         { FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x06 */
2175         { FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x07 */
2176         { SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x08 */
2177         { SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x09 */
2178         { SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0a */
2179         { SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0b */
2180         { SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_READONLY }, /* 0x0c */
2181         { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_READONLY }, /* 0x0d */
2182         { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_READWRITE }, /* 0x0e */
2183         { SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_READWRITE }, /* 0x0f */
2184         { FILE_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x20 */
2185         { FILE_MAP_EXECUTE | FILE_MAP_COPY, PAGE_EXECUTE_WRITECOPY }, /* 0x21 */
2186         { FILE_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x22 */
2187         { FILE_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x23 */
2188         { FILE_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x24 */
2189         { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x25 */
2190         { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x26 */
2191         { FILE_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x27 */
2192         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE, PAGE_NOACCESS }, /* 0x28 */
2193         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_COPY, PAGE_NOACCESS }, /* 0x29 */
2194         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2a */
2195         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE }, /* 0x2b */
2196         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ, PAGE_EXECUTE_READ }, /* 0x2c */
2197         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_COPY, PAGE_EXECUTE_READ }, /* 0x2d */
2198         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE, PAGE_EXECUTE_READWRITE }, /* 0x2e */
2199         { FILE_MAP_EXECUTE | SECTION_MAP_EXECUTE | FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_COPY, PAGE_EXECUTE_READWRITE } /* 0x2f */
2200     };
2201     void *base, *nt_base;
2202     DWORD i, j, k, ret, old_prot, prev_prot;
2203     SYSTEM_INFO si;
2204     char temp_path[MAX_PATH];
2205     char file_name[MAX_PATH];
2206     HANDLE hfile, hmap;
2207     MEMORY_BASIC_INFORMATION info, nt_info;
2208
2209     GetSystemInfo(&si);
2210     trace("system page size %#x\n", si.dwPageSize);
2211
2212     GetTempPath(MAX_PATH, temp_path);
2213     GetTempFileName(temp_path, "map", 0, file_name);
2214
2215     SetLastError(0xdeadbeef);
2216     hfile = CreateFile(file_name, GENERIC_READ|GENERIC_WRITE|GENERIC_EXECUTE, 0, NULL, CREATE_ALWAYS, 0, 0);
2217     ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
2218     SetFilePointer(hfile, si.dwPageSize, NULL, FILE_BEGIN);
2219     SetEndOfFile(hfile);
2220
2221     for (i = 0; i < sizeof(page_prot)/sizeof(page_prot[0]); i++)
2222     {
2223         SetLastError(0xdeadbeef);
2224         hmap = CreateFileMapping(hfile, NULL, page_prot[i] | SEC_COMMIT, 0, si.dwPageSize, NULL);
2225
2226         if (page_prot[i] == PAGE_NOACCESS)
2227         {
2228             HANDLE hmap2;
2229
2230             ok(!hmap, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2231             ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2232
2233             /* A trick to create a not accessible mapping */
2234             SetLastError(0xdeadbeef);
2235             hmap = CreateFileMapping(hfile, NULL, PAGE_READWRITE | SEC_COMMIT, 0, si.dwPageSize, NULL);
2236             ok(hmap != 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2237             SetLastError(0xdeadbeef);
2238             ret = DuplicateHandle(GetCurrentProcess(), hmap, GetCurrentProcess(), &hmap2, 0, FALSE, 0);
2239             ok(ret, "DuplicateHandle error %d\n", GetLastError());
2240             CloseHandle(hmap);
2241             hmap = hmap2;
2242         }
2243
2244         if (!hmap)
2245         {
2246             trace("%d: CreateFileMapping(%04x) failed: %d\n", i, page_prot[i], GetLastError());
2247
2248             /* NT4 and win2k don't support EXEC on file mappings */
2249             if (page_prot[i] == PAGE_EXECUTE_READ || page_prot[i] == PAGE_EXECUTE_READWRITE)
2250             {
2251                 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i);
2252                 continue;
2253             }
2254             /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2255             if (page_prot[i] == PAGE_EXECUTE_WRITECOPY)
2256             {
2257                 ok(broken(!hmap), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i);
2258                 continue;
2259             }
2260         }
2261
2262         ok(hmap != 0, "%d: CreateFileMapping(%04x) error %d\n", i, page_prot[i], GetLastError());
2263
2264         for (j = 0; j < sizeof(view)/sizeof(view[0]); j++)
2265         {
2266             nt_base = map_view_of_file(hmap, view[j].access);
2267             if (nt_base)
2268             {
2269                 SetLastError(0xdeadbeef);
2270                 ret = VirtualQuery(nt_base, &nt_info, sizeof(nt_info));
2271                 ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2272                 UnmapViewOfFile(nt_base);
2273             }
2274
2275             SetLastError(0xdeadbeef);
2276             base = MapViewOfFile(hmap, view[j].access, 0, 0, 0);
2277
2278             /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
2279             ok(!nt_base == !base ||
2280                broken((view[j].access & FILE_MAP_EXECUTE) && !nt_base != !base),
2281                "%d: (%04x/%04x) NT %p kernel %p\n", j, page_prot[i], view[j].access, nt_base, base);
2282
2283             if (!is_compatible_access(page_prot[i], view[j].access))
2284             {
2285                 ok(!base, "%d: MapViewOfFile(%04x/%04x) should fail\n", j, page_prot[i], view[j].access);
2286                 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
2287                 continue;
2288             }
2289
2290             /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
2291             if (!base && (view[j].access & FILE_MAP_EXECUTE))
2292             {
2293                 ok(broken(!base), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2294                 continue;
2295             }
2296
2297             ok(base != NULL, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j, page_prot[i], view[j].access, GetLastError());
2298
2299             SetLastError(0xdeadbeef);
2300             ret = VirtualQuery(base, &info, sizeof(info));
2301             ok(ret, "%d: VirtualQuery failed %d\n", j, GetLastError());
2302             ok(info.BaseAddress == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.BaseAddress, base);
2303             ok(info.RegionSize == si.dwPageSize, "%d: (%04x) got %#lx != expected %#x\n", j, view[j].access, info.RegionSize, si.dwPageSize);
2304             ok(info.Protect == view[j].prot ||
2305                broken(view[j].prot == PAGE_EXECUTE_READ && info.Protect == PAGE_READONLY) || /* win2k */
2306                broken(view[j].prot == PAGE_EXECUTE_READWRITE && info.Protect == PAGE_READWRITE) || /* win2k */
2307                broken(view[j].prot == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2308                "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.Protect, view[j].prot);
2309             ok(info.AllocationBase == base, "%d: (%04x) got %p, expected %p\n", j, view[j].access, info.AllocationBase, base);
2310             ok(info.AllocationProtect == info.Protect, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, info.AllocationProtect, info.Protect);
2311             ok(info.State == MEM_COMMIT, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j, view[j].access, info.State);
2312             ok(info.Type == MEM_MAPPED, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j, view[j].access, info.Type);
2313
2314             if (nt_base && base)
2315             {
2316                 ok(nt_info.RegionSize == info.RegionSize, "%d: (%04x) got %#lx != expected %#lx\n", j, view[j].access, nt_info.RegionSize, info.RegionSize);
2317                 ok(nt_info.Protect == info.Protect /* Vista+ */ ||
2318                    broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2319                    "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Protect, info.Protect);
2320                 ok(nt_info.AllocationProtect == info.AllocationProtect /* Vista+ */ ||
2321                    broken(nt_info.AllocationProtect == PAGE_EXECUTE_WRITECOPY && info.Protect == PAGE_NOACCESS), /* XP */
2322                    "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.AllocationProtect, info.AllocationProtect);
2323                 ok(nt_info.State == info.State, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.State, info.State);
2324                 ok(nt_info.Type == info.Type, "%d: (%04x) got %#x, expected %#x\n", j, view[j].access, nt_info.Type, info.Type);
2325             }
2326
2327             prev_prot = info.Protect;
2328
2329             for (k = 0; k < sizeof(page_prot)/sizeof(page_prot[0]); k++)
2330             {
2331                 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2332                 SetLastError(0xdeadbeef);
2333                 old_prot = 0xdeadbeef;
2334                 ret = VirtualProtect(base, si.dwPageSize, page_prot[k], &old_prot);
2335                 if (is_compatible_protection(page_prot[i], view[j].prot, page_prot[k]))
2336                 {
2337                     /* win2k and XP don't support EXEC on file mappings */
2338                     if (!ret && page_prot[k] == PAGE_EXECUTE)
2339                     {
2340                         ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2341                         continue;
2342                     }
2343                     /* NT4 and win2k don't support EXEC on file mappings */
2344                     if (!ret && (page_prot[k] == PAGE_EXECUTE_READ || page_prot[k] == PAGE_EXECUTE_READWRITE))
2345                     {
2346                         ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2347                         continue;
2348                     }
2349                     /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2350                     if (!ret && page_prot[k] == PAGE_EXECUTE_WRITECOPY)
2351                     {
2352                         ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
2353                         continue;
2354                     }
2355                     /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly  */
2356                     if (!ret && view[j].prot == PAGE_EXECUTE_WRITECOPY)
2357                     {
2358                         ok(broken(!ret), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
2359                         continue;
2360                     }
2361
2362                     ok(ret, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot[i], view[j].prot, page_prot[k]);
2363                     ok(old_prot == prev_prot, "got %#x, expected %#x\n", old_prot, prev_prot);
2364                     prev_prot = page_prot[k];
2365                 }
2366                 else
2367                 {
2368                     /* NT4 doesn't fail on incompatible map and view */
2369                     if (ret)
2370                     {
2371                         ok(broken(ret), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2372                         skip("Incompatible map and view are not properly handled on this platform\n");
2373                         break; /* NT4 won't pass remaining tests */
2374                     }
2375
2376                     ok(!ret, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);
2377                     ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2378                 }
2379             }
2380
2381             UnmapViewOfFile(base);
2382         }
2383
2384         CloseHandle(hmap);
2385     }
2386
2387     CloseHandle(hfile);
2388     DeleteFile(file_name);
2389 }
2390
2391 START_TEST(virtual)
2392 {
2393     int argc;
2394     char **argv;
2395     argc = winetest_get_mainargs( &argv );
2396
2397     if (argc >= 3)
2398     {
2399         if (!strcmp(argv[2], "sleep"))
2400         {
2401             Sleep(5000); /* spawned process runs for at most 5 seconds */
2402             return;
2403         }
2404         while (1)
2405         {
2406             void *mem;
2407             BOOL ret;
2408             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
2409                                PAGE_EXECUTE_READWRITE);
2410             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
2411             if (mem == NULL) break;
2412             ret = VirtualFree(mem, 0, MEM_RELEASE);
2413             ok(ret, "VirtualFree failed %u\n", GetLastError());
2414             if (!ret) break;
2415         }
2416         return;
2417     }
2418
2419     hkernel32 = GetModuleHandleA("kernel32.dll");
2420     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
2421     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
2422     pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
2423     pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
2424     pNtAreMappedFilesTheSame = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"),
2425                                                        "NtAreMappedFilesTheSame" );
2426     pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtMapViewOfSection");
2427     pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection");
2428
2429     test_mapping();
2430     test_CreateFileMapping_protection();
2431     test_VirtualAlloc_protection();
2432     test_VirtualProtect();
2433     test_VirtualAllocEx();
2434     test_VirtualAlloc();
2435     test_MapViewOfFile();
2436     test_NtMapViewOfSection();
2437     test_NtAreMappedFilesTheSame();
2438     test_CreateFileMapping();
2439     test_IsBadReadPtr();
2440     test_IsBadWritePtr();
2441     test_IsBadCodePtr();
2442     test_write_watch();
2443 }