Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve usabiliy of WasmerEnv trait #2838

Closed
gterzian opened this issue Apr 7, 2022 · 3 comments · Fixed by #2892
Closed

Improve usabiliy of WasmerEnv trait #2838

gterzian opened this issue Apr 7, 2022 · 3 comments · Fixed by #2892
Labels
🎉 enhancement New feature! priority-medium Medium priority issue
Milestone

Comments

@gterzian
Copy link

gterzian commented Apr 7, 2022

Motivation

init_with_instance provides the host env with a &Instance, where Instance is Clone, making it very tempting for people to simply clone the instance and store it on the host env for later use, which seems to create a memory leak.

Proposed solution

init_with_instance could take an argument that would enforce the implicit constraints of the method.

For example a &InitialiazedInstanceProxy, which would not be Clone, and would have a method like get_export_with_generics_weak, wich would proxy internally to instance.exports.get_with_generics_weak.

Alternative

Clarify this issue in the documentation. Good idea anyway, since people can still easily store an Instance on their host env outside of init_with_instance.

Additional context

The temptation to store Instance on the host env is especially tempting in the light of using wasmer_middlewares::metering, since helper functions like get_remaining_points take an &Instance as argument, and it's not obvious how to get access to it from host functions unless stored on the env.

Example of mistake made in the wild and fix: massalabs/massa-sc-runtime#121

@gterzian gterzian added the 🎉 enhancement New feature! label Apr 7, 2022
@heyjdp
Copy link

heyjdp commented Apr 27, 2022

We are rethinking the API for Wasmer 3.0, this issue should be fixed by those changes. Thanks for the ticket!

@heyjdp heyjdp added this to the v3.0 milestone Apr 27, 2022
@heyjdp heyjdp added the priority-medium Medium priority issue label Apr 27, 2022
@syrusakbary
Copy link
Member

This one should be fixed by #2924

@syrusakbary
Copy link
Member

Closing this in favor of #2924

bors bot added a commit that referenced this issue Jul 19, 2022
2892: Implement new Context API for Wasmer 3.0 r=epilys a=Amanieu

### Overview

This PR reworks the way Wasmer manages objects used by instances, specifically: `Instance`, `Memory`, `Table`, `Function`, `Global` and `ExternRef`. Previously these were tracked using reference counting, but this cannot work fully because Wasm code can manipulate references to functions (and by implication the instance that function is from) using tables. 

The new approach uses a `Context` type which owns all of the objects listed above: objects are only freed when the `Context` is dropped. The `Instance`, `Memory`, `Table`, `Function`, `Global` and `ExternRef` types are now simply wrappers around an integer which indexes a vector in the `Context`. As a result, any function on those "handle" types now need to take a `&Context` or `&mut Context` as a parameter to access the actual data in the context. This also vastly simplifies Wasmer's thread-safety story: since all mutating operations require a `&mut Context`, no synchronization or internal locks are needed.

Additionally, the way host functions are represented is also changed. The `WasmerEnv` trait is removed since it was unergonomic and hard to use correctly. Instead, the state for host functions is stored directly as the `T` in `Context<T>`. This state is initialized when the context is created, and can be accessed through the `data` and `data_mut` methods on `Context<T>`. All host functions receive a `ContextMut<'_, T>` as their first argument, which gives them access to the host state of the context. This is much better than the old approach since it provides mutable access without locks and allows all host functions to share the same state.

This design is heavily inspired by [this RFC](https://github.com/bytecodealliance/rfcs/blob/main/accepted/new-api.md) from Wasmtime.

### TODO

- [x] #2985
- [x] #2908
- [x] #2973 Upgrade `c-api` to use the new API
- [x] Update wasmer-wasi to use the new API
- [x] #2909
- [x] #2913
- [x] #2912 
- #2911  (Non blocking)
- [ ] #2910

Fixes #2893
Fixes #2873 
Fixes #2866
Fixes #1840 
Fixes #1734
Fixes #1632
Fixes #1522
Fixes #1630 
Fixes #2838
Fixes #2856 
Fixes #2544
Fixes #2816 

Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
Co-authored-by: Syrus Akbary <me@syrusakbary.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature! priority-medium Medium priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants