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 "Util/ThreadAPI.h"
36#include "Util/Options.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
55inline double getDoubleValue(const ConstantFP* fpValue)
56{
57 double dval = 0;
58 if (fpValue->isNormalFP())
59 {
60 const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
61 if (&semantics == &llvm::APFloat::IEEEhalf() ||
62 &semantics == &llvm::APFloat::IEEEsingle() ||
63 &semantics == &llvm::APFloat::IEEEdouble() ||
64 &semantics == &llvm::APFloat::IEEEquad() ||
65 &semantics == &llvm::APFloat::x87DoubleExtended())
66 {
67 dval = fpValue->getValueAPF().convertToDouble();
68 }
69 else
70 {
71 assert (false && "Unsupported floating point type");
72 abort();
73 }
74 }
75 else
76 {
77 // other cfp type, like isZero(), isInfinity(), isNegative(), etc.
78 // do nothing
79 }
80 return dval;
81}
82
83inline std::pair<s64_t, u64_t> getIntegerValue(const ConstantInt* intValue)
84{
85 if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
86 return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
87 else
88 return std::make_pair(0,0);
89}
90
92inline const CallBase* getLLVMCallSite(const Value* value)
93{
94 assert(isCallSite(value) && "not a callsite?");
95 return SVFUtil::cast<CallBase>(value);
96}
97
98inline const Function* getCallee(const CallBase* cs)
99{
100 // FIXME: do we need to strip-off the casts here to discover more library functions
101 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
102}
103
105inline const Function* getLLVMFunction(const Value* val)
106{
107 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
108}
109
111const Function* getProgFunction(const std::string& funName);
112
114inline bool isProgEntryFunction(const Function* fun)
115{
116 const char* main_name=Options::SVFMain() ? "svf.main" : "main";
117 return fun && fun->getName() == main_name;
118}
119
121inline bool isBlackholeSym(const Value* val)
122{
123 return SVFUtil::isa<UndefValue>(val);
124}
125
127inline bool isNullPtrSym(const Value* val)
128{
129 return SVFUtil::dyn_cast<ConstantPointerNull>(val);
130}
131
132static inline Type* getPtrElementType(const PointerType* pty)
133{
134#if (LLVM_VERSION_MAJOR < 14)
135 return pty->getPointerElementType();
136#elif (LLVM_VERSION_MAJOR < 17)
137 assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
138 return pty->getNonOpaquePointerElementType();
139#else
140 (void)pty; // Suppress warning of unused variable under release build
141 assert(false && "llvm version 17+ only support opaque pointers!");
142 return nullptr;
143#endif
144}
145
148
149
151bool isObject(const Value* ref);
152
155
156
157bool isUncalledFunction(const Function* fun);
158
160inline bool ArgInDeadFunction(const Value* val)
161{
162 return SVFUtil::isa<Argument>(val)
163 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
164}
166
169{
170 return SVFUtil::isa<Argument>(val) &&
172 SVFUtil::cast<Argument>(val)->getParent());
173}
175bool isPtrInUncalledFunction(const Value* value);
177
179
181
182
183inline bool isNoCallerFunction(const Function* fun)
184{
186}
187
190{
191 return SVFUtil::isa<Argument>(val)
192 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
193}
195
197bool basicBlockHasRetInst(const BasicBlock* bb);
198
201bool functionDoesNotRet(const Function* fun);
202
205 std::vector<const SVFBasicBlock*>& bbs);
206
208const Value* stripConstantCasts(const Value* val);
209
211const Value* stripAllCasts(const Value* val);
212
216
218
220{
221 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
222 {
223 if (constExpr->getOpcode() == Instruction::GetElementPtr)
224 return constExpr;
225 }
226 return nullptr;
227}
228
230{
231 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
232 {
233 if (constExpr->getOpcode() == Instruction::IntToPtr)
234 return constExpr;
235 }
236 return nullptr;
237}
238
240{
241 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
242 {
243 if (constExpr->getOpcode() == Instruction::PtrToInt)
244 return constExpr;
245 }
246 return nullptr;
247}
248
250{
251 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
252 {
253 if (constExpr->getOpcode() == Instruction::BitCast)
254 return constExpr;
255 }
256 return nullptr;
257}
258
260{
261 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
262 {
263 if (constExpr->getOpcode() == Instruction::Select)
264 return constExpr;
265 }
266 return nullptr;
267}
268
270{
271 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
272 {
273 if (constExpr->getOpcode() == Instruction::Trunc ||
274 constExpr->getOpcode() == Instruction::FPTrunc ||
275 constExpr->getOpcode() == Instruction::ZExt ||
276 constExpr->getOpcode() == Instruction::SExt ||
277 constExpr->getOpcode() == Instruction::FPExt)
278 return constExpr;
279 }
280 return nullptr;
281}
282
284{
285 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
286 {
287 if (constExpr->getOpcode() == Instruction::ICmp ||
288 constExpr->getOpcode() == Instruction::FCmp)
289 return constExpr;
290 }
291 return nullptr;
292}
293
295{
296 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
297 {
298 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
299 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
300 return constExpr;
301 }
302 return nullptr;
303}
304
306{
307 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
308 {
309 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
310 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
311 return constExpr;
312 }
313 return nullptr;
314}
316
318{
319 static DataLayout *dl = nullptr;
320 if (dl == nullptr)
321#if LLVM_VERSION_MAJOR >= 19
322 dl = new DataLayout(mod->getDataLayout());
323#else
324 dl = new DataLayout(mod);
325#endif
326 return dl;
327}
328
331 std::vector<const Instruction*>& instList);
332
333
340{
341 return bb != &bb->getParent()->getEntryBlock() &&
342 pred_empty(bb);
343}
344
346bool isIRFile(const std::string& filename);
347
349void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
350 std::vector<std::string>& moduleNameVec);
351
353
354const std::string getSourceLoc(const Value* val);
355const std::string getSourceLocOfFunction(const Function* F);
356
357bool isIntrinsicInst(const Instruction* inst);
358bool isIntrinsicFun(const Function* func);
359
361std::vector<const Function *> getCalledFunctions(const Function *F);
362// Converts a mangled name to C naming style to match functions in extapi.c.
363std::string restoreFuncName(std::string funcName);
364
365bool isExtCall(const Function* fun);
366
367bool isMemcpyExtFun(const Function *fun);
368
369bool isMemsetExtFun(const Function* fun);
370
372
373const FunObjVar* getFunObjVar(const std::string&name);
374
381
383const Value* getGlobalRep(const Value* val);
384
386bool isConstantObjSym(const Value* val);
387
388// Dump Control Flow Graph of llvm function, with instructions
389void viewCFG(const Function* fun);
390
391// Dump Control Flow Graph of llvm function, without instructions
392void viewCFGOnly(const Function* fun);
393
394std::string dumpValue(const Value* val);
395
396std::string dumpType(const Type* type);
397
398std::string dumpValueAndDbgInfo(const Value* val);
399
400bool isHeapAllocExtCallViaRet(const Instruction *inst);
401
402bool isHeapAllocExtCallViaArg(const Instruction *inst);
403
404inline bool isHeapAllocExtCall(const Instruction *inst)
405{
407}
408
409bool isStackAllocExtCallViaRet(const Instruction *inst);
410
411inline bool isStackAllocExtCall(const Instruction *inst)
412{
413 return isStackAllocExtCallViaRet(inst);
414}
415
416// Check if a given value represents a heap object.
417bool isHeapObj(const Value* val);
418
419// Check if a given value represents a stack object.
420bool isStackObj(const Value* val);
421
423bool isNonInstricCallSite(const Instruction* inst);
424
427{
428 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
429 {
430 const Function *fun = &(*it);
431 if (isProgEntryFunction(fun))
432 return (fun);
433 }
434 return nullptr;
435}
436
437} // End namespace LLVMUtil
438
439} // End namespace SVF
440
441#endif /* INCLUDE_SVF_FE_LLVMUTIL_H_ */
newitem type
Definition cJSON.cpp:2739
const char *const name
Definition cJSON.h:264
static Option< bool > SVFMain
Definition Options.h:180
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:40
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:638
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition LLVMUtil.h:183
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:92
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:294
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:404
void viewCFGOnly(const Function *fun)
Definition LLVMUtil.cpp:239
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:561
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition LLVMUtil.cpp:158
double getDoubleValue(const ConstantFP *fpValue)
Definition LLVMUtil.h:55
bool isConstantObjSym(const Value *val)
Check whether this value points-to a constant object.
Definition CppUtil.cpp:747
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
Definition LLVMUtil.cpp:250
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition LLVMUtil.h:229
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition LLVMUtil.h:189
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:259
bool isMemcpyExtFun(const Function *fun)
Definition LLVMUtil.cpp:389
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:339
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:411
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition LLVMUtil.h:160
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:123
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:269
std::string dumpType(const Type *type)
Definition LLVMUtil.cpp:616
std::pair< s64_t, u64_t > getIntegerValue(const ConstantInt *intValue)
Definition LLVMUtil.h:83
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition LLVMUtil.cpp:578
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:127
std::string dumpValueAndDbgInfo(const Value *val)
Definition LLVMUtil.cpp:627
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:376
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:453
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:239
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:687
const Value * getGlobalRep(const Value *val)
find the unique defined global across multiple modules
Definition LLVMUtil.cpp:440
const ConstantExpr * isUnaryConstantExpr(const Value *val)
Definition LLVMUtil.h:305
void getFunReachableBBs(const Function *svfFun, std::vector< const SVFBasicBlock * > &bbs)
Get reachable basic block from function entry.
Definition LLVMUtil.cpp:75
const ConstantExpr * isCastConstantExpr(const Value *val)
Definition LLVMUtil.h:249
bool isExtCall(const Function *fun)
Definition LLVMUtil.cpp:384
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:426
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition LLVMUtil.cpp:109
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:121
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:709
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:653
bool isMemsetExtFun(const Function *fun)
Definition LLVMUtil.cpp:395
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition LLVMUtil.h:114
bool isObject(const Value *ref)
Return true if this value refers to a object.
Definition LLVMUtil.cpp:60
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 ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main)
Definition LLVMUtil.h:168
static Type * getPtrElementType(const PointerType *pty)
Definition LLVMUtil.h:132
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:667
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition LLVMUtil.h:105
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition LLVMUtil.h:219
u32_t getHeapAllocHoldingArgPosition(const Function *fun)
Definition LLVMUtil.cpp:401
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:317
std::string restoreFuncName(std::string funcName)
Definition LLVMUtil.cpp:407
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:98
const FunObjVar * getFunObjVar(const std::string &name)
Definition LLVMUtil.cpp:436
bool isNonInstricCallSite(const Instruction *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition LLVMUtil.cpp:725
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:605
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:283
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
for isBitcode
Definition BasicTypes.h:70
llvm::DataLayout DataLayout
Definition BasicTypes.h:112
llvm::Type Type
Definition BasicTypes.h:87
llvm::CallBase CallBase
Definition BasicTypes.h:153
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:90
llvm::Function Function
Definition BasicTypes.h:89
llvm::ConstantData ConstantData
Definition BasicTypes.h:120
llvm::MetadataAsValue MetadataAsValue
Definition BasicTypes.h:106
llvm::Instruction Instruction
Definition BasicTypes.h:91
llvm::ConstantAggregate ConstantAggregate
Definition BasicTypes.h:121
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:86
llvm::ConstantExpr ConstantExpr
Definition BasicTypes.h:124
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
llvm::BlockAddress BlockAddress
Definition BasicTypes.h:95
llvm::Module Module
Definition BasicTypes.h:88
llvm::PointerType PointerType
Definition BasicTypes.h:100
unsigned u32_t
Definition GeneralType.h:47
llvm::ConstantFP ConstantFP
Definition BasicTypes.h:130
llvm::ConstantInt ConstantInt
Definition BasicTypes.h:129