Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasi-http: support inbound requests (proxy world) #7091

Merged
merged 26 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
65fc5c7
Move the incoming_handler impl into http_impl
elliottt Sep 21, 2023
1b817ea
Remove the incoming handler -- we need to use it as a guest export
elliottt Sep 21, 2023
df83b75
Start adding a test-programs test for the server side of wasi-http
elliottt Sep 22, 2023
4d36120
Progress towards running a server test
elliottt Sep 22, 2023
54c2a5c
Implement incoming-request-method
elliottt Sep 22, 2023
1697b77
Validate outparam value
elliottt Sep 22, 2023
25432ea
Initial incoming handler test
elliottt Sep 23, 2023
d021210
Implement more of the incoming api
elliottt Sep 25, 2023
b194929
Finish the incoming api implementations
elliottt Sep 25, 2023
894fdca
Initial cut at `wasmtime serve`
elliottt Sep 26, 2023
0b4a07c
fix warning
Sep 26, 2023
f5f20f7
wasmtime-cli: invoke ServeCommand, and add enough stuff to the linker…
Sep 26, 2023
06e120a
fix warnings
Sep 26, 2023
3f1c7d7
fix warnings
Sep 26, 2023
61d9093
argument parsing: allow --addr to specify sockaddr
Sep 27, 2023
622875b
rustfmt
Sep 27, 2023
0880e04
sync wit definitions between wasmtime-wasi and wasmtime-wasi-http
Sep 27, 2023
3585d2d
cargo vet: add an import config and wildcard audit for wasmtime-wmemc…
Sep 27, 2023
e9f77e1
cargo vet: audit signal-hook-registry
Sep 27, 2023
a6115d8
Remove duplicate add_to_linker calls for preview2 interfaces
elliottt Sep 27, 2023
19a574b
Add a method to finish outgoing responses
elliottt Sep 28, 2023
2a2f33d
Mark the result of the incoming_{request,response}_consume methods as…
elliottt Sep 28, 2023
698f0ff
Explicit versions for http-body and http-body-util
elliottt Sep 28, 2023
147713f
Explicit `serve` feature for the `wasmtime serve` command
elliottt Sep 28, 2023
cc24768
Move the spawn outside of the future returned by `ProxyHandler::call`
elliottt Sep 28, 2023
2c17fe7
Review feedback
elliottt Sep 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ serde_json = { workspace = true }
wasmparser = { workspace = true }
wasm-encoder = { workspace = true }

async-trait = { workspace = true }
tokio = { workspace = true, optional = true, features = [ "signal", "macros" ] }
hyper = { workspace = true, optional = true }
http-body = { workspace = true }
http-body-util = { workspace = true }

[target.'cfg(unix)'.dependencies]
rustix = { workspace = true, features = ["mm", "param"] }

