From 4d024b0cde7527e6b0fbb363881c2c1aa751b23b Mon Sep 17 00:00:00 2001 From: Kun Qin Date: Thu, 16 Jul 2026 13:55:57 -0700 Subject: [PATCH] UnitTestFrameworkPkg: SampleUnitTestGenerateException: Fix for AArch64 AArch64 does not generate divide-by-zero exceptions like x86. Yet the current generate exception test is using this operation to attempt triggering exceptions. This change abstracted the exception generation logic into per-arch files and use undefined instruction to trigger AArch64 exceptions. Signed-off-by: Kun Qin --- .../SampleUnitTestDxeGenerateException.inf | 8 ++- .../SampleUnitTestGenerateException.c | 66 ++++++------------ .../SampleUnitTestGenerateExceptionAArch64.c | 41 +++++++++++ .../SampleUnitTestGenerateExceptionIA32X64.c | 69 +++++++++++++++++++ .../SampleUnitTestHostGenerateException.inf | 8 ++- .../SampleUnitTestPeiGenerateException.inf | 8 ++- .../SampleUnitTestSmmGenerateException.inf | 3 + ...mpleUnitTestUefiShellGenerateException.inf | 8 ++- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc | 11 +-- 9 files changed, 169 insertions(+), 53 deletions(-) create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionAArch64.c create mode 100644 UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionIA32X64.c diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf index b742befe4d..d3ef3ddffd 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestDxeGenerateException.inf @@ -20,12 +20,18 @@ # # The following information is for reference only and not required by the build tools. # -# VALID_ARCHITECTURES = IA32 X64 +# VALID_ARCHITECTURES = IA32 X64 AARCH64 # [Sources] SampleUnitTestGenerateException.c +[Sources.IA32, Sources.X64] + SampleUnitTestGenerateExceptionIA32X64.c + +[Sources.AARCH64] + SampleUnitTestGenerateExceptionAArch64.c + [Packages] MdePkg/MdePkg.dec diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c index 4576e0c81e..26d3f8b020 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateException.c @@ -20,6 +20,27 @@ #define UNIT_TEST_NAME "Sample Unit Test Generate Exception" #define UNIT_TEST_VERSION "0.1" +/** + Sample unit test the triggers an unexpected exception + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +GenerateUnexpectedException ( + IN UNIT_TEST_CONTEXT Context + ); + /** Unit-Test Test Suite Setup (before) function that enables ASSERT() macros. **/ @@ -50,51 +71,6 @@ TestSuiteDisableAsserts ( PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (~BIT0)); } -UINTN -DivideWithNoParameterChecking ( - UINTN Dividend, - UINTN Divisor - ) -{ - // - // Perform integer division with no check for divide by zero - // - return (Dividend / Divisor); -} - -/** - Sample unit test the triggers an unexpected exception - - @param[in] Context [Optional] An optional parameter that enables: - 1) test-case reuse with varied parameters and - 2) test-case re-entry for Target tests that need a - reboot. This parameter is a VOID* and it is the - responsibility of the test author to ensure that the - contents are well understood by all test cases that may - consume it. - - @retval UNIT_TEST_PASSED The Unit test has completed and the test - case was successful. - @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. -**/ -UNIT_TEST_STATUS -EFIAPI -GenerateUnexpectedException ( - IN UNIT_TEST_CONTEXT Context - ) -{ - // - // Assertion that passes without generating an exception - // - UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 1), (UINTN)20); - // - // Assertion that generates divide by zero exception before result evaluated - // - UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 0), MAX_UINTN); - - return UNIT_TEST_PASSED; -} - /** Initialize the unit test framework, suite, and unit tests for the sample unit tests and run the unit tests. diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionAArch64.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionAArch64.c new file mode 100644 index 0000000000..b6ef5ea817 --- /dev/null +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionAArch64.c @@ -0,0 +1,41 @@ +/** @file + This is a sample to demonstrate the usage of the Unit Test Library that + supports the PEI, DXE, SMM, UEFI Shell, and host execution environments. + This test case generates an exception. For some host-based environments, this + is a fatal condition that terminates the unit tests and no additional test + cases are executed. On other environments, this condition may be report a unit + test failure and continue with additional unit tests. + + Copyright (c) 2024, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include + +/** + Sample unit test the triggers an unexpected exception + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +GenerateUnexpectedException ( + IN UNIT_TEST_CONTEXT Context + ) +{ + asm volatile ("udf #0"); + + return UNIT_TEST_PASSED; +} diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionIA32X64.c b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionIA32X64.c new file mode 100644 index 0000000000..5b5d4c81b5 --- /dev/null +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestGenerateExceptionIA32X64.c @@ -0,0 +1,69 @@ +/** @file + This is a sample to demonstrate the usage of the Unit Test Library that + supports the PEI, DXE, SMM, UEFI Shell, and host execution environments. + This test case generates an exception. For some host-based environments, this + is a fatal condition that terminates the unit tests and no additional test + cases are executed. On other environments, this condition may be report a unit + test failure and continue with additional unit tests. + + Copyright (c) 2024, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include + +/** + Helper function to perform a division operation. + + @param[in] Dividend Dividend of the operation. + @param[in] Divisor Divisor of the operation. + + @return Result of the division. +**/ +UINTN +DivideWithNoParameterChecking ( + UINTN Dividend, + UINTN Divisor + ) +{ + // + // Perform integer division with no check for divide by zero + // + return (Dividend / Divisor); +} + +/** + Sample unit test the triggers an unexpected exception + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +GenerateUnexpectedException ( + IN UNIT_TEST_CONTEXT Context + ) +{ + // + // Assertion that passes without generating an exception + // + UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 1), (UINTN)20); + // + // Assertion that generates divide by zero exception before result evaluated + // + UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 0), MAX_UINTN); + + return UNIT_TEST_PASSED; +} diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf index a9f10ff184..11328320b2 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestHostGenerateException.inf @@ -19,12 +19,18 @@ # # The following information is for reference only and not required by the build tools. # -# VALID_ARCHITECTURES = IA32 X64 +# VALID_ARCHITECTURES = IA32 X64 AARCH64 # [Sources] SampleUnitTestGenerateException.c +[Sources.IA32, Sources.X64] + SampleUnitTestGenerateExceptionIA32X64.c + +[Sources.AARCH64] + SampleUnitTestGenerateExceptionAArch64.c + [Packages] MdePkg/MdePkg.dec diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf index cb26961568..c20155ba09 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestPeiGenerateException.inf @@ -20,12 +20,18 @@ # # The following information is for reference only and not required by the build tools. # -# VALID_ARCHITECTURES = IA32 X64 +# VALID_ARCHITECTURES = IA32 X64 AARCH64 # [Sources] SampleUnitTestGenerateException.c +[Sources.IA32, Sources.X64] + SampleUnitTestGenerateExceptionIA32X64.c + +[Sources.AARCH64] + SampleUnitTestGenerateExceptionAArch64.c + [Packages] MdePkg/MdePkg.dec diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf index 5aee6a52bd..3c3c56afed 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf @@ -27,6 +27,9 @@ [Sources] SampleUnitTestGenerateException.c +[Sources.IA32, Sources.X64] + SampleUnitTestGenerateExceptionIA32X64.c + [Packages] MdePkg/MdePkg.dec diff --git a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf index 32d6f4270a..2a90537294 100644 --- a/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf +++ b/UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf @@ -20,12 +20,18 @@ # # The following information is for reference only and not required by the build tools. # -# VALID_ARCHITECTURES = IA32 X64 +# VALID_ARCHITECTURES = IA32 X64 AARCH64 # [Sources] SampleUnitTestGenerateException.c +[Sources.IA32, Sources.X64] + SampleUnitTestGenerateExceptionIA32X64.c + +[Sources.AARCH64] + SampleUnitTestGenerateExceptionAArch64.c + [Packages] MdePkg/MdePkg.dec diff --git a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc b/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc index 0fe700fc62..fed90a7cbe 100644 --- a/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc +++ b/UnitTestFrameworkPkg/UnitTestFrameworkPkg.dsc @@ -47,6 +47,7 @@ UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestSmmExpectFail.inf UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestExpectFail/SampleUnitTestUefiShellExpectFail.inf +[Components.IA32, Components.X64, Components.AARCH64] # # Disable warning for divide by zero to pass build of unit tests # that generate a divide by zero exception. @@ -59,11 +60,13 @@ MSFT:*_*_*_CC_FLAGS = /wd4723 } - UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf { - - MSFT:*_*_*_CC_FLAGS = /wd4723 - } UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf { MSFT:*_*_*_CC_FLAGS = /wd4723 } + +[Components.IA32, Components.X64] + UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf { + + MSFT:*_*_*_CC_FLAGS = /wd4723 + }