Commit Graph

820 Commits

Author SHA1 Message Date
Mark McCaskey
8669e92ed4 Move EngineExport to engine 2020-11-25 16:44:21 -08:00
Mark McCaskey
40eec3fbd6 Address feedback: misc clean ups 2020-11-25 14:19:29 -08:00
Mark McCaskey
7013163829 WIP attempt to make an Export wrapper type
I can't figure out how we can ever use the non-wrapped type though
2020-11-25 10:59:04 -08:00
Mark McCaskey
82fc0080ba Allow closures with non-native host functions 2020-11-24 13:15:38 -08:00
Mark McCaskey
29d50a55ca Disable closures as host functions for now + docs + tests 2020-11-24 12:44:13 -08:00
Ivan Enderlin
d5c7b628be Merge branch 'master' into fix-vm-leak 2020-11-23 17:37:21 +01:00
Ivan Enderlin
d7bcc211c2 big patch: add some traces. 2020-11-23 17:33:22 +01:00
Ivan Enderlin
e77afea383 big patch: Start injecting InstanceAllocator everywhere. 2020-11-23 17:25:09 +01:00
Mark McCaskey
de6cb9c4c4 Merge branch 'master' into feature/host-env-prototype 2020-11-20 15:54:35 -08:00
Mark McCaskey
77c12e9ff1 Merge branch 'master' into feature/make-env-immutable 2020-11-20 14:40:20 -08:00
losfair
e7dd725c6d Add module info transformation method to ModuleMiddleware. 2020-11-20 14:34:46 -08:00
Mark McCaskey
eeed1b9124 Rename LegacyEnv to UnsafeMutableEnv 2020-11-20 14:21:54 -08:00
Mark McCaskey
c5aec5dd9a Clean up typos in comments 2020-11-20 14:02:32 -08:00
Mark McCaskey
a95c44583c Add hack to get deprecated API to work 2020-11-19 17:43:33 -08:00
bors[bot]
875ac93355 Merge #1826
1826: feat(api) Faster `Exports::from_iter` r=Hywan a=Hywan

# Description

Instead of creating a new `Exports` with `Exports::new` and inserting
all pair `(String, Extern)` one after the other with
`Exports::insert`, this patch updates the code to create an `Exports`
directly with its `map` field built with `IndexMap::from_iter`, so
that we avoid calling `Arc::get_mut(…).unwrap().insert(…)` for every
pair.

# Review

