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

Alan-Jowett / ebpf-verifier / 15194704016

22 May 2025 08:53AM UTC coverage: 88.11% (-0.07%) from 88.177%
15194704016

push

github

elazarg
uniform class names and explicit constructors for adapt_sgraph.hpp

Signed-off-by: Elazar Gershuni <elazarg@gmail.com>

27 of 30 new or added lines in 1 file covered. (90.0%)

481 existing lines in 33 files now uncovered.

8552 of 9706 relevant lines covered (88.11%)

9089054.61 hits per line

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

53.32
/src/asm_ostream.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 "asm_syntax.hpp"
10
#include "cfg/cfg.hpp"
11
#include "crab/fwd_analyzer.hpp"
12
#include "crab/interval.hpp"
13
#include "crab/type_encoding.hpp"
14
#include "crab/variable.hpp"
15
#include "crab_utils/num_big.hpp"
16
#include "crab_verifier.hpp"
17
#include "helpers.hpp"
18
#include "platform.hpp"
19
#include "spec_type_descriptors.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) {
846✔
28
    if (interval.is_bottom()) {
846✔
UNCOV
29
        o << "_|_";
×
30
    } else {
31
        o << "[" << interval._lb << ", " << interval._ub << "]";
846✔
32
    }
33
    return o;
846✔
34
}
35
std::ostream& operator<<(std::ostream& o, const Number& z) { return o << z._n.str(); }
350,368✔
36

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

UNCOV
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,598✔
46
    if (label == Label::entry) {
1,598✔
47
        return os << "entry";
6✔
48
    }
49
    if (label == Label::exit) {
1,592✔
50
        return os << "exit";
6✔
51
    }
52
    if (!label.stack_frame_prefix.empty()) {
1,586✔
53
        os << label.stack_frame_prefix << STACK_FRAME_DELIMITER;
330✔
54
    }
55
    os << label.from;
1,586✔
56
    if (label.to != -1) {
1,586✔
57
        os << ":" << label.to;
50✔
58
    }
59
    if (!label.special_label.empty()) {
1,586✔
60
        os << " (" << label.special_label << ")";
20✔
61
    }
62
    return os;
793✔
63
}
64

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

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

UNCOV
75
    void print_line_info(const Label& label) {
×
UNCOV
76
        if (thread_local_options.verbosity_opts.print_line_info) {
×
UNCOV
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";
×
UNCOV
82
                previous_source_line = line_info->second.source_line;
×
83
            }
84
        }
85
    }
×
86
};
87

88
void print_jump(std::ostream& o, const std::string& direction, const std::set<Label>& labels) {
×
UNCOV
89
    auto [it, et] = std::pair{labels.begin(), labels.end()};
×
UNCOV
90
    if (it != et) {
×
91
        o << "  " << direction << " ";
×
92
        while (it != et) {
×
93
            o << *it;
×
94
            ++it;
×
95
            if (it == et) {
×
96
                o << ";";
×
97
            } else {
98
                o << ",";
×
99
            }
100
        }
101
    }
UNCOV
102
    o << "\n";
×
UNCOV
103
}
×
104

105
void print_program(const Program& prog, std::ostream& os, const bool simplify, const printfunc& prefunc,
×
106
                   const printfunc& postfunc) {
UNCOV
107
    LineInfoPrinter printer{os};
×
108
    for (const BasicBlock& bb : BasicBlock::collect_basic_blocks(prog.cfg(), simplify)) {
×
UNCOV
109
        prefunc(os, bb.first_label());
×
110
        print_jump(os, "from", prog.cfg().parents_of(bb.first_label()));
×
111
        os << bb.first_label() << ":\n";
×
112
        for (const Label& label : bb) {
×
113
            printer.print_line_info(label);
×
114
            for (const auto& pre : prog.assertions_at(label)) {
×
115
                os << "  " << "assert " << pre << ";\n";
×
116
            }
×
117
            os << "  " << prog.instruction_at(label) << ";\n";
×
118
        }
119
        print_jump(os, "goto", prog.cfg().children_of(bb.last_label()));
×
120
        postfunc(os, bb.last_label());
×
UNCOV
121
    }
×
122
    os << "\n";
×
123
}
×
124

