Skip to content

Commit

Permalink
Update wast to v219 (#1270)
Browse files Browse the repository at this point in the history
* update wast to v219

* update dependencies

* apply rustfmt

* fix clippy warnings
  • Loading branch information
Robbepop authored Oct 31, 2024
1 parent afbaaa4 commit ee13b5c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 107 deletions.
48 changes: 13 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/wasmi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ arrayvec = { version = "0.7.4", default-features = false }
[dev-dependencies]
wat = "1"
assert_matches = "1.5"
wast = "70.0.2"
wast = "219.0.1"
anyhow = "1.0"
criterion = { version = "0.5", default-features = false }

Expand Down
21 changes: 5 additions & 16 deletions crates/wasmi/tests/spec/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ impl<'a> TestContext<'a> {
}

impl TestContext<'_> {
/// Returns the file path of the associated `.wast` test file.
fn test_path(&self) -> &str {
self.descriptor.path()
}

/// Returns the [`TestDescriptor`] of the test context.
pub fn spanned(&self, span: Span) -> TestSpan {
self.descriptor.spanned(span)
Expand Down Expand Up @@ -162,19 +157,13 @@ impl TestContext<'_> {
/// If creating the [`Module`] fails.
pub fn compile_and_instantiate(
&mut self,
mut module: wast::core::Module,
id: Option<wast::token::Id>,
wasm: &[u8],
) -> Result<Instance, TestError> {
let module_name = module.id.map(|id| id.name());
let wasm = module.encode().unwrap_or_else(|error| {
panic!(
"encountered unexpected failure to encode `.wast` module into `.wasm`:{}: {}",
self.test_path(),
error
)
});
let module_name = id.map(|id| id.name());
let module = match self.runner_config.mode {
ParsingMode::Buffered => Module::new(self.engine(), &wasm[..])?,
ParsingMode::Streaming => Module::new_streaming(self.engine(), &wasm[..])?,
ParsingMode::Buffered => Module::new(self.engine(), wasm)?,
ParsingMode::Streaming => Module::new_streaming(self.engine(), wasm)?,
};
let instance_pre = self.linker.instantiate(&mut self.store, &module)?;
let instance = instance_pre.start(&mut self.store)?;
Expand Down
10 changes: 5 additions & 5 deletions crates/wasmi/tests/spec/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub struct TestProfile {
directives: usize,
/// The amount of executed [`WasmDirective::Module`].
module: usize,
/// The amount of executed [`WasmDirective::QuoteModule`].
quote_module: usize,
/// The amount of executed [`WasmDirective::ModuleDefinition`].
module_definition: usize,
/// The amount of executed [`WasmDirective::AssertMalformed`].
assert_malformed: usize,
/// The amount of executed [`WasmDirective::AssertInvalid`].
Expand Down Expand Up @@ -38,9 +38,9 @@ impl TestProfile {
self.module += 1;
}

/// Bumps the amount of [`WasmDirective::QuoteModule`] directives.
pub fn bump_quote_module(&mut self) {
self.quote_module += 1;
/// Bumps the amount of [`WasmDirective::ModuleDefinition`] directives.
pub fn bump_module_definition(&mut self) {
self.module_definition += 1;
}

/// Bumps the amount of [`WasmDirective::AssertMalformed`] directives.
Expand Down
119 changes: 69 additions & 50 deletions crates/wasmi/tests/spec/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use wasmi::{Config, ExternRef, FuncRef, Instance, Val};
use wasmi_core::{F32, F64};
use wast::{
core::{HeapType, NanPattern, WastRetCore},
core::{AbstractHeapType, HeapType, NanPattern, WastRetCore},
lexer::Lexer,
parser::ParseBuffer,
token::Span,
Expand Down Expand Up @@ -74,44 +74,49 @@ pub fn run_wasm_spec_test(name: &'static str, file: &'static str, config: Runner
}

fn execute_directives(wast: Wast, test_context: &mut TestContext) -> Result<()> {
'outer: for directive in wast.directives {
let span = directive.span();
for directive in wast.directives {
test_context.profile().bump_directives();
match directive {
WastDirective::Wat(QuoteWat::Wat(Wat::Module(module))) => {
module_compilation_succeeds(test_context, span, module);
test_context.profile().bump_module();
WastDirective::ModuleDefinition(
mut module @ (QuoteWat::Wat(wast::Wat::Module(_)) | QuoteWat::QuoteModule { .. }),
) => {
let wasm = module.encode().unwrap();
let span = module.span();
module_compilation_succeeds(test_context, span, None, &wasm);
test_context.profile().bump_module_definition();
}
WastDirective::Wat(_) => {
test_context.profile().bump_quote_module();
// For the purpose of testing Wasmi we are not
// interested in parsing `.wat` files, therefore
// we silently ignore this case for now.
// This might change once wasmi supports `.wat` files.
continue 'outer;
WastDirective::Module(
mut module @ (QuoteWat::Wat(wast::Wat::Module(_)) | QuoteWat::QuoteModule { .. }),
) => {
let wasm = module.encode().unwrap();
let span = module.span();
let id = module.name();
module_compilation_succeeds(test_context, span, id, &wasm);
test_context.profile().bump_module();
}
WastDirective::AssertMalformed {
span,
module: QuoteWat::Wat(Wat::Module(module)),
module: mut module @ QuoteWat::Wat(wast::Wat::Module(_)),
message,
} => {
test_context.profile().bump_assert_malformed();
module_compilation_fails(test_context, span, module, message);
let id = module.name();
let wasm = module.encode().unwrap();
module_compilation_fails(test_context, span, id, &wasm, message);
}
WastDirective::AssertMalformed { .. } => {
test_context.profile().bump_assert_malformed();
}
WastDirective::AssertInvalid {
span,
module,
module:
mut module @ (QuoteWat::Wat(wast::Wat::Module(_)) | QuoteWat::QuoteModule { .. }),
message,
} => {
test_context.profile().bump_assert_invalid();
let module = match extract_module(module) {
Some(module) => module,
None => continue 'outer,
};
module_compilation_fails(test_context, span, module, message);
let id = module.name();
let wasm = module.encode().unwrap();
module_compilation_fails(test_context, span, id, &wasm, message);
}
WastDirective::Register { span, name, module } => {
test_context.profile().bump_register();
Expand Down Expand Up @@ -190,11 +195,13 @@ fn execute_directives(wast: Wast, test_context: &mut TestContext) -> Result<()>
}
WastDirective::AssertUnlinkable {
span,
module: Wat::Module(module),
module: Wat::Module(mut module),
message,
} => {
let id = module.id;
let wasm = module.encode().unwrap();
test_context.profile().bump_assert_unlinkable();
module_compilation_fails(test_context, span, module, message);
module_compilation_fails(test_context, span, id, &wasm, message);
}
WastDirective::AssertUnlinkable { .. } => {
test_context.profile().bump_assert_unlinkable();
Expand Down Expand Up @@ -285,10 +292,22 @@ fn assert_results(context: &TestContext, span: Span, results: &[Val], expected:
);
}
},
(Val::FuncRef(funcref), WastRetCore::RefNull(Some(HeapType::Func))) => {
(
Val::FuncRef(funcref),
WastRetCore::RefNull(Some(HeapType::Abstract {
ty: AbstractHeapType::Func,
..
})),
) => {
assert!(funcref.is_null());
}
(Val::ExternRef(externref), WastRetCore::RefNull(Some(HeapType::Extern))) => {
(
Val::ExternRef(externref),
WastRetCore::RefNull(Some(HeapType::Abstract {
ty: AbstractHeapType::Extern,
..
})),
) => {
assert!(externref.is_null());
}
(Val::ExternRef(externref), WastRetCore::RefExtern(Some(expected))) => {
Expand All @@ -312,28 +331,13 @@ fn assert_results(context: &TestContext, span: Span, results: &[Val], expected:
}
}

fn extract_module(quote_wat: QuoteWat) -> Option<wast::core::Module> {
match quote_wat {
QuoteWat::Wat(Wat::Module(module)) => Some(module),
QuoteWat::Wat(Wat::Component(_))
| QuoteWat::QuoteModule(_, _)
| QuoteWat::QuoteComponent(_, _) => {
// We currently do not allow parsing `.wat` Wasm modules in `v1`
// therefore checks based on malformed `.wat` modules are uninteresting
// to us at the moment.
// This might become interesting once `v1` starts support parsing `.wat`
// Wasm modules.
None
}
}
}

fn module_compilation_succeeds(
context: &mut TestContext,
span: Span,
module: wast::core::Module,
id: Option<wast::token::Id>,
wasm: &[u8],
) -> Instance {
match context.compile_and_instantiate(module) {
match context.compile_and_instantiate(id, wasm) {
Ok(instance) => instance,
Err(error) => panic!(
"{}: failed to instantiate module but should have succeeded: {}",
Expand All @@ -346,10 +350,11 @@ fn module_compilation_succeeds(
fn module_compilation_fails(
context: &mut TestContext,
span: Span,
module: wast::core::Module,
id: Option<wast::token::Id>,
wasm: &[u8],
expected_message: &str,
) {
let result = context.compile_and_instantiate(module);
let result = context.compile_and_instantiate(id, wasm);
assert!(
result.is_err(),
"{}: succeeded to instantiate module but should have failed with: {}",
Expand All @@ -367,14 +372,22 @@ fn execute_wast_execute(
WastExecute::Invoke(invoke) => {
execute_wast_invoke(context, span, invoke).map_err(Into::into)
}
WastExecute::Wat(Wat::Module(module)) => {
context.compile_and_instantiate(module).map(|_| Vec::new())
WastExecute::Wat(Wat::Module(mut module)) => {
let id = module.id;
let wasm = module.encode().unwrap();
context
.compile_and_instantiate(id, &wasm)
.map(|_| Vec::new())
}
WastExecute::Wat(Wat::Component(_)) => {
// Wasmi currently does not support the Wasm component model.
Ok(vec![])
}
WastExecute::Get { module, global } => context
WastExecute::Get {
module,
global,
span: _,
} => context
.get_global(module, global)
.map(|result| vec![result]),
}
Expand Down Expand Up @@ -415,8 +428,14 @@ fn value(ctx: &mut wasmi::Store<()>, value: &wast::core::WastArgCore) -> Option<
wast::core::WastArgCore::I64(arg) => Val::I64(*arg),
wast::core::WastArgCore::F32(arg) => Val::F32(F32::from_bits(arg.bits)),
wast::core::WastArgCore::F64(arg) => Val::F64(F64::from_bits(arg.bits)),
wast::core::WastArgCore::RefNull(HeapType::Func) => Val::FuncRef(FuncRef::null()),
wast::core::WastArgCore::RefNull(HeapType::Extern) => Val::ExternRef(ExternRef::null()),
wast::core::WastArgCore::RefNull(HeapType::Abstract {
ty: AbstractHeapType::Func,
..
}) => Val::FuncRef(FuncRef::null()),
wast::core::WastArgCore::RefNull(HeapType::Abstract {
ty: AbstractHeapType::Extern,
..
}) => Val::ExternRef(ExternRef::null()),
wast::core::WastArgCore::RefExtern(value) => Val::ExternRef(ExternRef::new(ctx, *value)),
_ => return None,
})
Expand Down

0 comments on commit ee13b5c

Please sign in to comment.