Commit Graph

71 Commits

Author SHA1 Message Date
Alex Crichton
7a1b7cdf92 Implement RFC 11: Redesigning Wasmtime's APIs (#2897)
Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
2021-06-03 09:10:53 -05:00
Pat Hickey
0f5bdc6497 only wasi_cap_std_sync and wasi_tokio need to define WasiCtxBuilders (#2917)
* wasmtime-wasi: re-exporting this WasiCtxBuilder was shadowing the right one

wasi-common's WasiCtxBuilder is really only useful wasi_cap_std_sync and
wasi_tokio to implement their own Builder on top of.

This re-export of wasi-common's is 1. not useful and 2. shadow's the
re-export of the right one in sync::*.

* wasi-common: eliminate WasiCtxBuilder, make the builder methods on WasiCtx instead

* delete wasi-common::WasiCtxBuilder altogether

just put those methods directly on &mut WasiCtx.

As a bonus, the sync and tokio WasiCtxBuilder::build functions
are no longer fallible!

* bench fixes

* more test fixes
2021-05-21 12:59:39 -05:00
Pat Hickey
b19d86268c fix test harness stdio 2021-05-06 16:19:33 -07:00
Pat Hickey
add115ba00 fix 2021-05-06 15:53:23 -07:00
Pat Hickey
208013e34e de-duplicate code 2021-05-06 11:12:53 -07:00
Pat Hickey
f4d851126d tests dont need fuel 2021-05-06 10:53:25 -07:00
Pat Hickey
686d8c22f9 fix test harness 2021-05-04 11:18:20 -07:00
Pat Hickey
8667d8c244 test-programs: test wasi-tokio 2021-04-19 16:00:27 -07:00
Pat Hickey
b883bda022 fix test-programs for sync wasi 2021-04-14 16:06:50 -07:00
Alex Crichton
2697a18d2f Redo the statically typed Func API (#2719)
* Redo the statically typed `Func` API

This commit reimplements the `Func` API with respect to statically typed
dispatch. Previously `Func` had a `getN` and `getN_async` family of
methods which were implemented for 0 to 16 parameters. The return value
of these functions was an `impl Fn(..)` closure with the appropriate
parameters and return values.

There are a number of downsides with this approach that have become
apparent over time:

* The addition of `*_async` doubled the API surface area (which is quite
  large here due to one-method-per-number-of-parameters).
* The [documentation of `Func`][old-docs] are quite verbose and feel
  "polluted" with all these getters, making it harder to understand the
  other methods that can be used to interact with a `Func`.
* These methods unconditionally pay the cost of returning an owned `impl
  Fn` with a `'static` lifetime. While cheap, this is still paying the
  cost for cloning the `Store` effectively and moving data into the
  closed-over environment.
* Storage of the return value into a struct, for example, always
  requires `Box`-ing the returned closure since it otherwise cannot be
  named.
* Recently I had the desire to implement an "unchecked" path for
  invoking wasm where you unsafely assert the type signature of a wasm
  function. Doing this with today's scheme would require doubling
  (again) the API surface area for both async and synchronous calls,
  further polluting the documentation.

The main benefit of the previous scheme is that by returning a `impl Fn`
it was quite easy and ergonomic to actually invoke the function. In
practice, though, examples would often have something akin to
`.get0::<()>()?()?` which is a lot of things to interpret all at once.
Note that `get0` means "0 parameters" yet a type parameter is passed.
There's also a double function invocation which looks like a lot of
characters all lined up in a row.

Overall, I think that the previous design is starting to show too many
cracks and deserves a rewrite. This commit is that rewrite.

The new design in this commit is to delete the `getN{,_async}` family of
functions and instead have a new API:

    impl Func {
        fn typed<P, R>(&self) -> Result<&Typed<P, R>>;
    }

    impl Typed<P, R> {
        fn call(&self, params: P) -> Result<R, Trap>;
        async fn call_async(&self, params: P) -> Result<R, Trap>;
    }

This should entirely replace the current scheme, albeit by slightly
losing ergonomics use cases. The idea behind the API is that the
existence of `Typed<P, R>` is a "proof" that the underlying function
takes `P` and returns `R`. The `Func::typed` method peforms a runtime
type-check to ensure that types all match up, and if successful you get
a `Typed` value. Otherwise an error is returned.

Once you have a `Typed` then, like `Func`, you can either `call` or
`call_async`. The difference with a `Typed`, however, is that the
params/results are statically known and hence these calls can be much
more efficient.

This is a much smaller API surface area from before and should greatly
simplify the `Func` documentation. There's still a problem where
`Func::wrapN_async` produces a lot of functions to document, but that's
now the sole offender. It's a nice benefit that the
statically-typed-async verisons are now expressed with an `async`
function rather than a function-returning-a-future which makes it both
more efficient and easier to understand.

The type `P` and `R` are intended to either be bare types (e.g. `i32`)
or tuples of any length (including 0). At this time `R` is only allowed
to be `()` or a bare `i32`-style type because multi-value is not
supported with a native ABI (yet). The `P`, however, can be any size of
tuples of parameters. This is also where some ergonomics are lost
because instead of `f(1, 2)` you now have to write `f.call((1, 2))`
(note the double-parens). Similarly `f()` becomes `f.call(())`.

Overall I feel that this is a better tradeoff than before. While not
universally better due to the loss in ergonomics I feel that this design
is much more flexible in terms of what you can do with the return value
and also understanding the API surface area (just less to take in).

[old-docs]: https://docs.rs/wasmtime/0.24.0/wasmtime/struct.Func.html#method.get0

* Rename Typed to TypedFunc

* Implement multi-value returns through `Func::typed`

* Fix examples in docs

* Fix some more errors

* More test fixes

* Rebasing and adding `get_typed_func`

* Updating tests

* Fix typo

* More doc tweaks

* Tweak visibility on `Func::invoke`

* Fix tests again
2021-03-11 14:43:34 -06:00
Pat Hickey
e4ce04bab4 WasiCtx: default to empty/sink stdio files rather than throw
the test harness now uses the empty stdin file. I tested manually that
the sink stdout & stderr files work, but theres no test in tree at the
moment
2021-02-05 17:50:20 -08:00
Pat Hickey
31145060b2 remove virtfs - it is not suitable for use 2021-02-03 15:04:02 -08:00
Pat Hickey
7a35763d62 collapse two test flags into dangling_filesystem 2021-02-03 14:54:42 -08:00
Pat Hickey
c77a11bd5c tests: macos-specific behavior 2021-02-01 18:30:58 -08:00
Pat Hickey
bb3e391a27 accept fdread event as valid behavior of stdin poll 2021-02-01 15:26:06 -08:00
Pat Hickey
0c4aec391e actually empty ready bytes of stdin 2021-02-01 14:43:32 -08:00
Pat Hickey
b9a3f8694d cap-std-sync test runner: read stdin to end before inheriting stdio 2021-02-01 14:32:51 -08:00
Pat Hickey
40e541bfc3 test suite: cap-std-sync test environment does not support fdflags sync 2021-02-01 14:25:42 -08:00
Pat Hickey
321bf27292 check in virtfs backend test harness 2021-01-30 13:45:21 -08:00
Pat Hickey
fcecb3fea6 test-programs: test both cap-std-sync and virtfs backend 2021-01-30 13:39:18 -08:00
Pat Hickey
e940d31f95 add a noent / not_found errorkind 2021-01-30 13:36:41 -08:00
Pat Hickey
e1ca5d171c fix the second place i made the same dumb typo 2021-01-29 20:58:00 -08:00
Pat Hickey
6ed8638559 fix test runtime 2021-01-29 18:01:48 -08:00
Pat Hickey
9bd89abc0c rename everything c2 related to the "real" names 2021-01-28 15:34:03 -08:00
Pat Hickey
34ad8df169 dangling directories are a windows thing 2021-01-28 12:58:41 -08:00
Pat Hickey
1196e216e9 fix macro by denying trailing slashes. also other stuff 2021-01-28 12:23:48 -08:00
Pat Hickey
e758318fee wire env variables through test runner to TESTCONFIG 2021-01-28 11:34:18 -08:00
Pat Hickey
a46c2ad0aa split poll tests up, only one requires "real" stdio 2021-01-21 16:54:40 -08:00
Pat Hickey
12056885eb fix preopen dir to work on windows 2021-01-21 16:08:49 -08:00
Pat Hickey
f3e40e2fc4 restructure cap-std impls into their own crate 2021-01-20 19:09:15 -08:00
Pat Hickey
9a1ce1a272 TEMPORARY: inherit stdio for the wasi ctx
this is unfortunate but the poll_oneoff test insists on polling on stdio
handles. to undo this temporary fix later, lets rewrite the test to open
some regular files from the scratch directory and poll on them instead.
2021-01-14 17:38:43 -08:00
Pat Hickey
e0e205f8d2 ctx builder: fix warnings, test harness 2021-01-13 11:07:06 -08:00
Pat Hickey
04805fcc5f pass a test, dubiously 2020-12-14 19:48:30 -08:00
Pat Hickey
c16e731455 get rid of linker Rc cycle, and add debug info to test programs 2020-12-14 17:15:03 -08:00
Pat Hickey
1b8f9fd377 use virtual stdio
which works except for the lifetime issues, i think the trap still holds
an Rc to the store?
2020-12-11 18:22:13 -08:00
Pat Hickey
759455192b test-programs: learn how to preopen a dir again 2020-12-11 16:38:55 -08:00
Pat Hickey
dfcdbfd0fe test-programs: no longer test virtfs separately
wasi-c2 does not have a virtfs yet, and when it does we'll design a
better test harness

also fix prestat get: i was reporting the wrong error
2020-12-11 15:51:14 -08:00
Pat Hickey
d586574b1f port in args and env. slightly different style
building up a bunch of deferred errors in the CtxBuilder sucks. so does
reporting errors right away. Idk what to do here?
2020-12-11 15:33:59 -08:00
Pat Hickey
aef8be560f test-programs: use wasi-c2 instead of wasmtime 2020-12-11 15:00:58 -08:00
Jakub Konka
f47133b229 Allow different Handles to act as stdio (#1600)
* Allow any type which implements Handle to act as stdio

There have been requests to allow more than just raw OS handles to
act as stdio in `wasi-common`. This commit makes this possible by
loosening the requirement of the `WasiCtxBuilder` to accept any
type `T: Handle + 'static` to act as any of the stdio handles.

A couple words about correctness of this approach. Currently, since
we only have a single `Handle` super-trait to represent all possible
WASI handle types (files, dirs, stdio, pipes, virtual, etc.), it
is possible to pass in any type to act as stdio which can be wrong.
However, I envision this being a problem only in the near(est) future
until we work out how to split `Handle` into several traits, each
representing a different type of WASI resource. In this particular
case, this would be a resource which would implement the interface
required for a handle to act as a stdio (with appropriate rights, etc.).

* Use OsFile in c-api

* Add some documention to the types exposed by this PR, and a few others

Signed-off-by: Jakub Konka <kubkon@jakubkonka.com>

* Add construction examples and missing docs for Handle trait

* Fix example on Windows

* Merge wasi_preview_builder into create_preview1_instance

Co-authored-by: Pat Hickey <pat@moreproductive.org>
2020-06-09 20:19:20 +02:00
Yury Delendik
15c68f2cc1 Disconnects Store state fields from Compiler (#1761)
*  Moves CodeMemory, VMInterrupts and SignatureRegistry from Compiler
*  CompiledModule holds CodeMemory and GdbJitImageRegistration
*  Store keeps track of its JIT code
*  Makes "jit_int.rs" stuff Send+Sync
*  Adds the threads example.
2020-06-02 13:44:39 -05:00
Dan Gohman
3715e19c67 Reactor support. (#1565)
* Reactor support.

This implements the new WASI ABI described here:

https://github.com/WebAssembly/WASI/blob/master/design/application-abi.md

It adds APIs to `Instance` and `Linker` with support for running
WASI programs, and also simplifies the process of instantiating
WASI API modules.

This currently only includes Rust API support.

* Add comments and fix a typo in a comment.

* Fix a rustdoc warning.

* Tidy an unneeded `mut`.

* Factor out instance initialization with `NewInstance`.

This also separates instantiation from initialization in a manner
similar to https://github.com/bytecodealliance/lucet/pull/506.

* Update fuzzing oracles for the API changes.

* Remove `wasi_linker` and clarify that Commands/Reactors aren't connected to WASI.

* Move Command/Reactor semantics into the Linker.

* C API support.

* Fix fuzzer build.

* Update usage syntax from "::" to "=".

* Remove `NewInstance` and `start()`.

* Elaborate on Commands and Reactors and add a spec link.

* Add more comments.

* Fix wat syntax.

* Fix wat.

* Use the `Debug` formatter to format an anyhow::Error.

* Fix wat.
2020-05-26 10:39:40 -05:00
Dan Gohman
9364eb1d98 Refactor (#1524)
* Compute instance exports on demand.

Instead having instances eagerly compute a Vec of Externs, and bumping
the refcount for each Extern, compute Externs on demand.

This also enables `Instance::get_export` to avoid doing a linear search.

This also means that the closure returned by `get0` and friends now
holds an `InstanceHandle` to dynamically hold the instance live rather
than being scoped to a lifetime.

* Compute module imports and exports on demand too.

And compute Extern::ty on demand too.

* Add a utility function for computing an ExternType.

* Add a utility function for looking up a function's signature.

* Add a utility function for computing the ValType of a Global.

* Rename wasmtime_environ::Export to EntityIndex.

This helps differentiate it from other Export types in the tree, and
describes what it is.

* Fix a typo in a comment.

* Simplify module imports and exports.

* Make `Instance::exports` return the export names.

This significantly simplifies the public API, as it's relatively common
to need the names, and this avoids the need to do a zip with
`Module::exports`.

This also changes `ImportType` and `ExportType` to have public members
instead of private members and accessors, as I find that simplifies the
usage particularly in cases where there are temporary instances.

* Remove `Instance::module`.

This doesn't quite remove `Instance`'s `module` member, it gets a step
closer.

* Use a InstanceHandle utility function.

* Don't consume self in the `Func::get*` methods.

Instead, just create a closure containing the instance handle and the
export for them to call.

* Use `ExactSizeIterator` to avoid needing separate `num_*` methods.

* Rename `Extern::func()` etc. to `into_func()` etc.

* Revise examples to avoid using `nth`.

* Add convenience methods to instance for getting specific extern types.

* Use the convenience functions in more tests and examples.

* Avoid cloning strings for `ImportType` and `ExportType`.

* Remove more obviated clone() calls.

* Simplify `Func`'s closure state.

* Make wasmtime::Export's fields private.

This makes them more consistent with ExportType.

* Fix compilation error.

* Make a lifetime parameter explicit, and use better lifetime names.

Instead of 'me, use 'instance and 'module to make it clear what the
lifetime is.

* More lifetime cleanups.
2020-04-20 15:55:33 -05:00
iximeow
7e0d9decbf Virtual file support (#701)
* Add support for virtual files (eg, not backed by an OS file).

Virtual files are implemented through trait objects, with a default
implementation that tries to behave like on-disk files, but entirely
backed by in-memory structures.

Co-authored-by: Dan Gohman <sunfish@mozilla.com>
2020-03-06 11:08:13 -08: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
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
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
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