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
27 #include "wine/test.h"
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)
41 char cmdline[MAX_PATH];
42 PROCESS_INFORMATION pi;
43 STARTUPINFO si = { 0 };
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());
54 static void test_VirtualAllocEx(void)
56 const unsigned int alloc_size = 1<<15;
58 unsigned long bytes_written = 0, bytes_read = 0, i;
62 MEMORY_BASIC_INFORMATION info;
65 /* not exported in all windows-versions */
66 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
67 skip("VirtualAllocEx not found\n");
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)
79 skip("VirtualAllocEx not implemented\n");
80 TerminateProcess(hProcess, 0);
81 CloseHandle(hProcess);
85 src = HeapAlloc( GetProcessHeap(), 0, alloc_size );
86 dst = HeapAlloc( GetProcessHeap(), 0, alloc_size );
87 for (i = 0; i < alloc_size; i++)
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",
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());
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);
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)
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 HANDLE file, mapping;
275 MEMORY_BASIC_INFORMATION info;
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 );
284 /* read/write mapping */
286 SetLastError(0xdeadbeef);
287 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
288 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
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 );
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() );
301 SetLastError(0xdeadbeef);
302 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
303 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
304 UnmapViewOfFile( ptr );
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 );
312 /* read-only mapping */
314 SetLastError(0xdeadbeef);
315 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
316 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
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 );
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() );
329 SetLastError(0xdeadbeef);
330 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
331 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
332 UnmapViewOfFile( ptr );
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 );
341 /* copy-on-write mapping */
343 SetLastError(0xdeadbeef);
344 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
345 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
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 );
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 );
357 SetLastError(0xdeadbeef);
358 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
359 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
360 UnmapViewOfFile( ptr );
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 );
369 /* no access mapping */
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() );
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 );
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() );
387 SetLastError(0xdeadbeef);
388 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
389 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
390 UnmapViewOfFile( ptr );
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() );
397 CloseHandle( mapping );
402 /* now try read-only file */
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() );
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() );
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 );
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 );
425 /* now try no access file */
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() );
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() );
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() );
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() );
450 DeleteFileA( testfile );
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() );
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 );
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 );
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());
506 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
507 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
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);
516 ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
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);
521 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READWRITE);
522 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
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);
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);
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);
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);
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);
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);
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 );
567 static void test_NtMapViewOfSection(void)
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;
577 DWORD status, written;
579 LARGE_INTEGER offset;
581 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
582 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
583 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
585 skip( "NtMapViewOfSection not found\n" );
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 );
595 /* read/write mapping */
597 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
598 ok( mapping != 0, "CreateFileMapping failed\n" );
600 hProcess = create_target_process("sleep");
601 ok(hProcess != NULL, "Can't start process\n");
606 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
607 ok( !status, "NtMapViewOfSection failed status %x\n", status );
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" );
614 status = pNtUnmapViewOfSection( hProcess, ptr );
615 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
617 CloseHandle( mapping );
619 DeleteFileA( testfile );
621 TerminateProcess(hProcess, 0);
622 CloseHandle(hProcess);
625 static void test_CreateFileMapping(void)
627 HANDLE handle, handle2;
629 /* test case sensitivity */
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());
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 );
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 );
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 );
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());
661 CloseHandle( handle );
664 static void test_BadPtr(void)
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");
677 argc = winetest_get_mainargs( &argv );
681 if (!strcmp(argv[2], "sleep"))
683 Sleep(5000); /* spawned process runs for at most 5 seconds */
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());
701 hkernel32 = GetModuleHandleA("kernel32.dll");
702 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
703 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
705 test_VirtualAllocEx();
707 test_MapViewOfFile();
708 test_NtMapViewOfSection();
709 test_CreateFileMapping();