Skip to content

Commit

Permalink
Add test pass to run bpf_conformance with ELF
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett authored and Alan Jowett committed Jun 24, 2024
1 parent 54be1df commit e16099b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
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") {
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);
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;
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;
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
1 change: 0 additions & 1 deletion vm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) 2022-present, IO Visor Project
# SPDX-License-Identifier: Apache-2.0

#
# Copyright (c) 2022-present, IO Visor Project
# All rights reserved.
Expand Down

0 comments on commit e16099b

Please sign in to comment.