|
| 1 | +//! Generally LDK uses `std`'s `HashMap`s, however when building for no-std, LDK uses `hashbrown`'s |
| 2 | +//! `HashMap`s with the `std` `SipHasher` and uses `getrandom` to opportunistically randomize it, |
| 3 | +//! if randomization is available. |
| 4 | +//! |
| 5 | +//! This module simply re-exports the `HashMap` used in LDK for public consumption. |
| 6 | +
|
| 7 | +#[cfg(feature = "hashbrown")] |
| 8 | +extern crate hashbrown; |
| 9 | +#[cfg(feature = "possiblyrandom")] |
| 10 | +extern crate possiblyrandom; |
| 11 | + |
| 12 | +// For no-std builds, we need to use hashbrown, however, by default, it doesn't randomize the |
| 13 | +// hashing and is vulnerable to HashDoS attacks. Thus, we use the core SipHasher when not using |
| 14 | +// std, but use `getrandom` to randomize it if its available. |
| 15 | + |
| 16 | +#[cfg(not(feature = "hashbrown"))] |
| 17 | +mod std_hashtables { |
| 18 | + pub use std::collections::HashMap; |
| 19 | + pub use std::collections::hash_map::RandomState; |
| 20 | + |
| 21 | + pub(crate) use std::collections::{HashSet, hash_map}; |
| 22 | + |
| 23 | + pub(crate) type OccupiedHashMapEntry<'a, K, V> = |
| 24 | + std::collections::hash_map::OccupiedEntry<'a, K, V>; |
| 25 | + pub(crate) type VacantHashMapEntry<'a, K, V> = |
| 26 | + std::collections::hash_map::VacantEntry<'a, K, V>; |
| 27 | +} |
| 28 | +#[cfg(not(feature = "hashbrown"))] |
| 29 | +pub use std_hashtables::*; |
| 30 | + |
| 31 | +#[cfg(feature = "hashbrown")] |
| 32 | +pub(crate) use self::hashbrown::hash_map; |
| 33 | + |
| 34 | +#[cfg(all(feature = "hashbrown", fuzzing))] |
| 35 | +mod nonrandomized_hashbrown { |
| 36 | + pub use hashbrown::HashMap; |
| 37 | + pub use std::collections::hash_map::RandomState; |
| 38 | + |
| 39 | + pub(crate) use hashbrown::HashSet; |
| 40 | + |
| 41 | + pub(crate) type OccupiedHashMapEntry<'a, K, V> = |
| 42 | + hashbrown::hash_map::OccupiedEntry<'a, K, V, hashbrown::hash_map::DefaultHashBuilder>; |
| 43 | + pub(crate) type VacantHashMapEntry<'a, K, V> = |
| 44 | + hashbrown::hash_map::VacantEntry<'a, K, V, hashbrown::hash_map::DefaultHashBuilder>; |
| 45 | +} |
| 46 | +#[cfg(all(feature = "hashbrown", fuzzing))] |
| 47 | +pub use nonrandomized_hashbrown::*; |
| 48 | + |
| 49 | +#[cfg(all(feature = "hashbrown", not(fuzzing)))] |
| 50 | +mod randomized_hashtables { |
| 51 | + #[cfg(feature = "std")] |
| 52 | + mod hasher { |
| 53 | + pub use std::collections::hash_map::RandomState; |
| 54 | + } |
| 55 | + #[cfg(not(feature = "std"))] |
| 56 | + mod hasher { |
| 57 | + #![allow(deprecated)] // hash::SipHasher was deprecated in favor of something only in std. |
| 58 | + use core::hash::{BuildHasher, SipHasher}; |
| 59 | + |
| 60 | + #[derive(Clone, Copy)] |
| 61 | + /// A simple implementation of [`BuildHasher`] that uses `getrandom` to opportunistically |
| 62 | + /// randomize, if the platform supports it. |
| 63 | + pub struct RandomState { |
| 64 | + k0: u64, k1: u64, |
| 65 | + } |
| 66 | + |
| 67 | + impl RandomState { |
| 68 | + /// Constructs a new [`RandomState`] which may or may not be random, depending on the |
| 69 | + /// target platform. |
| 70 | + pub fn new() -> RandomState { |
| 71 | + let (k0, k1); |
| 72 | + #[cfg(feature = "possiblyrandom")] { |
| 73 | + let mut keys = [0; 16]; |
| 74 | + possiblyrandom::getpossiblyrandom(&mut keys); |
| 75 | + |
| 76 | + let mut k0_bytes = [0; 8]; |
| 77 | + let mut k1_bytes = [0; 8]; |
| 78 | + k0_bytes.copy_from_slice(&keys[..8]); |
| 79 | + k1_bytes.copy_from_slice(&keys[8..]); |
| 80 | + k0 = u64::from_le_bytes(k0_bytes); |
| 81 | + k1 = u64::from_le_bytes(k1_bytes); |
| 82 | + } |
| 83 | + #[cfg(not(feature = "possiblyrandom"))] { |
| 84 | + k0 = 0; |
| 85 | + k1 = 0; |
| 86 | + } |
| 87 | + RandomState { k0, k1 } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + impl BuildHasher for RandomState { |
| 92 | + type Hasher = SipHasher; |
| 93 | + fn build_hasher(&self) -> SipHasher { |
| 94 | + SipHasher::new_with_keys(self.k0, self.k1) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + pub use hasher::*; |
| 100 | + use super::*; |
| 101 | + |
| 102 | + /// The HashMap type used in LDK. |
| 103 | + pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>; |
| 104 | + pub(crate) type HashSet<K> = hashbrown::HashSet<K, RandomState>; |
| 105 | + |
| 106 | + pub(crate) type OccupiedHashMapEntry<'a, K, V> = |
| 107 | + hashbrown::hash_map::OccupiedEntry<'a, K, V, RandomState>; |
| 108 | + pub(crate) type VacantHashMapEntry<'a, K, V> = |
| 109 | + hashbrown::hash_map::VacantEntry<'a, K, V, RandomState>; |
| 110 | + |
| 111 | + pub(crate) fn new_hash_map<K, V>() -> HashMap<K, V> { |
| 112 | + HashMap::with_hasher(RandomState::new()) |
| 113 | + } |
| 114 | + pub(crate) fn hash_map_with_capacity<K, V>(cap: usize) -> HashMap<K, V> { |
| 115 | + HashMap::with_capacity_and_hasher(cap, RandomState::new()) |
| 116 | + } |
| 117 | + pub(crate) fn hash_map_from_iter<K: core::hash::Hash + Eq, V, I: IntoIterator<Item=(K, V)>>(iter: I) -> HashMap<K, V> { |
| 118 | + let iter = iter.into_iter(); |
| 119 | + let min_size = iter.size_hint().0; |
| 120 | + let mut res = HashMap::with_capacity_and_hasher(min_size, RandomState::new()); |
| 121 | + res.extend(iter); |
| 122 | + res |
| 123 | + } |
| 124 | + |
| 125 | + pub(crate) fn new_hash_set<K>() -> HashSet<K> { |
| 126 | + HashSet::with_hasher(RandomState::new()) |
| 127 | + } |
| 128 | + pub(crate) fn hash_set_with_capacity<K>(cap: usize) -> HashSet<K> { |
| 129 | + HashSet::with_capacity_and_hasher(cap, RandomState::new()) |
| 130 | + } |
| 131 | + pub(crate) fn hash_set_from_iter<K: core::hash::Hash + Eq, I: IntoIterator<Item=K>>(iter: I) -> HashSet<K> { |
| 132 | + let iter = iter.into_iter(); |
| 133 | + let min_size = iter.size_hint().0; |
| 134 | + let mut res = HashSet::with_capacity_and_hasher(min_size, RandomState::new()); |
| 135 | + res.extend(iter); |
| 136 | + res |
| 137 | + } |
| 138 | +} |
| 139 | +#[cfg(all(feature = "hashbrown", not(fuzzing)))] |
| 140 | +pub use randomized_hashtables::*; |
| 141 | + |
| 142 | +#[cfg(any(not(feature = "hashbrown"), fuzzing))] |
| 143 | +mod hashtable_constructors { |
| 144 | + use super::*; |
| 145 | + |
| 146 | + pub(crate) fn new_hash_map<K, V>() -> HashMap<K, V> { HashMap::new() } |
| 147 | + pub(crate) fn hash_map_with_capacity<K, V>(cap: usize) -> HashMap<K, V> { |
| 148 | + HashMap::with_capacity(cap) |
| 149 | + } |
| 150 | + pub(crate) fn hash_map_from_iter<K: core::hash::Hash + Eq, V, I: IntoIterator<Item=(K, V)>>(iter: I) -> HashMap<K, V> { |
| 151 | + HashMap::from_iter(iter) |
| 152 | + } |
| 153 | + |
| 154 | + pub(crate) fn new_hash_set<K>() -> HashSet<K> { HashSet::new() } |
| 155 | + pub(crate) fn hash_set_with_capacity<K>(cap: usize) -> HashSet<K> { |
| 156 | + HashSet::with_capacity(cap) |
| 157 | + } |
| 158 | + pub(crate) fn hash_set_from_iter<K: core::hash::Hash + Eq, I: IntoIterator<Item=K>>(iter: I) -> HashSet<K> { |
| 159 | + HashSet::from_iter(iter) |
| 160 | + } |
| 161 | +} |
| 162 | +#[cfg(any(not(feature = "hashbrown"), fuzzing))] |
| 163 | + |
| 164 | +pub use hashtable_constructors::*; |
0 commit comments