Commit Graph

479 Commits

Author SHA1 Message Date
Mark McCaskey
77c12e9ff1 Merge branch 'master' into feature/make-env-immutable 2020-11-20 14:40:20 -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
Mark McCaskey
84370c7930 Merge branch 'master' into feature/make-env-immutable 2020-11-18 15:40:11 -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
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
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
bors[bot]
962f3be626 Merge #1772
1772: Remove lifetime parameter from `NativeFunc` r=MarkMcCaskey a=MarkMcCaskey

Ran into this as an annoyance on #1739 ; will need to account for this when we fix the memory leak in `InstanceHandle`.

Note: the lifetime wasn't doing anything useful for us, this change doesn't make anything new possible (other than not having to deal with the lifetime parameter)

# Review

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


Co-authored-by: Mark McCaskey <mark@wasmer.io>
2020-10-27 22:13:57 +00:00
Mark McCaskey
608fbdca2a Remove lifetime parameter from NativeFunc
It wasn't doing anything anyways.
2020-10-27 14:37:24 -07:00
bors[bot]
58413f270c Merge #1770
1770: Enable these tests now that they pass. r=nlewycky a=nlewycky

Also fix a syntax error, use assert_eq! instead of assert! to compare two values for equality.

Also in passing, convert a comment about something not being done into a TODO.


Co-authored-by: Nick Lewycky <nick@wasmer.io>
2020-10-27 19:54:42 +00:00
Nick Lewycky
c9181e39c7 Enable these tests now that they pass.
Also fix a syntax error, use assert_eq! instead of assert! to compare two values for equality.

Also in passing, convert a comment about something not being done into a TODO.
2020-10-27 12:25:12 -07:00
Mark McCaskey
adc40aeae3 Merge branch 'master' into feature/add-table-example 2020-10-27 11:57:08 -07:00
jubianchi
2452b9a604 chore(doc): Add the Wasmer logo to the generated API documentation 2020-10-26 22:37:30 +01:00
jubianchi
120f0e2b5d doc(test): Add doctests on ExportError 2020-10-26 22:37:30 +01:00
jubianchi
8245f369d1 doc(examples): Add Memory examples 2020-10-26 22:37:29 +01:00
jubianchi
64bbfe6206 doc(examples): Add Function examples 2020-10-26 22:37:29 +01:00
jubianchi
8782c0f9ea doc(examples): Add Global examples 2020-10-26 22:37:29 +01:00
Mark McCaskey
67e53dd6d6 Clean up from feedback 2020-10-22 12:36:57 -07:00
Mark McCaskey
b124ea2032 Use a union for VMContext for functions
This makes the code more self documenting and correctly uses `unsafe`
to communicate that it's the user's responsibility to ensure that the
code paths that can lead to the access can only write the value they
expect to be there.
2020-10-22 11:46:42 -07:00
Mark McCaskey
729a044c75 Add misc updates from feedback 2020-10-21 16:34:27 -07:00
Nick Lewycky
29c9cb1471 Rename trampoline to call_trampoline, part 2. 2020-10-20 11:11:32 -07:00
Syrus Akbary
00c931051c Merge branch 'master' into feature/trampoline-in-artifact 2020-10-19 15:11:51 -07:00
Ivan Enderlin
afa5ab5e52 Merge branch 'master' into fix-c-api-trampoline 2020-10-19 09:22:13 +02:00
Nick Lewycky
57202f7522 Merge branch 'master' into feature/trampoline-in-artifact 2020-10-15 17:03:44 -07:00
Mark McCaskey
9fdbb0886c Split memory, table creation. Mark unsafe functions unsafe 2020-10-15 15:42:54 -07:00
Mark McCaskey
f3bcc36b59 Merge branch 'master' into fix/panic-in-memory-api 2020-10-15 10:11:43 -07:00
Mark McCaskey
de79a408ee Merge branch 'master' into feature/add-table-example 2020-10-14 16:31:25 -07:00
Mark McCaskey
49fe1734b3 Fix local table ownership issue 2020-10-14 16:12:35 -07:00
Mark McCaskey
4fc8ace118 Fix local memory ownership issue -- WIP
This commit contains a ton of debug code that we'll need to remove
2020-10-14 15:15:50 -07:00
nlewycky
ed630bf982 Merge branch 'master' into feature/trampoline-in-artifact 2020-10-14 01:02:11 -07:00
Syrus
4f0128f494 Merge branch 'master' of https://github.com/wasmerio/wasmer 2020-10-13 18:29:17 -07:00
Syrus
c97c70558d Fixed broken intra docs attrs 2020-10-13 18:27:29 -07:00
Mark McCaskey
e3ec9f34c1 Prevent panic when min > static bound and max is less than it 2020-10-13 14:39:25 -07:00
Nick Lewycky
c2646c1259 Permit exported functions to be host functions.
At the moment this only happens from lib/deprecated.
2020-10-12 11:17:45 -07:00
Nick Lewycky
d69617112a Merge branch 'master' into feature/trampoline-in-artifact 2020-10-09 10:43:57 -07:00
Nick Lewycky
cd494cb5cf Use Self instead of the full name of the structure.
https://rust-lang.github.io/rust-clippy/master/index.html#use_self
2020-10-08 20:37:53 -07:00
Nick Lewycky
9acf25d246 Use Self instead of the full name of the structure.
https://rust-lang.github.io/rust-clippy/master/index.html#use_self
2020-10-08 20:36:17 -07:00