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
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 */
120const ConstantStruct *getVtblStruct(const GlobalValue *vtbl);
121
123bool isValVtbl(const Value* val);
124bool isVirtualCallSite(const CallBase* cs);
125bool isConstructor(const Function* F);
126bool isDestructor(const Function* F);
127bool isCPPThunkFunction(const Function* F);
128const Function* getThunkTarget(const Function* F);
129
130/*
131 * VtableA = {&A::foo}
132 * A::A(this){
133 * *this = &VtableA;
134 * }
135 *
136 *
137 * A* p = new A;
138 * cs: p->foo(...)
139 * ==>
140 * vtptr = *p;
141 * vfn = &vtptr[i]
142 * %funp = *vfn
143 * call %funp(p,...)
144 * getConstructorThisPtr(A) return "this" pointer
145 * getVCallThisPtr(cs) return p (this pointer)
146 * getVCallVtblPtr(cs) return vtptr
147 * getVCallIdx(cs) return i
148 * getClassNameFromVtblObj(VtableA) return
149 * getClassNameFromType(type of p) return type A
150 */
151const Argument* getConstructorThisPtr(const Function* fun);
152const Value* getVCallThisPtr(const CallBase* cs);
153const Value* getVCallVtblPtr(const CallBase* cs);
154s32_t getVCallIdx(const CallBase* cs);
155bool classTyHasVTable(const StructType* ty);
156std::string getClassNameFromType(const StructType* ty);
157Set<std::string> getClassNameOfThisPtr(const CallBase* cs);
158std::string getFunNameOfVCallSite(const CallBase* cs);
159bool VCallInCtorOrDtor(const CallBase* cs);
160
161/*
162 * A(A* this){
163 * store this this.addr;
164 * tmp = load this.addr;
165 * this1 = bitcast(tmp);
166 * B(this1);
167 * }
168 * this and this1 are the same thisPtr in the constructor
169 */
170bool isSameThisPtrInConstructor(const Argument* thisPtr1,
171 const Value* thisPtr2);
172
174Set<std::string> extractClsNamesFromFunc(const Function *foo);
175
177Set<std::string> extractClsNamesFromTemplate(const std::string &oname);
178
181bool isClsNameSource(const Value *val);
182
184bool matchesLabel(const std::string &foo, const std::string &label);
185
187bool isTemplateFunc(const Function *foo);
188
190bool isDynCast(const Function *foo);
191
193std::string extractClsNameFromDynCast(const CallBase* callBase);
194
195const Type *cppClsNameToType(const std::string &className);
196} // End namespace cppUtil
197
198} // End namespace SVF
199
200#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:1050
bool isVtable(const std::string &name) override
Definition CppUtil.cpp:1006
bool isDestructor(const std::string &name) override
Definition CppUtil.cpp:1032
bool isConstructor(const std::string &name) override
Definition CppUtil.cpp:1017
DemangledName demangle(const std::string &name) override
Definition CppUtil.cpp:1065
bool isTypeInfo(const std::string &name) override
Definition CppUtil.cpp:1012
bool isTypeInfo(const std::string &name) override
Definition CppUtil.cpp:1112
DemangledName demangle(const std::string &name) override
Definition CppUtil.cpp:1147
bool isDestructor(const std::string &name) override
Definition CppUtil.cpp:1122
bool isVtable(const std::string &name) override
Definition CppUtil.cpp:1107
bool isConstructor(const std::string &name) override
Definition CppUtil.cpp:1117
std::string extractClassName(const std::string &name) override
Definition CppUtil.cpp:1127
std::string getFunNameOfVCallSite(const CallBase *cs)
Definition CppUtil.cpp:647
std::string getBeforeBrackets(const std::string &name)
Definition CppUtil.cpp:127
const Argument * getConstructorThisPtr(const Function *fun)
Definition CppUtil.cpp:465
std::string extractClsNameFromDynCast(const CallBase *callBase)
extract class name from cpp dyncast function
Definition CppUtil.cpp:930
s32_t getVCallIdx(const CallBase *cs)
Definition CppUtil.cpp:658
const Value * getVCallVtblPtr(const CallBase *cs)
Definition CppUtil.cpp:549
bool isTemplateFunc(const Function *foo)
whether foo is a cpp template function
Definition CppUtil.cpp:905
bool classTyHasVTable(const StructType *ty)
Definition CppUtil.cpp:581
bool isSameThisPtrInConstructor(const Argument *thisPtr1, const Value *thisPtr2)
Definition CppUtil.cpp:421
Set< std::string > getClsNamesInBrackets(const std::string &name)
Definition CppUtil.cpp:202
const Value * getVCallThisPtr(const CallBase *cs)
Definition CppUtil.cpp:348
bool matchesLabel(const std::string &foo, const std::string &label)
whether foo matches the mangler label
Definition CppUtil.cpp:894
std::string getClassNameFromType(const StructType *ty)
Definition CppUtil.cpp:595
struct DemangledName demangle(const std::string &name)
Definition CppUtil.cpp:195
Set< std::string > getClassNameOfThisPtr(const CallBase *cs)
Definition CppUtil.cpp:613
bool isCPPThunkFunction(const Function *F)
Definition CppUtil.cpp:320
bool VCallInCtorOrDtor(const CallBase *cs)
Definition CppUtil.cpp:565
bool isClsNameSource(const Value *val)
Definition CppUtil.cpp:872
CXXABI * getCXXABI()
Definition CppUtil.cpp:994
Set< std::string > extractClsNamesFromTemplate(const std::string &oname)
extract class names from template functions
Definition CppUtil.cpp:832
bool isVirtualCallSite(const CallBase *cs)
Definition CppUtil.cpp:289
const Type * cppClsNameToType(const std::string &className)
Definition CppUtil.cpp:946
const Function * getThunkTarget(const Function *F)
Definition CppUtil.cpp:326
const ConstantStruct * getVtblStruct(const GlobalValue *vtbl)
Definition CppUtil.cpp:261
bool isConstructor(const Function *F)
Definition CppUtil.cpp:501
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:718
bool isValVtbl(const Value *val)
A carefully crafted input such as FakeVTable _ZTV1A = {{nullptr, nullptr}}; can forge a vtable; detec...
Definition CppUtil.cpp:274
bool isDestructor(const Function *F)
Definition CppUtil.cpp:521
bool isDynCast(const Function *foo)
whether foo is a cpp dyncast function
Definition CppUtil.cpp:920
for isBitcode
Definition BasicTypes.h:70
llvm::Type Type
Definition BasicTypes.h:87
llvm::ConstantStruct ConstantStruct
Definition BasicTypes.h:110
llvm::Argument Argument
Definition BasicTypes.h:152
llvm::Function Function
Definition BasicTypes.h:89
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:86
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
signed s32_t
Definition GeneralType.h:68
llvm::Module Module
Definition BasicTypes.h:88
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51