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";
274 HANDLE file
, mapping
;
276 MEMORY_BASIC_INFORMATION info
;
279 SetLastError(0xdeadbeef);
280 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
281 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
282 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
283 SetEndOfFile( file
);
285 /* read/write mapping */
287 SetLastError(0xdeadbeef);
288 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
289 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
291 SetLastError(0xdeadbeef);
292 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
293 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
294 UnmapViewOfFile( ptr
);
296 /* this fails on win9x but succeeds on NT */
297 SetLastError(0xdeadbeef);
298 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
299 if (ptr
) UnmapViewOfFile( ptr
);
300 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
302 SetLastError(0xdeadbeef);
303 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
304 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
305 UnmapViewOfFile( ptr
);
307 SetLastError(0xdeadbeef);
308 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
309 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
310 UnmapViewOfFile( ptr
);
311 CloseHandle( mapping
);
313 /* read-only mapping */
315 SetLastError(0xdeadbeef);
316 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
317 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
319 SetLastError(0xdeadbeef);
320 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
321 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
322 UnmapViewOfFile( ptr
);
324 /* this fails on win9x but succeeds on NT */
325 SetLastError(0xdeadbeef);
326 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
327 if (ptr
) UnmapViewOfFile( ptr
);
328 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
330 SetLastError(0xdeadbeef);
331 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
332 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
333 UnmapViewOfFile( ptr
);
335 SetLastError(0xdeadbeef);
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 /* copy-on-write mapping */
344 SetLastError(0xdeadbeef);
345 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
346 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
348 SetLastError(0xdeadbeef);
349 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
350 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
351 UnmapViewOfFile( ptr
);
353 SetLastError(0xdeadbeef);
354 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
355 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
356 UnmapViewOfFile( ptr
);
358 SetLastError(0xdeadbeef);
359 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
360 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
361 UnmapViewOfFile( ptr
);
363 SetLastError(0xdeadbeef);
364 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
365 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
366 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
367 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
368 CloseHandle( mapping
);
370 /* no access mapping */
372 SetLastError(0xdeadbeef);
373 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
374 /* fails on NT but succeeds on win9x */
375 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
378 SetLastError(0xdeadbeef);
379 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
380 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
381 UnmapViewOfFile( ptr
);
383 SetLastError(0xdeadbeef);
384 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
385 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
386 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
388 SetLastError(0xdeadbeef);
389 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
390 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
391 UnmapViewOfFile( ptr
);
393 SetLastError(0xdeadbeef);
394 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
395 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
396 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
398 CloseHandle( mapping
);
403 /* now try read-only file */
405 SetLastError(0xdeadbeef);
406 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
407 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
409 SetLastError(0xdeadbeef);
410 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
411 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
412 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
413 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
415 SetLastError(0xdeadbeef);
416 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
417 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
418 CloseHandle( mapping
);
420 SetLastError(0xdeadbeef);
421 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
422 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
423 CloseHandle( mapping
);
426 /* now try no access file */
428 SetLastError(0xdeadbeef);
429 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
430 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
432 SetLastError(0xdeadbeef);
433 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
434 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
435 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
436 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
438 SetLastError(0xdeadbeef);
439 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
440 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
441 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
442 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
444 SetLastError(0xdeadbeef);
445 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
446 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
447 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
448 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
451 DeleteFileA( testfile
);
453 SetLastError(0xdeadbeef);
455 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
456 /* nt4 doesn't have Local\\ */
457 if (!file
&& GetLastError() == ERROR_PATH_NOT_FOUND
)
460 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
462 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
464 SetLastError(0xdeadbeef);
465 mapping
= OpenFileMapping( FILE_MAP_READ
, FALSE
, name
);
466 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
467 SetLastError(0xdeadbeef);
468 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
471 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
472 SetLastError(0xdeadbeef);
473 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
474 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
475 SetLastError(0xdeadbeef);
476 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
477 "VirtualQuery error %u\n", GetLastError() );
478 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
479 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
480 ok( info
.AllocationProtect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.AllocationProtect
);
481 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
482 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
483 ok( info
.Protect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.Protect
);
485 else todo_wine
win_skip( "no access checks on win9x\n" );
486 UnmapViewOfFile( ptr
);
487 CloseHandle( mapping
);
489 SetLastError(0xdeadbeef);
490 mapping
= OpenFileMapping( FILE_MAP_WRITE
, FALSE
, name
);
491 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
492 SetLastError(0xdeadbeef);
493 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
496 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
497 SetLastError(0xdeadbeef);
498 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
499 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
500 SetLastError(0xdeadbeef);
501 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
502 "VirtualQuery error %u\n", GetLastError() );
503 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
504 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
505 ok( info
.AllocationProtect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.AllocationProtect
);
506 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
507 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
508 ok( info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
510 else todo_wine
win_skip( "no access checks on win9x\n" );
511 UnmapViewOfFile( ptr
);
512 CloseHandle( mapping
);
516 /* read/write mapping with SEC_RESERVE */
517 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_RESERVE
, 0, MAPPING_SIZE
, NULL
);
518 ok(mapping
!= INVALID_HANDLE_VALUE
, "CreateFileMappingA failed with error %d\n", GetLastError());
520 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
521 ok(ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
523 ptr2
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
524 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
525 ok(ptr2
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
526 trace("mapping same section resulted in views %p and %p\n", ptr
, ptr2
);
528 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
529 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
530 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
531 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
532 ok(info
.AllocationProtect
== PAGE_READWRITE
, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
533 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
535 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
537 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
538 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
540 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
541 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
542 ok(info
.BaseAddress
== ptr2
, "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
543 ok(info
.AllocationBase
== ptr2
, "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
544 ok(info
.AllocationProtect
== PAGE_READWRITE
, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
545 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
547 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
549 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
550 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
552 ptr
= VirtualAlloc(ptr
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
553 ok(ptr
!= NULL
, "VirtualAlloc failed with error %d\n", GetLastError());
555 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
556 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
557 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
558 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
559 ok(info
.AllocationProtect
== PAGE_READWRITE
, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
560 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
561 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
562 ok(info
.Protect
== PAGE_READONLY
, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
563 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
565 /* shows that the VirtualAlloc above affects the mapping, not just the
566 * virtual memory in this process - it also affects all other processes
567 * with a view of the mapping, but that isn't tested here */
568 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
569 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
570 ok(info
.BaseAddress
== ptr2
, "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
571 ok(info
.AllocationBase
== ptr2
, "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
572 ok(info
.AllocationProtect
== PAGE_READWRITE
, "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
574 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
575 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
576 ok(info
.Protect
== PAGE_READWRITE
, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
577 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
579 ret
= VirtualFree( ptr
, 0x10000, MEM_DECOMMIT
);
580 ok( !ret
, "VirtualFree succeeded\n" );
581 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "VirtualFree failed with %u\n", GetLastError() );
583 ret
= UnmapViewOfFile(ptr2
);
584 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
585 ret
= UnmapViewOfFile(ptr
);
586 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
587 CloseHandle(mapping
);
590 static DWORD (WINAPI
*pNtMapViewOfSection
)( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
,
591 ULONG zero_bits
, SIZE_T commit_size
,
592 const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
593 ULONG inherit
, ULONG alloc_type
, ULONG protect
);
594 static DWORD (WINAPI
*pNtUnmapViewOfSection
)( HANDLE process
, PVOID addr
);
596 static void test_NtMapViewOfSection(void)
600 static const char testfile
[] = "testfile.xxx";
601 static const char data
[] = "test data for NtMapViewOfSection";
602 char buffer
[sizeof(data
)];
603 HANDLE file
, mapping
;
606 DWORD status
, written
;
608 LARGE_INTEGER offset
;
610 pNtMapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
611 pNtUnmapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
612 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
614 skip( "NtMapViewOfSection not found\n" );
618 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
619 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
620 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
621 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
622 SetEndOfFile( file
);
624 /* read/write mapping */
626 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
627 ok( mapping
!= 0, "CreateFileMapping failed\n" );
629 hProcess
= create_target_process("sleep");
630 ok(hProcess
!= NULL
, "Can't start process\n");
635 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
636 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
638 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
639 ok( ret
, "ReadProcessMemory failed\n" );
640 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
641 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
643 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
644 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
646 CloseHandle( mapping
);
648 DeleteFileA( testfile
);
650 TerminateProcess(hProcess
, 0);
651 CloseHandle(hProcess
);
654 static void test_CreateFileMapping(void)
656 HANDLE handle
, handle2
;
658 /* test case sensitivity */
660 SetLastError(0xdeadbeef);
661 handle
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
662 "Wine Test Mapping");
663 ok( handle
!= NULL
, "CreateFileMapping failed with error %u\n", GetLastError());
664 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
666 SetLastError(0xdeadbeef);
667 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
668 "Wine Test Mapping");
669 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
670 ok( GetLastError() == ERROR_ALREADY_EXISTS
, "wrong error %u\n", GetLastError());
671 CloseHandle( handle2
);
673 SetLastError(0xdeadbeef);
674 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
675 "WINE TEST MAPPING");
676 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
677 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
678 CloseHandle( handle2
);
680 SetLastError(0xdeadbeef);
681 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "Wine Test Mapping");
682 ok( handle2
!= NULL
, "OpenFileMapping failed with error %d\n", GetLastError());
683 CloseHandle( handle2
);
685 SetLastError(0xdeadbeef);
686 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "WINE TEST MAPPING");
687 ok( !handle2
, "OpenFileMapping succeeded\n");
688 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
690 CloseHandle( handle
);
693 static void test_BadPtr(void)
695 void *ptr
= (void*)1;
696 /* We assume address 1 is not mapped. */
697 ok(IsBadReadPtr(ptr
,1),"IsBadReadPtr(1) failed.\n");
698 ok(IsBadWritePtr(ptr
,1),"IsBadWritePtr(1) failed.\n");
699 ok(IsBadCodePtr(ptr
),"IsBadCodePtr(1) failed.\n");
706 argc
= winetest_get_mainargs( &argv
);
710 if (!strcmp(argv
[2], "sleep"))
712 Sleep(5000); /* spawned process runs for at most 5 seconds */
719 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
720 PAGE_EXECUTE_READWRITE
);
721 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
722 if (mem
== NULL
) break;
723 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
724 ok(ret
, "VirtualFree failed %u\n", GetLastError());
730 hkernel32
= GetModuleHandleA("kernel32.dll");
731 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
732 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
734 test_VirtualAllocEx();
736 test_MapViewOfFile();
737 test_NtMapViewOfSection();
738 test_CreateFileMapping();