2021-05-07 10:43:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2019-2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
|
|
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2021-07-01 03:34:12 +00:00
|
|
|
#ifndef AK_OS_MACOS
|
|
|
|
# include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2021-05-07 10:43:41 +00:00
|
|
|
namespace Test {
|
|
|
|
|
|
|
|
Crash::Crash(String test_type, Function<Crash::Failure()> crash_function)
|
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-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) {
|
2021-07-01 03:34:12 +00:00
|
|
|
#ifndef AK_OS_MACOS
|
|
|
|
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-05-07 20:40:18 +00:00
|
|
|
outln("\x1B[32mPASS\x1B[0m: Terminated with signal {}", WTERMSIG(status));
|
2021-05-07 11:38:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-15 15:03:26 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-05-07 10:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:58:40 +00:00
|
|
|
bool Crash::do_report(Failure failure)
|
|
|
|
{
|
|
|
|
// If we got here something went wrong
|
|
|
|
out("\x1B[31mFAIL\x1B[0m: ");
|
|
|
|
switch (failure) {
|
|
|
|
case Failure::DidNotCrash:
|
|
|
|
outln("Did not crash!");
|
|
|
|
break;
|
|
|
|
case Failure::UnexpectedError:
|
|
|
|
outln("Unexpected error!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-07 10:43:41 +00:00
|
|
|
}
|