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

Linker Feature - The Lost Commit #123

Merged
merged 1 commit into from
Jan 25, 2025
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
4 changes: 2 additions & 2 deletions src/execution/lut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Lut {
})
.collect::<Option<Vec<_>>>()?;

// TODO: what do we want to do if there is a missing import/export pair? Currently we fail the entire
// operation. Should it be a RuntimeError if said missing pair is called?
// If there is a missing import/export pair, we fail the entire
// operation. Better safe than sorry...

function_lut.push(module_lut);
}
Expand Down
3 changes: 2 additions & 1 deletion src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ where
let (module_idx, func_idx) =
self.get_indicies(&function_ref.module_name, &function_ref.function_name)?;

// TODO: figure out errors :)
if module_idx != function_ref.module_index {
return Err(RuntimeError::FunctionNotFound);
return Err(RuntimeError::ModuleNotFound);
}
if func_idx != function_ref.function_index {
return Err(RuntimeError::FunctionNotFound);
Expand Down
18 changes: 7 additions & 11 deletions src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ pub fn validate(wasm: &[u8]) -> Result<ValidationInfo> {
})?
.unwrap_or_default();

let imported_functions = imports
.iter()
.filter_map(|import| match &import.desc {
ImportDesc::Func(type_idx) => Some(*type_idx),
_ => None,
})
.collect::<Vec<_>>();
let imported_functions = imports.iter().filter_map(|import| match &import.desc {
ImportDesc::Func(type_idx) => Some(*type_idx),
_ => None,
});

let all_functions = imported_functions
.iter()
.chain(local_functions.iter())
.cloned()
.clone()
.chain(local_functions.iter().cloned())
.collect::<Vec<TypeIdx>>();

while (skip_section(&mut wasm, &mut header)?).is_some() {}
Expand Down Expand Up @@ -180,7 +176,7 @@ pub fn validate(wasm: &[u8]) -> Result<ValidationInfo> {
h,
&types,
&all_functions,
imported_functions.len(),
imported_functions.count(),
&globals,
&memories,
&data_count,
Expand Down
25 changes: 25 additions & 0 deletions tests/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const SIMPLE_IMPORT_ADDON: &str = r#"
)
)"#;

const CYCLICAL_IMPORT: &str = r#"
(module
(import "base" "get_three" (func $get_three (param) (result i32)))
(export "get_three" (func $get_three))
)"#;

const CALL_INDIRECT_BASE: &str = r#"
(module
(import "env" "get_one" (func $get_one (param) (result i32)))
Expand Down Expand Up @@ -123,3 +129,22 @@ pub fn run_call_indirect() {
let run = instance.get_function_by_name("base", "run").unwrap();
assert_eq!((1, 3), instance.invoke(&run, ()).unwrap());
}

#[test_log::test]
pub fn run_cyclical() {
let wasm_bytes = wat::parse_str(SIMPLE_IMPORT_BASE).unwrap();
let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance =
RuntimeInstance::new_named("base", &validation_info).expect("instantiation failed");

let wasm_bytes = wat::parse_str(CYCLICAL_IMPORT).unwrap();
let validation_info = validate(&wasm_bytes).expect("validation failed");
instance
.add_module("env", &validation_info)
.expect("Successful instantiation");

let run = instance.get_function_by_name("base", "get_three").unwrap();
// Unmet import since we can't have cyclical imports
// Currently, this passes since we don't allow chained imports.
assert!(instance.invoke::<(), i32>(&run, ()).unwrap_err() == wasm::RuntimeError::UnmetImport);
}
Loading