msi: Only insert entries into listbox if property value matches.
[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
31 static HINSTANCE hkernel32;
32 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
33 static BOOL   (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
34
35 /* ############################### */
36
37 static HANDLE create_target_process(const char *arg)
38 {
39     char **argv;
40     char cmdline[MAX_PATH];
41     PROCESS_INFORMATION pi;
42     STARTUPINFO si = { 0 };
43     si.cb = sizeof(si);
44
45     winetest_get_mainargs( &argv );
46     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
47     ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
48                      &si, &pi) != 0, "error: %u\n", GetLastError());
49     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
50     return pi.hProcess;
51 }
52
53 static void test_VirtualAllocEx(void)
54 {
55     const unsigned int alloc_size = 1<<15;
56     char *src, *dst;
57     unsigned long bytes_written = 0, bytes_read = 0, i;
58     void *addr1, *addr2;
59     BOOL b;
60     DWORD old_prot;
61     MEMORY_BASIC_INFORMATION info;
62     HANDLE hProcess;
63
64     /* not exported in all windows-versions  */
65     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
66         skip("VirtualAllocEx not found\n");
67         return;
68     }
69
70     hProcess = create_target_process("sleep");
71     ok(hProcess != NULL, "Can't start process\n");
72
73     SetLastError(0xdeadbeef);
74     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
75                            PAGE_EXECUTE_READWRITE);
76     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
77     {   /* Win9x */
78         skip("VirtualAllocEx not implemented\n");
79         TerminateProcess(hProcess, 0);
80         CloseHandle(hProcess);
81         return;
82     }
83
84     src = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
85     dst = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
86     for (i = 0; i < alloc_size; i++)
87         src[i] = 0xcafedead + i;
88
89     ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
90     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
91     ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
92        bytes_written);
93     b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
94     ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
95     ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
96     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
97     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
98
99     HeapFree( GetProcessHeap(), 0, src );
100     HeapFree( GetProcessHeap(), 0, dst );
101
102     /*
103      * The following tests parallel those in test_VirtualAlloc()
104      */
105
106     SetLastError(0xdeadbeef);
107     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
108     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
109     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
110        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
111         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
112
113     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
114     ok(addr1 != NULL, "VirtualAllocEx failed\n");
115
116     /* test a not committed memory */
117     memset(&info, 'q', sizeof(info));
118     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
119     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
120     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
121     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
122     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
123     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
124     /* NT reports Protect == 0 for a not committed memory block */
125     ok(info.Protect == 0 /* NT */ ||
126        info.Protect == PAGE_NOACCESS, /* Win9x */
127         "%x != PAGE_NOACCESS\n", info.Protect);
128     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
129
130     SetLastError(0xdeadbeef);
131     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
132        "VirtualProtectEx should fail on a not committed memory\n");
133     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
134        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
135         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
136
137     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
138     ok(addr1 == addr2, "VirtualAllocEx failed\n");
139
140     /* test a committed memory */
141     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
142         "VirtualQueryEx failed\n");
143     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
144     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
145     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
146     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
147     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
148     /* this time NT reports PAGE_NOACCESS as well */
149     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
150     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
151
152     /* this should fail, since not the whole range is committed yet */
153     SetLastError(0xdeadbeef);
154     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
155         "VirtualProtectEx should fail on a not committed memory\n");
156     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
157        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
158         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
159
160     old_prot = 0;
161     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
162     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
163
164     old_prot = 0;
165     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
166     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
167
168     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
169        "VirtualFreeEx should fail with type 0\n");
170     ok(GetLastError() == ERROR_INVALID_PARAMETER,
171         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
172
173     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
174
175     /* if the type is MEM_RELEASE, size must be 0 */
176     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
177        "VirtualFreeEx should fail\n");
178     ok(GetLastError() == ERROR_INVALID_PARAMETER,
179         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
180
181     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
182
183     TerminateProcess(hProcess, 0);
184     CloseHandle(hProcess);
185 }
186
187 static void test_VirtualAlloc(void)
188 {
189     void *addr1, *addr2;
190     DWORD old_prot;
191     MEMORY_BASIC_INFORMATION info;
192
193     SetLastError(0xdeadbeef);
194     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
195     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
196     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
197        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
198         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
199
200     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
201     ok(addr1 != NULL, "VirtualAlloc failed\n");
202
203     /* test a not committed memory */
204     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
205         "VirtualQuery failed\n");
206     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
207     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
208     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
209     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
210     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
211     /* NT reports Protect == 0 for a not committed memory block */
212     ok(info.Protect == 0 /* NT */ ||
213        info.Protect == PAGE_NOACCESS, /* Win9x */
214         "%x != PAGE_NOACCESS\n", info.Protect);
215     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
216
217     SetLastError(0xdeadbeef);
218     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
219        "VirtualProtect should fail on a not committed memory\n");
220     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
221        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
222         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
223
224     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
225     ok(addr1 == addr2, "VirtualAlloc failed\n");
226
227     /* test a committed memory */
228     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
229         "VirtualQuery failed\n");
230     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
231     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
232     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
233     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
234     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
235     /* this time NT reports PAGE_NOACCESS as well */
236     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
237     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
238
239     /* this should fail, since not the whole range is committed yet */
240     SetLastError(0xdeadbeef);
241     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
242         "VirtualProtect should fail on a not committed memory\n");
243     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
244        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
245         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
246
247     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
248     ok(old_prot == PAGE_NOACCESS,
249         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
250
251     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
252     ok(old_prot == PAGE_READONLY,
253         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
254
255     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
256     ok(GetLastError() == ERROR_INVALID_PARAMETER,
257         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
258
259     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
260
261     /* if the type is MEM_RELEASE, size must be 0 */
262     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
263     ok(GetLastError() == ERROR_INVALID_PARAMETER,
264         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
265
266     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
267 }
268
269 static void test_MapViewOfFile(void)
270 {
271     static const char testfile[] = "testfile.xxx";
272     HANDLE file, mapping;
273     void *ptr;
274
275     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
276     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
277     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
278     SetEndOfFile( file );
279
280     /* read/write mapping */
281
282     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
283     ok( mapping != 0, "CreateFileMapping failed\n" );
284
285     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
286     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ failed\n" );
287     UnmapViewOfFile( ptr );
288
289     /* this fails on win9x but succeeds on NT */
290     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
291     if (ptr) UnmapViewOfFile( ptr );
292     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
293
294     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
295     ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
296     UnmapViewOfFile( ptr );
297
298     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
299     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE failed\n" );
300     UnmapViewOfFile( ptr );
301     CloseHandle( mapping );
302
303     /* read-only mapping */
304
305     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
306     ok( mapping != 0, "CreateFileMapping failed\n" );
307
308     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
309     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
310     UnmapViewOfFile( ptr );
311
312     /* this fails on win9x but succeeds on NT */
313     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
314     if (ptr) UnmapViewOfFile( ptr );
315     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
316
317     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
318     ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
319     UnmapViewOfFile( ptr );
320
321     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
322     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
323     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
324         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
325     CloseHandle( mapping );
326
327     /* copy-on-write mapping */
328
329     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
330     ok( mapping != 0, "CreateFileMapping failed\n" );
331
332     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
333     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
334     UnmapViewOfFile( ptr );
335
336     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
337     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY failed\n" );
338     UnmapViewOfFile( ptr );
339
340     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
341     ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
342     UnmapViewOfFile( ptr );
343
344     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
345     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
346     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
347         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
348     CloseHandle( mapping );
349
350     /* no access mapping */
351
352     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
353     /* fails on NT but succeeds on win9x */
354     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
355     else
356     {
357         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
358         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
359         UnmapViewOfFile( ptr );
360
361         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
362         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
363         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
364
365         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
366         ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
367         UnmapViewOfFile( ptr );
368
369         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
370         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
371         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
372
373         CloseHandle( mapping );
374     }
375
376     CloseHandle( file );
377
378     /* now try read-only file */
379
380     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
381     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
382
383     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
384     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
385     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
386         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
387
388     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
389     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
390     CloseHandle( mapping );
391
392     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
393     ok( mapping != 0, "CreateFileMapping PAGE_READONLY failed\n" );
394     CloseHandle( mapping );
395     CloseHandle( file );
396
397     /* now try no access file */
398
399     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
400     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
401
402     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
403     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
404     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
405         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
406
407     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
408     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
409     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
410         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
411
412     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
413     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
414     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
415         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
416
417     CloseHandle( file );
418     DeleteFileA( testfile );
419 }
420
421 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
422                                             ULONG zero_bits, SIZE_T commit_size,
423                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
424                                             ULONG inherit, ULONG alloc_type, ULONG protect );
425 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
426
427 static void test_NtMapViewOfSection(void)
428 {
429     HANDLE hProcess;
430
431     static const char testfile[] = "testfile.xxx";
432     static const char data[] = "test data for NtMapViewOfSection";
433     char buffer[sizeof(data)];
434     HANDLE file, mapping;
435     void *ptr;
436     BOOL ret;
437     DWORD status, written;
438     SIZE_T size, result;
439     LARGE_INTEGER offset;
440
441     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
442     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
443     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
444     {
445         skip( "NtMapViewOfSection not found\n" );
446         return;
447     }
448
449     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
450     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
451     WriteFile( file, data, sizeof(data), &written, NULL );
452     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
453     SetEndOfFile( file );
454
455     /* read/write mapping */
456
457     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
458     ok( mapping != 0, "CreateFileMapping failed\n" );
459
460     hProcess = create_target_process("sleep");
461     ok(hProcess != NULL, "Can't start process\n");
462
463     ptr = NULL;
464     size = 0;
465     offset.QuadPart = 0;
466     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
467     ok( !status, "NtMapViewOfSection failed status %x\n", status );
468
469     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
470     ok( ret, "ReadProcessMemory failed\n" );
471     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
472     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
473
474     status = pNtUnmapViewOfSection( hProcess, ptr );
475     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
476
477     CloseHandle( mapping );
478     CloseHandle( file );
479     DeleteFileA( testfile );
480 }
481
482 START_TEST(virtual)
483 {
484     int argc;
485     char **argv;
486     argc = winetest_get_mainargs( &argv );
487
488     if (argc >= 3)
489     {
490         if (!strcmp(argv[2], "sleep"))
491         {
492             Sleep(5000); /* spawned process runs for at most 5 seconds */
493             return;
494         }
495         while (1)
496         {
497             void *mem;
498             BOOL ret;
499             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
500                                PAGE_EXECUTE_READWRITE);
501             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
502             if (mem == NULL) break;
503             ret = VirtualFree(mem, 0, MEM_RELEASE);
504             ok(ret, "VirtualFree failed %u\n", GetLastError());
505             if (!ret) break;
506         }
507         return;
508     }
509
510     hkernel32 = GetModuleHandleA("kernel32.dll");
511     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
512     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
513
514     test_VirtualAllocEx();
515     test_VirtualAlloc();
516     test_MapViewOfFile();
517     test_NtMapViewOfSection();
518 }