ntdll: NtAllocateVirtualMemory should fail to commit if an address range is already committed for a memory mapped file.

Dmitry Timoshkov dmitry at baikal.ru
Thu Jun 27 21:55:31 CDT 2013


This patch fixes the problem reported in the bug 2770, hopefully it's still
acceptable during code freeze.
---
 dlls/kernel32/tests/loader.c  | 16 +++++++++-------
 dlls/kernel32/tests/virtual.c | 16 +++++++---------
 dlls/ntdll/virtual.c          |  1 +
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c
index ef1cde4..0018d67 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 521ce6e..4a28104 100644
--- a/dlls/kernel32/tests/virtual.c
+++ b/dlls/kernel32/tests/virtual.c
@@ -713,7 +713,7 @@ static void test_MapViewOfFile(void)
     ok(info.AllocationBase == ptr, "AllocationBase should have been %p but was %p instead\n", ptr, info.AllocationBase);
     ok(info.RegionSize == 0x10000, "RegionSize should have been 0x10000 but was 0x%lx\n", info.RegionSize);
     ok(info.State == MEM_COMMIT, "State should have been MEM_RESERVE instead of 0x%x\n", info.State);
-    ok(info.Protect == PAGE_READONLY, "Protect should have been 0 instead of 0x%x\n", info.Protect);
+    ok(info.Protect == PAGE_READONLY, "Protect should have been PAGE_READONLY instead of 0x%x\n", info.Protect);
     if (info.Type == MEM_PRIVATE)  /* win9x is different for uncommitted mappings */
     {
         ok(info.AllocationProtect == PAGE_NOACCESS,
@@ -1923,14 +1923,13 @@ 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);
-            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());
+            else
+            ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
 
             SetLastError(0xdeadbeef);
             ret = VirtualProtect(base, si.dwPageSize, td[i].prot, &old_prot);
@@ -2427,14 +2426,13 @@ 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]);
-                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());
+                else
+                ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
             }
 
             UnmapViewOfFile(base);
diff --git a/dlls/ntdll/virtual.c b/dlls/ntdll/virtual.c
index 121071f..3ae1f48 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))
         {
-- 
1.8.3.1




More information about the wine-patches mailing list