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");
70 src = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
71 dst = (char *) HeapAlloc( GetProcessHeap(), 0, alloc_size );
72 for (i = 0; i < alloc_size; i++)
73 src[i] = 0xcafedead + i;
75 todo_wine ok(addr1 != NULL, "VirtualAllocEx error %u\n", GetLastError());
76 b = WriteProcessMemory(hProcess, addr1, src, alloc_size, &bytes_written);
77 ok(b && (bytes_written == alloc_size), "%lu bytes written\n",
79 b = ReadProcessMemory(hProcess, addr1, dst, alloc_size, &bytes_read);
80 ok(b && (bytes_read == alloc_size), "%lu bytes read\n", bytes_read);
81 ok(!memcmp(src, dst, alloc_size), "Data from remote process differs\n");
82 b = VirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
83 todo_wine ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
85 HeapFree( GetProcessHeap(), 0, src );
86 HeapFree( GetProcessHeap(), 0, dst );
89 * The following tests parallel those in test_VirtualAlloc()
92 SetLastError(0xdeadbeef);
93 addr1 = VirtualAllocEx(hProcess, 0, 0, MEM_RESERVE, PAGE_NOACCESS);
94 ok(addr1 == NULL, "VirtualAllocEx should fail on zero-sized allocation\n");
95 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
96 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
97 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
99 addr1 = VirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
100 todo_wine ok(addr1 != NULL, "VirtualAllocEx failed\n");
102 /* test a not committed memory */
103 memset(&info, 'q', sizeof(info));
104 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
105 == sizeof(info), "VirtualQueryEx failed\n");
106 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
108 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
109 info.AllocationBase, addr1);
110 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
111 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
112 todo_wine ok(info.RegionSize == 0x10000, "%lx != 0x10000\n",
114 todo_wine ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
115 /* NT reports Protect == 0 for a not committed memory block */
116 todo_wine ok(info.Protect == 0 /* NT */ ||
117 info.Protect == PAGE_NOACCESS, /* Win9x */
118 "%x != PAGE_NOACCESS\n", info.Protect);
119 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
121 SetLastError(0xdeadbeef);
122 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
123 "VirtualProtectEx should fail on a not committed memory\n");
124 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
125 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
126 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
128 addr2 = VirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
129 ok(addr1 == addr2, "VirtualAllocEx failed\n");
131 /* test a committed memory */
132 todo_wine ok(VirtualQueryEx(hProcess, addr1, &info, sizeof(info))
134 "VirtualQueryEx failed\n");
135 todo_wine ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress,
137 todo_wine ok(info.AllocationBase == addr1, "%p != %p\n",
138 info.AllocationBase, addr1);
139 todo_wine ok(info.AllocationProtect == PAGE_NOACCESS,
140 "%x != PAGE_NOACCESS\n", info.AllocationProtect);
141 todo_wine ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
142 todo_wine ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
143 /* this time NT reports PAGE_NOACCESS as well */
144 todo_wine ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
145 todo_wine ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
147 /* this should fail, since not the whole range is committed yet */
148 SetLastError(0xdeadbeef);
149 ok(!VirtualProtectEx(hProcess, addr1, 0xFFFC, PAGE_READONLY, &old_prot),
150 "VirtualProtectEx should fail on a not committed memory\n");
151 todo_wine ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
152 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
153 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
155 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READONLY,
156 &old_prot), "VirtualProtectEx failed\n");
157 todo_wine ok(old_prot == PAGE_NOACCESS,
158 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
160 todo_wine ok(VirtualProtectEx(hProcess, addr1, 0x1000, PAGE_READWRITE,
161 &old_prot), "VirtualProtectEx failed\n");
162 todo_wine ok(old_prot == PAGE_READONLY,
163 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
165 ok(!VirtualFreeEx(hProcess, addr1, 0x10000, 0),
166 "VirtualFreeEx should fail with type 0\n");
167 todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER,
168 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
170 todo_wine ok(VirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT),
171 "VirtualFreeEx failed\n");
173 /* if the type is MEM_RELEASE, size must be 0 */
174 ok(!VirtualFreeEx(hProcess, addr1, 1, MEM_RELEASE),
175 "VirtualFreeEx should fail\n");
176 todo_wine ok(GetLastError() == ERROR_INVALID_PARAMETER,
177 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
179 todo_wine ok(VirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE),
180 "VirtualFreeEx failed\n");
182 TerminateProcess(hProcess, 0);
183 CloseHandle(hProcess);
186 static void test_VirtualAlloc(void)
190 MEMORY_BASIC_INFORMATION info;
192 SetLastError(0xdeadbeef);
193 addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
194 ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
195 ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
196 GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
197 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
199 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
200 ok(addr1 != NULL, "VirtualAlloc failed\n");
202 /* test a not committed memory */
203 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
204 "VirtualQuery failed\n");
205 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
206 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
207 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
208 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
209 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
210 /* NT reports Protect == 0 for a not committed memory block */
211 ok(info.Protect == 0 /* NT */ ||
212 info.Protect == PAGE_NOACCESS, /* Win9x */
213 "%x != PAGE_NOACCESS\n", info.Protect);
214 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
216 SetLastError(0xdeadbeef);
217 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
218 "VirtualProtect should fail on a not committed memory\n");
219 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
220 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
221 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
223 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
224 ok(addr1 == addr2, "VirtualAlloc failed\n");
226 /* test a committed memory */
227 ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
228 "VirtualQuery failed\n");
229 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
230 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
231 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
232 ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
233 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
234 /* this time NT reports PAGE_NOACCESS as well */
235 ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
236 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
238 /* this should fail, since not the whole range is committed yet */
239 SetLastError(0xdeadbeef);
240 ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
241 "VirtualProtect should fail on a not committed memory\n");
242 ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
243 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
244 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
246 ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
247 ok(old_prot == PAGE_NOACCESS,
248 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
250 ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
251 ok(old_prot == PAGE_READONLY,
252 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
254 ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
255 ok(GetLastError() == ERROR_INVALID_PARAMETER,
256 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
258 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
260 /* if the type is MEM_RELEASE, size must be 0 */
261 ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
262 ok(GetLastError() == ERROR_INVALID_PARAMETER,
263 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
265 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
268 static void test_MapViewOfFile(void)
270 static const char testfile[] = "testfile.xxx";
271 HANDLE file, mapping;
274 file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
275 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
276 SetFilePointer( file, 4096, NULL, FILE_BEGIN );
277 SetEndOfFile( file );
279 /* read/write mapping */
281 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
282 ok( mapping != 0, "CreateFileMapping failed\n" );
284 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
285 ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ failed\n" );
286 UnmapViewOfFile( ptr );
288 /* this fails on win9x but succeeds on NT */
289 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
290 if (ptr) UnmapViewOfFile( ptr );
291 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
293 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
294 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
295 UnmapViewOfFile( ptr );
297 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
298 ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE failed\n" );
299 UnmapViewOfFile( ptr );
300 CloseHandle( mapping );
302 /* read-only mapping */
304 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
305 ok( mapping != 0, "CreateFileMapping failed\n" );
307 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
308 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
309 UnmapViewOfFile( ptr );
311 /* this fails on win9x but succeeds on NT */
312 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
313 if (ptr) UnmapViewOfFile( ptr );
314 else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
316 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
317 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
318 UnmapViewOfFile( ptr );
320 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
321 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
322 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
323 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
324 CloseHandle( mapping );
326 /* copy-on-write mapping */
328 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
329 ok( mapping != 0, "CreateFileMapping failed\n" );
331 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
332 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
333 UnmapViewOfFile( ptr );
335 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
336 ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY failed\n" );
337 UnmapViewOfFile( ptr );
339 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
340 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
341 UnmapViewOfFile( ptr );
343 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
344 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
345 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
346 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
347 CloseHandle( mapping );
349 /* no access mapping */
351 mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
352 /* fails on NT but succeeds on win9x */
353 if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
356 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
357 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
358 UnmapViewOfFile( ptr );
360 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
361 ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
362 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
364 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
365 ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
366 UnmapViewOfFile( ptr );
368 ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
369 ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
370 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
372 CloseHandle( mapping );
377 /* now try read-only file */
379 file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
380 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
382 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
383 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
384 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
385 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
387 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
388 ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
389 CloseHandle( mapping );
391 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
392 ok( mapping != 0, "CreateFileMapping PAGE_READONLY failed\n" );
393 CloseHandle( mapping );
396 /* now try no access file */
398 file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
399 ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
401 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
402 ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
403 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
404 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
406 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
407 ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
408 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
409 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
411 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
412 ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
413 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
414 GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
419 DeleteFileA( testfile );
426 argc = winetest_get_mainargs( &argv );
430 if (!strcmp(argv[2], "sleep"))
432 Sleep(5000); /* spawned process runs for at most 5 seconds */
439 mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
440 PAGE_EXECUTE_READWRITE);
441 ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
442 if (mem == NULL) break;
443 ret = VirtualFree(mem, 0, MEM_RELEASE);
444 ok(ret, "VirtualFree failed %u\n", GetLastError());
450 test_VirtualAllocEx();
452 test_MapViewOfFile();