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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -95,4 +95,24 @@ foreach(file ${files})
if(EXPECT_FAILURE)
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()
36 changes: 30 additions & 6 deletions ubpf_plugin/ubpf_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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 +134,12 @@ int main(int argc, char **argv)
jit = false;
args.erase(args.begin());
}
#if defined(UBPF_HAS_ELF_H)
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());
}
#endif

if (args.size() > 0 && args[0].size() > 0)
{
Expand All @@ -144,7 +151,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 +173,28 @@ 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 program: " << error << std::endl;
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "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 program: " << error << std::endl;
Alan-Jowett marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "Failed to load code: " << error << std::endl;
free(error);
return 1;
}
}

uint64_t external_dispatcher_result;
Expand Down Expand Up @@ -335,7 +359,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