125
static void nop(std::ostream&, const Label&) {}
×
126

UNCOV
127
void print_program(const Program& prog, std::ostream& os, const bool simplify) {
×
128
    print_program(prog, os, simplify, nop, nop);
×
UNCOV
129
}
×
130

131
void print_dot(const Program& prog, std::ostream& out) {
×
132
    out << "digraph program {\n";
×
UNCOV
133
    out << "    node [shape = rectangle];\n";
×
134
    for (const auto& label : prog.labels()) {
×
135
        out << "    \"" << label << "\"[xlabel=\"" << label << "\",label=\"";
×
136

137
        for (const auto& pre : prog.assertions_at(label)) {
×
138
            out << "assert " << pre << "\\l";
×
UNCOV
139
        }
×
140
        out << prog.instruction_at(label) << "\\l";
×
141

142
        out << "\"];\n";
×
143
        for (const Label& next : prog.cfg().children_of(label)) {
×
UNCOV
144
            out << "    \"" << label << "\" -> \"" << next << "\";\n";
×
145
        }
146
        out << "\n";
×
147
    }
UNCOV
148
    out << "}\n";
×
149
}
×
150

151
void print_dot(const Program& prog, const std::string& outfile) {
×
152
    std::ofstream out{outfile};
×
UNCOV
153
    if (out.fail()) {
×
154
        throw std::runtime_error(std::string("Could not open file ") + outfile);
×
155
    }
156
    print_dot(prog, out);
×
157
}
×
158

159
void print_reachability(std::ostream& os, const Report& report) {
×
160
    for (const auto& [label, notes] : report.reachability) {
×
UNCOV
161
        for (const auto& msg : notes) {
×
162
            os << label << ": " << msg << "\n";
×
163
        }
164
    }
165
    os << "\n";
×
UNCOV
166
}
×
167

168
void print_warnings(std::ostream& os, const Report& report) {
×
169
    LineInfoPrinter printer{os};
×
UNCOV
170
    for (const auto& [label, warnings] : report.warnings) {
×
171
        for (const auto& msg : warnings) {
×
172
            printer.print_line_info(label);
×
173
            os << label << ": " << msg << "\n";
×
174
        }
175
    }
176
    os << "\n";
×
UNCOV
177
}
×
178

179
void print_all_messages(std::ostream& os, const Report& report) {
×
180
    print_reachability(os, report);
×
UNCOV
181
    print_warnings(os, report);
×
182
}
×
183

184
void print_invariants(std::ostream& os, const Program& prog, const bool simplify, const Invariants& invariants) {
×
185
    print_program(
×
186
        prog, os, simplify,
187
        [&](std::ostream& os, const Label& label) -> void {
×
188
            os << "\nPre-invariant : " << invariants.invariants.at(label).pre << "\n";
×
UNCOV
189
        },
×
190
        [&](std::ostream& os, const Label& label) -> void {
×
191
            os << "\nPost-invariant : " << invariants.invariants.at(label).post << "\n";
×
192
        });
×
193
}
×
194

195
std::ostream& operator<<(std::ostream& os, const ArgSingle::Kind kind) {
74✔
196
    switch (kind) {
74✔
197
    case ArgSingle::Kind::ANYTHING: return os << "uint64_t";
14✔
198
    case ArgSingle::Kind::PTR_TO_CTX: return os << "ctx";
4✔
199
    case ArgSingle::Kind::MAP_FD: return os << "map_fd";
24✔
UNCOV
200
    case ArgSingle::Kind::MAP_FD_PROGRAMS: return os << "map_fd_programs";
×
201
    case ArgSingle::Kind::PTR_TO_MAP_KEY: return os << "map_key";
24✔
202
    case ArgSingle::Kind::PTR_TO_MAP_VALUE: return os << "map_value";
8✔
203
    }
204
    assert(false);
205
    return os;
206
}
207

