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 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
)
40 char cmdline
[MAX_PATH
];
41 PROCESS_INFORMATION pi
;
42 STARTUPINFO si
= { 0 };
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());
53 static void test_VirtualAllocEx(void)
55 const unsigned int alloc_size
= 1<<15;
57 unsigned long bytes_written
= 0, bytes_read
= 0, i
;
61 MEMORY_BASIC_INFORMATION info
;
64 /* not exported in all windows-versions */
65 if ((!pVirtualAllocEx
) || (!pVirtualFreeEx
)) {
66 skip("VirtualAllocEx not found\n");
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
)
78 skip("VirtualAllocEx not implemented\n");
79 TerminateProcess(hProcess
, 0);
80 CloseHandle(hProcess
);
84 src
= HeapAlloc( GetProcessHeap(), 0, alloc_size
);
85 dst
= HeapAlloc( GetProcessHeap(), 0, alloc_size
);
86 for (i
= 0; i
< alloc_size
; i
++)
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",
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());
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
);
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)
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
;
275 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
276 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
277 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
278 SetEndOfFile( file
);
280 /* read/write mapping */
282 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
283 ok( mapping
!= 0, "CreateFileMapping failed\n" );
285 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
286 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ failed\n" );
287 UnmapViewOfFile( ptr
);
289 /* this fails on win9x but succeeds on NT */
290 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
291 if (ptr
) UnmapViewOfFile( ptr
);
292 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
294 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
295 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
296 UnmapViewOfFile( ptr
);
298 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
299 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE failed\n" );
300 UnmapViewOfFile( ptr
);
301 CloseHandle( mapping
);
303 /* read-only mapping */
305 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
306 ok( mapping
!= 0, "CreateFileMapping failed\n" );
308 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
309 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
310 UnmapViewOfFile( ptr
);
312 /* this fails on win9x but succeeds on NT */
313 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
314 if (ptr
) UnmapViewOfFile( ptr
);
315 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
317 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
318 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
319 UnmapViewOfFile( ptr
);
321 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
322 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
323 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
324 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
325 CloseHandle( mapping
);
327 /* copy-on-write mapping */
329 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
330 ok( mapping
!= 0, "CreateFileMapping failed\n" );
332 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
333 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
334 UnmapViewOfFile( ptr
);
336 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
337 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY failed\n" );
338 UnmapViewOfFile( ptr
);
340 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
341 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
342 UnmapViewOfFile( ptr
);
344 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
345 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
346 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
347 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
348 CloseHandle( mapping
);
350 /* no access mapping */
352 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
353 /* fails on NT but succeeds on win9x */
354 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
357 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
358 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ failed\n" );
359 UnmapViewOfFile( ptr
);
361 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
362 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
363 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
365 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
366 ok( ptr
!= NULL
, "MapViewOfFile 0 failed\n" );
367 UnmapViewOfFile( ptr
);
369 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
370 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
371 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
373 CloseHandle( mapping
);
378 /* now try read-only file */
380 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
381 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
383 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
384 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
385 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
386 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
388 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
389 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
390 CloseHandle( mapping
);
392 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
393 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY failed\n" );
394 CloseHandle( mapping
);
397 /* now try no access file */
399 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
400 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
402 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
403 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
404 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
405 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
407 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
408 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
409 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
410 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
412 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
413 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
414 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
415 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
418 DeleteFileA( testfile
);
420 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, "Global\\Foo");
421 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE failed\n" );
423 mapping
= OpenFileMapping( FILE_MAP_READ
, FALSE
, "Global\\Foo" );
424 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ failed\n" );
425 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
426 todo_wine
ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE should fail\n" );
427 CloseHandle( mapping
);
432 static DWORD (WINAPI
*pNtMapViewOfSection
)( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
,
433 ULONG zero_bits
, SIZE_T commit_size
,
434 const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
435 ULONG inherit
, ULONG alloc_type
, ULONG protect
);
436 static DWORD (WINAPI
*pNtUnmapViewOfSection
)( HANDLE process
, PVOID addr
);
438 static void test_NtMapViewOfSection(void)
442 static const char testfile
[] = "testfile.xxx";
443 static const char data
[] = "test data for NtMapViewOfSection";
444 char buffer
[sizeof(data
)];
445 HANDLE file
, mapping
;
448 DWORD status
, written
;
450 LARGE_INTEGER offset
;
452 pNtMapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
453 pNtUnmapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
454 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
456 skip( "NtMapViewOfSection not found\n" );
460 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
461 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
462 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
463 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
464 SetEndOfFile( file
);
466 /* read/write mapping */
468 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
469 ok( mapping
!= 0, "CreateFileMapping failed\n" );
471 hProcess
= create_target_process("sleep");
472 ok(hProcess
!= NULL
, "Can't start process\n");
477 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
478 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
480 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
481 ok( ret
, "ReadProcessMemory failed\n" );
482 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
483 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
485 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
486 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
488 CloseHandle( mapping
);
490 DeleteFileA( testfile
);
497 argc
= winetest_get_mainargs( &argv
);
501 if (!strcmp(argv
[2], "sleep"))
503 Sleep(5000); /* spawned process runs for at most 5 seconds */
510 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
511 PAGE_EXECUTE_READWRITE
);
512 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
513 if (mem
== NULL
) break;
514 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
515 ok(ret
, "VirtualFree failed %u\n", GetLastError());
521 hkernel32
= GetModuleHandleA("kernel32.dll");
522 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
523 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
525 test_VirtualAllocEx();
527 test_MapViewOfFile();
528 test_NtMapViewOfSection();