From c9b10ce0c114f022185c47306f7243e455ccde8b Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Wed, 1 Mar 2023 13:06:26 +0300 Subject: [PATCH] MdePkg: Redefined DEBUG_RAISE() to facilitate fuzzing. --- MdePkg/Include/Base.h | 16 ++++++++++++++-- MdePkg/Library/BasePeCoffLib2/BasePeCoffLib2.inf | 1 + MdePkg/Library/BasePeCoffLib2/PeCoffDebug.c | 3 ++- MdePkg/Library/BasePeCoffLib2/PeCoffHash.c | 3 ++- MdePkg/Library/BasePeCoffLib2/PeCoffHii.c | 3 ++- MdePkg/Library/BasePeCoffLib2/PeCoffInit.c | 1 + MdePkg/Library/BasePeCoffLib2/PeCoffLoad.c | 1 + MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf | 1 + .../BaseUefiImageLib/BaseUefiImageLibPeCoff.inf | 1 + .../BaseUefiImageLib/BaseUefiImageLibUe.inf | 3 +++ MdePkg/Library/BaseUefiImageLib/CommonSupport.c | 2 ++ MdePkg/Library/BaseUefiImageLib/UeSupport.c | 1 + MdePkg/MdePkg.dec | 7 +++++++ 13 files changed, 38 insertions(+), 5 deletions(-) diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index 4e6e3ce283..88cefd0ab4 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -885,8 +885,20 @@ STATIC_ASSERT (ALIGNOF (__VERIFY_UINT32_ENUM_SIZE) == sizeof (__VERIFY_UINT32_EN **/ #define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFSET_OF (TYPE, Field))) -// FIXME: Upstream general variants of these macros. -#define DEBUG_RAISE() ASSERT (FALSE) +#define DEBUG_RAISE() \ + do { \ + if ((PcdGet8 (PcdDebugRaisePropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0) { \ + DEBUG ((DEBUG_WARN, "DEBUG RAISE: Constraint violation in %a:%a:%u\n", __FILE__, __func__, __LINE__));\ + } \ + \ + if ((PcdGet8 (PcdDebugRaisePropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0) { \ + ASSERT (FALSE); \ + } \ + \ + if ((PcdGet8 (PcdDebugRaisePropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) { \ + CpuBreakpoint (); \ + } \ + } while (FALSE) /** Checks whether a value is a power of two. diff --git a/MdePkg/Library/BasePeCoffLib2/BasePeCoffLib2.inf b/MdePkg/Library/BasePeCoffLib2/BasePeCoffLib2.inf index 286102adf3..8428ccf694 100644 --- a/MdePkg/Library/BasePeCoffLib2/BasePeCoffLib2.inf +++ b/MdePkg/Library/BasePeCoffLib2/BasePeCoffLib2.inf @@ -47,3 +47,4 @@ gEfiMdePkgTokenSpaceGuid.PcdImageLoaderProhibitTe gEfiMdePkgTokenSpaceGuid.PcdImageLoaderAllowMisalignedOffset gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRemoveXForWX + gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffDebug.c b/MdePkg/Library/BasePeCoffLib2/PeCoffDebug.c index 41c0636c82..df280b88a9 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffDebug.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffDebug.c @@ -16,6 +16,7 @@ #include +#include #include #include #include @@ -139,7 +140,7 @@ PeCoffGetPdbPath ( DEBUG_RAISE (); return RETURN_UNSUPPORTED; } -// + // // Determine the raw file offset of the Debug Directory. // Sections = (CONST EFI_IMAGE_SECTION_HEADER *) (CONST VOID *) ( diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffHash.c b/MdePkg/Library/BasePeCoffLib2/PeCoffHash.c index e26c1597df..59c5b84836 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffHash.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffHash.c @@ -16,6 +16,7 @@ #include +#include #include #include #include @@ -298,7 +299,7 @@ PeCoffHashImageAuthenticode ( DEBUG_RAISE (); return FALSE; } -// + // // Perform the Section-related steps of the algorithm. // Result = InternalHashSections ( diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffHii.c b/MdePkg/Library/BasePeCoffLib2/PeCoffHii.c index d06d83b11e..ab70344f5a 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffHii.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffHii.c @@ -15,6 +15,7 @@ #include +#include #include #include #include @@ -266,7 +267,7 @@ PeCoffGetHiiDataRva ( ResourceDataEntry = (CONST EFI_IMAGE_RESOURCE_DATA_ENTRY *) (CONST VOID *) ( (CONST CHAR8 *) Context->ImageBuffer + Offset ); -// + // // Verify the "HII" data is in bounds of the Image buffer. // Overflow = BaseOverflowAddU32 ( diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c b/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c index b4e747e0b2..c400a30175 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c @@ -18,6 +18,7 @@ #include +#include #include #include #include diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffLoad.c b/MdePkg/Library/BasePeCoffLib2/PeCoffLoad.c index 3184b92056..ce23beb2c2 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffLoad.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffLoad.c @@ -16,6 +16,7 @@ #include +#include #include #include #include diff --git a/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf b/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf index be1e57710d..3ed5ea4f69 100644 --- a/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf +++ b/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf @@ -29,3 +29,4 @@ [FixedPcd] gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRelocTypePolicy + gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask diff --git a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf index 4b99759c27..cf9bb407dd 100644 --- a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf +++ b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibPeCoff.inf @@ -37,3 +37,4 @@ gEfiMdePkgTokenSpaceGuid.PcdImageLoaderLoadHeader gEfiMdePkgTokenSpaceGuid.PcdImageLoaderProhibitTe gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRemoveXForWX + gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask diff --git a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibUe.inf b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibUe.inf index d4caa7aca6..c10cca03d5 100644 --- a/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibUe.inf +++ b/MdePkg/Library/BaseUefiImageLib/BaseUefiImageLibUe.inf @@ -31,3 +31,6 @@ DebugLib MemoryAllocationLib UeImageLib + +[FixedPcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask diff --git a/MdePkg/Library/BaseUefiImageLib/CommonSupport.c b/MdePkg/Library/BaseUefiImageLib/CommonSupport.c index 05b49c195c..9aa07b9c73 100644 --- a/MdePkg/Library/BaseUefiImageLib/CommonSupport.c +++ b/MdePkg/Library/BaseUefiImageLib/CommonSupport.c @@ -10,9 +10,11 @@ #include #include +#include #include #include #include +#include #include RETURN_STATUS diff --git a/MdePkg/Library/BaseUefiImageLib/UeSupport.c b/MdePkg/Library/BaseUefiImageLib/UeSupport.c index 0620eda82d..2c68f43380 100644 --- a/MdePkg/Library/BaseUefiImageLib/UeSupport.c +++ b/MdePkg/Library/BaseUefiImageLib/UeSupport.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 69aa8f4871..31c3a1dc73 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -2322,6 +2322,13 @@ # @Expression 0x80000002 | (gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask & 0xC0) == 0 gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0|UINT8|0x00000005 + ## The mask is used to control DEBUG_RAISE() behavior.

+ # BIT0 - Enable Debug Assert.
+ # BIT1 - Enable Debug Print.
+ # BIT4 - Enable BreakPoint as ASSERT.
+ # @Prompt DEBUG_RAISE() Property. + gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask|0x01|UINT8|0x0000001b + ## This flag is used to control the print out Debug message.

# BIT0 - Initialization message.
# BIT1 - Warning message.