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