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
41class SVFBasicBlock;
42
43namespace LLVMUtil
44{
45
47inline bool isCallSite(const Instruction* inst)
48{
49 return SVFUtil::isa<CallBase>(inst);
50}
52inline bool isCallSite(const Value* val)
53{
54 return SVFUtil::isa<CallBase>(val);
55}
56
57inline double getDoubleValue(const ConstantFP* fpValue)
58{
59 double dval = 0;
60 if (fpValue->isNormalFP())
61 {
62 const llvm::fltSemantics& semantics = fpValue->getValueAPF().getSemantics();
63 if (&semantics == &llvm::APFloat::IEEEhalf() ||
64 &semantics == &llvm::APFloat::IEEEsingle() ||
65 &semantics == &llvm::APFloat::IEEEdouble() ||
66 &semantics == &llvm::APFloat::IEEEquad() ||
67 &semantics == &llvm::APFloat::x87DoubleExtended())
68 {
69 dval = fpValue->getValueAPF().convertToDouble();
70 }
71 else
72 {
73 assert (false && "Unsupported floating point type");
74 abort();
75 }
76 }
77 else
78 {
79 // other cfp type, like isZero(), isInfinity(), isNegative(), etc.
80 // do nothing
81 }
82 return dval;
83}
84
85inline std::pair<s64_t, u64_t> getIntegerValue(const ConstantInt* intValue)
86{
87 if (intValue->getBitWidth() <= 64 && intValue->getBitWidth() >= 1)
88 return std::make_pair(intValue->getSExtValue(), intValue->getZExtValue());
89 else
90 return std::make_pair(0,0);
91}
92
94inline const CallBase* getLLVMCallSite(const Value* value)
95{
96 assert(isCallSite(value) && "not a callsite?");
97 return SVFUtil::cast<CallBase>(value);
98}
99
100inline const Function* getCallee(const CallBase* cs)
101{
102 // FIXME: do we need to strip-off the casts here to discover more library functions
103 return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
104}
105
107inline const Function* getLLVMFunction(const Value* val)
108{
109 return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
110}
111
113const Function* getProgFunction(const std::string& funName);
114
116inline bool isProgEntryFunction(const Function* fun)
117{
118 const char* main_name=Options::SVFMain() ? "svf.main" : "main";
119 return fun && fun->getName() == main_name;
120}
121
123inline bool isBlackholeSym(const Value* val)
124{
125 return SVFUtil::isa<UndefValue>(val);
126}
127
129inline bool isNullPtrSym(const Value* val)
130{
131 return SVFUtil::dyn_cast<ConstantPointerNull>(val);
132}
133
134static inline Type* getPtrElementType(const PointerType* pty)
135{
136#if (LLVM_VERSION_MAJOR < 14)
137 return pty->getPointerElementType();
138#elif (LLVM_VERSION_MAJOR < 17)
139 assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
140 return pty->getNonOpaquePointerElementType();
141#else
142 (void)pty; // Suppress warning of unused variable under release build
143 assert(false && "llvm version 17+ only support opaque pointers!");
144 return nullptr;
145#endif
146}
147
150
151
153bool isObject(const Value* ref);
154
157
158
159bool isUncalledFunction(const Function* fun);
160
162inline bool ArgInDeadFunction(const Value* val)
163{
164 return SVFUtil::isa<Argument>(val)
165 && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
166}
168
171{
172 return SVFUtil::isa<Argument>(val) &&
174 SVFUtil::cast<Argument>(val)->getParent());
175}
177bool isPtrInUncalledFunction(const Value* value);
179
181
183
184
185inline bool isNoCallerFunction(const Function* fun)
186{
188}
189
192{
193 return SVFUtil::isa<Argument>(val)
194 && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
195}
197
199bool basicBlockHasRetInst(const BasicBlock* bb);
200
203bool functionDoesNotRet(const Function* fun);
204
207 std::vector<const SVFBasicBlock*>& bbs);
208
210const Value* stripConstantCasts(const Value* val);
211
213const Value* stripAllCasts(const Value* val);
214
218
220
222{
223 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
224 {
225 if (constExpr->getOpcode() == Instruction::GetElementPtr)
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::IntToPtr)
236 return constExpr;
237 }
238 return nullptr;
239}
240
242{
243 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
244 {
245 if (constExpr->getOpcode() == Instruction::PtrToInt)
246 return constExpr;
247 }
248 return nullptr;
249}
250
252{
253 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
254 {
255 if (constExpr->getOpcode() == Instruction::BitCast)
256 return constExpr;
257 }
258 return nullptr;
259}
260
262{
263 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
264 {
265 if (constExpr->getOpcode() == Instruction::Select)
266 return constExpr;
267 }
268 return nullptr;
269}
270
272{
273 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
274 {
275 if (constExpr->getOpcode() == Instruction::Trunc ||
276 constExpr->getOpcode() == Instruction::FPTrunc ||
277 constExpr->getOpcode() == Instruction::ZExt ||
278 constExpr->getOpcode() == Instruction::SExt ||
279 constExpr->getOpcode() == Instruction::FPExt)
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::ICmp ||
290 constExpr->getOpcode() == Instruction::FCmp)
291 return constExpr;
292 }
293 return nullptr;
294}
295
297{
298 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
299 {
300 if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
301 (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
302 return constExpr;
303 }
304 return nullptr;
305}
306
308{
309 if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
310 {
311 if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
312 (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
313 return constExpr;
314 }
315 return nullptr;
316}
318
320{
321 static DataLayout *dl = nullptr;
322 if (dl == nullptr)
323#if LLVM_VERSION_MAJOR >= 19
324 dl = new DataLayout(mod->getDataLayout());
325#else
326 dl = new DataLayout(mod);
327#endif
328 return dl;
329}
330
333 std::vector<const Instruction*>& instList);
334
335
342{
343 return bb != &bb->getParent()->getEntryBlock() &&
344 pred_empty(bb);
345}
346
348bool isIRFile(const std::string& filename);
349
351void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
352 std::vector<std::string>& moduleNameVec);
353
355
356const std::string getSourceLoc(const Value* val);
357const std::string getSourceLocOfFunction(const Function* F);
358
359bool isIntrinsicInst(const Instruction* inst);
360bool isIntrinsicFun(const Function* func);
361
363std::vector<const Function *> getCalledFunctions(const Function *F);
364// Converts a mangled name to C naming style to match functions in extapi.c.
365std::string restoreFuncName(std::string funcName);
366
367bool isExtCall(const Function* fun);
368
369bool isMemcpyExtFun(const Function *fun);
370
371bool isMemsetExtFun(const Function* fun);
372
374
375const FunObjVar* getFunObjVar(const std::string&name);
376
383
385const Value* getGlobalRep(const Value* val);
386
388bool isConstantObjSym(const Value* val);
389
390// Dump Control Flow Graph of llvm function, with instructions
391void viewCFG(const Function* fun);
392
393// Dump Control Flow Graph of llvm function, without instructions
394void viewCFGOnly(const Function* fun);
395
396std::string dumpValue(const Value* val);
397
398std::string dumpType(const Type* type);
399
400std::string dumpValueAndDbgInfo(const Value* val);
401
402bool isHeapAllocExtCallViaRet(const Instruction *inst);
403
404bool isHeapAllocExtCallViaArg(const Instruction *inst);
405
406inline bool isHeapAllocExtCall(const Instruction *inst)
407{
409}
410
411bool isStackAllocExtCallViaRet(const Instruction *inst);
412
413inline bool isStackAllocExtCall(const Instruction *inst)
414{
415 return isStackAllocExtCallViaRet(inst);
416}
417
418// Check if a given value represents a heap object.
419bool isHeapObj(const Value* val);
420
421// Check if a given value represents a stack object.
422bool isStackObj(const Value* val);
423
425bool isNonInstricCallSite(const Instruction* inst);
426
429{
430 for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
431 {
432 const Function *fun = &(*it);
433 if (isProgEntryFunction(fun))
434 return (fun);
435 }
436 return nullptr;
437}
438
439} // End namespace LLVMUtil
440
441} // End namespace SVF
442
443#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:173
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:639
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition LLVMUtil.h:185
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:94
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:296
bool isHeapAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:406
void viewCFGOnly(const Function *fun)
Definition LLVMUtil.cpp:240
const std::string getSourceLocOfFunction(const Function *F)
Definition LLVMUtil.cpp:562
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:57
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:251
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition LLVMUtil.h:231
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition LLVMUtil.h:191
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:261
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:341
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition LLVMUtil.h:47
bool isStackAllocExtCall(const Instruction *inst)
Definition LLVMUtil.h:413
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition LLVMUtil.h:162
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:124
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:271
std::string dumpType(const Type *type)
Definition LLVMUtil.cpp:617
std::pair< s64_t, u64_t > getIntegerValue(const ConstantInt *intValue)
Definition LLVMUtil.h:85
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition LLVMUtil.cpp:579
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition LLVMUtil.h:129
std::string dumpValueAndDbgInfo(const Value *val)
Definition LLVMUtil.cpp:628
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition LLVMUtil.h:378
const std::string getSourceLoc(const Value *val)
Definition LLVMUtil.cpp:454
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:241
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:688
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:307
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:251
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:428
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:123
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:710
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition LLVMUtil.cpp:654
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:116
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
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:170
static Type * getPtrElementType(const PointerType *pty)
Definition LLVMUtil.h:134
bool isStackAllocExtCallViaRet(const Instruction *inst)
Definition LLVMUtil.cpp:668
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition LLVMUtil.h:107
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition LLVMUtil.h:221
u32_t getHeapAllocHoldingArgPosition(const Function *fun)
Definition LLVMUtil.cpp:402
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:319
std::string restoreFuncName(std::string funcName)
Definition LLVMUtil.cpp:408
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:100
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:726
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:606
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:285
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:67
llvm::ConstantFP ConstantFP
Definition BasicTypes.h:130
llvm::ConstantInt ConstantInt
Definition BasicTypes.h:129