diff --git a/wasmtime-api/src/externals.rs b/wasmtime-api/src/externals.rs index de3a4861bd..f1cde77b90 100644 --- a/wasmtime-api/src/externals.rs +++ b/wasmtime-api/src/externals.rs @@ -290,7 +290,7 @@ impl Table { match wasmtime_export { wasmtime_runtime::Export::Table { definition, .. } => { let index = wasmtime_handle.table_index(unsafe { &*definition }); - let len = unsafe { (*definition).current_elements as u32 }; + let len = unsafe { (*definition).current_elements }; for i in 0..len { let _success = set_table_item(&mut wasmtime_handle, &store, index, i, init.clone()); @@ -335,7 +335,7 @@ impl Table { pub fn size(&self) -> u32 { match self.wasmtime_export { wasmtime_runtime::Export::Table { definition, .. } => unsafe { - (*definition).current_elements as u32 + (*definition).current_elements }, _ => panic!("global definition not found"), } @@ -346,7 +346,7 @@ impl Table { if let Some(len) = self.wasmtime_handle.table_grow(index, delta) { let mut wasmtime_handle = self.wasmtime_handle.clone(); for i in 0..delta { - let i = len as u32 - (delta - i); + let i = len - (delta - i); let _success = set_table_item(&mut wasmtime_handle, &self.store, index, i, init.clone()); assert!(_success); diff --git a/wasmtime-obj/src/context.rs b/wasmtime-obj/src/context.rs index 779013afe7..f5bf37633a 100644 --- a/wasmtime-obj/src/context.rs +++ b/wasmtime-obj/src/context.rs @@ -45,7 +45,7 @@ pub fn layout_vmcontext( for (index, table) in module.table_plans.iter().skip(num_tables_imports) { let def_index = module.defined_table_index(index).unwrap(); let offset = ofs.vmctx_vmtable_definition(def_index) as usize; - let current_elements = table.table.minimum as usize; + let current_elements = table.table.minimum; unsafe { assert_eq!( ::std::mem::size_of::() as u8, @@ -56,7 +56,7 @@ pub fn layout_vmcontext( .as_mut_ptr() .add(offset) .add(ofs.vmtable_definition_current_elements() as usize); - ptr::write(to as *mut u32, current_elements as u32); + ptr::write(to as *mut u32, current_elements); } table_relocs.push(TableRelocation { index: def_index.index(), diff --git a/wasmtime-runtime/src/sig_registry.rs b/wasmtime-runtime/src/sig_registry.rs index 4a26850db2..77196f392e 100644 --- a/wasmtime-runtime/src/sig_registry.rs +++ b/wasmtime-runtime/src/sig_registry.rs @@ -29,6 +29,12 @@ impl SignatureRegistry { match self.signature_hash.entry(sig.clone()) { hash_map::Entry::Occupied(entry) => *entry.get(), hash_map::Entry::Vacant(entry) => { + // Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(std::u32::MAX) + // is reserved for VMSharedSignatureIndex::default(). + debug_assert!( + len < std::u32::MAX as usize, + "Invariant check: signature_hash.len() < std::u32::MAX" + ); let sig_id = VMSharedSignatureIndex::new(u32::try_from(len).unwrap()); entry.insert(sig_id); sig_id diff --git a/wasmtime-runtime/src/table.rs b/wasmtime-runtime/src/table.rs index 98e38b234f..b8c9c3e831 100644 --- a/wasmtime-runtime/src/table.rs +++ b/wasmtime-runtime/src/table.rs @@ -4,6 +4,7 @@ use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition}; use cranelift_wasm::TableElementType; +use std::convert::{TryFrom, TryInto}; use std::vec::Vec; use wasmtime_environ::{TablePlan, TableStyle}; @@ -23,7 +24,10 @@ impl Table { unimplemented!("tables of types other than anyfunc ({})", ty) } }; - + assert!( + plan.table.minimum <= std::u32::MAX, + "Invariant check: vec.len() <= u32::MAX" + ); match plan.style { TableStyle::CallerChecksSignature => Self { vec: vec![VMCallerCheckedAnyfunc::default(); plan.table.minimum as usize], @@ -34,7 +38,7 @@ impl Table { /// Returns the number of allocated elements. pub fn size(&self) -> u32 { - self.vec.len() as u32 + self.vec.len().try_into().unwrap() } /// Grow table by the specified amount of elements. @@ -55,8 +59,14 @@ impl Table { return None; } }; - self.vec - .resize(new_len as usize, VMCallerCheckedAnyfunc::default()); + assert!( + new_len <= std::u32::MAX, + "Invariant check: vec.len() <= u32::MAX" + ); + self.vec.resize( + usize::try_from(new_len).unwrap(), + VMCallerCheckedAnyfunc::default(), + ); Some(new_len) } @@ -78,7 +88,7 @@ impl Table { pub fn vmtable(&mut self) -> VMTableDefinition { VMTableDefinition { base: self.vec.as_mut_ptr() as *mut u8, - current_elements: self.vec.len(), + current_elements: self.vec.len().try_into().unwrap(), } } } diff --git a/wasmtime-runtime/src/vmcontext.rs b/wasmtime-runtime/src/vmcontext.rs index fe840b0020..1f7721a47b 100644 --- a/wasmtime-runtime/src/vmcontext.rs +++ b/wasmtime-runtime/src/vmcontext.rs @@ -217,7 +217,7 @@ pub struct VMTableDefinition { pub base: *mut u8, /// The current number of elements in the table. - pub current_elements: usize, + pub current_elements: u32, } #[cfg(test)]