Static Value-Flow Analysis
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 "SVFIR/SVFValue.h"
36 #include "Util/ThreadAPI.h"
37 
38 namespace SVF
39 {
40 
41 namespace LLVMUtil
42 {
43 
45 inline bool isCallSite(const Instruction* inst)
46 {
47  return SVFUtil::isa<CallBase>(inst);
48 }
50 inline bool isCallSite(const Value* val)
51 {
52  return SVFUtil::isa<CallBase>(val);
53 }
54 
55 
57 inline const CallBase* getLLVMCallSite(const Value* value)
58 {
59  assert(isCallSite(value) && "not a callsite?");
60  return SVFUtil::cast<CallBase>(value);
61 }
62 
63 inline const Function* getCallee(const CallBase* cs)
64 {
65  // FIXME: do we need to strip-off the casts here to discover more library functions
66  return SVFUtil::dyn_cast<Function>(cs->getCalledOperand()->stripPointerCasts());
67 }
68 
70 inline const Function* getLLVMFunction(const Value* val)
71 {
72  return SVFUtil::dyn_cast<Function>(val->stripPointerCasts());
73 }
74 
76 const Function* getProgFunction(const std::string& funName);
77 
79 inline bool isProgEntryFunction(const Function* fun)
80 {
81  return fun && fun->getName() == "main";
82 }
83 
85 inline bool isBlackholeSym(const Value* val)
86 {
87  return SVFUtil::isa<UndefValue>(val);
88 }
89 
91 inline bool isNullPtrSym(const Value* val)
92 {
93  return SVFUtil::dyn_cast<ConstantPointerNull>(val);
94 }
95 
96 static inline Type* getPtrElementType(const PointerType* pty)
97 {
98 #if (LLVM_VERSION_MAJOR < 14)
99  return pty->getPointerElementType();
100 #elif (LLVM_VERSION_MAJOR < 17)
101  assert(!pty->isOpaque() && "Opaque Pointer is used, please recompile the source adding '-Xclang -no-opaque-pointers'");
102  return pty->getNonOpaquePointerElementType();
103 #else
104  assert(false && "llvm version 17+ only support opaque pointers!");
105 #endif
106 }
107 
109 u32_t getNumOfElements(const Type* ety);
110 
111 
113 bool isObject(const Value* ref);
114 
117 
118 bool isUncalledFunction(const Function* fun);
120 
122 inline bool ArgInDeadFunction(const Value* val)
123 {
124  return SVFUtil::isa<Argument>(val)
125  && isUncalledFunction(SVFUtil::cast<Argument>(val)->getParent());
126 }
128 
130 inline bool ArgInProgEntryFunction(const Value* val)
131 {
132  return SVFUtil::isa<Argument>(val) &&
134  SVFUtil::cast<Argument>(val)->getParent());
135 }
137 bool isPtrInUncalledFunction(const Value* value);
139 
141 
143 
144 inline bool isNoCallerFunction(const Function* fun)
146 {
148 }
149 
151 inline bool isArgOfUncalledFunction (const Value* val)
152 {
153  return SVFUtil::isa<Argument>(val)
154  && isNoCallerFunction(SVFUtil::cast<Argument>(val)->getParent());
155 }
157 
159 bool basicBlockHasRetInst(const BasicBlock* bb);
160 
163 bool functionDoesNotRet(const Function* fun);
164 
166 void getFunReachableBBs(const Function* svfFun,
167  std::vector<const SVFBasicBlock*>& bbs);
168 
170 const Value* stripConstantCasts(const Value* val);
171 
173 const Value* stripAllCasts(const Value* val);
174 
177 const Value* getFirstUseViaCastInst(const Value* val);
178 
180 
181 inline const ConstantExpr* isGepConstantExpr(const Value* val)
182 {
183  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
184  {
185  if (constExpr->getOpcode() == Instruction::GetElementPtr)
186  return constExpr;
187  }
188  return nullptr;
189 }
190 
191 inline const ConstantExpr* isInt2PtrConstantExpr(const Value* val)
192 {
193  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
194  {
195  if (constExpr->getOpcode() == Instruction::IntToPtr)
196  return constExpr;
197  }
198  return nullptr;
199 }
200 
201 inline const ConstantExpr* isPtr2IntConstantExpr(const Value* val)
202 {
203  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
204  {
205  if (constExpr->getOpcode() == Instruction::PtrToInt)
206  return constExpr;
207  }
208  return nullptr;
209 }
210 
211 inline const ConstantExpr* isCastConstantExpr(const Value* val)
212 {
213  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
214  {
215  if (constExpr->getOpcode() == Instruction::BitCast)
216  return constExpr;
217  }
218  return nullptr;
219 }
220 
221 inline const ConstantExpr* isSelectConstantExpr(const Value* val)
222 {
223  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
224  {
225  if (constExpr->getOpcode() == Instruction::Select)
226  return constExpr;
227  }
228  return nullptr;
229 }
230 
231 inline const ConstantExpr* isTruncConstantExpr(const Value* val)
232 {
233  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
234  {
235  if (constExpr->getOpcode() == Instruction::Trunc ||
236  constExpr->getOpcode() == Instruction::FPTrunc ||
237  constExpr->getOpcode() == Instruction::ZExt ||
238  constExpr->getOpcode() == Instruction::SExt ||
239  constExpr->getOpcode() == Instruction::FPExt)
240  return constExpr;
241  }
242  return nullptr;
243 }
244 
245 inline const ConstantExpr* isCmpConstantExpr(const Value* val)
246 {
247  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
248  {
249  if (constExpr->getOpcode() == Instruction::ICmp ||
250  constExpr->getOpcode() == Instruction::FCmp)
251  return constExpr;
252  }
253  return nullptr;
254 }
255 
256 inline const ConstantExpr* isBinaryConstantExpr(const Value* val)
257 {
258  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
259  {
260  if ((constExpr->getOpcode() >= Instruction::BinaryOpsBegin) &&
261  (constExpr->getOpcode() <= Instruction::BinaryOpsEnd))
262  return constExpr;
263  }
264  return nullptr;
265 }
266 
267 inline const ConstantExpr* isUnaryConstantExpr(const Value* val)
268 {
269  if (const ConstantExpr* constExpr = SVFUtil::dyn_cast<ConstantExpr>(val))
270  {
271  if ((constExpr->getOpcode() >= Instruction::UnaryOpsBegin) &&
272  (constExpr->getOpcode() <= Instruction::UnaryOpsEnd))
273  return constExpr;
274  }
275  return nullptr;
276 }
278 
279 inline static DataLayout* getDataLayout(Module* mod)
280 {
281  static DataLayout *dl = nullptr;
282  if (dl == nullptr)
283  dl = new DataLayout(mod);
284  return dl;
285 }
286 
288 void getNextInsts(const Instruction* curInst,
289  std::vector<const Instruction*>& instList);
290 
291 
297 inline bool isNoPrecessorBasicBlock(const BasicBlock* bb)
298 {
299  return bb != &bb->getParent()->getEntryBlock() &&
300  pred_empty(bb);
301 }
302 
304 bool isIRFile(const std::string& filename);
305 
307 void processArguments(int argc, char** argv, int& arg_num, char** arg_value,
308  std::vector<std::string>& moduleNameVec);
309 
311 
312 const std::string getSourceLoc(const Value* val);
314 
315 bool isIntrinsicInst(const Instruction* inst);
316 bool isIntrinsicFun(const Function* func);
317 
319 std::vector<const Function *> getCalledFunctions(const Function *F);
320 // Converts a mangled name to C naming style to match functions in extapi.c.
322 
324 const SVFFunction* getFunction(const std::string& name);
325 
327 inline bool isConstDataOrAggData(const Value* val)
328 {
331 }
332 
334 const Value* getGlobalRep(const Value* val);
335 
337 bool isConstantObjSym(const SVFValue* val);
338 
340 bool isConstantObjSym(const Value* val);
341 
342 // Dump Control Flow Graph of llvm function, with instructions
343 void viewCFG(const Function* fun);
344 
345 // Dump Control Flow Graph of llvm function, without instructions
346 void viewCFGOnly(const Function* fun);
347 
348 std::string dumpValue(const Value* val);
349 
350 std::string dumpType(const Type* type);
351 
353 
354 bool isHeapAllocExtCallViaRet(const Instruction *inst);
355 
356 bool isHeapAllocExtCallViaArg(const Instruction *inst);
357 
358 inline bool isHeapAllocExtCall(const Instruction *inst)
359 {
361 }
362 
364 bool isNonInstricCallSite(const Instruction* inst);
365 
367 inline const Function* getProgEntryFunction(Module& module)
368 {
369  for (auto it = module.begin(), eit = module.end(); it != eit; ++it)
370  {
371  const Function *fun = &(*it);
372  if (isProgEntryFunction(fun))
373  return (fun);
374  }
375  return nullptr;
376 }
377 
378 } // End namespace LLVMUtil
379 
380 } // End namespace SVF
381 
382 #endif /* INCLUDE_SVF_FE_LLVMUTIL_H_ */
#define F(f)
newitem type
Definition: cJSON.cpp:2739
const char *const name
Definition: cJSON.h:264
const char *const string
Definition: cJSON.h:172
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition: LLVMUtil.cpp:204
const CallBase * getLLVMCallSite(const Value *value)
Return LLVM callsite given a value.
Definition: LLVMUtil.h:57
const Function * getProgFunction(const std::string &funName)
Get program entry function from module.
Definition: LLVMUtil.cpp:39
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:618
static DataLayout * getDataLayout(Module *mod)
Definition: LLVMUtil.h:279
bool isNoCallerFunction(const Function *fun)
Function does not have any possible caller in the call graph.
Definition: LLVMUtil.h:145
const Value * getFirstUseViaCastInst(const Value *val)
Definition: LLVMUtil.cpp:279
const ConstantExpr * isUnaryConstantExpr(const Value *val)
Definition: LLVMUtil.h:267
bool isHeapAllocExtCall(const Instruction *inst)
Definition: LLVMUtil.h:358
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition: LLVMUtil.h:201
void viewCFGOnly(const Function *fun)
Definition: LLVMUtil.cpp:240
const std::string getSourceLocOfFunction(const Function *F)
Definition: LLVMUtil.cpp:534
bool isUncalledFunction(const Function *fun)
whether this is a function without any possible caller?
Definition: LLVMUtil.cpp:159
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition: LLVMUtil.h:256
const Value * stripAllCasts(const Value *val)
Strip off the all casts.
Definition: LLVMUtil.cpp:251
bool isArgOfUncalledFunction(const Value *val)
Return true if the argument in a function does not have a caller.
Definition: LLVMUtil.h:151
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:297
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition: LLVMUtil.h:45
bool ArgInDeadFunction(const Value *val)
whether this is an argument in dead function
Definition: LLVMUtil.h:122
bool functionDoesNotRet(const Function *fun)
Definition: LLVMUtil.cpp:123
std::string dumpType(const Type *type)
Definition: LLVMUtil.cpp:596
void getNextInsts(const Instruction *curInst, std::vector< const Instruction * > &instList)
Get the next instructions following control flow.
Definition: LLVMUtil.cpp:551
const Function * getProgEntryFunction(Module &module)
Get program entry function from module.
Definition: LLVMUtil.h:367
bool isNullPtrSym(const Value *val)
Check whether this value is a black hole.
Definition: LLVMUtil.h:91
std::string dumpValueAndDbgInfo(const Value *val)
Definition: LLVMUtil.cpp:607
bool isConstDataOrAggData(const Value *val)
Return true if the value refers to constant data, e.g., i32 0.
Definition: LLVMUtil.h:327
const std::string getSourceLoc(const Value *val)
Definition: LLVMUtil.cpp:430
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition: LLVMUtil.h:231
const Value * getGlobalRep(const Value *val)
find the unique defined global across multiple modules
Definition: LLVMUtil.cpp:417
void getFunReachableBBs(const Function *svfFun, std::vector< const SVFBasicBlock * > &bbs)
Get reachable basic block from function entry.
Definition: LLVMUtil.cpp:74
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition: LLVMUtil.h:245
u32_t getNumOfElements(const Type *ety)
Return size of this object based on LLVM value.
Definition: LLVMUtil.cpp:297
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition: LLVMUtil.h:181
const SVFFunction * getFunction(const std::string &name)
Get the corresponding Function based on its name.
Definition: LLVMUtil.cpp:412
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition: LLVMUtil.cpp:109
static Type * getPtrElementType(const PointerType *pty)
Definition: LLVMUtil.h:96
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:85
bool isHeapAllocExtCallViaArg(const Instruction *inst)
Definition: LLVMUtil.cpp:634
bool isProgEntryFunction(const Function *fun)
Check whether a function is an entry function (i.e., main)
Definition: LLVMUtil.h:79
bool isObject(const Value *ref)
Return true if this value refers to a object.
Definition: LLVMUtil.cpp:59
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
const Function * getCallee(const CallBase *cs)
Definition: LLVMUtil.h:63
bool isIRFile(const std::string &filename)
Check whether a file is an LLVM IR file.
Definition: LLVMUtil.cpp:316
bool isConstantObjSym(const SVFValue *val)
Check whether this value points-to a constant object.
Definition: LLVMUtil.cpp:579
bool ArgInProgEntryFunction(const Value *val)
Return true if this is an argument of a program entry function (e.g. main)
Definition: LLVMUtil.h:130
const ConstantExpr * isInt2PtrConstantExpr(const Value *val)
Definition: LLVMUtil.h:191
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition: LLVMUtil.h:221
std::string restoreFuncName(std::string funcName)
Definition: LLVMUtil.cpp:384
const Function * getLLVMFunction(const Value *val)
Return LLVM function if this value is.
Definition: LLVMUtil.h:70
bool isNonInstricCallSite(const Instruction *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition: LLVMUtil.cpp:649
const ConstantExpr * isCastConstantExpr(const Value *val)
Definition: LLVMUtil.h:211
std::string dumpValue(const Value *val)
Definition: LLVMUtil.cpp:585
LLVM_NODISCARD bool isa(const Y &Val)
Definition: Casting.h:241
for isBitcode
Definition: BasicTypes.h:68
llvm::DataLayout DataLayout
Definition: BasicTypes.h:108
llvm::Type Type
Definition: BasicTypes.h:83
llvm::CallBase CallBase
Definition: BasicTypes.h:146
llvm::BasicBlock BasicBlock
Definition: BasicTypes.h:86
llvm::Function Function
Definition: BasicTypes.h:85
llvm::ConstantData ConstantData
Definition: BasicTypes.h:116
llvm::MetadataAsValue MetadataAsValue
Definition: BasicTypes.h:102
llvm::Instruction Instruction
Definition: BasicTypes.h:87
llvm::ConstantAggregate ConstantAggregate
Definition: BasicTypes.h:117
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82
llvm::ConstantExpr ConstantExpr
Definition: BasicTypes.h:120
llvm::BlockAddress BlockAddress
Definition: BasicTypes.h:91
llvm::Module Module
Definition: BasicTypes.h:84
llvm::PointerType PointerType
Definition: BasicTypes.h:96
unsigned u32_t
Definition: GeneralType.h:46