Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFIRBuilder.cpp
Go to the documentation of this file.
1//===- SVFIRBuilder.cpp -- SVFIR builder-----------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <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 * SVFIRBuilder.cpp
25 *
26 * Created on: Nov 1, 2013
27 * Author: Yulei Sui
28 * Refactored on: Jan 25, 2024
29 * Author: Xiao Cheng, Yulei Sui
30 */
31
33#include "SVF-LLVM/BasicTypes.h"
34#include "SVF-LLVM/CHGBuilder.h"
35#include "SVF-LLVM/CppUtil.h"
37#include "SVF-LLVM/LLVMUtil.h"
42#include "Graphs/CallGraph.h"
43#include "Util/Options.h"
44#include "Util/SVFUtil.h"
45
46using namespace std;
47using namespace SVF;
48using namespace SVFUtil;
49using namespace LLVMUtil;
50
51
56{
57 double startTime = SVFStat::getClk(true);
58
59 DBOUT(DGENERAL, outs() << pasMsg("\t Building SVFIR ...\n"));
60
61 // If the SVFIR has been built before, then we return the unique SVFIR of the program
63 return pag;
64
65
67
70 pag->icfg = icfgbuilder.build();
71
79
80
81
84 std::vector<const FunObjVar*> funset;
85 for (const auto& item: llvmModuleSet()->getFunctionSet())
86 {
88 }
89 pag->callGraph = callGraphBuilder.buildSVFIRCallGraph(funset);
90
91 CHGraph* chg = new CHGraph();
93 chgbuilder.buildCHG();
94 pag->setCHG(chg);
95
98 {
99 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
100 {
101 const Function& fun = *F;
102 const FunObjVar* svffun = llvmModuleSet()->getFunObjVar(&fun);
104 if(!fun.isDeclaration())
105 {
111 if (fun.doesNotReturn() == false &&
112 fun.getReturnType()->isVoidTy() == false)
113 {
116 }
117
120 for (Function::const_arg_iterator I = fun.arg_begin(), E = fun.arg_end();
121 I != E; ++I)
122 {
123 setCurrentLocation(&*I,&fun.getEntryBlock());
125 // if this is the function does not have caller (e.g. main)
126 // or a dead function, shall we create a black hole address edge for it?
127 // it is (1) too conservative, and (2) make FormalParmVFGNode defined at blackhole address PAGEdge.
128 // if(SVFUtil::ArgInNoCallerFunction(&*I)) {
129 // if(I->getType()->isPointerTy())
130 // addBlackHoleAddrEdge(argValNodeId);
131 //}
133 }
134 }
135 for (Function::const_iterator bit = fun.begin(), ebit = fun.end();
136 bit != ebit; ++bit)
137 {
138 const BasicBlock& bb = *bit;
139 for (BasicBlock::const_iterator it = bb.begin(), eit = bb.end();
140 it != eit; ++it)
141 {
142 const Instruction& inst = *it;
143 setCurrentLocation(&inst,&bb);
144 visit(const_cast<Instruction&>(inst));
145 }
146 }
147 }
148 }
149
150 sanityCheck();
151
153
155
156 // dump SVFIR
158 pag->dump("svfir_initial");
159
160 // print to command line of the SVFIR graph
161 if (Options::PAGPrint())
162 pag->print();
163
164 // dump ICFG
165 if (Options::DumpICFG())
166 pag->getICFG()->dump("icfg_initial");
167
169 {
172 }
173
174 // dump SVFIR as JSON
175 if (!Options::DumpJson().empty())
176 {
177 assert(false && "please implement SVFIRWriter::writeJsonToPath");
178 }
179
180 double endTime = SVFStat::getClk(true);
181 SVFStat::timeOfBuildingSVFIR = (endTime - startTime) / TIMEINTERVAL;
182
183 return pag;
184}
185
187{
189 {
191 for (const Function& f : mod.functions())
192 {
195
196 if (!LLVMUtil::isExtCall(&f))
197 {
199 }
202 svffun->setRelDefFun(realfun == nullptr ? nullptr : llvmModuleSet()->getFunObjVar(realfun));
203 }
204 }
205
206 // Store annotations of functions in extapi.bc
207 for (const auto& pair : llvmModuleSet()->ExtFun2Annotations)
208 {
210 }
211
212}
213
215{
217 for (Function::const_iterator bit = func->begin(), ebit = func->end(); bit != ebit; ++bit)
218 {
219 const BasicBlock* bb = &*bit;
222 {
225 }
227 {
230 }
231
233 if (svfbb->getSuccessors().empty())
234 {
236 {
238 SVFUtil::isa<ReturnInst>(bb->back())) &&
239 "last inst must be return inst");
240 svfFun->setExitBlock(svfbb);
241 }
242 }
243 }
244 // For no return functions, we set the last block as exit BB
245 // This ensures that each function that has definition must have an exit BB
246 if (svfFun->hasBasicBlock() && svfFun->exitBlock == nullptr)
247 {
248 SVFBasicBlock* retBB = const_cast<SVFBasicBlock*>(svfFun->back());
250 SVFUtil::isa<ReturnInst>(&func->back().back())) &&
251 "last inst must be return inst");
252 svfFun->setExitBlock(retBB);
253 }
254}
255
256
258{
259 if (fun->isDeclaration())
260 return;
261 //process and stored dt & df
264 df.analyze(dt);
266 PostDominatorTree pdt = PostDominatorTree(const_cast<Function&>(*fun));
267 SVFLoopAndDomInfo* ld = svffun->getLoopAndDomInfo();
268
270 for (DominanceFrontierBase::const_iterator dfIter = df.begin(), eDfIter = df.end(); dfIter != eDfIter; dfIter++)
271 {
272 const BasicBlock* keyBB = dfIter->first;
273#if LLVM_VERSION_MAJOR > 16
274 const llvm::SetVector<llvm::BasicBlock* >& domSet = dfIter->second;
275#else
276 const std::set<BasicBlock* >& domSet = dfIter->second;
277#endif
279 for (const BasicBlock* bbValue:domSet)
280 {
282 }
283 }
284 std::vector<const SVFBasicBlock*> reachableBBs;
285 LLVMUtil::getFunReachableBBs(fun, reachableBBs);
286 ld->setReachableBBs(reachableBBs);
287
288 for (Function::const_iterator bit = fun->begin(), beit = fun->end(); bit!=beit; ++bit)
289 {
290 const BasicBlock &bb = *bit;
292 if (DomTreeNode* dtNode = dt.getNode(&bb))
293 {
294 SVFLoopAndDomInfo::BBSet& bbSet = ld->getDomTreeMap()[svfBB];
295 for (const auto domBB : *dtNode)
296 {
297 const auto* domSVFBB = llvmModuleSet()->getSVFBasicBlock(domBB->getBlock());
298 bbSet.insert(domSVFBB);
299 }
300 }
301
302 if (DomTreeNode* pdtNode = pdt.getNode(&bb))
303 {
304 u32_t level = pdtNode->getLevel();
305 ld->getBBPDomLevel()[svfBB] = level;
306 BasicBlock* idomBB = pdtNode->getIDom()->getBlock();
308 ld->getBB2PIdom()[svfBB] = idom;
309
310 SVFLoopAndDomInfo::BBSet& bbSet = ld->getPostDomTreeMap()[svfBB];
311 for (const auto domBB : *pdtNode)
312 {
313 const auto* domSVFBB = llvmModuleSet()->getSVFBasicBlock(domBB->getBlock());
314 bbSet.insert(domSVFBB);
315 }
316 }
317
318 if (const Loop* loop = loopInfo.getLoopFor(&bb))
319 {
320 for (const BasicBlock* loopBlock : loop->getBlocks())
321 {
323 ld->addToBB2LoopMap(svfBB, loopbb);
324 }
325 }
326 }
327}
328
330{
331 std::vector<FunObjVar*> funset;
332 // Iterate over all object symbols in the symbol table
333 for (const auto* fun: llvmModuleSet()->getFunctionSet())
334 {
335 u32_t id = llvmModuleSet()->objSyms()[fun];
336 // Debug output for adding object node
337 DBOUT(DPAGBuild, outs() << "add obj node " << id << "\n");
338
339 // Check if the value is a function and add a function object node
340 pag->addFunObjNode(id, pag->getObjTypeInfo(id), nullptr);
342
343 FunObjVar *funObjVar = SVFUtil::cast<FunObjVar>(pag->getGNode(id));
344 funset.push_back(funObjVar);
345
346 funObjVar->initFunObjVar(fun->isDeclaration(), LLVMUtil::isIntrinsicFun(fun), fun->hasAddressTaken(),
348 SVFUtil::cast<SVFFunctionType>(llvmModuleSet()->getSVFType(fun->getFunctionType())),
349 new SVFLoopAndDomInfo, nullptr, nullptr,
350 {}, nullptr);
351 BasicBlockGraph* bbGraph = new BasicBlockGraph();
352 funObjVar->setBasicBlockGraph(bbGraph);
353
354
355 for (const BasicBlock& bb : *fun)
356 {
357 llvmModuleSet()->addBasicBlock(funObjVar, &bb);
358 }
359
361 for (auto& bb: *funObjVar->bbGraph)
362 {
363 bb.second->setFun(funObjVar);
364 }
366 }
367
369}
370
372{
373 // Iterate over all object symbols in the symbol table
374 for (LLVMModuleSet::ValueToIDMapTy::iterator iter =
375 llvmModuleSet()->objSyms().begin(); iter != llvmModuleSet()->objSyms().end();
376 ++iter)
377 {
378 // Debug output for adding object node
379 DBOUT(DPAGBuild, outs() << "add obj node " << iter->second << "\n");
380
381 // Skip blackhole and constant symbols
382 if(iter->second == pag->blackholeSymID() || iter->second == pag->constantSymID())
383 continue;
384
385 // Get the LLVM value corresponding to the symbol
386 const Value* llvmValue = iter->first;
387
388 const ICFGNode* icfgNode = nullptr;
389 if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(llvmValue))
390 {
391 if(llvmModuleSet()->hasICFGNode(inst))
392 icfgNode = llvmModuleSet()->getICFGNode(inst);
393 }
394
395 // Check if the value is a function and add a function object node
396 if (SVFUtil::dyn_cast<Function>(llvmValue))
397 {
398 // already one
399 }
400 // Check if the value is a heap object and add a heap object node
402 {
403 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
404 pag->addHeapObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
405 }
406 // Check if the value is an alloca instruction and add a stack object node
408 {
409 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
410 pag->addStackObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
411 }
412 else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
413 {
414 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
416 }
417 else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
418 {
419 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
421 }
422 else if (SVFUtil::isa<ConstantPointerNull>(llvmValue))
423 {
424 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
425 pag->addConstantNullPtrObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
426 }
427 else if (SVFUtil::isa<GlobalValue>(llvmValue))
428 {
429 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
430 pag->addGlobalObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
431 }
432 else if (SVFUtil::isa<ConstantData, ConstantExpr, MetadataAsValue, BlockAddress>(llvmValue))
433 {
434 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
435 pag->addConstantDataObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
436 }
437 else if (SVFUtil::isa<ConstantAggregate>(llvmValue))
438 {
439 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
440 pag->addConstantAggObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
441 }
442 // Add a generic object node for other types of values
443 else
444 {
445 NodeID id = llvmModuleSet()->getObjectNode(iter->first);
446 pag->addObjNode(iter->second, pag->getObjTypeInfo(id), icfgNode);
447 }
449 }
450
451}
452
454{
455 // Iterate over all value symbols in the symbol table
456 for (LLVMModuleSet::ValueToIDMapTy::iterator iter =
457 llvmModuleSet()->valSyms().begin(); iter != llvmModuleSet()->valSyms().end();
458 ++iter)
459 {
460 // Debug output for adding value node
461 DBOUT(DPAGBuild, outs() << "add val node " << iter->second << "\n");
462
463 // Skip blackhole and null pointer symbols
464 if(iter->second == pag->blkPtrSymID() || iter->second == pag->nullPtrSymID())
465 continue;
466
467 const ICFGNode* icfgNode = nullptr;
468 auto llvmValue = iter->first;
469
470 // Check if the value is a function and get its call graph node
471 if (const Function* func = SVFUtil::dyn_cast<Function>(llvmValue))
472 {
473 pag->addFunValNode(iter->second, icfgNode, llvmModuleSet()->getFunObjVar(func), llvmModuleSet()->getSVFType(llvmValue->getType()));
474 }
475 else if (auto argval = SVFUtil::dyn_cast<Argument>(llvmValue))
476 {
477 // Formal params are defined at FunEntryICFGNode (where CallPE copies actual args).
478 // External (declaration-only) functions have no entry node, so keep nullptr.
479 const FunObjVar* funObj = llvmModuleSet()->getFunObjVar(argval->getParent());
480 const ICFGNode* entryNode = funObj->isDeclaration() ? nullptr : pag->getICFG()->getFunEntryICFGNode(funObj);
482 iter->second, argval->getArgNo(), entryNode,
483 funObj, llvmModuleSet()->getSVFType(llvmValue->getType()));
484 if (!argval->hasName())
485 pag->getGNode(iter->second)->setName("arg_" + std::to_string(argval->getArgNo()));
486 }
487 else if (auto fpValue = SVFUtil::dyn_cast<ConstantFP>(llvmValue))
488 {
489 pag->addConstantFPValNode(iter->second, LLVMUtil::getDoubleValue(fpValue), icfgNode, llvmModuleSet()->getSVFType(llvmValue->getType()));
490 }
491 else if (auto intValue = SVFUtil::dyn_cast<ConstantInt>(llvmValue))
492 {
493 pag->addConstantIntValNode(iter->second, LLVMUtil::getIntegerValue(intValue), icfgNode, llvmModuleSet()->getSVFType(llvmValue->getType()));
494 }
495 else if (SVFUtil::isa<ConstantPointerNull>(llvmValue))
496 {
497 pag->addConstantNullPtrValNode(iter->second, icfgNode, llvmModuleSet()->getSVFType(llvmValue->getType()));
498 }
499 else if (SVFUtil::isa<GlobalValue>(llvmValue))
500 {
501 // Global variables are defined at the global ICFG node.
503 llvmModuleSet()->getSVFType(llvmValue->getType()));
504 }
505 else if (SVFUtil::isa<ConstantData, ConstantExpr, MetadataAsValue, BlockAddress>(llvmValue))
506 {
507 pag->addConstantDataValNode(iter->second, icfgNode, llvmModuleSet()->getSVFType(llvmValue->getType()));
508 }
509 else if (SVFUtil::isa<ConstantAggregate>(llvmValue))
510 {
511 pag->addConstantAggValNode(iter->second, icfgNode, llvmModuleSet()->getSVFType(llvmValue->getType()));
512 }
513 else if (SVFUtil::isa<BasicBlock>(llvmValue))
514 {
515 pag->addBasicBlockValNode(iter->second, llvmModuleSet()->getSVFType(llvmValue->getType()));
516 }
517 else if (SVFUtil::isa<InlineAsm>(llvmValue) ||
518 SVFUtil::isa<DSOLocalEquivalent>(llvmValue) ||
519 SVFUtil::isa<NoCFIValue>(llvmValue))
520 {
521 pag->addAsmPCValNode(iter->second, llvmModuleSet()->getSVFType(llvmValue->getType()));
522 }
523 else if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(llvmValue))
524 {
526 pag->addIntrinsicValNode(iter->second, llvmModuleSet()->getSVFType(llvmValue->getType()));
527 else
528 {
529 assert(llvmModuleSet()->hasICFGNode(inst) && "LLVM instruction is not associated with an ICFGNode");
530 icfgNode = llvmModuleSet()->getICFGNode(inst);
531 pag->addValNode(iter->second, llvmModuleSet()->getSVFType(llvmValue->getType()), icfgNode);
532 }
533 }
535 pag->getGNode(iter->second));
536 }
537}
538
539
540/*
541 * Initial all the nodes from symbol table
542 */
544{
545 DBOUT(DPAGBuild, outs() << "Initialise SVFIR Nodes ...\n");
546
547
552
555
556 for (LLVMModuleSet::FunToIDMapTy::iterator iter =
557 llvmModuleSet()->retSyms().begin(); iter != llvmModuleSet()->retSyms().end();
558 ++iter)
559 {
560 const Value* llvmValue = iter->first;
561 // retSyms keys are Function*, not Instruction, so dyn_cast<Instruction> always fails.
562 // RetValPN represents the callee's return value, defined at FunExitICFGNode.
563 // External functions have no exit node, so keep nullptr.
564 const FunObjVar* funObjVar = llvmModuleSet()->getFunObjVar(SVFUtil::cast<Function>(llvmValue));
565 const ICFGNode* icfgNode = funObjVar->isDeclaration() ? nullptr : pag->getICFG()->getFunExitICFGNode(funObjVar);
566 DBOUT(DPAGBuild, outs() << "add ret node " << iter->second << "\n");
567 pag->addRetNode(iter->second,
568 funObjVar,
569 llvmModuleSet()->getSVFType(iter->first->getType()), icfgNode);
571 pag->returnFunObjSymMap[funObjVar] = iter->second;
572 }
573
574 for (LLVMModuleSet::FunToIDMapTy::iterator iter =
575 llvmModuleSet()->varargSyms().begin();
576 iter != llvmModuleSet()->varargSyms().end(); ++iter)
577 {
578 const Value* llvmValue = iter->first;
579 // varargSyms keys are Function*, not Instruction.
580 // Variadic arguments are received at the function entry point.
581 // External functions have no entry node, so keep nullptr.
582 const FunObjVar* funObjVar = llvmModuleSet()->getFunObjVar(SVFUtil::cast<Function>(llvmValue));
583 const ICFGNode* icfgNode = funObjVar->isDeclaration() ? nullptr : pag->getICFG()->getFunEntryICFGNode(funObjVar);
584 DBOUT(DPAGBuild, outs() << "add vararg node " << iter->second << "\n");
585 pag->addVarargNode(iter->second,
586 funObjVar,
587 llvmModuleSet()->getSVFType(iter->first->getType()), icfgNode);
589 pag->varargFunObjSymMap[funObjVar] = iter->second;
590 }
591
593 for (LLVMModuleSet::ValueToIDMapTy::iterator iter =
594 llvmModuleSet()->objSyms().begin(); iter != llvmModuleSet()->objSyms().end(); ++iter)
595 {
596 DBOUT(DPAGBuild, outs() << "add address edges for constant node " << iter->second << "\n");
597 const Value* val = iter->first;
599 {
601 if(ptr!= pag->getBlkPtr() && ptr!= pag->getNullPtr())
602 {
604 addAddrEdge(iter->second, ptr);
605 }
606 }
607 }
608
610 && "not all node have been initialized!!!");
611
613 for (auto& fun: llvmModuleSet()->getFunctionSet())
614 {
615 for (const Argument& arg : fun->args())
616 {
617 const_cast<FunObjVar*>(llvmModuleSet()->getFunObjVar(fun))->addArgument(SVFUtil::cast<ArgValVar>(
619 }
620 }
621
622}
623
624/*
625 https://github.com/SVF-tools/SVF/issues/524
626 Handling single value types, for constant index, including pointer, integer, etc
627 e.g. field_idx = getelementptr i8, %i8* %p, i64 -4
628 We can obtain the field index by inferring the byteoffset if %p is casted from a pointer to a struct
629 For another example, the following can be an array access.
630 e.g. field_idx = getelementptr i8, %struct_type %p, i64 1
631
632*/
634{
635 return 0;
636}
637
645{
646 assert(V);
647
648 const llvm::GEPOperator *gepOp = SVFUtil::dyn_cast<const llvm::GEPOperator>(V);
649 DataLayout * dataLayout = getDataLayout(llvmModuleSet()->getMainLLVMModule());
650 llvm::APInt byteOffset(dataLayout->getIndexSizeInBits(gepOp->getPointerAddressSpace()),0,true);
651 if(gepOp && dataLayout && gepOp->accumulateConstantOffset(*dataLayout,byteOffset))
652 {
653 //s32_t bo = byteOffset.getSExtValue();
654 }
655
656 bool isConst = true;
657
658 bool prevPtrOperand = false;
659 for (bridge_gep_iterator gi = bridge_gep_begin(*V), ge = bridge_gep_end(*V);
660 gi != ge; ++gi)
661 {
662 const Type* gepTy = *gi;
664
665 assert((prevPtrOperand && svfGepTy->isPointerTy()) == false &&
666 "Expect no more than one gep operand to be of a pointer type");
667 if(!prevPtrOperand && svfGepTy->isPointerTy()) prevPtrOperand = true;
668 const Value* offsetVal = gi.getOperand();
669 assert(gepTy != offsetVal->getType() && "iteration and operand have the same type?");
670
671 const ArrayType* inferredPtrArrayTy = nullptr;
672 const SVFType* idxGepTy = svfGepTy;
673 if (svfGepTy->isPointerTy() && gepOp->getSourceElementType()->isSingleValueType())
674 {
675 const Type* baseObjType =
677 if (const auto* arrTy = SVFUtil::dyn_cast<ArrayType>(baseObjType))
678 {
679 if (arrTy->getElementType()->isPointerTy())
680 {
683 }
684 }
685 }
686
688
689 //The int value of the current index operand
690 const ConstantInt* op = SVFUtil::dyn_cast<ConstantInt>(offsetVal);
691
692 // if Options::ModelConsts() is disabled. We will treat whole array as one,
693 // but we can distinguish different field of an array of struct, e.g. s[1].f1 is different from s[0].f2
694 if(const ArrayType* arrTy = SVFUtil::dyn_cast<ArrayType>(gepTy))
695 {
696 if (!Options::ModelArrays() && arrTy->getElementType()->isPointerTy())
697 continue;
698 if(!op || (arrTy->getArrayNumElements() <= (u32_t)LLVMUtil::getIntegerValue(op).first))
699 continue;
703 }
704 else if (const StructType *ST = SVFUtil::dyn_cast<StructType>(gepTy))
705 {
706 assert(op && "non-const offset accessing a struct");
707 // guard against negative or out-of-bounds struct indices
708 // (e.g. rust hashbrown bucket back-offset: gep { ... }, ptr %p, i64 -1)
709 // a negative i64 wraps to a huge uint64_t that overflows u32_t,
710 // creating an invalid field index that severs points-to tracking
712 if (rawIdx >= ST->getNumElements())
713 {
714 isConst = false;
715 continue;
716 }
720 }
721 else if (gepTy->isSingleValueType())
722 {
724 {
725 if (!op || (inferredPtrArrayTy->getArrayNumElements() <= (u32_t)LLVMUtil::getIntegerValue(op).first))
726 continue;
730 continue;
731 }
732 // If it's a non-constant offset access
733 // If its point-to target is struct or array, it's likely an array accessing (%result = gep %struct.A* %a, i32 %non-const-index)
734 // If its point-to target is single value (pointer arithmetic), then it's a variant gep (%result = gep i8* %p, i32 %non-const-index)
735 if(!op && gepTy->isPointerTy() && gepOp->getSourceElementType()->isSingleValueType())
736 {
737 isConst = false;
738 }
739
740 // The actual index
741 //s32_t idx = op->getSExtValue();
742
743 // For pointer arithmetic we ignore the byte offset
744 // consider using inferFieldIdxFromByteOffset(geopOp,dataLayout,ap,idx)?
745 // ap.setFldIdx(ap.getConstantFieldIdx() + inferFieldIdxFromByteOffset(geopOp,idx));
746 }
747 }
748 return isConst;
749}
750
755{
756 if (const Constant* ref = SVFUtil::dyn_cast<Constant>(val))
757 {
759 {
760 DBOUT(DPAGBuild, outs() << "handle gep constant expression "
761 << LLVMUtil::dumpValue(ref) << "\n");
762 const Constant* opnd = gepce->getOperand(0);
763 // handle recursive constant express case (gep (bitcast (gep X 1)) 1)
765 auto &GEPOp = llvm::cast<llvm::GEPOperator>(*gepce);
766 Type *pType = GEPOp.getSourceElementType();
767 AccessPath ap(0, llvmModuleSet()->getSVFType(pType));
768 bool constGep = computeGepOffset(gepce, ap);
769 // must invoke pag methods here, otherwise it will be a dead recursion cycle
770 const Value* cval = getCurrentValue();
771 const SVFBasicBlock* cbb = getCurrentBB();
773 /*
774 * The gep edge created are like constexpr (same edge may appear at multiple callsites)
775 * so bb/inst of this edge may be rewritten several times, we treat it as global here.
776 */
779 }
780 else if (const ConstantExpr* castce = isCastConstantExpr(ref))
781 {
782 DBOUT(DPAGBuild, outs() << "handle cast constant expression "
783 << LLVMUtil::dumpValue(ref) << "\n");
784 const Constant* opnd = castce->getOperand(0);
786 const Value* cval = getCurrentValue();
787 const SVFBasicBlock* cbb = getCurrentBB();
791 }
793 {
794 DBOUT(DPAGBuild, outs() << "handle select constant expression "
795 << LLVMUtil::dumpValue(ref) << "\n");
796 const Constant* src1 = selectce->getOperand(1);
797 const Constant* src2 = selectce->getOperand(2);
800 const Value* cval = getCurrentValue();
801 const SVFBasicBlock* cbb = getCurrentBB();
803 NodeID cond = llvmModuleSet()->getValueNode(selectce->getOperand(0));
809 }
810 // if we meet a int2ptr, then it points-to black hole
812 {
813 const Constant* opnd = int2Ptrce->getOperand(0);
815 const SVFBasicBlock* cbb = getCurrentBB();
816 const Value* cval = getCurrentValue();
820 }
822 {
823 const Constant* opnd = ptr2Intce->getOperand(0);
825 const SVFBasicBlock* cbb = getCurrentBB();
826 const Value* cval = getCurrentValue();
830 }
832 {
833 // we don't handle trunc and cmp instruction for now
834 const Value* cval = getCurrentValue();
835 const SVFBasicBlock* cbb = getCurrentBB();
840 }
841 else if (isBinaryConstantExpr(ref))
842 {
843 // we don't handle binary constant expression like add(x,y) now
844 const Value* cval = getCurrentValue();
845 const SVFBasicBlock* cbb = getCurrentBB();
850 }
851 else if (isUnaryConstantExpr(ref))
852 {
853 // we don't handle unary constant expression like fneg(x) now
854 const Value* cval = getCurrentValue();
855 const SVFBasicBlock* cbb = getCurrentBB();
860 }
861 else if (SVFUtil::isa<ConstantAggregate>(ref))
862 {
863 // we don't handle constant aggregate like constant vectors
864 }
865 else if (SVFUtil::isa<BlockAddress>(ref))
866 {
867 // blockaddress instruction (e.g. i8* blockaddress(@run_vm, %182))
868 // is treated as constant data object for now, see LLVMUtil.h:397, SymbolTableInfo.cpp:674 and SVFIRBuilder.cpp:183-194
869 const Value* cval = getCurrentValue();
870 const SVFBasicBlock* cbb = getCurrentBB();
875 }
876 else
877 {
878 if(SVFUtil::isa<ConstantExpr>(val))
879 assert(false && "we don't handle all other constant expression for now!");
880 }
881 }
882}
889{
890
891 // if the global variable do not have any field needs to be initialized
892 if (offset == 0 && gvar->getInitializer()->getType()->isSingleValueType())
893 {
894 return getValueNode(gvar);
895 }
898 else
899 {
901 }
902}
903
904/*For global variable initialization
905 * Give a simple global variable
906 * int x = 10; // store 10 x (constant, non pointer) |
907 * int *y = &x; // store x y (pointer type)
908 * Given a struct
909 * struct Z { int s; int *t;};
910 * Global initialization:
911 * struct Z z = {10,&x}; // store x z.t (struct type)
912 * struct Z *m = &z; // store z m (pointer type)
913 * struct Z n = {10,&z.s}; // store z.s n , &z.s constant expression (constant expression)
914 */
917{
918 DBOUT(DPAGBuild, outs() << "global " << LLVMUtil::dumpValue(gvar)
919 << " constant initializer: "
920 << LLVMUtil::dumpValue(C) << "\n");
921 if (C->getType()->isSingleValueType())
922 {
923 NodeID src = getValueNode(C);
924 // get the field value if it is available, otherwise we create a dummy field node.
926 NodeID field = getGlobalVarField(gvar, offset, llvmModuleSet()->getSVFType(C->getType()));
927
928 if (SVFUtil::isa<GlobalVariable, Function>(C))
929 {
931 addStoreEdge(src, field);
932 }
933 else if (SVFUtil::isa<ConstantExpr>(C))
934 {
935 // add gep edge of C1 itself is a constant expression
936 processCE(C);
938 addStoreEdge(src, field);
939 }
940 else if (SVFUtil::isa<BlockAddress>(C))
941 {
942 // blockaddress instruction (e.g. i8* blockaddress(@run_vm, %182))
943 // is treated as constant data object for now, see LLVMUtil.h:397, SymbolTableInfo.cpp:674 and SVFIRBuilder.cpp:183-194
944 processCE(C);
947 }
948 else
949 {
951 addStoreEdge(src, field);
953 if (C->getType()->isPtrOrPtrVectorTy() && src != pag->getNullPtr())
955 }
956 }
957 else if (SVFUtil::isa<ConstantArray, ConstantStruct>(C))
958 {
960 return;
961 for (u32_t i = 0, e = C->getNumOperands(); i != e; i++)
962 {
964 InitialGlobal(gvar, SVFUtil::cast<Constant>(C->getOperand(i)), offset + off);
965 }
966 }
967 else if(ConstantData* data = SVFUtil::dyn_cast<ConstantData>(C))
968 {
970 {
971 if(ConstantDataSequential* seq = SVFUtil::dyn_cast<ConstantDataSequential>(data))
972 {
973 for(u32_t i = 0; i < seq->getNumElements(); i++)
974 {
975 u32_t off = pag->getFlattenedElemIdx(llvmModuleSet()->getSVFType(C->getType()), i);
976 Constant* ct = seq->getElementAsConstant(i);
978 }
979 }
980 else
981 {
982 assert((SVFUtil::isa<ConstantAggregateZero, UndefValue>(data)) && "Single value type data should have been handled!");
983 }
984 }
985 }
986 else
987 {
988 //TODO:assert(SVFUtil::isa<ConstantVector>(C),"what else do we have");
989 }
990}
991
996{
997
1000 {
1001 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
1002 {
1003 GlobalVariable *gvar = &*I;
1006
1009
1010 if (gvar->hasInitializer())
1011 {
1012 Constant *C = gvar->getInitializer();
1013 DBOUT(DPAGBuild, outs() << "add global var node "
1014 << LLVMUtil::dumpValue(gvar) << "\n");
1015 InitialGlobal(gvar, C, 0);
1016 }
1017 }
1018
1019
1021 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
1022 {
1023 const Function* fun = &*I;
1024 NodeID idx = getValueNode(fun);
1025 NodeID obj = getObjectNode(fun);
1026
1027 DBOUT(DPAGBuild, outs() << "add global function node " << fun->getName().str() << "\n");
1028 setCurrentLocation(fun, (SVFBasicBlock*) nullptr);
1030 }
1031
1032 // Handle global aliases (due to linkage of multiple bc files), e.g., @x = internal alias @y. We need to add a copy from y to x.
1033 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E; I++)
1034 {
1035 const GlobalAlias* alias = &*I;
1036 NodeID dst = llvmModuleSet()->getValueNode(alias);
1037 NodeID src = llvmModuleSet()->getValueNode(alias->getAliasee());
1038 processCE(alias->getAliasee());
1039 setCurrentLocation(alias, (SVFBasicBlock*) nullptr);
1040 addCopyEdge(src, dst, CopyStmt::COPYVAL);
1041 }
1042 }
1043}
1044
1050{
1051
1052 // AllocaInst should always be a pointer type
1053 assert(SVFUtil::isa<PointerType>(inst.getType()));
1054
1056 outs() << "process alloca " << LLVMUtil::dumpValue(&inst) << "\n");
1057 NodeID dst = getValueNode(&inst);
1058
1059 NodeID src = getObjectNode(&inst);
1060
1061 addAddrWithStackArraySz(src, dst, inst);
1062
1063}
1064
1069{
1070
1072 outs() << "process phi " << LLVMUtil::dumpValue(&inst) << "\n");
1073
1074 NodeID dst = getValueNode(&inst);
1075
1076 for (u32_t i = 0; i < inst.getNumIncomingValues(); ++i)
1077 {
1078 const Value* val = inst.getIncomingValue(i);
1079 const Instruction* incomingInst = SVFUtil::dyn_cast<Instruction>(val);
1080 bool matched = (incomingInst == nullptr ||
1081 incomingInst->getFunction() == inst.getFunction());
1082 (void) matched; // Suppress warning of unused variable under release build
1083 assert(matched && "incomingInst's Function incorrect");
1084 const Instruction* predInst = &inst.getIncomingBlock(i)->back();
1085 const ICFGNode* icfgNode = llvmModuleSet()->getICFGNode(predInst);
1086 NodeID src = getValueNode(val);
1087 addPhiStmt(dst,src,icfgNode);
1088 }
1089}
1090
1091/*
1092 * Visit load instructions
1093 */
1095{
1097 outs() << "process load " << LLVMUtil::dumpValue(&inst) << "\n");
1098
1099 NodeID dst = getValueNode(&inst);
1100
1101 NodeID src = getValueNode(inst.getPointerOperand());
1102 const Type* loadedTy = inst.getType();
1103 if (NodeID fieldZero = getDirectAccessFieldZeroValVar(inst.getPointerOperand(), loadedTy))
1104 src = fieldZero;
1105
1106 addLoadEdge(src, dst);
1107}
1108
1113{
1114 // StoreInst itself should always not be a pointer type
1115 assert(!SVFUtil::isa<PointerType>(inst.getType()));
1116
1118 outs() << "process store " << LLVMUtil::dumpValue(&inst) << "\n");
1119
1120 NodeID dst = getValueNode(inst.getPointerOperand());
1121 const Type* storedTy = inst.getValueOperand()->getType();
1122 if (NodeID fieldZero = getDirectAccessFieldZeroValVar(inst.getPointerOperand(), storedTy))
1123 dst = fieldZero;
1124
1125 NodeID src = getValueNode(inst.getValueOperand());
1126
1127 addStoreEdge(src, dst);
1128
1129}
1130
1135{
1136
1137 NodeID dst = getValueNode(&inst);
1138 // GetElementPtrInst should always be a pointer or a vector contains pointers
1139 // for now we don't handle vector type here
1140 if(SVFUtil::isa<VectorType>(inst.getType()))
1141 {
1143 return;
1144 }
1145
1146 assert(SVFUtil::isa<PointerType>(inst.getType()));
1147
1149 outs() << "process gep " << LLVMUtil::dumpValue(&inst) << "\n");
1150
1151 NodeID src = getValueNode(inst.getPointerOperand());
1152
1153 AccessPath ap(0, llvmModuleSet()->getSVFType(inst.getSourceElementType()));
1154 bool constGep = computeGepOffset(&inst, ap);
1156 {
1157 const Type* baseObjType =
1158 LLVMModuleSet::getLLVMModuleSet()->getTypeInference()->inferObjType(inst.getPointerOperand());
1159 if (const auto* arrTy = SVFUtil::dyn_cast<ArrayType>(baseObjType))
1160 {
1161 if (arrTy->getElementType()->isPointerTy())
1162 {
1163 addCopyEdge(src, dst, CopyStmt::COPYVAL);
1164 return;
1165 }
1166 }
1167 }
1168 addGepEdge(src, dst, ap, constGep);
1169}
1170
1171/*
1172 * Visit cast instructions
1173 */
1175{
1176
1178 outs() << "process cast " << LLVMUtil::dumpValue(&inst) << "\n");
1179 NodeID dst = getValueNode(&inst);
1180
1181 const Value* opnd = inst.getOperand(0);
1182 NodeID src = getValueNode(opnd);
1183 addCopyEdge(src, dst, getCopyKind(&inst));
1184}
1185
1190{
1191 NodeID dst = getValueNode(&inst);
1192 assert(inst.getNumOperands() == 2 && "not two operands for BinaryOperator?");
1193 Value* op1 = inst.getOperand(0);
1195 Value* op2 = inst.getOperand(1);
1197 u32_t opcode = inst.getOpcode();
1198 addBinaryOPEdge(op1Node, op2Node, dst, opcode);
1199}
1200
1205{
1206 NodeID dst = getValueNode(&inst);
1207 assert(inst.getNumOperands() == 1 && "not one operand for Unary instruction?");
1208 Value* opnd = inst.getOperand(0);
1209 NodeID src = getValueNode(opnd);
1210 u32_t opcode = inst.getOpcode();
1211 addUnaryOPEdge(src, dst, opcode);
1212}
1213
1218{
1219 NodeID dst = getValueNode(&inst);
1220 assert(inst.getNumOperands() == 2 && "not two operands for compare instruction?");
1221 Value* op1 = inst.getOperand(0);
1223 Value* op2 = inst.getOperand(1);
1225 u32_t predicate = inst.getPredicate();
1226 addCmpEdge(op1Node, op2Node, dst, predicate);
1227}
1228
1229
1234{
1235
1237 outs() << "process select " << LLVMUtil::dumpValue(&inst) << "\n");
1238
1239 NodeID dst = getValueNode(&inst);
1240 NodeID src1 = getValueNode(inst.getTrueValue());
1241 NodeID src2 = getValueNode(inst.getFalseValue());
1242 NodeID cond = getValueNode(inst.getCondition());
1244 addSelectStmt(dst,src1,src2, cond);
1245}
1246
1251
1256
1261
1262/*
1263 * Visit callsites
1264 */
1266{
1267
1268 // skip llvm intrinsics
1269 if(isIntrinsicInst(cs))
1270 return;
1271
1273 outs() << "process callsite " << LLVMUtil::dumpValue(cs) << "\n");
1274
1275 CallICFGNode* callBlockNode = llvmModuleSet()->getCallICFGNode(cs);
1277
1278 pag->addCallSite(callBlockNode);
1279
1281 for (u32_t i = 0; i < cs->arg_size(); i++)
1283 callBlockNode,
1284 pag->getValVar(getValueNode(cs->getArgOperand(i))));
1285
1286 if(!cs->getType()->isVoidTy())
1288
1289 if (callBlockNode->isVirtualCall())
1290 {
1291 const Value* value = cppUtil::getVCallVtblPtr(cs);
1292 callBlockNode->setVtablePtr(pag->getGNode(getValueNode(value)));
1293 }
1294 if (const Function *callee = LLVMUtil::getCallee(cs))
1295 {
1297 {
1298 handleExtCall(cs, callee);
1299 }
1300 else
1301 {
1303 }
1304 }
1305 else
1306 {
1307 //If the callee was not identified as a function (null F), this is indirect.
1308 handleIndCall(cs);
1309 }
1310}
1311
1316{
1317
1318 // ReturnInst itself should always not be a pointer type
1319 assert(!SVFUtil::isa<PointerType>(inst.getType()));
1320
1322 outs() << "process return " << LLVMUtil::dumpValue(&inst) << "\n");
1323
1324 if(Value* src = inst.getReturnValue())
1325 {
1326 const FunObjVar *F = llvmModuleSet()->getFunObjVar(inst.getParent()->getParent());
1327
1329 NodeID vnS = getValueNode(src);
1330 const ICFGNode* icfgNode = llvmModuleSet()->getICFGNode(&inst);
1331 //vnS may be null if src is a null ptr
1332 addPhiStmt(rnF,vnS,icfgNode);
1333 }
1334}
1335
1336
1350
1364
1370{
1371 NodeID brinst = getValueNode(&inst);
1372 NodeID cond;
1373 if (inst.isConditional())
1374 cond = getValueNode(inst.getCondition());
1375 else
1376 cond = pag->getNullPtr();
1377
1378 assert(inst.getNumSuccessors() <= 2 && "if/else has more than two branches?");
1379
1381 std::vector<const Instruction*> nextInsts;
1383 u32_t branchID = 0;
1384 for (const Instruction* succInst : nextInsts)
1385 {
1386 assert(branchID <= 1 && "if/else has more than two branches?");
1387 const ICFGNode* icfgNode = llvmModuleSet()->getICFGNode(succInst);
1388 successors.push_back(std::make_pair(icfgNode, 1-branchID));
1389 branchID++;
1390 }
1391 addBranchStmt(brinst, cond, successors);
1393 if (inst.isConditional())
1394 {
1395 for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
1396 {
1397 if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
1398 {
1399 intraEdge->setConditionVar(pag->getGNode(cond));
1400 }
1401 }
1402 }
1403}
1404
1405
1449
1452{
1453 NodeID brinst = getValueNode(&inst);
1454 NodeID cond = getValueNode(inst.getCondition());
1455
1457 std::vector<const Instruction*> nextInsts;
1459 for (const Instruction* succInst : nextInsts)
1460 {
1462 const ConstantInt* condVal = inst.findCaseDest(const_cast<BasicBlock*>(succInst->getParent()));
1464 s64_t val = -1;
1465 if (condVal && condVal->getBitWidth() <= 64)
1467 const ICFGNode* icfgNode = llvmModuleSet()->getICFGNode(succInst);
1468 successors.push_back(std::make_pair(icfgNode, val));
1469 }
1470 addBranchStmt(brinst, cond, successors);
1472 for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
1473 {
1474 if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
1475 {
1476 intraEdge->setConditionVar(pag->getGNode(cond));
1477 }
1478 }
1479}
1480
1481
1488{
1489 NodeID dst = getValueNode(&inst);
1490 Value* opnd = inst.getPointerOperand();
1491 NodeID src = getValueNode(opnd);
1492 addCopyEdge(src, dst, CopyStmt::COPYVAL);
1493}
1494
1500{
1501 NodeID dst = getValueNode(&inst);
1502 for (u32_t i = 0; i < inst.getNumOperands(); i++)
1503 {
1504 Value* opnd = inst.getOperand(i);
1505 NodeID src = getValueNode(opnd);
1506 addCopyEdge(src, dst, CopyStmt::COPYVAL);
1507 }
1508}
1509
1510
1515{
1516
1517 assert(F);
1520 DBOUT(DPAGBuild, outs() << "handle direct call " << LLVMUtil::dumpValue(cs)
1521 << " callee " << F->getName().str() << "\n");
1522
1523 //Only handle the ret.val. if it's used as a ptr.
1525 //Does it actually return a ptr?
1526 if (!cs->getType()->isVoidTy())
1527 {
1531 }
1532 //Iterators for the actual and formal parameters
1533 u32_t itA = 0, ieA = cs->arg_size();
1534 Function::const_arg_iterator itF = F->arg_begin(), ieF = F->arg_end();
1535 //Go through the fixed parameters.
1536 DBOUT(DPAGBuild, outs() << " args:");
1537 for (; itF != ieF; ++itA, ++itF)
1538 {
1539 //Some programs (e.g. Linux kernel) leave unneeded parameters empty.
1540 if (itA == ieA)
1541 {
1542 DBOUT(DPAGBuild, outs() << " !! not enough args\n");
1543 break;
1544 }
1545 const Value* AA = cs->getArgOperand(itA), *FA = &*itF; //current actual/formal arg
1546
1547 DBOUT(DPAGBuild, outs() << "process actual parm "
1548 << LLVMUtil::dumpValue(AA) << "\n");
1549
1554 }
1555 //Any remaining actual args must be varargs.
1556 if (F->isVarArg())
1557 {
1559 DBOUT(DPAGBuild, outs() << "\n varargs:");
1560 for (; itA != ieA; ++itA)
1561 {
1562 const Value* AA = cs->getArgOperand(itA);
1566 }
1567 }
1568 if(itA != ieA)
1569 {
1572 writeWrnMsg("too many args to non-vararg func.");
1573 writeWrnMsg("(" + callICFGNode->getSourceLoc() + ")");
1574
1575 }
1576}
1577
1610{
1611 const Value* value = stripAllCasts(V);
1612 assert(value && "null ptr?");
1614 [this](const GlobalVariable* glob, int64_t byteOffset) -> const Value*
1615 {
1616 if (!glob || !glob->hasInitializer())
1617 return nullptr;
1618
1619 auto* initializer = SVFUtil::dyn_cast<ConstantStruct>(glob->getInitializer());
1620 auto* structType = SVFUtil::dyn_cast<StructType>(glob->getValueType());
1621 if (!initializer || !structType)
1622 return nullptr;
1623
1624 DataLayout* dataLayout = getDataLayout(llvmModuleSet()->getMainLLVMModule());
1625 const StructLayout* layout =
1626 dataLayout->getStructLayout(const_cast<StructType*>(structType));
1627 for (u32_t fieldIdx = 0; fieldIdx < initializer->getNumOperands(); ++fieldIdx)
1628 {
1629 if (layout->getElementOffset(fieldIdx) != static_cast<uint64_t>(byteOffset))
1630 continue;
1631 if (auto* ptrValue =
1632 SVFUtil::dyn_cast<llvm::GlobalVariable>(initializer->getOperand(fieldIdx)))
1633 return ptrValue;
1634 return nullptr;
1635 }
1636 return nullptr;
1637 };
1638
1639 if(const GetElementPtrInst* gep = SVFUtil::dyn_cast<GetElementPtrInst>(value))
1640 {
1641 APOffset totalidx = 0;
1642 for (bridge_gep_iterator gi = bridge_gep_begin(gep), ge = bridge_gep_end(gep); gi != ge; ++gi)
1643 {
1644 if(const ConstantInt* op = SVFUtil::dyn_cast<ConstantInt>(gi.getOperand()))
1646 }
1647 if(totalidx == 0 && !SVFUtil::isa<StructType>(value->getType()))
1648 value = gep->getPointerOperand();
1649 }
1650 else if (const LoadInst* load = SVFUtil::dyn_cast<LoadInst>(value))
1651 {
1652 const Value* loadP = load->getPointerOperand();
1653 if (const GetElementPtrInst* gep = SVFUtil::dyn_cast<GetElementPtrInst>(loadP))
1654 {
1655 DataLayout* dataLayout = getDataLayout(llvmModuleSet()->getMainLLVMModule());
1656 llvm::APInt byteOffset(dataLayout->getIndexSizeInBits(gep->getPointerAddressSpace()), 0, true);
1657 const bool hasByteOffset = dataLayout && gep->accumulateConstantOffset(*dataLayout, byteOffset);
1658
1659 const Value * pointer_operand = gep->getPointerOperand();
1660 if (auto *glob = SVFUtil::dyn_cast<GlobalVariable>(pointer_operand))
1661 {
1662 if (hasByteOffset)
1663 {
1664 if (const Value* ptrValue = getGlobalFieldFromByteOffset(glob, byteOffset.getSExtValue()))
1665 return ptrValue;
1666 }
1667 }
1668 else if (hasByteOffset && !byteOffset.isNegative() &&
1669 SVFUtil::isa<AllocaInst>(pointer_operand) && load->getType()->isPointerTy())
1670 {
1671 const u64_t offset = byteOffset.getZExtValue();
1672 const u64_t accessBytes = dataLayout->getPointerSize(gep->getPointerAddressSpace());
1673
1674 auto isCoveredByMemcpy = [offset, accessBytes](const CallBase* cs) -> bool
1675 {
1676 if (cs->arg_size() < 3)
1677 return false;
1678
1679 const auto* copySize = SVFUtil::dyn_cast<ConstantInt>(cs->getArgOperand(2));
1680 if (!copySize)
1681 return false;
1682
1683 const u64_t copyBytes = copySize->getZExtValue();
1685 };
1686
1687 auto hasInterveningWrite = [load](const Instruction* from) -> bool
1688 {
1689 if (from->getParent() != load->getParent() || !from->comesBefore(load))
1690 return true;
1691
1692 auto it = from->getIterator();
1693 const auto end = load->getIterator();
1694 while (++it != end)
1695 {
1696 if (it->mayWriteToMemory())
1697 return true;
1698 }
1699 return false;
1700 };
1701
1702 for (const auto& use : pointer_operand->users())
1703 {
1704 const auto* cs = SVFUtil::dyn_cast<CallBase>(use);
1705 if (!cs || cs->getParent() != load->getParent() ||
1706 cs->arg_size() < 1 ||
1707 stripAllCasts(cs->getArgOperand(0)) != pointer_operand)
1708 continue;
1709
1710 const Function* calledFun = cs->getCalledFunction();
1713 continue;
1714
1715 const Value* copiedFrom = getBaseValueForExtArg(cs->getArgOperand(1));
1716 if (const auto* copiedGlob = SVFUtil::dyn_cast<GlobalVariable>(copiedFrom))
1717 {
1718 if (const Value* ptrValue =
1719 getGlobalFieldFromByteOffset(copiedGlob, byteOffset.getSExtValue()))
1720 return ptrValue;
1721 }
1722 }
1723 }
1724 }
1725 }
1726
1727 return value;
1728}
1729
1734{
1736 NodeID indFunPtrId = llvmModuleSet()->getValueNode(cs->getCalledOperand());
1737 const_cast<CallICFGNode*>(cbn)->setIndFunPtr(pag->getGNode(indFunPtrId));
1739}
1740
1742{
1743 CallGraph::CallEdgeMap::const_iterator iter = callgraph->getIndCallMap().begin();
1744 CallGraph::CallEdgeMap::const_iterator eiter = callgraph->getIndCallMap().end();
1745 for (; iter != eiter; iter++)
1746 {
1747 const CallICFGNode* callBlock = iter->first;
1748 const CallBase* callbase = SVFUtil::cast<CallBase>(llvmModuleSet()->getLLVMValue(callBlock));
1749 assert(callBlock->isIndirectCall() && "this is not an indirect call?");
1750 const CallGraph::FunctionSet& functions = iter->second;
1751 for (CallGraph::FunctionSet::const_iterator func_iter = functions.begin(); func_iter != functions.end(); func_iter++)
1752 {
1753 const Function* callee = SVFUtil::cast<Function>(llvmModuleSet()->getLLVMValue(*func_iter));
1754
1755 if (isExtCall(*func_iter))
1756 {
1757 setCurrentLocation(callee, callee->empty() ? nullptr : &callee->getEntryBlock());
1759 }
1760 else
1761 {
1762 setCurrentLocation(llvmModuleSet()->getLLVMValue(callBlock), callBlock->getBB());
1763 handleDirectCall(const_cast<CallBase*>(callbase), callee);
1764 }
1765 }
1766 }
1767
1768 // dump SVFIR
1770 pag->dump("svfir_final");
1771}
1772
1773/*
1774 * TODO: more sanity checks might be needed here
1775 */
1777{
1778 for (SVFIR::iterator nIter = pag->begin(); nIter != pag->end(); ++nIter)
1779 {
1780 (void) pag->getGNode(nIter->first);
1781 //TODO::
1782 // (1) every source(root) node of a pag tree should be object node
1783 // if a node has no incoming edge, but has outgoing edges
1784 // then it has to be an object node.
1785 // (2) make sure every variable should be initialized
1786 // otherwise it causes the a null pointer, the aliasing relation may not be captured
1787 // when loading a pointer value should make sure
1788 // some value has been store into this pointer before
1789 // q = load p, some value should stored into p first like store w p;
1790 // (3) make sure PAGNode should not have a const expr value (pointer should have unique def)
1791 // (4) look closely into addComplexConsForExt, make sure program locations(e.g.,inst bb)
1792 // are set correctly for dummy gepval node
1793 // (5) reduce unnecessary copy edge (const casts) and ensure correctness.
1794 }
1795}
1796
1797
1802NodeID SVFIRBuilder::getGepValVar(const Value* val, const AccessPath& ap, const SVFType* elementType)
1803{
1804 NodeID base = getValueNode(val);
1806 if (gepval==UINT_MAX)
1807 {
1808 assert(((int) UINT_MAX)==-1 && "maximum limit of unsigned int is not -1?");
1809 /*
1810 * getGepValVar can only be called from two places:
1811 * 1. SVFIRBuilder::addComplexConsForExt to handle external calls
1812 * 2. SVFIRBuilder::getGlobalVarField to initialize global variable
1813 * so curVal can only be
1814 * 1. Instruction
1815 * 2. GlobalVariable
1816 */
1817 assert(
1818 (SVFUtil::isa<Instruction>(curVal) || SVFUtil::isa<GlobalVariable>(curVal)) && "curVal not an instruction or a globalvariable?");
1819
1820 // We assume every GepValNode and its GepEdge to the baseNode are unique across the whole program
1821 // We preserve the current BB information to restore it after creating the gepNode
1822 const Value* cval = getCurrentValue();
1823 const SVFBasicBlock* cbb = getCurrentBB();
1826 const ICFGNode* node = nullptr;
1827 if (const Instruction* inst = SVFUtil::dyn_cast<Instruction>(curVal))
1828 {
1829 if (llvmmodule->hasICFGNode(inst))
1830 {
1831 node = llvmmodule->getICFGNode(inst);
1832 }
1833 }
1834 else if (SVFUtil::isa<GlobalVariable>(curVal))
1835 {
1836 // GEP on a global variable: the resulting GepValVar belongs to the global ICFG node.
1837 node = pag->getICFG()->getGlobalICFGNode();
1838 }
1840 NodeIDAllocator::get()->allocateValueId(),
1841 llvmmodule->getSVFType(PointerType::getUnqual(llvmmodule->getContext())), node);
1842 addGepEdge(base, gepNode, ap, true);
1844 return gepNode;
1845 }
1846 else
1847 return gepval;
1848}
1849
1851{
1852 if (!Options::ModelArrays() || SVFUtil::isa<llvm::GEPOperator>(ptr))
1853 return 0;
1854
1855 const Type* objTy =
1857 const ArrayType* arrTy = SVFUtil::dyn_cast<ArrayType>(objTy);
1858 if (!arrTy || !arrTy->getElementType()->isPointerTy() ||
1859 arrTy->getElementType() != accessTy)
1860 return 0;
1861
1862 AccessPath ap(0, llvmModuleSet()->getSVFType(arrTy));
1863 return getGepValVar(ptr, ap, llvmModuleSet()->getSVFType(accessTy));
1864}
1865
1866
1867/*
1868 * curVal <--------> PAGEdge
1869 * Instruction Any Edge
1870 * Argument CopyEdge (SVFIR::addFormalParamBlackHoleAddrEdge)
1871 * ConstantExpr CopyEdge (Int2PtrConstantExpr CastConstantExpr SVFIRBuilder::processCE)
1872 * GepEdge (GepConstantExpr SVFIRBuilder::processCE)
1873 * ConstantPointerNull CopyEdge (3-->2 NullPtr-->BlkPtr SVFIR::addNullPtrNode)
1874 * AddrEdge (0-->2 BlkObj-->BlkPtr SVFIR::addNullPtrNode)
1875 * GlobalVariable AddrEdge (SVFIRBuilder::visitGlobal)
1876 * GepEdge (SVFIRBuilder::getGlobalVarField)
1877 * Function AddrEdge (SVFIRBuilder::visitGlobal)
1878 * Constant StoreEdge (SVFIRBuilder::InitialGlobal)
1879 */
1881{
1883 return;
1884
1885 assert(curVal && "current Val is nullptr?");
1886 edge->setBB(curBB!=nullptr ? curBB : nullptr);
1888 ICFGNode* icfgNode = pag->getICFG()->getGlobalICFGNode();
1890 if (const Instruction* curInst = SVFUtil::dyn_cast<Instruction>(curVal))
1891 {
1892 const FunObjVar* srcFun = edge->getSrcNode()->getFunction();
1893 const FunObjVar* dstFun = edge->getDstNode()->getFunction();
1894 if(srcFun!=nullptr && !SVFUtil::isa<RetPE>(edge) && !SVFUtil::isa<FunValVar>(edge->getSrcNode()) && !SVFUtil::isa<FunObjVar>(edge->getSrcNode()))
1895 {
1896 assert(srcFun==llvmMS->getFunObjVar(curInst->getFunction()) && "SrcNode of the PAGEdge not in the same function?");
1897 }
1898 if(dstFun!=nullptr && !SVFUtil::isa<CallPE>(edge) && !SVFUtil::isa<RetValPN>(edge->getDstNode()))
1899 {
1900 assert(dstFun==llvmMS->getFunObjVar(curInst->getFunction()) && "DstNode of the PAGEdge not in the same function?");
1901 }
1902
1904 if (!(SVFUtil::isa<GepStmt>(edge) && SVFUtil::isa<GepValVar>(edge->getDstNode())))
1905 assert(curBB && "instruction does not have a basic block??");
1906
1908 if(SVFUtil::isa<ReturnInst>(curInst))
1909 {
1910 icfgNode = pag->getICFG()->getFunExitICFGNode(llvmMS->getFunObjVar(curInst->getFunction()));
1911 }
1912 else if(const CallPE* callPE = SVFUtil::dyn_cast<CallPE>(edge))
1913 {
1915 icfgNode = const_cast<FunEntryICFGNode*>(callPE->getFunEntryICFGNode());
1916 }
1917 else if(SVFUtil::isa<RetPE>(edge))
1918 {
1919 icfgNode = llvmMS->getRetICFGNode(SVFUtil::cast<Instruction>(curInst));
1920 }
1921 else
1922 {
1923 icfgNode = llvmMS->getICFGNode(SVFUtil::cast<Instruction>(curInst));
1924 }
1925 }
1926 else if (const Argument* arg = SVFUtil::dyn_cast<Argument>(curVal))
1927 {
1929 icfgNode = pag->getICFG()->getFunEntryICFGNode(
1930 llvmModuleSet()->getFunObjVar(SVFUtil::cast<Function>(arg->getParent())));
1931 }
1932 else if (SVFUtil::isa<Constant>(curVal) ||
1933 SVFUtil::isa<Function>(curVal) ||
1934 SVFUtil::isa<MetadataAsValue>(curVal))
1935 {
1936 if (!curBB)
1938 else
1939 {
1940 icfgNode = const_cast<ICFGNode*>(curBB->front());
1941 }
1942 }
1943 else
1944 {
1945 assert(false && "what else value can we have?");
1946 }
1947
1948 pag->addToSVFStmtList(icfgNode,edge);
1949 icfgNode->addSVFStmt(edge);
1950 if(const CallPE* callPE = SVFUtil::dyn_cast<CallPE>(edge))
1951 {
1954 FunEntryICFGNode* entryNode = const_cast<FunEntryICFGNode*>(callPE->getFunEntryICFGNode());
1955 for(u32_t i = 0; i < callPE->getOpVarNum(); i++)
1956 {
1957 CallICFGNode* callNode = const_cast<CallICFGNode*>(callPE->getOpCallICFGNode(i));
1959 SVFUtil::cast<CallCFGEdge>(icfgEdge)->addCallPE(callPE);
1960 }
1961 }
1962 else if(const RetPE* retPE = SVFUtil::dyn_cast<RetPE>(edge))
1963 {
1964 RetICFGNode* retNode = const_cast<RetICFGNode*>(retPE->getCallSite()->getRetICFGNode());
1965 FunExitICFGNode* exitNode = const_cast<FunExitICFGNode*>(retPE->getFunExitICFGNode());
1967 SVFUtil::cast<RetCFGEdge>(edge)->addRetPE(retPE);
1968 }
1969}
1970
1971
1979{
1980 SVFVar* node = pag->getGNode(nodeId);
1983 if(geps.empty())
1984 return AccessPath(0);
1985
1986 assert(geps.size()==1 && "one node can only be connected by at most one gep edge!");
1987 SVFVar::iterator it = geps.begin();
1988 const GepStmt* gepEdge = SVFUtil::cast<GepStmt>(*it);
1989 if(gepEdge->isVariantFieldGep())
1990 return AccessPath(0);
1991 else
1992 return gepEdge->getAccessPath();
1993}
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition SVFType.h:593
#define TIMEINTERVAL
Definition SVFType.h:621
#define DGENERAL
Definition SVFType.h:599
#define DPAGBuild
Definition SVFType.h:601
buffer offset
Definition cJSON.cpp:1113
cJSON * item
Definition cJSON.h:222
APOffset getConstantStructFldIdx() const
Get methods.
Definition AccessPath.h:98
void setFldIdx(APOffset idx)
Definition AccessPath.h:102
bool addOffsetVarAndGepTypePair(const ValVar *var, const SVFType *gepIterType)
std::vector< std::pair< const ICFGNode *, s32_t > > SuccAndCondPairVec
CallEdgeMap & getIndCallMap()
Get callees from an indirect callsite.
Definition CallGraph.h:331
Set< const FunObjVar * > FunctionSet
Definition CallGraph.h:247
bool isVirtualCall() const
Definition ICFGNode.h:509
void setVtablePtr(SVFVar *v)
Definition ICFGNode.h:514
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:44
void setExtFuncAnnotations(const FunObjVar *fun, const std::vector< std::string > &funcAnnotations)
Definition ExtAPI.cpp:223
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
const SVFBasicBlock * getEntryBlock() const
void setBasicBlockGraph(BasicBlockGraph *graph)
void initFunObjVar(bool decl, bool intrinc, bool addr, bool uncalled, bool notret, bool vararg, const SVFFunctionType *ft, SVFLoopAndDomInfo *ld, const FunObjVar *real, BasicBlockGraph *bbg, const std::vector< const ArgValVar * > &allarg, const SVFBasicBlock *exit)
BasicBlockGraph * bbGraph
the definition of a function across multiple modules
bool isDeclaration() const
iterator begin()
Iterators.
u32_t getTotalNodeNum() const
Get total number of node/edge.
IDToNodeMapTy::iterator iterator
Node Iterators.
NodeType * getGNode(NodeID id) const
Get a node.
GEdgeSetTy::iterator iterator
const GEdgeSetTy & getOutEdges() const
void addSVFStmt(const SVFStmt *edge)
Definition ICFGNode.h:110
FunExitICFGNode * getFunExitICFGNode(const FunObjVar *fun)
Add a function exit node.
Definition ICFG.cpp:249
ICFGEdge * hasInterICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Definition ICFG.cpp:276
void dump(const std::string &file, bool simple=false)
Dump graph into dot file.
Definition ICFG.cpp:411
FunEntryICFGNode * getFunEntryICFGNode(const FunObjVar *fun)
Add a function entry node.
Definition ICFG.cpp:242
GlobalICFGNode * getGlobalICFGNode() const
Definition ICFG.h:244
NodeID constantSymID() const
Definition IRGraph.h:188
u32_t getFlattenedElemIdx(const SVFType *T, u32_t origId)
Flattened element idx of an array or struct by considering stride.
Definition IRGraph.cpp:144
u32_t getNodeNumAfterPAGBuild() const
Definition IRGraph.h:321
void dump(std::string name)
Dump SVFIR.
Definition IRGraph.cpp:316
NodeID getBlkPtr() const
Definition IRGraph.h:255
NodeID blkPtrSymID() const
Definition IRGraph.h:178
NodeID getNullPtr() const
Definition IRGraph.h:259
NodeID nullPtrSymID() const
Definition IRGraph.h:183
u32_t getTotalSymNum() const
Statistics.
Definition IRGraph.h:200
FunObjVarToIDMapTy varargFunObjSymMap
vararg map
Definition IRGraph.h:86
NodeID getReturnNode(const FunObjVar *func) const
GetReturnNode - Return the unique node representing the return value of a function.
Definition IRGraph.cpp:60
void setNodeNumAfterPAGBuild(u32_t num)
Definition IRGraph.h:325
NodeID blackholeSymID() const
Definition IRGraph.h:193
ObjTypeInfo * getObjTypeInfo(NodeID id) const
Definition IRGraph.h:234
NodeID getConstantNode() const
Definition IRGraph.h:251
FunObjVarToIDMapTy returnFunObjSymMap
return map
Definition IRGraph.h:85
virtual void build(ICFG *icfg)
Start from here.
NodeID getValueNode(const Value *V)
ValueToIDMapTy & valSyms()
Definition LLVMModule.h:208
FunToIDMapTy & retSyms()
Definition LLVMModule.h:275
const FunObjVar * getFunObjVar(const Function *fun) const
Definition LLVMModule.h:268
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:131
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb)
Definition LLVMModule.h:299
DominatorTree & getDomTree(const Function *fun)
void addToSVFVar2LLVMValueMap(const Value *val, SVFValue *svfBaseNode)
LLVMFun2FunObjVarMap LLVMFun2FunObjVar
Map an LLVM Function to an SVF Funobjvar.
Definition LLVMModule.h:99
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
CallICFGNode * getCallICFGNode(const Instruction *cs)
get a call node
Fun2AnnoMap ExtFun2Annotations
Record annotations of function in extapi.bc.
Definition LLVMModule.h:91
NodeID getObjectNode(const Value *V)
RetICFGNode * getRetICFGNode(const Instruction *cs)
get a return node
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
Definition LLVMModule.h:158
const Function * getRealDefFun(const Function *fun) const
Definition LLVMModule.h:186
ValueToIDMapTy & objSyms()
Definition LLVMModule.h:213
void addBasicBlock(FunObjVar *fun, const BasicBlock *bb)
Definition LLVMModule.h:233
FunToIDMapTy & varargSyms()
Definition LLVMModule.h:280
const FunctionSet & getFunctionSet() const
Definition LLVMModule.h:193
ObjTypeInference * getTypeInference()
static NodeIDAllocator * get(void)
Return (singleton) allocator.
const Type * inferObjType(const Value *var)
get or infer the type of the object pointed by the value
static Option< bool > ModelArrays
Definition Options.h:185
static const Option< bool > PAGDotGraph
Definition Options.h:118
static const Option< std::string > DumpJson
Definition Options.h:121
static Option< bool > ModelConsts
Definition Options.h:184
static const Option< bool > PAGPrint
Definition Options.h:124
static const Option< bool > VtableInSVFIR
Definition Options.h:214
static const Option< bool > LoopAnalysis
Definition Options.h:239
static const Option< bool > DumpICFG
Definition Options.h:120
const FunObjVar * getParent() const
void addPredBasicBlock(const SVFBasicBlock *pred2)
void addSuccBasicBlock(const SVFBasicBlock *succ2)
const ICFGNode * front() const
u32_t inferFieldIdxFromByteOffset(const llvm::GEPOperator *gepOp, DataLayout *dl, AccessPath &ap, APOffset idx)
Infer field index from byteoffset.
CopyStmt::CopyKind getCopyKind(const Value *val)
void sanityCheck()
Sanity check for SVFIR.
SVFIR * getPAG() const
Return SVFIR.
void setCurrentLocation(const Value *val, const BasicBlock *bb)
Set current basic block in order to keep track of control flow information.
NodeID addNullPtrNode()
Add NullPtr PAGNode.
void visitLoadInst(LoadInst &I)
NodeID getVarargNode(const FunObjVar *func)
getVarargNode - Return the node representing the unique variadic argument of a function.
void addPhiStmt(NodeID res, NodeID opnd, const ICFGNode *pred)
Add Copy edge.
void updateCallGraph(CallGraph *callgraph)
connect PAG edges based on callgraph
void initSVFBasicBlock(const Function *func)
void addStoreEdge(NodeID src, NodeID dst)
Add Store edge.
AddrStmt * addAddrEdge(NodeID src, NodeID dst)
Add Address edge.
void visitInvokeInst(InvokeInst &II)
void handleDirectCall(CallBase *cs, const Function *F)
Handle direct call.
void addBinaryOPEdge(NodeID op1, NodeID op2, NodeID dst, u32_t opcode)
Add Copy edge.
void visitCallInst(CallInst &I)
void addLoadEdge(NodeID src, NodeID dst)
Add Load edge.
virtual void handleExtCall(const CallBase *cs, const Function *callee)
void visitGetElementPtrInst(GetElementPtrInst &I)
void visitBranchInst(BranchInst &I)
virtual void visitAllocaInst(AllocaInst &AI)
Our visit overrides.
void addGepEdge(NodeID src, NodeID dst, const AccessPath &ap, bool constGep)
Add Gep edge.
void addCmpEdge(NodeID op1, NodeID op2, NodeID dst, u32_t predict)
Add Copy edge.
LLVMModuleSet * llvmModuleSet()
void visitStoreInst(StoreInst &I)
NodeID getReturnNode(const FunObjVar *func)
getReturnNode - Return the node representing the unique return value of a function.
NodeID getObjectNode(const Value *V)
GetObject - Return the object node (stack/global/heap/function) according to a LLVM Value.
void visitCallSite(CallBase *cs)
void processCE(const Value *val)
Process constant expression.
void handleIndCall(CallBase *cs)
Handle indirect call.
const Value * curVal
Current Value during SVFIR construction when visiting the module.
void addSelectStmt(NodeID res, NodeID op1, NodeID op2, NodeID cond)
Add SelectStmt.
void addBranchStmt(NodeID br, NodeID cond, const BranchStmt::SuccAndCondPairVec &succs)
Add Branch statement.
virtual SVFIR * build()
Start building SVFIR here.
void visitCallBrInst(CallBrInst &I)
void visitExtractValueInst(ExtractValueInst &EVI)
AccessPath getAccessPathFromBaseNode(NodeID nodeId)
const SVFBasicBlock * curBB
Current basic block during SVFIR construction when visiting the module.
void visitSwitchInst(SwitchInst &I)
The following implementation follows ICFGBuilder::processFunBody.
void visitFreezeInst(FreezeInst &I)
const Value * getBaseValueForExtArg(const Value *V)
Get the base value of (i8* src and i8* dst) for external argument (e.g. memcpy(i8* dst,...
void initDomTree(FunObjVar *func, const Function *f)
void addRetEdge(NodeID src, NodeID dst, const CallICFGNode *cs, const FunExitICFGNode *exit)
Add Return edge.
void addBlackHoleAddrEdge(NodeID node)
NodeID getDirectAccessFieldZeroValVar(const Value *ptr, const Type *accessTy)
void visitGlobal()
Handle globals including (global variable and functions)
void addUnaryOPEdge(NodeID src, NodeID dst, u32_t opcode)
Add Unary edge.
const SVFBasicBlock * getCurrentBB() const
void visitPHINode(PHINode &I)
CopyStmt * addCopyEdge(NodeID src, NodeID dst, CopyStmt::CopyKind kind)
void addCallEdge(NodeID src, NodeID dst, const CallICFGNode *cs, const FunEntryICFGNode *entry)
Add Call edge.
void setCurrentBBAndValueForPAGEdge(PAGEdge *edge)
void visitSelectInst(SelectInst &I)
void visitVAArgInst(VAArgInst &)
void visitCmpInst(CmpInst &I)
void visitExtractElementInst(ExtractElementInst &I)
bool computeGepOffset(const User *V, AccessPath &ap)
Compute offset of a gep instruction or gep constant expression.
void visitReturnInst(ReturnInst &I)
const Value * getCurrentValue() const
NodeID getValueNode(const Value *V)
Get different kinds of node.
void visitCastInst(CastInst &I)
AddrStmt * addAddrWithStackArraySz(NodeID src, NodeID dst, llvm::AllocaInst &inst)
Add Address edge from allocinst with arraysize like "%4 = alloca i8, i64 3".
NodeID getGepValVar(const Value *val, const AccessPath &ap, const SVFType *elementType)
void InitialGlobal(const GlobalVariable *gvar, Constant *C, u32_t offset)
void visitUnaryOperator(UnaryOperator &I)
void visitBinaryOperator(BinaryOperator &I)
void initialiseNodes()
Initialize nodes and edges.
NodeID getGlobalVarField(const GlobalVariable *gvar, u32_t offset, SVFType *tpy)
NodeID addGlobalValNode(const NodeID i, const ICFGNode *icfgNode, const SVFType *svfType)
Definition SVFIR.h:702
void addFunArgs(const FunObjVar *fun, const ValVar *arg)
Get/set method for function/callsite arguments and returns.
Definition SVFIR.h:616
NodeID getGepValVar(NodeID curInst, NodeID base, const AccessPath &ap) const
Due to constraint expression, curInst is used to distinguish different instructions (e....
Definition SVFIR.cpp:613
void print()
Print SVFIR.
Definition SVFIR.cpp:648
NodeID addConstantAggObjNode(const NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:778
NodeID addBlackholePtrNode()
Definition SVFIR.h:844
NodeID addBlackholeObjNode()
Definition SVFIR.h:836
NodeID addGlobalObjNode(const NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:773
NodeID addGepValNode(NodeID curInst, const ValVar *base, const AccessPath &ap, NodeID i, const SVFType *type, const ICFGNode *node)
Add a temp field value node, this method can only invoked by getGepValVar.
Definition SVFIR.cpp:485
NodeID addConstantDataObjNode(const NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:783
NodeID addConstantFPObjNode(NodeID i, ObjTypeInfo *ti, double dval, const ICFGNode *node)
Definition SVFIR.h:752
NodeID addObjNode(NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Add a memory obj node.
Definition SVFIR.h:722
void addFunRet(const FunObjVar *fun, const ValVar *ret)
Add function returns.
Definition SVFIR.h:628
NodeID addBasicBlockValNode(NodeID i, const SVFType *type)
Definition SVFIR.h:852
NodeID addConstantNullPtrValNode(const NodeID i, const ICFGNode *icfgNode, const SVFType *type)
Definition SVFIR.h:696
NodeID addHeapObjNode(NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:730
static bool pagReadFromTXT()
Definition SVFIR.h:278
CallGraph * callGraph
all the callsites of a program
Definition SVFIR.h:101
void addToSVFStmtList(ICFGNode *inst, SVFStmt *edge)
Add a SVFStmt into instruction map.
Definition SVFIR.h:326
NodeID addConstantNullPtrObjNode(const NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:767
NodeID addConstantDataValNode(const NodeID i, const ICFGNode *icfgNode, const SVFType *type)
Definition SVFIR.h:714
NodeID addIntrinsicValNode(NodeID i, const SVFType *type)
Definition SVFIR.h:848
void addCallSiteArgs(CallICFGNode *callBlockNode, const ValVar *arg)
Add callsite arguments.
Definition SVFIR.h:640
NodeID addStackObjNode(NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:739
NodeID addConstantIntValNode(NodeID i, const std::pair< s64_t, u64_t > &intValue, const ICFGNode *icfgNode, const SVFType *type)
Definition SVFIR.h:689
ICFG * getICFG() const
Definition SVFIR.h:229
NodeID addFunValNode(NodeID i, const ICFGNode *icfgNode, const FunObjVar *funObjVar, const SVFType *type)
Definition SVFIR.h:669
NodeID addConstantFPValNode(const NodeID i, double dval, const ICFGNode *icfgNode, const SVFType *type)
Definition SVFIR.h:682
NodeID addConstantAggValNode(const NodeID i, const ICFGNode *icfgNode, const SVFType *svfType)
Definition SVFIR.h:708
void addCallSite(const CallICFGNode *call)
Add callsites.
Definition SVFIR.h:883
NodeID addValNode(NodeID i, const SVFType *type, const ICFGNode *icfgNode)
add node into SVFIR
Definition SVFIR.h:663
NodeID addVarargNode(NodeID i, const FunObjVar *val, const SVFType *type, const ICFGNode *n)
Add a unique vararg node for a procedure.
Definition SVFIR.h:796
NodeID addAsmPCValNode(NodeID i, const SVFType *type)
Definition SVFIR.h:856
void setCHG(CommonCHGraph *c)
Set/Get CHG.
Definition SVFIR.h:235
NodeID addFunObjNode(NodeID id, ObjTypeInfo *ti, const ICFGNode *node)
Definition SVFIR.h:745
ICFG * icfg
Definition SVFIR.h:98
NodeID addConstantIntObjNode(NodeID i, ObjTypeInfo *ti, const std::pair< s64_t, u64_t > &intValue, const ICFGNode *node)
Definition SVFIR.h:759
void addGlobalPAGEdge(const SVFStmt *edge)
Add global PAGEdges (not in a procedure)
Definition SVFIR.h:878
const ValVar * getValVar(NodeID id) const
Definition SVFIR.h:137
void addIndirectCallsites(const CallICFGNode *cs, NodeID funPtr)
Add indirect callsites.
Definition SVFIR.h:652
void initialiseCandidatePointers()
Initialize candidate pointers.
Definition SVFIR.cpp:733
NodeID addRetNode(NodeID i, const FunObjVar *callGraphNode, const SVFType *type, const ICFGNode *icn)
Add a unique return node for a procedure.
Definition SVFIR.h:790
NodeID addArgValNode(NodeID i, u32_t argNo, const ICFGNode *icfgNode, const FunObjVar *callGraphNode, const SVFType *type)
Definition SVFIR.h:675
void addCallSiteRets(RetICFGNode *retBlockNode, const ValVar *arg)
Add callsite returns.
Definition SVFIR.h:646
NodeID addConstantObjNode()
Definition SVFIR.h:840
const Map< const SVFBasicBlock *, BBSet > & getDomFrontierMap() const
Set< const SVFBasicBlock * > BBSet
static double getClk(bool mark=false)
Definition SVFStat.cpp:48
static double timeOfBuildingSVFIR
Definition SVFStat.h:95
GenericNode< SVFVar, SVFStmt >::GEdgeSetTy SVFStmtSetTy
virtual void setName(const std::string &nameInfo)
Definition SVFValue.h:179
SVFStmt::SVFStmtSetTy & getIncomingEdges(SVFStmt::PEDGEK kind)
Edge accessors and checkers.
#define NULL
Definition extapi.c:5
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition LLVMUtil.cpp:203
const ConstantExpr * isBinaryConstantExpr(const Value *val)
Definition LLVMUtil.h:294
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:229
const ConstantExpr * isSelectConstantExpr(const Value *val)
Definition LLVMUtil.h:259
bool isMemcpyExtFun(const Function *fun)
Definition LLVMUtil.cpp:389
bool isIntrinsicFun(const Function *func)
Definition LLVMUtil.cpp:190
bool functionDoesNotRet(const Function *fun)
Definition LLVMUtil.cpp:123
const ConstantExpr * isTruncConstantExpr(const Value *val)
Definition LLVMUtil.h:269
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
const ConstantExpr * isPtr2IntConstantExpr(const Value *val)
Definition LLVMUtil.h:239
bool isHeapObj(const Value *val)
Definition LLVMUtil.cpp:687
const ConstantExpr * isUnaryConstantExpr(const Value *val)
Definition LLVMUtil.h:305
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:249
bool isExtCall(const Function *fun)
Definition LLVMUtil.cpp:384
bool basicBlockHasRetInst(const BasicBlock *bb)
Return true if the function has a return instruction.
Definition LLVMUtil.cpp:109
bool isStackObj(const Value *val)
Definition LLVMUtil.cpp:709
const ConstantExpr * isGepConstantExpr(const Value *val)
Return corresponding constant expression, otherwise return nullptr.
Definition LLVMUtil.h:219
static DataLayout * getDataLayout(Module *mod)
Definition LLVMUtil.h:317
const Function * getCallee(const CallBase *cs)
Definition LLVMUtil.h:98
const FunObjVar * getFunObjVar(const std::string &name)
Definition LLVMUtil.cpp:436
std::string dumpValue(const Value *val)
Definition LLVMUtil.cpp:605
const ConstantExpr * isCmpConstantExpr(const Value *val)
Definition LLVMUtil.h:283
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition SVFUtil.cpp:101
void writeWrnMsg(const std::string &msg)
Writes a message run through wrnMsg.
Definition SVFUtil.cpp:68
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
const Value * getVCallVtblPtr(const CallBase *cs)
Definition CppUtil.cpp:612
bool isValVtbl(const Value *val)
Definition CppUtil.cpp:336
for isBitcode
Definition BasicTypes.h:70
llvm::DataLayout DataLayout
Definition BasicTypes.h:112
llvm::GlobalVariable GlobalVariable
Definition BasicTypes.h:137
llvm::GlobalAlias GlobalAlias
Definition BasicTypes.h:135
llvm::ArrayType ArrayType
Definition BasicTypes.h:99
llvm::Type Type
Definition BasicTypes.h:87
llvm::CallBase CallBase
Definition BasicTypes.h:153
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:90
llvm::UnaryOperator UnaryOperator
Definition BasicTypes.h:187
llvm::StructType StructType
LLVM types.
Definition BasicTypes.h:98
llvm::succ_const_iterator succ_const_iterator
LLVM Iterators.
Definition BasicTypes.h:287
unsigned long long u64_t
Definition GeneralType.h:49
llvm::AllocaInst AllocaInst
Definition BasicTypes.h:157
llvm::SwitchInst SwitchInst
Definition BasicTypes.h:162
u32_t NodeID
Definition GeneralType.h:56
llvm::StructLayout StructLayout
Definition BasicTypes.h:109
llvm::InvokeInst InvokeInst
Definition BasicTypes.h:170
llvm::Argument Argument
Definition BasicTypes.h:152
llvm::LoadInst LoadInst
Definition BasicTypes.h:156
s64_t APOffset
Definition GeneralType.h:60
llvm::const_pred_iterator const_pred_iterator
Definition BasicTypes.h:265
llvm::CmpInst CmpInst
Definition BasicTypes.h:166
llvm::Function Function
Definition BasicTypes.h:89
llvm::ConstantData ConstantData
Definition BasicTypes.h:120
llvm::LoopInfo LoopInfo
Definition BasicTypes.h:148
llvm::Instruction Instruction
Definition BasicTypes.h:91
llvm::Constant Constant
Definition BasicTypes.h:128
llvm::DomTreeNode DomTreeNode
Definition BasicTypes.h:141
llvm::ConstantDataSequential ConstantDataSequential
Definition BasicTypes.h:123
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::CastInst CastInst
Definition BasicTypes.h:165
llvm::FreezeInst FreezeInst
Definition BasicTypes.h:176
llvm::Module Module
Definition BasicTypes.h:88
llvm::BinaryOperator BinaryOperator
Definition BasicTypes.h:186
llvm::PostDominatorTree PostDominatorTree
Definition BasicTypes.h:143
llvm::DominanceFrontier DominanceFrontier
Definition BasicTypes.h:142
llvm::StoreInst StoreInst
Definition BasicTypes.h:155
llvm::SelectInst SelectInst
Definition BasicTypes.h:181
llvm::VAArgInst VAArgInst
Definition BasicTypes.h:182
llvm::Loop Loop
LLVM Loop.
Definition BasicTypes.h:147
llvm::GetElementPtrInst GetElementPtrInst
Definition BasicTypes.h:169
llvm::CallBrInst CallBrInst
Definition BasicTypes.h:163
llvm::ReturnInst ReturnInst
Definition BasicTypes.h:164
llvm::PHINode PHINode
Definition BasicTypes.h:172
llvm::BranchInst BranchInst
Definition BasicTypes.h:161
llvm::ExtractValueInst ExtractValueInst
Definition BasicTypes.h:167
unsigned u32_t
Definition GeneralType.h:47
signed long long s64_t
Definition GeneralType.h:50
llvm::CallInst CallInst
Definition BasicTypes.h:154
llvm::ConstantInt ConstantInt
Definition BasicTypes.h:129
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition BasicTypes.h:140
llvm::ExtractElementInst ExtractElementInst
Definition BasicTypes.h:168
llvm::User User
Definition BasicTypes.h:149