|
| 1 | +//! Type aliases for maps used by `wasmparser` |
| 2 | +//! |
| 3 | +//! This module contains type aliases used for [`HashMap`], [`HashSet`], |
| 4 | +//! [`IndexMap`], and [`IndexSet`]. Note that these differ from upstream types |
| 5 | +//! in the `indexmap` crate and the standard library due to customization of the |
| 6 | +//! hash algorithm type parameter. |
| 7 | +
|
| 8 | +use core::hash::{BuildHasher, Hasher}; |
| 9 | + |
| 10 | +/// Wasmparser-specific type alias for an ordered map. |
| 11 | +pub type IndexMap<K, V> = indexmap::IndexMap<K, V, RandomState>; |
| 12 | + |
| 13 | +/// Wasmparser-specific type alias for an ordered set. |
| 14 | +pub type IndexSet<K> = indexmap::IndexSet<K, RandomState>; |
| 15 | + |
| 16 | +/// Wasmparser-specific type alias for hash map. |
| 17 | +pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>; |
| 18 | + |
| 19 | +/// Wasmparser-specific type alias for hash set. |
| 20 | +pub type HashSet<K> = hashbrown::HashSet<K, RandomState>; |
| 21 | + |
| 22 | +/// Wasmparser's hashing state stored per-map. |
| 23 | +/// |
| 24 | +/// This is DoS-resistant when the `std` feature is activated and still somewhat |
| 25 | +/// resistant when it's not active but not as secure. |
| 26 | +#[derive(Clone, Debug)] |
| 27 | +pub struct RandomState(RandomStateImpl); |
| 28 | + |
| 29 | +impl Default for RandomState { |
| 30 | + #[inline] |
| 31 | + fn default() -> RandomState { |
| 32 | + RandomState(RandomStateImpl::default()) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl BuildHasher for RandomState { |
| 37 | + type Hasher = RandomStateHasher; |
| 38 | + |
| 39 | + #[inline] |
| 40 | + fn build_hasher(&self) -> RandomStateHasher { |
| 41 | + RandomStateHasher(self.0.build_hasher()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Wasmparser's hasher type used with [`RandomState`]. |
| 46 | +pub struct RandomStateHasher(<RandomStateImpl as BuildHasher>::Hasher); |
| 47 | + |
| 48 | +impl Hasher for RandomStateHasher { |
| 49 | + #[inline] |
| 50 | + fn finish(&self) -> u64 { |
| 51 | + self.0.finish() |
| 52 | + } |
| 53 | + #[inline] |
| 54 | + fn write(&mut self, bytes: &[u8]) { |
| 55 | + self.0.write(bytes) |
| 56 | + } |
| 57 | + #[inline] |
| 58 | + fn write_u8(&mut self, i: u8) { |
| 59 | + self.0.write_u8(i) |
| 60 | + } |
| 61 | + #[inline] |
| 62 | + fn write_u16(&mut self, i: u16) { |
| 63 | + self.0.write_u16(i) |
| 64 | + } |
| 65 | + #[inline] |
| 66 | + fn write_u32(&mut self, i: u32) { |
| 67 | + self.0.write_u32(i) |
| 68 | + } |
| 69 | + #[inline] |
| 70 | + fn write_u64(&mut self, i: u64) { |
| 71 | + self.0.write_u64(i) |
| 72 | + } |
| 73 | + #[inline] |
| 74 | + fn write_u128(&mut self, i: u128) { |
| 75 | + self.0.write_u128(i) |
| 76 | + } |
| 77 | + #[inline] |
| 78 | + fn write_usize(&mut self, i: usize) { |
| 79 | + self.0.write_usize(i) |
| 80 | + } |
| 81 | + #[inline] |
| 82 | + fn write_i8(&mut self, i: i8) { |
| 83 | + self.0.write_i8(i) |
| 84 | + } |
| 85 | + #[inline] |
| 86 | + fn write_i16(&mut self, i: i16) { |
| 87 | + self.0.write_i16(i) |
| 88 | + } |
| 89 | + #[inline] |
| 90 | + fn write_i32(&mut self, i: i32) { |
| 91 | + self.0.write_i32(i) |
| 92 | + } |
| 93 | + #[inline] |
| 94 | + fn write_i64(&mut self, i: i64) { |
| 95 | + self.0.write_i64(i) |
| 96 | + } |
| 97 | + #[inline] |
| 98 | + fn write_i128(&mut self, i: i128) { |
| 99 | + self.0.write_i128(i) |
| 100 | + } |
| 101 | + #[inline] |
| 102 | + fn write_isize(&mut self, i: isize) { |
| 103 | + self.0.write_isize(i) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// When the `std` feature is active reuse the standard library's implementation |
| 108 | +// of hash state and hasher. |
| 109 | +#[cfg(feature = "std")] |
| 110 | +use std::collections::hash_map::RandomState as RandomStateImpl; |
| 111 | + |
| 112 | +// When the `std` feature is NOT active then rely on `ahash::RandomState`. That |
| 113 | +// relies on ASLR by default for randomness. |
| 114 | +#[derive(Default, Clone, Debug)] |
| 115 | +#[cfg(not(feature = "std"))] |
| 116 | +struct RandomStateImpl; |
| 117 | + |
| 118 | +#[cfg(not(feature = "std"))] |
| 119 | +impl BuildHasher for RandomStateImpl { |
| 120 | + type Hasher = ahash::AHasher; |
| 121 | + |
| 122 | + #[inline] |
| 123 | + fn build_hasher(&self) -> ahash::AHasher { |
| 124 | + ahash::RandomState::new().build_hasher() |
| 125 | + } |
| 126 | +} |
0 commit comments