-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathLinearLifetimeCheckerPrivate.h
217 lines (178 loc) · 6.36 KB
/
LinearLifetimeCheckerPrivate.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===--- LinearLifetimeCheckerPrivate.h -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SIL_LINEARLIFETIMECHECKER_PRIVATE_H
#define SWIFT_SIL_LINEARLIFETIMECHECKER_PRIVATE_H
#include "swift/SIL/LinearLifetimeChecker.h"
#include "llvm/Support/ErrorHandling.h"
namespace swift {
struct LLVM_LIBRARY_VISIBILITY LinearLifetimeChecker::ErrorBehaviorKind {
enum inner_t {
Invalid = 0,
ReturnFalse = 1,
PrintMessage = 2,
Assert = 4,
ReturnFalseOnLeak = 8,
StoreNonConsumingUsesOutsideLifetime = 16,
PrintMessageAndReturnFalse = PrintMessage | ReturnFalse,
PrintMessageAndAssert = PrintMessage | Assert,
ReturnFalseOnLeakAssertOtherwise = ReturnFalseOnLeak | Assert,
} Value;
ErrorBehaviorKind() : Value(Invalid) {}
ErrorBehaviorKind(inner_t Inner) : Value(Inner) { assert(Value != Invalid); }
ErrorBehaviorKind(unsigned Inner) : Value(inner_t(Inner)) {
assert(Value != Invalid);
}
bool shouldStoreNonConsumingUsesOutsideLifetime() const {
assert(Value != Invalid);
return Value & StoreNonConsumingUsesOutsideLifetime;
}
bool shouldAssert() const {
assert(Value != Invalid);
return Value & Assert;
}
bool shouldReturnFalseOnLeak() const {
assert(Value != Invalid);
return Value & ReturnFalseOnLeak;
}
bool shouldPrintMessage() const {
assert(Value != Invalid);
return Value & PrintMessage;
}
bool shouldReturnFalse() const {
assert(Value != Invalid);
return Value & ReturnFalse;
}
};
class LLVM_LIBRARY_VISIBILITY LinearLifetimeChecker::Error {
friend class ErrorBuilder;
bool foundUseAfterFree = false;
bool foundLeak = false;
bool foundOverConsume = false;
bool foundUseOutsideOfLifetime = false;
public:
Error() {}
bool getFoundError() const {
return foundUseAfterFree || foundLeak || foundOverConsume ||
foundUseOutsideOfLifetime;
}
bool getFoundLeak() const { return foundLeak; }
bool getFoundUseAfterFree() const { return foundUseAfterFree; }
bool getFoundOverConsume() const { return foundOverConsume; }
bool getFoundUseOutsideOfLifetime() const {
return foundUseOutsideOfLifetime;
}
};
class LLVM_LIBRARY_VISIBILITY LinearLifetimeChecker::ErrorBuilder {
StringRef functionName;
ErrorBehaviorKind behavior;
std::optional<Error> error;
unsigned *errorMessageCounter;
public:
ErrorBuilder(const SILFunction &fn,
LinearLifetimeChecker::ErrorBehaviorKind behavior,
unsigned *errorMessageCounter = nullptr)
: functionName(fn.getName()), behavior(behavior), error(Error()),
errorMessageCounter(errorMessageCounter) {}
ErrorBuilder(const SILFunction &fn,
LinearLifetimeChecker::ErrorBehaviorKind::inner_t behavior,
unsigned *errorMessageCounter = nullptr)
: ErrorBuilder(fn, LinearLifetimeChecker::ErrorBehaviorKind(behavior),
errorMessageCounter) {}
ErrorBuilder(const ErrorBuilder &other)
: functionName(other.functionName), behavior(other.behavior),
error(other.error), errorMessageCounter(other.errorMessageCounter) {}
ErrorBuilder &operator=(const ErrorBuilder &other) {
functionName = other.functionName;
behavior = other.behavior;
error = other.error;
errorMessageCounter = other.errorMessageCounter;
return *this;
}
Error consumeAndGetFinalError() && {
auto result = *error;
error = std::nullopt;
errorMessageCounter = nullptr;
return result;
}
void tryDumpErrorCounter() const {
if (!errorMessageCounter) {
return;
}
llvm::errs() << "Error#: " << *errorMessageCounter << ". ";
}
void tryIncrementErrorCounter() {
if (!errorMessageCounter) {
return;
}
++(*errorMessageCounter);
}
bool handleLeak(llvm::function_ref<void()> &&messagePrinterFunc) {
error->foundLeak = true;
if (behavior.shouldPrintMessage()) {
tryDumpErrorCounter();
llvm::errs() << "Begin Error in Function: '" << functionName << "'\n";
messagePrinterFunc();
tryDumpErrorCounter();
llvm::errs() << "End Error in Function: '" << functionName << "'\n";
tryIncrementErrorCounter();
}
if (behavior.shouldReturnFalseOnLeak()) {
return false;
}
// We already printed out our error if we needed to, so don't pass it along.
return handleError([]() {}, true);
}
bool handleOverConsume(llvm::function_ref<void()> &&messagePrinterFunc) {
error->foundOverConsume = true;
return handleError(std::move(messagePrinterFunc));
}
bool handleUseAfterFree(llvm::function_ref<void()> &&messagePrinterFunc) {
error->foundUseAfterFree = true;
return handleError(std::move(messagePrinterFunc));
}
bool
handleMalformedSIL(llvm::function_ref<void()> &&messagePrinterFunc) const {
return handleError(std::move(messagePrinterFunc));
}
/// Handle a case where we either found a use-after-free due to a
/// non-consuming use after our lifetime has ended /or/ if we found a use
/// before def of a non consuming value.
void
handleUseOutsideOfLifetime(llvm::function_ref<void()> &&messagePrinterFunc) {
error->foundUseOutsideOfLifetime = true;
handleError(std::move(messagePrinterFunc));
}
private:
bool handleError(llvm::function_ref<void()> &&messagePrinterFunc,
bool quiet = false) const {
if (behavior.shouldPrintMessage()) {
if (!quiet) {
tryDumpErrorCounter();
llvm::errs() << "Begin Error in Function: '" << functionName << "'\n";
}
messagePrinterFunc();
if (!quiet) {
tryDumpErrorCounter();
llvm::errs() << "End Error in Function: '" << functionName << "'\n";
auto *self = const_cast<ErrorBuilder *>(this);
self->tryIncrementErrorCounter();
}
}
if (behavior.shouldReturnFalse()) {
return false;
}
llvm::errs() << "Found ownership error?!\n";
llvm::report_fatal_error("triggering standard assertion failure routine");
}
};
} // namespace swift
#endif