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/SVFVariables.h"
33#include "Util/Options.h"
34#include "Util/SVFUtil.h"
35#include "Graphs/CallGraph.h"
36
37using namespace SVF;
38using namespace SVFUtil;
39
40
48
50{
51 if (const FunObjVar* fun = getFunction())
52 {
53 return fun->isUncalledFunction();
54 }
55 else
56 {
57 return false;
58 }
59}
60
62{
63 if (getInEdges().empty() && getOutEdges().empty())
64 return true;
66 return true;
67 else
68 return false;
69}
70
71
72const std::string SVFVar::toString() const
73{
74 std::string str;
75 std::stringstream rawstr(str);
76 rawstr << "SVFVar ID: " << getId();
77 return rawstr.str();
78}
79
80void SVFVar::dump() const
81{
82 outs() << this->toString() << "\n";
83}
84
86 : SVFVar(i, svfType, ty), icfgNode(node)
87{
88 if (SVFUtil::isa<GlobalValVar>(this))
89 {
90 assert(node && "GlobalValVar must have a valid ICFGNode");
91 }
92 else if (SVFUtil::isa<GepValVar>(this))
93 {
94 assert(node && "GepValVar must have a valid ICFGNode");
95 }
96 else if (SVFUtil::isa<ArgValVar>(this) ||
97 SVFUtil::isa<RetValPN>(this) ||
98 SVFUtil::isa<VarArgValPN>(this))
99 {
100 // Conditional assert (isDeclaration || icn) is in each subclass constructor.
101 }
102 else if (SVFUtil::isa<ConstDataValVar>(this) ||
103 SVFUtil::isa<ConstAggValVar>(this) ||
104 SVFUtil::isa<FunValVar>(this) ||
105 SVFUtil::isa<DummyValVar>(this) ||
106 SVFUtil::isa<IntrinsicValVar>(this) ||
107 SVFUtil::isa<BasicBlockValVar>(this) ||
108 SVFUtil::isa<AsmPCValVar>(this))
109 {
110 // These ValVar subclasses don't require an ICFGNode.
111 }
112 else if (ty == ValNode)
113 {
114 assert(node && "Base ValVar must have a valid ICFGNode");
115 }
116 else
117 {
118 assert(false && "Unknown ValVar subclass -- update this check");
119 }
120}
121
123{
124 if(icfgNode)
125 return icfgNode->getFun();
126 return nullptr;
127}
128
129const std::string ValVar::toString() const
130{
131 std::string str;
132 std::stringstream rawstr(str);
133 rawstr << "ValVar ID: " << getId();
135 {
136 rawstr << "\n";
138 }
139 return rawstr.str();
140}
141
142const std::string ObjVar::toString() const
143{
144 std::string str;
145 std::stringstream rawstr(str);
146 rawstr << "ObjVar ID: " << getId();
148 {
149 rawstr << "\n";
151 }
152 return rawstr.str();
153}
154
156 const SVF::FunObjVar* callGraphNode, const SVFType* svfType)
157 : ValVar(i, svfType, icn, ArgValNode),
158 cgNode(callGraphNode), argNo(argNo)
159{
160 assert((callGraphNode->isDeclaration() || icn) &&
161 "ArgValVar of a defined function must have a valid ICFGNode");
162}
163
165{
166 return getParent();
167}
168
170{
171 return cgNode;
172}
173
178
180{
181 return cgNode->getArg(argNo)->getType()->isPointerTy();
182}
183
184const std::string ArgValVar::toString() const
185{
186 std::string str;
187 std::stringstream rawstr(str);
188 rawstr << "ArgValVar ID: " << getId();
190 {
191 rawstr << "\n";
193 }
194 return rawstr.str();
195}
196
198 const AccessPath& ap, const SVFType* ty, const ICFGNode* node)
199 : ValVar(i, ty, node, GepValNode), ap(ap), base(baseNode), gepValType(ty)
200{
201}
202
203const std::string GepValVar::toString() const
204{
205 std::string str;
206 std::stringstream rawstr(str);
207 rawstr << "GepValVar ID: " << getId() << " with offset_" + std::to_string(getConstantFieldIdx());
209 {
210 rawstr << "\n";
212 }
213 return rawstr.str();
214}
215
217 : ValVar(i, svfType, icn, RetValNode), callGraphNode(node)
218{
219 assert((node->isDeclaration() || icn) &&
220 "RetValPN of a defined function must have a valid ICFGNode");
221}
222
224{
225 return callGraphNode;
226}
227
229{
231}
232
233
234const std::string RetValPN::getValueName() const
235{
236 return callGraphNode->getName() + "_ret";
237}
238
239const std::string GepObjVar::toString() const
240{
241 std::string str;
242 std::stringstream rawstr(str);
243 rawstr << "GepObjVar ID: " << getId() << " with offset_" + std::to_string(apOffset);
245 {
246 rawstr << "\n";
248 }
249 return rawstr.str();
250}
251
256
258{
259 return IRGraph::isBlkObj(getId());
260}
261
262
264{
265 if(icfgNode)
266 return icfgNode->getFun();
267 return nullptr;
268}
269const std::string BaseObjVar::toString() const
270{
271 std::string str;
272 std::stringstream rawstr(str);
273 rawstr << "BaseObjVar ID: " << getId() << " (base object)";
275 {
276 rawstr << "\n";
278 }
279 return rawstr.str();
280}
281
282
283const std::string HeapObjVar::toString() const
284{
285 std::string str;
286 std::stringstream rawstr(str);
287 rawstr << "HeapObjVar ID: " << getId();
289 {
290 rawstr << "\n";
292 }
293 return rawstr.str();
294}
295
296const std::string StackObjVar::toString() const
297{
298 std::string str;
299 std::stringstream rawstr(str);
300 rawstr << "StackObjVar ID: " << getId();
302 {
303 rawstr << "\n";
305 }
306 return rawstr.str();
307}
308
309
310
312 : ValVar(i, svfType, icn, FunValNode), funObjVar(cgn)
313{
314}
315
316const std::string FunValVar::toString() const
317{
318 std::string str;
319 std::stringstream rawstr(str);
320 rawstr << "FunValVar ID: " << getId();
322 {
323 rawstr << "\n";
325 }
326 return rawstr.str();
327}
328
329const std::string ConstAggValVar::toString() const
330{
331 std::string str;
332 std::stringstream rawstr(str);
333 rawstr << "ConstAggValVar ID: " << getId();
335 {
336 rawstr << "\n";
338 }
339 return rawstr.str();
340}
341const std::string ConstDataValVar::toString() const
342{
343 std::string str;
344 std::stringstream rawstr(str);
345 rawstr << "ConstDataValVar ID: " << getId();
347 {
348 rawstr << "\n";
350 }
351 return rawstr.str();
352}
353
354const std::string GlobalValVar::toString() const
355{
356 std::string str;
357 std::stringstream rawstr(str);
358 rawstr << "GlobalValVar ID: " << getId();
360 {
361 rawstr << "\n";
363 }
364 return rawstr.str();
365}
366
367const std::string ConstFPValVar::toString() const
368{
369 std::string str;
370 std::stringstream rawstr(str);
371 rawstr << "ConstFPValVar ID: " << getId();
373 {
374 rawstr << "\n";
376 }
377 return rawstr.str();
378}
379
380const std::string ConstIntValVar::toString() const
381{
382 std::string str;
383 std::stringstream rawstr(str);
384 rawstr << "ConstIntValVar ID: " << getId();
386 {
387 rawstr << "\n";
389 }
390 return rawstr.str();
391}
392
393const std::string ConstNullPtrValVar::toString() const
394{
395 std::string str;
396 std::stringstream rawstr(str);
397 rawstr << "ConstNullPtrValVar ID: " << getId();
399 {
400 rawstr << "\n";
402 }
403 return rawstr.str();
404}
405
406const std::string GlobalObjVar::toString() const
407{
408 std::string str;
409 std::stringstream rawstr(str);
410 rawstr << "GlobalObjVar ID: " << getId();
412 {
413 rawstr << "\n";
415 }
416 return rawstr.str();
417}
418const std::string ConstAggObjVar::toString() const
419{
420 std::string str;
421 std::stringstream rawstr(str);
422 rawstr << "ConstAggObjVar ID: " << getId();
424 {
425 rawstr << "\n";
427 }
428 return rawstr.str();
429}
430const std::string ConstDataObjVar::toString() const
431{
432 std::string str;
433 std::stringstream rawstr(str);
434 rawstr << "ConstDataObjVar ID: " << getId();
436 {
437 rawstr << "\n";
439 }
440 return rawstr.str();
441}
442
443const std::string ConstFPObjVar::toString() const
444{
445 std::string str;
446 std::stringstream rawstr(str);
447 rawstr << "ConstFPObjVar ID: " << getId();
449 {
450 rawstr << "\n";
452 }
453 return rawstr.str();
454}
455
456const std::string ConstIntObjVar::toString() const
457{
458 std::string str;
459 std::stringstream rawstr(str);
460 rawstr << "ConstIntObjVar ID: " << getId();
462 {
463 rawstr << "\n";
465 }
466 return rawstr.str();
467}
468
469const std::string ConstNullPtrObjVar::toString() const
470{
471 std::string str;
472 std::stringstream rawstr(str);
473 rawstr << "ConstNullPtrObjVar ID: " << getId();
475 {
476 rawstr << "\n";
478 }
479 return rawstr.str();
480}
481
483 : BaseObjVar(i, ti, node, FunObjNode)
484{
485}
486
487void FunObjVar::initFunObjVar(bool decl, bool intrinc, bool addr, bool uncalled, bool notret, bool vararg,
489 const std::vector<const ArgValVar *> &allarg, const SVFBasicBlock *exit)
490{
491 isDecl = decl;
497 funcType = ft;
498 loopAndDom = ld;
500 bbGraph = bbg;
501 allArgs = allarg;
502 exitBlock = exit;
503}
504
505
507{
508 return isIntrinsic();
509}
510
512{
513 return this;
514}
515
516const std::string FunObjVar::toString() const
517{
518 std::string str;
519 std::stringstream rawstr(str);
520 rawstr << "FunObjVar ID: " << getId() << " (base object)";
522 {
523 rawstr << "\n";
524 rawstr << getName();
525 }
526 return rawstr.str();
527}
528
529const std::string RetValPN::toString() const
530{
531 std::string str;
532 std::stringstream rawstr(str);
533 rawstr << "RetValPN ID: " << getId() << " unique return node for function " << callGraphNode->getName();
534 return rawstr.str();
535}
536
538{
539 return callGraphNode;
540}
541
542const std::string VarArgValPN::getValueName() const
543{
544 return callGraphNode->getName() + "_vararg";
545}
546
547const std::string VarArgValPN::toString() const
548{
549 std::string str;
550 std::stringstream rawstr(str);
551 rawstr << "VarArgValPN ID: " << getId() << " Var arg node for function " << callGraphNode->getName();
552 return rawstr.str();
553}
554
555const std::string DummyValVar::toString() const
556{
557 std::string str;
558 std::stringstream rawstr(str);
559 rawstr << "DummyValVar ID: " << getId();
560 return rawstr.str();
561}
562
563const std::string IntrinsicValVar::toString() const
564{
565 std::string str;
566 std::stringstream rawstr(str);
567 rawstr << "IntrinsicValVar ID: " << getId();
568 return rawstr.str();
569}
570
571const std::string BasicBlockValVar::toString() const
572{
573 std::string str;
574 std::stringstream rawstr(str);
575 rawstr << "BasicBlockValVar ID: " << getId();
576 return rawstr.str();
577}
578
579const std::string AsmPCValVar::toString() const
580{
581 std::string str;
582 std::stringstream rawstr(str);
583 rawstr << "AsmPCValVar ID: " << getId();
584 return rawstr.str();
585}
586
587const std::string DummyObjVar::toString() const
588{
589 std::string str;
590 std::stringstream rawstr(str);
591 rawstr << "DummyObjVar ID: " << getId();
592 return rawstr.str();
593}
594
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.
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:74
const SVFType * getFlatternedElemType(const SVFType *baseType, u32_t flatten_idx)
Return the type of a flattened element given a flattened index.
Definition IRGraph.cpp:123
static bool isBlkObj(NodeID id)
Definition IRGraph.h:165
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:119
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:118
bool isPointerTy() const
Definition SVFType.h:294
NodeID getId() const
Get ID.
Definition SVFValue.h:163
virtual const SVFType * getType() const
Definition SVFValue.h:174
virtual const std::string & getName() const
Definition SVFValue.h:189
const std::string valueOnlyToString() const
Definition LLVMUtil.cpp:739
const SVFType * type
SVF type.
Definition SVFValue.h:211
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:56
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:47