UNCOV
208
std::ostream& operator<<(std::ostream& os, const ArgPair::Kind kind) {
×
UNCOV
209
    switch (kind) {
×
UNCOV
210
    case ArgPair::Kind::PTR_TO_READABLE_MEM: return os << "mem";
×
UNCOV
211
    case ArgPair::Kind::PTR_TO_READABLE_MEM_OR_NULL: return os << "mem?";
×
212
    case ArgPair::Kind::PTR_TO_WRITABLE_MEM: return os << "out";
×
213
    }
214
    assert(false);
215
    return os;
216
}
217

218
std::ostream& operator<<(std::ostream& os, const ArgSingle arg) {
74✔
219
    os << arg.kind << " " << arg.reg;
74✔
220
    return os;
74✔
221
}
222

UNCOV
223
std::ostream& operator<<(std::ostream& os, const ArgPair arg) {
×
UNCOV
224
    os << arg.kind << " " << arg.mem << "[" << arg.size;
×
UNCOV
225
    if (arg.can_be_zero) {
×
UNCOV
226
        os << "?";
×
227
    }
228
    os << "], uint64_t " << arg.size;
×
229
    return os;
×
230
}
231

232
std::ostream& operator<<(std::ostream& os, const Bin::Op op) {
278✔
233
    using Op = Bin::Op;
139✔
234
    switch (op) {
278✔
235
    case Op::MOV: return os;
81✔
UNCOV
236
    case Op::MOVSX8: return os << "s8";
×
UNCOV
237
    case Op::MOVSX16: return os << "s16";
×
UNCOV
238
    case Op::MOVSX32: return os << "s32";
×
239
    case Op::ADD: return os << "+";
70✔
240
    case Op::SUB: return os << "-";
×
241
    case Op::MUL: return os << "*";
×
242
    case Op::UDIV: return os << "/";
×
UNCOV
243
    case Op::SDIV: return os << "s/";
×
244
    case Op::UMOD: return os << "%";
×
245
    case Op::SMOD: return os << "s%";
×
246
    case Op::OR: return os << "|";
×
247
    case Op::AND: return os << "&";
30✔
248
    case Op::LSH: return os << "<<";
12✔
249
    case Op::RSH: return os << ">>";
×
250
    case Op::ARSH: return os << ">>>";
2✔
251
    case Op::XOR: return os << "^";
2✔
252
    }
253
    assert(false);
254
    return os;
255
}
256

257
std::ostream& operator<<(std::ostream& os, const Condition::Op op) {
232✔
258
    using Op = Condition::Op;
116✔
259
    switch (op) {
232✔
260
    case Op::EQ: return os << "==";
38✔
261
    case Op::NE: return os << "!=";
12✔
UNCOV
262
    case Op::SET: return os << "&==";
×
UNCOV
263
    case Op::NSET: return os << "&!="; // not in ebpf
×
264
    case Op::LT: return os << "<";     // TODO: os << "u<";
132✔
265
    case Op::LE: return os << "<=";    // TODO: os << "u<=";
12✔
266
    case Op::GT: return os << ">";     // TODO: os << "u>";
22✔
267
    case Op::GE: return os << ">=";    // TODO: os << "u>=";
6✔
268
    case Op::SLT: return os << "s<";
4✔
UNCOV
269
    case Op::SLE: return os << "s<=";
×
270
    case Op::SGT: return os << "s>";
4✔
271
    case Op::SGE: return os << "s>=";
2✔
272
    }
273
    assert(false);
274
    return os;
275
}
276

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

