Skip to content

Commit

Permalink
Refactor wasm canary code for new wasmtime version
Browse files Browse the repository at this point in the history
More updates for wasmtime bump

Add WASI HTTP interface to linker
  • Loading branch information
landonxjames committed Jun 18, 2024
1 parent de79ebe commit 9f6da68
Showing 1 changed file with 9 additions and 75 deletions.
84 changes: 9 additions & 75 deletions tools/ci-cdk/canary-lambda/src/latest/wasm_canary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{mk_canary, CanaryEnv};
use aws_config::SdkConfig;
use wasmtime::component::{bindgen, Component, Linker};
use wasmtime::{Engine, Store};
use wasmtime_wasi::preview2::WasiCtxBuilder;
use wasmtime_wasi::WasiCtxBuilder;

mk_canary!("wasm", |_sdk_config: &SdkConfig, _env: &CanaryEnv| {
wasm_canary()
Expand All @@ -22,25 +22,17 @@ bindgen!({
});

struct WasiHostCtx {
preview2_ctx: wasmtime_wasi::preview2::WasiCtx,
preview2_ctx: wasmtime_wasi::WasiCtx,
preview2_table: wasmtime::component::ResourceTable,
wasi_http_ctx: wasmtime_wasi_http::WasiHttpCtx,
}

impl wasmtime_wasi::preview2::WasiView for WasiHostCtx {
fn table(&self) -> &wasmtime::component::ResourceTable {
&self.preview2_table
}

fn ctx(&self) -> &wasmtime_wasi::preview2::WasiCtx {
&self.preview2_ctx
}

fn table_mut(&mut self) -> &mut wasmtime::component::ResourceTable {
impl wasmtime_wasi::WasiView for WasiHostCtx {
fn table(&mut self) -> &mut wasmtime::component::ResourceTable {
&mut self.preview2_table
}

fn ctx_mut(&mut self) -> &mut wasmtime_wasi::preview2::WasiCtx {
fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx {
&mut self.preview2_ctx
}
}
Expand Down Expand Up @@ -72,7 +64,8 @@ pub async fn wasm_canary() -> anyhow::Result<()> {

// Create the linker and link in the necessary WASI bindings
let mut linker: Linker<WasiHostCtx> = Linker::new(&engine);
link_all_the_things(&mut linker);
wasmtime_wasi::add_to_linker_async(&mut linker)?;
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;

// Configure and create a `WasiCtx`, which WASI functions need access to
// through the host state of the store (which in this case is the host state
Expand All @@ -84,8 +77,8 @@ pub async fn wasm_canary() -> anyhow::Result<()> {

let host_ctx = WasiHostCtx {
preview2_ctx: wasi_ctx,
preview2_table: wasmtime_wasi::preview2::ResourceTable::new(),
wasi_http_ctx: wasmtime_wasi_http::WasiHttpCtx {},
preview2_table: wasmtime_wasi::ResourceTable::new(),
wasi_http_ctx: wasmtime_wasi_http::WasiHttpCtx::new(),
};

let mut store: Store<WasiHostCtx> = Store::new(&engine, host_ctx);
Expand All @@ -105,65 +98,6 @@ pub async fn wasm_canary() -> anyhow::Result<()> {
Ok(())
}

/// This function adds all of the WASI bindings to the linker
fn link_all_the_things(linker: &mut Linker<WasiHostCtx>) {
//IO
wasmtime_wasi::preview2::bindings::io::poll::add_to_linker(linker, |cx| cx)
.expect("Failed to link Poll");
wasmtime_wasi::preview2::bindings::io::error::add_to_linker(linker, |cx| cx)
.expect("Failed to link Error");
wasmtime_wasi::preview2::bindings::io::streams::add_to_linker(linker, |cx| cx)
.expect("Failed to link Streams");

//Random
wasmtime_wasi::preview2::bindings::random::random::add_to_linker(linker, |cx| cx)
.expect("Failed to link Random");

//Clocks
wasmtime_wasi::preview2::bindings::wasi::clocks::monotonic_clock::add_to_linker(linker, |cx| {
cx
})
.expect("Failed to link Clock");
wasmtime_wasi::preview2::bindings::wasi::clocks::wall_clock::add_to_linker(linker, |cx| cx)
.expect("Failed to link Wall Clock");

//Filesystem
wasmtime_wasi::preview2::bindings::filesystem::types::add_to_linker(linker, |cx| cx)
.expect("Failed to link Filesystem Types");
wasmtime_wasi::preview2::bindings::filesystem::preopens::add_to_linker(linker, |cx| cx)
.expect("Failed to link Filesystem Preopen");

//CLI
wasmtime_wasi::preview2::bindings::wasi::cli::environment::add_to_linker(linker, |cx| cx)
.expect("Failed to link Environment");
wasmtime_wasi::preview2::bindings::wasi::cli::exit::add_to_linker(linker, |cx| cx)
.expect("Failed to link Environment");
wasmtime_wasi::preview2::bindings::wasi::cli::stdin::add_to_linker(linker, |cx| cx)
.expect("Failed to link Stdin");
wasmtime_wasi::preview2::bindings::wasi::cli::stdout::add_to_linker(linker, |cx| cx)
.expect("Failed to link Stdout");
wasmtime_wasi::preview2::bindings::wasi::cli::stderr::add_to_linker(linker, |cx| cx)
.expect("Failed to link Stderr");

// CLI Terminal
wasmtime_wasi::preview2::bindings::wasi::cli::terminal_input::add_to_linker(linker, |cx| cx)
.expect("Failed to link Terminal Input");
wasmtime_wasi::preview2::bindings::wasi::cli::terminal_output::add_to_linker(linker, |cx| cx)
.expect("Failed to link Terminal Output");
wasmtime_wasi::preview2::bindings::wasi::cli::terminal_stdin::add_to_linker(linker, |cx| cx)
.expect("Failed to link Terminal Stdin");
wasmtime_wasi::preview2::bindings::wasi::cli::terminal_stdout::add_to_linker(linker, |cx| cx)
.expect("Failed to link Terminal Stdout");
wasmtime_wasi::preview2::bindings::wasi::cli::terminal_stderr::add_to_linker(linker, |cx| cx)
.expect("Failed to link Terminal Stderr");

//HTTP
wasmtime_wasi_http::bindings::http::types::add_to_linker(linker, |cx| cx)
.expect("Failed to link HTTP Types");
wasmtime_wasi_http::bindings::http::outgoing_handler::add_to_linker(linker, |cx| cx)
.expect("Failed to link HTTP Outgoing Handler");
}

// #[ignore]
#[cfg(test)]
#[tokio::test]
Expand Down

0 comments on commit 9f6da68

Please sign in to comment.