2 * Unit test suite for Virtual* family of APIs.
4 * Copyright 2004 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
30 #include "wine/test.h"
33 #define MAPPING_SIZE 0x100000
35 static HINSTANCE hkernel32
;
36 static LPVOID (WINAPI
*pVirtualAllocEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
, DWORD
);
37 static BOOL (WINAPI
*pVirtualFreeEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
);
38 static UINT (WINAPI
*pGetWriteWatch
)(DWORD
,LPVOID
,SIZE_T
,LPVOID
*,ULONG_PTR
*,ULONG
*);
39 static UINT (WINAPI
*pResetWriteWatch
)(LPVOID
,SIZE_T
);
40 static NTSTATUS (WINAPI
*pNtAreMappedFilesTheSame
)(PVOID
,PVOID
);
42 /* ############################### */
44 static HANDLE
create_target_process(const char *arg
)
47 char cmdline
[MAX_PATH
];
48 PROCESS_INFORMATION pi
;
50 STARTUPINFO si
= { 0 };
53 winetest_get_mainargs( &argv
);
54 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
55 ret
= CreateProcess(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
56 ok(ret
, "error: %u\n", GetLastError());
57 ret
= CloseHandle(pi
.hThread
);
58 ok(ret
, "error %u\n", GetLastError());
62 static void test_VirtualAllocEx(void)
64 const unsigned int alloc_size
= 1<<15;
66 SIZE_T bytes_written
= 0, bytes_read
= 0, i
;
70 MEMORY_BASIC_INFORMATION info
;
73 /* not exported in all windows-versions */
74 if ((!pVirtualAllocEx
) || (!pVirtualFreeEx
)) {
75 win_skip("Virtual{Alloc,Free}Ex not available\n");
79 hProcess
= create_target_process("sleep");
80 ok(hProcess
!= NULL
, "Can't start process\n");
82 SetLastError(0xdeadbeef);
83 addr1
= pVirtualAllocEx(hProcess
, NULL
, alloc_size
, MEM_COMMIT
,
84 PAGE_EXECUTE_READWRITE
);
85 if (!addr1
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
87 win_skip("VirtualAllocEx not implemented\n");
88 TerminateProcess(hProcess
, 0);
89 CloseHandle(hProcess
);
93 src
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
94 dst
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
95 for (i
= 0; i
< alloc_size
; i
++)
98 ok(addr1
!= NULL
, "VirtualAllocEx error %u\n", GetLastError());
99 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
100 ok(b
&& (bytes_written
== alloc_size
), "%lu bytes written\n",
102 b
= ReadProcessMemory(hProcess
, addr1
, dst
, alloc_size
, &bytes_read
);
103 ok(b
&& (bytes_read
== alloc_size
), "%lu bytes read\n", bytes_read
);
104 ok(!memcmp(src
, dst
, alloc_size
), "Data from remote process differs\n");
106 /* test invalid source buffers */
108 b
= VirtualProtect( src
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
109 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
110 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
111 ok( !b
, "WriteProcessMemory succeeded\n" );
112 ok( GetLastError() == ERROR_NOACCESS
||
113 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
114 "wrong error %u\n", GetLastError() );
115 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
116 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
117 ok( !b
, "ReadProcessMemory succeeded\n" );
118 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
119 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
121 b
= VirtualProtect( src
, 0x2000, PAGE_NOACCESS
, &old_prot
);
122 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
123 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
124 ok( !b
, "WriteProcessMemory succeeded\n" );
125 ok( GetLastError() == ERROR_NOACCESS
||
126 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
127 "wrong error %u\n", GetLastError() );
128 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
129 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
130 ok( !b
, "ReadProcessMemory succeeded\n" );
131 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
132 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
134 b
= pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
);
135 ok(b
!= 0, "VirtualFreeEx, error %u\n", GetLastError());
137 VirtualFree( src
, 0, MEM_FREE
);
138 VirtualFree( dst
, 0, MEM_FREE
);
141 * The following tests parallel those in test_VirtualAlloc()
144 SetLastError(0xdeadbeef);
145 addr1
= pVirtualAllocEx(hProcess
, 0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
146 ok(addr1
== NULL
, "VirtualAllocEx should fail on zero-sized allocation\n");
147 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
148 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
149 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
151 addr1
= pVirtualAllocEx(hProcess
, 0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
152 ok(addr1
!= NULL
, "VirtualAllocEx failed\n");
154 /* test a not committed memory */
155 memset(&info
, 'q', sizeof(info
));
156 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
), "VirtualQueryEx failed\n");
157 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
158 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
159 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
160 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
161 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
162 /* NT reports Protect == 0 for a not committed memory block */
163 ok(info
.Protect
== 0 /* NT */ ||
164 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
165 "%x != PAGE_NOACCESS\n", info
.Protect
);
166 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
168 SetLastError(0xdeadbeef);
169 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
170 "VirtualProtectEx should fail on a not committed memory\n");
171 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
172 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
173 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
175 addr2
= pVirtualAllocEx(hProcess
, addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
176 ok(addr1
== addr2
, "VirtualAllocEx failed\n");
178 /* test a committed memory */
179 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
),
180 "VirtualQueryEx failed\n");
181 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
182 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
183 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
184 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
185 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
186 /* this time NT reports PAGE_NOACCESS as well */
187 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
188 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
190 /* this should fail, since not the whole range is committed yet */
191 SetLastError(0xdeadbeef);
192 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
193 "VirtualProtectEx should fail on a not committed memory\n");
194 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
195 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
196 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
199 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtectEx failed\n");
200 ok(old_prot
== PAGE_NOACCESS
, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
203 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtectEx failed\n");
204 ok(old_prot
== PAGE_READONLY
, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
206 ok(!pVirtualFreeEx(hProcess
, addr1
, 0x10000, 0),
207 "VirtualFreeEx should fail with type 0\n");
208 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
209 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
211 ok(pVirtualFreeEx(hProcess
, addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFreeEx failed\n");
213 /* if the type is MEM_RELEASE, size must be 0 */
214 ok(!pVirtualFreeEx(hProcess
, addr1
, 1, MEM_RELEASE
),
215 "VirtualFreeEx should fail\n");
216 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
217 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
219 ok(pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
), "VirtualFreeEx failed\n");
221 TerminateProcess(hProcess
, 0);
222 CloseHandle(hProcess
);
225 static void test_VirtualAlloc(void)
229 MEMORY_BASIC_INFORMATION info
;
231 SetLastError(0xdeadbeef);
232 addr1
= VirtualAlloc(0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
233 ok(addr1
== NULL
, "VirtualAlloc should fail on zero-sized allocation\n");
234 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
235 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
236 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
238 addr1
= VirtualAlloc(0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
239 ok(addr1
!= NULL
, "VirtualAlloc failed\n");
241 /* test a not committed memory */
242 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
243 "VirtualQuery failed\n");
244 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
245 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
246 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
247 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
248 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
249 /* NT reports Protect == 0 for a not committed memory block */
250 ok(info
.Protect
== 0 /* NT */ ||
251 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
252 "%x != PAGE_NOACCESS\n", info
.Protect
);
253 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
255 SetLastError(0xdeadbeef);
256 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
257 "VirtualProtect should fail on a not committed memory\n");
258 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
259 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
260 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
262 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
263 ok(addr1
== addr2
, "VirtualAlloc failed\n");
265 /* test a committed memory */
266 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
267 "VirtualQuery failed\n");
268 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
269 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
270 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
271 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
272 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
273 /* this time NT reports PAGE_NOACCESS as well */
274 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
275 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
277 /* this should fail, since not the whole range is committed yet */
278 SetLastError(0xdeadbeef);
279 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
280 "VirtualProtect should fail on a not committed memory\n");
281 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
282 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
283 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
285 ok(VirtualProtect(addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtect failed\n");
286 ok(old_prot
== PAGE_NOACCESS
,
287 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
289 ok(VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtect failed\n");
290 ok(old_prot
== PAGE_READONLY
,
291 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
293 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
294 "VirtualQuery failed\n");
295 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
296 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
297 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
298 memset( addr1
, 0x55, 20 );
299 ok( *(DWORD
*)addr1
== 0x55555555, "wrong data %x\n", *(DWORD
*)addr1
);
301 if(winetest_interactive
)
303 addr2
= VirtualAlloc( addr1
, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
304 ok( addr2
== addr1
|| broken( !addr2
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
305 "VirtualAlloc failed err %u\n", GetLastError() );
306 ok( *(DWORD
*)addr1
== 0x55555555 || *(DWORD
*)addr1
== 0, "wrong data %x\n", *(DWORD
*)addr1
);
309 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
310 "VirtualQuery failed\n");
311 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
312 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
313 ok(info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
315 addr2
= VirtualAlloc( (char *)addr1
+ 0x1000, 0x1000, MEM_RESET
, PAGE_NOACCESS
);
316 ok( (char *)addr2
== (char *)addr1
+ 0x1000, "VirtualAlloc failed\n" );
318 ok(VirtualQuery(addr2
, &info
, sizeof(info
)) == sizeof(info
),
319 "VirtualQuery failed\n");
320 ok(info
.RegionSize
== 0xf000, "%lx != 0xf000\n", info
.RegionSize
);
321 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
322 ok(info
.Protect
== 0, "%x != 0\n", info
.Protect
);
324 addr2
= VirtualAlloc( (char *)addr1
+ 0xf000, 0x2000, MEM_RESET
, PAGE_NOACCESS
);
325 ok( !addr2
, "VirtualAlloc failed\n" );
326 ok( GetLastError() == ERROR_INVALID_ADDRESS
, "wrong error %u\n", GetLastError() );
330 skip("MEM_RESET is not currently supported\n");
332 /* invalid protection values */
333 SetLastError(0xdeadbeef);
334 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_RESERVE
, 0);
335 ok(!addr2
, "VirtualAlloc succeeded\n");
336 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
338 SetLastError(0xdeadbeef);
339 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_COMMIT
, 0);
340 ok(!addr2
, "VirtualAlloc succeeded\n");
341 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
343 SetLastError(0xdeadbeef);
344 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_READONLY
| PAGE_EXECUTE
);
345 ok(!addr2
, "VirtualAlloc succeeded\n");
346 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
348 SetLastError(0xdeadbeef);
349 ok(!VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
| PAGE_EXECUTE_WRITECOPY
, &old_prot
),
350 "VirtualProtect succeeded\n");
351 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
353 SetLastError(0xdeadbeef);
354 ok(!VirtualProtect(addr1
, 0x1000, 0, &old_prot
), "VirtualProtect succeeded\n");
355 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
357 SetLastError(0xdeadbeef);
358 ok(!VirtualFree(addr1
, 0x10000, 0), "VirtualFree should fail with type 0\n");
359 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
360 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
362 ok(VirtualFree(addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFree failed\n");
364 /* if the type is MEM_RELEASE, size must be 0 */
365 ok(!VirtualFree(addr1
, 1, MEM_RELEASE
), "VirtualFree should fail\n");
366 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
367 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
369 ok(VirtualFree(addr1
, 0, MEM_RELEASE
), "VirtualFree failed\n");
372 static void test_MapViewOfFile(void)
374 static const char testfile
[] = "testfile.xxx";
376 HANDLE file
, mapping
, map2
;
377 void *ptr
, *ptr2
, *addr
;
378 MEMORY_BASIC_INFORMATION info
;
381 SetLastError(0xdeadbeef);
382 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
383 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
384 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
385 SetEndOfFile( file
);
387 /* read/write mapping */
389 SetLastError(0xdeadbeef);
390 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
391 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
393 SetLastError(0xdeadbeef);
394 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
395 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
396 UnmapViewOfFile( ptr
);
398 /* this fails on win9x but succeeds on NT */
399 SetLastError(0xdeadbeef);
400 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
401 if (ptr
) UnmapViewOfFile( ptr
);
402 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
404 SetLastError(0xdeadbeef);
405 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
406 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
407 UnmapViewOfFile( ptr
);
409 SetLastError(0xdeadbeef);
410 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
411 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
412 UnmapViewOfFile( ptr
);
414 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
415 FILE_MAP_READ
|FILE_MAP_WRITE
, FALSE
, 0 );
416 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
417 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
418 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
419 UnmapViewOfFile( ptr
);
422 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
423 FILE_MAP_READ
, FALSE
, 0 );
424 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
425 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
428 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
430 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
, 0, FALSE
, 0 );
431 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
432 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
433 ok( !ptr
, "MapViewOfFile succeeded\n" );
434 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
436 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
437 FILE_MAP_READ
, FALSE
, 0 );
438 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
439 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
440 ok( ptr
!= NULL
, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
442 else win_skip( "no access checks on win9x\n" );
444 UnmapViewOfFile( ptr
);
446 CloseHandle( mapping
);
448 /* read-only mapping */
450 SetLastError(0xdeadbeef);
451 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
452 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
454 SetLastError(0xdeadbeef);
455 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
456 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
457 UnmapViewOfFile( ptr
);
459 /* this fails on win9x but succeeds on NT */
460 SetLastError(0xdeadbeef);
461 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
462 if (ptr
) UnmapViewOfFile( ptr
);
463 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
465 SetLastError(0xdeadbeef);
466 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
467 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
468 UnmapViewOfFile( ptr
);
470 SetLastError(0xdeadbeef);
471 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
472 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
473 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
474 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
475 CloseHandle( mapping
);
477 /* copy-on-write mapping */
479 SetLastError(0xdeadbeef);
480 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
481 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
483 SetLastError(0xdeadbeef);
484 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
485 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
486 UnmapViewOfFile( ptr
);
488 SetLastError(0xdeadbeef);
489 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
490 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
491 UnmapViewOfFile( ptr
);
493 SetLastError(0xdeadbeef);
494 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
495 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
496 UnmapViewOfFile( ptr
);
498 SetLastError(0xdeadbeef);
499 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
500 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
501 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
502 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
503 CloseHandle( mapping
);
505 /* no access mapping */
507 SetLastError(0xdeadbeef);
508 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
509 /* fails on NT but succeeds on win9x */
510 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
513 SetLastError(0xdeadbeef);
514 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
515 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
516 UnmapViewOfFile( ptr
);
518 SetLastError(0xdeadbeef);
519 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
520 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
521 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
523 SetLastError(0xdeadbeef);
524 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
525 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
526 UnmapViewOfFile( ptr
);
528 SetLastError(0xdeadbeef);
529 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
530 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
531 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
533 CloseHandle( mapping
);
538 /* now try read-only file */
540 SetLastError(0xdeadbeef);
541 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
542 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
544 SetLastError(0xdeadbeef);
545 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
546 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
547 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
548 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
550 SetLastError(0xdeadbeef);
551 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
552 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
553 CloseHandle( mapping
);
555 SetLastError(0xdeadbeef);
556 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
557 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
558 CloseHandle( mapping
);
561 /* now try no access file */
563 SetLastError(0xdeadbeef);
564 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
565 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
567 SetLastError(0xdeadbeef);
568 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
569 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
570 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
571 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
573 SetLastError(0xdeadbeef);
574 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
575 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
576 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
577 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
579 SetLastError(0xdeadbeef);
580 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
581 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
582 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
583 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
586 DeleteFileA( testfile
);
588 SetLastError(0xdeadbeef);
590 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
591 /* nt4 doesn't have Local\\ */
592 if (!file
&& GetLastError() == ERROR_PATH_NOT_FOUND
)
595 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
597 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
599 SetLastError(0xdeadbeef);
600 mapping
= OpenFileMapping( FILE_MAP_READ
, FALSE
, name
);
601 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
602 SetLastError(0xdeadbeef);
603 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
606 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
607 SetLastError(0xdeadbeef);
608 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
609 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
610 SetLastError(0xdeadbeef);
611 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
612 "VirtualQuery error %u\n", GetLastError() );
613 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
614 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
615 ok( info
.AllocationProtect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.AllocationProtect
);
616 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
617 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
618 ok( info
.Protect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.Protect
);
620 else win_skip( "no access checks on win9x\n" );
621 UnmapViewOfFile( ptr
);
622 CloseHandle( mapping
);
624 SetLastError(0xdeadbeef);
625 mapping
= OpenFileMapping( FILE_MAP_WRITE
, FALSE
, name
);
626 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
627 SetLastError(0xdeadbeef);
628 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
631 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
632 SetLastError(0xdeadbeef);
633 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
634 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
635 SetLastError(0xdeadbeef);
636 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
637 "VirtualQuery error %u\n", GetLastError() );
638 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
639 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
640 ok( info
.AllocationProtect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.AllocationProtect
);
641 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
642 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
643 ok( info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
645 else win_skip( "no access checks on win9x\n" );
646 UnmapViewOfFile( ptr
);
647 CloseHandle( mapping
);
651 /* read/write mapping with SEC_RESERVE */
652 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_RESERVE
, 0, MAPPING_SIZE
, NULL
);
653 ok(mapping
!= INVALID_HANDLE_VALUE
, "CreateFileMappingA failed with error %d\n", GetLastError());
655 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
656 ok(ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
658 ptr2
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
659 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
660 ok(ptr2
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
661 trace("mapping same section resulted in views %p and %p\n", ptr
, ptr2
);
663 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
664 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
665 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
666 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
667 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
668 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
669 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
671 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
672 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
673 ok(info
.Protect
== PAGE_NOACCESS
,
674 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info
.Protect
);
678 ok(info
.AllocationProtect
== PAGE_READWRITE
,
679 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
680 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
681 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
686 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
687 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
688 ok(info
.BaseAddress
== ptr2
,
689 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
690 ok(info
.AllocationBase
== ptr2
,
691 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
692 ok(info
.AllocationProtect
== PAGE_READWRITE
,
693 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
694 ok(info
.RegionSize
== MAPPING_SIZE
,
695 "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
696 ok(info
.State
== MEM_RESERVE
,
697 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
698 ok(info
.Protect
== 0,
699 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
700 ok(info
.Type
== MEM_MAPPED
,
701 "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
704 ptr
= VirtualAlloc(ptr
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
705 ok(ptr
!= NULL
, "VirtualAlloc failed with error %d\n", GetLastError());
707 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
708 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
709 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
710 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
711 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
712 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
713 ok(info
.Protect
== PAGE_READONLY
, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
714 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
716 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
717 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
721 ok(info
.AllocationProtect
== PAGE_READWRITE
,
722 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
723 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
726 /* shows that the VirtualAlloc above affects the mapping, not just the
727 * virtual memory in this process - it also affects all other processes
728 * with a view of the mapping, but that isn't tested here */
731 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
732 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
733 ok(info
.BaseAddress
== ptr2
,
734 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
735 ok(info
.AllocationBase
== ptr2
,
736 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
737 ok(info
.AllocationProtect
== PAGE_READWRITE
,
738 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
739 ok(info
.RegionSize
== 0x10000,
740 "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
741 ok(info
.State
== MEM_COMMIT
,
742 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
743 ok(info
.Protect
== PAGE_READWRITE
,
744 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
745 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
748 if(winetest_interactive
)
750 addr
= VirtualAlloc( ptr
, MAPPING_SIZE
, MEM_RESET
, PAGE_READONLY
);
751 ok( addr
== ptr
|| broken(!addr
&& GetLastError() == ERROR_INVALID_PARAMETER
), /* win9x */
752 "VirtualAlloc failed with error %u\n", GetLastError() );
754 ret
= VirtualFree( ptr
, 0x10000, MEM_DECOMMIT
);
755 ok( !ret
|| broken(ret
) /* win9x */, "VirtualFree succeeded\n" );
757 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "VirtualFree failed with %u\n", GetLastError() );
760 skip("MEM_RESET is not currently supported\n");
762 ret
= UnmapViewOfFile(ptr2
);
763 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
764 ret
= UnmapViewOfFile(ptr
);
765 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
766 CloseHandle(mapping
);
768 addr
= VirtualAlloc(NULL
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
769 ok( addr
!= NULL
, "VirtualAlloc failed with error %u\n", GetLastError() );
771 SetLastError(0xdeadbeef);
772 ok( !UnmapViewOfFile(addr
), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
773 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
774 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
775 SetLastError(0xdeadbeef);
776 ok( !UnmapViewOfFile((char *)addr
+ 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
777 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
778 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
779 SetLastError(0xdeadbeef);
780 ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
781 ok( GetLastError() == ERROR_INVALID_ADDRESS
,
782 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
784 ok( VirtualFree(addr
, 0, MEM_RELEASE
), "VirtualFree failed\n" );
787 static DWORD (WINAPI
*pNtMapViewOfSection
)( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
,
788 ULONG zero_bits
, SIZE_T commit_size
,
789 const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
790 ULONG inherit
, ULONG alloc_type
, ULONG protect
);
791 static DWORD (WINAPI
*pNtUnmapViewOfSection
)( HANDLE process
, PVOID addr
);
793 static void test_NtMapViewOfSection(void)
797 static const char testfile
[] = "testfile.xxx";
798 static const char data
[] = "test data for NtMapViewOfSection";
799 char buffer
[sizeof(data
)];
800 HANDLE file
, mapping
;
803 DWORD status
, written
;
805 LARGE_INTEGER offset
;
807 pNtMapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
808 pNtUnmapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
809 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
811 win_skip( "NtMapViewOfSection not available\n" );
815 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
816 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
817 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
818 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
819 SetEndOfFile( file
);
821 /* read/write mapping */
823 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
824 ok( mapping
!= 0, "CreateFileMapping failed\n" );
826 hProcess
= create_target_process("sleep");
827 ok(hProcess
!= NULL
, "Can't start process\n");
832 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
833 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
835 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
836 ok( ret
, "ReadProcessMemory failed\n" );
837 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
838 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
840 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
841 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
843 CloseHandle( mapping
);
845 DeleteFileA( testfile
);
847 TerminateProcess(hProcess
, 0);
848 CloseHandle(hProcess
);
851 static void test_NtAreMappedFilesTheSame(void)
853 static const char testfile
[] = "testfile.xxx";
854 HANDLE file
, file2
, mapping
, map2
;
859 if (!pNtAreMappedFilesTheSame
)
861 win_skip( "NtAreMappedFilesTheSame not available\n" );
865 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
866 NULL
, CREATE_ALWAYS
, 0, 0 );
867 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
868 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
869 SetEndOfFile( file
);
871 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
872 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
874 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
875 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
877 file2
= CreateFileA( testfile
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
878 NULL
, OPEN_EXISTING
, 0, 0 );
879 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
881 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
882 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
883 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
884 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
885 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
886 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
887 UnmapViewOfFile( ptr2
);
889 ptr2
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
890 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
891 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
892 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
893 UnmapViewOfFile( ptr2
);
896 map2
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
897 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
898 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 4096 );
899 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
900 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
901 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
902 UnmapViewOfFile( ptr2
);
904 CloseHandle( file2
);
906 status
= pNtAreMappedFilesTheSame( ptr
, ptr
);
907 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
909 status
= pNtAreMappedFilesTheSame( ptr
, (char *)ptr
+ 30 );
910 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
912 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
913 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
915 status
= pNtAreMappedFilesTheSame( ptr
, (void *)0xdeadbeef );
916 ok( status
== STATUS_CONFLICTING_ADDRESSES
|| status
== STATUS_INVALID_ADDRESS
,
917 "NtAreMappedFilesTheSame returned %x\n", status
);
919 status
= pNtAreMappedFilesTheSame( ptr
, NULL
);
920 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
922 status
= pNtAreMappedFilesTheSame( ptr
, (void *)GetProcessHeap() );
923 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
925 status
= pNtAreMappedFilesTheSame( NULL
, NULL
);
926 ok( status
== STATUS_INVALID_ADDRESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
928 ptr2
= VirtualAlloc( NULL
, 0x10000, MEM_COMMIT
, PAGE_READWRITE
);
929 ok( ptr2
!= NULL
, "VirtualAlloc error %u\n", GetLastError() );
930 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
931 ok( status
== STATUS_CONFLICTING_ADDRESSES
, "NtAreMappedFilesTheSame returned %x\n", status
);
932 VirtualFree( ptr2
, 0, MEM_RELEASE
);
934 UnmapViewOfFile( ptr
);
935 CloseHandle( mapping
);
938 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("ntdll.dll"),
939 GetModuleHandleA("kernel32.dll") );
940 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
941 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
942 GetModuleHandleA("kernel32.dll") );
943 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
944 status
= pNtAreMappedFilesTheSame( GetModuleHandleA("kernel32.dll"),
945 (char *)GetModuleHandleA("kernel32.dll") + 4096 );
946 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
948 GetSystemDirectoryA( path
, MAX_PATH
);
949 strcat( path
, "\\kernel32.dll" );
950 file
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
951 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
953 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
954 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
955 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
956 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
957 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
958 ok( status
== STATUS_NOT_SAME_DEVICE
, "NtAreMappedFilesTheSame returned %x\n", status
);
959 UnmapViewOfFile( ptr
);
960 CloseHandle( mapping
);
962 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
963 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
964 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
965 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
966 status
= pNtAreMappedFilesTheSame( ptr
, GetModuleHandleA("kernel32.dll") );
968 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
970 file2
= CreateFileA( path
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
971 ok( file2
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
972 map2
= CreateFileMappingA( file2
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, NULL
);
973 ok( map2
!= 0, "CreateFileMapping error %u\n", GetLastError() );
974 ptr2
= MapViewOfFile( map2
, FILE_MAP_READ
, 0, 0, 0 );
975 ok( ptr2
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
976 status
= pNtAreMappedFilesTheSame( ptr
, ptr2
);
977 ok( status
== STATUS_SUCCESS
, "NtAreMappedFilesTheSame returned %x\n", status
);
978 UnmapViewOfFile( ptr2
);
980 CloseHandle( file2
);
982 UnmapViewOfFile( ptr
);
983 CloseHandle( mapping
);
986 DeleteFileA( testfile
);
989 static void test_CreateFileMapping(void)
991 HANDLE handle
, handle2
;
993 /* test case sensitivity */
995 SetLastError(0xdeadbeef);
996 handle
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
997 "Wine Test Mapping");
998 ok( handle
!= NULL
, "CreateFileMapping failed with error %u\n", GetLastError());
999 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1001 SetLastError(0xdeadbeef);
1002 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1003 "Wine Test Mapping");
1004 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1005 ok( GetLastError() == ERROR_ALREADY_EXISTS
, "wrong error %u\n", GetLastError());
1006 CloseHandle( handle2
);
1008 SetLastError(0xdeadbeef);
1009 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
1010 "WINE TEST MAPPING");
1011 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
1012 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
1013 CloseHandle( handle2
);
1015 SetLastError(0xdeadbeef);
1016 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "Wine Test Mapping");
1017 ok( handle2
!= NULL
, "OpenFileMapping failed with error %d\n", GetLastError());
1018 CloseHandle( handle2
);
1020 SetLastError(0xdeadbeef);
1021 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "WINE TEST MAPPING");
1022 ok( !handle2
, "OpenFileMapping succeeded\n");
1023 ok( GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_INVALID_NAME
/* win9x */,
1024 "wrong error %u\n", GetLastError());
1026 CloseHandle( handle
);
1029 static void test_IsBadReadPtr(void)
1032 void *ptr
= (void *)0xdeadbeef;
1035 ret
= IsBadReadPtr(NULL
, 0);
1036 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1038 ret
= IsBadReadPtr(NULL
, 1);
1039 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1041 ret
= IsBadReadPtr(ptr
, 0);
1042 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1044 ret
= IsBadReadPtr(ptr
, 1);
1045 ok(ret
== TRUE
, "Expected IsBadReadPtr to return TRUE, got %d\n", ret
);
1047 ret
= IsBadReadPtr(&stackvar
, 0);
1048 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1050 ret
= IsBadReadPtr(&stackvar
, sizeof(char));
1051 ok(ret
== FALSE
, "Expected IsBadReadPtr to return FALSE, got %d\n", ret
);
1054 static void test_IsBadWritePtr(void)
1057 void *ptr
= (void *)0xdeadbeef;
1060 ret
= IsBadWritePtr(NULL
, 0);
1061 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1063 ret
= IsBadWritePtr(NULL
, 1);
1064 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1066 ret
= IsBadWritePtr(ptr
, 0);
1067 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1069 ret
= IsBadWritePtr(ptr
, 1);
1070 ok(ret
== TRUE
, "Expected IsBadWritePtr to return TRUE, got %d\n", ret
);
1072 ret
= IsBadWritePtr(&stackval
, 0);
1073 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1075 ret
= IsBadWritePtr(&stackval
, sizeof(char));
1076 ok(ret
== FALSE
, "Expected IsBadWritePtr to return FALSE, got %d\n", ret
);
1079 static void test_IsBadCodePtr(void)
1082 void *ptr
= (void *)0xdeadbeef;
1085 ret
= IsBadCodePtr(NULL
);
1086 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1088 ret
= IsBadCodePtr(ptr
);
1089 ok(ret
== TRUE
, "Expected IsBadCodePtr to return TRUE, got %d\n", ret
);
1091 ret
= IsBadCodePtr((void *)&stackval
);
1092 ok(ret
== FALSE
, "Expected IsBadCodePtr to return FALSE, got %d\n", ret
);
1095 static void test_write_watch(void)
1098 DWORD ret
, size
, old_prot
;
1099 MEMORY_BASIC_INFORMATION info
;
1104 if (!pGetWriteWatch
|| !pResetWriteWatch
)
1106 win_skip( "GetWriteWatch not supported\n" );
1111 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1113 (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
1115 win_skip( "MEM_WRITE_WATCH not supported\n" );
1118 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1119 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1120 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1121 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1122 ok( info
.AllocationProtect
== PAGE_READWRITE
, "wrong AllocationProtect %x\n", info
.AllocationProtect
);
1123 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1124 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1125 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1126 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
1129 SetLastError( 0xdeadbeef );
1130 ret
= pGetWriteWatch( 0, NULL
, size
, results
, &count
, &pagesize
);
1131 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1132 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
1133 broken( GetLastError() == 0xdeadbeef ), /* win98 */
1134 "wrong error %u\n", GetLastError() );
1136 SetLastError( 0xdeadbeef );
1137 ret
= pGetWriteWatch( 0, GetModuleHandle(0), size
, results
, &count
, &pagesize
);
1140 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1141 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1145 ok( count
== 0, "wrong count %lu\n", count
);
1148 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1149 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1150 ok( count
== 0, "wrong count %lu\n", count
);
1152 base
[pagesize
+ 1] = 0x44;
1155 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1156 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1157 ok( count
== 1, "wrong count %lu\n", count
);
1158 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1161 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1162 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1163 ok( count
== 1, "wrong count %lu\n", count
);
1164 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1167 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1168 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1169 ok( count
== 0, "wrong count %lu\n", count
);
1171 base
[2*pagesize
+ 3] = 0x11;
1172 base
[4*pagesize
+ 8] = 0x11;
1175 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1176 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1177 ok( count
== 2, "wrong count %lu\n", count
);
1178 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1179 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1182 ret
= pGetWriteWatch( 0, base
+ 3*pagesize
, 2*pagesize
, results
, &count
, &pagesize
);
1183 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1184 ok( count
== 1, "wrong count %lu\n", count
);
1185 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1187 ret
= pResetWriteWatch( base
, 3*pagesize
);
1188 ok( !ret
, "pResetWriteWatch failed %u\n", GetLastError() );
1191 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1192 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1193 ok( count
== 1, "wrong count %lu\n", count
);
1194 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
1196 *(DWORD
*)(base
+ 2*pagesize
- 2) = 0xdeadbeef;
1199 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1200 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1201 ok( count
== 3, "wrong count %lu\n", count
);
1202 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1203 ok( results
[1] == base
+ 2*pagesize
, "wrong result %p\n", results
[1] );
1204 ok( results
[2] == base
+ 4*pagesize
, "wrong result %p\n", results
[2] );
1207 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
1208 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1209 ok( count
== 1, "wrong count %lu\n", count
);
1210 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
1213 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1214 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1215 ok( count
== 2, "wrong count %lu\n", count
);
1216 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1217 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1219 /* changing protections doesn't affect watches */
1221 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READONLY
, &old_prot
);
1222 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1223 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1225 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1226 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1227 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1228 ok( info
.RegionSize
== 3*pagesize
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1229 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1230 ok( info
.Protect
== PAGE_READONLY
, "wrong Protect 0x%x\n", info
.Protect
);
1232 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READWRITE
, &old_prot
);
1233 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1234 ok( old_prot
== PAGE_READONLY
, "wrong old prot %x\n", old_prot
);
1237 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1238 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1239 ok( count
== 2, "wrong count %lu\n", count
);
1240 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
1241 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
1243 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
1244 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
1245 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
1246 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
1247 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
1248 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
1250 /* some invalid parameter tests */
1252 SetLastError( 0xdeadbeef );
1254 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1257 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1258 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1260 SetLastError( 0xdeadbeef );
1261 ret
= pGetWriteWatch( 0, base
, size
, results
, NULL
, &pagesize
);
1262 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1263 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1265 SetLastError( 0xdeadbeef );
1267 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, NULL
);
1268 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1269 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1271 SetLastError( 0xdeadbeef );
1273 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1274 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1275 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1277 SetLastError( 0xdeadbeef );
1279 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1280 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1281 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1283 SetLastError( 0xdeadbeef );
1285 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1286 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1287 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1289 SetLastError( 0xdeadbeef );
1291 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1292 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1293 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1295 SetLastError( 0xdeadbeef );
1297 ret
= pGetWriteWatch( 0, base
, size
* 2, results
, &count
, &pagesize
);
1298 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1299 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1301 SetLastError( 0xdeadbeef );
1303 ret
= pGetWriteWatch( 0, base
+ size
- pagesize
, pagesize
+ 1, results
, &count
, &pagesize
);
1304 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1305 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1307 SetLastError( 0xdeadbeef );
1308 ret
= pResetWriteWatch( base
, 0 );
1309 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1310 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1312 SetLastError( 0xdeadbeef );
1313 ret
= pResetWriteWatch( GetModuleHandle(0), size
);
1314 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1315 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1317 else /* win98 is completely different */
1319 SetLastError( 0xdeadbeef );
1321 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1322 ok( ret
== ERROR_INVALID_PARAMETER
, "GetWriteWatch succeeded %u\n", ret
);
1323 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1326 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1327 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1330 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1331 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1334 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1335 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1337 ret
= pResetWriteWatch( base
, 0 );
1338 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1340 ret
= pResetWriteWatch( GetModuleHandle(0), size
);
1341 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1344 VirtualFree( base
, 0, MEM_FREE
);
1346 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1347 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1348 VirtualFree( base
, 0, MEM_FREE
);
1350 base
= VirtualAlloc( 0, size
, MEM_WRITE_WATCH
, PAGE_READWRITE
);
1351 ok( !base
, "VirtualAlloc succeeded\n" );
1352 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1354 /* initial protect doesn't matter */
1356 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_NOACCESS
);
1357 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1358 base
= VirtualAlloc( base
, size
, MEM_COMMIT
, PAGE_NOACCESS
);
1359 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1362 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1363 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1364 ok( count
== 0, "wrong count %lu\n", count
);
1366 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_READWRITE
, &old_prot
);
1367 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1368 ok( old_prot
== PAGE_NOACCESS
, "wrong old prot %x\n", old_prot
);
1370 base
[5*pagesize
+ 200] = 3;
1372 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_NOACCESS
, &old_prot
);
1373 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1374 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1377 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1378 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1379 ok( count
== 1, "wrong count %lu\n", count
);
1380 ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1382 ret
= VirtualFree( base
, size
, MEM_DECOMMIT
);
1383 ok( ret
, "VirtualFree failed %u\n", GetLastError() );
1386 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1387 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1388 ok( count
== 1 || broken(count
== 0), /* win98 */
1389 "wrong count %lu\n", count
);
1390 if (count
) ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1392 VirtualFree( base
, 0, MEM_FREE
);
1399 argc
= winetest_get_mainargs( &argv
);
1403 if (!strcmp(argv
[2], "sleep"))
1405 Sleep(5000); /* spawned process runs for at most 5 seconds */
1412 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
1413 PAGE_EXECUTE_READWRITE
);
1414 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
1415 if (mem
== NULL
) break;
1416 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
1417 ok(ret
, "VirtualFree failed %u\n", GetLastError());
1423 hkernel32
= GetModuleHandleA("kernel32.dll");
1424 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
1425 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
1426 pGetWriteWatch
= (void *) GetProcAddress(hkernel32
, "GetWriteWatch");
1427 pResetWriteWatch
= (void *) GetProcAddress(hkernel32
, "ResetWriteWatch");
1428 pNtAreMappedFilesTheSame
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"),
1429 "NtAreMappedFilesTheSame" );
1431 test_VirtualAllocEx();
1432 test_VirtualAlloc();
1433 test_MapViewOfFile();
1434 test_NtMapViewOfSection();
1435 test_NtAreMappedFilesTheSame();
1436 test_CreateFileMapping();
1437 test_IsBadReadPtr();
1438 test_IsBadWritePtr();
1439 test_IsBadCodePtr();
1440 if(winetest_interactive
)
1443 skip("test_write_watch - MEM_WRITE_WATCH is currently not supported\n");