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