From d436d5ca0936b1be59bd60087cd0b6e0d2e08d76 Mon Sep 17 00:00:00 2001 From: mdkinney Date: Fri, 22 Mar 2013 21:20:07 +0000 Subject: [PATCH] Guarantee that free memory in the 4K page starting at address 0 is always cleared to 0. The algorithm is to clear page zero if it is registered with the DXE Core with type EfiConventionalMemory, and to also clear page zero if it is freed using the UEFI Boot Service FreePages(). This patch improves OS compatibility for OSes that may evaluate page 0 for legacy data structures. Before this patch, free memory may contain random values which induces random boot failures for some OSes. This patch may also help find NULL pointer bugs sooner because all of the fields in a data structure dereferenced through NULL will also be NULL now. Signed-off-by: Michael Kinney Reviewed-by: Laszlo Ersek Reviewed-by: Jordan Justen git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14218 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Core/Dxe/Mem/Page.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index 8a6d35d6e4..90976a1f0f 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -177,7 +177,20 @@ CoreAddRange ( ASSERT_LOCKED (&gMemoryLock); DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type)); - + + // + // If memory of type EfiConventionalMemory is being added that includes the page + // starting at address 0, then zero the page starting at address 0. This has + // two benifits. It helps find NULL pointer bugs and it also maximizes + // compatibility with operating systems that may evaluate memory in this page + // for legacy data structures. If memory of any other type is added starting + // at address 0, then do not zero the page at address 0 because the page is being + // used for other purposes. + // + if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) { + SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0); + } + // // Memory map being altered so updated key // -- 2.39.2