forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntimeCrashTest.cpp
157 lines (139 loc) · 6.17 KB
/
RuntimeCrashTest.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//===-- flang/unittests/RuntimeGTest/CrashHandlerFixture.cpp ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// Selected APIs are tested here to support development of unit tests for other
/// runtime components and ensure the test fixture handles crashes as we expect.
//
//===----------------------------------------------------------------------===//
#include "CrashHandlerFixture.h"
#include "../../runtime/terminator.h"
#include "flang/Runtime/io-api.h"
#include <gtest/gtest.h>
using namespace Fortran::runtime;
using namespace Fortran::runtime::io;
//------------------------------------------------------------------------------
/// Test crashes through direct calls to terminator methods
//------------------------------------------------------------------------------
struct TestTerminator : CrashHandlerFixture {};
#define TEST_CRASH_HANDLER_MESSAGE \
"Intentionally crashing runtime for unit test"
TEST(TestTerminator, CrashTest) {
static Fortran::runtime::Terminator t;
ASSERT_DEATH(t.Crash(TEST_CRASH_HANDLER_MESSAGE), TEST_CRASH_HANDLER_MESSAGE);
}
#undef TEST_CRASH_HANDLER_MESSAGE
TEST(TestTerminator, CheckFailedLocationTest) {
static Fortran::runtime::Terminator t;
ASSERT_DEATH(t.CheckFailed("predicate", "someFileName", 789),
"RUNTIME_CHECK\\(predicate\\) failed at someFileName\\(789\\)");
}
TEST(TestTerminator, CheckFailedTest) {
static Fortran::runtime::Terminator t;
ASSERT_DEATH(t.CheckFailed("predicate"),
"RUNTIME_CHECK\\(predicate\\) failed at \\(null\\)\\(0\\)");
}
//------------------------------------------------------------------------------
/// Test misuse of io api
//------------------------------------------------------------------------------
struct TestIOCrash : CrashHandlerFixture {};
TEST(TestIOCrash, FormatDescriptorWriteMismatchTest) {
static constexpr int bufferSize{4};
static char buffer[bufferSize];
static const char *format{"(A4)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
ASSERT_DEATH(IONAME(OutputLogical)(cookie, true),
"Data edit descriptor 'A' may not be used with a LOGICAL data item");
}
TEST(TestIOCrash, InvalidFormatCharacterTest) {
static constexpr int bufferSize{1};
static char buffer[bufferSize];
static const char *format{"(C1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
ASSERT_DEATH(IONAME(OutputInteger64)(cookie, 0xfeedface),
"Unknown 'C' edit descriptor in FORMAT");
}
//------------------------------------------------------------------------------
/// Test buffer overwrites with Output* functions
/// Each test performs the tested IO operation correctly first, before causing
/// an overwrite to demonstrate that the failure is caused by the overwrite and
/// not a misuse of the API.
//------------------------------------------------------------------------------
TEST(TestIOCrash, OverwriteBufferAsciiTest) {
static constexpr int bufferSize{4};
static char buffer[bufferSize];
static const char *format{"(A4)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputAscii)(cookie, "four", bufferSize);
ASSERT_DEATH(IONAME(OutputAscii)(cookie, "Too many characters!", 20),
"Internal write overran available records");
}
TEST(TestIOCrash, OverwriteBufferCharacterTest) {
static constexpr int bufferSize{1};
static char buffer[bufferSize];
static const char *format{"(A1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputCharacter)(cookie, "a", 1);
ASSERT_DEATH(IONAME(OutputCharacter)(cookie, "a", 1),
"Internal write overran available records");
}
TEST(TestIOCrash, OverwriteBufferLogicalTest) {
static constexpr int bufferSize{1};
static char buffer[bufferSize];
static const char *format{"(L1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputLogical)(cookie, true);
ASSERT_DEATH(IONAME(OutputLogical)(cookie, true),
"Internal write overran available records");
}
TEST(TestIOCrash, OverwriteBufferRealTest) {
static constexpr int bufferSize{1};
static char buffer[bufferSize];
static const char *format{"(F1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputReal32)(cookie, 1.);
EXPECT_DEATH(IONAME(OutputReal32)(cookie, 1.),
"Internal write overran available records");
std::memset(buffer, '\0', bufferSize);
cookie = IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format));
IONAME(OutputReal64)(cookie, 1.);
EXPECT_DEATH(IONAME(OutputReal64)(cookie, 1.),
"Internal write overran available records");
}
TEST(TestIOCrash, OverwriteBufferComplexTest) {
static constexpr int bufferSize{8};
static char buffer[bufferSize];
static const char *format{"(Z1,Z1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputComplex32)(cookie, 1., 1.);
EXPECT_DEATH(IONAME(OutputComplex32)(cookie, 1., 1.),
"Internal write overran available records");
std::memset(buffer, '\0', bufferSize);
cookie = IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format));
IONAME(OutputComplex64)(cookie, 1., 1.);
EXPECT_DEATH(IONAME(OutputComplex64)(cookie, 1., 1.),
"Internal write overran available records");
}
TEST(TestIOCrash, OverwriteBufferIntegerTest) {
static constexpr int bufferSize{1};
static char buffer[bufferSize];
static const char *format{"(I1)"};
auto *cookie{IONAME(BeginInternalFormattedOutput)(
buffer, bufferSize, format, std::strlen(format))};
IONAME(OutputInteger64)(cookie, 0xdeadbeef);
ASSERT_DEATH(IONAME(OutputInteger64)(cookie, 0xdeadbeef),
"Internal write overran available records");
}