Static Value-Flow Analysis
CppUtil.h
Go to the documentation of this file.
1 //===- CPPUtil.h -- Base class of pointer analyses ---------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2017> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * CPPUtil.h
25  *
26  * Created on: Apr 13, 2016
27  * Author: Xiaokang Fan
28  */
29 
30 #ifndef CPPUtil_H_
31 #define CPPUtil_H_
32 
33 #include "SVFIR/SVFValue.h"
34 #include "SVF-LLVM/BasicTypes.h"
35 
36 namespace SVF
37 {
38 
39 class CHGraph;
40 /*
41  * Util class to assist pointer analysis for cpp programs
42  */
43 
44 namespace cppUtil
45 {
46 
48 {
52 };
53 
54 struct DemangledName demangle(const std::string& name);
55 
56 
57 Set<std::string> getClsNamesInBrackets(const std::string& name);
58 
59 std::string getBeforeBrackets(const std::string& name);
60 std::string getClassNameFromVtblObj(const std::string& vtblName);
61 
62 /*
63  * Get the vtable struct of a class.
64  *
65  * Given the class:
66  *
67  * class A {
68  * virtual ~A();
69  * };
70  * A::~A() = default;
71  *
72  * The corresponding vtable @_ZTV1A is of type:
73  *
74  * { [4 x i8*] }
75  *
76  * If the program has been compiled with AddressSanitizer,
77  * the vtable will have redzones and appear as:
78  *
79  * { { [4 x i8*] }, [32 x i8] }
80  *
81  * See https://github.com/SVF-tools/SVF/issues/1114 for more.
82  */
83 const ConstantStruct *getVtblStruct(const GlobalValue *vtbl);
84 
85 bool isValVtbl(const Value* val);
86 bool isVirtualCallSite(const CallBase* cs);
87 bool isConstructor(const Function* F);
88 bool isDestructor(const Function* F);
89 bool isCPPThunkFunction(const Function* F);
90 const Function* getThunkTarget(const Function* F);
91 
92 /*
93  * VtableA = {&A::foo}
94  * A::A(this){
95  * *this = &VtableA;
96  * }
97  *
98  *
99  * A* p = new A;
100  * cs: p->foo(...)
101  * ==>
102  * vtptr = *p;
103  * vfn = &vtptr[i]
104  * %funp = *vfn
105  * call %funp(p,...)
106  * getConstructorThisPtr(A) return "this" pointer
107  * getVCallThisPtr(cs) return p (this pointer)
108  * getVCallVtblPtr(cs) return vtptr
109  * getVCallIdx(cs) return i
110  * getClassNameFromVtblObj(VtableA) return
111  * getClassNameFromType(type of p) return type A
112  */
113 const Argument* getConstructorThisPtr(const Function* fun);
114 const Value* getVCallThisPtr(const CallBase* cs);
115 const Value* getVCallVtblPtr(const CallBase* cs);
116 s32_t getVCallIdx(const CallBase* cs);
117 bool classTyHasVTable(const StructType* ty);
118 std::string getClassNameFromType(const StructType* ty);
119 Set<std::string> getClassNameOfThisPtr(const CallBase* cs);
120 std::string getFunNameOfVCallSite(const CallBase* cs);
121 bool VCallInCtorOrDtor(const CallBase* cs);
122 
123 /*
124  * A(A* this){
125  * store this this.addr;
126  * tmp = load this.addr;
127  * this1 = bitcast(tmp);
128  * B(this1);
129  * }
130  * this and this1 are the same thisPtr in the constructor
131  */
132 bool isSameThisPtrInConstructor(const Argument* thisPtr1,
133  const Value* thisPtr2);
134 
136 Set<std::string> extractClsNamesFromFunc(const Function *foo);
137 
139 Set<std::string> extractClsNamesFromTemplate(const std::string &oname);
140 
143 bool isClsNameSource(const Value *val);
144 
146 bool matchesLabel(const std::string &foo, const std::string &label);
147 
149 bool isTemplateFunc(const Function *foo);
150 
152 bool isDynCast(const Function *foo);
153 
155 std::string extractClsNameFromDynCast(const CallBase* callBase);
156 
157 const Type *cppClsNameToType(const std::string &className);
158 
159 
160 
163 namespace ctir
164 {
168 const std::string derefMDName = "ctir";
171 const std::string vtMDName = "ctir.vt";
174 const std::string vtInitMDName = "ctir.vt.init";
175 
177 const uint32_t moduleFlagValue = 1;
178 } // namespace ctir
179 
180 } // End namespace cppUtil
181 
182 } // End namespace SVF
183 
184 #endif /* CPPUtil_H_ */
const char *const name
Definition: cJSON.h:264
const char *const string
Definition: cJSON.h:172
const std::string vtInitMDName
Definition: CppUtil.h:174
const uint32_t moduleFlagValue
Value we expect a ctir-annotated module to have.
Definition: CppUtil.h:177
const std::string derefMDName
Definition: CppUtil.h:168
const std::string vtMDName
Definition: CppUtil.h:171
std::string getFunNameOfVCallSite(const CallBase *cs)
Definition: CppUtil.cpp:635
std::string getBeforeBrackets(const std::string &name)
Definition: CppUtil.cpp:127
const Argument * getConstructorThisPtr(const Function *fun)
Definition: CppUtil.cpp:461
std::string extractClsNameFromDynCast(const CallBase *callBase)
extract class name from cpp dyncast function
Definition: CppUtil.cpp:921
s32_t getVCallIdx(const CallBase *cs)
Definition: CppUtil.cpp:646
const Value * getVCallVtblPtr(const CallBase *cs)
Definition: CppUtil.cpp:537
bool isTemplateFunc(const Function *foo)
whether foo is a cpp template function
Definition: CppUtil.cpp:894
bool classTyHasVTable(const StructType *ty)
Definition: CppUtil.cpp:569
bool isSameThisPtrInConstructor(const Argument *thisPtr1, const Value *thisPtr2)
Definition: CppUtil.cpp:437
Set< std::string > getClsNamesInBrackets(const std::string &name)
Definition: CppUtil.cpp:242
const Value * getVCallThisPtr(const CallBase *cs)
Definition: CppUtil.cpp:411
bool matchesLabel(const std::string &foo, const std::string &label)
whether foo matches the mangler label
Definition: CppUtil.cpp:883
std::string getClassNameFromType(const StructType *ty)
Definition: CppUtil.cpp:583
struct DemangledName demangle(const std::string &name)
Definition: CppUtil.cpp:195
Set< std::string > getClassNameOfThisPtr(const CallBase *cs)
Definition: CppUtil.cpp:601
bool isCPPThunkFunction(const Function *F)
Definition: CppUtil.cpp:383
bool VCallInCtorOrDtor(const CallBase *cs)
Definition: CppUtil.cpp:553
bool isClsNameSource(const Value *val)
Definition: CppUtil.cpp:861
Set< std::string > extractClsNamesFromTemplate(const std::string &oname)
extract class names from template functions
Definition: CppUtil.cpp:821
bool isVirtualCallSite(const CallBase *cs)
Definition: CppUtil.cpp:352
const Type * cppClsNameToType(const std::string &className)
Definition: CppUtil.cpp:937
const Function * getThunkTarget(const Function *F)
Definition: CppUtil.cpp:389
const ConstantStruct * getVtblStruct(const GlobalValue *vtbl)
Definition: CppUtil.cpp:323
bool isConstructor(const Function *F)
Definition: CppUtil.cpp:489
std::string getClassNameFromVtblObj(const std::string &vtblName)
Definition: CppUtil.cpp:304
Set< std::string > extractClsNamesFromFunc(const Function *foo)
extract class name from the c++ function name, e.g., constructor/destructors
Definition: CppUtil.cpp:706
bool isValVtbl(const Value *val)
Definition: CppUtil.cpp:336
bool isDestructor(const Function *F)
Definition: CppUtil.cpp:509
bool isDynCast(const Function *foo)
whether foo is a cpp dyncast function
Definition: CppUtil.cpp:910
for isBitcode
Definition: BasicTypes.h:68
llvm::Type Type
Definition: BasicTypes.h:83
llvm::ConstantStruct ConstantStruct
Definition: BasicTypes.h:106
llvm::Argument Argument
Definition: BasicTypes.h:145
llvm::Function Function
Definition: BasicTypes.h:85
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82
signed s32_t
Definition: GeneralType.h:47
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96