Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Revert "wasmtime: replace hasher NopHasher with foldhash Fixedstate (#15202)" (#15251)

Alex Crichtoncc5a02af3f04
diff --git a/crates/wasmtime/src/runtime/store/gc.rs b/crates/wasmtime/src/runtime/store/gc.rs
index 37df7e6cc2..76e637fdfb 100644
--- a/crates/wasmtime/src/runtime/store/gc.rs
+++ b/crates/wasmtime/src/runtime/store/gc.rs
@@ -9,8 +9,8 @@ use crate::store::{
};
use crate::type_registry::RegisteredType;
use crate::vm::{
- self, Backtrace, Frame, GcRootsList, GcStore, InstanceAllocationRequest, SendSyncPtr,
- StoreGcHostAllocTypes, TraceInfo, VMGcRef,
+ self, Backtrace, Frame, GcRootsList, GcStore, InstanceAllocationRequest, NopHasher,
+ SendSyncPtr, StoreGcHostAllocTypes, TraceInfo, VMGcRef,
};
use crate::{
Engine, ExnRef, GcHeapOutOfMemory, Result, Rooted, Store, StoreContextMut, Tag,
@@ -21,8 +21,8 @@ use core::mem::ManuallyDrop;
use core::num::NonZeroU32;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
+use wasmtime_environ::DefinedTagIndex;
use wasmtime_environ::packed_option::ReservedValue;
-use wasmtime_environ::{DefinedTagIndex, VMSharedTypeIndex};
#[derive(Default)]
pub(crate) struct StoreGcData {
@@ -64,7 +64,7 @@ pub(crate) struct StoreGcData {
/// solution" where supertypes are stored inline in a compiled-code-visible
/// location which means that a libcall isn't needed at all (nor
/// synchronization) to determine subtype/supertype relationships.
- subtype_check_cache: HashMap<(VMSharedTypeIndex, VMSharedTypeIndex), bool>,
+ subtype_check_cache: HashMap<u64, bool, NopHasher>,
}
impl<T> Store<T> {
@@ -1087,12 +1087,12 @@ impl StoreOpaque {
/// for details.
pub(crate) fn is_subtype_cached(
&mut self,
- sub: VMSharedTypeIndex,
- sup: VMSharedTypeIndex,
+ sub: wasmtime_environ::VMSharedTypeIndex,
+ sup: wasmtime_environ::VMSharedTypeIndex,
) -> bool {
const MAX_SIZE: usize = 1 << 16; // 64k entries
- let key = (sub, sup);
+ let key = (u64::from(sub.as_u32()) << 32) | u64::from(sup.as_u32());
let engine_answer = || self.engine.signatures().is_subtype(sub, sup);
if self.gc_data.subtype_check_cache.len() < MAX_SIZE {
*self
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled.rs b/crates/wasmtime/src/runtime/vm/gc/enabled.rs
index 0090e1f100..9d228521be 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled.rs
@@ -40,3 +40,57 @@ fn is_poisoned(bytes: &[u8]) -> bool {
.chunks(CHUNK)
.all(|chunk| chunk == &POISONED[..chunk.len()])
}
+
+/// A hasher that doesn't hash, for use in the trace-info hash map, where we are
+/// just using scalar keys and aren't overly concerned with collision-based DoS.
+#[derive(Default)]
+pub struct NopHasher(u64);
+
+impl core::hash::BuildHasher for NopHasher {
+ type Hasher = Self;
+
+ #[inline]
+ fn build_hasher(&self) -> Self::Hasher {
+ NopHasher::default()
+ }
+}
+
+impl core::hash::Hasher for NopHasher {
+ #[inline]
+ fn finish(&self) -> u64 {
+ self.0
+ }
+
+ #[inline]
+ fn write(&mut self, bytes: &[u8]) {
+ let mut hash = self.0.to_ne_bytes();
+ let n = hash.len().min(bytes.len());
+ hash[..n].copy_from_slice(bytes);
+ self.0 = u64::from_ne_bytes(hash);
+ }
+
+ #[inline]
+ fn write_u8(&mut self, i: u8) {
+ self.write_u64(i.into());
+ }
+
+ #[inline]
+ fn write_u16(&mut self, i: u16) {
+ self.write_u64(i.into())
+ }
+
+ #[inline]
+ fn write_u32(&mut self, i: u32) {
+ self.write_u64(i.into())
+ }
+
+ #[inline]
+ fn write_u64(&mut self, i: u64) {
+ self.0 = i;
+ }
+
+ #[inline]
+ fn write_usize(&mut self, i: usize) {
+ self.write_u64(i.try_into().unwrap());
+ }
+}
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/trace_infos.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/trace_infos.rs
index 96d998483e..c0d429353b 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled/trace_infos.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/trace_infos.rs
@@ -6,7 +6,7 @@
use crate::hash_map::{Entry, HashMap};
use crate::module::RegisteredModuleId;
-use crate::vm::{GcStoreTraceState, TraceInfo};
+use crate::vm::{GcStoreTraceState, NopHasher, TraceInfo};
use crate::{bail_bug, prelude::*};
use wasmtime_environ::{ModuleInternedTypeIndex, VMSharedTypeIndex};
@@ -19,7 +19,7 @@ enum TraceInfoLoc {
/// A map from GC type indices to where their tracing information can be found.
#[derive(Default)]
pub(super) struct TraceInfos {
- map: HashMap<VMSharedTypeIndex, TraceInfoLoc>,
+ map: HashMap<VMSharedTypeIndex, TraceInfoLoc, NopHasher>,
}
impl TraceInfos {