mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-10 06:38:22 +00:00
Make globals non leaking
This commit is contained in:
@@ -113,15 +113,10 @@ impl Global {
|
||||
Self::from_type(store, GlobalType::new(val.ty(), Mutability::Var), val).unwrap()
|
||||
}
|
||||
|
||||
pub fn from_type(store: &Store, ty: GlobalType, val: Val) -> Result<Global, RuntimeError> {
|
||||
fn from_type(store: &Store, ty: GlobalType, val: Val) -> Result<Global, RuntimeError> {
|
||||
if !val.comes_from_same_store(store) {
|
||||
return Err(RuntimeError::new("cross-`Store` globals are not supported"));
|
||||
}
|
||||
if val.ty() != ty.ty.clone() {
|
||||
return Err(RuntimeError::new(
|
||||
"value provided does not match the type of this global",
|
||||
));
|
||||
}
|
||||
let mut definition = VMGlobalDefinition::new();
|
||||
unsafe {
|
||||
match val {
|
||||
|
||||
@@ -10,8 +10,8 @@ use wasm_common::entity::PrimaryMap;
|
||||
use wasm_common::FuncType;
|
||||
use wasm_common::{
|
||||
DataIndex, DataInitializer, DataInitializerLocation, ElemIndex, ExportIndex, FuncIndex,
|
||||
GlobalIndex, GlobalType, ImportIndex, LocalFuncIndex, MemoryIndex, MemoryType, SignatureIndex,
|
||||
TableIndex, TableType,
|
||||
GlobalIndex, GlobalInit, GlobalType, ImportIndex, LocalFuncIndex, MemoryIndex, MemoryType,
|
||||
SignatureIndex, TableIndex, TableType,
|
||||
};
|
||||
use wasmer_runtime::{Module, TableElements};
|
||||
|
||||
@@ -263,8 +263,13 @@ impl<'data> ModuleEnvironment<'data> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn declare_global(&mut self, global: GlobalType) -> WasmResult<()> {
|
||||
pub(crate) fn declare_global(
|
||||
&mut self,
|
||||
global: GlobalType,
|
||||
initializer: GlobalInit,
|
||||
) -> WasmResult<()> {
|
||||
self.result.module.globals.push(global);
|
||||
self.result.module.global_initializers.push(initializer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +130,6 @@ pub fn parse_import_section<'data>(
|
||||
GlobalType {
|
||||
ty: wptype_to_type(ty.content_type).unwrap(),
|
||||
mutability: ty.mutable.into(),
|
||||
initializer: GlobalInit::Import,
|
||||
},
|
||||
module_name,
|
||||
field_name,
|
||||
@@ -254,9 +253,8 @@ pub fn parse_global_section(
|
||||
let global = GlobalType {
|
||||
ty: wptype_to_type(content_type).unwrap(),
|
||||
mutability: mutable.into(),
|
||||
initializer,
|
||||
};
|
||||
environ.declare_global(global)?;
|
||||
environ.declare_global(global, initializer)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1229,22 +1229,20 @@ fn initialize_memories(
|
||||
|
||||
fn initialize_globals(instance: &Instance) {
|
||||
let module = Arc::clone(&instance.module);
|
||||
let num_imports = module.num_imported_globals;
|
||||
for (index, global) in module.globals.iter().skip(num_imports) {
|
||||
let def_index = module.local_global_index(index).unwrap();
|
||||
for (index, initializer) in module.global_initializers.iter() {
|
||||
unsafe {
|
||||
let to = instance.global_ptr(def_index);
|
||||
match global.initializer {
|
||||
GlobalInit::I32Const(x) => *(*to).as_i32_mut() = x,
|
||||
GlobalInit::I64Const(x) => *(*to).as_i64_mut() = x,
|
||||
GlobalInit::F32Const(x) => *(*to).as_f32_mut() = x,
|
||||
GlobalInit::F64Const(x) => *(*to).as_f64_mut() = x,
|
||||
let to = instance.global_ptr(index);
|
||||
match initializer {
|
||||
GlobalInit::I32Const(x) => *(*to).as_i32_mut() = *x,
|
||||
GlobalInit::I64Const(x) => *(*to).as_i64_mut() = *x,
|
||||
GlobalInit::F32Const(x) => *(*to).as_f32_mut() = *x,
|
||||
GlobalInit::F64Const(x) => *(*to).as_f64_mut() = *x,
|
||||
GlobalInit::V128Const(x) => *(*to).as_u128_bits_mut() = *x.bytes(),
|
||||
GlobalInit::GetGlobal(x) => {
|
||||
let from = if let Some(def_x) = module.local_global_index(x) {
|
||||
let from = if let Some(def_x) = module.local_global_index(*x) {
|
||||
instance.global(def_x)
|
||||
} else {
|
||||
*instance.imported_global(x).definition
|
||||
*instance.imported_global(*x).definition
|
||||
};
|
||||
*to = from;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,9 @@ use std::sync::Arc;
|
||||
use wasm_common::entity::{EntityRef, PrimaryMap};
|
||||
use wasm_common::{
|
||||
DataIndex, ElemIndex, ExportIndex, ExportType, ExternType, FuncIndex, FuncType, GlobalIndex,
|
||||
GlobalType, ImportIndex, ImportType, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex,
|
||||
LocalTableIndex, MemoryIndex, MemoryType, Pages, SignatureIndex, TableIndex, TableType,
|
||||
GlobalInit, GlobalType, ImportIndex, ImportType, LocalFuncIndex, LocalGlobalIndex,
|
||||
LocalMemoryIndex, LocalTableIndex, MemoryIndex, MemoryType, Pages, SignatureIndex, TableIndex,
|
||||
TableType,
|
||||
};
|
||||
|
||||
/// A WebAssembly table initializer.
|
||||
@@ -124,6 +125,9 @@ pub struct Module {
|
||||
/// WebAssembly passive data segments.
|
||||
pub passive_data: HashMap<DataIndex, Arc<[u8]>>,
|
||||
|
||||
/// WebAssembly global initializers.
|
||||
pub global_initializers: PrimaryMap<LocalGlobalIndex, GlobalInit>,
|
||||
|
||||
/// WebAssembly function names.
|
||||
pub func_names: HashMap<FuncIndex, String>,
|
||||
|
||||
@@ -167,6 +171,7 @@ impl Module {
|
||||
table_elements: Vec::new(),
|
||||
passive_elements: HashMap::new(),
|
||||
passive_data: HashMap::new(),
|
||||
global_initializers: PrimaryMap::new(),
|
||||
func_names: HashMap::new(),
|
||||
signatures: PrimaryMap::new(),
|
||||
functions: PrimaryMap::new(),
|
||||
|
||||
@@ -110,20 +110,13 @@ pub enum ExternType {
|
||||
}
|
||||
|
||||
fn is_global_compatible(exported: &GlobalType, imported: &GlobalType) -> bool {
|
||||
match imported.initializer {
|
||||
GlobalInit::Import => (),
|
||||
_ => panic!("imported Global should have an Imported initializer"),
|
||||
}
|
||||
|
||||
let GlobalType {
|
||||
ty: exported_ty,
|
||||
mutability: exported_mutability,
|
||||
initializer: _exported_initializer,
|
||||
} = exported;
|
||||
let GlobalType {
|
||||
ty: imported_ty,
|
||||
mutability: imported_mutability,
|
||||
initializer: _imported_initializer,
|
||||
} = imported;
|
||||
exported_ty == imported_ty && imported_mutability == exported_mutability
|
||||
}
|
||||
@@ -318,8 +311,6 @@ pub struct GlobalType {
|
||||
pub ty: Type,
|
||||
/// A flag indicating whether the value may change at runtime.
|
||||
pub mutability: Mutability,
|
||||
/// The source of the initial value.
|
||||
pub initializer: GlobalInit,
|
||||
}
|
||||
|
||||
// Global Types
|
||||
@@ -344,7 +335,6 @@ impl GlobalType {
|
||||
Self {
|
||||
ty: ty,
|
||||
mutability: mutability,
|
||||
initializer: GlobalInit::Import,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user