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

Implement the allow-unknown-exports option for the run command. #2879

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion crates/wasmtime/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Linker {
strings: Vec<Rc<str>>,
map: HashMap<ImportKey, Extern>,
allow_shadowing: bool,
allow_unknown_exports: bool,
}

#[derive(Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -69,6 +70,7 @@ impl Linker {
string2idx: HashMap::new(),
strings: Vec::new(),
allow_shadowing: false,
allow_unknown_exports: false,
}
}

Expand Down Expand Up @@ -102,6 +104,32 @@ impl Linker {
self
}

/// Configures whether this [`Linker`] will allow unknown exports from
/// command modules.
///
/// By default a [`Linker`] will error when unknown exports are encountered
/// in a command module while using [`Linker::module`].
///
/// This method can be used to allow unknown exports from command modules.
///
/// # Examples
///
/// ```
/// # use wasmtime::*;
/// # fn main() -> anyhow::Result<()> {
/// # let store = Store::default();
/// # let module = Module::new(store.engine(), "(module)")?;
/// let mut linker = Linker::new(&store);
/// linker.allow_unknown_exports(true);
/// linker.module("mod", &module)?;
/// # Ok(())
/// # }
/// ```
pub fn allow_unknown_exports(&mut self, allow: bool) -> &mut Linker {
self.allow_unknown_exports = allow;
self
}

/// Defines a new item in this [`Linker`].
///
/// This method will add a new definition, by name, to this instance of
Expand Down Expand Up @@ -469,7 +497,7 @@ impl Linker {
// Allow an exported "__rtti_base" memory for compatibility with
// AssemblyScript.
warn!("command module exporting '__rtti_base' is deprecated; pass `--runtime half` to the AssemblyScript compiler");
} else {
} else if !self.allow_unknown_exports {
bail!("command export '{}' is not a function", export.name());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct RunCommand {
#[structopt(flatten)]
common: CommonOptions,

/// Allow unknown exports when running commands.
#[structopt(long = "allow-unknown-exports")]
allow_unknown_exports: bool,

/// Grant access to the given host directory
#[structopt(long = "dir", number_of_values = 1, value_name = "DIRECTORY")]
dirs: Vec<String>,
Expand Down Expand Up @@ -146,6 +150,8 @@ impl RunCommand {
let argv = self.compute_argv();

let mut linker = Linker::new(&store);
linker.allow_unknown_exports(self.allow_unknown_exports);

populate_with_wasi(
&mut linker,
preopen_dirs,
Expand Down
18 changes: 18 additions & 0 deletions tests/all/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ fn module_interposition() -> Result<()> {
Ok(())
}

#[test]
fn allow_unknown_exports() -> Result<()> {
let store = Store::default();
let mut linker = Linker::new(&store);
let module = Module::new(
store.engine(),
r#"(module (func (export "_start")) (global (export "g") i32 (i32.const 0)))"#,
)?;

assert!(linker.module("module", &module).is_err());

let mut linker = Linker::new(&store);
linker.allow_unknown_exports(true);
linker.module("module", &module)?;

Ok(())
}

#[test]
fn no_leak() -> Result<()> {
struct DropMe(Rc<Cell<bool>>);
Expand Down