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
25 #define WIN32_NO_STATUS
30 #include "wine/test.h"
33 #define MAPPING_SIZE 0x100000
35 static HINSTANCE hkernel32
;
36 static LPVOID (WINAPI
*pVirtualAllocEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
, DWORD
);
37 static BOOL (WINAPI
*pVirtualFreeEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
);
38 static UINT (WINAPI
*pGetWriteWatch
)(DWORD
,LPVOID
,SIZE_T
,LPVOID
*,ULONG_PTR
*,ULONG
*);
39 static UINT (WINAPI
*pResetWriteWatch
)(LPVOID
,SIZE_T
);
40 static NTSTATUS (WINAPI
*pNtAreMappedFilesTheSame
)(PVOID
,PVOID
);
41 static NTSTATUS (WINAPI
*pNtMapViewOfSection
)(HANDLE
, HANDLE
, PVOID
*, ULONG
, SIZE_T
, const LARGE_INTEGER
*, SIZE_T
*, ULONG
, ULONG
, ULONG
);
42 static DWORD (WINAPI
*pNtUnmapViewOfSection
)(HANDLE
, PVOID
);
44 /* ############################### */
46 static HANDLE
create_target_process(const char *arg
)
49 char cmdline
[MAX_PATH
];
50 PROCESS_INFORMATION pi
;
52 STARTUPINFOA si
= { 0 };
55 winetest_get_mainargs( &argv
);
56 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
57 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
58 ok(ret
, "error: %u\n", GetLastError());
59 ret
= CloseHandle(pi
.hThread
);
60 ok(ret
, "error %u\n", GetLastError());
64 static void test_VirtualAllocEx(void)
66 const unsigned int alloc_size
= 1<<15;
68 SIZE_T bytes_written
= 0, bytes_read
= 0, i
;
72 MEMORY_BASIC_INFORMATION info
;
75 /* not exported in all windows-versions */
76 if ((!pVirtualAllocEx
) || (!pVirtualFreeEx
)) {
77 win_skip("Virtual{Alloc,Free}Ex not available\n");
81 hProcess
= create_target_process("sleep");
82 ok(hProcess
!= NULL
, "Can't start process\n");
84 SetLastError(0xdeadbeef);
85 addr1
= pVirtualAllocEx(hProcess
, NULL
, alloc_size
, MEM_COMMIT
,
86 PAGE_EXECUTE_READWRITE
);
87 if (!addr1
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
89 win_skip("VirtualAllocEx not implemented\n");
90 TerminateProcess(hProcess
, 0);
91 CloseHandle(hProcess
);
95 src
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
96 dst
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
97 for (i
= 0; i
< alloc_size
; i
++)
100 ok(addr1
!= NULL
, "VirtualAllocEx error %u\n", GetLastError());
101 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
102 ok(b
&& (bytes_written
== alloc_size
), "%lu bytes written\n",
104 b
= ReadProcessMemory(hProcess
, addr1
, dst
, alloc_size
, &bytes_read
);
105 ok(b
&& (bytes_read
== alloc_size
), "%lu bytes read\n", bytes_read
);
106 ok(!memcmp(src
, dst
, alloc_size
), "Data from remote process differs\n");
108 /* test invalid source buffers */
110 b
= VirtualProtect( src
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
111 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
112 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
113 ok( !b
, "WriteProcessMemory succeeded\n" );
114 ok( GetLastError() == ERROR_NOACCESS
||
115 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
116 "wrong error %u\n", GetLastError() );
117 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
118 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
119 ok( !b
, "ReadProcessMemory succeeded\n" );
120 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
121 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
123 b
= VirtualProtect( src
, 0x2000, PAGE_NOACCESS
, &old_prot
);
124 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
125 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
126 ok( !b
, "WriteProcessMemory succeeded\n" );
127 ok( GetLastError() == ERROR_NOACCESS
||
128 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
129 "wrong error %u\n", GetLastError() );
130 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
131 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
132 ok( !b
, "ReadProcessMemory succeeded\n" );
133 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
134 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
136 b
= pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
);
137 ok(b
!= 0, "VirtualFreeEx, error %u\n", GetLastError());
139 VirtualFree( src
, 0, MEM_FREE
);
140 VirtualFree( dst
, 0, MEM_FREE
);
143 * The following tests parallel those in test_VirtualAlloc()
146 SetLastError(0xdeadbeef);
147 addr1
= pVirtualAllocEx(hProcess
, 0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
148 ok(addr1
== NULL
, "VirtualAllocEx should fail on zero-sized allocation\n");
149 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
150 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
151 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
153 addr1
= pVirtualAllocEx(hProcess
, 0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
154 ok(addr1
!= NULL
, "VirtualAllocEx failed\n");
156 /* test a not committed memory */
157 memset(&info
, 'q', sizeof(info
));
158 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
), "VirtualQueryEx failed\n");
159 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
160 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
161 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
162 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
163 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
164 /* NT reports Protect == 0 for a not committed memory block */
165 ok(info
.Protect
== 0 /* NT */ ||
166 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
167 "%x != PAGE_NOACCESS\n", info
.Protect
);
168 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
170 SetLastError(0xdeadbeef);
171 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
172 "VirtualProtectEx should fail on a not committed memory\n");
173 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
174 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
175 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
177 addr2
= pVirtualAllocEx(hProcess
, addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
178 ok(addr1
== addr2
, "VirtualAllocEx failed\n");
180 /* test a committed memory */
181 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
),
182 "VirtualQueryEx failed\n");
183 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
184 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
185 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
186 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
187 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
188 /* this time NT reports PAGE_NOACCESS as well */
189 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
190 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
192 /* this should fail, since not the whole range is committed yet */
193 SetLastError(0xdeadbeef);
194 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
195 "VirtualProtectEx should fail on a not committed memory\n");
196 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
197 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
198 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
201 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtectEx failed\n");
202 ok(old_prot
== PAGE_NOACCESS
, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
205 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtectEx failed\n");
206 ok(old_prot
== PAGE_READONLY
, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
208 ok(!pVirtualFreeEx(hProcess
, addr1
, 0x10000, 0),
209 "VirtualFreeEx should fail with type 0\n");
210 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
211 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
213 ok(pVirtualFreeEx(hProcess
, addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFreeEx failed\n");
215 /* if the type is MEM_RELEASE, size must be 0 */
216 ok(!pVirtualFreeEx(hProcess
, addr1
, 1, MEM_RELEASE
),
217 "VirtualFreeEx should fail\n");
218 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
219 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
221 ok(pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
), "VirtualFreeEx failed\n");
223 TerminateProcess(hProcess
, 0);
224 CloseHandle(hProcess
);
227 static void test_VirtualAlloc(void)
231 MEMORY_BASIC_INFORMATION info
;
233 SetLastError(0xdeadbeef);
234 addr1
= VirtualAlloc(0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
235 ok(addr1
== NULL
, "VirtualAlloc should fail on zero-sized allocation\n");
236 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
237 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
238 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
240 addr1
= VirtualAlloc(0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
241 ok(addr1
!= NULL
, "VirtualAlloc failed\n");
243 /* test a not committed memory */
244 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
245 "VirtualQuery failed\n");
246 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
247 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
248 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
249 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
250 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
251 /* NT reports Protect == 0 for a not committed memory block */
252 ok(info
.Protect
== 0 /* NT */ ||
253 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
254 "%x != PAGE_NOACCESS\n", info
.Protect
);
255 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
257 SetLastError(0xdeadbeef);
258 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
259 "VirtualProtect should fail on a not committed memory\n");
260 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
261 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
262 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
264 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
265 ok(addr1
== addr2
, "VirtualAlloc failed\n");
267 /* test a committed memory */
268 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
269 "VirtualQuery failed\n");
270 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
271 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
272 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
273 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
274 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
275 /* this time NT reports PAGE_NOACCESS as well */
276 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
277 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
279 /* this should fail, since not the whole range is committed yet */
280 SetLastError(0xdeadbeef);
281 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
282 "VirtualProtect should fail on a not committed memory\n");
283 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
284 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
285 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
287 ok(VirtualProtect(addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtect failed\n");
288 ok(old_prot
== PAGE_NOACCESS
,
289 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
291 ok(VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtect failed\n");
292 ok(old_prot
== PAGE_READONLY
,
293 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
295 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
296 "VirtualQuery failed\n");
297 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
298 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
299 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
300 memset( addr1
, 0x55, 20 );
301 ok( *(DWORD
*)addr1
== 0x55555555, "wrong data %x\n", *(DWORD
*)addr1
);
303 addr2
= VirtualAlloc( addr1
, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
304 ok( addr2
== addr1
|| broken( !addr2
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
305 "VirtualAlloc failed err %u\n", GetLastError() );
306 ok( *(DWORD
*)addr1
== 0x55555555 || *(DWORD
*)addr1
== 0, "wrong data %x\n", *(DWORD
*)addr1
);
309 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
310 "VirtualQuery failed\n");
311 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
312 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
313 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
315 addr2
= VirtualAlloc( (char *)addr1
+ 0x1000, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
316 ok( (char *)addr2
== (char *)addr1
+ 0x1000, "VirtualAlloc failed\n" );
318 ok(VirtualQuery(addr2
, &info
, sizeof(info
)) == sizeof(info
),
319 "VirtualQuery failed\n");
320 ok(info
.RegionSize
== 0xf000, "%lx != 0xf000\n", info
.RegionSize
);
321 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
322 ok(info
.Protect
== 0, "%x != 0\n", info
.Protect
);
324 addr2
= VirtualAlloc( (char *)addr1
+ 0xf000, 0x2000, MEM_RESET
, PAGE_NOACCESS
);
325 ok( !addr2
, "VirtualAlloc failed\n" );
326 ok( GetLastError() == ERROR_INVALID_ADDRESS
, "wrong error %u\n", GetLastError() );
329 /* invalid protection values */
330 SetLastError(0xdeadbeef);
331 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_RESERVE
, 0);
332 ok(!addr2
, "VirtualAlloc succeeded\n");
333 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
335 SetLastError(0xdeadbeef);
336 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_COMMIT
, 0);
337 ok(!addr2
, "VirtualAlloc succeeded\n");
338 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
340 SetLastError(0xdeadbeef);
341 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_READONLY
| PAGE_EXECUTE
);
342 ok(!addr2
, "VirtualAlloc succeeded\n");
343 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
345 SetLastError(0xdeadbeef);
346 ok(!VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
| PAGE_EXECUTE_WRITECOPY
, &old_prot
),
347 "VirtualProtect succeeded\n");
348 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
350 SetLastError(0xdeadbeef);
351 ok(!VirtualProtect(addr1
, 0x1000, 0, &old_prot
), "VirtualProtect succeeded\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 ok(!VirtualFree(addr1
, 0x10000, 0), "VirtualFree should fail with type 0\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
357 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
359 ok(VirtualFree(addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFree failed\n");
361 /* if the type is MEM_RELEASE, size must be 0 */
362 ok(!VirtualFree(addr1
, 1, MEM_RELEASE
), "VirtualFree should fail\n");
363 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
364 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
366 ok(VirtualFree(addr1
, 0, MEM_RELEASE
), "VirtualFree failed\n");
369 static void test_MapViewOfFile(void)
371 static const char testfile
[] = "testfile.xxx";
373 HANDLE file
, mapping
, map2
;
374 void *ptr
, *ptr2
, *addr
;
375 MEMORY_BASIC_INFORMATION info
;
378 SetLastError(0xdeadbeef);
379 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
380 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
381 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
382 SetEndOfFile( file
);
384 /* read/write mapping */
386 SetLastError(0xdeadbeef);
387 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
388 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
390 SetLastError(0xdeadbeef);
391 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
392 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
393 UnmapViewOfFile( ptr
);
395 /* this fails on win9x but succeeds on NT */
396 SetLastError(0xdeadbeef);
397 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
398 if (ptr
) UnmapViewOfFile( ptr
);
399 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
401 SetLastError(0xdeadbeef);
402 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
403 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
404 UnmapViewOfFile( ptr
);
406 SetLastError(0xdeadbeef);
407 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
408 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
409 UnmapViewOfFile( ptr
);
411 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
412 FILE_MAP_READ
|FILE_MAP_WRITE
, FALSE
, 0 );
413 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
414 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
415 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
416 UnmapViewOfFile( ptr
);
419 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
420 FILE_MAP_READ
, FALSE
, 0 );
421 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
422 SetLastError(0xdeadbeef);
423 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
426 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
428 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
, 0, FALSE
, 0 );
429 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
430 SetLastError(0xdeadbeef);
431 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
432 ok( !ptr
, "MapViewOfFile succeeded\n" );
433 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
435 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
436 FILE_MAP_READ
, FALSE
, 0 );
437 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
438 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
439 ok( ptr
!= NULL
, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
441 else win_skip( "no access checks on win9x\n" );
443 UnmapViewOfFile( ptr
);
445 CloseHandle( mapping
);
447 /* read-only mapping */
449 SetLastError(0xdeadbeef);
450 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
451 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
453 SetLastError(0xdeadbeef);
454 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
455 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
456 UnmapViewOfFile( ptr
);
458 /* this fails on win9x but succeeds on NT */
459 SetLastError(0xdeadbeef);
460 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
461 if (ptr
) UnmapViewOfFile( ptr
);
462 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
464 SetLastError(0xdeadbeef);
465 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
466 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
467 UnmapViewOfFile( ptr
);
469 SetLastError(0xdeadbeef);
470 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
471 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
472 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
473 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
474 CloseHandle( mapping
);
476 /* copy-on-write mapping */
478 SetLastError(0xdeadbeef);
479 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
480 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
482 SetLastError(0xdeadbeef);
483 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
484 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
485 UnmapViewOfFile( ptr
);
487 SetLastError(0xdeadbeef);
488 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
489 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
490 UnmapViewOfFile( ptr
);
492 SetLastError(0xdeadbeef);
493 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
494 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
495 UnmapViewOfFile( ptr
);
497 SetLastError(0xdeadbeef);
498 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
499 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
500 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
501 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
502 CloseHandle( mapping
);
504 /* no access mapping */
506 SetLastError(0xdeadbeef);
507 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
508 /* fails on NT but succeeds on win9x */
509 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
512 SetLastError(0xdeadbeef);
513 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
514 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
515 UnmapViewOfFile( ptr
);
517 SetLastError(0xdeadbeef);
518 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
519 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
520 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
522 SetLastError(0xdeadbeef);
523 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
524 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
525 UnmapViewOfFile( ptr
);
527 SetLastError(0xdeadbeef);
528 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
529 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
530 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
532 CloseHandle( mapping
);
537 /* now try read-only file */
539 SetLastError(0xdeadbeef);
540 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
541 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
543 SetLastError(0xdeadbeef);
544 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
545 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
546 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
547 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
549 SetLastError(0xdeadbeef);
550 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
551 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
552 CloseHandle( mapping
);
554 SetLastError(0xdeadbeef);
555 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
556 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
557 CloseHandle( mapping
);
560 /* now try no access file */
562 SetLastError(0xdeadbeef);
563 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
564 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
566 SetLastError(0xdeadbeef);
567 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
568 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
569 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
570 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
572 SetLastError(0xdeadbeef);
573 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
574 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
575 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
576 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
578 SetLastError(0xdeadbeef);
579 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
580 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
581 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
582 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
585 DeleteFileA( testfile
);
587 SetLastError(0xdeadbeef);
589 file
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
590 /* nt4 doesn't have Local\\ */
591 if (!file
&& GetLastError() == ERROR_PATH_NOT_FOUND
)
594 file
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
596 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
598 SetLastError(0xdeadbeef);
599 mapping
= OpenFileMappingA( FILE_MAP_READ
, FALSE
, name
);
600 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
601 SetLastError(0xdeadbeef);
602 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
606 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
607 SetLastError(0xdeadbeef);
608 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
609 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
610 SetLastError(0xdeadbeef);
611 size
= VirtualQuery( ptr
, &info
, sizeof(info
) );
612 ok( size
== sizeof(info
),
613 "VirtualQuery error %u\n", GetLastError() );
614 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
615 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
616 ok( info
.AllocationProtect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.AllocationProtect
);
617 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
618 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
619 ok( info
.Protect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.Protect
);
621 else win_skip( "no access checks on win9x\n" );
622 UnmapViewOfFile( ptr
);
623 CloseHandle( mapping
);
625 SetLastError(0xdeadbeef);
626 mapping
= OpenFileMappingA( FILE_MAP_WRITE
, FALSE
, name
);
627 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
628 SetLastError(0xdeadbeef);
629 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
633 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
634 SetLastError(0xdeadbeef);
635 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
636 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
637 SetLastError(0xdeadbeef);
638 size
= VirtualQuery( ptr
, &info
, sizeof(info
) );
639 ok( size
== sizeof(info
),
640 "VirtualQuery error %u\n", GetLastError() );
641 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
642 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
643 ok( info
.AllocationProtect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.AllocationProtect
);
644 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
645 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
646 ok( info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
648 else win_skip( "no access checks on win9x\n" );
649 UnmapViewOfFile( ptr
);
650 CloseHandle( mapping
);
654 /* read/write mapping with SEC_RESERVE */
655 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_RESERVE
, 0, MAPPING_SIZE
, NULL
);
656 ok(mapping
!= INVALID_HANDLE_VALUE
, "CreateFileMappingA failed with error %d\n", GetLastError());
658 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
659 ok(ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
661 ptr2
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
662 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
663 ok(ptr2
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
664 trace("mapping same section resulted in views %p and %p\n", ptr
, ptr2
);
666 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
667 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
668 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
669 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
670 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE
, info
.RegionSize
);
671 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
672 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
674 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
675 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
676 ok(info
.Protect
== PAGE_NOACCESS
,
677 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info
.Protect
);
681 ok(info
.AllocationProtect
== PAGE_READWRITE
,
682 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
683 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
684 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
689 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
690 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
691 ok(info
.BaseAddress
== ptr2
,
692 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
693 ok(info
.AllocationBase
== ptr2
,
694 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
695 ok(info
.AllocationProtect
== PAGE_READWRITE
,
696 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
697 ok(info
.RegionSize
== MAPPING_SIZE
,
698 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE
, info
.RegionSize
);
699 ok(info
.State
== MEM_RESERVE
,
700 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
701 ok(info
.Protect
== 0,
702 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
703 ok(info
.Type
== MEM_MAPPED
,
704 "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
707 ptr
= VirtualAlloc(ptr
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
708 ok(ptr
!= NULL
, "VirtualAlloc failed with error %d\n", GetLastError());
710 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
711 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
712 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
713 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
714 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info
.RegionSize
);
715 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_COMMIT instead of 0x%x\n", info
.State
);
716 ok(info
.Protect
== PAGE_READONLY
, "Protect should have been PAGE_READONLY instead of 0x%x\n", info
.Protect
);
717 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
719 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
720 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
724 ok(info
.AllocationProtect
== PAGE_READWRITE
,
725 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
726 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
729 /* shows that the VirtualAlloc above affects the mapping, not just the
730 * virtual memory in this process - it also affects all other processes
731 * with a view of the mapping, but that isn't tested here */
734 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
735 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
736 ok(info
.BaseAddress
== ptr2
,
737 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
738 ok(info
.AllocationBase
== ptr2
,
739 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
740 ok(info
.AllocationProtect
== PAGE_READWRITE
,
741 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
742 ok(info
.RegionSize
== 0x10000,
743 "RegionSize should have been 0x10000 but was 0x%lx\n", info
.RegionSize
);
744 ok(info
.State
== MEM_COMMIT
,
745 "State should have been MEM_COMMIT instead of 0x%x\n", info
.State
);
746 ok(info
.Protect
== PAGE_READWRITE
,
747 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info
.Protect
);
748 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
751 addr
= VirtualAlloc( ptr
, MAPPING_SIZE
, MEM_RESET
, PAGE_READONLY
);
752 ok( addr
== ptr
|| broken(!addr
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
753 "VirtualAlloc failed with error %u\n", GetLastError() );
755 ret
= VirtualFree( ptr
, 0x10000, MEM_DECOMMIT
);
756 ok( !ret
|| broken(ret
) /* win9x */, "VirtualFree succeeded\n" );
758 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "VirtualFree failed with %u\n", GetLastError() );
760 ret
= UnmapViewOfFile(ptr2
);
761 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
762 ret
= UnmapViewOfFile(ptr
);
763 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
764 CloseHandle(mapping
);
766 addr
= VirtualAlloc(NULL
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
767 ok( addr
!= NULL
, "VirtualAlloc failed with error %u\n", GetLastError() );
769 SetLastError(0xdeadbeef);
770 ok( !UnmapViewOfFile(addr
), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
771 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
772 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
773 SetLastError(0xdeadbeef);
774 ok( !UnmapViewOfFile((char *)addr
+ 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
775 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
776 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
777 SetLastError(0xdeadbeef);
778 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
780 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
782 ok( VirtualFree(addr
, 0, MEM_RELEASE
), "VirtualFree failed\n" );
784 /* close named mapping handle without unmapping */
786 SetLastError(0xdeadbeef);
787 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
788 ok( mapping
!= 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
789 SetLastError(0xdeadbeef);
790 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
791 ok( ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError() );
792 SetLastError(0xdeadbeef);
793 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
794 ok( map2
!= 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
795 SetLastError(0xdeadbeef);
796 ret
= CloseHandle(map2
);
797 ok(ret
, "CloseHandle error %d\n", GetLastError());
798 SetLastError(0xdeadbeef);
799 ret
= CloseHandle(mapping
);
800 ok(ret
, "CloseHandle error %d\n", GetLastError());
802 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
803 ok( !ret
, "memory is not accessible\n" );
805 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
806 ok(ret
, "VirtualQuery error %d\n", GetLastError());
807 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
808 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
809 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
810 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
811 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
812 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
813 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
815 SetLastError(0xdeadbeef);
816 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
818 ok( map2
== 0, "OpenFileMappingA succeeded\n" );
820 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "OpenFileMappingA set error %d\n", GetLastError() );
821 if (map2
) CloseHandle(map2
); /* FIXME: remove once Wine is fixed */
822 SetLastError(0xdeadbeef);
823 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
824 ok( mapping
!= 0, "CreateFileMappingA failed\n" );
826 ok( GetLastError() == ERROR_SUCCESS
, "CreateFileMappingA set error %d\n", GetLastError() );
827 SetLastError(0xdeadbeef);
828 ret
= CloseHandle(mapping
);
829 ok(ret
, "CloseHandle error %d\n", GetLastError());
831 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
832 ok( !ret
, "memory is not accessible\n" );
834 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
835 ok(ret
, "VirtualQuery error %d\n", GetLastError());
836 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
837 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
838 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
839 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
840 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
841 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
842 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
844 SetLastError(0xdeadbeef);
845 ret
= UnmapViewOfFile(ptr
);
846 ok( ret
, "UnmapViewOfFile failed with error %d\n", GetLastError() );
848 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
849 ok( ret
, "memory is accessible\n" );
851 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
852 ok(ret
, "VirtualQuery error %d\n", GetLastError());
853 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
854 ok(info
.Protect
== PAGE_NOACCESS
, "got %#x != expected PAGE_NOACCESS\n", info
.Protect
);
855 ok(info
.AllocationBase
== NULL
, "%p != NULL\n", info
.AllocationBase
);
856 ok(info
.AllocationProtect
== 0, "%#x != 0\n", info
.AllocationProtect
);
857 ok(info
.State
== MEM_FREE
, "%#x != MEM_FREE\n", info
.State
);
858 ok(info
.Type
== 0, "%#x != 0\n", info
.Type
);
860 SetLastError(0xdeadbeef);
861 file
= CreateFileA(testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
862 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
863 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
866 SetLastError(0xdeadbeef);
867 mapping
= CreateFileMappingA(file
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
868 ok( mapping
!= 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
869 SetLastError(0xdeadbeef);
870 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
871 ok( ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError() );
872 SetLastError(0xdeadbeef);
873 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
874 ok( map2
!= 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
875 SetLastError(0xdeadbeef);
876 ret
= CloseHandle(map2
);
877 ok(ret
, "CloseHandle error %d\n", GetLastError());
878 SetLastError(0xdeadbeef);
879 ret
= CloseHandle(mapping
);
880 ok(ret
, "CloseHandle error %d\n", GetLastError());
882 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
883 ok( !ret
, "memory is not accessible\n" );
885 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
886 ok(ret
, "VirtualQuery error %d\n", GetLastError());
887 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
888 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
889 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
890 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
891 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
892 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
893 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
895 SetLastError(0xdeadbeef);
896 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
898 ok( map2
== 0, "OpenFileMappingA succeeded\n" );
900 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "OpenFileMappingA set error %d\n", GetLastError() );
902 SetLastError(0xdeadbeef);
903 mapping
= CreateFileMappingA(file
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
904 ok( mapping
!= 0, "CreateFileMappingA failed\n" );
906 ok( GetLastError() == ERROR_SUCCESS
, "CreateFileMappingA set error %d\n", GetLastError() );
907 SetLastError(0xdeadbeef);
908 ret
= CloseHandle(mapping
);
909 ok(ret
, "CloseHandle error %d\n", GetLastError());
911 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
912 ok( !ret
, "memory is not accessible\n" );
914 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
915 ok(ret
, "VirtualQuery error %d\n", GetLastError());
916 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
917 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
918 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
919 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
920 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
921 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
922 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
924 SetLastError(0xdeadbeef);
925 ret
= UnmapViewOfFile(ptr
);
926 ok( ret
, "UnmapViewOfFile failed with error %d\n", GetLastError() );
928 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
929 ok( ret
, "memory is accessible\n" );
931 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
932 ok(ret
, "VirtualQuery error %d\n", GetLastError());
933 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
934 ok(info
.Protect
== PAGE_NOACCESS
, "got %#x != expected PAGE_NOACCESS\n", info
.Protect
);
935 ok(info
.AllocationBase
== NULL
, "%p != NULL\n", info
.AllocationBase
);
936 ok(info
.AllocationProtect
== 0, "%#x != 0\n", info
.AllocationProtect
);
937 ok(info
.State
== MEM_FREE
, "%#x != MEM_FREE\n", info
.State
);
938 ok(info
.Type
== 0, "%#x != 0\n", info
.Type
);
941 DeleteFileA(testfile
);
944 static void test_NtMapViewOfSection(void)
948 static const char testfile
[] = "testfile.xxx";
949 static const char data
[] = "test data for NtMapViewOfSection";
950 char buffer
[sizeof(data
)];
951 HANDLE file
, mapping
;
954 DWORD status
, written
;
956 LARGE_INTEGER offset
;
958 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
960 win_skip( "NtMapViewOfSection not available\n" );
964 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
965 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
966 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
967 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
968 SetEndOfFile( file
);
970 /* read/write mapping */
972 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
973 ok( mapping
!= 0, "CreateFileMapping failed\n" );
975 hProcess
= create_target_process("sleep");
976 ok(hProcess
!= NULL
, "Can't start process\n");
981 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
982 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
984 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
985 ok( ret
, "ReadProcessMemory failed\n" );
986 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
987 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
989 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
990 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
992 CloseHandle( mapping
);
994 DeleteFileA( testfile
);
996 TerminateProcess(hProcess
, 0);
997 CloseHandle(hProcess
);
1000 static void test_NtAreMappedFilesTheSame(void)
1002 static const char testfile
[] = "testfile.xxx";
1003 HANDLE file
, file2
, mapping
, map2
;
1006 char path
[MAX_PATH
];
1008 if (!pNtAreMappedFilesTheSame
)
1010 win_skip( "NtAreMappedFilesTheSame not available\n" );
1014 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1015 NULL
, CREATE_ALWAYS
, 0, 0 );
1016 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1017 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
1018 SetEndOfFile( file
);
1020 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
1021 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1023 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1024 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1026 file2
= CreateFileA( testfile
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1027 NULL
, OPEN_EXISTING
, 0, 0 );
1028 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1030 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1031 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1032 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
1033 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1034 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1035 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1036 UnmapViewOfFile( ptr2
);
1038 ptr2
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1039 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1040 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1041 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1042 UnmapViewOfFile( ptr2
);
1043 CloseHandle( map2
);
1045 map2
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1046 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1047 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
1048 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1049 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1050 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1051 UnmapViewOfFile( ptr2
);
1052 CloseHandle( map2
);
1053 CloseHandle( file2
);
1055 status
= pNtAreMappedFilesTheSame( ptr
, ptr
);
1056 ok( status
== STATUS_SUCCESS
|| broken(status
== STATUS_NOT_SAME_DEVICE
),
1057 "NtAreMappedFilesTheSame returned %x\n", status
);
1059 status
= pNtAreMappedFilesTheSame( ptr
, (char *)ptr
+ 30 );
1060 ok( status
== STATUS_SUCCESS
|| broken(status
== STATUS_NOT_SAME_DEVICE
),
1061 "NtAreMappedFilesTheSame returned %x\n", status
);
1063 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1064 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1066 status
= pNtAreMappedFilesTheSame( ptr
, (void *)0xdeadbeef );
1067 ok( status
== STATUS_CONFLICTING_ADDRESSES
|| status
== STATUS_INVALID_ADDRESS
,
1068 "NtAreMappedFilesTheSame returned %x\n", status
);
1070 status
= pNtAreMappedFilesTheSame( ptr
, NULL
);
1071 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1073 status
= pNtAreMappedFilesTheSame( ptr
, (void *)GetProcessHeap() );
1074 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
1076 status
= pNtAreMappedFilesTheSame( NULL
, NULL
);
1077 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1079 ptr2
= VirtualAlloc( NULL
, 0x10000, MEM_COMMIT
, PAGE_READWRITE
);
1080 ok( ptr2
!= NULL
, "VirtualAlloc error %u\n", GetLastError() );
1081 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1082 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
1083 VirtualFree( ptr2
, 0, MEM_RELEASE
);
1085 UnmapViewOfFile( ptr
);
1086 CloseHandle( mapping
);
1087 CloseHandle( file
);
1089 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1090 GetModuleHandleA("kernel32.dll") );
1091 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1092 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1093 GetModuleHandleA("kernel32.dll") );
1094 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1095 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1096 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1097 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1099 GetSystemDirectoryA( path
, MAX_PATH
);
1100 strcat( path
, "\\kernel32.dll" );
1101 file
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1102 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1104 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1105 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1106 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1107 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1108 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1109 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1110 UnmapViewOfFile( ptr
);
1111 CloseHandle( mapping
);
1113 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
1114 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1115 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
1116 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1117 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1119 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1121 file2
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1122 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1123 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
1124 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1125 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 0 );
1126 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1127 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1128 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1129 UnmapViewOfFile( ptr2
);
1130 CloseHandle( map2
);
1131 CloseHandle( file2
);
1133 UnmapViewOfFile( ptr
);
1134 CloseHandle( mapping
);
1136 CloseHandle( file
);
1137 DeleteFileA( testfile
);
1140 static void test_CreateFileMapping(void)
1142 HANDLE handle
, handle2
;
1144 /* test case sensitivity */
1146 SetLastError(0xdeadbeef);
1147 handle
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1148 "Wine Test Mapping");
1149 ok( handle
!= NULL
, "CreateFileMapping failed with error %u\n", GetLastError());
1150 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1152 SetLastError(0xdeadbeef);
1153 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1154 "Wine Test Mapping");
1155 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1156 ok( GetLastError() == ERROR_ALREADY_EXISTS
, "wrong error %u\n", GetLastError());
1157 CloseHandle( handle2
);
1159 SetLastError(0xdeadbeef);
1160 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1161 "WINE TEST MAPPING");
1162 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1163 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1164 CloseHandle( handle2
);
1166 SetLastError(0xdeadbeef);
1167 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "Wine Test Mapping");
1168 ok( handle2
!= NULL
, "OpenFileMapping failed with error %d\n", GetLastError());
1169 CloseHandle( handle2
);
1171 SetLastError(0xdeadbeef);
1172 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "WINE TEST MAPPING");
1173 ok( !handle2
, "OpenFileMapping succeeded\n");
1174 ok( GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_INVALID_NAME
/* win9x */,
1175 "wrong error %u\n", GetLastError());
1177 CloseHandle( handle
);
1180 static void test_IsBadReadPtr(void)
1183 void *ptr
= (void *)0xdeadbeef;
1186 ret
= IsBadReadPtr(NULL
, 0);
1187 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1189 ret
= IsBadReadPtr(NULL
, 1);
1190 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1192 ret
= IsBadReadPtr(ptr
, 0);
1193 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1195 ret
= IsBadReadPtr(ptr
, 1);
1196 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1198 ret
= IsBadReadPtr(&stackvar
, 0);
1199 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1201 ret
= IsBadReadPtr(&stackvar
, sizeof(char));
1202 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1205 static void test_IsBadWritePtr(void)
1208 void *ptr
= (void *)0xdeadbeef;
1211 ret
= IsBadWritePtr(NULL
, 0);
1212 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1214 ret
= IsBadWritePtr(NULL
, 1);
1215 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1217 ret
= IsBadWritePtr(ptr
, 0);
1218 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1220 ret
= IsBadWritePtr(ptr
, 1);
1221 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1223 ret
= IsBadWritePtr(&stackval
, 0);
1224 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1226 ret
= IsBadWritePtr(&stackval
, sizeof(char));
1227 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1230 static void test_IsBadCodePtr(void)
1233 void *ptr
= (void *)0xdeadbeef;
1236 ret
= IsBadCodePtr(NULL
);
1237 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1239 ret
= IsBadCodePtr(ptr
);
1240 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1242 ret
= IsBadCodePtr((void *)&stackval
);
1243 ok(ret
== FALSE
, "Expected IsBadCodePtr to return FALSE, got %d\n", ret
);
1246 static void test_write_watch(void)
1249 DWORD ret
, size
, old_prot
;
1250 MEMORY_BASIC_INFORMATION info
;
1255 if (!pGetWriteWatch
|| !pResetWriteWatch
)
1257 win_skip( "GetWriteWatch not supported\n" );
1262 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1264 (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
1266 win_skip( "MEM_WRITE_WATCH not supported\n" );
1269 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1270 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1271 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1272 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1273 ok( info
.AllocationProtect
== PAGE_READWRITE
, "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1274 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1275 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1276 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1277 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1280 SetLastError( 0xdeadbeef );
1281 ret
= pGetWriteWatch( 0, NULL
, size
, results
, &count
, &pagesize
);
1282 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1283 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
1284 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1285 "wrong error %u\n", GetLastError() );
1287 SetLastError( 0xdeadbeef );
1288 ret
= pGetWriteWatch( 0, GetModuleHandleW(NULL
), size
, results
, &count
, &pagesize
);
1291 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1292 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1296 ok( count
== 0, "wrong count %lu\n", count
);
1299 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1300 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1301 ok( count
== 0, "wrong count %lu\n", count
);
1303 base
[pagesize
+ 1] = 0x44;
1306 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1307 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1308 ok( count
== 1, "wrong count %lu\n", count
);
1309 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1312 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1313 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1314 ok( count
== 1, "wrong count %lu\n", count
);
1315 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1318 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1319 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1320 ok( count
== 0, "wrong count %lu\n", count
);
1322 base
[2*pagesize
+ 3] = 0x11;
1323 base
[4*pagesize
+ 8] = 0x11;
1326 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1327 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1328 ok( count
== 2, "wrong count %lu\n", count
);
1329 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1330 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1333 ret
= pGetWriteWatch( 0, base
+ 3*pagesize
, 2*pagesize
, results
, &count
, &pagesize
);
1334 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1335 ok( count
== 1, "wrong count %lu\n", count
);
1336 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1338 ret
= pResetWriteWatch( base
, 3*pagesize
);
1339 ok( !ret
, "pResetWriteWatch failed %u\n", GetLastError() );
1342 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1343 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1344 ok( count
== 1, "wrong count %lu\n", count
);
1345 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1347 *(DWORD
*)(base
+ 2*pagesize
- 2) = 0xdeadbeef;
1350 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1351 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1352 ok( count
== 3, "wrong count %lu\n", count
);
1353 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1354 ok( results
[1] == base
+ 2*pagesize
, "wrong result %p\n", results
[1] );
1355 ok( results
[2] == base
+ 4*pagesize
, "wrong result %p\n", results
[2] );
1358 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1359 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1360 ok( count
== 1, "wrong count %lu\n", count
);
1361 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1364 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1365 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1366 ok( count
== 2, "wrong count %lu\n", count
);
1367 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1368 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1370 /* changing protections doesn't affect watches */
1372 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READONLY
, &old_prot
);
1373 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1374 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1376 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1377 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1378 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1379 ok( info
.RegionSize
== 3*pagesize
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1380 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1381 ok( info
.Protect
== PAGE_READONLY
, "wrong Protect 0x%x\n", info
.Protect
);
1383 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READWRITE
, &old_prot
);
1384 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1385 ok( old_prot
== PAGE_READONLY
, "wrong old prot %x\n", old_prot
);
1388 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1389 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1390 ok( count
== 2, "wrong count %lu\n", count
);
1391 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1392 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1394 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1395 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1396 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1397 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1398 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1399 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1401 /* some invalid parameter tests */
1403 SetLastError( 0xdeadbeef );
1405 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1408 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1409 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1411 SetLastError( 0xdeadbeef );
1412 ret
= pGetWriteWatch( 0, base
, size
, results
, NULL
, &pagesize
);
1413 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1414 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1416 SetLastError( 0xdeadbeef );
1418 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, NULL
);
1419 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1420 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1422 SetLastError( 0xdeadbeef );
1424 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1425 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1426 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1428 SetLastError( 0xdeadbeef );
1430 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1431 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1432 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1434 SetLastError( 0xdeadbeef );
1436 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1437 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1438 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1440 SetLastError( 0xdeadbeef );
1442 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1443 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1444 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1446 SetLastError( 0xdeadbeef );
1448 ret
= pGetWriteWatch( 0, base
, size
* 2, results
, &count
, &pagesize
);
1449 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1450 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1452 SetLastError( 0xdeadbeef );
1454 ret
= pGetWriteWatch( 0, base
+ size
- pagesize
, pagesize
+ 1, results
, &count
, &pagesize
);
1455 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1456 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1458 SetLastError( 0xdeadbeef );
1459 ret
= pResetWriteWatch( base
, 0 );
1460 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1461 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1463 SetLastError( 0xdeadbeef );
1464 ret
= pResetWriteWatch( GetModuleHandleW(NULL
), size
);
1465 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1466 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1468 else /* win98 is completely different */
1470 SetLastError( 0xdeadbeef );
1472 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1473 ok( ret
== ERROR_INVALID_PARAMETER
, "GetWriteWatch succeeded %u\n", ret
);
1474 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1477 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1478 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1481 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1482 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1485 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1486 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1488 ret
= pResetWriteWatch( base
, 0 );
1489 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1491 ret
= pResetWriteWatch( GetModuleHandleW(NULL
), size
);
1492 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1495 VirtualFree( base
, 0, MEM_FREE
);
1497 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1498 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1499 VirtualFree( base
, 0, MEM_FREE
);
1501 base
= VirtualAlloc( 0, size
, MEM_WRITE_WATCH
, PAGE_READWRITE
);
1502 ok( !base
, "VirtualAlloc succeeded\n" );
1503 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1505 /* initial protect doesn't matter */
1507 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_NOACCESS
);
1508 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1509 base
= VirtualAlloc( base
, size
, MEM_COMMIT
, PAGE_NOACCESS
);
1510 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1513 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1514 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1515 ok( count
== 0, "wrong count %lu\n", count
);
1517 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_READWRITE
, &old_prot
);
1518 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1519 ok( old_prot
== PAGE_NOACCESS
, "wrong old prot %x\n", old_prot
);
1521 base
[5*pagesize
+ 200] = 3;
1523 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_NOACCESS
, &old_prot
);
1524 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1525 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1528 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1529 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1530 ok( count
== 1, "wrong count %lu\n", count
);
1531 ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1533 ret
= VirtualFree( base
, size
, MEM_DECOMMIT
);
1534 ok( ret
, "VirtualFree failed %u\n", GetLastError() );
1537 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1538 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1539 ok( count
== 1 || broken(count
== 0), /* win98 */
1540 "wrong count %lu\n", count
);
1541 if (count
) ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1543 VirtualFree( base
, 0, MEM_FREE
);
1546 static void test_VirtualProtect(void)
1548 static const struct test_data
1550 DWORD prot_set
, prot_get
;
1553 { 0, 0 }, /* 0x00 */
1554 { PAGE_NOACCESS
, PAGE_NOACCESS
}, /* 0x01 */
1555 { PAGE_READONLY
, PAGE_READONLY
}, /* 0x02 */
1556 { PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x03 */
1557 { PAGE_READWRITE
, PAGE_READWRITE
}, /* 0x04 */
1558 { PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x05 */
1559 { PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x06 */
1560 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x07 */
1561 { PAGE_WRITECOPY
, 0 }, /* 0x08 */
1562 { PAGE_WRITECOPY
| PAGE_NOACCESS
, 0 }, /* 0x09 */
1563 { PAGE_WRITECOPY
| PAGE_READONLY
, 0 }, /* 0x0a */
1564 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, 0 }, /* 0x0b */
1565 { PAGE_WRITECOPY
| PAGE_READWRITE
, 0 }, /* 0x0c */
1566 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x0d */
1567 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x0e */
1568 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x0f */
1570 { PAGE_EXECUTE
, PAGE_EXECUTE
}, /* 0x10 */
1571 { PAGE_EXECUTE_READ
, PAGE_EXECUTE_READ
}, /* 0x20 */
1572 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x30 */
1573 { PAGE_EXECUTE_READWRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x40 */
1574 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0x50 */
1575 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0x60 */
1576 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x70 */
1577 { PAGE_EXECUTE_WRITECOPY
, 0 }, /* 0x80 */
1578 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, 0 }, /* 0x90 */
1579 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, 0 }, /* 0xa0 */
1580 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0xb0 */
1581 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, 0 }, /* 0xc0 */
1582 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0xd0 */
1583 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0xe0 */
1584 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 } /* 0xf0 */
1587 DWORD ret
, old_prot
, rw_prot
, exec_prot
, i
, j
;
1588 MEMORY_BASIC_INFORMATION info
;
1592 trace("system page size %#x\n", si
.dwPageSize
);
1594 SetLastError(0xdeadbeef);
1595 base
= VirtualAlloc(0, si
.dwPageSize
, MEM_RESERVE
| MEM_COMMIT
, PAGE_NOACCESS
);
1596 ok(base
!= NULL
, "VirtualAlloc failed %d\n", GetLastError());
1598 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1600 SetLastError(0xdeadbeef);
1601 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1602 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1603 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
1604 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
1605 ok(info
.Protect
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, info
.Protect
);
1606 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1607 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%d: %#x != PAGE_NOACCESS\n", i
, info
.AllocationProtect
);
1608 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1609 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
1611 old_prot
= 0xdeadbeef;
1612 SetLastError(0xdeadbeef);
1613 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot_set
, &old_prot
);
1616 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
1617 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
1619 SetLastError(0xdeadbeef);
1620 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1621 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1622 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
1623 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
1624 ok(info
.Protect
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_get
);
1625 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1626 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%d: %#x != PAGE_NOACCESS\n", i
, info
.AllocationProtect
);
1627 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1628 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
1632 ok(!ret
, "%d: VirtualProtect should fail\n", i
);
1633 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
1636 old_prot
= 0xdeadbeef;
1637 SetLastError(0xdeadbeef);
1638 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
1639 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
1641 ok(old_prot
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_get
);
1643 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
1648 for (i
= 0; i
<= 4; i
++)
1652 for (j
= 0; j
<= 4; j
++)
1654 DWORD prot
= exec_prot
| rw_prot
;
1656 SetLastError(0xdeadbeef);
1657 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, prot
);
1658 if ((rw_prot
&& exec_prot
) || (!rw_prot
&& !exec_prot
))
1660 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", prot
);
1661 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1665 if (prot
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
))
1667 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", prot
);
1668 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1672 ok(ptr
!= NULL
, "VirtualAlloc(%02x) error %d\n", prot
, GetLastError());
1673 ok(ptr
== base
, "expected %p, got %p\n", base
, ptr
);
1677 SetLastError(0xdeadbeef);
1678 ret
= VirtualProtect(base
, si
.dwPageSize
, prot
, &old_prot
);
1679 if ((rw_prot
&& exec_prot
) || (!rw_prot
&& !exec_prot
))
1681 ok(!ret
, "VirtualProtect(%02x) should fail\n", prot
);
1682 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1686 if (prot
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
))
1688 ok(!ret
, "VirtualProtect(%02x) should fail\n", prot
);
1689 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1692 ok(ret
, "VirtualProtect(%02x) error %d\n", prot
, GetLastError());
1698 exec_prot
= 1 << (i
+ 4);
1701 VirtualFree(base
, 0, MEM_FREE
);
1704 static BOOL
is_mem_writable(DWORD prot
)
1706 switch (prot
& 0xff)
1708 case PAGE_READWRITE
:
1709 case PAGE_WRITECOPY
:
1710 case PAGE_EXECUTE_READWRITE
:
1711 case PAGE_EXECUTE_WRITECOPY
:
1719 static void test_VirtualAlloc_protection(void)
1721 static const struct test_data
1727 { 0, FALSE
}, /* 0x00 */
1728 { PAGE_NOACCESS
, TRUE
}, /* 0x01 */
1729 { PAGE_READONLY
, TRUE
}, /* 0x02 */
1730 { PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x03 */
1731 { PAGE_READWRITE
, TRUE
}, /* 0x04 */
1732 { PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
}, /* 0x05 */
1733 { PAGE_READWRITE
| PAGE_READONLY
, FALSE
}, /* 0x06 */
1734 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x07 */
1735 { PAGE_WRITECOPY
, FALSE
}, /* 0x08 */
1736 { PAGE_WRITECOPY
| PAGE_NOACCESS
, FALSE
}, /* 0x09 */
1737 { PAGE_WRITECOPY
| PAGE_READONLY
, FALSE
}, /* 0x0a */
1738 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, FALSE
}, /* 0x0b */
1739 { PAGE_WRITECOPY
| PAGE_READWRITE
, FALSE
}, /* 0x0c */
1740 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
}, /* 0x0d */
1741 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, FALSE
}, /* 0x0e */
1742 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x0f */
1744 { PAGE_EXECUTE
, TRUE
}, /* 0x10 */
1745 { PAGE_EXECUTE_READ
, TRUE
}, /* 0x20 */
1746 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0x30 */
1747 { PAGE_EXECUTE_READWRITE
, TRUE
}, /* 0x40 */
1748 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
}, /* 0x50 */
1749 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
}, /* 0x60 */
1750 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0x70 */
1751 { PAGE_EXECUTE_WRITECOPY
, FALSE
}, /* 0x80 */
1752 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, FALSE
}, /* 0x90 */
1753 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, FALSE
}, /* 0xa0 */
1754 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0xb0 */
1755 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, FALSE
}, /* 0xc0 */
1756 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
}, /* 0xd0 */
1757 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
}, /* 0xe0 */
1758 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
} /* 0xf0 */
1762 MEMORY_BASIC_INFORMATION info
;
1766 trace("system page size %#x\n", si
.dwPageSize
);
1768 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1770 SetLastError(0xdeadbeef);
1771 base
= VirtualAlloc(0, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
1775 ok(base
!= NULL
, "%d: VirtualAlloc failed %d\n", i
, GetLastError());
1777 SetLastError(0xdeadbeef);
1778 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1779 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1780 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
1781 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
1782 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
1783 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1784 ok(info
.AllocationProtect
== td
[i
].prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, td
[i
].prot
);
1785 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1786 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
1788 if (is_mem_writable(info
.Protect
))
1792 SetLastError(0xdeadbeef);
1793 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1794 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1795 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
1798 SetLastError(0xdeadbeef);
1799 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
1800 ok(ptr
== base
, "%d: VirtualAlloc failed %d\n", i
, GetLastError());
1802 VirtualFree(base
, 0, MEM_FREE
);
1806 ok(!base
, "%d: VirtualAlloc should fail\n", i
);
1807 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
1812 static void test_CreateFileMapping_protection(void)
1814 static const struct test_data
1818 DWORD prot_after_write
;
1821 { 0, FALSE
, 0 }, /* 0x00 */
1822 { PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x01 */
1823 { PAGE_READONLY
, TRUE
, PAGE_READONLY
}, /* 0x02 */
1824 { PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x03 */
1825 { PAGE_READWRITE
, TRUE
, PAGE_READWRITE
}, /* 0x04 */
1826 { PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x05 */
1827 { PAGE_READWRITE
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x06 */
1828 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x07 */
1829 { PAGE_WRITECOPY
, TRUE
, PAGE_READWRITE
}, /* 0x08 */
1830 { PAGE_WRITECOPY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x09 */
1831 { PAGE_WRITECOPY
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0a */
1832 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0b */
1833 { PAGE_WRITECOPY
| PAGE_READWRITE
, FALSE
, PAGE_NOACCESS
}, /* 0x0c */
1834 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x0d */
1835 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0e */
1836 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x0f */
1838 { PAGE_EXECUTE
, FALSE
, PAGE_EXECUTE
}, /* 0x10 */
1839 { PAGE_EXECUTE_READ
, TRUE
, PAGE_EXECUTE_READ
}, /* 0x20 */
1840 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_EXECUTE_READ
}, /* 0x30 */
1841 { PAGE_EXECUTE_READWRITE
, TRUE
, PAGE_EXECUTE_READWRITE
}, /* 0x40 */
1842 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x50 */
1843 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0x60 */
1844 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x70 */
1845 { PAGE_EXECUTE_WRITECOPY
, TRUE
, PAGE_EXECUTE_READWRITE
}, /* 0x80 */
1846 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x90 */
1847 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0xa0 */
1848 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0xb0 */
1849 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, FALSE
, PAGE_NOACCESS
}, /* 0xc0 */
1850 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0xd0 */
1851 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0xe0 */
1852 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
} /* 0xf0 */
1855 DWORD ret
, i
, alloc_prot
, prot
, old_prot
;
1856 MEMORY_BASIC_INFORMATION info
;
1858 char temp_path
[MAX_PATH
];
1859 char file_name
[MAX_PATH
];
1861 BOOL page_exec_supported
= TRUE
;
1864 trace("system page size %#x\n", si
.dwPageSize
);
1866 GetTempPathA(MAX_PATH
, temp_path
);
1867 GetTempFileNameA(temp_path
, "map", 0, file_name
);
1869 SetLastError(0xdeadbeef);
1870 hfile
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
1871 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile(%s) error %d\n", file_name
, GetLastError());
1872 SetFilePointer(hfile
, si
.dwPageSize
, NULL
, FILE_BEGIN
);
1873 SetEndOfFile(hfile
);
1875 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1877 SetLastError(0xdeadbeef);
1878 hmap
= CreateFileMappingW(hfile
, NULL
, td
[i
].prot
| SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
1884 trace("%d: CreateFileMapping(%04x) failed: %d\n", i
, td
[i
].prot
, GetLastError());
1885 /* NT4 and win2k don't support EXEC on file mappings */
1886 if (td
[i
].prot
== PAGE_EXECUTE_READ
|| td
[i
].prot
== PAGE_EXECUTE_READWRITE
)
1888 page_exec_supported
= FALSE
;
1889 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i
);
1892 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
1893 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
1895 page_exec_supported
= FALSE
;
1896 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
1900 ok(hmap
!= 0, "%d: CreateFileMapping(%04x) error %d\n", i
, td
[i
].prot
, GetLastError());
1902 base
= MapViewOfFile(hmap
, FILE_MAP_READ
, 0, 0, 0);
1903 ok(base
!= NULL
, "%d: MapViewOfFile failed %d\n", i
, GetLastError());
1905 SetLastError(0xdeadbeef);
1906 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1907 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1908 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
1909 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
1910 ok(info
.Protect
== PAGE_READONLY
, "%d: got %#x != expected PAGE_READONLY\n", i
, info
.Protect
);
1911 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1912 ok(info
.AllocationProtect
== PAGE_READONLY
, "%d: %#x != PAGE_READONLY\n", i
, info
.AllocationProtect
);
1913 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1914 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
1916 if (is_mem_writable(info
.Protect
))
1920 SetLastError(0xdeadbeef);
1921 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1922 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1923 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
1926 SetLastError(0xdeadbeef);
1927 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
1928 ok(!ptr
, "%d: VirtualAlloc(%02x) should fail\n", i
, td
[i
].prot
);
1929 /* FIXME: remove once Wine is fixed */
1930 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
1932 ok(GetLastError() == ERROR_ACCESS_DENIED
, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
1934 ok(GetLastError() == ERROR_ACCESS_DENIED
, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
1936 SetLastError(0xdeadbeef);
1937 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot
, &old_prot
);
1938 if (td
[i
].prot
== PAGE_READONLY
|| td
[i
].prot
== PAGE_WRITECOPY
)
1939 ok(ret
, "%d: VirtualProtect(%02x) error %d\n", i
, td
[i
].prot
, GetLastError());
1942 ok(!ret
, "%d: VirtualProtect(%02x) should fail\n", i
, td
[i
].prot
);
1943 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
1946 UnmapViewOfFile(base
);
1951 ok(!hmap
, "%d: CreateFileMapping should fail\n", i
);
1952 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
1956 if (page_exec_supported
) alloc_prot
= PAGE_EXECUTE_READWRITE
;
1957 else alloc_prot
= PAGE_READWRITE
;
1958 SetLastError(0xdeadbeef);
1959 hmap
= CreateFileMappingW(hfile
, NULL
, alloc_prot
, 0, si
.dwPageSize
, NULL
);
1960 ok(hmap
!= 0, "%d: CreateFileMapping error %d\n", i
, GetLastError());
1962 SetLastError(0xdeadbeef);
1963 base
= MapViewOfFile(hmap
, FILE_MAP_READ
| FILE_MAP_WRITE
| (page_exec_supported
? FILE_MAP_EXECUTE
: 0), 0, 0, 0);
1964 ok(base
!= NULL
, "MapViewOfFile failed %d\n", GetLastError());
1966 old_prot
= 0xdeadbeef;
1967 SetLastError(0xdeadbeef);
1968 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
1969 ok(ret
, "VirtualProtect error %d\n", GetLastError());
1970 ok(old_prot
== alloc_prot
, "got %#x != expected %#x\n", old_prot
, alloc_prot
);
1972 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1974 SetLastError(0xdeadbeef);
1975 ret
= VirtualQuery(base
, &info
, sizeof(info
));
1976 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1977 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
1978 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
1979 ok(info
.Protect
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, info
.Protect
);
1980 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1981 ok(info
.AllocationProtect
== alloc_prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, alloc_prot
);
1982 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1983 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
1985 old_prot
= 0xdeadbeef;
1986 SetLastError(0xdeadbeef);
1987 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot
, &old_prot
);
1988 if (td
[i
].success
|| td
[i
].prot
== PAGE_NOACCESS
|| td
[i
].prot
== PAGE_EXECUTE
)
1992 /* win2k and XP don't support EXEC on file mappings */
1993 if (td
[i
].prot
== PAGE_EXECUTE
)
1995 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i
);
1998 /* NT4 and win2k don't support EXEC on file mappings */
1999 if (td
[i
].prot
== PAGE_EXECUTE_READ
|| td
[i
].prot
== PAGE_EXECUTE_READWRITE
)
2001 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i
);
2004 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2005 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2007 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
2012 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2013 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
2016 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
2017 if (prot
== PAGE_EXECUTE_WRITECOPY
) prot
= PAGE_EXECUTE_READWRITE
;
2019 SetLastError(0xdeadbeef);
2020 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2021 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2022 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2023 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2024 /* FIXME: remove the condition below once Wine is fixed */
2025 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2026 todo_wine
ok(info
.Protect
== prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, prot
);
2028 ok(info
.Protect
== prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, prot
);
2029 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2030 ok(info
.AllocationProtect
== alloc_prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, alloc_prot
);
2031 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2032 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
2034 if (is_mem_writable(info
.Protect
))
2038 SetLastError(0xdeadbeef);
2039 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2040 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2041 /* FIXME: remove the condition below once Wine is fixed */
2042 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2043 todo_wine
ok(info
.Protect
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_after_write
);
2045 ok(info
.Protect
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_after_write
);
2050 ok(!ret
, "%d: VirtualProtect should fail\n", i
);
2051 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2055 old_prot
= 0xdeadbeef;
2056 SetLastError(0xdeadbeef);
2057 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
2058 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2059 /* FIXME: remove the condition below once Wine is fixed */
2060 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2061 todo_wine
ok(old_prot
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_after_write
);
2063 ok(old_prot
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_after_write
);
2066 UnmapViewOfFile(base
);
2070 DeleteFileA(file_name
);
2073 #define ACCESS_READ 0x01
2074 #define ACCESS_WRITE 0x02
2075 #define ACCESS_EXECUTE 0x04
2076 #define ACCESS_WRITECOPY 0x08
2078 static DWORD
page_prot_to_access(DWORD prot
)
2082 case PAGE_READWRITE
:
2083 return ACCESS_READ
| ACCESS_WRITE
;
2086 case PAGE_EXECUTE_READ
:
2087 return ACCESS_READ
| ACCESS_EXECUTE
;
2089 case PAGE_EXECUTE_READWRITE
:
2090 return ACCESS_READ
| ACCESS_WRITE
| ACCESS_WRITECOPY
| ACCESS_EXECUTE
;
2092 case PAGE_EXECUTE_WRITECOPY
:
2093 return ACCESS_READ
| ACCESS_WRITECOPY
| ACCESS_EXECUTE
;
2098 case PAGE_WRITECOPY
:
2106 static BOOL
is_compatible_protection(DWORD map_prot
, DWORD view_prot
, DWORD prot
)
2108 DWORD map_access
, view_access
, prot_access
;
2110 map_access
= page_prot_to_access(map_prot
);
2111 view_access
= page_prot_to_access(view_prot
);
2112 prot_access
= page_prot_to_access(prot
);
2114 if (view_access
== prot_access
) return TRUE
;
2115 if (!view_access
) return FALSE
;
2117 if ((view_access
& prot_access
) != prot_access
) return FALSE
;
2118 if ((map_access
& prot_access
) == prot_access
) return TRUE
;
2123 static DWORD
map_prot_to_access(DWORD prot
)
2127 case PAGE_READWRITE
:
2128 case PAGE_EXECUTE_READWRITE
:
2129 return SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
| SECTION_MAP_EXECUTE_EXPLICIT
| SECTION_QUERY
;
2131 case PAGE_WRITECOPY
:
2133 case PAGE_EXECUTE_READ
:
2134 case PAGE_EXECUTE_WRITECOPY
:
2135 return SECTION_MAP_READ
| SECTION_MAP_EXECUTE
| SECTION_MAP_EXECUTE_EXPLICIT
| SECTION_QUERY
;
2141 static BOOL
is_compatible_access(DWORD map_prot
, DWORD view_prot
)
2143 DWORD access
= map_prot_to_access(map_prot
);
2144 if (!view_prot
) view_prot
= SECTION_MAP_READ
;
2145 return (view_prot
& access
) == view_prot
;
2148 static void *map_view_of_file(HANDLE handle
, DWORD access
)
2151 LARGE_INTEGER offset
;
2157 if (!pNtMapViewOfSection
) return NULL
;
2160 offset
.u
.LowPart
= 0;
2161 offset
.u
.HighPart
= 0;
2163 exec
= access
& FILE_MAP_EXECUTE
;
2164 access
&= ~FILE_MAP_EXECUTE
;
2166 if (access
== FILE_MAP_COPY
)
2169 protect
= PAGE_EXECUTE_WRITECOPY
;
2171 protect
= PAGE_WRITECOPY
;
2173 else if (access
& FILE_MAP_WRITE
)
2176 protect
= PAGE_EXECUTE_READWRITE
;
2178 protect
= PAGE_READWRITE
;
2180 else if (access
& FILE_MAP_READ
)
2183 protect
= PAGE_EXECUTE_READ
;
2185 protect
= PAGE_READONLY
;
2187 else protect
= PAGE_NOACCESS
;
2190 status
= pNtMapViewOfSection(handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
2191 &count
, 1 /* ViewShare */, 0, protect
);
2194 /* for simplicity */
2195 SetLastError(ERROR_ACCESS_DENIED
);
2201 static void test_mapping(void)
2203 static const DWORD page_prot
[] =
2205 PAGE_NOACCESS
, PAGE_READONLY
, PAGE_READWRITE
, PAGE_WRITECOPY
,
2206 PAGE_EXECUTE_READ
, PAGE_EXECUTE_READWRITE
, PAGE_EXECUTE_WRITECOPY
2213 { 0, PAGE_NOACCESS
}, /* 0x00 */
2214 { FILE_MAP_COPY
, PAGE_WRITECOPY
}, /* 0x01 */
2215 { FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x02 */
2216 { FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x03 */
2217 { FILE_MAP_READ
, PAGE_READONLY
}, /* 0x04 */
2218 { FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_READONLY
}, /* 0x05 */
2219 { FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x06 */
2220 { FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x07 */
2221 { SECTION_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x08 */
2222 { SECTION_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_NOACCESS
}, /* 0x09 */
2223 { SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x0a */
2224 { SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x0b */
2225 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_READONLY
}, /* 0x0c */
2226 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_READONLY
}, /* 0x0d */
2227 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x0e */
2228 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x0f */
2229 { FILE_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x20 */
2230 { FILE_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_EXECUTE_WRITECOPY
}, /* 0x21 */
2231 { FILE_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x22 */
2232 { FILE_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x23 */
2233 { FILE_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_EXECUTE_READ
}, /* 0x24 */
2234 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_EXECUTE_READ
}, /* 0x25 */
2235 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x26 */
2236 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x27 */
2237 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x28 */
2238 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_NOACCESS
}, /* 0x29 */
2239 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x2a */
2240 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x2b */
2241 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_EXECUTE_READ
}, /* 0x2c */
2242 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_EXECUTE_READ
}, /* 0x2d */
2243 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x2e */
2244 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
} /* 0x2f */
2246 void *base
, *nt_base
, *ptr
;
2247 DWORD i
, j
, k
, ret
, old_prot
, prev_prot
;
2249 char temp_path
[MAX_PATH
];
2250 char file_name
[MAX_PATH
];
2252 MEMORY_BASIC_INFORMATION info
, nt_info
;
2255 trace("system page size %#x\n", si
.dwPageSize
);
2257 GetTempPathA(MAX_PATH
, temp_path
);
2258 GetTempFileNameA(temp_path
, "map", 0, file_name
);
2260 SetLastError(0xdeadbeef);
2261 hfile
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
2262 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile(%s) error %d\n", file_name
, GetLastError());
2263 SetFilePointer(hfile
, si
.dwPageSize
, NULL
, FILE_BEGIN
);
2264 SetEndOfFile(hfile
);
2266 for (i
= 0; i
< sizeof(page_prot
)/sizeof(page_prot
[0]); i
++)
2268 SetLastError(0xdeadbeef);
2269 hmap
= CreateFileMappingW(hfile
, NULL
, page_prot
[i
] | SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
2271 if (page_prot
[i
] == PAGE_NOACCESS
)
2275 ok(!hmap
, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2276 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2278 /* A trick to create a not accessible mapping */
2279 SetLastError(0xdeadbeef);
2280 hmap
= CreateFileMappingW(hfile
, NULL
, PAGE_READWRITE
| SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
2281 ok(hmap
!= 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2282 SetLastError(0xdeadbeef);
2283 ret
= DuplicateHandle(GetCurrentProcess(), hmap
, GetCurrentProcess(), &hmap2
, 0, FALSE
, 0);
2284 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
2291 trace("%d: CreateFileMapping(%04x) failed: %d\n", i
, page_prot
[i
], GetLastError());
2293 /* NT4 and win2k don't support EXEC on file mappings */
2294 if (page_prot
[i
] == PAGE_EXECUTE_READ
|| page_prot
[i
] == PAGE_EXECUTE_READWRITE
)
2296 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i
);
2299 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2300 if (page_prot
[i
] == PAGE_EXECUTE_WRITECOPY
)
2302 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
2307 ok(hmap
!= 0, "%d: CreateFileMapping(%04x) error %d\n", i
, page_prot
[i
], GetLastError());
2309 for (j
= 0; j
< sizeof(view
)/sizeof(view
[0]); j
++)
2311 nt_base
= map_view_of_file(hmap
, view
[j
].access
);
2314 SetLastError(0xdeadbeef);
2315 ret
= VirtualQuery(nt_base
, &nt_info
, sizeof(nt_info
));
2316 ok(ret
, "%d: VirtualQuery failed %d\n", j
, GetLastError());
2317 UnmapViewOfFile(nt_base
);
2320 SetLastError(0xdeadbeef);
2321 base
= MapViewOfFile(hmap
, view
[j
].access
, 0, 0, 0);
2323 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
2324 ok(!nt_base
== !base
||
2325 broken((view
[j
].access
& FILE_MAP_EXECUTE
) && !nt_base
!= !base
),
2326 "%d: (%04x/%04x) NT %p kernel %p\n", j
, page_prot
[i
], view
[j
].access
, nt_base
, base
);
2328 if (!is_compatible_access(page_prot
[i
], view
[j
].access
))
2330 ok(!base
, "%d: MapViewOfFile(%04x/%04x) should fail\n", j
, page_prot
[i
], view
[j
].access
);
2331 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %d\n", GetLastError());
2335 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
2336 if (!base
&& (view
[j
].access
& FILE_MAP_EXECUTE
))
2338 ok(broken(!base
), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j
, page_prot
[i
], view
[j
].access
, GetLastError());
2342 ok(base
!= NULL
, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j
, page_prot
[i
], view
[j
].access
, GetLastError());
2344 SetLastError(0xdeadbeef);
2345 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2346 ok(ret
, "%d: VirtualQuery failed %d\n", j
, GetLastError());
2347 ok(info
.BaseAddress
== base
, "%d: (%04x) got %p, expected %p\n", j
, view
[j
].access
, info
.BaseAddress
, base
);
2348 ok(info
.RegionSize
== si
.dwPageSize
, "%d: (%04x) got %#lx != expected %#x\n", j
, view
[j
].access
, info
.RegionSize
, si
.dwPageSize
);
2349 ok(info
.Protect
== view
[j
].prot
||
2350 broken(view
[j
].prot
== PAGE_EXECUTE_READ
&& info
.Protect
== PAGE_READONLY
) || /* win2k */
2351 broken(view
[j
].prot
== PAGE_EXECUTE_READWRITE
&& info
.Protect
== PAGE_READWRITE
) || /* win2k */
2352 broken(view
[j
].prot
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
2353 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, info
.Protect
, view
[j
].prot
);
2354 ok(info
.AllocationBase
== base
, "%d: (%04x) got %p, expected %p\n", j
, view
[j
].access
, info
.AllocationBase
, base
);
2355 ok(info
.AllocationProtect
== info
.Protect
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, info
.AllocationProtect
, info
.Protect
);
2356 ok(info
.State
== MEM_COMMIT
, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j
, view
[j
].access
, info
.State
);
2357 ok(info
.Type
== MEM_MAPPED
, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j
, view
[j
].access
, info
.Type
);
2359 if (nt_base
&& base
)
2361 ok(nt_info
.RegionSize
== info
.RegionSize
, "%d: (%04x) got %#lx != expected %#lx\n", j
, view
[j
].access
, nt_info
.RegionSize
, info
.RegionSize
);
2362 ok(nt_info
.Protect
== info
.Protect
/* Vista+ */ ||
2363 broken(nt_info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
2364 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.Protect
, info
.Protect
);
2365 ok(nt_info
.AllocationProtect
== info
.AllocationProtect
/* Vista+ */ ||
2366 broken(nt_info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
2367 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.AllocationProtect
, info
.AllocationProtect
);
2368 ok(nt_info
.State
== info
.State
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.State
, info
.State
);
2369 ok(nt_info
.Type
== info
.Type
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.Type
, info
.Type
);
2372 prev_prot
= info
.Protect
;
2374 for (k
= 0; k
< sizeof(page_prot
)/sizeof(page_prot
[0]); k
++)
2376 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2377 SetLastError(0xdeadbeef);
2378 old_prot
= 0xdeadbeef;
2379 ret
= VirtualProtect(base
, si
.dwPageSize
, page_prot
[k
], &old_prot
);
2380 if (is_compatible_protection(page_prot
[i
], view
[j
].prot
, page_prot
[k
]))
2382 /* win2k and XP don't support EXEC on file mappings */
2383 if (!ret
&& page_prot
[k
] == PAGE_EXECUTE
)
2385 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2388 /* NT4 and win2k don't support EXEC on file mappings */
2389 if (!ret
&& (page_prot
[k
] == PAGE_EXECUTE_READ
|| page_prot
[k
] == PAGE_EXECUTE_READWRITE
))
2391 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE\n");
2394 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2395 if (!ret
&& page_prot
[k
] == PAGE_EXECUTE_WRITECOPY
)
2397 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
2400 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
2401 if (!ret
&& view
[j
].prot
== PAGE_EXECUTE_WRITECOPY
)
2403 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
2407 ok(ret
, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
2408 ok(old_prot
== prev_prot
, "got %#x, expected %#x\n", old_prot
, prev_prot
);
2409 prev_prot
= page_prot
[k
];
2413 /* NT4 doesn't fail on incompatible map and view */
2416 ok(broken(ret
), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
2417 skip("Incompatible map and view are not properly handled on this platform\n");
2418 break; /* NT4 won't pass remaining tests */
2421 ok(!ret
, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
2422 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2426 for (k
= 0; k
< sizeof(page_prot
)/sizeof(page_prot
[0]); k
++)
2428 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
2429 SetLastError(0xdeadbeef);
2430 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, page_prot
[k
]);
2431 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", page_prot
[k
]);
2432 /* FIXME: remove once Wine is fixed */
2433 if (page_prot
[k
] == PAGE_WRITECOPY
|| page_prot
[k
] == PAGE_EXECUTE_WRITECOPY
)
2435 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2437 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2440 UnmapViewOfFile(base
);
2447 DeleteFileA(file_name
);
2450 static void test_shared_memory(int is_child
)
2455 SetLastError(0xdeadbef);
2456 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, "winetest_virtual.c");
2457 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
2459 ok(GetLastError() == ERROR_ALREADY_EXISTS
, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
2461 SetLastError(0xdeadbef);
2462 p
= MapViewOfFile(mapping
, FILE_MAP_READ
|FILE_MAP_WRITE
, 0, 0, 4096);
2463 ok(p
!= NULL
, "MapViewOfFile error %d\n", GetLastError());
2467 ok(*p
== 0x1a2b3c4d, "expected 0x1a2b3c4d in child, got %#x\n", *p
);
2472 char cmdline
[MAX_PATH
];
2473 PROCESS_INFORMATION pi
;
2474 STARTUPINFOA si
= { sizeof(si
) };
2479 winetest_get_mainargs(&argv
);
2480 sprintf(cmdline
, "\"%s\" virtual sharedmem", argv
[0]);
2481 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2482 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2483 winetest_wait_child_process(pi
.hProcess
);
2484 CloseHandle(pi
.hThread
);
2485 CloseHandle(pi
.hProcess
);
2489 CloseHandle(mapping
);
2496 argc
= winetest_get_mainargs( &argv
);
2500 if (!strcmp(argv
[2], "sleep"))
2502 Sleep(5000); /* spawned process runs for at most 5 seconds */
2505 if (!strcmp(argv
[2], "sharedmem"))
2507 test_shared_memory(1);
2514 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
2515 PAGE_EXECUTE_READWRITE
);
2516 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
2517 if (mem
== NULL
) break;
2518 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
2519 ok(ret
, "VirtualFree failed %u\n", GetLastError());
2525 hkernel32
= GetModuleHandleA("kernel32.dll");
2526 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
2527 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
2528 pGetWriteWatch
= (void *) GetProcAddress(hkernel32
, "GetWriteWatch");
2529 pResetWriteWatch
= (void *) GetProcAddress(hkernel32
, "ResetWriteWatch");
2530 pNtAreMappedFilesTheSame
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"),
2531 "NtAreMappedFilesTheSame" );
2532 pNtMapViewOfSection
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtMapViewOfSection");
2533 pNtUnmapViewOfSection
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection");
2535 test_shared_memory(0);
2537 test_CreateFileMapping_protection();
2538 test_VirtualAlloc_protection();
2539 test_VirtualProtect();
2540 test_VirtualAllocEx();
2541 test_VirtualAlloc();
2542 test_MapViewOfFile();
2543 test_NtMapViewOfSection();
2544 test_NtAreMappedFilesTheSame();
2545 test_CreateFileMapping();
2546 test_IsBadReadPtr();
2547 test_IsBadWritePtr();
2548 test_IsBadCodePtr();