• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alan-Jowett / ebpf-verifier / 21968098219

12 Feb 2026 11:19PM UTC coverage: 87.328% (+0.5%) from 86.783%
21968098219

Pull #161

github

web-flow
Merge fcfbad30c into f1f1d42d7
Pull Request #161: Failure slice

527 of 677 new or added lines in 8 files covered. (77.84%)

20 existing lines in 2 files now uncovered.

10020 of 11474 relevant lines covered (87.33%)

2947086.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.82
/src/printing.cpp
1
// Copyright (c) Prevail Verifier contributors.
2
// SPDX-License-Identifier: MIT
3
#include <fstream>
4
#include <iomanip>
5
#include <iostream>
6
#include <variant>
7
#include <vector>
8

9
#include "arith/num_big.hpp"
10
#include "arith/variable.hpp"
11
#include "cfg/cfg.hpp"
12
#include "crab/interval.hpp"
13
#include "crab/type_encoding.hpp"
14
#include "crab/var_registry.hpp"
15
#include "ir/syntax.hpp"
16
#include "linux/gpl/spec_type_descriptors.hpp"
17
#include "platform.hpp"
18
#include "spec/function_prototypes.hpp"
19
#include "verifier.hpp"
20

21
using std::optional;
22
using std::string;
23
using std::vector;
24

