Commit Graph

89 Commits

Author SHA1 Message Date
Peter Huene
c78bf3c08a Merge pull request #784 from marmistrz/path_open_doc
Document the behavior of some rights-related functions.
2020-01-23 09:39:25 -08:00
Peter Huene
ef6e1ca2a8 Merge pull request #552 from marmistrz/poll
Minimal viable implementation of poll_oneoff for Windows
2020-01-23 09:30:21 -08:00
Alex Crichton
5953215bac 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
2020-01-22 14:54:39 -06:00
Dan Gohman
9a88d3d894 Replace the global-exports mechanism with a caller-vmctx mechanism. (#789)
* Replace the global-exports mechanism with a caller-vmctx mechanism.

This eliminates the global exports mechanism, and instead adds a
caller-vmctx argument to wasm functions so that WASI can obtain the
memory and other things from the caller rather than looking them up in a
global registry.

This replaces #390.

* Fixup some merge conflicts

* Rustfmt

* Ensure VMContext is aligned to 16 bytes

With the removal of `global_exports` it "just so happens" that this
isn't happening naturally any more.

* Fixup some bugs with double vmctx in wasmtime crate

* Trampoline stub needed adjusting
* Use pointer type instead of always using I64 for caller vmctx
* Don't store `ir::Signature` in `Func` since we don't know the pointer
  size at creation time.
* Skip the first 2 arguments in IR signatures since that's the two vmctx
  parameters.

* Update cranelift to 0.56.0

* Handle more merge conflicts

* Rustfmt

Co-authored-by: Alex Crichton <alex@alexcrichton.com>
2020-01-21 14:50:59 -08:00
Marcin Mielniczuk
815576edc5 Return EINVAL in poll_oneoff with no events. (#838)
* Return EINVAL in poll_oneoff with no events.

We adhere to WebAssembly/WASI#193.

* Add a test for empty poll.
2020-01-17 13:41:37 -08:00
Marcin Mielniczuk
13afbd0bae Fix a typo.
Co-Authored-By: Peter Huene <peterhuene@protonmail.com>
2020-01-17 22:27:37 +01:00
Marcin Mielniczuk
3d29244203 Cleanup empty event behavior 2020-01-17 22:26:22 +01:00
Marcin Mielniczuk
919190e062 Document the behavior of some rights-related functions.
cf. #770
2020-01-17 20:02:37 +01:00
Marcin Mielniczuk
5b5f9a7b06 Properly return errors. 2020-01-17 09:12:12 +01:00
Marcin Mielniczuk
8a02a48e91 Merge remote-tracking branch 'upstream/master' into poll 2020-01-17 08:55:42 +01:00
Jakub Konka
5f1c0eb86b Generate strerror from witx; tweak Display for WasiError (#832)
This commit introduces two small changes:
* it adds `gen_errno_strerror` to `wig` crate which generates a
  `strerror` function for `__wasi_errno_t` directly from `*.witx`,
  similarly to how it's done in the `wasi` crate
* it tweaks `WasiError` type to include the error message generated
  with `strerror` when displaying the error
2020-01-16 16:39:53 -06:00
Jakub Konka
e474a9e822 [wasi-common] Log string representation of WASI errno at the trace level (#760)
* Log str repr of WASI errno at trace level

This commit refactors `Error` enum, and adds logging of the WASI
errno string representation at the trace level. Now, when tracing
WASI syscalls, we will be greeted with a nicely formatted errno
value after each syscall:

```
path_open(...)
     | *fd=5
     | errno=ESUCCESS
```

This commit gets rid of `errno_from_nix`, `errno_from_win` and
`errno_from_host` helper fns in favour of direct `From` implementations
for the relevant types such as `yanix::Errno` and `winx::winerror::WinError`.
`errno_from_host` is replaced by a trait `FromRawOsError`.

* Back port changes to snapshot0

* Fix indentation in logs
2020-01-16 21:52:04 +01:00
Marcin Mielniczuk
e4905c3100 Extra comments 2020-01-16 19:50:16 +01:00
Marcin Mielniczuk
1c050b6a33 Reset crates/wasi-common/WASI to upstream/master 2020-01-16 19:47:04 +01:00
Marcin Mielniczuk
4f9218eded Get rid of hangup, it's incorrect 2020-01-16 19:41:34 +01:00
Marcin Mielniczuk
caa6897af5 Finish minimal impl 2020-01-16 19:24:24 +01:00
Marcin Mielniczuk
716acf77d1 Move to mpsc, drop crossbeam. Simplify 2020-01-16 18:34:20 +01:00
Marcin Mielniczuk
3c132d6909 Improve comments 2020-01-16 17:54:12 +01:00
Marcin Mielniczuk
5b9272f2a6 fix build 2020-01-16 15:23:08 +01:00
Marcin Mielniczuk
410777de52 Handle timeout 2020-01-16 15:09:36 +01:00
Marcin Mielniczuk
33818ea18e Align with Unix 2020-01-16 14:59:18 +01:00
Marcin Mielniczuk
3261626fd8 wip 2020-01-16 13:29:33 +01:00
Marcin Mielniczuk
8e8826d19f wip 2020-01-16 13:22:33 +01:00
Marcin Mielniczuk
748894a121 wip 2020-01-16 13:20:24 +01:00
Marcin Mielniczuk
cea6542fd8 Merge remote-tracking branch 'upstream/master' into poll 2020-01-16 13:06:02 +01:00
Dan Gohman
b8e4354efc Implement write_vectored for SandboxedTTYWriter.
Fixes #629.
2020-01-15 22:17:42 +01:00
Marcin Mielniczuk
b2c63290dd Merge remote-tracking branch 'upstream/master' into poll 2020-01-14 16:40:23 +01:00
Marcin Mielniczuk
432dbf0e74 More WIP 2020-01-14 16:33:35 +01:00
Dan Gohman
ef2177ed3a Update to the latest spec_testsuite and dependencies. (#803)
* Update to the latest spec_testsuite and dependencies.

Update to target-lexicon 0.10, cranelift 0.54, wast 0.6, faerie 0.14,
and the latest spec_testsuite.

For wast and cranelift-wasm, update the code for API changes.

* Factor out the code for matching f32, f64, and v128.

This takes the idea from #802 to split out `f32_matches`, `f64_matches`,
and `v128_matches` functions, which better factor out the matching
functionality between scalar and vector.
2020-01-10 13:57:38 -08:00
Dan Gohman
336ee94c89 Bump version to 0.9.0 (#790) 2020-01-09 21:57:40 -08:00
Peter Huene
4b7677e4da Additional PR feedback changes.
* Add more comments.
* Use `contains` from bitflags.
* Format wasi-test source.
* Remove permission check from Windows `path_open` impl.
2020-01-09 17:13:21 -08:00
Peter Huene
8fdd776f81 Implement fd_fdstat_set_flags for Windows.
This commit implements `fd_fdstat_set_flags` for Windows.

Additionally, it fixes a problem where `O_APPEND` was not working correctly
because `GENERIC_WRITE` was always being set; as a result, `FILE_WRITE_DATA`
could not be removed from the permission set to properly enable append-only
mode.

It also treats `O_TRUNC` with `O_APPEND` as an invalid argument error.  This is
because Windows cannot support these two flags together. To support `O_TRUNC`,
the `GENERIC_WRITE` bit must be set for the file access flags.  Setting this
bit will cause `FILE_WRITE_DATA` to be set, which will not properly treat the
file as append-only (it requires `FILE_APPEND_DATA` without `FILE_WRITE_DATA`).
2020-01-09 17:13:21 -08:00
Marcin Mielniczuk
f7f10c12b3 Fix rights checks across the codebase.
* Fix path_open granting more rights than requested
* Add missing rights checks in: fd_fdstat_set_flags, fd_filestat_get, poll_oneoff
* Fix `open_scratch_directory` not requesting any rights.
* Properly request needed rights in various tests
* Add some extra trace-level logging
* Remove a no-op restriction of rights to the ones returned by
  `determine_type_rights`. It was redundant, because `FdEntry:from`
  internally also called `determine_type_rights` and only dropped some of them.
2020-01-09 10:01:01 -08:00
Marcin Mielniczuk
9197a68837 WIP 2020-01-09 18:46:04 +01:00
Marcin Mielniczuk
f20b5a4cac WIP 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
54a398ad69 Fix writing timeout events. Check that we only return one timeout event. 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
a2b556f1b0 Do not loop with nfds=0, timeout=-1 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
5cd3e9904f Rename make_read_event to make_rw_event 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
a8e9b1a0d5 Comment on infinite sleep 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
7cb8137fae Avoid issuing syscalls if we're requested to return immediately 2020-01-09 18:46:03 +01:00
Marcin Mielniczuk
40ec01a1e8 Fix poll_oneoff behavior when fd_events are empty 2020-01-09 18:46:02 +01:00
Marcin Mielniczuk
98e84ae487 Refactor poll_oneoff and return stdin if immediately readable. 2020-01-09 18:46:02 +01:00
Marcin Mielniczuk
4695c95374 WIP implementation of poll_oneoff on Windows 2020-01-09 18:46:02 +01:00
Jakub Konka
06be4b1495 [wasi-common] Clean up fd_filestat_get implementation (#757)
* Clean up fd_filestat_get implementation

This commit does 4 things:
* Adds `yanix::file::fstat`, a wrapper around `libc::fstat`.
* It essentially reverts 89fbde2 for Unix hosts -- in other words,
  it brings back the use of `fstat` to obtain `libc::stat` from a
  file descriptor, rather than relying on `std::fs::Metadata`. This
  way, we reuse `host_impl::filestat_from_nix` in
  `hostcalls_impl::fd_filestat_get` implementation rather than
  unnecessarily duplicate code for converting filestats into
  `__wasi_filestat_t`.
* Moves `crate::helpers::systemtime_to_timestamp` to Windows `host_impl`
  module. It does the same thing with helpers which assist in converting
  `std::fs::Metadata` into `__wasi_filestat_t`. This should retain symmetry
  between *nix and Windows impls.
* Makes timestamp conversions in `host_impl::filestat_from_nix` fallible.

* Backport changes to snapshot0

* Signal no overflow with `from` rather than `as` cast
2020-01-08 16:34:38 +01:00
Jakub Konka
e674eee609 [wasi-common] Use thiserror proc macros for auto From impls (#758)
* Use thiserror proc macros for auto From impls

This commit refactors `wasi_common::error::Error` by using `#[from]`
proc macro to autoderive `From` for wrapped errors.

* Back port changes to snapshot0

* Auto impl Display for WasiError

* Fix stack overflow when auto generating Display for WasiError
2020-01-06 11:44:35 -06:00
Dan Gohman
1d810a5de9 Initial support for securing tty I/O. (#684)
* Initial support for securing tty I/O.

* Update the tests.

* Fix warnings

* Update crates/wasi-common/src/fdentry.rs

Co-Authored-By: Jakub Konka <jakub.konka@golem.network>

* Properly sandbox stderr.

* Document why the scratch buffer is 4 elements long.

* Update crates/wasi-common/src/sandboxed_tty_writer.rs

Co-Authored-By: Jakub Konka <jakub.konka@golem.network>

* Update crates/wasi-common/src/sandboxed_tty_writer.rs

Co-Authored-By: Jakub Konka <jakub.konka@golem.network>

* Add comments explaining how we report the number of bytes written.

* Always sanitize stderr.

* Port the changes to the snapshot_0 directory.

* Fix snapshot_0 compilation error.

* Replace the scratch buffer with a temporary buffer.

* Update crates/wasi-common/src/sandboxed_tty_writer.rs

Co-Authored-By: bjorn3 <bjorn3@users.noreply.github.com>

* Format with latest stable rustfmt.

Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>
2020-01-02 11:53:26 +01:00
Alex Crichton
cc4be18119 Reduce boilerplate in wasmtime-wasi (#707)
This commit uses the `*.witx` files describing the current wasi API to
reduce the boilerplate used to define implementations in the
`wasmtime-wasi` crate. Eventually I'd like to remove lots of boilerplate
in the `wasi-common` crate too, but this should at least be a good start!

The boilerplate removed here is:

* No need to list each function to add it to the
  `wasmtime_runtime::Module` being created

* No need to list the signature of the function in a separate
  `syscalls.rs` file.

Instead the `*.witx` file is processed in a single-use macro inside the
`wasmtime-wasi` crate. This macro uses the signatures known from
`*.witx` to automatically register with the right type in the wasm
module as well as define a wrapper that the wasm module will call into.
Functionally this is all the same as before, it's just defined in a
different way now!

The shim generated by this macro which wasmtime calls into only uses
`i32`/`i64`/etc wasm types, and it internally uses `as` casts to convert
to the right wasi types when delegating into the `wasi-common` crate.

One change was necessary to get this implemented, however. The functions
in `wasi-common` sometimes took `WasiCtx` and sometimes took a slice of
memory. After this PR they uniformly all require both `WasiCtx` and
memory so the wrappers can be auto-generated. The arguments are ignored
if they weren't previously required.
2019-12-16 16:37:20 -06:00
Dan Gohman
c2ba419409 Misc yanix fixes (#715)
* Correctly handle possibly misaligned pointers in readdir

This reapplies #615, which was inadvertently reverted.

* Tidy up unneeded `self::` qualifiers.

* Make Dir's contents private.

Also remove the `unsafe` from `impl_iter`. With `Dir`'s field being
private, we can rely on the pointer being only what we've assigned to
it.

* Make `poll`'s timeout argument a `libc::c_int`.

This clarifies that there are no subsequent conversions before calling the
underlying libc API.

* Use clock_gettime instead of clock_getres to get the time.

* Mark FileType::from_raw as safe.

It handles unknown values, so it can be marked safe.
2019-12-16 13:34:22 -08:00
Jakub Konka
77bf76897c Fix wasi-common-cbindgen missing no_mangle attr 2019-12-16 10:51:39 +01:00
Alex Crichton
054b79427e Fix the path_filestat test on Linux (#706)
Only very recently in #700 did we actually start running wasi tests
again (they weren't running by accident). Just before that landed we
also landed #688 which had some refactorings. Unfortunately #688 had a
minor issue in it which wasn't caught because tests weren't run. This
means that the bug in #688 slipped in and is now being caught by #700
now that both are landed on master.

This commit fixes the small issue introduced and should get our CI green
again!
2019-12-12 15:19:58 -08:00