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.
WIP: Support external stacks and local function calls using local memory
1. Add support for invoking the interpreter and JIT'd code with an external stack. This feature is generally useful and will also make it easier to fuzz the runtime and check for correctness. 2. Add support for local functions that use local memory. Prior to this commit, a local function could be called but could not use any local memory (without overwriting memory from another function). Signed-off-by: Will Hawkins <[email protected]>
- Loading branch information
Showing
15 changed files
with
792 additions
and
247 deletions.
There are no files selected for viewing
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 @@ | ||
b7 06 00 00 0a 00 00 00 b7 07 00 00 0a 00 00 00 b7 08 00 00 0a 00 00 00 b7 09 00 00 0a 00 00 00 b7 01 00 00 05 00 00 00 7b 1a f8 ff 00 00 00 00 85 10 00 00 02 00 00 00 79 a0 f8 ff 00 00 00 00 95 00 00 00 00 00 00 00 b7 01 00 00 37 00 00 00 7b 1a f8 ff 00 00 00 00 95 00 00 00 00 00 00 00 |
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 it is possible to update the external helper | ||
functions for an eBPF program that has already been JIT'd. |
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,103 @@ | ||
// Copyright (c) Will Hawkins | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
#include <memory> | ||
#include <stdint.h> | ||
#include <vector> | ||
#include <string> | ||
|
||
extern "C" | ||
{ | ||
#include "ubpf.h" | ||
} | ||
|
||
#include "ubpf_custom_test_support.h" | ||
|
||
int | ||
stack_usage_calculator(const struct ubpf_vm *vm, uint16_t pc, void *cookie) | ||
{ | ||
UNREFERENCED_PARAMETER(vm); | ||
UNREFERENCED_PARAMETER(pc); | ||
UNREFERENCED_PARAMETER(cookie); | ||
return 16; | ||
} | ||
|
||
int | ||
overwrite_stack_usage_calculator(const struct ubpf_vm *vm, uint16_t pc, void *cookie) | ||
{ | ||
UNREFERENCED_PARAMETER(vm); | ||
UNREFERENCED_PARAMETER(pc); | ||
UNREFERENCED_PARAMETER(cookie); | ||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
std::vector<std::string> args(argv, argv + argc); | ||
std::string program_string{}; | ||
ubpf_jit_fn jit_fn; | ||
|
||
std::getline(std::cin, program_string); | ||
|
||
uint64_t no_overwrite_interp_result = 0; | ||
uint64_t no_overwrite_jit_result = 0; | ||
uint64_t overwrite_interp_result = 0; | ||
uint64_t overwrite_jit_result = 0; | ||
|
||
{ | ||
|
||
std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy); | ||
std::string error{}; | ||
if (!ubpf_setup_custom_test( | ||
vm, | ||
program_string, | ||
[](ubpf_vm_up& vm, std::string& error) { | ||
if (ubpf_register_stack_usage_calculator(vm.get(), stack_usage_calculator, nullptr) < 0) { | ||
error = "Failed to register stack usage calculator."; | ||
return false; | ||
} | ||
return true; | ||
}, | ||
jit_fn, | ||
error)) { | ||
std::cerr << "Problem setting up custom test: " << error << std::endl; | ||
return 1; | ||
} | ||
|
||
no_overwrite_jit_result = jit_fn(nullptr, 0); | ||
[[maybe_unused]] auto exec_result = ubpf_exec(vm.get(), NULL, 0, &no_overwrite_interp_result); | ||
} | ||
|
||
{ | ||
|
||
std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy); | ||
std::string error{}; | ||
if (!ubpf_setup_custom_test( | ||
vm, | ||
program_string, | ||
[](ubpf_vm_up& vm, std::string& error) { | ||
if (ubpf_register_stack_usage_calculator(vm.get(), overwrite_stack_usage_calculator, nullptr) < 0) { | ||
error = "Failed to register stack usage calculator."; | ||
return false; | ||
} | ||
return true; | ||
}, | ||
jit_fn, | ||
error)) { | ||
std::cerr << "Problem setting up custom test: " << error << std::endl; | ||
return 1; | ||
} | ||
|
||
overwrite_jit_result = jit_fn(nullptr, 0); | ||
|
||
[[maybe_unused]] auto exec_result = ubpf_exec(vm.get(), NULL, 0, &overwrite_interp_result); | ||
} | ||
// ... because of the semantics of external_dispatcher, the result of the eBPF | ||
// program execution should point to the same place to which &memory points. | ||
return !(no_overwrite_interp_result == no_overwrite_jit_result && | ||
no_overwrite_interp_result == 0x5 && | ||
overwrite_interp_result == overwrite_jit_result && | ||
overwrite_interp_result == 0x37); | ||
} |
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
Oops, something went wrong.