From 60e4c6522803629d885e137b3a7721494bfaa921 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 14:32:04 +0100 Subject: [PATCH] Frontend: reject 64-bit memories and tables and shared memories. Waffle doesn't handle these properly, and produces invalid output in some cases, and panics in others. The memory section, memory imports, table section, and table imports now return `FrontendError::UnsupportedFeature` for all of these. --- src/frontend.rs | 56 ++++++++++++++++++++++------------- tests/unsupported_features.rs | 32 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 tests/unsupported_features.rs diff --git a/src/frontend.rs b/src/frontend.rs index d308d09..2960add 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -147,6 +147,37 @@ fn parse_element_items(items: wasmparser::ElementItems) -> Result> { }) } +/// Converts a memory type to a `MemoryData` with no segments. Returns +/// `FrontendError::UnsupportedFeature` for a 64-bit or shared memory. +fn memory_data(ty: &wasmparser::MemoryType) -> Result { + if ty.memory64 { + bail!(FrontendError::UnsupportedFeature("64-bit memory".into())); + } + if ty.shared { + bail!(FrontendError::UnsupportedFeature("shared memory".into())); + } + Ok(MemoryData { + initial_pages: ty.initial as usize, + maximum_pages: ty.maximum.map(|max| max as usize), + segments: vec![], + }) +} + +/// Converts a table type to a `TableData` with no elements. Returns +/// `FrontendError::UnsupportedFeature` for a 64-bit table. +fn table_data(ty: &wasmparser::TableType) -> Result { + if ty.table64 { + bail!(FrontendError::UnsupportedFeature("64-bit table".into())); + } + Ok(TableData { + ty: ty.element_type.into(), + initial: ty.initial, + max: ty.maximum, + initializer: None, + func_elements: Some(vec![]), + }) +} + #[derive(Default)] struct ExtraSections<'a> { #[cfg(feature = "dwarf")] @@ -243,21 +274,11 @@ fn handle_payload<'a>( ImportKind::Global(global) } TypeRef::Table(ty) => { - let table = module.tables.push(TableData { - ty: ty.element_type.into(), - initial: ty.initial, - max: ty.maximum, - initializer: None, - func_elements: Some(vec![]), - }); + let table = module.tables.push(table_data(&ty)?); ImportKind::Table(table) } TypeRef::Memory(mem) => { - let mem = module.memories.push(MemoryData { - initial_pages: mem.initial as usize, - maximum_pages: mem.maximum.map(|max| max as usize), - segments: vec![], - }); + let mem = module.memories.push(memory_data(&mem)?); ImportKind::Memory(mem) } t => { @@ -313,6 +334,7 @@ fn handle_payload<'a>( Payload::TableSection(reader) => { for table in reader { let table = table?; + let data = table_data(&table.ty)?; let initializer = table_initializer(&table.init)?; let func_elements = match initializer { Some(func) if func.is_valid() => { @@ -327,11 +349,9 @@ fn handle_payload<'a>( _ => vec![], }; module.tables.push(TableData { - ty: table.ty.element_type.into(), - initial: table.ty.initial, - max: table.ty.maximum, initializer, func_elements: Some(func_elements), + ..data }); } } @@ -375,11 +395,7 @@ fn handle_payload<'a>( Payload::MemorySection(reader) => { for memory in reader { let memory = memory?; - module.memories.push(MemoryData { - initial_pages: memory.initial as usize, - maximum_pages: memory.maximum.map(|max| max as usize), - segments: vec![], - }); + module.memories.push(memory_data(&memory)?); } } Payload::DataCountSection { .. } => { diff --git a/tests/unsupported_features.rs b/tests/unsupported_features.rs new file mode 100644 index 0000000..675aa06 --- /dev/null +++ b/tests/unsupported_features.rs @@ -0,0 +1,32 @@ +//! Tests that the frontend rejects module features that the IR cannot +//! represent. + +use waffle::{FrontendError, FrontendOptions, Module}; + +fn assert_unsupported(wat: &str) { + let bytes = wat::parse_str(wat).unwrap(); + let err = Module::from_wasm_bytes(&bytes, &FrontendOptions::default()) + .err() + .unwrap_or_else(|| panic!("module was accepted: {}", wat)); + assert!( + matches!( + err.downcast_ref::(), + Some(FrontendError::UnsupportedFeature(_)) + ), + "unexpected error for {}: {:?}", + wat, + err + ); +} + +#[test] +fn memory64_and_shared_memories_are_unsupported() { + assert_unsupported("(module (memory i64 1))"); + assert_unsupported("(module (import \"\" \"\" (memory 1 1 shared)))"); +} + +#[test] +fn table64_is_unsupported() { + assert_unsupported("(module (table i64 1 funcref))"); + assert_unsupported("(module (import \"\" \"\" (table i64 1 funcref)))"); +}