forked from rlane/ubpf
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rationalize parameters to helper functions (#515)
The external helper functions should get 6 parameters no matter whether the eBPF program is interpeted or JIT'd. The 6 parameter, the one in addition to the 5 specified by the eBPF standard, is the context parameter. Add tests to make sure that the 6th parameter is properly passed to the external helper functions. Signed-off-by: Will Hawkins <[email protected]>
- Loading branch information
Showing
10 changed files
with
174 additions
and
42 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
custom_tests/data/ubpf_test_default_dispatcher_helper_context.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
85 00 00 00 01 00 00 00 95 00 00 00 00 00 00 00 |
4 changes: 4 additions & 0 deletions
4
custom_tests/descrs/ubpf_test_default_dispatcher_helper_context.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
##Test Description | ||
|
||
This custom test program tests whether JIT'd and interpreted eBPF programs properly pass the context to external | ||
helper functions. |
81 changes: 81 additions & 0 deletions
81
custom_tests/srcs/ubpf_test_default_dispatcher_helper_context.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Will Hawkins | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
#include <memory> | ||
#include <vector> | ||
#include <string> | ||
|
||
extern "C" | ||
{ | ||
#include "ubpf.h" | ||
} | ||
|
||
#include "ubpf_custom_test_support.h" | ||
|
||
uint64_t | ||
simple_helper(uint64_t p0, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, void* cookie) | ||
{ | ||
UNREFERENCED_PARAMETER(p0); | ||
UNREFERENCED_PARAMETER(p1); | ||
UNREFERENCED_PARAMETER(p2); | ||
UNREFERENCED_PARAMETER(p3); | ||
UNREFERENCED_PARAMETER(p4); | ||
uint64_t* ccookie = (uint64_t*)cookie; | ||
return *ccookie; | ||
} | ||
|
||
int | ||
main(int argc, char** argv) | ||
{ | ||
std::string program_string{}; | ||
std::string error{}; | ||
ubpf_jit_fn jit_fn; | ||
uint64_t memory{0x123456789}; | ||
|
||
// The test program invokes an external function at index 1 (which is invoked via the | ||
// default external helper dispatcher). The result of that external function is given as the | ||
// result of the eBPF's program execution. Therefore, ... | ||
if (!get_program_string(argc, argv, program_string, error)) { | ||
std::cerr << error << std::endl; | ||
return 1; | ||
} | ||
|
||
std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy); | ||
if (!ubpf_setup_custom_test( | ||
vm, | ||
program_string, | ||
[](ubpf_vm_up& vm, std::string& error) { | ||
if (ubpf_register(vm.get(), 1, "simple helper", as_external_function_t((void*)simple_helper)) < 0) { | ||
error = "Failed to register external helper function at index 1."; | ||
return false; | ||
} | ||
return true; | ||
}, | ||
jit_fn, | ||
error)) { | ||
std::cerr << "Problem setting up custom test: " << error << std::endl; | ||
return 1; | ||
} | ||
|
||
[[maybe_unused]] auto result = jit_fn(&memory, sizeof(uint64_t)); | ||
|
||
// ... because of the semantics of external helper functions, the result of the eBPF | ||
// program execution should point to the same place to which &memory points. | ||
if (result != memory) { | ||
std::cerr << "result and memory are not equal (JIT version).\n"; | ||
return 1; | ||
} | ||
|
||
if (ubpf_exec(vm.get(), &memory, sizeof(uint64_t), &result)) { | ||
std::cerr << "There was an error interpreting the test program.\n"; | ||
return 1; | ||
} | ||
|
||
if (result != memory) { | ||
std::cerr << "result and memory are not equal (interpreter version).\n"; | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters