Commit Graph

217 Commits

Author SHA1 Message Date
Jakub Konka
f4d3f08fc6 Fix path_get returning ENOTDIR when base not dir (#159)
* 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.
2019-10-30 14:41:43 -07:00
Dan Gohman
0302f1a164 Make path_get return ENOTDIR when the base isn't a directory.
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.
2019-10-30 10:20:42 +01:00
Jakub Konka
f3a5186230 Refactor poll_oneoff on *nix (#137)
* Fixes CraneStation/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 fixes CraneStation/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.
2019-10-30 09:57:59 +01:00
Dan Gohman
aecec187d7 Check for overflow when incrementing the preopen_fd counter. (#157)
If someone somehow defines more preopens than can fit in the
`__wasi_fd_t` index space, we should detect it instead of silently
wrapping around.
2019-10-29 17:11:43 -07:00
Peter Huene
de71dbec0d Implement a helper for converting &str to CString. (#154)
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.
2019-10-29 16:30:22 -07:00
Dan Gohman
9a5c53b3ed Make wasi-common's Error messages prettier. (#156)
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.
2019-10-29 15:46:11 -07:00
Marcin Mielniczuk
2659641132 Implement clock_time_get on Windows (#119)
* 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>
2019-10-29 15:05:42 +01:00
Jakub Konka
bb5c879718 Fixes path_symlink_trailing_slashes on Windows
This commit:
* adds missing `ERROR_ALREADY_EXISTS => __WASI_EEXIST` mapping
* re-routes Win errors into correct WASI values in `symlink_*`
  fns when target exists and/or contains a trailing slash
* remaps `ERROR_INVALID_NAME => __WASI_ENOENT`
2019-10-29 11:04:16 +01:00
Jakub Konka
59bbfbc0d7 Remove needs_close from FdObject
This commit removes `needs_close` field from `FdObject`, and
stores the underlying `Descriptor` without the `ManuallyDrop`
wrapper.
2019-10-25 17:39:47 +02:00
Maxim Vorobjov
6eb6e342d7 Rebuild misc tests when any of test files change (#150)
* rebuild misc tests when any of test file changes

* fix build formatting

* file change validation -> build_and_generate_tests
2019-10-25 15:48:58 +02:00
Pat Hickey
f9a8329424 WasiCtxBuilder: add methods to provide Files each for stdio (#147) 2019-10-25 14:23:26 +02:00
Jakub Konka
b0e896e7d0 Add Github Actions badge to README 2019-10-25 11:52:18 +02:00
Jakub Konka
1c5422e052 Remove Azure Pipelines conf 2019-10-25 11:52:18 +02:00
Jakub Konka
997d411870 Bump required rustc to 1.37 in README 2019-10-25 11:52:18 +02:00
Jakub Konka
3821ce28c8 Add beta & nightly builds 2019-10-25 11:52:18 +02:00
Jakub Konka
2ada299b95 Add docs section 2019-10-25 11:52:18 +02:00
Jakub Konka
a04aa5860d Update test configuration 2019-10-25 11:52:18 +02:00
Jakub Konka
10e0b87ebf Update MSRV to 1.37.0 2019-10-25 11:52:18 +02:00
Jakub Konka
be1f8c0325 Update ci.yml 2019-10-25 11:52:18 +02:00
Jakub Konka
b0199ef396 Enable Github Actions 2019-10-25 11:52:18 +02:00
Jakub Konka
8e45a14cec Fast-forward wasmtime to latest rev 2019-10-24 12:07:45 +02:00
Jakub Konka
3a374d0016 Update poll_oneoff API
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.
2019-10-24 11:19:33 +02:00
Dan Gohman
bd8d550ba3 Improve Cargo.toml files for publishing (#141)
* Add versions to the crates.

This is needed for publishing on crates.io.

* Add a few Cargo.toml attributes to make the crates.io page friendlier.
2019-10-23 19:17:22 +02:00
Dan Gohman
056e1d8cc0 Add more links in the fs module's documentation. 2019-10-23 19:16:18 +02:00
Jakub Konka
951456d79b Update misc_testsuite to latest master 2019-10-23 19:13:05 +02:00
Jakub Konka
63c1f71036 Sync with latest wasmtime rev
This commit syncs tests with latest wasmtime revision.
As such, it now utilises the `wasmtime-api` crate for
runtime setup.

Closes #126, #127, #128, #129.
2019-10-22 15:27:21 +02:00
Jakub Konka
0d63cc2dbc Fix remove_directory_trailing_slashes on Windows
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.
2019-10-22 10:12:54 +02:00
Dan Gohman
ef010b44b7 Fix nondeterminism in the preopen order.
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.
2019-10-18 19:52:08 +02:00
Dan Gohman
4120d3b44f Optimize away an owned string in path_readlink. 2019-10-18 19:37:58 +02:00
Dan Gohman
14bad3cb97 Ensure that each crate has its own copy of the LICENSE file.
On crates.io, each crate is distributed separately, so ensure that each
crate has a license file.
2019-10-18 19:27:36 +02:00
Jakub Konka
c3bf04042e Fixes path_symlink_trailing_slashes test case (#125)
* 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
2019-10-17 11:33:56 +02:00
Jakub Konka
d458fb6815 Clean up BSD vs Linux implementation details
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
2019-10-17 11:15:01 +02:00
Jakub Konka
f1f6b3780b Fixes CraneStation/wasmtime#396
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.
2019-10-17 10:38:58 +02:00
Marcin Mielniczuk
968fb19f17 Add a typesafe enum for file type 2019-10-15 19:25:05 +09:00
Marcin Mielniczuk
5dad532a43 Use cvt from the cvt crate in winx 2019-10-15 17:15:10 +09:00
Marcin Mielniczuk
692bb27209 Correct the clippy::use_self lint where possible. (#114) 2019-10-04 18:10:30 -07:00
Jakub Konka
603f7a9f22 Misc testsuite feature gated (#113)
* Put misc_testsuite behind a feature gate

This PR puts building and generating of misc_testsuite behind
a feature gate "misc_testsuite". This is mainly to allow projects
which pull `wasi-common` as a dependency not to have to have
`wasm32-wasi` target installed in order to build it as it currently
is.

* Update the CI

* Rename feature to wasm_tests

* Explain integration testing in the README
2019-10-03 23:08:55 +02:00
Jakub Konka
da59c95f0c Update dependencies and use Once::new() instead of ONCE_INIT 2019-10-03 10:59:45 +02:00
dependabot-preview[bot]
6749015c26 Update target-lexicon requirement from 0.4.0 to 0.8.1
Updates the requirements on [target-lexicon](https://github.com/CraneStation/target-lexicon) to permit the latest version.
- [Release notes](https://github.com/CraneStation/target-lexicon/releases)
- [Commits](https://github.com/CraneStation/target-lexicon/commits)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-03 10:03:59 +02:00
dependabot-preview[bot]
cf9cbbb519 Update cranelift-wasm requirement from 0.41.0 to 0.44.0
Updates the requirements on [cranelift-wasm](https://github.com/CraneStation/cranelift) to permit the latest version.
- [Release notes](https://github.com/CraneStation/cranelift/releases)
- [Commits](https://github.com/CraneStation/cranelift/compare/v0.41.0...v0.44.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-03 10:00:46 +02:00
dependabot-preview[bot]
c097f567a9 Update cranelift-native requirement from 0.41.0 to 0.44.0
Updates the requirements on [cranelift-native](https://github.com/CraneStation/cranelift) to permit the latest version.
- [Release notes](https://github.com/CraneStation/cranelift/releases)
- [Commits](https://github.com/CraneStation/cranelift/compare/v0.41.0...v0.44.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-03 09:58:19 +02:00
Jakub Konka
b4c60f0c36 Merge pull request #111 from CraneStation/dependabot/cargo/rand-0.7
Update rand requirement from 0.6 to 0.7
2019-10-03 09:54:18 +02:00
dependabot-preview[bot]
183611d796 Update rand requirement from 0.6 to 0.7
Updates the requirements on [rand](https://github.com/rust-random/rand) to permit the latest version.
- [Release notes](https://github.com/rust-random/rand/releases)
- [Changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-random/rand/compare/0.6.0...0.7.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-03 07:53:58 +00:00
dependabot-preview[bot]
825ed4f4d4 Update cranelift-entity requirement from 0.41.0 to 0.44.0
Updates the requirements on [cranelift-entity](https://github.com/CraneStation/cranelift) to permit the latest version.
- [Release notes](https://github.com/CraneStation/cranelift/releases)
- [Commits](https://github.com/CraneStation/cranelift/compare/v0.41.0...v0.44.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-03 09:52:12 +02:00
Dan Gohman
6b2f3ebf7c Begin sketching out a new high-level fs API. (#91)
* Begin sketching out a new high-level `fs` API.

This is a very preliminary sketch of #83. It doesn't even compile yet,
but it shows a possible high-level structure of such an API.

* Stub out more functionality.

* Switch from a lazy_static WasiCtx to a borrowed one.

* Reformat some comments.

* Code-quote `Self`.

* Implement error translation for Windows.

* Calls to `fd_close` are now unsafe.

* Implement a few more functions.
2019-09-30 11:27:53 -07:00
Dan Gohman
a679412dd0 Sync with lucet-wasi (#106)
* Open /dev/null for writing as well as reading.

Port this fix to wasi-common:

b905c44483

* Remove all remaining uses of `std::mem::uninitialized`.

Patch inspired by:

2d6519d051

* Replace libc::memcpy() calls with std::ptr::copy_nonoverlapping()

Port this fix to wasi-common:

a3f3a33e9b

* Pass `WasiError` by value.

It's a `u16` underneath, so we can pass it by value.

* Avoid unnecessary explicit lifetime parameters.

* Use immutable references rather than mutable references.

Patch inspired by:

54baa4c38c
2019-09-30 19:22:11 +02:00
Jakub Konka
d33036a3b5 Fix path_rename on *nix hosts
The fix contains an errno remapping in macOS case where in case
when we try to rename a file into a path with a trailing slash an
ENOENT is returned. In this case, if the destination does not exist,
an ENOTDIR should be thrown as is thrown correctly on Linux hosts.
Thus, as a fix, if an ENOENT is thrown, an additional check is
performed to see whether the destination path indeed contains
a trailing slash, and if so, the errno is adjusted to ENOTDIR
to match the POSIX/WASI spec.
2019-09-25 22:20:39 +02:00
Jakub Konka
90f1cd5c96 Fix path_remove_directory on *nixes 2019-09-17 23:22:43 +02:00
Dan Gohman
30a4f73cce Mark public API functions as unsafe. (#90)
* Mark public API functions as unsafe.

This marks the public hostcalls functions as unsafe.

This is generalizing from Rust's `from_raw_fd` function, which is
unsafe. The observation is that nothing prevents code using this
function from passing a bogus or stale dangling file descriptor and
corrupting an arbitrary open stream.

Technically, some of these functions don't use file descriptors, such as
random, clocks, and a few others. However I expect that in the future,
random and clocks will switch to using file descriptors anyway, and it
keeps the macro definitions simpler if we only have to handle one form.

* Mark WasiCtx functions that operate on file descriptors unsafe too.

* `fd_filestat_set_times_impl` doesn't need to be unsafe.

* Remove unnecessary unsafes
2019-09-16 15:55:35 -07:00
Jakub Konka
fec5b7ab0a Refactor mgmt of misc testsuite (#101)
Changes:
* use [tempfile] crate for auto mgmt of temp dirs
* use concrete types in place of generics in `utils` module

[tempfile]: https://github.com/Stebalien/tempfile
2019-09-16 15:35:14 -07:00