public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for space on offset allocation
@ 2022-09-07 15:45 Jeff Brasen
  2022-09-07 16:16 ` Ard Biesheuvel
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff Brasen @ 2022-09-07 15:45 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, abner.chang, git, quic_llindhol, Jeff Brasen

Update check for enough space to occur prior to alignment offset
modification. This prevents a case where EfiFreeMemoryTop could be
less than EfiFreeMemoryBottom

Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
---
 .../MemoryAllocationLib.c                     | 53 +++++++++++--------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
index 2cc2a71121..9208826565 100644
--- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
+++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
@@ -27,37 +27,44 @@ InternalAllocatePages (
 
   Hob.Raw = GetHobList ();
 
-  // Check to see if on 4k boundary
   Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
+  //
+  // Verify that there is sufficient memory to satisfy the allocation and padding prior to updating anything
+  //
+  if ((Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) - Offset) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
+    if (Offset != 0) {
+      DEBUG ((DEBUG_ERROR, "Offset applied without enough space\r\n"));
+    } else {
+      DEBUG ((DEBUG_ERROR, "Out of memory\r\n"));
+    }
+
+    ASSERT (FALSE);
+    return 0;
+  }
+
+  // Check to see if on 4k boundary
   if (Offset != 0) {
     // If not aligned, make the allocation aligned.
     Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
   }
 
   //
-  // Verify that there is sufficient memory to satisfy the allocation
+  // Update the PHIT to reflect the memory usage
   //
-  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
-    return 0;
-  } else {
-    //
-    // Update the PHIT to reflect the memory usage
-    //
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
-
-    // This routine used to create a memory allocation HOB a la PEI, but that's not
-    // necessary for us.
-
-    //
-    // Create a memory allocation HOB.
-    //
-    BuildMemoryAllocationHob (
-      Hob.HandoffInformationTable->EfiFreeMemoryTop,
-      Pages * EFI_PAGE_SIZE,
-      MemoryType
-      );
-    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
-  }
+  Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
+
+  // This routine used to create a memory allocation HOB a la PEI, but that's not
+  // necessary for us.
+
+  //
+  // Create a memory allocation HOB.
+  //
+  BuildMemoryAllocationHob (
+    Hob.HandoffInformationTable->EfiFreeMemoryTop,
+    Pages * EFI_PAGE_SIZE,
+    MemoryType
+    );
+  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
 }
 
 /**
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-07 20:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-07 15:45 [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for space on offset allocation Jeff Brasen
2022-09-07 16:16 ` Ard Biesheuvel
2022-09-07 20:56   ` Jeff Brasen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox