Static Value-Flow Analysis
Loading...
Searching...
No Matches
AbstractInterpretation.h
Go to the documentation of this file.
1//===- AbstractInterpretation.h -- Abstract Execution----------//
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//
25// Created by Jiawei Wang on 2024/1/10.
26// The implementation is based on
27// Xiao Cheng, Jiawei Wang and Yulei Sui. Precise Sparse Abstract Execution via Cross-Domain Interaction.
28// 46th International Conference on Software Engineering. (ICSE24)
29//
30#pragma once
32#include "AE/Core/ICFGWTO.h"
34#include "AE/Svfexe/AbsExtAPI.h"
35#include "Util/SVFBugReport.h"
36#include "Util/SVFStat.h"
37#include "Graphs/SCC.h"
38
39namespace SVF
40{
41class AbstractInterpretation;
42class AbsExtAPI;
43class AEStat;
44class AEAPI;
45
46template<typename T> class FILOWorkList;
47
49class AEStat : public SVFStat
50{
51public:
52 void countStateSize();
58 {
59 }
60 inline std::string getMemUsage()
61 {
63 return SVFUtil::getMemoryUsageKB(&vmrss, &vmsize) ? std::to_string(vmsize) + "KB" : "cannot read memory usage";
64 }
65
66 void finializeStat();
67 void performStat() override;
68
69public:
72 std::string memory_usage;
73 std::string memUsage;
74
75
77 {
78 if (generalNumMap.count("Function_Trace") == 0)
79 {
80 generalNumMap["Function_Trace"] = 0;
81 }
82 return generalNumMap["Function_Trace"];
83 }
85 {
86 if (generalNumMap.count("Block_Trace") == 0)
87 {
88 generalNumMap["Block_Trace"] = 0;
89 }
90 return generalNumMap["Block_Trace"];
91 }
93 {
94 if (generalNumMap.count("ICFG_Node_Trace") == 0)
95 {
96 generalNumMap["ICFG_Node_Trace"] = 0;
97 }
98 return generalNumMap["ICFG_Node_Trace"];
99 }
100};
101
104{
105 friend class AEStat;
106 friend class AEAPI;
109
110public:
112
113 /*
114 * For recursive test case
115 * int demo(int a) {
116 if (a >= 10000)
117 return a;
118 demo(a+1);
119 }
120
121 int main() {
122 int result = demo(0);
123 }
124 * if set TOP, result = [-oo, +oo] since the return value, and any stored object pointed by q at *q = p in recursive functions will be set to the top value.
125 * if set WIDEN_ONLY, result = [10000, +oo] since only widening is applied at the cycle head of recursive functions without narrowing.
126 * if set WIDEN_NARROW, result = [10000, 10000] since both widening and narrowing are applied at the cycle head of recursive functions.
127 * */
134
137
138 virtual void runOnModule(ICFG* icfg);
139
141 virtual ~AbstractInterpretation();
142
144 void analyse();
145
147 {
149 return instance;
150 }
151
152 void addDetector(std::unique_ptr<AEDetector> detector)
153 {
154 detectors.push_back(std::move(detector));
155 }
156
158
166 {
167 const ICFGNode* repNode = icfg->getRepNode(node);
168 if (abstractTrace.count(repNode) == 0)
169 {
170 assert(false && "No preAbsTrace for this node");
171 }
172 else
173 {
174 return abstractTrace[repNode];
175 }
176 }
177
178private:
180 virtual void handleGlobalNode();
181
183 void initWTO();
184
191 bool mergeStatesFromPredecessors(const ICFGNode * icfgNode);
192
200
207
213 virtual void handleCallSite(const ICFGNode* node);
214
220 virtual void handleCycleWTO(const ICFGCycleWTO* cycle);
221
222 void handleWTOComponents(const std::list<const ICFGWTOComp*>& wtoComps);
223
225
226
232 virtual void handleSVFStatement(const SVFStmt* stmt);
233
239 virtual void SkipRecursiveCall(const CallICFGNode* callnode);
240
241
251
261
262
263 void collectCheckPoint();
264 void checkPointAllSet();
265
266 void updateStateOnAddr(const AddrStmt *addr);
267
269
270 void updateStateOnCmp(const CmpStmt *cmp);
271
272 void updateStateOnLoad(const LoadStmt *load);
273
274 void updateStateOnStore(const StoreStmt *store);
275
276 void updateStateOnCopy(const CopyStmt *copy);
277
278 void updateStateOnCall(const CallPE *callPE);
279
280 void updateStateOnRet(const RetPE *retPE);
281
282 void updateStateOnGep(const GepStmt *gep);
283
285
286 void updateStateOnPhi(const PhiStmt *phi);
287
288
292 AEAPI* api{nullptr};
293
296
297 std::vector<const CallICFGNode*> callSiteStack;
301
302
304 {
305 const ICFGNode* repNode = icfg->getRepNode(node);
306 return abstractTrace.count(repNode) != 0;
307 }
308
310 {
311 return utils;
312 }
313
314 // helper functions in handleCallSite
315 virtual bool isExtCall(const CallICFGNode* callNode);
316 virtual void extCallPass(const CallICFGNode* callNode);
317 virtual bool isRecursiveFun(const FunObjVar* fun);
318 virtual bool isRecursiveCall(const CallICFGNode* callNode);
319 virtual void recursiveCallPass(const CallICFGNode *callNode);
320 virtual bool isRecursiveCallSite(const CallICFGNode* callNode, const FunObjVar *);
321 virtual bool isDirectCall(const CallICFGNode* callNode);
322 virtual void directCallFunPass(const CallICFGNode* callNode);
323 virtual bool isIndirectCall(const CallICFGNode* callNode);
324 virtual void indirectCallFunPass(const CallICFGNode* callNode);
325
326 // there data should be shared with subclasses
327 Map<std::string, std::function<void(const CallICFGNode*)>> func_map;
328
329 Map<const ICFGNode*, AbstractState> abstractTrace; // abstract states immediately after nodes
330 std::string moduleName;
331
332 std::vector<std::unique_ptr<AEDetector>> detectors;
334
335 // according to varieties of cmp insts,
336 // maybe var X var, var X const, const X var, const X const
337 // we accept 'var X const' 'var X var' 'const X const'
338 // if 'const X var', we need to reverse op0 op1 and its predicate 'var X' const'
339 // X' is reverse predicate of X
340 // == -> !=, != -> ==, > -> <=, >= -> <, < -> >=, <= -> >
341
361
362
382
383};
384}
copy
Definition cJSON.cpp:414
AEStat: Statistic for AE.
std::string memory_usage
void performStat() override
AEStat(AbstractInterpretation *ae)
std::string getMemUsage()
AbstractInterpretation * _ae
Handles external API calls and manages abstract states.
Definition AbsExtAPI.h:44
AbstractInterpretation is same as Abstract Execution.
void updateStateOnCall(const CallPE *callPE)
virtual bool isRecursiveCall(const CallICFGNode *callNode)
virtual void recursiveCallPass(const CallICFGNode *callNode)
Map< std::string, std::function< void(const CallICFGNode *)> > func_map
void updateStateOnStore(const StoreStmt *store)
virtual bool isDirectCall(const CallICFGNode *callNode)
bool hasAbsStateFromTrace(const ICFGNode *node)
static AbstractInterpretation & getAEInstance()
virtual void handleCycleWTO(const ICFGCycleWTO *cycle)
handle wto cycle (loop)
Set< const FunObjVar * > recursiveFuns
void updateStateOnGep(const GepStmt *gep)
virtual void extCallPass(const CallICFGNode *callNode)
virtual void handleGlobalNode()
Global ICFGNode is handled at the entry of the program,.
bool mergeStatesFromPredecessors(const ICFGNode *icfgNode)
virtual void directCallFunPass(const CallICFGNode *callNode)
void handleWTOComponent(const ICFGWTOComp *wtoComp)
virtual bool isExtCall(const CallICFGNode *callNode)
virtual bool isIndirectCall(const CallICFGNode *callNode)
void initWTO()
Compute IWTO for each function partition entry.
void updateStateOnPhi(const PhiStmt *phi)
bool isBranchFeasible(const IntraCFGEdge *intraEdge, AbstractState &as)
std::vector< std::unique_ptr< AEDetector > > detectors
void addDetector(std::unique_ptr< AEDetector > detector)
AbstractState & getAbsStateFromTrace(const ICFGNode *node)
Retrieves the abstract state from the trace for a given ICFG node.
Set< const CallICFGNode * > checkpoints
bool isSwitchBranchFeasible(const SVFVar *var, s64_t succ, AbstractState &as)
void updateStateOnSelect(const SelectStmt *select)
virtual void handleSVFStatement(const SVFStmt *stmt)
virtual void indirectCallFunPass(const CallICFGNode *callNode)
SVFIR * svfir
protected data members, also used in subclasses
virtual void runOnModule(ICFG *icfg)
virtual bool isRecursiveCallSite(const CallICFGNode *callNode, const FunObjVar *)
virtual bool isRecursiveFun(const FunObjVar *fun)
void updateStateOnAddr(const AddrStmt *addr)
virtual ~AbstractInterpretation()
Destructor.
bool isCmpBranchFeasible(const CmpStmt *cmpStmt, s64_t succ, AbstractState &as)
virtual void handleCallSite(const ICFGNode *node)
void updateStateOnRet(const RetPE *retPE)
void handleWTOComponents(const std::list< const ICFGWTOComp * > &wtoComps)
Handle two types of WTO components (singleton and cycle)
void updateStateOnCopy(const CopyStmt *copy)
Set< std::pair< const CallICFGNode *, NodeID > > nonRecursiveCallSites
AEAPI * api
Execution State, used to store the Interval Value of every SVF variable.
void updateStateOnLoad(const LoadStmt *load)
void updateStateOnBinary(const BinaryOPStmt *binary)
Map< const ICFGNode *, AbstractState > abstractTrace
std::vector< const CallICFGNode * > callSiteStack
SCCDetection< CallGraph * > CallGraphSCC
virtual void handleSingletonWTO(const ICFGSingletonWTO *icfgSingletonWto)
handle instructions in svf basic blocks
Map< s32_t, s32_t > _switch_lhsrhs_predicate
void updateStateOnCmp(const CmpStmt *cmp)
virtual void SkipRecursiveCall(const CallICFGNode *callnode)
Map< const FunObjVar *, const ICFGWTO * > funcToWTO
Detector for identifying buffer overflow issues.
Definition AEDetector.h:135
@ ICMP_SGT
signed greater than
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_ULE
unsigned less or equal
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
@ FCMP_OLT
0 1 0 0 True if ordered and less than
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
@ ICMP_NE
not equal
@ ICMP_ULT
unsigned less than
@ ICMP_SLT
signed less than
@ ICMP_UGT
unsigned greater than
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
@ ICMP_SGE
signed greater or equal
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
@ ICMP_SLE
signed less or equal
const ICFGNode * getRepNode(const ICFGNode *node) const
Definition ICFG.h:246
NUMStatMap generalNumMap
Definition SVFStat.h:76
double startTime
Definition SVFStat.h:80
static double getClk(bool mark=false)
Definition SVFStat.cpp:48
bool getMemoryUsageKB(u32_t *vmrss_kb, u32_t *vmsize_kb)
Get memory usage from system file. Return TRUE if succeed.
Definition SVFUtil.cpp:179
for isBitcode
Definition BasicTypes.h:68
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:48
unsigned u32_t
Definition GeneralType.h:47
signed long long s64_t
Definition GeneralType.h:50