-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Move precompiled module detection into wasmtime #5342
Changes from 1 commit
675dded
910cbad
666988a
63fcb6f
3112eb8
8bf8518
1ac8554
b30937e
61136ba
c1ef3c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Previously, wasmtime-cli checked the module to be loaded is precompiled or not, by pre-opening the given file path to check if the "\x7FELF" header exists. This commit moves this branch into the `Module::from_trusted_file`, which is only invoked with `--allow-precompiled` flag on CLI. The initial motivation of the commit is, feeding a module to wasmtime from piped inputs, is blocked by the pre-opening of the module. The `Module::from_trusted_file`, assumes the --allow-precompiled flag so there is no piped inputs, happily mmap-ing the module to test if the header exists. If --allow-precompiled is not supplied, the existing `Module::from_file` will be used, without the additional header check as the precompiled modules are intentionally not allowed on piped inputs for security measures. One caveat of this approach is that the user may be confused if he or she tries to execute a precompiled module without --allow-precompiled, as wasmtime shows an 'input bytes aren't valid utf-8' error, not directly getting what's going wrong. So this commit includes a hack-ish workaround for this: If the error on `Module::new` ends with "input bytes aren't valid utf-8" strings, show a simple note to the standard error stream. This might be a small hiccup on preparing i18n or changing error format on the `wat` crate, but I feel comfortable (yet) this strategy because the `wat` crate includes tests for the error messages, so one would notice the breakage if the error message have changed. Thanks to @jameysharp for suggesting this idea with a detailed guidance.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,6 +241,10 @@ impl Module { | |
if #[cfg(feature = "wat")] { | ||
let mut e = e.downcast::<wat::Error>()?; | ||
e.set_path(file); | ||
if e.to_string().ends_with("input bytes aren't valid utf-8") { | ||
eprintln!("note: wasmtime might be trying to load a precompiled binary without --allow-compiled."); | ||
} | ||
|
||
bail!(e) | ||
} else { | ||
Err(e) | ||
|
@@ -332,8 +336,28 @@ impl Module { | |
} | ||
} | ||
|
||
/// Compiles a binary-encoded WebAssembly module to an artifact usable by | ||
/// Wasmtime. | ||
/// Creates a new WebAssembly `Module` from the contents of the given | ||
/// `file` on disk, but with assumptions that the file is 'sane'. | ||
/// In other words, the file should be a binary- or text-format WebAssembly | ||
/// module or a precompiled version of it generated by wasmtime. | ||
/// | ||
/// # Unsafety | ||
/// | ||
/// This function is marked as `unsafe` as it relies on [`Module::deserialize`]. | ||
cr0sh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg(compiler)] | ||
#[cfg_attr(nightlydoc, doc(cfg(feature = "cranelift")))] // see build.rs | ||
pub unsafe fn from_trusted_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> { | ||
let mmap = MmapVec::from_file(file.as_ref())?; | ||
if &mmap[0..4] == b"\x7fELF" { | ||
return SerializedModule::from_mmap(mmap, &engine.config().module_version)? | ||
.into_module(engine); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, things changed underneath you (in #5153), so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I forgot to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done and I found a small typo near load_code so fixed it too ;) |
||
} | ||
|
||
Module::new(engine, &*mmap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It needs a type implementing |
||
} | ||
|
||
/// Converts an input binary-encoded WebAssembly module to compilation | ||
/// artifacts and type information. | ||
/// | ||
/// This is where compilation actually happens of WebAssembly modules and | ||
/// translation/parsing/validation of the binary input occurs. The binary | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant
--allow-precompiled
here:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the wasmtime library should print to stderr. There is no way for a binary to suppress this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, wasmtime is a library so eprintln should be avoided if possible. How about deferring this eprintln to the wasmtime-cli? This would make wasmtime-cli add
wat
dependency to downcast error.Also the
#[cfg(feature = "wat")]
branch condition cannot be used on wasmtime-cli, so this eprintln would be fired unconditionally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just suggested a different way to report this error, so let's delete this
eprintln
in favor of that alternative.