From: Jian J Wang Date: Thu, 15 Mar 2018 04:43:20 +0000 (+0800) Subject: MdeModulePkg/PiSmmCore: fix bits operation error on a boundary condition X-Git-Tag: edk2-stable201903~2108 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=883787a2c6f83a59fa56f9529317aa86be71fe91 MdeModulePkg/PiSmmCore: fix bits operation error on a boundary condition If given address is on 64K boundary and the requested bit number is 64, all SetBits(), ClearBits() and GetBits() will encounter ASSERT problem in trying to do a 64 bits of shift, which is not allowed by LShift() and RShift(). This patch tries to fix this issue by turning bits operation into whole integer operation in such situation. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Cc: Ruiyu Ni Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang Reviewed-by: Ruiyu Ni --- diff --git a/MdeModulePkg/Core/PiSmmCore/HeapGuard.c b/MdeModulePkg/Core/PiSmmCore/HeapGuard.c index 923af93de2..f9657f9baa 100644 --- a/MdeModulePkg/Core/PiSmmCore/HeapGuard.c +++ b/MdeModulePkg/Core/PiSmmCore/HeapGuard.c @@ -73,7 +73,7 @@ SetBits ( StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address); EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS; - if ((StartBit + BitNumber) > GUARDED_HEAP_MAP_ENTRY_BITS) { + if ((StartBit + BitNumber) >= GUARDED_HEAP_MAP_ENTRY_BITS) { Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) % GUARDED_HEAP_MAP_ENTRY_BITS; Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS; @@ -126,7 +126,7 @@ ClearBits ( StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address); EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS; - if ((StartBit + BitNumber) > GUARDED_HEAP_MAP_ENTRY_BITS) { + if ((StartBit + BitNumber) >= GUARDED_HEAP_MAP_ENTRY_BITS) { Msbs = (GUARDED_HEAP_MAP_ENTRY_BITS - StartBit) % GUARDED_HEAP_MAP_ENTRY_BITS; Lsbs = (EndBit + 1) % GUARDED_HEAP_MAP_ENTRY_BITS; @@ -191,10 +191,14 @@ GetBits ( Lsbs = 0; } - Result = RShiftU64 ((*BitMap), StartBit) & (LShiftU64 (1, Msbs) - 1); - if (Lsbs > 0) { - BitMap += 1; - Result |= LShiftU64 ((*BitMap) & (LShiftU64 (1, Lsbs) - 1), Msbs); + if (StartBit == 0 && BitNumber == GUARDED_HEAP_MAP_ENTRY_BITS) { + Result = *BitMap; + } else { + Result = RShiftU64((*BitMap), StartBit) & (LShiftU64(1, Msbs) - 1); + if (Lsbs > 0) { + BitMap += 1; + Result |= LShiftU64 ((*BitMap) & (LShiftU64 (1, Lsbs) - 1), Msbs); + } } return Result;