- ~[ ] Add a short description of the the change to the CHANGELOG.md file~ (not sure it's necessary)


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2020-11-19 23:13:37 +00:00
bors[bot]
43b3d4b0cd Merge #1822
1822: fix(vm) Fix memory leak of `InstanceHandle` r=syrusakbary a=Hywan

# Description

In `wasmer_engine::Artifact::instantiate`, the engine artifact
allocates a new instance by calling
`wasmer_vm::InstanceHandle::allocate_instance`. To make it short, this
function calculates the [`Layout`] of an `wasmer_vm::Instance`, and
allocates space for it by calling [`alloc`]. This last part is
important because it means we are responsible to deallocate it with
[`dealloc`].

The pointer to this `wasmer_vm::Instance` is stored in the
`wasmer_vm::InstanceHandle` structure. That's the handle that is
returned by the engine artifact `Artifact::instantiate` method.

This instance handle is then stored in `wasmer::Instance` (through the
intervention of `wasmer::Module`).

How is it freed? It wasn't. That's the leak. [`dealloc`] was never
called.

How to free it? We must call [`dealloc`]. There is even a
`wasmer_vm::InstanceHandle::deallocate` helper to do that
properly. Neat!

When to free it? That's the tricky part. A `wasmer::Instance` can be
clonable. To do so, `wasmer_vm::InstanceHandle` must be clonable
too. There was a `Clone` implementation, that was constructing a new
`wasmer_vm:InstanceHandle` by using the same pointer to
`wasmer_vm::Instance`. That's annoying because it's the best way to
get many pointers that point to the same `wasmer_vm::Instance` in the
nature, and it's difficult to track them.

This patch changes the paradigm. There is only one and unique
`wasmer_vm::InstanceHandle` per `wasmer::Instance`, including its
clones. The handle is now stored inside a
`Arc<Mutex<wasmer_vm::InstanceHandle>>`. Consequently, when a
`wasmer::Instance` is cloned, it uses the same
`wasmer_vm::InstanceHandle`, not a clone of it.

Bonus: `wasmer::Instance` continues to be `Send` + `Sync`.

So. Let's back to our question. When to free
`wasmer_vm::InstanceHandle`? Response: When `wasmer::Instance` is
dropped. Right? There is a unique path from `wasmer::Instance`, to
`wasmer_vm::InstanceHandle`, to `wasmer_vm::Instance` now. So we just
need to call `wasmer_vm::InstanceHandle::dealloc` in a specific `Drop`
implementation for `wasmer_vm::InstanceHandle`, and the Rust borrow
checker does the rest.

Yes. … No. There is another use case: It is possible to create a
`wasmer_vm::InstanceHandle` with `InstanceHandle::from_vmctx`. Indeed,
a `wasmer_vm::VMContext` also stores a pointer to
`wasmer_vm::Instance`. In this, we consider `wasmer_vm::VMContext`
owns the instance pointer, somehow, and is responsible to free it
properly.

Consequently, we need another flag inside `wasmer_vm::InstanceHandle`
to know whether this structure owns the pointer to
`wasmer_vm::Instance` or not.

So. Let's back to our question. When to free
`wasmer_vm::InstanceHandle`? Response: Inside the `Drop`
implementation of `wasmer_vm::InstanceHandle` with its `Self::dealloc`
method if and only if the handle owns the pointer to
`wasmer_vm::Instance`.

Testing with Valgrind shows that the leak has been removed.

[`Layout`]: https://doc.rust-lang.org/std/alloc/struct.Layout.html
[`alloc`]: https://doc.rust-lang.org/std/alloc/fn.alloc.html
[`dealloc`]: https://doc.rust-lang.org/std/alloc/fn.dealloc.html

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2020-11-19 18:14:04 +00:00
Ivan Enderlin
297eb952ab feat(api) Faster Exports::from_iter.
Instead of creating a new `Exports` with `Exports::new` and inserting
all pair `(String, Extern)` one after the other with
`Exports::insert`, this patch updates the code to create an `Exports`
directly with its `map` field built with `IndexMap::from_iter`, so
that we avoid calling `Arc::get_mut(…).unwrap().insert(…)` for every
pair.
2020-11-19 11:41:19 +01:00
Ivan Enderlin
ca89bd5d75 feat(api) Rename Extern::from_export to …::from_vm_export.
I hope this little change will clarify a little bit that the `Export`
passed to `Extern::from_vm_export` is not a `wasmer::Export` but a
`wasmer_vm::Export`.
2020-11-19 11:06:18 +01:00
Mark McCaskey
84370c7930 Merge branch 'master' into feature/make-env-immutable 2020-11-18 15:40:11 -08:00
Mark McCaskey
0e03544163 Merge branch 'feature/host-env-prototype' of github.com:wasmerio/wasmer into feature/host-env-prototype 2020-11-18 14:24:26 -08:00
Mark McCaskey
85169f6996 Add misc clean ups and corrections 2020-11-18 14:23:40 -08:00
Mark McCaskey
2065556c41 Merge branch 'master' into feature/host-env-prototype 2020-11-18 13:49:38 -08:00
Mark McCaskey
15e88a1195 Clean up error message in proc macro, improve docs on WasmerEnv 2020-11-18 12:12:24 -08:00
Mark McCaskey
7131caa8d9 Implement WasmerEnv for dyn Any 2020-11-18 11:31:57 -08:00
Mark McCaskey
3580f165e8 Rename WasmerEnv::finish to WasmerEnv::init_with_instance 2020-11-17 17:14:11 -08:00
Mark McCaskey
6aab77e503 Add default impl for WasmerEnv::finish remove macro 2020-11-17 16:25:07 -08:00
Mark McCaskey
2b0464a4b2 Port wasmer-emscripten to use WasmerEnv 2020-11-17 16:19:00 -08:00
Mark McCaskey
38b296e36f Move WasmerEnv into its own mod, implement it for stdlib types 2020-11-17 15:30:21 -08:00
Mark McCaskey
655ac093a2 Fix up helpers generated by WasmerEnv; add unchecked variant 2020-11-17 11:19:42 -08:00
Ivan Enderlin
8016a34b4c fix(vm) Fix memory leak of InstanceHandle.
In `wasmer_engine::Artifact::instantiate`, the engine artifact
allocates a new instance by calling
`wasmer_vm::InstanceHandle::allocate_instance`. To make it short, this
function calculates the [`Layout`] of an `wasmer_vm::Instance`, and
allocates space for it by calling [`alloc`]. This last part is
important because it means we are responsible to deallocate it with
[`dealloc`].

The pointer to this `wasmer_vm::Instance` is stored in the
`wasmer_vm::InstanceHandle` structure. That's the handle that is
returned by the engine artifact `Artifact::instantiate` method.

This instance handle is then stored in `wasmer::Instance` (through the
intervention of `wasmer::Module`).

How is it freed? It wasn't. That's the leak. [`dealloc`] was never
called.

How to free it? We must call [`dealloc`]. There is even a
`wasmer_vm::InstanceHandle::deallocate` helper to do that
properly. Neat!

When to free it? That's the tricky part. A `wasmer::Instance` can be
clonable. To do so, `wasmer_vm::InstanceHandle` must be clonable
too. There was a `Clone` implementation, that was constructing a new
`wasmer_vm:InstanceHandle` by using the same pointer to
`wasmer_vm::Instance`. That's annoying because it's the best way to
get many pointers that point to the same `wasmer_vm::Instance` in the
nature, and it's difficult to track them.

This patch changes the paradigm. There is only one and unique
`wasmer_vm::InstanceHandle` per `wasmer::Instance`, including its
clones. The handle is now stored inside a
`Arc<Mutex<wasmer_vm::InstanceHandle>>`. Consequently, when a
`wasmer::Instance` is cloned, it uses the same
`wasmer_vm::InstanceHandle`, not a clone of it.

Bonus: `wasmer::Instance` continues to be `Send` + `Sync`.

So. Let's back to our question. When to free
`wasmer_vm::InstanceHandle`? Response: When `wasmer::Instance` is
dropped. Right? There is a unique path from `wasmer::Instance`, to
`wasmer_vm::InstanceHandle`, to `wasmer_vm::Instance` now. So we just
need to call `wasmer_vm::InstanceHandle::dealloc` in a specific `Drop`
implementation for `wasmer_vm::InstanceHandle`, and the Rust borrow
checker does the rest.

Yes. … No. There is another use case: It is possible to create a
`wasmer_vm::InstanceHandle` with `InstanceHandle::from_vmctx`. Indeed,
a `wasmer_vm::VMContext` also stores a pointer to
`wasmer_vm::Instance`. In this, we consider `wasmer_vm::VMContext`
owns the instance pointer, somehow, and is responsible to free it
properly.

Consequently, we need another flag inside `wasmer_vm::InstanceHandle`
to know whether this structure owns the pointer to
`wasmer_vm::Instance` or not.

So. Let's back to our question. When to free
`wasmer_vm::InstanceHandle`? Response: Inside the `Drop`
implementation of `wasmer_vm::InstanceHandle` with its `Self::dealloc`
method if and only if the handle owns the pointer to
`wasmer_vm::Instance`.

Testing with Valgrind shows that the leak has been removed.

[`Layout`]: https://doc.rust-lang.org/std/alloc/struct.Layout.html
[`alloc`]: https://doc.rust-lang.org/std/alloc/fn.alloc.html
[`dealloc`]: https://doc.rust-lang.org/std/alloc/fn.dealloc.html
2020-11-17 15:58:47 +01:00
Ivan Enderlin
012a24b8fd test(api) Add more tests for the .to_native API. 2020-11-17 10:48:48 +01:00
Mark McCaskey
75707106d9 Add test initializing and calling host function manually 2020-11-16 17:04:08 -08:00
Mark McCaskey
a19705ae11 Remove free method, call finalizers in C API 2020-11-16 16:36:58 -08:00
Mark McCaskey
2690e5e8f6 Merge branch 'master' into feature/host-env-prototype 2020-11-16 15:13:29 -08:00
Nick Lewycky
49b0a4a16a Prepare for 1.0.0-alpha5 release. 2020-11-06 11:50:07 -08:00
bors[bot]
a5281eaab8 Merge #1753
1753: Use a union for VMContext for functions r=MarkMcCaskey a=MarkMcCaskey

This makes the code more self documenting and correctly uses `unsafe`
to communicate that it's the caller's responsibility to ensure that the
code paths that can lead to the access can only write the value they
expect to be there.

spun off of #1739


- [x] There's still some inconsistent use of `vmctx` and `extra_data` might be good to unify this more.
- [x] Improve docs on the new type


Co-authored-by: Mark McCaskey <mark@wasmer.io>
2020-10-31 00:48:30 +00:00
Mark McCaskey
e3dfcf8b6f Rename VMFunctionExtraData to VMFunctionEnvironment 2020-10-30 17:31:52 -07:00
Nick Lewycky
f0aa9de495 Document soundness rules for the slice returned by data_unchecked{,_mut}. 2020-10-30 12:29:39 -07:00
Syrus
2387ec07b6 Upgrade dependencies 2020-10-29 22:59:48 -07:00
Syrus
9bd2c47730 Upgraded Cranelift to 0.67 2020-10-29 20:19:16 -07:00
Mark McCaskey
1c9fc1f0d4 Fix up doc tests in api 2020-10-29 15:45:23 -07:00
Mark McCaskey
a25288a0ae Improve error messages on WasmerEnv macro, clean up code, add tests 2020-10-29 15:09:24 -07:00
Mark McCaskey
78f958b52c Clean up misc code 2020-10-29 13:07:04 -07:00
Mark McCaskey
83008a7771 Fix up error handling code, get tests passing 2020-10-29 11:13:51 -07:00
Mark McCaskey
be3ab95688 Add error handling to host init logic 2020-10-28 17:39:37 -07:00
Mark McCaskey
8b87526d9d Create single interface to get exports from Instance.exports
This adds a method that takes generic arguments as well so that
NativeFunc<Args, Rets> can also be `got` even though it has generic
parameters
2020-10-28 13:32:27 -07:00
Simon Warta
1102d4c508 Revert code change and add explaining comment instead 2020-10-28 19:41:52 +01:00
Simon Warta
ff0cdb1260 Test Tunables::memory_style; fix static bound 2020-10-28 16:21:46 +01:00
bors[bot]
eec90e7940 Merge #1730
1730: Create example for limiting memory with Tunables r=syrusakbary a=webmaster128

Closes #1588

# Description

This is an attempt for solving #1360 with the new Tunables API. It could be developed further to address #1588 if you like.

I'd appreciate a code review to ensure I'm on the right track.

Open questions:
1. Is it expected that I had to add `wasmer-vm`?
2. Should I really create a new `Target` for this use case when engine already has one set?
3. I used `BaseTunables` for the base implementation and sticked with `Tunables` for the trait name, because it makes most sense to me. However, in `lib/api/src/tunables.rs` it is done the other way round. Any thoughts on the naming issue?
4. The import collision between `wamer::Memory` and `wasmer_vm::Memory` is inconvenient. Can it be avoided or do I do it correctly here?


# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Simon Warta <simon@warta.it>
2020-10-28 01:32:57 +00:00
Simon Warta
984d478d05 Update example to latest Tunables trait 2020-10-28 00:30:13 +01:00