Skip to content

Privatize storage handling #141

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

Merged
merged 3 commits into from
Feb 3, 2020
Merged

Privatize storage handling #141

merged 3 commits into from
Feb 3, 2020

Conversation

webmaster128
Copy link
Member

@webmaster128 webmaster128 commented Jan 31, 2020

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.

One comment. Will review more monday

pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A) {
(instance.wasmer_instance, instance.api)
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A, Option<S>) {
let storage = take_storage(instance.wasmer_instance.context());
Copy link
Member

Choose a reason for hiding this comment

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

What about making this Option.

If storage is none we can just drop api. It is a simple, clone able struct, so little is lost

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand what you mean. The storage variable is of type Option<S> already due to the return type of take_storage from context.rs.

I was even thinking in the opposite direction: why is this optional? How can this ever be unavailable?

Copy link
Member

Choose a reason for hiding this comment

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

2 points. Seems my message was eaten by the github

  1. I would make the second result Option < Extern > (seems the rust generic syntax got eaten as an html tag)
  2. If we now privatize all external ways of having no Storage set (take_storage), and can reason through all internal cases to prove to ourselves this will never happen (like during with_storage), then we can just return Extern instead of Option<Extern> and panic if not present (only if we prove to devs, not compiler, this will never happen).

I still stand with my point of returning Extern not Storage there

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. makes sense, implemented.

  2. I found a case where the instance has no storage anymore:

    #[test]
    fn has_no_storage_when_nested() {
        // this should fail with the assertion, but not cause a double-free crash (issue #59)
        let instance = mock_instance(&CONTRACT_0_7);
        instance.with_storage(|storage1| {
            instance.with_storage(|storage2| {
                assert!(false, "storage missing, so this must not be executed");
            });
        });
    }

But for the purpose of this PR we can leave it as is I think.

@webmaster128 webmaster128 force-pushed the privatize_storage-handling branch from 20db69e to 6781f3c Compare February 1, 2020 08:38
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.

Looks good, two points on recycle return value - to make it Extern struct (to keep return val from growing), and then (optionally) to make it non-Option if you want to prove it will always be set.

}

/// Takes ownership of instance and decomposes it into its components.
/// The components we want to preserve are returned, the rest is dropped.
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A) {
(instance.wasmer_instance, instance.api)
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A, Option<S>) {
Copy link
Member

Choose a reason for hiding this comment

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

Very nice, yes, consume Self

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, Self the type, not the reference. I tried to do the same as an instance method but that does not work trivially.

Copy link
Member

Choose a reason for hiding this comment

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

This is good. It means it "consumes" the object, so no one can use it afterwards.

I think this means the destructor is called at the end, so any objects owned by instance at the end of the method are freed

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.

Thanks for changes

pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A, Option<S>) {
let storage = take_storage(instance.wasmer_instance.context());
(instance.wasmer_instance, instance.api, storage)
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, Option<Extern<S, A>>) {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the changes. I know this just moved the same code blob down one level, but I prefer this to keep the public api simpler.

let storage = take_storage(instance.wasmer_instance.context());
(instance.wasmer_instance, instance.api, storage)
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, Option<Extern<S, A>>) {
let ext = if let Some(storage) = take_storage(instance.wasmer_instance.context()) {
Copy link
Member

Choose a reason for hiding this comment

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

Note: this is totally valid. You can also use a match statement here. I'm not sure which is "better" here. I like let when there is one option, and match for 3 or more. For 2, personal choice

@ethanfrey ethanfrey merged commit 99e5737 into 0.7 Feb 3, 2020
@ethanfrey ethanfrey deleted the privatize_storage-handling branch February 3, 2020 08:43
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.

2 participants