UefiCpuPkg/CpuPageTableLib:Modify RandomBoolean() in RandomTest

Add an input parameter to control the probability of returning
true. Change RandomBoolean() in RandomTest from 50% chance
returning true to returning true with the percentage of input
Probability.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Tested-by: Gerd Hoffmann <kraxel@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Dun Tan 2023-03-17 12:09:50 +08:00 committed by mergify[bot]
parent 14ef21e559
commit 661a669c81

View File

@ -1,7 +1,7 @@
/** @file /** @file
Random test case for Unit tests of the CpuPageTableLib instance of the CpuPageTableLib class Random test case for Unit tests of the CpuPageTableLib instance of the CpuPageTableLib class
Copyright (c) 2022, Intel Corporation. All rights reserved.<BR> Copyright (c) 2022 - 2023, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent SPDX-License-Identifier: BSD-2-Clause-Patent
**/ **/
@ -81,22 +81,6 @@ LocalRandomBytes (
} }
} }
/**
Return a random boolean.
@return boolean
**/
BOOLEAN
RandomBoolean (
VOID
)
{
BOOLEAN Value;
LocalRandomBytes ((UINT8 *)&Value, sizeof (BOOLEAN));
return Value%2;
}
/** /**
Return a 32bit random number. Return a 32bit random number.
@ -139,6 +123,21 @@ Random64 (
return (UINT64)(Value % (Limit - Start + 1)) + Start; return (UINT64)(Value % (Limit - Start + 1)) + Start;
} }
/**
Returns true with the percentage of input Probability.
@param[in] Probability The percentage to return true.
@return boolean
**/
BOOLEAN
RandomBoolean (
UINT8 Probability
)
{
return ((Probability > ((UINT8)Random64 (0, 100))) ? TRUE : FALSE);
}
/** /**
Check if the Page table entry is valid Check if the Page table entry is valid
@ -178,7 +177,7 @@ ValidateAndRandomeModifyPageTablePageTableEntry (
UT_ASSERT_EQUAL ((PagingEntry->Uint64 & mValidMaskLeaf[Level].Uint64), PagingEntry->Uint64); UT_ASSERT_EQUAL ((PagingEntry->Uint64 & mValidMaskLeaf[Level].Uint64), PagingEntry->Uint64);
} }
if ((RandomNumber < 100) && RandomBoolean ()) { if ((RandomNumber < 100) && RandomBoolean (50)) {
RandomNumber++; RandomNumber++;
if (Level == 1) { if (Level == 1) {
TempPhysicalBase = PagingEntry->Pte4K.Bits.PageTableBaseAddress; TempPhysicalBase = PagingEntry->Pte4K.Bits.PageTableBaseAddress;
@ -211,7 +210,7 @@ ValidateAndRandomeModifyPageTablePageTableEntry (
UT_ASSERT_EQUAL ((PagingEntry->Uint64 & mValidMaskNoLeaf[Level].Uint64), PagingEntry->Uint64); UT_ASSERT_EQUAL ((PagingEntry->Uint64 & mValidMaskNoLeaf[Level].Uint64), PagingEntry->Uint64);
} }
if ((RandomNumber < 100) && RandomBoolean ()) { if ((RandomNumber < 100) && RandomBoolean (50)) {
RandomNumber++; RandomNumber++;
TempPhysicalBase = PagingEntry->Pnle.Bits.PageTableBaseAddress; TempPhysicalBase = PagingEntry->Pnle.Bits.PageTableBaseAddress;
@ -299,7 +298,7 @@ GenerateSingleRandomMapEntry (
// //
// use AlignedTable to avoid that a random number can be very hard to be 1G or 2M aligned // use AlignedTable to avoid that a random number can be very hard to be 1G or 2M aligned
// //
if ((MapsIndex != 0) && (RandomBoolean ())) { if ((MapsIndex != 0) && (RandomBoolean (50))) {
FormerLinearAddress = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].LinearAddress; FormerLinearAddress = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].LinearAddress;
if (FormerLinearAddress < 2 * (UINT64)SIZE_1GB) { if (FormerLinearAddress < 2 * (UINT64)SIZE_1GB) {
FormerLinearAddressBottom = 0; FormerLinearAddressBottom = 0;
@ -323,7 +322,7 @@ GenerateSingleRandomMapEntry (
// //
MapEntrys->Maps[MapsIndex].Length = Random64 (0, MIN (MaxAddress - MapEntrys->Maps[MapsIndex].LinearAddress, 10 * (UINT64)SIZE_1GB)) & AlignedTable[Random32 (0, ARRAY_SIZE (AlignedTable) -1)]; MapEntrys->Maps[MapsIndex].Length = Random64 (0, MIN (MaxAddress - MapEntrys->Maps[MapsIndex].LinearAddress, 10 * (UINT64)SIZE_1GB)) & AlignedTable[Random32 (0, ARRAY_SIZE (AlignedTable) -1)];
if ((MapsIndex != 0) && (RandomBoolean ())) { if ((MapsIndex != 0) && (RandomBoolean (50))) {
MapEntrys->Maps[MapsIndex].Attribute.Uint64 = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].Attribute.Uint64; MapEntrys->Maps[MapsIndex].Attribute.Uint64 = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].Attribute.Uint64;
MapEntrys->Maps[MapsIndex].Mask.Uint64 = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].Mask.Uint64; MapEntrys->Maps[MapsIndex].Mask.Uint64 = MapEntrys->Maps[Random32 (0, (UINT32)MapsIndex-1)].Mask.Uint64;
} else { } else {
@ -344,7 +343,7 @@ GenerateSingleRandomMapEntry (
// Need to avoid such case when remove the Random option ONLY_ONE_ONE_MAPPING // Need to avoid such case when remove the Random option ONLY_ONE_ONE_MAPPING
// //
MapEntrys->Maps[MapsIndex].Attribute.Bits.PageTableBaseAddress = (Random64 (0, (((UINT64)1)<<52) - 1) & AlignedTable[Random32 (0, ARRAY_SIZE (AlignedTable) -1)])>> 12; MapEntrys->Maps[MapsIndex].Attribute.Bits.PageTableBaseAddress = (Random64 (0, (((UINT64)1)<<52) - 1) & AlignedTable[Random32 (0, ARRAY_SIZE (AlignedTable) -1)])>> 12;
if (RandomBoolean ()) { if (RandomBoolean (50)) {
MapEntrys->Maps[MapsIndex].Mask.Bits.PageTableBaseAddress = 0; MapEntrys->Maps[MapsIndex].Mask.Bits.PageTableBaseAddress = 0;
} }
} }