279
// ReSharper disable CppMemberFunctionMayBeConst
280
struct AssertionPrinterVisitor {
281
    std::ostream& _os;
282

283
    void operator()(ValidStore const& a) {
2✔
284
        _os << a.mem << ".type != stack -> " << TypeConstraint{a.val, TypeGroup::number};
2✔
285
    }
2✔
286

287
    void operator()(ValidAccess const& a) {
48,806✔
288
        if (a.or_null) {
48,806✔
289
            _os << "(" << TypeConstraint{a.reg, TypeGroup::number} << " and " << a.reg << ".value == 0) or ";
3,360✔
290
        }
291
        _os << "valid_access(" << a.reg << ".offset";
48,806✔
292
        if (a.offset > 0) {
48,806✔
293
            _os << "+" << a.offset;
30,964✔
294
        } else if (a.offset < 0) {
17,842✔
295
            _os << a.offset;
108✔
296
        }
297

298
        if (a.width == Value{Imm{0}}) {
48,806✔
299
            // a.width == 0, meaning we only care it's an in-bound pointer,
300
            // so it can be compared with another pointer to the same region.
301
            _os << ") for comparison/subtraction";
4,184✔
302
        } else {
303
            _os << ", width=" << a.width << ") for ";
44,622✔
304
            if (a.access_type == AccessType::read) {
44,622✔
305
                _os << "read";
33,068✔
306
            } else {
307
                _os << "write";
11,554✔
308
            }
309
        }
310
    }
48,806✔
311

312
    void operator()(const BoundedLoopCount& a) { _os << Variable::loop_counter(to_string(a.name)) << " < " << a.limit; }
48✔
313

314
    static Variable typereg(const Reg& r) { return Variable::reg(DataKind::types, r.v); }
6,578✔
315

316
    void operator()(ValidSize const& a) {
4,206✔
317
        const auto op = a.can_be_zero ? " >= " : " > ";
4,206✔
318
        _os << a.reg << ".value" << op << 0;
4,206✔
319
    }
4,206✔
320

321
    void operator()(ValidCall const& a) {
2✔
322
        const EbpfHelperPrototype proto = thread_local_program_info->platform->get_helper_prototype(a.func);
2✔
323
        _os << "valid call(" << proto.name << ")";
2✔
324
    }
2✔
325

326
    void operator()(ValidMapKeyValue const& a) {
142✔
327
        _os << "within stack(" << a.access_reg << ":" << (a.key ? "key_size" : "value_size") << "(" << a.map_fd_reg
178✔
328
            << "))";
142✔
329
    }
142✔
330

331
    void operator()(ZeroCtxOffset const& a) { _os << Variable::reg(DataKind::ctx_offsets, a.reg.v) << " == 0"; }
5,236✔
332

333
    void operator()(Comparable const& a) {
2,204✔
334
        if (a.or_r2_is_number) {
2,204✔
335
            _os << TypeConstraint{a.r2, TypeGroup::number} << " or ";
366✔
336
        }
337
        _os << typereg(a.r1) << " == " << typereg(a.r2) << " in " << TypeGroup::singleton_ptr;
2,204✔
338
    }
2,204✔
339

340
    void operator()(Addable const& a) {
4✔
341
        _os << TypeConstraint{a.ptr, TypeGroup::pointer} << " -> " << TypeConstraint{a.num, TypeGroup::number};
8✔
342
    }
4✔
343

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

346
    void operator()(TypeConstraint const& tc) {
2,164✔
347
        const string cmp_op = is_singleton_type(tc.types) ? "==" : "in";
2,190✔
348
        _os << typereg(tc.reg) << " " << cmp_op << " " << tc.types;
2,164✔
349
    }
2,164✔
350

351
    void operator()(FuncConstraint const& fc) { _os << typereg(fc.reg) << " is helper"; }
6✔
352
};
353