Expand All @@ -60,7 +66,7 @@ filecheck = { workspace = true }
tempfile = { workspace = true }
test-programs = { path = "crates/test-programs" }
wasmtime-runtime = { workspace = true }
tokio = { version = "1.8.0", features = ["rt", "time", "macros", "rt-multi-thread"] }
tokio = { workspace = true, features = ["rt", "time", "macros", "rt-multi-thread"] }
wast = { workspace = true }
criterion = "0.5.0"
num_cpus = "1.13.0"
Expand Down Expand Up @@ -101,6 +107,7 @@ members = [
"crates/jit-icache-coherence",
"crates/test-programs/wasi-tests",
"crates/test-programs/wasi-http-tests",
"crates/test-programs/wasi-http-proxy-tests",
"crates/test-programs/wasi-sockets-tests",
"crates/test-programs/command-tests",
"crates/test-programs/reactor-tests",
Expand Down Expand Up @@ -251,7 +258,11 @@ tempfile = "3.1.0"
filecheck = "0.5.0"
libc = "0.2.60"
file-per-thread-logger = "0.2.0"
tokio = { version = "1.26.0" }
tokio = { version = "1.26.0", features = [ "rt", "time" ] }
hyper = "=1.0.0-rc.3"
http = "0.2.9"
http-body = "=1.0.0-rc.2"
http-body-util = "=0.1.0-rc.2"
bytes = "1.4"
futures = { version = "0.3.27", default-features = false }
indexmap = "2.0.0"
Expand All @@ -275,7 +286,7 @@ jitdump = ["wasmtime/jitdump"]
vtune = ["wasmtime/vtune"]
wasi-nn = ["dep:wasmtime-wasi-nn"]
wasi-threads = ["dep:wasmtime-wasi-threads"]
wasi-http = ["dep:wasmtime-wasi-http", "wasmtime-wasi-http?/sync"]
wasi-http = ["component-model", "dep:wasmtime-wasi-http", "dep:tokio", "dep:hyper", "wasmtime-wasi-http?/sync"]
pooling-allocator = ["wasmtime/pooling-allocator", "wasmtime-cli-flags/pooling-allocator"]
all-arch = ["wasmtime/all-arch"]
component-model = [
Expand All @@ -286,6 +297,9 @@ component-model = [
winch = ["wasmtime/winch"]
wmemcheck = ["wasmtime/wmemcheck"]

# Enable the `wasmtime serve` command
serve = ["wasi-http", "component-model"]

[[test]]
name = "host_segfault"
harness = false
Expand Down
1 change: 1 addition & 0 deletions crates/test-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ heck = { workspace = true }

[dependencies]
anyhow = { workspace = true }
bytes = { workspace = true }
http = { version = "0.2.9" }
http-body = "1.0.0-rc.2"
http-body-util = "0.1.0-rc.2"
Expand Down
10 changes: 9 additions & 1 deletion crates/test-programs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn build_and_generate_tests() {
println!("cargo:rerun-if-changed=./wasi-sockets-tests");
if BUILD_WASI_HTTP_TESTS {
println!("cargo:rerun-if-changed=./wasi-http-tests");
println!("cargo:rerun-if-changed=./wasi-http-proxy-tests");
} else {
println!("cargo:rustc-cfg=skip_wasi_http_tests");
}
Expand All @@ -50,6 +51,7 @@ fn build_and_generate_tests() {
.env_remove("CARGO_ENCODED_RUSTFLAGS");
if BUILD_WASI_HTTP_TESTS {
cmd.arg("--package=wasi-http-tests");
cmd.arg("--package=wasi-http-proxy-tests");
}
let status = cmd.status().unwrap();
assert!(status.success());
Expand All @@ -60,8 +62,14 @@ fn build_and_generate_tests() {
components_rs(&meta, "wasi-tests", "bin", &command_adapter, &out_dir);

if BUILD_WASI_HTTP_TESTS {
modules_rs(&meta, "wasi-http-tests", "bin", &out_dir);
components_rs(&meta, "wasi-http-tests", "bin", &command_adapter, &out_dir);
components_rs(
&meta,
"wasi-http-proxy-tests",
"cdylib",
&reactor_adapter,
&out_dir,
);
}

components_rs(&meta, "command-tests", "bin", &command_adapter, &out_dir);
Expand Down
8 changes: 3 additions & 5 deletions crates/test-programs/tests/wasi-http-components-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use wasmtime::{
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{
command::sync::{add_to_linker, Command},
pipe::MemoryOutputPipe,
IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView,
command::sync::Command, pipe::MemoryOutputPipe, IsATTY, Table, WasiCtx, WasiCtxBuilder,
WasiView,
};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};

Expand Down Expand Up @@ -60,8 +59,7 @@ fn instantiate_component(
ctx: Ctx,
) -> Result<(Store<Ctx>, Command), anyhow::Error> {
let mut linker = Linker::new(&ENGINE);
add_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::sync::add_to_linker(&mut linker)?;

let mut store = Store::new(&ENGINE, ctx);

Expand Down
5 changes: 1 addition & 4 deletions crates/test-programs/tests/wasi-http-components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use wasmtime::{
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{
command::{add_to_linker, Command},
pipe::MemoryOutputPipe,
IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView,
command::Command, pipe::MemoryOutputPipe, IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView,
};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};

Expand Down Expand Up @@ -60,7 +58,6 @@ async fn instantiate_component(
ctx: Ctx,
) -> Result<(Store<Ctx>, Command), anyhow::Error> {
let mut linker = Linker::new(&ENGINE);
add_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;

let mut store = Store::new(&ENGINE, ctx);
Expand Down
146 changes: 146 additions & 0 deletions crates/test-programs/tests/wasi-http-proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#![cfg(all(feature = "test_programs", not(skip_wasi_http_tests)))]
use anyhow::Context;
use wasmtime::{
component::{Component, Linker},
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{
self, pipe::MemoryOutputPipe, IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView,
};
use wasmtime_wasi_http::{proxy::Proxy, WasiHttpCtx, WasiHttpView};

lazy_static::lazy_static! {
static ref ENGINE: Engine = {
let mut config = Config::new();
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
config.wasm_component_model(true);
config.async_support(true);
let engine = Engine::new(&config).unwrap();
engine
};
}
// uses ENGINE, creates a fn get_module(&str) -> Module
include!(concat!(
env!("OUT_DIR"),
"/wasi_http_proxy_tests_components.rs"
));

struct Ctx {
table: Table,
wasi: WasiCtx,
http: WasiHttpCtx,
}

impl WasiView for Ctx {
fn table(&self) -> &Table {
&self.table
}
fn table_mut(&mut self) -> &mut Table {
&mut self.table
}
fn ctx(&self) -> &WasiCtx {
&self.wasi
}
fn ctx_mut(&mut self) -> &mut WasiCtx {
&mut self.wasi
}
}

impl WasiHttpView for Ctx {
fn table(&mut self) -> &mut Table {
&mut self.table
}
fn ctx(&mut self) -> &mut WasiHttpCtx {
&mut self.http
}
}

async fn instantiate(component: Component, ctx: Ctx) -> Result<(Store<Ctx>, Proxy), anyhow::Error> {
let mut linker = Linker::new(&ENGINE);
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;

let mut store = Store::new(&ENGINE, ctx);

let (proxy, _instance) = Proxy::instantiate_async(&mut store, &component, &linker).await?;
Ok((store, proxy))
}

#[test_log::test(tokio::test)]
async fn wasi_http_proxy_tests() -> anyhow::Result<()> {
let stdout = MemoryOutputPipe::new(4096);
let stderr = MemoryOutputPipe::new(4096);

let mut table = Table::new();
let component = get_component("wasi_http_proxy_tests");

// Create our wasi context.
let mut builder = WasiCtxBuilder::new();
builder.stdout(stdout.clone(), IsATTY::No);
builder.stderr(stderr.clone(), IsATTY::No);
for (var, val) in test_programs::wasi_tests_environment() {
builder.env(var, val);
}
let wasi = builder.build(&mut table)?;
let http = WasiHttpCtx;

let ctx = Ctx { table, wasi, http };

let (mut store, proxy) = instantiate(component, ctx).await?;

let req = {
use http_body_util::{BodyExt, Empty};

let req = hyper::Request::builder().method(http::Method::GET).body(
Empty::<bytes::Bytes>::new()
.map_err(|e| anyhow::anyhow!(e))
.boxed(),
)?;
store.data_mut().new_incoming_request(req)?
};

let (sender, receiver) = tokio::sync::oneshot::channel();
let out = store.data_mut().new_response_outparam(sender)?;

let handle = preview2::spawn(async move {
proxy
.wasi_http_incoming_handler()
.call_handle(&mut store, req, out)
.await?;

Ok::<_, anyhow::Error>(())
});

let resp = match receiver.await {
Ok(Ok(resp)) => {
use http_body_util::BodyExt;
let (parts, body) = resp.into_parts();
let collected = BodyExt::collect(body).await?;
Ok(hyper::Response::from_parts(parts, collected))
}

Ok(Err(e)) => Err(e),

// This happens if the wasm never calls `set-response-outparam`
Err(e) => panic!("Failed to receive a response: {e:?}"),
};

// Now that the response has been processed, we can wait on the wasm to finish without
// deadlocking.
handle.await.context("Component execution")?;

let stdout = stdout.contents();
if !stdout.is_empty() {
println!("[guest] stdout:\n{}\n===", String::from_utf8_lossy(&stdout));
}
let stderr = stderr.contents();
if !stderr.is_empty() {
println!("[guest] stderr:\n{}\n===", String::from_utf8_lossy(&stderr));
}

match resp {
Ok(resp) => println!("response: {resp:?}"),
Err(e) => panic!("Error given in response: {e:?}"),
};

Ok(())
}
12 changes: 12 additions & 0 deletions crates/test-programs/wasi-http-proxy-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "wasi-http-proxy-tests"
version = "0.0.0"
readme = "README.md"
edition = "2021"
publish = false

[lib]
crate-type=["cdylib"]

[dependencies]
wit-bindgen = { workspace = true, features = ["macros", "realloc"] }
Loading
Loading