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.
This commit is contained in:
ayeteadoe 2025-05-15 07:55:33 -07:00 committed by Andrew Kaster
commit 744fd91d0b
Notes: github-actions[bot] 2025-05-16 19:24:44 +00:00
13 changed files with 133 additions and 101 deletions

View file

@ -0,0 +1,48 @@
/*
* 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);
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2025, ayeteadoe <ayeteadoe@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Platform.h>
#include <LibTest/Export.h>
#include <setjmp.h>
#ifndef AK_OS_WINDOWS
# define LIBTEST_SETJMP(env) sigsetjmp(env, 1)
# define LIBTEST_LONGJMP siglongjmp
#else
# define LIBTEST_SETJMP(env) setjmp(env)
# define LIBTEST_LONGJMP longjmp
#endif
namespace Test {
jmp_buf& assertion_jump_buffer();
void set_assertion_jump_validity(bool);
bool assertion_jump_validity();
}

View file

@ -1,4 +1,5 @@
add_library(LibTestMain OBJECT TestMain.cpp)
add_library(LibTestMain OBJECT TestMain.cpp AssertionHandler.cpp)
target_link_libraries(LibTestMain PUBLIC GenericClangPlugin)
add_library(JavaScriptTestRunnerMain OBJECT JavaScriptTestRunnerMain.cpp)

View file

@ -7,11 +7,10 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/CheckedFormatString.h>
#include <AK/Math.h>
#include <AK/SourceLocation.h>
#include <LibTest/CrashTest.h>
#include <LibTest/AssertionHandler.h>
#include <LibTest/Export.h>
#include <LibTest/Randomized/RandomnessSource.h>
#include <LibTest/TestResult.h>
@ -185,29 +184,32 @@ consteval void expect_consteval(T) { }
#define EXPECT_CONSTEVAL(...) ::Test::expect_consteval(__VA_ARGS__)
// To use, specify the lambda to execute in a sub process and verify it exits:
// EXPECT_CRASH("This should fail", []{
// return Test::Crash::Failure::DidNotCrash;
// });
#define EXPECT_CRASH(test_message, test_func) \
do { \
Test::Crash crash(test_message, test_func); \
if (!crash.run()) \
::Test::set_current_test_result(::Test::TestResult::Failed); \
#define EXPECT_DEATH(message, expression) \
do { \
::Test::set_assertion_jump_validity(true); \
if (LIBTEST_SETJMP(::Test::assertion_jump_buffer()) == 0) { \
(expression); \
::Test::set_assertion_jump_validity(false); \
if (::Test::is_reporting_enabled()) \
::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_DEATH({}) did not crash", __FILE__, __LINE__, message); \
::Test::set_current_test_result(::Test::TestResult::Failed); \
} else { \
::Test::set_assertion_jump_validity(false); \
} \
} while (false)
#define EXPECT_CRASH_WITH_SIGNAL(test_message, signal, test_func) \
do { \
Test::Crash crash(test_message, test_func, (signal)); \
if (!crash.run()) \
::Test::set_current_test_result(::Test::TestResult::Failed); \
} while (false)
#define EXPECT_NO_CRASH(test_message, test_func) \
do { \
Test::Crash crash(test_message, test_func, 0); \
if (!crash.run()) \
::Test::set_current_test_result(::Test::TestResult::Failed); \
#define EXPECT_NO_DEATH(message, expression) \
do { \
::Test::set_assertion_jump_validity(true); \
if (LIBTEST_SETJMP(::Test::assertion_jump_buffer()) == 0) { \
(expression); \
::Test::set_assertion_jump_validity(false); \
} else { \
::Test::set_assertion_jump_validity(false); \
if (::Test::is_reporting_enabled()) \
::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_NO_DEATH({}) crashed", __FILE__, __LINE__, message); \
::Test::set_current_test_result(::Test::TestResult::Failed); \
} \
} while (false)
#define TRY_OR_FAIL(expression) \

View file

@ -110,6 +110,7 @@ private:
// Helper to hide implementation of TestSuite from users
TEST_API void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case);
TEST_API void set_suite_setup_function(Function<void()> setup);
}
#define TEST_SETUP \

View file

@ -12,8 +12,6 @@
#include <LibTest/TestResult.h>
#include <LibTest/TestSuite.h>
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
namespace Test {