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

vbpf / ebpf-verifier / 12845504567

18 Jan 2025 04:08PM UTC coverage: 88.169% (-1.5%) from 89.646%
12845504567

push

github

web-flow
Handle upgrade from LCOV 1.15 to LCOV 2.0 (#826)

* Testing code coverage with a comment only change
* Workaround for failing code coverage
* Testing code coverage fix

Signed-off-by: Alan Jowett <alanjo@microsoft.com>

8481 of 9619 relevant lines covered (88.17%)

7430805.79 hits per line

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

73.47
/src/test/ebpf_yaml.cpp
1
// Copyright (c) Prevail Verifier contributors.
2
// SPDX-License-Identifier: MIT
3

4
#include <algorithm>
5
#include <bit>
6
#include <iostream>
7
#include <set>
8
#include <variant>
9

10
#include <boost/algorithm/string.hpp>
11

12
#include <yaml-cpp/yaml.h>
13

14
#include "asm_parse.hpp"
15
#include "asm_syntax.hpp"
16
#include "ebpf_verifier.hpp"
17
#include "ebpf_yaml.hpp"
18
#include "string_constraints.hpp"
19

20
using std::string;
21
using std::vector;
22

23
// The YAML tests for Call depend on Linux prototypes.
24
// parse_instruction() in asm_parse.cpp explicitly uses
25
// g_ebpf_platform_linux when parsing Call instructions
26
// so we do the same here.
27

28
static EbpfProgramType ebpf_get_program_type(const string& section, const string& path) {
×
29
    return g_ebpf_platform_linux.get_program_type(section, path);
×
30
}
31

32
static EbpfMapType ebpf_get_map_type(const uint32_t platform_specific_type) {
28✔
33
    return g_ebpf_platform_linux.get_map_type(platform_specific_type);
28✔
34
}
35

36
static EbpfHelperPrototype ebpf_get_helper_prototype(const int32_t n) {
92✔
37
    return g_ebpf_platform_linux.get_helper_prototype(n);
92✔
38
}
39

40
static bool ebpf_is_helper_usable(const int32_t n) { return g_ebpf_platform_linux.is_helper_usable(n); }
96✔
41

42
static void ebpf_parse_maps_section(vector<EbpfMapDescriptor>&, const char*, size_t, int, const ebpf_platform_t*,
×
43
                                    ebpf_verifier_options_t) {}
×
44

45
static EbpfMapDescriptor test_map_descriptor = {.original_fd = 0,
46
                                                .type = 0,
47
                                                .key_size = sizeof(uint32_t),
48
                                                .value_size = sizeof(uint32_t),
49
                                                .max_entries = 4,
50
                                                .inner_map_fd = 0};
51

52
static EbpfMapDescriptor& ebpf_get_map_descriptor(int) { return test_map_descriptor; }
240✔
53

54
ebpf_platform_t g_platform_test = {.get_program_type = ebpf_get_program_type,
55
                                   .get_helper_prototype = ebpf_get_helper_prototype,
56
                                   .is_helper_usable = ebpf_is_helper_usable,
57
                                   .map_record_size = 0,
58
                                   .parse_maps_section = ebpf_parse_maps_section,
59
                                   .get_map_descriptor = ebpf_get_map_descriptor,
60
                                   .get_map_type = ebpf_get_map_type,
61
                                   .supported_conformance_groups = bpf_conformance_groups_t::default_groups |
62
                                                                   bpf_conformance_groups_t::packet |
63
                                                                   bpf_conformance_groups_t::callx};
64

65
static EbpfProgramType make_program_type(const string& name, const ebpf_context_descriptor_t* context_descriptor) {
1,822✔
66
    return EbpfProgramType{.name = name,
1,130✔
67
                           .context_descriptor = context_descriptor,
68
                           .platform_specific_data = 0,
69
                           .section_prefixes = {},
70
                           .is_privileged = false};
911✔
71
}
72

73
static std::set<string> vector_to_set(const vector<string>& s) {
3,480✔
74
    std::set<string> res;
3,480✔
75
    for (const auto& item : s) {
16,728✔
76
        res.insert(item);
13,248✔
77
    }
78
    return res;
3,480✔
79
}
×
80

81
std::set<string> operator-(const std::set<string>& a, const std::set<string>& b) {
×
82
    std::set<string> res;
×
83
    std::ranges::set_difference(a, b, std::inserter(res, res.begin()));
×
84
    return res;
×
85
}
×
86

87
static string_invariant read_invariant(const vector<string>& raw_invariant) {
2,768✔
88
    const std::set<string> res = vector_to_set(raw_invariant);
2,768✔
89
    if (res == std::set<string>{"_|_"}) {
6,920✔
90
        return string_invariant{};
×
91
    }
92
    return string_invariant{res};
2,768✔
93
}
9,688✔
94

95
struct RawTestCase {
96
    string test_case;
97
    std::set<string> options;
98
    vector<string> pre;
99
    vector<std::tuple<string, vector<string>>> raw_blocks;
100
    vector<string> post;
101
    std::set<string> messages;
102
};
103

104
static vector<string> parse_block(const YAML::Node& block_node) {
1,620✔
105
    vector<string> block;
1,620✔
106
    std::istringstream is{block_node.as<string>()};
1,620✔
107
    string line;
1,620✔
108
    while (std::getline(is, line)) {
3,752✔
109
        block.emplace_back(line);
2,132✔
110
    }
111
    return block;
3,240✔
112
}
1,620✔
113

114
static auto parse_code(const YAML::Node& code_node) {
1,384✔
115
    vector<std::tuple<string, vector<string>>> res;
1,384✔
116
    for (const auto& item : code_node) {
4,624✔
117
        res.emplace_back(item.first.as<string>(), parse_block(item.second));
2,430✔
118
    }
3,004✔
119
    return res;
1,384✔
120
}
×
121

122
static std::set<string> as_set_empty_default(const YAML::Node& optional_node) {
2,768✔
123
    if (!optional_node.IsDefined() || optional_node.IsNull()) {
3,124✔
124
        return {};
2,056✔
125
    }
126
    return vector_to_set(optional_node.as<vector<string>>());
712✔
127
}
128

129
static RawTestCase parse_case(const YAML::Node& case_node) {
1,384✔
130
    return RawTestCase{
2,076✔
131
        .test_case = case_node["test-case"].as<string>(),
132
        .options = as_set_empty_default(case_node["options"]),
133
        .pre = case_node["pre"].as<vector<string>>(),
134
        .raw_blocks = parse_code(case_node["code"]),
135
        .post = case_node["post"].as<vector<string>>(),
136
        .messages = as_set_empty_default(case_node["messages"]),
137
    };
2,076✔
138
}
139

140
static InstructionSeq raw_cfg_to_instruction_seq(const vector<std::tuple<string, vector<string>>>& raw_blocks) {
1,384✔
141
    std::map<string, crab::label_t> label_name_to_label;
1,384✔
142

143
    int label_index = 0;
1,384✔
144
    for (const auto& [label_name, raw_block] : raw_blocks) {
3,004✔
145
        label_name_to_label.emplace(label_name, label_index);
1,620✔
146
        // don't count large instructions as 2
147
        label_index += gsl::narrow<int>(raw_block.size());
1,620✔
148
    }
149

150
    InstructionSeq res;
1,384✔
151
    label_index = 0;
1,384✔
152
    for (const auto& [label_name, raw_block] : raw_blocks) {
3,004✔
153
        for (const string& line : raw_block) {
3,752✔
154
            try {
1,066✔
155
                const Instruction& ins = parse_instruction(line, label_name_to_label);
2,132✔
156
                if (std::holds_alternative<Undefined>(ins)) {
2,132✔
157
                    std::cout << "text:" << line << "; ins: " << ins << "\n";
×
158
                }
159
                res.emplace_back(label_index, ins, std::optional<btf_line_info_t>());
3,198✔
160
            } catch (const std::exception& e) {
2,132✔
161
                std::cout << "text:" << line << "; error: " << e.what() << "\n";
×
162
                res.emplace_back(label_index, Undefined{0}, std::optional<btf_line_info_t>());
×
163
            }
×
164
            label_index++;
2,132✔
165
        }
166
    }
167
    return res;
2,076✔
168
}
1,384✔
169

170
static ebpf_verifier_options_t raw_options_to_options(const std::set<string>& raw_options) {
1,384✔
171
    ebpf_verifier_options_t options{};
1,384✔
172

173
    // Use ~simplify for YAML tests unless otherwise specified.
174
    options.verbosity_opts.simplify = false;
1,384✔
175

176
    // All YAML tests use !setup_constraints.
177
    options.setup_constraints = false;
1,384✔
178

179
    // Default to the machine's native endianness.
180
    options.big_endian = std::endian::native == std::endian::big;
1,384✔
181

182
    // Default to not assuming assertions.
183
    options.assume_assertions = false;
1,384✔
184

185
    // Permit test cases to not have an exit instruction.
186
    options.cfg_opts.must_have_exit = false;
1,384✔
187

188
    for (const string& name : raw_options) {
1,604✔
189
        if (name == "!allow_division_by_zero") {
220✔
190
            options.allow_division_by_zero = false;
32✔
191
        } else if (name == "termination") {
156✔
192
            options.cfg_opts.check_for_termination = true;
17✔
193
        } else if (name == "strict") {
122✔
194
            options.strict = true;
195
        } else if (name == "simplify") {
122✔
196
            options.verbosity_opts.simplify = true;
1✔
197
        } else if (name == "big_endian") {
120✔
198
            options.big_endian = true;
17✔
199
        } else if (name == "!big_endian") {
86✔
200
            options.big_endian = false;
19✔
201
        } else if (name == "assume_assertions") {
48✔
202
            options.assume_assertions = true;
24✔
203
        } else {
204
            throw std::runtime_error("Unknown option: " + name);
×
205
        }
206
    }
207
    return options;
1,384✔
208
}
209

210
static TestCase read_case(const RawTestCase& raw_case) {
1,384✔
211
    return TestCase{.name = raw_case.test_case,
1,384✔
212
                    .options = raw_options_to_options(raw_case.options),
1,384✔
213
                    .assumed_pre_invariant = read_invariant(raw_case.pre),
1,384✔
214
                    .instruction_seq = raw_cfg_to_instruction_seq(raw_case.raw_blocks),
1,384✔
215
                    .expected_post_invariant = read_invariant(raw_case.post),
1,384✔
216
                    .expected_messages = raw_case.messages};
1,384✔
217
}
218

219
static vector<TestCase> read_suite(const string& path) {
48✔
220
    std::ifstream f{path};
48✔
221
    vector<TestCase> res;
48✔
222
    for (const YAML::Node& config : YAML::LoadAll(f)) {
1,432✔
223
        res.push_back(read_case(parse_case(config)));
2,076✔
224
    }
48✔
225
    return res;
72✔
226
}
48✔
227

228
template <typename T>
229
static Diff<T> make_diff(const T& actual, const T& expected) {
×
230
    return Diff<T>{
231
        .unexpected = actual - expected,
232
        .unseen = expected - actual,
233
    };
×
234
}
×
235

236
std::optional<Failure> run_yaml_test_case(TestCase test_case, bool debug) {
1,384✔
237
    test_case.options.verbosity_opts.print_failures = true;
1,384✔
238
    if (debug) {
1,384✔
239
        test_case.options.verbosity_opts.print_invariants = true;
×
240
    }
241

242
    ebpf_context_descriptor_t context_descriptor{64, 0, 4, -1};
1,384✔
243
    EbpfProgramType program_type = make_program_type(test_case.name, &context_descriptor);
1,384✔
244

245
    program_info info{&g_platform_test, {}, program_type};
1,384✔
246
    thread_local_options = test_case.options;
1,384✔
247
    try {
692✔
248
        const Program prog = Program::from_sequence(test_case.instruction_seq, info, test_case.options.cfg_opts);
1,384✔
249
        const Invariants invariants = analyze(prog, test_case.assumed_pre_invariant);
1,376✔
250
        const string_invariant actual_last_invariant = invariants.invariant_at(label_t::exit);
1,376✔
251
        const std::set<string> actual_messages = invariants.check_assertions(prog).all_messages();
1,376✔
252

253
        if (actual_last_invariant == test_case.expected_post_invariant &&
2,064✔
254
            actual_messages == test_case.expected_messages) {
1,376✔
255
            return {};
1,376✔
256
        }
257
        return Failure{
×
258
            .invariant = make_diff(actual_last_invariant, test_case.expected_post_invariant),
×
259
            .messages = make_diff(actual_messages, test_case.expected_messages),
×
260
        };
×
261
    } catch (InvalidControlFlow& ex) {
1,384✔
262
        const std::set<string> actual_messages{ex.what()};
20✔
263
        if (test_case.expected_post_invariant == string_invariant::top() &&
16✔
264
            actual_messages == test_case.expected_messages) {
8✔
265
            return {};
8✔
266
        }
267
        return Failure{
×
268
            .invariant = make_diff(string_invariant::top(), test_case.expected_post_invariant),
×
269
            .messages = make_diff(actual_messages, test_case.expected_messages),
×
270
        };
×
271
    }
8✔
272
}
2,096✔
273

274
template <typename T>
275
    requires std::is_trivially_copyable_v<T>
276
static vector<T> vector_of(const std::vector<std::byte>& bytes) {
438✔
277
    auto data = bytes.data();
438✔
278
    const auto size = bytes.size();
438✔
279
    if (size % sizeof(T) != 0 || size > std::numeric_limits<uint32_t>::max() || !data) {
438✔
280
        throw std::runtime_error("Invalid argument to vector_of");
×
281
    }
282
    return {reinterpret_cast<const T*>(data), reinterpret_cast<const T*>(data + size)};
876✔
283
}
284

285
template <std::signed_integral TS>
286
void add_stack_variable(std::set<std::string>& more, int& offset, const std::vector<std::byte>& memory_bytes) {
132✔
287
    using TU = std::make_unsigned_t<TS>;
288
    constexpr size_t size = sizeof(TS);
132✔
289
    static_assert(sizeof(TU) == size);
290
    const auto src = memory_bytes.data() + offset + memory_bytes.size() - EBPF_TOTAL_STACK_SIZE;
132✔
291
    TS svalue;
292
    std::memcpy(&svalue, src, size);
132✔
293
    TU uvalue;
294
    std::memcpy(&uvalue, src, size);
132✔
295
    const auto range = "s[" + std::to_string(offset) + "..." + std::to_string(offset + size - 1) + "]";
264✔
296
    more.insert(range + ".svalue=" + std::to_string(svalue));
198✔
297
    more.insert(range + ".uvalue=" + std::to_string(uvalue));
198✔
298
    offset += size;
132✔
299
}
132✔
300

301
string_invariant stack_contents_invariant(const std::vector<std::byte>& memory_bytes) {
76✔
302
    std::set<std::string> more = {"r1.type=stack",
38✔
303
                                  "r1.stack_offset=" + std::to_string(EBPF_TOTAL_STACK_SIZE - memory_bytes.size()),
114✔
304
                                  "r1.stack_numeric_size=" + std::to_string(memory_bytes.size()),
152✔
305
                                  "r10.type=stack",
306
                                  "r10.stack_offset=" + std::to_string(EBPF_TOTAL_STACK_SIZE),
114✔
307
                                  "s[" + std::to_string(EBPF_TOTAL_STACK_SIZE - memory_bytes.size()) + "..." +
190✔
308
                                      std::to_string(EBPF_TOTAL_STACK_SIZE - 1) + "].type=number"};
722✔
309

310
    int offset = EBPF_TOTAL_STACK_SIZE - gsl::narrow<int>(memory_bytes.size());
114✔
311
    if (offset % 2 != 0) {
76✔
312
        add_stack_variable<int8_t>(more, offset, memory_bytes);
6✔
313
    }
314
    if (offset % 4 != 0) {
76✔
315
        add_stack_variable<int16_t>(more, offset, memory_bytes);
20✔
316
    }
317
    if (offset % 8 != 0) {
76✔
318
        add_stack_variable<int32_t>(more, offset, memory_bytes);
26✔
319
    }
320
    while (offset < EBPF_TOTAL_STACK_SIZE) {
156✔
321
        add_stack_variable<int64_t>(more, offset, memory_bytes);
80✔
322
    }
323

324
    return string_invariant(more);
114✔
325
}
456✔
326

327
ConformanceTestResult run_conformance_test_case(const std::vector<std::byte>& memory_bytes,
438✔
328
                                                const std::vector<std::byte>& program_bytes, bool debug) {
329
    ebpf_context_descriptor_t context_descriptor{64, -1, -1, -1};
438✔
330
    EbpfProgramType program_type = make_program_type("conformance_check", &context_descriptor);
657✔
331

332
    program_info info{&g_platform_test, {}, program_type};
438✔
333

334
    auto insts = vector_of<ebpf_inst>(program_bytes);
438✔
335
    string_invariant pre_invariant = string_invariant::top();
438✔
336

337
    if (!memory_bytes.empty()) {
438✔
338
        if (memory_bytes.size() > EBPF_TOTAL_STACK_SIZE) {
76✔
339
            std::cerr << "memory size overflow\n";
×
340
            return {};
×
341
        }
342
        pre_invariant = pre_invariant + stack_contents_invariant(memory_bytes);
114✔
343
    }
344
    raw_program raw_prog{.prog = insts};
438✔
345
    ebpf_platform_t platform = g_ebpf_platform_linux;
438✔
346
    platform.supported_conformance_groups |= bpf_conformance_groups_t::callx;
438✔
347
    raw_prog.info.platform = &platform;
438✔
348

349
    // Convert the raw program section to a set of instructions.
350
    std::variant<InstructionSeq, std::string> prog_or_error = unmarshal(raw_prog);
438✔
351
    if (auto prog = std::get_if<std::string>(&prog_or_error)) {
438✔
352
        std::cerr << "unmarshaling error at " << *prog << "\n";
×
353
        return {};
×
354
    }
355

356
    const InstructionSeq& inst_seq = std::get<InstructionSeq>(prog_or_error);
438✔
357

358
    ebpf_verifier_options_t options{};
438✔
359
    if (debug) {
438✔
360
        print(inst_seq, std::cout, {});
×
361
        options.verbosity_opts.print_failures = true;
×
362
        options.verbosity_opts.print_invariants = true;
×
363
        options.verbosity_opts.simplify = false;
×
364
    }
365
    thread_local_options = options;
438✔
366

367
    try {
219✔
368
        const Program prog = Program::from_sequence(inst_seq, info, options.cfg_opts);
438✔
369
        const Invariants invariants = analyze(prog, pre_invariant);
438✔
370
        return ConformanceTestResult{.success = invariants.verified(prog), .r0_value = invariants.exit_value()};
438✔
371
    } catch (const std::exception&) {
438✔
372
        // Catch exceptions thrown in ebpf_domain.cpp.
373
        return {};
×
374
    }
×
375
}
876✔
376

377
void print_failure(const Failure& failure, std::ostream& os) {
×
378
    constexpr auto INDENT = "  ";
×
379
    if (!failure.invariant.unexpected.empty()) {
×
380
        os << "Unexpected properties:\n" << INDENT << failure.invariant.unexpected << "\n";
×
381
    } else {
382
        os << "Unexpected properties: None\n";
×
383
    }
384
    if (!failure.invariant.unseen.empty()) {
×
385
        os << "Unseen properties:\n" << INDENT << failure.invariant.unseen << "\n";
×
386
    } else {
387
        os << "Unseen properties: None\n";
×
388
    }
389

390
    if (!failure.messages.unexpected.empty()) {
×
391
        os << "Unexpected messages:\n";
×
392
        for (const auto& item : failure.messages.unexpected) {
×
393
            os << INDENT << item << "\n";
×
394
        }
395
    } else {
396
        os << "Unexpected messages: None\n";
×
397
    }
398

399
    if (!failure.messages.unseen.empty()) {
×
400
        os << "Unseen messages:\n";
×
401
        for (const auto& item : failure.messages.unseen) {
×
402
            os << INDENT << item << "\n";
×
403
        }
404
    } else {
405
        os << "Unseen messages: None\n";
×
406
    }
407
}
×
408

409
bool all_suites(const string& path) {
×
410
    bool result = true;
×
411
    for (const TestCase& test_case : read_suite(path)) {
×
412
        result = result && static_cast<bool>(run_yaml_test_case(test_case));
×
413
    }
×
414
    return result;
×
415
}
416

417
void foreach_suite(const string& path, const std::function<void(const TestCase&)>& f) {
48✔
418
    for (const TestCase& test_case : read_suite(path)) {
1,432✔
419
        f(test_case);
1,384✔
420
    }
48✔
421
}
48✔
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