Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test pass to run bpf_conformance with ELF #273

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ include("cmake/settings.cmake")
include("cmake/options.cmake")
include("cmake/version.cmake")

find_path(UBPF_ELF_H_PATH "elf.h" NO_CACHE)
if(UBPF_ELF_H_PATH)
set(UBPF_HAS_ELF_H true)
else()
message(WARNING "ubpf - elf.h was not found, disabling ELF support")
endif()

if(UBPF_ENABLE_TESTS)
include("CTest")
endif()
Expand Down
20 changes: 20 additions & 0 deletions ubpf_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,24 @@ foreach(file ${files})
if(EXPECT_FAILURE OR EXPECT_FAILURE_INTERPRET)
set_tests_properties(${file}-Interpreter PROPERTIES WILL_FAIL TRUE)
endif()

if(UBPF_ELF_H_PATH AND NOT CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
add_test(
NAME ${file}-JIT-ELF
COMMAND ${BPF_CONFORMANCE_RUNNER} --elf true --test_file_path ${file} ${PLUGIN_JIT}
)

if(EXPECT_FAILURE)
set_tests_properties(${file}-JIT-ELF PROPERTIES WILL_FAIL TRUE)
endif()

add_test(
NAME ${file}-Interpreter-ELF
COMMAND ${BPF_CONFORMANCE_RUNNER} --elf true --test_file_path ${file} ${PLUGIN_INTERPRET}
)

if(EXPECT_FAILURE)
set_tests_properties(${file}-Interpreter-ELF PROPERTIES WILL_FAIL TRUE)
endif()
endif()
endforeach()
47 changes: 41 additions & 6 deletions ubpf_plugin/ubpf_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ stack_usage_calculator(const struct ubpf_vm* vm, uint16_t pc, void* cookie)
return 64;
}

void print_help()
{
std::cout << "Usage: ubpf_plugin [--program <program>] [--jit|--interpret] [--elf] [<memory>]" << std::endl;
std::cout << " --program <program> BPF program to execute in hex" << std::endl;
std::cout << " --jit Use JIT compiler" << std::endl;
std::cout << " --interpret Use interpreter" << std::endl;
std::cout << " --elf Load program as ELF" << std::endl;
std::cout << " <memory> Memory contents in hex" << std::endl;
std::cout << " If <program> is not provided, it will be read from stdin" << std::endl;
}

/**
* @brief This program reads BPF instructions from stdin and memory contents from
* the first agument. It then executes the BPF program and prints the
Expand All @@ -101,6 +112,7 @@ stack_usage_calculator(const struct ubpf_vm* vm, uint16_t pc, void* cookie)
int main(int argc, char **argv)
{
bool jit = false; // JIT == true, interpreter == false
bool is_elf = false;
std::vector<std::string> args(argv, argv + argc);
std::string program_string;
std::string memory_string;
Expand Down Expand Up @@ -133,6 +145,14 @@ int main(int argc, char **argv)
jit = false;
args.erase(args.begin());
}
if (args.size() > 0 && args[0] == "--elf") {
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
is_elf = true;
args.erase(args.begin());
#if !defined(UBPF_HAS_ELF_H)
std::cerr << "ELF support not compiled in" << std::endl;
return 1;
#endif
}

if (args.size() > 0 && args[0].size() > 0)
{
Expand All @@ -144,7 +164,7 @@ int main(int argc, char **argv)
std::getline(std::cin, program_string);
}

std::vector<ebpf_inst> program = bytes_to_ebpf_inst(base16_decode(program_string));
auto program_byte = base16_decode(program_string);
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
std::vector<uint8_t> memory = base16_decode(memory_string);

std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy);
Expand All @@ -166,11 +186,26 @@ int main(int argc, char **argv)
return 1;
}

if (ubpf_load(vm.get(), program.data(), static_cast<uint32_t>(program.size() * sizeof(ebpf_inst)), &error) != 0)
{
std::cout << "Failed to load code: " << error << std::endl;
free(error);
if (is_elf) {
#if defined(UBPF_HAS_ELF_H)
if (ubpf_load_elf(vm.get(), program_byte.data(), static_cast<uint32_t>(program_byte.size()), &error) != 0)
{
std::cerr << "Failed to load code: " << error << std::endl;
free(error);
return 1;
}
#else
std::cerr << "ELF support not compiled in" << std::endl;
return 1;
#endif
}
else {
if (ubpf_load(vm.get(), program_byte.data(), static_cast<uint32_t>(program_byte.size()), &error) != 0)
{
std::cerr << "Failed to load code: " << error << std::endl;
free(error);
return 1;
}
}

uint64_t external_dispatcher_result;
Expand Down Expand Up @@ -335,7 +370,7 @@ int main(int argc, char **argv)
// ... and make sure the results are the same.
if (external_dispatcher_result != index_helper_result) {
std::cerr << "Execution of the interpreted code with external and indexed helpers gave difference results: 0x"
<< std::hex << external_dispatcher_result
<< std::hex << external_dispatcher_result
<< " vs 0x" << std::hex << index_helper_result << "." << std::endl;
return 1;
}
Expand Down
Loading