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>
14 #if MAGIC_USE_QPROF_INSTRUMENTATION
15 #include <common/qprof_common.h>
22 #define magicPassLog(M) DEBUG(dbgs() << "MagicPass: " << M << "\n")
23 #define magicPassErr(M) errs() << "MagicPass: " << M << "\n"
25 class MagicPass
: public ModulePass
{
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;
41 virtual bool runOnModule(Module
&M
);
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
;
57 GlobalVariable
* magicArray
;
58 GlobalVariable
* magicTypeArray
;
59 GlobalVariable
* magicFunctionArray
;
60 GlobalVariable
* magicDsindexArray
;
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
;
76 #if MAGIC_USE_QPROF_INSTRUMENTATION
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
);
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
);
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
);
104 inline std::vector
<GlobalVariable
*> MagicPass::getGlobalVariables() const {
105 return globalVariables
;
108 inline std::vector
<int> MagicPass::getGlobalVariableSizes() const {
109 return globalVariableSizes
;
112 inline std::vector
<GlobalVariable
*> MagicPass::getShadowGlobalVariables() const {
113 return shadowGlobalVariables
;
116 inline std::vector
<Function
*> MagicPass::getFunctions() const {
120 inline GlobalVariable
* MagicPass::getMagicArray() const {
124 inline GlobalVariable
* MagicPass::getMagicTypeArray() const {
125 return magicTypeArray
;
128 inline GlobalVariable
* MagicPass::getMagicFunctionArray() const {
129 return magicFunctionArray
;
132 inline GlobalVariable
* MagicPass::getMagicDsindexArray() const {
133 return magicDsindexArray
;