1 #ifndef MAGIC_PASS_H 2 3 #define MAGIC_PASS_H 4 5 #include <pass.h> 6 #include <magic/magic.h> 7 #include <magic/support/MagicUtil.h> 8 #include <magic/support/SmartType.h> 9 #include <magic/support/TypeInfo.h> 10 #include <magic/support/MagicDebugFunction.h> 11 #include <magic/support/MagicMemFunction.h> 12 #include <magic/support/MagicMmapCtlFunction.h> 13 14 #if MAGIC_USE_QPROF_INSTRUMENTATION 15 #include <common/qprof_common.h> 16 #endif 17 18 using namespace llvm; 19 20 namespace llvm { 21 22 #define magicPassLog(M) DEBUG(dbgs() << "MagicPass: " << M << "\n") 23 #define magicPassErr(M) errs() << "MagicPass: " << M << "\n" 24 25 class MagicPass : public ModulePass { 26 27 public: 28 static char ID; 29 30 MagicPass(); 31 32 std::vector<GlobalVariable*> getGlobalVariables() const; 33 std::vector<int> getGlobalVariableSizes() const; 34 std::vector<GlobalVariable*> getShadowGlobalVariables() const; 35 std::vector<Function*> getFunctions() const; 36 GlobalVariable* getMagicArray() const; 37 GlobalVariable* getMagicTypeArray() const; 38 GlobalVariable* getMagicFunctionArray() const; 39 GlobalVariable* getMagicDsindexArray() const; 40 41 virtual bool runOnModule(Module &M); 42 43 private: 44 std::vector<GlobalVariable*> globalVariables; 45 std::set<GlobalVariable*> globalVariablesWithAddressTaken; 46 std::vector<int> globalVariableSizes; 47 std::vector<GlobalVariable*> shadowGlobalVariables; 48 std::vector<Function*> functions; 49 std::vector<TypeInfo*> globalTypeInfos; 50 std::map<GlobalValue*, TypeInfo*> globalParentMap; 51 std::map<GlobalValue*, TypeInfo*>::iterator parentMapIt; 52 std::map<std::string, GlobalVariable*> stringOwnerMap; 53 std::map<std::string, GlobalVariable*>::iterator stringOwnerMapIt; 54 std::map<GlobalVariable*, std::string> stringOwnerInvertedMap; 55 std::map<GlobalVariable*, std::string>::iterator stringOwnerInvertedMapIt; 56 57 GlobalVariable* magicArray; 58 GlobalVariable* magicTypeArray; 59 GlobalVariable* magicFunctionArray; 60 GlobalVariable* magicDsindexArray; 61 62 std::vector<std::string> libPathRegexes; 63 std::vector<std::string> voidTypeAliases; 64 std::set<std::string> voidTypeAliasesSet; 65 std::vector<std::string> mmFuncPrefixes; 66 std::set<std::pair<std::string, std::string> > mmFuncPairs; 67 std::vector<std::string> mmPoolFunctions; 68 std::vector<std::string> mmapCtlFunctions; 69 std::set<std::string>::iterator stringSetIt; 70 std::set<Function*> brkFunctions; 71 std::set<Function*> sbrkFunctions; 72 std::vector<Regex*> magicDataSectionRegexes; 73 std::vector<Regex*> magicFunctionSectionRegexes; 74 std::vector<Regex*> extLibSectionRegexes; 75 76 #if MAGIC_USE_QPROF_INSTRUMENTATION 77 QProfConf *qprofConf; 78 #endif 79 80 void qprofInstrumentationInit(Module &M); 81 void qprofInstrumentationApply(Module &M); 82 bool checkPointerVariableIndexes(TYPECONST Type* type, std::vector<int> &ptrVarIndexes, unsigned offset=0); 83 void findPointerVariables(Function* function, Value *value, std::vector<Value*> &ptrVars, std::vector<std::vector<int> > &ptrVarIndexes, Value *parent = NULL, bool isUser=false); 84 TypeInfo* typeInfoFromPointerVariables(Module &M, TypeInfo *voidPtrTypeInfo, std::vector<Value*> &ptrVars, std::vector<std::vector<int> > &ptrVarIndexes, std::string &allocName); 85 TypeInfo* getAllocTypeInfo(Module &M, TypeInfo *voidPtrTypeInfo, const CallSite &CS, std::string &allocName, std::string &allocParentName); 86 TypeInfo* fillTypeInfos(TypeInfo &sourceTypeInfo, std::vector<TypeInfo*> &typeInfos); 87 TypeInfo* fillExternalTypeInfos(TYPECONST Type* sourceType, GlobalValue *parent, std::vector<TypeInfo*> &typeInfos); 88 void printInterestingTypes(TYPECONST TypeInfo *aTypeInfo); 89 unsigned getMaxRecursiveSequenceLength(TYPECONST TypeInfo *aTypeInfo); 90 FunctionType* getFunctionType(TYPECONST FunctionType *baseType, std::vector<unsigned> selectedArgs); 91 bool isCompatibleMagicMemFuncType(TYPECONST FunctionType *type, TYPECONST FunctionType* magicType); 92 Function* findWrapper(Module &M, std::string *magicMemPrefixes, Function *f, std::string fName); 93 94 void indexCasts(Module &M, User *U, std::vector<TYPECONST Type*> &intCastTypes, std::vector<int> &intCastValues, std::map<TYPECONST Type*, std::set<TYPECONST Type*> > &bitcastMap); 95 96 void fillStackInstrumentedFunctions(std::vector<Function*> &stackIntrumentedFuncs, Function *deepestLLFunction); 97 void indexLocalTypeInfos(Module &M, Function *F, std::map<AllocaInst*, std::pair<TypeInfo*, std::string> > &localMap); 98 void addMagicStackDsentryFuncCalls(Module &M, Function *insertCallsInFunc, Function *localsFromFunc, Function *dsentryCreateFunc, Function *dsentryDestroyFunc, TYPECONST StructType *dsentryStructType, std::map<AllocaInst*, std::pair<TypeInfo*, std::string> > localTypeInfoMap, std::map<TypeInfo*, Constant*> &magicArrayTypePtrMap, TypeInfo *voidPtrTypeInfo, std::vector<TypeInfo*> &typeInfoList, std::vector<std::pair<std::string, std::string> > &namesList, std::vector<int> &flagsList); 99 bool isExtLibrary(GlobalValue *GV, DIDescriptor *DID); 100 bool isMagicGV(Module &M, GlobalVariable *GV); 101 bool isMagicFunction(Module &M, Function *F); 102 }; 103 104 inline std::vector<GlobalVariable*> MagicPass::getGlobalVariables() const { 105 return globalVariables; 106 } 107 108 inline std::vector<int> MagicPass::getGlobalVariableSizes() const { 109 return globalVariableSizes; 110 } 111 112 inline std::vector<GlobalVariable*> MagicPass::getShadowGlobalVariables() const { 113 return shadowGlobalVariables; 114 } 115 116 inline std::vector<Function*> MagicPass::getFunctions() const { 117 return functions; 118 } 119 120 inline GlobalVariable* MagicPass::getMagicArray() const { 121 return magicArray; 122 } 123 124 inline GlobalVariable* MagicPass::getMagicTypeArray() const { 125 return magicTypeArray; 126 } 127 128 inline GlobalVariable* MagicPass::getMagicFunctionArray() const { 129 return magicFunctionArray; 130 } 131 132 inline GlobalVariable* MagicPass::getMagicDsindexArray() const { 133 return magicDsindexArray; 134 } 135 136 } 137 138 #endif 139