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 "SVF-LLVM/BasicTypes.h"
34#include "Util/GeneralType.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
54class CXXABI
55{
56public:
57 virtual ~CXXABI() = default;
58 virtual bool isVtable(const std::string& name) = 0;
59 virtual bool isTypeInfo(const std::string& name) = 0;
60 virtual bool isConstructor(const std::string& name) = 0;
61 virtual bool isDestructor(const std::string& name) = 0;
62 virtual std::string extractClassName(const std::string& name) = 0;
63 virtual DemangledName demangle(const std::string& name) = 0;
64};
65
66class ItaniumABI : public CXXABI
67{
68public:
69 bool isVtable(const std::string& name) override;
70 bool isTypeInfo(const std::string& name) override;
71 bool isConstructor(const std::string& name) override;
72 bool isDestructor(const std::string& name) override;
73 std::string extractClassName(const std::string& name) override;
74 DemangledName demangle(const std::string& name) override;
75};
76
77class MSVCABI : public CXXABI
78{
79public:
80 bool isVtable(const std::string& name) override;
81 bool isTypeInfo(const std::string& name) override;
82 bool isConstructor(const std::string& name) override;
83 bool isDestructor(const std::string& name) override;
84 std::string extractClassName(const std::string& name) override;
85 DemangledName demangle(const std::string& name) override;
86};
87
89CXXABI* getCXXABI(const Module* M);
90CXXABI* getCXXABI(const Value* val);
91
92struct DemangledName demangle(const std::string& name);
93
94Set<std::string> getClsNamesInBrackets(const std::string& name);
95
96std::string getBeforeBrackets(const std::string& name);
97std::string getClassNameFromVtblObj(const std::string& vtblName);
98
99/*
100 * Get the vtable struct of a class.
101 *
102 * Given the class:
103 *
104 * class A {
105 * virtual ~A();
106 * };
107 * A::~A() = default;
108 *
109 * The corresponding vtable @_ZTV1A is of type:
110 *
111 * { [4 x i8*] }
112 *
113 * If the program has been compiled with AddressSanitizer,
114 * the vtable will have redzones and appear as:
115 *
116 * { { [4 x i8*] }, [32 x i8] }
117 *
118 * See https://github.com/SVF-tools/SVF/issues/1114 for more.
119 *
120 * Flat arrays are returned in a synthetic struct without changing the global.
121 * Returns nullptr for declarations or unsupported initializer layouts.
122 */
123const ConstantStruct *getVtblStruct(const GlobalValue *vtbl);
124
126bool isValVtbl(const Value* val);
127bool isVirtualCallSite(const CallBase* cs);
128bool isConstructor(const Function* F);
129bool isDestructor(const Function* F);
130bool isCPPThunkFunction(const Function* F);
131const Function* getThunkTarget(const Function* F);
132
133/*
134 * VtableA = {&A::foo}
135 * A::A(this){
136 * *this = &VtableA;
137 * }
138 *
139 *
140 * A* p = new A;
141 * cs: p->foo(...)
142 * ==>
143 * vtptr = *p;
144 * vfn = &vtptr[i]
145 * %funp = *vfn
146 * call %funp(p,...)
147 * getConstructorThisPtr(A) return "this" pointer
148 * getVCallThisPtr(cs) return p (this pointer)
149 * getVCallVtblPtr(cs) return vtptr
150 * getVCallIdx(cs) return i
151 * getClassNameFromVtblObj(VtableA) return
152 * getClassNameFromType(type of p) return type A
153 */
154const Argument* getConstructorThisPtr(const Function* fun);
155const Value* getVCallThisPtr(const CallBase* cs);
156const Value* getVCallVtblPtr(const CallBase* cs);
157s32_t getVCallIdx(const CallBase* cs);
158bool classTyHasVTable(const StructType* ty);
159std::string getClassNameFromType(const StructType* ty);
160Set<std::string> getClassNameOfThisPtr(const CallBase* cs);
161std::string getFunNameOfVCallSite(const CallBase* cs);
162bool VCallInCtorOrDtor(const CallBase* cs);
163
164/*
165 * A(A* this){
166 * store this this.addr;
167 * tmp = load this.addr;
168 * this1 = bitcast(tmp);
169 * B(this1);
170 * }
171 * this and this1 are the same thisPtr in the constructor
172 */
173bool isSameThisPtrInConstructor(const Argument* thisPtr1,
174 const Value* thisPtr2);
175
177Set<std::string> extractClsNamesFromFunc(const Function *foo);
178
180Set<std::string> extractClsNamesFromTemplate(const std::string &oname);
181
184bool isClsNameSource(const Value *val);
185
187bool matchesLabel(const std::string &foo, const std::string &label);
188
190bool isTemplateFunc(const Function *foo);
191
193bool isDynCast(const Function *foo);
194
196std::string extractClsNameFromDynCast(const CallBase* callBase);
197
198const Type *cppClsNameToType(const std::string &className);
199} // End namespace cppUtil
200
201} // End namespace SVF
202
203#endif /* CPPUtil_H_ */
const char *const name
Definition cJSON.h:264
const char *const string
Definition cJSON.h:172
virtual ~CXXABI()=default
virtual DemangledName demangle(const std::string &name)=0
virtual std::string extractClassName(const std::string &name)=0
virtual bool isTypeInfo(const std::string &name)=0
virtual bool isConstructor(const std::string &name)=0
virtual bool isVtable(const std::string &name)=0
virtual bool isDestructor(const std::string &name)=0
std::string extractClassName(const std::string &name) override
Definition CppUtil.cpp:1071
bool isVtable(const std::string &name) override
Definition CppUtil.cpp:1027
bool isDestructor(const std::string &name) override
Definition CppUtil.cpp:1053
bool isConstructor(const std::string &name) override
Definition CppUtil.cpp:1038
DemangledName demangle(const std::string &name) override
Definition CppUtil.cpp:1086
bool isTypeInfo(const std::string &name) override
Definition CppUtil.cpp:1033
bool isTypeInfo(const std::string &name) override
Definition CppUtil.cpp:1133
DemangledName demangle(const std::string &name) override
Definition CppUtil.cpp:1168
bool isDestructor(const std::string &name) override
Definition CppUtil.cpp:1143
bool isVtable(const std::string &name) override
Definition CppUtil.cpp:1128
bool isConstructor(const std::string &name) override
Definition CppUtil.cpp:1138
std::string extractClassName(const std::string &name) override
Definition CppUtil.cpp:1148
std::string getFunNameOfVCallSite(const CallBase *cs)
Definition CppUtil.cpp:668
std::string getBeforeBrackets(const std::string &name)
Definition CppUtil.cpp:127
const Argument * getConstructorThisPtr(const Function *fun)
Definition CppUtil.cpp:486
std::string extractClsNameFromDynCast(const CallBase *callBase)
extract class name from cpp dyncast function
Definition CppUtil.cpp:951
s32_t getVCallIdx(const CallBase *cs)
Definition CppUtil.cpp:679
const Value * getVCallVtblPtr(const CallBase *cs)
Definition CppUtil.cpp:570
bool isTemplateFunc(const Function *foo)
whether foo is a cpp template function
Definition CppUtil.cpp:926
bool classTyHasVTable(const StructType *ty)
Definition CppUtil.cpp:602
bool isSameThisPtrInConstructor(const Argument *thisPtr1, const Value *thisPtr2)
Definition CppUtil.cpp:442
Set< std::string > getClsNamesInBrackets(const std::string &name)
Definition CppUtil.cpp:202
const Value * getVCallThisPtr(const CallBase *cs)
Definition CppUtil.cpp:369
bool matchesLabel(const std::string &foo, const std::string &label)
whether foo matches the mangler label
Definition CppUtil.cpp:915
std::string getClassNameFromType(const StructType *ty)
Definition CppUtil.cpp:616
struct DemangledName demangle(const std::string &name)
Definition CppUtil.cpp:195
Set< std::string > getClassNameOfThisPtr(const CallBase *cs)
Definition CppUtil.cpp:634
bool isCPPThunkFunction(const Function *F)
Definition CppUtil.cpp:341
bool VCallInCtorOrDtor(const CallBase *cs)
Definition CppUtil.cpp:586
bool isClsNameSource(const Value *val)
Definition CppUtil.cpp:893
CXXABI * getCXXABI()
Definition CppUtil.cpp:1015
Set< std::string > extractClsNamesFromTemplate(const std::string &oname)
extract class names from template functions
Definition CppUtil.cpp:853
bool isVirtualCallSite(const CallBase *cs)
Definition CppUtil.cpp:310
const Type * cppClsNameToType(const std::string &className)
Definition CppUtil.cpp:967
const Function * getThunkTarget(const Function *F)
Definition CppUtil.cpp:347
const ConstantStruct * getVtblStruct(const GlobalValue *vtbl)
Definition CppUtil.cpp:261
bool isConstructor(const Function *F)
Definition CppUtil.cpp:522
std::string getClassNameFromVtblObj(const std::string &vtblName)
Definition CppUtil.cpp:256
Set< std::string > extractClsNamesFromFunc(const Function *foo)
extract class name from the c++ function name, e.g., constructor/destructors
Definition CppUtil.cpp:739
bool isValVtbl(const Value *val)
A carefully crafted input such as FakeVTable _ZTV1A = {{nullptr, nullptr}}; can forge a vtable; detec...
Definition CppUtil.cpp:295
bool isDestructor(const Function *F)
Definition CppUtil.cpp:542
bool isDynCast(const Function *foo)
whether foo is a cpp dyncast function
Definition CppUtil.cpp:941
for isBitcode
Definition BasicTypes.h:70
llvm::Type Type
Definition BasicTypes.h:81
llvm::ConstantStruct ConstantStruct
Definition BasicTypes.h:104
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
llvm::Argument Argument
Definition BasicTypes.h:146
llvm::Function Function
Definition BasicTypes.h:83
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:80
signed s32_t
Definition GeneralType.h:68
llvm::Module Module
Definition BasicTypes.h:82