25
namespace prevail {
26

27
std::ostream& operator<<(std::ostream& o, const Interval& interval) {
688✔
28
    if (interval.is_bottom()) {
688✔
29
        o << "_|_";
×
30
    } else {
31
        o << "[" << interval._lb << ", " << interval._ub << "]";
688✔
32
    }
33
    return o;
688✔
34
}
35
std::ostream& operator<<(std::ostream& o, const Number& z) { return o << z._n.str(); }
311,692✔
36

37
std::string Number::to_string() const { return _n.str(); }
×
38

39
std::string Interval::to_string() const {
×
40
    std::ostringstream s;
×
41
    s << *this;
×
42
    return s.str();
×
43
}
×
44

45
std::ostream& operator<<(std::ostream& os, const Label& label) {
1,542✔
46
    if (label == Label::entry) {
1,542✔
47
        return os << "entry";
6✔
48
    }
49
    if (label == Label::exit) {
1,536✔
50
        return os << "exit";
10✔
51
    }
52
    if (!label.stack_frame_prefix.empty()) {
1,526✔
53
        os << label.stack_frame_prefix << STACK_FRAME_DELIMITER;
330✔
54
    }
55
    os << label.from;
1,526✔
56
    if (label.to != -1) {
1,526✔
57
        os << ":" << label.to;
50✔
58
    }
59
    if (!label.special_label.empty()) {
1,526✔
60
        os << " (" << label.special_label << ")";
20✔
61
    }
62
    return os;
763✔
63
}
64

65
string to_string(Label const& label) {
1,164✔
66
    std::stringstream str;
1,164✔
67
    str << label;
1,164✔
68
    return str.str();
2,328✔
69
}
1,164✔
70

71
struct LineInfoPrinter {
1✔
72
    std::ostream& os;
73
    std::string previous_source_line;
74

75
    void print_line_info(const Label& label) {
6✔
76
        if (thread_local_options.verbosity_opts.print_line_info) {
6✔
77
            const auto& line_info_map = thread_local_program_info.get().line_info;
×
78
            const auto& line_info = line_info_map.find(label.from);
×
79
            // Print line info only once.
80
            if (line_info != line_info_map.end() && line_info->second.source_line != previous_source_line) {
×
81
                os << "\n" << line_info->second << "\n";
×
82
                previous_source_line = line_info->second.source_line;
×
83
            }
84
        }
85
    }
6✔
86
};
87

88
struct DetailedPrinter : LineInfoPrinter {
89
    const Program& prog;
90

91
    DetailedPrinter(std::ostream& os, const Program& prog) : LineInfoPrinter{os}, prog(prog) {}
2✔
92

93
    void print_labels(const std::string& direction, const std::set<Label>& labels) {
6✔
94
        auto [it, et] = std::pair{labels.begin(), labels.end()};
6✔
95
        if (it != et) {
6✔
96
            os << "  " << direction << " ";
6✔
97
            while (it != et) {
12✔
98
                os << *it;
6✔
99
                ++it;
6✔
100
                if (it == et) {
6✔
101
                    os << ";";
6✔
102
                } else {
103
                    os << ",";
×
104
                }
105
            }
106
        }
107
        os << "\n";
6✔
108
    }
6✔
109

110
    void print_jump(const std::string& direction, const Label& label) {
6✔
111
        print_labels(direction, direction == "from" ? prog.cfg().parents_of(label) : prog.cfg().children_of(label));
6✔
112
    }
6✔
113

114
    void print_instruction(const Program& prog, const Label& label) {
×
115
        for (const auto& pre : prog.assertions_at(label)) {
×
116
            os << "  " << "assert " << pre << ";\n";
×
117
        }
×
118
        os << "  " << prog.instruction_at(label) << ";\n";
×
119
    }
×
120
};
121

122
void print_program(const Program& prog, std::ostream& os, const bool simplify) {
×
123
    DetailedPrinter printer{os, prog};
×
124
    for (const BasicBlock& bb : BasicBlock::collect_basic_blocks(prog.cfg(), simplify)) {
×
125
        printer.print_jump("from", bb.first_label());
×
126
        os << bb.first_label() << ":\n";
×
127
        for (const Label& label : bb) {
×
128
            printer.print_line_info(label);
×
129
            printer.print_instruction(prog, label);
×
130
        }
131
        printer.print_jump("goto", bb.last_label());
×
132
    }
×
133
    os << "\n";
×
134
}
×
135

136
void print_invariants(std::ostream& os, const Program& prog, const bool simplify, const AnalysisResult& result) {
×
137
    DetailedPrinter printer{os, prog};
×
138
    for (const BasicBlock& bb : BasicBlock::collect_basic_blocks(prog.cfg(), simplify)) {
×
139
        if (result.invariants.at(bb.first_label()).pre.is_bottom()) {
×
140
            continue;
×
141
        }
142
        os << "\nPre-invariant : " << result.invariants.at(bb.first_label()).pre << "\n";
×
143
        printer.print_jump("from", bb.first_label());
×
144
        os << bb.first_label() << ":\n";
×
145
        Label last_label = bb.first_label();
×
146
        for (const Label& label : bb) {
×
147
            printer.print_line_info(label);
×
148
            printer.print_instruction(prog, label);
×
149
            last_label = label;
×
150

151
            const auto& current = result.invariants.at(last_label);
×
152
            if (current.error) {
×
153
                os << "\nVerification error:\n";
×
154
                if (label != bb.last_label()) {
×
155
                    os << "After " << current.pre << "\n";
×
156
                }
157
                print_error(os, *current.error);
×
158
                os << "\n";
×
159
                return;
×
160
            }
161
        }
162
        const auto& current = result.invariants.at(last_label);
×
163
        if (!current.post.is_bottom()) {
×
164
            printer.print_jump("goto", last_label);
×
165
            os << "\nPost-invariant : " << current.post << "\n";
×
166
        }
167
    }
×
168
    os << "\n";
×
169
}
×
170

171
void print_dot(const Program& prog, std::ostream& out) {
×
172
    out << "digraph program {\n";
×
173
    out << "    node [shape = rectangle];\n";
×
174
    for (const auto& label : prog.labels()) {
×
175
        out << "    \"" << label << "\"[xlabel=\"" << label << "\",label=\"";
×
176

177
        for (const auto& pre : prog.assertions_at(label)) {
×
178
            out << "assert " << pre << "\\l";
×
179
        }
×
180
        out << prog.instruction_at(label) << "\\l";
×
181

182
        out << "\"];\n";
×
183
        for (const Label& next : prog.cfg().children_of(label)) {
×
184
            out << "    \"" << label << "\" -> \"" << next << "\";\n";
×
185
        }
186
        out << "\n";
×
187
    }
188
    out << "}\n";
×
189
}
×
190

191
void print_dot(const Program& prog, const std::string& outfile) {
×
192
    std::ofstream out{outfile};
×
193
    if (out.fail()) {
×
194
        throw std::runtime_error(std::string("Could not open file ") + outfile);
×
195
    }
196
    print_dot(prog, out);
×
197
}
×
198

199
void print_unreachable(std::ostream& os, const Program& prog, const AnalysisResult& result) {
×
200
    for (const auto& [label, notes] : result.find_unreachable(prog)) {
×
201
        for (const auto& msg : notes) {
×
202
            os << label << ": " << msg << "\n";
×
203
        }
204
    }
×
205
    os << "\n";
×
206
}
×
207

208
std::string to_string(const VerificationError& error) {
314✔
209
    std::stringstream ss;
314✔
210
    if (const auto& label = error.where) {
314✔
211
        ss << *label << ": ";
314✔
212
    }
213
    ss << error.what();
314✔
214
    return ss.str();
628✔
215
}
314✔
216

217
void print_error(std::ostream& os, const VerificationError& error) {
2✔
218
    LineInfoPrinter printer{os};
2✔
219
    if (const auto& label = error.where) {
2✔
220
        printer.print_line_info(*label);
2✔
221
        os << *label << ": ";
2✔
222
    }
223
    os << error.what() << "\n";
2✔
224
    os << "\n";
2✔
225
}
2✔
226

227
std::ostream& operator<<(std::ostream& os, const ArgSingle::Kind kind) {
74✔
228
    switch (kind) {
74✔
229
    case ArgSingle::Kind::ANYTHING: return os << "uint64_t";
14✔
230
    case ArgSingle::Kind::PTR_TO_CTX: return os << "ctx";
4✔
231
    case ArgSingle::Kind::PTR_TO_STACK: return os << "stack";
×
232
    case ArgSingle::Kind::MAP_FD: return os << "map_fd";
24✔
233
    case ArgSingle::Kind::MAP_FD_PROGRAMS: return os << "map_fd_programs";
×
234
    case ArgSingle::Kind::PTR_TO_MAP_KEY: return os << "map_key";
24✔
235
    case ArgSingle::Kind::PTR_TO_MAP_VALUE: return os << "map_value";
8✔
236
    }
237
    assert(false);
238
    return os;
239
}
240

241
std::ostream& operator<<(std::ostream& os, const ArgPair::Kind kind) {
×
242
    switch (kind) {
×
243
    case ArgPair::Kind::PTR_TO_READABLE_MEM: return os << "mem";
×
244
    case ArgPair::Kind::PTR_TO_WRITABLE_MEM: return os << "out";
×
245
    }
246
    assert(false);
247
    return os;
248
}
249

250
std::ostream& operator<<(std::ostream& os, const ArgSingle arg) {
74✔
251
    os << arg.kind;
74✔
252
    if (arg.or_null) {
74✔
253
        os << "?";
×
254
    }
255
    os << " " << arg.reg;
74✔
256
    return os;
74✔
257
}
258

259
std::ostream& operator<<(std::ostream& os, const ArgPair arg) {
×
260
    os << arg.kind;
×
261
    if (arg.or_null) {
×
262
        os << "?";
×
263
    }
264
    os << " " << arg.mem << "[" << arg.size;
×
265
    if (arg.can_be_zero) {
×
266
        os << "?";
×
267
    }
268
    os << "], uint64_t " << arg.size;
×
269
    return os;
×
270
}
271

272
std::ostream& operator<<(std::ostream& os, const Bin::Op op) {
280✔
273
    using Op = Bin::Op;
140✔
274
    switch (op) {
280✔
275
    case Op::MOV: return os;
81✔
276
    case Op::MOVSX8: return os << "s8";
×
277
    case Op::MOVSX16: return os << "s16";
×
278
    case Op::MOVSX32: return os << "s32";
×
279
    case Op::ADD: return os << "+";
72✔
280
    case Op::SUB: return os << "-";
×
281
    case Op::MUL: return os << "*";
×
282
    case Op::UDIV: return os << "/";
×
283
    case Op::SDIV: return os << "s/";
×
284
    case Op::UMOD: return os << "%";
×
285
    case Op::SMOD: return os << "s%";
×
286
    case Op::OR: return os << "|";
×
287
    case Op::AND: return os << "&";
30✔
288
    case Op::LSH: return os << "<<";
12✔
289
    case Op::RSH: return os << ">>";
×
290
    case Op::ARSH: return os << ">>>";
2✔
291
    case Op::XOR: return os << "^";
2✔
292
    }
293
    assert(false);
294
    return os;
295
}
296

297
std::ostream& operator<<(std::ostream& os, const Condition::Op op) {
222✔
298
    using Op = Condition::Op;
111✔
299
    switch (op) {
222✔
300
    case Op::EQ: return os << "==";
38✔
301
    case Op::NE: return os << "!=";
10✔
302
    case Op::SET: return os << "&==";
×
303
    case Op::NSET: return os << "&!="; // not in ebpf
×
304
    case Op::LT: return os << "<";     // TODO: os << "u<";
124✔
305
    case Op::LE: return os << "<=";    // TODO: os << "u<=";
12✔
306
    case Op::GT: return os << ">";     // TODO: os << "u>";
22✔
307
    case Op::GE: return os << ">=";    // TODO: os << "u>=";
6✔
308
    case Op::SLT: return os << "s<";
4✔
309
    case Op::SLE: return os << "s<=";
×
310
    case Op::SGT: return os << "s>";
4✔
311
    case Op::SGE: return os << "s>=";
2✔
312
    }
313
    assert(false);
314
    return os;
315
}
316

317
static string size(const int w) { return string("u") + std::to_string(w * 8); }
460✔
318

319
// ReSharper disable CppMemberFunctionMayBeConst
320
struct AssertionPrinterVisitor {
321
    std::ostream& _os;
322

323
    void operator()(ValidStore const& a) {
2✔
324
        _os << a.mem << ".type != stack -> " << TypeConstraint{a.val, TypeGroup::number};
2✔
325
    }
2✔
326

327
    void operator()(ValidAccess const& a) {
318✔
328
        if (a.or_null) {
318✔
329
            _os << "(" << TypeConstraint{a.reg, TypeGroup::number} << " and " << a.reg << ".value == 0) or ";
4✔
330
        }
331
        _os << "valid_access(" << a.reg << ".offset";
318✔
332
        if (a.offset > 0) {
318✔
333
            _os << "+" << a.offset;
46✔
334
        } else if (a.offset < 0) {
272✔
335
            _os << a.offset;
6✔
336
        }
337

338
        if (a.width == Value{Imm{0}}) {
318✔
339
            // a.width == 0, meaning we only care it's an in-bound pointer,
340
            // so it can be compared with another pointer to the same region.
341
            _os << ") for comparison/subtraction";
4✔
342
        } else {
343
            _os << ", width=" << a.width << ") for ";
314✔
344
            if (a.access_type == AccessType::read) {
314✔
345
                _os << "read";
278✔
346
            } else {
347
                _os << "write";
36✔
348
            }
349
        }
350
    }
318✔
351

352
    void operator()(const BoundedLoopCount& a) {
20✔
353
        _os << variable_registry->loop_counter(to_string(a.name)) << " < " << a.limit;
20✔
354
    }
20✔
355

356
    void operator()(ValidSize const& a) {
2✔
357
        const auto op = a.can_be_zero ? " >= " : " > ";
2✔
358
        _os << a.reg << ".value" << op << 0;
2✔
359
    }
2✔
360

361
    void operator()(ValidCall const& a) {
2✔
362
        const EbpfHelperPrototype proto = thread_local_program_info->platform->get_helper_prototype(a.func);
2✔
363
        _os << "valid call(" << proto.name << ")";
2✔
364
    }
2✔
365

366
    void operator()(ValidMapKeyValue const& a) {
42✔
367
        _os << "within(" << a.access_reg << ":" << (a.key ? "key_size" : "value_size") << "(" << a.map_fd_reg << "))";
51✔
368
    }
42✔
369

370
    void operator()(ZeroCtxOffset const& a) {
2✔
371
        _os << variable_registry->reg(DataKind::ctx_offsets, a.reg.v) << " == 0";
2✔
372
    }
2✔
373

374
    void operator()(Comparable const& a) {
10✔
375
        if (a.or_r2_is_number) {
10✔
376
            _os << TypeConstraint{a.r2, TypeGroup::number} << " or ";
15✔
377
        }
378
        _os << variable_registry->type_reg(a.r1.v) << " == " << variable_registry->type_reg(a.r2.v) << " in "
10✔
379
            << TypeGroup::singleton_ptr;
10✔
380
    }
10✔
381

382
    void operator()(Addable const& a) {
2✔
383
        _os << TypeConstraint{a.ptr, TypeGroup::pointer} << " -> " << TypeConstraint{a.num, TypeGroup::number};
4✔
384
    }
2✔
385

386
    void operator()(ValidDivisor const& a) { _os << a.reg << " != 0"; }
76✔
387

388
    void operator()(TypeConstraint const& tc) {
220✔
389
        const string cmp_op = is_singleton_type(tc.types) ? "==" : "in";
239✔
390
        _os << variable_registry->type_reg(tc.reg.v) << " " << cmp_op << " " << tc.types;
220✔
391
    }
220✔
392

393
    void operator()(FuncConstraint const& fc) { _os << variable_registry->type_reg(fc.reg.v) << " is helper"; }
6✔
394
};
395

396
// ReSharper disable CppMemberFunctionMayBeConst
397
struct CommandPrinterVisitor {
398
    std::ostream& os_;
399

400
    void visit(const auto& item) { std::visit(*this, item); }
401

402
    void operator()(Undefined const& a) { os_ << "Undefined{" << a.opcode << "}"; }
×
403

404
    void operator()(LoadMapFd const& b) { os_ << b.dst << " = map_fd " << b.mapfd; }
26✔
405

406
    void operator()(LoadMapAddress const& b) { os_ << b.dst << " = map_val(" << b.mapfd << ") + " << b.offset; }
×
407

408
    // llvm-objdump uses "w<number>" for 32-bit operations and "r<number>" for 64-bit operations.
409
    // We use the same convention here for consistency.
410
    static std::string reg_name(Reg const& a, const bool is64) { return ((is64) ? "r" : "w") + std::to_string(a.v); }
420✔
411

412
    void operator()(Bin const& b) {
280✔
413
        os_ << reg_name(b.dst, b.is64) << " " << b.op << "= " << b.v;
420✔
414
        if (b.lddw) {
280✔
415
            os_ << " ll";
2✔
416
        }
417
    }
280✔
418

419
    void operator()(Un const& b) {
12✔
420
        os_ << b.dst << " = ";
12✔
421
        switch (b.op) {
12✔
422
        case Un::Op::BE16: os_ << "be16 "; break;
2✔
423
        case Un::Op::BE32: os_ << "be32 "; break;
2✔
424
        case Un::Op::BE64: os_ << "be64 "; break;
2✔
425
        case Un::Op::LE16: os_ << "le16 "; break;
2✔
426
        case Un::Op::LE32: os_ << "le32 "; break;
2✔
427
        case Un::Op::LE64: os_ << "le64 "; break;
2✔
428
        case Un::Op::SWAP16: os_ << "swap16 "; break;
×
429
        case Un::Op::SWAP32: os_ << "swap32 "; break;
×
430
        case Un::Op::SWAP64: os_ << "swap64 "; break;
×
431
        case Un::Op::NEG: os_ << "-"; break;
×
432
        }
433
        os_ << b.dst;
12✔
434
    }
12✔
435

436
    void operator()(Call const& call) {
70✔
437
        os_ << "r0 = " << call.name << ":" << call.func << "(";
70✔
438
        for (uint8_t r = 1; r <= 5; r++) {
144✔
439
            // Look for a singleton.
440
            auto single = std::ranges::find_if(call.singles, [r](const ArgSingle arg) { return arg.reg.v == r; });
372✔
441
            if (single != call.singles.end()) {
144✔
442
                if (r > 1) {
74✔
443
                    os_ << ", ";
48✔
444
                }
445
                os_ << *single;
74✔
446
                continue;
74✔
447
            }
448

449
            // Look for the start of a pair.
450
            auto pair = std::ranges::find_if(call.pairs, [r](const ArgPair arg) { return arg.mem.v == r; });
70✔
451
            if (pair != call.pairs.end()) {
70✔
452
                if (r > 1) {
×
453
                    os_ << ", ";
×
454
                }
455
                os_ << *pair;
×
456
                r++;
×
457
                continue;
×
458
            }
459

460
            // Not found.
461
            break;
70✔
462
        }
463
        os_ << ")";
70✔
464
    }
70✔
465

466
    void operator()(CallLocal const& call) { os_ << "call <" << to_string(call.target) << ">"; }
×
467

468
    void operator()(Callx const& callx) { os_ << "callx " << callx.func; }
×
469

470
    void operator()(Exit const& b) { os_ << "exit"; }
50✔
471

472
    void operator()(Jmp const& b) {
×
473
        // A "standalone" jump Instruction.
474
        // Print the label without offset calculations.
475
        if (b.cond) {
×
476
            os_ << "if ";
×
477
            print(*b.cond);
×
478
            os_ << " ";
×
479
        }
480
        os_ << "goto label <" << to_string(b.target) << ">";
×
481
    }
×
482

483
    void operator()(Jmp const& b, const int offset) {
54✔
484
        const string sign = offset > 0 ? "+" : "";
54✔
485
        const string target = sign + std::to_string(offset) + " <" + to_string(b.target) + ">";
108✔
486

487
        if (b.cond) {
54✔
488
            os_ << "if ";
40✔
489
            print(*b.cond);
40✔
490
            os_ << " ";
40✔
491
        }
492
        os_ << "goto " << target;
54✔
493
    }
54✔
494

495
    void operator()(Packet const& b) {
×
496
        /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
497
        /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
498
        const string s = size(b.width);
×
499
        os_ << "r0 = ";
×
500
        os_ << "*(" << s << " *)skb[";
×
501
        if (b.regoffset) {
×
502
            os_ << *b.regoffset;
×
503
        }
504
        if (b.offset != 0) {
×
505
            if (b.regoffset) {
×
506
                os_ << " + ";
×
507
            }
508
            os_ << b.offset;
×
509
        }
510
        os_ << "]";
×
511
    }
×
512

513
    void print(Deref const& access) {
184✔
514
        const string sign = access.offset < 0 ? " - " : " + ";
212✔
515
        const int offset = std::abs(access.offset); // what about INT_MIN?
184✔
516
        os_ << "*(" << size(access.width) << " *)";
276✔
517
        os_ << "(" << access.basereg << sign << offset << ")";
184✔
518
    }
184✔
519

520
    void print(Condition const& cond) {
222✔
521
        os_ << cond.left << " " << ((!cond.is64) ? "w" : "") << cond.op << " " << cond.right;
261✔
522
    }
222✔
523

524
    void operator()(Mem const& b) {
184✔
525
        if (b.is_load) {
184✔
526
            os_ << b.value << " = ";
44✔
527
        }
528
        print(b.access);
184✔
529
        if (!b.is_load) {
184✔
530
            os_ << " = " << b.value;
140✔
531
        }
532
    }
184✔
533

534
    void operator()(Atomic const& b) {
×
535
        os_ << "lock ";
×
536
        print(b.access);
×
537
        os_ << " ";
×
538
        bool showfetch = true;
×
539
        switch (b.op) {
×
540
        case Atomic::Op::ADD: os_ << "+"; break;
×
541
        case Atomic::Op::OR: os_ << "|"; break;
×
542
        case Atomic::Op::AND: os_ << "&"; break;
×
543
        case Atomic::Op::XOR: os_ << "^"; break;
×
544
        case Atomic::Op::XCHG:
×
545
            os_ << "x";
×
546
            showfetch = false;
×
547
            break;
×
548
        case Atomic::Op::CMPXCHG:
×
549
            os_ << "cx";
×
550
            showfetch = false;
×
551
            break;
×
552
        }
553
        os_ << "= " << b.valreg;
×
554

555
        if (showfetch && b.fetch) {
×
556
            os_ << " fetch";
×
557
        }
558
    }
×
559

560
    void operator()(Assume const& b) {
182✔
561
        os_ << "assume ";
182✔
562
        print(b.cond);
182✔
563
    }
182✔
564

565
    void operator()(IncrementLoopCounter const& a) {
×
566
        os_ << variable_registry->loop_counter(to_string(a.name)) << "++";
×
567
    }
×
568
};
569
// ReSharper restore CppMemberFunctionMayBeConst
570

571
std::ostream& operator<<(std::ostream& os, Instruction const& ins) {
188✔
572
    std::visit(CommandPrinterVisitor{os}, ins);
94✔
573
    return os;
94✔
574
}
575

576
string to_string(Instruction const& ins) {
184✔
577
    std::stringstream str;
184✔
578
    str << ins;
184✔
579
    return str.str();
368✔
580
}
184✔
581

582
std::ostream& operator<<(std::ostream& os, const Assertion& a) {
702✔
583
    std::visit(AssertionPrinterVisitor{os}, a);
360✔
584
    return os;
360✔
585
}
586

587
string to_string(Assertion const& constraint) {
682✔
588
    std::stringstream str;
682✔
589
    str << constraint;
682✔
590
    return str.str();
1,364✔
591
}
682✔
592

593
auto get_labels(const InstructionSeq& insts) {
38✔
594
    Pc pc = 0;
38✔
595
    std::map<Label, Pc> pc_of_label;
38✔
596
    for (const auto& [label, inst, _] : insts) {
708✔
597
        pc_of_label[label] = pc;
670✔
598
        pc += size(inst);
670✔
599
    }
600
    return pc_of_label;
38✔
601
}
×
602

603
void print(const InstructionSeq& insts, std::ostream& out, const std::optional<const Label>& label_to_print,
38✔
604
           const bool print_line_info) {
605
    const auto pc_of_label = get_labels(insts);
38✔
606
    Pc pc = 0;
38✔
607
    std::string previous_source;
38✔
608
    CommandPrinterVisitor visitor{out};
38✔
609
    for (const LabeledInstruction& labeled_inst : insts) {
708✔
610
        const auto& [label, ins, line_info] = labeled_inst;
670✔
611
        if (!label_to_print.has_value() || label == label_to_print) {
670✔
612
            if (line_info.has_value() && print_line_info) {
670✔
613
                auto& [file, source, line, column] = line_info.value();
×
614
                // Only decorate the first instruction associated with a source line.
615
                if (source != previous_source) {
×
616
                    out << line_info.value();
×
617
                    previous_source = source;
×
618
                }
619
            }
620
            if (label.isjump()) {
670✔
621
                out << "\n";
×
622
                out << label << ":\n";
×
623
            }
624
            if (label_to_print.has_value()) {
670✔
625
                out << pc << ": ";
×
626
            } else {
627
                out << std::setw(8) << pc << ":\t";
670✔
628
            }
629
            if (const auto jmp = std::get_if<Jmp>(&ins)) {
670✔
630
                if (!pc_of_label.contains(jmp->target)) {
54✔
631
                    throw std::runtime_error(string("Cannot find label ") + to_string(jmp->target));
×
632
                }
633
                const Pc target_pc = pc_of_label.at(jmp->target);
54✔
634
                visitor(*jmp, target_pc - static_cast<int>(pc) - 1);
54✔
635
            } else {
636
                std::visit(visitor, ins);
616✔
637
            }
638
            out << "\n";
670✔
639
        }
640
        pc += size(ins);
670✔
641
    }
642
}
38✔
643

644
std::ostream& operator<<(std::ostream& o, const EbpfMapDescriptor& desc) {
28✔
645
    return o << "(" << "original_fd = " << desc.original_fd << ", " << "inner_map_fd = " << desc.inner_map_fd << ", "
28✔
646
             << "type = " << desc.type << ", " << "max_entries = " << desc.max_entries << ", "
28✔
647
             << "value_size = " << desc.value_size << ", " << "key_size = " << desc.key_size << ")";
28✔
648
}
649

650
void print_map_descriptors(const std::vector<EbpfMapDescriptor>& descriptors, std::ostream& o) {
38✔
651
    int i = 0;
38✔
652
    for (const auto& desc : descriptors) {
66✔
653
        o << "map " << i << ":" << desc << "\n";
28✔
654
        i++;
28✔
655
    }
656
}
38✔
657

658
std::ostream& operator<<(std::ostream& os, const btf_line_info_t& line_info) {
×
659
    os << "; " << line_info.file_name << ":" << line_info.line_number << "\n";
×
660
    os << "; " << line_info.source_line << "\n";
×
661
    return os;
×
662
}
663

664
void print_invariants_filtered(std::ostream& os, const Program& prog, const bool simplify, const AnalysisResult& result,
2✔
665
                               const std::set<Label>& filter, const bool compact,
666
                               const std::map<Label, RelevantState>* relevance) {
667
    DetailedPrinter printer{os, prog};
2✔
668
    const auto basic_blocks = BasicBlock::collect_basic_blocks(prog.cfg(), simplify);
2✔
669

670
    // Build a mapping from each label in a basic block to the block's first label.
671
    // Needed to look up post-invariants for mid-block predecessor labels at join points.
672
    std::map<Label, Label> label_to_block_leader;
2✔
673
    for (const BasicBlock& bb : basic_blocks) {
28✔
674
        for (const Label& label : bb) {
52✔
675
            label_to_block_leader.insert({label, bb.first_label()});
52✔
676
        }
677
    }
678

679
    // Helper to look up the post-invariant for a predecessor label.
680
    // Mid-block labels don't have direct invariant entries, so we map
681
    // through the block leader to find the containing block's post-state.
682
    // Note: when simplify=true, the block leader's post represents the
683
    // entire collapsed block, which is correct for the last instruction
684
    // but approximate for mid-block predecessors. Failure slicing defaults
685
    // to simplify=false, so this approximation is rarely triggered.
686
    auto get_parent_post_invariant = [&](const Label& parent) -> const EbpfDomain* {
1✔
NEW
687
        const auto leader_it = label_to_block_leader.find(parent);
×
NEW
688
        const Label& lookup_label = (leader_it != label_to_block_leader.end()) ? leader_it->second : parent;
×
NEW
689
        const auto inv_it = result.invariants.find(lookup_label);
×
NEW
690
        if (inv_it != result.invariants.end() && !inv_it->second.post.is_bottom()) {
×
691
            return &inv_it->second.post;
692
        }
693
        return nullptr;
694
    };
2✔
695

696
    for (const BasicBlock& bb : basic_blocks) {
28✔
697
        // Check if any label in this basic block is in the filter
698
        bool bb_has_filtered_label = false;
26✔
699
        for (const Label& label : bb) {
48✔
700
            if (filter.contains(label)) {
26✔
701
                bb_has_filtered_label = true;
2✔
702
                break;
2✔
703
            }
704
        }
705
        if (!bb_has_filtered_label) {
26✔
706
            continue;
22✔
707
        }
708

709
        // Find the first filtered label in this block to use as the block header
710
        Label first_filtered_label = bb.first_label();
4✔
711
        for (const Label& label : bb) {
4✔
712
            if (filter.contains(label)) {
4✔
713
                first_filtered_label = label;
4✔
714
                break;
2✔
715
            }
716
        }
717

718
        // Use bb.first_label() for reachability check: if the block's entry is unreachable,
719
        // skip the entire block. The filtered label's pre-invariant is printed below.
720
        if (result.invariants.at(bb.first_label()).pre.is_bottom()) {
6✔
NEW
721
            continue;
×
722
        }
723

724
        // Print pre-invariant for first filtered label in block (unless compact)
725
        if (!compact) {
4✔
726
            // Set invariant filter if we have relevance info for this label
727
            const auto* label_relevance =
2✔
728
                relevance ? (relevance->contains(first_filtered_label) ? &relevance->at(first_filtered_label) : nullptr)
4✔
729
                          : nullptr;
4✔
730
            os << invariant_filter(label_relevance);
4✔
731
            os << "\nPre-invariant : " << result.invariants.at(first_filtered_label).pre << "\n";
4✔
732
            os << invariant_filter(nullptr); // Clear filter
4✔
733
        }
734

735
        // Print the jump and block header anchored to the basic block entry label
736
        // for correct CFG structure representation.
737
        printer.print_jump("from", bb.first_label());
10✔
738
        os << bb.first_label() << ":\n";
4✔
739

740
        // R3: Show per-predecessor invariants at join points.
741
        // When multiple predecessors exist and at least 2 are in the slice,
742
        // show what each incoming edge contributed to help diagnose lost correlations.
743
        if (!compact && relevance) {
4✔
744
            const auto parents = prog.cfg().parents_of(bb.first_label());
4✔
745
            std::vector<Label> in_slice_parents;
4✔
746
            for (const auto& parent : parents) {
8✔
747
                if (filter.contains(parent)) {
4✔
748
                    in_slice_parents.push_back(parent);
2✔
749
                }
750
            }
751
            if (in_slice_parents.size() >= 2) {
4✔
752
                // Build the union of relevant registers from this label and all in-slice parents
NEW
753
                RelevantState join_relevance;
×
NEW
754
                if (relevance->contains(first_filtered_label)) {
×
NEW
755
                    const auto& fl = relevance->at(first_filtered_label);
×
NEW
756
                    join_relevance.registers.insert(fl.registers.begin(), fl.registers.end());
×
NEW
757
                    join_relevance.stack_offsets.insert(fl.stack_offsets.begin(), fl.stack_offsets.end());
×
758
                }
NEW
759
                for (const auto& parent : in_slice_parents) {
×
NEW
760
                    if (relevance->contains(parent)) {
×
NEW
761
                        const auto& pr = relevance->at(parent);
×
NEW
762
                        join_relevance.registers.insert(pr.registers.begin(), pr.registers.end());
×
NEW
763
                        join_relevance.stack_offsets.insert(pr.stack_offsets.begin(), pr.stack_offsets.end());
×
764
                    }
765
                }
766

NEW
767
                os << "  --- join point: per-predecessor state ---\n";
×
NEW
768
                for (const auto& parent : in_slice_parents) {
×
NEW
769
                    const auto* post = get_parent_post_invariant(parent);
×
NEW
770
                    if (post) {
×
NEW
771
                        os << invariant_filter(&join_relevance);
×
NEW
772
                        os << "  from " << parent << ": " << *post << "\n";
×
NEW
773
                        os << invariant_filter(nullptr);
×
774
                    }
775
                }
NEW
776
                os << "  --- end join point ---\n";
×
NEW
777
            }
×
778
        }
4✔
779

780
        if (first_filtered_label != bb.first_label()) {
6✔
781
            // Indicate that some labels/instructions were skipped due to filtering.
NEW
782
            os << "  ... skipped ...\n";
×
783
        }
784

785
        Label last_label = bb.first_label();
4✔
786
        Label prev_filtered_label = bb.first_label();
4✔
787
        bool has_prev_filtered = false;
4✔
788
        for (const Label& label : bb) {
8✔
789
            if (!filter.contains(label)) {
4✔
NEW
790
                continue;
×
791
            }
792

793
            // If there was a gap since the previous filtered label in this block,
794
            // close the previous label's output and show a skip indicator.
795
            if (has_prev_filtered && prev_filtered_label != label) {
4✔
796
                // Print post-invariant and goto for the previous filtered label
NEW
797
                if (!compact) {
×
NEW
798
                    const auto& prev_current = result.invariants.at(prev_filtered_label);
×
NEW
799
                    if (!prev_current.post.is_bottom()) {
×
800
                        const auto* prev_label_relevance =
NEW
801
                            relevance ? (relevance->contains(prev_filtered_label) ? &relevance->at(prev_filtered_label)
×
802
                                                                                  : nullptr)
NEW
803
                                      : nullptr;
×
NEW
804
                        os << invariant_filter(prev_label_relevance);
×
NEW
805
                        printer.print_jump("goto", prev_filtered_label);
×
NEW
806
                        os << "\nPost-invariant : " << prev_current.post << "\n";
×
NEW
807
                        os << invariant_filter(nullptr);
×
808
                    }
809
                }
810
                // Check if there are skipped labels between prev and current
NEW
811
                bool has_gap = false;
×
NEW
812
                for (const Label& mid : bb) {
×
NEW
813
                    if (mid <= prev_filtered_label) {
×
NEW
814
                        continue;
×
815
                    }
NEW
816
                    if (mid >= label) {
×
817
                        break;
818
                    }
NEW
819
                    has_gap = true;
×
NEW
820
                    break;
×
821
                }
NEW
822
                if (has_gap) {
×
NEW
823
                    os << "  ... skipped ...\n";
×
824
                }
825
                // Print pre-invariant for this label
NEW
826
                if (!compact) {
×
827
                    const auto* label_rel =
NEW
828
                        relevance ? (relevance->contains(label) ? &relevance->at(label) : nullptr) : nullptr;
×
NEW
829
                    os << invariant_filter(label_rel);
×
NEW
830
                    os << "\nPre-invariant : " << result.invariants.at(label).pre << "\n";
×
NEW
831
                    os << invariant_filter(nullptr);
×
NEW
832
                    printer.print_jump("from", label);
×
833
                }
834
            }
835

836
            printer.print_line_info(label);
4✔
837

838
            // Print assertions, filtered by relevance if provided
839
            const auto* label_relevance =
2✔
840
                relevance ? (relevance->contains(label) ? &relevance->at(label) : nullptr) : nullptr;
4✔
841
            for (const auto& assertion : prog.assertions_at(label)) {
6✔
842
                // If we have relevance info, only print assertions involving relevant registers.
843
                // Assertions with no register deps (e.g., ValidCall, BoundedLoopCount) are always
844
                // printed to avoid hiding the failing assertion from the slice output.
845
                if (label_relevance) {
2✔
846
                    auto assertion_regs = extract_assertion_registers(assertion);
2✔
847
                    if (!assertion_regs.empty()) {
2✔
848
                        bool is_relevant = false;
2✔
849
                        for (const auto& reg : assertion_regs) {
2✔
850
                            if (label_relevance->registers.contains(reg)) {
2✔
851
                                is_relevant = true;
1✔
852
                                break;
1✔
853
                            }
854
                        }
855
                        if (!is_relevant) {
2✔
NEW
856
                            continue; // Skip this assertion
×
857
                        }
858
                    }
859
                }
2✔
860
                os << "  assert " << assertion << ";\n";
2✔
861
            }
4✔
862
            os << "  " << prog.instruction_at(label) << ";\n";
4✔
863

864
            last_label = label;
4✔
865
            prev_filtered_label = label;
4✔
866
            has_prev_filtered = true;
4✔
867

868
            const auto& current = result.invariants.at(label);
4✔
869
            if (current.error) {
4✔
870
                os << "\nVerification error:\n";
2✔
871
                print_error(os, *current.error);
2✔
872
                os << "\n";
2✔
873
            }
874
        }
875

876
        // Print post-invariant (unless compact)
877
        if (!compact) {
4✔
878
            const auto& current = result.invariants.at(last_label);
4✔
879
            if (!current.post.is_bottom()) {
4✔
880
                // Set invariant filter for post-invariant
881
                const auto* label_relevance =
1✔
882
                    relevance ? (relevance->contains(last_label) ? &relevance->at(last_label) : nullptr) : nullptr;
2✔
883
                os << invariant_filter(label_relevance);
2✔
884
                printer.print_jump("goto", last_label);
2✔
885
                os << "\nPost-invariant : " << current.post << "\n";
2✔
886
                os << invariant_filter(nullptr); // Clear filter
2✔
887
            }
888
        }
889
    }
8✔
890
    os << "\n";
2✔
891
}
2✔
892

893
void print_failure_slices(std::ostream& os, const Program& prog, const bool simplify, const AnalysisResult& result,
2✔
894
                          const std::vector<FailureSlice>& slices, const bool compact) {
895
    if (slices.empty()) {
2✔
NEW
896
        os << "No verification failures found.\n";
×
NEW
897
        return;
×
898
    }
899

900
    for (size_t i = 0; i < slices.size(); ++i) {
4✔
901
        const auto& slice = slices[i];
2✔
902

903
        os << "=== Failure Slice " << (i + 1) << " of " << slices.size() << " ===\n\n";
2✔
904

905
        // Print error summary
906
        os << "[ERROR] " << slice.error.what() << "\n";
2✔
907
        os << "[LOCATION] " << slice.failing_label << "\n";
2✔
908

909
        // Print relevant registers at failure point
910
        const auto it = slice.relevance.find(slice.failing_label);
2✔
911
        if (it != slice.relevance.end()) {
2✔
912
            os << "[RELEVANT REGISTERS] ";
2✔
913
            bool first = true;
2✔
914
            for (const auto& reg : it->second.registers) {
4✔
915
                if (!first) {
2✔
NEW
916
                    os << ", ";
×
917
                }
918
                os << "r" << static_cast<int>(reg.v);
2✔
919
                first = false;
2✔
920
            }
921
            if (!it->second.stack_offsets.empty()) {
2✔
NEW
922
                for (const auto& offset : it->second.stack_offsets) {
×
NEW
923
                    if (!first) {
×
NEW
924
                        os << ", ";
×
925
                    }
NEW
926
                    os << "stack[" << offset << "]";
×
NEW
927
                    first = false;
×
928
                }
929
            }
930
            os << "\n";
2✔
931
        }
932

933
        os << "[SLICE SIZE] " << slice.relevance.size() << " instructions\n\n";
2✔
934

935
        // Print a compact control-flow summary showing the branch-path skeleton
936
        // through the slice. Lists labels in order with Assume/Jmp annotations.
937
        // At join points (labels with ≥2 in-slice predecessors), the converging
938
        // predecessors are grouped as {pred1 | pred2} → join_label.
939
        {
1✔
940
            os << "[CONTROL FLOW] ";
2✔
941
            // Collect and sort impacted labels
942
            auto labels = slice.impacted_labels();
2✔
943

944
            // Build a map: join_label → set of in-slice predecessors
945
            // Also collect which labels are convergence predecessors (to skip them in linear output)
946
            std::map<Label, std::vector<Label>> join_predecessors;
2✔
947
            for (const auto& lbl : labels) {
6✔
948
                const auto& parents = prog.cfg().parents_of(lbl);
4✔
949
                std::vector<Label> in_slice_parents;
4✔
950
                for (const auto& p : parents) {
8✔
951
                    if (labels.contains(p)) {
4✔
952
                        in_slice_parents.push_back(p);
2✔
953
                    }
954
                }
955
                if (in_slice_parents.size() >= 2) {
4✔
NEW
956
                    join_predecessors[lbl] = in_slice_parents;
×
957
                }
958
            }
4✔
959
            // Labels consumed by a {..|..} group are skipped in linear output,
960
            // unless they are themselves join points (nested joins).
961
            std::set<Label> convergence_members;
2✔
962
            for (const auto& [join_lbl, preds] : join_predecessors) {
2✔
NEW
963
                for (const auto& p : preds) {
×
NEW
964
                    if (!join_predecessors.contains(p)) {
×
NEW
965
                        convergence_members.insert(p);
×
966
                    }
967
                }
968
            }
969

970
            // Helper to annotate a label with its instruction type
971
            auto annotate_label = [&](const Label& lbl) {
5✔
972
                os << lbl;
4✔
973
                const auto& ins = prog.instruction_at(lbl);
4✔
974
                if (const auto* assume = std::get_if<Assume>(&ins)) {
4✔
NEW
975
                    os << " (assume " << assume->cond.left << " " << assume->cond.op << " " << assume->cond.right
×
NEW
976
                       << ")";
×
977
                } else if (const auto* jmp = std::get_if<Jmp>(&ins)) {
4✔
NEW
978
                    if (jmp->cond) {
×
NEW
979
                        os << " (if " << jmp->cond->left << " " << jmp->cond->op << " " << jmp->cond->right << ")";
×
980
                    }
981
                }
982
            };
6✔
983

984
            bool first_cf = true;
2✔
985
            for (const auto& lbl : labels) {
6✔
986
                // Skip labels that are part of a convergence group (printed with their join)
987
                if (convergence_members.contains(lbl)) {
4✔
NEW
988
                    continue;
×
989
                }
990

991
                if (!first_cf) {
4✔
992
                    os << ", ";
2✔
993
                }
994
                first_cf = false;
4✔
995

996
                // If this label is a join point, print {pred1 | pred2} → lbl
997
                if (join_predecessors.contains(lbl)) {
4✔
NEW
998
                    os << "{";
×
NEW
999
                    bool first_pred = true;
×
NEW
1000
                    for (const auto& pred : join_predecessors.at(lbl)) {
×
NEW
1001
                        if (!first_pred) {
×
NEW
1002
                            os << " | ";
×
1003
                        }
NEW
1004
                        first_pred = false;
×
NEW
1005
                        annotate_label(pred);
×
1006
                    }
NEW
1007
                    os << "} -> ";
×
1008
                }
1009

1010
                annotate_label(lbl);
4✔
1011
            }
1012
            if (labels.contains(slice.failing_label)) {
2✔
1013
                os << " FAIL";
2✔
1014
            }
1015
            os << "\n\n";
2✔
1016
        }
2✔
1017

1018
        // Print the filtered CFG with assertion filtering based on relevance
1019
        os << "[CAUSAL TRACE]\n";
2✔
1020
        print_invariants_filtered(os, prog, simplify, result, slice.impacted_labels(), compact, &slice.relevance);
2✔
1021

1022
        if (i + 1 < slices.size()) {
2✔
NEW
1023
            os << "\n";
×
1024
        }
1025
    }
1026
}
1027

1028
} // namespace prevail
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc