ntdll: Don't force anonymous file mappings to always be fully committed.
[wine/multimedia.git] / dlls / kernel32 / tests / virtual.c
blob032af2b9c8b3d4f982162f6b3f8ba50fc1c2cf9a
1 /*
2 * Unit test suite for Virtual* family of APIs.
4 * Copyright 2004 Dmitry Timoshkov
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/test.h"
29 #define NUM_THREADS 4
30 #define MAPPING_SIZE 0x100000
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);
36 /* ############################### */
38 static HANDLE create_target_process(const char *arg)
40 char **argv;
41 char cmdline[MAX_PATH];
42 PROCESS_INFORMATION pi;
43 STARTUPINFO si = { 0 };
44 si.cb = sizeof(si);
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;
54 static void test_VirtualAllocEx(void)
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;
65 /* not exported in all windows-versions */
66 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
67 skip("VirtualAllocEx not found\n");
68 return;
71 hProcess = create_target_process("sleep");
72 ok(hProcess != NULL, "Can't start process\n");
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;
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;
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());
100 HeapFree( GetProcessHeap(), 0, src );
101 HeapFree( GetProcessHeap(), 0, dst );
104 * The following tests parallel those in test_VirtualAlloc()
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());
114 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
115 ok(addr1 != NULL, "VirtualAllocEx failed\n");
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);
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());
138 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
139 ok(addr1 == addr2, "VirtualAllocEx failed\n");
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);
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());
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);
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);
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());
174 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
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());
182 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
184 TerminateProcess(hProcess, 0);
185 CloseHandle(hProcess);
188 static void test_VirtualAlloc(void)
190 void *addr1, *addr2;
191 DWORD old_prot;
192 MEMORY_BASIC_INFORMATION info;
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());
201 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
202 ok(addr1 != NULL, "VirtualAlloc failed\n");
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);
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());
225 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
226 ok(addr1 == addr2, "VirtualAlloc failed\n");
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);
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());
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);
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);
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());
260 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
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());
267 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
270 static void test_MapViewOfFile(void)
272 static const char testfile[] = "testfile.xxx";
273 const char *name;
274 HANDLE file, mapping;
275 void *ptr, *ptr2;
276 MEMORY_BASIC_INFORMATION info;
277 BOOL ret;
279 SetLastError(0xdeadbeef);
280 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
281 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
282 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
283 SetEndOfFile( file );
285 /* read/write mapping */
287 SetLastError(0xdeadbeef);
288 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
289 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
291 SetLastError(0xdeadbeef);
292 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
293 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
294 UnmapViewOfFile( ptr );
296 /* this fails on win9x but succeeds on NT */
297 SetLastError(0xdeadbeef);
298 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
299 if (ptr) UnmapViewOfFile( ptr );
300 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
302 SetLastError(0xdeadbeef);
303 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
304 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
305 UnmapViewOfFile( ptr );
307 SetLastError(0xdeadbeef);
308 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
309 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
310 UnmapViewOfFile( ptr );
311 CloseHandle( mapping );
313 /* read-only mapping */
315 SetLastError(0xdeadbeef);
316 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
317 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
319 SetLastError(0xdeadbeef);
320 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
321 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
322 UnmapViewOfFile( ptr );
324 /* this fails on win9x but succeeds on NT */
325 SetLastError(0xdeadbeef);
326 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
327 if (ptr) UnmapViewOfFile( ptr );
328 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
330 SetLastError(0xdeadbeef);
331 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
332 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
333 UnmapViewOfFile( ptr );
335 SetLastError(0xdeadbeef);
336 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
337 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
338 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
339 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
340 CloseHandle( mapping );
342 /* copy-on-write mapping */
344 SetLastError(0xdeadbeef);
345 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
346 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
348 SetLastError(0xdeadbeef);
349 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
350 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
351 UnmapViewOfFile( ptr );
353 SetLastError(0xdeadbeef);
354 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
355 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
356 UnmapViewOfFile( ptr );
358 SetLastError(0xdeadbeef);
359 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
360 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
361 UnmapViewOfFile( ptr );
363 SetLastError(0xdeadbeef);
364 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
365 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
366 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
367 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
368 CloseHandle( mapping );
370 /* no access mapping */
372 SetLastError(0xdeadbeef);
373 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
374 /* fails on NT but succeeds on win9x */
375 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
376 else
378 SetLastError(0xdeadbeef);
379 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
380 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
381 UnmapViewOfFile( ptr );
383 SetLastError(0xdeadbeef);
384 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
385 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
386 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
388 SetLastError(0xdeadbeef);
389 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
390 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
391 UnmapViewOfFile( ptr );
393 SetLastError(0xdeadbeef);
394 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
395 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
396 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
398 CloseHandle( mapping );
401 CloseHandle( file );
403 /* now try read-only file */
405 SetLastError(0xdeadbeef);
406 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
407 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
409 SetLastError(0xdeadbeef);
410 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
411 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
412 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
413 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
415 SetLastError(0xdeadbeef);
416 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
417 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
418 CloseHandle( mapping );
420 SetLastError(0xdeadbeef);
421 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
422 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
423 CloseHandle( mapping );
424 CloseHandle( file );
426 /* now try no access file */
428 SetLastError(0xdeadbeef);
429 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
430 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
432 SetLastError(0xdeadbeef);
433 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
434 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
435 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
436 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
438 SetLastError(0xdeadbeef);
439 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
440 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
441 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
442 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
444 SetLastError(0xdeadbeef);
445 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
446 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
447 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
448 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
450 CloseHandle( file );
451 DeleteFileA( testfile );
453 SetLastError(0xdeadbeef);
454 name = "Local\\Foo";
455 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
456 /* nt4 doesn't have Local\\ */
457 if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
459 name = "Foo";
460 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
462 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
464 SetLastError(0xdeadbeef);
465 mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
466 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
467 SetLastError(0xdeadbeef);
468 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
469 if (!ptr)
471 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
472 SetLastError(0xdeadbeef);
473 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
474 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
475 SetLastError(0xdeadbeef);
476 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
477 "VirtualQuery error %u\n", GetLastError() );
478 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
479 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
480 ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
481 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
482 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
483 ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
485 else todo_wine win_skip( "no access checks on win9x\n" );
486 UnmapViewOfFile( ptr );
487 CloseHandle( mapping );
489 SetLastError(0xdeadbeef);
490 mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
491 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
492 SetLastError(0xdeadbeef);
493 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
494 if (!ptr)
496 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
497 SetLastError(0xdeadbeef);
498 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
499 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
500 SetLastError(0xdeadbeef);
501 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
502 "VirtualQuery error %u\n", GetLastError() );
503 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
504 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
505 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
506 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
507 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
508 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
510 else todo_wine win_skip( "no access checks on win9x\n" );
511 UnmapViewOfFile( ptr );
512 CloseHandle( mapping );
514 CloseHandle( file );
516 /* read/write mapping with SEC_RESERVE */
517 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
518 ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
520 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
521 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
523 ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
524 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
525 ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
526 trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
528 ret = VirtualQuery(ptr, &info, sizeof(info));
529 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
530 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
531 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
532 ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
533 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
534 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
535 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
536 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
538 ret = VirtualQuery(ptr2, &info, sizeof(info));
539 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
540 ok(info.BaseAddress == ptr2, "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
541 ok(info.AllocationBase == ptr2, "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
542 ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
543 ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
544 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
545 ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
546 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
548 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
549 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
551 ret = VirtualQuery(ptr, &info, sizeof(info));
552 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
553 ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
554 ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
555 ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
556 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
557 ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
558 ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
559 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
561 /* shows that the VirtualAlloc above affects the mapping, not just the
562 * virtual memory in this process - it also affects all other processes
563 * with a view of the mapping, but that isn't tested here */
564 ret = VirtualQuery(ptr2, &info, sizeof(info));
565 ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
566 ok(info.BaseAddress == ptr2, "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
567 ok(info.AllocationBase == ptr2, "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
568 ok(info.AllocationProtect == PAGE_READWRITE, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
569 ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
570 ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
571 ok(info.Protect == PAGE_READWRITE, "Protect should have been 0 instead of 0x%x\n", info.Protect);
572 ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
574 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
575 ok( !ret, "VirtualFree succeeded\n" );
576 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
578 ret = UnmapViewOfFile(ptr2);
579 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
580 ret = UnmapViewOfFile(ptr);
581 ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
582 CloseHandle(mapping);
585 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
586 ULONG zero_bits, SIZE_T commit_size,
587 const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
588 ULONG inherit, ULONG alloc_type, ULONG protect );
589 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
591 static void test_NtMapViewOfSection(void)
593 HANDLE hProcess;
595 static const char testfile[] = "testfile.xxx";
596 static const char data[] = "test data for NtMapViewOfSection";
597 char buffer[sizeof(data)];
598 HANDLE file, mapping;
599 void *ptr;
600 BOOL ret;
601 DWORD status, written;
602 SIZE_T size, result;
603 LARGE_INTEGER offset;
605 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
606 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
607 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
609 skip( "NtMapViewOfSection not found\n" );
610 return;
613 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
614 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
615 WriteFile( file, data, sizeof(data), &written, NULL );
616 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
617 SetEndOfFile( file );
619 /* read/write mapping */
621 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
622 ok( mapping != 0, "CreateFileMapping failed\n" );
624 hProcess = create_target_process("sleep");
625 ok(hProcess != NULL, "Can't start process\n");
627 ptr = NULL;
628 size = 0;
629 offset.QuadPart = 0;
630 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
631 ok( !status, "NtMapViewOfSection failed status %x\n", status );
633 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
634 ok( ret, "ReadProcessMemory failed\n" );
635 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
636 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
638 status = pNtUnmapViewOfSection( hProcess, ptr );
639 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
641 CloseHandle( mapping );
642 CloseHandle( file );
643 DeleteFileA( testfile );
645 TerminateProcess(hProcess, 0);
646 CloseHandle(hProcess);
649 static void test_CreateFileMapping(void)
651 HANDLE handle, handle2;
653 /* test case sensitivity */
655 SetLastError(0xdeadbeef);
656 handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
657 "Wine Test Mapping");
658 ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
659 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
661 SetLastError(0xdeadbeef);
662 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
663 "Wine Test Mapping");
664 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
665 ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
666 CloseHandle( handle2 );
668 SetLastError(0xdeadbeef);
669 handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
670 "WINE TEST MAPPING");
671 ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
672 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
673 CloseHandle( handle2 );
675 SetLastError(0xdeadbeef);
676 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
677 ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
678 CloseHandle( handle2 );
680 SetLastError(0xdeadbeef);
681 handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
682 ok( !handle2, "OpenFileMapping succeeded\n");
683 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError());
685 CloseHandle( handle );
688 static void test_BadPtr(void)
690 void *ptr = (void*)1;
691 /* We assume address 1 is not mapped. */
692 ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
693 ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
694 ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
697 START_TEST(virtual)
699 int argc;
700 char **argv;
701 argc = winetest_get_mainargs( &argv );
703 if (argc >= 3)
705 if (!strcmp(argv[2], "sleep"))
707 Sleep(5000); /* spawned process runs for at most 5 seconds */
708 return;
710 while (1)
712 void *mem;
713 BOOL ret;
714 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
715 PAGE_EXECUTE_READWRITE);
716 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
717 if (mem == NULL) break;
718 ret = VirtualFree(mem, 0, MEM_RELEASE);
719 ok(ret, "VirtualFree failed %u\n", GetLastError());
720 if (!ret) break;
722 return;
725 hkernel32 = GetModuleHandleA("kernel32.dll");
726 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
727 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
729 test_VirtualAllocEx();
730 test_VirtualAlloc();
731 test_MapViewOfFile();
732 test_NtMapViewOfSection();
733 test_CreateFileMapping();
734 test_BadPtr();