354
// ReSharper disable CppMemberFunctionMayBeConst
355
struct CommandPrinterVisitor {
356
    std::ostream& os_;
357

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

UNCOV
360
    void operator()(Undefined const& a) { os_ << "Undefined{" << a.opcode << "}"; }
×
361

362
    void operator()(LoadMapFd const& b) { os_ << b.dst << " = map_fd " << b.mapfd; }
24✔
363

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

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

370
    void operator()(Bin const& b) {
278✔
371
        os_ << reg_name(b.dst, b.is64) << " " << b.op << "= " << b.v;
417✔
372
        if (b.lddw) {
278✔
373
            os_ << " ll";
2✔
374
        }
375
    }
278✔
376

377
    void operator()(Un const& b) {
12✔
378
        os_ << b.dst << " = ";
12✔
379
        switch (b.op) {
12✔
380
        case Un::Op::BE16: os_ << "be16 "; break;
2✔
381
        case Un::Op::BE32: os_ << "be32 "; break;
2✔
382
        case Un::Op::BE64: os_ << "be64 "; break;
2✔
383
        case Un::Op::LE16: os_ << "le16 "; break;
2✔
384
        case Un::Op::LE32: os_ << "le32 "; break;
2✔
385
        case Un::Op::LE64: os_ << "le64 "; break;
2✔
UNCOV
386
        case Un::Op::SWAP16: os_ << "swap16 "; break;
×
UNCOV
387
        case Un::Op::SWAP32: os_ << "swap32 "; break;
×
UNCOV
388
        case Un::Op::SWAP64: os_ << "swap64 "; break;
×
UNCOV
389
        case Un::Op::NEG: os_ << "-"; break;
×
390
        }
391
        os_ << b.dst;
12✔
392
    }
12✔
393

394
    void operator()(Call const& call) {
70✔
395
        os_ << "r0 = " << call.name << ":" << call.func << "(";
70✔
396
        for (uint8_t r = 1; r <= 5; r++) {
144✔
397
            // Look for a singleton.
398
            auto single = std::ranges::find_if(call.singles, [r](const ArgSingle arg) { return arg.reg.v == r; });
372✔
399
            if (single != call.singles.end()) {
144✔
400
                if (r > 1) {
74✔
401
                    os_ << ", ";
48✔
402
                }
403
                os_ << *single;
74✔
404
                continue;
74✔
405
            }
406

407
            // Look for the start of a pair.
408
            auto pair = std::ranges::find_if(call.pairs, [r](const ArgPair arg) { return arg.mem.v == r; });
70✔
409
            if (pair != call.pairs.end()) {
70✔
UNCOV
410
                if (r > 1) {
×
UNCOV
411
                    os_ << ", ";
×
412
                }
UNCOV
413
                os_ << *pair;
×
UNCOV
414
                r++;
×
UNCOV
415
                continue;
×
416
            }
417

418
            // Not found.
419
            break;
70✔
420
        }
421
        os_ << ")";
70✔
422
    }
70✔
423

UNCOV
424
    void operator()(CallLocal const& call) { os_ << "call <" << to_string(call.target) << ">"; }
×
425

UNCOV
426
    void operator()(Callx const& callx) { os_ << "callx " << callx.func; }
×
427

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

UNCOV
430
    void operator()(Jmp const& b) {
×
431
        // A "standalone" jump Instruction.
432
        // Print the label without offset calculations.
UNCOV
433
        if (b.cond) {
×
434
            os_ << "if ";
×
UNCOV
435
            print(*b.cond);
×
UNCOV
436
            os_ << " ";
×
437
        }
438
        os_ << "goto label <" << to_string(b.target) << ">";
×
UNCOV
439
    }
×
440

441
    void operator()(Jmp const& b, const int offset) {
54✔
442
        const string sign = offset > 0 ? "+" : "";
54✔
443
        const string target = sign + std::to_string(offset) + " <" + to_string(b.target) + ">";
108✔
444

445
        if (b.cond) {
54✔
446
            os_ << "if ";
40✔
447
            print(*b.cond);
40✔
448
            os_ << " ";
40✔
449
        }
450
        os_ << "goto " << target;
54✔
451
    }
54✔
452

UNCOV
453
    void operator()(Packet const& b) {
×
454
        /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
455
        /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
UNCOV
456
        const string s = size(b.width);
×
UNCOV
457
        os_ << "r0 = ";
×
UNCOV
458
        os_ << "*(" << s << " *)skb[";
×
UNCOV
459
        if (b.regoffset) {
×
UNCOV
460
            os_ << *b.regoffset;
×
461
        }
UNCOV
462
        if (b.offset != 0) {
×
UNCOV
463
            if (b.regoffset) {
×
464
                os_ << " + ";
×
465
            }
466
            os_ << b.offset;
×
467
        }
468
        os_ << "]";
×
UNCOV
469
    }
×
470

471
    void print(Deref const& access) {
184✔
472
        const string sign = access.offset < 0 ? " - " : " + ";
212✔
473
        const int offset = std::abs(access.offset); // what about INT_MIN?
184✔
474
        os_ << "*(" << size(access.width) << " *)";
276✔
475
        os_ << "(" << access.basereg << sign << offset << ")";
184✔
476
    }
184✔
477

478
    void print(Condition const& cond) {
232✔
479
        os_ << cond.left << " " << ((!cond.is64) ? "w" : "") << cond.op << " " << cond.right;
273✔
480
    }
232✔
481

482
    void operator()(Mem const& b) {
184✔
483
        if (b.is_load) {
184✔
484
            os_ << b.value << " = ";
44✔
485
        }
486
        print(b.access);
184✔
487
        if (!b.is_load) {
184✔
488
            os_ << " = " << b.value;
140✔
489
        }
490
    }
184✔
491

UNCOV
492
    void operator()(Atomic const& b) {
×
UNCOV
493
        os_ << "lock ";
×
UNCOV
494
        print(b.access);
×
UNCOV
495
        os_ << " ";
×
UNCOV
496
        bool showfetch = true;
×
UNCOV
497
        switch (b.op) {
×
UNCOV
498
        case Atomic::Op::ADD: os_ << "+"; break;
×
UNCOV
499
        case Atomic::Op::OR: os_ << "|"; break;
×
500
        case Atomic::Op::AND: os_ << "&"; break;
×
501
        case Atomic::Op::XOR: os_ << "^"; break;
×
502
        case Atomic::Op::XCHG:
×
503
            os_ << "x";
×
504
            showfetch = false;
×
505
            break;
×
506
        case Atomic::Op::CMPXCHG:
×
507
            os_ << "cx";
×
508
            showfetch = false;
×
509
            break;
×
510
        }
511
        os_ << "= " << b.valreg;
×
512

513
        if (showfetch && b.fetch) {
×
514
            os_ << " fetch";
×
515
        }
516
    }
×
517

518
    void operator()(Assume const& b) {
192✔
519
        os_ << "assume ";
192✔
520
        print(b.cond);
192✔
521
    }
192✔
522

UNCOV
523
    void operator()(IncrementLoopCounter const& a) { os_ << Variable::loop_counter(to_string(a.name)) << "++"; }
×
524
};
525
// ReSharper restore CppMemberFunctionMayBeConst
526

