From 3d759a0c69732721fab2881e484bf716c196dfe3 Mon Sep 17 00:00:00 2001 From: Dmitry Timoshkov Date: Fri, 19 Jul 2013 14:01:16 +0900 Subject: [PATCH] ntdll: NtAllocateVirtualMemory should fail to commit if an address range is already committed for a memory mapped file. --- dlls/kernel32/tests/loader.c | 16 +++++++++------- dlls/kernel32/tests/virtual.c | 14 ++++++-------- dlls/ntdll/virtual.c | 1 + 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c index ef1cde408c5..0018d670dd9 100644 --- a/dlls/kernel32/tests/loader.c +++ b/dlls/kernel32/tests/loader.c @@ -396,12 +396,13 @@ static void test_Loader(void) SetLastError(0xdeadbeef); ptr = VirtualAlloc(hlib, si.dwPageSize, MEM_COMMIT, info.Protect); - /* FIXME: Remove once Wine is fixed */ - if (ptr) todo_wine ok(!ptr, "%d: VirtualAlloc should fail\n", i); - else ok(!ptr, "%d: VirtualAlloc should fail\n", i); + /* FIXME: Remove once Wine is fixed */ + if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY) todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); + else + ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); SetLastError(0xdeadbeef); size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)); @@ -484,14 +485,15 @@ todo_wine SetLastError(0xdeadbeef); ptr = VirtualAlloc((char *)hlib + section.VirtualAddress, si.dwPageSize, MEM_COMMIT, info.Protect); - /* FIXME: Remove once Wine is fixed */ - if (ptr) todo_wine ok(!ptr, "%d: VirtualAlloc should fail\n", i); - else ok(!ptr, "%d: VirtualAlloc should fail\n", i); + /* FIXME: Remove once Wine is fixed */ + if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY) todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); - + else + ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS, + "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); } SetLastError(0xdeadbeef); diff --git a/dlls/kernel32/tests/virtual.c b/dlls/kernel32/tests/virtual.c index e605242249c..99d32864d16 100644 --- a/dlls/kernel32/tests/virtual.c +++ b/dlls/kernel32/tests/virtual.c @@ -1923,13 +1923,12 @@ static void test_CreateFileMapping_protection(void) SetLastError(0xdeadbeef); ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, td[i].prot); + ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot); /* FIXME: remove once Wine is fixed */ if (td[i].prot == PAGE_WRITECOPY || td[i].prot == PAGE_EXECUTE_WRITECOPY) - ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot); +todo_wine + ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); else -todo_wine - ok(!ptr, "%d: VirtualAlloc(%02x) should fail\n", i, td[i].prot); -todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError()); SetLastError(0xdeadbeef); @@ -2427,13 +2426,12 @@ static void test_mapping(void) /*trace("map %#x, view %#x, requested prot %#x\n", page_prot[i], view[j].prot, page_prot[k]);*/ SetLastError(0xdeadbeef); ptr = VirtualAlloc(base, si.dwPageSize, MEM_COMMIT, page_prot[k]); + ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]); /* FIXME: remove once Wine is fixed */ if (page_prot[k] == PAGE_WRITECOPY || page_prot[k] == PAGE_EXECUTE_WRITECOPY) - ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]); +todo_wine + ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError()); else -todo_wine - ok(!ptr, "VirtualAlloc(%02x) should fail\n", page_prot[k]); -todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError()); } diff --git a/dlls/ntdll/virtual.c b/dlls/ntdll/virtual.c index 121071fd559..3ae1f4832ab 100644 --- a/dlls/ntdll/virtual.c +++ b/dlls/ntdll/virtual.c @@ -1974,6 +1974,7 @@ NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_ else /* commit the pages */ { if (!(view = VIRTUAL_FindView( base, size ))) status = STATUS_NOT_MAPPED_VIEW; + else if (view->mapping && (view->protect & VPROT_COMMITTED)) status = STATUS_ALREADY_COMMITTED; else if (!VIRTUAL_SetProt( view, base, size, vprot )) status = STATUS_ACCESS_DENIED; else if (view->mapping && !(view->protect & VPROT_COMMITTED)) {