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 on: Jan 10, 2024
26// Author: Xiao Cheng, Jiawei Wang
27// The implementation is based on
28// Xiao Cheng, Jiawei Wang and Yulei Sui. Precise Sparse Abstract Execution via Cross-Domain Interaction.
29// 46th International Conference on Software Engineering. (ICSE24)
30//
31#pragma once
33#include "AE/Core/ICFGWTO.h"
35#include "AE/Svfexe/AEWTO.h"
36#include "AE/Svfexe/AbsExtAPI.h"
37#include "AE/Svfexe/AEStat.h"
38#include "SVFIR/SVFIR.h"
39#include "Util/SVFBugReport.h"
40#include "Graphs/SCC.h"
41#include "Graphs/CallGraph.h"
42#include <deque>
43
44namespace SVF
45{
46class AbstractInterpretation;
47class AbsExtAPI;
48class AEStat;
49class AEAPI;
50class AndersenWaveDiff;
51
52template<typename T> class FILOWorkList;
53
61{
62 friend class AEStat;
63 friend class AEAPI;
64 friend class BufOverflowDetector;
66
67public:
68 /*
69 * For recursive test case
70 * int demo(int a) {
71 if (a >= 10000)
72 return a;
73 demo(a+1);
74 }
75
76 int main() {
77 int result = demo(0);
78 }
79 * 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.
80 * if set WIDEN_ONLY, result = [10000, +oo] since only widening is applied at the cycle head of recursive functions without narrowing.
81 * if set WIDEN_NARROW, result = [10000, 10000] since both widening and narrowing are applied at the cycle head of recursive functions.
82 * */
89
96
97 virtual void runOnModule();
98
100 virtual ~AbstractInterpretation();
101
103 void analyse();
104
107
109 std::deque<const FunObjVar*> collectProgEntryFuns();
110
117
118 void addDetector(std::unique_ptr<AEDetector> detector)
119 {
120 detectors.push_back(std::move(detector));
121 }
122
124 inline const SVFVar* getSVFVar(NodeID varId) const
125 {
126 return svfir->getSVFVar(varId);
127 }
128
129 // ---- Abstract Value Access ----------------------------------------
130
136 virtual const AbstractValue& getAbsValue(const ValVar* var, const ICFGNode* node);
137 virtual const AbstractValue& getAbsValue(const ObjVar* var, const ICFGNode* node);
138 virtual const AbstractValue& getAbsValue(const SVFVar* var, const ICFGNode* node);
139
141 virtual bool hasAbsValue(const ValVar* var, const ICFGNode* node) const;
142 virtual bool hasAbsValue(const ObjVar* var, const ICFGNode* node) const;
143 virtual bool hasAbsValue(const SVFVar* var, const ICFGNode* node) const;
144
147 virtual void updateAbsValue(const ValVar* var, const AbstractValue& val, const ICFGNode* node);
148 virtual void updateAbsValue(const ObjVar* var, const AbstractValue& val, const ICFGNode* node);
149 virtual void updateAbsValue(const SVFVar* var, const AbstractValue& val, const ICFGNode* node);
150
151 // ---- State Access -------------------------------------------------
152
153 AbstractState& getAbsState(const ICFGNode* node);
154
157 virtual void updateAbsState(const ICFGNode* node, const AbstractState& state);
158
161 virtual void joinStates(AbstractState& dst, const AbstractState& src);
162
163 bool hasAbsState(const ICFGNode* node);
164
168
169 // ---- GEP / Load-Store / Type Helpers ------------------------------
170
174
175 AbstractValue loadValue(const ValVar* pointer, const ICFGNode* node);
176 void storeValue(const ValVar* pointer, const AbstractValue& val, const ICFGNode* node);
177
178 const SVFType* getPointeeElement(const ObjVar* var, const ICFGNode* node);
180
181 // ---- Direct Trace Access ------------------------------------------
182
188 {
189 return abstractTrace[node];
190 }
191
192protected:
196
197 // ---- Cycle helpers overridden by SparseAbstractInterpretation ----
198 // The dense versions write only to trace[cycle_head]. The semi-sparse
199 // subclass adds def-site scatter on top for body ValVars.
200
204
208 virtual bool widenCycleState(const AbstractState& prev, const AbstractState& cur,
209 const ICFGCycleWTO* cycle);
210
214 virtual bool narrowCycleState(const AbstractState& prev, const AbstractState& cur,
215 const ICFGCycleWTO* cycle);
216
217private:
219 virtual void handleGlobalNode();
220
224 bool mergeStatesFromPredecessors(const ICFGNode* node);
225
228
230 virtual void handleCallSite(const ICFGNode* node);
231
233 virtual void handleLoopOrRecursion(const ICFGCycleWTO* cycle, const CallICFGNode* caller = nullptr);
234
236 void handleFunction(const ICFGNode* funEntry, const CallICFGNode* caller = nullptr);
237
239 bool handleICFGNode(const ICFGNode* node);
240
242 virtual void handleSVFStatement(const SVFStmt* stmt);
243
246
249
250 void updateStateOnAddr(const AddrStmt *addr);
251
253
254 void updateStateOnCmp(const CmpStmt *cmp);
255
256 void updateStateOnLoad(const LoadStmt *load);
257
258 void updateStateOnStore(const StoreStmt *store);
259
260 void updateStateOnCopy(const CopyStmt *copy);
261
262 void updateStateOnCall(const CallPE *callPE);
263
264 void updateStateOnRet(const RetPE *retPE);
265
266 void updateStateOnGep(const GepStmt *gep);
267
269
270 void updateStateOnPhi(const PhiStmt *phi);
271
273 AEAPI* api{nullptr};
274
278
280 {
281 return utils;
282 }
283
284 // helper functions in handleCallSite
285 virtual bool isExtCall(const CallICFGNode* callNode);
286 virtual void handleExtCall(const CallICFGNode* callNode);
287 virtual bool isRecursiveFun(const FunObjVar* fun);
288 virtual void skipRecursionWithTop(const CallICFGNode *callNode);
289 virtual bool isRecursiveCallSite(const CallICFGNode* callNode, const FunObjVar *);
290 virtual void handleFunCall(const CallICFGNode* callNode);
291
294
295 // there data should be shared with subclasses
296 Map<std::string, std::function<void(const CallICFGNode*)>> func_map;
297
298 Set<const ICFGNode*> allAnalyzedNodes; // All nodes ever analyzed (across all entry points)
299 std::string moduleName;
300
301 std::vector<std::unique_ptr<AEDetector>> detectors;
303
304protected:
306 SVFIR* svfir{nullptr};
309
310 bool shouldApplyNarrowing(const FunObjVar* fun);
311};
312}
copy
Definition cJSON.cpp:414
newitem prev
Definition cJSON.cpp:2285
buffer offset
Definition cJSON.cpp:1113
AEStat: Statistic for AE.
Definition AEStat.h:36
Handles external API calls and manages abstract states.
Definition AbsExtAPI.h:43
void updateStateOnCall(const CallPE *callPE)
const FunObjVar * getCallee(const CallICFGNode *callNode)
Get callee function: directly for direct calls, via pointer analysis for indirect calls.
AbstractState & getAbsState(const ICFGNode *node)
bool isBranchFeasible(const IntraCFGEdge *edge, AbstractState &as)
Returns true if the branch is reachable; narrows as in-place.
u32_t getAllocaInstByteSize(const AddrStmt *addr)
Map< std::string, std::function< void(const CallICFGNode *)> > func_map
void updateStateOnStore(const StoreStmt *store)
virtual bool narrowCycleState(const AbstractState &prev, const AbstractState &cur, const ICFGCycleWTO *cycle)
virtual void handleFunCall(const CallICFGNode *callNode)
void analyzeFromAllProgEntries()
Analyze all entry points (functions without callers)
void updateStateOnGep(const GepStmt *gep)
virtual void handleGlobalNode()
Initialize abstract state for the global ICFG node and process global statements.
virtual bool hasAbsValue(const ValVar *var, const ICFGNode *node) const
Side-effect-free existence check.
void handleFunction(const ICFGNode *funEntry, const CallICFGNode *caller=nullptr)
Handle a function body via worklist-driven WTO traversal starting from funEntry.
virtual bool isExtCall(const CallICFGNode *callNode)
bool isSwitchBranchFeasible(const IntraCFGEdge *edge, AbstractState &as)
Returns true if the switch branch is feasible; narrows as in-place.
AbstractState & operator[](const ICFGNode *node)
virtual AbstractState getFullCycleHeadState(const ICFGCycleWTO *cycle)
void updateStateOnPhi(const PhiStmt *phi)
bool handleICFGNode(const ICFGNode *node)
Handle an ICFG node: execute statements; return true if state changed.
IntervalValue getGepByteOffset(const GepStmt *gep)
std::vector< std::unique_ptr< AEDetector > > detectors
void addDetector(std::unique_ptr< AEDetector > detector)
AbstractValue loadValue(const ValVar *pointer, const ICFGNode *node)
virtual void handleExtCall(const CallICFGNode *callNode)
Map< const ICFGNode *, AbstractState > & getTrace()
AddressValue getGepObjAddrs(const ValVar *pointer, IntervalValue offset)
virtual void skipRecursionWithTop(const CallICFGNode *callNode)
virtual bool widenCycleState(const AbstractState &prev, const AbstractState &cur, const ICFGCycleWTO *cycle)
IntervalValue getGepElementIndex(const GepStmt *gep)
virtual void joinStates(AbstractState &dst, const AbstractState &src)
bool mergeStatesFromPredecessors(const ICFGNode *node)
void updateStateOnSelect(const SelectStmt *select)
virtual void handleSVFStatement(const SVFStmt *stmt)
Dispatch an SVF statement (Addr/Binary/Cmp/Load/Store/Copy/Gep/Select/Phi/Call/Ret) to its handler.
bool skipRecursiveCall(const CallICFGNode *callNode)
Skip recursive callsites (within SCC); entry calls from outside SCC are not skipped.
SVFIR * svfir
Data and helpers reachable from SparseAbstractInterpretation.
virtual bool isRecursiveCallSite(const CallICFGNode *callNode, const FunObjVar *)
Check if caller and callee are in the same CallGraph SCC (i.e. a recursive callsite)
bool shouldApplyNarrowing(const FunObjVar *fun)
Check if narrowing should be applied: always for regular loops, mode-dependent for recursion.
virtual bool isRecursiveFun(const FunObjVar *fun)
Check if a function is recursive (part of a call graph SCC)
void updateStateOnAddr(const AddrStmt *addr)
virtual ~AbstractInterpretation()
Destructor.
virtual const AbstractValue & getAbsValue(const ValVar *var, const ICFGNode *node)
virtual void handleCallSite(const ICFGNode *node)
Handle a call site node: dispatch to ext-call, direct-call, or indirect-call handling.
void updateStateOnRet(const RetPE *retPE)
bool hasAbsState(const ICFGNode *node)
void updateStateOnCopy(const CopyStmt *copy)
std::deque< const FunObjVar * > collectProgEntryFuns()
Get all entry point functions (functions without callers)
AEAPI * api
Execution State, used to store the Interval Value of every SVF variable.
const SVFType * getPointeeElement(const ObjVar *var, const ICFGNode *node)
void updateStateOnLoad(const LoadStmt *load)
void updateStateOnBinary(const BinaryOPStmt *binary)
Map< const ICFGNode *, AbstractState > abstractTrace
per-node trace; owned here
static AbstractInterpretation & getAEInstance()
bool isCmpBranchFeasible(const IntraCFGEdge *edge, AbstractState &as)
Returns true if the cmp-conditional branch is feasible; narrows as in-place.
virtual void updateAbsValue(const ValVar *var, const AbstractValue &val, const ICFGNode *node)
Set< const ICFGNode * > allAnalyzedNodes
void storeValue(const ValVar *pointer, const AbstractValue &val, const ICFGNode *node)
virtual void handleLoopOrRecursion(const ICFGCycleWTO *cycle, const CallICFGNode *caller=nullptr)
Handle a WTO cycle (loop or recursive function) using widening/narrowing iteration.
const SVFVar * getSVFVar(NodeID varId) const
Retrieve SVFVar given its ID; asserts if no such variable exists.
void updateStateOnCmp(const CmpStmt *cmp)
virtual void updateAbsState(const ICFGNode *node, const AbstractState &state)
Detector for identifying buffer overflow issues.
Definition AEDetector.h:139
const SVFVar * getSVFVar(NodeID id) const
ObjVar/GepObjVar/BaseObjVar.
Definition SVFIR.h:133
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:56
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:47