-
Notifications
You must be signed in to change notification settings - Fork 830
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
Allow configuration of default memory #1360
Comments
A clean solution I could imagine would be to modify The backends would then add support to use the |
Looking more, I am not sure that is where Memories are created, but rather in the VM Instance...
|
I think there are two ways of solving this:
However, there are a few considerations to make before jumping into an implementation:
|
@syrusakbary Thanks for your response and I agree this should not be rushed. I think I don't understand Wasm internals and memory well enough to contribute to those specific questions. Could you maybe help by explaining where the first, unlimited |
So, when no maximum memory is specified By default, this will mean that the memory is dynamically allocated (as is less expensive for creation). However, dynamically allocated memory pushes more effort into the compiler since it have to do bounds-checking each time it does an operation in memory. Having said that, in the wasm side if the memory maximum is not provided, we can easily decide what to do with it (meaning: we can easily allow a custom maximum, if not set in the wasm module). This is the ideal scenario since the implementation would be quite simple. However, it's important to be aware that this solution will not work if the user set's a different custom maximum in the memory module. |
Thanks!
In this case "the user" is the person who wrote the Wasm bytecode? Since we don't trust this entity (at least for browser and blockchain use cases), I think we need a way to detect and reject that Wasm. |
Assuming this is deterministic amount of memory used, I agree that we should fail with a WasmerRuntimeError or such when the contract tries to exceed the limit. This is a requirement for all blockchain projects I believe. |
If the wasm contract has a If that sounds right, I think we can get into an easy solution :) |
Yes. At least in our case we are able to define the rule: memory maximum must be undefined or below X. Non-compliant contracts can be rejected. Then the host sets the maximum. |
I think the idea proposed by @syrusakbary is the correct one:
I think the best way to address (1) is to use Possible connection to the following PR: #1299. |
At Near we use |
Thanks for the input @nearmax So you check and rewrite the memory request of the contract before sending it to wasmer. This seems quite solid. It would be nice to allow this to be done with a config option or a middleware, but a working solution is great. |
Thank you @nearmax, very helpful to understand the flow! If possible, I'd like to find a solution that avoids manipulating and re-serializing the orginial contract. However, this is just a personal feeling with no specific reason. The more I learn about memory management in Wasm, the more I give up the idea of a simple max memory setting on the VM. I guess some kind of callback allowing to inject custom commands like this one or a middleware solution would be nice. |
After some iteration of this, we got into a nice way of solving the custom memory generation with a very clean API. |
This should be very easily solved by the refactor, that is now in Closing the issue |
Sounds good. Can you please link to the relevant code for those of us who do not know the details? (Ideally a testcase to see this in action) |
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 <[email protected]>
For those who find this thread: We now have an example that shows how to limit memory consumption by adjusting the contract's imported memory's maximum via the new Tunables API (see also #1730 and #1775). The exports required to copy |
Motivation
I'd like to be able to limit the memory consumption of a Wasm sandbox. It seems like
MemoryDescriptor
can do that. However, I don't see a way to configure the default memory (memory(0)
), which is created for me automatically.Proposed solution
After digging into the code, I found a way to patch the memory limit into Wasmer (0.16.2) like this:
The loop iterates over one key only. For some visibility reasons I was not able to construct
LocalMemoryIndex(0)
directly.This patch changes the default memory from
to
and in my tests this new maximum seems to be respected.
The problem with this is primarily, that
compiler.compile
is explicitely designed to be crate internal and I don't see an API that allows for setting memory limits.Alternatives
I don't know if this is the correct approach to set memory limits.
Additional context
The text was updated successfully, but these errors were encountered: