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

vbpf / ebpf-verifier / 13955273819

19 Mar 2025 07:38PM UTC coverage: 88.66% (+0.5%) from 88.134%
13955273819

push

github

web-flow
Fix sign extension (#850)

* fix sign extension
* add exhaustive tests for sign extension of <=3 bits
* fix interval::size()
* streamline bit/width operations

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

203 of 214 new or added lines in 9 files covered. (94.86%)

9 existing lines in 5 files now uncovered.

8639 of 9744 relevant lines covered (88.66%)

8977818.22 hits per line

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

53.77
/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 "crab/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 crab::TypeGroup;
22
using std::optional;
23
using std::string;
24
using std::vector;
25

26
namespace crab {
27

28
std::ostream& operator<<(std::ostream& o, const number_t& z) { return o << z._n.str(); }
349,844✔
29

UNCOV
30
std::string number_t::to_string() const { return _n.str(); }
×
31

32
std::ostream& operator<<(std::ostream& o, const interval_t& interval) {
856✔
33
    if (interval.is_bottom()) {
856✔
NEW
34
        o << "_|_";
×
35
    } else {
36
        o << "[" << interval._lb << ", " << interval._ub << "]";
856✔
37
    }
38
    return o;
856✔
39
}
40

41
std::string interval_t::to_string() const {
×
42
    std::ostringstream s;
×
43
    s << *this;
×
44
    return s.str();
×
45
}
×
46

47
std::ostream& operator<<(std::ostream& os, const label_t& label) {
1,592✔
48
    if (label == label_t::entry) {
1,592✔
49
        return os << "entry";
6✔
50
    }
51
    if (label == label_t::exit) {
1,586✔
52
        return os << "exit";
6✔
53
    }
54
    if (!label.stack_frame_prefix.empty()) {
1,580✔
55
        os << label.stack_frame_prefix << STACK_FRAME_DELIMITER;
330✔
56
    }
57
    os << label.from;
1,580✔
58
    if (label.to != -1) {
1,580✔
59
        os << ":" << label.to;
50✔
60
    }
61
    if (!label.special_label.empty()) {
1,580✔
62
        os << " (" << label.special_label << ")";
20✔
63
    }
64
    return os;
790✔
65
}
66

67
string to_string(label_t const& label) {
1,546✔
68
    std::stringstream str;
1,546✔
69
    str << label;
1,546✔
70
    return str.str();
3,092✔
71
}
1,546✔
72

73
} // namespace crab
74

75
struct LineInfoPrinter {
76
    std::ostream& os;
77
    std::string previous_source_line;
78

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

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

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

129
static void nop(std::ostream&, const label_t&) {}
×
130

131
void print_program(const Program& prog, std::ostream& os, const bool simplify) {
×
132
    print_program(prog, os, simplify, nop, nop);
×
133
}
×
134

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

141
        for (const auto& pre : prog.assertions_at(label)) {
×
142
            out << "assert " << pre << "\\l";
×
143
        }
×
144
        out << prog.instruction_at(label) << "\\l";
×
145

146
        out << "\"];\n";
×
147
        for (const label_t& next : prog.cfg().children_of(label)) {
×
148
            out << "    \"" << label << "\" -> \"" << next << "\";\n";
×
149
        }
150
        out << "\n";
×
151
    }
152
    out << "}\n";
×
153
}
×
154

155
void print_dot(const Program& prog, const std::string& outfile) {
×
156
    std::ofstream out{outfile};
×
157
    if (out.fail()) {
×
158
        throw std::runtime_error(std::string("Could not open file ") + outfile);
×
159
    }
160
    print_dot(prog, out);
×
161
}
×
162

163
void print_reachability(std::ostream& os, const Report& report) {
×
164
    for (const auto& [label, notes] : report.reachability) {
×
165
        for (const auto& msg : notes) {
×
166
            os << label << ": " << msg << "\n";
×
167
        }
168
    }
169
    os << "\n";
×
170
}
×
171

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

183
void print_all_messages(std::ostream& os, const Report& report) {
×
184
    print_reachability(os, report);
×
185
    print_warnings(os, report);
×
186
}
×
187

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

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

213
std::ostream& operator<<(std::ostream& os, const ArgPair::Kind kind) {
×
214
    switch (kind) {
×
215
    case ArgPair::Kind::PTR_TO_READABLE_MEM: return os << "mem";
×
216
    case ArgPair::Kind::PTR_TO_READABLE_MEM_OR_NULL: return os << "mem?";
×
217
    case ArgPair::Kind::PTR_TO_WRITABLE_MEM: return os << "out";
×
218
    }
219
    assert(false);
220
    return os;
221
}
222

