mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00
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:
parent
dc707e6ed8
commit
744fd91d0b
Notes:
github-actions[bot]
2025-05-16 19:24:44 +00:00
Author: https://github.com/ayeteadoe
Commit: 744fd91d0b
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4698
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/AtkinsSJ
Reviewed-by: https://github.com/R-Goc
13 changed files with 133 additions and 101 deletions
48
Libraries/LibTest/AssertionHandler.cpp
Normal file
48
Libraries/LibTest/AssertionHandler.cpp
Normal 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);
|
||||
}
|
27
Libraries/LibTest/AssertionHandler.h
Normal file
27
Libraries/LibTest/AssertionHandler.h
Normal 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();
|
||||
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -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 \
|
||||
|
|
|
@ -12,8 +12,6 @@
|
|||
#include <LibTest/TestResult.h>
|
||||
#include <LibTest/TestSuite.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace Test {
|
||||
|
||||
|
|
|
@ -151,44 +151,22 @@ TEST_CASE(parse_ascii_base36_digit)
|
|||
EXPECT_EQ(parse_ascii_base36_digit('Z'), 35u);
|
||||
EXPECT_EQ(parse_ascii_base36_digit('a'), 10u);
|
||||
EXPECT_EQ(parse_ascii_base36_digit('z'), 35u);
|
||||
EXPECT_CRASH("parsing Base36 digit before valid numeric range", [] {
|
||||
parse_ascii_base36_digit('/');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing Base36 digit after valid numeric range", [] {
|
||||
parse_ascii_base36_digit(':');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing Base36 digit before valid uppercase range", [] {
|
||||
parse_ascii_base36_digit('@');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing Base36 digit after valid uppercase range", [] {
|
||||
parse_ascii_base36_digit('[');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing Base36 digit before valid lowercase range", [] {
|
||||
parse_ascii_base36_digit('`');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing Base36 digit after valid lowercase range", [] {
|
||||
parse_ascii_base36_digit('{');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_DEATH("parsing Base36 digit before valid numeric range", parse_ascii_base36_digit('/'));
|
||||
EXPECT_DEATH("parsing Base36 digit after valid numeric range", parse_ascii_base36_digit(':'));
|
||||
EXPECT_DEATH("parsing Base36 digit before valid uppercase range", parse_ascii_base36_digit('@'));
|
||||
EXPECT_DEATH("parsing Base36 digit after valid uppercase range", parse_ascii_base36_digit('['));
|
||||
EXPECT_DEATH("parsing Base36 digit before valid lowercase range", parse_ascii_base36_digit('`'));
|
||||
EXPECT_DEATH("parsing Base36 digit after valid lowercase range", parse_ascii_base36_digit('{'));
|
||||
}
|
||||
|
||||
TEST_CASE(parse_ascii_digit)
|
||||
{
|
||||
EXPECT_EQ(parse_ascii_digit('0'), 0u);
|
||||
EXPECT_EQ(parse_ascii_digit('9'), 9u);
|
||||
EXPECT_CRASH("parsing invalid ASCII digit", [] {
|
||||
parse_ascii_digit('a');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_CRASH("parsing invalid unicode digit", [] {
|
||||
parse_ascii_digit(0x00A9);
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_DEATH("parsing invalid ASCII digit", parse_ascii_digit('a'));
|
||||
EXPECT_DEATH("parsing invalid unicode digit", parse_ascii_digit(0x00A9));
|
||||
}
|
||||
|
||||
TEST_CASE(parse_ascii_hex_digit)
|
||||
|
@ -196,10 +174,8 @@ TEST_CASE(parse_ascii_hex_digit)
|
|||
EXPECT_EQ(parse_ascii_hex_digit('0'), 0u);
|
||||
EXPECT_EQ(parse_ascii_hex_digit('F'), 15u);
|
||||
EXPECT_EQ(parse_ascii_hex_digit('f'), 15u);
|
||||
EXPECT_CRASH("parsing invalid ASCII hex digit", [] {
|
||||
parse_ascii_hex_digit('g');
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_DEATH("parsing invalid ASCII hex digit", parse_ascii_hex_digit('g'));
|
||||
}
|
||||
|
||||
BENCHMARK_CASE(is_ascii)
|
||||
|
|
|
@ -59,17 +59,16 @@ TEST_CASE(move)
|
|||
TEST_CASE(no_allocation)
|
||||
{
|
||||
FixedArray<int> array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
|
||||
EXPECT_NO_CRASH("Assignments", [&] {
|
||||
EXPECT_NO_DEATH("Assignments", [&]() {
|
||||
NoAllocationGuard guard;
|
||||
array[0] = 0;
|
||||
array[1] = 1;
|
||||
array[2] = 2;
|
||||
array[4] = array[1];
|
||||
array[3] = array[0] + array[2];
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
}());
|
||||
|
||||
EXPECT_NO_CRASH("Move", [&] {
|
||||
EXPECT_NO_DEATH("Move", [&]() {
|
||||
FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
|
||||
// We need an Optional here to ensure that the NoAllocationGuard is
|
||||
// destroyed before the moved_to_array, because that would call free
|
||||
|
@ -79,16 +78,13 @@ TEST_CASE(no_allocation)
|
|||
NoAllocationGuard guard;
|
||||
moved_to_array.emplace(move(moved_from_array));
|
||||
}
|
||||
}());
|
||||
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_NO_CRASH("Swap", [&] {
|
||||
EXPECT_NO_DEATH("Swap", [&]() {
|
||||
FixedArray<int> target_for_swapping;
|
||||
{
|
||||
NoAllocationGuard guard;
|
||||
array.swap(target_for_swapping);
|
||||
}
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
}());
|
||||
}
|
||||
|
|
|
@ -206,10 +206,7 @@ TEST_CASE(from_code_points)
|
|||
auto string = String::from_code_point(0x10ffff);
|
||||
EXPECT_EQ(string, "\xF4\x8F\xBF\xBF"sv);
|
||||
|
||||
EXPECT_CRASH("Creating a string from an invalid code point", [] {
|
||||
String::from_code_point(0xffffffff);
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_DEATH("Creating a string from an invalid code point", (void)String::from_code_point(0xffffffff));
|
||||
}
|
||||
|
||||
TEST_CASE(substring)
|
||||
|
@ -1147,10 +1144,7 @@ TEST_CASE(repeated)
|
|||
EXPECT_EQ(string3, "𐌀𐌀𐌀𐌀𐌀𐌀𐌀𐌀𐌀𐌀"sv);
|
||||
}
|
||||
|
||||
EXPECT_CRASH("Creating a string from an invalid code point", [] {
|
||||
(void)String::repeated(0xffffffff, 1);
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_DEATH("Creating a string from an invalid code point", (void)String::repeated(0xffffffff, 1));
|
||||
}
|
||||
|
||||
TEST_CASE(join)
|
||||
|
|
|
@ -178,15 +178,10 @@ TEST_CASE(iterate_utf16)
|
|||
EXPECT(iterator.length_in_code_units() == 2);
|
||||
|
||||
EXPECT(++iterator == view.end());
|
||||
EXPECT_CRASH("Dereferencing Utf16CodePointIterator which is at its end.", [&iterator] {
|
||||
*iterator;
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_CRASH("Incrementing Utf16CodePointIterator which is at its end.", [&iterator] {
|
||||
++iterator;
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_DEATH("Dereferencing Utf16CodePointIterator which is at its end.", *iterator);
|
||||
|
||||
EXPECT_DEATH("Incrementing Utf16CodePointIterator which is at its end.", ++iterator);
|
||||
}
|
||||
|
||||
TEST_CASE(validate_invalid_utf16)
|
||||
|
|
|
@ -170,10 +170,8 @@ TEST_CASE(iterate_utf8)
|
|||
|
||||
EXPECT(iterator.done());
|
||||
EXPECT(!iterator.peek(0).has_value());
|
||||
EXPECT_CRASH("Dereferencing Utf8CodePointIterator which is already done.", [&iterator] {
|
||||
*iterator;
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
|
||||
EXPECT_DEATH("Dereferencing Utf8CodePointIterator which is already done.", *iterator);
|
||||
}
|
||||
|
||||
TEST_CASE(decode_invalid_ut8)
|
||||
|
|
|
@ -8,7 +8,5 @@
|
|||
|
||||
TEST_CASE(raise)
|
||||
{
|
||||
EXPECT_NO_CRASH("This should never crash", [] {
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
EXPECT_NO_DEATH("This should never crash", [] { }());
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
TEST_CASE(char_data_ending)
|
||||
{
|
||||
EXPECT_NO_CRASH("parsing character data ending by itself should not crash", [] {
|
||||
EXPECT_NO_DEATH("parsing character data ending by itself should not crash", [] {
|
||||
// After seeing `<C>`, the parser will start parsing the content of the element. The content parser will then parse any character data it sees.
|
||||
// The character parser would see the first two `]]` and consume them. Then, it would see the `>` and set the state machine to say we have seen this,
|
||||
// but it did _not_ consume it and would instead tell GenericLexer that it should stop consuming characters. Therefore, we only consumed 2 characters.
|
||||
|
@ -17,17 +17,15 @@ TEST_CASE(char_data_ending)
|
|||
// input when we only have 2 characters, causing an assertion failure as we are asking to take off more characters than there really is.
|
||||
XML::Parser parser("<C>]]>"sv);
|
||||
(void)parser.parse();
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
}());
|
||||
}
|
||||
|
||||
TEST_CASE(character_reference_integer_overflow)
|
||||
{
|
||||
EXPECT_NO_CRASH("parsing character references that do not fit in 32 bits should not crash", [] {
|
||||
EXPECT_NO_DEATH("parsing character references that do not fit in 32 bits should not crash", [] {
|
||||
XML::Parser parser("<G>�"sv);
|
||||
(void)parser.parse();
|
||||
return Test::Crash::Failure::DidNotCrash;
|
||||
});
|
||||
}());
|
||||
}
|
||||
|
||||
TEST_CASE(predefined_character_reference)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue