diff --git a/cranelift/wasm/src/environ/dummy.rs b/cranelift/wasm/src/environ/dummy.rs index 6c94116e04..cf8b997c84 100644 --- a/cranelift/wasm/src/environ/dummy.rs +++ b/cranelift/wasm/src/environ/dummy.rs @@ -128,6 +128,9 @@ pub struct DummyEnvironment { /// Instructs to collect debug data during translation. debug_info: bool, + /// Name of the module from the wasm file. + pub module_name: Option, + /// Function names. function_names: SecondaryMap, } @@ -141,6 +144,7 @@ impl DummyEnvironment { func_bytecode_sizes: Vec::new(), return_mode, debug_info, + module_name: None, function_names: SecondaryMap::new(), } } @@ -726,6 +730,11 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment { Ok(()) } + fn declare_module_name(&mut self, name: &'data str) -> WasmResult<()> { + self.module_name = Some(String::from(name)); + Ok(()) + } + fn declare_func_name(&mut self, func_index: FuncIndex, name: &'data str) -> WasmResult<()> { self.function_names[func_index] = String::from(name); Ok(()) diff --git a/cranelift/wasm/src/environ/spec.rs b/cranelift/wasm/src/environ/spec.rs index b719724b5e..6788599797 100644 --- a/cranelift/wasm/src/environ/spec.rs +++ b/cranelift/wasm/src/environ/spec.rs @@ -648,6 +648,14 @@ pub trait ModuleEnvironment<'data>: TargetEnvironment { data: &'data [u8], ) -> WasmResult<()>; + /// Declares the name of a module to the environment. + /// + /// By default this does nothing, but implementations can use this to read + /// the module name subsection of the custom name section if desired. + fn declare_module_name(&mut self, _name: &'data str) -> WasmResult<()> { + Ok(()) + } + /// Declares the name of a function to the environment. /// /// By default this does nothing, but implementations can use this to read diff --git a/cranelift/wasm/src/sections_translator.rs b/cranelift/wasm/src/sections_translator.rs index 847baf08cf..c80d1430f1 100644 --- a/cranelift/wasm/src/sections_translator.rs +++ b/cranelift/wasm/src/sections_translator.rs @@ -429,9 +429,13 @@ pub fn parse_name_section<'data>( environ.declare_func_name(index, name)?; } } - return Ok(()); } - wasmparser::Name::Local(_) | wasmparser::Name::Module(_) => {} + wasmparser::Name::Module(module) => { + if let Ok(name) = module.get_name() { + environ.declare_module_name(name)?; + } + } + wasmparser::Name::Local(_) => {} }; } Ok(()) diff --git a/crates/api/src/frame_info.rs b/crates/api/src/frame_info.rs index ce23490850..1797c77f5a 100644 --- a/crates/api/src/frame_info.rs +++ b/crates/api/src/frame_info.rs @@ -1,8 +1,8 @@ -use crate::module::Names; use std::collections::BTreeMap; use std::sync::{Arc, RwLock}; use wasmtime_environ::entity::EntityRef; use wasmtime_environ::wasm::FuncIndex; +use wasmtime_environ::Module; use wasmtime_jit::CompiledModule; lazy_static::lazy_static! { @@ -39,7 +39,7 @@ pub struct GlobalFrameInfoRegistration { struct ModuleFrameInfo { start: usize, functions: BTreeMap, - names: Arc, + module: Arc, } impl GlobalFrameInfo { @@ -49,11 +49,7 @@ impl GlobalFrameInfo { /// compiled functions within `module`. If the `module` has no functions /// then `None` will be returned. Otherwise the returned object, when /// dropped, will be used to unregister all name information from this map. - pub fn register( - &self, - names: &Arc, - module: &CompiledModule, - ) -> Option { + pub fn register(&self, module: &CompiledModule) -> Option { let mut min = usize::max_value(); let mut max = 0; let mut functions = BTreeMap::new(); @@ -92,7 +88,7 @@ impl GlobalFrameInfo { ModuleFrameInfo { start: min, functions, - names: names.clone(), + module: module.module().clone(), }, ); assert!(prev.is_none()); @@ -114,9 +110,9 @@ impl GlobalFrameInfo { return None; } Some(FrameInfo { - module_name: info.names.module_name.clone(), + module_name: info.module.name.clone(), func_index: func_index.index() as u32, - func_name: info.names.module.func_names.get(func_index).cloned(), + func_name: info.module.func_names.get(func_index).cloned(), }) } } diff --git a/crates/api/src/module.rs b/crates/api/src/module.rs index 627559b4cb..0338cdbb4b 100644 --- a/crates/api/src/module.rs +++ b/crates/api/src/module.rs @@ -7,10 +7,7 @@ use crate::types::{ use anyhow::{bail, Error, Result}; use std::path::Path; use std::sync::{Arc, Mutex}; -use wasmparser::{ - validate, CustomSectionKind, ExternalKind, ImportSectionEntryType, ModuleReader, Name, - SectionCode, -}; +use wasmparser::{validate, ExternalKind, ImportSectionEntryType, ModuleReader, SectionCode}; use wasmtime_jit::CompiledModule; fn into_memory_type(mt: wasmparser::MemoryType) -> Result { @@ -141,12 +138,6 @@ struct ModuleInner { exports: Box<[ExportType]>, compiled: CompiledModule, frame_info_registration: Mutex>>, - names: Arc, -} - -pub struct Names { - pub module: Arc, - pub module_name: Option, } impl Module { @@ -234,7 +225,7 @@ impl Module { pub fn new_with_name(store: &Store, bytes: impl AsRef<[u8]>, name: &str) -> Result { let mut module = Module::new(store, bytes.as_ref())?; let inner = Arc::get_mut(&mut module.inner).unwrap(); - Arc::get_mut(&mut inner.names).unwrap().module_name = Some(name.to_string()); + Arc::get_mut(inner.compiled.module_mut()).unwrap().name = Some(name.to_string()); Ok(module) } @@ -379,16 +370,11 @@ impl Module { store.engine().config().profiler.as_ref(), )?; - let names = Arc::new(Names { - module_name: None, - module: compiled.module().clone(), - }); Ok(Module { inner: Arc::new(ModuleInner { store: store.clone(), imports: Box::new([]), exports: Box::new([]), - names, compiled, frame_info_registration: Mutex::new(None), }), @@ -424,7 +410,7 @@ impl Module { /// # } /// ``` pub fn name(&self) -> Option<&str> { - self.inner.names.module_name.as_deref() + self.inner.compiled.module().name.as_deref() } /// Returns the list of imports that this [`Module`] has and must be @@ -644,23 +630,6 @@ impl Module { exports.push(ExportType::new(entry.field, r#type)); } } - SectionCode::Custom { - kind: CustomSectionKind::Name, - .. - } => { - // Read name section. Per spec, ignore invalid custom section. - if let Ok(mut reader) = section.get_name_section_reader() { - while let Ok(entry) = reader.read() { - if let Name::Module(name) = entry { - if let Ok(name) = name.get_name() { - Arc::get_mut(&mut inner.names).unwrap().module_name = - Some(name.to_string()); - } - break; - } - } - } - } SectionCode::Custom { name: "webidl-bindings", .. @@ -702,6 +671,6 @@ and for re-adding support for interface types you can see this issue: if info.is_some() { return; } - *info = Some(FRAME_INFO.register(&self.inner.names, &self.inner.compiled)); + *info = Some(FRAME_INFO.register(&self.inner.compiled)); } } diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index 46528c395f..3264d4664e 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -141,6 +141,9 @@ pub struct Module { /// A unique identifier (within this process) for this module. pub id: usize, + /// The name of this wasm module, often found in the wasm file. + pub name: Option, + /// Local information about a module which is the bare minimum necessary to /// translate a function body. This is derived as `Hash` whereas this module /// isn't, since it contains too much information needed to translate a @@ -222,6 +225,7 @@ impl Module { Self { id: NEXT_ID.fetch_add(1, SeqCst), + name: None, imported_funcs: PrimaryMap::new(), imported_tables: PrimaryMap::new(), imported_memories: PrimaryMap::new(), diff --git a/crates/environ/src/module_environ.rs b/crates/environ/src/module_environ.rs index 0fb7b13e74..12bc68bdf8 100644 --- a/crates/environ/src/module_environ.rs +++ b/crates/environ/src/module_environ.rs @@ -409,6 +409,11 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data Ok(()) } + fn declare_module_name(&mut self, name: &'data str) -> WasmResult<()> { + self.result.module.name = Some(name.to_string()); + Ok(()) + } + fn declare_func_name(&mut self, func_index: FuncIndex, name: &'data str) -> WasmResult<()> { self.result .module diff --git a/crates/jit/src/instantiate.rs b/crates/jit/src/instantiate.rs index b93eafa1dd..aa6d80438a 100644 --- a/crates/jit/src/instantiate.rs +++ b/crates/jit/src/instantiate.rs @@ -252,6 +252,11 @@ impl CompiledModule { &self.module } + /// Return a reference-counting pointer to a module. + pub fn module_mut(&mut self) -> &mut Arc { + &mut self.module + } + /// Return a reference to a module. pub fn module_ref(&self) -> &Module { &self.module