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
38#include <llvm/Config/llvm-config.h>
39#include <llvm/TargetParser/Triple.h>
40
41namespace SVF
42{
43
45enum class TargetABI
46{
49};
50
51class SVFBasicBlock;
52
53namespace LLVMUtil
54{
55
57inline bool isCallSite(const Instruction* inst)
58{
59 return SVFUtil::isa<CallBase>(inst);
60}
61
62inline bool isCallSite(const Value* val)
63{
64 return SVFUtil::isa<CallBase>(val);
65}
66
67inline double getDoubleValue(const ConstantFP* fpValue)
68{
69 double dval = 0;
70 if (fpValue->isNormalFP())
71 {
72 const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
73 if (&semantics == &llvm::APFloat::IEEEhalf() ||
74 &semantics == &llvm::APFloat::IEEEsingle() ||
75 &semantics == &llvm::APFloat::IEEEdouble() ||
76 &semantics == &llvm::APFloat::IEEEquad() ||
77 &semantics == &llvm::APFloat::x87DoubleExtended())
78 {
79 dval = fpValue->getValueAPF().convertToDouble();
80 }
81 else
82 {
83 assert (false && "Unsupported floating point type");
84 abort();
85 }
86 }
87 else
88 {
89 // other cfp type, like isZero(), isInfinity(), isNegative(), etc.
90 // do nothing
91 }
92 return dval;
93}
94
95inline std::pair<s64_t, u64_t> getIntegerValue(const ConstantInt* intValue)
96{
97 if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
98 return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
99 else
100 return std::make_pair(0,0);
101}
102
104inline const CallBase* getLLVMCallSite(const Value* value)
105{
106 assert(isCallSite(value) && "not a callsite?");
107 return SVFUtil::cast<CallBase>(value);
108}
109
110inline const Function* getCallee(const CallBase* cs)
111{
112 // FIXME: do we need to strip-off the casts here to discover more library functions
113 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
114}
115
117inline const Function* getLLVMFunction(const Value* val)
118{
119 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
120}
121
123const Function* getProgFunction(const std::string& funName);
124
126inline bool isProgEntryFunction(const Function* fun)
127{
128 const char* main_name=Options::SVFMain() ? "svf.main" : "main";
129 return fun && fun->getName() == main_name;
130}
131
133inline bool isBlackholeSym(const Value* val)
134{
135 return SVFUtil::isa<UndefValue>(val);
136}
137
139inline bool isNullPtrSym(const Value* val)
140{
142}
143
145u32_t getNumOfElements(const Type* ety);
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}
163
164
166inline bool ArgInProgEntryFunction(const Value* val)
167{
168 return SVFUtil::isa<Argument>(val) &&
170 SVFUtil::cast<Argument>(val)->getParent());
171}
172
173bool isPtrInUncalledFunction(const Value* value);
175
177
179
180
181inline bool isNoCallerFunction(const Function* fun)
182{
184}
185
187inline bool isArgOfUncalledFunction (const Value* val)
188{
189 return SVFUtil::isa<Argument>(val)
190 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
191}
192
193
195bool basicBlockHasRetInst(const BasicBlock* bb);
196
199bool functionDoesNotRet(const Function* fun);
200
202void getFunReachableBBs(const Function* svfFun,
203 std::vector<const SVFBasicBlock*>& bbs);
204
206const Value* stripConstantCasts(const Value* val);
207
209const Value* stripAllCasts(const Value* val);
210
213const Value* getFirstUseViaCastInst(const Value* val);
214
216
217inline const ConstantExpr* isGepConstantExpr(const Value* val)
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
227inline const ConstantExpr* isInt2PtrConstantExpr(const Value* val)
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
237inline const ConstantExpr* isPtr2IntConstantExpr(const Value* val)
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
247inline const ConstantExpr* isCastConstantExpr(const Value* val)
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
257inline const ConstantExpr* isSelectConstantExpr(const Value* val)
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
267inline const ConstantExpr* isTruncConstantExpr(const Value* val)
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
281inline const ConstantExpr* isCmpConstantExpr(const Value* val)
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
292inline const ConstantExpr* isBinaryConstantExpr(const Value* val)
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
303inline const ConstantExpr* isUnaryConstantExpr(const Value* val)
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}
313
314
315inline static DataLayout* getDataLayout(Module* mod)
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
327inline static llvm::Triple getTargetTriple(const Module* mod)
328{
329 assert(mod && "null module");
330#if LLVM_VERSION_MAJOR >= 21
331 return mod->getTargetTriple();
332#else
333 return llvm::Triple(mod->getTargetTriple());
334#endif
335}
336
337inline static TargetABI detectTargetABI(const llvm::Triple &triple)
338{
339 if (triple.isWindowsMSVCEnvironment())
340 return TargetABI::MSVC;
341
342 if (triple.isWindowsItaniumEnvironment() ||
343 triple.isWindowsGNUEnvironment() ||
344 triple.isWindowsCygwinEnvironment())
345 return TargetABI::Itanium;
346
347 return TargetABI::Itanium;
348}
349
351void getNextInsts(const Instruction* curInst,
352 std::vector<const Instruction*>& instList);
353
354
361{
362 return bb != &bb->getParent()->getEntryBlock() &&
363 pred_empty(bb);
364}
365
367bool isIRFile(const std::string& filename);
368
370void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
371 std::vector<std::string>& moduleNameVec);
372
374
375const std::string getSourceLoc(const Value* val);
376const std::string getSourceLocOfFunction(const Function* F);
377
378bool isIntrinsicInst(const Instruction* inst);
379bool isIntrinsicFun(const Function* func);
380
382std::vector<const Function *> getCalledFunctions(const Function *F);
383// Converts a mangled name to C naming style to match functions in extapi.c.
384std::string restoreFuncName(std::string funcName);
385
386bool isExtCall(const Function* fun);
387
388bool isMemcpyExtFun(const Function *fun);
389
390bool isMemsetExtFun(const Function* fun);
391
393
394const FunObjVar* getFunObjVar(const std::string&name);
395
397inline bool isConstDataOrAggData(const Value* val)
398{
401}
402
404const Value* getGlobalRep(const Value* val);
405
407bool isConstantObjSym(const Value* val);
408
409// Dump Control Flow Graph of llvm function, with instructions
410void viewCFG(const Function* fun);
411
412// Dump Control Flow Graph of llvm function, without instructions
413void viewCFGOnly(const Function* fun);
414
415std::string dumpValue(const Value* val);
416
417std::string dumpType(const Type* type);
418
419std::string dumpValueAndDbgInfo(const Value* val);
420
421bool isHeapAllocExtCallViaRet(const Instruction *inst);
422
423bool isHeapAllocExtCallViaArg(const Instruction *inst);
424
425inline bool isHeapAllocExtCall(const Instruction *inst)
426{
428}
429
430bool isStackAllocExtCallViaRet(const Instruction *inst);
431
432inline bool isStackAllocExtCall(const Instruction *inst)
433{
434 return isStackAllocExtCallViaRet(inst);
435}
436
437// Check if a given value represents a heap object.
438bool isHeapObj(const Value* val);
439
440// Check if a given value represents a stack object.
441bool isStackObj(const Value* val);
442
444bool isNonInstricCallSite(const Instruction* inst);
445
448{
449 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
450 {
451 const Function *fun = &(*it);
452 if (isProgEntryFunction(fun))
453 return (fun);
454 }
455 return nullptr;
456}
457
458} // End namespace LLVMUtil
459
460} // End namespace SVF
461
462#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:175
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition LLVMUtil.cpp:204
const Function * getProgFunction(const std::string &funName)
Get program entry function from module.
Definition LLVMUtil.cpp:41
const Value * stripConstantCasts(const Value *val)
Strip off the constant casts.
Definition LLVMUtil.cpp:220
bool isPtrInUncalledFunction(const Value *value)
Return true if this is value in a dead function (function without any caller).
Definition LLVMUtil.cpp:176
bool isHeapAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:645
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:279
const CallBase * getLLVMCallSite(const Value *value)
Return LLVM callsite given a value.
Definition LLVMUtil.h:104
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:292
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:425
void viewCFGOnly(const Function *fun)
Definition LLVMUtil.cpp:240
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:568
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition LLVMUtil.cpp:159
double getDoubleValue(const ConstantFP *fpValue)
Definition LLVMUtil.h:67
bool isConstantObjSym(const Value *val)
Check whether this value points-to a constant object.
Definition CppUtil.cpp:705
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
Definition LLVMUtil.cpp:251
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:390
bool isIntrinsicFun(const Function *func)
Definition LLVMUtil.cpp:191
std::vector< const Function * > getCalledFunctions(const Function *F)
Get all called funcions in a parent function.
Definition LLVMUtil.cpp:365
bool isNoPrecessorBasicBlock(const BasicBlock *bb)
Definition LLVMUtil.h:360
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition LLVMUtil.h:57
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:432
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:124
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:267
std::string dumpType(const Type *type)
Definition LLVMUtil.cpp:623
std::pair< s64_t, u64_t > getIntegerValue(const ConstantInt *intValue)
Definition LLVMUtil.h:95
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition LLVMUtil.cpp:585
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:139
std::string dumpValueAndDbgInfo(const Value *val)
Definition LLVMUtil.cpp:634
static TargetABI detectTargetABI(const llvm::Triple &triple)
Definition LLVMUtil.h:337
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:397
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:454
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:237
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:694
const Value * getGlobalRep(const Value *val)
find the unique defined global across multiple modules
Definition LLVMUtil.cpp:441
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:76
const ConstantExpr * isCastConstantExpr(const Value *val)
Definition LLVMUtil.h:247
bool isExtCall(const Function *fun)
Definition LLVMUtil.cpp:385
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
Definition LLVMUtil.cpp:297
const Function * getProgEntryFunction(Module &module)
Get program entry function from module.
Definition LLVMUtil.h:447
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition LLVMUtil.cpp:110
void viewCFG(const Function *fun)
Definition LLVMUtil.cpp:232
bool isBlackholeSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:133
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:716
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:660
bool isMemsetExtFun(const Function *fun)
Definition LLVMUtil.cpp:396
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main).
Definition LLVMUtil.h:126
bool isObject(const Value *ref)
Return true if this value refers to a object.
Definition LLVMUtil.cpp:61
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:337
static llvm::Triple getTargetTriple(const Module *mod)
Definition LLVMUtil.h:327
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
Definition LLVMUtil.cpp:316
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main).
Definition LLVMUtil.h:166
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:674
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition LLVMUtil.h:117
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:402
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:315
std::string restoreFuncName(std::string funcName)
Definition LLVMUtil.cpp:408
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:110
const FunObjVar * getFunObjVar(const std::string &name)
Definition LLVMUtil.cpp:437
bool isNonInstricCallSite(const Instruction *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition LLVMUtil.cpp:732
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:612
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:281
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
Definition Casting.h:360
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition Casting.h:405
for isBitcode
Definition BasicTypes.h:70
llvm::DataLayout DataLayout
Definition BasicTypes.h:106
llvm::Type Type
Definition BasicTypes.h:81
llvm::CallBase CallBase
Definition BasicTypes.h:147
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:84
llvm::Function Function
Definition BasicTypes.h:83
llvm::ConstantData ConstantData
Definition BasicTypes.h:114
llvm::MetadataAsValue MetadataAsValue
Definition BasicTypes.h:100
llvm::Instruction Instruction
Definition BasicTypes.h:85
llvm::ConstantAggregate ConstantAggregate
Definition BasicTypes.h:115
TargetABI
C++ ABI inferred from the target triple.
Definition LLVMUtil.h:46
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:80
llvm::ConstantExpr ConstantExpr
Definition BasicTypes.h:118
llvm::BlockAddress BlockAddress
Definition BasicTypes.h:89
llvm::Module Module
Definition BasicTypes.h:82
unsigned u32_t
Definition GeneralType.h:67
llvm::ConstantFP ConstantFP
Definition BasicTypes.h:124
llvm::ConstantInt ConstantInt
Definition BasicTypes.h:123