-
Notifications
You must be signed in to change notification settings - Fork 74
/
RustTestFinder.cpp
345 lines (278 loc) · 11.2 KB
/
RustTestFinder.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "Rust/RustTestFinder.h"
#include "Context.h"
#include "Logger.h"
#include "MutationOperators/MutationOperator.h"
#include "MutationOperators/MutationOperatorFilter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "Rust/RustTest.h"
#include "MutationOperators/AddMutationOperator.h"
#include "MutationOperators/NegateConditionMutationOperator.h"
#include "MutationOperators/RemoveVoidFunctionMutationOperator.h"
#include <queue>
#include <set>
#include <vector>
#include <cxxabi.h>
using namespace mull;
using namespace llvm;
class RustTestMutationOperatorFilter : public MutationOperatorFilter {
public:
bool shouldSkipInstruction(llvm::Instruction *instruction) {
if (CallInst *callInst = dyn_cast<CallInst>(instruction)) {
if (Function *calledFunction = callInst->getCalledFunction()) {
if (calledFunction->getName().find("panic") != std::string::npos) {
return true;
}
}
}
// For now we ignore all instructions without a debug information.
if (instruction->hasMetadata() == false) {
return true;
}
int debugInfoKindID = 0;
MDNode *debug = instruction->getMetadata(debugInfoKindID);
if (DILocation *location = dyn_cast<DILocation>(debug)) {
if (location->getFilename().str().find("buildbot") != std::string::npos) {
return true;
}
}
return false;
};
};
RustTestFinder::RustTestFinder(
std::vector<std::unique_ptr<MutationOperator>> mutationOperators,
const std::vector<std::string> testsToFilter) : TestFinder(),
mutationOperators(std::move(mutationOperators)),
testsToFilter(testsToFilter)
{
}
/// The algorithm is the following:
///
/// When a Rust module with functions and tests is compiled to LLVM IR via
/// `rustc --test`, the following functions and constants are important:
///
/// - `main` function which is a primary test runner
/// - `test_main` (called by `main`)
/// - `test_main_static` (called by `test_main`, it is the actual test runner),
/// See https://doc.rust-lang.org/1.1.0/src/test/lib.rs.html#267
///
/// Rust stores the test methods to run: their names and function pointers into
/// a global variable: literal of type `internal unnamed_addr constant` which is
/// then given by reference to `test_main_static`.
///
/// Algorithm works in two steps:
/// 1) First we find a `test_main_static` function and find that global
/// variable.
/// 2) We go through that literal and extract the test function pointers.
///
/// See prettified LLVM IR example for simple Rust module with tests:
/// https://github.com/mull-project/mull/blob/54d0e6f4ddea458a5d77fa707b26acef9f5a6093/lab/rust/example.ll.pretty
///
/// See also original StackOverflow question we asked in order to get a feedback
/// about possible approaches:
/// http://stackoverflow.com/questions/42177712/how-do-i-find-the-function-pointers-for-tests-from-the-llvm-ir-code-of-a-rust-pr#42177712
///
/// TODO:
/// `main` function calls `test_main_static` indirectly: the pointer to it
/// is given to a `std::rt::lang_start` function. There might be some important
/// setup going on (setup of global variables, static constructors, etc).
/// The work on real-world Rust code bases will most likely reveal more about
/// this.
std::vector<std::unique_ptr<Test>> RustTestFinder::findTests(Context &Ctx) {
std::vector<std::unique_ptr<Test>> tests;
/// If we have multiple IR modules compiled, each module will have its own
/// global variable with test functions.
std::vector<GlobalValue *> testContentsGlobalVariables;
for (auto &module : Ctx.getModules()) {
auto &functionList = module->getModule()->getFunctionList();
for (auto &function : functionList) {
for (auto &bb : function) {
for (auto &instruction : bb) {
CallInst *callInst = dyn_cast<CallInst>(&instruction);
if (callInst == nullptr) {
continue;
}
Function *calledFunction = callInst->getCalledFunction();
if (calledFunction == nullptr) {
continue;
}
StringRef cfName = calledFunction->getName();
if (cfName.find("test_main_static") == std::string::npos) {
continue;
}
assert(callInst->getNumArgOperands() == 2);
auto operand1 = callInst->getOperand(0);
assert(operand1->getType()->getTypeID() == llvm::Type::PointerTyID);
auto ref = dyn_cast<ConstantExpr>(operand1);
assert(ref);
auto global = dyn_cast<GlobalValue>(ref->getOperand(0));
assert(global);
testContentsGlobalVariables.push_back(global);
}
}
}
}
if (testContentsGlobalVariables.size() == 0) {
return tests;
}
for (auto &testContentsGV : testContentsGlobalVariables) {
assert(testContentsGV->getValueType()->getTypeID() == Type::StructTyID);
auto testContents = dyn_cast<Constant>(testContentsGV->getOperand(0));
assert(testContents->getType()->getTypeID() == Type::StructTyID);
int numberOfTestDescriptions = testContents->getNumOperands();
for (int i = 0; i < numberOfTestDescriptions; i++) {
auto testStruct = dyn_cast<Constant>(testContents->getOperand(i));
assert(testStruct->getType()->getTypeID() == Type::StructTyID);
assert(testStruct->getNumOperands() == 2);
auto testPointerStruct =
dyn_cast<ConstantStruct>(testStruct->getOperand(1));
assert(testPointerStruct->getNumOperands() == 3);
auto testFunction = dyn_cast<Function>(testPointerStruct->getOperand(1));
if (testsToFilter.empty() == false &&
std::find(testsToFilter.begin(),
testsToFilter.end(),
testFunction->getName().str()) == testsToFilter.end()) {
continue;
}
tests.push_back(make_unique<RustTest>(testFunction->getName(),
testFunction));
}
}
return tests;
}
static bool shouldSkipDefinedFunction(llvm::Function *definedFunction) {
// _ZN3std9panicking11begin_panic17h6c6bf73f39cdbdacE
if (definedFunction->getName().find(StringRef("begin_panic")) != StringRef::npos) {
return true;
}
return false;
}
std::vector<std::unique_ptr<Testee>>
RustTestFinder::findTestees(Test *Test,
Context &Ctx,
int maxDistance) {
RustTest *rustTest = dyn_cast<RustTest>(Test);
Function *testFunction = rustTest->getFunction();
std::vector<std::unique_ptr<Testee>> testees;
std::queue<std::unique_ptr<Testee>> traversees;
std::set<Function *> checkedFunctions;
Module *testBodyModule = testFunction->getParent();
traversees.push(make_unique<Testee>(testFunction, nullptr, nullptr, 0));
while (!traversees.empty()) {
std::unique_ptr<Testee> traversee = std::move(traversees.front());
Testee *traverseePointer = traversee.get();
traversees.pop();
Function *traverseeFunction = traversee->getTesteeFunction();
const int mutationDistance = traversee->getDistance();
testees.push_back(std::move(traversee));
/// The function reached the max allowed distance
/// Hence we don't go deeper
if (mutationDistance == maxDistance) {
continue;
}
for (auto &BB : *traverseeFunction) {
for (auto &I : BB) {
auto *instruction = &I;
CallInst *callInstruction = dyn_cast<CallInst>(instruction);
if (callInstruction == nullptr) {
continue;
}
int callOperandIndex = callInstruction->getNumOperands() - 1;
Value *callOperand = callInstruction->getOperand(callOperandIndex);
Function *functionOperand = dyn_cast<Function>(callOperand);
if (!functionOperand) {
continue;
}
/// Two modules may have static function with the same name, e.g.:
///
/// // ModuleA
/// define range() {
/// // ...
/// }
///
/// define test_A() {
/// call range()
/// }
///
/// // ModuleB
/// define range() {
/// // ...
/// }
///
/// define test_B() {
/// call range()
/// }
///
/// Depending on the order of processing either `range` from `A` or `B`
/// will be added to the `context`, hence we may find function `range`
/// from module `B` while processing body of the `test_A`.
/// To avoid this problem we first look for function inside of a current
/// module.
///
/// FIXME: Context should report if a function being added already exist
/// FIXME: What other problems such behaviour may bring?
Function *definedFunction =
testBodyModule->getFunction(functionOperand->getName());
if (!definedFunction || definedFunction->isDeclaration()) {
definedFunction =
Ctx.lookupDefinedFunction(functionOperand->getName());
} else {
// Logger::debug() << "RustTestFinder> did not find a function: "
// << definedFunction->getName() << '\n';
}
if (definedFunction) {
auto functionWasNotProcessed =
checkedFunctions.find(definedFunction) == checkedFunctions.end();
checkedFunctions.insert(definedFunction);
if (functionWasNotProcessed) {
/// Filtering
if (shouldSkipDefinedFunction(definedFunction)) {
continue;
}
/// The code below is not actually correct
/// For each C++ constructor compiler can generate up to three
/// functions*. Which means that the distance might be incorrect
/// We need to find a clever way to fix this problem
///
/// * Here is a good overview of what's going on:
/// http://stackoverflow.com/a/6921467/829116
///
traversees.push(make_unique<Testee>(definedFunction,
callInstruction,
traverseePointer,
mutationDistance + 1));
}
}
}
}
}
return testees;
}
std::vector<MutationPoint *>
RustTestFinder::findMutationPoints(const Context &context,
llvm::Function &testee) {
if (MutationPointsRegistry.count(&testee) != 0) {
return MutationPointsRegistry.at(&testee);
}
std::vector<MutationPoint *> points;
RustTestMutationOperatorFilter filter;
for (auto &mutationOperator : mutationOperators) {
for (auto point : mutationOperator->getMutationPoints(context, &testee, filter)) {
points.push_back(point);
MutationPoints.emplace_back(std::unique_ptr<MutationPoint>(point));
}
}
MutationPointsRegistry.insert(std::make_pair(&testee, points));
return points;
}
std::vector<std::unique_ptr<MutationPoint>> RustTestFinder::findMutationPoints(
std::vector<MutationOperator *> &MutationOperators,
llvm::Function &F) {
return std::vector<std::unique_ptr<MutationPoint>>();
}