Skip to content

Commit 2cb8bf6

Browse files
mdkinneymergify[bot]
authored andcommitted
UnitTestFrameworkPkg: Fix false positives from address sanitizer
PR tianocore#6408 introduced a host specific NORETURN function to resolve false positives from the address sanitizer when LongJump() is called and the stack is reset to a previous stack frame. This approach was discussed here: https://github.com/tianocore/edk2/pull/6408/files#r1918810499 False positives are still being observed with this initial solution. The address sanitizer provides __asan_handle_no_return() to clean up shadow memory before a NORETURN function is called and provides a simpler implementation for this issue without having to introduce a host specific NORETURN function. https://github.com/llvm/llvm-project/blob/main/compiler-rt/include/sanitizer/asan_interface.h Signed-off-by: Michael D Kinney <[email protected]>
1 parent 3600675 commit 2cb8bf6

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

UnitTestFrameworkPkg/Library/UnitTestDebugAssertLib/UnitTestDebugAssertLibHost.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,23 @@ extern "C" {
1818
#include <Library/BaseLib.h>
1919
#include <Library/UnitTestLib.h>
2020

21+
//
22+
// If address sanitizer is enabled, then declare the function that is used to
23+
// handle custom long jump implementation.
24+
//
25+
#ifdef __SANITIZE_ADDRESS__
26+
void
27+
__asan_handle_no_return (
28+
);
29+
30+
#endif
31+
2132
///
2233
/// Point to jump buffer used with SetJump()/LongJump() to test if a function
2334
/// under test generates an expected ASSERT() condition.
2435
///
2536
BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer = NULL;
2637

27-
/**
28-
LongJump wrapper for host-based unit test environments that is declared
29-
NORETURN to avoid false positives from address sanitizer.
30-
31-
@param JumpBuffer A pointer to CPU context buffer.
32-
@param Value The value to return when the SetJump() context is
33-
restored and must be non-zero.
34-
**/
35-
static
36-
VOID
37-
NORETURN
38-
EFIAPI
39-
HostLongJump (
40-
IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer,
41-
IN UINTN Value
42-
)
43-
{
44-
LongJump (JumpBuffer, Value);
45-
UNREACHABLE ();
46-
}
47-
4838
/**
4939
Unit test library replacement for DebugAssert() in DebugLib.
5040
@@ -68,8 +58,18 @@ extern "C" {
6858

6959
if (gUnitTestExpectAssertFailureJumpBuffer != NULL) {
7060
UT_LOG_INFO ("Detected expected ASSERT: %a(%d): %a\n", FileName, LineNumber, Description);
71-
HostLongJump (gUnitTestExpectAssertFailureJumpBuffer, 1);
72-
UNREACHABLE ();
61+
62+
//
63+
// If address sanitizer is enabled, then inform sanitizer that a no return
64+
// function is being called that will reset to a previous stack frame.
65+
// This is required to avoid false positives from the address sanitizer
66+
// due to the use of a custom long jump implementation.
67+
//
68+
#ifdef __SANITIZE_ADDRESS__
69+
__asan_handle_no_return ();
70+
#endif
71+
72+
LongJump (gUnitTestExpectAssertFailureJumpBuffer, 1);
7373
} else {
7474
if (GetActiveFrameworkHandle () != NULL) {
7575
AsciiStrCpyS (Message, sizeof (Message), "Detected unexpected ASSERT(");

0 commit comments

Comments
 (0)