Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFVariables.cpp
Go to the documentation of this file.
1//===- SVFVariables.cpp -- SVF symbols and variables----------------------//
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 * SVFVariables.cpp
25 *
26 * Created on: Oct 11, 2013
27 * Author: Yulei Sui
28* Refactored on: Nov 30, 2024
29 * Author: Xiao Cheng, Yulei Sui
30 */
31
32#include "SVFIR/SVFIR.h"
33#include "SVFIR/SVFVariables.h"
34#include "Util/Options.h"
35#include "Util/SVFUtil.h"
36#include "Graphs/CallGraph.h"
37
38using namespace SVF;
39using namespace SVFUtil;
40
41
49
51{
52 if (const FunObjVar* fun = getFunction())
53 {
54 return fun->isUncalledFunction();
55 }
56 else
57 {
58 return false;
59 }
60}
61
63{
64 if (getInEdges().empty() && getOutEdges().empty())
65 return true;
67 return true;
68 else
69 return false;
70}
71
72
73const std::string SVFVar::toString() const
74{
75 std::string str;
76 std::stringstream rawstr(str);
77 rawstr << "SVFVar ID: " << getId();
78 return rawstr.str();
79}
80
81void SVFVar::dump() const
82{
83 outs() << this->toString() << "\n";
84}
85
87 : SVFVar(i, svfType, ty), icfgNode(node)
88{
89 if (SVFUtil::isa<GlobalValVar>(this))
90 {
91 assert(node && "GlobalValVar must have a valid ICFGNode");
92 }
93 else if (SVFUtil::isa<GepValVar>(this))
94 {
95 assert(node && "GepValVar must have a valid ICFGNode");
96 }
97 else if (SVFUtil::isa<ArgValVar>(this) ||
98 SVFUtil::isa<RetValPN>(this) ||
99 SVFUtil::isa<VarArgValPN>(this))
100 {
101 // Conditional assert (isDeclaration || icn) is in each subclass constructor.
102 }
103 else if (SVFUtil::isa<ConstDataValVar>(this) ||
104 SVFUtil::isa<FunValVar>(this) ||
105 SVFUtil::isa<DummyValVar>(this) ||
106 SVFUtil::isa<IntrinsicValVar>(this) ||
107 SVFUtil::isa<AsmPCValVar>(this))
108 {
109 // These ValVar subclasses don't require an ICFGNode.
110 }
111 else if (ty == ValNode)
112 {
113 assert(node && "Base ValVar must have a valid ICFGNode");
114 }
115 else
116 {
117 assert(false && "Unknown ValVar subclass -- update this check");
118 }
119}
120
122{
123 if(icfgNode)
124 return icfgNode->getFun();
125 return nullptr;
126}
127
128const std::string ValVar::toString() const
129{
130 std::string str;
131 std::stringstream rawstr(str);
132 rawstr << "ValVar ID: " << getId();
134 {
135 rawstr << "\n";
137 }
138 return rawstr.str();
139}
140
141const std::string ObjVar::toString() const
142{
143 std::string str;
144 std::stringstream rawstr(str);
145 rawstr << "ObjVar ID: " << getId();
147 {
148 rawstr << "\n";
150 }
151 return rawstr.str();
152}
153
155 const SVF::FunObjVar* callGraphNode, const SVFType* svfType)
156 : ValVar(i, svfType, icn, ArgValNode),
157 cgNode(callGraphNode), argNo(argNo)
158{
159 assert((callGraphNode->isDeclaration() || icn) &&
160 "ArgValVar of a defined function must have a valid ICFGNode");
161}
162
164{
165 return getParent();
166}
167
169{
170 return cgNode;
171}
172
177
179{
180 return cgNode->getArg(argNo)->getType()->isPointerTy();
181}
182
183const std::string ArgValVar::toString() const
184{
185 std::string str;
186 std::stringstream rawstr(str);
187 rawstr << "ArgValVar ID: " << getId();
189 {
190 rawstr << "\n";
192 }
193 return rawstr.str();
194}
195
197 const AccessPath& ap, const SVFType* ty, const ICFGNode* node)
198 : ValVar(i, ty, node, GepValNode), ap(ap), base(baseNode), gepValType(ty)
199{
200}
201
202const std::string GepValVar::toString() const
203{
204 std::string str;
205 std::stringstream rawstr(str);
206 rawstr << "GepValVar ID: " << getId() << " with offset_" + std::to_string(getConstantFieldIdx());
208 {
209 rawstr << "\n";
211 }
212 return rawstr.str();
213}
214
216 : ValVar(i, svfType, icn, RetValNode), callGraphNode(node)
217{
218 assert((node->isDeclaration() || icn) &&
219 "RetValPN of a defined function must have a valid ICFGNode");
220}
221
223{
224 return callGraphNode;
225}
226
228{
230}
231
232
233const std::string RetValPN::getValueName() const
234{
235 return callGraphNode->getName() + "_ret";
236}
237
238const std::string GepObjVar::toString() const
239{
240 std::string str;
241 std::stringstream rawstr(str);
242 rawstr << "GepObjVar ID: " << getId() << " with offset_" + std::to_string(apOffset);
244 {
245 rawstr << "\n";
247 }
248 return rawstr.str();
249}
250
255
257{
258 return IRGraph::isBlkObj(getId());
259}
260
261
263{
264 if(icfgNode)
265 return icfgNode->getFun();
266 return nullptr;
267}
268const std::string BaseObjVar::toString() const
269{
270 std::string str;
271 std::stringstream rawstr(str);
272 rawstr << "BaseObjVar ID: " << getId() << " (base object)";
274 {
275 rawstr << "\n";
277 }
278 return rawstr.str();
279}
280
281
282const std::string HeapObjVar::toString() const
283{
284 std::string str;
285 std::stringstream rawstr(str);
286 rawstr << "HeapObjVar ID: " << getId();
288 {
289 rawstr << "\n";
291 }
292 return rawstr.str();
293}
294
295const std::string StackObjVar::toString() const
296{
297 std::string str;
298 std::stringstream rawstr(str);
299 rawstr << "StackObjVar ID: " << getId();
301 {
302 rawstr << "\n";
304 }
305 return rawstr.str();
306}
307
308
309
311 : ValVar(i, svfType, icn, FunValNode), funObjVar(cgn)
312{
313}
314
315const std::string FunValVar::toString() const
316{
317 std::string str;
318 std::stringstream rawstr(str);
319 rawstr << "FunValVar ID: " << getId();
321 {
322 rawstr << "\n";
324 }
325 return rawstr.str();
326}
327
328const std::string ConstDataValVar::toString() const
329{
330 std::string str;
331 std::stringstream rawstr(str);
332 rawstr << "ConstDataValVar ID: " << getId();
334 {
335 rawstr << "\n";
337 }
338 return rawstr.str();
339}
340
341const std::string GlobalValVar::toString() const
342{
343 std::string str;
344 std::stringstream rawstr(str);
345 rawstr << "GlobalValVar ID: " << getId();
347 {
348 rawstr << "\n";
350 }
351 return rawstr.str();
352}
353
354const std::string ConstFPValVar::toString() const
355{
356 std::string str;
357 std::stringstream rawstr(str);
358 rawstr << "ConstFPValVar ID: " << getId();
360 {
361 rawstr << "\n";
363 }
364 return rawstr.str();
365}
366
367const std::string ConstIntValVar::toString() const
368{
369 std::string str;
370 std::stringstream rawstr(str);
371 rawstr << "ConstIntValVar ID: " << getId();
373 {
374 rawstr << "\n";
376 }
377 return rawstr.str();
378}
379
380const std::string ConstNullPtrValVar::toString() const
381{
382 std::string str;
383 std::stringstream rawstr(str);
384 rawstr << "ConstNullPtrValVar ID: " << getId();
386 {
387 rawstr << "\n";
389 }
390 return rawstr.str();
391}
392
393const std::string GlobalObjVar::toString() const
394{
395 std::string str;
396 std::stringstream rawstr(str);
397 rawstr << "GlobalObjVar ID: " << getId();
399 {
400 rawstr << "\n";
402 }
403 return rawstr.str();
404}
405const std::string ConstDataObjVar::toString() const
406{
407 std::string str;
408 std::stringstream rawstr(str);
409 rawstr << "ConstDataObjVar ID: " << getId();
411 {
412 rawstr << "\n";
414 }
415 return rawstr.str();
416}
417
418const std::string ConstFPObjVar::toString() const
419{
420 std::string str;
421 std::stringstream rawstr(str);
422 rawstr << "ConstFPObjVar ID: " << getId();
424 {
425 rawstr << "\n";
427 }
428 return rawstr.str();
429}
430
431const std::string ConstIntObjVar::toString() const
432{
433 std::string str;
434 std::stringstream rawstr(str);
435 rawstr << "ConstIntObjVar ID: " << getId();
437 {
438 rawstr << "\n";
440 }
441 return rawstr.str();
442}
443
444const std::string ConstNullPtrObjVar::toString() const
445{
446 std::string str;
447 std::stringstream rawstr(str);
448 rawstr << "ConstNullPtrObjVar ID: " << getId();
450 {
451 rawstr << "\n";
453 }
454 return rawstr.str();
455}
456
458 : BaseObjVar(i, ti, node, FunObjNode)
459{
460}
461
462void FunObjVar::initFunObjVar(bool decl, bool intrinc, bool addr, bool uncalled, bool notret, bool vararg,
464 const std::vector<const ArgValVar *> &allarg, const SVFBasicBlock *exit)
465{
466 isDecl = decl;
472 funcType = ft;
473 loopAndDom = ld;
475 bbGraph = bbg;
476 allArgs = allarg;
477 exitBlock = exit;
478}
479
480
482{
483 return isIntrinsic();
484}
485
487{
488 return this;
489}
490
491const std::string FunObjVar::toString() const
492{
493 std::string str;
494 std::stringstream rawstr(str);
495 rawstr << "FunObjVar ID: " << getId() << " (base object)";
497 {
498 rawstr << "\n";
499 rawstr << getName();
500 }
501 return rawstr.str();
502}
503
504const std::string RetValPN::toString() const
505{
506 std::string str;
507 std::stringstream rawstr(str);
508 rawstr << "RetValPN ID: " << getId() << " unique return node for function " << callGraphNode->getName();
509 return rawstr.str();
510}
511
513{
514 return callGraphNode;
515}
516
517const std::string VarArgValPN::getValueName() const
518{
519 return callGraphNode->getName() + "_vararg";
520}
521
522const std::string VarArgValPN::toString() const
523{
524 std::string str;
525 std::stringstream rawstr(str);
526 rawstr << "VarArgValPN ID: " << getId() << " Var arg node for function " << callGraphNode->getName();
527 return rawstr.str();
528}
529
530const std::string DummyValVar::toString() const
531{
532 std::string str;
533 std::stringstream rawstr(str);
534 rawstr << "DummyValVar ID: " << getId();
535 return rawstr.str();
536}
537
538const std::string IntrinsicValVar::toString() const
539{
540 std::string str;
541 std::stringstream rawstr(str);
542 rawstr << "IntrinsicValVar ID: " << getId();
543 return rawstr.str();
544}
545
546const std::string AsmPCValVar::toString() const
547{
548 std::string str;
549 std::stringstream rawstr(str);
550 rawstr << "AsmPCValVar ID: " << getId();
551 return rawstr.str();
552}
553
554const std::string DummyObjVar::toString() const
555{
556 std::string str;
557 std::stringstream rawstr(str);
558 rawstr << "DummyObjVar ID: " << getId();
559 return rawstr.str();
560}
561
bool isArgOfUncalledFunction() const
ArgValVar(NodeID i, u32_t argNo, const ICFGNode *icn, const FunObjVar *callGraphNode, const SVFType *svfType)
Constructor.
const FunObjVar * cgNode
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
virtual bool isPointer() const
Check if this variable represents a pointer.
const FunObjVar * getParent() const
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
const ICFGNode * icfgNode
virtual const std::string toString() const
Get string representation.
NodeID getId() const
Get the memory object id.
bool isBlackHoleObj() const
Whether it is a black hole object.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
const ArgValVar * getArg(u32_t idx) const
virtual const std::string toString() const
Get string representation.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
virtual bool isIsolatedNode() const
Check if this node is isolated (no edges) in the SVFIR graph.
bool isAddrTaken
return true if this function is an intrinsic function (e.g., llvm.dbg), which does not reside in the ...
bool isNotRet
return true if this function is never called
SVFLoopAndDomInfo * loopAndDom
FunctionType, which is different from the type (PointerType) of this SVF Function.
const SVFType * getReturnType() const
Returns the FunctionType.
std::vector< const ArgValVar * > allArgs
the basic block graph of this function
bool supVarArg
return true if this function never returns
const SVFBasicBlock * exitBlock
all formal arguments of this function
bool isUncalledFunction() const
const SVFFunctionType * funcType
return true if this function supports variable arguments
bool intrinsic
return true if this function does not have a body
const FunObjVar * realDefFun
the loop and dominate information
bool isUncalled
return true if this function is address-taken (for indirect call purposes)
bool isIntrinsic() const
FunObjVar(NodeID i, ObjTypeInfo *ti, const ICFGNode *node)
Constructor.
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
virtual const std::string toString() const
Get string representation.
const FunObjVar * funObjVar
FunValVar(NodeID i, const ICFGNode *icn, const FunObjVar *cgn, const SVFType *svfType)
Constructor.
const GEdgeSetTy & getOutEdges() const
const GEdgeSetTy & getInEdges() const
const BaseObjVar * getBaseObj() const
virtual const std::string toString() const
Get string representation.
APOffset apOffset
virtual const SVFType * getType() const
Return the type of this gep object.
const ValVar * getBaseNode(void) const
Return the base object from which this GEP node came from.
GepValVar(const ValVar *baseNode, NodeID i, const AccessPath &ap, const SVFType *ty, const ICFGNode *node)
Constructor.
virtual const std::string toString() const
Get string representation.
APOffset getConstantFieldIdx() const
offset of the base value variable
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const FunObjVar * getFun() const
Return the function of this ICFGNode.
Definition ICFGNode.h:75
const SVFType * getFlatternedElemType(const SVFType *baseType, u32_t flatten_idx)
Return the type of a flattened element given a flattened index.
Definition IRGraph.cpp:127
static bool isBlkObj(NodeID id)
Definition IRGraph.h:164
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
static const Option< bool > ShowSVFIRValue
Definition Options.h:118
const FunObjVar * callGraphNode
RetValPN(NodeID i, const FunObjVar *node, const SVFType *svfType, const ICFGNode *icn)
Constructor.
virtual bool isPointer() const
Check if this variable represents a pointer.
const std::string getValueName() const
Return name of a LLVM value.
virtual const std::string toString() const
Get string representation.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:120
bool isPointerTy() const
Definition SVFType.h:292
NodeID getId() const
Get ID.
Definition SVFValue.h:158
virtual const SVFType * getType() const
Definition SVFValue.h:169
virtual const std::string & getName() const
Definition SVFValue.h:184
const std::string valueOnlyToString() const
Definition LLVMUtil.cpp:741
const SVFType * type
SVF type.
Definition SVFValue.h:206
SVFVar(NodeID i, const SVFType *svfType, PNODEK k)
Standard constructor with ID, type and kind.
virtual bool isConstDataOrAggDataButNotNullPtr() const
Check if this variable represents constant data/metadata but not null pointer.
virtual bool ptrInUncalledFunction() const
Check if this pointer is in an uncalled function.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
void dump() const
Debug dump to console.
virtual bool isIsolatedNode() const
Check if this node is isolated (no edges) in the SVFIR graph.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const std::string toString() const
Get string representation.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
ValVar(NodeID i, const SVFType *svfType, const ICFGNode *node, PNODEK ty=ValNode)
Constructor.
const ICFGNode * icfgNode
virtual const std::string toString() const
Get string representation.
const FunObjVar * callGraphNode
const std::string getValueName() const
Return name of a LLVM value.
virtual const FunObjVar * getFunction() const
Get containing function, or null for globals/constants.
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67