-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 missing imports in WASM module through a feature flag #2637
Conversation
Subscribe to Label Actioncc @peterhuene
This issue or pull request has been labeled: "wasmtime:api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
API-wise I personally feel like this would instead be a flag on For example one option to implement this externally today is that instead of One worry I'd have with this as-is as well is that it assumes all missing imports are missing function imports, but it could be a missing import of any type and it's not entirely clear what you'd do for the other types of missing imports (e.g. a missing imported table for example). |
Yes, it just allows functions to be missing, for other things it still has the behavior it had today, as I'm not sure what a "sane" alternative would be for missing non-functions. I like your alternative, I wasn't aware it's possible. do you have an example where those APIs are used that I could start from? |
I believe a function like this should work for your purposes: fn instantiate(linker: &Linker, module: &Module) -> Result<Instance> {
let mut imports = Vec::new();
for import in module.imports() {
match linker.get(&import) {
Some(value) => imports.push(value),
None => match import.ty() {
ExternType::Func(ty) => imports.push(
Func::new(linker.store(), ty, |_, _, _| {
Err(Trap::new("function not implemented"))
})
.into(),
),
_ => anyhow::bail!("import not defined"),
},
}
}
Instance::new(linker.store(), module, &imports)
} |
I do feel that having a Additionally, it'd be great to also add this as an option |
Yes, I can check how to add that to the CLI too. (and fix the name) |
@tschneidereit on the command line option. I've added a I've also added a boolean setting in the linker, but it's unclear to me where I should configure the linker to use that value from @alexcrichton: On the manual specification of missing methods, the code does look promising, I did translate it to |
I don't think that this needs to be in @AlexEne for the C API I think |
Yes, |
It seems that there isn't really an consensus on how to proceed with this, so I will close this PR for now, and will handle this in my mirror as my use-case can be solved by a patch of a couple of lines in the linker that makes this behavior default. Once optional imports are implemented this would not be needed anyway. |
I've added the possibility to allow missing imports in modules. This has been discussed here.
To re-iterate the need for this, there are use-cases where not all host programs VMs have been updated to the latest version that exports all functions required by modules that execute on these hosts.
In such instances it's more convenient to dynamically check a feature flag and call the native methods only if they exist vs emitting different binaries for each VM version.
For example, this allows your WASM program to use this type of pattern:
If the you call a missing import it will cause the VM issue a trap.
Once optional imports are standard and implemented, the escape hatch can be removed, in the meantime it's a really useful feature to have.
Maybe @tschneidereit can review this since we discussed it on zulip?