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#if LLVM_VERSION_MAJOR < 16
40#include <llvm/ADT/Triple.h>
41#else
42#include <llvm/TargetParser/Triple.h>
43#endif
44
45namespace SVF
46{
47
49enum class TargetABI
50{
51 Itanium,
52 MSVC,
53};
54
55class SVFBasicBlock;
56
57namespace LLVMUtil
58{
59
61inline bool isCallSite(const Instruction* inst)
62{
63 return SVFUtil::isa<CallBase>(inst);
64}
66inline bool isCallSite(const Value* val)
67{
68 return SVFUtil::isa<CallBase>(val);
69}
70
71inline double getDoubleValue(const ConstantFP* fpValue)
72{
73 double dval = 0;
74 if (fpValue->isNormalFP())
75 {
76 const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
77 if (&semantics == &llvm::APFloat::IEEEhalf() ||
78 &semantics == &llvm::APFloat::IEEEsingle() ||
79 &semantics == &llvm::APFloat::IEEEdouble() ||
80 &semantics == &llvm::APFloat::IEEEquad() ||
81 &semantics == &llvm::APFloat::x87DoubleExtended())
82 {
83 dval = fpValue->getValueAPF().convertToDouble();
84 }
85 else
86 {
87 assert (false && "Unsupported floating point type");
88 abort();
89 }
90 }
91 else
92 {
93 // other cfp type, like isZero(), isInfinity(), isNegative(), etc.
94 // do nothing
95 }
96 return dval;
97}
98
99inline std::pair<s64_t, u64_t> getIntegerValue(const ConstantInt* intValue)
100{
101 if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
102 return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
103 else
104 return std::make_pair(0,0);
105}
106
108inline const CallBase* getLLVMCallSite(const Value* value)
109{
110 assert(isCallSite(value) && "not a callsite?");
111 return SVFUtil::cast<CallBase>(value);
112}
113
114inline const Function* getCallee(const CallBase* cs)
115{
116 // FIXME: do we need to strip-off the casts here to discover more library functions
117 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
118}
119
121inline const Function* getLLVMFunction(const Value* val)
122{
123 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
124}
125
127const Function* getProgFunction(const std::string& funName);
128
130inline bool isProgEntryFunction(const Function* fun)
131{
132 const char* main_name=Options::SVFMain() ? "svf.main" : "main";
133 return fun && fun->getName() == main_name;
134}
135
137inline bool isBlackholeSym(const Value* val)
138{
139 return SVFUtil::isa<UndefValue>(val);
140}
141
143inline bool isNullPtrSym(const Value* val)
144{
145 return SVFUtil::dyn_cast<ConstantPointerNull>(val);
146}
147
148static inline Type* getPtrElementType(const PointerType* pty)
149{
150#if (LLVM_VERSION_MAJOR < 14)
151 return pty->getPointerElementType();
152#elif (LLVM_VERSION_MAJOR < 17)
153 assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
154 return pty->getNonOpaquePointerElementType();
155#else
156 (void)pty; // Suppress warning of unused variable under release build
157 assert(false && "llvm version 17+ only support opaque pointers!");
158 return nullptr;
159#endif
160}
161
164
165
167bool isObject(const Value* ref);
168
171
172
173bool isUncalledFunction(const Function* fun);
174
176inline bool ArgInDeadFunction(const Value* val)
177{
178 return SVFUtil::isa<Argument>(val)
179 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
180}
182
185{
186 return SVFUtil::isa<Argument>(val) &&
188 SVFUtil::cast<Argument>(val)->getParent());
189}
191bool isPtrInUncalledFunction(const Value* value);
193
195
197
198
199inline bool isNoCallerFunction(const Function* fun)
200{
202}
203
206{
207 return SVFUtil::isa<Argument>(val)
208 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
209}
211
213bool basicBlockHasRetInst(const BasicBlock* bb);
214
217bool functionDoesNotRet(const Function* fun);
218
221 std::vector<const SVFBasicBlock*>& bbs);
222
224const Value* stripConstantCasts(const Value* val);
225
227const Value* stripAllCasts(const Value* val);
228
232
234
236{
237 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
238 {
239 if (constExpr->getOpcode() == Instruction::GetElementPtr)
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::IntToPtr)
250 return constExpr;
251 }
252 return nullptr;
253}
254
256{
257 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
258 {
259 if (constExpr->getOpcode() == Instruction::PtrToInt)
260 return constExpr;
261 }
262 return nullptr;
263}
264
266{
267 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
268 {
269 if (constExpr->getOpcode() == Instruction::BitCast)
270 return constExpr;
271 }
272 return nullptr;
273}
274
276{
277 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
278 {
279 if (constExpr->getOpcode() == Instruction::Select)
280 return constExpr;
281 }
282 return nullptr;
283}
284
286{
287 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
288 {
289 if (constExpr->getOpcode() == Instruction::Trunc ||
290 constExpr->getOpcode() == Instruction::FPTrunc ||
291 constExpr->getOpcode() == Instruction::ZExt ||
292 constExpr->getOpcode() == Instruction::SExt ||
293 constExpr->getOpcode() == Instruction::FPExt)
294 return constExpr;
295 }
296 return nullptr;
297}
298
300{
301 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
302 {
303 if (constExpr->getOpcode() == Instruction::ICmp ||
304 constExpr->getOpcode() == Instruction::FCmp)
305 return constExpr;
306 }
307 return nullptr;
308}
309
311{
312 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
313 {
314 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
315 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
316 return constExpr;
317 }
318 return nullptr;
319}
320
322{
323 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
324 {
325 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
326 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
327 return constExpr;
328 }
329 return nullptr;
330}
332
334{
335 static DataLayout *dl = nullptr;
336 if (dl == nullptr)
337#if LLVM_VERSION_MAJOR >= 19
338 dl = new DataLayout(mod->getDataLayout());
339#else
340 dl = new DataLayout(mod);
341#endif
342 return dl;
343}
344
345inline static llvm::Triple getTargetTriple(const Module* mod)
346{
347 assert(mod && "null module");
348#if LLVM_VERSION_MAJOR >= 21
349 return mod->getTargetTriple();
350#else
351 return llvm::Triple(mod->getTargetTriple());
352#endif
353}
354
355inline static TargetABI detectTargetABI(const llvm::Triple &triple)
356{
357 if (triple.isWindowsMSVCEnvironment())
358 return TargetABI::MSVC;
359
360 if (triple.isWindowsItaniumEnvironment() ||
361 triple.isWindowsGNUEnvironment() ||
362 triple.isWindowsCygwinEnvironment())
363 return TargetABI::Itanium;
364
365 return TargetABI::Itanium;
366}
367
370 std::vector<const Instruction*>& instList);
371
372
379{
380 return bb != &bb->getParent()->getEntryBlock() &&
381 pred_empty(bb);
382}
383
385bool isIRFile(const std::string& filename);
386
388void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
389 std::vector<std::string>& moduleNameVec);
390
392
393const std::string getSourceLoc(const Value* val);
394const std::string getSourceLocOfFunction(const Function* F);
395
396bool isIntrinsicInst(const Instruction* inst);
397bool isIntrinsicFun(const Function* func);
398
400std::vector<const Function *> getCalledFunctions(const Function *F);
401// Converts a mangled name to C naming style to match functions in extapi.c.
402std::string restoreFuncName(std::string funcName);
403
404bool isExtCall(const Function* fun);
405
406bool isMemcpyExtFun(const Function *fun);
407
408bool isMemsetExtFun(const Function* fun);
409
411
412const FunObjVar* getFunObjVar(const std::string&name);
413
420
422const Value* getGlobalRep(const Value* val);
423
425bool isConstantObjSym(const Value* val);
426
427// Dump Control Flow Graph of llvm function, with instructions
428void viewCFG(const Function* fun);
429
430// Dump Control Flow Graph of llvm function, without instructions
431void viewCFGOnly(const Function* fun);
432
433std::string dumpValue(const Value* val);
434
435std::string dumpType(const Type* type);
436
437std::string dumpValueAndDbgInfo(const Value* val);
438
439bool isHeapAllocExtCallViaRet(const Instruction *inst);
440
441bool isHeapAllocExtCallViaArg(const Instruction *inst);
442
443inline bool isHeapAllocExtCall(const Instruction *inst)
444{
446}
447
448bool isStackAllocExtCallViaRet(const Instruction *inst);
449
450inline bool isStackAllocExtCall(const Instruction *inst)
451{
452 return isStackAllocExtCallViaRet(inst);
453}
454
455// Check if a given value represents a heap object.
456bool isHeapObj(const Value* val);
457
458// Check if a given value represents a stack object.
459bool isStackObj(const Value* val);
460
462bool isNonInstricCallSite(const Instruction* inst);
463
466{
467 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
468 {
469 const Function *fun = &(*it);
470 if (isProgEntryFunction(fun))
471 return (fun);
472 }
473 return nullptr;
474}
475
476} // End namespace LLVMUtil
477
478} // End namespace SVF
479
480#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:174
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:649
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition LLVMUtil.h:199
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:108
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:310
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:443
void viewCFGOnly(const Function *fun)
Definition LLVMUtil.cpp:240
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:572
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:71
bool isConstantObjSym(const Value *val)
Check whether this value points-to a constant object.
Definition CppUtil.cpp:684
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
Definition LLVMUtil.cpp:251
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition LLVMUtil.h:245
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition LLVMUtil.h:205
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:275
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:378
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition LLVMUtil.h:61
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:450
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition LLVMUtil.h:176
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:124
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:285
std::string dumpType(const Type *type)
Definition LLVMUtil.cpp:627
std::pair< s64_t, u64_t > getIntegerValue(const ConstantInt *intValue)
Definition LLVMUtil.h:99
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition LLVMUtil.cpp:589
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:143
std::string dumpValueAndDbgInfo(const Value *val)
Definition LLVMUtil.cpp:638
static TargetABI detectTargetABI(const llvm::Triple &triple)
Definition LLVMUtil.h:355
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:415
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:454
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:255
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:698
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:321
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:265
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:465
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:137
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:720
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:664
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:130
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:345
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:184
static Type * getPtrElementType(const PointerType *pty)
Definition LLVMUtil.h:148
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:678
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition LLVMUtil.h:121
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition LLVMUtil.h:235
u32_t getHeapAllocHoldingArgPosition(const Function *fun)
Definition LLVMUtil.cpp:402
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:333
std::string restoreFuncName(std::string funcName)
Definition LLVMUtil.cpp:408
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:114
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:736
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:616
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:299
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
TargetABI
C++ ABI inferred from the target triple.
Definition LLVMUtil.h:50
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:67
llvm::ConstantFP ConstantFP
Definition BasicTypes.h:130
llvm::ConstantInt ConstantInt
Definition BasicTypes.h:129