Commit Graph

53 Commits

Author SHA1 Message Date
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
c975a92a3a Remove unsafety from Trap API (#779)
* Remove unsafety from `Trap` API

This commit removes the `unsafe impl Send` for `Trap` by removing the
internal `HostRef` and leaving `HostRef` entirely as an implementation
detail of the C API.

cc #708

* Run rustfmt
2020-01-08 14:41:47 -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
Andrew Brown
296ebc46fd Update wasmparser to 0.45.1 (#776) 2020-01-07 16:21:50 -06:00
Peter Huene
59258730c2 Use structopt instead of docopt.
This commit refactors the Wasmtime CLI tools to use `structopt` instead of
`docopt`.

The `wasmtime` tool now has the following subcommands:

* `config new` - creates a new Wasmtime configuration file.
* `run` - runs a WebAssembly module.
* `wasm2obj` - translates a Wasm module to native object file.
* `wast` - runs a test script file.

If no subcommand is specified, the `run` subcommand is used. Thus,
`wasmtime foo.wasm` should continue to function as expected.

The `wasm2obj` and `wast` tools still exist, but delegate to the same
implementation as the `wasmtime` subcommands.  The standalone `wasm2obj` and
`wast` tools may be removed in the future in favor of simply using `wasmtime`.

Included in this commit is a breaking change to the default Wasmtime
configuration file: it has been renamed from `wasmtime-cache-config.toml` to
simply `config.toml`.  The new name is less specific which will allow for
additional (non-cache-related) settings in the future.

There are some breaking changes to improve command line UX:

* The `--cache-config` option has been renamed to `--config`.
* The `--create-config-file` option has moved to the `config new` subcommand.
As a result, the `wasm2obj` and `wast` tools cannot be used to create a new
config file.
* The short form of the `--optimize` option has changed from
`-o` to `-O` for consistency.
* The `wasm2obj` command takes the output object file as a
required positional argument rather than the former required output *option*
(e.g. `wasmtime wasm2obj foo.wasm foo.obj`).
2020-01-07 13:15:28 -08:00
Alex Crichton
d142a39113 Tweak some API doc generation (#772)
* Build docs with the nightly toolchain so [foo::bar] links work by
  default. This is a relatively new feature of rustdoc and I thought it
  was stabilized at this point but apparently it's not!

* Tweak some API docs on `wasmtime::Strategy`

* Use `--all` to build all local crate documentation instead of trying
  to list the number of local crates

* Tweak some documentation configuration to avoid warnings generated by
  Cargo.
2020-01-07 14:42:35 -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
9ead93684e Ensure Trap is returned for start function traps (#768)
* Ensure `Trap` is returned for start function traps

Handle another case of errors coming out of instantiation, resolve a
FIXME, and remove an unneeded dependency from the wast testsuite crate.

* Run rustfmt
2020-01-07 11:45:13 -06:00
Alex Crichton
7474633cca Remove usage of CompilationStrategy from Config (#764)
* Remove usage of `CompilationStrategy` from `Config`

This commit removes the public API usage of the internal
`CompilationStrategy` enumeration from the `Config` type in the
`wasmtime` crate. To do this the `enum` was copied locally into the
crate and renamed `Strategy`. The high-level description of this change
is:

* The `Config::strategy` method now takes a locally-defined `Strategy`
  enumeration instead of an internal type.

* The contents of `Strategy` are always the same, not relying on Cargo
  features to indicate which variants are present. This avoids
  unnecessary downstream `#[cfg]`.

* A `lightbeam` feature was added to the `wasmtime` crate itself to
  lightbeam compilation support.

* The `Config::strategy` method is now fallible. It returns a runtime
  error if support for the selected strategy wasn't compiled in.

* The `Strategy` enum is listed as `#[non_exhaustive]` so we can safely
  add variants over time to it.

This reduces the public crate dependencies of the `wasmtime` crate
itself, removing the need to reach into internal crates even more!

cc #708

* Fix fuzz targets

* Update nightly used to build releases

* Run rustfmt
2020-01-06 18:08:13 -06:00
Alex Crichton
787f50e107 Remove usage of Features from wasmtime::Config API (#763)
Instead expose a number of boolean accessors which doesn't require users
to construct a foreign `Features` type and allows us to decouple the API
of the `wasmtime` crate from the underlying implementation detail.
2020-01-06 17:34: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
XAMPPRocky
907e7aac01 Clippy fixes (#692) 2019-12-24 12:50:07 -08:00
Andrew Brown
086ff63e6b Update wasmparser to 0.45.0 (#733) 2019-12-18 10:24:01 -06:00
Alex Crichton
d5a2eb397c Update the *.wast runner to use the wasmtime API (#690)
* Update the `*.wast` runner to use the `wasmtime` API

This commit migrates the `wasmtime-wast` crate, which executes `*.wast`
test suites, to use the `wasmtime` crate exclusively instead of the raw
support provided by the `wasmtime-*` family of crates.

The primary motivation for this change is to use `*.wast` test to test
the support for interface types, but interface types is only being added
in the `wasmtime` crate for now rather than all throughout the core
crates. This means that without this transition it's much more difficult
to write tests for wasm interface types!

A secondary motivation for this is that it's testing the support we
provide to users through the `wasmtime` crate, since that's the
expectation of what most users would use rather than the raw
`wasmtime-*` crates.

* Run rustfmt

* Fix the multi example

* Handle v128 values in the `wasmtime` crate

Ensure that we allocate 128-bit stack slots instead of 64-bit stack
slots.

* Update to master

* Add comment
2019-12-17 13:30:50 -06:00
Alex Crichton
dc3f88b297 Store v128 as u128 in wasmtime crate (#689)
As [suggested], this seems like a better and more ergonomic idea than
using `[u8; 16]`!

[suggested]: 3d69e04659 (r36326017)
2019-12-17 09:25:09 -06:00
XAMPPRocky
ddd2300010 Document Callable, Trap, HostRef, and ValType (#693) 2019-12-11 10:46:45 -08: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
Alex Crichton
3d69e04659 Tweak the API of the Val type (#679)
* Tweak the API of the `Val` type

A few updates to the API of the `Val` type:

* Added a payload for `V128`.
* Replace existing accessor methods with `Option`-returning versions.
* Add `unwrap_xxx` family of methods to extract a value and panic.
* Remove `Into` conversions which panic, since panicking in `From` or
  `Into` isn't idiomatic in Rust
* Add documentation to all methods/values/enums/etc.
* Rename `Val::default` to `Val::null`

* Run rustfmt

* Review comments
2019-12-06 16:19:37 -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
Peter Huene
b7c8408978 Merge pull request #676 from peterhuene/fix-dotnet-interop
Fix .NET interop issue for Windows release builds.
2019-12-05 13:32:46 -08:00
Peter Huene
cc02214084 Fix .NET interop issue for Windows release builds.
For Windows release builds, the `wasm_valtype_kind` C API return value
is being returned as a single byte.

The .NET interop signature for this function was expecting an
integer-sized return, resulting in three extra bytes being used on
Windows.

The fix is to limit the corresponding C# enum to a byte representation,
which will properly mask the return value from `wasm_valtype_kind`.

CI has also been updated to test both debug and release configurations
(previously it was only testing debug, hence why this was missed).

Also fixed a cast bug in the `declare_vec!` macro in the C API when the
element types were pointers to values.  The `as_slice` implementation
was incorrectly casting away a level of pointer indirection, resulting
in corrupted data when accessing the slice's elements.
2019-12-05 12:44:47 -08:00
Nick Fitzgerald
0fce2ac288 deps: Update wasmparser in most places (#670)
Doesn't update it in lightbeam.
2019-12-05 13:51:47 -06:00
Yury Delendik
991592c4ba [wasmtime-api] Record original Trap from API callback. (#657)
* Record original Trap from API callback.

Fixes #645

* use TrapRegistry

* comment about magic number
2019-12-04 07:57:24 -06:00
Josh Triplett
4b001912ce Update rayon dependencies to avoid duplicate crossbeam dependencies (#651)
rayon 1.2.1 avoids introducing two different versions of crossbeam
crates into the dependency tree (one through rayon, the other through
rayon-core).
2019-12-02 17:32:00 -08:00
Dan Gohman
16b8b3e58d Make Module::new perform validation. (#621)
* Make `Module::new` perform validation.

As noticed in #602, `Module::new` did not perform validation, which
turns out to be error-prone in practice. Rename it to
`Module::new_unchecked`, and add a new `Module::new` which does
perform validation.

Preserve wasm-c-api's `wasm_module_new`'s behavior by using
`Module::new_unchecked`, and implement `wasm_module_validate`.

* Change `validate`'s store argument to `&HostRef<Store>`.

* Enable multi-value in validation.
2019-11-27 10:58:38 -06:00
Yury Delendik
e71ab6b846 Fix build dependency for make run-hello-c (#622) 2019-11-27 10:56:31 -06:00
Yury Delendik
e9622654c5 [wasmtime-api] Collect and wrap cranelift dependencies (#623)
* Collect and wrap cranelift dependencies
* rename all _cranelift_ named methods
2019-11-25 10:34:28 -06:00
Josh Triplett
3cb238366d Simplify gcd example: remove unnecessary clone 2019-11-21 06:57:14 +01:00
Josh Triplett
7c8ac3d71c Simplify examples: avoid unnecessary HostRef wrap/unwrap
Several of the examples wrap the Instance in a HostRef, only to
immediately borrow it again to get the exports,and then never touch it
again. Simplify this by owning the Instance directly.
2019-11-21 06:57:14 +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
Alex Crichton
39e57e3e9a Migrate back to std:: stylistically (#554)
* Migrate back to `std::` stylistically

This commit moves away from idioms such as `alloc::` and `core::` as
imports of standard data structures and types. Instead it migrates all
crates to uniformly use `std::` for importing standard data structures
and types. This also removes the `std` and `core` features from all
crates to and removes any conditional checking for `feature = "std"`

All of this support was previously added in #407 in an effort to make
wasmtime/cranelift "`no_std` compatible". Unfortunately though this
change comes at a cost:

* The usage of `alloc` and `core` isn't idiomatic. Especially trying to
  dual between types like `HashMap` from `std` as well as from
  `hashbrown` causes imports to be surprising in some cases.
* Unfortunately there was no CI check that crates were `no_std`, so none
  of them actually were. Many crates still imported from `std` or
  depended on crates that used `std`.

It's important to note, however, that **this does not mean that wasmtime
will not run in embedded environments**. The style of the code today and
idioms aren't ready in Rust to support this degree of multiplexing and
makes it somewhat difficult to keep up with the style of `wasmtime`.
Instead it's intended that embedded runtime support will be added as
necessary. Currently only `std` is necessary to build `wasmtime`, and
platforms that natively need to execute `wasmtime` will need to use a
Rust target that supports `std`. Note though that not all of `std` needs
to be supported, but instead much of it could be configured off to
return errors, and `wasmtime` would be configured to gracefully handle
errors.

The goal of this PR is to move `wasmtime` back to idiomatic usage of
features/`std`/imports/etc and help development in the short-term.
Long-term when platform concerns arise (if any) they can be addressed by
moving back to `no_std` crates (but fixing the issues mentioned above)
or ensuring that the target in Rust has `std` available.

* Start filling out platform support doc
2019-11-18 22:04:06 -08:00
Andrew Brown
ea04aa5b98 Improve error messages received from Cranelift (#583)
As discussed in https://github.com/bytecodealliance/cranelift/pull/1226, the context of Cranelift errors is lost after exiting the scope containing the Cranelift function. `CodegenError` then only contains something like `inst2: arg 0 (v4) has type i16x8, expected i8x16`, which is rarely enough information for investigating a codegen failure. This change uses Cranelift's `pretty_error` function to improve the error messages wrapped in `CompileError`; `CompileError` has lost the reference to `CodegenError` due to `pretty_error` taking ownership but this seems preferable since no backtrace is attached and losing the pretty-printed context would be worse (if `CodegenError` gains a `Backtrace` or implements `Clone` we can revisit this).
2019-11-16 11:42:17 -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
Dan Gohman
c5f998add2 Update the crates.io publishing scripts (#580)
* Fix fuzz target compilation.

* Bump version to 0.7.0

* Temporarily disable fuzz tests

Temporarily disable fuzz tests until https://github.com/bytecodealliance/cranelift/issues/1216 is resolved.

* Fix publish-all.sh to not modify the witx crate.

* Remove the "publish = false" attribute from Lightbeam.

* Add a README.md for wasmtime-interface-types.

* Remove the "rust" category.

This fixes the following warning:

warning: the following are not valid category slugs and were ignored: rust. Please see https://crates.io/category_slugs for the list of all category slugs.

* Mark wasmtime-cli as "publish = false".

* Sort the publishing rules in topological order.

Also, publish nightly-only crates with cargo +nightly.
2019-11-15 12:17:19 -08:00
Yury Delendik
36cb806c54 Use embedding api in python extension (#569)
Now embedding API is used in the Python extension, this allows us to remove ModuleData::invoke() from wasmtime-interface-types
2019-11-14 10:40:04 -06:00
Alex Crichton
399295a708 Remove all checked in *.wasm files to the repo (#563)
* Tidy up the `hello` example for `wasmtime`

* Remove the `*.wat` and `*.wasm` files and instead just inline the
  `*.wat` into the example.

* Touch up comments so they're not just a repeat of the `println!`
  below.

* Move `*.wat` for `memory` example inline

No need to handle auxiliary files with the ability to parse it inline!

* Move `multi.wasm` inline into `multi.rs` example

* Move `*.wasm` for gcd example inline

* Move `*.wat` inline with `import_calling_export` test

* Remove checked in `lightbeam/test.wasm`

Instead move the `*.wat` into the source and parse it into wasm there.

* Run rustfmt
2019-11-13 13:00:06 -06:00
Nick Fitzgerald
5ca38bdd4a Merge pull request #562 from alexcrichton/less-public-api-dependencies
Reduce number of crates needed for `Config` usage
2019-11-13 10:02:51 -08:00
Alex Crichton
fb60a21930 Reduce number of crates needed for Config usage
This commit is an attempt to reduce the number of crates necessary to
link to when using `wasmtime::Config` in "default mode" or with only one
or two tweaks. The change moves to a builder-style pattern for `Config`
to only require importing crates as necessary if you configure a
particular setting. This then also propagates that change to `Context`
as well by taking a `Config` instead of requiring that all arguments are
passed alone.
2019-11-13 08:32:13 -08:00
Alex Crichton
8b0cfd9245 Squash improper_ctypes warnings on nightly
Lots more warnings are showing up on nightly compilers due to a recent
change. I've opened rust-lang/rust#66373 on the compiler side for this
as well.
2019-11-13 08:05:55 -08:00
Yury Delendik
98266498af Use embedded API in the wasmtime-rust (#540) 2019-11-13 09:15:37 -06:00
Dan Gohman
c78196bd01 Update repository URLs for the Bytecode Alliance. (#550) 2019-11-12 09:18:59 -08:00
Nick Fitzgerald
01ab20e372 Bump cranelift deps to 0.50.0 2019-11-11 15:52:49 -08:00
Nick Fitzgerald
4c4699a226 Test the multi-value example on windows 2019-11-11 15:39:48 -08:00
Nick Fitzgerald
a29303dedd Expand the muli-value example for wasmtime-api to use many return values
This should exercise the Rust-calling-Wasm code path for when there are more
return values than fit into return registers.
2019-11-11 15:39:48 -08:00
Dan Gohman
061b453255 Remove unneeded extern crate, macro_use, and tidy uses. 2019-11-08 17:55:38 -08:00
Dan Gohman
5b0031ece8 Use pointer::add instead of pointer::offset with a cast. 2019-11-08 17:15:37 -08:00
Dan Gohman
39b0d670c5 rustfmt and trim trailing whitespace. 2019-11-08 17:15:37 -08:00
Dan Gohman
1a0ed6e388 Use the more-asserts crate in more places.
This provides assert_le, assert_lt, and so on, which can print the
values of the operands.
2019-11-08 15:24:53 -08:00