~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/virtual.c

Version: ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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 "windef.h"
 25 #include "winbase.h"
 26 #include "winerror.h"
 27 #include "wine/test.h"
 28 
 29 #define NUM_THREADS 4
 30 #define MAPPING_SIZE 0x100000
 31 
 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);
 37 
 38 /* ############################### */
 39 
 40 static HANDLE create_target_process(const char *arg)
 41 {
 42     char **argv;
 43     char cmdline[MAX_PATH];
 44     PROCESS_INFORMATION pi;
 45     STARTUPINFO si = { 0 };
 46     si.cb = sizeof(si);
 47 
 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());
 53     return pi.hProcess;
 54 }
 55 
 56 static void test_VirtualAllocEx(void)
 57 {
 58     const unsigned int alloc_size = 1<<15;
 59     char *src, *dst;
 60     SIZE_T bytes_written = 0, bytes_read = 0, i;
 61     void *addr1, *addr2;
 62     BOOL b;
 63     DWORD old_prot;
 64     MEMORY_BASIC_INFORMATION info;
 65     HANDLE hProcess;
 66 
 67     /* not exported in all windows-versions  */
 68     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
 69         win_skip("Virtual{Alloc,Free}Ex not available\n");
 70         return;
 71     }
 72 
 73     hProcess = create_target_process("sleep");
 74     ok(hProcess != NULL, "Can't start process\n");
 75 
 76     SetLastError(0xdeadbeef);
 77     addr1 = pVirtualAllocEx(hProcess, NULL, alloc_size, MEM_COMMIT,
 78                            PAGE_EXECUTE_READWRITE);
 79     if (!addr1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
 80     {   /* Win9x */
 81         win_skip("VirtualAllocEx not implemented\n");
 82         TerminateProcess(hProcess, 0);
 83         CloseHandle(hProcess);
 84         return;
 85     }
 86 
 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++)
 90         src[i] = i & 0xff;
 91 
 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",
 95        bytes_written);
 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");
 99 
