2004-02-09 21:58:16 +01:00
|
|
|
/*
|
|
|
|
* Unit test suite for Virtual* family of APIs.
|
|
|
|
*
|
|
|
|
* Copyright 2004 Dmitry Timoshkov
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-02-09 21:58:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "wine/test.h"
|
|
|
|
|
|
|
|
static void test_VirtualAlloc(void)
|
|
|
|
{
|
|
|
|
void *addr1, *addr2;
|
|
|
|
DWORD old_prot;
|
|
|
|
MEMORY_BASIC_INFORMATION info;
|
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
ok(addr1 == NULL, "VirtualAlloc should fail on zero-sized allocation\n");
|
|
|
|
ok(GetLastError() == ERROR_INVALID_PARAMETER /* NT */ ||
|
|
|
|
GetLastError() == ERROR_NOT_ENOUGH_MEMORY, /* Win9x */
|
2006-10-10 01:06:48 +02:00
|
|
|
"got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
addr1 = VirtualAlloc(0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
ok(addr1 != NULL, "VirtualAlloc failed\n");
|
|
|
|
|
|
|
|
/* test a not committed memory */
|
|
|
|
ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
|
|
|
|
"VirtualQuery failed\n");
|
|
|
|
ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
|
|
|
|
ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
|
2006-10-10 01:06:48 +02:00
|
|
|
ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
|
2004-02-09 21:58:16 +01:00
|
|
|
ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
|
2006-10-10 01:06:48 +02:00
|
|
|
ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
|
2004-02-09 21:58:16 +01:00
|
|
|
/* NT reports Protect == 0 for a not committed memory block */
|
|
|
|
ok(info.Protect == 0 /* NT */ ||
|
|
|
|
info.Protect == PAGE_NOACCESS, /* Win9x */
|
2006-10-10 01:06:48 +02:00
|
|
|
"%x != PAGE_NOACCESS\n", info.Protect);
|
|
|
|
ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
|
|
|
|
"VirtualProtect should fail on a not committed memory\n");
|
|
|
|
ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
|
|
|
|
GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
|
2006-10-10 01:06:48 +02:00
|
|
|
"got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
addr2 = VirtualAlloc(addr1, 0x1000, MEM_COMMIT, PAGE_NOACCESS);
|
|
|
|
ok(addr1 == addr2, "VirtualAlloc failed\n");
|
|
|
|
|
|
|
|
/* test a committed memory */
|
|
|
|
ok(VirtualQuery(addr1, &info, sizeof(info)) == sizeof(info),
|
|
|
|
"VirtualQuery failed\n");
|
|
|
|
ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
|
|
|
|
ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
|
2006-10-10 01:06:48 +02:00
|
|
|
ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
|
2004-02-09 21:58:16 +01:00
|
|
|
ok(info.RegionSize == 0x1000, "%lx != 0x1000\n", info.RegionSize);
|
2006-10-10 01:06:48 +02:00
|
|
|
ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
|
2004-02-09 21:58:16 +01:00
|
|
|
/* this time NT reports PAGE_NOACCESS as well */
|
2006-10-10 01:06:48 +02:00
|
|
|
ok(info.Protect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.Protect);
|
|
|
|
ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
/* this should fail, since not the whole range is committed yet */
|
|
|
|
SetLastError(0xdeadbeef);
|
|
|
|
ok(!VirtualProtect(addr1, 0xFFFC, PAGE_READONLY, &old_prot),
|
|
|
|
"VirtualProtect should fail on a not committed memory\n");
|
|
|
|
ok(GetLastError() == ERROR_INVALID_ADDRESS /* NT */ ||
|
|
|
|
GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x */
|
2006-10-10 01:06:48 +02:00
|
|
|
"got %d, expected ERROR_INVALID_ADDRESS\n", GetLastError());
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
ok(VirtualProtect(addr1, 0x1000, PAGE_READONLY, &old_prot), "VirtualProtect failed\n");
|
|
|
|
ok(old_prot == PAGE_NOACCESS,
|
2006-10-10 01:06:48 +02:00
|
|
|
"wrong old protection: got %04x instead of PAGE_NOACCESS\n", old_prot);
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
ok(VirtualProtect(addr1, 0x1000, PAGE_READWRITE, &old_prot), "VirtualProtect failed\n");
|
|
|
|
ok(old_prot == PAGE_READONLY,
|
2006-10-10 01:06:48 +02:00
|
|
|
"wrong old protection: got %04x instead of PAGE_READONLY\n", old_prot);
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
ok(!VirtualFree(addr1, 0x10000, 0), "VirtualFree should fail with type 0\n");
|
|
|
|
ok(GetLastError() == ERROR_INVALID_PARAMETER,
|
2006-10-10 01:06:48 +02:00
|
|
|
"got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
ok(VirtualFree(addr1, 0x10000, MEM_DECOMMIT), "VirtualFree failed\n");
|
|
|
|
|
|
|
|
/* if the type is MEM_RELEASE, size must be 0 */
|
|
|
|
ok(!VirtualFree(addr1, 1, MEM_RELEASE), "VirtualFree should fail\n");
|
|
|
|
ok(GetLastError() == ERROR_INVALID_PARAMETER,
|
2006-10-10 01:06:48 +02:00
|
|
|
"got %d, expected ERROR_INVALID_PARAMETER\n", GetLastError());
|
2004-02-09 21:58:16 +01:00
|
|
|
|
|
|
|
ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
|
|
|
|
}
|
|
|
|
|
2004-05-21 22:58:44 +02:00
|
|
|
static void test_MapViewOfFile(void)
|
|
|
|
{
|
|
|
|
static const char testfile[] = "testfile.xxx";
|
|
|
|
HANDLE file, mapping;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
file = CreateFileA( testfile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
|
|
|
|
ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
|
|
|
|
SetFilePointer( file, 4096, NULL, FILE_BEGIN );
|
|
|
|
SetEndOfFile( file );
|
|
|
|
|
|
|
|
/* read/write mapping */
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
|
|
|
|
ok( mapping != 0, "CreateFileMapping failed\n" );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAPE_READ failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
/* this fails on win9x but succeeds on NT */
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
|
|
|
|
if (ptr) UnmapViewOfFile( ptr );
|
2006-10-10 01:06:48 +02:00
|
|
|
else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAP_WRITE failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
/* read-only mapping */
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
|
|
|
|
ok( mapping != 0, "CreateFileMapping failed\n" );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
/* this fails on win9x but succeeds on NT */
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
|
|
|
|
if (ptr) UnmapViewOfFile( ptr );
|
2006-10-10 01:06:48 +02:00
|
|
|
else ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
|
|
|
|
ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
/* copy-on-write mapping */
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
|
|
|
|
ok( mapping != 0, "CreateFileMapping failed\n" );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAP_COPY failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
|
|
|
|
ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
/* no access mapping */
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_NOACCESS, 0, 4096, NULL );
|
|
|
|
/* fails on NT but succeeds on win9x */
|
2006-10-10 01:06:48 +02:00
|
|
|
if (!mapping) ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile FILE_MAP_READ failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 4096 );
|
|
|
|
ok( !ptr, "MapViewOfFile FILE_MAP_COPY succeeded\n" );
|
2006-10-10 01:06:48 +02:00
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, 0, 0, 0, 4096 );
|
|
|
|
ok( ptr != NULL, "MapViewOfFile 0 failed\n" );
|
|
|
|
UnmapViewOfFile( ptr );
|
|
|
|
|
|
|
|
ptr = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 4096 );
|
|
|
|
ok( !ptr, "MapViewOfFile FILE_MAP_WRITE succeeded\n" );
|
2006-10-10 01:06:48 +02:00
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
CloseHandle( mapping );
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle( file );
|
|
|
|
|
|
|
|
/* now try read-only file */
|
|
|
|
|
|
|
|
file = CreateFileA( testfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0 );
|
|
|
|
ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
|
|
|
|
ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
|
|
|
|
ok( mapping != 0, "CreateFileMapping PAGE_WRITECOPY failed\n" );
|
|
|
|
CloseHandle( mapping );
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
|
|
|
|
ok( mapping != 0, "CreateFileMapping PAGE_READONLY failed\n" );
|
|
|
|
CloseHandle( mapping );
|
|
|
|
CloseHandle( file );
|
|
|
|
|
|
|
|
/* now try no access file */
|
|
|
|
|
|
|
|
file = CreateFileA( testfile, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
|
|
|
|
ok( file != INVALID_HANDLE_VALUE, "Failed to create test file\n" );
|
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READWRITE, 0, 4096, NULL );
|
|
|
|
ok( !mapping, "CreateFileMapping PAGE_READWRITE succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_WRITECOPY, 0, 4096, NULL );
|
|
|
|
ok( !mapping, "CreateFileMapping PAGE_WRITECOPY succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
mapping = CreateFileMappingA( file, NULL, PAGE_READONLY, 0, 4096, NULL );
|
|
|
|
ok( !mapping, "CreateFileMapping PAGE_READONLY succeeded\n" );
|
|
|
|
ok( GetLastError() == ERROR_INVALID_PARAMETER ||
|
2006-10-10 01:06:48 +02:00
|
|
|
GetLastError() == ERROR_ACCESS_DENIED, "Wrong error %x\n", GetLastError() );
|
2004-05-21 22:58:44 +02:00
|
|
|
|
|
|
|
CloseHandle( file );
|
|
|
|
|
|
|
|
CloseHandle( file );
|
|
|
|
DeleteFileA( testfile );
|
|
|
|
}
|
|
|
|
|
2004-02-09 21:58:16 +01:00
|
|
|
START_TEST(virtual)
|
|
|
|
{
|
|
|
|
test_VirtualAlloc();
|
2004-05-21 22:58:44 +02:00
|
|
|
test_MapViewOfFile();
|
2004-02-09 21:58:16 +01:00
|
|
|
}
|