1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00
ladybird/Libraries/LibTest/AssertionHandler.cpp
ayeteadoe 744fd91d0b LibTest: Support death tests without child process cloning
A challenge for getting LibTest working on Windows has always
been CrashTest. It implements death tests similar to Google Test
where a child process is cloned to invoke the expression that
should abort/terminate the program. Then the exit code of the
child is used by the parent test process to verify if the
application correctly aborted/terminated due to invoking
the expression.

The problem was that finding an equivalent way to port Crash::run()
to Windows was not looking very likely as publicly exposed Win32/
Native APIs have no equivalent to fork(); however, Windows actually
does have native support for process cloning via undocumented NT
APIs that clever people reverse engineered and published, see
`NtCreateUserProcess()`.

All that being said, this `EXPECT_DEATH()` implementation avoids
needing to use a child process in general, allowing us to remove
CrashTest in favour of a single cross-platform solution for death
tests.
2025-05-16 13:23:32 -06:00

48 lines
1.1 KiB
C++

/*
* Copyright (c) 2025, ayeteadoe <ayeteadoe@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/Macros.h>
namespace Test {
static jmp_buf g_assert_jmp_buf = {};
static bool g_assert_jmp_buf_valid = false;
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")))
#endif
extern "C" EXPORT void ak_assertion_handler(char const* message);
void ak_assertion_handler(char const* message)
{
::Test::assertion_handler_impl(message);
}