1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

AK: Expose ak_assertion_handler weak symbol for custom death handling

When a `VERIFY()` assertion fails `ak_verification_failed` is invoked
which means the program will invoke `__builtin_trap` and immediately
quit. But with this change we have now allowed for an optional hook
into `ak_*_failed` that lets us perform some custom
action before program exits. This foundation is what we will
(ab)use to implement death tests without the process cloning
CrashTest infrastructure.
This commit is contained in:
ayeteadoe 2025-05-15 07:54:52 -07:00 committed by Andrew Kaster
parent d35486cb74
commit dc707e6ed8
Notes: github-actions[bot] 2025-05-16 19:24:50 +00:00
2 changed files with 38 additions and 0 deletions

View file

@ -9,6 +9,10 @@
#include <AK/Format.h>
#include <AK/Platform.h>
#ifdef AK_OS_WINDOWS
# include <Windows.h>
#endif
#if defined(AK_OS_ANDROID) && (__ANDROID_API__ >= 33)
# include <android/log.h>
# define EXECINFO_BACKTRACE
@ -114,8 +118,37 @@ void ak_trap(void)
__builtin_trap();
}
#ifndef AK_OS_WINDOWS
[[gnu::weak]] void ak_assertion_handler(char const* message);
#endif
using AssertionHandlerFunc = void (*)(char const*);
static AssertionHandlerFunc get_custom_assertion_handler()
{
#ifndef AK_OS_WINDOWS
return ak_assertion_handler;
#else
// Windows doesn't support weak symbols as nicely as ELF platforms.
// Instead, rely on the fact that we only want this to be overridden from
// the main executable, and grab it from there if present.
if (HMODULE module = GetModuleHandle(nullptr)) {
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
auto handler = reinterpret_cast<AssertionHandlerFunc>(GetProcAddress(module, "ak_assertion_handler"));
# pragma clang diagnostic pop
FreeLibrary(module);
return handler;
}
return nullptr;
#endif
}
void ak_verification_failed(char const* message)
{
if (auto assertion_handler = get_custom_assertion_handler()) {
assertion_handler(message);
}
if (ak_colorize_output())
ERRORLN("\033[31;1mVERIFICATION FAILED\033[0m: {}", message);
else
@ -126,6 +159,9 @@ void ak_verification_failed(char const* message)
void ak_assertion_failed(char const* message)
{
if (auto assertion_handler = get_custom_assertion_handler()) {
assertion_handler(message);
}
if (ak_colorize_output())
ERRORLN("\033[31;1mASSERTION FAILED\033[0m: {}", message);
else

View file

@ -73,4 +73,6 @@ if (WIN32)
# FIXME: Windows on ARM
target_link_libraries(AK PRIVATE clang_rt.builtins-x86_64.lib)
target_link_libraries(AK PRIVATE Bcrypt.lib)
elseif (APPLE)
target_link_options(AK PRIVATE LINKER:-U,_ak_assertion_handler)
endif()