-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathZ3BasedPathSensitivityManager.cpp
More file actions
580 lines (487 loc) · 18 KB
/
Z3BasedPathSensitivityManager.cpp
File metadata and controls
580 lines (487 loc) · 18 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include "phasar/PhasarLLVM/DataFlow/PathSensitivity/LLVMPathConstraints.h"
#include "phasar/PhasarLLVM/DataFlow/PathSensitivity/Z3BasedPathSensitvityManager.h"
#include "phasar/PhasarLLVM/Utils/LLVMShorthands.h"
#include "phasar/Utils/Logger.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Support/Casting.h"
namespace psr {
z3::expr Z3BasedPathSensitivityManagerBase::filterOutUnreachableNodes(
graph_type &RevDAG, vertex_t Leaf,
const Z3BasedPathSensitivityConfig &Config,
LLVMPathConstraints &LPC) const {
struct FilterContext {
llvm::BitVector Visited{};
z3::expr True;
z3::expr False;
llvm::SmallVector<z3::expr, 0> NodeConstraints{};
size_t Ctr = 0;
z3::solver Solver;
explicit FilterContext(z3::context &Z3Ctx)
: True(Z3Ctx.bool_val(true)), False(Z3Ctx.bool_val(false)),
Solver(Z3Ctx) {}
} Ctx(LPC.getContext());
Ctx.Visited.resize(graph_traits_t::size(RevDAG));
Ctx.NodeConstraints.resize(graph_traits_t::size(RevDAG), Ctx.True);
[[maybe_unused]] size_t TotalNumEdges = 0;
for (auto I : graph_traits_t::vertices(RevDAG)) {
TotalNumEdges += graph_traits_t::outDegree(RevDAG, I);
}
if (Config.AdditionalConstraint) {
Ctx.Solver.add(*Config.AdditionalConstraint);
}
// NOLINTNEXTLINE(readability-identifier-naming)
auto doFilter = [&Ctx, &RevDAG, &LPC, Leaf](auto &doFilter,
vertex_t Vtx) -> z3::expr {
Ctx.Visited.set(Vtx);
z3::expr X = Ctx.True;
llvm::ArrayRef<n_t> PartialPath = graph_traits_t::node(RevDAG, Vtx);
assert(!PartialPath.empty());
for (size_t I = PartialPath.size() - 1; I; --I) {
if (auto Constr =
LPC.getConstraintFromEdge(PartialPath[I], PartialPath[I - 1])) {
X = X && *Constr;
}
}
llvm::SmallVector<z3::expr> Ys;
for (const auto *Iter = graph_traits_t::outEdges(RevDAG, Vtx).begin();
Iter != graph_traits_t::outEdges(RevDAG, Vtx).end();) {
// NOLINTNEXTLINE(readability-qualified-auto, llvm-qualified-auto)
auto It = Iter++;
auto Edge = *It;
auto Adj = graph_traits_t::target(Edge);
if (!Ctx.Visited.test(Adj)) {
doFilter(doFilter, Adj);
}
Ctx.Solver.push();
Ctx.Solver.add(X);
auto Y = Ctx.NodeConstraints[Adj];
const auto &AdjPP = graph_traits_t::node(RevDAG, Adj);
assert(!AdjPP.empty());
if (auto Constr =
LPC.getConstraintFromEdge(PartialPath.front(), AdjPP.back())) {
Y = Y && *Constr;
}
Ctx.Solver.add(Y);
auto Sat = Ctx.Solver.check();
if (Sat == z3::check_result::unsat) {
Iter = graph_traits_t::removeEdge(RevDAG, Vtx, It);
Ctx.Ctr++;
} else {
Ys.push_back(std::move(Y));
}
Ctx.Solver.pop();
// Idx++;
}
if (graph_traits_t::outDegree(RevDAG, Vtx) == 0) {
return Ctx.NodeConstraints[Vtx] = Vtx == Leaf ? X : Ctx.False;
}
if (Ys.empty()) {
llvm_unreachable("Adj nonempty and Ys empty is unexpected");
}
auto Y = Ys[0];
for (const auto &Constr : llvm::ArrayRef(Ys).drop_front()) {
Y = Y || Constr;
}
return Ctx.NodeConstraints[Vtx] = (X && Y).simplify();
};
z3::expr Ret = LPC.getContext().bool_val(false);
for (const auto *Iter = graph_traits_t::roots(RevDAG).begin();
Iter != graph_traits_t::roots(RevDAG).end();) {
// NOLINTNEXTLINE(readability-qualified-auto, llvm-qualified-auto)
auto It = Iter++;
auto Rt = *It;
auto Res = doFilter(doFilter, Rt);
Ret = Ret || Res;
if (Rt != Leaf && RevDAG.Adj[Rt].empty()) {
// llvm::errs() << "> Remove root " << Rt << "\n";
Iter = graph_traits_t::removeRoot(RevDAG, It);
}
}
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
"> Filtered out " << Ctx.Ctr << " edges from the DAG");
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
">> " << (TotalNumEdges - Ctx.Ctr)
<< " edges remaining");
return Ret.simplify();
}
/// This could be a C++20 concept!
struct PathFilter {
// void saveState();
// void restoreState();
// void saveEdge(n_t Prev, n_t Inst);
// bool isValid() const;
// bool saveFinalEdge(n_t Prev, n_t FinalInst);
};
template <typename... T> class PathFilterList : public std::tuple<T...> {
public:
using n_t = const llvm::Instruction *;
using std::tuple<T...>::tuple;
void saveState() { saveStateImpl(std::make_index_sequence<sizeof...(T)>()); }
void restoreState() {
restoreStateImpl(std::make_index_sequence<sizeof...(T)>());
}
void saveEdge(n_t Prev, n_t Inst) {
saveEdgeImpl(Prev, Inst, std::make_index_sequence<sizeof...(T)>());
}
[[nodiscard]] bool isValid() {
return isValidImpl(std::make_index_sequence<sizeof...(T)>());
}
bool saveFinalEdge(n_t Prev, n_t Inst) {
return saveFinalEdgeImpl(Prev, Inst,
std::make_index_sequence<sizeof...(T)>());
}
private:
template <size_t... I>
void saveStateImpl(std::index_sequence<I...> /*unused*/) {
(std::get<I>(*this).saveState(), ...);
}
template <size_t... I>
void restoreStateImpl(std::index_sequence<I...> /*unused*/) {
(std::get<I>(*this).restoreState(), ...);
}
template <size_t... I>
void saveEdgeImpl(n_t Prev, n_t Inst, std::index_sequence<I...> /*unused*/) {
(std::get<I>(*this).saveEdge(Prev, Inst), ...);
}
template <size_t... I>
bool saveFinalEdgeImpl(n_t Prev, n_t Inst,
std::index_sequence<I...> /*unused*/) {
return (std::get<I>(*this).saveFinalEdge(Prev, Inst) && ...);
}
template <size_t... I>
bool isValidImpl(std::index_sequence<I...> /*unused*/) {
return (std::get<I>(*this).isValid() && ...);
}
};
template <typename... T>
static PathFilterList<T...> makePathFilterList(T &&...Filters) {
return PathFilterList<T...>(std::forward<T>(Filters)...);
}
class CallStackPathFilter {
public:
using n_t = const llvm::Instruction *;
void saveState() { CallStackSafe.emplace_back(CallStackOwner.size(), TOS); }
void restoreState() {
auto [SaveCallStackSize, CSSave] = CallStackSafe.pop_back_val();
assert(CallStackOwner.size() >= SaveCallStackSize);
CallStackOwner.pop_back_n(CallStackOwner.size() - SaveCallStackSize);
TOS = CSSave;
Valid = true;
}
void saveEdge(n_t Prev, n_t Inst) {
if (!Valid) {
return;
}
if (const auto *CS = llvm::dyn_cast<llvm::CallBase>(Prev);
CS && !isDirectSuccessorOf(Inst, CS)) {
PHASAR_LOG_LEVEL_CAT(DEBUG, "CallStackPathFilter",
"Push CS: " << llvmIRToString(CS));
pushCS(CS);
} else if (llvm::isa<llvm::ReturnInst>(Prev) ||
llvm::isa<llvm::ResumeInst>(Prev)) {
/// Allow unbalanced returns
if (!emptyCS()) {
const auto *CS = popCS();
PHASAR_LOG_LEVEL_CAT(DEBUG, "CallStackPathFilter",
"Pop CS: " << llvmIRToString(CS) << " at exit "
<< llvmIRToString(Prev)
<< " and ret-site "
<< llvmIRToString(Inst));
if (!isDirectSuccessorOf(Inst, CS)) {
/// Invalid return
Valid = false;
PHASAR_LOG_LEVEL_CAT(DEBUG, "CallStackPathFilter",
"> Invalid return");
} else {
PHASAR_LOG_LEVEL_CAT(DEBUG, "CallStackPathFilter", "> Valid return");
}
} else {
PHASAR_LOG_LEVEL_CAT(DEBUG, "CallStackPathFilter",
"> Unbalanced return at exit "
<< llvmIRToString(Prev));
}
/// else: unbalanced return
}
}
bool saveFinalEdge(n_t Prev, n_t FinalInst) {
if (!Valid) {
return false;
}
if (Prev && (llvm::isa<llvm::ReturnInst>(Prev) ||
llvm::isa<llvm::ResumeInst>(Prev))) {
if (!emptyCS() && !isDirectSuccessorOf(FinalInst, popCS())) {
/// Invalid return
return false;
}
}
return true;
}
[[nodiscard]] bool isValid() const noexcept { return Valid; }
private:
bool isDirectSuccessorOf(const llvm::Instruction *Succ,
const llvm::Instruction *Of) {
while (const auto *Nxt = Of->getNextNode()) {
if (Nxt == Succ) {
return true;
}
Of = Nxt;
if (Nxt->isTerminator()) {
break;
}
if (const auto *Call = llvm::dyn_cast<llvm::CallBase>(Nxt);
Call && Call->getCalledFunction() &&
!Call->getCalledFunction()->isDeclaration()) {
/// Don't skip function calls. We might call the same fun twice in the
/// same BB, so we recognize invalid paths there as well!
return false;
}
}
assert(Of->isTerminator());
return std::find_if(llvm::succ_begin(Of), llvm::succ_end(Of),
[&Succ](const llvm::BasicBlock *BB) {
return &BB->front() == Succ;
}) != llvm::succ_end(Of);
}
void pushCS(const llvm::CallBase *CS) {
auto *NewNode = &CallStackOwner.emplace_back();
NewNode->Prev = TOS;
NewNode->CS = CS;
TOS = NewNode;
}
const llvm::CallBase *popCS() noexcept {
assert(TOS && "We should already have checked for nullness...");
const auto *Ret = TOS->CS;
TOS = TOS->Prev;
/// Defer the deallocation, such that we still can rollback
return Ret;
}
[[nodiscard]] bool emptyCS() const noexcept { return TOS == nullptr; }
struct CallStackNode {
CallStackNode *Prev = nullptr;
const llvm::CallBase *CS = nullptr;
};
/// Now, filter directly on the reversed DAG
StableVector<CallStackNode> CallStackOwner;
llvm::SmallVector<std::pair<size_t, CallStackNode *>> CallStackSafe;
CallStackNode *TOS = nullptr;
bool Valid = true;
};
class ConstraintPathFilter {
public:
using n_t = const llvm::Instruction *;
ConstraintPathFilter(LLVMPathConstraints &LPC,
const z3::expr &AdditionalConstraint,
const size_t *CompletedCtr) noexcept
: LPC(LPC), Solver(LPC.getContext()), CompletedCtr(*CompletedCtr) {
Solver.add(AdditionalConstraint);
Solver.push();
NumAtomsStack.push_back(0);
}
void saveState() {
NumAtomsStack.push_back(NumAtomsStack.back());
Solver.push();
}
void restoreState() {
Solver.pop();
auto NumAtoms = NumAtomsStack.pop_back_val();
assert(!NumAtomsStack.empty());
auto Diff = NumAtoms - NumAtomsStack.back();
for (size_t I = 0; I < Diff; ++I) {
SymbolicAtoms.pop_back();
}
NeedSolverInvocation = false;
LocalAtoms.clear();
Model = std::nullopt;
}
void saveEdge(n_t Prev, n_t Inst) {
if (auto ConstrAndVariables =
LPC.getConstraintAndVariablesFromEdge(Prev, Inst)) {
Solver.add(ConstrAndVariables->Constraint);
LocalAtoms.append(ConstrAndVariables->Variables);
}
}
[[nodiscard]] bool isValid() {
++IsValidCalls;
if (LocalAtoms.empty() && !NeedSolverInvocation) {
/// Nothing (new) to check
return true;
}
if (!checkLocalAtomsOverlap()) {
return true;
}
NeedSolverInvocation = false;
auto Res = Solver.check();
++Ctr;
#ifdef DYNAMIC_LOG
if (Ctr % 10000 == 0) {
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
Ctr << " solver invocations so far...");
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
">> " << IsValidCalls << " calls to isValid()");
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
">> " << RejectedCtr << " paths rejected");
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
">> " << CompletedCtr << " paths completed");
}
#endif
auto Ret = Res != z3::check_result::unsat;
if (!Ret) {
++RejectedCtr;
} else {
Model = Solver.get_model();
}
return Ret;
}
bool saveFinalEdge(n_t Prev, n_t FinalInst) {
saveEdge(Prev, FinalInst);
NeedSolverInvocation = true;
return isValid();
}
z3::expr getPathConstraints() {
auto Vec = Solver.assertions();
if (Vec.empty()) {
return LPC.getContext().bool_val(true);
}
auto Ret = Vec[0];
for (int I = 1, End = int(Vec.size()); I != End; ++I) {
Ret = Ret && Vec[I];
}
return Ret.simplify();
}
z3::model getModel() { return Model.value(); }
[[nodiscard]] size_t getNumSolverInvocations() const noexcept { return Ctr; }
private:
[[nodiscard]] bool checkLocalAtomsOverlap() {
if (NeedSolverInvocation) {
return true;
}
std::sort(LocalAtoms.begin(), LocalAtoms.end());
LocalAtoms.erase(std::unique(LocalAtoms.begin(), LocalAtoms.end()),
LocalAtoms.end());
size_t NumLocalAtoms = LocalAtoms.size();
size_t OldSize = SymbolicAtoms.size();
SymbolicAtoms.insert(LocalAtoms.begin(), LocalAtoms.end());
size_t NewSize = SymbolicAtoms.size();
LocalAtoms.clear();
/// The newly added atoms don't overlap with the atoms from the
/// already seen constraints. So, there can be no contradicion (would
/// have already been filtered out in the previous step)
return NewSize - OldSize != NumLocalAtoms;
}
LLVMPathConstraints &LPC;
z3::solver Solver;
llvm::SmallSetVector<const llvm::Value *, 8> SymbolicAtoms;
llvm::SmallVector<unsigned> NumAtomsStack;
llvm::SmallVector<const llvm::Value *> LocalAtoms;
std::optional<z3::model> Model;
bool NeedSolverInvocation = false;
size_t Ctr = 0;
size_t RejectedCtr = 0;
size_t IsValidCalls = 0;
[[maybe_unused]] const size_t &CompletedCtr;
};
auto Z3BasedPathSensitivityManagerBase::filterAndFlattenRevDag(
graph_type &RevDAG, vertex_t Leaf, n_t FinalInst,
const Z3BasedPathSensitivityConfig &Config, LLVMPathConstraints &LPC) const
-> FlowPathSequence<n_t> {
/// Here, we do the following:
/// - Traversing the ReverseDAG in a simple DFS order and maintaining the
/// exact path reaching the current node.
/// - On the fly constructing and updating a call-stack to regain
/// context-sensitivity by filtering out paths with invalid returns
/// - Similarly on the fly constructing and solving Z3 Path Constraints and
/// filtering out all paths with unsatisfiable constraints
/// - Saving all "surviving" paths that end at a leaf to the overall vector
/// that gets returned at the end
/// Problem: We still have way too many Z3 solver invocations (> 900000 for
/// some small test programs)
/// Solution idea: In contrast to the context sensitivity check, the Path
/// constraints are context-independent. So, it might be beneficial to
/// compute the end-reachability constraints of each _node_ in a bottom-up
/// fashion leading to PathNodeOwner.size() solver invocations. We then
/// still need a subsequent DFS order traversal to collect all remaining
/// satisfiable paths, so there we can still apply the context-sensitivity
/// check OTF.
/// NOTE: This is implemented now in filterOutUnreachableNodes()
FlowPathSequence<n_t> Ret;
size_t CompletedCtr = 0;
auto Filters = makePathFilterList(
CallStackPathFilter{},
ConstraintPathFilter{
LPC,
Config.AdditionalConstraint.value_or(LPC.getContext().bool_val(true)),
&CompletedCtr});
llvm::SmallVector<n_t, 0> CurrPath;
n_t Prev = nullptr;
auto doFilter = [FinalInst, &Prev, &Filters, &RevDAG, &CurrPath, &Ret,
&CompletedCtr, MaxNumPaths{Config.NumPathsThreshold},
Leaf](auto &doFilter, vertex_t Vtx) {
auto CurrPathSave = CurrPath.size();
scope_exit RestoreCurrPath = [&CurrPath, CurrPathSave] {
assert(CurrPathSave <= CurrPath.size());
CurrPath.resize(CurrPathSave);
};
const auto *PrevSave = Prev;
scope_exit RestorePrev = [PrevSave, &Prev] { Prev = PrevSave; };
Filters.saveState();
scope_exit RestoreFilters = [&Filters] { Filters.restoreState(); };
for (const auto *Inst : llvm::reverse(graph_traits_t::node(RevDAG, Vtx))) {
CurrPath.push_back(Inst);
if (!Prev) {
Prev = Inst;
continue;
}
Filters.saveEdge(Prev, Inst);
Prev = Inst;
}
if (Vtx == Leaf) {
// llvm::errs() << "> Reached Leaf!\n";
assert(!CurrPath.empty() && "Reported paths must not be empty!");
/// Reached the end
/// TODO: No need to add the final inst separately anymore. Now, it
/// has its own PathNode and is handled implicitly
if (Filters.saveFinalEdge(Prev, FinalInst)) {
auto Model = std::get<1>(Filters).getModel();
Ret.emplace_back(CurrPath, std::get<1>(Filters).getPathConstraints(),
Model);
++CompletedCtr;
}
return;
}
if (graph_traits_t::outDegree(RevDAG, Vtx) == 0) {
llvm::report_fatal_error("Non-leaf node has no successors!");
}
if (CompletedCtr >= MaxNumPaths) {
return;
}
if (!Filters.isValid()) {
return;
}
/// TODO: Verify that we have no concurrent modification here and the
/// iterator is never dangling!
for (auto Edge : graph_traits_t::outEdges(RevDAG, Vtx)) {
doFilter(doFilter, graph_traits_t::target(Edge));
}
};
for (auto Rt : graph_traits_t::roots(RevDAG)) {
doFilter(doFilter, Rt);
}
PHASAR_LOG_LEVEL_CAT(DEBUG, "PathSensitivityManager",
"Num Solver invocations: "
<< std::get<1>(Filters).getNumSolverInvocations());
return Ret;
}
void Z3BasedPathSensitivityManagerBase::deduplicatePaths(
FlowPathSequence<n_t> &Paths) {
/// Some kind of lexical sort for being able to deduplicate the paths easily
std::sort(Paths.begin(), Paths.end(),
[](const FlowPath<n_t> &LHS, const FlowPath<n_t> &RHS) {
return LHS.size() < RHS.size() ||
(LHS.size() == RHS.size() &&
std::lexicographical_compare(LHS.begin(), LHS.end(),
RHS.begin(), RHS.end()));
});
Paths.erase(std::unique(Paths.begin(), Paths.end()), Paths.end());
}
} // namespace psr