100     /* test invalid source buffers */
101 
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 );
114 
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 );
127 
128     b = pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE);
129     ok(b != 0, "VirtualFreeEx, error %u\n", GetLastError());
130 
131     VirtualFree( src, 0, MEM_FREE );
132     VirtualFree( dst, 0, MEM_FREE );
133 
134     /*
135      * The following tests parallel those in test_VirtualAlloc()
136      */
137 
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());
144 
145     addr1 = pVirtualAllocEx(hProcess, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
146     ok(addr1 != NULL, "VirtualAllocEx failed\n");
147 
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);
161 
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());
168 
169     addr2 = pVirtualAllocEx(hProcess, addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
170     ok(addr1 == addr2, "VirtualAllocEx failed\n");
171 
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);
183 
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());
191 
192     old_prot = 0;
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);
195 
196     old_prot = 0;
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);
199 
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());
204 
205     ok(pVirtualFreeEx(hProcess, addr1, 0x10000, MEM_DECOMMIT), "VirtualFreeEx failed\n");
206 
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());
212 
213     ok(pVirtualFreeEx(hProcess, addr1, 0, MEM_RELEASE), "VirtualFreeEx failed\n");
214 
215     TerminateProcess(hProcess, 0);
216     CloseHandle(hProcess);
217 }
218 
219 static void test_VirtualAlloc(void)
220 {
221     void *addr1, *addr2;
222     DWORD old_prot;
223     MEMORY_BASIC_INFORMATION info;
224 
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());
231 
232     addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
233     ok(addr1 != NULL, "VirtualAlloc failed\n");
234 
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);
248 
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());
255 
256     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
257     ok(addr1 == addr2, "VirtualAlloc failed\n");
258 
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);
270 
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());
278 
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);
282 
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);
286 
287     ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
288         "VirtualQuery failed\n");
289     ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
290     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
291     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
292     memset( addr1, 0x55, 20 );
293     ok( *(DWORD *)addr1 == 0x55555555, "wrong data %x\n", *(DWORD *)addr1 );
294 
295     addr2 = VirtualAlloc( addr1, 0x1000, MEM_RESET, PAGE_NOACCESS );
296     ok( addr2 == addr1 || broken( !addr2 && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
297         "VirtualAlloc failed err %u\n", GetLastError() );
298     ok( *(DWORD *)addr1 == 0x55555555 || *(DWORD *)addr1 == 0, "wrong data %x\n", *(DWORD *)addr1 );
299     if (addr2)
300     {
301         ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
302            "VirtualQuery failed\n");
303         ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
304         ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
305         ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
306 
307         addr2 = VirtualAlloc( (char *)addr1 + 0x1000, 0x1000, MEM_RESET, PAGE_NOACCESS );
308         ok( (char *)addr2 == (char *)addr1 + 0x1000, "VirtualAlloc failed\n" );
309 
310         ok(VirtualQuery(addr2, &info, sizeof(info)) == sizeof(info),
311            "VirtualQuery failed\n");
312         ok(info.RegionSize == 0xf000, "%lx != 0xf000\n", info.RegionSize);
313         ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
314         ok(info.Protect == 0, "%x != 0\n", info.Protect);
315 
316         addr2 = VirtualAlloc( (char *)addr1 + 0xf000, 0x2000, MEM_RESET, PAGE_NOACCESS );
317         ok( !addr2, "VirtualAlloc failed\n" );
318         ok( GetLastError() == ERROR_INVALID_ADDRESS, "wrong error %u\n", GetLastError() );
319     }
320 
321     /* invalid protection values */
322     SetLastError(0xdeadbeef);
323     addr2 = VirtualAlloc(NULL, 0x1000, MEM_RESERVE, 0);
324     ok(!addr2, "VirtualAlloc succeeded\n");
325     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
326 
327     SetLastError(0xdeadbeef);
328     addr2 = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, 0);
329     ok(!addr2, "VirtualAlloc succeeded\n");
330     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
331 
332     SetLastError(0xdeadbeef);
333     addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_READONLY | PAGE_EXECUTE);
334     ok(!addr2, "VirtualAlloc succeeded\n");
335     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
336 
337     SetLastError(0xdeadbeef);
338     ok(!VirtualProtect(addr1, 0x1000, PAGE_READWRITE | PAGE_EXECUTE_WRITECOPY, &old_prot),
339        "VirtualProtect succeeded\n");
340     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
341 
342     SetLastError(0xdeadbeef);
343     ok(!VirtualProtect(addr1, 0x1000, 0, &old_prot), "VirtualProtect succeeded\n");
344     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
345 
346     SetLastError(0xdeadbeef);
347     ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
348     ok(GetLastError() == ERROR_INVALID_PARAMETER,
349         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
350 
351     ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
352 
353     /* if the type is MEM_RELEASE, size must be 0 */
354     ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
355     ok(GetLastError() == ERROR_INVALID_PARAMETER,
356         "got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
357 
358     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
359 }
360 
361 static void test_MapViewOfFile(void)
362 {
363     static const char testfile[] = "testfile.xxx";
364     const char *name;
365     HANDLE file, mapping, map2;
366     void *ptr, *ptr2, *addr;
367     MEMORY_BASIC_INFORMATION info;
368     BOOL ret;
369 
370     SetLastError(0xdeadbeef);
371     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
372     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
373     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
374     SetEndOfFile( file );
375 
376     /* read/write mapping */
377 
378     SetLastError(0xdeadbeef);
379     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
380     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
381 
382     SetLastError(0xdeadbeef);
383     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
384     ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ error %u\n", GetLastError() );
385     UnmapViewOfFile( ptr );
386 
387     /* this fails on win9x but succeeds on NT */
388     SetLastError(0xdeadbeef);
389     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
390     if (ptr) UnmapViewOfFile( ptr );
391     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
392 
393     SetLastError(0xdeadbeef);
394     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
395     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
396     UnmapViewOfFile( ptr );
397 
398     SetLastError(0xdeadbeef);
399     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
400     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
401     UnmapViewOfFile( ptr );
402 
403     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
404                            FILE_MAP_READ|FILE_MAP_WRITE, FALSE, 0 );
405     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
406     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
407     ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
408     UnmapViewOfFile( ptr );
409     CloseHandle( map2 );
410 
411     ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
412                            FILE_MAP_READ, FALSE, 0 );
413     ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
414     ptr = MapViewOfFile( map2, FILE_MAP_WRITE, 0, 0, 4096 );
415     if (!ptr)
416     {
417         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
418         CloseHandle( map2 );
419         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2, 0, FALSE, 0 );
420         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
421         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
422         ok( !ptr, "MapViewOfFile succeeded\n" );
423         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
424         CloseHandle( map2 );
425         ret = DuplicateHandle( GetCurrentProcess(), mapping, GetCurrentProcess(), &map2,
426                                FILE_MAP_READ, FALSE, 0 );
427         ok( ret, "DuplicateHandle failed error %u\n", GetLastError());
428         ptr = MapViewOfFile( map2, 0, 0, 0, 4096 );
429         ok( ptr != NULL, "MapViewOfFile NO_ACCESS error %u\n", GetLastError() );
430     }
431     else win_skip( "no access checks on win9x\n" );
432 
433     UnmapViewOfFile( ptr );
434     CloseHandle( map2 );
435     CloseHandle( mapping );
436 
437     /* read-only mapping */
438 
439     SetLastError(0xdeadbeef);
440     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
441     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
442 
443     SetLastError(0xdeadbeef);
444     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
445     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
446     UnmapViewOfFile( ptr );
447 
448     /* this fails on win9x but succeeds on NT */
449     SetLastError(0xdeadbeef);
450     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
451     if (ptr) UnmapViewOfFile( ptr );
452     else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
453 
454     SetLastError(0xdeadbeef);
455     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
456     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
457     UnmapViewOfFile( ptr );
458 
459     SetLastError(0xdeadbeef);
460     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
461     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
462     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
463         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
464     CloseHandle( mapping );
465 
466     /* copy-on-write mapping */
467 
468     SetLastError(0xdeadbeef);
469     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
470     ok( mapping != 0, "CreateFileMapping error %u\n", GetLastError() );
471 
472     SetLastError(0xdeadbeef);
473     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
474     ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
475     UnmapViewOfFile( ptr );
476 
477     SetLastError(0xdeadbeef);
478     ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
479     ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY error %u\n", GetLastError() );
480     UnmapViewOfFile( ptr );
481 
482     SetLastError(0xdeadbeef);
483     ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
484     ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
485     UnmapViewOfFile( ptr );
486 
487     SetLastError(0xdeadbeef);
488     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
489     ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
490     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
491         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
492     CloseHandle( mapping );
493 
494     /* no access mapping */
495 
496     SetLastError(0xdeadbeef);
497     mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
498     /* fails on NT but succeeds on win9x */
499     if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
500     else
501     {
502         SetLastError(0xdeadbeef);
503         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
504         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
505         UnmapViewOfFile( ptr );
506 
507         SetLastError(0xdeadbeef);
508         ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
509         ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
510         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
511 
512         SetLastError(0xdeadbeef);
513         ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
514         ok( ptr != NULL, "MapViewOfFile 0 error %u\n", GetLastError() );
515         UnmapViewOfFile( ptr );
516 
517         SetLastError(0xdeadbeef);
518         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
519         ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
520         ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %d\n", GetLastError() );
521 
522         CloseHandle( mapping );
523     }
524 
525     CloseHandle( file );
526 
527     /* now try read-only file */
528 
529     SetLastError(0xdeadbeef);
530     file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
531     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
532 
533     SetLastError(0xdeadbeef);
534     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
535     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
536     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
537         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
538 
539     SetLastError(0xdeadbeef);
540     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
541     ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY error %u\n", GetLastError() );
542     CloseHandle( mapping );
543 
544     SetLastError(0xdeadbeef);
545     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
546     ok( mapping != 0, "CreateFileMapping PAGE_READONLY error %u\n", GetLastError() );
547     CloseHandle( mapping );
548     CloseHandle( file );
549 
550     /* now try no access file */
551 
552     SetLastError(0xdeadbeef);
553     file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
554     ok( file != INVALID_HANDLE_VALUE, "CreateFile error %u\n", GetLastError() );
555 
556     SetLastError(0xdeadbeef);
557     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
558     ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
559     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
560         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
561 
562     SetLastError(0xdeadbeef);
563     mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
564     ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
565     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
566         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
567 
568     SetLastError(0xdeadbeef);
569     mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
570     ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
571     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
572         GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
573 
574     CloseHandle( file );
575     DeleteFileA( testfile );
576 
577     SetLastError(0xdeadbeef);
578     name = "Local\\Foo";
579     file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
580     /* nt4 doesn't have Local\\ */
581     if (!file && GetLastError() == ERROR_PATH_NOT_FOUND)
582     {
583         name = "Foo";
584         file = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, name );
585     }
586     ok( file != 0, "CreateFileMapping PAGE_READWRITE error %u\n", GetLastError() );
587 
588     SetLastError(0xdeadbeef);
589     mapping = OpenFileMapping( FILE_MAP_READ, FALSE, name );
590     ok( mapping != 0, "OpenFileMapping FILE_MAP_READ error %u\n", GetLastError() );
591     SetLastError(0xdeadbeef);
592     ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
593     if (!ptr)
594     {
595         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
596         SetLastError(0xdeadbeef);
597         ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
598         ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ error %u\n", GetLastError() );
599         SetLastError(0xdeadbeef);
600         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
601             "VirtualQuery error %u\n", GetLastError() );
602         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
603         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
604         ok( info.AllocationProtect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.AllocationProtect );
605         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
606         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
607         ok( info.Protect == PAGE_READONLY, "%x != PAGE_READONLY\n", info.Protect );
608     }
609     else win_skip( "no access checks on win9x\n" );
610     UnmapViewOfFile( ptr );
611     CloseHandle( mapping );
612 
613     SetLastError(0xdeadbeef);
614     mapping = OpenFileMapping( FILE_MAP_WRITE, FALSE, name );
615     ok( mapping != 0, "OpenFileMapping FILE_MAP_WRITE error %u\n", GetLastError() );
616     SetLastError(0xdeadbeef);
617     ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
618     if (!ptr)
619     {
620         ok( GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %d\n", GetLastError() );
621         SetLastError(0xdeadbeef);
622         ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
623         ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE error %u\n", GetLastError() );
624         SetLastError(0xdeadbeef);
625         ok( VirtualQuery( ptr, &info, sizeof(info) ) == sizeof(info),
626             "VirtualQuery error %u\n", GetLastError() );
627         ok( info.BaseAddress == ptr, "%p != %p\n", info.BaseAddress, ptr );
628         ok( info.AllocationBase == ptr, "%p != %p\n", info.AllocationBase, ptr );
629         ok( info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect );
630         ok( info.RegionSize == 4096, "%lx != 4096\n", info.RegionSize );
631         ok( info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State );
632         ok( info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect );
633     }
634     else win_skip( "no access checks on win9x\n" );
635     UnmapViewOfFile( ptr );
636     CloseHandle( mapping );
637 
638     CloseHandle( file );
639 
640     /* read/write mapping with SEC_RESERVE */
641     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_RESERVE, 0, MAPPING_SIZE, NULL);
642     ok(mapping != INVALID_HANDLE_VALUE, "CreateFileMappingA failed with error %d\n", GetLastError());
643 
644     ptr = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
645     ok(ptr != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
646 
647     ptr2 = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
648     /* on NT ptr != ptr2 but on Win9x ptr == ptr2 */
649     ok(ptr2 != NULL, "MapViewOfFile failed with error %d\n", GetLastError());
650     trace("mapping same section resulted in views %p and %p\n", ptr, ptr2);
651 
652     ret = VirtualQuery(ptr, &info, sizeof(info));
653     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
654     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
655     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
656     ok(info.RegionSize == MAPPING_SIZE, "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
657     ok(info.State == MEM_RESERVE, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
658     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
659     {
660         ok(info.AllocationProtect == PAGE_NOACCESS,
661            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
662         ok(info.Protect == PAGE_NOACCESS,
663            "Protect should have been PAGE_NOACCESS instead of 0x%x\n", info.Protect);
664     }
665     else
666     {
667         ok(info.AllocationProtect == PAGE_READWRITE,
668            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
669         ok(info.Protect == 0, "Protect should have been 0 instead of 0x%x\n", info.Protect);
670         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
671     }
672 
673     if (ptr != ptr2)
674     {
675         ret = VirtualQuery(ptr2, &info, sizeof(info));
676         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
677         ok(info.BaseAddress == ptr2,
678            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
679         ok(info.AllocationBase == ptr2,
680            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
681         ok(info.AllocationProtect == PAGE_READWRITE,
682            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
683         ok(info.RegionSize == MAPPING_SIZE,
684            "RegionSize should have been 0x%x but was 0x%x\n", MAPPING_SIZE, (unsigned int)info.RegionSize);
685         ok(info.State == MEM_RESERVE,
686            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
687         ok(info.Protect == 0,
688            "Protect should have been 0 instead of 0x%x\n", info.Protect);
689         ok(info.Type == MEM_MAPPED,
690            "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
691     }
692 
693     ptr = VirtualAlloc(ptr, 0x10000, MEM_COMMIT, PAGE_READONLY);
694     ok(ptr != NULL, "VirtualAlloc failed with error %d\n", GetLastError());
695 
696     ret = VirtualQuery(ptr, &info, sizeof(info));
697     ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
698     ok(info.BaseAddress == ptr, "BaseAddress should have been %p but was %p instead\n", ptr, info.BaseAddress);
699     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
700     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
701     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
702     ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
703     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
704     {
705         ok(info.AllocationProtect == PAGE_NOACCESS,
706            "AllocationProtect should have been PAGE_NOACCESS but was 0x%x\n", info.AllocationProtect);
707     }
708     else
709     {
710         ok(info.AllocationProtect == PAGE_READWRITE,
711            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
712         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
713     }
714 
715     /* shows that the VirtualAlloc above affects the mapping, not just the
716      * virtual memory in this process - it also affects all other processes
717      * with a view of the mapping, but that isn't tested here */
718     if (ptr != ptr2)
719     {
720         ret = VirtualQuery(ptr2, &info, sizeof(info));
721         ok(ret, "VirtualQuery failed with error %d\n", GetLastError());
722         ok(info.BaseAddress == ptr2,
723            "BaseAddress should have been %p but was %p instead\n", ptr2, info.BaseAddress);
724         ok(info.AllocationBase == ptr2,
725            "AllocationBase should have been %p but was %p instead\n", ptr2, info.AllocationBase);
726         ok(info.AllocationProtect == PAGE_READWRITE,
727            "AllocationProtect should have been PAGE_READWRITE but was 0x%x\n", info.AllocationProtect);
728         ok(info.RegionSize == 0x10000,
729            "RegionSize should have been 0x10000 but was 0x%x\n", (unsigned int)info.RegionSize);
730         ok(info.State == MEM_COMMIT,
731            "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
732         ok(info.Protect == PAGE_READWRITE,
733            "Protect should have been 0 instead of 0x%x\n", info.Protect);
734         ok(info.Type == MEM_MAPPED, "Type should have been MEM_MAPPED instead of 0x%x\n", info.Type);
735     }
736 
737     addr = VirtualAlloc( ptr, MAPPING_SIZE, MEM_RESET, PAGE_READONLY );
738     ok( addr == ptr || broken(!addr && GetLastError() == ERROR_INVALID_PARAMETER), /* win9x */
739         "VirtualAlloc failed with error %u\n", GetLastError() );
740 
741     ret = VirtualFree( ptr, 0x10000, MEM_DECOMMIT );
742     ok( !ret || broken(ret) /* win9x */, "VirtualFree succeeded\n" );
743     if (!ret)
744         ok( GetLastError() == ERROR_INVALID_PARAMETER, "VirtualFree failed with %u\n", GetLastError() );
745 
746     ret = UnmapViewOfFile(ptr2);
747     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
748     ret = UnmapViewOfFile(ptr);
749     ok(ret, "UnmapViewOfFile failed with error %d\n", GetLastError());
750     CloseHandle(mapping);
751 
752     addr = VirtualAlloc(NULL, 0x10000, MEM_COMMIT, PAGE_READONLY );
753     ok( addr != NULL, "VirtualAlloc failed with error %u\n", GetLastError() );
754 
755     SetLastError(0xdeadbeef);
756     ok( !UnmapViewOfFile(addr), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
757     ok( GetLastError() == ERROR_INVALID_ADDRESS,
758         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
759     SetLastError(0xdeadbeef);
760     ok( !UnmapViewOfFile((char *)addr + 0x3000), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
761     ok( GetLastError() == ERROR_INVALID_ADDRESS,
762         "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
763     SetLastError(0xdeadbeef);
764     ok( !UnmapViewOfFile((void *)0xdeadbeef), "UnmapViewOfFile should fail on VirtualAlloc mem\n" );
765     ok( GetLastError() == ERROR_INVALID_ADDRESS,
766        "got %u, expected ERROR_INVALID_ADDRESS\n", GetLastError());
767 
768     ok( VirtualFree(addr, 0, MEM_RELEASE), "VirtualFree failed\n" );
769 }
770 
771 static DWORD (WINAPI *pNtMapViewOfSection)( HANDLE handle, HANDLE process, PVOID *addr_ptr,
772                                             ULONG zero_bits, SIZE_T commit_size,
773                                             const LARGE_INTEGER *offset_ptr, SIZE_T *size_ptr,
774                                             ULONG inherit, ULONG alloc_type, ULONG protect );
775 static DWORD (WINAPI *pNtUnmapViewOfSection)( HANDLE process, PVOID addr );
776 
777 static void test_NtMapViewOfSection(void)
778 {
779     HANDLE hProcess;
780 
781     static const char testfile[] = "testfile.xxx";
782     static const char data[] = "test data for NtMapViewOfSection";
783     char buffer[sizeof(data)];
784     HANDLE file, mapping;
785     void *ptr;
786     BOOL ret;
787     DWORD status, written;
788     SIZE_T size, result;
789     LARGE_INTEGER offset;
790 
791     pNtMapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtMapViewOfSection" );
792     pNtUnmapViewOfSection = (void *)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection" );
793     if (!pNtMapViewOfSection || !pNtUnmapViewOfSection)
794     {
795         win_skip( "NtMapViewOfSection not available\n" );
796         return;
797     }
798 
799     file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
800     ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
801     WriteFile( file, data, sizeof(data), &written, NULL );
802     SetFilePointer( file, 4096, NULL, FILE_BEGIN );
803     SetEndOfFile( file );
804 
805     /* read/write mapping */
806 
807     mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
808     ok( mapping != 0, "CreateFileMapping failed\n" );
809 
810     hProcess = create_target_process("sleep");
811     ok(hProcess != NULL, "Can't start process\n");
812 
813     ptr = NULL;
814     size = 0;
815     offset.QuadPart = 0;
816     status = pNtMapViewOfSection( mapping, hProcess, &ptr, 0, 0, &offset, &size, 1, 0, PAGE_READWRITE );
817     ok( !status, "NtMapViewOfSection failed status %x\n", status );
818 
819     ret = ReadProcessMemory( hProcess, ptr, buffer, sizeof(buffer), &result );
820     ok( ret, "ReadProcessMemory failed\n" );
821     ok( result == sizeof(buffer), "ReadProcessMemory didn't read all data (%lx)\n", result );
822     ok( !memcmp( buffer, data, sizeof(buffer) ), "Wrong data read\n" );
823 
824     status = pNtUnmapViewOfSection( hProcess, ptr );
825     ok( !status, "NtUnmapViewOfSection failed status %x\n", status );
826 
827     CloseHandle( mapping );
828     CloseHandle( file );
829     DeleteFileA( testfile );
830 
831     TerminateProcess(hProcess, 0);
832     CloseHandle(hProcess);
833 }
834 
835 static void test_CreateFileMapping(void)
836 {
837     HANDLE handle, handle2;
838 
839     /* test case sensitivity */
840 
841     SetLastError(0xdeadbeef);
842     handle = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
843                                  "Wine Test Mapping");
844     ok( handle != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
845     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
846 
847     SetLastError(0xdeadbeef);
848     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
849                                   "Wine Test Mapping");
850     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
851     ok( GetLastError() == ERROR_ALREADY_EXISTS, "wrong error %u\n", GetLastError());
852     CloseHandle( handle2 );
853 
854     SetLastError(0xdeadbeef);
855     handle2 = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE, 0, 0x1000,
856                                  "WINE TEST MAPPING");
857     ok( handle2 != NULL, "CreateFileMapping failed with error %d\n", GetLastError());
858     ok( GetLastError() == 0, "wrong error %u\n", GetLastError());
859     CloseHandle( handle2 );
860 
861     SetLastError(0xdeadbeef);
862     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "Wine Test Mapping");
863     ok( handle2 != NULL, "OpenFileMapping failed with error %d\n", GetLastError());
864     CloseHandle( handle2 );
865 
866     SetLastError(0xdeadbeef);
867     handle2 = OpenFileMappingA( FILE_MAP_ALL_ACCESS, FALSE, "WINE TEST MAPPING");
868     ok( !handle2, "OpenFileMapping succeeded\n");
869     ok( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_NAME /* win9x */,
870         "wrong error %u\n", GetLastError());
871 
872     CloseHandle( handle );
873 }
874 
875 static void test_IsBadReadPtr(void)
876 {
877     BOOL ret;
878     void *ptr = (void *)0xdeadbeef;
879     char stackvar;
880 
881     ret = IsBadReadPtr(NULL, 0);
882     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
883 
884     ret = IsBadReadPtr(NULL, 1);
885     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
886 
887     ret = IsBadReadPtr(ptr, 0);
888     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
889 
890     ret = IsBadReadPtr(ptr, 1);
891     ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
892 
893     ret = IsBadReadPtr(&stackvar, 0);
894     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
895 
896     ret = IsBadReadPtr(&stackvar, sizeof(char));
897     ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
898 }
899 
900 static void test_IsBadWritePtr(void)
901 {
902     BOOL ret;
903     void *ptr = (void *)0xdeadbeef;
904     char stackval;
905 
906     ret = IsBadWritePtr(NULL, 0);
907     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
908 
909     ret = IsBadWritePtr(NULL, 1);
910     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
911 
912     ret = IsBadWritePtr(ptr, 0);
913     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
914 
915     ret = IsBadWritePtr(ptr, 1);
916     ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
917 
918     ret = IsBadWritePtr(&stackval, 0);
919     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
920 
921     ret = IsBadWritePtr(&stackval, sizeof(char));
922     ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
923 }
924 
925 static void test_IsBadCodePtr(void)
926 {
927     BOOL ret;
928     void *ptr = (void *)0xdeadbeef;
929     char stackval;
930 
931     ret = IsBadCodePtr(NULL);
932     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
933 
934     ret = IsBadCodePtr(ptr);
935     ok(ret == TRUE, "Expected IsBadCodePtr to return TRUE, got %d\n", ret);
936 
937     ret = IsBadCodePtr((void *)&stackval);
938     ok(ret == FALSE, "Expected IsBadCodePtr to return FALSE, got %d\n", ret);
939 }
940 
941 static void test_write_watch(void)
942 {
943     char *base;
944     DWORD ret, size, old_prot;
945     MEMORY_BASIC_INFORMATION info;
946     void *results[64];
947     ULONG_PTR count;
948     ULONG pagesize;
949 
950     if (!pGetWriteWatch || !pResetWriteWatch)
951     {
952         win_skip( "GetWriteWatch not supported\n" );
953         return;
954     }
955 
956     size = 0x10000;
957     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE );
958     if (!base &&
959         (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_NOT_SUPPORTED))
960     {
961         win_skip( "MEM_WRITE_WATCH not supported\n" );
962         return;
963     }
964     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
965     ret = VirtualQuery( base, &info, sizeof(info) );
966     ok(ret, "VirtualQuery failed %u\n", GetLastError());
967     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
968     ok( info.AllocationProtect == PAGE_READWRITE, "wrong AllocationProtect %x\n", info.AllocationProtect );
969     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
970     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
971     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
972     ok( info.Type == MEM_PRIVATE, "wrong Type 0x%x\n", info.Type );
973 
974     count = 64;
975     SetLastError( 0xdeadbeef );
976     ret = pGetWriteWatch( 0, NULL, size, results, &count, &pagesize );
977     ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
978     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
979         broken( GetLastError() == 0xdeadbeef ), /* win98 */
980         "wrong error %u\n", GetLastError() );
981 
982     SetLastError( 0xdeadbeef );
983     ret = pGetWriteWatch( 0, GetModuleHandle(0), size, results, &count, &pagesize );
984     if (ret)
985     {
986         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
987         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
988     }
989     else  /* win98 */
990     {
991         ok( count == 0, "wrong count %lu\n", count );
992     }
993 
994     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
995     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
996     ok( count == 0, "wrong count %lu\n", count );
997 
998     base[pagesize + 1] = 0x44;
999 
1000     count = 64;
1001     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1002     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1003     ok( count == 1, "wrong count %lu\n", count );
1004     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1005 
1006     count = 64;
1007     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1008     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1009     ok( count == 1, "wrong count %lu\n", count );
1010     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1011 
1012     count = 64;
1013     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1014     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1015     ok( count == 0, "wrong count %lu\n", count );
1016 
1017     base[2*pagesize + 3] = 0x11;
1018     base[4*pagesize + 8] = 0x11;
1019 
1020     count = 64;
1021     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1022     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1023     ok( count == 2, "wrong count %lu\n", count );
1024     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1025     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1026 
1027     count = 64;
1028     ret = pGetWriteWatch( 0, base + 3*pagesize, 2*pagesize, results, &count, &pagesize );
1029     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1030     ok( count == 1, "wrong count %lu\n", count );
1031     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1032 
1033     ret = pResetWriteWatch( base, 3*pagesize );
1034     ok( !ret, "pResetWriteWatch failed %u\n", GetLastError() );
1035 
1036     count = 64;
1037     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1038     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1039     ok( count == 1, "wrong count %lu\n", count );
1040     ok( results[0] == base + 4*pagesize, "wrong result %p\n", results[0] );
1041 
1042     *(DWORD *)(base + 2*pagesize - 2) = 0xdeadbeef;
1043 
1044     count = 64;
1045     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1046     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1047     ok( count == 3, "wrong count %lu\n", count );
1048     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1049     ok( results[1] == base + 2*pagesize, "wrong result %p\n", results[1] );
1050     ok( results[2] == base + 4*pagesize, "wrong result %p\n", results[2] );
1051 
1052     count = 1;
1053     ret = pGetWriteWatch( WRITE_WATCH_FLAG_RESET, base, size, results, &count, &pagesize );
1054     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1055     ok( count == 1, "wrong count %lu\n", count );
1056     ok( results[0] == base + pagesize, "wrong result %p\n", results[0] );
1057 
1058     count = 64;
1059     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1060     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1061     ok( count == 2, "wrong count %lu\n", count );
1062     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1063     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1064 
1065     /* changing protections doesn't affect watches */
1066 
1067     ret = VirtualProtect( base, 3*pagesize, PAGE_READONLY, &old_prot );
1068     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1069     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1070 
1071     ret = VirtualQuery( base, &info, sizeof(info) );
1072     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1073     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1074     ok( info.RegionSize == 3*pagesize, "wrong RegionSize 0x%lx\n", info.RegionSize );
1075     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1076     ok( info.Protect == PAGE_READONLY, "wrong Protect 0x%x\n", info.Protect );
1077 
1078     ret = VirtualProtect( base, 3*pagesize, PAGE_READWRITE, &old_prot );
1079     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1080     ok( old_prot == PAGE_READONLY, "wrong old prot %x\n", old_prot );
1081 
1082     count = 64;
1083     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1084     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1085     ok( count == 2, "wrong count %lu\n", count );
1086     ok( results[0] == base + 2*pagesize, "wrong result %p\n", results[0] );
1087     ok( results[1] == base + 4*pagesize, "wrong result %p\n", results[1] );
1088 
1089     ret = VirtualQuery( base, &info, sizeof(info) );
1090     ok(ret, "VirtualQuery failed %u\n", GetLastError());
1091     ok( info.BaseAddress == base, "BaseAddress %p instead of %p\n", info.BaseAddress, base );
1092     ok( info.RegionSize == size, "wrong RegionSize 0x%lx\n", info.RegionSize );
1093     ok( info.State == MEM_COMMIT, "wrong State 0x%x\n", info.State );
1094     ok( info.Protect == PAGE_READWRITE, "wrong Protect 0x%x\n", info.Protect );
1095 
1096     /* some invalid parameter tests */
1097 
1098     SetLastError( 0xdeadbeef );
1099     count = 0;
1100     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1101     if (ret)
1102     {
1103         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1104         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1105 
1106         SetLastError( 0xdeadbeef );
1107         ret = pGetWriteWatch( 0, base, size, results, NULL, &pagesize );
1108         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1109         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1110 
1111         SetLastError( 0xdeadbeef );
1112         count = 64;
1113         ret = pGetWriteWatch( 0, base, size, results, &count, NULL );
1114         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1115         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1116 
1117         SetLastError( 0xdeadbeef );
1118         count = 64;
1119         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1120         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1121         ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
1122 
1123         SetLastError( 0xdeadbeef );
1124         count = 0;
1125         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1126         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1127         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1128 
1129         SetLastError( 0xdeadbeef );
1130         count = 64;
1131         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1132         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1133         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1134 
1135         SetLastError( 0xdeadbeef );
1136         count = 64;
1137         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1138         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1139         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1140 
1141         SetLastError( 0xdeadbeef );
1142         count = 64;
1143         ret = pGetWriteWatch( 0, base, size * 2, results, &count, &pagesize );
1144         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1145         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1146 
1147         SetLastError( 0xdeadbeef );
1148         count = 64;
1149         ret = pGetWriteWatch( 0, base + size - pagesize, pagesize + 1, results, &count, &pagesize );
1150         ok( ret == ~0u, "GetWriteWatch succeeded %u\n", ret );
1151         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1152 
1153         SetLastError( 0xdeadbeef );
1154         ret = pResetWriteWatch( base, 0 );
1155         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1156         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1157 
1158         SetLastError( 0xdeadbeef );
1159         ret = pResetWriteWatch( GetModuleHandle(0), size );
1160         ok( ret == ~0u, "ResetWriteWatch succeeded %u\n", ret );
1161         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1162     }
1163     else  /* win98 is completely different */
1164     {
1165         SetLastError( 0xdeadbeef );
1166         count = 64;
1167         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1168         ok( ret == ERROR_INVALID_PARAMETER, "GetWriteWatch succeeded %u\n", ret );
1169         ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
1170 
1171         count = 0;
1172         ret = pGetWriteWatch( 0, base, size, NULL, &count, &pagesize );
1173         ok( !ret, "GetWriteWatch failed %u\n", ret );
1174 
1175         count = 64;
1176         ret = pGetWriteWatch( 0xdeadbeef, base, size, results, &count, &pagesize );
1177         ok( !ret, "GetWriteWatch failed %u\n", ret );
1178 
1179         count = 64;
1180         ret = pGetWriteWatch( 0, base, 0, results, &count, &pagesize );
1181         ok( !ret, "GetWriteWatch failed %u\n", ret );
1182 
1183         ret = pResetWriteWatch( base, 0 );
1184         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1185 
1186         ret = pResetWriteWatch( GetModuleHandle(0), size );
1187         ok( !ret, "ResetWriteWatch failed %u\n", ret );
1188     }
1189 
1190     VirtualFree( base, 0, MEM_FREE );
1191 
1192     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE );
1193     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1194     VirtualFree( base, 0, MEM_FREE );
1195 
1196     base = VirtualAlloc( 0, size, MEM_WRITE_WATCH, PAGE_READWRITE );
1197     ok( !base, "VirtualAlloc succeeded\n" );
1198     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1199 
1200     /* initial protect doesn't matter */
1201 
1202     base = VirtualAlloc( 0, size, MEM_RESERVE | MEM_WRITE_WATCH, PAGE_NOACCESS );
1203     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1204     base = VirtualAlloc( base, size, MEM_COMMIT, PAGE_NOACCESS );
1205     ok( base != NULL, "VirtualAlloc failed %u\n", GetLastError() );
1206 
1207     count = 64;
1208     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1209     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1210     ok( count == 0, "wrong count %lu\n", count );
1211 
1212     ret = VirtualProtect( base, 6*pagesize, PAGE_READWRITE, &old_prot );
1213     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1214     ok( old_prot == PAGE_NOACCESS, "wrong old prot %x\n", old_prot );
1215 
1216     base[5*pagesize + 200] = 3;
1217 
1218     ret = VirtualProtect( base, 6*pagesize, PAGE_NOACCESS, &old_prot );
1219     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
1220     ok( old_prot == PAGE_READWRITE, "wrong old prot %x\n", old_prot );
1221 
1222     count = 64;
1223     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1224     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1225     ok( count == 1, "wrong count %lu\n", count );
1226     ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1227 
1228     ret = VirtualFree( base, size, MEM_DECOMMIT );
1229     ok( ret, "VirtualFree failed %u\n", GetLastError() );
1230 
1231     count = 64;
1232     ret = pGetWriteWatch( 0, base, size, results, &count, &pagesize );
1233     ok( !ret, "GetWriteWatch failed %u\n", GetLastError() );
1234     ok( count == 1 || broken(count == 0), /* win98 */
1235         "wrong count %lu\n", count );
1236     if (count) ok( results[0] == base + 5*pagesize, "wrong result %p\n", results[0] );
1237 
1238     VirtualFree( base, 0, MEM_FREE );
1239 }
1240 
1241 START_TEST(virtual)
1242 {
1243     int argc;
1244     char **argv;
1245     argc = winetest_get_mainargs( &argv );
1246 
1247     if (argc >= 3)
1248     {
1249         if (!strcmp(argv[2], "sleep"))
1250         {
1251             Sleep(5000); /* spawned process runs for at most 5 seconds */
1252             return;
1253         }
1254         while (1)
1255         {
1256             void *mem;
1257             BOOL ret;
1258             mem = VirtualAlloc(NULL, 1<<20, MEM_COMMIT|MEM_RESERVE,
1259                                PAGE_EXECUTE_READWRITE);
1260             ok(mem != NULL, "VirtualAlloc failed %u\n", GetLastError());
1261             if (mem == NULL) break;
1262             ret = VirtualFree(mem, 0, MEM_RELEASE);
1263             ok(ret, "VirtualFree failed %u\n", GetLastError());
1264             if (!ret) break;
1265         }
1266         return;
1267     }
1268 
1269     hkernel32 = GetModuleHandleA("kernel32.dll");
1270     pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
1271     pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
1272     pGetWriteWatch = (void *) GetProcAddress(hkernel32, "GetWriteWatch");
1273     pResetWriteWatch = (void *) GetProcAddress(hkernel32, "ResetWriteWatch");
1274 
1275     test_VirtualAllocEx();
1276     test_VirtualAlloc();
1277     test_MapViewOfFile();
1278     test_NtMapViewOfSection();
1279     test_CreateFileMapping();
1280     test_IsBadReadPtr();
1281     test_IsBadWritePtr();
1282     test_IsBadCodePtr();
1283     test_write_watch();
1284 }
1285 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.