~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/virtual.c

Version: ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  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 
 36 /* ############################### */
 37 
 38 static HANDLE create_target_process(const char *arg)
 39 {
 40     char **argv;
 41     char cmdline[MAX_PATH];
 42     PROCESS_INFORMATION pi;
 43     STARTUPINFO si = { 0 };
 44     si.cb = sizeof(si);
 45 
 46     winetest_get_mainargs( &argv );
 47     sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
 48     ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
 49                      &si, &pi) != 0, "error: %u\n", GetLastError());
 50     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
 51     return pi.hProcess;
 52 }
 53 
 54 static void test_VirtualAllocEx(void)
 55 {
 56     const unsigned int alloc_size = 1<<15;
 57     char *src, *dst;
 58     unsigned long bytes_written = 0, bytes_read = 0, i;
 59     void *addr1, *addr2;
 60     BOOL b;
 61     DWORD old_prot;
 62     MEMORY_BASIC_INFORMATION info;
 63     HANDLE hProcess;
 64 
 65     /* not exported in all windows-versions  */
 66     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
 67         skip("VirtualAllocEx not found\n");
 68         return;
 69     }
 70 
 71     hProcess = create_target_process("sleep");
 72     ok(hProcess != NULL, "Can't start process\n");
 73 
 74     SetLastError(0xdeadbeef);
 75     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
 76                            PAGE_EXECUTE_READWRITE);
 77     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
 78     {   /* Win9x */
 79         skip("VirtualAllocEx not implemented\n");
 80         TerminateProcess(hProcess, 0);
 81         CloseHandle(hProcess);
 82         return;
 83     }
 84 
 85     src = HeapAlloc( GetProcessHeap(), 0, alloc_size );
 86     dst = HeapAlloc( GetProcessHeap(), 0, alloc_size );
 87     for (i = 0; i < alloc_size; i++)
 88         src[i] = i & 0xff;
 89 
 90     ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
 91     b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
 92     ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
 93        bytes_written);
 94     b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
 95     ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
 96     ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
 97     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
 98     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
 99 
