-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecDev.cpp
More file actions
254 lines (230 loc) · 6.68 KB
/
SecDev.cpp
File metadata and controls
254 lines (230 loc) · 6.68 KB
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
//===- SecDev.cpp - Skeleton code for LLVM Tutorial -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides skeleton code for a transformation that adds calls to
// every memory access operation and every branch.
//
//===----------------------------------------------------------------------===//
#include "SecDev.h"
#include <llvm/ADT/Statistic.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/Pass.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
#define DEBUG_TYPE "secdev"
//
// Create statistics that the opt and clang tools can report
//
STATISTIC(NumLoads, "Number of load instructions instrumented");
STATISTIC(NumStores, "Number of store instructions instrumented");
//
// Function: getVoidPtrType()
//
// Description:
// Return a pointer to the void pointer type that belongs to the specified
// LLVM Context. In LLVM IR, a void pointer type is a pointer to an 8-bit
// value (i.e., a "char *" in C).
//
// Inputs:
// C - A reference to the LLVM Context in which to create or retrieve the
// void pointer type.
//
// Outputs:
// None.
//
// Return value:
// A pointer to the void pointer type is returned.
//
static PointerType *
getVoidPtrType (LLVMContext & C) {
//
// Create an integer type that is 8-bits in size and then create a pointer
// type that pointers to this integer type.
//
return (PointerType::getUnqual(IntegerType::get(C, 8)));
}
//
// Function: createRuntimeCheckFunc()
//
// Description:
// This function creates the declaration of the checkMemory function that
// checks the pointer dereferenced in a load or store instruction at run-time.
// If the checkMemory function is already declared or defined within the
// specified Module, a pointer to it is returned.
//
// Inputs:
// M - A reference to the Module to which the checkMemory function declaration
// should be added.
//
// Outputs:
// M - The Module modified (if necessary) to contain a checkMemory function
// declaration.
//
// Return value:
// A pointer to the checkMemory function is returned.
//
static FunctionCallee
createRuntimeCheckFunc (Module & M) {
//
// Create the types needed for the declaration.
//
Type * VoidType = Type::getVoidTy(M.getContext());
Type * VoidPtrType = getVoidPtrType(M.getContext());
FunctionType * FuncType = FunctionType::get(VoidType,
ArrayRef<Type *>(VoidPtrType),
false);
//
// Create the run-time check function.
//
return M.getOrInsertFunction ("checkMemory", FuncType);
}
//
// Method: visitLoadInst()
//
// Description:
// Add a call to a library function before every load instruction. This
// method is called by the InstVisitor class whenever it encounters a load
// instruction.
//
// Inputs:
// LI - A pointer to the load instruction to transform.
//
// Outputs:
// None.
//
// Return value:
// None
//
void
SecDev::visitLoadInst (LoadInst * LI) {
//
// Retrieve the pointer operand from the load instruction.
//
Value * Pointer = LI->getPointerOperand();
//
// Cast the pointer argument of the load instruction to our void pointer
// type.
//
LLVMContext & C = LI->getContext();
Pointer = new BitCastInst(Pointer, getVoidPtrType(C), Pointer->getName(), LI);
//
// Insert a call instruction to the check memory function before the load
// instruction.
//
CallInst::Create(checkMemory, ArrayRef<Value*>(Pointer), "", LI);
//
// Increment the count of load instructions that have been instrumented.
//
++NumLoads;
return;
}
//
// Method: visitStoreInst()
//
// Description:
// Add a call to a library function before every store instruction. This
// method is called by the InstVisitor class whenever it encounters a store
// instruction.
//
// Inputs:
// SI - A pointer to the store instruction to transform.
//
// Outputs:
// None.
//
// Return value:
// None
//
void
SecDev::visitStoreInst (StoreInst * SI) {
//
// Retrieve the pointer operand from the store instruction.
//
Value * Pointer = SI->getPointerOperand();
//
// Cast the pointer argument of the store instruction to our void pointer
// type.
//
LLVMContext & C = SI->getContext();
Pointer = new BitCastInst(Pointer, getVoidPtrType(C), Pointer->getName(), SI);
//
// Insert a call instruction to the check memory function before the store
// instruction.
//
CallInst::Create(checkMemory, ArrayRef<Value*>(Pointer), "", SI);
//
// Increment the count of store instructions that have been instrumented.
//
++NumStores;
return;
}
//
// Method: runOnModule()
//
// Description:
// This method is called by the PassManager when this pass needs to transform
// a module. This pass will insert calls to a library function for every
// load and store instruction and every branch.
//
// Inputs:
// M - A reference to the Module to transform.
//
// Outputs:
// M - The transformed Module.
//
// Return value:
// true - The module has been modified.
// false - The module has not been modified.
//
bool
SecDev::runOnModule (Module & M) {
//
// Add a declaration of the run-time check functions.
//
checkMemory = createRuntimeCheckFunc(M);
//
// Scan all of the instructions within the module to determine if we need to
// transform them. If so, call the appropriate visit method to transform
// the instruction.
//
for (Module::iterator fi = M.begin(); fi != M.end(); ++fi) {
for (Function::iterator bi = fi->begin(); bi != fi->end(); ++bi) {
for (BasicBlock::iterator it = bi->begin(); it != bi->end(); ++it) {
Instruction * I = &*it;
//
// If this is a load instruction, instrument it with a call to the
// function that checks the pointer used in the load.
//
if (LoadInst * LI = dyn_cast<LoadInst>(I)) {
visitLoadInst(LI);
}
//
// If this is a store instruction, instrument it with a call to the
// function that checks the pointer used in the store.
//
if (StoreInst * SI = dyn_cast<StoreInst>(I)) {
visitStoreInst(SI);
}
}
}
}
// Assume that we have modified the module
return true;
}
// Pass identifier variable (used by PassManager)
char SecDev::ID = 0;
// Utility method to create a SecDev pass object
namespace llvm {
ModulePass *createSecDevPass() {
return new SecDev();
}
}
static RegisterPass<SecDev> X ("secdev", "SecDev Tutorial Pass", false, false);