diff --git a/AK/Assertions.cpp b/AK/Assertions.cpp index dfc8ab6ec37..36a5fdc69e6 100644 --- a/AK/Assertions.cpp +++ b/AK/Assertions.cpp @@ -9,6 +9,10 @@ #include #include +#ifdef AK_OS_WINDOWS +# include +#endif + #if defined(AK_OS_ANDROID) && (__ANDROID_API__ >= 33) # include # 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(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 diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index e3c960f8d97..6fad9546850 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -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()