223
std::ostream& operator<<(std::ostream& os, const ArgSingle arg) {
74✔
224
    os << arg.kind << " " << arg.reg;
74✔
225
    return os;
74✔
226
}
227

228
std::ostream& operator<<(std::ostream& os, const ArgPair arg) {
×
229
    os << arg.kind << " " << arg.mem << "[" << arg.size;
×
230
    if (arg.can_be_zero) {
×
231
        os << "?";
×
232
    }
233
    os << "], uint64_t " << arg.size;
×
234
    return os;
×
235
}
236

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

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

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

284
// ReSharper disable CppMemberFunctionMayBeConst
285
struct AssertionPrinterVisitor {
286
    std::ostream& _os;
287

288
    void operator()(ValidStore const& a) {
2✔
289
        _os << a.mem << ".type != stack -> " << TypeConstraint{a.val, TypeGroup::number};
2✔
290
    }
2✔
291

292
    void operator()(ValidAccess const& a) {
47,746✔
293
        if (a.or_null) {
47,746✔
294
            _os << "(" << TypeConstraint{a.reg, TypeGroup::number} << " and " << a.reg << ".value == 0) or ";
3,360✔
295
        }
296
        _os << "valid_access(" << a.reg << ".offset";
47,746✔
297
        if (a.offset > 0) {
47,746✔
298
            _os << "+" << a.offset;
30,140✔
299
        } else if (a.offset < 0) {
17,606✔
300
            _os << a.offset;
100✔
301
        }
302

303
        if (a.width == Value{Imm{0}}) {
47,746✔
304
            // a.width == 0, meaning we only care it's an in-bound pointer,
305
            // so it can be compared with another pointer to the same region.
306
            _os << ") for comparison/subtraction";
4,040✔
307
        } else {
308
            _os << ", width=" << a.width << ") for ";
43,706✔
309
            if (a.access_type == AccessType::read) {
43,706✔
310
                _os << "read";
32,152✔
311
            } else {
312
                _os << "write";
11,554✔
313
            }
314
        }
315
    }
47,746✔
316

317
    void operator()(const BoundedLoopCount& a) {
32✔
318
        _os << crab::variable_t::loop_counter(to_string(a.name)) << " < " << a.limit;
32✔
319
    }
32✔
320

321
    static crab::variable_t typereg(const Reg& r) { return crab::variable_t::reg(crab::data_kind_t::types, r.v); }
6,194✔
322

323
    void operator()(ValidSize const& a) {
4,206✔
324
        const auto op = a.can_be_zero ? " >= " : " > ";
4,206✔
325
        _os << a.reg << ".value" << op << 0;
4,206✔
326
    }
4,206✔
327

328
    void operator()(ValidCall const& a) {
2✔
329
        const EbpfHelperPrototype proto = thread_local_program_info->platform->get_helper_prototype(a.func);
2✔
330
        _os << "valid call(" << proto.name << ")";
2✔
331
    }
2✔
332

333
    void operator()(ValidMapKeyValue const& a) {
142✔
334
        _os << "within stack(" << a.access_reg << ":" << (a.key ? "key_size" : "value_size") << "(" << a.map_fd_reg
178✔
335
            << "))";
142✔
336
    }
142✔
337

338
    void operator()(ZeroCtxOffset const& a) {
5,236✔
339
        _os << crab::variable_t::reg(crab::data_kind_t::ctx_offsets, a.reg.v) << " == 0";
5,236✔
340
    }
5,236✔
341

342
    void operator()(Comparable const& a) {
2,036✔
343
        if (a.or_r2_is_number) {
2,036✔
344
            _os << TypeConstraint{a.r2, TypeGroup::number} << " or ";
306✔
345
        }
346
        _os << typereg(a.r1) << " == " << typereg(a.r2) << " in " << TypeGroup::singleton_ptr;
2,036✔
347
    }
2,036✔
348

349
    void operator()(Addable const& a) {
4✔
350
        _os << TypeConstraint{a.ptr, TypeGroup::pointer} << " -> " << TypeConstraint{a.num, TypeGroup::number};
8✔
351
    }
4✔
352

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

355
    void operator()(TypeConstraint const& tc) {
2,116✔
356
        const string cmp_op = is_singleton_type(tc.types) ? "==" : "in";
2,142✔
357
        _os << typereg(tc.reg) << " " << cmp_op << " " << tc.types;
2,116✔
358
    }
2,116✔
359

360
    void operator()(FuncConstraint const& fc) { _os << typereg(fc.reg) << " is helper"; }
6✔
361
};
362

