Skip to content

Commit

Permalink
Add support for user-registered host functions (#89)
Browse files Browse the repository at this point in the history
* Add support for user-registered host functions

* format the code
  • Loading branch information
Officeyutong authored Mar 21, 2023
1 parent a1ae8e0 commit e5126f5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
20 changes: 16 additions & 4 deletions runtime/wasm-bpf-rs/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::mpsc;

use anyhow::{anyhow, Context};
use wasmtime::{Engine, Linker, Module, Store, TypedFunc};
use wasmtime::{Engine, IntoFunc, Linker, Module, Store, TypedFunc};
use wasmtime_wasi::WasiCtxBuilder;

use crate::add_bind_function_with_module;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub struct WasmBpfModuleRunner {
/// The linker which will be used
pub linker: Linker<AppState>,
operation_tx: mpsc::Sender<ProgramOperation>,
main_module: Module,
}

impl WasmBpfModuleRunner {
Expand Down Expand Up @@ -88,14 +89,12 @@ impl WasmBpfModuleRunner {
wrapper_poll::bpf_buffer_poll_wrapper,
POLL_WRAPPER_FUNCTION_NAME
)?;
linker
.module(&mut store, MAIN_MODULE_NAME, &main_module)
.with_context(|| anyhow!("Failed to link main module"))?;
Ok(Self {
engine,
store,
linker,
operation_tx: tx,
main_module,
})
}
/// Consume this runner, return a handle to the wasm program, which can control the pause/resume/terminate of the program
Expand All @@ -104,6 +103,10 @@ impl WasmBpfModuleRunner {
pub fn into_engine_and_entry_func(
mut self,
) -> anyhow::Result<(WasmProgramHandle, WasmBpfEntryFuncWrapper)> {
self.linker
.module(&mut self.store, MAIN_MODULE_NAME, &self.main_module)
.with_context(|| anyhow!("Failed to link main module"))?;

let func = self
.linker
.get(&mut self.store, MAIN_MODULE_NAME, "_start")
Expand All @@ -119,4 +122,13 @@ impl WasmBpfModuleRunner {
},
))
}
/// Register a custom host function. It has the similar signature as `wasmtime::linker::Linker::func_wrap`
pub fn register_host_function<Params, Args>(
&mut self,
module: &str,
name: &str,
func: impl IntoFunc<AppState, Params, Args>,
) -> anyhow::Result<()> {
self.linker.func_wrap(module, name, func).map(|_| ())
}
}
18 changes: 18 additions & 0 deletions runtime/wasm-bpf-rs/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::handle::WasmProgramHandle;
use crate::pipe::ReadableWritePipe;
use crate::state::CallerType;

use super::*;
use std::fs::File;
Expand Down Expand Up @@ -154,3 +155,20 @@ fn test_pause_and_resume_wasm_program() {
assert!(tick_count_3 - tick_count_2 >= 2);
handle.as_mut().unwrap().terminate().unwrap();
}

#[test]
fn test_custom_host_function() {
let module_binary = std::fs::read(get_test_file_path("custom_host_func.wasm")).unwrap();
let args = vec!["test".to_string()];
let mut runner =
WasmBpfModuleRunner::new(&module_binary[..], &args[..], Config::default()).unwrap();
runner
.register_host_function("host_func_test", "plus_i32", host_func_plus_i32)
.unwrap();
let (_, wrapper) = runner.into_engine_and_entry_func().unwrap();
wrapper.run().unwrap();
}

fn host_func_plus_i32(_caller: CallerType, a: i32, b: i32) -> i32 {
a + b
}
Binary file added runtime/wasm-bpf-rs/tests/custom_host_func.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions runtime/wasm-bpf-rs/tests/custom_host_func/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom_host_func.wasm
6 changes: 6 additions & 0 deletions runtime/wasm-bpf-rs/tests/custom_host_func/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WASI_CLANG = /opt/wasi-sdk/bin/clang
WASI_CFLAGS = -O2 --sysroot=/opt/wasi-sdk/share/wasi-sysroot -Wl,--allow-undefined,--export-table

custom_host_func.wasm: custom_host_func.c
$(WASI_CLANG) $(WASI_CFLAGS) -o $@ $<
cp custom_host_func.wasm ..
3 changes: 3 additions & 0 deletions runtime/wasm-bpf-rs/tests/custom_host_func/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test program for custom host func

It invokes a host function to calculate `a+b`, and verifies the result.
10 changes: 10 additions & 0 deletions runtime/wasm-bpf-rs/tests/custom_host_func/custom_host_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <inttypes.h>
#include <assert.h>
__attribute__((import_module("host_func_test"), import_name("plus_i32")))
int32_t
plus_i32(int32_t a, int32_t b);
int main() {
int32_t c = plus_i32(0xABCD, 0x1234);
assert(c == 0xABCD + 0x1234);
return 0;
}

0 comments on commit e5126f5

Please sign in to comment.