Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. // #include "llvm/Pass.h"
  2. // #include "llvm/IR/Function.h"
  3. // #include "llvm/Support/raw_ostream.h"
  4. // #include "llvm/IR/LegacyPassManager.h"
  5. // #include "llvm/IR/InstrTypes.h"
  6. // #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  7. // #include "llvm/IR/IRBuilder.h"
  8. // #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  9. // #include "llvm/IR/Attributes.h"
  10. // #include "llvm/IR/DerivedTypes.h"
  11. // #include "llvm/IR/Function.h"
  12.  
  13. // using namespace llvm;
  14.  
  15. // namespace {
  16.  
  17. // std::map<std::string, std::tuple<size_t, int> > mem_addrs; // stores pointer name, address and no. of bytes allocated
  18.  
  19.  
  20.  
  21. // struct SkeletonPass : public FunctionPass {
  22. // static char ID;
  23. // SkeletonPass() : FunctionPass(ID) {}
  24.  
  25. // virtual bool runOnFunction(Function &F) {
  26. // for (auto &B : F) {
  27. // for (auto &I : 😎 {
  28.  
  29. // if(CallInst *CI = dyn_cast<CallInst>(&I)) {
  30.  
  31.  
  32. // Function *Callee = CI->getCalledFunction();
  33. // if (Callee->getName() == "malloc") {
  34. // // Found malloc instruction
  35. // errs() << "INSIDE MALLOC " << "\n";
  36. // errs() << "Instruction: ";
  37. // I.print(llvm::errs(), true);
  38. // errs() << "\n";
  39.  
  40. // // // Find register
  41. // // Function* fn = CI->getCalledFunction();
  42. // // StringRef fn_name = fn->getName();
  43.  
  44. // LLVMContext &Context = F.getContext();
  45. // IRBuilder<> builder(CI);
  46. // builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
  47.  
  48. // Type *parameterTypes[] = {IntegerType::getIntPtry}
  49. // std::vector<Value *> ArgsV;
  50. // ArgsV.push_back()
  51. // builder.CreateCall(F.getParent()->getOrInsertFunction("printf", FunctionType::get(IntegerType::getInt32Ty(Context), PointerType::get(Type::getInt8Ty(Context), 0), true /* this is var arg func type*/), "PENIS", "printfCall"));
  52.  
  53.  
  54. // }
  55. // }
  56. // }
  57. // }
  58.  
  59. // return true;
  60. // }
  61. // };
  62. // }
  63.  
  64. #include "llvm/Pass.h"
  65. #include "llvm/IR/Module.h"
  66. #include "llvm/IR/Function.h"
  67. #include "llvm/IR/BasicBlock.h"
  68. #include "llvm/ADT/Statistic.h"
  69. #include "llvm/IR/InstrTypes.h"
  70. #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  71. #include "llvm/IR/IRBuilder.h"
  72. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  73. #include "llvm/IR/LegacyPassManager.h"
  74. #include "llvm/Support/raw_ostream.h"
  75.  
  76.  
  77. using namespace llvm;
  78. namespace {
  79.  
  80. struct CountStats : public ModulePass {
  81. static char ID;
  82. CountStats() : ModulePass(ID) {}
  83. bool runOnModule(Module &M);
  84. bool runOnFunction(Function &F, Module &M);
  85. };
  86.  
  87. }
  88.  
  89. char CountStats::ID = 0;
  90.  
  91. static RegisterPass<CountStats>
  92.  
  93. X("DynCount", "Count Statistics");
  94.  
  95. bool CountStats::runOnModule(Module &M) {
  96. for(Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  97.  
  98. runOnFunction(*F, M);
  99. }
  100. return false;
  101. }
  102.  
  103.  
  104. bool CountStats::runOnFunction(Function &F, Module &M) {
  105.  
  106. LLVMContext &Context = F.getContext();
  107.  
  108. Constant *BOLog = F.getParent()->getOrInsertFunction("BO", Type::getVoidTy(Context), NULL);
  109.  
  110. Constant *allLog = F.getParent()->getOrInsertFunction("ALL", Type::getVoidTy(Context), NULL);
  111.  
  112. Constant *branLog = F.getParent()->getOrInsertFunction("BRAN", Type::getVoidTy(Context), NULL);
  113.  
  114. Constant *loadLog = F.getParent()->getOrInsertFunction("LOAD", Type::getVoidTy(Context), NULL);
  115.  
  116. Constant *storeLog = F.getParent()->getOrInsertFunction("STORE", Type::getVoidTy(Context), NULL);
  117.  
  118. Constant *callLog = F.getParent()->getOrInsertFunction("CALL", Type::getVoidTy(Context), NULL);
  119.  
  120. Constant *instLog = F.getParent()->getOrInsertFunction("INST", Type::getVoidTy(Context), NULL);
  121.  
  122.  
  123. for (auto& B : F) {
  124. for (auto& I : 😎 {
  125.  
  126. //errs().write_escaped(I.getOpcodeName()) << '\n';
  127. if (auto* cont = dyn_cast<AllocaInst>(&I)) {
  128.  
  129. IRBuilder<> builder(cont);
  130. builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
  131. builder.CreateCall(allLog);
  132.  
  133.  
  134.  
  135.  
  136.  
  137. }else if (auto* cont = dyn_cast<BinaryOperator>(&I)) {
  138.  
  139. IRBuilder<> builder(cont);
  140. builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
  141. builder.CreateCall(BOLog);
  142.  
  143.  
  144.  
  145.  
  146. } else if (auto* cont = dyn_cast<StoreInst>(&I)) {
  147.  
  148. IRBuilder<> builder(cont);
  149. builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
  150. builder.CreateCall(storeLog);
  151.  
  152.  
  153. } else if (auto* cont = dyn_cast<LoadInst>(&I)) {
  154.  
  155. IRBuilder<> builder(cont);
  156. builder.SetInsertPoint(&B, ++builder.GetInsertPoint());
  157. builder.CreateCall(loadLog);
  158.  
  159.  
  160. } else if (auto* cont = dyn_cast<BranchInst>(&I)) {
  161.  
  162. IRBuilder<> builder(cont);
  163. builder.SetInsertPoint(&B, --builder.GetInsertPoint());
  164. builder.CreateCall(branLog);
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
  175. }
  176. return false;
  177. }
  178.  
  179. // char SkeletonPass::ID = 0;
  180.  
  181. // static RegisterPass<SkeletonPass> X("skeleton", "use after free");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement