]> xenbits.xensource.com Git - ovmf.git/commitdiff
UefiCpuPkg/CpuPageTableLib:Modify RandomBoolean() in RandomTest
authorDun Tan <dun.tan@intel.com>
Fri, 17 Mar 2023 04:09:50 +0000 (12:09 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 27 Mar 2023 08:21:58 +0000 (08:21 +0000)
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>
UefiCpuPkg/Library/CpuPageTableLib/UnitTest/RandomTest.c

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