]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/DxeCore: Fix issues in Heap Guard
authorJian J Wang <jian.j.wang@intel.com>
Sat, 9 Dec 2017 11:15:49 +0000 (19:15 +0800)
committerStar Zeng <star.zeng@intel.com>
Wed, 13 Dec 2017 03:42:57 +0000 (11:42 +0800)
One issue is that macros defined in HeapGuard.h

    GUARD_HEAP_TYPE_PAGE
    GUARD_HEAP_TYPE_POOL

doesn't match the definition of PCD PcdHeapGuardPropertyMask in
MdeModulePkg.dec. This patch fixed it by exchanging the BIT0 and BIT1
of them.

Another is that method AdjustMemoryF() will return a bigger NumberOfPages than
the value passed in. This is caused by counting twice of a shared Guard page
which can be used for both head and tail Guard of the memory before it and
after it. This happens only when partially freeing just one page in the middle
of a bunch of allocated pages. The freed page should be turned into a new
Guard page.

Cc: Jie Lin <jie.lin@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
MdeModulePkg/Core/Dxe/Mem/HeapGuard.c
MdeModulePkg/Core/Dxe/Mem/HeapGuard.h
MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/Core/Dxe/Mem/Pool.c

index 3a829854af5eb7f7ebaa57aa81aca23536a657df..bee229f4c8a73b3101b6c2c4e600706c73822766 100644 (file)
@@ -768,6 +768,7 @@ UnsetGuardForMemory (
   )\r
 {\r
   EFI_PHYSICAL_ADDRESS  GuardPage;\r
+  UINT64                GuardBitmap;\r
 \r
   if (NumberOfPages == 0) {\r
     return;\r
@@ -776,16 +777,29 @@ UnsetGuardForMemory (
   //\r
   // Head Guard must be one page before, if any.\r
   //\r
+  //          MSB-> 1     0 <-LSB\r
+  //          -------------------\r
+  //  Head Guard -> 0     1 -> Don't free Head Guard  (shared Guard)\r
+  //  Head Guard -> 0     0 -> Free Head Guard either (not shared Guard)\r
+  //                1     X -> Don't free first page  (need a new Guard)\r
+  //                           (it'll be turned into a Guard page later)\r
+  //          -------------------\r
+  //      Start -> -1    -2\r
+  //\r
   GuardPage = Memory - EFI_PAGES_TO_SIZE (1);\r
-  if (IsHeadGuard (GuardPage)) {\r
-    if (!IsMemoryGuarded (GuardPage - EFI_PAGES_TO_SIZE (1))) {\r
+  GuardBitmap = GetGuardedMemoryBits (Memory - EFI_PAGES_TO_SIZE (2), 2);\r
+  if ((GuardBitmap & BIT1) == 0) {\r
+    //\r
+    // Head Guard exists.\r
+    //\r
+    if ((GuardBitmap & BIT0) == 0) {\r
       //\r
       // If the head Guard is not a tail Guard of adjacent memory block,\r
       // unset it.\r
       //\r
       UnsetGuardPage (GuardPage);\r
     }\r
-  } else if (IsMemoryGuarded (GuardPage)) {\r
+  } else {\r
     //\r
     // Pages before memory to free are still in Guard. It's a partial free\r
     // case. Turn first page of memory block to free into a new Guard.\r
@@ -796,16 +810,29 @@ UnsetGuardForMemory (
   //\r
   // Tail Guard must be the page after this memory block to free, if any.\r
   //\r
+  //   MSB-> 1     0 <-LSB\r
+  //  --------------------\r
+  //         1     0 <- Tail Guard -> Don't free Tail Guard  (shared Guard)\r
+  //         0     0 <- Tail Guard -> Free Tail Guard either (not shared Guard)\r
+  //         X     1               -> Don't free last page   (need a new Guard)\r
+  //                                 (it'll be turned into a Guard page later)\r
+  //  --------------------\r
+  //        +1    +0 <- End\r
+  //\r
   GuardPage = Memory + EFI_PAGES_TO_SIZE (NumberOfPages);\r
-  if (IsTailGuard (GuardPage)) {\r
-    if (!IsMemoryGuarded (GuardPage + EFI_PAGES_TO_SIZE (1))) {\r
+  GuardBitmap = GetGuardedMemoryBits (GuardPage, 2);\r
+  if ((GuardBitmap & BIT0) == 0) {\r
+    //\r
+    // Tail Guard exists.\r
+    //\r
+    if ((GuardBitmap & BIT1) == 0) {\r
       //\r
       // If the tail Guard is not a head Guard of adjacent memory block,\r
       // free it; otherwise, keep it.\r
       //\r
       UnsetGuardPage (GuardPage);\r
     }\r
-  } else if (IsMemoryGuarded (GuardPage)) {\r
+  } else {\r
     //\r
     // Pages after memory to free are still in Guard. It's a partial free\r
     // case. We need to keep one page to be a head Guard.\r
@@ -895,6 +922,7 @@ AdjustMemoryF (
   EFI_PHYSICAL_ADDRESS  Start;\r
   EFI_PHYSICAL_ADDRESS  MemoryToTest;\r
   UINTN                 PagesToFree;\r
+  UINT64                GuardBitmap;\r
 \r
   if (Memory == NULL || NumberOfPages == NULL || *NumberOfPages == 0) {\r
     return;\r
@@ -906,9 +934,22 @@ AdjustMemoryF (
   //\r
   // Head Guard must be one page before, if any.\r
   //\r
-  MemoryToTest = Start - EFI_PAGES_TO_SIZE (1);\r
-  if (IsHeadGuard (MemoryToTest)) {\r
-    if (!IsMemoryGuarded (MemoryToTest - EFI_PAGES_TO_SIZE (1))) {\r
+  //          MSB-> 1     0 <-LSB\r
+  //          -------------------\r
+  //  Head Guard -> 0     1 -> Don't free Head Guard  (shared Guard)\r
+  //  Head Guard -> 0     0 -> Free Head Guard either (not shared Guard)\r
+  //                1     X -> Don't free first page  (need a new Guard)\r
+  //                           (it'll be turned into a Guard page later)\r
+  //          -------------------\r
+  //      Start -> -1    -2\r
+  //\r
+  MemoryToTest = Start - EFI_PAGES_TO_SIZE (2);\r
+  GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);\r
+  if ((GuardBitmap & BIT1) == 0) {\r
+    //\r
+    // Head Guard exists.\r
+    //\r
+    if ((GuardBitmap & BIT0) == 0) {\r
       //\r
       // If the head Guard is not a tail Guard of adjacent memory block,\r
       // free it; otherwise, keep it.\r
@@ -916,10 +957,10 @@ AdjustMemoryF (
       Start       -= EFI_PAGES_TO_SIZE (1);\r
       PagesToFree += 1;\r
     }\r
-  } else if (IsMemoryGuarded (MemoryToTest)) {\r
+  } else {\r
     //\r
-    // Pages before memory to free are still in Guard. It's a partial free\r
-    // case. We need to keep one page to be a tail Guard.\r
+    // No Head Guard, and pages before memory to free are still in Guard. It's a\r
+    // partial free case. We need to keep one page to be a tail Guard.\r
     //\r
     Start       += EFI_PAGES_TO_SIZE (1);\r
     PagesToFree -= 1;\r
@@ -928,19 +969,32 @@ AdjustMemoryF (
   //\r
   // Tail Guard must be the page after this memory block to free, if any.\r
   //\r
+  //   MSB-> 1     0 <-LSB\r
+  //  --------------------\r
+  //         1     0 <- Tail Guard -> Don't free Tail Guard  (shared Guard)\r
+  //         0     0 <- Tail Guard -> Free Tail Guard either (not shared Guard)\r
+  //         X     1               -> Don't free last page   (need a new Guard)\r
+  //                                 (it'll be turned into a Guard page later)\r
+  //  --------------------\r
+  //        +1    +0 <- End\r
+  //\r
   MemoryToTest = Start + EFI_PAGES_TO_SIZE (PagesToFree);\r
-  if (IsTailGuard (MemoryToTest)) {\r
-    if (!IsMemoryGuarded (MemoryToTest + EFI_PAGES_TO_SIZE (1))) {\r
+  GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);\r
+  if ((GuardBitmap & BIT0) == 0) {\r
+    //\r
+    // Tail Guard exists.\r
+    //\r
+    if ((GuardBitmap & BIT1) == 0) {\r
       //\r
       // If the tail Guard is not a head Guard of adjacent memory block,\r
       // free it; otherwise, keep it.\r
       //\r
       PagesToFree += 1;\r
     }\r
-  } else if (IsMemoryGuarded (MemoryToTest)) {\r
+  } else if (PagesToFree > 0) {\r
     //\r
-    // Pages after memory to free are still in Guard. It's a partial free\r
-    // case. We need to keep one page to be a head Guard.\r
+    // No Tail Guard, and pages after memory to free are still in Guard. It's a\r
+    // partial free case. We need to keep one page to be a head Guard.\r
     //\r
     PagesToFree -= 1;\r
   }\r
@@ -1054,11 +1108,14 @@ CoreConvertPagesWithGuard (
 {\r
   if (NewType == EfiConventionalMemory) {\r
     AdjustMemoryF (&Start, &NumberOfPages);\r
+    if (NumberOfPages == 0) {\r
+      return EFI_SUCCESS;\r
+    }\r
   } else {\r
     AdjustMemoryA (&Start, &NumberOfPages);\r
   }\r
 \r
-  return CoreConvertPages(Start, NumberOfPages, NewType);\r
+  return CoreConvertPages (Start, NumberOfPages, NewType);\r
 }\r
 \r
 /**\r
index bd7abd7c53266703871a32f642391fdfeb5d0629..30ac0e678f7784beee8f9bb2c5a620f08e96f603 100644 (file)
@@ -158,8 +158,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 //\r
 // Memory type to guard (matching the related PCD definition)\r
 //\r
-#define GUARD_HEAP_TYPE_POOL        BIT0\r
-#define GUARD_HEAP_TYPE_PAGE        BIT1\r
+#define GUARD_HEAP_TYPE_PAGE        BIT0\r
+#define GUARD_HEAP_TYPE_POOL        BIT1\r
 \r
 //\r
 // Debug message level\r
index 853606653cdb568013a93b581fa397fdbcbc44c5..db32d0f940de983898fc5a7a2d22a2eff8c58ea8 100644 (file)
@@ -920,21 +920,22 @@ CoreConvertPagesEx (
         UINT64  EndToClear;\r
 \r
         StartToClear = Start;\r
-        EndToClear   = RangeEnd;\r
+        EndToClear   = RangeEnd + 1;\r
         if (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT1|BIT0)) {\r
           if (IsGuardPage(StartToClear)) {\r
             StartToClear += EFI_PAGE_SIZE;\r
           }\r
-          if (IsGuardPage (EndToClear)) {\r
+          if (IsGuardPage (EndToClear - 1)) {\r
             EndToClear -= EFI_PAGE_SIZE;\r
           }\r
-          ASSERT (EndToClear > StartToClear);\r
         }\r
 \r
-        DEBUG_CLEAR_MEMORY(\r
-          (VOID *)(UINTN)StartToClear,\r
-          (UINTN)(EndToClear - StartToClear + 1)\r
-          );\r
+        if (EndToClear > StartToClear) {\r
+          DEBUG_CLEAR_MEMORY(\r
+            (VOID *)(UINTN)StartToClear,\r
+            (UINTN)(EndToClear - StartToClear)\r
+            );\r
+        }\r
       }\r
     }\r
 \r
index b82b51595c77335a8dece9a2aa6f892c019c9e51..7464d8773a2787b904d142dce9fa031cee566ef3 100644 (file)
@@ -642,7 +642,9 @@ CoreFreePoolPagesWithGuard (
   NoPagesGuarded = NoPages;\r
 \r
   AdjustMemoryF (&Memory, &NoPages);\r
-  CoreFreePoolPagesI (PoolType, Memory, NoPages);\r
+  if (NoPages > 0) {\r
+    CoreFreePoolPagesI (PoolType, Memory, NoPages);\r
+  }\r
 \r
   UnsetGuardForMemory (MemoryGuarded, NoPagesGuarded);\r
 }\r