Commit Graph

5659 Commits

Author SHA1 Message Date
Alex Crichton
70345aff31 Remove all global state from the caching system (#863)
* Remove all global state from the caching system

This commit is a continuation of an effort to remove usages of
`lazy_static!` and similar global state macros which can otherwise be
accomodated with passing objects around. Previously there was a global
cache system initialized per-process, but it was initialized in a bit of
a roundabout way and wasn't actually reachable from the `wasmtime` crate
itself. The changes here remove all global state, refactor many of the
internals in the cache system, and makes configuration possible through
the `wasmtime` crate.

Specifically some changes here are:

* Usage of `lazy_static!` and many `static` items in the cache module
  have all been removed.
* Global `cache_config()`, `worker()`, and `init()` functions have all
  been removed. Instead a `CacheConfig` is a "root object" which
  internally owns its worker and passing around the `CacheConfig` is
  required for cache usage.
* The `wasmtime::Config` structure has grown options to load and parse
  cache files at runtime. Currently only loading files is supported,
  although we can likely eventually support programmatically configuring
  APIs as well.
* Usage of the `spin` crate has been removed and the dependency is removed.
* The internal `errors` field of `CacheConfig` is removed, instead
  changing all relevant methods to return a `Result<()>` instead of
  storing errors internally.
* Tests have all been updated with the new interfaces and APIs.

Functionally no real change is intended here. Usage of the `wasmtime`
CLI, for example, should still enable the cache by default.

* Fix lightbeam compilation
2020-02-06 13:11:06 -06:00
Alex Crichton
4ff8257b17 Update binaryen fuzzing dependency (#913)
Fixes an infinite loop in fuzz test case generation, pulling in
WebAssembly/binaryen#2637
2020-02-06 19:58:16 +01:00
Alex Crichton
348c597a8e Remove global state for trap registration (#909)
* Remove global state for trap registration

There's a number of changes brought about in this commit, motivated by a
few things. One motivation was to remove an instance of using
`lazy_static!` in an effort to remove global state and encapsulate it
wherever possible. A second motivation came when investigating a
slowly-compiling wasm module (a bit too slowly) where a good chunk of
time was spent in managing trap registrations.

The specific change made here is that `TrapRegistry` is now stored
inside of a `Compiler` instead of inside a global. Additionally traps
are "bulk registered" for a module rather than one-by-one. This form of
bulk-registration allows optimizing the locks used here, where a lock is
only held for a module at-a-time instead of once-per-function.

With these changes the "unregister" logic has also been tweaked a bit
here and there to continue to work. As a nice side effect the `Compiler`
type now has one fewer field that requires actual mutability and has
been updated for multi-threaded compilation, nudging us closer to a
world where we can support multi-threaded compilation. Yay!

In terms of performance improvements, a local wasm test file that
previously took 3 seconds to compile is now 10% faster to compile,
taking ~2.7 seconds now.

* Perform trap resolution after unwinding

This avoids taking locks in signal handlers which feels a bit iffy...

* Remove `TrapRegistration::dummy()`

Avoid an case where you're trying to lookup trap information from a
dummy module for something that happened in a different module.

* Tweak some comments
2020-02-06 12:40:50 -06:00
Alex Crichton
9dffaf9d57 Update wasmparser dependency (#912)
* Update wasmparser dependency

Closes #905

* Fix lightbeam compilation
2020-02-06 12:25:32 -06:00
Alex Crichton
c860edc14f Disable cranelift's verifier by default (#882)
The intention of the `wasmtime` crate was to disable this verifier by
default, but it looks like cranelift actually has it turned on by
default which was making our documentation incorrect!

This was discovered by seeing a number of timeouts when fuzzing. The
debug verifier is great for fuzzing, however, so fuzzing is updated to
enable this unconditionally, meaning we'll still have timeouts. For
general users though this should make the documentation correct that the
`wasmtime` crate, by default, disables the debug verifier.
2020-02-06 19:04:53 +01:00
Alex Crichton
8e0651374a Deregister JIT frames on Linux in reverse order (#910)
Investigating a susprisingly slow-compiling module recently, it turns
out that if you create a wasm module with 40k empty functions (e.g.
`(module (func) (func) (func) ...)`) then it takes **3 seconds** to
compile and drop via the CLI locally on a Linux system. This seems like
an extraordinary amount of time for "doing nothing", and after some
profiling I found that basically all of the time was spent in
`__deregister_frame` calls.

Poking around in the source it looks like libgcc is managing some form
of linked list, and by deregistering in the LIFO order instead of FIFO
order it avoids a quadratic search of all registered functions. Now that
being said it's still pretty bad to do a linear search all the time, and
nothing will be fixed if there are *two* instances both with 40k
functions.

For now though I hope that this will patch over the performance issue
and we can figure out better ways to manage this in the future.
2020-02-06 18:41:44 +01: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
Yury Delendik
c9dce98ba2 Test wasmtime-c-api crate (#904)
* Test c-api
2020-02-05 14:14:07 -06:00
Nathan Froyd
efb7f0a6c7 factor out declaring exports (#906) 2020-02-05 11:17:12 -06:00
Alex Crichton
9f76df6c85 Remove trap registration from wasmtime crate (#903)
Our trampoline shims no longer have traps baked into them so this is
largely all dead code now at this point.
2020-02-05 10:28:50 -06:00
Yury Delendik
961853fd1c Implement wasm_config_new and wasm_engine_new_with_config (#901)
* Implement wasm_config_new and wasm_engine_new_with_config
2020-02-05 09:29:46 -06:00
Yury Delendik
b3ac718421 Implement FIXME in debug/src/expression.rs (#902) 2020-02-04 18:47:20 -06:00
Nathan Froyd
a136d1cb00 add documentation mentions of cranelift-object where appropriate
This change makes it slightly more obvious that `cranelift-object` can
be used in lieu of `cranelift-faerie`.
2020-02-04 15:42:39 -06:00
Alex Crichton
1bfca842b0 Support Func imports with zero shims (#839)
* Move `Func` to its own file

* Support `Func` imports with zero shims

This commit extends the `Func` type in the `wasmtime` crate with static
`wrap*` constructors. The goal of these constructors is to create a
`Func` type which has zero shims associated with it, creating as small
of a layer as possible between wasm code and calling imported Rust code.

This is achieved by creating an `extern "C"` shim function which matches
the ABI of what Cranelift will generate, and then the host function is
passed directly into an `InstanceHandle` to get called later. This also
enables enough inlining opportunities that LLVM will be able to see all
functions and inline everything to the point where your function is
called immediately from wasm, no questions asked.
2020-02-04 14:32:35 -06:00
Alex Crichton
e09231e33f Add a test tha call_indirect traps produce good errors (#889)
Closes #178
2020-02-04 14:05:28 -06:00
Yury Delendik
a01bcff219 Implement wasm_global_type (#898) 2020-02-04 13:50:56 -06:00
Nick Fitzgerald
19a188789b Bump to 0.57.0 (#1375)
* Update wasmparser to 0.48.2

* Bump to version 0.57.0
2020-02-04 16:18:59 +01:00
Alex Crichton
76f9e7ea41 Verify correct number and types of arguments enter wasm (#890)
Whenever we enter wasm code we need to verify that the correct number
and the correct types of arguments were passed in, lest we misinterpret
bits!

Closes #52
2020-02-04 09:13:13 -06:00
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