527
std::ostream& operator<<(std::ostream& os, Instruction const& ins) {
194✔
528
    std::visit(CommandPrinterVisitor{os}, ins);
97✔
529
    return os;
97✔
530
}
531

532
string to_string(Instruction const& ins) {
194✔
533
    std::stringstream str;
194✔
534
    str << ins;
194✔
535
    return str.str();
388✔
536
}
194✔
537

538
std::ostream& operator<<(std::ostream& os, const Assertion& a) {
62,880✔
539
    std::visit(AssertionPrinterVisitor{os}, a);
32,407✔
540
    return os;
32,407✔
541
}
542

543
string to_string(Assertion const& constraint) {
60,946✔
544
    std::stringstream str;
60,946✔
545
    str << constraint;
60,946✔
546
    return str.str();
121,892✔
547
}
60,946✔
548

549
auto get_labels(const InstructionSeq& insts) {
38✔
550
    Pc pc = 0;
38✔
551
    std::map<Label, Pc> pc_of_label;
38✔
552
    for (const auto& [label, inst, _] : insts) {
708✔
553
        pc_of_label[label] = pc;
670✔
554
        pc += size(inst);
670✔
555
    }
556
    return pc_of_label;
38✔
UNCOV
557
}
×
558

559
void print(const InstructionSeq& insts, std::ostream& out, const std::optional<const Label>& label_to_print,
38✔
560
           const bool print_line_info) {
561
    const auto pc_of_label = get_labels(insts);
38✔
562
    Pc pc = 0;
38✔
563
    std::string previous_source;
38✔
564
    CommandPrinterVisitor visitor{out};
38✔
565
    for (const LabeledInstruction& labeled_inst : insts) {
708✔
566
        const auto& [label, ins, line_info] = labeled_inst;
670✔
567
        if (!label_to_print.has_value() || label == label_to_print) {
670✔
568
            if (line_info.has_value() && print_line_info) {
670✔
UNCOV
569
                auto& [file, source, line, column] = line_info.value();
×
570
                // Only decorate the first instruction associated with a source line.
UNCOV
571
                if (source != previous_source) {
×
UNCOV
572
                    out << line_info.value();
×
UNCOV
573
                    previous_source = source;
×
574
                }
575
            }
576
            if (label.isjump()) {
670✔
577
                out << "\n";
×
UNCOV
578
                out << label << ":\n";
×
579
            }
580
            if (label_to_print.has_value()) {
670✔
581
                out << pc << ": ";
×
582
            } else {
583
                out << std::setw(8) << pc << ":\t";
670✔
584
            }
585
            if (const auto jmp = std::get_if<Jmp>(&ins)) {
670✔
586
                if (!pc_of_label.contains(jmp->target)) {
54✔
UNCOV
587
                    throw std::runtime_error(string("Cannot find label ") + to_string(jmp->target));
×
588
                }
589
                const Pc target_pc = pc_of_label.at(jmp->target);
54✔
590
                visitor(*jmp, target_pc - static_cast<int>(pc) - 1);
54✔
591
            } else {
592
                std::visit(visitor, ins);
616✔
593
            }
594
            out << "\n";
670✔
595
        }
596
        pc += size(ins);
670✔
597
    }
598
}
38✔
599

600
std::ostream& operator<<(std::ostream& o, const EbpfMapDescriptor& desc) {
28✔
601
    return o << "(" << "original_fd = " << desc.original_fd << ", " << "inner_map_fd = " << desc.inner_map_fd << ", "
28✔
602
             << "type = " << desc.type << ", " << "max_entries = " << desc.max_entries << ", "
28✔
603
             << "value_size = " << desc.value_size << ", " << "key_size = " << desc.key_size << ")";
28✔
604
}
605

606
void print_map_descriptors(const std::vector<EbpfMapDescriptor>& descriptors, std::ostream& o) {
38✔
607
    int i = 0;
38✔
608
    for (const auto& desc : descriptors) {
66✔
609
        o << "map " << i << ":" << desc << "\n";
28✔
610
        i++;
28✔
611
    }
612
}
38✔
613

UNCOV
614
std::ostream& operator<<(std::ostream& os, const btf_line_info_t& line_info) {
×
UNCOV
615
    os << "; " << line_info.file_name << ":" << line_info.line_number << "\n";
×
UNCOV
616
    os << "; " << line_info.source_line << "\n";
×
UNCOV
617
    return os;
×
618
}
619
} // 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