Skip to content

Commit

Permalink
Update *_badfd methods in the adapter to return an errno (#7672)
Browse files Browse the repository at this point in the history
Makes them a bit more consistent with the rest of the WASI functions as
opposed to returning a boolean.
  • Loading branch information
alexcrichton authored Dec 11, 2023
1 parent ef70686 commit 2acfb63
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
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

0 comments on commit 2acfb63

Please sign in to comment.