Browse Source

Auto-generate the `hostcalls` module of `wasi-common` (#846)

* Auto-generate shims for old `wasi_unstable` module

This commit is effectively just doing what #707 already did, but
applying it to the `snapshot_0` module as well. The end result is the
same, where we cut down on all the boilerplate in `snapshot_0` and bring
it in line with the main `wasi_snapshot_preview1` implementation. The
goal here is to make it easier to change the two in tandem since they're
both doing the same thing.

* Migrate `wasi_common::hostcalls` to a macro

This commit migrates the `hostcalls` module to being auto-generated by a
macro rather than duplicating a handwritten signature for each wasi
syscall.

* Auto-generate snapshot_0's `hostcalls` module

Similar to the previous commit, but for `snapshot_0`

* Delete the `wasi-common-cbindgen` crate

This is no longer needed with the hostcalls macro now, we can easily
fold the definition of the cbindgen macro into the same crate.

* Rustfmt

* Fix windows build errors

* Rustfmt

* Remove now no-longer-necessary code

* rustfmt
pull/853/head
Alex Crichton 5 years ago
committed by GitHub
parent
commit
5953215bac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      Cargo.lock
  2. 1
      crates/wasi-common/Cargo.toml
  3. 2
      crates/wasi-common/src/fs/dir.rs
  4. 265
      crates/wasi-common/src/fs/error.rs
  5. 29
      crates/wasi-common/src/fs/file.rs
  6. 1
      crates/wasi-common/src/fs/mod.rs
  7. 0
      crates/wasi-common/src/hostcalls.rs
  8. 263
      crates/wasi-common/src/hostcalls/fs.rs
  9. 88
      crates/wasi-common/src/hostcalls/misc.rs
  10. 7
      crates/wasi-common/src/hostcalls/mod.rs
  11. 43
      crates/wasi-common/src/hostcalls/sock.rs
  12. 15
      crates/wasi-common/src/hostcalls_impl/misc.rs
  13. 2
      crates/wasi-common/src/hostcalls_impl/mod.rs
  14. 36
      crates/wasi-common/src/hostcalls_impl/sock.rs
  15. 15
      crates/wasi-common/src/lib.rs
  16. 29
      crates/wasi-common/src/macros.rs
  17. 256
      crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs
  18. 82
      crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs
  19. 7
      crates/wasi-common/src/old/snapshot_0/hostcalls/mod.rs
  20. 43
      crates/wasi-common/src/old/snapshot_0/hostcalls/sock.rs
  21. 25
      crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs
  22. 20
      crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs
  23. 2
      crates/wasi-common/src/old/snapshot_0/hostcalls_impl/mod.rs
  24. 35
      crates/wasi-common/src/old/snapshot_0/hostcalls_impl/sock.rs
  25. 5
      crates/wasi-common/src/old/snapshot_0/mod.rs
  26. 22
      crates/wasi-common/wasi-common-cbindgen/Cargo.toml
  27. 220
      crates/wasi-common/wasi-common-cbindgen/LICENSE
  28. 157
      crates/wasi-common/wasi-common-cbindgen/src/lib.rs
  29. 18
      crates/wasi-common/wasi-common-cbindgen/tests/array_args.rs
  30. 18
      crates/wasi-common/wasi-common-cbindgen/tests/mut_args.rs
  31. 10
      crates/wasi-common/wasi-common-cbindgen/tests/no_args.rs
  32. 18
      crates/wasi-common/wasi-common-cbindgen/tests/ref_args.rs
  33. 9
      crates/wasi-common/wasi-common-cbindgen/tests/test.rs
  34. 10
      crates/wasi-common/wasi-common-cbindgen/tests/val_args.rs
  35. 5
      crates/wasi-common/wig/Cargo.toml
  36. 131
      crates/wasi-common/wig/src/hostcalls.rs
  37. 6
      crates/wasi-common/wig/src/lib.rs
  38. 17
      crates/wasi-common/wig/src/raw_types.rs
  39. 12
      crates/wasi-common/wig/src/utils.rs
  40. 21
      crates/wasi-common/wig/src/wasi.rs
  41. 1
      crates/wasi/src/instantiate.rs
  42. 125
      crates/wasi/src/old/snapshot_0/instantiate.rs
  43. 1
      crates/wasi/src/old/snapshot_0/mod.rs
  44. 1030
      crates/wasi/src/old/snapshot_0/syscalls.rs

31
Cargo.lock

@ -1860,20 +1860,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
[[package]]
name = "trybuild"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "987d6fdc45ddd7f3be5aa7386c8c8a844d1655c95b9ed948a9cd9cded8f2b79f"
dependencies = [
"glob",
"lazy_static",
"serde",
"serde_json",
"termcolor",
"toml",
]
[[package]]
name = "typemap"
version = "0.3.3"
@ -1992,23 +1978,12 @@ dependencies = [
"log",
"num",
"thiserror",
"wasi-common-cbindgen",
"wig",
"winapi",
"winx",
"yanix",
]
[[package]]
name = "wasi-common-cbindgen"
version = "0.9.0"
dependencies = [
"proc-macro2 1.0.7",
"quote 1.0.2",
"syn 1.0.13",
"trybuild",
]
[[package]]
name = "wasm-webidl-bindings"
version = "0.6.0"
@ -2427,11 +2402,13 @@ dependencies = [
[[package]]
name = "witx"
version = "0.6.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abc432537dbc9940e06816ebc3e1c4694fc835b90720615c32038769d7b2967d"
checksum = "a6ee25990cb94f39b8d5637b8c7d9b9eaaed76795d95f45342a7ef3d2e574931"
dependencies = [
"clap",
"log",
"pretty_env_logger",
"thiserror",
"wast 3.0.4",
]

1
crates/wasi-common/Cargo.toml

@ -11,7 +11,6 @@ readme = "README.md"
edition = "2018"
[dependencies]
wasi-common-cbindgen = { path = "wasi-common-cbindgen", version = "0.9.0" }
anyhow = "1.0"
thiserror = "1.0"
libc = "0.2"

2
crates/wasi-common/src/fs/dir.rs

@ -1,4 +1,4 @@
use crate::fs::{error::wasi_errno_to_io_error, File, OpenOptions, ReadDir};
use crate::fs::{File, OpenOptions, ReadDir};
use crate::{host, hostcalls, wasi, WasiCtx};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;

265
crates/wasi-common/src/fs/error.rs

@ -1,265 +0,0 @@
use crate::wasi;
use std::io;
/// Translate a WASI errno code into an `io::Result<()>`.
///
/// TODO: Would it be better to have our own version of `io::Error` (and
/// `io::Result`), rather than trying to shoehorn WASI errors into the
/// libstd version?
pub(crate) fn wasi_errno_to_io_error(errno: wasi::__wasi_errno_t) -> io::Result<()> {
#[cfg(unix)]
let raw_os_error = match errno {
wasi::__WASI_ERRNO_SUCCESS => return Ok(()),
wasi::__WASI_ERRNO_IO => libc::EIO,
wasi::__WASI_ERRNO_PERM => libc::EPERM,
wasi::__WASI_ERRNO_INVAL => libc::EINVAL,
wasi::__WASI_ERRNO_PIPE => libc::EPIPE,
wasi::__WASI_ERRNO_NOTCONN => libc::ENOTCONN,
wasi::__WASI_ERRNO_2BIG => libc::E2BIG,
wasi::__WASI_ERRNO_ACCES => libc::EACCES,
wasi::__WASI_ERRNO_ADDRINUSE => libc::EADDRINUSE,
wasi::__WASI_ERRNO_ADDRNOTAVAIL => libc::EADDRNOTAVAIL,
wasi::__WASI_ERRNO_AFNOSUPPORT => libc::EAFNOSUPPORT,
wasi::__WASI_ERRNO_AGAIN => libc::EAGAIN,
wasi::__WASI_ERRNO_ALREADY => libc::EALREADY,
wasi::__WASI_ERRNO_BADF => libc::EBADF,
wasi::__WASI_ERRNO_BADMSG => libc::EBADMSG,
wasi::__WASI_ERRNO_BUSY => libc::EBUSY,
wasi::__WASI_ERRNO_CANCELED => libc::ECANCELED,
wasi::__WASI_ERRNO_CHILD => libc::ECHILD,
wasi::__WASI_ERRNO_CONNABORTED => libc::ECONNABORTED,
wasi::__WASI_ERRNO_CONNREFUSED => libc::ECONNREFUSED,
wasi::__WASI_ERRNO_CONNRESET => libc::ECONNRESET,
wasi::__WASI_ERRNO_DEADLK => libc::EDEADLK,
wasi::__WASI_ERRNO_DESTADDRREQ => libc::EDESTADDRREQ,
wasi::__WASI_ERRNO_DOM => libc::EDOM,
wasi::__WASI_ERRNO_DQUOT => libc::EDQUOT,
wasi::__WASI_ERRNO_EXIST => libc::EEXIST,
wasi::__WASI_ERRNO_FAULT => libc::EFAULT,
wasi::__WASI_ERRNO_FBIG => libc::EFBIG,
wasi::__WASI_ERRNO_HOSTUNREACH => libc::EHOSTUNREACH,
wasi::__WASI_ERRNO_IDRM => libc::EIDRM,
wasi::__WASI_ERRNO_ILSEQ => libc::EILSEQ,
wasi::__WASI_ERRNO_INPROGRESS => libc::EINPROGRESS,
wasi::__WASI_ERRNO_INTR => libc::EINTR,
wasi::__WASI_ERRNO_ISCONN => libc::EISCONN,
wasi::__WASI_ERRNO_ISDIR => libc::EISDIR,
wasi::__WASI_ERRNO_LOOP => libc::ELOOP,
wasi::__WASI_ERRNO_MFILE => libc::EMFILE,
wasi::__WASI_ERRNO_MLINK => libc::EMLINK,
wasi::__WASI_ERRNO_MSGSIZE => libc::EMSGSIZE,
wasi::__WASI_ERRNO_MULTIHOP => libc::EMULTIHOP,
wasi::__WASI_ERRNO_NAMETOOLONG => libc::ENAMETOOLONG,
wasi::__WASI_ERRNO_NETDOWN => libc::ENETDOWN,
wasi::__WASI_ERRNO_NETRESET => libc::ENETRESET,
wasi::__WASI_ERRNO_NETUNREACH => libc::ENETUNREACH,
wasi::__WASI_ERRNO_NFILE => libc::ENFILE,
wasi::__WASI_ERRNO_NOBUFS => libc::ENOBUFS,
wasi::__WASI_ERRNO_NODEV => libc::ENODEV,
wasi::__WASI_ERRNO_NOENT => libc::ENOENT,
wasi::__WASI_ERRNO_NOEXEC => libc::ENOEXEC,
wasi::__WASI_ERRNO_NOLCK => libc::ENOLCK,
wasi::__WASI_ERRNO_NOLINK => libc::ENOLINK,
wasi::__WASI_ERRNO_NOMEM => libc::ENOMEM,
wasi::__WASI_ERRNO_NOMSG => libc::ENOMSG,
wasi::__WASI_ERRNO_NOPROTOOPT => libc::ENOPROTOOPT,
wasi::__WASI_ERRNO_NOSPC => libc::ENOSPC,
wasi::__WASI_ERRNO_NOSYS => libc::ENOSYS,
wasi::__WASI_ERRNO_NOTDIR => libc::ENOTDIR,
wasi::__WASI_ERRNO_NOTEMPTY => libc::ENOTEMPTY,
wasi::__WASI_ERRNO_NOTRECOVERABLE => libc::ENOTRECOVERABLE,
wasi::__WASI_ERRNO_NOTSOCK => libc::ENOTSOCK,
wasi::__WASI_ERRNO_NOTSUP => libc::ENOTSUP,
wasi::__WASI_ERRNO_NOTTY => libc::ENOTTY,
wasi::__WASI_ERRNO_NXIO => libc::ENXIO,
wasi::__WASI_ERRNO_OVERFLOW => libc::EOVERFLOW,
wasi::__WASI_ERRNO_OWNERDEAD => libc::EOWNERDEAD,
wasi::__WASI_ERRNO_PROTO => libc::EPROTO,
wasi::__WASI_ERRNO_PROTONOSUPPORT => libc::EPROTONOSUPPORT,
wasi::__WASI_ERRNO_PROTOTYPE => libc::EPROTOTYPE,
wasi::__WASI_ERRNO_RANGE => libc::ERANGE,
wasi::__WASI_ERRNO_ROFS => libc::EROFS,
wasi::__WASI_ERRNO_SPIPE => libc::ESPIPE,
wasi::__WASI_ERRNO_SRCH => libc::ESRCH,
wasi::__WASI_ERRNO_STALE => libc::ESTALE,
wasi::__WASI_ERRNO_TIMEDOUT => libc::ETIMEDOUT,
wasi::__WASI_ERRNO_TXTBSY => libc::ETXTBSY,
wasi::__WASI_ERRNO_XDEV => libc::EXDEV,
#[cfg(target_os = "wasi")]
wasi::__WASI_ERRNO_NOTCAPABLE => libc::ENOTCAPABLE,
#[cfg(not(target_os = "wasi"))]
wasi::__WASI_ERRNO_NOTCAPABLE => libc::EIO,
_ => panic!("unexpected wasi errno value"),
};
#[cfg(windows)]
use winapi::shared::winerror::*;
#[cfg(windows)]
let raw_os_error = match errno {
wasi::__WASI_ERRNO_SUCCESS => return Ok(()),
wasi::__WASI_ERRNO_INVAL => WSAEINVAL,
wasi::__WASI_ERRNO_PIPE => ERROR_BROKEN_PIPE,
wasi::__WASI_ERRNO_NOTCONN => WSAENOTCONN,
wasi::__WASI_ERRNO_PERM | wasi::__WASI_ERRNO_ACCES => ERROR_ACCESS_DENIED,
wasi::__WASI_ERRNO_ADDRINUSE => WSAEADDRINUSE,
wasi::__WASI_ERRNO_ADDRNOTAVAIL => WSAEADDRNOTAVAIL,
wasi::__WASI_ERRNO_AGAIN => WSAEWOULDBLOCK,
wasi::__WASI_ERRNO_CONNABORTED => WSAECONNABORTED,
wasi::__WASI_ERRNO_CONNREFUSED => WSAECONNREFUSED,
wasi::__WASI_ERRNO_CONNRESET => WSAECONNRESET,
wasi::__WASI_ERRNO_EXIST => ERROR_ALREADY_EXISTS,
wasi::__WASI_ERRNO_NOENT => ERROR_FILE_NOT_FOUND,
wasi::__WASI_ERRNO_TIMEDOUT => WSAETIMEDOUT,
wasi::__WASI_ERRNO_AFNOSUPPORT => WSAEAFNOSUPPORT,
wasi::__WASI_ERRNO_ALREADY => WSAEALREADY,
wasi::__WASI_ERRNO_BADF => WSAEBADF,
wasi::__WASI_ERRNO_DESTADDRREQ => WSAEDESTADDRREQ,
wasi::__WASI_ERRNO_DQUOT => WSAEDQUOT,
wasi::__WASI_ERRNO_FAULT => WSAEFAULT,
wasi::__WASI_ERRNO_HOSTUNREACH => WSAEHOSTUNREACH,
wasi::__WASI_ERRNO_INPROGRESS => WSAEINPROGRESS,
wasi::__WASI_ERRNO_INTR => WSAEINTR,
wasi::__WASI_ERRNO_ISCONN => WSAEISCONN,
wasi::__WASI_ERRNO_LOOP => WSAELOOP,
wasi::__WASI_ERRNO_MFILE => WSAEMFILE,
wasi::__WASI_ERRNO_MSGSIZE => WSAEMSGSIZE,
wasi::__WASI_ERRNO_NAMETOOLONG => WSAENAMETOOLONG,
wasi::__WASI_ERRNO_NETDOWN => WSAENETDOWN,
wasi::__WASI_ERRNO_NETRESET => WSAENETRESET,
wasi::__WASI_ERRNO_NETUNREACH => WSAENETUNREACH,
wasi::__WASI_ERRNO_NOBUFS => WSAENOBUFS,
wasi::__WASI_ERRNO_NOPROTOOPT => WSAENOPROTOOPT,
wasi::__WASI_ERRNO_NOTEMPTY => WSAENOTEMPTY,
wasi::__WASI_ERRNO_NOTSOCK => WSAENOTSOCK,
wasi::__WASI_ERRNO_PROTONOSUPPORT => WSAEPROTONOSUPPORT,
wasi::__WASI_ERRNO_PROTOTYPE => WSAEPROTOTYPE,
wasi::__WASI_ERRNO_STALE => WSAESTALE,
wasi::__WASI_ERRNO_IO
| wasi::__WASI_ERRNO_ISDIR
| wasi::__WASI_ERRNO_2BIG
| wasi::__WASI_ERRNO_BADMSG
| wasi::__WASI_ERRNO_BUSY
| wasi::__WASI_ERRNO_CANCELED
| wasi::__WASI_ERRNO_CHILD
| wasi::__WASI_ERRNO_DEADLK
| wasi::__WASI_ERRNO_DOM
| wasi::__WASI_ERRNO_FBIG
| wasi::__WASI_ERRNO_IDRM
| wasi::__WASI_ERRNO_ILSEQ
| wasi::__WASI_ERRNO_MLINK
| wasi::__WASI_ERRNO_MULTIHOP
| wasi::__WASI_ERRNO_NFILE
| wasi::__WASI_ERRNO_NODEV
| wasi::__WASI_ERRNO_NOEXEC
| wasi::__WASI_ERRNO_NOLCK
| wasi::__WASI_ERRNO_NOLINK
| wasi::__WASI_ERRNO_NOMEM
| wasi::__WASI_ERRNO_NOMSG
| wasi::__WASI_ERRNO_NOSPC
| wasi::__WASI_ERRNO_NOSYS
| wasi::__WASI_ERRNO_NOTDIR
| wasi::__WASI_ERRNO_NOTRECOVERABLE
| wasi::__WASI_ERRNO_NOTSUP
| wasi::__WASI_ERRNO_NOTTY
| wasi::__WASI_ERRNO_NXIO
| wasi::__WASI_ERRNO_OVERFLOW
| wasi::__WASI_ERRNO_OWNERDEAD
| wasi::__WASI_ERRNO_PROTO
| wasi::__WASI_ERRNO_RANGE
| wasi::__WASI_ERRNO_ROFS
| wasi::__WASI_ERRNO_SPIPE
| wasi::__WASI_ERRNO_SRCH
| wasi::__WASI_ERRNO_TXTBSY
| wasi::__WASI_ERRNO_XDEV
| wasi::__WASI_ERRNO_NOTCAPABLE => {
return Err(io::Error::new(io::ErrorKind::Other, error_str(errno)))
}
_ => panic!("unrecognized WASI errno value"),
} as i32;
Err(io::Error::from_raw_os_error(raw_os_error))
}
#[cfg(windows)]
fn error_str(errno: wasi::__wasi_errno_t) -> &'static str {
match errno {
wasi::__WASI_ERRNO_2BIG => "Argument list too long",
wasi::__WASI_ERRNO_ACCES => "Permission denied",
wasi::__WASI_ERRNO_ADDRINUSE => "Address in use",
wasi::__WASI_ERRNO_ADDRNOTAVAIL => "Address not available",
wasi::__WASI_ERRNO_AFNOSUPPORT => "Address family not supported by protocol",
wasi::__WASI_ERRNO_AGAIN => "Resource temporarily unavailable",
wasi::__WASI_ERRNO_ALREADY => "Operation already in progress",
wasi::__WASI_ERRNO_BADF => "Bad file descriptor",
wasi::__WASI_ERRNO_BADMSG => "Bad message",
wasi::__WASI_ERRNO_BUSY => "Resource busy",
wasi::__WASI_ERRNO_CANCELED => "Operation canceled",
wasi::__WASI_ERRNO_CHILD => "No child process",
wasi::__WASI_ERRNO_CONNABORTED => "Connection aborted",
wasi::__WASI_ERRNO_CONNREFUSED => "Connection refused",
wasi::__WASI_ERRNO_CONNRESET => "Connection reset by peer",
wasi::__WASI_ERRNO_DEADLK => "Resource deadlock would occur",
wasi::__WASI_ERRNO_DESTADDRREQ => "Destination address required",
wasi::__WASI_ERRNO_DOM => "Domain error",
wasi::__WASI_ERRNO_DQUOT => "Quota exceeded",
wasi::__WASI_ERRNO_EXIST => "File exists",
wasi::__WASI_ERRNO_FAULT => "Bad address",
wasi::__WASI_ERRNO_FBIG => "File too large",
wasi::__WASI_ERRNO_HOSTUNREACH => "Host is unreachable",
wasi::__WASI_ERRNO_IDRM => "Identifier removed",
wasi::__WASI_ERRNO_ILSEQ => "Illegal byte sequence",
wasi::__WASI_ERRNO_INPROGRESS => "Operation in progress",
wasi::__WASI_ERRNO_INTR => "Interrupted system call",
wasi::__WASI_ERRNO_INVAL => "Invalid argument",
wasi::__WASI_ERRNO_IO => "Remote I/O error",
wasi::__WASI_ERRNO_ISCONN => "Socket is connected",
wasi::__WASI_ERRNO_ISDIR => "Is a directory",
wasi::__WASI_ERRNO_LOOP => "Symbolic link loop",
wasi::__WASI_ERRNO_MFILE => "No file descriptors available",
wasi::__WASI_ERRNO_MLINK => "Too many links",
wasi::__WASI_ERRNO_MSGSIZE => "Message too large",
wasi::__WASI_ERRNO_MULTIHOP => "Multihop attempted",
wasi::__WASI_ERRNO_NAMETOOLONG => "Filename too long",
wasi::__WASI_ERRNO_NETDOWN => "Network is down",
wasi::__WASI_ERRNO_NETRESET => "Connection reset by network",
wasi::__WASI_ERRNO_NETUNREACH => "Network unreachable",
wasi::__WASI_ERRNO_NFILE => "Too many open files in system",
wasi::__WASI_ERRNO_NOBUFS => "No buffer space available",
wasi::__WASI_ERRNO_NODEV => "No such device",
wasi::__WASI_ERRNO_NOENT => "No such file or directory",
wasi::__WASI_ERRNO_NOEXEC => "Exec format error",
wasi::__WASI_ERRNO_NOLCK => "No locks available",
wasi::__WASI_ERRNO_NOLINK => "Link has been severed",
wasi::__WASI_ERRNO_NOMEM => "Out of memory",
wasi::__WASI_ERRNO_NOMSG => "No message of desired type",
wasi::__WASI_ERRNO_NOPROTOOPT => "Protocol not available",
wasi::__WASI_ERRNO_NOSPC => "No space left on device",
wasi::__WASI_ERRNO_NOSYS => "Function not implemented",
wasi::__WASI_ERRNO_NOTCONN => "Socket not connected",
wasi::__WASI_ERRNO_NOTDIR => "Not a directory",
wasi::__WASI_ERRNO_NOTEMPTY => "Directory not empty",
wasi::__WASI_ERRNO_NOTRECOVERABLE => "State not recoverable",
wasi::__WASI_ERRNO_NOTSOCK => "Not a socket",
wasi::__WASI_ERRNO_NOTSUP => "Not supported",
wasi::__WASI_ERRNO_NOTTY => "Not a tty",
wasi::__WASI_ERRNO_NXIO => "No such device or address",
wasi::__WASI_ERRNO_OVERFLOW => "Value too large for data type",
wasi::__WASI_ERRNO_OWNERDEAD => "Previous owner died",
wasi::__WASI_ERRNO_PERM => "Operation not permitted",
wasi::__WASI_ERRNO_PIPE => "Broken pipe",
wasi::__WASI_ERRNO_PROTO => "Protocol error",
wasi::__WASI_ERRNO_PROTONOSUPPORT => "Protocol not supported",
wasi::__WASI_ERRNO_PROTOTYPE => "Protocol wrong type for socket",
wasi::__WASI_ERRNO_RANGE => "Result not representable",
wasi::__WASI_ERRNO_ROFS => "Read-only file system",
wasi::__WASI_ERRNO_SPIPE => "Invalid seek",
wasi::__WASI_ERRNO_SRCH => "No such process",
wasi::__WASI_ERRNO_STALE => "Stale file handle",
wasi::__WASI_ERRNO_TIMEDOUT => "Operation timed out",
wasi::__WASI_ERRNO_TXTBSY => "Text file busy",
wasi::__WASI_ERRNO_XDEV => "Cross-device link",
wasi::__WASI_ERRNO_NOTCAPABLE => "Capabilities insufficient",
_ => panic!("unrecognized WASI errno value"),
}
}

29
crates/wasi-common/src/fs/file.rs

@ -1,5 +1,5 @@
use crate::fs::{error::wasi_errno_to_io_error, Metadata};
use crate::{host, hostcalls, wasi, WasiCtx};
use crate::fs::Metadata;
use crate::{host, hostcalls, hostcalls_impl, wasi, Result, WasiCtx};
use std::io;
/// A reference to an open file on the filesystem.
@ -34,8 +34,11 @@ impl<'ctx> File<'ctx> {
/// This corresponds to [`std::fs::File::sync_all`].
///
/// [`std::fs::File::sync_all`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.sync_all
pub fn sync_all(&self) -> io::Result<()> {
wasi_errno_to_io_error(unsafe { hostcalls::fd_sync(self.ctx, &mut [], self.fd) })
pub fn sync_all(&self) -> Result<()> {
unsafe {
hostcalls_impl::fd_sync(self.ctx, &mut [], self.fd)?;
}
Ok(())
}
/// This function is similar to `sync_all`, except that it may not synchronize
@ -44,8 +47,11 @@ impl<'ctx> File<'ctx> {
/// This corresponds to [`std::fs::File::sync_data`].
///
/// [`std::fs::File::sync_data`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.sync_data
pub fn sync_data(&self) -> io::Result<()> {
wasi_errno_to_io_error(unsafe { hostcalls::fd_datasync(self.ctx, &mut [], self.fd) })
pub fn sync_data(&self) -> Result<()> {
unsafe {
hostcalls_impl::fd_datasync(self.ctx, &mut [], self.fd)?;
}
Ok(())
}
/// Truncates or extends the underlying file, updating the size of this file
@ -54,10 +60,11 @@ impl<'ctx> File<'ctx> {
/// This corresponds to [`std::fs::File::set_len`].
///
/// [`std::fs::File::set_len`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.set_len
pub fn set_len(&self, size: u64) -> io::Result<()> {
wasi_errno_to_io_error(unsafe {
hostcalls::fd_filestat_set_size(self.ctx, &mut [], self.fd, size)
})
pub fn set_len(&self, size: u64) -> Result<()> {
unsafe {
hostcalls_impl::fd_filestat_set_size(self.ctx, &mut [], self.fd, size)?;
}
Ok(())
}
/// Queries metadata about the underlying file.
@ -65,7 +72,7 @@ impl<'ctx> File<'ctx> {
/// This corresponds to [`std::fs::File::metadata`].
///
/// [`std::fs::File::metadata`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.metadata
pub fn metadata(&self) -> io::Result<Metadata> {
pub fn metadata(&self) -> Result<Metadata> {
Ok(Metadata {})
}
}

1
crates/wasi-common/src/fs/mod.rs

@ -32,7 +32,6 @@
mod dir;
mod dir_builder;
mod dir_entry;
mod error;
mod file;
mod file_type;
mod metadata;

0
crates/wasi-common/src/hostcalls.rs

263
crates/wasi-common/src/hostcalls/fs.rs

@ -1,263 +0,0 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::{wasi, wasi32};
hostcalls! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_datasync(wasi_ctx: &WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pread(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pwrite(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_read(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_renumber(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
from: wasi::__wasi_fd_t,
to: wasi::__wasi_fd_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_seek(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filedelta_t,
whence: wasi::__wasi_whence_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_tell(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
fdstat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_flags(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
fdflags: wasi::__wasi_fdflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_sync(wasi_ctx: &WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_write(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_advise(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
advice: wasi::__wasi_advice_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_allocate(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_create_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_link(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_flags: wasi::__wasi_lookupflags_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_open(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
oflags: wasi::__wasi_oflags_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
fs_flags: wasi::__wasi_fdflags_t,
fd_out_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_readdir(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
buf: wasi32::uintptr_t,
buf_len: wasi32::size_t,
cookie: wasi::__wasi_dircookie_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_readlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_rename(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
st_size: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_set_times(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_symlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_unlink_file(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_remove_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
prestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_dir_name(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
}

88
crates/wasi-common/src/hostcalls/misc.rs

@ -1,88 +0,0 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::{wasi, wasi32};
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
pub unsafe fn proc_exit(_wasi_ctx: &WasiCtx, _memory: &mut [u8], rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
#[wasi_common_cbindgen]
pub unsafe fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> wasi::__wasi_errno_t {
unimplemented!("proc_raise")
}
hostcalls! {
pub unsafe fn args_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argv_ptr: wasi32::uintptr_t,
argv_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn args_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argc_ptr: wasi32::uintptr_t,
argv_buf_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_ptr: wasi32::uintptr_t,
environ_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_count_ptr: wasi32::uintptr_t,
environ_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn random_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_res_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
resolution_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_time_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
precision: wasi::__wasi_timestamp_t,
time_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn poll_oneoff(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
input: wasi32::uintptr_t,
output: wasi32::uintptr_t,
nsubscriptions: wasi32::size_t,
nevents: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn sched_yield(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
) -> wasi::__wasi_errno_t;
}

7
crates/wasi-common/src/hostcalls/mod.rs

@ -1,7 +0,0 @@
mod fs;
mod misc;
mod sock;
pub use self::fs::*;
pub use self::misc::*;
pub use self::sock::*;

43
crates/wasi-common/src/hostcalls/sock.rs

@ -1,43 +0,0 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
#![allow(unused)]
use crate::ctx::WasiCtx;
use crate::{wasi, wasi32};
use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
pub unsafe fn sock_recv(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
ri_data: wasi32::uintptr_t,
ri_data_len: wasi32::size_t,
ri_flags: wasi::__wasi_riflags_t,
ro_datalen: wasi32::uintptr_t,
ro_flags: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_recv")
}
#[wasi_common_cbindgen]
pub unsafe fn sock_send(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
si_data: wasi32::uintptr_t,
si_data_len: wasi32::size_t,
si_flags: wasi::__wasi_siflags_t,
so_datalen: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_send")
}
#[wasi_common_cbindgen]
pub unsafe fn sock_shutdown(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
how: wasi::__wasi_sdflags_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_shutdown")
}

15
crates/wasi-common/src/hostcalls_impl/misc.rs

@ -326,3 +326,18 @@ pub(crate) struct FdEventData<'a> {
pub(crate) r#type: wasi::__wasi_eventtype_t,
pub(crate) userdata: wasi::__wasi_userdata_t,
}
pub(crate) fn proc_exit(_wasi_ctx: &WasiCtx, _memory: &mut [u8], rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
pub(crate) fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> Result<()> {
unimplemented!("proc_raise")
}

2
crates/wasi-common/src/hostcalls_impl/mod.rs

@ -1,7 +1,9 @@
mod fs;
mod fs_helpers;
mod misc;
mod sock;
pub(crate) use self::fs::*;
pub(crate) use self::fs_helpers::PathGet;
pub(crate) use self::misc::*;
pub(crate) use self::sock::*;

36
crates/wasi-common/src/hostcalls_impl/sock.rs

@ -0,0 +1,36 @@
use crate::ctx::WasiCtx;
use crate::{wasi, wasi32, Result};
pub fn sock_recv(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_ri_data: wasi32::uintptr_t,
_ri_data_len: wasi32::size_t,
_ri_flags: wasi::__wasi_riflags_t,
_ro_datalen: wasi32::uintptr_t,
_ro_flags: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_recv")
}
pub fn sock_send(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_si_data: wasi32::uintptr_t,
_si_data_len: wasi32::size_t,
_si_flags: wasi::__wasi_siflags_t,
_so_datalen: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_send")
}
pub fn sock_shutdown(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_how: wasi::__wasi_sdflags_t,
) -> Result<()> {
unimplemented!("sock_shutdown")
}

15
crates/wasi-common/src/lib.rs

@ -24,20 +24,21 @@
mod ctx;
mod error;
mod fdentry;
mod helpers;
mod hostcalls_impl;
mod sandboxed_tty_writer;
mod sys;
#[macro_use]
mod macros;
pub mod fs;
mod helpers;
mod host;
pub mod hostcalls;
mod hostcalls_impl;
mod memory;
pub mod old;
mod sandboxed_tty_writer;
mod sys;
pub mod wasi;
pub mod wasi32;
pub mod hostcalls {
wig::define_hostcalls!("snapshot" "wasi_snapshot_preview1");
}
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub use sys::preopen_dir;

29
crates/wasi-common/src/macros.rs

@ -1,29 +0,0 @@
macro_rules! hostcalls {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen]
pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = crate::hostcalls_impl::$name($($arg,)*)
.err()
.unwrap_or(crate::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
)*)
}
// Like `hostcalls`, but uses `wasi_common_cbindgen_old`, which means
// it doesn't declare a non-mangled function name.
macro_rules! hostcalls_old {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen_old]
pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = crate::old::snapshot_0::hostcalls_impl::$name($($arg,)*)
.err()
.unwrap_or(crate::old::snapshot_0::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
)*)
}

256
crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs

@ -1,256 +0,0 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
hostcalls_old! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pread(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_pwrite(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
offset: wasi::__wasi_filesize_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_read(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nread: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_renumber(
wasi_ctx: &mut WasiCtx,
from: wasi::__wasi_fd_t,
to: wasi::__wasi_fd_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_seek(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filedelta_t,
whence: wasi::__wasi_whence_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_tell(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
newoffset: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
fdstat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_flags(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
fdflags: wasi::__wasi_fdflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
fd: wasi::__wasi_fd_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
pub unsafe fn fd_write(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
iovs_ptr: wasi32::uintptr_t,
iovs_len: wasi32::size_t,
nwritten: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_advise(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
advice: wasi::__wasi_advice_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_allocate(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_create_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_link(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_flags: wasi::__wasi_lookupflags_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_open(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
oflags: wasi::__wasi_oflags_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
fs_flags: wasi::__wasi_fdflags_t,
fd_out_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_readdir(
wasi_ctx: &mut WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
buf: wasi32::uintptr_t,
buf_len: wasi32::size_t,
cookie: wasi::__wasi_dircookie_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_readlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
buf_used: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_rename(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_dirfd: wasi::__wasi_fd_t,
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
new_dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
fd: wasi::__wasi_fd_t,
st_size: wasi::__wasi_filesize_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
filestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_filestat_set_times(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
dirflags: wasi::__wasi_lookupflags_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_symlink(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
old_path_ptr: wasi32::uintptr_t,
old_path_len: wasi32::size_t,
dirfd: wasi::__wasi_fd_t,
new_path_ptr: wasi32::uintptr_t,
new_path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_unlink_file(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn path_remove_directory(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
dirfd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
prestat_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn fd_prestat_dir_name(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasi::__wasi_fd_t,
path_ptr: wasi32::uintptr_t,
path_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
}

82
crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs

@ -1,82 +0,0 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen_old;
#[wasi_common_cbindgen_old]
pub unsafe fn proc_exit(rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
#[wasi_common_cbindgen_old]
pub unsafe fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> wasi::__wasi_errno_t {
unimplemented!("proc_raise")
}
hostcalls_old! {
pub unsafe fn args_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argv_ptr: wasi32::uintptr_t,
argv_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn args_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argc_ptr: wasi32::uintptr_t,
argv_buf_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_ptr: wasi32::uintptr_t,
environ_buf: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn environ_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_count_ptr: wasi32::uintptr_t,
environ_size_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn random_get(
memory: &mut [u8],
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_res_get(
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
resolution_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn clock_time_get(
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
precision: wasi::__wasi_timestamp_t,
time_ptr: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn poll_oneoff(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
input: wasi32::uintptr_t,
output: wasi32::uintptr_t,
nsubscriptions: wasi32::size_t,
nevents: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t;
pub unsafe fn sched_yield() -> wasi::__wasi_errno_t;
}

7
crates/wasi-common/src/old/snapshot_0/hostcalls/mod.rs

@ -1,7 +0,0 @@
mod fs;
mod misc;
mod sock;
pub use self::fs::*;
pub use self::misc::*;
pub use self::sock::*;

43
crates/wasi-common/src/old/snapshot_0/hostcalls/sock.rs

@ -1,43 +0,0 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
#![allow(unused)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{wasi, wasi32};
use wasi_common_cbindgen::wasi_common_cbindgen_old;
#[wasi_common_cbindgen_old]
pub unsafe fn sock_recv(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
ri_data: wasi32::uintptr_t,
ri_data_len: wasi32::size_t,
ri_flags: wasi::__wasi_riflags_t,
ro_datalen: wasi32::uintptr_t,
ro_flags: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_recv")
}
#[wasi_common_cbindgen_old]
pub unsafe fn sock_send(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
si_data: wasi32::uintptr_t,
si_data_len: wasi32::size_t,
si_flags: wasi::__wasi_siflags_t,
so_datalen: wasi32::uintptr_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_send")
}
#[wasi_common_cbindgen_old]
pub unsafe fn sock_shutdown(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
sock: wasi::__wasi_fd_t,
how: wasi::__wasi_sdflags_t,
) -> wasi::__wasi_errno_t {
unimplemented!("sock_shutdown")
}

25
crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs.rs

@ -16,7 +16,11 @@ use std::io::{self, Read, Seek, SeekFrom, Write};
use std::ops::DerefMut;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_close(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_close(fd={:?})", fd);
if let Ok(fe) = wasi_ctx.get_fd_entry(fd) {
@ -30,7 +34,11 @@ pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) ->
Ok(())
}
pub(crate) unsafe fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_datasync(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_datasync(fd={:?})", fd);
let fd = wasi_ctx
@ -175,6 +183,7 @@ pub(crate) unsafe fn fd_read(
pub(crate) unsafe fn fd_renumber(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
from: wasi::__wasi_fd_t,
to: wasi::__wasi_fd_t,
) -> Result<()> {
@ -291,6 +300,7 @@ pub(crate) unsafe fn fd_fdstat_get(
pub(crate) unsafe fn fd_fdstat_set_flags(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
fdflags: wasi::__wasi_fdflags_t,
) -> Result<()> {
@ -306,6 +316,7 @@ pub(crate) unsafe fn fd_fdstat_set_flags(
pub(crate) unsafe fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
fs_rights_base: wasi::__wasi_rights_t,
fs_rights_inheriting: wasi::__wasi_rights_t,
@ -329,7 +340,11 @@ pub(crate) unsafe fn fd_fdstat_set_rights(
Ok(())
}
pub(crate) unsafe fn fd_sync(wasi_ctx: &WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
pub(crate) unsafe fn fd_sync(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
) -> Result<()> {
trace!("fd_sync(fd={:?})", fd);
let fd = wasi_ctx
@ -397,6 +412,7 @@ pub(crate) unsafe fn fd_write(
pub(crate) unsafe fn fd_advise(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
@ -420,6 +436,7 @@ pub(crate) unsafe fn fd_advise(
pub(crate) unsafe fn fd_allocate(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
offset: wasi::__wasi_filesize_t,
len: wasi::__wasi_filesize_t,
@ -702,6 +719,7 @@ pub(crate) unsafe fn fd_filestat_get(
pub(crate) unsafe fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
st_atim: wasi::__wasi_timestamp_t,
st_mtim: wasi::__wasi_timestamp_t,
@ -761,6 +779,7 @@ pub(crate) fn fd_filestat_set_times_impl(
pub(crate) unsafe fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
_mem: &mut [u8],
fd: wasi::__wasi_fd_t,
st_size: wasi::__wasi_filesize_t,
) -> Result<()> {

20
crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs

@ -128,6 +128,7 @@ pub(crate) fn environ_sizes_get(
}
pub(crate) fn random_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
buf_ptr: wasi32::uintptr_t,
buf_len: wasi32::size_t,
@ -143,6 +144,7 @@ pub(crate) fn random_get(
}
pub(crate) fn clock_res_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
resolution_ptr: wasi32::uintptr_t,
@ -161,6 +163,7 @@ pub(crate) fn clock_res_get(
}
pub(crate) fn clock_time_get(
_wasi_ctx: &WasiCtx,
memory: &mut [u8],
clock_id: wasi::__wasi_clockid_t,
precision: wasi::__wasi_timestamp_t,
@ -180,7 +183,7 @@ pub(crate) fn clock_time_get(
enc_timestamp_byref(memory, time_ptr, time)
}
pub(crate) fn sched_yield() -> Result<()> {
pub(crate) fn sched_yield(_wasi_ctx: &WasiCtx, _memory: &mut [u8]) -> Result<()> {
trace!("sched_yield()");
std::thread::yield_now();
@ -314,3 +317,18 @@ pub(crate) struct FdEventData<'a> {
pub(crate) r#type: wasi::__wasi_eventtype_t,
pub(crate) userdata: wasi::__wasi_userdata_t,
}
pub(crate) fn proc_exit(_wasi_ctx: &WasiCtx, _memory: &mut [u8], rval: wasi::__wasi_exitcode_t) {
trace!("proc_exit(rval={:?})", rval);
// TODO: Rather than call std::process::exit here, we should trigger a
// stack unwind similar to a trap.
std::process::exit(rval as i32);
}
pub(crate) fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasi::__wasi_signal_t,
) -> Result<()> {
unimplemented!("proc_raise")
}

2
crates/wasi-common/src/old/snapshot_0/hostcalls_impl/mod.rs

@ -1,7 +1,9 @@
mod fs;
mod fs_helpers;
mod misc;
mod sock;
pub(crate) use self::fs::*;
pub(crate) use self::fs_helpers::PathGet;
pub(crate) use self::misc::*;
pub(crate) use self::sock::*;

35
crates/wasi-common/src/old/snapshot_0/hostcalls_impl/sock.rs

@ -0,0 +1,35 @@
use crate::old::snapshot_0::{wasi, wasi32, Result, WasiCtx};
pub fn sock_recv(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_ri_data: wasi32::uintptr_t,
_ri_data_len: wasi32::size_t,
_ri_flags: wasi::__wasi_riflags_t,
_ro_datalen: wasi32::uintptr_t,
_ro_flags: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_recv")
}
pub fn sock_send(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_si_data: wasi32::uintptr_t,
_si_data_len: wasi32::size_t,
_si_flags: wasi::__wasi_siflags_t,
_so_datalen: wasi32::uintptr_t,
) -> Result<()> {
unimplemented!("sock_send")
}
pub fn sock_shutdown(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sock: wasi::__wasi_fd_t,
_how: wasi::__wasi_sdflags_t,
) -> Result<()> {
unimplemented!("sock_shutdown")
}

5
crates/wasi-common/src/old/snapshot_0/mod.rs

@ -3,13 +3,16 @@ mod error;
mod fdentry;
mod helpers;
mod host;
pub mod hostcalls;
mod hostcalls_impl;
mod memory;
mod sys;
pub mod wasi;
pub mod wasi32;
pub mod hostcalls {
wig::define_hostcalls!("old/snapshot_0" "wasi_unstable");
}
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub type Error = error::Error;

22
crates/wasi-common/wasi-common-cbindgen/Cargo.toml

@ -1,22 +0,0 @@
[package]
name = "wasi-common-cbindgen"
version = "0.9.0"
authors = ["Jakub Konka <kubkon@jakubkonka.com>"]
description = "Interface generator utilities used by wasi-common"
license = "Apache-2.0 WITH LLVM-exception"
repository = "https://github.com/bytecodealliance/wasmtime"
edition = "2018"
[lib]
proc-macro = true
[dependencies]
syn = { version = "1.0.5", features = ["full"] }
quote = "1.0.2"
proc-macro2 = "1.0.6"
[dev-dependencies]
trybuild = "1.0.4"
[badges]
maintenance = { status = "actively-developed" }

220
crates/wasi-common/wasi-common-cbindgen/LICENSE

@ -1,220 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--- LLVM Exceptions to the Apache 2.0 License ----
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into an Object form of such source code, you
may redistribute such embedded portions in such Object form without complying
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
In addition, if you combine or link compiled forms of this Software with
software that is licensed under the GPLv2 ("Combined Software") and if a
court of competent jurisdiction determines that the patent provision (Section
3), the indemnity provision (Section 9) or other Section of the License
conflicts with the conditions of the GPLv2, you may retroactively and
prospectively choose to deem waived or otherwise exclude such Section(s) of
the License, but only in their entirety and only with respect to the Combined
Software.

157
crates/wasi-common/wasi-common-cbindgen/src/lib.rs

@ -1,157 +0,0 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{FnArg, Pat, PatType, Type, TypeReference, TypeSlice};
fn capture_input_args(
function: &syn::ItemFn,
) -> (
Vec<proc_macro2::TokenStream>,
Vec<proc_macro2::TokenStream>,
Vec<proc_macro2::TokenStream>,
) {
let mut arg_ident = Vec::new();
let mut arg_type = Vec::new();
let mut call_arg_ident = Vec::new();
for input in &function.sig.inputs {
match input {
FnArg::Typed(PatType {
attrs,
pat,
colon_token: _,
ty,
}) => {
// parse arg identifier
let ident = if let Pat::Ident(ident) = &**pat {
&ident.ident
} else {
panic!("expected function input to be an identifier")
};
if !attrs.is_empty() {
panic!("unsupported attributes on function arg");
}
arg_ident.push(quote!(#ident));
// parse arg type
if let Type::Reference(ty @ TypeReference { .. }) = &**ty {
// if we're here, then we found a &-ref
// so substitute it for *mut since we're exporting to C
let elem = &*ty.elem;
if let Type::Slice(elem @ TypeSlice { .. }) = &elem {
// slice: &[type] or &mut [type]
// in C it requires a signature *mut type
let elem = &elem.elem;
arg_type.push(quote!(*mut #elem));
// since it's a slice, we'll need to do more work here
// simple dereferencing is not enough
// firstly, we need to add a len arg to C fn
// secondly, we need to invoke std::slice::from_raw_parts_mut(..)
let concatenated = format!("{}_len", ident);
let len_ident = syn::Ident::new(&concatenated, ident.span());
call_arg_ident.push(quote! {
std::slice::from_raw_parts_mut(#ident, #len_ident)
});
arg_ident.push(quote!(#len_ident));
arg_type.push(quote!(usize));
} else {
// & or &mut type; substitute with *const or *mut type.
// Also, we need to properly dereference the substituted raw
// pointer if we are to properly call the hostcall fn.
if ty.mutability.is_none() {
arg_type.push(quote!(*const #elem));
call_arg_ident.push(quote!(&*#ident));
} else {
arg_type.push(quote!(*mut #elem));
call_arg_ident.push(quote!(&mut *#ident));
}
}
} else {
arg_type.push(quote!(#ty));
// non-&-ref type, so preserve whatever the arg was
call_arg_ident.push(quote!(#ident));
}
}
_ => {
unimplemented!("unrecognized function input pattern");
}
}
}
(arg_ident, arg_type, call_arg_ident)
}
#[proc_macro_attribute]
pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenStream {
assert!(attr.is_empty());
let function = syn::parse_macro_input!(function as syn::ItemFn);
// capture visibility
let vis = &function.vis;
// generate C fn name prefixed with wasi_common_
let fn_ident = &function.sig.ident;
let concatenated = format!("wasi_common_{}", fn_ident);
let c_fn_ident = syn::Ident::new(&concatenated, fn_ident.span());
// capture input args
let (arg_ident, arg_type, call_arg_ident) = capture_input_args(&function);
// capture output arg
let output = &function.sig.output;
let result = quote! {
#function
#[no_mangle]
#vis unsafe extern "C" fn #c_fn_ident(
#(
#arg_ident: #arg_type,
)*
) #output {
#fn_ident(#(
#call_arg_ident,
)*)
}
};
result.into()
}
#[proc_macro_attribute]
pub fn wasi_common_cbindgen_old(attr: TokenStream, function: TokenStream) -> TokenStream {
assert!(attr.is_empty());
let function = syn::parse_macro_input!(function as syn::ItemFn);
// capture visibility
let vis = &function.vis;
// generate C fn name prefixed with old_wasi_common_
let fn_ident = &function.sig.ident;
let concatenated = format!("old_wasi_common_{}", fn_ident);
let c_fn_ident = syn::Ident::new(&concatenated, fn_ident.span());
// capture input args
let (arg_ident, arg_type, call_arg_ident) = capture_input_args(&function);
// capture output arg
let output = &function.sig.output;
let result = quote! {
#function
#[no_mangle]
#vis unsafe extern "C" fn #c_fn_ident(
#(
#arg_ident: #arg_type,
)*
) #output {
#fn_ident(#(
#call_arg_ident,
)*)
}
};
result.into()
}

18
crates/wasi-common/wasi-common-cbindgen/tests/array_args.rs

@ -1,18 +0,0 @@
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn array_args(a: &mut [u8]) {
a[0] = 1;
}
fn main() {
let mut expected: &mut [u8] = &mut [0, 0];
array_args(&mut expected);
let given: &mut [u8] = &mut [0, 0];
unsafe {
wasi_common_array_args(given.as_mut_ptr(), given.len());
}
assert_eq!(given, expected);
}

18
crates/wasi-common/wasi-common-cbindgen/tests/mut_args.rs

@ -1,18 +0,0 @@
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn mut_args(a: &mut usize) {
*a = *a + 1
}
fn main() {
let mut expected = Box::new(2);
mut_args(expected.as_mut());
let given = unsafe {
let given = Box::new(2);
let raw = Box::into_raw(given);
wasi_common_mut_args(raw);
Box::from_raw(raw)
};
assert_eq!(*given, *expected);
}

10
crates/wasi-common/wasi-common-cbindgen/tests/no_args.rs

@ -1,10 +0,0 @@
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn no_args() -> u32 {
0
}
fn main() {
assert_eq!(unsafe { wasi_common_no_args() }, no_args());
}

18
crates/wasi-common/wasi-common-cbindgen/tests/ref_args.rs

@ -1,18 +0,0 @@
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn ref_args(a: &usize) -> usize {
a + 1
}
fn main() {
let a = Box::new(2);
let expected = ref_args(a.as_ref());
let given = unsafe {
let raw = Box::into_raw(a);
let res = wasi_common_ref_args(raw);
Box::from_raw(raw);
res
};
assert_eq!(given, expected);
}

9
crates/wasi-common/wasi-common-cbindgen/tests/test.rs

@ -1,9 +0,0 @@
#[test]
fn tests() {
let t = trybuild::TestCases::new();
t.pass("tests/no_args.rs");
t.pass("tests/val_args.rs");
t.pass("tests/ref_args.rs");
t.pass("tests/mut_args.rs");
t.pass("tests/array_args.rs");
}

10
crates/wasi-common/wasi-common-cbindgen/tests/val_args.rs

@ -1,10 +0,0 @@
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn val_args(a: usize, b: usize) -> usize {
a + b
}
fn main() {
assert_eq!(unsafe { wasi_common_val_args(1, 2) }, val_args(1, 2));
}

5
crates/wasi-common/wig/Cargo.toml

@ -17,10 +17,7 @@ proc-macro = true
quote = "1.0.2"
proc-macro2 = "1.0.6"
heck = "0.3.1"
# We include the WASI repo primarily for the witx files, but it's also useful
# to use the witx parser it contains, rather than the witx crate from
# crates.io, so that it always matches the version of the witx files.
witx = "0.6.0"
witx = "0.7.0"
[badges]
maintenance = { status = "actively-developed" }

131
crates/wasi-common/wig/src/hostcalls.rs

@ -0,0 +1,131 @@
use crate::utils;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
pub fn define(args: TokenStream) -> TokenStream {
let (path, phase) = utils::witx_path_from_args(args);
let doc = match witx::load(&[&path]) {
Ok(doc) => doc,
Err(e) => {
panic!("error opening file {}: {}", path, e);
}
};
let mut ret = TokenStream::new();
let old = match phase.as_str() {
"snapshot" => false,
"old/snapshot_0" => true,
s => panic!("unsupported phase: {}", s),
};
for module in doc.modules() {
for func in module.funcs() {
ret.extend(generate_wrappers(&func, old));
}
}
return ret;
}
fn generate_wrappers(func: &witx::InterfaceFunc, old: bool) -> TokenStream {
let name = format_ident!("{}", func.name.as_str());
let mut arg_declarations = Vec::new();
let mut arg_names = Vec::new();
for param in func.params.iter() {
let name = utils::param_name(param);
if let witx::TypePassedBy::PointerLengthPair = param.tref.type_().passed_by() {
let ptr = format_ident!("{}_ptr", name);
let len = format_ident!("{}_len", name);
arg_declarations.push(quote! { #ptr: super::wasi32::uintptr_t });
arg_declarations.push(quote! { #len: super::wasi32::size_t });
arg_names.push(ptr);
arg_names.push(len);
continue;
}
match &param.tref {
witx::TypeRef::Name(n) => {
if n.name.as_str() == "size" {
arg_declarations.push(quote! { #name: super::wasi32::size_t });
} else {
let ty_name = format_ident!("__wasi_{}_t", n.name.as_str());
arg_declarations.push(quote! { #name: super::wasi::#ty_name });
}
}
witx::TypeRef::Value(v) => match &**v {
witx::Type::ConstPointer(_) | witx::Type::Pointer(_) => {
arg_declarations.push(quote! { #name: super::wasi32::uintptr_t });
}
_ => panic!("unexpected value type"),
},
}
arg_names.push(name);
}
let mut ret = quote!(());
for (i, result) in func.results.iter().enumerate() {
if i == 0 {
match &result.tref {
witx::TypeRef::Name(n) => {
let ty_name = format_ident!("__wasi_{}_t", n.name.as_str());
ret = quote! { super::wasi::#ty_name };
}
witx::TypeRef::Value(_) => panic!("unexpected value type"),
}
continue;
}
let name = utils::param_name(result);
arg_declarations.push(quote! { #name: super::wasi32::uintptr_t });
arg_names.push(name);
}
let call = quote! {
super::hostcalls_impl::#name(wasi_ctx, memory, #(#arg_names,)*)
};
let body = if func.results.len() == 0 {
call
} else {
quote! {
let ret = #call
.err()
.unwrap_or(super::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
};
let c_abi_name = if old {
format_ident!("old_wasi_common_{}", name)
} else {
format_ident!("wasi_common_{}", name)
};
quote! {
pub unsafe fn #name(
wasi_ctx: &mut super::WasiCtx,
memory: &mut [u8],
#(#arg_declarations,)*
) -> #ret {
#body
}
#[no_mangle]
pub unsafe fn #c_abi_name(
wasi_ctx: *mut super::WasiCtx,
memory: *mut u8,
memory_len: usize,
#(#arg_declarations,)*
) -> #ret {
#name(
&mut *wasi_ctx,
std::slice::from_raw_parts_mut(memory, memory_len),
#(#arg_names,)*
)
}
}
}

6
crates/wasi-common/wig/src/lib.rs

@ -1,5 +1,6 @@
extern crate proc_macro;
mod hostcalls;
mod raw_types;
mod utils;
mod wasi;
@ -36,3 +37,8 @@ pub fn witx_wasi32_types(args: TokenStream) -> TokenStream {
pub fn define_add_wrappers_to_module(args: TokenStream) -> TokenStream {
wasi::add_wrappers_to_module(args.into()).into()
}
#[proc_macro]
pub fn define_hostcalls(args: TokenStream) -> TokenStream {
hostcalls::define(args.into()).into()
}

17
crates/wasi-common/wig/src/raw_types.rs

@ -50,12 +50,13 @@ fn gen_datatypes(output: &mut TokenStream, doc: &witx::Document, mode: Mode) {
fn gen_datatype(output: &mut TokenStream, mode: Mode, namedtype: &witx::NamedType) {
let wasi_name = format_ident!("__wasi_{}_t", namedtype.name.as_str());
match &namedtype.dt {
match &namedtype.tref {
witx::TypeRef::Name(alias_to) => {
let to = tref_tokens(mode, &alias_to.dt);
let to = tref_tokens(mode, &alias_to.tref);
output.extend(quote!(pub type #wasi_name = #to;));
}
witx::TypeRef::Value(v) => match &**v {
witx::Type::Int(_) => panic!("unsupported int datatype"),
witx::Type::Enum(e) => {
let repr = int_repr_tokens(e.repr);
output.extend(quote!(pub type #wasi_name = #repr;));
@ -143,7 +144,7 @@ fn gen_datatype(output: &mut TokenStream, mode: Mode, namedtype: &witx::NamedTyp
witx::Type::Pointer { .. }
| witx::Type::ConstPointer { .. }
| witx::Type::Array { .. } => {
let tref_tokens = tref_tokens(mode, &namedtype.dt);
let tref_tokens = tref_tokens(mode, &namedtype.tref);
output.extend(quote!(pub type #wasi_name = #tref_tokens;));
}
},
@ -156,7 +157,7 @@ fn gen_datatype(output: &mut TokenStream, mode: Mode, namedtype: &witx::NamedTyp
}
fn gen_errno_strerror(output: &mut TokenStream, namedtype: &witx::NamedType) {
let inner = match &namedtype.dt {
let inner = match &namedtype.tref {
witx::TypeRef::Value(v) => match &**v {
witx::Type::Enum(e) => e,
x => panic!("expected Enum('errno'), instead received {:?}", x),
@ -198,6 +199,7 @@ fn builtin_tokens(mode: Mode, builtin: witx::BuiltinType) -> TokenStream {
Mode::Wasi => panic!("strings have target-specific size"),
Mode::Wasi32 => quote!((u32, u32)),
},
witx::BuiltinType::Char8 => quote!(i8),
witx::BuiltinType::U8 => quote!(u8),
witx::BuiltinType::U16 => quote!(u16),
witx::BuiltinType::U32 => quote!(u32),
@ -208,6 +210,11 @@ fn builtin_tokens(mode: Mode, builtin: witx::BuiltinType) -> TokenStream {
witx::BuiltinType::S64 => quote!(i64),
witx::BuiltinType::F32 => quote!(f32),
witx::BuiltinType::F64 => quote!(f64),
witx::BuiltinType::USize => match mode {
Mode::Host => quote!(usize),
Mode::Wasi => panic!("usize has target-specific size"),
Mode::Wasi32 => quote!(u32),
},
}
}
@ -270,7 +277,7 @@ fn namedtype_has_target_size(nt: &witx::NamedType) -> bool {
if nt.name.as_str() == "size" {
true
} else {
tref_has_target_size(&nt.dt)
tref_has_target_size(&nt.tref)
}
}

12
crates/wasi-common/wig/src/utils.rs

@ -1,4 +1,4 @@
use proc_macro2::{Literal, TokenStream, TokenTree};
use proc_macro2::{Ident, Literal, TokenStream, TokenTree};
/// Given the input tokens to a macro invocation, return the path to the
/// witx file to process.
@ -53,3 +53,13 @@ fn parse_string_literal(literal: Literal) -> String {
trimmed
}
pub fn param_name(param: &witx::InterfaceFuncParam) -> Ident {
quote::format_ident!(
"{}",
match param.name.as_str() {
"in" | "type" => format!("r#{}", param.name.as_str()),
s => s.to_string(),
}
)
}

21
crates/wasi-common/wig/src/wasi.rs

@ -45,13 +45,7 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
let mut hostcall_args = Vec::new();
for param in func.params.iter() {
let name = format_ident!(
"{}",
match param.name.as_str() {
"in" | "type" => format!("r#{}", param.name.as_str()),
s => s.to_string(),
}
);
let name = utils::param_name(param);
// Registers a new parameter to the shim we're making with the
// given `name`, the `abi_ty` wasm type and `hex` defines
@ -89,6 +83,11 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
};
match &*param.tref.type_() {
witx::Type::Int(e) => match e.repr {
witx::IntRepr::U64 => add_param(&name, Abi::I64, false),
_ => add_param(&name, Abi::I32, false),
},
witx::Type::Enum(e) => match e.repr {
witx::IntRepr::U64 => add_param(&name, Abi::I64, false),
_ => add_param(&name, Abi::I32, false),
@ -99,12 +98,14 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
_ => add_param(&name, Abi::I32, true),
},
witx::Type::Builtin(witx::BuiltinType::S8)
witx::Type::Builtin(witx::BuiltinType::Char8)
| witx::Type::Builtin(witx::BuiltinType::S8)
| witx::Type::Builtin(witx::BuiltinType::U8)
| witx::Type::Builtin(witx::BuiltinType::S16)
| witx::Type::Builtin(witx::BuiltinType::U16)
| witx::Type::Builtin(witx::BuiltinType::S32)
| witx::Type::Builtin(witx::BuiltinType::U32) => {
| witx::Type::Builtin(witx::BuiltinType::U32)
| witx::Type::Builtin(witx::BuiltinType::USize) => {
add_param(&name, Abi::I32, false);
}
@ -208,7 +209,7 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
Ok(e) => e,
Err(e) => #handle_early_error,
};
wasi_common::hostcalls::#name_ident(
hostcalls::#name_ident(
wasi_ctx,
memory,
#(#hostcall_args),*

1
crates/wasi/src/instantiate.rs

@ -5,6 +5,7 @@ use cranelift_wasm::DefinedFuncIndex;
use std::fs::File;
use std::rc::Rc;
use target_lexicon::HOST;
use wasi_common::hostcalls;
use wasi_common::wasi;
use wasi_common::{WasiCtx, WasiCtxBuilder};
use wasmtime_environ::{translate_signature, Export, Module};

125
crates/wasi/src/old/snapshot_0/instantiate.rs

@ -1,4 +1,3 @@
use super::syscalls;
use cranelift_codegen::ir::types;
use cranelift_codegen::{ir, isa};
use cranelift_entity::PrimaryMap;
@ -6,9 +5,11 @@ use cranelift_wasm::DefinedFuncIndex;
use std::fs::File;
use std::rc::Rc;
use target_lexicon::HOST;
use wasi_common::old::snapshot_0::hostcalls;
use wasi_common::old::snapshot_0::{WasiCtx, WasiCtxBuilder};
use wasi_common::wasi;
use wasmtime_environ::{translate_signature, Export, Module};
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMFunctionBody};
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMContext};
/// Creates `wasmtime::Instance` object implementing the "wasi" interface.
pub fn create_wasi_instance(
@ -59,79 +60,19 @@ pub fn instantiate_wasi_with_context(
) -> Result<InstanceHandle, InstantiationError> {
let pointer_type = types::Type::triple_pointer_type(&HOST);
let mut module = Module::new();
let mut finished_functions: PrimaryMap<DefinedFuncIndex, *const VMFunctionBody> =
PrimaryMap::new();
let mut finished_functions = PrimaryMap::new();
let call_conv = isa::CallConv::triple_default(&HOST);
macro_rules! signature {
($name:ident) => {{
let sig = module.signatures.push(translate_signature(
ir::Signature {
params: syscalls::$name::params()
.into_iter()
.map(ir::AbiParam::new)
.collect(),
returns: syscalls::$name::results()
.into_iter()
.map(ir::AbiParam::new)
.collect(),
call_conv,
},
pointer_type,
));
let func = module.functions.push(sig);
module
.exports
.insert(stringify!($name).to_owned(), Export::Function(func));
finished_functions.push(syscalls::$name::SHIM as *const VMFunctionBody);
}};
}
signature!(args_get);
signature!(args_sizes_get);
signature!(clock_res_get);
signature!(clock_time_get);
signature!(environ_get);
signature!(environ_sizes_get);
signature!(fd_prestat_get);
signature!(fd_prestat_dir_name);
signature!(fd_close);
signature!(fd_datasync);
signature!(fd_pread);
signature!(fd_pwrite);
signature!(fd_read);
signature!(fd_renumber);
signature!(fd_seek);
signature!(fd_tell);
signature!(fd_fdstat_get);
signature!(fd_fdstat_set_flags);
signature!(fd_fdstat_set_rights);
signature!(fd_sync);
signature!(fd_write);
signature!(fd_advise);
signature!(fd_allocate);
signature!(path_create_directory);
signature!(path_link);
signature!(path_open);
signature!(fd_readdir);
signature!(path_readlink);
signature!(path_rename);
signature!(fd_filestat_get);
signature!(fd_filestat_set_times);
signature!(fd_filestat_set_size);
signature!(path_filestat_get);
signature!(path_filestat_set_times);
signature!(path_symlink);
signature!(path_unlink_file);
signature!(path_remove_directory);
signature!(poll_oneoff);
signature!(proc_exit);
signature!(proc_raise);
signature!(random_get);
signature!(sched_yield);
signature!(sock_recv);
signature!(sock_send);
signature!(sock_shutdown);
// This function is defined in the macro invocation of
// `define_add_wrappers_to_module` below. For more information about how
// this works it'd recommended to read the source in
// `crates/wasi-common/wig/src/wasi.rs`.
add_wrappers_to_module(
&mut module,
&mut finished_functions,
call_conv,
pointer_type,
);
let imports = Imports::none();
let data_initializers = Vec::new();
@ -147,3 +88,41 @@ pub fn instantiate_wasi_with_context(
Box::new(wasi_ctx),
)
}
// Used by `add_wrappers_to_module` defined in the macro above
fn get_wasi_ctx(vmctx: &mut VMContext) -> Result<&mut WasiCtx, wasi::__wasi_errno_t> {
unsafe {
vmctx
.host_state()
.downcast_mut::<WasiCtx>()
.ok_or_else(|| panic!("no host state named WasiCtx available"))
}
}
// Used by `add_wrappers_to_module` defined in the macro above
fn get_memory(caller_vmctx: &mut VMContext) -> Result<&mut [u8], wasi::__wasi_errno_t> {
match unsafe { InstanceHandle::from_vmctx(caller_vmctx) }.lookup("memory") {
Some(wasmtime_runtime::Export::Memory {
definition,
vmctx: _,
memory: _,
}) => unsafe {
let definition = &*definition;
let ptr = definition.base;
let len = definition.current_length;
Ok(std::slice::from_raw_parts_mut(ptr, len))
},
Some(export) => {
log::error!("export named \"memory\" isn't a memory: {:?}", export);
Err(wasi::__WASI_ERRNO_INVAL)
}
None => {
log::error!("no export named \"memory\" available from caller");
Err(wasi::__WASI_ERRNO_INVAL)
}
}
}
wig::define_add_wrappers_to_module!(
"old/snapshot_0" "wasi_unstable"
);

1
crates/wasi/src/old/snapshot_0/mod.rs

@ -1,7 +1,6 @@
extern crate alloc;
mod instantiate;
mod syscalls;
pub use instantiate::{create_wasi_instance, instantiate_wasi, instantiate_wasi_with_context};

1030
crates/wasi/src/old/snapshot_0/syscalls.rs

File diff suppressed because it is too large
Loading…
Cancel
Save