push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / dlls / kernel32 / tests / virtual.c
blob712ddf3996fd29601d9d69bc63f0ab2fbfecd651
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
31 static HINSTANCE hkernel32;
32 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
33 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
35 /* ############################### */
37 static HANDLE create_target_process(const char *arg)
39 char **argv;
40 char cmdline[MAX_PATH];
41 PROCESS_INFORMATION pi;
42 STARTUPINFO si = { 0 };
43 si.cb = sizeof(si);
45 winetest_get_mainargs( &argv );
46 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
47 ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
48 &si, &pi) != 0, "error: %u\n", GetLastError());
49 ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
50 return pi.hProcess;
53 static void test_VirtualAllocEx(void)
55 const unsigned int alloc_size = 1<<15;
56 char *src, *dst;
57 unsigned long bytes_written = 0, bytes_read = 0, i;
58 void *addr1, *addr2;
59 BOOL b;
60 DWORD old_prot;
61 MEMORY_BASIC_INFORMATION info;
62 HANDLE hProcess;
64 /* not exported in all windows-versions */
65 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
66 skip("VirtualAllocEx not found\n");
67 return;
70 hProcess = create_target_process("sleep");
71 ok(hProcess != NULL, "Can't start process\n");
73 SetLastError(0xdeadbeef);
74 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
75 PAGE_EXECUTE_READWRITE);
76 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
77 { /* Win9x */
78 skip("VirtualAllocEx not implemented\n");
79 TerminateProcess(hProcess, 0);
80 CloseHandle(hProcess);
81 return;
84 src = HeapAlloc( GetProcessHeap(), 0, alloc_size );
85 dst = HeapAlloc( GetProcessHeap(), 0, alloc_size );
86 for (i = 0; i < alloc_size; i++)
87 src[i] = i & 0xff;
89 ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
90 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
91 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
92 bytes_written);
93 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
94 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
95 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
96 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
97 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
99 HeapFree( GetProcessHeap(), 0, src );
100 HeapFree( GetProcessHeap(), 0, dst );
103 * The following tests parallel those in test_VirtualAlloc()
106 SetLastError(0xdeadbeef);
107 addr1 = pVirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
108 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
109 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
110 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
111 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
113 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
114 ok(addr1 != NULL, "VirtualAllocEx failed\n");
116 /* test a not committed memory */
117 memset(&info, 'q', sizeof(info));
118 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info), "VirtualQueryEx failed\n");
119 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
120 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
121 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
122 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
123 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
124 /* NT reports Protect == 0 for a not committed memory block */
125 ok(info.Protect == 0 /* NT */ ||
126 info.Protect == PAGE_NOACCESS, /* Win9x */
127 "%x != PAGE_NOACCESS\n", info.Protect);
128 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
130 SetLastError(0xdeadbeef);
131 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
132 "VirtualProtectEx should fail on a not committed memory\n");
133 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
134 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
135 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
137 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
138 ok(addr1 == addr2, "VirtualAllocEx failed\n");
140 /* test a committed memory */
141 ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info)) == sizeof(info),
142 "VirtualQueryEx failed\n");
143 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
144 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
145 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
146 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
147 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
148 /* this time NT reports PAGE_NOACCESS as well */
149 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
150 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
152 /* this should fail, since not the whole range is committed yet */
153 SetLastError(0xdeadbeef);
154 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
155 "VirtualProtectEx should fail on a not committed memory\n");
156 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
157 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
158 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
160 old_prot = 0;
161 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtectEx failed\n");
162 ok(old_prot == PAGE_NOACCESS, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
164 old_prot = 0;
165 ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtectEx failed\n");
166 ok(old_prot == PAGE_READONLY, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
168 ok(!pVirtualFreeEx(hProcess, addr1, 0x10000, 0),
169 "VirtualFreeEx should fail with type 0\n");
170 ok(GetLastError() == ERROR_INVALID_PARAMETER,
171 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
173 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
175 /* if the type is MEM_RELEASE, size must be 0 */
176 ok(!pVirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
177 "VirtualFreeEx should fail\n");
178 ok(GetLastError() == ERROR_INVALID_PARAMETER,
179 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
181 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
183 TerminateProcess(hProcess, 0);
184 CloseHandle(hProcess);
187 static void test_VirtualAlloc(void)
189 void *addr1, *addr2;
190 DWORD old_prot;
191 MEMORY_BASIC_INFORMATION info;
193 SetLastError(0xdeadbeef);
194 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
195 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
196 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
197 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
198 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
200 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
201 ok(addr1 != NULL, "VirtualAlloc failed\n");
203 /* test a not committed memory */
204 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
205 "VirtualQuery failed\n");
206 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
207 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
208 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
209 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
210 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
211 /* NT reports Protect == 0 for a not committed memory block */
212 ok(info.Protect == 0 /* NT */ ||
213 info.Protect == PAGE_NOACCESS, /* Win9x */
214 "%x != PAGE_NOACCESS\n", info.Protect);
215 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
217 SetLastError(0xdeadbeef);
218 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
219 "VirtualProtect should fail on a not committed memory\n");
220 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
221 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
222 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
224 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
225 ok(addr1 == addr2, "VirtualAlloc failed\n");
227 /* test a committed memory */
228 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
229 "VirtualQuery failed\n");
230 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
231 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
232 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
233 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
234 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
235 /* this time NT reports PAGE_NOACCESS as well */
236 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
237 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
239 /* this should fail, since not the whole range is committed yet */
240 SetLastError(0xdeadbeef);
241 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
242 "VirtualProtect should fail on a not committed memory\n");
243 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
244 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
245 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
247 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
248 ok(old_prot == PAGE_NOACCESS,
249 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
251 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
252 ok(old_prot == PAGE_READONLY,
253 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
255 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
256 ok(GetLastError() == ERROR_INVALID_PARAMETER,
257 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
259 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
261 /* if the type is MEM_RELEASE, size must be 0 */
262 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
263 ok(GetLastError() == ERROR_INVALID_PARAMETER,
264 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
266 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
269 static void test_MapViewOfFile(void)
271 static const char testfile[] = "testfile.xxx";
272 HANDLE file, mapping;
273 void *ptr;
274 MEMORY_BASIC_INFORMATION info;
276 SetLastError(0xdeadbeef);
277 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
278 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
279 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
280 SetEndOfFile( file );
282 /* read/write mapping */
284 SetLastError(0xdeadbeef);
285 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
286 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
288 SetLastError(0xdeadbeef);
289 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
290 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
291 UnmapViewOfFile( ptr );
293 /* this fails on win9x but succeeds on NT */
294 SetLastError(0xdeadbeef);
295 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
296 if (ptr) UnmapViewOfFile( ptr );
297 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
299 SetLastError(0xdeadbeef);
300 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
301 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
302 UnmapViewOfFile( ptr );
304 SetLastError(0xdeadbeef);
305 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
306 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
307 UnmapViewOfFile( ptr );
308 CloseHandle( mapping );
310 /* read-only mapping */
312 SetLastError(0xdeadbeef);
313 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
314 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
316 SetLastError(0xdeadbeef);
317 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
318 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
319 UnmapViewOfFile( ptr );
321 /* this fails on win9x but succeeds on NT */
322 SetLastError(0xdeadbeef);
323 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
324 if (ptr) UnmapViewOfFile( ptr );
325 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
327 SetLastError(0xdeadbeef);
328 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
329 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
330 UnmapViewOfFile( ptr );
332 SetLastError(0xdeadbeef);
333 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
334 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
335 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
336 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
337 CloseHandle( mapping );
339 /* copy-on-write mapping */
341 SetLastError(0xdeadbeef);
342 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
343 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
345 SetLastError(0xdeadbeef);
346 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
347 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
348 UnmapViewOfFile( ptr );
350 SetLastError(0xdeadbeef);
351 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
352 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
353 UnmapViewOfFile( ptr );
355 SetLastError(0xdeadbeef);
356 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
357 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
358 UnmapViewOfFile( ptr );
360 SetLastError(0xdeadbeef);
361 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
362 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
363 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
364 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
365 CloseHandle( mapping );
367 /* no access mapping */
369 SetLastError(0xdeadbeef);
370 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
371 /* fails on NT but succeeds on win9x */
372 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
373 else
375 SetLastError(0xdeadbeef);
376 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
377 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
378 UnmapViewOfFile( ptr );
380 SetLastError(0xdeadbeef);
381 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
382 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
383 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
385 SetLastError(0xdeadbeef);
386 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
387 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
388 UnmapViewOfFile( ptr );
390 SetLastError(0xdeadbeef);
391 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
392 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
393 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
395 CloseHandle( mapping );
398 CloseHandle( file );
400 /* now try read-only file */
402 SetLastError(0xdeadbeef);
403 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
404 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
406 SetLastError(0xdeadbeef);
407 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
408 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
409 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
410 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
412 SetLastError(0xdeadbeef);
413 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
414 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
415 CloseHandle( mapping );
417 SetLastError(0xdeadbeef);
418 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
419 ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
420 CloseHandle( mapping );
421 CloseHandle( file );
423 /* now try no access file */
425 SetLastError(0xdeadbeef);
426 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
427 ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
429 SetLastError(0xdeadbeef);
430 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
431 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
432 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
433 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
435 SetLastError(0xdeadbeef);
436 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
437 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
438 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
439 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
441 SetLastError(0xdeadbeef);
442 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
443 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
444 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
445 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
447 CloseHandle( file );
448 DeleteFileA( testfile );
450 SetLastError(0xdeadbeef);
451 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "Global\\Foo");
452 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
454 SetLastError(0xdeadbeef);
455 mapping = OpenFileMapping( FILE_MAP_READ, FALSE, "Global\\Foo" );
456 ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
457 SetLastError(0xdeadbeef);
458 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
459 todo_wine ok( !ptr, "MapViewOfFile FILE_MAP_WRITE should fail\n" );
460 todo_wine ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
461 SetLastError(0xdeadbeef);
462 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
463 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
464 SetLastError(0xdeadbeef);
465 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
466 "VirtualQuery error %u\n", GetLastError() );
467 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
468 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
469 todo_wine ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
470 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
471 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
472 todo_wine ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
473 UnmapViewOfFile( ptr );
474 CloseHandle( mapping );
476 SetLastError(0xdeadbeef);
477 mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, "Global\\Foo" );
478 ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
479 SetLastError(0xdeadbeef);
480 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
481 todo_wine ok( !ptr, "MapViewOfFile FILE_MAP_READ should fail\n" );
482 todo_wine ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
483 SetLastError(0xdeadbeef);
484 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
485 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
486 SetLastError(0xdeadbeef);
487 ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
488 "VirtualQuery error %u\n", GetLastError() );
489 ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
490 ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
491 ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
492 ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
493 ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
494 ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
495 UnmapViewOfFile( ptr );
496 CloseHandle( mapping );
498 CloseHandle( file );
501 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
502 ULONG zero_bits, SIZE_T commit_size,
503 const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
504 ULONG inherit, ULONG alloc_type, ULONG protect );
505 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
507 static void test_NtMapViewOfSection(void)
509 HANDLE hProcess;
511 static const char testfile[] = "testfile.xxx";
512 static const char data[] = "test data for NtMapViewOfSection";
513 char buffer[sizeof(data)];
514 HANDLE file, mapping;
515 void *ptr;
516 BOOL ret;
517 DWORD status, written;
518 SIZE_T size, result;
519 LARGE_INTEGER offset;
521 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
522 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
523 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
525 skip( "NtMapViewOfSection not found\n" );
526 return;
529 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
530 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
531 WriteFile( file, data, sizeof(data), &written, NULL );
532 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
533 SetEndOfFile( file );
535 /* read/write mapping */
537 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
538 ok( mapping != 0, "CreateFileMapping failed\n" );
540 hProcess = create_target_process("sleep");
541 ok(hProcess != NULL, "Can't start process\n");
543 ptr = NULL;
544 size = 0;
545 offset.QuadPart = 0;
546 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
547 ok( !status, "NtMapViewOfSection failed status %x\n", status );
549 ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
550 ok( ret, "ReadProcessMemory failed\n" );
551 ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
552 ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
554 status = pNtUnmapViewOfSection( hProcess, ptr );
555 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
557 CloseHandle( mapping );
558 CloseHandle( file );
559 DeleteFileA( testfile );
562 static void test_BadPtr(void)
564 void *ptr = (void*)1;
565 /* We assume address 1 is not mapped. */
566 ok(IsBadReadPtr(ptr,1),"IsBadReadPtr(1) failed.\n");
567 ok(IsBadWritePtr(ptr,1),"IsBadWritePtr(1) failed.\n");
568 ok(IsBadCodePtr(ptr),"IsBadCodePtr(1) failed.\n");
571 START_TEST(virtual)
573 int argc;
574 char **argv;
575 argc = winetest_get_mainargs( &argv );
577 if (argc >= 3)
579 if (!strcmp(argv[2], "sleep"))
581 Sleep(5000); /* spawned process runs for at most 5 seconds */
582 return;
584 while (1)
586 void *mem;
587 BOOL ret;
588 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
589 PAGE_EXECUTE_READWRITE);
590 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
591 if (mem == NULL) break;
592 ret = VirtualFree(mem, 0, MEM_RELEASE);
593 ok(ret, "VirtualFree failed %u\n", GetLastError());
594 if (!ret) break;
596 return;
599 hkernel32 = GetModuleHandleA("kernel32.dll");
600 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
601 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
603 test_VirtualAllocEx();
604 test_VirtualAlloc();
605 test_MapViewOfFile();
606 test_NtMapViewOfSection();
607 test_BadPtr();