edk2/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTestGenerateException/SampleGoogleTestGenerateException.cpp
Kun Qin d47954ceb4 UnitTestFrameworkPkg: SampleGoogleTestGenerateException: Support AArch64
The SampleGoogleTestGenerateException GoogleTest sample previously
generated a CPU exception by performing an integer divide by zero. That
mechanism is X86-specific: the AArch64 architecture defines SDIV/UDIV by
zero to return zero rather than raising a synchronous abort, so the
sample produced no exception when built and run on AArch64 hosts and the
test failed due to the test assert instead of triggering an exception
through the framework.

This change replaces the divide-by-zero with a NULL pointer write
performed through a small volatile helper, which is expected to work on
all host systems.

The MSVC '/wd4723' build option is no longer needed because it was added
to silence the divide-by-zero compile-time warning.

Signed-off-by: Kun Qin <kun.qin@microsoft.com>
2026-07-31 15:16:47 +00:00

66 lines
1.8 KiB
C++

/** @file
This is a sample to demonstrates the use of GoogleTest that supports host
execution environments for test case that 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.
A NULL pointer write is used to generate the exception because it produces
a fatal CPU exception (translation/page fault) on every architecture
supported by EDK II host-based testing.
Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/GoogleTestLib.h>
extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
}
VOID
WriteByteWithNoParameterChecking (
VOID *Address,
UINT8 Value
)
{
//
// Perform a byte write to the supplied address with no validity check.
//
*(volatile UINT8 *)Address = Value;
}
/**
Sample unit test that generates an unexpected exception by writing to a
NULL pointer.
**/
TEST (ExceptionTest, GenerateExceptionExpectTestFail) {
UINT8 LocalByte;
LocalByte = 0;
//
// Assertion that passes without generating an exception
//
WriteByteWithNoParameterChecking (&LocalByte, 0xA5);
EXPECT_EQ (LocalByte, (UINT8)0xA5);
//
// Assertion that generates a NULL pointer access exception before the
// following EXPECT_EQ result is evaluated
//
WriteByteWithNoParameterChecking (NULL, 0xA5);
EXPECT_EQ (LocalByte, (UINT8)0xA5);
}
int
main (
int argc,
char *argv[]
)
{
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}