15std::string baseName(
const PAG& pag,
NodeID base)
17 const SVFVar* var =
const_cast<PAG&
>(pag).getGNode(base);
19 return name.empty() ?
"o" + std::to_string(base) :
name;
22std::string labelText(
const PAG& pag,
const NodeBS& bases)
26 out += (out.empty() ?
"" :
",") + baseName(pag, base);
33 std::ostringstream out;
38 out << (first ?
"" :
",") << base;
47 std::ostringstream out;
50 out <<
" branch " <<
branch->getSrcNode()->getId()
51 <<
" -> " <<
branch->getDstNode()->getId()
52 <<
" label " <<
branch->getSuccessorCondValue();
62 for (
auto it = svfg.
begin(); it != svfg.
end(); ++it)
64 const VFGNode* vfg = it->second;
65 const ICFGNode* location = vfg->getICFGNode();
68 if (location && location->getFun())
74 for (const VFGEdge* edge : vfg->getInEdges())
76 if (const auto* call = SVFUtil::dyn_cast<CallIndSVFGEdge>(edge))
78 const ICFGNode* caller = call->getSrcNode()->getICFGNode();
81 for (NodeID obj : call->getPointsTo())
83 if (!pag.isNullOrBlkObj(obj))
85 const NodeID base = pag.getBaseObject(obj)->getId();
86 eventsByBase[base].insert(caller);
87 eventsByBase[base].insert(location);
88 addRoute(base, caller, location, nullptr);
97 const ValVar* pointer = nullptr;
98 if (const auto* memory = SVFUtil::dyn_cast<MRSVFGNode>(vfg))
100 for (NodeID obj : memory->getPointsTo())
101 if (!pag.isNullOrBlkObj(obj))
102 eventsByBase[pag.getBaseObject(obj)->getId()].insert(location);
104 else if (const auto* store = SVFUtil::dyn_cast<StoreVFGNode>(vfg))
105 pointer = store->getLHSVar();
106 else if (const auto* load = SVFUtil::dyn_cast<LoadVFGNode>(vfg))
107 pointer = load->getRHSVar();
114 for (NodeID obj : pta.getPts(pointer->getId()))
115 if (!pag.isNullOrBlkObj(obj))
116 eventsByBase[pag.getBaseObject(obj)->getId()].insert(location);
124 for (
auto it = icfg.begin(); it != icfg.end(); ++it)
126 if (const auto* ret = SVFUtil::dyn_cast<RetICFGNode>(it->second))
128 for (const VFGNode* target : ret->getVFGNodes())
130 for (const VFGEdge* edge : target->getInEdges())
132 if (const auto* output = SVFUtil::dyn_cast<RetIndSVFGEdge>(edge))
134 const ICFGNode* source = output->getSrcNode()->getICFGNode();
137 for (NodeID obj : output->getPointsTo())
139 if (!pag.isNullOrBlkObj(obj))
141 const NodeID base = pag.getBaseObject(obj)->getId();
142 returnedGroups[ret].set(base);
143 if (source->getFun())
144 returnedGroupsByCallee[ret][source->getFun()].set(base);
145 eventsByBase[base].insert(ret);
146 if (source->getFun())
147 eventsByBase[base].insert(source);
148 addRoute(base, source, ret, nullptr);
164 for (
const auto& [base, events] : eventsByBase)
167 globalBases.insert(base);
168 for (
const ICFGNode* event : events)
170 eventsAt[event].set(base);
171 funBases[
event->getFun()].set(base);
176 if (!globalBases.empty())
179 for (NodeID base : globalBases)
181 const ICFGNode* global = icfg.getGlobalICFGNode();
182 for (const ICFGEdge* edge : global->getOutEdges())
184 const ICFGNode* entry = edge->getDstNode();
185 if (!SVFUtil::isa<FunEntryICFGNode>(entry))
187 const auto bases = funBases.find(entry->getFun());
188 if (bases == funBases.end())
190 NodeBS label = globals & bases->second;
192 addRoute(label, global, entry, nullptr);
198 for (
const auto& [fun, bases] : funBases)
199 connectFunction(fun, bases, eventsAt);
212 if (intra && intra->getCondition())
230 addRoute(bases, source, target, branch);
240 const NodeKey from{source,
nullptr};
241 const NodeKey to{target,
nullptr};
244 const NodeKey via{source, branch};
265 for (
const auto& event : eventsAt)
266 if (event.first->getFun() == fun)
321 returned = all->second;
327 const NodeBS* calleeBases =
nullptr;
330 const auto callee = byCallee->second.find(out->getDstNode()->getFun());
331 if (callee != byCallee->second.end())
332 calleeBases = &callee->second;
335 returned &= *calleeBases;
346 addRoute(bypass, call, ret,
nullptr);
359 std::deque<std::pair<const ICFGNode*, NodeBS>> walk;
360 walk.emplace_back(start, bases);
361 while (!walk.empty())
363 const ICFGNode* current = walk.front().first;
364 NodeBS travelling = std::move(walk.front().second);
366 if (!current || current->
getFun() != fun)
368 NodeBS& seen = arrived[current];
370 if (travelling.
empty())
376 addRoute(travelling, source, current, branch);
383 addRoute(stop, source, current, branch);
386 if (travelling.
empty())
390 walk.emplace_back(intra->getDstNode(), travelling);
397 static const NodeBS empty;
398 const auto it = eventsAt.find(node);
399 return it == eventsAt.end() ? empty : it->second;
405 Node*& node =
nodes[ {location, branch}];
416 u64_t hash = 1469598103934665603ULL;
418 hash = (hash ^ base) * 1099511628211ULL;
419 auto& bucket =
labels[hash];
420 for (
const auto& label : bucket)
423 bucket.push_back(std::make_unique<NodeBS>(bases));
424 return bucket.back().get();
436 assert(addedOut && addedIn);
446const std::vector<const SparseObjGraph::Edge*>&
449 static const std::vector<const Edge*> empty;
450 const auto it =
inputs.find(node);
451 return it ==
inputs.end() ? empty : it->second;
456 std::ostringstream out;
457 for (
const auto& entry : *
this)
458 out << entry.second->toString() <<
'\n';
459 for (
const auto& entry : *
this)
460 for (
const Edge* edge : entry.second->getOutEdges())
461 out << edge->toString() <<
'\n';
475 return "Sparse object routes";
480 std::ostringstream out;
484 out <<
"branch ICFG" << loc->
getId() <<
" -> ICFG"
489 out <<
"ICFG" << loc->
getId();
503 if (k && kinds.find(k) == std::string::npos)
504 kinds += std::string(kinds.empty() ?
"" :
"+") + k;
509 const size_t ln = src.find(
"\"ln\": ");
510 if (ln != std::string::npos)
511 line = src.substr(ln + 6, src.find_first_of(
",}", ln + 6) - ln - 6);
513 if (!line.empty() && line !=
"0")
514 out <<
" line " << line;
516 out <<
"\n" << kinds;
522 return node->
branch ?
"shape=diamond,style=filled,fillcolor=\"#ffedd5\""
523 :
"shape=box,style=filled,fillcolor=\"#dbeafe\"";
526 template <
class EdgeIter>
532 std::string attr =
"label=\"{" + label +
"}\"";
535 attr +=
",color=blue,style=dashed,fontcolor=blue";
537 attr +=
",color=\"#c2410c\",fontcolor=\"#c2410c\"";
const RetICFGNode * getRetICFGNode() const
Return callsite.
NodeType * getDstNode() const
void addGNode(NodeID id, NodeType *node)
iterator begin()
Iterators.
u32_t getTotalNodeNum() const
const GEdgeSetTy & getOutEdges() const
bool addIncomingEdge(EdgeType *inEdge)
Add incoming and outgoing edges.
bool addOutgoingEdge(EdgeType *outEdge)
static void WriteGraphToFile(SVF::OutStream &O, const std::string &GraphName, const GraphType >, bool simple=false)
virtual const FunObjVar * getFun() const
Return the function of this ICFGNode.
const SVFStmtList & getSVFStmts() const
s64_t getSuccessorCondValue() const
const SVFVar * getCondition() const
const BaseObjVar * getBaseObject(NodeID id) const
NodeID getId() const
Get ID.
virtual const std::string getSourceLoc() const
virtual const std::string & getName() const
bool intersectWithComplement(const SparseBitVector &RHS)
const NodeBS & getBases() const
Base Obj IDs routed over this edge; each base carries all its fields.
std::string toString() const
const std::vector< const Edge * > & getInputs(const ICFGNode *node) const
Return incoming edges at this ICFG location for client-side value flow.
void addSource(const ICFGNode *node, const NodeBS &bases)
bool isBranch(const ICFGNode *node) const
Map< const ICFGNode *, std::vector< const Edge * > > inputs
static const NodeBS & getEvents(const Map< const ICFGNode *, NodeBS > &eventsAt, const ICFGNode *node)
const PAG & getPAG() const
Map< NodeKey, Node * > nodes
std::pair< const ICFGNode *, const IntraCFGEdge * > NodeKey
Map< u64_t, std::vector< std::unique_ptr< NodeBS > > > labels
Interned labels, bucketed by hash.
SparseObjGraph(const SVFG &svfg, ICFG &icfg, PointerAnalysis &pta, const PAG &pag)
Construction uses static graphs and points-to results, not AE state.
void connectCallBypass(const CallICFGNode *call, const NodeBS &bases)
Route the bases that no callee returns from the call to its return site.
Map< const ICFGNode *, NodeBS > returnedGroups
Map< const ICFGNode *, Map< const FunObjVar *, NodeBS > > returnedGroupsByCallee
bool isControlBoundary(const ICFGNode *node) const
std::map< EdgeKey, NodeBS > routeLabels
Map< const ICFGNode *, NodeBS > doneSources
Worklist of connectFunction for the function being connected.
Node * getOrCreateNode(const ICFGNode *location, const IntraCFGEdge *branch)
void connectFunction(const FunObjVar *fun, const NodeBS &funBases, const Map< const ICFGNode *, NodeBS > &eventsAt)
Walk one function once, carrying base sets instead of one base.
void connectSegment(const FunObjVar *fun, const ICFGNode *source, const ICFGNode *start, const IntraCFGEdge *branch, const NodeBS &bases, const Map< const ICFGNode *, NodeBS > &eventsAt)
~SparseObjGraph() override
std::deque< const ICFGNode * > sourceWorklist
std::string toString() const
Describe the static nodes and edges as text, without AE values.
const NodeBS * intern(const NodeBS &bases)
Map< const ICFGNode *, NodeBS > pendingSources
void dump(const std::string &file)
Write a DOT graph to <file>.dot through GraphPrinter.
void addRoute(const NodeBS &bases, const ICFGNode *source, const ICFGNode *target, const IntraCFGEdge *branch)
const IntraCFGEdge * branch
const ICFGNode * location
std::string toString() const
LLVM_NODISCARD bool isa(const Y &Val)
std::ostream & outs()
Overwrite llvm::outs().
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
static std::string getEdgeAttributes(SparseObjNode *node, EdgeIter it, SparseObjGraph *graph)
static std::string getGraphName(SparseObjGraph *)
DOTGraphTraits(bool simple=false)
static std::string getNodeLabel(SparseObjNode *node, SparseObjGraph *)
static std::string getNodeAttributes(SparseObjNode *node, SparseObjGraph *)
DefaultDOTGraphTraits(bool simple=false)