196 Commits

Author SHA1 Message Date
Alex Crichton
62be6841e4 Propagate optional import names to the wasmtime/C API
With the module linking proposal the field name on imports is now
optional, and only the module is required to be specified. This commit
propagates this API change to the boundary of wasmtime's API, ensuring
consumers are aware of what's optional with module linking and what
isn't. Note that it's expected that all existing users will either
update accordingly or unwrap the result since module linking is
presumably disabled.
2020-11-23 15:26:26 -08:00
Alex Crichton
73cda83548 Propagate module-linking types to wasmtime (#2115)
This commit adds lots of plumbing to get the type section from the
module linking proposal plumbed all the way through to the `wasmtime`
crate and the `wasmtime-c-api` crate. This isn't all that useful right
now because Wasmtime doesn't support imported/exported
modules/instances, but this is all necessary groundwork to getting that
exported at some point. I've added some light tests but I suspect the
bulk of the testing will come in a future commit.

One major change in this commit is that `SignatureIndex` no longer
follows type type index space in a wasm module. Instead a new
`TypeIndex` type is used to track that. Function signatures, still
indexed by `SignatureIndex`, are then packed together tightly.
2020-11-06 14:48:09 -06:00
Alex Crichton
e4c3fc5cf2 Update immediate and transitive dependencies
I don't think this has happened in awhile but I've run a `cargo update`
as well as trimming some of the duplicate/older dependencies in
`Cargo.lock` by updating some of our immediate dependencies as well.
2020-11-05 08:34:09 -08:00
Alex Crichton
a277cf5ee4 Store WasmFuncType in FuncType (#2365)
This commit updates `wasmtime::FuncType` to exactly store an internal
`WasmFuncType` from the cranelift crates. This allows us to remove a
translation layer when we are given a `FuncType` and want to get an
internal cranelift type out as a result.

The other major change from this commit was changing the constructor and
accessors of `FuncType` to be iterator-based instead of exposing
implementation details.
2020-11-05 08:49:03 -06:00
Pat Hickey
bacf470a3e bump all wat dependencies to 1.0.23
this gets us down to one version of `wast` in dependency tree!
2020-08-17 16:03:35 -07:00
Alex Crichton
08f9eb1725 Making caching support optional in Wasmtime (#2119)
This commit moves all of the caching support that currently lives in
`wasmtime-environ` into a `wasmtime-cache` crate and makes it optional. The
goal here is to slim down the `wasmtime-environ` crate and clearly separate
boundaries where caching is a standalone and optional feature, not intertwined
with other crates.
2020-08-07 15:42:40 -05:00
Yury Delendik
399ee0a54c Serialize and deserialize compilation artifacts. (#2020)
* Serialize and deserialize Module
* Use bincode to serialize
* Add wasm_module_serialize; docs
* Simple tests
2020-07-21 15:05:50 -05:00
Nick Fitzgerald
2efb46afd5 wasmtime-c-api: Add wasmtime_store_gc for GCing externrefs 2020-07-21 09:33:34 -07:00
Alex Crichton
fbc05faa49 Fix wasm_val_copy for null funcref/externref (#2041)
This commit fixes `Clone for wasm_val_t` to avoid attempting to chase a
null pointer. It also fixes the implementation for `FuncRef` values by
cloning their internal `wasm_ref_t` as well.
2020-07-17 14:46:02 -05:00
Alex Crichton
3aeab23bf1 Fix leaking funcrefs in the C API (#2040)
This commit adds a case to the destructor of `wasm_val_t` to be sure to
deallocate the `Box<wasm_ref_t>`.
2020-07-17 14:45:55 -05:00
Alex Crichton
63d5b91930 Wasmtime 0.19.0 and Cranelift 0.66.0 (#2027)
This commit updates Wasmtime's version to 0.19.0, Cranelift's version to
0.66.0, and updates the release notes as well.
2020-07-16 12:46:21 -05:00
Alex Crichton
41e1300247 Allow improper_ctypes_definitions in C API
This was enabled in rust-lang/rust#72700 but it looks like it's still
too noisy for it to be useful to us.
2020-07-16 17:28:54 +02:00
Nick Fitzgerald
a9455a8e51 C API tweaks for wasmtime-py (#2029)
* wasmtime-c-api: Only drop non-null `*mut wasm_ref_t`s

* wasmtime-c-api: Handle null refs in `wasm_val_t` to `Val` conversion

* wasmtime-c-api: Don't unwrap and rewrap `Option`s

The `unwrap` can panic, and there isn't any point to this unwrap+rewrap.

* wasmtime-c-api: Add conversions between `funcref` and `wasm_func_t`

* wasmtime-c-api: More ownership documentation for `wasmtime.h`
2020-07-15 19:55:31 -05:00
Yury Delendik
a817470fab Fix signature of wasmtime_module_new 2020-07-15 14:07:36 -05:00
Nick Fitzgerald
9f0ec620f0 wasmtime-c-api: Remove duplicate tests of examples
The canonical examples tests will be the examples run via `cargo run -p
run-examples` from now on. Those tests have two advantages over the ones being
deleted:

1. They will automatically pick up new examples and run/test them.
2. They support all of Linux, MacOS, and Windows, while the tests being deleted
   are Linux only.
2020-07-13 08:42:55 -07:00
Nick Fitzgerald
89603bc6b3 wasmtime-c-api: Make wasm_table_set *not* take ownership of its reference
Same for `wasm_table_grow` and `wasm_table_new` and their `init` values.
2020-07-10 13:09:08 -07:00
Nick Fitzgerald
d07fdca73a wasmtime-c-api: Use a safe helper for initializing MaybeUninit out pointers 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
377b213fd5 wasmtime-c-api: Remove unused HostInfoState struct 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
b73b2e0d5a wasmtime-c-api: Make wasmtime_externref_new write to an out pointer
The C API prefers not to return structs by value.

Same for `wasmtime_externref_new_with_finalizer`.
2020-07-10 13:09:08 -07:00
Nick Fitzgerald
b9bb095e0f wasmtime-c-api: Remove nested options from wasm_ref_t representation 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
742f8ee1fc wasmtime-c-api: Make wasmtime_externref_new proxy to wasmtime_externref_new_with_finalizer 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
5ed8a9ba0e wasmtime-c-api: Add an externrefs example for the C API 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
267bf16683 wasmtime-c-api: Add Wasmtime-specific APIs for externrefs
This commit adds APIs to create new `externref` values (with and without
finalizers) and to get an `externref`'s wrapped data.
2020-07-10 13:09:08 -07:00
Nick Fitzgerald
4c01ffd087 wasmtime-c-api: Document new reference types support 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
aaf4c941b8 wasmtime-c-api: Add support for funcref values 2020-07-10 13:09:08 -07:00
Nick Fitzgerald
4cdf8b7cfd wasmtime-c-api: Add support for externref values
This required that `wasm_val_t` have a `Drop` implementation, an explicit
`Clone` implementation, and no longer be `Copy`, which rippled out through the
crate a bit.

Additionally, `wasm_func_call` and friends were creating references to
uninitialized data for its out pointers and assigning to them. As soon as
`wasm_val_t` gained a `Drop` impl and tried to drop the old value of the
assignment (which is uninitialized data), then things blew up. The fix is to
properly represent the out pointers with `MaybeUninit`, and use `ptr::write` to
initialize them without dropping the old data.

Part of #929
2020-07-10 13:09:08 -07:00
Alex Crichton
920e0984d3 Link to C API docs and make a landing page
Spice up the landing page a bit for the C API documentation and then
link to it from a few places to ensure it's discoverable.
2020-07-02 08:49:27 -07:00
Alex Crichton
47a218f908 Document the rest of the C API (#1959)
This commit fills out documentation for all remaining functions in the C
API, and additionally enables "warn if undocumented" which will fail CI
since warnings are also treated as errors.
2020-07-02 09:26:10 -05:00
Alex Crichton
d72b330de2 Add support for documenting the C API (#1928)
This commit adds a bit of a skeleton of what it might look like to
document the C API. Today the C API has virtually zero documentation
because the upstream documentation does not exist and we haven't put a
ton of effort into documenting our own extensions. Given that this is
one of the main vectors we expect users to use Wasmtime, we should make
sure it's thoroughly documented!

I've never really done much documentation generation of C myself before,
but I did a bit of searching and Doxygen seems reasonable proficient for
doing this. This commit sets up what it might look like for Doxygen to
be used for the C API. One nice feature of DOxygen is that we can
document the items in `wasm.h` without actually modifying `wasm.h`. For
those purposes a `doc-wasm.h` file was added here which is where we can
put Wasmtime-specific documentation about `wasm.h`.

There's quite a few functions in the C API so I didn't want to get them
all done before getting consensus on this. I've started some skeletons
of documentation for global types in `wasm.h` and also confirmed that
documentation works for our own `wasmtime.h` and such header files. If
this looks good to everyone and it runs reasonable well on CI then I can
spend more time filling out the rest of the documentation.
2020-07-01 14:05:18 -05:00
Alex Crichton
cca558cd20 Remove HostRef<T> from the C API (#1926)
This commit removes `HostRef<T>` from the C API which only served the
purpose now of converting each type to a `wasm_ref_t*`. Our
implementation, however, does not guarantee that you'll get the same
`wasm_ref_t*` for each actual underlying item (e.g. if you put a func in
a table and then get the func as an export and from the table then
`same` will report `false`). Additionally the fate of `wasm_ref_t*`
seems somewhat unclear at this point.

The change here is to make the `same` and cast functions all abort
saying they're unimplemented. (similar to the host info functions). If
and when we get around to reimplementing these functions we can ensure
they're implemented uniformly and work well for all intended use cases.
2020-06-26 14:34:34 -05:00
Nick Fitzgerald
e40c039e65 wasmtime: Rip out incomplete/incorrect externref "host info" support
Better to be loud that we don't support attaching arbitrary host info to
`externref`s than to limp along and pretend we do support it. Supporting it
properly won't reuse any of this code anyways.
2020-06-25 14:00:40 -07:00
Nick Fitzgerald
58bb5dd953 wasmtime: Add support for func.ref and table.grow with funcrefs
`funcref`s are implemented as `NonNull<VMCallerCheckedAnyfunc>`.

This should be more efficient than using a `VMExternRef` that points at a
`VMCallerCheckedAnyfunc` because it gets rid of an indirection, dynamic
allocation, and some reference counting.

Note that the null function reference is *NOT* a null pointer; it is a
`VMCallerCheckedAnyfunc` that has a null `func_ptr` member.

Part of #929
2020-06-24 10:08:13 -07:00
Thibault Charbonnier
8082aeaa5f C API: expose wasmtime_linker_get_one_by_name() (#1897)
* C API: expose wasmtime_linker_get_one_by_name()

* C API: remove unnecessary 'unsafe' qualifiers

* C API: avoid unnecessary mutable borrows of the Linker
2020-06-23 14:23:49 -05:00
Alex Crichton
6f6296816b Bind Trap::i32_exit_status in C API (#1912)
This allows embedders to take a different action when a WASI program
exits depending on whether it exited with an exit status or exited with
a trap.

cc bytecodealliance/wasmtime-py#30
2020-06-23 11:40:51 -05:00
Alexandru Ene
50ef890ae0 Fix compilation error due to redefinition. 2020-06-18 16:56:03 +01:00
Dan Gohman
caa87048ab Wasmtime 0.18.0 and Cranelift 0.65.0. 2020-06-11 17:49:56 -07: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
Dan Gohman
a76639c6fb Wasmtime 0.17.0 and Cranelift 0.64.0. (#1805) 2020-06-02 18:51:59 -07: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
Nick Fitzgerald
58b08b9d3a Move HostRef<T> into the C API crate
It isn't used by anything except for the C API and all of our embedder-exposed
APIs are already internally `Rc`-based, so it doesn't make sense to use with
them.
2020-06-01 15:09:51 -07:00
Nick Fitzgerald
a8ee0554a9 wasmtime: Initial, partial support for externref
This is enough to get an `externref -> externref` identity function
passing.

However, `externref`s that are dropped by compiled Wasm code are (safely)
leaked. Follow up work will leverage cranelift's stack maps to resolve this
issue.
2020-06-01 15:09:51 -07:00
Leonardo Yvens
0b3b9c298e impl From<anyhow::Error> for Trap (#1753)
* From<anyhow::Error> for Trap

* Add TrapReason::Error

* wasmtime: Improve Error to Trap conversion

* Remove Trap::message
2020-05-29 15:24:12 -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
Alex Crichton
036c8ec8c5 Fix assertion about length/capacity in C API (#1736)
I recently saw some [errors] crop up in wasmtime-go's CI, so let's be
sure to convert to `Box<[T]>` which guarantees the length/capacity of
the vector are equal.

[errors]: https://github.com/bytecodealliance/wasmtime-go/runs/694744570
2020-05-21 15:06:30 -05:00
Alex Crichton
68f0d2f6d0 Enable env_logger in the C API (#1737)
This commit ensures that `env_logger` and `RUST_LOG` are configured to
work with the C API.
2020-05-21 11:02:49 -05:00
Nick Fitzgerald
f28b3738ee Rename anyref to externref across the board 2020-05-20 11:58:55 -07:00
Nick Fitzgerald
1a4f3fb2df Update deps and tests for anyref --> externref
* Update to using `wasmparser` 0.55.0
* Update wasmprinter to 0.2.5
* Update `wat` to 1.0.18, and `wast` to 17.0.0
2020-05-14 12:47:37 -07:00
Alex Crichton
1e3a1fa372 Remove stray debugging printlns (#1698)
Forgot to do this earlier!
2020-05-14 10:26:09 -05:00
Alex Crichton
1247f2b4ae Add wasmtime-specific C APIs for tables (#1654)
This commit adds a suite of `wasmtime_funcref_table_*` APIs which mirror
the standard APIs but have a few differences:

* More errors are returned. For example error messages are communicated
  through `wasmtime_error_t` and out-of-bounds vs load of null can be
  differentiated in the `get` API.

* APIs take `wasm_func_t` instead of `wasm_ref_t`. Given the recent
  decision to remove subtyping from the anyref proposal it's not clear
  how the C API for tables will be affected, so for now these APIs are
  all specialized to only funcref tables.

* Growth now allows access to the previous size of the table, if
  desired, which mirrors the `table.grow` instruction.

This was originally motivated by bytecodealliance/wasmtime-go#5 where
the current APIs we have for working with tables don't quite work. We
don't have a great way to take an anyref constructed from a `Func` and
get the `Func` back out, so for now this sidesteps those concerns while
we sort out the anyref story.

It's intended that once the anyref story has settled and the official C
API has updated we'll likely delete these wasmtime-specific APIs or
implement them as trivial wrappers around the official ones.
2020-05-13 16:16:29 -07:00
Josh Triplett
08983bf39c Move crates/api to crates/wasmtime (#1693)
The `wasmtime` crate currently lives in `crates/api` for historical
reasons, because we once called it `wasmtime-api` crate. This creates a
stumbling block for new contributors.

As discussed on Zulip, rename the directory to `crates/wasmtime`.
2020-05-13 16:04:31 -05:00