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 <kun.qin@microsoft.com>
This commit is contained in:
Kun Qin 2026-07-16 13:55:57 -07:00 committed by mergify[bot]
parent d47954ceb4
commit 4d024b0cde
9 changed files with 169 additions and 53 deletions

View file

@ -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

View file

@ -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.

View file

@ -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.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiPei.h>
#include <Uefi.h>
#include <Library/UnitTestLib.h>
/**
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;
}

View file

@ -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.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiPei.h>
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/UnitTestLib.h>
/**
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;
}

View file

@ -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

View file

@ -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

View file

@ -27,6 +27,9 @@
[Sources]
SampleUnitTestGenerateException.c
[Sources.IA32, Sources.X64]
SampleUnitTestGenerateExceptionIA32X64.c
[Packages]
MdePkg/MdePkg.dec

View file

@ -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

View file

@ -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 @@
<BuildOptions>
MSFT:*_*_*_CC_FLAGS = /wd4723
}
UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf {
<BuildOptions>
MSFT:*_*_*_CC_FLAGS = /wd4723
}
UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestUefiShellGenerateException.inf {
<BuildOptions>
MSFT:*_*_*_CC_FLAGS = /wd4723
}
[Components.IA32, Components.X64]
UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTestGenerateException/SampleUnitTestSmmGenerateException.inf {
<BuildOptions>
MSFT:*_*_*_CC_FLAGS = /wd4723
}