-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[16.0.0] Backport
adapter_{open|close}_badfd
exports (#7675)
* add `adapter_{open|close}_badfd` exports to Preview 1 adapter (#7663) * add `adapter_{open|close}_badfd` exports to Preview 1 adapter This is to be used by `wasi-libc` to reserve file descriptors for its own use (e.g. for sockets), ensuring that any attempt to pass them directly to Preview 1 functions will consistently return an error. See WebAssembly/wasi-libc#447 for further details. Signed-off-by: Joel Dice <joel.dice@fermyon.com> * add `preview2_adapter_badfd` test Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com> * Update `*_badfd` methods in the adapter to return an errno (#7672) Makes them a bit more consistent with the rest of the WASI functions as opposed to returning a boolean. --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com> Co-authored-by: Alex Crichton <alex@alexcrichton.com>
- Loading branch information
1 parent
759aa58
commit 64c2a24
Showing
5 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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) -> wasi::Errno; | ||
|
||
#[cfg_attr(target_arch = "wasm32", link_name = "adapter_close_badfd")] | ||
fn adapter_close_badfd(fd: u32) -> wasi::Errno; | ||
} | ||
|
||
unsafe { | ||
let mut fd = 0; | ||
assert_eq!(adapter_open_badfd(&mut fd), wasi::ERRNO_SUCCESS); | ||
|
||
assert_eq!(wasi::fd_close(fd), Err(wasi::ERRNO_BADF)); | ||
|
||
assert_eq!(wasi::fd_fdstat_get(fd).map(drop), Err(wasi::ERRNO_BADF)); | ||
|
||
assert_eq!(wasi::fd_fdstat_set_rights(fd, 0, 0), Err(wasi::ERRNO_BADF)); | ||
|
||
let mut buffer = [0_u8; 1]; | ||
assert_eq!( | ||
wasi::fd_read( | ||
fd, | ||
&[wasi::Iovec { | ||
buf: buffer.as_mut_ptr(), | ||
buf_len: 1 | ||
}] | ||
), | ||
Err(wasi::ERRNO_BADF) | ||
); | ||
|
||
assert_eq!( | ||
wasi::fd_write( | ||
fd, | ||
&[wasi::Ciovec { | ||
buf: buffer.as_ptr(), | ||
buf_len: 1 | ||
}] | ||
), | ||
Err(wasi::ERRNO_BADF) | ||
); | ||
|
||
assert_eq!(adapter_close_badfd(fd), wasi::ERRNO_SUCCESS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters