diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2020-11-05 19:29:40 +0200 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <eddyb@lyken.rs> | 2022-06-13 07:59:44 +0000 |
| commit | 07c7ba72056cf03830dc508ad634b9c371cd4a89 (patch) | |
| tree | cba0c44e481d19225ef914f9561b7a7719597d2e /library/proc_macro/src | |
| parent | d76573abd19d50c25c5b58ec7fc5cfef579e6eef (diff) | |
proc_macro: bypass RandomState to remove ASLR-like effects.
Diffstat (limited to 'library/proc_macro/src')
| -rw-r--r-- | library/proc_macro/src/bridge/handle.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/library/proc_macro/src/bridge/handle.rs b/library/proc_macro/src/bridge/handle.rs index 7d6adda48ec..c219a9465d3 100644 --- a/library/proc_macro/src/bridge/handle.rs +++ b/library/proc_macro/src/bridge/handle.rs @@ -1,7 +1,7 @@ //! Server-side handles and storage for per-handle data. use std::collections::{BTreeMap, HashMap}; -use std::hash::Hash; +use std::hash::{BuildHasher, Hash}; use std::num::NonZeroU32; use std::ops::{Index, IndexMut}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -51,15 +51,31 @@ impl<T> IndexMut<Handle> for OwnedStore<T> { } } +// HACK(eddyb) deterministic `std::collections::hash_map::RandomState` replacement +// that doesn't require adding any dependencies to `proc_macro` (like `rustc-hash`). +#[derive(Clone)] +struct NonRandomState; + +impl BuildHasher for NonRandomState { + type Hasher = std::collections::hash_map::DefaultHasher; + #[inline] + fn build_hasher(&self) -> Self::Hasher { + Self::Hasher::new() + } +} + /// Like `OwnedStore`, but avoids storing any value more than once. pub(super) struct InternedStore<T: 'static> { owned: OwnedStore<T>, - interner: HashMap<T, Handle>, + interner: HashMap<T, Handle, NonRandomState>, } impl<T: Copy + Eq + Hash> InternedStore<T> { pub(super) fn new(counter: &'static AtomicUsize) -> Self { - InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() } + InternedStore { + owned: OwnedStore::new(counter), + interner: HashMap::with_hasher(NonRandomState), + } } pub(super) fn alloc(&mut self, x: T) -> Handle { |
