Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions ci/build-wasi-preview1-component-adapter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ $build_adapter --release
$verify $release
wasm-tools metadata add --name "wasi_preview1_component_adapter.reactor.adapter:${VERSION}" $release \
-o target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.reactor.wasm

# Release build, proxy
$build_adapter --release --no-default-features --features proxy
$verify $release
wasm-tools metadata add --name "wasi_preview1_component_adapter.proxy.adapter:${VERSION}" $release \
-o target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.proxy.wasm
6 changes: 6 additions & 0 deletions crates/test-programs/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ fn build_and_generate_tests() {
"command",
&["--no-default-features", "--features=command"],
);
let proxy_adapter = build_adapter(
&out_dir,
"proxy",
&["--no-default-features", "--features=proxy"],
);

println!("cargo:rerun-if-changed=../src");

Expand Down Expand Up @@ -62,6 +67,7 @@ fn build_and_generate_tests() {

let adapter = match target.as_str() {
"reactor" => &reactor_adapter,
s if s.starts_with("api_proxy") => &proxy_adapter,
_ => &command_adapter,
};
let path = compile_component(&wasm, adapter);
Expand Down
20 changes: 17 additions & 3 deletions crates/wasi-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Resul
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
{
// TODO: this shouldn't be required, but the adapter unconditionally pulls in all of these
// dependencies.
preview2::command::add_to_linker(l)?;
Comment on lines -28 to -30
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

preview2::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
preview2::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
preview2::bindings::io::poll::add_to_linker(l, |t| t)?;
preview2::bindings::io::error::add_to_linker(l, |t| t)?;
preview2::bindings::io::streams::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stdin::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stdout::add_to_linker(l, |t| t)?;
preview2::bindings::cli::stderr::add_to_linker(l, |t| t)?;
preview2::bindings::random::random::add_to_linker(l, |t| t)?;

add_only_http_to_linker(l)
}

#[doc(hidden)]
pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
{
bindings::http::outgoing_handler::add_to_linker(l, |t| t)?;
bindings::http::types::add_to_linker(l, |t| t)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/wasi-http/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;
wasmtime_wasi::preview2::command::add_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?;
let result = command.wasi_cli_run().call_run(&mut store).await?;
result.map_err(|()| anyhow::anyhow!("run returned an error"))
Expand Down
1 change: 1 addition & 0 deletions crates/wasi-preview1-component-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ name = "wasi_snapshot_preview1"
default = ["reactor"]
reactor = []
command = []
proxy = []
52 changes: 40 additions & 12 deletions crates/wasi-preview1-component-adapter/src/descriptors.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::bindings::wasi::cli::{
stderr, stdin, stdout, terminal_stderr, terminal_stdin, terminal_stdout,
};
use crate::bindings::wasi::filesystem::types as filesystem;
use crate::bindings::wasi::cli::{stderr, stdin, stdout};
use crate::bindings::wasi::io::streams::{InputStream, OutputStream};
use crate::{BlockingMode, BumpArena, File, ImportAlloc, TrappingUnwrap, WasmStr};
use crate::{BlockingMode, BumpArena, ImportAlloc, TrappingUnwrap, WasmStr};
use core::cell::{Cell, OnceCell, UnsafeCell};
use core::mem::MaybeUninit;
use wasi::{Errno, Fd};

#[cfg(not(feature = "proxy"))]
use crate::bindings::wasi::filesystem::types as filesystem;
#[cfg(not(feature = "proxy"))]
use crate::File;

pub const MAX_DESCRIPTORS: usize = 128;

#[repr(C)]
Expand Down Expand Up @@ -43,12 +45,14 @@ impl Streams {
let input = match &self.type_ {
// For directories, preview 1 behavior was to return ERRNO_BADF on attempts to read
// or write.
#[cfg(not(feature = "proxy"))]
StreamType::File(File {
descriptor_type: filesystem::DescriptorType::Directory,
..
}) => return Err(wasi::ERRNO_BADF),
// For files, we may have adjusted the position for seeking, so
// create a new stream.
#[cfg(not(feature = "proxy"))]
StreamType::File(file) => {
let input = file.fd.read_via_stream(file.position.get())?;
input
Expand All @@ -69,12 +73,14 @@ impl Streams {
let output = match &self.type_ {
// For directories, preview 1 behavior was to return ERRNO_BADF on attempts to read
// or write.
#[cfg(not(feature = "proxy"))]
StreamType::File(File {
descriptor_type: filesystem::DescriptorType::Directory,
..
}) => return Err(wasi::ERRNO_BADF),
// For files, we may have adjusted the position for seeking, so
// create a new stream.
#[cfg(not(feature = "proxy"))]
StreamType::File(file) => {
let output = if file.append {
file.fd.append_via_stream()?
Expand All @@ -97,6 +103,7 @@ pub enum StreamType {
Stdio(Stdio),

/// Streaming data with a file.
#[cfg(not(feature = "proxy"))]
File(File),
}

Expand All @@ -108,11 +115,17 @@ pub enum Stdio {

impl Stdio {
pub fn filetype(&self) -> wasi::Filetype {
let is_terminal = match self {
Stdio::Stdin => terminal_stdin::get_terminal_stdin().is_some(),
Stdio::Stdout => terminal_stdout::get_terminal_stdout().is_some(),
Stdio::Stderr => terminal_stderr::get_terminal_stderr().is_some(),
#[cfg(not(feature = "proxy"))]
let is_terminal = {
use crate::bindings::wasi::cli;
match self {
Stdio::Stdin => cli::terminal_stdin::get_terminal_stdin().is_some(),
Stdio::Stdout => cli::terminal_stdout::get_terminal_stdout().is_some(),
Stdio::Stderr => cli::terminal_stderr::get_terminal_stderr().is_some(),
}
};
#[cfg(feature = "proxy")]
let is_terminal = false;
if is_terminal {
wasi::FILETYPE_CHARACTER_DEVICE
} else {
Expand All @@ -133,6 +146,7 @@ pub struct Descriptors {

/// Preopened directories. Initialized lazily. Access with `State::get_preopens`
/// to take care of initialization.
#[cfg(not(feature = "proxy"))]
preopens: Cell<Option<&'static [Preopen]>>,
}

Expand All @@ -142,6 +156,7 @@ impl Descriptors {
table: UnsafeCell::new(MaybeUninit::uninit()),
table_len: Cell::new(0),
closed: None,
#[cfg(not(feature = "proxy"))]
preopens: Cell::new(None),
};

Expand Down Expand Up @@ -170,6 +185,13 @@ impl Descriptors {
}))
.trapping_unwrap();

#[cfg(not(feature = "proxy"))]
d.open_preopens(import_alloc, arena);
d
}

#[cfg(not(feature = "proxy"))]
fn open_preopens(&self, import_alloc: &ImportAlloc, arena: &BumpArena) {
#[link(wasm_import_module = "wasi:filesystem/preopens@0.2.0-rc-2023-11-10")]
#[allow(improper_ctypes)] // FIXME(bytecodealliance/wit-bindgen#684)
extern "C" {
Expand All @@ -195,7 +217,7 @@ impl Descriptors {
// Expectation is that the descriptor index is initialized with
// stdio (0,1,2) and no others, so that preopens are 3..
let descriptor_type = descriptor.get_type().trapping_unwrap();
d.push(Descriptor::Streams(Streams {
self.push(Descriptor::Streams(Streams {
input: OnceCell::new(),
output: OnceCell::new(),
type_: StreamType::File(File {
Expand All @@ -209,8 +231,7 @@ impl Descriptors {
.trapping_unwrap();
}

d.preopens.set(Some(preopens));
d
self.preopens.set(Some(preopens));
}

fn push(&self, desc: Descriptor) -> Result<Fd, Errno> {
Expand Down Expand Up @@ -276,6 +297,7 @@ impl Descriptors {
.ok_or(wasi::ERRNO_BADF)
}

#[cfg(not(feature = "proxy"))]
pub fn get_preopen(&self, fd: Fd) -> Option<&Preopen> {
let preopens = self.preopens.get().trapping_unwrap();
// Subtract 3 for the stdio indices to compute the preopen index.
Expand Down Expand Up @@ -340,6 +362,7 @@ impl Descriptors {
}
}

#[cfg(not(feature = "proxy"))]
pub fn get_file_with_error(&self, fd: Fd, error: Errno) -> Result<&File, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
Expand All @@ -359,10 +382,12 @@ impl Descriptors {
}
}

#[cfg(not(feature = "proxy"))]
pub fn get_file(&self, fd: Fd) -> Result<&File, Errno> {
self.get_file_with_error(fd, wasi::ERRNO_INVAL)
}

#[cfg(not(feature = "proxy"))]
pub fn get_dir(&self, fd: Fd) -> Result<&File, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
Expand All @@ -383,6 +408,7 @@ impl Descriptors {
}
}

#[cfg(not(feature = "proxy"))]
pub fn get_seekable_file(&self, fd: Fd) -> Result<&File, Errno> {
self.get_file_with_error(fd, wasi::ERRNO_SPIPE)
}
Expand All @@ -406,6 +432,7 @@ impl Descriptors {
}
}

#[cfg(not(feature = "proxy"))]
#[repr(C)]
pub struct Preopen {
/// This is `MaybeUninit` because we take ownership of the `Descriptor` to
Expand All @@ -414,6 +441,7 @@ pub struct Preopen {
pub path: WasmStr,
}

#[cfg(not(feature = "proxy"))]
#[repr(C)]
pub struct PreopenList {
pub base: *const Preopen,
Expand Down
Loading