Skip to content

Commit

Permalink
Refactor libbtf into it's own repo for re-use
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Alan-Jowett authored and elazarg committed Jun 15, 2023
1 parent 2ccf873 commit eb95b83
Showing 7 changed files with 29 additions and 1,895 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -10,3 +10,6 @@
[submodule "external/bpf_conformance"]
path = external/bpf_conformance
url = https://github.com/Alan-Jowett/bpf_conformance.git
[submodule "external/libbtf"]
path = external/libbtf
url = https://github.com/Alan-Jowett/libbtf.git
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ endif ()
include_directories(./external)
include_directories(./external/ELFIO)
include_directories(./src)
include_directories(./external/libbtf)
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${YAML_CPP_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
@@ -78,7 +79,6 @@ file(GLOB LIB_SRC

file(GLOB ALL_TEST
"./src/test/test.cpp"
"./src/test/test_btf.cpp"
"./src/test/test_conformance.cpp"
"./src/test/test_loop.cpp"
"./src/test/test_marshal.cpp"
@@ -133,6 +133,7 @@ target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:RELEASE>:${RELEASE_FLAGS}
target_compile_options(ebpfverifier PUBLIC "$<$<CONFIG:SANITIZE>:${SANITIZE_FLAGS}>")

add_subdirectory("external/bpf_conformance/src")
add_subdirectory("external/libbtf")

# CMake derives a Visual Studio project GUID from the file path but can be overridden via a property
# (see https://gitlab.kitware.com/cmake/cmake/-/commit/c85367f4). Using a non-constant GUID
@@ -163,6 +164,7 @@ target_link_libraries(tests PRIVATE Threads::Threads)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

target_link_libraries(ebpfverifier PRIVATE ${YAML_CPP_LIBRARIES})
target_link_libraries(ebpfverifier PRIVATE libbtf)
target_compile_options(ebpfverifier PRIVATE ${COMMON_FLAGS})

target_link_libraries(run_yaml PRIVATE ebpfverifier)
1 change: 1 addition & 0 deletions external/libbtf
Submodule libbtf added at eff329
28 changes: 22 additions & 6 deletions src/asm_files.cpp
Original file line number Diff line number Diff line change
@@ -10,8 +10,10 @@
#include <sys/stat.h>

#include "asm_files.hpp"
#include "btf.h"
#include "btf_parser.h"
#include "libbtf/btf.h"
#include "libbtf/btf_json.h"
#include "libbtf/btf_map.h"
#include "libbtf/btf_parse.h"
#include "platform.hpp"

#include "elfio/elfio.hpp"
@@ -136,7 +138,7 @@ vector<raw_program> read_elf(std::istream& input_stream, const std::string& path

auto btf = reader.sections[string(".BTF")];
auto btf_ext = reader.sections[string(".BTF.ext")];
std::optional<btf_type_data> btf_data;
std::optional<libbtf::btf_type_data> btf_data;

auto symbol_section = reader.sections[".symtab"];
if (!symbol_section) {
@@ -161,7 +163,7 @@ vector<raw_program> read_elf(std::istream& input_stream, const std::string& path
std::cout << "Dumping BTF data for" << path << std::endl;
// Dump the BTF data to cout for debugging purposes.
btf_data->to_json(output);
std::cout << pretty_print_json(output.str()) << std::endl;
std::cout << libbtf::pretty_print_json(output.str()) << std::endl;
std::cout << std::endl;
}
}
@@ -171,7 +173,21 @@ vector<raw_program> read_elf(std::istream& input_stream, const std::string& path
if (!btf_data.has_value()){
throw std::runtime_error(string("No BTF section found in ELF file ") + path);
}
map_record_size_or_map_offsets = parse_btf_map_sections(btf_data.value(), info.map_descriptors);
//map_record_size_or_map_offsets = parse_btf_map_sections(btf_data.value(), info.map_descriptors);
auto map_data = libbtf::parse_btf_map_section(btf_data.value());
std::map<std::string, size_t> map_offsets;
for (auto& map : map_data) {
map_offsets.insert({map.name, info.map_descriptors.size()});
info.map_descriptors.push_back({
.original_fd = static_cast<int>(map.type_id),
.type = map.map_type,
.key_size = map.key_size,
.value_size = map.value_size,
.max_entries = map.max_entries,
.inner_map_fd = map.inner_map_type_id != 0 ? map.inner_map_type_id : -1,
});
}
map_record_size_or_map_offsets = map_offsets;
// Prevail requires:
// Map fds are sequential starting from 1.
// Map fds are assigned in the order of the maps in the ELF file.
@@ -317,7 +333,7 @@ vector<raw_program> read_elf(std::istream& input_stream, const std::string& path
program.line_info[instruction_offset / sizeof(ebpf_inst)] = {file_name, source, line_number, column_number};
};

btf_parse_line_information(vector_of<std::byte>(*btf), vector_of<std::byte>(*btf_ext), visitor);
libbtf::btf_parse_line_information(vector_of<std::byte>(*btf), vector_of<std::byte>(*btf_ext), visitor);

// BTF doesn't include line info for every instruction, only on the first instruction per source line.
for (auto& [name, program] : segment_to_program) {
165 changes: 0 additions & 165 deletions src/btf.h

This file was deleted.

1,477 changes: 0 additions & 1,477 deletions src/btf_parser.cpp

This file was deleted.

246 changes: 0 additions & 246 deletions src/btf_parser.h

This file was deleted.

0 comments on commit eb95b83

Please sign in to comment.