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

Update *_badfd methods in the adapter to return an errno #7672

Merged
merged 1 commit into from
Dec 11, 2023
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
8 changes: 4 additions & 4 deletions crates/test-programs/src/bin/preview2_adapter_badfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ fn main() {
#[link(wasm_import_module = "wasi_snapshot_preview1")]
extern "C" {
#[cfg_attr(target_arch = "wasm32", link_name = "adapter_open_badfd")]
fn adapter_open_badfd(fd: *mut u32) -> bool;
fn adapter_open_badfd(fd: *mut u32) -> wasi::Errno;

#[cfg_attr(target_arch = "wasm32", link_name = "adapter_close_badfd")]
fn adapter_close_badfd(fd: u32) -> bool;
fn adapter_close_badfd(fd: u32) -> wasi::Errno;
}

unsafe {
let mut fd = 0;
assert!(adapter_open_badfd(&mut fd));
assert_eq!(adapter_open_badfd(&mut fd), wasi::ERRNO_SUCCESS);

assert_eq!(wasi::fd_close(fd), Err(wasi::ERRNO_BADF));

Expand Down Expand Up @@ -41,6 +41,6 @@ fn main() {
Err(wasi::ERRNO_BADF)
);

assert!(adapter_close_badfd(fd));
assert_eq!(adapter_close_badfd(fd), wasi::ERRNO_SUCCESS);
}
}
8 changes: 4 additions & 4 deletions crates/wasi-preview1-component-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ impl<T, E> TrappingUnwrap<T> for Result<T, E> {
/// from WASI Preview 1 to Preview 2. It will use this function to reserve
/// descriptors for its own use, valid only for use with libc functions.
#[no_mangle]
pub unsafe extern "C" fn adapter_open_badfd(fd: *mut u32) -> bool {
pub unsafe extern "C" fn adapter_open_badfd(fd: *mut u32) -> Errno {
State::with(|state| {
*fd = state.descriptors_mut().open(Descriptor::Bad)?;
Ok(())
}) == wasi::ERRNO_SUCCESS
})
}

/// Close a descriptor previously opened using `adapter_open_badfd`.
#[no_mangle]
pub unsafe extern "C" fn adapter_close_badfd(fd: u32) -> bool {
State::with(|state| state.descriptors_mut().close(fd)) == wasi::ERRNO_SUCCESS
pub unsafe extern "C" fn adapter_close_badfd(fd: u32) -> Errno {
State::with(|state| state.descriptors_mut().close(fd))
}

#[no_mangle]
Expand Down