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 assert(false && "llvm version 17+ only support opaque pointers!");
141#endif
142}
143
146
147
149bool isObject(const Value* ref);
150
153
154
155bool isUncalledFunction(const Function* fun);
156
158inline bool ArgInDeadFunction(const Value* val)
159{
160 return SVFUtil::isa<Argument>(val)
161 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
162}
164
167{
168 return SVFUtil::isa<Argument>(val) &&
170 SVFUtil::cast<Argument>(val)->getParent());
171}
173bool isPtrInUncalledFunction(const Value* value);
175
177
179
180
181inline bool isNoCallerFunction(const Function* fun)
182{
184}
185
188{
189 return SVFUtil::isa<Argument>(val)
190 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
191}
193
195bool basicBlockHasRetInst(const BasicBlock* bb);
196
199bool functionDoesNotRet(const Function* fun);
200
203 std::vector<const SVFBasicBlock*>& bbs);
204
206const Value* stripConstantCasts(const Value* val);
207
209const Value* stripAllCasts(const Value* val);
210
214
216
218{
219 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
220 {
221 if (constExpr->getOpcode() == Instruction::GetElementPtr)
222 return constExpr;
223 }
224 return nullptr;
225}
226
228{
229 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
230 {
231 if (constExpr->getOpcode() == Instruction::IntToPtr)
232 return constExpr;
233 }
234 return nullptr;
235}
236
238{
239 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
240 {
241 if (constExpr->getOpcode() == Instruction::PtrToInt)
242 return constExpr;
243 }
244 return nullptr;
245}
246
248{
249 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
250 {
251 if (constExpr->getOpcode() == Instruction::BitCast)
252 return constExpr;
253 }
254 return nullptr;
255}
256
258{
259 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
260 {
261 if (constExpr->getOpcode() == Instruction::Select)
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::Trunc ||
272 constExpr->getOpcode() == Instruction::FPTrunc ||
273 constExpr->getOpcode() == Instruction::ZExt ||
274 constExpr->getOpcode() == Instruction::SExt ||
275 constExpr->getOpcode() == Instruction::FPExt)
276 return constExpr;
277 }
278 return nullptr;
279}
280
282{
283 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
284 {
285 if (constExpr->getOpcode() == Instruction::ICmp ||
286 constExpr->getOpcode() == Instruction::FCmp)
287 return constExpr;
288 }
289 return nullptr;
290}
291
293{
294 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
295 {
296 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
297 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
298 return constExpr;
299 }
300 return nullptr;
301}
302
304{
305 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
306 {
307 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
308 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
309 return constExpr;
310 }
311 return nullptr;
312}
314
316{
317 static DataLayout *dl = nullptr;
318 if (dl == nullptr)
319#if LLVM_VERSION_MAJOR >= 19
320 dl = new DataLayout(mod->getDataLayout());
321#else
322 dl = new DataLayout(mod);
323#endif
324 return dl;
325}
326
329 std::vector<const Instruction*>& instList);
330
331
338{
339 return bb != &bb->getParent()->getEntryBlock() &&
340 pred_empty(bb);
341}
342
344bool isIRFile(const std::string& filename);
345
347void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
348 std::vector<std::string>& moduleNameVec);
349
351
352const std::string getSourceLoc(const Value* val);
353const std::string getSourceLocOfFunction(const Function* F);
354
355bool isIntrinsicInst(const Instruction* inst);
356bool isIntrinsicFun(const Function* func);
357
359std::vector<const Function *> getCalledFunctions(const Function *F);
360// Converts a mangled name to C naming style to match functions in extapi.c.
361std::string restoreFuncName(std::string funcName);
362
363bool isExtCall(const Function* fun);
364
365bool isMemcpyExtFun(const Function *fun);
366
367bool isMemsetExtFun(const Function* fun);
368
370
371const FunObjVar* getFunObjVar(const std::string&name);
372
379
381const Value* getGlobalRep(const Value* val);
382
384bool isConstantObjSym(const Value* val);
385
386// Dump Control Flow Graph of llvm function, with instructions
387void viewCFG(const Function* fun);
388
389// Dump Control Flow Graph of llvm function, without instructions
390void viewCFGOnly(const Function* fun);
391
392std::string dumpValue(const Value* val);
393
394std::string dumpType(const Type* type);
395
396std::string dumpValueAndDbgInfo(const Value* val);
397
398bool isHeapAllocExtCallViaRet(const Instruction *inst);
399
400bool isHeapAllocExtCallViaArg(const Instruction *inst);
401
402inline bool isHeapAllocExtCall(const Instruction *inst)
403{
405}
406
407bool isStackAllocExtCallViaRet(const Instruction *inst);
408
409inline bool isStackAllocExtCall(const Instruction *inst)
410{
411 return isStackAllocExtCallViaRet(inst);
412}
413
414// Check if a given value represents a heap object.
415bool isHeapObj(const Value* val);
416
417// Check if a given value represents a stack object.
418bool isStackObj(const Value* val);
419
421bool isNonInstricCallSite(const Instruction* inst);
422
425{
426 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
427 {
428 const Function *fun = &(*it);
429 if (isProgEntryFunction(fun))
430 return (fun);
431 }
432 return nullptr;
433}
434
435} // End namespace LLVMUtil
436
437} // End namespace SVF
438
439#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:181
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:292
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:402
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:227
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition LLVMUtil.h:187
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:257
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:337
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:409
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition LLVMUtil.h:158
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:123
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:267
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:374
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:453
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:237
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:303
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:247
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:424
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:166
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:217
u32_t getHeapAllocHoldingArgPosition(const Function *fun)
Definition LLVMUtil.cpp:401
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:315
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:281
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