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 src = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
62 dst = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
63 for (i = 0; i < alloc_size; i++)
64 src[i] = 0xcafedead + i;
66 addr1 = VirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
67 PAGE_EXECUTE_READWRITE);
68 todo_wine ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
69 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
70 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
72 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
73 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
74 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
75 b = VirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
76 todo_wine ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
78 HeapFree( GetProcessHeap(), 0, src );
79 HeapFree( GetProcessHeap(), 0, dst );
82 * The following tests parallel those in test_VirtualAlloc()
85 SetLastError(0xdeadbeef);
86 addr1 = VirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
87 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
88 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
89 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
90 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
92 addr1 = VirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
93 todo_wine ok(addr1 != NULL, "VirtualAllocEx failed\n");
95 /* test a not committed memory */
96 memset(&info, 'q', sizeof(info));
97 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
98 == sizeof(info), "VirtualQueryEx failed\n");
99 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
101 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
102 info.AllocationBase, addr1);
103 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
104 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
105 todo_wine ok(info.RegionSize == 0x10000, "%lx != 0x10000\n",
107 todo_wine ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
108 /* NT reports Protect == 0 for a not committed memory block */
109 todo_wine ok(info.Protect == 0 /* NT */ ||
110 info.Protect == PAGE_NOACCESS, /* Win9x */
111 "%x != PAGE_NOACCESS\n", info.Protect);
112 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
114 SetLastError(0xdeadbeef);
115 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
116 "VirtualProtectEx should fail on a not committed memory\n");
117 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
118 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
119 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
121 addr2 = VirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
122 ok(addr1 == addr2, "VirtualAllocEx failed\n");
124 /* test a committed memory */
125 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
127 "VirtualQueryEx failed\n");
128 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
130 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
131 info.AllocationBase, addr1);
132 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
133 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
134 todo_wine ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
135 todo_wine ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
136 /* this time NT reports PAGE_NOACCESS as well */
137 todo_wine ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
138 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
140 /* this should fail, since not the whole range is committed yet */
141 SetLastError(0xdeadbeef);
142 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
143 "VirtualProtectEx should fail on a not committed memory\n");
144 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
145 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
146 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
148 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY,
149 &old_prot), "VirtualProtectEx failed\n");
150 todo_wine ok(old_prot == PAGE_NOACCESS,
151 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
153 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE,
154 &old_prot), "VirtualProtectEx failed\n");
155 todo_wine ok(old_prot == PAGE_READONLY,
156 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
158 ok(!VirtualFreeEx(hProcess, addr1, 0x10000, 0),
159 "VirtualFreeEx should fail with type 0\n");
160 todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER,
161 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
163 todo_wine ok(VirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT),
164 "VirtualFreeEx failed\n");
166 /* if the type is MEM_RELEASE, size must be 0 */
167 ok(!VirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
168 "VirtualFreeEx should fail\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, 0, MEM_RELEASE),
173 "VirtualFreeEx failed\n");
175 TerminateProcess(hProcess, 0);
176 CloseHandle(hProcess);
179 static void test_VirtualAlloc(void)
183 MEMORY_BASIC_INFORMATION info;
185 SetLastError(0xdeadbeef);
186 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
187 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
188 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
189 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
190 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
192 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
193 ok(addr1 != NULL, "VirtualAlloc failed\n");
195 /* test a not committed memory */
196 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
197 "VirtualQuery failed\n");
198 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
199 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
200 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
201 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
202 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
203 /* NT reports Protect == 0 for a not committed memory block */
204 ok(info.Protect == 0 /* NT */ ||
205 info.Protect == PAGE_NOACCESS, /* Win9x */
206 "%x != PAGE_NOACCESS\n", info.Protect);
207 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
209 SetLastError(0xdeadbeef);
210 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
211 "VirtualProtect should fail on a not committed memory\n");
212 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
213 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
214 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
216 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
217 ok(addr1 == addr2, "VirtualAlloc failed\n");
219 /* test a committed memory */
220 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
221 "VirtualQuery failed\n");
222 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
223 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
224 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
225 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
226 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
227 /* this time NT reports PAGE_NOACCESS as well */
228 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
229 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
231 /* this should fail, since not the whole range is committed yet */
232 SetLastError(0xdeadbeef);
233 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
234 "VirtualProtect should fail on a not committed memory\n");
235 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
236 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
237 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
239 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
240 ok(old_prot == PAGE_NOACCESS,
241 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
243 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
244 ok(old_prot == PAGE_READONLY,
245 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
247 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
248 ok(GetLastError() == ERROR_INVALID_PARAMETER,
249 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
251 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
253 /* if the type is MEM_RELEASE, size must be 0 */
254 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
255 ok(GetLastError() == ERROR_INVALID_PARAMETER,
256 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
258 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
261 static void test_MapViewOfFile(void)
263 static const char testfile[] = "testfile.xxx";
264 HANDLE file, mapping;
267 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
268 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
269 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
270 SetEndOfFile( file );
272 /* read/write mapping */
274 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
275 ok( mapping != 0, "CreateFileMapping failed\n" );
277 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
278 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ failed\n" );
279 UnmapViewOfFile( ptr );
281 /* this fails on win9x but succeeds on NT */
282 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
283 if (ptr) UnmapViewOfFile( ptr );
284 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
286 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
287 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
288 UnmapViewOfFile( ptr );
290 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
291 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE failed\n" );
292 UnmapViewOfFile( ptr );
293 CloseHandle( mapping );
295 /* read-only mapping */
297 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
298 ok( mapping != 0, "CreateFileMapping failed\n" );
300 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
301 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
302 UnmapViewOfFile( ptr );
304 /* this fails on win9x but succeeds on NT */
305 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
306 if (ptr) UnmapViewOfFile( ptr );
307 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
309 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
310 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
311 UnmapViewOfFile( ptr );
313 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
314 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
315 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
316 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
317 CloseHandle( mapping );
319 /* copy-on-write mapping */
321 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
322 ok( mapping != 0, "CreateFileMapping failed\n" );
324 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
325 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
326 UnmapViewOfFile( ptr );
328 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
329 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY failed\n" );
330 UnmapViewOfFile( ptr );
332 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
333 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
334 UnmapViewOfFile( ptr );
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 /* no access mapping */
344 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
345 /* fails on NT but succeeds on win9x */
346 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
349 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
350 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
351 UnmapViewOfFile( ptr );
353 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
354 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
355 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
357 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
358 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
359 UnmapViewOfFile( ptr );
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, "Wrong error %d\n", GetLastError() );
365 CloseHandle( mapping );
370 /* now try read-only file */
372 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
373 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
375 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
376 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
377 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
378 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
380 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
381 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
382 CloseHandle( mapping );
384 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
385 ok( mapping != 0, "CreateFileMapping PAGE_READONLY failed\n" );
386 CloseHandle( mapping );
389 /* now try no access file */
391 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
392 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
394 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
395 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
396 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
397 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
399 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
400 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
401 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
402 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
404 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
405 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
406 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
407 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
412 DeleteFileA( testfile );
419 argc = winetest_get_mainargs( &argv );
423 if (!strcmp(argv[2], "sleep"))
425 Sleep(5000); /* spawned process runs for at most 5 seconds */
432 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
433 PAGE_EXECUTE_READWRITE);
434 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
435 if (mem == NULL) break;
436 ret = VirtualFree(mem, 0, MEM_RELEASE);
437 ok(ret, "VirtualFree failed %u\n", GetLastError());
443 test_VirtualAllocEx();
445 test_MapViewOfFile();