100     HeapFree( GetProcessHeap(), 0, src );
101     HeapFree( GetProcessHeap(), 0, dst );
102 
103     /*
104      * The following tests parallel those in test_VirtualAlloc()
105      */
106 
107     SetLastError(0xdeadbeef);
108     addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
109     ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
110     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
111        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
112         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
113 
114     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
115     ok(addr1 != NULL, "VirtualAllocEx failed\n");
116 
117     /* test a not committed memory */
118     memset(&info, 'q', sizeof(info));
119     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
120     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
121     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
122     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
123     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
124     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
125     /* NT reports Protect == 0 for a not committed memory block */
126     ok(info.Protect == 0 /* NT */ ||
127        info.Protect == PAGE_NOACCESS, /* Win9x */
128         "%x != PAGE_NOACCESS\n", info.Protect);
129     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
130 
131     SetLastError(0xdeadbeef);
132     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
133        "VirtualProtectEx should fail on a not committed memory\n");
134     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
135        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
136         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
137 
138     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
139     ok(addr1 == addr2, "VirtualAllocEx failed\n");
140 
141     /* test a committed memory */
142     ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
143         "VirtualQueryEx failed\n");
144     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
145     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
146     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
147     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
148     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
149     /* this time NT reports PAGE_NOACCESS as well */
150     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
151     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
152 
153     /* this should fail, since not the whole range is committed yet */
154     SetLastError(0xdeadbeef);
155     ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
156         "VirtualProtectEx should fail on a not committed memory\n");
157     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
158        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
159         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
160 
161     old_prot = 0;
162     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
163     ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
164 
165     old_prot = 0;
166     ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
167     ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
168 
169     ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
170        "VirtualFreeEx should fail with type 0\n");
171     ok(GetLastError() == ERROR_INVALID_PARAMETER,
172         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
173 
174     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
175 
176     /* if the type is MEM_RELEASE, size must be 0 */
177     ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
178        "VirtualFreeEx should fail\n");
179     ok(GetLastError() == ERROR_INVALID_PARAMETER,
180         "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
181 
182     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
183 
184     TerminateProcess(hProcess, 0);
185     CloseHandle(hProcess);
186 }
187 
188 static void test_VirtualAlloc(void)
189 {
190     void *addr1, *addr2;
191     DWORD old_prot;
192     MEMORY_BASIC_INFORMATION info;
193 
194     SetLastError(0xdeadbeef);
195     addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
196     ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
197     ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
198        GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
199         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
200 
201     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
202     ok(addr1 != NULL, "VirtualAlloc failed\n");
203 
204     /* test a not committed memory */
205     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
206         "VirtualQuery failed\n");
207     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
208     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
209     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
210     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
211     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
212     /* NT reports Protect == 0 for a not committed memory block */
213     ok(info.Protect == 0 /* NT */ ||
214        info.Protect == PAGE_NOACCESS, /* Win9x */
215         "%x != PAGE_NOACCESS\n", info.Protect);
216     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
217 
218     SetLastError(0xdeadbeef);
219     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
220        "VirtualProtect should fail on a not committed memory\n");
221     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
222        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
223         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
224 
225     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
226     ok(addr1 == addr2, "VirtualAlloc failed\n");
227 
228     /* test a committed memory */
229     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
230         "VirtualQuery failed\n");
231     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
232     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
233     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
234     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
235     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
236     /* this time NT reports PAGE_NOACCESS as well */
237     ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
238     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
239 
240     /* this should fail, since not the whole range is committed yet */
241     SetLastError(0xdeadbeef);
242     ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
243         "VirtualProtect should fail on a not committed memory\n");
244     ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
245        GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
246         "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
247 
248     ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
249     ok(old_prot == PAGE_NOACCESS,
250         "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
251 
252     ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
253     ok(old_prot == PAGE_READONLY,
254         "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
255 
256     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
257     ok(GetLastError() == ERROR_INVALID_PARAMETER,
258         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
259 
260     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
261 
262     /* if the type is MEM_RELEASE, size must be 0 */
263     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
264     ok(GetLastError() == ERROR_INVALID_PARAMETER,
265         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
266 
267     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
268 }
269 
270 static void test_MapViewOfFile(void)
271 {
272     static const char testfile[] = "testfile.xxx";
273     HANDLE file, mapping;
274     void *ptr, *ptr2;
275     MEMORY_BASIC_INFORMATION info;
276     BOOL ret;
277 
278     SetLastError(0xdeadbeef);
279     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
280     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
281     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
282     SetEndOfFile( file );
283 
284     /* read/write mapping */
285 
286     SetLastError(0xdeadbeef);
287     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
288     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
289 
290     SetLastError(0xdeadbeef);
291     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
292     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
293     UnmapViewOfFile( ptr );
294 
295     /* this fails on win9x but succeeds on NT */
296     SetLastError(0xdeadbeef);
297     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
298     if (ptr) UnmapViewOfFile( ptr );
299     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
300 
301     SetLastError(0xdeadbeef);
302     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
303     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
304     UnmapViewOfFile( ptr );
305 
306     SetLastError(0xdeadbeef);
307     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
308     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
309     UnmapViewOfFile( ptr );
310     CloseHandle( mapping );
311 
312     /* read-only mapping */
313 
314     SetLastError(0xdeadbeef);
315     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
316     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
317 
318     SetLastError(0xdeadbeef);
319     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
320     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
321     UnmapViewOfFile( ptr );
322 
323     /* this fails on win9x but succeeds on NT */
324     SetLastError(0xdeadbeef);
325     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
326     if (ptr) UnmapViewOfFile( ptr );
327     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
328 
329     SetLastError(0xdeadbeef);
330     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
331     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
332     UnmapViewOfFile( ptr );
333 
334     SetLastError(0xdeadbeef);
335     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
336     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
337     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
338         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
339     CloseHandle( mapping );
340 
341     /* copy-on-write mapping */
342 
343     SetLastError(0xdeadbeef);
344     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
345     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
346 
347     SetLastError(0xdeadbeef);
348     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
349     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
350     UnmapViewOfFile( ptr );
351 
352     SetLastError(0xdeadbeef);
353     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
354     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
355     UnmapViewOfFile( ptr );
356 
357     SetLastError(0xdeadbeef);
358     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
359     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
360     UnmapViewOfFile( ptr );
361 
362     SetLastError(0xdeadbeef);
363     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
364     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
365     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
366         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
367     CloseHandle( mapping );
368 
369     /* no access mapping */
370 
371     SetLastError(0xdeadbeef);
372     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
373     /* fails on NT but succeeds on win9x */
374     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
375     else
376     {
377         SetLastError(0xdeadbeef);
378         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
379         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
380         UnmapViewOfFile( ptr );
381 
382         SetLastError(0xdeadbeef);
383         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
384         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
385         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
386 
387         SetLastError(0xdeadbeef);
388         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
389         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
390         UnmapViewOfFile( ptr );
391 
392         SetLastError(0xdeadbeef);
393         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
394         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
395         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
396 
397         CloseHandle( mapping );
398     }
399 
400     CloseHandle( file );
401 
402     /* now try read-only file */
403 
404     SetLastError(0xdeadbeef);
405     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
406     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
407 
408     SetLastError(0xdeadbeef);
409     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
410     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
411     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
412         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
413 
414     SetLastError(0xdeadbeef);
415     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
416     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
417     CloseHandle( mapping );
418 
419     SetLastError(0xdeadbeef);
420     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
421     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
422     CloseHandle( mapping );
423     CloseHandle( file );
424 
425     /* now try no access file */
426 
427     SetLastError(0xdeadbeef);
428     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
429     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
430 
431     SetLastError(0xdeadbeef);
432     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
433     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
434     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
435         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
436 
437     SetLastError(0xdeadbeef);
438     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
439     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
440     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
441         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
442 
443     SetLastError(0xdeadbeef);
444     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
445     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
446     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
447         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
448 
449     CloseHandle( file );
450     DeleteFileA( testfile );
451 
452     SetLastError(0xdeadbeef);
453     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "Local\\Foo");
454     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
455 
456     SetLastError(0xdeadbeef);
457     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, "Local\\Foo" );
458     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
459     SetLastError(0xdeadbeef);
460     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
461 todo_wine ok( !ptr, "MapViewOfFile FILE_MAP_WRITE should fail\n" );
462 todo_wine ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
463     SetLastError(0xdeadbeef);
464     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
465     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
466     SetLastError(0xdeadbeef);
467     ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
468         "VirtualQuery error %u\n", GetLastError() );
469     ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
470     ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
471 todo_wine ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
472     ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
473     ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
474 todo_wine ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
475     UnmapViewOfFile( ptr );
476     CloseHandle( mapping );
477 
478     SetLastError(0xdeadbeef);
479     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, "Local\\Foo" );
480     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
481     SetLastError(0xdeadbeef);
482     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
483 todo_wine ok( !ptr, "MapViewOfFile FILE_MAP_READ should fail\n" );
484 todo_wine ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
485     SetLastError(0xdeadbeef);
486     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
487     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
488     SetLastError(0xdeadbeef);
489     ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
490         "VirtualQuery error %u\n", GetLastError() );
491     ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
492     ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
493     ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
494     ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
495     ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
496     ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
497     UnmapViewOfFile( ptr );
498     CloseHandle( mapping );
499 
500     CloseHandle( file );
501 
502     /* read/write mapping with SEC_RESERVE */
503     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
504     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
505 
506     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
507     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
508 
509     ret = VirtualQuery(ptr, &info, sizeof(info));
510     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
511     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
512     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
513     ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
514     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
515 todo_wine
516     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
517 todo_wine
518     ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
519     ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
520 
521     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READWRITE);
522     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
523 
524     ret = VirtualQuery(ptr, &info, sizeof(info));
525     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
526     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
527     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
528     ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
529 todo_wine
530     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
531     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
532     ok(info.Protect == PAGE_READWRITE, "Protect should have been 0 instead of 0x%x\n", info.Protect);
533     ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
534 
535     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
536     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
537     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
538     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
539 
540     /* shows that the VirtualAlloc above affects the mapping, not just the
541      * virtual memory in this process - it also affects all other processes
542      * with a view of the mapping, but that isn't tested here */
543     ret = VirtualQuery(ptr2, &info, sizeof(info));
544     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
545     ok(info.BaseAddress == ptr2, "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
546     ok(info.AllocationBase == ptr2, "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
547     ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
548 todo_wine
549     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
550     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
551     ok(info.Protect == PAGE_READWRITE, "Protect should have been 0 instead of 0x%x\n", info.Protect);
552     ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
553 
554     ret = UnmapViewOfFile(ptr2);
555     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
556     ret = UnmapViewOfFile(ptr);
557     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
558     CloseHandle(mapping);
559 }
560 
561 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
562                                             ULONG zero_bits, SIZE_T commit_size,
563                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
564                                             ULONG inherit, ULONG alloc_type, ULONG protect );
565 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
566 
567 static void test_NtMapViewOfSection(void)
568 {
569     HANDLE hProcess;
570 
571     static const char testfile[] = "testfile.xxx";
572     static const char data[] = "test data for NtMapViewOfSection";
573     char buffer[sizeof(data)];
574     HANDLE file, mapping;
575     void *ptr;
576     BOOL ret;
577     DWORD status, written;
578     SIZE_T size, result;
579     LARGE_INTEGER offset;
580 
581     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
582     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
583     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
584     {
585         skip( "NtMapViewOfSection not found\n" );
586         return;
587     }
588 
589     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
590     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
591     WriteFile( file, data, sizeof(data), &written, NULL );
592     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
593     SetEndOfFile( file );
594 
595     /* read/write mapping */
596 
597     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
598     ok( mapping != 0, "CreateFileMapping failed\n" );
599 
600     hProcess = create_target_process("sleep");
601     ok(hProcess != NULL, "Can't start process\n");
602 
603     ptr = NULL;
604     size = 0;
605     offset.QuadPart = 0;
606     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
607     ok( !status, "NtMapViewOfSection failed status %x\n", status );
608 
609     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
610     ok( ret, "ReadProcessMemory failed\n" );
611     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
612     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
613 
614     status = pNtUnmapViewOfSection( hProcess, ptr );
615     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
616 
617     CloseHandle( mapping );
618     CloseHandle( file );
619     DeleteFileA( testfile );
620 
621     TerminateProcess(hProcess, 0);
622     CloseHandle(hProcess);
623 }
624 
625 static void test_CreateFileMapping(void)
626 {
627     HANDLE handle, handle2;
628 
629     /* test case sensitivity */
630 
631     SetLastError(0xdeadbeef);
632     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
633                                  __FILE__ ": Test Mapping");
634     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
635     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
636 
637     SetLastError(0xdeadbeef);
638     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
639                                   __FILE__ ": Test Mapping");
640     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
641     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
642     CloseHandle( handle2 );
643 
644     SetLastError(0xdeadbeef);
645     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
646                                  __FILE__ ": TEST MAPPING");
647     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
648     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
649     CloseHandle( handle2 );
650 
651     SetLastError(0xdeadbeef);
652     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, __FILE__ ": Test Mapping");
653     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
654     CloseHandle( handle2 );
655 
656     SetLastError(0xdeadbeef);
657     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, __FILE__ ": TEST MAPPING");
658     ok( !handle2, "OpenFileMapping succeeded\n");
659     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError());
660 
661     CloseHandle( handle );
662 }
663 
664 static void test_BadPtr(void)
665 {
666     void *ptr = (void*)1;
667     /* We assume address 1 is not mapped. */
668     ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
669     ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
670     ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
671 }
672 
673 START_TEST(virtual)
674 {
675     int argc;
676     char **argv;
677     argc = winetest_get_mainargs( &argv );
678 
679     if (argc >= 3)
680     {
681         if (!strcmp(argv[2], "sleep"))
682         {
683             Sleep(5000); /* spawned process runs for at most 5 seconds */
684             return;
685         }
686         while (1)
687         {
688             void *mem;
689             BOOL ret;
690             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
691                                PAGE_EXECUTE_READWRITE);
692             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
693             if (mem == NULL) break;
694             ret = VirtualFree(mem, 0, MEM_RELEASE);
695             ok(ret, "VirtualFree failed %u\n", GetLastError());
696             if (!ret) break;
697         }
698         return;
699     }
700 
701     hkernel32 = GetModuleHandleA("kernel32.dll");
702     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
703     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
704 
705