Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Conversation

@cecton
Copy link
Contributor

@cecton cecton commented Jan 7, 2020

This is part of the implementation for #4456, only for wasmi

(Let me squash merge so I can put a proper commit message)

@cecton cecton added J0-enhancement An additional feature request. Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be. labels Jan 7, 2020
@cecton cecton requested review from bkchr, jimpo and pepyakin January 7, 2020 10:29
@cecton cecton self-assigned this Jan 7, 2020
@parity-cla-bot
Copy link

It looks like @cecton signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

.map_err(wasmi::Trap::from)
.map(|v| v.map(Into::into))
} else if self.enable_stub {
panic!("function {} does not exist", self.method);
Copy link
Member

Choose a reason for hiding this comment

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

There can be more than 1 function that does not exist.
And please return an Err

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I return an Err it doesn't panic anymore and therefore it breaks my test. Does the test should panic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (now it unwrap() and catch the panic)

.map(|v| v.map(Into::into))
} else if self.enable_stub {
Err(Error::from(
format!("functions do not exist: {}", self.missing_functions.join(", "))
Copy link
Member

Choose a reason for hiding this comment

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

Please only print the actual function that was tried to be called (by using the unique index generated in resolve).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I don't print all the not existing functions, why am I gathering them in a Vec? This implementation here was printing the function that was tried: e19948e

Copy link
Member

Choose a reason for hiding this comment

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

Because you only want to know which function exactly was called. It does not helps us to know that 50 functions are not provided ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so after discussion it finally 💡 in my mind. It's the name of the missing export we need, not the name of the function in the module. Will fix 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.

done

})?;
call_in_wasm_module(ext, &self.instance, method, data, &self.host_functions)
call_in_wasm_module(
ext, &self.instance, method, data, &self.host_functions, self.enable_stub,
Copy link
Member

Choose a reason for hiding this comment

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

Please put each parameter it its own line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

call_in_wasm_module(ext, &self.instance, method, data, &self.host_functions)
call_in_wasm_module(
ext, &self.instance, method, data, &self.host_functions, self.enable_stub,
&self.missing_functions)
Copy link
Member

Choose a reason for hiding this comment

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

) needs to go to a new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/// The host functions registered for this instance.
host_functions: Vec<&'static dyn Function>,
/// Enable STUB for function called that are missing
enable_stub: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of enable_stub I'd like to suggest to use something like allow_missing_imports. The rationale is that in the wasm spec a missing import is specified an instantiation error, so allow_missing_imports would convey that this is a custom mode. enable_stub is on contrary a little bit vague since there might be different kinds of stubs out there.

Aside, expanding the role of this field in the doc would be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fine by me. @bkchr any remark?

Copy link
Member

Choose a reason for hiding this comment

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

I like @pepyakin idea :) Sounds much better!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@cecton cecton force-pushed the cecton-issue-4456 branch from a9caa1f to 79f4a2a Compare January 8, 2020 09:00
ext,
code,
heap_pages,
false,
Copy link
Member

Choose a reason for hiding this comment

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

Won't the other tests fails because of missing imports? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done (but I think wasmtime is failing at instantiation because of the missing function. I will fix that)

&test_code[..],
8,
);
assert!(output.is_err());
Copy link
Member

Choose a reason for hiding this comment

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

Please check for the exact error you expect. Otherwise it could happen that the error type returned changes and the test still finishes successfully.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done but I removed the test entirely as we test only with stub enabled

if self.enable_stub {
trace!("Could not find function {}, a stub will be provided instead.", name);
let id = self.missing_function_id.borrow().clone();
self.missing_functions.borrow_mut().insert(id, name.to_string());
Copy link
Member

Choose a reason for hiding this comment

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

We actually just require missing_functions to be a Vec.

The index for the function will be computed as:

self.host_functions.len() + self.missing_functions.len()

(before adding the new missing function name!)

And in invoke we do the following

if index >= host_functions.len() {
     Err("{} not found whatever", self.missing_functions[index - host_functions.len())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's what I implemented before as you can see here: 2333994

But I told you this looks way too clever and I should probably use a hashmap and you said you would have use a hashmap.

I don't mind reverting it but I'm not in favor of this because I find that it doesn't look like it brings performance improvement, maybe memory, but it certainly make the code harder to understand.

Now that I shared my humble opinion. What do you prefer? 😁

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I wanted to use a hashmap because I did not thought about this solution 😅
It clearly will improve the performance, as we just need to access the memory by index and not need to look into a hashmap. Please revert, but also make sure that invoke does not tries to use an invalid index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@bkchr bkchr added A5-grumble and removed J0-enhancement An additional feature request. Z6-mentor An easy task where a mentor is available. Please indicate in the issue who the mentor could be. labels Jan 8, 2020
@cecton
Copy link
Contributor Author

cecton commented Jan 8, 2020

This is ready to be merged. I don't know why that test failed but it doesn't seem related to my changes.

}
}

struct MissingExternalFunction;
Copy link
Member

Choose a reason for hiding this comment

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

You only need one of these structs. Just put the name into the struct as parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

cecton and others added 2 commits January 8, 2020 17:34
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Last round and than we are good to go.


let mut fec = FunctionExecutor::new(memory.clone(), heap_base, table, host_functions)?;
let mut fec = FunctionExecutor::new(
memory.clone(), heap_base, table, host_functions, allow_missing_imports, missing_functions)?;
Copy link
Member

Choose a reason for hiding this comment

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

Please put every parameter on its own line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

// Instantiate this module.
let instance = instantiate_module(heap_pages as usize, &module, &host_functions)
let (instance, missing_functions) = instantiate_module(
heap_pages as usize, &module, &host_functions, allow_missing_imports)
Copy link
Member

Choose a reason for hiding this comment

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

Each parameter on its own line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

cecton and others added 16 commits January 9, 2020 09:52
@cecton cecton merged commit fc6b7e8 into master Jan 9, 2020
@cecton cecton deleted the cecton-issue-4456 branch January 9, 2020 10:18
@pepyakin pepyakin removed the request for review from jimpo January 9, 2020 18:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants