-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiDomainSfi.cpp
More file actions
65 lines (51 loc) · 1.96 KB
/
Copy pathMultiDomainSfi.cpp
File metadata and controls
65 lines (51 loc) · 1.96 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
#include <set>
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
using namespace llvm;
#define BITMASK 0xFFFFFFFF
namespace {
struct MultiDomainSfiPass : public FunctionPass {
static char ID;
MultiDomainSfiPass() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
bool modified = false;
std::set <Instruction*> maskedInsts;
for (auto &B : F) {
for (auto &I : B) {
if (auto *store = dyn_cast<StoreInst>(&I)) {
if (!maskedInsts.count(store)) {
// Insert at the point where the instruction `op` appears.
IRBuilder<> builder(store);
// Mask the target as an integer
Value *target = builder.CreatePtrToInt(store->getPointerOperand(), builder.getInt32Ty());
Value *masked = builder.CreateAnd(target, BITMASK);
Value *masked_target = builder.CreateIntToPtr(masked, store->getPointerOperand()->getType());
Instruction *newStore = new StoreInst(store->getValueOperand(), masked_target);
ReplaceInstWithInst(store, newStore);
maskedInsts.insert(newStore);
errs() << "Masking Store instruction with " << BITMASK << "\n";
// We modified the code.
return true;
}
}
}
}
return false;
}
};
}
char MultiDomainSfiPass::ID = 0;
// Automatically enable the pass.
static void registerMultiDomainSfiPass(const PassManagerBuilder &,
legacy::PassManagerBase &PM) {
PM.add(new MultiDomainSfiPass());
}
static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
registerMultiDomainSfiPass);