diff --git a/lib/clif-backend/src/code.rs b/lib/clif-backend/src/code.rs index 6425415bc..71412b7c2 100644 --- a/lib/clif-backend/src/code.rs +++ b/lib/clif-backend/src/code.rs @@ -25,7 +25,7 @@ use cranelift_wasm::{get_vmctx_value_label, translate_operator, TranslationState use cranelift_wasm::{FuncEnvironment, ReturnMode, WasmError, WasmResult}; use std::mem; use std::rc::Rc; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use wasmer_runtime_core::error::CompileError; use wasmer_runtime_core::{ backend::{Backend, CacheGen, Token}, @@ -80,7 +80,7 @@ impl ModuleCodeGenerator fn next_function( &mut self, - module_info: Arc, + module_info: Arc>, ) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> { // define_function_body( @@ -92,8 +92,8 @@ impl ModuleCodeGenerator let sig = generate_signature( self, self.get_func_type( - &module_info, - Converter(func_index.convert_up(&module_info)).into(), + &module_info.read().unwrap(), + Converter(func_index.convert_up(&module_info.read().unwrap())).into(), ), ); @@ -375,13 +375,13 @@ pub struct CraneliftFunctionCodeGenerator { func_translator: FuncTranslator, next_local: usize, pub clif_signatures: Map, - module_info: Arc, + module_info: Arc>, target_config: isa::TargetFrontendConfig, position: Position, } pub struct FunctionEnvironment { - module_info: Arc, + module_info: Arc>, target_config: isa::TargetFrontendConfig, clif_signatures: Map, } @@ -419,7 +419,9 @@ impl FuncEnvironment for FunctionEnvironment { let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let ptr_type = self.pointer_type(); - let local_global_addr = match global_index.local_or_import(&self.module_info) { + let local_global_addr = match global_index + .local_or_import(&self.module_info.read().unwrap()) + { LocalOrImport::Local(local_global_index) => { let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load { base: vmctx, @@ -489,49 +491,49 @@ impl FuncEnvironment for FunctionEnvironment { let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let ptr_type = self.pointer_type(); - let (local_memory_ptr_ptr, description) = match mem_index.local_or_import(&self.module_info) - { - LocalOrImport::Local(local_mem_index) => { - let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load { - base: vmctx, - offset: (vm::Ctx::offset_memories() as i32).into(), - global_type: ptr_type, - readonly: true, - }); - - let local_memory_ptr_offset = - local_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>(); - - ( - func.create_global_value(ir::GlobalValueData::IAddImm { - base: memories_base_addr, - offset: (local_memory_ptr_offset as i64).into(), + let (local_memory_ptr_ptr, description) = + match mem_index.local_or_import(&self.module_info.read().unwrap()) { + LocalOrImport::Local(local_mem_index) => { + let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load { + base: vmctx, + offset: (vm::Ctx::offset_memories() as i32).into(), global_type: ptr_type, - }), - self.module_info.memories[local_mem_index], - ) - } - LocalOrImport::Import(import_mem_index) => { - let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load { - base: vmctx, - offset: (vm::Ctx::offset_imported_memories() as i32).into(), - global_type: ptr_type, - readonly: true, - }); + readonly: true, + }); - let local_memory_ptr_offset = - import_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>(); + let local_memory_ptr_offset = + local_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>(); - ( - func.create_global_value(ir::GlobalValueData::IAddImm { - base: memories_base_addr, - offset: (local_memory_ptr_offset as i64).into(), + ( + func.create_global_value(ir::GlobalValueData::IAddImm { + base: memories_base_addr, + offset: (local_memory_ptr_offset as i64).into(), + global_type: ptr_type, + }), + self.module_info.read().unwrap().memories[local_mem_index], + ) + } + LocalOrImport::Import(import_mem_index) => { + let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load { + base: vmctx, + offset: (vm::Ctx::offset_imported_memories() as i32).into(), global_type: ptr_type, - }), - self.module_info.imported_memories[import_mem_index].1, - ) - } - }; + readonly: true, + }); + + let local_memory_ptr_offset = + import_mem_index.index() * mem::size_of::<*mut vm::LocalMemory>(); + + ( + func.create_global_value(ir::GlobalValueData::IAddImm { + base: memories_base_addr, + offset: (local_memory_ptr_offset as i64).into(), + global_type: ptr_type, + }), + self.module_info.read().unwrap().imported_memories[import_mem_index].1, + ) + } + }; let (local_memory_ptr, local_memory_base) = { let local_memory_ptr = func.create_global_value(ir::GlobalValueData::Load { @@ -599,7 +601,7 @@ impl FuncEnvironment for FunctionEnvironment { let ptr_type = self.pointer_type(); let (table_struct_ptr_ptr, description) = match table_index - .local_or_import(&self.module_info) + .local_or_import(&self.module_info.read().unwrap()) { LocalOrImport::Local(local_table_index) => { let tables_base = func.create_global_value(ir::GlobalValueData::Load { @@ -620,7 +622,7 @@ impl FuncEnvironment for FunctionEnvironment { ( table_struct_ptr_ptr, - self.module_info.tables[local_table_index], + self.module_info.read().unwrap().tables[local_table_index], ) } LocalOrImport::Import(import_table_index) => { @@ -642,7 +644,7 @@ impl FuncEnvironment for FunctionEnvironment { ( table_struct_ptr_ptr, - self.module_info.imported_tables[import_table_index].1, + self.module_info.read().unwrap().imported_tables[import_table_index].1, ) } }; @@ -829,7 +831,7 @@ impl FuncEnvironment for FunctionEnvironment { let callee_index: FuncIndex = Converter(clif_callee_index).into(); let ptr_type = self.pointer_type(); - match callee_index.local_or_import(&self.module_info) { + match callee_index.local_or_import(&self.module_info.read().unwrap()) { LocalOrImport::Local(local_function_index) => { // this is an internal function let vmctx = pos @@ -939,19 +941,19 @@ impl FuncEnvironment for FunctionEnvironment { let mem_index: MemoryIndex = Converter(clif_mem_index).into(); - let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info) - { - LocalOrImport::Local(local_mem_index) => ( - call_names::LOCAL_NAMESPACE, - local_mem_index.index(), - self.module_info.memories[local_mem_index], - ), - LocalOrImport::Import(import_mem_index) => ( - call_names::IMPORT_NAMESPACE, - import_mem_index.index(), - self.module_info.imported_memories[import_mem_index].1, - ), - }; + let (namespace, mem_index, description) = + match mem_index.local_or_import(&self.module_info.read().unwrap()) { + LocalOrImport::Local(local_mem_index) => ( + call_names::LOCAL_NAMESPACE, + local_mem_index.index(), + self.module_info.read().unwrap().memories[local_mem_index], + ), + LocalOrImport::Import(import_mem_index) => ( + call_names::IMPORT_NAMESPACE, + import_mem_index.index(), + self.module_info.read().unwrap().imported_memories[import_mem_index].1, + ), + }; let name_index = match description.memory_type() { MemoryType::Dynamic => call_names::DYNAMIC_MEM_GROW, @@ -1003,19 +1005,19 @@ impl FuncEnvironment for FunctionEnvironment { let mem_index: MemoryIndex = Converter(clif_mem_index).into(); - let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info) - { - LocalOrImport::Local(local_mem_index) => ( - call_names::LOCAL_NAMESPACE, - local_mem_index.index(), - self.module_info.memories[local_mem_index], - ), - LocalOrImport::Import(import_mem_index) => ( - call_names::IMPORT_NAMESPACE, - import_mem_index.index(), - self.module_info.imported_memories[import_mem_index].1, - ), - }; + let (namespace, mem_index, description) = + match mem_index.local_or_import(&self.module_info.read().unwrap()) { + LocalOrImport::Local(local_mem_index) => ( + call_names::LOCAL_NAMESPACE, + local_mem_index.index(), + self.module_info.read().unwrap().memories[local_mem_index], + ), + LocalOrImport::Import(import_mem_index) => ( + call_names::IMPORT_NAMESPACE, + import_mem_index.index(), + self.module_info.read().unwrap().imported_memories[import_mem_index].1, + ), + }; let name_index = match description.memory_type() { MemoryType::Dynamic => call_names::DYNAMIC_MEM_SIZE, @@ -1048,7 +1050,8 @@ impl FunctionEnvironment { &self, func_index: cranelift_wasm::FuncIndex, ) -> cranelift_wasm::SignatureIndex { - let sig_index: SigIndex = self.module_info.func_assoc[Converter(func_index).into()]; + let sig_index: SigIndex = + self.module_info.read().unwrap().func_assoc[Converter(func_index).into()]; Converter(sig_index).into() } diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index 5abfa70ca..1cdd10c2f 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -11,7 +11,7 @@ use smallvec::SmallVec; use std::fmt; use std::fmt::Debug; use std::marker::PhantomData; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use wasmparser::{self, WasmDecoder}; use wasmparser::{Operator, Type as WpType}; @@ -49,7 +49,7 @@ pub trait ModuleCodeGenerator, RM: RunnableModule, fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>; /// Creates a new function and returns the function-scope code generator for it. - fn next_function(&mut self, module_info: Arc) -> Result<&mut FCG, E>; + fn next_function(&mut self, module_info: Arc>) -> Result<&mut FCG, E>; fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box), E>; fn feed_signatures(&mut self, signatures: Map) -> Result<(), E>; @@ -162,14 +162,14 @@ impl< &compiler_config, )?; let (exec_context, cache_gen) = - mcg.finalize(&info) + mcg.finalize(&info.read().unwrap()) .map_err(|x| CompileError::InternalError { msg: format!("{:?}", x), })?; Ok(ModuleInner { cache_gen, runnable_module: Box::new(exec_context), - info: Arc::try_unwrap(info).unwrap(), + info: Arc::try_unwrap(info).unwrap().into_inner().unwrap(), }) } diff --git a/lib/runtime-core/src/parse.rs b/lib/runtime-core/src/parse.rs index 2ab4f33ae..dfa5c4eae 100644 --- a/lib/runtime-core/src/parse.rs +++ b/lib/runtime-core/src/parse.rs @@ -16,7 +16,7 @@ use crate::{ }; use hashbrown::HashMap; use std::fmt::Debug; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use wasmparser::{ BinaryReaderError, ExternalKind, FuncType, ImportSectionEntryType, Operator, Type as WpType, WasmDecoder, @@ -53,8 +53,8 @@ pub fn read_module< mcg: &mut MCG, middlewares: &mut MiddlewareChain, compiler_config: &CompilerConfig, -) -> Result, LoadError> { - let mut info = Arc::new(ModuleInfo { +) -> Result>, LoadError> { + let mut info = Arc::new(RwLock::new(ModuleInfo { memories: Map::new(), globals: Map::new(), tables: Map::new(), @@ -81,7 +81,7 @@ pub fn read_module< em_symbol_map: compiler_config.symbol_map.clone(), custom_sections: HashMap::new(), - }); + })); let mut parser = wasmparser::ValidatingParser::new( wasm, @@ -107,14 +107,14 @@ pub fn read_module< ParserState::EndWasm => break, ParserState::Error(err) => Err(LoadError::Parse(err))?, ParserState::TypeSectionEntry(ref ty) => { - Arc::get_mut(&mut info) + info.write() .unwrap() .signatures .push(func_type_to_func_sig(ty)?); } ParserState::ImportSectionEntry { module, field, ty } => { - let namespace_index = namespace_builder.as_mut().unwrap().register(module); - let name_index = name_builder.as_mut().unwrap().register(field); + let namespace_index = namespace_builder.as_mut().expect("116").register(module); + let name_index = name_builder.as_mut().expect("117").register(field); let import_name = ImportName { namespace_index, name_index, @@ -123,11 +123,8 @@ pub fn read_module< match ty { ImportSectionEntryType::Function(sigindex) => { let sigindex = SigIndex::new(sigindex as usize); - Arc::get_mut(&mut info) - .unwrap() - .imported_functions - .push(import_name); - Arc::get_mut(&mut info).unwrap().func_assoc.push(sigindex); + info.write().unwrap().imported_functions.push(import_name); + info.write().unwrap().func_assoc.push(sigindex); mcg.feed_import_function() .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; } @@ -139,7 +136,7 @@ pub fn read_module< maximum: table_ty.limits.maximum, }; - Arc::get_mut(&mut info) + info.write() .unwrap() .imported_tables .push((import_name, table_desc)); @@ -150,7 +147,7 @@ pub fn read_module< maximum: memory_ty.limits.maximum.map(|max| Pages(max)), shared: memory_ty.shared, }; - Arc::get_mut(&mut info) + info.write() .unwrap() .imported_memories .push((import_name, mem_desc)); @@ -160,7 +157,7 @@ pub fn read_module< mutable: global_ty.mutable, ty: wp_type_to_type(global_ty.content_type)?, }; - Arc::get_mut(&mut info) + info.write() .unwrap() .imported_globals .push((import_name, global_desc)); @@ -169,7 +166,7 @@ pub fn read_module< } ParserState::FunctionSectionEntry(sigindex) => { let sigindex = SigIndex::new(sigindex as usize); - Arc::get_mut(&mut info).unwrap().func_assoc.push(sigindex); + info.write().unwrap().func_assoc.push(sigindex); } ParserState::TableSectionEntry(table_ty) => { let table_desc = TableDescriptor { @@ -178,7 +175,7 @@ pub fn read_module< maximum: table_ty.limits.maximum, }; - Arc::get_mut(&mut info).unwrap().tables.push(table_desc); + info.write().unwrap().tables.push(table_desc); } ParserState::MemorySectionEntry(memory_ty) => { let mem_desc = MemoryDescriptor { @@ -187,7 +184,7 @@ pub fn read_module< shared: memory_ty.shared, }; - Arc::get_mut(&mut info).unwrap().memories.push(mem_desc); + info.write().unwrap().memories.push(mem_desc); } ParserState::ExportSectionEntry { field, kind, index } => { let export_index = match kind { @@ -197,28 +194,26 @@ pub fn read_module< ExternalKind::Global => ExportIndex::Global(GlobalIndex::new(index as usize)), }; - Arc::get_mut(&mut info) + info.write() .unwrap() .exports .insert(field.to_string(), export_index); } ParserState::StartSectionEntry(start_index) => { - Arc::get_mut(&mut info).unwrap().start_func = - Some(FuncIndex::new(start_index as usize)); + info.write().unwrap().start_func = Some(FuncIndex::new(start_index as usize)); } ParserState::BeginFunctionBody { .. } => { let id = func_count.wrapping_add(1); func_count = id; if func_count == 0 { - Arc::get_mut(&mut info).unwrap().namespace_table = - namespace_builder.take().unwrap().finish(); - Arc::get_mut(&mut info).unwrap().name_table = - name_builder.take().unwrap().finish(); - mcg.feed_signatures(info.signatures.clone()) + info.write().unwrap().namespace_table = + namespace_builder.take().expect("214").finish(); + info.write().unwrap().name_table = name_builder.take().expect("216").finish(); + mcg.feed_signatures(info.read().unwrap().signatures.clone()) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; - mcg.feed_function_signatures(info.func_assoc.clone()) + mcg.feed_function_signatures(info.read().unwrap().func_assoc.clone()) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; - mcg.check_precondition(&info) + mcg.check_precondition(&info.read().unwrap()) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; } @@ -229,19 +224,24 @@ pub fn read_module< .run( Some(fcg), Event::Internal(InternalEvent::FunctionBegin(id as u32)), - &info, + &info.read().unwrap(), ) .map_err(|x| LoadError::Codegen(x))?; - let sig = info + let info_read = info.read().unwrap(); + let sig = info_read .signatures .get( *info + .read() + .unwrap() .func_assoc - .get(FuncIndex::new(id as usize + info.imported_functions.len())) - .unwrap(), + .get(FuncIndex::new( + id as usize + info.read().unwrap().imported_functions.len(), + )) + .expect("242"), ) - .unwrap(); + .expect("244"); for ret in sig.returns() { fcg.feed_return(type_to_wp_type(*ret)) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; @@ -266,11 +266,11 @@ pub fn read_module< ParserState::CodeOperator(op) => { if !body_begun { body_begun = true; - fcg.begin_body(&info) + fcg.begin_body(&info.read().unwrap()) .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; } middlewares - .run(Some(fcg), Event::Wasm(op), &info) + .run(Some(fcg), Event::Wasm(op), &info.read().unwrap()) .map_err(|x| LoadError::Codegen(x))?; } ParserState::EndFunctionBody => break, @@ -281,7 +281,7 @@ pub fn read_module< .run( Some(fcg), Event::Internal(InternalEvent::FunctionEnd), - &info, + &info.read().unwrap(), ) .map_err(|x| LoadError::Codegen(x))?; fcg.finalize() @@ -317,14 +317,11 @@ pub fn read_module< let table_init = TableInitializer { table_index, - base: base.unwrap(), - elements: elements.unwrap(), + base: base.expect("320"), + elements: elements.expect("321"), }; - Arc::get_mut(&mut info) - .unwrap() - .elem_initializers - .push(table_init); + info.write().unwrap().elem_initializers.push(table_init); } ParserState::BeginActiveDataSectionEntry(memory_index) => { let memory_index = MemoryIndex::new(memory_index as usize); @@ -352,13 +349,10 @@ pub fn read_module< let data_init = DataInitializer { memory_index, - base: base.unwrap(), + base: base.expect("355"), data, }; - Arc::get_mut(&mut info) - .unwrap() - .data_initializers - .push(data_init); + info.write().unwrap().data_initializers.push(data_init); } ParserState::BeginGlobalSectionEntry(ty) => { let init = loop { @@ -379,7 +373,7 @@ pub fn read_module< let global_init = GlobalInit { desc, init }; - Arc::get_mut(&mut info).unwrap().globals.push(global_init); + info.write().unwrap().globals.push(global_init); } _ => {}