Static Value-Flow Analysis
Loading...
Searching...
No Matches
LLVMUtil.h
Go to the documentation of this file.
1//===- LLVMUtil.h -- Analysis helper functions----------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <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 * LLVMUtil.h
25 *
26 * Created on: Apr 11, 2013
27 * Author: Yulei Sui
28 */
29
30#ifndef INCLUDE_SVF_FE_LLVMUTIL_H_
31#define INCLUDE_SVF_FE_LLVMUTIL_H_
32
33#include "Util/SVFUtil.h"
34#include "SVF-LLVM/BasicTypes.h"
35#include "SVFIR/SVFValue.h"
36#include "Util/ThreadAPI.h"
37
38namespace SVF
39{
40
41namespace LLVMUtil
42{
43
45inline bool isCallSite(const Instruction* inst)
46{
47 return SVFUtil::isa<CallBase>(inst);
48}
50inline bool isCallSite(const Value* val)
51{
52 return SVFUtil::isa<CallBase>(val);
53}
54
55
57inline const CallBase* getLLVMCallSite(const Value* value)
58{
59 assert(isCallSite(value) && "not a callsite?");
60 return SVFUtil::cast<CallBase>(value);
61}
62
63inline const Function* getCallee(const CallBase* cs)
64{
65 // FIXME: do we need to strip-off the casts here to discover more library functions
66 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
67}
68
70inline const Function* getLLVMFunction(const Value* val)
71{
72 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
73}
74
76const Function* getProgFunction(const std::string& funName);
77
79inline bool isProgEntryFunction(const Function* fun)
80{
81 return fun && fun->getName() == "main";
82}
83
85inline bool isBlackholeSym(const Value* val)
86{
87 return SVFUtil::isa<UndefValue>(val);
88}
89
91inline bool isNullPtrSym(const Value* val)
92{
93 return SVFUtil::dyn_cast<ConstantPointerNull>(val);
94}
95
96static inline Type* getPtrElementType(const PointerType* pty)
97{
98#if (LLVM_VERSION_MAJOR < 14)
99 return pty->getPointerElementType();
100#elif (LLVM_VERSION_MAJOR < 17)
101 assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
102 return pty->getNonOpaquePointerElementType();
103#else
104 assert(false && "llvm version 17+ only support opaque pointers!");
105#endif
106}
107
110
111
113bool isObject(const Value* ref);
114
117
118
119bool isUncalledFunction(const Function* fun);
120
122inline bool ArgInDeadFunction(const Value* val)
123{
124 return SVFUtil::isa<Argument>(val)
125 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
126}
128
131{
132 return SVFUtil::isa<Argument>(val) &&
134 SVFUtil::cast<Argument>(val)->getParent());
135}
137bool isPtrInUncalledFunction(const Value* value);
139
141
143
144
145inline bool isNoCallerFunction(const Function* fun)
146{
148}
149
152{
153 return SVFUtil::isa<Argument>(val)
154 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
155}
157
159bool basicBlockHasRetInst(const BasicBlock* bb);
160
163bool functionDoesNotRet(const Function* fun);
164
167 std::vector<const SVFBasicBlock*>& bbs);
168
170const Value* stripConstantCasts(const Value* val);
171
173const Value* stripAllCasts(const Value* val);
174
178
180
182{
183 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
184 {
185 if (constExpr->getOpcode() == Instruction::GetElementPtr)
186 return constExpr;
187 }
188 return nullptr;
189}
190
192{
193 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
194 {
195 if (constExpr->getOpcode() == Instruction::IntToPtr)
196 return constExpr;
197 }
198 return nullptr;
199}
200
202{
203 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
204 {
205 if (constExpr->getOpcode() == Instruction::PtrToInt)
206 return constExpr;
207 }
208 return nullptr;
209}
210
212{
213 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
214 {
215 if (constExpr->getOpcode() == Instruction::BitCast)
216 return constExpr;
217 }
218 return nullptr;
219}
220
222{
223 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
224 {
225 if (constExpr->getOpcode() == Instruction::Select)
226 return constExpr;
227 }
228 return nullptr;
229}
230
232{
233 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
234 {
235 if (constExpr->getOpcode() == Instruction::Trunc ||
236 constExpr->getOpcode() == Instruction::FPTrunc ||
237 constExpr->getOpcode() == Instruction::ZExt ||
238 constExpr->getOpcode() == Instruction::SExt ||
239 constExpr->getOpcode() == Instruction::FPExt)
240 return constExpr;
241 }
242 return nullptr;
243}
244
246{
247 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
248 {
249 if (constExpr->getOpcode() == Instruction::ICmp ||
250 constExpr->getOpcode() == Instruction::FCmp)
251 return constExpr;
252 }
253 return nullptr;
254}
255
257{
258 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
259 {
260 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
261 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
262 return constExpr;
263 }
264 return nullptr;
265}
266
268{
269 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
270 {
271 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
272 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
273 return constExpr;
274 }
275 return nullptr;
276}
278
279inline static DataLayout* getDataLayout(Module* mod)
280{
281 static DataLayout *dl = nullptr;
282 if (dl == nullptr)
283 dl = new DataLayout(mod);
284 return dl;
285}
286
289 std::vector<const Instruction*>& instList);
290
291
298{
299 return bb != &bb->getParent()->getEntryBlock() &&
300 pred_empty(bb);
301}
302
304bool isIRFile(const std::string& filename);
305
307void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
308 std::vector<std::string>& moduleNameVec);
309
311
312const std::string getSourceLoc(const Value* val);
313const std::string getSourceLocOfFunction(const Function* F);
314
315bool isIntrinsicInst(const Instruction* inst);
316bool isIntrinsicFun(const Function* func);
317
319std::vector<const Function *> getCalledFunctions(const Function *F);
320// Converts a mangled name to C naming style to match functions in extapi.c.
321std::string restoreFuncName(std::string funcName);
322
324const SVFFunction* getFunction(const std::string& name);
325
332
334const Value* getGlobalRep(const Value* val);
335
337bool isConstantObjSym(const SVFValue* val);
338
340bool isConstantObjSym(const Value* val);
341
342// Dump Control Flow Graph of llvm function, with instructions
343void viewCFG(const Function* fun);
344
345// Dump Control Flow Graph of llvm function, without instructions
346void viewCFGOnly(const Function* fun);
347
348std::string dumpValue(const Value* val);
349
350std::string dumpType(const Type* type);
351
352std::string dumpValueAndDbgInfo(const Value* val);
353
354bool isHeapAllocExtCallViaRet(const Instruction *inst);
355
356bool isHeapAllocExtCallViaArg(const Instruction *inst);
357
358inline bool isHeapAllocExtCall(const Instruction *inst)
359{
361}
362
363bool isStackAllocExtCallViaRet(const Instruction *inst);
364
365inline bool isStackAllocExtCall(const Instruction *inst)
366{
367 return isStackAllocExtCallViaRet(inst);
368}
369
370// Check if a given value represents a heap object.
371bool isHeapObj(const Value* val);
372
373// Check if a given value represents a stack object.
374bool isStackObj(const Value* val);
375
377bool isNonInstricCallSite(const Instruction* inst);
378
381{
382 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
383 {
384 const Function *fun = &(*it);
385 if (isProgEntryFunction(fun))
386 return (fun);
387 }
388 return nullptr;
389}
390
391} // End namespace LLVMUtil
392
393} // End namespace SVF
394
395#endif /* INCLUDE_SVF_FE_LLVMUTIL_H_ */
#define F(f)
newitem type
Definition cJSON.cpp:2739
const char *const name
Definition cJSON.h:264
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition LLVMUtil.cpp:203
const Function * getProgFunction(const std::string &funName)
Get program entry function from module.
Definition LLVMUtil.cpp:39
const Value * stripConstantCasts(const Value *val)
Strip off the constant casts.
Definition LLVMUtil.cpp:219
bool isPtrInUncalledFunction(const Value *value)
Return true if this is value in a dead function (function without any caller)
Definition LLVMUtil.cpp:175
bool isHeapAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:617
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition LLVMUtil.h:145
const Value * getFirstUseViaCastInst(const Value *val)
Definition LLVMUtil.cpp:278
const CallBase * getLLVMCallSite(const Value *value)
Return LLVM callsite given a value.
Definition LLVMUtil.h:57
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:256
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:358
void viewCFGOnly(const Function *fun)
Definition LLVMUtil.cpp:239
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:533
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition LLVMUtil.cpp:158
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
Definition LLVMUtil.cpp:250
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition LLVMUtil.h:191
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition LLVMUtil.h:151
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:221
bool isIntrinsicFun(const Function *func)
Definition LLVMUtil.cpp:190
std::vector< const Function * > getCalledFunctions(const Function *F)
Get all called funcions in a parent function.
Definition LLVMUtil.cpp:364
bool isNoPrecessorBasicBlock(const BasicBlock *bb)
Definition LLVMUtil.h:297
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition LLVMUtil.h:45
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:365
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition LLVMUtil.h:122
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:122
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:231
std::string dumpType(const Type *type)
Definition LLVMUtil.cpp:595
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition LLVMUtil.cpp:550
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:91
std::string dumpValueAndDbgInfo(const Value *val)
Definition LLVMUtil.cpp:606
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:327
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:429
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:201
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:669
const Value * getGlobalRep(const Value *val)
find the unique defined global across multiple modules
Definition LLVMUtil.cpp:416
const ConstantExpr * isUnaryConstantExpr(const Value *val)
Definition LLVMUtil.h:267
void getFunReachableBBs(const Function *svfFun, std::vector< const SVFBasicBlock * > &bbs)
Get reachable basic block from function entry.
Definition LLVMUtil.cpp:74
const ConstantExpr * isCastConstantExpr(const Value *val)
Definition LLVMUtil.h:211
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
Definition LLVMUtil.cpp:296
const Function * getProgEntryFunction(Module &module)
Get program entry function from module.
Definition LLVMUtil.h:380
const SVFFunction * getFunction(const std::string &name)
Get the corresponding Function based on its name.
Definition LLVMUtil.cpp:411
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition LLVMUtil.cpp:108
void viewCFG(const Function *fun)
Definition LLVMUtil.cpp:231
bool isBlackholeSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:85
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:691
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:633
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition LLVMUtil.h:79
bool isObject(const Value *ref)
Return true if this value refers to a object.
Definition LLVMUtil.cpp:59
void processArguments(int argc, char **argv, int &arg_num, char **arg_value, std::vector< std::string > &moduleNameVec)
Parse argument for multi-module analysis.
Definition LLVMUtil.cpp:336
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
Definition LLVMUtil.cpp:315
bool isConstantObjSym(const SVFValue *val)
Check whether this value points-to a constant object.
Definition LLVMUtil.cpp:578
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main)
Definition LLVMUtil.h:130
static Type * getPtrElementType(const PointerType *pty)
Definition LLVMUtil.h:96
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:648
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition LLVMUtil.h:70
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition LLVMUtil.h:181
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:279
std::string restoreFuncName(std::string funcName)
Definition LLVMUtil.cpp:383
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:63
bool isNonInstricCallSite(const Instruction *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition LLVMUtil.cpp:707
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:584
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:245
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
for isBitcode
Definition BasicTypes.h:68
llvm::DataLayout DataLayout
Definition BasicTypes.h:108
llvm::Type Type
Definition BasicTypes.h:83
llvm::CallBase CallBase
Definition BasicTypes.h:146
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:86
llvm::Function Function
Definition BasicTypes.h:85
llvm::ConstantData ConstantData
Definition BasicTypes.h:116
llvm::MetadataAsValue MetadataAsValue
Definition BasicTypes.h:102
llvm::Instruction Instruction
Definition BasicTypes.h:87
llvm::ConstantAggregate ConstantAggregate
Definition BasicTypes.h:117
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:82
llvm::ConstantExpr ConstantExpr
Definition BasicTypes.h:120
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
llvm::BlockAddress BlockAddress
Definition BasicTypes.h:91
llvm::Module Module
Definition BasicTypes.h:84
llvm::PointerType PointerType
Definition BasicTypes.h:96
unsigned u32_t
Definition GeneralType.h:46