Commit Graph

5591 Commits

Author SHA1 Message Date
Alex Crichton
70f179b499 Update wast to 7.0.0, fix stack overflow (#897)
This updates the `wast` dependency to include bytecodealliance/wat#48
which fixes a stack overflow for deeply recursive `*.wat` files.
2020-02-04 15:14:16 +01:00
Yury Delendik
169dbef784 Properly preserve and restore CFA state in FDE (#1373)
* Properly preserve and restore CFA state in FDE
2020-02-03 14:08:40 -08:00
Nick Fitzgerald
6559ea5ce6 Merge pull request #885 from alexcrichton/less-rebuild
Don't rebuild `wasmtime-cli` on all file changes
2020-02-03 21:50:55 +01:00
Nick Fitzgerald
a817a0879e Merge pull request #886 from alexcrichton/debug-config
Add a `Debug` implementation for `wsmtime::Config`
2020-02-03 21:50:23 +01:00
Yury Delendik
4599234c6f Don't generate DWARF sections when no functions were compiled. (#894) 2020-02-03 14:41:29 -06:00
Alex Crichton
ef843b9e5a Add a Debug implementation for wsmtime::Config
Handy to have in some situations!
2020-02-01 02:47:46 -08:00
Alex Crichton
1a64acf43b Don't rebuild wasmtime-cli on all file changes
Print out an explicit `rerun-if-changed` annotation in the `build.rs`
for the `wasmtime-cli` crate to avoid rebuilding it constantly as files
like tests change which don't need to cause a rebuild.
2020-02-01 02:45:27 -08:00
Alex Crichton
ea4faa4a01 Don't panic on shared memories (#883)
* Don't panic on shared memories

Instead return a first-class error
2020-02-01 10:33:30 +01:00
Peter Huene
f2fa484abf Merge pull request #880 from joshtriplett/profile-overrides
Don't spend build time optimizing build-time-only crates
2020-01-31 21:29:23 +01:00
Josh Triplett
8affec0896 Don't spend build time optimizing build-time-only crates
Crates used exclusively at build time, such as proc-macro crates and
their dependencies, don't benefit substantially from optimization; they
take far longer to optimize than time saved when running them during the
build. No machine code from these crates will appear in the final
compiled binary.

Use the new profile-overrides mechanism in Rust 1.41
(https://doc.rust-lang.org/cargo/reference/profiles.html#overrides) to
build such crates with opt-level 0.

On a 4-thread laptop, this brings build time from 6m27s to 4m21s, and
CPU time spent from 23m43s to 16m23s.
2020-01-31 11:47:15 -08:00
Alex Crichton
97ff297683 Remove another thread local in instance.rs (#862)
* Remove another thread local in `instance.rs`

This commit removes another usage of `thread_local!` in the continued
effort to centralize all thread-local state per-call (or basically state
needed for traps) in one location. This removal is targeted at the
support for custom signal handlers on instances, removing the previous
stack of instances with instead a linked list of instances.

The `with_signals_on` method is no longer necessary (since it was always
called anyway) and is inferred from the first `vmctx` argument of the
entrypoints into wasm. These functions establish a linked list of
instances on the stack, if needed, to handle signals when they happen.

This involved some refactoring where some C++ glue was moved into Rust,
so now Rust handles a bit more of the signal handling logic.

* Update some inline docs about `HandleTrap`
2020-01-31 13:45:54 +01:00
Nick Fitzgerald
cc07565985 Merge pull request #879 from alexcrichton/fix-leak
Fix a memory leak with link errors
2020-01-31 11:59:49 +01:00
Alex Crichton
d22b60e834 Fix a memory leak with link errors
During creation of an `InstanceHandle` if a link error occurred (such as
an element segment doesn't fit) then the instance itself would be leaked
by accident. This commit fixes the issue by ensuring that an
`InstanceHandle` is created very quickly so if any initialization later
fails it will be cleaned up through normal destructors.
2020-01-30 23:47:12 -08:00
Nick Fitzgerald
84c4d8cc6c Remove always-on logging from fuzz targets (#878)
Now that the `cargo fuzz` tooling is better, it is easier to reproduce failures,
and we don't need to be super paranoid about logging here.
2020-01-30 23:46:50 +01:00
Alex Crichton
c6438d0d44 fuzz: Don't panic on module compilation errors (#875)
Let's avoid having two phases of checks and just ignore the module
compilation errors during the instantiate oracle, only relying on one check.
2020-01-30 22:22:05 +01:00
Sergei Pepyakin
f2382db461 Check the types of values returned by Callable (#876)
If the values mismatch to the ones that were specified by the
signature of the callable, raise a trap!
2020-01-30 21:11:41 +01:00
Sergei Pepyakin
eb183d7ab3 Run rustfmt 1.41 (#877) 2020-01-30 21:03:50 +01:00
Alex Crichton
43c2da04b6 Try to fix Python wheel management on CI (#874)
We've been getting some errors on Linux which seem like they might be
related to a pinned `wheel` dependency. Apparently I originally added
this dependency to CI and I have no idea why I wrote down a `==`
dependency for it, so let's try not pinning and see what happens.
2020-01-30 18:23:40 +01:00
Alex Crichton
83ff0150b4 Improve panics/traps from imported functions (#857)
* Improve panics/traps from imported functions

This commit performs a few refactorings and fixes a bug as well. The
changes here are:

* The `thread_local!` in the `wasmtime` crate for trap information is
  removed. The thread local in the `wasmtime_runtime` crate is now
  leveraged to transmit trap information.

* Panics in user-provided functions are now caught explicitly to be
  carried across JIT code manually. Getting Rust panics unwinding
  through JIT code is pretty likely to be super tricky and difficult to
  do, so in the meantime we can get by with catching panics and resuming
  the panic once we've resumed in Rust code.

* Various take/record trap apis have all been removed in favor of
  working directly with `Trap` objects, where the internal trap object
  has been expanded slightly to encompass user-provided errors as well.

This borrows a bit #839 and otherwise will...

Closes #848

* Rename `r#return` to `ret`
2020-01-30 15:15:20 +01:00
Yury Delendik
a8cad05e80 Fix 'not enough arguments' during wasmtime run (#858)
* Fix 'not enough arguments' during wasmtime run

* add simple cli smoke tests

* autogenerate wasm
2020-01-30 14:29:50 +01:00
Andrew Brown
bc50815eac Re-enable simd_const.wast spec test (#872) 2020-01-29 09:02:24 +01:00
Andrew Brown
3502cd3cd1 Remove unused import; fixes #1367 (#1368) 2020-01-28 13:11:37 -05:00
Alex Crichton
1defae2742 Run cargo update on dependencies (#868)
Looks like we're able to actually drop a good number of various deps,
especially some large-ish ones like syn 0.14!

      Adding proc-macro2 v1.0.8
      Adding syn v1.0.14

    Removing base64 v0.10.1
    Removing cloudabi v0.0.3
    Removing crossbeam-utils v0.6.6
    Removing failure v0.1.6
    Removing failure_derive v0.1.6
    Removing fuchsia-cprng v0.1.1
    Removing proc-macro2 v0.4.30
    Removing proc-macro2 v1.0.7
    Removing quote v0.6.13
    Removing rand_core v0.3.1
    Removing rand_core v0.4.2
    Removing rand_os v0.1.3
    Removing rdrand v0.4.0
    Removing syn v0.14.9
    Removing syn v1.0.13
    Removing synstructure v0.9.0
    Removing unicode-xid v0.1.0
    Removing wincolor v1.0.2

    Updating arbitrary v0.3.2 -> v0.3.3
    Updating arrayref v0.3.5 -> v0.3.6
    Updating constant_time_eq v0.1.4 -> v0.1.5
    Updating crates.io index
    Updating derive_arbitrary v0.3.1 -> v0.3.3
    Updating indexmap v1.3.0 -> v1.3.1
    Updating itoa v0.4.4 -> v0.4.5
    Updating jobserver v0.1.18 -> v0.1.19
    Updating memchr v2.2.1 -> v2.3.0
    Updating num_cpus v1.11.1 -> v1.12.0
    Updating proc-macro-error v0.4.4 -> v0.4.5
    Updating proc-macro-error-attr v0.4.3 -> v0.4.5
    Updating quickcheck v0.9.0 -> v0.9.2
    Updating rand v0.7.2 -> v0.7.3
    Updating redox_users v0.3.1 -> v0.3.4
    Updating rust-argon2 v0.5.1 -> v0.7.0
    Updating rustversion v1.0.1 -> v1.0.2
    Updating serde_json v1.0.44 -> v1.0.45
    Updating structopt v0.3.7 -> v0.3.8
    Updating structopt-derive v0.4.0 -> v0.4.1
    Updating termcolor v1.0.5 -> v1.1.0
    Updating thread_local v1.0.0 -> v1.0.1
    Updating toml v0.5.5 -> v0.5.6
    Updating winapi-util v0.1.2 -> v0.1.3
2020-01-27 10:57:25 +01:00
Jakub Konka
4b84d19f77 Update instantiate.rs (#864)
* Update instantiate.rs

This must have snuck in the latest refactor of auto-generating the hostcalls from `*.witx` files.

* Fix formatting
2020-01-26 16:44:10 -06:00
Jakub Konka
daf0772bd5 Fix environ crate tests on nightly 2020-01-26 22:24:40 +01:00
Nick Fitzgerald
573cae2dda Merge pull request #861 from alexcrichton/empty-file
Remove a stray empty file
2020-01-24 12:58:51 -08:00
Alex Crichton
62a86ae52b Remove a stray empty file
Forgotten in a previous refactoring!
2020-01-24 12:30:38 -08:00
Alex Crichton
16804673a2 Support parsing the text format in wasmtime crate (#813)
* Support parsing the text format in `wasmtime` crate

This commit adds support to the `wasmtime::Module` type to parse the
text format. This is often quite convenient to support in testing or
tinkering with the runtime. Additionally the `wat` parser is pretty
lightweight and easy to add to builds, so it's relatively easy for us to
support as well!

The exact manner that this is now supported comes with a few updates to
the existing API:

* A new optional feature of the `wasmtime` crate, `wat`, has been added.
  This is enabled by default.
* The `Module::new` API now takes `impl AsRef<[u8]>` instead of just
  `&[u8]`, and when the `wat` feature is enabled it will attempt to
  interpret it either as a wasm binary or as the text format. Note that
  this check is quite cheap since you just check the first byte.
* A `Module::from_file` API was added as a convenience to parse a file
  from disk, allowing error messages for `*.wat` files on disk to be a
  bit nicer.
* APIs like `Module::new_unchecked` and `Module::validate` remain
  unchanged, they require the binary format to be called.

The intention here is to make this as convenient as possible for new
developers of the `wasmtime` crate. By changing the default behavior
though this has ramifications such as, for example, supporting the text
format implicitly through the C API now.

* Handle review comments

* Update more tests to avoid usage of `wat` crate

* Go back to unchecked for now in wasm_module_new

Looks like C# tests rely on this?
2020-01-24 14:20:51 -06:00
Alex Crichton
47d6db0be8 Reel in unsafety around InstanceHandle (#856)
* Reel in unsafety around `InstanceHandle`

This commit is an attempt, or at least is targeted at being a start, at
reeling in the unsafety around the `InstanceHandle` type. Currently this
type represents a sort of moral `Rc<Instance>` but is a bit more
specialized since the underlying memory is allocated through mmap.

Additionally, though, `InstanceHandle` exposes a fundamental flaw in its
safety by safetly allowing mutable access so long as you have `&mut
InstanceHandle`. This type, however, is trivially created by simply
cloning a `InstanceHandle` to get an owned reference. This means that
`&mut InstanceHandle` does not actually provide any guarantees about
uniqueness, so there's no more safety than `&InstanceHandle` itself.

This commit removes all `&mut self` APIs from `InstanceHandle`,
additionally removing some where `&self` was `unsafe` and `&mut self`
was safe (since it was trivial to subvert this "safety"). In doing so
interior mutability patterns are now used much more extensively through
structures such as `Table` and `Memory`. Additionally a number of
methods were refactored to be a bit clearer and use helper functions
where possible.

This is a relatively large commit unfortunately, but it snowballed very
quickly into touching quite a few places. My hope though is that this
will prevent developers working on wasmtime internals as well as
developers still yet to migrate to the `wasmtime` crate from falling
into trivial unsafe traps by accidentally using `&mut` when they can't.
All existing users relying on `&mut` will need to migrate to some form
of interior mutability, such as using `RefCell` or `Cell`.

This commit also additionally marks `InstanceHandle::new` as an `unsafe`
function. The rationale for this is that the `&mut`-safety is only the
beginning for the safety of `InstanceHandle`. In general the wasmtime
internals are extremely unsafe and haven't been audited for appropriate
usage of `unsafe`. Until that's done it's hoped that we can warn users
with this `unsafe` constructor and otherwise push users to the
`wasmtime` crate which we know is safe.

* Fix windows build

* Wrap up mutable memory state in one structure

Rather than having separate fields

* Use `Cell::set`, not `Cell::replace`, where possible

* Add a helper function for offsets from VMContext

* Fix a typo from merging

* rustfmt

* Use try_from, not as

* Tweak style of some setters
2020-01-24 14:20:35 -06:00
Alex Crichton
3db1074c15 Improve handling of strings for backtraces (#843)
* Improve handling of strings for backtraces

Largely avoid storing strings at all in the `wasmtime-*` internal
crates, and instead only store strings in a separate global cache
specific to the `wasmtime` crate itself. This global cache is inserted
and removed from dynamically as modules are created and deallocated, and
the global cache is consulted whenever a `Trap` is created to
symbolicate any wasm frames.

This also avoids the need to thread `module_name` through the jit crates
and back, and additionally removes the need for `ModuleSyncString`.

* Run rustfmt
2020-01-24 11:53:55 -06:00
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
Joshua Nelson
5edf015ada Make get_libcall_funcref pub(crate) (#1291)
* Make `get_libcall_funcref` `pub(crate)`

Closes https://github.com/bytecodealliance/cranelift/issues/1273.

Since get_libcall_funcref is only used internally by the verifier,
it doesn't make sense to have it be public. This will encourage users to
look elsewhere for `memcpy` (they should be looking at
https://docs.rs/cranelift-frontend/0.51.0/cranelift_frontend/struct.FunctionBuilder.html#method.emit_small_memcpy)
2020-01-24 16:43:44 +01:00
Ryan Hunt
c360007b19 Drop 'basic-blocks' feature (#1363)
* All: Drop 'basic-blocks' feature

This makes it so that 'basic-blocks' cannot be disabled and we can
start assuming it everywhere.

* Tests: Replace non-bb filetests with bb version

* Tests: Adapt solver-fixedconflict filetests to use basic blocks
2020-01-23 22:36:06 -07:00
Ryan Hunt
710182ad26 Wasm: Add support for nullref type
Reference types now supports proper a nullref type. This commit changes
the Wasm translator to support this.
2020-01-23 16:31:34 -06:00
Ryan Hunt
32a95a89eb Wasm: Add support for typed select instruction
Reference types introduces a typed select operation. It has identical execution
semantics so no codegen change is needed.
2020-01-23 16:31:34 -06: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
Ryan Hunt
fc58dd6aff Tests: Add basic test for r32, r64
This commit adds a basic test for reference types on 32/64bit systems.
 * Storing a ref type in a table
 * Loading a ref type from a table
 * Passing ref types to a function
 * Returning ref types from a function
 * `is_null` instruction
 * `is_invalid` instruction
2020-01-23 13:37:11 -06:00
Ryan Hunt
946251e655 Codegen: Align representation of stackmap with SpiderMonkey
This commit aligns the representation of stackmaps to be the same
as Spidermonkey's by:
 * Reversing the order of the bitmap from low addresses to high addresses
 * Including incoming stack arguments
 * Excluding outgoing stack arguments

Additionally, some accessor functions were added to allow Spidermonkey
to access the internals of the bitmap.
2020-01-23 13:37:11 -06:00
Ryan Hunt
bbc0a328c7 Codegen: Allow encoding of (r32|r64).(load|store)
Accessing Wasm reference globals that are reference types will
want to use the plain load/store instructions. This commit adds
encodings for these instructions to match loading a i32/i64.
Producers of IR are required to insert the appropriate barriers
around the loads/stores.
2020-01-23 13:37:11 -06:00
Ryan Hunt
848baa0aa7 Codegen: Add ref.is_invalid instruction
Spidermonkey returns a sentinel ref value of '-1' from some VM functions
to indicate failure. This commit adds an instruction analagous to ref.is_null
that checks for this value.
2020-01-23 13:37:11 -06:00
Ryan Hunt
a15bb9cfcb Codegen: Use GPR regclass for reference types on x86 2020-01-23 13:37:11 -06:00
Ryan Hunt
3b1dda8e92 Wasm: Ensure result of ref.is_null is I32
The (r32|r64).is_null instruction yields a boolean type, so we must
convert a Wasm `ref.is_null` to an integer so we don't get verifier
errors.
2020-01-23 13:37:11 -06:00
Ryan Hunt
41f225804b Wasm: Allow environment to translate some global.set/get operations
Spidermonkey will need to emit pre/post barriers for global.set/get to a
reference type. #1176 and #1299 plan to add a template concept that could
be used to implement this. Once that has been stabilized, we should be able
to remove this code in favor of templates easily.
2020-01-23 13:37:11 -06:00
Ryan Hunt
f41bf5ecca Wasm: Use environment to translate reference types instructions and add support for multiple tables
This commit introduces environment functions to handle the translation of
reference type instructions, analogous to how bulk-memory was implemented.

Additionally, the bulk-memory instructions that operate on tables are extended
to support multiple table indices.
2020-01-23 13:37:11 -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
Benjamin Bouvier
3125431ece Address nits from #1325 2020-01-23 09:39:49 +01: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