363
// ReSharper disable CppMemberFunctionMayBeConst
364
struct CommandPrinterVisitor {
365
    std::ostream& os_;
366

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

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

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

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

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

379
    void operator()(Bin const& b) {
294✔
380
        os_ << reg_name(b.dst, b.is64) << " " << b.op << "= " << b.v;
441✔
381
        if (b.lddw) {
294✔
382
            os_ << " ll";
2✔
383
        }
384
    }
294✔
385

386
    void operator()(Un const& b) {
12✔
387
        os_ << b.dst << " = ";
12✔
388
        switch (b.op) {
12✔
389
        case Un::Op::BE16: os_ << "be16 "; break;
2✔
390
        case Un::Op::BE32: os_ << "be32 "; break;
2✔
391
        case Un::Op::BE64: os_ << "be64 "; break;
2✔
392
        case Un::Op::LE16: os_ << "le16 "; break;
2✔
393
        case Un::Op::LE32: os_ << "le32 "; break;
2✔
394
        case Un::Op::LE64: os_ << "le64 "; break;
2✔
395
        case Un::Op::SWAP16: os_ << "swap16 "; break;
×
396
        case Un::Op::SWAP32: os_ << "swap32 "; break;
×
397
        case Un::Op::SWAP64: os_ << "swap64 "; break;
×
398
        case Un::Op::NEG: os_ << "-"; break;
×
399
        }
400
        os_ << b.dst;
12✔
401
    }
12✔
402

403
    void operator()(Call const& call) {
70✔
404
        os_ << "r0 = " << call.name << ":" << call.func << "(";
70✔
405
        for (uint8_t r = 1; r <= 5; r++) {
144✔
406
            // Look for a singleton.
407
            auto single = std::ranges::find_if(call.singles, [r](const ArgSingle arg) { return arg.reg.v == r; });
372✔
408
            if (single != call.singles.end()) {
144✔
409
                if (r > 1) {
74✔
410
                    os_ << ", ";
48✔
411
                }
412
                os_ << *single;
74✔
413
                continue;
74✔
414
            }
415

416
            // Look for the start of a pair.
417
            auto pair = std::ranges::find_if(call.pairs, [r](const ArgPair arg) { return arg.mem.v == r; });
70✔
418
            if (pair != call.pairs.end()) {
70✔
419
                if (r > 1) {
×
420
                    os_ << ", ";
×
421
                }
422
                os_ << *pair;
×
423
                r++;
×
424
                continue;
×
425
            }
426

427
            // Not found.
428
            break;
70✔
429
        }
430
        os_ << ")";
70✔
431
    }
70✔
432

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

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

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

439
    void operator()(Jmp const& b) {
×
440
        // A "standalone" jump Instruction.
441
        // Print the label without offset calculations.
442
        if (b.cond) {
×
443
            os_ << "if ";
×
444
            print(*b.cond);
×
445
            os_ << " ";
×
446
        }
447
        os_ << "goto label <" << to_string(b.target) << ">";
×
448
    }
×
449

450
    void operator()(Jmp const& b, const int offset) {
54✔
451
        const string sign = offset > 0 ? "+" : "";
54✔
452
        const string target = sign + std::to_string(offset) + " <" + to_string(b.target) + ">";
108✔
453

454
        if (b.cond) {
54✔
455
            os_ << "if ";
40✔
456
            print(*b.cond);
40✔
457
            os_ << " ";
40✔
458
        }
459
        os_ << "goto " << target;
54✔
460
    }
54✔
461

462
    void operator()(Packet const& b) {
×
463
        /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
464
        /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
465
        const string s = size(b.width);
×
466
        os_ << "r0 = ";
×
467
        os_ << "*(" << s << " *)skb[";
×
468
        if (b.regoffset) {
×
469
            os_ << *b.regoffset;
×
470
        }
471
        if (b.offset != 0) {
×
472
            if (b.regoffset) {
×
473
                os_ << " + ";
×
474
            }
475
            os_ << b.offset;
×
476
        }
477
        os_ << "]";
×
478
    }
×
479

480
    void print(Deref const& access) {
184✔
481
        const string sign = access.offset < 0 ? " - " : " + ";
212✔
482
        int offset = std::abs(access.offset); // what about INT_MIN?
184✔
483
        os_ << "*(" << size(access.width) << " *)";
276✔
484
        os_ << "(" << access.basereg << sign << offset << ")";
184✔
485
    }
184✔
486

487
    void print(Condition const& cond) {
232✔
488
        os_ << cond.left << " " << ((!cond.is64) ? "w" : "") << cond.op << " " << cond.right;
273✔
489
    }
232✔
490

491
    void operator()(Mem const& b) {
184✔
492
        if (b.is_load) {
184✔
493
            os_ << b.value << " = ";
44✔
494
        }
495
        print(b.access);
184✔
496
        if (!b.is_load) {
184✔
497
            os_ << " = " << b.value;
140✔
498
        }
499
    }
184✔
500

501
    void operator()(Atomic const& b) {
×
502
        os_ << "lock ";
×
503
        print(b.access);
×
504
        os_ << " ";
×
505
        bool showfetch = true;
×
506
        switch (b.op) {
×
507
        case Atomic::Op::ADD: os_ << "+"; break;
×
508
        case Atomic::Op::OR: os_ << "|"; break;
×
509
        case Atomic::Op::AND: os_ << "&"; break;
×
510
        case Atomic::Op::XOR: os_ << "^"; break;
×
511
        case Atomic::Op::XCHG:
×
512
            os_ << "x";
×
513
            showfetch = false;
×
514
            break;
×
515
        case Atomic::Op::CMPXCHG:
×
516
            os_ << "cx";
×
517
            showfetch = false;
×
518
            break;
×
519
        }
520
        os_ << "= " << b.valreg;
×
521

522
        if (showfetch && b.fetch) {
×
523
            os_ << " fetch";
×
524
        }
525
    }
×
526

527
    void operator()(Assume const& b) {
192✔
528
        os_ << "assume ";
192✔
529
        print(b.cond);
192✔
530
    }
192✔
531

532
    void operator()(IncrementLoopCounter const& a) { os_ << crab::variable_t::loop_counter(to_string(a.name)) << "++"; }
×
533
};
534
// ReSharper restore CppMemberFunctionMayBeConst
535

