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

boxed the wasmer_instance field of Instance to make it safe to reference #306

Closed

Conversation

reuvenpo
Copy link
Contributor

@reuvenpo reuvenpo commented Apr 26, 2020

This PR modifies the original draft PR to add boxing of the wasmer instance. This makes creating a pointer to it valid, as long as it is not shared between thread (Which isn't possible for the Instance anyways).

For now I opened this just as a demonstration. A lot of APIs have changed and i'm too tired to fix up all the conflicts with master at the moment.

Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking the time to look into this. This does look like a valid way to hold a reference.

In the meantime, we got pre-allocated fields working well with up to 128kB and minimal gas cost, which should cover any foreseeable use case. We also have great unit test coverage of all the context callbacks. (See cosmwasm_vm::imports). This uses a stub instance that holds context data, but we never execute the wasm code. Instead we set items in the wasm memory space and simulate a callback. This is much easier to test than the other integration code where we need to recompile the wasm testdata/contract_0.8.wasm to properly test it.

Unless you see a large benefit without interfering with the ease of unit testing, I would just put this on ice. I don't see the advantage of using this in CosmWasm. But it would be nice to add this link to the wasmer instance to demonstrate how it is possible. (Other people are also wondering, not just for memory allocation).

@@ -18,7 +18,6 @@ use cosmwasm_std::{
QuerierResponse, QueryRequest, Storage,
};

#[cfg(feature = "iterator")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imports under it are needed in your PR, but they are not available without this feature enabled. this caused a compilation fail when not using this feature.

@@ -121,7 +121,14 @@ where
gas_limit: u64,
) -> Self {
set_gas(&mut wasmer_instance, gas_limit);
let instance_ptr = NonNull::from(&mut wasmer_instance);

// The pointer shenanigans below are sound because:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a safe way to get a reference, but not tested with actually calling it.

Copy link
Contributor Author

@reuvenpo reuvenpo Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. still requires work, of course

@reuvenpo reuvenpo marked this pull request as draft April 27, 2020 09:55
@webmaster128
Copy link
Member

This is a super interesting learning, thank you @reuvenpo

When I'm done with a few other things, I'll integrate this into my PoC PR and update my PR to latest master.

Even if this is low priority, I'd like to build up knowledge about accessing the Wasmer instance from the Wasmer context, which we might need for other purposes as well.

we got pre-allocated fields working well with up to 128kB and minimal gas cost

This works for us just because the singlepass metering system charges the same amount of gas for the memory.grow operation, no matter how many pages you grow. Other metering systems like the one used by Near and Enigma charge per grown page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants