Update to Arc<RwLock<ModuleInfo>> for interior mutability

This commit is contained in:
Brandon Fish
2019-05-25 19:30:44 -05:00
parent d440776bc0
commit 5da0c4766a
3 changed files with 126 additions and 129 deletions

View File

@ -25,7 +25,7 @@ use cranelift_wasm::{get_vmctx_value_label, translate_operator, TranslationState
use cranelift_wasm::{FuncEnvironment, ReturnMode, WasmError, WasmResult}; use cranelift_wasm::{FuncEnvironment, ReturnMode, WasmError, WasmResult};
use std::mem; use std::mem;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::{Arc, RwLock};
use wasmer_runtime_core::error::CompileError; use wasmer_runtime_core::error::CompileError;
use wasmer_runtime_core::{ use wasmer_runtime_core::{
backend::{Backend, CacheGen, Token}, backend::{Backend, CacheGen, Token},
@ -80,7 +80,7 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
fn next_function( fn next_function(
&mut self, &mut self,
module_info: Arc<ModuleInfo>, module_info: Arc<RwLock<ModuleInfo>>,
) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> { ) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
// define_function_body( // define_function_body(
@ -92,8 +92,8 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
let sig = generate_signature( let sig = generate_signature(
self, self,
self.get_func_type( self.get_func_type(
&module_info, &module_info.read().unwrap(),
Converter(func_index.convert_up(&module_info)).into(), Converter(func_index.convert_up(&module_info.read().unwrap())).into(),
), ),
); );
@ -375,13 +375,13 @@ pub struct CraneliftFunctionCodeGenerator {
func_translator: FuncTranslator, func_translator: FuncTranslator,
next_local: usize, next_local: usize,
pub clif_signatures: Map<SigIndex, ir::Signature>, pub clif_signatures: Map<SigIndex, ir::Signature>,
module_info: Arc<ModuleInfo>, module_info: Arc<RwLock<ModuleInfo>>,
target_config: isa::TargetFrontendConfig, target_config: isa::TargetFrontendConfig,
position: Position, position: Position,
} }
pub struct FunctionEnvironment { pub struct FunctionEnvironment {
module_info: Arc<ModuleInfo>, module_info: Arc<RwLock<ModuleInfo>>,
target_config: isa::TargetFrontendConfig, target_config: isa::TargetFrontendConfig,
clif_signatures: Map<SigIndex, ir::Signature>, clif_signatures: Map<SigIndex, ir::Signature>,
} }
@ -419,7 +419,9 @@ impl FuncEnvironment for FunctionEnvironment {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_type = self.pointer_type(); 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) => { LocalOrImport::Local(local_global_index) => {
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load { let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
@ -489,8 +491,8 @@ impl FuncEnvironment for FunctionEnvironment {
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext); let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
let ptr_type = self.pointer_type(); let ptr_type = self.pointer_type();
let (local_memory_ptr_ptr, description) = match mem_index.local_or_import(&self.module_info) let (local_memory_ptr_ptr, description) =
{ match mem_index.local_or_import(&self.module_info.read().unwrap()) {
LocalOrImport::Local(local_mem_index) => { LocalOrImport::Local(local_mem_index) => {
let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load { let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load {
base: vmctx, base: vmctx,
@ -508,7 +510,7 @@ impl FuncEnvironment for FunctionEnvironment {
offset: (local_memory_ptr_offset as i64).into(), offset: (local_memory_ptr_offset as i64).into(),
global_type: ptr_type, global_type: ptr_type,
}), }),
self.module_info.memories[local_mem_index], self.module_info.read().unwrap().memories[local_mem_index],
) )
} }
LocalOrImport::Import(import_mem_index) => { LocalOrImport::Import(import_mem_index) => {
@ -528,7 +530,7 @@ impl FuncEnvironment for FunctionEnvironment {
offset: (local_memory_ptr_offset as i64).into(), offset: (local_memory_ptr_offset as i64).into(),
global_type: ptr_type, global_type: ptr_type,
}), }),
self.module_info.imported_memories[import_mem_index].1, self.module_info.read().unwrap().imported_memories[import_mem_index].1,
) )
} }
}; };
@ -599,7 +601,7 @@ impl FuncEnvironment for FunctionEnvironment {
let ptr_type = self.pointer_type(); let ptr_type = self.pointer_type();
let (table_struct_ptr_ptr, description) = match table_index 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) => { LocalOrImport::Local(local_table_index) => {
let tables_base = func.create_global_value(ir::GlobalValueData::Load { let tables_base = func.create_global_value(ir::GlobalValueData::Load {
@ -620,7 +622,7 @@ impl FuncEnvironment for FunctionEnvironment {
( (
table_struct_ptr_ptr, 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) => { LocalOrImport::Import(import_table_index) => {
@ -642,7 +644,7 @@ impl FuncEnvironment for FunctionEnvironment {
( (
table_struct_ptr_ptr, 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 callee_index: FuncIndex = Converter(clif_callee_index).into();
let ptr_type = self.pointer_type(); 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) => { LocalOrImport::Local(local_function_index) => {
// this is an internal function // this is an internal function
let vmctx = pos let vmctx = pos
@ -939,17 +941,17 @@ impl FuncEnvironment for FunctionEnvironment {
let mem_index: MemoryIndex = Converter(clif_mem_index).into(); let mem_index: MemoryIndex = Converter(clif_mem_index).into();
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info) let (namespace, mem_index, description) =
{ match mem_index.local_or_import(&self.module_info.read().unwrap()) {
LocalOrImport::Local(local_mem_index) => ( LocalOrImport::Local(local_mem_index) => (
call_names::LOCAL_NAMESPACE, call_names::LOCAL_NAMESPACE,
local_mem_index.index(), local_mem_index.index(),
self.module_info.memories[local_mem_index], self.module_info.read().unwrap().memories[local_mem_index],
), ),
LocalOrImport::Import(import_mem_index) => ( LocalOrImport::Import(import_mem_index) => (
call_names::IMPORT_NAMESPACE, call_names::IMPORT_NAMESPACE,
import_mem_index.index(), import_mem_index.index(),
self.module_info.imported_memories[import_mem_index].1, self.module_info.read().unwrap().imported_memories[import_mem_index].1,
), ),
}; };
@ -1003,17 +1005,17 @@ impl FuncEnvironment for FunctionEnvironment {
let mem_index: MemoryIndex = Converter(clif_mem_index).into(); let mem_index: MemoryIndex = Converter(clif_mem_index).into();
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info) let (namespace, mem_index, description) =
{ match mem_index.local_or_import(&self.module_info.read().unwrap()) {
LocalOrImport::Local(local_mem_index) => ( LocalOrImport::Local(local_mem_index) => (
call_names::LOCAL_NAMESPACE, call_names::LOCAL_NAMESPACE,
local_mem_index.index(), local_mem_index.index(),
self.module_info.memories[local_mem_index], self.module_info.read().unwrap().memories[local_mem_index],
), ),
LocalOrImport::Import(import_mem_index) => ( LocalOrImport::Import(import_mem_index) => (
call_names::IMPORT_NAMESPACE, call_names::IMPORT_NAMESPACE,
import_mem_index.index(), import_mem_index.index(),
self.module_info.imported_memories[import_mem_index].1, self.module_info.read().unwrap().imported_memories[import_mem_index].1,
), ),
}; };
@ -1048,7 +1050,8 @@ impl FunctionEnvironment {
&self, &self,
func_index: cranelift_wasm::FuncIndex, func_index: cranelift_wasm::FuncIndex,
) -> cranelift_wasm::SignatureIndex { ) -> 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() Converter(sig_index).into()
} }

View File

@ -11,7 +11,7 @@ use smallvec::SmallVec;
use std::fmt; use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::{Arc, RwLock};
use wasmparser::{self, WasmDecoder}; use wasmparser::{self, WasmDecoder};
use wasmparser::{Operator, Type as WpType}; use wasmparser::{Operator, Type as WpType};
@ -49,7 +49,7 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>; fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
/// Creates a new function and returns the function-scope code generator for it. /// Creates a new function and returns the function-scope code generator for it.
fn next_function(&mut self, module_info: Arc<ModuleInfo>) -> Result<&mut FCG, E>; fn next_function(&mut self, module_info: Arc<RwLock<ModuleInfo>>) -> Result<&mut FCG, E>;
fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>; fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>;
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>; fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>;
@ -162,14 +162,14 @@ impl<
&compiler_config, &compiler_config,
)?; )?;
let (exec_context, cache_gen) = let (exec_context, cache_gen) =
mcg.finalize(&info) mcg.finalize(&info.read().unwrap())
.map_err(|x| CompileError::InternalError { .map_err(|x| CompileError::InternalError {
msg: format!("{:?}", x), msg: format!("{:?}", x),
})?; })?;
Ok(ModuleInner { Ok(ModuleInner {
cache_gen, cache_gen,
runnable_module: Box::new(exec_context), runnable_module: Box::new(exec_context),
info: Arc::try_unwrap(info).unwrap(), info: Arc::try_unwrap(info).unwrap().into_inner().unwrap(),
}) })
} }

View File

@ -16,7 +16,7 @@ use crate::{
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc; use std::sync::{Arc, RwLock};
use wasmparser::{ use wasmparser::{
BinaryReaderError, ExternalKind, FuncType, ImportSectionEntryType, Operator, Type as WpType, BinaryReaderError, ExternalKind, FuncType, ImportSectionEntryType, Operator, Type as WpType,
WasmDecoder, WasmDecoder,
@ -53,8 +53,8 @@ pub fn read_module<
mcg: &mut MCG, mcg: &mut MCG,
middlewares: &mut MiddlewareChain, middlewares: &mut MiddlewareChain,
compiler_config: &CompilerConfig, compiler_config: &CompilerConfig,
) -> Result<Arc<ModuleInfo>, LoadError> { ) -> Result<Arc<RwLock<ModuleInfo>>, LoadError> {
let mut info = Arc::new(ModuleInfo { let mut info = Arc::new(RwLock::new(ModuleInfo {
memories: Map::new(), memories: Map::new(),
globals: Map::new(), globals: Map::new(),
tables: Map::new(), tables: Map::new(),
@ -81,7 +81,7 @@ pub fn read_module<
em_symbol_map: compiler_config.symbol_map.clone(), em_symbol_map: compiler_config.symbol_map.clone(),
custom_sections: HashMap::new(), custom_sections: HashMap::new(),
}); }));
let mut parser = wasmparser::ValidatingParser::new( let mut parser = wasmparser::ValidatingParser::new(
wasm, wasm,
@ -107,14 +107,14 @@ pub fn read_module<
ParserState::EndWasm => break, ParserState::EndWasm => break,
ParserState::Error(err) => Err(LoadError::Parse(err))?, ParserState::Error(err) => Err(LoadError::Parse(err))?,
ParserState::TypeSectionEntry(ref ty) => { ParserState::TypeSectionEntry(ref ty) => {
Arc::get_mut(&mut info) info.write()
.unwrap() .unwrap()
.signatures .signatures
.push(func_type_to_func_sig(ty)?); .push(func_type_to_func_sig(ty)?);
} }
ParserState::ImportSectionEntry { module, field, ty } => { ParserState::ImportSectionEntry { module, field, ty } => {
let namespace_index = namespace_builder.as_mut().unwrap().register(module); let namespace_index = namespace_builder.as_mut().expect("116").register(module);
let name_index = name_builder.as_mut().unwrap().register(field); let name_index = name_builder.as_mut().expect("117").register(field);
let import_name = ImportName { let import_name = ImportName {
namespace_index, namespace_index,
name_index, name_index,
@ -123,11 +123,8 @@ pub fn read_module<
match ty { match ty {
ImportSectionEntryType::Function(sigindex) => { ImportSectionEntryType::Function(sigindex) => {
let sigindex = SigIndex::new(sigindex as usize); let sigindex = SigIndex::new(sigindex as usize);
Arc::get_mut(&mut info) info.write().unwrap().imported_functions.push(import_name);
.unwrap() info.write().unwrap().func_assoc.push(sigindex);
.imported_functions
.push(import_name);
Arc::get_mut(&mut info).unwrap().func_assoc.push(sigindex);
mcg.feed_import_function() mcg.feed_import_function()
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
} }
@ -139,7 +136,7 @@ pub fn read_module<
maximum: table_ty.limits.maximum, maximum: table_ty.limits.maximum,
}; };
Arc::get_mut(&mut info) info.write()
.unwrap() .unwrap()
.imported_tables .imported_tables
.push((import_name, table_desc)); .push((import_name, table_desc));
@ -150,7 +147,7 @@ pub fn read_module<
maximum: memory_ty.limits.maximum.map(|max| Pages(max)), maximum: memory_ty.limits.maximum.map(|max| Pages(max)),
shared: memory_ty.shared, shared: memory_ty.shared,
}; };
Arc::get_mut(&mut info) info.write()
.unwrap() .unwrap()
.imported_memories .imported_memories
.push((import_name, mem_desc)); .push((import_name, mem_desc));
@ -160,7 +157,7 @@ pub fn read_module<
mutable: global_ty.mutable, mutable: global_ty.mutable,
ty: wp_type_to_type(global_ty.content_type)?, ty: wp_type_to_type(global_ty.content_type)?,
}; };
Arc::get_mut(&mut info) info.write()
.unwrap() .unwrap()
.imported_globals .imported_globals
.push((import_name, global_desc)); .push((import_name, global_desc));
@ -169,7 +166,7 @@ pub fn read_module<
} }
ParserState::FunctionSectionEntry(sigindex) => { ParserState::FunctionSectionEntry(sigindex) => {
let sigindex = SigIndex::new(sigindex as usize); 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) => { ParserState::TableSectionEntry(table_ty) => {
let table_desc = TableDescriptor { let table_desc = TableDescriptor {
@ -178,7 +175,7 @@ pub fn read_module<
maximum: table_ty.limits.maximum, 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) => { ParserState::MemorySectionEntry(memory_ty) => {
let mem_desc = MemoryDescriptor { let mem_desc = MemoryDescriptor {
@ -187,7 +184,7 @@ pub fn read_module<
shared: memory_ty.shared, 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 } => { ParserState::ExportSectionEntry { field, kind, index } => {
let export_index = match kind { let export_index = match kind {
@ -197,28 +194,26 @@ pub fn read_module<
ExternalKind::Global => ExportIndex::Global(GlobalIndex::new(index as usize)), ExternalKind::Global => ExportIndex::Global(GlobalIndex::new(index as usize)),
}; };
Arc::get_mut(&mut info) info.write()
.unwrap() .unwrap()
.exports .exports
.insert(field.to_string(), export_index); .insert(field.to_string(), export_index);
} }
ParserState::StartSectionEntry(start_index) => { ParserState::StartSectionEntry(start_index) => {
Arc::get_mut(&mut info).unwrap().start_func = info.write().unwrap().start_func = Some(FuncIndex::new(start_index as usize));
Some(FuncIndex::new(start_index as usize));
} }
ParserState::BeginFunctionBody { .. } => { ParserState::BeginFunctionBody { .. } => {
let id = func_count.wrapping_add(1); let id = func_count.wrapping_add(1);
func_count = id; func_count = id;
if func_count == 0 { if func_count == 0 {
Arc::get_mut(&mut info).unwrap().namespace_table = info.write().unwrap().namespace_table =
namespace_builder.take().unwrap().finish(); namespace_builder.take().expect("214").finish();
Arc::get_mut(&mut info).unwrap().name_table = info.write().unwrap().name_table = name_builder.take().expect("216").finish();
name_builder.take().unwrap().finish(); mcg.feed_signatures(info.read().unwrap().signatures.clone())
mcg.feed_signatures(info.signatures.clone())
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .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)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
mcg.check_precondition(&info) mcg.check_precondition(&info.read().unwrap())
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
} }
@ -229,19 +224,24 @@ pub fn read_module<
.run( .run(
Some(fcg), Some(fcg),
Event::Internal(InternalEvent::FunctionBegin(id as u32)), Event::Internal(InternalEvent::FunctionBegin(id as u32)),
&info, &info.read().unwrap(),
) )
.map_err(|x| LoadError::Codegen(x))?; .map_err(|x| LoadError::Codegen(x))?;
let sig = info let info_read = info.read().unwrap();
let sig = info_read
.signatures .signatures
.get( .get(
*info *info
.read()
.unwrap()
.func_assoc .func_assoc
.get(FuncIndex::new(id as usize + info.imported_functions.len())) .get(FuncIndex::new(
.unwrap(), id as usize + info.read().unwrap().imported_functions.len(),
))
.expect("242"),
) )
.unwrap(); .expect("244");
for ret in sig.returns() { for ret in sig.returns() {
fcg.feed_return(type_to_wp_type(*ret)) fcg.feed_return(type_to_wp_type(*ret))
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
@ -266,11 +266,11 @@ pub fn read_module<
ParserState::CodeOperator(op) => { ParserState::CodeOperator(op) => {
if !body_begun { if !body_begun {
body_begun = true; body_begun = true;
fcg.begin_body(&info) fcg.begin_body(&info.read().unwrap())
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
} }
middlewares middlewares
.run(Some(fcg), Event::Wasm(op), &info) .run(Some(fcg), Event::Wasm(op), &info.read().unwrap())
.map_err(|x| LoadError::Codegen(x))?; .map_err(|x| LoadError::Codegen(x))?;
} }
ParserState::EndFunctionBody => break, ParserState::EndFunctionBody => break,
@ -281,7 +281,7 @@ pub fn read_module<
.run( .run(
Some(fcg), Some(fcg),
Event::Internal(InternalEvent::FunctionEnd), Event::Internal(InternalEvent::FunctionEnd),
&info, &info.read().unwrap(),
) )
.map_err(|x| LoadError::Codegen(x))?; .map_err(|x| LoadError::Codegen(x))?;
fcg.finalize() fcg.finalize()
@ -317,14 +317,11 @@ pub fn read_module<
let table_init = TableInitializer { let table_init = TableInitializer {
table_index, table_index,
base: base.unwrap(), base: base.expect("320"),
elements: elements.unwrap(), elements: elements.expect("321"),
}; };
Arc::get_mut(&mut info) info.write().unwrap().elem_initializers.push(table_init);
.unwrap()
.elem_initializers
.push(table_init);
} }
ParserState::BeginActiveDataSectionEntry(memory_index) => { ParserState::BeginActiveDataSectionEntry(memory_index) => {
let memory_index = MemoryIndex::new(memory_index as usize); let memory_index = MemoryIndex::new(memory_index as usize);
@ -352,13 +349,10 @@ pub fn read_module<
let data_init = DataInitializer { let data_init = DataInitializer {
memory_index, memory_index,
base: base.unwrap(), base: base.expect("355"),
data, data,
}; };
Arc::get_mut(&mut info) info.write().unwrap().data_initializers.push(data_init);
.unwrap()
.data_initializers
.push(data_init);
} }
ParserState::BeginGlobalSectionEntry(ty) => { ParserState::BeginGlobalSectionEntry(ty) => {
let init = loop { let init = loop {
@ -379,7 +373,7 @@ pub fn read_module<
let global_init = GlobalInit { desc, init }; let global_init = GlobalInit { desc, init };
Arc::get_mut(&mut info).unwrap().globals.push(global_init); info.write().unwrap().globals.push(global_init);
} }
_ => {} _ => {}