536
std::ostream& operator<<(std::ostream& os, Instruction const& ins) {
210✔
537
    std::visit(CommandPrinterVisitor{os}, ins);
113✔
538
    return os;
105✔
539
}
540

541
string to_string(Instruction const& ins) {
194✔
542
    std::stringstream str;
194✔
543
    str << ins;
194✔
544
    return str.str();
388✔
545
}
194✔
546

547
std::ostream& operator<<(std::ostream& os, const Assertion& a) {
61,604✔
548
    std::visit(AssertionPrinterVisitor{os}, a);
31,749✔
549
    return os;
31,749✔
550
}
551

552
string to_string(Assertion const& constraint) {
59,710✔
553
    std::stringstream str;
59,710✔
554
    str << constraint;
59,710✔
555
    return str.str();
119,420✔
556
}
59,710✔
557

558
auto get_labels(const InstructionSeq& insts) {
38✔
559
    pc_t pc = 0;
38✔
560
    std::map<label_t, pc_t> pc_of_label;
38✔
561
    for (const auto& [label, inst, _] : insts) {
708✔
562
        pc_of_label[label] = pc;
670✔
563
        pc += size(inst);
670✔
564
    }
565
    return pc_of_label;
38✔
566
}
×
567

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

609
} // namespace asm_syntax
610

611
std::ostream& operator<<(std::ostream& o, const EbpfMapDescriptor& desc) {
28✔
612
    return o << "(" << "original_fd = " << desc.original_fd << ", " << "inner_map_fd = " << desc.inner_map_fd << ", "
28✔
613
             << "type = " << desc.type << ", " << "max_entries = " << desc.max_entries << ", "
28✔
614
             << "value_size = " << desc.value_size << ", " << "key_size = " << desc.key_size << ")";
28✔
615
}
616

617
void print_map_descriptors(const std::vector<EbpfMapDescriptor>& descriptors, std::ostream& o) {
38✔
618
    int i = 0;
38✔
619
    for (const auto& desc : descriptors) {
66✔
620
        o << "map " << i << ":" << desc << "\n";
28✔
621
        i++;
28✔
622
    }
623
}
38✔
624

625
std::ostream& operator<<(std::ostream& os, const btf_line_info_t& line_info) {
×
626
    os << "; " << line_info.file_name << ":" << line_info.line_number << "\n";
×
627
    os << "; " << line_info.source_line << "\n";
×
628
    return os;
×
629
}
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

© 2025 Coveralls, Inc