Commit Graph

54 Commits

Author SHA1 Message Date
Nick Fitzgerald
6d01fd4103 deps: Update wat to 1.0.9 2020-02-26 14:23:33 -08:00
Alex Crichton
33a39ff4f0 Bump to 0.12.0 (#997)
* Bump to 0.12.0

* Another lockfile update
2020-02-26 16:19:12 -06:00
Peter Huene
fa65a14dba Make WasiCtxBuilder by-ref.
This commit makes `WasiCtxBuilder` take `&mut Self` and return `&mut
Self` for its methods.  This is needed to allow for the same
(unmoved) `WasiCtxBuilder` to be used when building a WASI context.

Also fixes up the C API to remove the unnecessary `Box::from_raw` and
`forget` calls which were previously needed for the moving version of
`WasiCtxBuilder`.
2020-02-25 15:41:18 -08:00
Alex Crichton
3dd5a3cb3f Reimplement wasmtime-wasi on top of wasmtime (#899)
* Reimplement `wasmtime-wasi` on top of `wasmtime`

This commit reimplements the `wasmtime-wasi` crate on top of the
`wasmtime` API crate, instead of being placed on top of the `wasmtime-*`
family of internal crates. The purpose here is to continue to exercise
the API as well as avoid usage of internals wherever possible and
instead use the safe API as much as possible.

The `wasmtime-wasi` crate's API has been updated as part of this PR as
well. The general outline of it is now:

* Each module snapshot has a `WasiCtxBuilder`, `WasiCtx`, and `Wasi`
  type.
  * The `WasiCtx*` types are reexported from `wasi-common`.
  * The `Wasi` type is synthesized by the `wig` crate's procedural macro
* The `Wasi` type exposes one constructor which takes a `Store` and a
  `WasiCtx`, and produces a `Wasi`
* Each `Wasi` struct fields for all the exported functions in that wasi
  module. They're all public an they all have type `wasmtime::Func`
* The `Wasi` type has a `get_export` method to fetch an struct field by
  name.

The intention here is that we can continue to make progress on #727 by
integrating WASI construction into the `Instance::new` experience, but
it requires everything to be part of the same system!

The main oddity required by the `wasmtime-wasi` crate is that it needs
access to the caller's `memory` export, if any. This is currently done
with a bit of a hack and is expected to go away once interface types are
more fully baked in.

* Remove now no-longer-necessary APIs from `wasmtime`

* rustfmt

* Rename to from_abi
2020-02-06 09:23:06 -06: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
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
8a02a48e91 Merge remote-tracking branch 'upstream/master' into poll 2020-01-17 08:55:42 +01:00
Alex Crichton
e5afdd2ede Document the wasmtime::Instance APIs (#814)
* Document the `wasmtime::Instance` APIs

This documents oddities like the import list and export list and how to
match them all up. Addtionally this largely just expands all the docs
related to `Instance` to get filled out.

This also moves the `set_signal_handler` functions into
platform-specific modules in order to follow Rust idioms about how to
expose platform-specific information. Additionally the methods are
marked `unsafe` because I figure anything having to do with signal
handling is `unsafe` inherently. I don't actually know what these
functions do, so they're currently still undocumented.

* Fix build of python bindings

* Fix some rebase conflicts
2020-01-16 17:58:44 -06:00
Alex Crichton
c417d4b587 Improve trap error messages (#831)
* Improve trap error messages

The new trap error message for the issue #828 looks like:

```
thread 'main' panicked at 'a', /proc/self/fd/11:1:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Error: failed to run main module `test.wasm`

Caused by:
    0: failed to invoke `_start`
    1: wasm trap: unreachable, source location: @6cea
       wasm backtrace:
         0: __rust_start_panic
         1: rust_panic
         2: std::panicking::rust_panic_with_hook::h57f0cff11449798f
         3: std::panicking::begin_panic::hd620695467c5dd1f
         4: test::main::ha54db001eabbde1b
         5: std::rt::lang_start::{{closure}}::h5acfb82693695869
         6: std::sys_common::backtrace::__rust_begin_short_backtrace::h39e8b9420da241f9
         7: std::panicking::try::do_call::hb7ebfcd70d5f703e
         8: __rust_maybe_catch_panic
         9: std::rt::lang_start_internal::hd5f64f52a5c5315c
         10: std::rt::lang_start::h2a51d79994dd0c4b
         11: __original_main
         12: _start
```

Closes #828

* Tidy up the style of the traps tests

* Add some tests and module names
2020-01-16 17:39:52 -06:00
Marcin Mielniczuk
b2c63290dd Merge remote-tracking branch 'upstream/master' into poll 2020-01-14 16:40:23 +01:00
Alex Crichton
420dcd76fd Don't require Store in Instance constructor (#810)
* Don't require `Store` in `Instance` constructor

This can be inferred from the `Module` argument. Additionally add a
`store` accessor to an `Instance` in case it's needed to instantiate
another `Module`.

cc #708

* Update more constructors

* Fix a doctest

* Don't ignore store in `wasm_instance_new`

* Run rustfmt
2020-01-13 17:50:57 -06: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
Alex Crichton
6571fb8f4f Remove HostRef from the wasmtime public API (#788)
* Remove `HostRef` from the `wasmtime` public API

This commit removes all remaining usages of `HostRef` in the public API
of the `wasmtime` crate. This involved a number of API decisions such
as:

* None of `Func`, `Global`, `Table`, or `Memory` are wrapped in `HostRef`
* All of `Func`, `Global`, `Table`, and `Memory` implement `Clone` now.
* Methods called `type` are renamed to `ty` to avoid typing `r#type`.
* Methods requiring mutability for external items now no longer require
  mutability. The mutable reference here is sort of a lie anyway since
  the internals are aliased by the underlying module anyway. This
  affects:
  * `Table::set`
  * `Table::grow`
  * `Memory::grow`
  * `Instance::set_signal_handler`
* The `Val::FuncRef` type is now no longer automatically coerced to
  `AnyRef`. This is technically a breaking change which is pretty bad,
  but I'm hoping that we can live with this interim state while we sort
  out the `AnyRef` story in general.
* The implementation of the C API was refactored and updated in a few
  locations to account for these changes:
  * Accessing the exports of an instance are now cached to ensure we
    always hand out the same `HostRef` values.
  * `wasm_*_t` for external values no longer have internal cache,
    instead they all wrap `wasm_external_t` and have an unchecked
    accessor for the underlying variant (since the type is proof that
    it's there). This makes casting back and forth much more trivial.

This is all related to #708 and while there's still more work to be done
in terms of documentation, this is the major bulk of the rest of the
implementation work on #708 I believe.

* More API updates

* Run rustfmt

* Fix a doc test

* More test updates
2020-01-10 10:42:14 -06: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
1c4c78ab03 Add the Cargo.lock for the test programs. 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
Jakub Konka
036fd8ead5 Fix path_open_read_without_rights test
Fixes `path_open_read_without_rights` test making the CI green again.
2020-01-09 21:16:31 +01: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
5efa640e23 Add a test for path_open read rights 2020-01-09 10:01:01 -08:00
Marcin Mielniczuk
0b50274680 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
4695c95374 WIP implementation of poll_oneoff on Windows 2020-01-09 18:46:02 +01:00
Alex Crichton
1fe76ef9e3 Remove the need for HostRef<Module>
This commit continues previous work and also #708 by removing the need
to use `HostRef<Module>` in the API of the `wasmtime` crate. The API
changes performed here are:

* The `Module` type is now itself internally reference counted.
* The `Module::store` function now returns the `Store` that was used to
  create a `Module`
* Documentation for `Module` and its methods have been expanded.
2020-01-08 12:46:18 -08:00
Alex Crichton
eb1991c579 Revert "Remove the need for HostRef<Module> (#778)"
This reverts commit 7b33f1c619.

Pushed a few extra commits by accident, so reverting this.
2020-01-08 12:44:59 -08:00
Alex Crichton
7b33f1c619 Remove the need for HostRef<Module> (#778)
* Remove the need for `HostRef<Module>`

This commit continues previous work and also #708 by removing the need
to use `HostRef<Module>` in the API of the `wasmtime` crate. The API
changes performed here are:

* The `Module` type is now itself internally reference counted.
* The `Module::store` function now returns the `Store` that was used to
  create a `Module`
* Documentation for `Module` and its methods have been expanded.

* Fix compliation of test programs harness

* Fix the python extension

* Update `CodeMemory` to be `Send + Sync`

This commit updates the `CodeMemory` type in wasmtime to be both `Send`
and `Sync` by updating the implementation of `Mmap` to not store raw
pointers. This avoids the need for an `unsafe impl` and leaves the
unsafety as it is currently.

* Fix a typo
2020-01-08 14:42:37 -06:00
Alex Crichton
045d6a7310 Remove the need for HostRef<Store> (#771)
* Remove the need for `HostRef<Store>`

This commit goes through the public API of the `wasmtime` crate and
removes the need for `HostRef<Store>`, as discussed in #708. This commit
is accompanied with a few changes:

* The `Store` type now also implements `Default`, creating a new
  `Engine` with default settings and returning that.

* The `Store` type now implements `Clone`, and is documented as being a
  "cheap clone" aka being reference counted. As before there is no
  supported way to create a deep clone of a `Store`.

* All APIs take/return `&Store` or `Store` instead of `HostRef<Store>`,
  and `HostRef<T>` is left as purely a detail of the C API.

* The `global_exports` function is tagged as `#[doc(hidden)]` for now
  while we await its removal.

* The `Store` type is not yet `Send` nor `Sync` due to the usage of
  `global_exports`, but it is intended to become so eventually.

* Touch up comments on some examples

* Run rustfmt
2020-01-07 16:29:44 -06:00
Alex Crichton
41528c82bc Remove the Flags type from Config API (#769)
* Remove the `Flags` type from `Config` API

This commit removes the final foreign type from the `Config` API in the
`wasmtime` crate. The cranelift `Flags` type is now expanded into
various options on the `Config` structure itself, all prefixed with
`cranelift_` since they're only relevant to the Cranelift backend. The
various changes here were:

* The `avoid_div_traps` feature is enabled by default since it seemed
  that was done anywhere anyway.
* Enabling the wasm SIMD feature enables the requisite features in
  Cranelift as well.
* A method for enabling the debug verifier has been added.
* A method for configuring the Cranelift optimization level, as well as
  a corresponding enumeration, has been added.

* Assert that `Config` is both `Send` and `Sync`
2020-01-07 14:07:48 -06:00
Alex Crichton
b9dc38f4e1 Remove need for HostRef<Engine> (#762)
This commit removes the need to use `HostRef<Engine>` in the Rust API.
Usage is retained in the C API in one location, but otherwise `Engine`
can always be used directly.

This is the first step of progress on #708 for the `Engine` type.
Changes here include:

* `Engine` is now `Clone`, and is documented as being cheap. It's not
  intended that cloning an engine creates a deep copy.
* `Engine` is now both `Send` and `Sync`, and asserted to be so.
* Usage of `Engine` in APIs no longer requires or uses `HostRef`.
2020-01-06 15:17:03 -06:00
Yury Delendik
681445b18b Fail with Trap in Instance::new() instead of Error (#683) 2019-12-30 16:25:16 -06:00
Jakub Konka
51f3ac0c45 Update WASI tests to use wasi crate v0.9.0 (#743)
This commit updates _all_ WASI test programs to use the latest
version of the `wasi` crate (`v0.9.0`). While at it, it also
unifies asserting error conditions across all test programs.
2019-12-24 13:04:14 -08:00
Alex Crichton
d641e6e7b0 Update some more wasi test programs to 0.9.0
Largely the same as the previous update!
2019-12-13 17:27:59 +01:00
Peter Huene
4e67ccfbc3 Fix WASI test program running.
PR #585 moved the execution of a `_start` function to the CLI rather than have
it automatically invoked by module instantiation.

Unfortunately, this broke the WASI test programs that were relying on this
behavior from instantiation.

This fixes it by adding an invocation of the `_start` function in the test
runner.

Fixes #698.
2019-12-11 16:12:53 -08:00
Alex Crichton
e13fabb276 Start to update the wasi crate in wasi tests (#675)
* Move `wasi` to `wasi_old` in wasi-tests

Leave space for the new `wasi` crate but allow us to incrementally
update tests.

* Update the big_random_buf test

* Update clock_time_get test

* Update close_preopen test

* Review comments

* Update to latest Wasmtime API
2019-12-09 13:08:47 +01:00
Alex Crichton
e134505b90 Refactor the types.rs types and structures (#681)
* Refactor the `types.rs` types and structures

A few changes applied along the way:

* Documentation added to most methods and types.
* Limits are now stored with the maximum as optional rather than a
  sentinel u32 value for `None`.
* The `Name` type was removed in favor of just using a bare `String`.
* The `Extern` prefix in the varaints of `ExternType` has been removed
  since it was redundant.
* Accessors of `ExternType` variants no longer panic, and unwrapping
  versions were added with "unwrap" in the name.
* Fields and methods named `r#type` were renamed to `ty` to avoid
  requiring a raw identifier to use them.

* Remove `fail-fast: false`

This was left around since the development of GitHub Actions for
wasmtime, but they're no longer needed!

* Fix compilation of the test-programs code

* Fix compilation of wasmtime-py package

* Run rustfmt
2019-12-06 16:19:55 -06:00
Yury Delendik
cc6e8e1af2 Move cranelift dependencies to wasmtime-environ (#669)
Groups all CL data structures into single dependency to be used accross wasmtime project.
2019-12-05 16:07:34 -06:00
Jakub Konka
45f41b3c8e Deny warnings in the CI by default (#616)
* Deny warnings in the CI by default

This commit passes `-D warnings` flag to `rustc` in the Test stage
of the CI in order to deny warnings on _all_ tested platforms.

* Fix warnings

* Fix warnings
2019-12-04 17:43:36 +01:00
Dan Gohman
1f9d764d5d Support fd_fdstat_get and fd_renumber on stdin/stdout/stderr (#631)
* Support fd_fdstat_get on stdin/stdout/stderr.

Add a routine for obtaining an `OsFile` containing a file descriptor for
stdin/stdout/stderr so that we can do fd_fdstat_get on them.

* Add a testcase for fd_fdstat_get etc. on stdin etc.

* Don't dup file descriptors in fd_renumber.

* Fix compilation on macOS

* Rename OsFile to OsHandle

This commits renames `OsFile` to `OsHandle` which seems to make
more sense semantically as it is permitted to hold a valid OS handle
to OS entities other than simply file/dir (e.g., socket, stream, etc.).
As such, this commit also renames methods on `Descriptor` struct
from `as_actual_file` to `as_file` as this in reality does pertain
ops on FS entities such as files/dirs, and `as_file` to `as_os_handle`
as in this case it can be anything, from file, through a socket, to
a stream.

* Fix compilation on Linux

* Introduce `OsHandleRef` for borrowing OS resources.

To prevent a `ManuallyDrop<OsHandleRef>` from outliving the resource it
holds on to, create an `OsHandleRef` class parameterized on the lifetime
of the `Descriptor`.

* Fix scoping to pub-priv and backport to snapshot_0
2019-11-28 14:36:18 +01:00
Marcin Mielniczuk
b69758f672 Correctly handle possibly misaligned pointers in readdir (#615) 2019-11-27 19:11:32 -08:00
Peter Huene
0cf54ffeba Implement __wasi_fd_fdstat_get for Windows.
This commit fully implements `__wasi_fd_fdstat_get` on Windows so that
the descriptor flags can be determined.

It does this by calling into `NtQueryInformationFile` (safe to call from
user mode) to get the open mode and access of the underlying OS handle.

`NtQueryInformationFile` isn't included in the `winapi` crate, so it is
manually being linked against.

This commit also fixes several bugs on Windows:

* Ignore `__WASI_FDFLAG_NONBLOCK` by not setting `FILE_FLAG_OVERLAPPED`
  on file handles (the POSIX behavior for `O_NONBLOCK` on files).
* Use `FILE_FLAG_WRITE_THROUGH` for the `__WASI_FDFLAG_?SYNC` flags.
* `__WASI_FDFLAG_APPEND` should disallow `FILE_WRITE_DATA` access to
  force append-only on write operations.
* Use `GENERIC_READ` and `GENERIC_WRITE` access flags.  The
  latter is required when opening a file for truncation.
2019-11-26 22:24:12 +01:00
Jakub Konka
bbe2a797ba Fix feature-gating of test-programs (#612)
* Fix feature-gating of test-programs

This commit fixes bugs in enabling feature-gating of `test-programs`
which was introduced in #600. It turns out, #600 accidentally
disabled `test-programs` from ever running, and this commit fixes
that.

* Fix the CI
2019-11-21 13:52:15 +01:00
Jakub Konka
a58709d99e Feature-gate test-programs
This commit feature-gates `test-programs` as a workaround for
`cargo test` not allowing for optional dev-dependencies.

This commit addresses #595.
2019-11-21 07:01:53 +01:00
Josh Triplett
2635ccb742 Rename the wasmtime_api library to match the containing wasmtime crate (#594)
* Rename the `wasmtime_api` library to match the containing `wasmtime` crate

Commit d9ca508f80 renamed the
`wasmtime-api` crate to `wasmtime`, but left the name of the library it
contains as `wasmtime_api`.

It's fairly unusual for a crate to contain a library with a different
name, and it results in rather confusing error messages for a user; if
you list `wasmtime = "0.7"` in `Cargo.toml`, you can't `use
wasmtime::*`, you have to `use wasmtime_api::*;`.

Rename the `wasmtime_api` library to `wasmtime`.

* Stop renaming wasmtime to api on imports

Various users renamed the crate formerly known as wasmtime_api to api,
and then used api:: prefixes everywhere; change those all to wasmtime::
and drop the renaming.
2019-11-19 14:47:39 -08:00
Dan Gohman
58dd4c6c88 Replace more miscellaenous read_wasm utilities with wat::parse_file. (#601) 2019-11-19 09:34:17 -06:00
Jakub Konka
7d7360b32d Move dangling_fd test case into test-programs (#590)
* Move dangling_fd test case in test-programs

* Fix build errors
2019-11-19 12:20:47 +01:00
Dan Gohman
d645902620 Add support for wasi_snapshot_preview1. (#592)
* Add support for wasi_snapshot_preview1.

This adds support for the new ABI, while preserving compatibility
support for the old ABI.

* Fix compilation on platforms where nlink_t isn't 64-bit.

* rustfmt

* Fix Windows build errors.
2019-11-18 22:07:16 -08:00
Dan Gohman
7d47a04277 Remove unneded prefix argument from instantiate_wasi. (#584)
* Remove unneded prefix argument from `instantiate_wasi`.

This was an artifact of an earlier backwards-compatibility mechanism
which is no longer needed.

* Remove unneeded prefix arg from remaning uses
2019-11-16 11:40:14 -08:00
Yury Delendik
ea56118651 Add/use create_wasi_instance() instead of instantiate_wasi(). (#571)
* Add/use create_wasi_instance() instead of instantiate_wasi().

* rm Result from Instance::from_handle
2019-11-15 16:48:05 -08:00
Jakub Konka
9896a5cabd Add test for dangling file/dir handles (#566)
* Add test for dangling file/dir handles

This commit adds a test for dangling file/dir handles. The logic is
quite simple: we first create a resource (file or dir), get a WASI file
descriptor to it, remove the resource without closing the FD, and then
try to re-create it.

* Disable on Windows for now
2019-11-15 23:28:50 +01:00