[KERNEL32_WINETEST]
[reactos.git] / rostests / winetests / kernel32 / virtual.c
1 /*
2 * Unit test suite for Virtual* family of APIs.
3 *
4 * Copyright 2004 Dmitry Timoshkov
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "winerror.h"
30 #include "wine/test.h"
31
32 #define NUM_THREADS 4
33 #define MAPPING_SIZE 0x100000
34
35 static HINSTANCE hkernel32;
36 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
37 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
38 static UINT (WINAPI *pGetWriteWatch)(DWORD,LPVOID,SIZE_T,LPVOID*,ULONG_PTR*,ULONG*);
39 static UINT (WINAPI *pResetWriteWatch)(LPVOID,SIZE_T);
40 static NTSTATUS (WINAPI *pNtAreMappedFilesTheSame)(PVOID,PVOID);
41
42 /* ############################### */
43
44 static HANDLE create_target_process(const char *arg)
45 {
46 char **argv;
47 char cmdline[MAX_PATH];
48 PROCESS_INFORMATION pi;
49 BOOL ret;
50 STARTUPINFO si = { 0 };
51 si.cb = sizeof(si);
52
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());
59 return pi.hProcess;
60 }
61
62 static void test_VirtualAllocEx(void)
63 {
64 const unsigned int alloc_size = 1<<15;
65 char *src, *dst;
66 SIZE_T bytes_written = 0, bytes_read = 0, i;
67 void *addr1, *addr2;
68 BOOL b;
69 DWORD old_prot;
70 MEMORY_BASIC_INFORMATION info;
71 HANDLE hProcess;
72
73 /* not exported in all windows-versions */
74 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
75 win_skip("Virtual{Alloc,Free}Ex not available\n");
76 return;
77 }
78
79 hProcess = create_target_process("sleep");
80 ok(hProcess != NULL, "Can't start process\n");
81
82 SetLastError(0xdeadbeef);
83 addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
84 PAGE_EXECUTE_READWRITE);
85 if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
86 { /* Win9x */
87 win_skip("VirtualAllocEx not implemented\n");
88 TerminateProcess(hProcess, 0);
89 CloseHandle(hProcess);
90 return;
91 }
92
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++)
96 src[i] = i & 0xff;
97
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",
101 bytes_written);
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");
105
106 /* test invalid source buffers */
107
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 );
120
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 );
133
134 b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
135 ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
136
137 VirtualFree( src, 0, MEM_FREE );
138 VirtualFree( dst, 0, MEM_FREE );
139
140 /*
141 * The following tests parallel those in test_VirtualAlloc()
142 */
143
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());
150
151 addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
152 ok(addr1 != NULL, "VirtualAllocEx failed\n");
153
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);
167
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());
174
175 addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
176 ok(addr1 == addr2, "VirtualAllocEx failed\n");
177
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);
189
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());
197
198 old_prot = 0;
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);
201
202 old_prot = 0;
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);
205
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());
210
211 ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
212
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());
218
219 ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
220
221 TerminateProcess(hProcess, 0);
222 CloseHandle(hProcess);
223 }
224
225 static void test_VirtualAlloc(void)
226 {
227 void *addr1, *addr2;
228 DWORD old_prot;
229 MEMORY_BASIC_INFORMATION info;
230
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());
237
238 addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
239 ok(addr1 != NULL, "VirtualAlloc failed\n");
240
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);
254
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());
261
262 addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
263 ok(addr1 == addr2, "VirtualAlloc failed\n");
264
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);
276
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());
284
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);
288
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);
292
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 );
300
301 if(winetest_interactive)
302 {
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 );
307 if (addr2)
308 {
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);
314
315 addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
316 ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
317
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);
323
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() );
327 }
328 }
329 else
330 skip("MEM_RESET is not currently supported\n");
331
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());
337
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());
342
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());
347
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());
352
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());
356
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());
361
362 ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
363
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());
368
369 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
370 }
371
372 static void test_MapViewOfFile(void)
373 {
374 static const char testfile[] = "testfile.xxx";
375 const char *name;
376 HANDLE file, mapping, map2;
377 void *ptr, *ptr2, *addr;
378 MEMORY_BASIC_INFORMATION info;
379 BOOL ret;
380
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 );
386
387 /* read/write mapping */
388
389 SetLastError(0xdeadbeef);
390 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
391 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
392
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 );
397
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() );
403
404 SetLastError(0xdeadbeef);
405 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
406 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
407 UnmapViewOfFile( ptr );
408
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 );
413
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 );
420 CloseHandle( map2 );
421
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 );
426 if (!ptr)
427 {
428 ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
429 CloseHandle( map2 );
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() );
435 CloseHandle( map2 );
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() );
441 }
442 else win_skip( "no access checks on win9x\n" );
443
444 UnmapViewOfFile( ptr );
445 CloseHandle( map2 );
446 CloseHandle( mapping );
447
448 /* read-only mapping */
449
450 SetLastError(0xdeadbeef);
451 mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
452 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
453
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 );
458
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() );
464
465 SetLastError(0xdeadbeef);
466 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
467 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
468 UnmapViewOfFile( ptr );
469
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 );
476
477 /* copy-on-write mapping */
478
479 SetLastError(0xdeadbeef);
480 mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
481 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
482
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 );
487
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 );
492
493 SetLastError(0xdeadbeef);
494 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
495 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
496 UnmapViewOfFile( ptr );
497
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 );
504
505 /* no access mapping */
506
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() );
511 else
512 {
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 );
517
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() );
522
523 SetLastError(0xdeadbeef);
524 ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
525 ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
526 UnmapViewOfFile( ptr );
527
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() );
532
533 CloseHandle( mapping );
534 }
535
536 CloseHandle( file );
537
538 /* now try read-only file */
539
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() );
543
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() );
549
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 );
554
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 );
559 CloseHandle( file );
560
561 /* now try no access file */
562
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() );
566
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() );
572
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() );
578
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() );
584
585 CloseHandle( file );
586 DeleteFileA( testfile );
587
588 SetLastError(0xdeadbeef);
589 name = "Local\\Foo";
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)
593 {
594 name = "Foo";
595 file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
596 }
597 ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
598
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 );
604 if (!ptr)
605 {
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 );
619 }
620 else win_skip( "no access checks on win9x\n" );
621 UnmapViewOfFile( ptr );
622 CloseHandle( mapping );
623
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 );
629 if (!ptr)
630 {
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 );
644 }
645 else win_skip( "no access checks on win9x\n" );
646 UnmapViewOfFile( ptr );
647 CloseHandle( mapping );
648
649 CloseHandle( file );
650
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());
654
655 ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
656 ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
657
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);
662
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 */
670 {
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);
675 }
676 else
677 {
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);
682 }
683
684 if (ptr != ptr2)
685 {
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);
702 }
703
704 ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
705 ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
706
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 */
715 {
716 ok(info.AllocationProtect == PAGE_NOACCESS,
717 "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
718 }
719 else
720 {
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);
724 }
725
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 */
729 if (ptr != ptr2)
730 {
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);
746 }
747
748 if(winetest_interactive)
749 {
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() );
753
754 ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
755 ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
756 if (!ret)
757 ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
758 }
759 else
760 skip("MEM_RESET is not currently supported\n");
761
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);
767
768 addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
769 ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
770
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());
783
784 ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
785 }
786
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 );
792
793 static void test_NtMapViewOfSection(void)
794 {
795 HANDLE hProcess;
796
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;
801 void *ptr;
802 BOOL ret;
803 DWORD status, written;
804 SIZE_T size, result;
805 LARGE_INTEGER offset;
806
807 pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
808 pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
809 if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
810 {
811 win_skip( "NtMapViewOfSection not available\n" );
812 return;
813 }
814
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 );
820
821 /* read/write mapping */
822
823 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
824 ok( mapping != 0, "CreateFileMapping failed\n" );
825
826 hProcess = create_target_process("sleep");
827 ok(hProcess != NULL, "Can't start process\n");
828
829 ptr = NULL;
830 size = 0;
831 offset.QuadPart = 0;
832 status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
833 ok( !status, "NtMapViewOfSection failed status %x\n", status );
834
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" );
839
840 status = pNtUnmapViewOfSection( hProcess, ptr );
841 ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
842
843 CloseHandle( mapping );
844 CloseHandle( file );
845 DeleteFileA( testfile );
846
847 TerminateProcess(hProcess, 0);
848 CloseHandle(hProcess);
849 }
850
851 static void test_NtAreMappedFilesTheSame(void)
852 {
853 static const char testfile[] = "testfile.xxx";
854 HANDLE file, file2, mapping, map2;
855 void *ptr, *ptr2;
856 NTSTATUS status;
857 char path[MAX_PATH];
858
859 if (!pNtAreMappedFilesTheSame)
860 {
861 win_skip( "NtAreMappedFilesTheSame not available\n" );
862 return;
863 }
864
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 );
870
871 mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
872 ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
873
874 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
875 ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
876
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() );
880
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 );
888
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 );
894 CloseHandle( map2 );
895
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 );
903 CloseHandle( map2 );
904 CloseHandle( file2 );
905
906 status = pNtAreMappedFilesTheSame( ptr, ptr );
907 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
908
909 status = pNtAreMappedFilesTheSame( ptr, (char *)ptr + 30 );
910 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
911
912 status = pNtAreMappedFilesTheSame( ptr, GetModuleHandleA("kernel32.dll") );
913 ok( status == STATUS_NOT_SAME_DEVICE, "NtAreMappedFilesTheSame returned %x\n", status );
914
915 status = pNtAreMappedFilesTheSame( ptr, (void *)0xdeadbeef );
916 ok( status == STATUS_CONFLICTING_ADDRESSES || status == STATUS_INVALID_ADDRESS,
917 "NtAreMappedFilesTheSame returned %x\n", status );
918
919 status = pNtAreMappedFilesTheSame( ptr, NULL );
920 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
921
922 status = pNtAreMappedFilesTheSame( ptr, (void *)GetProcessHeap() );
923 ok( status == STATUS_CONFLICTING_ADDRESSES, "NtAreMappedFilesTheSame returned %x\n", status );
924
925 status = pNtAreMappedFilesTheSame( NULL, NULL );
926 ok( status == STATUS_INVALID_ADDRESS, "NtAreMappedFilesTheSame returned %x\n", status );
927
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 );
933
934 UnmapViewOfFile( ptr );
935 CloseHandle( mapping );
936 CloseHandle( file );
937
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 );
947
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() );
952
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 );
961
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") );
967 todo_wine
968 ok( status == STATUS_SUCCESS, "NtAreMappedFilesTheSame returned %x\n", status );
969
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 );
979 CloseHandle( map2 );
980 CloseHandle( file2 );
981
982 UnmapViewOfFile( ptr );
983 CloseHandle( mapping );
984
985 CloseHandle( file );
986 DeleteFileA( testfile );
987 }
988
989 static void test_CreateFileMapping(void)
990 {
991 HANDLE handle, handle2;
992
993 /* test case sensitivity */
994
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());
1000
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 );
1007
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 );
1014
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 );
1019
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());
1025
1026 CloseHandle( handle );
1027 }
1028
1029 static void test_IsBadReadPtr(void)
1030 {
1031 BOOL ret;
1032 void *ptr = (void *)0xdeadbeef;
1033 char stackvar;
1034
1035 ret = IsBadReadPtr(NULL, 0);
1036 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1037
1038 ret = IsBadReadPtr(NULL, 1);
1039 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1040
1041 ret = IsBadReadPtr(ptr, 0);
1042 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1043
1044 ret = IsBadReadPtr(ptr, 1);
1045 ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
1046
1047 ret = IsBadReadPtr(&stackvar, 0);
1048 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1049
1050 ret = IsBadReadPtr(&stackvar, sizeof(char));
1051 ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
1052 }
1053
1054 static void test_IsBadWritePtr(void)
1055 {
1056 BOOL ret;
1057 void *ptr = (void *)0xdeadbeef;
1058 char stackval;
1059
1060 ret = IsBadWritePtr(NULL, 0);
1061 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1062
1063 ret = IsBadWritePtr(NULL, 1);
1064 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1065
1066 ret = IsBadWritePtr(ptr, 0);
1067 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1068
1069 ret = IsBadWritePtr(ptr, 1);
1070 ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
1071
1072 ret = IsBadWritePtr(&stackval, 0);
1073 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1074
1075 ret = IsBadWritePtr(&stackval, sizeof(char));
1076 ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
1077 }
1078
1079 static void test_IsBadCodePtr(void)
1080 {
1081 BOOL ret;
1082 void *ptr = (void *)0xdeadbeef;
1083 char stackval;
1084
1085 ret = IsBadCodePtr(NULL);
1086 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1087
1088 ret = IsBadCodePtr(ptr);
1089 ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
1090
1091 ret = IsBadCodePtr((void *)&stackval);
1092 ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
1093 }
1094
1095 static void test_write_watch(void)
1096 {
1097 char *base;
1098 DWORD ret, size, old_prot;
1099 MEMORY_BASIC_INFORMATION info;
1100 void *results[64];
1101 ULONG_PTR count;
1102 ULONG pagesize;
1103
1104 if (!pGetWriteWatch || !pResetWriteWatch)
1105 {
1106 win_skip( "GetWriteWatch not supported\n" );
1107 return;
1108 }
1109
1110 size = 0x10000;
1111 base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
1112 if (!base &&
1113 (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
1114 {
1115 win_skip( "MEM_WRITE_WATCH not supported\n" );
1116 return;
1117 }
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 );
1127
1128 count = 64;
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() );
1135
1136 SetLastError( 0xdeadbeef );
1137 ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
1138 if (ret)
1139 {
1140 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1141 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1142 }
1143 else /* win98 */
1144 {
1145 ok( count == 0, "wrong count %lu\n", count );
1146 }
1147
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 );
1151
1152 base[pagesize + 1] = 0x44;
1153
1154 count = 64;
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] );
1159
1160 count = 64;
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] );
1165
1166 count = 64;
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 );
1170
1171 base[2*pagesize + 3] = 0x11;
1172 base[4*pagesize + 8] = 0x11;
1173
1174 count = 64;
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] );
1180
1181 count = 64;
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] );
1186
1187 ret = pResetWriteWatch( base, 3*pagesize );
1188 ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1189
1190 count = 64;
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] );
1195
1196 *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1197
1198 count = 64;
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] );
1205
1206 count = 1;
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] );
1211
1212 count = 64;
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] );
1218
1219 /* changing protections doesn't affect watches */
1220
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 );
1224
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 );
1231
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 );
1235
1236 count = 64;
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] );
1242
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 );
1249
1250 /* some invalid parameter tests */
1251
1252 SetLastError( 0xdeadbeef );
1253 count = 0;
1254 ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1255 if (ret)
1256 {
1257 ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1258 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1259
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() );
1264
1265 SetLastError( 0xdeadbeef );
1266 count = 64;
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() );
1270
1271 SetLastError( 0xdeadbeef );
1272 count = 64;
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() );
1276
1277 SetLastError( 0xdeadbeef );
1278 count = 0;
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() );
1282
1283 SetLastError( 0xdeadbeef );
1284 count = 64;
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() );
1288
1289 SetLastError( 0xdeadbeef );
1290 count = 64;
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() );
1294
1295 SetLastError( 0xdeadbeef );
1296 count = 64;
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() );
1300
1301 SetLastError( 0xdeadbeef );
1302 count = 64;
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() );
1306
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() );
1311
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() );
1316 }
1317 else /* win98 is completely different */
1318 {
1319 SetLastError( 0xdeadbeef );
1320 count = 64;
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() );
1324
1325 count = 0;
1326 ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1327 ok( !ret, "GetWriteWatch failed %u\n", ret );
1328
1329 count = 64;
1330 ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1331 ok( !ret, "GetWriteWatch failed %u\n", ret );
1332
1333 count = 64;
1334 ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1335 ok( !ret, "GetWriteWatch failed %u\n", ret );
1336
1337 ret = pResetWriteWatch( base, 0 );
1338 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1339
1340 ret = pResetWriteWatch( GetModuleHandle(0), size );
1341 ok( !ret, "ResetWriteWatch failed %u\n", ret );
1342 }
1343
1344 VirtualFree( base, 0, MEM_FREE );
1345
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 );
1349
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() );
1353
1354 /* initial protect doesn't matter */
1355
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() );
1360
1361 count = 64;
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 );
1365
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 );
1369
1370 base[5*pagesize + 200] = 3;
1371
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 );
1375
1376 count = 64;
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] );
1381
1382 ret = VirtualFree( base, size, MEM_DECOMMIT );
1383 ok( ret, "VirtualFree failed %u\n", GetLastError() );
1384
1385 count = 64;
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] );
1391
1392 VirtualFree( base, 0, MEM_FREE );
1393 }
1394
1395 START_TEST(virtual)
1396 {
1397 int argc;
1398 char **argv;
1399 argc = winetest_get_mainargs( &argv );
1400
1401 if (argc >= 3)
1402 {
1403 if (!strcmp(argv[2], "sleep"))
1404 {
1405 Sleep(5000); /* spawned process runs for at most 5 seconds */
1406 return;
1407 }
1408 while (1)
1409 {
1410 void *mem;
1411 BOOL ret;
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());
1418 if (!ret) break;
1419 }
1420 return;
1421 }
1422
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" );
1430
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)
1441 test_write_watch();
1442 else
1443 skip("test_write_watch - MEM_WRITE_WATCH is currently not supported\n");
1444 }