Commit Graph

477 Commits

Author SHA1 Message Date
Jakub Konka
dab9ffcca5 Merge pull request #1278 from kubkon/hello-wiggle
[wasi-common]: Hello wiggle!
2020-03-11 18:11:07 +01:00
Jakub Konka
7bcbf40f1d Fix wiggle's tests 2020-03-11 17:33:29 +01:00
Jakub Konka
ae0a0240ed Add 'crates/wiggle/' from commit 'cd484e49932d8dd8f1bd1a002e0717ad8bff07fb'
git-subtree-dir: crates/wiggle
git-subtree-mainline: 2ead747f48
git-subtree-split: cd484e4993
2020-03-11 17:30:49 +01:00
Yury Delendik
f76b36f737 Write .debug_frame information (#53)
* Write .debug_frame information

* mv map_reg
2020-03-11 10:22:51 -05:00
Nick Fitzgerald
2ead747f48 Enable bulk memory in the fuzzers (#1277) 2020-03-11 08:02:19 -05:00
Pat Hickey
cd484e4993 add a lifetime to the wiggle_runtime::GuestErrorType trait (#41)
* add a lifetime to the wiggle_runtime::GuestErrorType trait, wiggle_tests::WasiCtx struct

* wiggle-generate: make config parsing public so it can be reused in lucet
2020-03-10 14:48:57 -07:00
Pat Hickey
2139020d6d add manual interface for borrowing a GuestPtr (#40)
* add manual interface for borrowing a GuestPtr

* add manual borrow checking methods for slice and str as well

* update test to use borrow_slice
2020-03-10 12:33:02 -07:00
Jakub Konka
bd5e71b038 [wasi-common]: add armv7 support to wasi-common (#1269)
* Add armv7 support to wasi-common

This commit enables `target_pointer_width = 32` compatibility for
`wasi-common` (and by transitivity, any crate found inside, e.g., `yanix`).
I've also added a simplistic (bare minimum) check to our CI to ensure
that `wasi-common` cross-compiles to `armv7-unknown-gnueabihf` fine.
While here, I've done the same for `wasm32-unknown-emscripten`.

* Clean arch-specific impls + reuse libc consts

* Make SeekLoc::from_raw platform independent

* Collapse CI cc jobs into one
2020-03-10 19:18:59 +01:00
Nick Fitzgerald
67bfeea16f fuzzing: Limit the total number of API calls generated (#1265)
To avoid libfuzzer timeouts, limit the total number of API calls we generate in
the `api_calls` fuzz target. We were already limiting the number of exported
function calls we made, and this extends the limit to all API calls.
2020-03-10 11:28:00 -05:00
Nick Fitzgerald
674a6208d8 Implement data.drop and memory.init and get the rest of the bulk memory spec tests passing (#1264)
* Enable the already-passing `bulk-memoryoperations/imports.wast` test

* Implement support for the `memory.init` instruction and passive data

This adds support for passive data segments and the `memory.init` instruction
from the bulk memory operations proposal. Passive data segments are stored on
the Wasm module and then `memory.init` instructions copy their contents into
memory.

* Implement the `data.drop` instruction

This allows wasm modules to deallocate passive data segments that it doesn't
need anymore. We keep track of which segments have not been dropped on an
`Instance` and when dropping them, remove the entry from the instance's hash
map. The module always needs all of the segments for new instantiations.

* Enable final bulk memory operations spec test

This requires special casing an expected error message for an `assert_trap`,
since the expected error message contains the index of an uninitialized table
element, but our trap implementation doesn't save that diagnostic information
and shepherd it out.
2020-03-10 09:30:11 -05:00
Alex Crichton
11510ec426 Disallow values to cross stores (#1016)
* Disallow values to cross stores

Lots of internals in the wasmtime-{jit,runtime} crates are highly
unsafe, so it's up to the `wasmtime` API crate to figure out how to make
it safe. One guarantee we need to provide is that values never cross
between stores. For example you can't take a function in one store and
move it over into a different instance in a different store. This
dynamic check can't be performed at compile time and it's up to
`wasmtime` to do the check itself.

This adds a number of checks, but not all of them, to the codebase for
now. This primarily adds checks around instantiation, globals, and
tables. The main hole in this is functions, where you can pass in
arguments or return values that are not from the right store. For now
though we can't compile modules with `anyref` parameters/returns anyway,
so we should be good. Eventually when that is supported we'll need to
put the guards in place.

Closes #958

* Clarify how values test they come from stores

* Allow null anyref to initialize tables
2020-03-10 09:28:31 -05:00
Jakub Konka
773915b4bf [wasi-common]: clean up error handling (#1253)
* Introduce WasiCtxBuilderError error type

`WasiCtxBuilderError` is the `wasi-common` client-facing error type
which is exclusively thrown when building a new `WasiCtx` instance.
As such, building such an instance should not require the client to
understand different WASI errno values as was assumed until now.

This commit is a first step at streamlining error handling in
`wasi-common` and makes way for the `wiggle` crate.

When adding the `WasiCtxBuilderError`, I've had to do two things of
notable importance:
1. I've removed a couple of `ok_or` calls in `WasiCtxBuilder::build`
   and replaced them with `unwrap`s, following the same pattern in
   different builder methods above. This is fine since we _always_
   operate on non-empty `Option`s in `WasiCtxBuilder` thus `unwrap`ing
   will never fail. On the other hand, this might be a good opportunity
   to rethink the structure of our builder, and how we good remove
   the said `Option`s especially since we always populate them with
   empty containers to begin with. I understand this is to make
   chaining of builder methods easier which take and return `&mut self`
   and the same applies to `WasiCtxBuilder::build(&mut self)` method,
   but perhaps it would more cleanly signal the intentions if we simply
   moved `WasiCtxBuilder` instance around. Food for thought!
2. Methods specific to determining rights of passed around `std::fs::File`
   objects when populating `WasiCtx` `FdEntry` entities now return
   `io::Error` directly so that we can reuse them in `WasiCtxBuilder` methods
   (returning `WasiCtxBuilderError` error type), and in syscalls
   (returning WASI errno).

* Return WasiError directly in syscalls

Also, removes `error::Error` type altogether. Now, `io::Error` and
related are automatically converted to their corresponding WASI
errno value encapsulated as `WasiError`.

While here, it made sense to me to move `WasiError` to `wasi` module
which will align itself well with the upcoming changes introduced
by `wiggle`. To different standard `Result` from WASI specific, I've
created a helper alias `WasiResult` also residing in `wasi` module.

* Update wig

* Add from ffi::NulError and pass context to NotADirectory

* Add dummy commit to test CI
2020-03-09 22:58:55 +01:00
Yury Delendik
963bf0e255 Export wasm_config_delete (and few more _delete) (#1262) 2020-03-09 13:44:16 -05:00
Yury Delendik
ba1f10f4d4 Removes panic! from the debug crate. (#1261) 2020-03-09 12:25:38 -05:00
Jakub Konka
061390ee1b [wasi-common]: move filetime module to yanix (#1255)
* Move filetime module to yanix

I've noticed that we could replace every occurrence of `crate::Result`
in `filetime` mods with `io::Result`, so I thought why not move it
to `yanix` and get rid off a lot of unnecessary code duplication
within `wasi-common`. Now, ideally I'd have our `filetime` modifications
backported to Alex's [`filetime`] crate, but one step at a time
(apologies Alex, I was meant to backport this ages ago, just didn't
find the time yet... :-().

Anyway, this commit does just that; i.e., moves the `filetime` modules
into `yanix` which seems a better fit for this type of code.

[`filetime`]: https://github.com/alexcrichton/filetime

There is one caveat here. On Emscripten, converting between `filetime::Filetime`
and `libc::timespec` appears to be lossy, at least as far as the
types are concerned. Now, `filetime::Filetime`'s seconds field is
`i64` while nanoseconds field is `u32`, while Emscripten's
`libc::timespec` requires both to be `i32` width. This might actually
not be a problem since I don't think it's possible to fill `filetime::Filetime`
struct with values of width wider than `i32` since Emscripten is 32bit
but just to be on the safe side, we do a `TryInto` conversion, log
the error (if any), and return `libc::EOVERFLOW`.

* Run cargo fmt

* Use i64::from instead of as cast
2020-03-09 16:07:09 +01:00
Jakub Konka
e5b9f1b786 [wasi-common]: winx now returns io::Error directly (#1243)
* Winx now returns io::Error

This commit is a spiritual follower of #1242 in the sense that it
adjusts `winx` to also return `io::Error` directly rather than
tossing a custom error type here and there.

* Adapt wasi-common to changes in winx

* Run cargo fmt

* Swap overly big map_err with explicit match
2020-03-09 10:32:01 +01:00
Dan Gohman
fbe29da5cc Miscelaneous docs updates and fixes. (#1249)
Update references to things in CraneStation which have moved, WASI documentation
which has moved to the WASI repo, and fix a few typos.
2020-03-08 16:11:17 +01:00
Pat Hickey
06bcac3e43 Allow creation of GuestPtr<[T]> from GuestPtr<T> and length (#39)
* add GuestPtr::as_array method

* wasi test: show with type signatures we have achieved the desired api
2020-03-06 17:45:09 -08:00
Pat Hickey
6e3ec6a96d Minor refactors to make wiggle-generate reusable externally (#34)
* wiggle-generate: pass witx doc in explicitly

* wiggle-generate: Names takes &Config, minor refactor for reuse
2020-03-06 16:55:05 -08:00
Pat Hickey
c78416912c Check safety of as_raw with a simplified borrow checker (#37)
* wiggle-runtime: add as_raw method for [T]

* add trivial borrow checker back in

* integrate runtime borrow checker with as_raw methods

* handle pointer arith overflow correctly in as_raw, create PtrOverflow error

* runtime: add validation back to GuestType

* generate: impl validate for enums, flags, handles, ints

* oops! make validate its own method on trait GuestTypeTransparent

* fix transparent impls for enum, flag, handle, int

* some structs are transparent. fix tests.

* tests: define byte_slice_strat and friends

* wiggle-tests: i believe my allocator is working now

* some type juggling around memset for ease of use

* make GuestTypeTransparent an unsafe trait

* delete redundant validation of pointer align

* fix doc

* wiggle_test: aha, you cant use sets to track memory areas

* add multi-string test

which exercises the runtime borrow checker against
HostMemory::byte_slice_strat

* oops left debug panic in

* remove redundant (& incorrect, since unchecked) length calc

* redesign validate again, and actually hook to as_raw

* makr all validate impls as inline

this should hopefully allow as_raw's check loop to be unrolled to a
no-op in most cases!

* code review fixes
2020-03-06 16:04:56 -08:00
Jakub Konka
42fae4e3b8 [wasi-common]: yanix now returns io::Error directly (#1242)
* Yanix now returns io::Error

This commit may seem somewhat controversial at first, but hear me
out first. Currently, Yanix would return a custom error that's a
wrapper around three other error types returned by various entities
inside Rust's `libstd`. In particular, Yanix's error type would wrap
`io::Error`, `num::TryFromIntError` and `ffi::NulError`. It turns
out that there is a natural conversion between the first and the last
and provided by the standard library, i.e., `From<ffi::NulError> for io::Error`
is provided. So at the surface it may seem that only the first two
wrapped error types are worth keeping.

Digging a little bit deeper into `libstd`, `num::TryFromIntError`
is essentially speaking only a marker that the integral conversion
went wrong. The struct implementing this error stores a unit type,
and nothing more. It therefore seems like a waste to wrap this
particular error when we could unify everything under `io::Error`.
And so, whenever we perform an int conversion, I suggest we simply
remap the error to `io::Error::from_raw_os_error(libc::EOVERFLOW)`
since this carries a comparable amount of information.

As a result of completely discarding `yanix::Error` custom error type,
we are invariably simplifying `yanix` itself, but also allowing
`wasi-common` to simplify in several places as well.

* Adapt wasi-common to changes in yanix

* Add Cargo.lock

* Unwrap try_into's where possible

* Remove unnecessary type annotation
2020-03-06 14:20:54 -08:00
iximeow
7e0d9decbf Virtual file support (#701)
* Add support for virtual files (eg, not backed by an OS file).

Virtual files are implemented through trait objects, with a default
implementation that tries to behave like on-disk files, but entirely
backed by in-memory structures.

Co-authored-by: Dan Gohman <sunfish@mozilla.com>
2020-03-06 11:08:13 -08:00
Andrew Brown
1d15054310 Remove the debug crate's hard-coded dependency on register ordering 2020-03-06 10:53:22 -08:00
Alex Crichton
85fab0ab56 Expand Func documentation, rewrite Rust embed docs (#1236)
This commit expands the documentation of the `Func` type as well as
updating the Rust embedding tutorial with more recent APIs. I wanted to
also leave space in the Rust tutorial to get more ambitious over time
with what it's documenting, but I stopped around here, curious to see
what others think about it!
2020-03-05 12:54:42 -06:00
Alex Crichton
92a6636b6d Add more thorough safety documentation to types 2020-03-05 08:25:04 -08:00
Jakub Konka
135a48ca7e wasi-common error cleanup: part 1, yanix (#1226)
* Reuse std::io::Error for raw *nix errno

This commit removes custom `yanix::Errno` and instead (as was
previously suggested) reuses `std::io::Error` to generate and wrap
raw *nix errno value.

* Update wasi-common to use new Yanix error type

This commit updates `wasi-common` to use new way of handling raw
OS error in `yanix`; i.e., via re-use of `std::io::Error` instead
of a custom `Errno` enum.

* Fix formatting

* Unwrap if io::Error created from raw OS error

This commit calls `unwrap` on `err` if that one was created via
`io::Error::last_os_error()`. It also refactors error matching
in several syscalls on the BSD platform (mainly).
2020-03-05 10:08:28 +01:00
Yury Delendik
d5c0f6bff8 Fix infinite loop in DWARF address transform algorithm (#1228) 2020-03-04 13:31:14 -06:00
Alex Crichton
84bcbd4650 Fix some tests 2020-03-04 10:37:10 -08:00
Alex Crichton
8cee547519 Run rustfmt 2020-03-04 10:36:26 -08:00
Alex Crichton
ca9f33b6d9 Rewrite for recursive safety
This commit rewrites the runtime crate to provide safety in the face
of recursive calls to the guest. The basic principle is that
`GuestMemory` is now a trait which dynamically returns the
pointer/length pair. This also has an implicit contract (hence the
`unsafe` trait) that the pointer/length pair point to a valid list of
bytes in host memory "until something is reentrant".

After this changes the various suite of `Guest*` types were rewritten.
`GuestRef` and `GuestRefMut` were both removed since they cannot safely
exist. The `GuestPtrMut` type was removed for simplicity, and the final
`GuestPtr` type subsumes `GuestString` and `GuestArray`. This means
that there's only one guest pointer type, `GuestPtr<'a, T>`, where `'a`
is the borrow into host memory, basically borrowing the `GuestMemory`
trait object itself.

Some core utilities are exposed on `GuestPtr`, but they're all 100%
safe. Unsafety is now entirely contained within a few small locations:

* Implementations of the `GuestType` for primitive types (e.g. `i8`,
  `u8`, etc) use `unsafe` to read/write memory. The `unsafe` trait of
  `GuestMemory` though should prove that they're safe.

* `GuestPtr<'_, str>` has a method which validates utf-8 contents, and
  this requires `unsafe` internally to read all the bytes. This is
  guaranteed to be safe however given the contract of `GuestMemory`.

And that's it! Everything else is a bunch of safe combinators all built
up on the various utilities provided by `GuestPtr`. The general idioms
are roughly the same as before, with various tweaks here and there. A
summary of expected idioms are:

* For small values you'd `.read()` or `.write()` very quickly. You'd
  pass around the type itself.

* For strings, you'd pass `GuestPtr<'_, str>` down to the point where
  it's actually consumed. At that moment you'd either decide to copy it
  out (a safe operation) or you'd get a raw view to the string (an
  unsafe operation) and assert that you won't call back into wasm while
  you're holding that pointer.

* Arrays are similar to strings, passing around `GuestPtr<'_, [T]>`.
  Arrays also have a `iter()` method which yields an iterator of
  `GuestPtr<'_, T>` for convenience.

Overall there's still a lot of missing documentation on the runtime
crate specifically around the safety of the `GuestMemory` trait as well
as how the utilities/methods are expected to be used. Additionally
there's utilities which aren't currently implemented which would be easy
to implement. For example there's no method to copy out a string or a
slice, although that would be pretty easy to add.

In any case I'm curious to get feedback on this approach and see what
y'all think!
2020-03-04 10:26:47 -08:00
Maciej Woś
8acfdbdd8a add more wrappers and getters (#1222) 2020-03-03 22:58:11 -06:00
Jakub Konka
3764204250 Put context object behind a ref rather than mut ref
This commit puts context object, i.e., the implementor of the
WASI snapshot, behind a reference `&self` rather than a mutable
reference `&mut self`. As suggested by @alexcrichton, this gives
the implementor the possibility to determine how it handles its
interior mutability.
2020-03-03 17:50:32 +01:00
Alex Crichton
77e17d8f71 Add a wasmtime-specific wasmtime_wat2wasm C API (#1206)
* Add a wasmtime-specific `wasmtime_wat2wasm` C API

This commit implements a wasmtime-specific C API for converting the text
format to the binary format. An upstream spec issue exists for adding
this to the C API, but in the meantime we can experiment with our own
version of this API and use it in the C# extension, for example!

Closes #1000

* Reorder arguments

* Use wasm_byte_vec_t for input `*.wat`

* Mark wat input as const

* Return an error message and use `fixed`

* Actually include the error message

* Use `fixed` in `Module.cs` as well
2020-03-03 10:29:20 -06:00
Jakub Konka
ea4d2d0535 Return *mut u8 in GuestPtrMut::as_raw
Currently, we create an immutable `GuestPtr` from `self` and call
`as_raw` on it which correctly returns `*const u8`. However, since
we're dealing with `GuestPtrMut` I thought it might make more sense
to return `*mut u8` directly instead. This will be needed (and will
save us from silly casts `*const _ as *mut _`) in plugging in
`Iovec<'_>` into `std::io::IoSliceMut` in `fd_read` and `fd_pread` calls.
2020-03-03 10:19:50 +01:00
Jakub Konka
db8fec354d Add current snapshot1 WASI spec as test + fixes (#31)
* Add WASI spec (minus unions)

* Fill in all WASI shims

* Clean up derives and fix noncopy struct write method

This commit does three things:
* it uses the full, current snapshot1 WASI spec as a compilation test
* it fixes noncopy struct write method (which was incorrectly resolved
  in certain cases to the inherent method of the `GuestPtrMut` rather
  than the interface method `GuestType::write`
* it cleans up derives for structs and unions which should not auto-derive
  `PartialEq`, `Eq`, or `Hash` since their members are not guaranteed to
  be compatible
2020-03-02 23:28:13 +01:00
Alex Crichton
fe9debfed3 Update WASI submodule to update transitive wast crate (#1207)
One less version to build!
2020-03-02 11:34:04 -06:00
Alex Crichton
8597930eed rename PassiveElemIndex to ElemIndex and same for PassiveDataIndex (#1188)
* rename PassiveElemIndex to ElemIndex and same for PassiveDataIndex (#1411)

* rename PassiveDataIndex to DataIndex

* rename PassiveElemIndex to ElemIndex

* Apply renamings to wasmtime as well

* Run rustfmt

Co-authored-by: csmoe <csmoe@msn.com>
2020-03-02 08:55:25 -06:00
Nick Fitzgerald
4866fa0e6a Limit rayon to one thread during fuzzing
This should enable more deterministic execution.
2020-02-28 18:35:09 -08:00
Nick Fitzgerald
6e2bb9ebdd Limit the number of exported function calls we make in the API calls fuzzer
This should fix some fuzzing timeouts like
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20847
2020-02-28 15:48:24 -08:00
Nick Fitzgerald
f2fef600c6 Implement Arbitrary::size_hint for WasmOptTtf 2020-02-28 15:48:24 -08:00
Nick Fitzgerald
5ed9796ef3 Implement Arbitrary::size_hint for ApiCalls 2020-02-28 15:48:24 -08:00
Nick Fitzgerald
506a83d8ef Update to arbitrary@0.4.0 and libfuzzer-sys@0.3.1 2020-02-28 15:48:24 -08:00
Alex Crichton
86ff6d7aef Merge pull request #1019 from alexcrichton/cranelift-merge-for-realz
Merge the Cranelift repository into Wasmtime
2020-02-28 17:13:14 -06:00
Jakub Konka
9cc9dacc08 Escape reserved keywords in generate
This commit escapes certain (hopefully all keywords present in
snapshot1!) reserved keywords in Rust that are autogenerated by
wiggle.
2020-02-28 21:58:43 +01:00
Jakub Konka
16fe947e65 Make generated modules public
While public might be an overkill, until we successfully merge
`wiggle` with `wasi-common` (and others), I suggest we just make
the modules fully public and work from there.
2020-02-28 21:49:08 +01:00
Jakub Konka
5db335b7c7 Add EMPTY_FLAGS to flags generator
This seems like a useful primitive to have especially when dealing
with `Rights` flags in `wasi-common` (and WASI in general).
2020-02-28 21:41:17 +01:00
Pat Hickey
974f5617e0 depend on witx 0.8.3, rather than a path dependency. (#27)
and delete the submodule that was providing the path dep
2020-02-28 21:12:58 +01:00
Pat Hickey
bb6995ceaf make wiggle-generate ordinary lib, and wiggle the proc-macro lib
this allows us to reuse the code in wiggle-generate elsewhere, because
a proc-macro=true lib can only export a #[proc_macro] and not ordinary
functions.

In lucet, I will depend on wiggle-generate to define a proc macro that
glues wiggle to the specifics of the runtime.
2020-02-28 11:43:43 -08:00
Alex Crichton
0dbfad3aa7 Fix broken API doc links (#1020)
Also enable the lint to guarantee they stay un-broken.
2020-02-28 12:44:27 -06:00
Alex Crichton
5f1cba0b7f Hook up all crates via path dependencies 2020-02-28 09:16:05 -08:00