ladybird/Libraries/LibTest/AssertionHandler.cpp
Johan Dahlin 29355a0e33 LibTest: Prevent -dead_strip from removing assertion handler
The ak_assertion_handler symbol is referenced only through a weak
symbol in AK/Assertions.cpp. With -dead_strip on macOS, the linker
strips the strong definition, breaking EXPECT_DEATH tests which rely
on the handler to longjmp back instead of crashing.

Add retain and used attributes to prevent the linker from removing it.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 18:36:27 +01:00

48 lines
1.2 KiB
C++

/*
* Copyright (c) 2025, ayeteadoe <ayeteadoe@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/Macros.h>
namespace Test {
static libtest_jmp_buf g_assert_jmp_buf = {};
static bool g_assert_jmp_buf_valid = false;
libtest_jmp_buf& assertion_jump_buffer() { return g_assert_jmp_buf; }
void set_assertion_jump_validity(bool validity)
{
g_assert_jmp_buf_valid = validity;
}
static bool is_assertion_jump_valid()
{
return g_assert_jmp_buf_valid;
}
static void assertion_handler_impl(char const*)
{
if (is_assertion_jump_valid()) {
set_assertion_jump_validity(false);
LIBTEST_LONGJMP(assertion_jump_buffer(), 1); /* NOLINT(cert-err52-cpp, bugprone-setjmp-longjmp) Isolated to test infrastructure and allows us to not depend on spawning child processes for death tests */
}
// Fall through to default assertion handler
}
}
#if defined(AK_OS_WINDOWS)
# define EXPORT __declspec(dllexport)
#else
# define EXPORT __attribute__((visibility("default"), retain, used))
#endif
extern "C" EXPORT void ak_assertion_handler(char const* message);
void ak_assertion_handler(char const* message)
{
::Test::assertion_handler_impl(message);
}