Commit Graph

2348 Commits

Author SHA1 Message Date
Alex Crichton
21e0a99884 Delete the wasmtime-wasi-c crate (#844)
This commit deletes the old C implementation of the original
`wasi_unstable` module, instead only leaving around our single
`wasmtime-wasi` crate as the implementation for both
`wasi_snapshot_preview1` and `wasi_unstable`.

This hasn't been discussed (AFAIK) up until now, so this is also a
proposal! Some thoughts in favor of this deletion I would have are:

* This has been off-by-default for ages
* We don't build or test any of this on CI
* Published binaries with `wasmtime` do not have this possibility
  enabled
* Future refactorings to the `wasmtime-wasi` crate will either need to
  work around how the C implementation is different or bring it up to
  speed.

This is motivated by the last bullet point where I was working on
getting `wasmtime-wasi` working purely as an implementation detail on
top of the `wasmtime` crate itself, but quickly ran into a case where
the CLI would need to multiplex all sorts of wasi implementations. In
any case I'm curious what others think, is this too soon? Is there
something remaining blocking this? (etc)
2020-01-24 08:54:17 -08:00
Alex Crichton
177af53578 Simplify the signalhandlers module (#854)
* Remove the unused EnsureDarwinMachPorts function

When compiling the C++ shims for longjmp/setjmp/signal handling we don't
use the `USE_APPLE_MACH_PORTS` directive, so this function was entirely
unused anyway. This looks to be a holdover from when this was originally
copied from C++, but no need for keeping around this now-legacy
initialization.

* Remove the `wasmtime_init_finish` function

This looks like it's perhaps largely historical cruft at this point now
I think? The function, with the removal of the mach ports from the
previous commit, only reads the initializtion state of the signal
handlers. If the signal handlers failed to get installed, though, it
simply returns early rather than erroring out anyway. In any case a
follow-up commit will refactor `wasmtime_init_eager` to handle this as
well.

* Pare down `wasmtime_init_eager`

Similar to previous commits it looks like this function may have accrued
some debt over time, nowadays it doesn't really do much other than
capture a backtrace and install signal handlers. The `lazy_static` state
isn't really that necessary and we can rely on the `Once` primitive in
the standard library for one-time initialization.

This also updates the code to unconditionally panic if signal handlers
fail to get installed, which I think is the behavior that we'll want for
now and we can enhance it over time if necessary, but I don't think we
have a use case where it's currently necessary.
2020-01-23 15:42:55 -06:00
Alex Crichton
05d6c27142 Reduce number of thread locals in trap handling (#852)
* Reduce number of thread locals in trap handling

This commit refactors the trap handling portion of wasmtime with a few
goals in mind. I've been reading around a bit lately and feel that we
have a bit too few globals and thread locals floating around rather than
handles attached to contexts. I'm hoping that we can reduce the number
of thread locals and globals, and this commit is the start of reducing
this number.

The changes applied in this commit remove the set of thread locals in
the `traphandlers` module in favor of one thread local that's managed in
a sort of stack discipline. This way each call to `wasmtime_call*` sets
up its own stack local state that can be managed and read on that stack
frame.

Additionally the C++ glue code around `setjmp` and `longjmp` has all
been refactored to avoid going back and forth between Rust and C++. Now
we'll simply enter C++, go straight into `setjmp`/the call, and then
traps will enter Rust only once to both learn if the trap should be
acted upon and record information about the trap.

Overall the hope here is that context passing between `wasmtime_call*`
and the trap handling function will be a bit easier. For example I hope
to remove the global `get_trap_registry()` function next in favor of
storing a handle to a registry inside each instance, and the `*mut
VMContext` can be used to reach the `InstanceHandle` underneath, and
this trap registry.

* Update crates/runtime/src/traphandlers.rs

Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com>

Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
2020-01-23 14:34:47 -06:00
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
e5af0ae3de Move the Store::signature_cache field (#847)
This commit removes the `signature_cache` field from the `Store` type
and performs a few internal changes which are aimed to be a bit forward
looking towards #777, making `Store` threadsafe.

The changes made here are:

* The `SignatureRegistry` internal type now contains the reverse map
  that `signature_cache` was serving to do. This is populated on calls
  to `register` automatically and is accompanied by a `lookup` method as
  well.

* The `register_wasmtime_signature` and `lookup_wasmtime_signature`
  methods were removed from `Store` and now instead work by using the
  `Compiler::signatures` field.

* The `SignatureRegistry` type was updated to have interior mutability.
  The global `Compiler` type is highly likely to get shared across many
  threads through `Store`, so it needs some form of lock somewhere for
  mutation of the registry of signatures and this commit opts to put it
  inside `SignatureRegistry` which will eventually allow for the removal
  of most `&mut self` method on `Compiler`.
2020-01-22 14:54:55 -06: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
Julian Popescu
5d7635c351 Replaces load_file with load_bytes in rust macro (#750)
* Replaces `load_file` with `load_bytes` in macro

Loading an `AsRef<[u8]>` object is just more flexible than a filestring.
In the end you can just do `std::fs::read(&str)?` as the argument to get
the same behavior, but the reverse is a lot harder.

* updates markdown rust macro test example with new macro syntax

* Adds the `load_file` method back to rust macro

`load_file` was removed preferring `load_bytes`, but then later readded
with the `load_bytes` method as backend
2020-01-22 12:11:24 -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
Nick Fitzgerald
de72435576 Merge pull request #840 from jfoote/master
Update libfuzzer-sys dependency version number
2020-01-21 12:12:29 -08:00
Jonathan Foote
903f972cb4 Update Cargo.lock for libfuzzer-sys version number increment 2020-01-21 19:31:14 +00:00
Yury Delendik
3992b8669f [wasmtime-debug] Update DWARF expression transform to use new format. (#842) 2020-01-21 12:36:11 -06:00
Nick Fitzgerald
df04e7fdc7 Merge pull request #833 from fitzgen/initial-differential-fuzzing
Add initial differential fuzzing
2020-01-21 10:17:26 -08:00
Nick Fitzgerald
386c3a94b0 fuzz: Don't run tests or docs for differential fuzz target 2020-01-21 09:32:52 -08:00
Jonathan Foote
47f8c1e561 Update libfuzzer-sys dependency version number
To support oss-fuzz PoC, see
https://github.com/bytecodealliance/wasmtime/issues/611
2020-01-20 15:28:53 +00:00
Nick Fitzgerald
1667c462c5 CI: run a sample of our differential fuzz test corpus in CI 2020-01-17 16:19:38 -08:00
Nick Fitzgerald
1bf8de35f3 Add initial differential fuzzing
Part of #611
2020-01-17 16:17:04 -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
Alex Crichton
448faed5ca Deny missing documentation in wasmtime crate (#836)
* Deny missing documentation in `wasmtime` crate

Everything is largely documented now, so let's be sure to keep it that
way!

* Add windows docs
2020-01-17 12:36:57 -06:00
Alex Crichton
0bee67a852 Document and update the API of the externals.rs module (#812)
* Document and update the API of the `externals.rs` module

This commit ensures that all public methods and items are documented in
the `externals.rs` module, notably all external values that can be
imported and exported in WebAssembly. Along the way this also tidies up
the API and fixes a few bugs:

* `Global::new` now returns a `Result` and fails if the provided value
  does not match the type of the global.
* `Global::set` now returns a `Result` and fails if the global is either
  immutable or the provided value doesn't match the type of the global.
* `Table::new` now fails if the provided initializer does not match the
  element type.
* `Table::get` now returns `Option<Val>` instead of implicitly returning
  null.
* `Table::set` now returns `Result<()>`, returning an error on out of
  bounds or if the input type is of the wrong type.
* `Table::grow` now returns `Result<u32>`, returning the previous number
  of table elements if succesful or an error if the maximum is reached
  or the initializer value is of the wrong type. Additionally a bug was
  fixed here where if the wrong initializer was provided the table would
  be grown still, but initialization would fail.
* `Memory::data` was renamed to `Memory::data_unchecked_mut`.
  Additionally `Memory::data_unchecked` was added. Lots of caveats were
  written down about how using the method can go wrong.
* `Memory::grow` now returns `Result<u32>`, returning an error if growth
  fails or the number of pages previous the growth if successful.

* Run rustfmt

* Fix another test

* Update crates/api/src/externals.rs

Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com>

Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
2020-01-17 09:43:35 -06: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
Sergei Pepyakin
7890fa6705 Use __chkstk for aarch64 instead of __rust_probestack. (#800)
* Use __chkstk for aarch64 instead of __rust_probestack.

* Demote rustdoc to a regular comment to fix the build.
2020-01-16 20:23:32 -08:00
Andrew Chin
d81aa203bb Disable rustdoc on the wasmtime cli binary (#834)
Currently the wasmtime binary (src/bin/wasmtime) and the wasmtime API
crate (crates/api) share the same output filename.  This causes the
output files of the wasmtime binary to clobber the output files of the
wasmtime API crate.   So running `cargo doc --all` will often not build
the wasmtime API docs, which is the more useful of the two.

This is a rustdoc bug, and can be worked around by disabling
documentation for the wasmtime binary.
2020-01-16 19:55:08 -06: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
0c99ac3d7e Capture a backtrace before calling wasm (#830)
* Capture a backtrace before calling wasm

This helps mitigate the issue, at least locally, described in #829 and
there's some more comments inline in the code as well.

* Run rustfmt

* Move around where the trace happens
2020-01-16 15:40:44 -08: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
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
Sergei Pepyakin
5b8be5f262 Move compilation into Module from Instance. (#822)
* Move compilation into Module from Instance.

* Fix fuzzing

* Use wasmtime::Module in fuzzing crates

Instead of wasmtime_jit.

* Compile eagerly.

* Review fixes.

* Always use the saved name.

* Preserve the former behavior for fuzzing oracle
2020-01-16 16:37:10 -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
b5f293a71f Reset crates/api/c-examples/wasm-c-api to upstream/master 2020-01-16 19:48:00 +01:00
Marcin Mielniczuk
1c050b6a33 Reset crates/wasi-common/WASI to upstream/master 2020-01-16 19:47:04 +01:00
Marcin Mielniczuk
947e74d2ef Merge remote-tracking branch 'upstream/master' into poll 2020-01-16 19:43:12 +01:00
Marcin Mielniczuk
4f9218eded Get rid of hangup, it's incorrect 2020-01-16 19:41:34 +01:00
Yury Delendik
b2bfb98f1f Provide proper function index and name in the FrameInfo (#824)
* fix function index

* Add function name to JITFunctionTag

* Add ModuleSyncString.
2020-01-16 12:36:51 -06: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
Nick Fitzgerald
adcc047f4a Update fuzz crates (#826)
* deps: Update to arbitrary 0.3.x and libfuzzer-sys 0.2.0

* ci: Use cargo-fuzz 0.7.x in CI
2020-01-15 23:05:37 -06:00