• 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

86.83
/src/asm_files.cpp
1
// Copyright (c) Prevail Verifier contributors.
2
// SPDX-License-Identifier: MIT
3
#include <iostream>
4
#include <map>
5
#include <set>
6
#include <string>
7
#include <sys/stat.h>
8
#include <vector>
9

10
#include "elfio/elfio.hpp"
11
#include "libbtf/btf_json.h"
12
#include "libbtf/btf_map.h"
13
#include "libbtf/btf_parse.h"
14

15
#include "asm_files.hpp"
16
#include "crab_utils/num_safety.hpp"
17
#include "platform.hpp"
18

19
namespace prevail {
20

21
template <typename T>
22
    requires std::is_trivially_copyable_v<T>
23
static std::vector<T> vector_of(const char* data, const ELFIO::Elf_Xword size) {
7,604✔
24
    if (size % sizeof(T) != 0 || size > std::numeric_limits<uint32_t>::max() || !data) {
7,604✔
UNCOV
25
        throw UnmarshalError("Invalid argument to vector_of");
×
26
    }
27
    return {reinterpret_cast<const T*>(data), reinterpret_cast<const T*>(data + size)};
15,208✔
28
}
29

30
template <typename T>
31
    requires std::is_trivially_copyable_v<T>
32
static std::vector<T> vector_of(const ELFIO::section& sec) {
488✔
33
    return vector_of<T>(sec.get_data(), sec.get_size());
488✔
34
}
35

36
int create_map_crab(const EbpfMapType& map_type, const uint32_t key_size, const uint32_t value_size,
5,800✔
37
                    const uint32_t max_entries, ebpf_verifier_options_t) {
38
    const EquivalenceKey equiv{map_type.value_type, key_size, value_size, map_type.is_array ? max_entries : 0};
5,800✔
39
    if (!thread_local_program_info->cache.contains(equiv)) {
5,800✔
40
        // +1 so 0 is the null FD
41
        thread_local_program_info->cache[equiv] = gsl::narrow<int>(thread_local_program_info->cache.size()) + 1;
5,022✔
42
    }
43
    return thread_local_program_info->cache.at(equiv);
8,700✔
44
}
45

46
EbpfMapDescriptor* find_map_descriptor(const int map_fd) {
25,490✔
47
    for (EbpfMapDescriptor& map : thread_local_program_info->map_descriptors) {
234,290✔
48
        if (map.original_fd == map_fd) {
234,288✔
49
            return &map;
25,488✔
50
        }
51
    }
52
    return nullptr;
1✔
53
}
54

55
// Maps sections are identified as any section called "maps", or matching "maps/<map-name>".
56
static bool is_map_section(const std::string& name) {
38,954✔
57
    const std::string maps_prefix = "maps/";
38,954✔
58
    return name == "maps" || (name.length() > 5 && name.compare(0, maps_prefix.length(), maps_prefix) == 0);
77,908✔
59
}
38,954✔
60

61
struct symbol_details_t {
336,343✔
62
    std::string name;
63
    ELFIO::Elf64_Addr value{}; // also relocation offset
64
    ELFIO::Elf_Xword size{};
65
    unsigned char bind{};
66
    unsigned char type{};
67
    ELFIO::Elf_Half section_index{};
68
    unsigned char other{};
69
};
70

71
static symbol_details_t get_symbol_details(const ELFIO::const_symbol_section_accessor& symbols,
30,441,468✔
72
                                           const ELFIO::Elf_Xword index) {
73
    symbol_details_t details;
30,441,468✔
74
    symbols.get_symbol(index, details.name, details.value, details.size, details.bind, details.type,
30,441,468✔
75
                       details.section_index, details.other);
30,441,468✔
76
    return details;
30,441,468✔
UNCOV
77
}
×
78

79
struct parse_params_t {
3✔
80
    const std::string& path;
81
    const ebpf_verifier_options_t& options;
82
    const ebpf_platform_t* platform;
83
    const std::string desired_section;
84
};
85

86
std::vector<RawProgram> read_elf(const std::string& path, const std::string& desired_section,
756✔
87
                                 const ebpf_verifier_options_t& options, const ebpf_platform_t* platform) {
88
    if (std::ifstream stream{path, std::ios::in | std::ios::binary}) {
756✔
89
        return read_elf(stream, path, desired_section, options, platform);
1,502✔
90
    }
756✔
91
    struct stat st;
1✔
92
    if (stat(path.c_str(), &st)) {
2✔
93
        throw UnmarshalError(std::string(strerror(errno)) + " opening " + path);
6✔
94
    }
UNCOV
95
    throw UnmarshalError("Can't process ELF file " + path);
×
96
}
97

98
static std::tuple<std::string, ELFIO::Elf_Xword>
99
get_program_name_and_size(const ELFIO::section& sec, const ELFIO::Elf_Xword start,
7,116✔
100
                          const ELFIO::const_symbol_section_accessor& symbols) {
101
    const ELFIO::Elf_Xword symbol_count = symbols.get_symbols_num();
7,116✔
102
    const ELFIO::Elf_Half section_index = sec.get_index();
7,116✔
103
    std::string program_name = sec.get_name();
7,116✔
104
    ELFIO::Elf_Xword size = sec.get_size() - start;
7,116✔
105
    for (ELFIO::Elf_Xword index = 0; index < symbol_count; index++) {
29,619,048✔
106
        auto symbol_details = get_symbol_details(symbols, index);
29,611,932✔
107
        if (symbol_details.section_index == section_index && !symbol_details.name.empty()) {
29,611,932✔
108
            if (symbol_details.type != ELFIO::STT_FUNC) {
258,322✔
109
                continue;
255,140✔
110
            }
111
            const auto relocation_offset = symbol_details.value;
3,182✔
112
            if (relocation_offset == start) {
3,182✔
113
                // We found the program name for this program.
114
                program_name = symbol_details.name;
14,681,366✔
115
            } else if (relocation_offset > start && relocation_offset < start + size) {
212✔
116
                // We found another program that follows, so truncate the size of this program.
117
                size = relocation_offset - start;
76✔
118
            }
119
        }
120
    }
29,611,932✔
121
    return {program_name, size};
14,232✔
122
}
7,116✔
123

UNCOV
124
static std::string bad_reloc_value(const size_t reloc_value) {
×
UNCOV
125
    return "Bad reloc value (" + std::to_string(reloc_value) + "). " + "Make sure to compile with -O2.";
×
126
}
127

128
// Structure used to keep track of subprogram relocation data until any subprograms
129
// are loaded and can be appended to the calling program.
130
struct FunctionRelocation {
68,676✔
131
    size_t prog_index{};              // Index of source program in std::vector of raw programs.
132
    ELFIO::Elf_Xword source_offset{}; // Instruction offset in source section of source instruction.
133
    ELFIO::Elf_Xword relocation_entry_index{};
134
    std::string target_function_name;
135
};
136

137
static RawProgram* find_subprogram(std::vector<RawProgram>& programs, const ELFIO::section& subprogram_section,
1,958✔
138
                                   const std::string& symbol_name) {
139
    // Find subprogram by name.
140
    for (auto& subprog : programs) {
186,646✔
141
        if (subprog.section_name == subprogram_section.get_name() && subprog.function_name == symbol_name) {
277,050✔
142
            return &subprog;
12✔
143
        }
144
    }
145
    return nullptr;
973✔
146
}
147

148
using MapOffsets = std::map<std::string, size_t>;
149

150
struct elf_global_data {
340✔
151
    std::set<ELFIO::Elf_Half> map_section_indices;
152
    std::vector<EbpfMapDescriptor> map_descriptors;
153
    std::variant<size_t, MapOffsets> map_record_size_or_map_offsets;
154
    std::set<ELFIO::Elf_Half> variable_section_indices;
155
};
156

157
static std::map<int, int> map_typeid_to_fd(const std::vector<EbpfMapDescriptor>& map_descriptors) {
116✔
158
    std::map<int, int> type_id_to_fd_map;
116✔
159
    int pseudo_fd = 1;
116✔
160
    // Gather the typeids for each map and assign a pseudo-fd to each map.
161
    for (const auto& map_descriptor : map_descriptors) {
208✔
162
        if (!type_id_to_fd_map.contains(map_descriptor.original_fd)) {
92✔
163
            type_id_to_fd_map[map_descriptor.original_fd] = pseudo_fd++;
92✔
164
        }
165
    }
166
    return type_id_to_fd_map;
116✔
UNCOV
167
}
×
168

169
static ELFIO::const_symbol_section_accessor read_and_validate_symbol_section(const ELFIO::elfio& reader,
754✔
170
                                                                             const std::string& path) {
171
    const ELFIO::section* symbol_section = reader.sections[".symtab"];
754✔
172
    if (!symbol_section) {
754✔
UNCOV
173
        throw UnmarshalError("No symbol section found in ELF file " + path);
×
174
    }
175

176
    // Make sure the ELFIO library will be able to parse the symbol section correctly.
177
    const auto expected_entry_size =
377✔
178
        reader.get_class() == ELFIO::ELFCLASS32 ? sizeof(ELFIO::Elf32_Sym) : sizeof(ELFIO::Elf64_Sym);
754✔
179

180
    if (symbol_section->get_entry_size() != expected_entry_size) {
754✔
181
        throw UnmarshalError("Invalid symbol section found in ELF file " + path);
3✔
182
    }
183
    return ELFIO::const_symbol_section_accessor{reader, symbol_section};
752✔
184
}
185

186
static ELFIO::elfio load_elf(std::istream& input_stream, const std::string& path) {
754✔
187
    ELFIO::elfio reader;
754✔
188
    if (!reader.load(input_stream)) {
754✔
UNCOV
189
        throw UnmarshalError("Can't process ELF file " + path);
×
190
    }
191
    return reader;
754✔
192
}
×
193

194
static void dump_btf_types(const libbtf::btf_type_data& btf_data, const std::string& path) {
×
UNCOV
195
    std::stringstream output;
×
196
    std::cout << "Dumping BTF data for" << path << std::endl;
×
197
    // Dump the BTF data to stdout for debugging purposes.
198
    btf_data.to_json(output);
×
199
    std::cout << libbtf::pretty_print_json(output.str()) << std::endl;
×
UNCOV
200
    std::cout << std::endl;
×
UNCOV
201
}
×
202

203
static void update_line_info(std::vector<RawProgram>& raw_programs, const ELFIO::section* btf_section,
186✔
204
                             const ELFIO::section* btf_ext) {
205
    auto visitor = [&raw_programs](const std::string& section, const uint32_t instruction_offset,
1,256,823✔
206
                                   const std::string& file_name, const std::string& source, const uint32_t line_number,
207
                                   const uint32_t column_number) {
208
        for (auto& program : raw_programs) {
61,377,566✔
209
            if (program.section_name == section && instruction_offset >= program.insn_off &&
60,749,352✔
210
                instruction_offset < program.insn_off + program.prog.size() * sizeof(EbpfInst)) {
1,257,032✔
211
                const size_t inst_index = (instruction_offset - program.insn_off) / sizeof(EbpfInst);
1,256,730✔
212
                if (inst_index >= program.prog.size()) {
1,256,730✔
UNCOV
213
                    throw UnmarshalError("Invalid BTF data");
×
214
                }
215
                program.info.line_info.insert_or_assign(inst_index,
628,365✔
216
                                                        btf_line_info_t{file_name, source, line_number, column_number});
1,885,095✔
217
            }
218
        }
219
    };
1,256,730✔
220

221
    libbtf::btf_parse_line_information(vector_of<std::byte>(*btf_section), vector_of<std::byte>(*btf_ext), visitor);
279✔
222

223
    // BTF doesn't include line info for every instruction, only on the first instruction per source line.
224
    for (auto& program : raw_programs) {
3,154✔
225
        for (size_t i = 1; i < program.prog.size(); i++) {
4,884,832✔
226
            // If the previous PC has line info, copy it.
227
            if (program.info.line_info[i].line_number == 0 && program.info.line_info[i - 1].line_number != 0) {
4,881,864✔
228
                program.info.line_info[i] = program.info.line_info[i - 1];
3,782,354✔
229
            }
230
        }
231
    }
232
}
186✔
233

234
static elf_global_data parse_btf_section(const parse_params_t& parse_params, const ELFIO::elfio& reader) {
188✔
235
    const auto btf_section = reader.sections[".BTF"];
188✔
236
    if (!btf_section) {
188✔
237
        return {};
72✔
238
    }
239
    const libbtf::btf_type_data btf_data = vector_of<std::byte>(*btf_section);
116✔
240
    if (parse_params.options.verbosity_opts.dump_btf_types_json) {
116✔
UNCOV
241
        dump_btf_types(btf_data, parse_params.path);
×
242
    }
243

244
    elf_global_data global;
116✔
245

246
    {
58✔
247
        MapOffsets map_offsets;
116✔
248
        for (const auto& map : parse_btf_map_section(btf_data)) {
208✔
249
            map_offsets.emplace(map.name, global.map_descriptors.size());
92✔
250
            global.map_descriptors.push_back({
92✔
251
                .original_fd = gsl::narrow_cast<int>(map.type_id),
92✔
252
                .type = map.map_type,
92✔
253
                .key_size = map.key_size,
92✔
254
                .value_size = map.value_size,
92✔
255
                .max_entries = map.max_entries,
92✔
256
                .inner_map_fd = map.inner_map_type_id != 0 ? map.inner_map_type_id : DEFAULT_MAP_FD,
92✔
257
            });
258
        }
116✔
259
        global.map_record_size_or_map_offsets = std::move(map_offsets);
116✔
260
    }
58✔
261

262
    {
58✔
263
        // Prevail requires:
264
        // Map fds are sequential starting from 1.
265
        // Map fds are assigned in the order of the maps in the ELF file.
266
        const std::map<int, int> type_id_to_fd_map = map_typeid_to_fd(global.map_descriptors);
116✔
267
        for (auto& map_descriptor : global.map_descriptors) {
208✔
268
            map_descriptor.original_fd = type_id_to_fd_map.at(map_descriptor.original_fd);
92✔
269
            if (map_descriptor.inner_map_fd != DEFAULT_MAP_FD) {
92✔
270
                map_descriptor.inner_map_fd = type_id_to_fd_map.at(map_descriptor.inner_map_fd);
6✔
271
            }
272
        }
273
    }
58✔
274

275
    if (const auto maps_section = reader.sections[".maps"]) {
116✔
276
        global.map_section_indices.insert(maps_section->get_index());
74✔
277
    }
278

279
    for (const auto section_name : {".rodata", ".data", ".bss"}) {
464✔
280
        if (const auto section = reader.sections[section_name]) {
348✔
281
            if (section->get_size() != 0) {
6✔
282
                global.variable_section_indices.insert(section->get_index());
6✔
283
            }
284
        }
285
    }
286
    return global;
116✔
287
}
188✔
288

289
// parse_maps_sections processes all maps sections in the provided ELF file by calling the platform-specific maps'
290
// parser. The section index of each maps section is inserted into map_section_indices.
291
static elf_global_data parse_map_sections(const parse_params_t& parse_params, const ELFIO::elfio& reader,
564✔
292
                                          const ELFIO::const_symbol_section_accessor& symbols) {
293
    elf_global_data global;
564✔
294
    size_t map_record_size = parse_params.platform->map_record_size;
564✔
295
    for (ELFIO::Elf_Half i = 0; i < reader.sections.size(); ++i) {
20,532✔
296
        const auto s = reader.sections[i];
19,968✔
297
        if (!is_map_section(s->get_name())) {
19,968✔
298
            continue;
19,402✔
299
        }
300

301
        // Count the number of symbols that point into this maps section.
302
        int map_count = 0;
283✔
303
        for (ELFIO::Elf_Xword index = 0; index < symbols.get_symbols_num(); index++) {
484,974✔
304
            const auto symbol_details = get_symbol_details(symbols, index);
484,408✔
305
            if (symbol_details.section_index == i && !symbol_details.name.empty()) {
484,408✔
306
                map_count++;
5,800✔
307
            }
308
        }
484,408✔
309

310
        if (map_count > 0) {
566✔
311
            map_record_size = s->get_size() / map_count;
566✔
312
            if (s->get_data() == nullptr || map_record_size == 0) {
566✔
UNCOV
313
                throw UnmarshalError("bad maps section");
×
314
            }
315
            if (s->get_size() % map_record_size != 0) {
566✔
UNCOV
316
                throw UnmarshalError("bad maps section size");
×
317
            }
318
            parse_params.platform->parse_maps_section(global.map_descriptors, s->get_data(), map_record_size, map_count,
566✔
319
                                                      parse_params.platform, parse_params.options);
566✔
320
        }
321
        global.map_section_indices.insert(s->get_index());
566✔
322
    }
323
    parse_params.platform->resolve_inner_map_references(global.map_descriptors);
564✔
324
    global.map_record_size_or_map_offsets = map_record_size;
564✔
325
    return global;
846✔
UNCOV
326
}
×
327

328
static elf_global_data extract_global_data(const parse_params_t& parse_params, const ELFIO::elfio& reader,
752✔
329
                                           const ELFIO::const_symbol_section_accessor& symbols) {
330
    if (std::ranges::any_of(reader.sections, [](const auto& section) { return is_map_section(section->get_name()); })) {
19,738✔
331
        return parse_map_sections(parse_params, reader, symbols);
564✔
332
    }
333
    return parse_btf_section(parse_params, reader);
188✔
334
}
335

336
class ProgramReader {
337
    const parse_params_t& parse_params;
338
    const ELFIO::elfio& reader;
339
    const ELFIO::const_symbol_section_accessor& symbols;
340
    const elf_global_data& global;
341

342
    std::vector<FunctionRelocation> function_relocations;
343
    std::vector<std::string> unresolved_symbol_errors;
344
    std::map<const RawProgram*, bool> resolved_subprograms;
345

346
  public:
347
    std::vector<RawProgram> raw_programs;
348

349
    ProgramReader(const parse_params_t& parse_params, const ELFIO::elfio& reader,
752✔
350
                  const ELFIO::const_symbol_section_accessor& symbols, const elf_global_data& global)
351
        : parse_params{parse_params}, reader{reader}, symbols{symbols}, global{global} {}
752✔
352

353
    // Returns an error message, or empty string on success.
354
    std::string append_subprograms(RawProgram& prog) {
7,128✔
355
        if (resolved_subprograms[&prog]) {
7,128✔
356
            // We've already appended any relevant subprograms.
357
            return {};
12✔
358
        }
359
        resolved_subprograms[&prog] = true;
7,116✔
360

361
        // Perform function relocations and fill in the inst.imm values of CallLocal instructions.
362
        std::map<std::string, ELFIO::Elf_Xword> subprogram_offsets;
7,116✔
363
        for (const auto& reloc : function_relocations) {
1,880,892✔
364
            if (reloc.prog_index >= raw_programs.size()) {
1,875,722✔
365
                continue;
1,873,764✔
366
            }
367
            if (raw_programs[reloc.prog_index].function_name != prog.function_name) {
1,875,722✔
368
                continue;
1,873,752✔
369
            }
370

371
            // Check whether we already appended the target program, and append it if not.
372
            if (!subprogram_offsets.contains(reloc.target_function_name)) {
1,970✔
373
                subprogram_offsets[reloc.target_function_name] = prog.prog.size();
1,958✔
374

375
                const auto symbol_details = get_symbol_details(symbols, reloc.relocation_entry_index);
1,958✔
376
                if (symbol_details.section_index >= reader.sections.size()) {
1,958✔
UNCOV
377
                    throw UnmarshalError("Invalid section index " + std::to_string(symbol_details.section_index) +
×
UNCOV
378
                                         " at source offset " + std::to_string(reloc.source_offset));
×
379
                }
380
                const ELFIO::section& subprogram_section = *reader.sections[symbol_details.section_index];
1,958✔
381

382
                if (const auto subprogram = find_subprogram(raw_programs, subprogram_section, symbol_details.name)) {
1,958✔
383
                    // Don't permit recursive subprograms.
384
                    if (subprogram == &prog) {
12✔
UNCOV
385
                        throw UnmarshalError("Subprogram '" + symbol_details.name +
×
UNCOV
386
                                             "' is the same as the calling program");
×
387
                    }
388

389
                    // Make sure subprogram has already had any subprograms of its own appended.
390
                    std::string error = append_subprograms(*subprogram);
12✔
391
                    if (!error.empty()) {
12✔
UNCOV
392
                        return error;
×
393
                    }
394

395
                    // Append subprogram to program.
396
                    prog.prog.insert(prog.prog.end(), subprogram->prog.begin(), subprogram->prog.end());
12✔
397
                    for (size_t i = 0; i < subprogram->info.line_info.size(); i++) {
216✔
398
                        prog.info.line_info[prog.info.line_info.size()] = subprogram->info.line_info[i];
204✔
399
                    }
400
                } else {
12✔
401
                    // The program will be invalid, but continue rather than throwing an exception
402
                    // since we might be verifying a different program in the file.
403
                    return std::string("Subprogram '" + symbol_details.name + "' not found in section '" +
4,865✔
404
                                       subprogram_section.get_name() + "'");
5,838✔
405
                }
406
            }
1,958✔
407

408
            // Fill in the PC offset into the imm field of the CallLocal instruction.
409
            const int64_t target_offset = gsl::narrow_cast<int64_t>(subprogram_offsets[reloc.target_function_name]);
24✔
410
            const auto offset_diff = target_offset - gsl::narrow<int64_t>(reloc.source_offset) - 1;
24✔
411
            if (offset_diff < std::numeric_limits<int32_t>::min() ||
36✔
412
                offset_diff > std::numeric_limits<int32_t>::max()) {
12✔
UNCOV
413
                throw UnmarshalError("Offset difference out of int32_t range for instruction at source offset " +
×
UNCOV
414
                                     std::to_string(reloc.source_offset));
×
415
            }
416
            prog.prog[reloc.source_offset].imm = gsl::narrow_cast<int32_t>(offset_diff);
24✔
417
        }
418
        return {};
5,170✔
419
    }
7,116✔
420

421
    int relocate_map(const std::string& symbol_name, const ELFIO::Elf_Word index) const {
156,922✔
422
        // Relocation value is an offset into the "maps" or ".maps" section.
423
        size_t reloc_value{};
156,922✔
424
        if (const auto* map_record_size = std::get_if<size_t>(&global.map_record_size_or_map_offsets)) {
156,922✔
425
            // The older maps section format uses a single map_record_size value,
426
            // so we can calculate the map descriptor index directly.
427
            const auto symbol_details = get_symbol_details(symbols, index);
156,838✔
428
            const auto relocation_offset = symbol_details.value;
156,838✔
429
            reloc_value = relocation_offset / *map_record_size;
156,838✔
430
        } else {
156,838✔
431
            // The newer .maps section format uses a variable-length map descriptor array,
432
            // so we need to look up the map descriptor index in a map.
433
            const auto& map_descriptors_offsets = std::get<MapOffsets>(global.map_record_size_or_map_offsets);
84✔
434
            const auto it = map_descriptors_offsets.find(symbol_name);
84✔
435
            if (it == map_descriptors_offsets.end()) {
84✔
UNCOV
436
                throw UnmarshalError("Map descriptor not found for symbol " + symbol_name);
×
437
            }
438
            reloc_value = it->second;
84✔
439
        }
440
        if (reloc_value >= global.map_descriptors.size()) {
156,922✔
UNCOV
441
            throw UnmarshalError(bad_reloc_value(reloc_value));
×
442
        }
443
        return global.map_descriptors.at(reloc_value).original_fd;
156,922✔
444
    }
445

446
    int relocate_global_variable(const std::string& symbol_name) const {
8✔
447
        const auto map_descriptors_offsets = std::get_if<MapOffsets>(&global.map_record_size_or_map_offsets);
8✔
448
        if (!map_descriptors_offsets) {
8✔
UNCOV
449
            throw UnmarshalError("Invalid map_offsets");
×
450
        }
451
        const auto it = map_descriptors_offsets->find(symbol_name);
8✔
452
        if (it == map_descriptors_offsets->end()) {
8✔
UNCOV
453
            throw UnmarshalError("Map descriptor not found for symbol " + symbol_name);
×
454
        }
455
        const size_t reloc_value = it->second;
8✔
456
        if (reloc_value >= global.map_descriptors.size()) {
8✔
UNCOV
457
            throw UnmarshalError(bad_reloc_value(reloc_value));
×
458
        }
459
        return global.map_descriptors.at(reloc_value).original_fd;
12✔
460
    }
461

462
    bool try_reloc(const std::string& symbol_name, const ELFIO::Elf_Half symbol_section_index,
186,332✔
463
                   std::vector<EbpfInst>& instructions, const size_t location, const ELFIO::Elf_Word index) {
464
        EbpfInst& instruction_to_relocate = instructions[location];
186,332✔
465

466
        // Queue up relocation for function symbols.
467
        if (instruction_to_relocate.opcode == INST_OP_CALL && instruction_to_relocate.src == INST_CALL_LOCAL) {
186,332✔
468
            function_relocations.emplace_back(FunctionRelocation{
29,402✔
469
                .prog_index = raw_programs.size(),
29,402✔
470
                .source_offset = location,
471
                .relocation_entry_index = index,
29,402✔
472
                .target_function_name = symbol_name,
473
            });
474
            return true;
29,402✔
475
        }
476

477
        // Verify that this is a map or global variable relocation.
478
        if ((instruction_to_relocate.opcode & INST_CLS_MASK) != INST_CLS_LD) {
156,930✔
UNCOV
479
            throw UnmarshalError("Illegal operation on symbol " + symbol_name + " at location " +
×
UNCOV
480
                                 std::to_string(location));
×
481
        }
482

483
        // Perform relocation for symbols located in the maps section.
484
        if (global.map_section_indices.contains(symbol_section_index)) {
156,930✔
485
            instruction_to_relocate.src = INST_LD_MODE_MAP_FD;
156,922✔
486
            instruction_to_relocate.imm = relocate_map(symbol_name, index);
156,922✔
487
            return true;
156,922✔
488
        }
489

490
        if (global.variable_section_indices.contains(symbol_section_index)) {
8✔
491
            // Load instructions are two instructions long, so we need to check the next instruction.
492
            if (instructions.size() <= location + 1) {
8✔
UNCOV
493
                throw UnmarshalError("Invalid relocation data");
×
494
            }
495
            // Copy the immediate value to the next instruction.
496
            instructions[location + 1].imm = instruction_to_relocate.imm;
8✔
497
            instruction_to_relocate.src = INST_LD_MODE_MAP_VALUE;
8✔
498
            instruction_to_relocate.imm = relocate_global_variable(reader.sections[symbol_section_index]->get_name());
12✔
499
            return true;
8✔
500
        }
501
        return false;
502
    }
503

504
    void process_relocations(std::vector<EbpfInst>& instructions, const ELFIO::const_relocation_section_accessor& reloc,
6,676✔
505
                             const std::string& section_name, const ELFIO::Elf_Xword program_offset,
506
                             const size_t program_size) {
507
        for (ELFIO::Elf_Xword i = 0; i < reloc.get_entries_num(); i++) {
193,060✔
508
            ELFIO::Elf64_Addr offset{};
186,384✔
509
            ELFIO::Elf_Word index{};
186,384✔
510
            unsigned type{};
186,384✔
511
            ELFIO::Elf_Sxword addend{};
186,384✔
512
            if (reloc.get_entry(i, offset, index, type, addend)) {
186,384✔
513
                if (offset < program_offset || offset >= program_offset + program_size) {
186,384✔
514
                    // Relocation is not for this program.
515
                    continue;
52✔
516
                }
517
                offset -= program_offset;
186,332✔
518
                const unsigned long location = offset / sizeof(EbpfInst);
186,332✔
519
                if (location >= instructions.size()) {
186,332✔
UNCOV
520
                    throw UnmarshalError("Invalid relocation data");
×
521
                }
522

523
                const auto symbol_details = get_symbol_details(symbols, index);
186,332✔
524
                if (!try_reloc(symbol_details.name, symbol_details.section_index, instructions, location, index)) {
186,332✔
525
                    unresolved_symbol_errors.push_back("Unresolved external symbol " + symbol_details.name +
×
UNCOV
526
                                                       " in section " + section_name + " at location " +
×
UNCOV
527
                                                       std::to_string(location));
×
528
                }
529
            }
186,332✔
530
        }
531
    }
6,676✔
532

533
    const ELFIO::section* get_relocation_section(const std::string& section_name) const {
7,116✔
534
        const ELFIO::section* prelocs = reader.sections[".rel" + section_name];
7,116✔
535
        if (!prelocs) {
7,116✔
536
            prelocs = reader.sections[".rela" + section_name];
1,070✔
537
        }
538
        if (!prelocs) {
4,093✔
539
            return nullptr;
220✔
540
        }
541
        if (!prelocs->get_data()) {
6,676✔
UNCOV
542
            throw UnmarshalError("Malformed relocation data");
×
543
        }
544
        return prelocs;
3,338✔
545
    }
546

547
    void read_programs() {
752✔
548
        for (const auto& section : reader.sections) {
24,200✔
549
            if (!(section->get_flags() & ELFIO::SHF_EXECINSTR)) {
23,448✔
550
                // Section does not contain executable instructions.
551
                continue;
16,378✔
552
            }
553
            const auto section_size = section->get_size();
7,738✔
554
            if (section_size == 0) {
7,738✔
555
                continue;
668✔
556
            }
557
            const auto section_data = section->get_data();
7,070✔
558
            if (section_data == nullptr) {
7,070✔
UNCOV
559
                continue;
×
560
            }
561
            const std::string section_name = section->get_name();
7,070✔
562
            const EbpfProgramType program_type =
3,535✔
563
                parse_params.platform->get_program_type(section_name, parse_params.path);
7,070✔
564

565
            for (ELFIO::Elf_Xword program_offset = 0; program_offset < section_size;) {
14,186✔
566
                auto [program_name, program_size] = get_program_name_and_size(*section, program_offset, symbols);
7,116✔
567
                std::vector<EbpfInst> instructions = vector_of<EbpfInst>(section_data + program_offset, program_size);
7,116✔
568

569
                if (const ELFIO::section* reloc_section = get_relocation_section(section_name)) {
7,116✔
570
                    const ELFIO::const_relocation_section_accessor reloc{reader, reloc_section};
6,676✔
571
                    process_relocations(instructions, reloc, section_name, program_offset, program_size);
6,676✔
572
                }
573

574
                raw_programs.emplace_back(RawProgram{
17,790✔
575
                    parse_params.path,
7,116✔
576
                    section_name,
577
                    gsl::narrow_cast<uint32_t>(program_offset),
7,116✔
578
                    program_name,
579
                    std::move(instructions),
3,558✔
580
                    ProgramInfo{
581
                        .platform = parse_params.platform,
7,116✔
582
                        .map_descriptors = global.map_descriptors,
7,116✔
583
                        .type = program_type,
584
                    },
585
                });
586
                program_offset += program_size;
7,116✔
587
            }
7,116✔
588
        }
7,070✔
589

590
        // Below, only relocations of symbols located in the maps sections are allowed,
591
        // so if there are relocations there needs to be a maps section.
592
        if (!unresolved_symbol_errors.empty()) {
752✔
UNCOV
593
            for (const auto& unresolved_symbol : unresolved_symbol_errors) {
×
594
                std::cerr << unresolved_symbol << std::endl;
×
595
            }
UNCOV
596
            throw UnmarshalError("There are relocations in section but no maps sections in file " + parse_params.path +
×
UNCOV
597
                                 "\nMake sure to inline all function calls.");
×
598
        }
599

600
        if (const auto btf_section = reader.sections[".BTF"]) {
752✔
601
            if (const auto btf_ext = reader.sections[".BTF.ext"]) {
186✔
602
                update_line_info(raw_programs, btf_section, btf_ext);
186✔
603
            }
604
        }
605

606
        // Now that we have all programs in the list, we can recursively append any subprograms
607
        // to the calling programs.  We have to keep them as programs themselves in case the caller
608
        // wants to verify them separately, but we also have to append them if used as subprograms to
609
        // allow the caller to be fully verified since inst.imm can only point into the same program.
610
        for (auto& prog : raw_programs) {
7,866✔
611
            std::string error = append_subprograms(prog);
7,116✔
612
            if (!error.empty()) {
7,116✔
613
                if (prog.section_name == parse_params.desired_section) {
1,946✔
614
                    throw UnmarshalError(error);
2✔
615
                }
616
            }
617
        }
7,116✔
618

619
        // Now that we've incorporated any subprograms from other sections, we can narrow the list
620
        // to return to just those programs in the desired section, if any.
621
        if (!parse_params.desired_section.empty() && !raw_programs.empty()) {
750✔
622
            for (int index = raw_programs.size() - 1; index >= 0; index--) {
7,784✔
623
                if (raw_programs[index].section_name != parse_params.desired_section) {
7,072✔
624
                    raw_programs.erase(raw_programs.begin() + index);
6,318✔
625
                }
626
            }
627
        }
628

629
        if (raw_programs.empty()) {
750✔
630
            if (parse_params.desired_section.empty()) {
2✔
UNCOV
631
                throw UnmarshalError("Can't find any non-empty TEXT sections in file " + parse_params.path);
×
632
            }
633
            throw UnmarshalError("Can't find section " + parse_params.desired_section + " in file " +
4✔
634
                                 parse_params.path);
6✔
635
        }
636
    }
748✔
637
};
638

639
std::vector<RawProgram> read_elf(std::istream& input_stream, const std::string& path,
754✔
640
                                 const std::string& desired_section, const ebpf_verifier_options_t& options,
641
                                 const ebpf_platform_t* platform) {
642
    const parse_params_t parse_params{
377✔
643
        .path = path, .options = options, .platform = platform, .desired_section = desired_section};
754✔
644
    const ELFIO::elfio reader = load_elf(input_stream, path);
754✔
645
    const ELFIO::const_symbol_section_accessor symbols = read_and_validate_symbol_section(reader, path);
754✔
646
    const elf_global_data global = extract_global_data(parse_params, reader, symbols);
752✔
647
    ProgramReader program_reader{parse_params, reader, symbols, global};
752✔
648
    program_reader.read_programs();
752✔
649
    return std::move(program_reader.raw_programs);
1,122✔
650
}
765✔
651
} // 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