2 * Unit test suite for Virtual* family of APIs.
4 * Copyright 2004 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/test.h"
30 #define MAPPING_SIZE 0x100000
32 static HINSTANCE hkernel32
;
33 static LPVOID (WINAPI
*pVirtualAllocEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
, DWORD
);
34 static BOOL (WINAPI
*pVirtualFreeEx
)(HANDLE
, LPVOID
, SIZE_T
, DWORD
);
35 static UINT (WINAPI
*pGetWriteWatch
)(DWORD
,LPVOID
,SIZE_T
,LPVOID
*,ULONG_PTR
*,ULONG
*);
36 static UINT (WINAPI
*pResetWriteWatch
)(LPVOID
,SIZE_T
);
38 /* ############################### */
40 static HANDLE
create_target_process(const char *arg
)
43 char cmdline
[MAX_PATH
];
44 PROCESS_INFORMATION pi
;
45 STARTUPINFO si
= { 0 };
48 winetest_get_mainargs( &argv
);
49 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
50 ok(CreateProcess(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
,
51 &si
, &pi
) != 0, "error: %u\n", GetLastError());
52 ok(CloseHandle(pi
.hThread
) != 0, "error %u\n", GetLastError());
56 static void test_VirtualAllocEx(void)
58 const unsigned int alloc_size
= 1<<15;
60 SIZE_T bytes_written
= 0, bytes_read
= 0, i
;
64 MEMORY_BASIC_INFORMATION info
;
67 /* not exported in all windows-versions */
68 if ((!pVirtualAllocEx
) || (!pVirtualFreeEx
)) {
69 win_skip("Virtual{Alloc,Free}Ex not available\n");
73 hProcess
= create_target_process("sleep");
74 ok(hProcess
!= NULL
, "Can't start process\n");
76 SetLastError(0xdeadbeef);
77 addr1
= pVirtualAllocEx(hProcess
, NULL
, alloc_size
, MEM_COMMIT
,
78 PAGE_EXECUTE_READWRITE
);
79 if (!addr1
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
81 win_skip("VirtualAllocEx not implemented\n");
82 TerminateProcess(hProcess
, 0);
83 CloseHandle(hProcess
);
87 src
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
88 dst
= VirtualAlloc( NULL
, alloc_size
, MEM_COMMIT
, PAGE_READWRITE
);
89 for (i
= 0; i
< alloc_size
; i
++)
92 ok(addr1
!= NULL
, "VirtualAllocEx error %u\n", GetLastError());
93 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
94 ok(b
&& (bytes_written
== alloc_size
), "%lu bytes written\n",
96 b
= ReadProcessMemory(hProcess
, addr1
, dst
, alloc_size
, &bytes_read
);
97 ok(b
&& (bytes_read
== alloc_size
), "%lu bytes read\n", bytes_read
);
98 ok(!memcmp(src
, dst
, alloc_size
), "Data from remote process differs\n");
100 /* test invalid source buffers */
102 b
= VirtualProtect( src
+ 0x2000, 0x2000, PAGE_NOACCESS
, &old_prot
);
103 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
104 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
105 ok( !b
, "WriteProcessMemory succeeded\n" );
106 ok( GetLastError() == ERROR_NOACCESS
||
107 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
108 "wrong error %u\n", GetLastError() );
109 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
110 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
111 ok( !b
, "ReadProcessMemory succeeded\n" );
112 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
113 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
115 b
= VirtualProtect( src
, 0x2000, PAGE_NOACCESS
, &old_prot
);
116 ok( b
, "VirtualProtect failed error %u\n", GetLastError() );
117 b
= WriteProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_written
);
118 ok( !b
, "WriteProcessMemory succeeded\n" );
119 ok( GetLastError() == ERROR_NOACCESS
||
120 GetLastError() == ERROR_PARTIAL_COPY
, /* vista */
121 "wrong error %u\n", GetLastError() );
122 ok( bytes_written
== 0, "%lu bytes written\n", bytes_written
);
123 b
= ReadProcessMemory(hProcess
, addr1
, src
, alloc_size
, &bytes_read
);
124 ok( !b
, "ReadProcessMemory succeeded\n" );
125 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
126 ok( bytes_read
== 0, "%lu bytes written\n", bytes_read
);
128 b
= pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
);
129 ok(b
!= 0, "VirtualFreeEx, error %u\n", GetLastError());
131 VirtualFree( src
, 0, MEM_FREE
);
132 VirtualFree( dst
, 0, MEM_FREE
);
135 * The following tests parallel those in test_VirtualAlloc()
138 SetLastError(0xdeadbeef);
139 addr1
= pVirtualAllocEx(hProcess
, 0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
140 ok(addr1
== NULL
, "VirtualAllocEx should fail on zero-sized allocation\n");
141 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
142 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
143 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
145 addr1
= pVirtualAllocEx(hProcess
, 0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
146 ok(addr1
!= NULL
, "VirtualAllocEx failed\n");
148 /* test a not committed memory */
149 memset(&info
, 'q', sizeof(info
));
150 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
), "VirtualQueryEx failed\n");
151 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
152 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
153 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
154 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
155 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
156 /* NT reports Protect == 0 for a not committed memory block */
157 ok(info
.Protect
== 0 /* NT */ ||
158 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
159 "%x != PAGE_NOACCESS\n", info
.Protect
);
160 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
162 SetLastError(0xdeadbeef);
163 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
164 "VirtualProtectEx should fail on a not committed memory\n");
165 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
166 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
167 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
169 addr2
= pVirtualAllocEx(hProcess
, addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
170 ok(addr1
== addr2
, "VirtualAllocEx failed\n");
172 /* test a committed memory */
173 ok(VirtualQueryEx(hProcess
, addr1
, &info
, sizeof(info
)) == sizeof(info
),
174 "VirtualQueryEx failed\n");
175 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
176 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
177 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
178 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
179 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
180 /* this time NT reports PAGE_NOACCESS as well */
181 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
182 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
184 /* this should fail, since not the whole range is committed yet */
185 SetLastError(0xdeadbeef);
186 ok(!VirtualProtectEx(hProcess
, addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
187 "VirtualProtectEx should fail on a not committed memory\n");
188 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
189 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
190 "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
193 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtectEx failed\n");
194 ok(old_prot
== PAGE_NOACCESS
, "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
197 ok(VirtualProtectEx(hProcess
, addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtectEx failed\n");
198 ok(old_prot
== PAGE_READONLY
, "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
200 ok(!pVirtualFreeEx(hProcess
, addr1
, 0x10000, 0),
201 "VirtualFreeEx should fail with type 0\n");
202 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
203 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
205 ok(pVirtualFreeEx(hProcess
, addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFreeEx failed\n");
207 /* if the type is MEM_RELEASE, size must be 0 */
208 ok(!pVirtualFreeEx(hProcess
, addr1
, 1, MEM_RELEASE
),
209 "VirtualFreeEx should fail\n");
210 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
211 "got %u, expected ERROR_INVALID_PARAMETER\n", GetLastError());
213 ok(pVirtualFreeEx(hProcess
, addr1
, 0, MEM_RELEASE
), "VirtualFreeEx failed\n");
215 TerminateProcess(hProcess
, 0);
216 CloseHandle(hProcess
);
219 static void test_VirtualAlloc(void)
223 MEMORY_BASIC_INFORMATION info
;
225 SetLastError(0xdeadbeef);
226 addr1
= VirtualAlloc(0, 0, MEM_RESERVE
, PAGE_NOACCESS
);
227 ok(addr1
== NULL
, "VirtualAlloc should fail on zero-sized allocation\n");
228 ok(GetLastError() == ERROR_INVALID_PARAMETER
/* NT */ ||
229 GetLastError() == ERROR_NOT_ENOUGH_MEMORY
, /* Win9x */
230 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
232 addr1
= VirtualAlloc(0, 0xFFFC, MEM_RESERVE
, PAGE_NOACCESS
);
233 ok(addr1
!= NULL
, "VirtualAlloc failed\n");
235 /* test a not committed memory */
236 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
237 "VirtualQuery failed\n");
238 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
239 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
240 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
241 ok(info
.RegionSize
== 0x10000, "%lx != 0x10000\n", info
.RegionSize
);
242 ok(info
.State
== MEM_RESERVE
, "%x != MEM_RESERVE\n", info
.State
);
243 /* NT reports Protect == 0 for a not committed memory block */
244 ok(info
.Protect
== 0 /* NT */ ||
245 info
.Protect
== PAGE_NOACCESS
, /* Win9x */
246 "%x != PAGE_NOACCESS\n", info
.Protect
);
247 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
249 SetLastError(0xdeadbeef);
250 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
251 "VirtualProtect should fail on a not committed memory\n");
252 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
253 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
254 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
256 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_NOACCESS
);
257 ok(addr1
== addr2
, "VirtualAlloc failed\n");
259 /* test a committed memory */
260 ok(VirtualQuery(addr1
, &info
, sizeof(info
)) == sizeof(info
),
261 "VirtualQuery failed\n");
262 ok(info
.BaseAddress
== addr1
, "%p != %p\n", info
.BaseAddress
, addr1
);
263 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
264 ok(info
.AllocationProtect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.AllocationProtect
);
265 ok(info
.RegionSize
== 0x1000, "%lx != 0x1000\n", info
.RegionSize
);
266 ok(info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
267 /* this time NT reports PAGE_NOACCESS as well */
268 ok(info
.Protect
== PAGE_NOACCESS
, "%x != PAGE_NOACCESS\n", info
.Protect
);
269 ok(info
.Type
== MEM_PRIVATE
, "%x != MEM_PRIVATE\n", info
.Type
);
271 /* this should fail, since not the whole range is committed yet */
272 SetLastError(0xdeadbeef);
273 ok(!VirtualProtect(addr1
, 0xFFFC, PAGE_READONLY
, &old_prot
),
274 "VirtualProtect should fail on a not committed memory\n");
275 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* NT */ ||
276 GetLastError() == ERROR_INVALID_PARAMETER
, /* Win9x */
277 "got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
279 ok(VirtualProtect(addr1
, 0x1000, PAGE_READONLY
, &old_prot
), "VirtualProtect failed\n");
280 ok(old_prot
== PAGE_NOACCESS
,
281 "wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot
);
283 ok(VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
, &old_prot
), "VirtualProtect failed\n");
284 ok(old_prot
== PAGE_READONLY
,
285 "wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot
);
287 /* invalid protection values */
288 SetLastError(0xdeadbeef);
289 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_RESERVE
, 0);
290 ok(!addr2
, "VirtualAlloc succeeded\n");
291 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
293 SetLastError(0xdeadbeef);
294 addr2
= VirtualAlloc(NULL
, 0x1000, MEM_COMMIT
, 0);
295 ok(!addr2
, "VirtualAlloc succeeded\n");
296 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
298 SetLastError(0xdeadbeef);
299 addr2
= VirtualAlloc(addr1
, 0x1000, MEM_COMMIT
, PAGE_READONLY
| PAGE_EXECUTE
);
300 ok(!addr2
, "VirtualAlloc succeeded\n");
301 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
303 SetLastError(0xdeadbeef);
304 ok(!VirtualProtect(addr1
, 0x1000, PAGE_READWRITE
| PAGE_EXECUTE_WRITECOPY
, &old_prot
),
305 "VirtualProtect succeeded\n");
306 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
308 SetLastError(0xdeadbeef);
309 ok(!VirtualProtect(addr1
, 0x1000, 0, &old_prot
), "VirtualProtect succeeded\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError());
312 SetLastError(0xdeadbeef);
313 ok(!VirtualFree(addr1
, 0x10000, 0), "VirtualFree should fail with type 0\n");
314 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
315 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
317 ok(VirtualFree(addr1
, 0x10000, MEM_DECOMMIT
), "VirtualFree failed\n");
319 /* if the type is MEM_RELEASE, size must be 0 */
320 ok(!VirtualFree(addr1
, 1, MEM_RELEASE
), "VirtualFree should fail\n");
321 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
322 "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
324 ok(VirtualFree(addr1
, 0, MEM_RELEASE
), "VirtualFree failed\n");
327 static void test_MapViewOfFile(void)
329 static const char testfile
[] = "testfile.xxx";
331 HANDLE file
, mapping
, map2
;
333 MEMORY_BASIC_INFORMATION info
;
336 SetLastError(0xdeadbeef);
337 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
338 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
339 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
340 SetEndOfFile( file
);
342 /* read/write mapping */
344 SetLastError(0xdeadbeef);
345 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
346 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
348 SetLastError(0xdeadbeef);
349 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
350 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
351 UnmapViewOfFile( ptr
);
353 /* this fails on win9x but succeeds on NT */
354 SetLastError(0xdeadbeef);
355 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
356 if (ptr
) UnmapViewOfFile( ptr
);
357 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
359 SetLastError(0xdeadbeef);
360 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
361 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
362 UnmapViewOfFile( ptr
);
364 SetLastError(0xdeadbeef);
365 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
366 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
367 UnmapViewOfFile( ptr
);
369 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
370 FILE_MAP_READ
|FILE_MAP_WRITE
, FALSE
, 0 );
371 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
372 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
373 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
374 UnmapViewOfFile( ptr
);
377 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
378 FILE_MAP_READ
, FALSE
, 0 );
379 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
380 ptr
= MapViewOfFile( map2
, FILE_MAP_WRITE
, 0, 0, 4096 );
383 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
385 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
, 0, FALSE
, 0 );
386 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
387 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
388 ok( !ptr
, "MapViewOfFile succeeded\n" );
389 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
391 ret
= DuplicateHandle( GetCurrentProcess(), mapping
, GetCurrentProcess(), &map2
,
392 FILE_MAP_READ
, FALSE
, 0 );
393 ok( ret
, "DuplicateHandle failed error %u\n", GetLastError());
394 ptr
= MapViewOfFile( map2
, 0, 0, 0, 4096 );
395 ok( ptr
!= NULL
, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
397 else win_skip( "no access checks on win9x\n" );
399 UnmapViewOfFile( ptr
);
401 CloseHandle( mapping
);
403 /* read-only mapping */
405 SetLastError(0xdeadbeef);
406 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
407 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
409 SetLastError(0xdeadbeef);
410 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
411 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
412 UnmapViewOfFile( ptr
);
414 /* this fails on win9x but succeeds on NT */
415 SetLastError(0xdeadbeef);
416 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
417 if (ptr
) UnmapViewOfFile( ptr
);
418 else ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
420 SetLastError(0xdeadbeef);
421 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
422 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
423 UnmapViewOfFile( ptr
);
425 SetLastError(0xdeadbeef);
426 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
427 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
428 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
429 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
430 CloseHandle( mapping
);
432 /* copy-on-write mapping */
434 SetLastError(0xdeadbeef);
435 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
436 ok( mapping
!= 0, "CreateFileMapping error %u\n", GetLastError() );
438 SetLastError(0xdeadbeef);
439 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
440 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
441 UnmapViewOfFile( ptr
);
443 SetLastError(0xdeadbeef);
444 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
445 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
446 UnmapViewOfFile( ptr
);
448 SetLastError(0xdeadbeef);
449 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
450 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
451 UnmapViewOfFile( ptr
);
453 SetLastError(0xdeadbeef);
454 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
455 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
456 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
457 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
458 CloseHandle( mapping
);
460 /* no access mapping */
462 SetLastError(0xdeadbeef);
463 mapping
= CreateFileMappingA( file
, NULL
, PAGE_NOACCESS
, 0, 4096, NULL
);
464 /* fails on NT but succeeds on win9x */
465 if (!mapping
) ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
468 SetLastError(0xdeadbeef);
469 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 4096 );
470 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
471 UnmapViewOfFile( ptr
);
473 SetLastError(0xdeadbeef);
474 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 4096 );
475 ok( !ptr
, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
476 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
478 SetLastError(0xdeadbeef);
479 ptr
= MapViewOfFile( mapping
, 0, 0, 0, 4096 );
480 ok( ptr
!= NULL
, "MapViewOfFile 0 error %u\n", GetLastError() );
481 UnmapViewOfFile( ptr
);
483 SetLastError(0xdeadbeef);
484 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 4096 );
485 ok( !ptr
, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
486 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "Wrong error %d\n", GetLastError() );
488 CloseHandle( mapping
);
493 /* now try read-only file */
495 SetLastError(0xdeadbeef);
496 file
= CreateFileA( testfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
497 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
499 SetLastError(0xdeadbeef);
500 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
501 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
502 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
503 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
505 SetLastError(0xdeadbeef);
506 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
507 ok( mapping
!= 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
508 CloseHandle( mapping
);
510 SetLastError(0xdeadbeef);
511 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
512 ok( mapping
!= 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
513 CloseHandle( mapping
);
516 /* now try no access file */
518 SetLastError(0xdeadbeef);
519 file
= CreateFileA( testfile
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
520 ok( file
!= INVALID_HANDLE_VALUE
, "CreateFile error %u\n", GetLastError() );
522 SetLastError(0xdeadbeef);
523 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
524 ok( !mapping
, "CreateFileMapping PAGE_READWRITE succeeded\n" );
525 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
526 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
528 SetLastError(0xdeadbeef);
529 mapping
= CreateFileMappingA( file
, NULL
, PAGE_WRITECOPY
, 0, 4096, NULL
);
530 ok( !mapping
, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
531 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
532 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
534 SetLastError(0xdeadbeef);
535 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READONLY
, 0, 4096, NULL
);
536 ok( !mapping
, "CreateFileMapping PAGE_READONLY succeeded\n" );
537 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
538 GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
541 DeleteFileA( testfile
);
543 SetLastError(0xdeadbeef);
545 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
546 /* nt4 doesn't have Local\\ */
547 if (!file
&& GetLastError() == ERROR_PATH_NOT_FOUND
)
550 file
= CreateFileMapping( INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, name
);
552 ok( file
!= 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
554 SetLastError(0xdeadbeef);
555 mapping
= OpenFileMapping( FILE_MAP_READ
, FALSE
, name
);
556 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
557 SetLastError(0xdeadbeef);
558 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
561 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
562 SetLastError(0xdeadbeef);
563 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
564 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
565 SetLastError(0xdeadbeef);
566 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
567 "VirtualQuery error %u\n", GetLastError() );
568 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
569 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
570 ok( info
.AllocationProtect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.AllocationProtect
);
571 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
572 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
573 ok( info
.Protect
== PAGE_READONLY
, "%x != PAGE_READONLY\n", info
.Protect
);
575 else win_skip( "no access checks on win9x\n" );
576 UnmapViewOfFile( ptr
);
577 CloseHandle( mapping
);
579 SetLastError(0xdeadbeef);
580 mapping
= OpenFileMapping( FILE_MAP_WRITE
, FALSE
, name
);
581 ok( mapping
!= 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
582 SetLastError(0xdeadbeef);
583 ptr
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
586 ok( GetLastError() == ERROR_ACCESS_DENIED
, "Wrong error %d\n", GetLastError() );
587 SetLastError(0xdeadbeef);
588 ptr
= MapViewOfFile( mapping
, FILE_MAP_WRITE
, 0, 0, 0 );
589 ok( ptr
!= NULL
, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
590 SetLastError(0xdeadbeef);
591 ok( VirtualQuery( ptr
, &info
, sizeof(info
) ) == sizeof(info
),
592 "VirtualQuery error %u\n", GetLastError() );
593 ok( info
.BaseAddress
== ptr
, "%p != %p\n", info
.BaseAddress
, ptr
);
594 ok( info
.AllocationBase
== ptr
, "%p != %p\n", info
.AllocationBase
, ptr
);
595 ok( info
.AllocationProtect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.AllocationProtect
);
596 ok( info
.RegionSize
== 4096, "%lx != 4096\n", info
.RegionSize
);
597 ok( info
.State
== MEM_COMMIT
, "%x != MEM_COMMIT\n", info
.State
);
598 ok( info
.Protect
== PAGE_READWRITE
, "%x != PAGE_READWRITE\n", info
.Protect
);
600 else win_skip( "no access checks on win9x\n" );
601 UnmapViewOfFile( ptr
);
602 CloseHandle( mapping
);
606 /* read/write mapping with SEC_RESERVE */
607 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
| SEC_RESERVE
, 0, MAPPING_SIZE
, NULL
);
608 ok(mapping
!= INVALID_HANDLE_VALUE
, "CreateFileMappingA failed with error %d\n", GetLastError());
610 ptr
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
611 ok(ptr
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
613 ptr2
= MapViewOfFile(mapping
, FILE_MAP_WRITE
, 0, 0, 0);
614 /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
615 ok(ptr2
!= NULL
, "MapViewOfFile failed with error %d\n", GetLastError());
616 trace("mapping same section resulted in views %p and %p\n", ptr
, ptr2
);
618 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
619 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
620 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
621 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
622 ok(info
.RegionSize
== MAPPING_SIZE
, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
623 ok(info
.State
== MEM_RESERVE
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
624 if (info
.Type
== MEM_PRIVATE
) /* win9x is different for uncommitted mappings */
626 ok(info
.AllocationProtect
== PAGE_NOACCESS
,
627 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info
.AllocationProtect
);
628 ok(info
.Protect
== PAGE_NOACCESS
,
629 "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info
.Protect
);
633 ok(info
.AllocationProtect
== PAGE_READWRITE
,
634 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
635 ok(info
.Protect
== 0, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
636 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
641 ret
= VirtualQuery(ptr2
, &info
, sizeof(info
));
642 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
643 ok(info
.BaseAddress
== ptr2
,
644 "BaseAddress should have been %p but was %p instead\n", ptr2
, info
.BaseAddress
);
645 ok(info
.AllocationBase
== ptr2
,
646 "AllocationBase should have been %p but was %p instead\n", ptr2
, info
.AllocationBase
);
647 ok(info
.AllocationProtect
== PAGE_READWRITE
,
648 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
649 ok(info
.RegionSize
== MAPPING_SIZE
,
650 "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE
, (unsigned int)info
.RegionSize
);
651 ok(info
.State
== MEM_RESERVE
,
652 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
653 ok(info
.Protect
== 0,
654 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
655 ok(info
.Type
== MEM_MAPPED
,
656 "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
659 ptr
= VirtualAlloc(ptr
, 0x10000, MEM_COMMIT
, PAGE_READONLY
);
660 ok(ptr
!= NULL
, "VirtualAlloc failed with error %d\n", GetLastError());
662 ret
= VirtualQuery(ptr
, &info
, sizeof(info
));
663 ok(ret
, "VirtualQuery failed with error %d\n", GetLastError());
664 ok(info
.BaseAddress
== ptr
, "BaseAddress should have been %p but was %p instead\n", ptr
, info
.BaseAddress
);
665 ok(info
.AllocationBase
== ptr
, "AllocationBase should have been %p but was %p instead\n", ptr
, info
.AllocationBase
);
666 ok(info
.RegionSize
== 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
667 ok(info
.State
== MEM_COMMIT
, "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
668 ok(info
.Protect
== PAGE_READONLY
, "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
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
);
676 ok(info
.AllocationProtect
== PAGE_READWRITE
,
677 "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info
.AllocationProtect
);
678 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
681 /* shows that the VirtualAlloc above affects the mapping, not just the
682 * virtual memory in this process - it also affects all other processes
683 * with a view of the mapping, but that isn't tested here */
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
== 0x10000,
695 "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info
.RegionSize
);
696 ok(info
.State
== MEM_COMMIT
,
697 "State should have been MEM_RESERVE instead of 0x%x\n", info
.State
);
698 ok(info
.Protect
== PAGE_READWRITE
,
699 "Protect should have been 0 instead of 0x%x\n", info
.Protect
);
700 ok(info
.Type
== MEM_MAPPED
, "Type should have been MEM_MAPPED instead of 0x%x\n", info
.Type
);
703 ret
= VirtualFree( ptr
, 0x10000, MEM_DECOMMIT
);
704 ok( !ret
|| broken(ret
) /* win9x */, "VirtualFree succeeded\n" );
706 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "VirtualFree failed with %u\n", GetLastError() );
708 ret
= UnmapViewOfFile(ptr2
);
709 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
710 ret
= UnmapViewOfFile(ptr
);
711 ok(ret
, "UnmapViewOfFile failed with error %d\n", GetLastError());
712 CloseHandle(mapping
);
715 static DWORD (WINAPI
*pNtMapViewOfSection
)( HANDLE handle
, HANDLE process
, PVOID
*addr_ptr
,
716 ULONG zero_bits
, SIZE_T commit_size
,
717 const LARGE_INTEGER
*offset_ptr
, SIZE_T
*size_ptr
,
718 ULONG inherit
, ULONG alloc_type
, ULONG protect
);
719 static DWORD (WINAPI
*pNtUnmapViewOfSection
)( HANDLE process
, PVOID addr
);
721 static void test_NtMapViewOfSection(void)
725 static const char testfile
[] = "testfile.xxx";
726 static const char data
[] = "test data for NtMapViewOfSection";
727 char buffer
[sizeof(data
)];
728 HANDLE file
, mapping
;
731 DWORD status
, written
;
733 LARGE_INTEGER offset
;
735 pNtMapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
736 pNtUnmapViewOfSection
= (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
737 if (!pNtMapViewOfSection
|| !pNtUnmapViewOfSection
)
739 win_skip( "NtMapViewOfSection not available\n" );
743 file
= CreateFileA( testfile
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
744 ok( file
!= INVALID_HANDLE_VALUE
, "Failed to create test file\n" );
745 WriteFile( file
, data
, sizeof(data
), &written
, NULL
);
746 SetFilePointer( file
, 4096, NULL
, FILE_BEGIN
);
747 SetEndOfFile( file
);
749 /* read/write mapping */
751 mapping
= CreateFileMappingA( file
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
752 ok( mapping
!= 0, "CreateFileMapping failed\n" );
754 hProcess
= create_target_process("sleep");
755 ok(hProcess
!= NULL
, "Can't start process\n");
760 status
= pNtMapViewOfSection( mapping
, hProcess
, &ptr
, 0, 0, &offset
, &size
, 1, 0, PAGE_READWRITE
);
761 ok( !status
, "NtMapViewOfSection failed status %x\n", status
);
763 ret
= ReadProcessMemory( hProcess
, ptr
, buffer
, sizeof(buffer
), &result
);
764 ok( ret
, "ReadProcessMemory failed\n" );
765 ok( result
== sizeof(buffer
), "ReadProcessMemory didn't read all data (%lx)\n", result
);
766 ok( !memcmp( buffer
, data
, sizeof(buffer
) ), "Wrong data read\n" );
768 status
= pNtUnmapViewOfSection( hProcess
, ptr
);
769 ok( !status
, "NtUnmapViewOfSection failed status %x\n", status
);
771 CloseHandle( mapping
);
773 DeleteFileA( testfile
);
775 TerminateProcess(hProcess
, 0);
776 CloseHandle(hProcess
);
779 static void test_CreateFileMapping(void)
781 HANDLE handle
, handle2
;
783 /* test case sensitivity */
785 SetLastError(0xdeadbeef);
786 handle
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
787 "Wine Test Mapping");
788 ok( handle
!= NULL
, "CreateFileMapping failed with error %u\n", GetLastError());
789 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
791 SetLastError(0xdeadbeef);
792 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
793 "Wine Test Mapping");
794 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
795 ok( GetLastError() == ERROR_ALREADY_EXISTS
, "wrong error %u\n", GetLastError());
796 CloseHandle( handle2
);
798 SetLastError(0xdeadbeef);
799 handle2
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
, 0, 0x1000,
800 "WINE TEST MAPPING");
801 ok( handle2
!= NULL
, "CreateFileMapping failed with error %d\n", GetLastError());
802 ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
803 CloseHandle( handle2
);
805 SetLastError(0xdeadbeef);
806 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "Wine Test Mapping");
807 ok( handle2
!= NULL
, "OpenFileMapping failed with error %d\n", GetLastError());
808 CloseHandle( handle2
);
810 SetLastError(0xdeadbeef);
811 handle2
= OpenFileMappingA( FILE_MAP_ALL_ACCESS
, FALSE
, "WINE TEST MAPPING");
812 ok( !handle2
, "OpenFileMapping succeeded\n");
813 ok( GetLastError() == ERROR_FILE_NOT_FOUND
|| GetLastError() == ERROR_INVALID_NAME
/* win9x */,
814 "wrong error %u\n", GetLastError());
816 CloseHandle( handle
);
819 static void test_BadPtr(void)
821 void *ptr
= (void*)1;
822 /* We assume address 1 is not mapped. */
823 ok(IsBadReadPtr(ptr
,1),"IsBadReadPtr(1) failed.\n");
824 ok(IsBadWritePtr(ptr
,1),"IsBadWritePtr(1) failed.\n");
825 ok(IsBadCodePtr(ptr
),"IsBadCodePtr(1) failed.\n");
828 static void test_write_watch(void)
831 DWORD ret
, size
, old_prot
;
832 MEMORY_BASIC_INFORMATION info
;
837 if (!pGetWriteWatch
|| !pResetWriteWatch
)
839 win_skip( "GetWriteWatch not supported\n" );
844 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_COMMIT
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
846 (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_NOT_SUPPORTED
))
848 win_skip( "MEM_WRITE_WATCH not supported\n" );
851 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
852 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
853 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
854 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
855 ok( info
.AllocationProtect
== PAGE_READWRITE
, "wrong AllocationProtect %x\n", info
.AllocationProtect
);
856 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
857 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
858 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
859 ok( info
.Type
== MEM_PRIVATE
, "wrong Type 0x%x\n", info
.Type
);
862 SetLastError( 0xdeadbeef );
863 ret
= pGetWriteWatch( 0, NULL
, size
, results
, &count
, &pagesize
);
864 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
865 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
866 broken( GetLastError() == 0xdeadbeef ), /* win98 */
867 "wrong error %u\n", GetLastError() );
869 SetLastError( 0xdeadbeef );
870 ret
= pGetWriteWatch( 0, GetModuleHandle(0), size
, results
, &count
, &pagesize
);
873 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
874 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
878 ok( count
== 0, "wrong count %lu\n", count
);
881 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
882 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
883 ok( count
== 0, "wrong count %lu\n", count
);
885 base
[pagesize
+ 1] = 0x44;
888 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
889 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
890 ok( count
== 1, "wrong count %lu\n", count
);
891 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
894 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
895 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
896 ok( count
== 1, "wrong count %lu\n", count
);
897 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
900 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
901 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
902 ok( count
== 0, "wrong count %lu\n", count
);
904 base
[2*pagesize
+ 3] = 0x11;
905 base
[4*pagesize
+ 8] = 0x11;
908 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
909 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
910 ok( count
== 2, "wrong count %lu\n", count
);
911 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
912 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
915 ret
= pGetWriteWatch( 0, base
+ 3*pagesize
, 2*pagesize
, results
, &count
, &pagesize
);
916 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
917 ok( count
== 1, "wrong count %lu\n", count
);
918 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
920 ret
= pResetWriteWatch( base
, 3*pagesize
);
921 ok( !ret
, "pResetWriteWatch failed %u\n", GetLastError() );
924 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
925 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
926 ok( count
== 1, "wrong count %lu\n", count
);
927 ok( results
[0] == base
+ 4*pagesize
, "wrong result %p\n", results
[0] );
929 *(DWORD
*)(base
+ 2*pagesize
- 2) = 0xdeadbeef;
932 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
933 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
934 ok( count
== 3, "wrong count %lu\n", count
);
935 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
936 ok( results
[1] == base
+ 2*pagesize
, "wrong result %p\n", results
[1] );
937 ok( results
[2] == base
+ 4*pagesize
, "wrong result %p\n", results
[2] );
940 ret
= pGetWriteWatch( WRITE_WATCH_FLAG_RESET
, base
, size
, results
, &count
, &pagesize
);
941 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
942 ok( count
== 1, "wrong count %lu\n", count
);
943 ok( results
[0] == base
+ pagesize
, "wrong result %p\n", results
[0] );
946 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
947 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
948 ok( count
== 2, "wrong count %lu\n", count
);
949 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
950 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
952 /* changing protections doesn't affect watches */
954 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READONLY
, &old_prot
);
955 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
956 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
958 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
959 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
960 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
961 ok( info
.RegionSize
== 3*pagesize
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
962 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
963 ok( info
.Protect
== PAGE_READONLY
, "wrong Protect 0x%x\n", info
.Protect
);
965 ret
= VirtualProtect( base
, 3*pagesize
, PAGE_READWRITE
, &old_prot
);
966 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
967 ok( old_prot
== PAGE_READONLY
, "wrong old prot %x\n", old_prot
);
970 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
971 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
972 ok( count
== 2, "wrong count %lu\n", count
);
973 ok( results
[0] == base
+ 2*pagesize
, "wrong result %p\n", results
[0] );
974 ok( results
[1] == base
+ 4*pagesize
, "wrong result %p\n", results
[1] );
976 ret
= VirtualQuery( base
, &info
, sizeof(info
) );
977 ok(ret
, "VirtualQuery failed %u\n", GetLastError());
978 ok( info
.BaseAddress
== base
, "BaseAddress %p instead of %p\n", info
.BaseAddress
, base
);
979 ok( info
.RegionSize
== size
, "wrong RegionSize 0x%lx\n", info
.RegionSize
);
980 ok( info
.State
== MEM_COMMIT
, "wrong State 0x%x\n", info
.State
);
981 ok( info
.Protect
== PAGE_READWRITE
, "wrong Protect 0x%x\n", info
.Protect
);
983 /* some invalid parameter tests */
985 SetLastError( 0xdeadbeef );
987 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
990 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
991 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
993 SetLastError( 0xdeadbeef );
994 ret
= pGetWriteWatch( 0, base
, size
, results
, NULL
, &pagesize
);
995 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
996 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
998 SetLastError( 0xdeadbeef );
1000 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, NULL
);
1001 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1002 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1004 SetLastError( 0xdeadbeef );
1006 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1007 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1008 ok( GetLastError() == ERROR_NOACCESS
, "wrong error %u\n", GetLastError() );
1010 SetLastError( 0xdeadbeef );
1012 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1013 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1014 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1016 SetLastError( 0xdeadbeef );
1018 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1019 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1020 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1022 SetLastError( 0xdeadbeef );
1024 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1025 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1026 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1028 SetLastError( 0xdeadbeef );
1030 ret
= pGetWriteWatch( 0, base
, size
* 2, results
, &count
, &pagesize
);
1031 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1032 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1034 SetLastError( 0xdeadbeef );
1036 ret
= pGetWriteWatch( 0, base
+ size
- pagesize
, pagesize
+ 1, results
, &count
, &pagesize
);
1037 ok( ret
== ~0u, "GetWriteWatch succeeded %u\n", ret
);
1038 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1040 SetLastError( 0xdeadbeef );
1041 ret
= pResetWriteWatch( base
, 0 );
1042 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1043 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1045 SetLastError( 0xdeadbeef );
1046 ret
= pResetWriteWatch( GetModuleHandle(0), size
);
1047 ok( ret
== ~0u, "ResetWriteWatch succeeded %u\n", ret
);
1048 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1050 else /* win98 is completely different */
1052 SetLastError( 0xdeadbeef );
1054 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1055 ok( ret
== ERROR_INVALID_PARAMETER
, "GetWriteWatch succeeded %u\n", ret
);
1056 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1059 ret
= pGetWriteWatch( 0, base
, size
, NULL
, &count
, &pagesize
);
1060 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1063 ret
= pGetWriteWatch( 0xdeadbeef, base
, size
, results
, &count
, &pagesize
);
1064 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1067 ret
= pGetWriteWatch( 0, base
, 0, results
, &count
, &pagesize
);
1068 ok( !ret
, "GetWriteWatch failed %u\n", ret
);
1070 ret
= pResetWriteWatch( base
, 0 );
1071 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1073 ret
= pResetWriteWatch( GetModuleHandle(0), size
);
1074 ok( !ret
, "ResetWriteWatch failed %u\n", ret
);
1077 VirtualFree( base
, 0, MEM_FREE
);
1079 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_READWRITE
);
1080 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1081 VirtualFree( base
, 0, MEM_FREE
);
1083 base
= VirtualAlloc( 0, size
, MEM_WRITE_WATCH
, PAGE_READWRITE
);
1084 ok( !base
, "VirtualAlloc succeeded\n" );
1085 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "wrong error %u\n", GetLastError() );
1087 /* initial protect doesn't matter */
1089 base
= VirtualAlloc( 0, size
, MEM_RESERVE
| MEM_WRITE_WATCH
, PAGE_NOACCESS
);
1090 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1091 base
= VirtualAlloc( base
, size
, MEM_COMMIT
, PAGE_NOACCESS
);
1092 ok( base
!= NULL
, "VirtualAlloc failed %u\n", GetLastError() );
1095 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1096 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1097 ok( count
== 0, "wrong count %lu\n", count
);
1099 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_READWRITE
, &old_prot
);
1100 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1101 ok( old_prot
== PAGE_NOACCESS
, "wrong old prot %x\n", old_prot
);
1103 base
[5*pagesize
+ 200] = 3;
1105 ret
= VirtualProtect( base
, 6*pagesize
, PAGE_NOACCESS
, &old_prot
);
1106 ok( ret
, "VirtualProtect failed error %u\n", GetLastError() );
1107 ok( old_prot
== PAGE_READWRITE
, "wrong old prot %x\n", old_prot
);
1110 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1111 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1112 ok( count
== 1, "wrong count %lu\n", count
);
1113 ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1115 ret
= VirtualFree( base
, size
, MEM_DECOMMIT
);
1116 ok( ret
, "VirtualFree failed %u\n", GetLastError() );
1119 ret
= pGetWriteWatch( 0, base
, size
, results
, &count
, &pagesize
);
1120 ok( !ret
, "GetWriteWatch failed %u\n", GetLastError() );
1121 ok( count
== 1 || broken(count
== 0), /* win98 */
1122 "wrong count %lu\n", count
);
1123 if (count
) ok( results
[0] == base
+ 5*pagesize
, "wrong result %p\n", results
[0] );
1125 VirtualFree( base
, 0, MEM_FREE
);
1132 argc
= winetest_get_mainargs( &argv
);
1136 if (!strcmp(argv
[2], "sleep"))
1138 Sleep(5000); /* spawned process runs for at most 5 seconds */
1145 mem
= VirtualAlloc(NULL
, 1<<20, MEM_COMMIT
|MEM_RESERVE
,
1146 PAGE_EXECUTE_READWRITE
);
1147 ok(mem
!= NULL
, "VirtualAlloc failed %u\n", GetLastError());
1148 if (mem
== NULL
) break;
1149 ret
= VirtualFree(mem
, 0, MEM_RELEASE
);
1150 ok(ret
, "VirtualFree failed %u\n", GetLastError());
1156 hkernel32
= GetModuleHandleA("kernel32.dll");
1157 pVirtualAllocEx
= (void *) GetProcAddress(hkernel32
, "VirtualAllocEx");
1158 pVirtualFreeEx
= (void *) GetProcAddress(hkernel32
, "VirtualFreeEx");
1159 pGetWriteWatch
= (void *) GetProcAddress(hkernel32
, "GetWriteWatch");
1160 pResetWriteWatch
= (void *) GetProcAddress(hkernel32
, "ResetWriteWatch");
1162 test_VirtualAllocEx();
1163 test_VirtualAlloc();
1164 test_MapViewOfFile();
1165 test_NtMapViewOfSection();
1166 test_CreateFileMapping();