From a3657e3e7aaaccb1b9ea1b41a72d4564452e272d Mon Sep 17 00:00:00 2001 From: qhuang8 Date: Thu, 25 May 2006 10:13:26 +0000 Subject: [PATCH] =?utf8?q?MemoryAllocationLib:=20For=20boundary=20case:=20?= =?utf8?q?=E2=80=9CAllocationSize=20+=20OverAllocationSize=20>=3D=204G?= =?utf8?q?=E2=80=9D=20DxeMemoryAllocationLib:=20Change=20the=20behavior=20?= =?utf8?q?from=20returning=20NULL=20to=20ASSERT=20()=20PeiMemoryAllocation?= =?utf8?q?Lib:=20Add=20ASSERT=20()=20I=20also=20add=20ASSERT=20()=20in=20P?= =?utf8?q?ei=20Service=20AllocatePool=20()=20to=20catch=20if=20allocation?= =?utf8?q?=20size=20>=2064K=20DebugLib:=20Header=20file=20(DebugLib.h):=20?= =?utf8?q?Fix=20an=20issue=20in=20ASSERT=5FPROTOCOL=5FALREADY=5FINSTALLED(?= =?utf8?q?Handle,=20Guid).=20In=20contrast=20with=20LocateProtocol=20(),?= =?utf8?q?=20the=20first=20&=20second=20parameter=20type=20of=20HandleProt?= =?utf8?q?ocol=20()=20is=20EFI=5FHANDLE=20&=20EFI=5FGUID=20respectively.?= =?utf8?q?=20UefiLib:=20For=20UnicodeStringDisplayLength=20(CONST=20CHAR8?= =?utf8?q?=20*String),=20return=200=20if=20String=20is=20NULL.=20BasePrint?= =?utf8?q?Lib:=20Add=20missing=20=E2=80=9CEFIAPI=E2=80=9D=20to=20UnicodeVa?= =?utf8?q?lueToString()=20and=20AsciiValueToString()=20and=20move=20their?= =?utf8?q?=20definitions=20from=20PrintLibInternal.c=20to=20PrintLib.c.=20?= =?utf8?q?Fix=20the=20comments=20error(Maximum=20Length=20TIME=E2=80=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@275 6f19259b-4bc3-4df7-8a09-765794883524 --- EdkModulePkg/Core/Pei/Memory/MemoryServices.c | 8 +++++-- MdePkg/Include/Library/DebugLib.h | 12 +++++----- MdePkg/Include/Library/PrintLib.h | 2 ++ MdePkg/Library/BasePrintLib/PrintLib.c | 24 +++++++++++++++++++ .../Library/BasePrintLib/PrintLibInternal.c | 23 ------------------ .../Library/BasePrintLib/PrintLibInternal.h | 10 +++++++- .../MemoryAllocationLib.c | 23 ++++++++---------- .../MemoryAllocationLib.c | 13 ++++++++++ MdePkg/Library/UefiLib/Console.c | 4 ++++ 9 files changed, 74 insertions(+), 45 deletions(-) diff --git a/EdkModulePkg/Core/Pei/Memory/MemoryServices.c b/EdkModulePkg/Core/Pei/Memory/MemoryServices.c index 48b2b3818c..eb99a8040d 100644 --- a/EdkModulePkg/Core/Pei/Memory/MemoryServices.c +++ b/EdkModulePkg/Core/Pei/Memory/MemoryServices.c @@ -301,8 +301,12 @@ Returns: EFI_STATUS Status; EFI_HOB_MEMORY_POOL *Hob; - - Status = PeiCoreCreateHob ( + // + // If some ¡°post-memory¡± PEIM wishes to allocate larger pool, + // it should use AllocatePages service instead. + // + ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)); + Status = PeiCoreCreateHob ( EFI_HOB_TYPE_PEI_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size), (VOID **)&Hob diff --git a/MdePkg/Include/Library/DebugLib.h b/MdePkg/Include/Library/DebugLib.h index 82f9e6fdba..5efb304016 100644 --- a/MdePkg/Include/Library/DebugLib.h +++ b/MdePkg/Include/Library/DebugLib.h @@ -72,7 +72,7 @@ DebugPrint ( Prints an assert message containing a filename, line number, and description. This may be followed by a breakpoint or a dead loop. - Print a message of the form “ASSERT (): \n” + Print a message of the form “ASSERT (): \n? to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then @@ -81,9 +81,9 @@ DebugPrint ( DebugAssert() must actively prevent recusrsion. If DebugAssert() is called while processing another DebugAssert(), then DebugAssert() must return immediately. - If FileName is NULL, then a string of “(NULL) Filename” is printed. + If FileName is NULL, then a string of ?NULL) Filename?is printed. - If Description is NULL, then a string of “(NULL) Description” is printed. + If Description is NULL, then a string of ?NULL) Description?is printed. @param FileName Pointer to the name of the source file that generated the assert condition. @param LineNumber The line number in the source file that generated the assert condition @@ -108,7 +108,7 @@ DebugAssert ( If Buffer is NULL, then ASSERT(). - If Length is greater than (MAX_ADDRESS – Buffer + 1), then ASSERT(). + If Length is greater than (MAX_ADDRESS ?Buffer + 1), then ASSERT(). @param Buffer Pointer to the target buffer to fill with PcdDebugClearMemoryValue. @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. @@ -321,7 +321,7 @@ DebugClearMemoryEnabled ( _ASSERT (Guid already installed in database); \ } \ } else { \ - if (!EFI_ERROR (gBS->HandleProtocol (Guid, Handle, &Instance))) { \ + if (!EFI_ERROR (gBS->HandleProtocol (Handle, Guid, &Instance))) { \ _ASSERT (Guid already installed on Handle); \ } \ } \ @@ -411,7 +411,7 @@ DebugClearMemoryEnabled ( by TYPE is compared to TestSignature. If the signatures match, then a pointer to the pointer to a data structure of the type specified by TYPE is returned. If the signatures do not match, then DebugAssert() is called with a description - of “CR has a bad signature” and Record is returned. + of “CR has a bad signature?and Record is returned. If the data type specified by TYPE does not contain the field specified by Field, then the module will not compile. diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePkg/Include/Library/PrintLib.h index c84b7c36aa..3eb84f311b 100644 --- a/MdePkg/Include/Library/PrintLib.h +++ b/MdePkg/Include/Library/PrintLib.h @@ -98,6 +98,7 @@ AsciiSPrintUnicodeFormat ( ); UINTN +EFIAPI UnicodeValueToString ( IN OUT CHAR16 *Buffer, IN UINTN Flags, @@ -106,6 +107,7 @@ UnicodeValueToString ( ); UINTN +EFIAPI AsciiValueToString ( IN OUT CHAR8 *Buffer, IN UINTN Flags, diff --git a/MdePkg/Library/BasePrintLib/PrintLib.c b/MdePkg/Library/BasePrintLib/PrintLib.c index 816c887088..bbd0820106 100644 --- a/MdePkg/Library/BasePrintLib/PrintLib.c +++ b/MdePkg/Library/BasePrintLib/PrintLib.c @@ -637,3 +637,27 @@ AsciiSPrintUnicodeFormat ( VA_START (Marker, FormatString); return AsciiVSPrintUnicodeFormat (StartOfBuffer, BufferSize, FormatString, Marker); } + +UINTN +EFIAPI +UnicodeValueToString ( + IN OUT CHAR16 *Buffer, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2); +} + +UINTN +EFIAPI +AsciiValueToString ( + IN OUT CHAR8 *Buffer, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width + ) +{ + return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1); +} diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Library/BasePrintLib/PrintLibInternal.c index 24d8b56598..56d357525c 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -128,26 +128,3 @@ BasePrintLibConvertValueToString ( return ((Buffer - OriginalBuffer) / Increment); } - - -UINTN -UnicodeValueToString ( - IN OUT CHAR16 *Buffer, - IN UINTN Flags, - IN INT64 Value, - IN UINTN Width - ) -{ - return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 2); -} - -UINTN -AsciiValueToString ( - IN OUT CHAR8 *Buffer, - IN UINTN Flags, - IN INT64 Value, - IN UINTN Width - ) -{ - return BasePrintLibConvertValueToString ((CHAR8 *)Buffer, Flags, Value, Width, 1); -} diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.h b/MdePkg/Library/BasePrintLib/PrintLibInternal.h index de08f0edfc..c7fecfd34c 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.h +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.h @@ -39,7 +39,7 @@ /// Maximum Length Decimal String = 28 "-9,223,372,036,854,775,808" /// Maximum Length Hexidecimal String = 17 "FFFFFFFFFFFFFFFF" /// Maximum Length GUID = 37 "00000000-0000-0000-0000-000000000000" -/// Maximum Length TIME = 17 "12/12/2006 12:12" +/// Maximum Length TIME = 18 "12/12/2006 12:12" /// #define MAXIMUM_VALUE_CHARACTERS 38 @@ -85,3 +85,11 @@ BasePrintLibValueToString ( IN UINTN Radix ); +UINTN +BasePrintLibConvertValueToString ( + IN OUT CHAR8 *Buffer, + IN UINTN Flags, + IN INT64 Value, + IN UINTN Width, + IN UINTN Increment + ); diff --git a/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c index ef643eed42..088a10bb56 100644 --- a/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c @@ -168,13 +168,11 @@ InternalAllocateAlignedPages ( // AlignmentMask = Alignment - 1; RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); - if (RealPages <= Pages) { - // - // This extra checking is to make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. - // - return NULL; - } - + // + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + ASSERT (RealPages > Pages); + Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); if (EFI_ERROR (Status)) { return NULL; @@ -601,12 +599,11 @@ InternalAllocateAlignedPool ( // OverAllocationSize = sizeof (RawAddress) + AlignmentMask; RealAllocationSize = AllocationSize + OverAllocationSize; - if (RealAllocationSize <= AllocationSize ) { - // - // This extra checking is to make sure that AllocationSize plus OverAllocationSize does not overflow. - // - return NULL; - } + // + // Make sure that AllocationSize plus OverAllocationSize does not overflow. + // + ASSERT (RealAllocationSize > AllocationSize); + RawAddress = InternalAllocatePool (PoolType, RealAllocationSize); if (RawAddress == NULL) { return NULL; diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 63641569ff..274287cf03 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -158,6 +158,10 @@ InternalAllocateAlignedPages ( return NULL; } // + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); + // // We would rather waste some memory to save PEI code size. // Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment)); @@ -569,6 +573,10 @@ InternalAllocateAlignedPool ( } else { AlignmentMask = Alignment - 1; } + // + // Make sure that AllocationSize plus AlignmentMask does not overflow. + // + ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask)); RawAddress = InternalAllocatePool (PoolType, AllocationSize + AlignmentMask); @@ -609,6 +617,11 @@ AllocateAlignedPool ( AlignmentMask = Alignment - 1; } + // + // Make sure that AllocationSize plus AlignmentMask does not overflow. + // + ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask)); + RawAddress = AllocatePool (AllocationSize + AlignmentMask); AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask; diff --git a/MdePkg/Library/UefiLib/Console.c b/MdePkg/Library/UefiLib/Console.c index d06b0a932d..4fbdef7350 100644 --- a/MdePkg/Library/UefiLib/Console.c +++ b/MdePkg/Library/UefiLib/Console.c @@ -263,6 +263,10 @@ UnicodeStringDisplayLength ( UINTN Length; UINTN Width; + if (String == NULL) { + return 0; + } + Length = 0; while (*String != 0) { Width = GetGlyphWidth (*String); -- 2.39.2