* fix Linux `isatty` implementation
* defer `WasiCtxBuilder` errors to `build()`; don't change API yet
This changes the fields on the builder to types that let the various `.arg()`, `.env()`, etc methods
infallible, so we don't have to worry about handling any errors till we actually build. This reduces
line noise when using a builder in a downstream application.
Deferring the processing of the builder fields also has the advantage of eliminating the opening and
closing of `/dev/null` for the default stdio file descriptors unless they're actually used by the
resulting `WasiCtx`.
Unicode errors when inheriting arguments and environment variables no longer cause a panic, but
instead go through `OsString`. We return `ENOTCAPABLE` at the end if there are NULs, or if UTF-8
conversion fails on Windows.
This also changes the bounds on some of the methods from `AsRef<str>` to `AsRef<[u8]>`. This
shouldn't break any existing code, but allows more flexibility when providing arguments. Depending
on the outcome of https://github.com/WebAssembly/WASI/issues/8 we may eventually want to require
these bytes be UTF-8, so we might want to revisit this later.
Finally, this fixes a tiny bug that could arise if we had exactly the maximum number of file
descriptors when populating the preopens.
* make `WasiCtxBuilder` method types less restrictive
This is a separate commit, since it changes the interface that downstream clients have to use, and
therefore requires a different commit of `wasmtime` for testing. That `wasmtime` commit is currently
on my private fork, so this will need to be amended before merging.
Now that failures are deferred until `WasiCtxBuilder::build()`, we don't need to have `Result` types
on the other methods any longer.
Additionally, using `IntoIterator` rather than `Iterator` as the trait bound for these methods is
slightly more general, and saves the client some typing.
* enforce that arguments and environment variables are valid UTF-8
* remove now-unnecessary platform-specific OsString handling
* `ENOTCAPABLE` -> `EILSEQ` for failed arg/env string conversions
* fix up comment style
* Apply @acfoltzer's fix to isatty on Linux to BSD
* Reimpleent fd_readdir on Linux using quasi-nix.
* Implement fd_readdir on Windows.
* wip
* Adapt to upstream changes.
* Cleanup dir.rs
* Address review
* Fix macos build
* host -> wasi
* Partially address review, more to come later.
* Address more review comments
* Fix compilation on Windows
* Implement clock_time_get on Windows.
Also update misc_testsuite to include latest clock_time_get test
changes.
* Try to somehow implement clock_res_get on Windows.
* Fix 55ms
* Cache the perf counter resolution
* Fix integration tests
* Use generated type bindings.
Use the witx API descriptions to generate the bulk of the contents of
host.rs, wasi.rs, and wasi32.rs.
This also prunes out many of the miscellaneous libc definitions from
those files which aren't currently in use by wasi-common. If there's
anything removed that's still needed by someone, it's easy to add things
back in.
* Remove unneeded iovec conversion routines.
* Migrate from failure to thiserror and anyhow
The failure crate invents its own traits that don't use
std::error::Error (because failure predates certain features added to
Error); this prevents using ? on an error from failure in a function
using Error. The thiserror and anyhow crates integrate with the standard
Error trait instead.
This change does not attempt to semantically change or refactor the
approach to error-handling in any portion of the code, to ensure that
the change remains straightforward to review. Modules using specific
differentiated error types move from failure_derive and derive(Fail) to
thiserror and derive(Error). Modules boxing all errors opaquely move
from failure::Error to anyhow. Modules using String as an error type
continue to do so. Code using unwrap or expect continues to do so.
Drop Display implementations when thiserror can easily derive an
identical instance.
Drop manual traversal of iter_causes; anyhow's Debug instance prints the
chain of causes by default.
Use anyhow's type alias anyhow::Result<T> in place of
std::result::Result<T, anyhow::Error> whenever possible.
* wasm2obj: Simplify error handling using existing messages
handle_module in wasm2obj manually maps
cranelift_codegen::isa::LookupError values to strings, but LookupError
values already have strings that say almost exactly the same thing.
Rely on the strings from cranelift.
* wasmtime: Rely on question-mark-in-main
The main() wrapper around rmain() completely matches the behavior of
question-mark-in-main (print error to stderr and return 1), so switch to
question-mark-in-main.
* Update to walrus 0.13 and wasm-webidl-bindings 0.6
Both crates switched from failure to anyhow; updating lets us avoid a
translation from failure to anyhow within wasmtime-interface-types.
This commit adds a utility routine
`strip_trailing_slashes_and_concatenate` which is common for
`path_rename` and `path_symlink` on Windows, and checks if the resolved
`PathGet` indeed contains a trailing slash(es) before striping them
off. Secondly, this commit fixes `path_rename_trailing_slashes` test
case by adding two additional checks for potentially erroneous
conditions, and raising `ENOTDIR` if any happens to be true.
Since `wasmtime` now uses `wasi` and `wasi32` modules, we can now
safely remove the `wasm32` module. This commit also updates `wasmtime`
to the latest upstream.
This commit fixes incorrect rights check in `fd_pwrite`. Until now,
we were erroneously checking whether the `Descriptor` has
`__WASI_RIGHT_FD_READ` rights instead of `__WASI_RIGHT_FD_WRITE`.
Additionally, this commit removes redundant borrows from
`wasi_ctx.get_fd_entry(..)` calls.
* Reorganize host.rs and wasm32.rs.
Reorganize host.rs and wasm32.rs into host.rs, wasi.rs, and wasi32.rs.
Most of the contents of host.rs was not actually host-specific, as most
of the types are fixed-size types like u32 or i64. These types are now
in wasi.rs.
The few types which do have pointer or usize-sized values now remain,
in two versions: host.rs has versions which use actual raw pointers and
usize, and wasi32.rs has versions which use u32 to represent them.
* Fix compilation on BSD
* Fix compilation on Windows
* Fully encapsulate endianness in memory.rs.
This refactors memory.rs to fully encapsulte endianness concerns, so
that outside that file, all values are in host-endian order.
This adds a dependency on the `num` crate, though it's only used for
the `PrimInt` trait, for handling endianness in a generic way.
* Use pub(crate).
Unfortunately the helpers added in #154 were only used from non-Windows
implementations, which caused compiler warnings on Windows.
This commit moves the helper to be unix-specific and removes the tiny wrapper
around calling `str_to_cstring` on `PathGet.path`.
* Fix path_get returning ENOTDIR when base not dir
This commit makes certain adjustments to `WasiCtx` and `FdEntry`
by introducing methods `get_nonvalidated_fd_entry` and
`get_nonvalidated_fd_entry_mut` which only check if the `FdEntry`
corresponding to the specified raw WASI fd exists in the context
object but **without** automatically validating the rights. Thanks
to postponing the validation until after the `FdEntry` object is
extracted from the context, it is possible to check if the extracted
`FdEntry` object is indeed a valid dir descriptor when processing
paths in `path_get` helper fn. In essence then, this commit closes
#158.
* Remove potentially useless FdObject struct
This commit removes `FdObject` struct which wasn't really used
in our codebase, and flattens its contents into `FdEntry`. IMHO,
this cleans up a fair amount of code and generally unclutters it.
* Refactor and document `WasiCtx` and `FdEntry`
This commit refactors `WasiCtx` struct by making the collection
of `FdEntry`s stored within private, and only allowing it to be
accessed via the provided set of modifier methods (push, insert,
remove, etc.).
Additionally, this commit documents the methods of `WasiCtx` and
`FdEntry` structs for easier debugging and maintenance in the future.
Return `ENOTDIR` instead of `ENOTCAPABLE` when `*at` functions are
given a non-directory as their base. This is in accordance with POSIX:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html
Currently wasi-libc contains some code to detect such cases and rewrite
`ENOTCAPABLE` to `ENOTDIR`, however it's better for WASI implementations
to just do the right thing in the first place.
* FixesCraneStation/wasmtime#440
This commit introduces a couple of changes/fixes:
* it annotates `log::debug!` messages with "host" to differentiate
between file descriptors stored on the WASI side (aka the wrappers)
and those managed by the host (aka the wrapped)
* it fixesCraneStation/wasmtime#440, i.e., incorrect passing of
file descriptor to `poll_oneoff` where currently errenously we
pass in the wrapper instead of the wrapped value
* it adds a couple more `log::debug!` macros calls for easier future
debugging
* Add partial refactorting to poll_oneoff
This commit lays the groundwork for more clean up to come in
subsequent commits.
* Finalise refactoring of `poll_oneoff`
* Fix compilation error on Windows
* Address majority of suggestions and refactor
Co-authored-by: Marcin Mielniczuk <marmistrz.dev@zoho.eu>
* Add poll_oneoff test case
* Leave timeout in nanoseconds in ClockEventData
Instead of converting the timeout value from nanoseconds to
milliseconds in the host-independent impl, move the conversion
to *nix-specific impl as the conversion is currently only warranted
by the POSIX `poll` syscall.
* Don't fail immediately on bad descriptor
If the user specifies an invalid descriptor inside a subscription,
don't fail immediately but rather generate an event with the thrown
WASI error code, and continue with the remaining, potentially
correct subscriptions.
This commit implements a simple helper for converting `&str` to `CString` and
mapping to the appropriate WASI error.
It also adds a `path_cstring` helper method in `PathGet` where the conversion was
used the most.
Fixes#104.
Instead of returning the debug formatting, which includes the enum
names, like `Io(...)`, just run the formatting function of the inner
error objects, which is nicer for command-line use.
* Fix some Windows warnings.
* Implement clock_time_get on Windows.
Also update misc_testsuite to include latest clock_time_get test
changes.
* improve comments
* Remove a leftover import.
Co-Authored-By: Jakub Konka <kubkon@jakubkonka.com>
This commit updates `poll_oneoff`'s API in a potentially least
invasive way. That is, it adds unused `WasiCtx` argument to the
syscall which will be required by #137. I am hopeful that this way
#137 can pass all tests and hence this commit should aid the review
process.
This commit provides a fix for `remove_directory_trailing_slashes`
test case on Windows. It adds a missing mapping between the following
WinAPI error code and WASI error:
```
ERROR_DIRECTORY => __WASI_ENOTDIR
```
where `ERROR_DIRECTORY` is thrown when the directory name is invalid.
* Switch lightbeam from `wabt` to `wast`
Switch from a C++-based `*.wat` parser to a Rust-based parser
* Remove unneeded `wabt` dev-dependency from wasmtime-api
* Rewrite `wasmtime-wast` crate with `wast-parser`
This commit moves the `wasmtime-wast` crate off the `wabt` crate on to
the `wast-parser` crate which is a Rust implementation of a `*.wast` and
`*.wat` parser. The intention here is to continue to reduce the amount
of C++ required to build wasmtime!
* Use new `wat` and `wast` crate names
We iterate over the preopens to present them to the WASI program, so
storing them in a `HashMap` means this order is nondeterministic. Switch
to a `Vec` of tuples instead. This means we don't eliminate duplicates,
but they should be rare.
* deps: bump wasmparser to 0.39.2
This has a bug fix for multi-value Wasm validation that is required for getting
the spec tests passing.
https://github.com/yurydelendik/wasmparser.rs/pull/135
* Update cranelift to 0.46.1 to get multi-value Wasm support
The `cranelift_wasm` APIs had to change a little bit to maintain state necessary
when translating multi-value Wasm blocks. The `translate_module` function now
returns a `ModuleTranslationState` that is borrowed during each function's
translation.
* Enable multi-value proposal's spec tests
This enables all the Wasm multi-value proposal's spec tests other than the ones
that rely on functions having more return values than registers available on the
target. That is not supported by cranelift yet.
* wasmtime-interface-types: always use multi-value Wasm
And remove the return pointer hacks that work around the lack of multi-value.
* Fixes `path_symlink_trailing_slashes` test case
This commit:
* adds a couple `log::debug!` macro calls in and around `path_get`
for easier future debugging
* changes impl of `path_symlink` hostcall to actually *require*
the final component (matching the impl of WASI in C)
* ignores the error `__WASI_ENOTDIR` in `path_get`'s `readlinkat` call
which is not meant to be an error at this stage (i.e., this
potentially erroneous condition *will be* handled later, in
one of the layers above)
* Fixes `path_symlink_trailing` slashes on BSD-nixes
This commit:
* makes `path_symlink` host-specific (Linux and BSD-like nixes
now have their own differing implementations)
* on BSD-like nixes, when `ENOTDIR` is returned from `symlinkat`
it checks whether the target path contains a trailing slash,
strips it, and then checks if the target path without the trailing
slash exists; if yes, then converts the error code to `EEXIST` to
match Linux/POSIX spec
This commit moves a couple of things around:
* separates the logic of `path_unlink_file` into separate impls
for linux and BSD-style nixes
* moves implementation consts into appropriate impl modules: linux
or bsd
* cleans up `utime_now` and `utime_omit` for BSD-style nixes
This commit fixes an issue with incorrect handling of /dev/(u)random
on Linux. It turns out that `nix::unistd::isatty` call handled only
the POSIX spec case where `ENOTTY` is returned in case the passed
in file descriptor is OK but not a TTY, whereas on Linux this is not
always the case. On Linux, it can be the case that `EINVAL` is returned
instead and this case AFAIK is not handled by the `nix` crate. This
commit fixes this by using `libc::isatty` syscall directly and checking
the return values.
This adds a `--always-lightbeam` option as well as an `--always-cranelift`
option, to allow the compilation strategy to be selected via the
command-line. This also enables regular testing for Lightbeam.