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
33 #include "wine/test.h"
36 #define MAPPING_SIZE 0x100000
38 static HINSTANCE hkernel32
;
39 static LPVOID (WINAPI
*pVirtualAllocEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
, DWORD
);
40 static BOOL (WINAPI
*pVirtualFreeEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
);
41 static UINT (WINAPI
*pGetWriteWatch
)(DWORD
,LPVOID
,SIZE_T
,LPVOID
*,ULONG_PTR
*,ULONG
*);
42 static UINT (WINAPI
*pResetWriteWatch
)(LPVOID
,SIZE_T
);
43 static NTSTATUS (WINAPI
*pNtAreMappedFilesTheSame
)(PVOID
,PVOID
);
44 static NTSTATUS (WINAPI
*pNtMapViewOfSection
)(HANDLE
, HANDLE
, PVOID
*, ULONG
, SIZE_T
, const LARGE_INTEGER
*, SIZE_T
*, ULONG
, ULONG
, ULONG
);
45 static DWORD (WINAPI
*pNtUnmapViewOfSection
)(HANDLE
, PVOID
);
46 static struct _TEB
* (WINAPI
*pNtCurrentTeb
)(void);
48 /* ############################### */
50 static HANDLE
create_target_process(const char *arg
)
53 char cmdline
[MAX_PATH
];
54 PROCESS_INFORMATION pi
;
56 STARTUPINFOA si
= { 0 };
59 winetest_get_mainargs( &argv
);
60 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
61 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
62 ok(ret
, "error: %u\n", GetLastError());
63 ret
= CloseHandle(pi
.hThread
);
64 ok(ret
, "error %u\n", GetLastError());
68 static void test_VirtualAllocEx(void)
70 const unsigned int alloc_size
= 1<<15;
72 SIZE_T bytes_written
= 0, bytes_read
= 0, i
;
76 MEMORY_BASIC_INFORMATION info
;
79 /* not exported in all windows-versions */
80 if ((!pVirtualAllocEx
) || (!pVirtualFreeEx
)) {
81 win_skip("Virtual{Alloc,Free}Ex not available\n");
85 hProcess
= create_target_process("sleep");
86 ok(hProcess
!= NULL
, "Can't start process\n");
88 SetLastError(0xdeadbeef);
89 addr1
= pVirtualAllocEx(hProcess
, NULL
, alloc_size
, MEM_COMMIT
,
90 PAGE_EXECUTE_READWRITE
);
91 if (!addr1
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
93 win_skip("VirtualAllocEx not implemented\n");
94 TerminateProcess(hProcess
, 0);
95 CloseHandle(hProcess
);
99 src
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
100 dst
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
101 for (i
= 0; i
< alloc_size
; i
++)
104 ok(addr1
!= NULL
, "VirtualAllocEx error %u\n", GetLastError());
105 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
106 ok(b
&& (bytes_written
== alloc_size
), "%lu bytes written\n",
108 b
= ReadProcessMemory(hProcess
, addr1
, dst
, alloc_size
, &bytes_read
);
109 ok(b
&& (bytes_read
== alloc_size
), "%lu bytes read\n", bytes_read
);
110 ok(!memcmp(src
, dst
, alloc_size
), "Data from remote process differs\n");
112 /* test invalid source buffers */
114 b
= VirtualProtect( src
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
115 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
116 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
117 ok( !b
, "WriteProcessMemory succeeded\n" );
118 ok( GetLastError() == ERROR_NOACCESS
||
119 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
120 "wrong error %u\n", GetLastError() );
121 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
122 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
123 ok( !b
, "ReadProcessMemory succeeded\n" );
124 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
125 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
127 b
= VirtualProtect( src
, 0x2000, PAGE_NOACCESS
, &old_prot
);
128 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
129 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
130 ok( !b
, "WriteProcessMemory succeeded\n" );
131 ok( GetLastError() == ERROR_NOACCESS
||
132 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
133 "wrong error %u\n", GetLastError() );
134 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
135 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
136 ok( !b
, "ReadProcessMemory succeeded\n" );
137 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
138 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
140 b
= pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
);
141 ok(b
!= 0, "VirtualFreeEx, error %u\n", GetLastError());
143 VirtualFree( src
, 0, MEM_FREE
);
144 VirtualFree( dst
, 0, MEM_FREE
);
147 * The following tests parallel those in test_VirtualAlloc()
150 SetLastError(0xdeadbeef);
151 addr1
= pVirtualAllocEx(hProcess
, 0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
152 ok(addr1
== NULL
, "VirtualAllocEx should fail on zero-sized allocation\n");
153 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
154 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
155 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
157 addr1
= pVirtualAllocEx(hProcess
, 0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
158 ok(addr1
!= NULL
, "VirtualAllocEx failed\n");
160 /* test a not committed memory */
161 memset(&info
, 'q', sizeof(info
));
162 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
), "VirtualQueryEx failed\n");
163 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
164 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
165 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
166 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
167 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
168 /* NT reports Protect == 0 for a not committed memory block */
169 ok(info
.Protect
== 0 /* NT */ ||
170 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
171 "%x != PAGE_NOACCESS\n", info
.Protect
);
172 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
174 SetLastError(0xdeadbeef);
175 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
176 "VirtualProtectEx should fail on a not committed memory\n");
177 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
178 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
179 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
181 addr2
= pVirtualAllocEx(hProcess
, addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
182 ok(addr1
== addr2
, "VirtualAllocEx failed\n");
184 /* test a committed memory */
185 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
),
186 "VirtualQueryEx failed\n");
187 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
188 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
189 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
190 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
191 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
192 /* this time NT reports PAGE_NOACCESS as well */
193 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
194 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
196 /* this should fail, since not the whole range is committed yet */
197 SetLastError(0xdeadbeef);
198 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
199 "VirtualProtectEx should fail on a not committed memory\n");
200 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
201 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
202 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
205 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtectEx failed\n");
206 ok(old_prot
== PAGE_NOACCESS
, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
209 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtectEx failed\n");
210 ok(old_prot
== PAGE_READONLY
, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
212 ok(!pVirtualFreeEx(hProcess
, addr1
, 0x10000, 0),
213 "VirtualFreeEx should fail with type 0\n");
214 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
215 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
217 ok(pVirtualFreeEx(hProcess
, addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFreeEx failed\n");
219 /* if the type is MEM_RELEASE, size must be 0 */
220 ok(!pVirtualFreeEx(hProcess
, addr1
, 1, MEM_RELEASE
),
221 "VirtualFreeEx should fail\n");
222 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
223 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
225 ok(pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
), "VirtualFreeEx failed\n");
227 TerminateProcess(hProcess
, 0);
228 CloseHandle(hProcess
);
231 static void test_VirtualAlloc(void)
235 MEMORY_BASIC_INFORMATION info
;
237 SetLastError(0xdeadbeef);
238 addr1
= VirtualAlloc(0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
239 ok(addr1
== NULL
, "VirtualAlloc should fail on zero-sized allocation\n");
240 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
241 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
242 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
244 addr1
= VirtualAlloc(0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
245 ok(addr1
!= NULL
, "VirtualAlloc failed\n");
247 /* test a not committed memory */
248 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
249 "VirtualQuery failed\n");
250 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
251 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
252 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
253 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
254 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
255 /* NT reports Protect == 0 for a not committed memory block */
256 ok(info
.Protect
== 0 /* NT */ ||
257 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
258 "%x != PAGE_NOACCESS\n", info
.Protect
);
259 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
261 SetLastError(0xdeadbeef);
262 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
263 "VirtualProtect should fail on a not committed memory\n");
264 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
265 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
266 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
268 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
269 ok(addr1
== addr2
, "VirtualAlloc failed\n");
271 /* test a committed memory */
272 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
273 "VirtualQuery failed\n");
274 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
275 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
276 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
277 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
278 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
279 /* this time NT reports PAGE_NOACCESS as well */
280 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
281 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
283 /* this should fail, since not the whole range is committed yet */
284 SetLastError(0xdeadbeef);
285 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
286 "VirtualProtect should fail on a not committed memory\n");
287 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
288 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
289 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
291 ok(VirtualProtect(addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtect failed\n");
292 ok(old_prot
== PAGE_NOACCESS
,
293 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
295 ok(VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtect failed\n");
296 ok(old_prot
== PAGE_READONLY
,
297 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
299 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
300 "VirtualQuery failed\n");
301 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
302 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
303 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
304 memset( addr1
, 0x55, 20 );
305 ok( *(DWORD
*)addr1
== 0x55555555, "wrong data %x\n", *(DWORD
*)addr1
);
307 addr2
= VirtualAlloc( addr1
, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
308 ok( addr2
== addr1
|| broken( !addr2
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
309 "VirtualAlloc failed err %u\n", GetLastError() );
310 ok( *(DWORD
*)addr1
== 0x55555555 || *(DWORD
*)addr1
== 0, "wrong data %x\n", *(DWORD
*)addr1
);
313 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
314 "VirtualQuery failed\n");
315 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
316 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
317 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
319 addr2
= VirtualAlloc( (char *)addr1
+ 0x1000, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
320 ok( (char *)addr2
== (char *)addr1
+ 0x1000, "VirtualAlloc failed\n" );
322 ok(VirtualQuery(addr2
, &info
, sizeof(info
)) == sizeof(info
),
323 "VirtualQuery failed\n");
324 ok(info
.RegionSize
== 0xf000, "%lx != 0xf000\n", info
.RegionSize
);
325 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
326 ok(info
.Protect
== 0, "%x != 0\n", info
.Protect
);
328 addr2
= VirtualAlloc( (char *)addr1
+ 0xf000, 0x2000, MEM_RESET
, PAGE_NOACCESS
);
329 ok( !addr2
, "VirtualAlloc failed\n" );
330 ok( GetLastError() == ERROR_INVALID_ADDRESS
, "wrong error %u\n", GetLastError() );
333 /* invalid protection values */
334 SetLastError(0xdeadbeef);
335 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_RESERVE
, 0);
336 ok(!addr2
, "VirtualAlloc succeeded\n");
337 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
339 SetLastError(0xdeadbeef);
340 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_COMMIT
, 0);
341 ok(!addr2
, "VirtualAlloc succeeded\n");
342 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
344 SetLastError(0xdeadbeef);
345 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_READONLY
| PAGE_EXECUTE
);
346 ok(!addr2
, "VirtualAlloc succeeded\n");
347 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
349 SetLastError(0xdeadbeef);
350 ok(!VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
| PAGE_EXECUTE_WRITECOPY
, &old_prot
),
351 "VirtualProtect succeeded\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 ok(!VirtualProtect(addr1
, 0x1000, 0, &old_prot
), "VirtualProtect succeeded\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
358 SetLastError(0xdeadbeef);
359 ok(!VirtualFree(addr1
, 0x10000, 0), "VirtualFree should fail with type 0\n");
360 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
361 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
363 ok(VirtualFree(addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFree failed\n");
365 /* if the type is MEM_RELEASE, size must be 0 */
366 ok(!VirtualFree(addr1
, 1, MEM_RELEASE
), "VirtualFree should fail\n");
367 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
368 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
370 ok(VirtualFree(addr1
, 0, MEM_RELEASE
), "VirtualFree failed\n");
373 static void test_MapViewOfFile(void)
375 static const char testfile
[] = "testfile.xxx";
377 HANDLE file
, mapping
, map2
;
378 void *ptr
, *ptr2
, *addr
;
379 MEMORY_BASIC_INFORMATION info
;
382 SetLastError(0xdeadbeef);
383 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
384 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
385 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
386 SetEndOfFile( file
);
388 /* read/write mapping */
390 SetLastError(0xdeadbeef);
391 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
392 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
394 SetLastError(0xdeadbeef);
395 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
396 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
397 UnmapViewOfFile( ptr
);
399 /* this fails on win9x but succeeds on NT */
400 SetLastError(0xdeadbeef);
401 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
402 if (ptr
) UnmapViewOfFile( ptr
);
403 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
405 SetLastError(0xdeadbeef);
406 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
407 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
408 UnmapViewOfFile( ptr
);
410 SetLastError(0xdeadbeef);
411 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
412 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
413 UnmapViewOfFile( ptr
);
415 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
416 FILE_MAP_READ
|FILE_MAP_WRITE
, FALSE
, 0 );
417 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
418 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
419 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
420 UnmapViewOfFile( ptr
);
423 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
424 FILE_MAP_READ
, FALSE
, 0 );
425 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
426 SetLastError(0xdeadbeef);
427 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
430 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
432 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
, 0, FALSE
, 0 );
433 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
434 SetLastError(0xdeadbeef);
435 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
436 ok( !ptr
, "MapViewOfFile succeeded\n" );
437 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
439 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
440 FILE_MAP_READ
, FALSE
, 0 );
441 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
442 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
443 ok( ptr
!= NULL
, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
445 else win_skip( "no access checks on win9x\n" );
447 UnmapViewOfFile( ptr
);
449 CloseHandle( mapping
);
451 /* read-only mapping */
453 SetLastError(0xdeadbeef);
454 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
455 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
457 SetLastError(0xdeadbeef);
458 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
459 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
460 UnmapViewOfFile( ptr
);
462 /* this fails on win9x but succeeds on NT */
463 SetLastError(0xdeadbeef);
464 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
465 if (ptr
) UnmapViewOfFile( ptr
);
466 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
468 SetLastError(0xdeadbeef);
469 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
470 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
471 UnmapViewOfFile( ptr
);
473 SetLastError(0xdeadbeef);
474 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
475 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
476 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
477 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
478 CloseHandle( mapping
);
480 /* copy-on-write mapping */
482 SetLastError(0xdeadbeef);
483 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
484 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
486 SetLastError(0xdeadbeef);
487 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
488 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
489 UnmapViewOfFile( ptr
);
491 SetLastError(0xdeadbeef);
492 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
493 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
494 UnmapViewOfFile( ptr
);
496 SetLastError(0xdeadbeef);
497 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
498 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
499 UnmapViewOfFile( ptr
);
501 SetLastError(0xdeadbeef);
502 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
503 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
504 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
505 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
506 CloseHandle( mapping
);
508 /* no access mapping */
510 SetLastError(0xdeadbeef);
511 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
512 /* fails on NT but succeeds on win9x */
513 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
516 SetLastError(0xdeadbeef);
517 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
518 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
519 UnmapViewOfFile( ptr
);
521 SetLastError(0xdeadbeef);
522 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
523 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
524 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
526 SetLastError(0xdeadbeef);
527 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
528 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
529 UnmapViewOfFile( ptr
);
531 SetLastError(0xdeadbeef);
532 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
533 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
534 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
536 CloseHandle( mapping
);
541 /* now try read-only file */
543 SetLastError(0xdeadbeef);
544 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
545 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
547 SetLastError(0xdeadbeef);
548 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
549 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
550 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
551 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
553 SetLastError(0xdeadbeef);
554 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
555 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
556 CloseHandle( mapping
);
558 SetLastError(0xdeadbeef);
559 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
560 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
561 CloseHandle( mapping
);
564 /* now try no access file */
566 SetLastError(0xdeadbeef);
567 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
568 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
570 SetLastError(0xdeadbeef);
571 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
572 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
573 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
574 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
576 SetLastError(0xdeadbeef);
577 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
578 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
579 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
580 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
582 SetLastError(0xdeadbeef);
583 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
584 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
585 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
586 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
589 DeleteFileA( testfile
);
591 SetLastError(0xdeadbeef);
593 file
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
594 /* nt4 doesn't have Local\\ */
595 if (!file
&& GetLastError() == ERROR_PATH_NOT_FOUND
)
598 file
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
600 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
602 SetLastError(0xdeadbeef);
603 mapping
= OpenFileMappingA( FILE_MAP_READ
, FALSE
, name
);
604 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
605 SetLastError(0xdeadbeef);
606 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
610 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
611 SetLastError(0xdeadbeef);
612 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
613 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
614 SetLastError(0xdeadbeef);
615 size
= VirtualQuery( ptr
, &info
, sizeof(info
) );
616 ok( size
== sizeof(info
),
617 "VirtualQuery error %u\n", GetLastError() );
618 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
619 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
620 ok( info
.AllocationProtect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.AllocationProtect
);
621 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
622 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
623 ok( info
.Protect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.Protect
);
625 else win_skip( "no access checks on win9x\n" );
626 UnmapViewOfFile( ptr
);
627 CloseHandle( mapping
);
629 SetLastError(0xdeadbeef);
630 mapping
= OpenFileMappingA( FILE_MAP_WRITE
, FALSE
, name
);
631 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
632 SetLastError(0xdeadbeef);
633 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
637 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
638 SetLastError(0xdeadbeef);
639 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
640 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
641 SetLastError(0xdeadbeef);
642 size
= VirtualQuery( ptr
, &info
, sizeof(info
) );
643 ok( size
== sizeof(info
),
644 "VirtualQuery error %u\n", GetLastError() );
645 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
646 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
647 ok( info
.AllocationProtect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.AllocationProtect
);
648 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
649 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
650 ok( info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
652 else win_skip( "no access checks on win9x\n" );
653 UnmapViewOfFile( ptr
);
654 CloseHandle( mapping
);
658 /* read/write mapping with SEC_RESERVE */
659 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_RESERVE
, 0, MAPPING_SIZE
, NULL
);
660 ok(mapping
!= INVALID_HANDLE_VALUE
, "CreateFileMappingA failed with error %d\n", GetLastError());
662 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
663 ok(ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
665 ptr2
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
666 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
667 ok(ptr2
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
668 trace("mapping same section resulted in views %p and %p\n", ptr
, ptr2
);
670 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
671 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
672 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
673 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
674 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE
, info
.RegionSize
);
675 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
676 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
678 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
679 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
680 ok(info
.Protect
== PAGE_NOACCESS
,
681 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info
.Protect
);
685 ok(info
.AllocationProtect
== PAGE_READWRITE
,
686 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
687 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
688 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
693 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
694 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
695 ok(info
.BaseAddress
== ptr2
,
696 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
697 ok(info
.AllocationBase
== ptr2
,
698 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
699 ok(info
.AllocationProtect
== PAGE_READWRITE
,
700 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
701 ok(info
.RegionSize
== MAPPING_SIZE
,
702 "RegionSize should have been 0x%x but was 0x%lx\n", MAPPING_SIZE
, info
.RegionSize
);
703 ok(info
.State
== MEM_RESERVE
,
704 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
705 ok(info
.Protect
== 0,
706 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
707 ok(info
.Type
== MEM_MAPPED
,
708 "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
711 ptr
= VirtualAlloc(ptr
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
712 ok(ptr
!= NULL
, "VirtualAlloc failed with error %d\n", GetLastError());
714 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
715 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
716 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
717 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
718 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info
.RegionSize
);
719 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_COMMIT instead of 0x%x\n", info
.State
);
720 ok(info
.Protect
== PAGE_READONLY
, "Protect should have been PAGE_READONLY instead of 0x%x\n", info
.Protect
);
721 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
723 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
724 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
728 ok(info
.AllocationProtect
== PAGE_READWRITE
,
729 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
730 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
733 /* shows that the VirtualAlloc above affects the mapping, not just the
734 * virtual memory in this process - it also affects all other processes
735 * with a view of the mapping, but that isn't tested here */
738 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
739 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
740 ok(info
.BaseAddress
== ptr2
,
741 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
742 ok(info
.AllocationBase
== ptr2
,
743 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
744 ok(info
.AllocationProtect
== PAGE_READWRITE
,
745 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
746 ok(info
.RegionSize
== 0x10000,
747 "RegionSize should have been 0x10000 but was 0x%lx\n", info
.RegionSize
);
748 ok(info
.State
== MEM_COMMIT
,
749 "State should have been MEM_COMMIT instead of 0x%x\n", info
.State
);
750 ok(info
.Protect
== PAGE_READWRITE
,
751 "Protect should have been PAGE_READWRITE instead of 0x%x\n", info
.Protect
);
752 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
755 addr
= VirtualAlloc( ptr
, MAPPING_SIZE
, MEM_RESET
, PAGE_READONLY
);
756 ok( addr
== ptr
|| broken(!addr
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
757 "VirtualAlloc failed with error %u\n", GetLastError() );
759 ret
= VirtualFree( ptr
, 0x10000, MEM_DECOMMIT
);
760 ok( !ret
|| broken(ret
) /* win9x */, "VirtualFree succeeded\n" );
762 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "VirtualFree failed with %u\n", GetLastError() );
764 ret
= UnmapViewOfFile(ptr2
);
765 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
766 ret
= UnmapViewOfFile(ptr
);
767 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
768 CloseHandle(mapping
);
770 addr
= VirtualAlloc(NULL
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
771 ok( addr
!= NULL
, "VirtualAlloc failed with error %u\n", GetLastError() );
773 SetLastError(0xdeadbeef);
774 ok( !UnmapViewOfFile(addr
), "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((char *)addr
+ 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
779 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
780 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
781 SetLastError(0xdeadbeef);
782 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
783 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
784 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
786 ok( VirtualFree(addr
, 0, MEM_RELEASE
), "VirtualFree failed\n" );
788 /* close named mapping handle without unmapping */
790 SetLastError(0xdeadbeef);
791 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
792 ok( mapping
!= 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
793 SetLastError(0xdeadbeef);
794 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
795 ok( ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError() );
796 SetLastError(0xdeadbeef);
797 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
798 ok( map2
!= 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
799 SetLastError(0xdeadbeef);
800 ret
= CloseHandle(map2
);
801 ok(ret
, "CloseHandle error %d\n", GetLastError());
802 SetLastError(0xdeadbeef);
803 ret
= CloseHandle(mapping
);
804 ok(ret
, "CloseHandle error %d\n", GetLastError());
806 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
807 ok( !ret
, "memory is not accessible\n" );
809 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
810 ok(ret
, "VirtualQuery error %d\n", GetLastError());
811 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
812 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
813 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
814 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
815 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
816 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
817 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
819 SetLastError(0xdeadbeef);
820 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
822 ok( map2
== 0, "OpenFileMappingA succeeded\n" );
824 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "OpenFileMappingA set error %d\n", GetLastError() );
825 if (map2
) CloseHandle(map2
); /* FIXME: remove once Wine is fixed */
826 SetLastError(0xdeadbeef);
827 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
828 ok( mapping
!= 0, "CreateFileMappingA failed\n" );
830 ok( GetLastError() == ERROR_SUCCESS
, "CreateFileMappingA set error %d\n", GetLastError() );
831 SetLastError(0xdeadbeef);
832 ret
= CloseHandle(mapping
);
833 ok(ret
, "CloseHandle error %d\n", GetLastError());
835 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
836 ok( !ret
, "memory is not accessible\n" );
838 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
839 ok(ret
, "VirtualQuery error %d\n", GetLastError());
840 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
841 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
842 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
843 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
844 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
845 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
846 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
848 SetLastError(0xdeadbeef);
849 ret
= UnmapViewOfFile(ptr
);
850 ok( ret
, "UnmapViewOfFile failed with error %d\n", GetLastError() );
852 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
853 ok( ret
, "memory is accessible\n" );
855 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
856 ok(ret
, "VirtualQuery error %d\n", GetLastError());
857 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
858 ok(info
.Protect
== PAGE_NOACCESS
, "got %#x != expected PAGE_NOACCESS\n", info
.Protect
);
859 ok(info
.AllocationBase
== NULL
, "%p != NULL\n", info
.AllocationBase
);
860 ok(info
.AllocationProtect
== 0, "%#x != 0\n", info
.AllocationProtect
);
861 ok(info
.State
== MEM_FREE
, "%#x != MEM_FREE\n", info
.State
);
862 ok(info
.Type
== 0, "%#x != 0\n", info
.Type
);
864 SetLastError(0xdeadbeef);
865 file
= CreateFileA(testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
866 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
867 SetFilePointer(file
, 4096, NULL
, FILE_BEGIN
);
870 SetLastError(0xdeadbeef);
871 mapping
= CreateFileMappingA(file
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
872 ok( mapping
!= 0, "CreateFileMappingA failed with error %d\n", GetLastError() );
873 SetLastError(0xdeadbeef);
874 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
875 ok( ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError() );
876 SetLastError(0xdeadbeef);
877 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
878 ok( map2
!= 0, "OpenFileMappingA failed with error %d\n", GetLastError() );
879 SetLastError(0xdeadbeef);
880 ret
= CloseHandle(map2
);
881 ok(ret
, "CloseHandle error %d\n", GetLastError());
882 SetLastError(0xdeadbeef);
883 ret
= CloseHandle(mapping
);
884 ok(ret
, "CloseHandle error %d\n", GetLastError());
886 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
887 ok( !ret
, "memory is not accessible\n" );
889 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
890 ok(ret
, "VirtualQuery error %d\n", GetLastError());
891 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
892 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
893 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
894 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
895 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
896 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
897 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
899 SetLastError(0xdeadbeef);
900 map2
= OpenFileMappingA(FILE_MAP_READ
, FALSE
, name
);
902 ok( map2
== 0, "OpenFileMappingA succeeded\n" );
904 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "OpenFileMappingA set error %d\n", GetLastError() );
906 SetLastError(0xdeadbeef);
907 mapping
= CreateFileMappingA(file
, NULL
, PAGE_READWRITE
, 0, MAPPING_SIZE
, name
);
908 ok( mapping
!= 0, "CreateFileMappingA failed\n" );
910 ok( GetLastError() == ERROR_SUCCESS
, "CreateFileMappingA set error %d\n", GetLastError() );
911 SetLastError(0xdeadbeef);
912 ret
= CloseHandle(mapping
);
913 ok(ret
, "CloseHandle error %d\n", GetLastError());
915 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
916 ok( !ret
, "memory is not accessible\n" );
918 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
919 ok(ret
, "VirtualQuery error %d\n", GetLastError());
920 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
921 ok(info
.RegionSize
== MAPPING_SIZE
, "got %#lx != expected %#x\n", info
.RegionSize
, MAPPING_SIZE
);
922 ok(info
.Protect
== PAGE_READWRITE
, "got %#x != expected PAGE_READWRITE\n", info
.Protect
);
923 ok(info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
924 ok(info
.AllocationProtect
== PAGE_READWRITE
, "%#x != PAGE_READWRITE\n", info
.AllocationProtect
);
925 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
926 ok(info
.Type
== MEM_MAPPED
, "%#x != MEM_MAPPED\n", info
.Type
);
928 SetLastError(0xdeadbeef);
929 ret
= UnmapViewOfFile(ptr
);
930 ok( ret
, "UnmapViewOfFile failed with error %d\n", GetLastError() );
932 ret
= IsBadReadPtr(ptr
, MAPPING_SIZE
);
933 ok( ret
, "memory is accessible\n" );
935 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
936 ok(ret
, "VirtualQuery error %d\n", GetLastError());
937 ok(info
.BaseAddress
== ptr
, "got %p != expected %p\n", info
.BaseAddress
, ptr
);
938 ok(info
.Protect
== PAGE_NOACCESS
, "got %#x != expected PAGE_NOACCESS\n", info
.Protect
);
939 ok(info
.AllocationBase
== NULL
, "%p != NULL\n", info
.AllocationBase
);
940 ok(info
.AllocationProtect
== 0, "%#x != 0\n", info
.AllocationProtect
);
941 ok(info
.State
== MEM_FREE
, "%#x != MEM_FREE\n", info
.State
);
942 ok(info
.Type
== 0, "%#x != 0\n", info
.Type
);
945 DeleteFileA(testfile
);
948 static void test_NtMapViewOfSection(void)
952 static const char testfile
[] = "testfile.xxx";
953 static const char data
[] = "test data for NtMapViewOfSection";
954 char buffer
[sizeof(data
)];
955 HANDLE file
, mapping
;
958 DWORD status
, written
;
960 LARGE_INTEGER offset
;
962 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
964 win_skip( "NtMapViewOfSection not available\n" );
968 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
969 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
970 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
971 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
972 SetEndOfFile( file
);
974 /* read/write mapping */
976 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
977 ok( mapping
!= 0, "CreateFileMapping failed\n" );
979 hProcess
= create_target_process("sleep");
980 ok(hProcess
!= NULL
, "Can't start process\n");
985 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
986 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
988 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
989 ok( ret
, "ReadProcessMemory failed\n" );
990 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
991 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
993 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
994 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
996 CloseHandle( mapping
);
998 DeleteFileA( testfile
);
1000 TerminateProcess(hProcess
, 0);
1001 CloseHandle(hProcess
);
1004 static void test_NtAreMappedFilesTheSame(void)
1006 static const char testfile
[] = "testfile.xxx";
1007 HANDLE file
, file2
, mapping
, map2
;
1010 char path
[MAX_PATH
];
1012 if (!pNtAreMappedFilesTheSame
)
1014 win_skip( "NtAreMappedFilesTheSame not available\n" );
1018 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1019 NULL
, CREATE_ALWAYS
, 0, 0 );
1020 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1021 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
1022 SetEndOfFile( file
);
1024 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
1025 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1027 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1028 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1030 file2
= CreateFileA( testfile
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1031 NULL
, OPEN_EXISTING
, 0, 0 );
1032 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1034 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1035 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1036 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
1037 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1038 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1039 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1040 UnmapViewOfFile( ptr2
);
1042 ptr2
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1043 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1044 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1045 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1046 UnmapViewOfFile( ptr2
);
1047 CloseHandle( map2
);
1049 map2
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1050 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1051 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
1052 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1053 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1054 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1055 UnmapViewOfFile( ptr2
);
1056 CloseHandle( map2
);
1057 CloseHandle( file2
);
1059 status
= pNtAreMappedFilesTheSame( ptr
, ptr
);
1060 ok( status
== STATUS_SUCCESS
|| broken(status
== STATUS_NOT_SAME_DEVICE
),
1061 "NtAreMappedFilesTheSame returned %x\n", status
);
1063 status
= pNtAreMappedFilesTheSame( ptr
, (char *)ptr
+ 30 );
1064 ok( status
== STATUS_SUCCESS
|| broken(status
== STATUS_NOT_SAME_DEVICE
),
1065 "NtAreMappedFilesTheSame returned %x\n", status
);
1067 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1068 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1070 status
= pNtAreMappedFilesTheSame( ptr
, (void *)0xdeadbeef );
1071 ok( status
== STATUS_CONFLICTING_ADDRESSES
|| status
== STATUS_INVALID_ADDRESS
,
1072 "NtAreMappedFilesTheSame returned %x\n", status
);
1074 status
= pNtAreMappedFilesTheSame( ptr
, NULL
);
1075 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1077 status
= pNtAreMappedFilesTheSame( ptr
, (void *)GetProcessHeap() );
1078 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
1080 status
= pNtAreMappedFilesTheSame( NULL
, NULL
);
1081 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1083 ptr2
= VirtualAlloc( NULL
, 0x10000, MEM_COMMIT
, PAGE_READWRITE
);
1084 ok( ptr2
!= NULL
, "VirtualAlloc error %u\n", GetLastError() );
1085 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1086 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
1087 VirtualFree( ptr2
, 0, MEM_RELEASE
);
1089 UnmapViewOfFile( ptr
);
1090 CloseHandle( mapping
);
1091 CloseHandle( file
);
1093 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
1094 GetModuleHandleA("kernel32.dll") );
1095 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1096 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1097 GetModuleHandleA("kernel32.dll") );
1098 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1099 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
1100 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
1101 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1103 GetSystemDirectoryA( path
, MAX_PATH
);
1104 strcat( path
, "\\kernel32.dll" );
1105 file
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1106 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1108 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
1109 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1110 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
1111 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1112 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1113 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
1114 UnmapViewOfFile( ptr
);
1115 CloseHandle( mapping
);
1117 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
1118 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1119 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
1120 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1121 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
1123 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1125 file2
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1126 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
1127 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
1128 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
1129 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 0 );
1130 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
1131 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
1132 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
1133 UnmapViewOfFile( ptr2
);
1134 CloseHandle( map2
);
1135 CloseHandle( file2
);
1137 UnmapViewOfFile( ptr
);
1138 CloseHandle( mapping
);
1140 CloseHandle( file
);
1141 DeleteFileA( testfile
);
1144 static void test_CreateFileMapping(void)
1146 HANDLE handle
, handle2
;
1148 /* test case sensitivity */
1150 SetLastError(0xdeadbeef);
1151 handle
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1152 "Wine Test Mapping");
1153 ok( handle
!= NULL
, "CreateFileMapping failed with error %u\n", GetLastError());
1154 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1156 SetLastError(0xdeadbeef);
1157 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1158 "Wine Test Mapping");
1159 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1160 ok( GetLastError() == ERROR_ALREADY_EXISTS
, "wrong error %u\n", GetLastError());
1161 CloseHandle( handle2
);
1163 SetLastError(0xdeadbeef);
1164 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1165 "WINE TEST MAPPING");
1166 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1167 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1168 CloseHandle( handle2
);
1170 SetLastError(0xdeadbeef);
1171 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "Wine Test Mapping");
1172 ok( handle2
!= NULL
, "OpenFileMapping failed with error %d\n", GetLastError());
1173 CloseHandle( handle2
);
1175 SetLastError(0xdeadbeef);
1176 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "WINE TEST MAPPING");
1177 ok( !handle2
, "OpenFileMapping succeeded\n");
1178 ok( GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_INVALID_NAME
/* win9x */,
1179 "wrong error %u\n", GetLastError());
1181 CloseHandle( handle
);
1184 static void test_IsBadReadPtr(void)
1187 void *ptr
= (void *)0xdeadbeef;
1190 ret
= IsBadReadPtr(NULL
, 0);
1191 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1193 ret
= IsBadReadPtr(NULL
, 1);
1194 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1196 ret
= IsBadReadPtr(ptr
, 0);
1197 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1199 ret
= IsBadReadPtr(ptr
, 1);
1200 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1202 ret
= IsBadReadPtr(&stackvar
, 0);
1203 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1205 ret
= IsBadReadPtr(&stackvar
, sizeof(char));
1206 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1209 static void test_IsBadWritePtr(void)
1212 void *ptr
= (void *)0xdeadbeef;
1215 ret
= IsBadWritePtr(NULL
, 0);
1216 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1218 ret
= IsBadWritePtr(NULL
, 1);
1219 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1221 ret
= IsBadWritePtr(ptr
, 0);
1222 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1224 ret
= IsBadWritePtr(ptr
, 1);
1225 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1227 ret
= IsBadWritePtr(&stackval
, 0);
1228 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1230 ret
= IsBadWritePtr(&stackval
, sizeof(char));
1231 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1234 static void test_IsBadCodePtr(void)
1237 void *ptr
= (void *)0xdeadbeef;
1240 ret
= IsBadCodePtr(NULL
);
1241 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1243 ret
= IsBadCodePtr(ptr
);
1244 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1246 ret
= IsBadCodePtr((void *)&stackval
);
1247 ok(ret
== FALSE
, "Expected IsBadCodePtr to return FALSE, got %d\n", ret
);
1250 static void test_write_watch(void)
1253 DWORD ret
, size
, old_prot
;
1254 MEMORY_BASIC_INFORMATION info
;
1259 if (!pGetWriteWatch
|| !pResetWriteWatch
)
1261 win_skip( "GetWriteWatch not supported\n" );
1266 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1268 (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
1270 win_skip( "MEM_WRITE_WATCH not supported\n" );
1273 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1274 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1275 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1276 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1277 ok( info
.AllocationProtect
== PAGE_READWRITE
, "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1278 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1279 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1280 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1281 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1284 SetLastError( 0xdeadbeef );
1285 ret
= pGetWriteWatch( 0, NULL
, size
, results
, &count
, &pagesize
);
1286 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1287 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
1288 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1289 "wrong error %u\n", GetLastError() );
1291 SetLastError( 0xdeadbeef );
1292 ret
= pGetWriteWatch( 0, GetModuleHandleW(NULL
), size
, results
, &count
, &pagesize
);
1295 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1296 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1300 ok( count
== 0, "wrong count %lu\n", count
);
1303 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1304 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1305 ok( count
== 0, "wrong count %lu\n", count
);
1307 base
[pagesize
+ 1] = 0x44;
1310 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1311 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1312 ok( count
== 1, "wrong count %lu\n", count
);
1313 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1316 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1317 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1318 ok( count
== 1, "wrong count %lu\n", count
);
1319 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1322 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1323 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1324 ok( count
== 0, "wrong count %lu\n", count
);
1326 base
[2*pagesize
+ 3] = 0x11;
1327 base
[4*pagesize
+ 8] = 0x11;
1330 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1331 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1332 ok( count
== 2, "wrong count %lu\n", count
);
1333 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1334 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1337 ret
= pGetWriteWatch( 0, base
+ 3*pagesize
, 2*pagesize
, results
, &count
, &pagesize
);
1338 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1339 ok( count
== 1, "wrong count %lu\n", count
);
1340 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1342 ret
= pResetWriteWatch( base
, 3*pagesize
);
1343 ok( !ret
, "pResetWriteWatch failed %u\n", GetLastError() );
1346 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1347 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1348 ok( count
== 1, "wrong count %lu\n", count
);
1349 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1351 *(DWORD
*)(base
+ 2*pagesize
- 2) = 0xdeadbeef;
1354 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1355 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1356 ok( count
== 3, "wrong count %lu\n", count
);
1357 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1358 ok( results
[1] == base
+ 2*pagesize
, "wrong result %p\n", results
[1] );
1359 ok( results
[2] == base
+ 4*pagesize
, "wrong result %p\n", results
[2] );
1362 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1363 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1364 ok( count
== 1, "wrong count %lu\n", count
);
1365 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1368 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1369 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1370 ok( count
== 2, "wrong count %lu\n", count
);
1371 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1372 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1374 /* changing protections doesn't affect watches */
1376 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READONLY
, &old_prot
);
1377 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1378 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1380 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1381 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1382 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1383 ok( info
.RegionSize
== 3*pagesize
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1384 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1385 ok( info
.Protect
== PAGE_READONLY
, "wrong Protect 0x%x\n", info
.Protect
);
1387 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READWRITE
, &old_prot
);
1388 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1389 ok( old_prot
== PAGE_READONLY
, "wrong old prot %x\n", old_prot
);
1392 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1393 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1394 ok( count
== 2, "wrong count %lu\n", count
);
1395 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1396 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1398 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1399 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1400 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1401 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1402 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1403 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1405 /* some invalid parameter tests */
1407 SetLastError( 0xdeadbeef );
1409 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1412 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1413 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1415 SetLastError( 0xdeadbeef );
1416 ret
= pGetWriteWatch( 0, base
, size
, results
, NULL
, &pagesize
);
1417 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1418 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1420 SetLastError( 0xdeadbeef );
1422 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, NULL
);
1423 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1424 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1426 SetLastError( 0xdeadbeef );
1428 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1429 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1430 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1432 SetLastError( 0xdeadbeef );
1434 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1435 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1436 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1438 SetLastError( 0xdeadbeef );
1440 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1441 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1442 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1444 SetLastError( 0xdeadbeef );
1446 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1447 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1448 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1450 SetLastError( 0xdeadbeef );
1452 ret
= pGetWriteWatch( 0, base
, size
* 2, results
, &count
, &pagesize
);
1453 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1454 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1456 SetLastError( 0xdeadbeef );
1458 ret
= pGetWriteWatch( 0, base
+ size
- pagesize
, pagesize
+ 1, results
, &count
, &pagesize
);
1459 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1460 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1462 SetLastError( 0xdeadbeef );
1463 ret
= pResetWriteWatch( base
, 0 );
1464 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1465 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1467 SetLastError( 0xdeadbeef );
1468 ret
= pResetWriteWatch( GetModuleHandleW(NULL
), size
);
1469 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1470 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1472 else /* win98 is completely different */
1474 SetLastError( 0xdeadbeef );
1476 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1477 ok( ret
== ERROR_INVALID_PARAMETER
, "GetWriteWatch succeeded %u\n", ret
);
1478 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1481 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1482 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1485 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1486 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1489 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1490 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1492 ret
= pResetWriteWatch( base
, 0 );
1493 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1495 ret
= pResetWriteWatch( GetModuleHandleW(NULL
), size
);
1496 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1499 VirtualFree( base
, 0, MEM_FREE
);
1501 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1502 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1503 VirtualFree( base
, 0, MEM_FREE
);
1505 base
= VirtualAlloc( 0, size
, MEM_WRITE_WATCH
, PAGE_READWRITE
);
1506 ok( !base
, "VirtualAlloc succeeded\n" );
1507 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1509 /* initial protect doesn't matter */
1511 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_NOACCESS
);
1512 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1513 base
= VirtualAlloc( base
, size
, MEM_COMMIT
, PAGE_NOACCESS
);
1514 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1517 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1518 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1519 ok( count
== 0, "wrong count %lu\n", count
);
1521 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_READWRITE
, &old_prot
);
1522 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1523 ok( old_prot
== PAGE_NOACCESS
, "wrong old prot %x\n", old_prot
);
1525 base
[5*pagesize
+ 200] = 3;
1527 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_NOACCESS
, &old_prot
);
1528 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1529 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1532 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1533 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1534 ok( count
== 1, "wrong count %lu\n", count
);
1535 ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1537 ret
= VirtualFree( base
, size
, MEM_DECOMMIT
);
1538 ok( ret
, "VirtualFree failed %u\n", GetLastError() );
1541 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1542 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1543 ok( count
== 1 || broken(count
== 0), /* win98 */
1544 "wrong count %lu\n", count
);
1545 if (count
) ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1547 VirtualFree( base
, 0, MEM_FREE
);
1552 static DWORD num_guard_page_calls
;
1554 static DWORD
guard_page_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
1555 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
1557 trace( "exception: %08x flags:%x addr:%p\n",
1558 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
1560 ok( rec
->NumberParameters
== 2, "NumberParameters is %d instead of 2\n", rec
->NumberParameters
);
1561 ok( rec
->ExceptionCode
== STATUS_GUARD_PAGE_VIOLATION
, "ExceptionCode is %08x instead of %08x\n",
1562 rec
->ExceptionCode
, STATUS_GUARD_PAGE_VIOLATION
);
1564 num_guard_page_calls
++;
1565 *(int *)rec
->ExceptionInformation
[1] += 0x100;
1567 return ExceptionContinueExecution
;
1570 static void test_guard_page(void)
1572 EXCEPTION_REGISTRATION_RECORD frame
;
1573 MEMORY_BASIC_INFORMATION info
;
1574 DWORD ret
, size
, old_prot
;
1575 int *value
, old_value
;
1584 win_skip( "NtCurrentTeb not supported\n" );
1589 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
| PAGE_GUARD
);
1590 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1591 value
= (int *)base
;
1593 /* verify info structure */
1594 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1595 ok( ret
, "VirtualQuery failed %u\n", GetLastError());
1596 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1597 ok( info
.AllocationProtect
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1598 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1599 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1600 ok( info
.Protect
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong Protect 0x%x\n", info
.Protect
);
1601 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1603 /* put some initial value into the memory */
1604 success
= VirtualProtect( base
, size
, PAGE_READWRITE
, &old_prot
);
1605 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1606 ok( old_prot
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong old prot %x\n", old_prot
);
1611 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1612 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1613 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1615 /* test behaviour of VirtualLock - first attempt should fail */
1616 SetLastError( 0xdeadbeef );
1617 success
= VirtualLock( base
, size
);
1618 ok( !success
, "VirtualLock unexpectedly succeded\n" );
1620 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION
, "wrong error %u\n", GetLastError() );
1622 success
= VirtualLock( base
, size
);
1624 ok( success
, "VirtualLock failed %u\n", GetLastError() );
1627 ok( *value
== 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value
);
1628 success
= VirtualUnlock( base
, size
);
1629 ok( success
, "VirtualUnlock failed %u\n", GetLastError() );
1632 /* check info structure again, PAGE_GUARD should be removed now */
1633 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1634 ok( ret
, "VirtualQuery failed %u\n", GetLastError());
1635 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1636 ok( info
.AllocationProtect
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1637 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1638 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1640 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1641 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1643 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1644 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1646 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1648 /* test directly accessing the memory - we need to setup an exception handler first */
1649 frame
.Handler
= guard_page_handler
;
1650 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
1651 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
1653 num_guard_page_calls
= 0;
1654 old_value
= *value
; /* exception handler increments value by 0x100 */
1656 ok( old_value
== 0x101, "memory block contains wrong value, expected 0x101, got 0x%x\n", old_value
);
1657 ok( num_guard_page_calls
== 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls
);
1659 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
1661 /* check info structure again, PAGE_GUARD should be removed now */
1662 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1663 ok( ret
, "VirtualQuery failed %u\n", GetLastError());
1664 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1666 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1667 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1668 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1670 /* test accessing second integer in memory */
1671 frame
.Handler
= guard_page_handler
;
1672 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
1673 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
1675 num_guard_page_calls
= 0;
1676 old_value
= *(value
+ 1);
1677 ok( old_value
== 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value
);
1678 ok( *value
== 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value
);
1679 ok( num_guard_page_calls
== 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls
);
1681 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
1683 success
= VirtualLock( base
, size
);
1684 ok( success
, "VirtualLock failed %u\n", GetLastError() );
1687 ok( *value
== 2, "memory block contains wrong value, expected 2, got 0x%x\n", *value
);
1688 success
= VirtualUnlock( base
, size
);
1689 ok( success
, "VirtualUnlock failed %u\n", GetLastError() );
1692 VirtualFree( base
, 0, MEM_FREE
);
1694 /* combined guard page / write watch tests */
1695 if (!pGetWriteWatch
|| !pResetWriteWatch
)
1697 win_skip( "GetWriteWatch not supported, skipping combined guard page / write watch tests\n" );
1701 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
| PAGE_GUARD
);
1702 if (!base
&& (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
1704 win_skip( "MEM_WRITE_WATCH not supported\n" );
1707 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1708 value
= (int *)base
;
1710 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1711 ok( ret
, "VirtualQuery failed %u\n", GetLastError() );
1712 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1713 ok( info
.AllocationProtect
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1714 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1715 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1716 ok( info
.Protect
== (PAGE_READWRITE
| PAGE_GUARD
), "wrong Protect 0x%x\n", info
.Protect
);
1717 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1720 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1721 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1722 ok( count
== 0, "wrong count %lu\n", count
);
1724 /* writing to a page should trigger should trigger guard page, even if write watch is set */
1725 frame
.Handler
= guard_page_handler
;
1726 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
1727 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
1729 num_guard_page_calls
= 0;
1732 ok( num_guard_page_calls
== 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls
);
1734 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
1737 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1738 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1739 ok( count
== 1, "wrong count %lu\n", count
);
1740 ok( results
[0] == base
, "wrong result %p\n", results
[0] );
1742 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1743 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1745 /* write watch is triggered from inside of the guard page handler */
1746 frame
.Handler
= guard_page_handler
;
1747 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
1748 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
1750 num_guard_page_calls
= 0;
1751 old_value
= *(value
+ 1); /* doesn't trigger write watch */
1752 ok( old_value
== 0x102, "memory block contains wrong value, expected 0x102, got 0x%x\n", old_value
);
1753 ok( *value
== 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value
);
1754 ok( num_guard_page_calls
== 1, "expected one callback of guard page handler, got %d calls\n", num_guard_page_calls
);
1756 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
1759 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1760 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1761 ok( count
== 1, "wrong count %lu\n", count
);
1762 ok( results
[0] == base
, "wrong result %p\n", results
[0] );
1764 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1765 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1767 /* test behaviour of VirtualLock - first attempt should fail without triggering write watches */
1768 SetLastError( 0xdeadbeef );
1769 success
= VirtualLock( base
, size
);
1770 ok( !success
, "VirtualLock unexpectedly succeded\n" );
1772 ok( GetLastError() == STATUS_GUARD_PAGE_VIOLATION
, "wrong error %u\n", GetLastError() );
1775 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1776 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1777 ok( count
== 0, "wrong count %lu\n", count
);
1779 success
= VirtualLock( base
, size
);
1781 ok( success
, "VirtualLock failed %u\n", GetLastError() );
1784 ok( *value
== 1, "memory block contains wrong value, expected 1, got 0x%x\n", *value
);
1785 success
= VirtualUnlock( base
, size
);
1786 ok( success
, "VirtualUnlock failed %u\n", GetLastError() );
1790 results
[0] = (void *)0xdeadbeef;
1791 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1792 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1794 ok( count
== 1 || broken(count
== 0) /* Windows 8 */, "wrong count %lu\n", count
);
1796 ok( results
[0] == base
|| broken(results
[0] == (void *)0xdeadbeef) /* Windows 8 */, "wrong result %p\n", results
[0] );
1798 VirtualFree( base
, 0, MEM_FREE
);
1801 DWORD num_execute_fault_calls
;
1803 static DWORD
execute_fault_seh_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
1804 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
1806 ULONG flags
= MEM_EXECUTE_OPTION_ENABLE
;
1808 trace( "exception: %08x flags:%x addr:%p info[0]:%ld info[1]:%p\n",
1809 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
1810 rec
->ExceptionInformation
[0], (void *)rec
->ExceptionInformation
[1] );
1812 ok( rec
->NumberParameters
== 2, "NumberParameters is %d instead of 2\n", rec
->NumberParameters
);
1813 ok( rec
->ExceptionCode
== STATUS_ACCESS_VIOLATION
|| rec
->ExceptionCode
== STATUS_GUARD_PAGE_VIOLATION
,
1814 "ExceptionCode is %08x instead of STATUS_ACCESS_VIOLATION or STATUS_GUARD_PAGE_VIOLATION\n", rec
->ExceptionCode
);
1816 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
, &flags
, sizeof(flags
), NULL
);
1818 if (rec
->ExceptionCode
== STATUS_GUARD_PAGE_VIOLATION
)
1821 ok( rec
->ExceptionInformation
[0] == EXCEPTION_EXECUTE_FAULT
||
1822 broken(!(flags
& MEM_EXECUTE_OPTION_DISABLE
) && rec
->ExceptionInformation
[0] == EXCEPTION_READ_FAULT
), /* Windows 2000 */
1823 "ExceptionInformation[0] is %d instead of %d\n", (DWORD
)rec
->ExceptionInformation
[0], EXCEPTION_EXECUTE_FAULT
);
1825 num_guard_page_calls
++;
1827 else if (rec
->ExceptionCode
== STATUS_ACCESS_VIOLATION
)
1829 DWORD err
, old_prot
;
1832 err
= (flags
& MEM_EXECUTE_OPTION_DISABLE
) ? EXCEPTION_EXECUTE_FAULT
: EXCEPTION_READ_FAULT
;
1833 ok( rec
->ExceptionInformation
[0] == err
, "ExceptionInformation[0] is %d instead of %d\n",
1834 (DWORD
)rec
->ExceptionInformation
[0], err
);
1836 success
= VirtualProtect( (void *)rec
->ExceptionInformation
[1], 16, PAGE_EXECUTE_READWRITE
, &old_prot
);
1837 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1838 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1840 num_execute_fault_calls
++;
1843 return ExceptionContinueExecution
;
1846 static inline DWORD
send_message_excpt( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1848 EXCEPTION_REGISTRATION_RECORD frame
;
1851 frame
.Handler
= execute_fault_seh_handler
;
1852 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
1853 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
1855 num_guard_page_calls
= num_execute_fault_calls
= 0;
1856 ret
= SendMessageA( hWnd
, WM_USER
, 0, 0 );
1858 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
1863 static LRESULT CALLBACK
jmp_test_func( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1865 if (uMsg
== WM_USER
)
1868 return DefWindowProcA( hWnd
, uMsg
, wParam
, lParam
);
1871 static LRESULT CALLBACK
atl_test_func( HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1873 DWORD arg
= (DWORD
)hWnd
;
1874 ok( arg
== 0x11223344, "arg is 0x%08x instead of 0x11223344\n", arg
);
1878 static void test_atl_thunk_emulation( ULONG dep_flags
)
1880 static const char code_jmp
[] = {0xE9, 0x00, 0x00, 0x00, 0x00};
1881 static const char code_atl
[] = {0xC7, 0x44, 0x24, 0x04, 0x44, 0x33, 0x22, 0x11, 0xE9, 0x00, 0x00, 0x00, 0x00};
1882 static const char cls_name
[] = "atl_thunk_class";
1883 DWORD ret
, size
, old_prot
;
1884 ULONG old_flags
= MEM_EXECUTE_OPTION_ENABLE
;
1895 win_skip( "NtCurrentTeb not supported\n" );
1899 trace( "Running DEP tests with ProcessExecuteFlags = %d\n", dep_flags
);
1901 NtQueryInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
, &old_flags
, sizeof(old_flags
), NULL
);
1902 if (old_flags
!= dep_flags
)
1904 ret
= NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
, &dep_flags
, sizeof(dep_flags
) );
1905 if (ret
== STATUS_INVALID_INFO_CLASS
) /* Windows 2000 */
1907 win_skip( "Skipping DEP tests with ProcessExecuteFlags = %d\n", dep_flags
);
1910 ok( !ret
, "NtSetInformationProcess failed with status %08x\n", ret
);
1914 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
);
1915 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1917 memcpy( base
, code_jmp
, sizeof(code_jmp
) );
1918 *(DWORD
*)(base
+ 1) = (DWORD_PTR
)jmp_test_func
- (DWORD_PTR
)(base
+ 5);
1920 /* On Windows, the ATL Thunk emulation is only enabled while running WndProc functions,
1921 * whereas in Wine such a limitation doesn't exist yet. We want to test in a scenario
1922 * where it is active, so that application which depend on that still work properly.
1923 * We have no exception handler enabled yet, so give proper EXECUTE permissions to
1924 * prevent crashes while creating the window. */
1926 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
1927 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1929 memset( &wc
, 0, sizeof(wc
) );
1930 wc
.cbSize
= sizeof(wc
);
1931 wc
.style
= CS_VREDRAW
| CS_HREDRAW
;
1932 wc
.hInstance
= GetModuleHandleA( 0 );
1933 wc
.hCursor
= LoadCursorA( NULL
, (LPCSTR
)IDC_ARROW
);
1934 wc
.hbrBackground
= NULL
;
1935 wc
.lpszClassName
= cls_name
;
1936 wc
.lpfnWndProc
= (WNDPROC
)base
;
1937 success
= RegisterClassExA(&wc
) != 0;
1938 ok( success
, "RegisterClassExA failed %u\n", GetLastError() );
1940 hWnd
= CreateWindowExA(0, cls_name
, "Test", WS_TILEDWINDOW
, 0, 0, 640, 480, 0, 0, 0, 0);
1941 ok( hWnd
!= 0, "CreateWindowExA failed %u\n", GetLastError() );
1943 ret
= SendMessageA(hWnd
, WM_USER
, 0, 0);
1944 ok( ret
== 42, "SendMessage returned unexpected result %d\n", ret
);
1946 /* At first try with an instruction which is not recognized as proper ATL thunk
1947 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
1948 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
1950 success
= VirtualProtect( base
, size
, PAGE_READWRITE
, &old_prot
);
1951 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1953 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
1954 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
1955 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
1956 if (dep_flags
& MEM_EXECUTE_OPTION_DISABLE
)
1957 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
1959 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
1961 /* Now a bit more complicated, the page containing the code is protected with
1962 * PAGE_GUARD memory protection. */
1964 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
1965 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1967 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
1968 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
1969 ok( num_guard_page_calls
== 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
1970 if (dep_flags
& MEM_EXECUTE_OPTION_DISABLE
)
1971 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
1973 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
1975 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
1976 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
1977 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
1978 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
1980 /* Now test with a proper ATL thunk instruction. */
1982 memcpy( base
, code_atl
, sizeof(code_atl
) );
1983 *(DWORD
*)(base
+ 9) = (DWORD_PTR
)atl_test_func
- (DWORD_PTR
)(base
+ 13);
1985 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
1986 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1988 ret
= SendMessageA(hWnd
, WM_USER
, 0, 0);
1989 ok( ret
== 43, "SendMessage returned unexpected result %d\n", ret
);
1991 /* Try executing with PAGE_READWRITE protection. */
1993 success
= VirtualProtect( base
, size
, PAGE_READWRITE
, &old_prot
);
1994 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
1996 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
1997 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
1998 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
1999 if ((dep_flags
& MEM_EXECUTE_OPTION_DISABLE
) && (dep_flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
))
2001 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2003 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2005 /* Now a bit more complicated, the page containing the code is protected with
2006 * PAGE_GUARD memory protection. */
2008 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
2009 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2011 /* the same, but with PAGE_GUARD set */
2012 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2013 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2014 ok( num_guard_page_calls
== 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2015 if ((dep_flags
& MEM_EXECUTE_OPTION_DISABLE
) && (dep_flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
))
2017 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2019 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2021 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2022 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2023 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2024 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2026 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2028 memcpy( base
, code_jmp
, sizeof(code_jmp
) );
2029 *(DWORD
*)(base
+ 1) = (DWORD_PTR
)jmp_test_func
- (DWORD_PTR
)(base
+ 5);
2031 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
2032 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2034 DestroyWindow( hWnd
);
2036 success
= UnregisterClassA( cls_name
, GetModuleHandleA(0) );
2037 ok( success
, "UnregisterClass failed %u\n", GetLastError() );
2039 VirtualFree( base
, 0, MEM_FREE
);
2041 /* Repeat the tests from above with MEM_WRITE_WATCH protected memory. */
2043 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
2044 if (!base
&& (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
2046 win_skip( "MEM_WRITE_WATCH not supported\n" );
2049 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
2052 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2053 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2054 ok( count
== 0, "wrong count %lu\n", count
);
2056 memcpy( base
, code_jmp
, sizeof(code_jmp
) );
2057 *(DWORD
*)(base
+ 1) = (DWORD_PTR
)jmp_test_func
- (DWORD_PTR
)(base
+ 5);
2060 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2061 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2062 ok( count
== 1, "wrong count %lu\n", count
);
2063 ok( results
[0] == base
, "wrong result %p\n", results
[0] );
2065 /* Create a new window class and associcated Window (see above) */
2067 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
2068 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2070 memset( &wc
, 0, sizeof(wc
) );
2071 wc
.cbSize
= sizeof(wc
);
2072 wc
.style
= CS_VREDRAW
| CS_HREDRAW
;
2073 wc
.hInstance
= GetModuleHandleA( 0 );
2074 wc
.hCursor
= LoadCursorA( NULL
, (LPCSTR
)IDC_ARROW
);
2075 wc
.hbrBackground
= NULL
;
2076 wc
.lpszClassName
= cls_name
;
2077 wc
.lpfnWndProc
= (WNDPROC
)base
;
2078 success
= RegisterClassExA(&wc
) != 0;
2079 ok( success
, "RegisterClassExA failed %u\n", GetLastError() );
2081 hWnd
= CreateWindowExA(0, cls_name
, "Test", WS_TILEDWINDOW
, 0, 0, 640, 480, 0, 0, 0, 0);
2082 ok( hWnd
!= 0, "CreateWindowExA failed %u\n", GetLastError() );
2084 ret
= SendMessageA(hWnd
, WM_USER
, 0, 0);
2085 ok( ret
== 42, "SendMessage returned unexpected result %d\n", ret
);
2088 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2089 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2090 ok( count
== 0, "wrong count %lu\n", count
);
2092 /* At first try with an instruction which is not recognized as proper ATL thunk
2093 * by the Windows ATL Thunk Emulator. Removing execute permissions will lead to
2094 * STATUS_ACCESS_VIOLATION exceptions when DEP is enabled. */
2096 success
= VirtualProtect( base
, size
, PAGE_READWRITE
, &old_prot
);
2097 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2099 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2100 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
2101 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2102 if (dep_flags
& MEM_EXECUTE_OPTION_DISABLE
)
2103 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2105 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2108 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2109 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2110 ok( count
== 0, "wrong count %lu\n", count
);
2112 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2113 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
2114 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2115 if (dep_flags
& MEM_EXECUTE_OPTION_DISABLE
)
2116 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2119 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2121 /* Now a bit more complicated, the page containing the code is protected with
2122 * PAGE_GUARD memory protection. */
2124 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
2125 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2127 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2128 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
2129 ok( num_guard_page_calls
== 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2130 if (dep_flags
& MEM_EXECUTE_OPTION_DISABLE
)
2131 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2133 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2135 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2136 ok( ret
== 42, "call returned wrong result, expected 42, got %d\n", ret
);
2137 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2138 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2141 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2142 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2143 ok( count
== 0 || broken(count
== 1) /* Windows 8 */, "wrong count %lu\n", count
);
2145 /* Now test with a proper ATL thunk instruction. */
2147 memcpy( base
, code_atl
, sizeof(code_atl
) );
2148 *(DWORD
*)(base
+ 9) = (DWORD_PTR
)atl_test_func
- (DWORD_PTR
)(base
+ 13);
2151 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2152 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2153 ok( count
== 1, "wrong count %lu\n", count
);
2154 ok( results
[0] == base
, "wrong result %p\n", results
[0] );
2156 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
2157 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2159 ret
= SendMessageA(hWnd
, WM_USER
, 0, 0);
2160 ok( ret
== 43, "SendMessage returned unexpected result %d\n", ret
);
2162 /* Try executing with PAGE_READWRITE protection. */
2164 success
= VirtualProtect( base
, size
, PAGE_READWRITE
, &old_prot
);
2165 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2167 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2168 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2169 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2170 if ((dep_flags
& MEM_EXECUTE_OPTION_DISABLE
) && (dep_flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
))
2172 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2174 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2177 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2178 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2179 ok( count
== 0, "wrong count %lu\n", count
);
2181 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2182 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2183 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2184 if ((dep_flags
& MEM_EXECUTE_OPTION_DISABLE
) && (dep_flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
))
2185 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2187 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2189 /* Now a bit more complicated, the page containing the code is protected with
2190 * PAGE_GUARD memory protection. */
2192 success
= VirtualProtect( base
, size
, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
2193 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2195 /* the same, but with PAGE_GUARD set */
2196 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2197 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2198 ok( num_guard_page_calls
== 1, "expected one STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2199 if ((dep_flags
& MEM_EXECUTE_OPTION_DISABLE
) && (dep_flags
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
))
2201 ok( num_execute_fault_calls
== 1, "expected one STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2203 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2205 ret
= send_message_excpt( hWnd
, WM_USER
, 0, 0 );
2206 ok( ret
== 43, "call returned wrong result, expected 43, got %d\n", ret
);
2207 ok( num_guard_page_calls
== 0, "expected no STATUS_GUARD_PAGE_VIOLATION exception, got %d exceptions\n", num_guard_page_calls
);
2208 ok( num_execute_fault_calls
== 0, "expected no STATUS_ACCESS_VIOLATION exception, got %d exceptions\n", num_execute_fault_calls
);
2211 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2212 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2213 ok( count
== 0 || broken(count
== 1) /* Windows 8 */, "wrong count %lu\n", count
);
2215 /* Restore the JMP instruction, set to executable, and then destroy the Window */
2217 memcpy( base
, code_jmp
, sizeof(code_jmp
) );
2218 *(DWORD
*)(base
+ 1) = (DWORD_PTR
)jmp_test_func
- (DWORD_PTR
)(base
+ 5);
2221 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
2222 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
2223 ok( count
== 1, "wrong count %lu\n", count
);
2224 ok( results
[0] == base
, "wrong result %p\n", results
[0] );
2226 success
= VirtualProtect( base
, size
, PAGE_EXECUTE_READWRITE
, &old_prot
);
2227 ok( success
, "VirtualProtect failed %u\n", GetLastError() );
2229 DestroyWindow( hWnd
);
2231 success
= UnregisterClassA( cls_name
, GetModuleHandleA(0) );
2232 ok( success
, "UnregisterClass failed %u\n", GetLastError() );
2234 VirtualFree( base
, 0, MEM_FREE
);
2237 if (old_flags
!= dep_flags
)
2239 ret
= NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
, &old_flags
, sizeof(old_flags
) );
2240 ok( !ret
, "NtSetInformationProcess failed with status %08x\n", ret
);
2244 #endif /* __i386__ */
2246 static void test_VirtualProtect(void)
2248 static const struct test_data
2250 DWORD prot_set
, prot_get
;
2253 { 0, 0 }, /* 0x00 */
2254 { PAGE_NOACCESS
, PAGE_NOACCESS
}, /* 0x01 */
2255 { PAGE_READONLY
, PAGE_READONLY
}, /* 0x02 */
2256 { PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x03 */
2257 { PAGE_READWRITE
, PAGE_READWRITE
}, /* 0x04 */
2258 { PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x05 */
2259 { PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x06 */
2260 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x07 */
2261 { PAGE_WRITECOPY
, 0 }, /* 0x08 */
2262 { PAGE_WRITECOPY
| PAGE_NOACCESS
, 0 }, /* 0x09 */
2263 { PAGE_WRITECOPY
| PAGE_READONLY
, 0 }, /* 0x0a */
2264 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, 0 }, /* 0x0b */
2265 { PAGE_WRITECOPY
| PAGE_READWRITE
, 0 }, /* 0x0c */
2266 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x0d */
2267 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x0e */
2268 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x0f */
2270 { PAGE_EXECUTE
, PAGE_EXECUTE
}, /* 0x10 */
2271 { PAGE_EXECUTE_READ
, PAGE_EXECUTE_READ
}, /* 0x20 */
2272 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x30 */
2273 { PAGE_EXECUTE_READWRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x40 */
2274 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0x50 */
2275 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0x60 */
2276 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x70 */
2277 { PAGE_EXECUTE_WRITECOPY
, 0 }, /* 0x80 */
2278 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, 0 }, /* 0x90 */
2279 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, 0 }, /* 0xa0 */
2280 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0xb0 */
2281 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, 0 }, /* 0xc0 */
2282 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0xd0 */
2283 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0xe0 */
2284 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 } /* 0xf0 */
2287 DWORD ret
, old_prot
, rw_prot
, exec_prot
, i
, j
;
2288 MEMORY_BASIC_INFORMATION info
;
2292 trace("system page size %#x\n", si
.dwPageSize
);
2294 SetLastError(0xdeadbeef);
2295 base
= VirtualAlloc(0, si
.dwPageSize
, MEM_RESERVE
| MEM_COMMIT
, PAGE_NOACCESS
);
2296 ok(base
!= NULL
, "VirtualAlloc failed %d\n", GetLastError());
2298 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2300 SetLastError(0xdeadbeef);
2301 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2302 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2303 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2304 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2305 ok(info
.Protect
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, info
.Protect
);
2306 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2307 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%d: %#x != PAGE_NOACCESS\n", i
, info
.AllocationProtect
);
2308 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2309 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
2311 old_prot
= 0xdeadbeef;
2312 SetLastError(0xdeadbeef);
2313 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot_set
, &old_prot
);
2316 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2317 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
2319 SetLastError(0xdeadbeef);
2320 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2321 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2322 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2323 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2324 ok(info
.Protect
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_get
);
2325 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2326 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%d: %#x != PAGE_NOACCESS\n", i
, info
.AllocationProtect
);
2327 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2328 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
2332 ok(!ret
, "%d: VirtualProtect should fail\n", i
);
2333 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2336 old_prot
= 0xdeadbeef;
2337 SetLastError(0xdeadbeef);
2338 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
2339 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2341 ok(old_prot
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_get
);
2343 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
2348 for (i
= 0; i
<= 4; i
++)
2352 for (j
= 0; j
<= 4; j
++)
2354 DWORD prot
= exec_prot
| rw_prot
;
2356 SetLastError(0xdeadbeef);
2357 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, prot
);
2358 if ((rw_prot
&& exec_prot
) || (!rw_prot
&& !exec_prot
))
2360 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", prot
);
2361 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2365 if (prot
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
))
2367 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", prot
);
2368 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2372 ok(ptr
!= NULL
, "VirtualAlloc(%02x) error %d\n", prot
, GetLastError());
2373 ok(ptr
== base
, "expected %p, got %p\n", base
, ptr
);
2377 SetLastError(0xdeadbeef);
2378 ret
= VirtualProtect(base
, si
.dwPageSize
, prot
, &old_prot
);
2379 if ((rw_prot
&& exec_prot
) || (!rw_prot
&& !exec_prot
))
2381 ok(!ret
, "VirtualProtect(%02x) should fail\n", prot
);
2382 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2386 if (prot
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
))
2388 ok(!ret
, "VirtualProtect(%02x) should fail\n", prot
);
2389 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2392 ok(ret
, "VirtualProtect(%02x) error %d\n", prot
, GetLastError());
2398 exec_prot
= 1 << (i
+ 4);
2401 VirtualFree(base
, 0, MEM_FREE
);
2404 static BOOL
is_mem_writable(DWORD prot
)
2406 switch (prot
& 0xff)
2408 case PAGE_READWRITE
:
2409 case PAGE_WRITECOPY
:
2410 case PAGE_EXECUTE_READWRITE
:
2411 case PAGE_EXECUTE_WRITECOPY
:
2419 static void test_VirtualAlloc_protection(void)
2421 static const struct test_data
2427 { 0, FALSE
}, /* 0x00 */
2428 { PAGE_NOACCESS
, TRUE
}, /* 0x01 */
2429 { PAGE_READONLY
, TRUE
}, /* 0x02 */
2430 { PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x03 */
2431 { PAGE_READWRITE
, TRUE
}, /* 0x04 */
2432 { PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
}, /* 0x05 */
2433 { PAGE_READWRITE
| PAGE_READONLY
, FALSE
}, /* 0x06 */
2434 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x07 */
2435 { PAGE_WRITECOPY
, FALSE
}, /* 0x08 */
2436 { PAGE_WRITECOPY
| PAGE_NOACCESS
, FALSE
}, /* 0x09 */
2437 { PAGE_WRITECOPY
| PAGE_READONLY
, FALSE
}, /* 0x0a */
2438 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, FALSE
}, /* 0x0b */
2439 { PAGE_WRITECOPY
| PAGE_READWRITE
, FALSE
}, /* 0x0c */
2440 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
}, /* 0x0d */
2441 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, FALSE
}, /* 0x0e */
2442 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
}, /* 0x0f */
2444 { PAGE_EXECUTE
, TRUE
}, /* 0x10 */
2445 { PAGE_EXECUTE_READ
, TRUE
}, /* 0x20 */
2446 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0x30 */
2447 { PAGE_EXECUTE_READWRITE
, TRUE
}, /* 0x40 */
2448 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
}, /* 0x50 */
2449 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
}, /* 0x60 */
2450 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0x70 */
2451 { PAGE_EXECUTE_WRITECOPY
, FALSE
}, /* 0x80 */
2452 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, FALSE
}, /* 0x90 */
2453 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, FALSE
}, /* 0xa0 */
2454 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
}, /* 0xb0 */
2455 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, FALSE
}, /* 0xc0 */
2456 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
}, /* 0xd0 */
2457 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
}, /* 0xe0 */
2458 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
} /* 0xf0 */
2462 MEMORY_BASIC_INFORMATION info
;
2466 trace("system page size %#x\n", si
.dwPageSize
);
2468 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2470 SetLastError(0xdeadbeef);
2471 base
= VirtualAlloc(0, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
2475 ok(base
!= NULL
, "%d: VirtualAlloc failed %d\n", i
, GetLastError());
2477 SetLastError(0xdeadbeef);
2478 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2479 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2480 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2481 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2482 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
2483 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2484 ok(info
.AllocationProtect
== td
[i
].prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, td
[i
].prot
);
2485 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2486 ok(info
.Type
== MEM_PRIVATE
, "%d: %#x != MEM_PRIVATE\n", i
, info
.Type
);
2488 if (is_mem_writable(info
.Protect
))
2492 SetLastError(0xdeadbeef);
2493 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2494 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2495 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
2498 SetLastError(0xdeadbeef);
2499 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
2500 ok(ptr
== base
, "%d: VirtualAlloc failed %d\n", i
, GetLastError());
2502 VirtualFree(base
, 0, MEM_FREE
);
2506 ok(!base
, "%d: VirtualAlloc should fail\n", i
);
2507 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2512 static void test_CreateFileMapping_protection(void)
2514 static const struct test_data
2518 DWORD prot_after_write
;
2521 { 0, FALSE
, 0 }, /* 0x00 */
2522 { PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x01 */
2523 { PAGE_READONLY
, TRUE
, PAGE_READONLY
}, /* 0x02 */
2524 { PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x03 */
2525 { PAGE_READWRITE
, TRUE
, PAGE_READWRITE
}, /* 0x04 */
2526 { PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x05 */
2527 { PAGE_READWRITE
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x06 */
2528 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x07 */
2529 { PAGE_WRITECOPY
, TRUE
, PAGE_READWRITE
}, /* 0x08 */
2530 { PAGE_WRITECOPY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x09 */
2531 { PAGE_WRITECOPY
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0a */
2532 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0b */
2533 { PAGE_WRITECOPY
| PAGE_READWRITE
, FALSE
, PAGE_NOACCESS
}, /* 0x0c */
2534 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x0d */
2535 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, FALSE
, PAGE_NOACCESS
}, /* 0x0e */
2536 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, FALSE
, PAGE_NOACCESS
}, /* 0x0f */
2538 { PAGE_EXECUTE
, FALSE
, PAGE_EXECUTE
}, /* 0x10 */
2539 { PAGE_EXECUTE_READ
, TRUE
, PAGE_EXECUTE_READ
}, /* 0x20 */
2540 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_EXECUTE_READ
}, /* 0x30 */
2541 { PAGE_EXECUTE_READWRITE
, TRUE
, PAGE_EXECUTE_READWRITE
}, /* 0x40 */
2542 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x50 */
2543 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0x60 */
2544 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x70 */
2545 { PAGE_EXECUTE_WRITECOPY
, TRUE
, PAGE_EXECUTE_READWRITE
}, /* 0x80 */
2546 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0x90 */
2547 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0xa0 */
2548 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0xb0 */
2549 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, FALSE
, PAGE_NOACCESS
}, /* 0xc0 */
2550 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
}, /* 0xd0 */
2551 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, FALSE
, PAGE_NOACCESS
}, /* 0xe0 */
2552 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, FALSE
, PAGE_NOACCESS
} /* 0xf0 */
2555 DWORD ret
, i
, alloc_prot
, prot
, old_prot
;
2556 MEMORY_BASIC_INFORMATION info
;
2558 char temp_path
[MAX_PATH
];
2559 char file_name
[MAX_PATH
];
2561 BOOL page_exec_supported
= TRUE
;
2564 trace("system page size %#x\n", si
.dwPageSize
);
2566 GetTempPathA(MAX_PATH
, temp_path
);
2567 GetTempFileNameA(temp_path
, "map", 0, file_name
);
2569 SetLastError(0xdeadbeef);
2570 hfile
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
2571 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile(%s) error %d\n", file_name
, GetLastError());
2572 SetFilePointer(hfile
, si
.dwPageSize
, NULL
, FILE_BEGIN
);
2573 SetEndOfFile(hfile
);
2575 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2577 SetLastError(0xdeadbeef);
2578 hmap
= CreateFileMappingW(hfile
, NULL
, td
[i
].prot
| SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
2584 trace("%d: CreateFileMapping(%04x) failed: %d\n", i
, td
[i
].prot
, GetLastError());
2585 /* NT4 and win2k don't support EXEC on file mappings */
2586 if (td
[i
].prot
== PAGE_EXECUTE_READ
|| td
[i
].prot
== PAGE_EXECUTE_READWRITE
)
2588 page_exec_supported
= FALSE
;
2589 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i
);
2592 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2593 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2595 page_exec_supported
= FALSE
;
2596 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
2600 ok(hmap
!= 0, "%d: CreateFileMapping(%04x) error %d\n", i
, td
[i
].prot
, GetLastError());
2602 base
= MapViewOfFile(hmap
, FILE_MAP_READ
, 0, 0, 0);
2603 ok(base
!= NULL
, "%d: MapViewOfFile failed %d\n", i
, GetLastError());
2605 SetLastError(0xdeadbeef);
2606 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2607 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2608 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2609 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2610 ok(info
.Protect
== PAGE_READONLY
, "%d: got %#x != expected PAGE_READONLY\n", i
, info
.Protect
);
2611 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2612 ok(info
.AllocationProtect
== PAGE_READONLY
, "%d: %#x != PAGE_READONLY\n", i
, info
.AllocationProtect
);
2613 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2614 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
2616 if (is_mem_writable(info
.Protect
))
2620 SetLastError(0xdeadbeef);
2621 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2622 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2623 ok(info
.Protect
== td
[i
].prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot
);
2626 SetLastError(0xdeadbeef);
2627 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, td
[i
].prot
);
2628 ok(!ptr
, "%d: VirtualAlloc(%02x) should fail\n", i
, td
[i
].prot
);
2629 /* FIXME: remove once Wine is fixed */
2630 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2632 ok(GetLastError() == ERROR_ACCESS_DENIED
, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
2634 ok(GetLastError() == ERROR_ACCESS_DENIED
, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
2636 SetLastError(0xdeadbeef);
2637 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot
, &old_prot
);
2638 if (td
[i
].prot
== PAGE_READONLY
|| td
[i
].prot
== PAGE_WRITECOPY
)
2639 ok(ret
, "%d: VirtualProtect(%02x) error %d\n", i
, td
[i
].prot
, GetLastError());
2642 ok(!ret
, "%d: VirtualProtect(%02x) should fail\n", i
, td
[i
].prot
);
2643 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2646 UnmapViewOfFile(base
);
2651 ok(!hmap
, "%d: CreateFileMapping should fail\n", i
);
2652 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2656 if (page_exec_supported
) alloc_prot
= PAGE_EXECUTE_READWRITE
;
2657 else alloc_prot
= PAGE_READWRITE
;
2658 SetLastError(0xdeadbeef);
2659 hmap
= CreateFileMappingW(hfile
, NULL
, alloc_prot
, 0, si
.dwPageSize
, NULL
);
2660 ok(hmap
!= 0, "%d: CreateFileMapping error %d\n", i
, GetLastError());
2662 SetLastError(0xdeadbeef);
2663 base
= MapViewOfFile(hmap
, FILE_MAP_READ
| FILE_MAP_WRITE
| (page_exec_supported
? FILE_MAP_EXECUTE
: 0), 0, 0, 0);
2664 ok(base
!= NULL
, "MapViewOfFile failed %d\n", GetLastError());
2666 old_prot
= 0xdeadbeef;
2667 SetLastError(0xdeadbeef);
2668 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
2669 ok(ret
, "VirtualProtect error %d\n", GetLastError());
2670 ok(old_prot
== alloc_prot
, "got %#x != expected %#x\n", old_prot
, alloc_prot
);
2672 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2674 SetLastError(0xdeadbeef);
2675 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2676 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2677 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2678 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2679 ok(info
.Protect
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, info
.Protect
);
2680 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2681 ok(info
.AllocationProtect
== alloc_prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, alloc_prot
);
2682 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2683 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
2685 old_prot
= 0xdeadbeef;
2686 SetLastError(0xdeadbeef);
2687 ret
= VirtualProtect(base
, si
.dwPageSize
, td
[i
].prot
, &old_prot
);
2688 if (td
[i
].success
|| td
[i
].prot
== PAGE_NOACCESS
|| td
[i
].prot
== PAGE_EXECUTE
)
2692 /* win2k and XP don't support EXEC on file mappings */
2693 if (td
[i
].prot
== PAGE_EXECUTE
)
2695 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i
);
2698 /* NT4 and win2k don't support EXEC on file mappings */
2699 if (td
[i
].prot
== PAGE_EXECUTE_READ
|| td
[i
].prot
== PAGE_EXECUTE_READWRITE
)
2701 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE\n", i
);
2704 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
2705 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2707 ok(broken(!ret
), "%d: VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
2712 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2713 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
2716 /* looks strange but Windows doesn't do this for PAGE_WRITECOPY */
2717 if (prot
== PAGE_EXECUTE_WRITECOPY
) prot
= PAGE_EXECUTE_READWRITE
;
2719 SetLastError(0xdeadbeef);
2720 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2721 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2722 ok(info
.BaseAddress
== base
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, base
);
2723 ok(info
.RegionSize
== si
.dwPageSize
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, si
.dwPageSize
);
2724 /* FIXME: remove the condition below once Wine is fixed */
2725 if (td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2726 todo_wine
ok(info
.Protect
== prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, prot
);
2728 ok(info
.Protect
== prot
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, prot
);
2729 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
2730 ok(info
.AllocationProtect
== alloc_prot
, "%d: %#x != %#x\n", i
, info
.AllocationProtect
, alloc_prot
);
2731 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
2732 ok(info
.Type
== MEM_MAPPED
, "%d: %#x != MEM_MAPPED\n", i
, info
.Type
);
2734 if (is_mem_writable(info
.Protect
))
2738 SetLastError(0xdeadbeef);
2739 ret
= VirtualQuery(base
, &info
, sizeof(info
));
2740 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
2741 /* FIXME: remove the condition below once Wine is fixed */
2742 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2743 todo_wine
ok(info
.Protect
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_after_write
);
2745 ok(info
.Protect
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_after_write
);
2750 ok(!ret
, "%d: VirtualProtect should fail\n", i
);
2751 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
2755 old_prot
= 0xdeadbeef;
2756 SetLastError(0xdeadbeef);
2757 ret
= VirtualProtect(base
, si
.dwPageSize
, PAGE_NOACCESS
, &old_prot
);
2758 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
2759 /* FIXME: remove the condition below once Wine is fixed */
2760 if (td
[i
].prot
== PAGE_WRITECOPY
|| td
[i
].prot
== PAGE_EXECUTE_WRITECOPY
)
2761 todo_wine
ok(old_prot
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_after_write
);
2763 ok(old_prot
== td
[i
].prot_after_write
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_after_write
);
2766 UnmapViewOfFile(base
);
2770 DeleteFileA(file_name
);
2773 #define ACCESS_READ 0x01
2774 #define ACCESS_WRITE 0x02
2775 #define ACCESS_EXECUTE 0x04
2776 #define ACCESS_WRITECOPY 0x08
2778 static DWORD
page_prot_to_access(DWORD prot
)
2782 case PAGE_READWRITE
:
2783 return ACCESS_READ
| ACCESS_WRITE
;
2786 case PAGE_EXECUTE_READ
:
2787 return ACCESS_READ
| ACCESS_EXECUTE
;
2789 case PAGE_EXECUTE_READWRITE
:
2790 return ACCESS_READ
| ACCESS_WRITE
| ACCESS_WRITECOPY
| ACCESS_EXECUTE
;
2792 case PAGE_EXECUTE_WRITECOPY
:
2793 return ACCESS_READ
| ACCESS_WRITECOPY
| ACCESS_EXECUTE
;
2798 case PAGE_WRITECOPY
:
2806 static BOOL
is_compatible_protection(DWORD map_prot
, DWORD view_prot
, DWORD prot
)
2808 DWORD map_access
, view_access
, prot_access
;
2810 map_access
= page_prot_to_access(map_prot
);
2811 view_access
= page_prot_to_access(view_prot
);
2812 prot_access
= page_prot_to_access(prot
);
2814 if (view_access
== prot_access
) return TRUE
;
2815 if (!view_access
) return FALSE
;
2817 if ((view_access
& prot_access
) != prot_access
) return FALSE
;
2818 if ((map_access
& prot_access
) == prot_access
) return TRUE
;
2823 static DWORD
map_prot_to_access(DWORD prot
)
2827 case PAGE_READWRITE
:
2828 case PAGE_EXECUTE_READWRITE
:
2829 return SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
| SECTION_MAP_EXECUTE_EXPLICIT
| SECTION_QUERY
;
2831 case PAGE_WRITECOPY
:
2833 case PAGE_EXECUTE_READ
:
2834 case PAGE_EXECUTE_WRITECOPY
:
2835 return SECTION_MAP_READ
| SECTION_MAP_EXECUTE
| SECTION_MAP_EXECUTE_EXPLICIT
| SECTION_QUERY
;
2841 static BOOL
is_compatible_access(DWORD map_prot
, DWORD view_prot
)
2843 DWORD access
= map_prot_to_access(map_prot
);
2844 if (!view_prot
) view_prot
= SECTION_MAP_READ
;
2845 return (view_prot
& access
) == view_prot
;
2848 static void *map_view_of_file(HANDLE handle
, DWORD access
)
2851 LARGE_INTEGER offset
;
2857 if (!pNtMapViewOfSection
) return NULL
;
2860 offset
.u
.LowPart
= 0;
2861 offset
.u
.HighPart
= 0;
2863 exec
= access
& FILE_MAP_EXECUTE
;
2864 access
&= ~FILE_MAP_EXECUTE
;
2866 if (access
== FILE_MAP_COPY
)
2869 protect
= PAGE_EXECUTE_WRITECOPY
;
2871 protect
= PAGE_WRITECOPY
;
2873 else if (access
& FILE_MAP_WRITE
)
2876 protect
= PAGE_EXECUTE_READWRITE
;
2878 protect
= PAGE_READWRITE
;
2880 else if (access
& FILE_MAP_READ
)
2883 protect
= PAGE_EXECUTE_READ
;
2885 protect
= PAGE_READONLY
;
2887 else protect
= PAGE_NOACCESS
;
2890 status
= pNtMapViewOfSection(handle
, GetCurrentProcess(), &addr
, 0, 0, &offset
,
2891 &count
, 1 /* ViewShare */, 0, protect
);
2894 /* for simplicity */
2895 SetLastError(ERROR_ACCESS_DENIED
);
2901 static void test_mapping(void)
2903 static const DWORD page_prot
[] =
2905 PAGE_NOACCESS
, PAGE_READONLY
, PAGE_READWRITE
, PAGE_WRITECOPY
,
2906 PAGE_EXECUTE_READ
, PAGE_EXECUTE_READWRITE
, PAGE_EXECUTE_WRITECOPY
2913 { 0, PAGE_NOACCESS
}, /* 0x00 */
2914 { FILE_MAP_COPY
, PAGE_WRITECOPY
}, /* 0x01 */
2915 { FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x02 */
2916 { FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x03 */
2917 { FILE_MAP_READ
, PAGE_READONLY
}, /* 0x04 */
2918 { FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_READONLY
}, /* 0x05 */
2919 { FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x06 */
2920 { FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x07 */
2921 { SECTION_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x08 */
2922 { SECTION_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_NOACCESS
}, /* 0x09 */
2923 { SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x0a */
2924 { SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x0b */
2925 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_READONLY
}, /* 0x0c */
2926 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_READONLY
}, /* 0x0d */
2927 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_READWRITE
}, /* 0x0e */
2928 { SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_READWRITE
}, /* 0x0f */
2929 { FILE_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x20 */
2930 { FILE_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_EXECUTE_WRITECOPY
}, /* 0x21 */
2931 { FILE_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x22 */
2932 { FILE_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x23 */
2933 { FILE_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_EXECUTE_READ
}, /* 0x24 */
2934 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_EXECUTE_READ
}, /* 0x25 */
2935 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x26 */
2936 { FILE_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x27 */
2937 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
, PAGE_NOACCESS
}, /* 0x28 */
2938 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_COPY
, PAGE_NOACCESS
}, /* 0x29 */
2939 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x2a */
2940 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
}, /* 0x2b */
2941 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
, PAGE_EXECUTE_READ
}, /* 0x2c */
2942 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_COPY
, PAGE_EXECUTE_READ
}, /* 0x2d */
2943 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
, PAGE_EXECUTE_READWRITE
}, /* 0x2e */
2944 { FILE_MAP_EXECUTE
| SECTION_MAP_EXECUTE
| FILE_MAP_READ
| FILE_MAP_WRITE
| FILE_MAP_COPY
, PAGE_EXECUTE_READWRITE
} /* 0x2f */
2946 void *base
, *nt_base
, *ptr
;
2947 DWORD i
, j
, k
, ret
, old_prot
, prev_prot
;
2949 char temp_path
[MAX_PATH
];
2950 char file_name
[MAX_PATH
];
2952 MEMORY_BASIC_INFORMATION info
, nt_info
;
2955 trace("system page size %#x\n", si
.dwPageSize
);
2957 GetTempPathA(MAX_PATH
, temp_path
);
2958 GetTempFileNameA(temp_path
, "map", 0, file_name
);
2960 SetLastError(0xdeadbeef);
2961 hfile
= CreateFileA(file_name
, GENERIC_READ
|GENERIC_WRITE
|GENERIC_EXECUTE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
2962 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile(%s) error %d\n", file_name
, GetLastError());
2963 SetFilePointer(hfile
, si
.dwPageSize
, NULL
, FILE_BEGIN
);
2964 SetEndOfFile(hfile
);
2966 for (i
= 0; i
< sizeof(page_prot
)/sizeof(page_prot
[0]); i
++)
2968 SetLastError(0xdeadbeef);
2969 hmap
= CreateFileMappingW(hfile
, NULL
, page_prot
[i
] | SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
2971 if (page_prot
[i
] == PAGE_NOACCESS
)
2975 ok(!hmap
, "CreateFileMapping(PAGE_NOACCESS) should fail\n");
2976 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2978 /* A trick to create a not accessible mapping */
2979 SetLastError(0xdeadbeef);
2980 hmap
= CreateFileMappingW(hfile
, NULL
, PAGE_READWRITE
| SEC_COMMIT
, 0, si
.dwPageSize
, NULL
);
2981 ok(hmap
!= 0, "CreateFileMapping(PAGE_READWRITE) error %d\n", GetLastError());
2982 SetLastError(0xdeadbeef);
2983 ret
= DuplicateHandle(GetCurrentProcess(), hmap
, GetCurrentProcess(), &hmap2
, 0, FALSE
, 0);
2984 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
2991 trace("%d: CreateFileMapping(%04x) failed: %d\n", i
, page_prot
[i
], GetLastError());
2993 /* NT4 and win2k don't support EXEC on file mappings */
2994 if (page_prot
[i
] == PAGE_EXECUTE_READ
|| page_prot
[i
] == PAGE_EXECUTE_READWRITE
)
2996 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE\n", i
);
2999 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3000 if (page_prot
[i
] == PAGE_EXECUTE_WRITECOPY
)
3002 ok(broken(!hmap
), "%d: CreateFileMapping doesn't support PAGE_EXECUTE_WRITECOPY\n", i
);
3007 ok(hmap
!= 0, "%d: CreateFileMapping(%04x) error %d\n", i
, page_prot
[i
], GetLastError());
3009 for (j
= 0; j
< sizeof(view
)/sizeof(view
[0]); j
++)
3011 nt_base
= map_view_of_file(hmap
, view
[j
].access
);
3014 SetLastError(0xdeadbeef);
3015 ret
= VirtualQuery(nt_base
, &nt_info
, sizeof(nt_info
));
3016 ok(ret
, "%d: VirtualQuery failed %d\n", j
, GetLastError());
3017 UnmapViewOfFile(nt_base
);
3020 SetLastError(0xdeadbeef);
3021 base
= MapViewOfFile(hmap
, view
[j
].access
, 0, 0, 0);
3023 /* Vista+ supports FILE_MAP_EXECUTE properly, earlier versions don't */
3024 ok(!nt_base
== !base
||
3025 broken((view
[j
].access
& FILE_MAP_EXECUTE
) && !nt_base
!= !base
),
3026 "%d: (%04x/%04x) NT %p kernel %p\n", j
, page_prot
[i
], view
[j
].access
, nt_base
, base
);
3028 if (!is_compatible_access(page_prot
[i
], view
[j
].access
))
3030 ok(!base
, "%d: MapViewOfFile(%04x/%04x) should fail\n", j
, page_prot
[i
], view
[j
].access
);
3031 ok(GetLastError() == ERROR_ACCESS_DENIED
, "wrong error %d\n", GetLastError());
3035 /* Vista+ properly supports FILE_MAP_EXECUTE, earlier versions don't */
3036 if (!base
&& (view
[j
].access
& FILE_MAP_EXECUTE
))
3038 ok(broken(!base
), "%d: MapViewOfFile(%04x/%04x) failed %d\n", j
, page_prot
[i
], view
[j
].access
, GetLastError());
3042 ok(base
!= NULL
, "%d: MapViewOfFile(%04x/%04x) failed %d\n", j
, page_prot
[i
], view
[j
].access
, GetLastError());
3044 SetLastError(0xdeadbeef);
3045 ret
= VirtualQuery(base
, &info
, sizeof(info
));
3046 ok(ret
, "%d: VirtualQuery failed %d\n", j
, GetLastError());
3047 ok(info
.BaseAddress
== base
, "%d: (%04x) got %p, expected %p\n", j
, view
[j
].access
, info
.BaseAddress
, base
);
3048 ok(info
.RegionSize
== si
.dwPageSize
, "%d: (%04x) got %#lx != expected %#x\n", j
, view
[j
].access
, info
.RegionSize
, si
.dwPageSize
);
3049 ok(info
.Protect
== view
[j
].prot
||
3050 broken(view
[j
].prot
== PAGE_EXECUTE_READ
&& info
.Protect
== PAGE_READONLY
) || /* win2k */
3051 broken(view
[j
].prot
== PAGE_EXECUTE_READWRITE
&& info
.Protect
== PAGE_READWRITE
) || /* win2k */
3052 broken(view
[j
].prot
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
3053 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, info
.Protect
, view
[j
].prot
);
3054 ok(info
.AllocationBase
== base
, "%d: (%04x) got %p, expected %p\n", j
, view
[j
].access
, info
.AllocationBase
, base
);
3055 ok(info
.AllocationProtect
== info
.Protect
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, info
.AllocationProtect
, info
.Protect
);
3056 ok(info
.State
== MEM_COMMIT
, "%d: (%04x) got %#x, expected MEM_COMMIT\n", j
, view
[j
].access
, info
.State
);
3057 ok(info
.Type
== MEM_MAPPED
, "%d: (%04x) got %#x, expected MEM_MAPPED\n", j
, view
[j
].access
, info
.Type
);
3059 if (nt_base
&& base
)
3061 ok(nt_info
.RegionSize
== info
.RegionSize
, "%d: (%04x) got %#lx != expected %#lx\n", j
, view
[j
].access
, nt_info
.RegionSize
, info
.RegionSize
);
3062 ok(nt_info
.Protect
== info
.Protect
/* Vista+ */ ||
3063 broken(nt_info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
3064 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.Protect
, info
.Protect
);
3065 ok(nt_info
.AllocationProtect
== info
.AllocationProtect
/* Vista+ */ ||
3066 broken(nt_info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
&& info
.Protect
== PAGE_NOACCESS
), /* XP */
3067 "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.AllocationProtect
, info
.AllocationProtect
);
3068 ok(nt_info
.State
== info
.State
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.State
, info
.State
);
3069 ok(nt_info
.Type
== info
.Type
, "%d: (%04x) got %#x, expected %#x\n", j
, view
[j
].access
, nt_info
.Type
, info
.Type
);
3072 prev_prot
= info
.Protect
;
3074 for (k
= 0; k
< sizeof(page_prot
)/sizeof(page_prot
[0]); k
++)
3076 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3077 SetLastError(0xdeadbeef);
3078 old_prot
= 0xdeadbeef;
3079 ret
= VirtualProtect(base
, si
.dwPageSize
, page_prot
[k
], &old_prot
);
3080 if (is_compatible_protection(page_prot
[i
], view
[j
].prot
, page_prot
[k
]))
3082 /* win2k and XP don't support EXEC on file mappings */
3083 if (!ret
&& page_prot
[k
] == PAGE_EXECUTE
)
3085 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3088 /* NT4 and win2k don't support EXEC on file mappings */
3089 if (!ret
&& (page_prot
[k
] == PAGE_EXECUTE_READ
|| page_prot
[k
] == PAGE_EXECUTE_READWRITE
))
3091 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE\n");
3094 /* Vista+ supports PAGE_EXECUTE_WRITECOPY, earlier versions don't */
3095 if (!ret
&& page_prot
[k
] == PAGE_EXECUTE_WRITECOPY
)
3097 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY\n");
3100 /* win2k and XP don't support PAGE_EXECUTE_WRITECOPY views properly */
3101 if (!ret
&& view
[j
].prot
== PAGE_EXECUTE_WRITECOPY
)
3103 ok(broken(!ret
), "VirtualProtect doesn't support PAGE_EXECUTE_WRITECOPY view properly\n");
3107 ok(ret
, "VirtualProtect error %d, map %#x, view %#x, requested prot %#x\n", GetLastError(), page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
3108 ok(old_prot
== prev_prot
, "got %#x, expected %#x\n", old_prot
, prev_prot
);
3109 prev_prot
= page_prot
[k
];
3113 /* NT4 doesn't fail on incompatible map and view */
3116 ok(broken(ret
), "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
3117 skip("Incompatible map and view are not properly handled on this platform\n");
3118 break; /* NT4 won't pass remaining tests */
3121 ok(!ret
, "VirtualProtect should fail, map %#x, view %#x, requested prot %#x\n", page_prot
[i
], view
[j
].prot
, page_prot
[k
]);
3122 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3126 for (k
= 0; k
< sizeof(page_prot
)/sizeof(page_prot
[0]); k
++)
3128 /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/
3129 SetLastError(0xdeadbeef);
3130 ptr
= VirtualAlloc(base
, si
.dwPageSize
, MEM_COMMIT
, page_prot
[k
]);
3131 ok(!ptr
, "VirtualAlloc(%02x) should fail\n", page_prot
[k
]);
3132 /* FIXME: remove once Wine is fixed */
3133 if (page_prot
[k
] == PAGE_WRITECOPY
|| page_prot
[k
] == PAGE_EXECUTE_WRITECOPY
)
3135 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3137 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3140 UnmapViewOfFile(base
);
3147 DeleteFileA(file_name
);
3150 static void test_shared_memory(BOOL is_child
)
3155 SetLastError(0xdeadbef);
3156 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, "winetest_virtual.c");
3157 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
3159 ok(GetLastError() == ERROR_ALREADY_EXISTS
, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3161 SetLastError(0xdeadbef);
3162 p
= MapViewOfFile(mapping
, FILE_MAP_READ
|FILE_MAP_WRITE
, 0, 0, 4096);
3163 ok(p
!= NULL
, "MapViewOfFile error %d\n", GetLastError());
3167 ok(*p
== 0x1a2b3c4d, "expected 0x1a2b3c4d in child, got %#x\n", *p
);
3172 char cmdline
[MAX_PATH
];
3173 PROCESS_INFORMATION pi
;
3174 STARTUPINFOA si
= { sizeof(si
) };
3179 winetest_get_mainargs(&argv
);
3180 sprintf(cmdline
, "\"%s\" virtual sharedmem", argv
[0]);
3181 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
3182 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
3183 winetest_wait_child_process(pi
.hProcess
);
3184 CloseHandle(pi
.hThread
);
3185 CloseHandle(pi
.hProcess
);
3189 CloseHandle(mapping
);
3192 static void test_shared_memory_ro(BOOL is_child
, DWORD child_access
)
3197 SetLastError(0xdeadbef);
3198 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, "winetest_virtual.c_ro");
3199 ok(mapping
!= 0, "CreateFileMapping error %d\n", GetLastError());
3201 ok(GetLastError() == ERROR_ALREADY_EXISTS
, "expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
3203 SetLastError(0xdeadbef);
3204 p
= MapViewOfFile(mapping
, is_child
? child_access
: FILE_MAP_READ
, 0, 0, 4096);
3205 ok(p
!= NULL
, "MapViewOfFile error %d\n", GetLastError());
3214 char cmdline
[MAX_PATH
];
3215 PROCESS_INFORMATION pi
;
3216 STARTUPINFOA si
= { sizeof(si
) };
3219 winetest_get_mainargs(&argv
);
3220 sprintf(cmdline
, "\"%s\" virtual sharedmemro %x", argv
[0], child_access
);
3221 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
3222 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
3223 winetest_wait_child_process(pi
.hProcess
);
3224 CloseHandle(pi
.hThread
);
3225 CloseHandle(pi
.hProcess
);
3227 if(child_access
& FILE_MAP_WRITE
)
3228 ok(*p
== 0xdeadbeef, "*p = %x, expected 0xdeadbeef\n", *p
);
3230 ok(!*p
, "*p = %x, expected 0\n", *p
);
3234 CloseHandle(mapping
);
3241 argc
= winetest_get_mainargs( &argv
);
3245 if (!strcmp(argv
[2], "sleep"))
3247 Sleep(5000); /* spawned process runs for at most 5 seconds */
3250 if (!strcmp(argv
[2], "sharedmem"))
3252 test_shared_memory(TRUE
);
3255 if (!strcmp(argv
[2], "sharedmemro"))
3257 test_shared_memory_ro(TRUE
, strtol(argv
[3], NULL
, 16));
3264 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
3265 PAGE_EXECUTE_READWRITE
);
3266 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
3267 if (mem
== NULL
) break;
3268 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
3269 ok(ret
, "VirtualFree failed %u\n", GetLastError());
3275 hkernel32
= GetModuleHandleA("kernel32.dll");
3276 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
3277 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
3278 pGetWriteWatch
= (void *) GetProcAddress(hkernel32
, "GetWriteWatch");
3279 pResetWriteWatch
= (void *) GetProcAddress(hkernel32
, "ResetWriteWatch");
3280 pNtAreMappedFilesTheSame
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"),
3281 "NtAreMappedFilesTheSame" );
3282 pNtMapViewOfSection
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtMapViewOfSection");
3283 pNtUnmapViewOfSection
= (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection");
3284 pNtCurrentTeb
= (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtCurrentTeb" );
3286 test_shared_memory(FALSE
);
3287 test_shared_memory_ro(FALSE
, FILE_MAP_READ
|FILE_MAP_WRITE
);
3288 test_shared_memory_ro(FALSE
, FILE_MAP_COPY
);
3289 test_shared_memory_ro(FALSE
, FILE_MAP_COPY
|FILE_MAP_WRITE
);
3291 test_CreateFileMapping_protection();
3292 test_VirtualAlloc_protection();
3293 test_VirtualProtect();
3294 test_VirtualAllocEx();
3295 test_VirtualAlloc();
3296 test_MapViewOfFile();
3297 test_NtMapViewOfSection();
3298 test_NtAreMappedFilesTheSame();
3299 test_CreateFileMapping();
3300 test_IsBadReadPtr();
3301 test_IsBadWritePtr();
3302 test_IsBadCodePtr();
3306 /* The following tests should be executed as a last step, and in exactly this
3307 * order, since ATL thunk emulation cannot be enabled anymore on Windows. */
3308 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_ENABLE
);
3309 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE
);
3310 test_atl_thunk_emulation( MEM_EXECUTE_OPTION_DISABLE
| MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
);