2021-05-07 10:43:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-07-15 10:33:22 +00:00
|
|
|
* Copyright (c) 2019-2020, Shannon Booth <shannon@serenityos.org>
|
2021-05-07 10:43:41 +00:00
|
|
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-12-17 17:12:47 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-07-01 03:34:12 +00:00
|
|
|
#include <AK/Platform.h>
|
2021-05-07 10:43:41 +00:00
|
|
|
#include <LibTest/CrashTest.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-11-20 03:23:14 +00:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_EMSCRIPTEN)
|
2021-07-01 03:34:12 +00:00
|
|
|
# include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2021-05-07 10:43:41 +00:00
|
|
|
namespace Test {
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Crash::Crash(DeprecatedString test_type, Function<Crash::Failure()> crash_function, int crash_signal)
|
2021-05-07 11:38:56 +00:00
|
|
|
: m_type(move(test_type))
|
2021-05-07 10:43:41 +00:00
|
|
|
, m_crash_function(move(crash_function))
|
2021-12-17 17:12:47 +00:00
|
|
|
, m_crash_signal(crash_signal)
|
2021-05-07 10:43:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-07 11:38:56 +00:00
|
|
|
bool Crash::run(RunType run_type)
|
2021-05-07 10:43:41 +00:00
|
|
|
{
|
2021-05-07 20:40:18 +00:00
|
|
|
outln("\x1B[33mTesting\x1B[0m: \"{}\"", m_type);
|
2021-05-07 10:43:41 +00:00
|
|
|
|
|
|
|
if (run_type == RunType::UsingCurrentProcess) {
|
2021-12-15 07:58:40 +00:00
|
|
|
return do_report(m_crash_function());
|
2021-05-07 10:43:41 +00:00
|
|
|
} else {
|
|
|
|
// Run the test in a child process so that we do not crash the crash program :^)
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("fork");
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
} else if (pid == 0) {
|
2022-11-20 03:23:14 +00:00
|
|
|
#if !defined(AK_OS_MACOS) && !defined(AK_OS_EMSCRIPTEN)
|
2021-07-01 03:34:12 +00:00
|
|
|
if (prctl(PR_SET_DUMPABLE, 0, 0) < 0)
|
|
|
|
perror("prctl(PR_SET_DUMPABLE)");
|
|
|
|
#endif
|
2021-12-15 15:03:26 +00:00
|
|
|
exit((int)m_crash_function());
|
2021-05-07 10:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
2021-12-15 15:03:26 +00:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
return do_report(Failure(WEXITSTATUS(status)));
|
|
|
|
}
|
2021-05-07 11:38:56 +00:00
|
|
|
if (WIFSIGNALED(status)) {
|
2021-12-17 17:12:47 +00:00
|
|
|
int signal = WTERMSIG(status);
|
|
|
|
VERIFY(signal > 0);
|
|
|
|
return do_report(signal);
|
2021-05-07 11:38:56 +00:00
|
|
|
}
|
2021-12-15 15:03:26 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-07 10:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 08:20:38 +00:00
|
|
|
bool Crash::do_report(Report report)
|
2021-12-15 07:58:40 +00:00
|
|
|
{
|
2021-12-17 17:12:47 +00:00
|
|
|
bool pass = false;
|
|
|
|
if (m_crash_signal == ANY_SIGNAL) {
|
|
|
|
pass = report.has<int>();
|
2021-12-14 23:47:43 +00:00
|
|
|
} else if (m_crash_signal == 0) {
|
|
|
|
pass = report.has<Failure>() && report.get<Failure>() == Failure::DidNotCrash;
|
2021-12-17 17:12:47 +00:00
|
|
|
} else if (m_crash_signal > 0) {
|
|
|
|
pass = report.has<int>() && report.get<int>() == m_crash_signal;
|
|
|
|
} else {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2021-12-15 08:20:38 +00:00
|
|
|
|
|
|
|
if (pass)
|
|
|
|
out("\x1B[32mPASS\x1B[0m: ");
|
|
|
|
else
|
|
|
|
out("\x1B[31mFAIL\x1B[0m: ");
|
|
|
|
|
|
|
|
report.visit(
|
2022-04-01 17:58:27 +00:00
|
|
|
[&](Failure const& failure) {
|
2021-12-15 08:20:38 +00:00
|
|
|
switch (failure) {
|
|
|
|
case Failure::DidNotCrash:
|
2021-12-17 17:12:47 +00:00
|
|
|
out("Did not crash");
|
2021-12-15 08:20:38 +00:00
|
|
|
break;
|
|
|
|
case Failure::UnexpectedError:
|
2021-12-17 17:12:47 +00:00
|
|
|
out("Unexpected error");
|
2021-12-15 08:20:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
},
|
2022-04-01 17:58:27 +00:00
|
|
|
[&](int const& signal) {
|
2021-12-17 17:12:47 +00:00
|
|
|
out("Terminated with signal {}", signal);
|
2021-12-15 08:20:38 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 17:12:47 +00:00
|
|
|
if (!pass) {
|
|
|
|
if (m_crash_signal == ANY_SIGNAL) {
|
|
|
|
out(" while expecting any signal");
|
|
|
|
} else if (m_crash_signal > 0) {
|
|
|
|
out(" while expecting signal {}", m_crash_signal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outln();
|
|
|
|
|
2021-12-15 08:20:38 +00:00
|
|
|
return pass;
|
2021-12-15 07:58:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 10:43:41 +00:00
|
|
|
}
|