public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Jeff Brasen" <jbrasen@nvidia.com>
To: <devel@edk2.groups.io>
Cc: <quic_llindhol@quicinc.com>, <ardb+tianocore@kernel.org>,
	<abner.chang@hpe.com>, <daniel.schaefer@hpe.com>,
	Jeff Brasen <jbrasen@nvidia.com>
Subject: [PATCH] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation
Date: Thu, 30 Jun 2022 13:06:35 -0600	[thread overview]
Message-ID: <b59b641774b74933c5cb02a37417949cce88900d.1656615928.git.jbrasen@nvidia.com> (raw)

Update check for enough space to occur prior to alignment offset.
This prevents cases where EfiFreeMemoryTop < 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 78f8da5e95..1956d644c3 100644
--- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
+++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
@@ -38,37 +38,44 @@ AllocatePages (
 
   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,
-      EfiBootServicesData
-      );
-    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,
+    EfiBootServicesData
+    );
+  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
 }
 
 /**
-- 
2.25.1


             reply	other threads:[~2022-06-30 19:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30 19:06 Jeff Brasen [this message]
2022-09-07  8:33 ` [PATCH] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation Ard Biesheuvel
2022-09-07 14:46   ` Jeff Brasen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b59b641774b74933c5cb02a37417949cce88900d.1656615928.git.jbrasen@nvidia.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox