1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* 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
*/
#include <AK/Assertions.h>
#include <AK/Platform.h>
#include <LibTest/CrashTest.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef AK_OS_MACOS
# include <sys/prctl.h>
#endif
namespace Test {
Crash::Crash(String test_type, Function<Crash::Failure()> crash_function, int crash_signal)
: m_type(move(test_type))
, m_crash_function(move(crash_function))
, m_crash_signal(crash_signal)
{
}
bool Crash::run(RunType run_type)
{
outln("\x1B[33mTesting\x1B[0m: \"{}\"", m_type);
if (run_type == RunType::UsingCurrentProcess) {
return do_report(m_crash_function());
} 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) {
#ifndef AK_OS_MACOS
if (prctl(PR_SET_DUMPABLE, 0, 0) < 0)
perror("prctl(PR_SET_DUMPABLE)");
#endif
exit((int)m_crash_function());
}
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
return do_report(Failure(WEXITSTATUS(status)));
}
if (WIFSIGNALED(status)) {
int signal = WTERMSIG(status);
VERIFY(signal > 0);
return do_report(signal);
}
VERIFY_NOT_REACHED();
}
}
bool Crash::do_report(Report report)
{
bool pass = false;
if (m_crash_signal == ANY_SIGNAL) {
pass = report.has<int>();
} else if (m_crash_signal == 0) {
pass = report.has<Failure>() && report.get<Failure>() == Failure::DidNotCrash;
} else if (m_crash_signal > 0) {
pass = report.has<int>() && report.get<int>() == m_crash_signal;
} else {
VERIFY_NOT_REACHED();
}
if (pass)
out("\x1B[32mPASS\x1B[0m: ");
else
out("\x1B[31mFAIL\x1B[0m: ");
report.visit(
[&](const Failure& failure) {
switch (failure) {
case Failure::DidNotCrash:
out("Did not crash");
break;
case Failure::UnexpectedError:
out("Unexpected error");
break;
default:
VERIFY_NOT_REACHED();
}
},
[&](const int& signal) {
out("Terminated with signal {}", signal);
});
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();
return pass;
}
}
|