-
Notifications
You must be signed in to change notification settings - Fork 135
/
Pass.cpp
89 lines (70 loc) · 2.71 KB
/
Pass.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
// This file is part of SymCC.
//
// SymCC is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// SymCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// SymCC. If not, see <https://www.gnu.org/licenses/>.
#include "Pass.h"
#include <llvm/ADT/SmallVector.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Transforms/Utils/ModuleUtils.h>
#include "Runtime.h"
#include "Symbolizer.h"
using namespace llvm;
#ifndef NDEBUG
#define DEBUG(X) \
do { \
X; \
} while (false)
#else
#define DEBUG(X) ((void)0)
#endif
char SymbolizePass::ID = 0;
bool SymbolizePass::doInitialization(Module &M) {
DEBUG(errs() << "Symbolizer module init\n");
// Redirect calls to external functions to the corresponding wrappers and
// rename internal functions.
for (auto &function : M.functions()) {
auto name = function.getName();
if (isInterceptedFunction(function))
function.setName(name + "_symbolized");
}
// Insert a constructor that initializes the runtime and any globals.
Function *ctor;
std::tie(ctor, std::ignore) = createSanitizerCtorAndInitFunctions(
M, kSymCtorName, "_sym_initialize", {}, {});
appendToGlobalCtors(M, ctor, 0);
return true;
}
bool SymbolizePass::runOnFunction(Function &F) {
auto functionName = F.getName();
if (functionName == kSymCtorName)
return false;
DEBUG(errs() << "Symbolizing function ");
DEBUG(errs().write_escaped(functionName) << '\n');
SmallVector<Instruction *, 0> allInstructions;
allInstructions.reserve(F.getInstructionCount());
for (auto &I : instructions(F))
allInstructions.push_back(&I);
Symbolizer symbolizer(*F.getParent());
symbolizer.symbolizeFunctionArguments(F);
for (auto &basicBlock : F)
symbolizer.insertBasicBlockNotification(basicBlock);
for (auto *instPtr : allInstructions)
symbolizer.visit(instPtr);
symbolizer.finalizePHINodes();
symbolizer.shortCircuitExpressionUses();
// DEBUG(errs() << F << '\n');
assert(!verifyFunction(F, &errs()) &&
"SymbolizePass produced invalid bitcode");
return true;
}