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"
31 static HANDLE create_target_process(const char *arg)
34 char cmdline[MAX_PATH];
35 PROCESS_INFORMATION pi;
36 STARTUPINFO si = { 0 };
39 winetest_get_mainargs( &argv );
40 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
41 ok(CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
42 &si, &pi) != 0, "error: %u\n", GetLastError());
43 ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
47 static void test_VirtualAllocEx(void)
49 const unsigned int alloc_size = 1<<15;
51 unsigned long bytes_written = 0, bytes_read = 0, i;
55 MEMORY_BASIC_INFORMATION info;
58 hProcess = create_target_process("sleep");
59 ok(hProcess != NULL, "Can't start process\n");
61 SetLastError(0xdeadbeef);
62 addr1 = VirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
63 PAGE_EXECUTE_READWRITE);
64 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
66 trace("VirtualAllocEx is not implemented, skipping the test\n");
67 TerminateProcess(hProcess, 0);
68 CloseHandle(hProcess);
72 src = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
73 dst = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
74 for (i = 0; i < alloc_size; i++)
75 src[i] = 0xcafedead + i;
77 todo_wine ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
78 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
79 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
81 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
82 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
83 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
84 b = VirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
85 todo_wine ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
87 HeapFree( GetProcessHeap(), 0, src );
88 HeapFree( GetProcessHeap(), 0, dst );
91 * The following tests parallel those in test_VirtualAlloc()
94 SetLastError(0xdeadbeef);
95 addr1 = VirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
96 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
97 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
98 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
99 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
101 addr1 = VirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
102 todo_wine ok(addr1 != NULL, "VirtualAllocEx failed\n");
104 /* test a not committed memory */
105 memset(&info, 'q', sizeof(info));
106 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
107 == sizeof(info), "VirtualQueryEx failed\n");
108 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
110 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
111 info.AllocationBase, addr1);
112 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
113 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
114 todo_wine ok(info.RegionSize == 0x10000, "%lx != 0x10000\n",
116 todo_wine ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
117 /* NT reports Protect == 0 for a not committed memory block */
118 todo_wine ok(info.Protect == 0 /* NT */ ||
119 info.Protect == PAGE_NOACCESS, /* Win9x */
120 "%x != PAGE_NOACCESS\n", info.Protect);
121 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
123 SetLastError(0xdeadbeef);
124 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
125 "VirtualProtectEx should fail on a not committed memory\n");
126 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
127 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
128 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
130 addr2 = VirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
131 ok(addr1 == addr2, "VirtualAllocEx failed\n");
133 /* test a committed memory */
134 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
136 "VirtualQueryEx failed\n");
137 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
139 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
140 info.AllocationBase, addr1);
141 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
142 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
143 todo_wine ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
144 todo_wine ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
145 /* this time NT reports PAGE_NOACCESS as well */
146 todo_wine ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
147 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
149 /* this should fail, since not the whole range is committed yet */
150 SetLastError(0xdeadbeef);
151 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
152 "VirtualProtectEx should fail on a not committed memory\n");
153 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
154 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
155 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
157 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY,
158 &old_prot), "VirtualProtectEx failed\n");
159 todo_wine ok(old_prot == PAGE_NOACCESS,
160 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
162 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE,
163 &old_prot), "VirtualProtectEx failed\n");
164 todo_wine ok(old_prot == PAGE_READONLY,
165 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
167 ok(!VirtualFreeEx(hProcess, addr1, 0x10000, 0),
168 "VirtualFreeEx should fail with type 0\n");
169 todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER,
170 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
172 todo_wine ok(VirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT),
173 "VirtualFreeEx failed\n");
175 /* if the type is MEM_RELEASE, size must be 0 */
176 ok(!VirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
177 "VirtualFreeEx should fail\n");
178 todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER,
179 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
181 todo_wine ok(VirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE),
182 "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;
276 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
277 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
278 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
279 SetEndOfFile( file );
281 /* read/write mapping */
283 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
284 ok( mapping != 0, "CreateFileMapping failed\n" );
286 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
287 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ failed\n" );
288 UnmapViewOfFile( ptr );
290 /* this fails on win9x but succeeds on NT */
291 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
292 if (ptr) UnmapViewOfFile( ptr );
293 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
295 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
296 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
297 UnmapViewOfFile( ptr );
299 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
300 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE failed\n" );
301 UnmapViewOfFile( ptr );
302 CloseHandle( mapping );
304 /* read-only mapping */
306 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
307 ok( mapping != 0, "CreateFileMapping failed\n" );
309 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
310 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
311 UnmapViewOfFile( ptr );
313 /* this fails on win9x but succeeds on NT */
314 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
315 if (ptr) UnmapViewOfFile( ptr );
316 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
318 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
319 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
320 UnmapViewOfFile( ptr );
322 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
323 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
324 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
325 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
326 CloseHandle( mapping );
328 /* copy-on-write mapping */
330 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
331 ok( mapping != 0, "CreateFileMapping failed\n" );
333 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
334 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
335 UnmapViewOfFile( ptr );
337 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
338 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY failed\n" );
339 UnmapViewOfFile( ptr );
341 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
342 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
343 UnmapViewOfFile( ptr );
345 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
346 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
347 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
348 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
349 CloseHandle( mapping );
351 /* no access mapping */
353 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
354 /* fails on NT but succeeds on win9x */
355 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
358 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
359 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
360 UnmapViewOfFile( ptr );
362 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
363 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
364 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
366 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
367 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
368 UnmapViewOfFile( ptr );
370 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
371 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
372 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
374 CloseHandle( mapping );
379 /* now try read-only file */
381 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
382 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
384 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
385 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
386 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
387 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
389 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
390 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
391 CloseHandle( mapping );
393 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
394 ok( mapping != 0, "CreateFileMapping PAGE_READONLY failed\n" );
395 CloseHandle( mapping );
398 /* now try no access file */
400 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
401 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
403 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
404 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
405 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
406 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
408 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
409 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
410 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
411 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
413 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
414 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
415 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
416 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
421 DeleteFileA( testfile );
428 argc = winetest_get_mainargs( &argv );
432 if (!strcmp(argv[2], "sleep"))
434 Sleep(5000); /* spawned process runs for at most 5 seconds */
441 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
442 PAGE_EXECUTE_READWRITE);
443 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
444 if (mem == NULL) break;
445 ret = VirtualFree(mem, 0, MEM_RELEASE);
446 ok(ret, "VirtualFree failed %u\n", GetLastError());
452 test_VirtualAllocEx();
454 test_MapViewOfFile();