Static Value-Flow Analysis
Loading...
Searching...
No Matches
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
36namespace SVF
37{
38
39class CHGraph;
40/*
41 * Util class to assist pointer analysis for cpp programs
42 */
43
44namespace cppUtil
45{
46
48{
49 std::string className;
50 std::string funcName;
52};
53
55
56
57Set<std::string> getClsNamesInBrackets(const std::string& name);
58
59std::string getBeforeBrackets(const std::string& name);
60std::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 */
83const ConstantStruct *getVtblStruct(const GlobalValue *vtbl);
84
85bool isValVtbl(const Value* val);
86bool isVirtualCallSite(const CallBase* cs);
87bool isConstructor(const Function* F);
88bool isDestructor(const Function* F);
89bool isCPPThunkFunction(const Function* F);
90const 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 */
113const Argument* getConstructorThisPtr(const Function* fun);
114const Value* getVCallThisPtr(const CallBase* cs);
115const Value* getVCallVtblPtr(const CallBase* cs);
116s32_t getVCallIdx(const CallBase* cs);
117bool classTyHasVTable(const StructType* ty);
118std::string getClassNameFromType(const StructType* ty);
119Set<std::string> getClassNameOfThisPtr(const CallBase* cs);
120std::string getFunNameOfVCallSite(const CallBase* cs);
121bool 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 */
132bool isSameThisPtrInConstructor(const Argument* thisPtr1,
133 const Value* thisPtr2);
134
136Set<std::string> extractClsNamesFromFunc(const Function *foo);
137
139Set<std::string> extractClsNamesFromTemplate(const std::string &oname);
140
143bool isClsNameSource(const Value *val);
144
146bool matchesLabel(const std::string &foo, const std::string &label);
147
149bool isTemplateFunc(const Function *foo);
150
152bool isDynCast(const Function *foo);
153
155std::string extractClsNameFromDynCast(const CallBase* callBase);
156
157const Type *cppClsNameToType(const std::string &className);
158
159
160
164{
168const std::string derefMDName = "ctir";
171const std::string vtMDName = "ctir.vt";
174const std::string vtInitMDName = "ctir.vt.init";
175
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:918
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:893
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:882
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:860
Set< std::string > extractClsNamesFromTemplate(const std::string &oname)
extract class names from template functions
Definition CppUtil.cpp:820
bool isVirtualCallSite(const CallBase *cs)
Definition CppUtil.cpp:352
const Type * cppClsNameToType(const std::string &className)
Definition CppUtil.cpp:934
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:908
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
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:47
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:96