|
| 1 | +#include <llvm/IR/LegacyPassManager.h> |
| 2 | +#include <llvm/Transforms/IPO/PassManagerBuilder.h> |
| 3 | +#include <llvm/IR/Module.h> |
| 4 | +#include <llvm/IR/Value.h> |
| 5 | +#include <llvm/IR/Type.h> |
| 6 | +#include <llvm/IR/Operator.h> |
| 7 | +#include <llvm/IR/Instructions.h> |
| 8 | +#include <llvm/IR/InstrTypes.h> |
| 9 | +#include <llvm/Bitcode/BitcodeWriter.h> |
| 10 | + |
| 11 | +#include "llvm/IR/LegacyPassManager.h" |
| 12 | +#include "llvm/IR/CallSite.h" |
| 13 | +#include "llvm/IR/IRBuilder.h" |
| 14 | +#include "llvm/IR/InlineAsm.h" |
| 15 | +#include "llvm/ADT/Statistic.h" |
| 16 | +#include "llvm/IR/Function.h" |
| 17 | +#include "llvm/Pass.h" |
| 18 | +#include "llvm/Support/raw_ostream.h" |
| 19 | + |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | +using namespace legacy; |
| 23 | + |
| 24 | +static const char *const SanCovTraceSrt1Name = "__sanitizer_cov_trace_srt1"; |
| 25 | +static const char *const SanCovTraceSrt2Name = "__sanitizer_cov_trace_srt2"; |
| 26 | +static const char *const SanCovTraceSrt4Name = "__sanitizer_cov_trace_srt4"; |
| 27 | +static const char *const SanCovTraceSrt8Name = "__sanitizer_cov_trace_srt8"; |
| 28 | + |
| 29 | +namespace { |
| 30 | + struct AssignTracker : public ModulePass { |
| 31 | + static char ID; // Pass identification, replacement for typeid |
| 32 | + FunctionCallee SanCovTraceSrt1; |
| 33 | + FunctionCallee SanCovTraceSrt2; |
| 34 | + FunctionCallee SanCovTraceSrt4; |
| 35 | + FunctionCallee SanCovTraceSrt8; |
| 36 | + Type *VoidTy; |
| 37 | + Type *Int8Ty; |
| 38 | + Type *Int16Ty; |
| 39 | + Type *Int32Ty; |
| 40 | + Type *Int64Ty; |
| 41 | + std::hash<std::string> hashSrt; |
| 42 | + std::map<std::string, unsigned> StructIDMap; |
| 43 | + |
| 44 | + LLVMContext *C; |
| 45 | + AssignTracker() : ModulePass(ID) {} |
| 46 | + |
| 47 | + bool runOnModule(Module &M) override { |
| 48 | + C = &M.getContext(); |
| 49 | + IRBuilder<> IRB(*C); |
| 50 | + |
| 51 | + VoidTy = IRB.getVoidTy(); |
| 52 | + Int8Ty = IRB.getInt8Ty(); |
| 53 | + Int16Ty = IRB.getInt16Ty(); |
| 54 | + Int32Ty = IRB.getInt32Ty(); |
| 55 | + Int64Ty = IRB.getInt64Ty(); |
| 56 | + |
| 57 | + SanCovTraceSrt1 = M.getOrInsertFunction(SanCovTraceSrt1Name, VoidTy, Int32Ty, Int8Ty); |
| 58 | + SanCovTraceSrt2 = M.getOrInsertFunction(SanCovTraceSrt2Name, VoidTy, Int32Ty, Int16Ty); |
| 59 | + SanCovTraceSrt4 = M.getOrInsertFunction(SanCovTraceSrt4Name, VoidTy, Int32Ty, Int32Ty); |
| 60 | + SanCovTraceSrt8 = M.getOrInsertFunction(SanCovTraceSrt8Name, VoidTy, Int32Ty, Int64Ty); |
| 61 | + |
| 62 | + for (Function &F : M) |
| 63 | + instrumentFieldAssign(F); |
| 64 | + for (auto i : StructIDMap) { |
| 65 | + errs() << i.first << ": " << std::to_string(i.second) << "\n"; |
| 66 | + } |
| 67 | + return true; |
| 68 | + } |
| 69 | + void injectFieldAssignTracker(Instruction *I, unsigned id); |
| 70 | + void instrumentFieldAssign(Function &func); |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +std::string stripNum(std::string name) { |
| 75 | + size_t len = name.size(); |
| 76 | + char tmp[len]; |
| 77 | + strncpy(tmp, name.c_str(), len); |
| 78 | + if (len < 1) |
| 79 | + return name; |
| 80 | + while ((tmp[len-1] <= '9' && tmp[len-1] >= '0' && len > 1) |
| 81 | + || (tmp[len-1] == 'i' && tmp[len-2] == '.' && len > 2) |
| 82 | + || (tmp[len-1] == '.' && len > 1)) { |
| 83 | + if (tmp[len-1] == 'i' && tmp[len-2] == '.') { |
| 84 | + tmp[len-1] = 0; |
| 85 | + tmp[len-2] = 0; |
| 86 | + len -= 2; |
| 87 | + continue; |
| 88 | + } |
| 89 | + tmp[len-1] = 0; |
| 90 | + len--; |
| 91 | + } |
| 92 | + name = name.substr(0, len); |
| 93 | + return name; |
| 94 | + |
| 95 | +; |
| 96 | +} |
| 97 | + |
| 98 | +void AssignTracker::instrumentFieldAssign(Function &func) { |
| 99 | + if (!func.size()) |
| 100 | + return; |
| 101 | + for (BasicBlock &bb : func) { |
| 102 | + for (Instruction &i : bb) { |
| 103 | + if (StoreInst *si = dyn_cast<StoreInst>(&i)) { |
| 104 | + const Value *val_op = si->getOperand(0); |
| 105 | + const Value *var_op = si->getPointerOperand(); |
| 106 | + if (!val_op->getType()->isIntegerTy()) |
| 107 | + continue; |
| 108 | + if (const GEPOperator *gep = dyn_cast<GEPOperator>(var_op)) { |
| 109 | + const Value *intPtr = gep->getPointerOperand(); |
| 110 | + if (Type *gepOpTy = dyn_cast<PointerType>(intPtr->getType())->getElementType()) { |
| 111 | + if (gepOpTy->isStructTy()) { |
| 112 | + std::string srtName = gepOpTy->getStructName(); |
| 113 | + std::string fieldName = var_op->getName(); |
| 114 | + std::string key = stripNum(srtName) + "->" + stripNum(fieldName); |
| 115 | + if (StructIDMap.find(key) == StructIDMap.end()) |
| 116 | + StructIDMap[key] = hashSrt(key); |
| 117 | + injectFieldAssignTracker(si, StructIDMap[key]); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void AssignTracker::injectFieldAssignTracker(Instruction *I, unsigned id) { |
| 127 | + IRBuilder<> IRB(I); |
| 128 | + Value *val = I->getOperand(0); |
| 129 | + unsigned bitWidth = val->getType()->getIntegerBitWidth(); |
| 130 | + switch (bitWidth) { |
| 131 | + case 8: { |
| 132 | + IRB.CreateCall(SanCovTraceSrt1, {IRB.getInt32(id), IRB.CreateIntCast(val, Int8Ty, true)}); |
| 133 | + break; |
| 134 | + } |
| 135 | + case 16: { |
| 136 | + IRB.CreateCall(SanCovTraceSrt2, {IRB.getInt32(id), IRB.CreateIntCast(val, Int16Ty, true)}); |
| 137 | + break; |
| 138 | + } |
| 139 | + case 32: { |
| 140 | + IRB.CreateCall(SanCovTraceSrt4, {IRB.getInt32(id), IRB.CreateIntCast(val, Int32Ty, true)}); |
| 141 | + break; |
| 142 | + } |
| 143 | + case 64: { |
| 144 | + IRB.CreateCall(SanCovTraceSrt8, {IRB.getInt32(id), IRB.CreateIntCast(val, Int64Ty, true)}); |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +char AssignTracker::ID = 0; |
| 151 | +static RegisterPass<AssignTracker> X("AssignTracker", "AssignTracker Pass", false, false); |
| 152 | + |
| 153 | +static void registerAssignTrackerPass(const PassManagerBuilder &, legacy::PassManagerBase &PM) |
| 154 | +{ |
| 155 | + PM.add(new AssignTracker()); |
| 156 | +} |
| 157 | + |
| 158 | +static RegisterStandardPasses RegisterAPass(PassManagerBuilder::EP_OptimizerLast, registerAssignTrackerPass); |
0 commit comments