-
Notifications
You must be signed in to change notification settings - Fork 438
/
ProgSlice.cpp
248 lines (222 loc) · 8.83 KB
/
ProgSlice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//===- ProgSlice.cpp -- Program slicing--------------------------------------//
//
// SVF: Static Value-Flow Analysis
//
// Copyright (C) <2013-> <Yulei Sui>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
/*
* ProgSlice.cpp
*
* Created on: Apr 5, 2014
* Author: Yulei Sui
*/
#include "SABER/ProgSlice.h"
using namespace SVF;
using namespace SVFUtil;
/*!
* Compute path conditions for nodes on the backward slice
* path condition of each node is calculated starting from root node (source)
* Given a SVFGNode n, its path condition C is allocated (path_i stands for one of m program paths reaches n)
*
* C = \bigvee Guard(path_i), 0 < i < m
* Guard(path_i) = \bigwedge VFGGuard(x,y), suppose (x,y) are two SVFGNode nodes on path_i
*/
bool ProgSlice::AllPathReachableSolve()
{
const SVFGNode* source = getSource();
VFWorkList worklist;
worklist.push(source);
/// mark source node conditions to be true
setVFCond(source,getTrueCond());
while(!worklist.empty())
{
const SVFGNode* node = worklist.pop();
setCurSVFGNode(node);
Condition invalidCond = computeInvalidCondFromRemovedSUVFEdge(node);
Condition cond = getVFCond(node);
for(SVFGNode::const_iterator it = node->OutEdgeBegin(), eit = node->OutEdgeEnd(); it!=eit; ++it)
{
const SVFGEdge* edge = (*it);
const SVFGNode* succ = edge->getDstNode();
if(inBackwardSlice(succ))
{
Condition vfCond;
const SVFBasicBlock* nodeBB = getSVFGNodeBB(node);
const SVFBasicBlock* succBB = getSVFGNodeBB(succ);
/// clean up the control flow conditions for next round guard computation
clearCFCond();
if(edge->isCallVFGEdge())
{
vfCond = ComputeInterCallVFGGuard(nodeBB,succBB, getCallSite(edge)->getParent());
}
else if(edge->isRetVFGEdge())
{
vfCond = ComputeInterRetVFGGuard(nodeBB,succBB, getRetSite(edge)->getParent());
}
else
vfCond = ComputeIntraVFGGuard(nodeBB,succBB);
vfCond = condAnd(vfCond, condNeg(invalidCond));
Condition succPathCond = condAnd(cond, vfCond);
if(setVFCond(succ, condOr(getVFCond(succ), succPathCond) ))
worklist.push(succ);
}
DBOUT(DSaber, outs() << " node (" << node->getId() <<
") --> " << "succ (" << succ->getId() << ") condition: " << getVFCond(succ) << "\n");
}
}
return isSatisfiableForAll();
}
/*!
* Compute invalid branch condition stemming from removed strong update value-flow edges
*
* Fix issue: https://github.com/SVF-tools/SVF/issues/1306
* Line 11->13 is removed due to a strong update at Line 13, which means Line 11 is unreachable to Line 13 on the value flow graph.
* However on the control flow graph they are still considered as reachable,
* making the vf guard on Line 11 -> Line 15 a true condition (should consider the infeasible branch Line 11 -> Line 13)
* Therefore, we collect this infeasible branch condition (condition on Line 11 -> Line 13, `a == b`) as an invalid condition (invalidCond),
* and add the negation of invalidCond when computing value flow guard starting from the source of the SU.
* In this example, we add `a != b` on Line 11 -> Line 15.
*
* @param cur current SVFG node
* @return invalid branch condition
*/
ProgSlice::Condition ProgSlice::computeInvalidCondFromRemovedSUVFEdge(const SVFGNode * cur)
{
Set<const SVFBasicBlock*> validOutBBs; // the BBs of valid successors
for(SVFGNode::const_iterator it = cur->OutEdgeBegin(), eit = cur->OutEdgeEnd(); it!=eit; ++it)
{
const SVFGEdge* edge = (*it);
const SVFGNode* succ = edge->getDstNode();
if(inBackwardSlice(succ))
{
validOutBBs.insert(getSVFGNodeBB(succ));
}
}
Condition invalidCond = getFalseCond();
auto suVFEdgesIt = getRemovedSUVFEdges().find(cur);
if (suVFEdgesIt != getRemovedSUVFEdges().end())
{
for (const auto &succ: suVFEdgesIt->second)
{
if (!validOutBBs.count(getSVFGNodeBB(succ)))
{
// removed vfg node does not reside in the BBs of valid successors
const SVFBasicBlock *nodeBB = getSVFGNodeBB(cur);
const SVFBasicBlock *succBB = getSVFGNodeBB(succ);
clearCFCond();
invalidCond = condOr(invalidCond, ComputeIntraVFGGuard(nodeBB, succBB));
}
}
}
return invalidCond;
}
/*!
* Solve by computing disjunction of conditions from all sinks (e.g., memory leak)
*/
bool ProgSlice::isSatisfiableForAll()
{
Condition guard = getFalseCond();
for(SVFGNodeSetIter it = sinksBegin(), eit = sinksEnd(); it!=eit; ++it)
{
guard = condOr(guard,getVFCond(*it));
}
setFinalCond(guard);
return pathAllocator->isAllPathReachable(guard);
}
/*!
* Solve by analysing each pair of sinks (e.g., double free)
*/
bool ProgSlice::isSatisfiableForPairs()
{
for(SVFGNodeSetIter it = sinksBegin(), eit = sinksEnd(); it!=eit; ++it)
{
for(SVFGNodeSetIter sit = it, esit = sinksEnd(); sit!=esit; ++sit)
{
if(*it == *sit)
continue;
Condition guard = condAnd(getVFCond(*sit),getVFCond(*it));
if(!isEquivalentBranchCond(guard, getFalseCond()))
{
setFinalCond(guard);
return false;
}
}
}
return true;
}
const CallICFGNode* ProgSlice::getCallSite(const SVFGEdge* edge) const
{
assert(edge->isCallVFGEdge() && "not a call svfg edge?");
if(const CallDirSVFGEdge* callEdge = SVFUtil::dyn_cast<CallDirSVFGEdge>(edge))
return getSVFG()->getCallSite(callEdge->getCallSiteId());
else
return getSVFG()->getCallSite(SVFUtil::cast<CallIndSVFGEdge>(edge)->getCallSiteId());
}
const CallICFGNode* ProgSlice::getRetSite(const SVFGEdge* edge) const
{
assert(edge->isRetVFGEdge() && "not a return svfg edge?");
if(const RetDirSVFGEdge* callEdge = SVFUtil::dyn_cast<RetDirSVFGEdge>(edge))
return getSVFG()->getCallSite(callEdge->getCallSiteId());
else
return getSVFG()->getCallSite(SVFUtil::cast<RetIndSVFGEdge>(edge)->getCallSiteId());
}
void ProgSlice::evalFinalCond2Event(GenericBug::EventStack &eventStack) const
{
NodeBS elems = pathAllocator->exactCondElem(finalCond);
for(NodeBS::iterator it = elems.begin(), eit = elems.end(); it!=eit; ++it)
{
const ICFGNode* tinst = pathAllocator->getCondInst(*it);
if(pathAllocator->isNegCond(*it))
eventStack.push_back(SVFBugEvent(
SVFBugEvent::Branch|((((u32_t)false) << 4) & BRANCHFLAGMASK), tinst));
else
eventStack.push_back(SVFBugEvent(
SVFBugEvent::Branch|((((u32_t)true) << 4) & BRANCHFLAGMASK), tinst));
}
}
/*!
* Evaluate Atoms of a condition
* TODO: for now we only evaluate one path, evaluate every single path
*
* Atom -- a propositional variable: a, b, c
* Literal -- an atom or its negation: a, ~a
* Clause -- A disjunction of some literals: a \vee b
* CNF formula -- a conjunction of some clauses: (a \vee b ) \wedge (c \vee d)
*/
std::string ProgSlice::evalFinalCond() const
{
std::string str;
std::stringstream rawstr(str);
Set<std::string> locations;
NodeBS elems = pathAllocator->exactCondElem(finalCond);
for(NodeBS::iterator it = elems.begin(), eit = elems.end(); it!=eit; ++it)
{
const ICFGNode* tinst = pathAllocator->getCondInst(*it);
if(pathAllocator->isNegCond(*it))
locations.insert(tinst->getSourceLoc()+"|False");
else
locations.insert(tinst->getSourceLoc()+"|True");
}
/// print leak path after eliminating duplicated element
for(Set<std::string>::iterator iter = locations.begin(), eiter = locations.end();
iter!=eiter; ++iter)
{
rawstr << "\t\t --> (" << *iter << ") \n";
}
return rawstr.str();
}
void ProgSlice::destroy()
{
}