Skip to content

Commit 07c7ba7

Browse files
committed
proc_macro: bypass RandomState to remove ASLR-like effects.
1 parent d76573a commit 07c7ba7

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

library/proc_macro/src/bridge/handle.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Server-side handles and storage for per-handle data.
22
33
use std::collections::{BTreeMap, HashMap};
4-
use std::hash::Hash;
4+
use std::hash::{BuildHasher, Hash};
55
use std::num::NonZeroU32;
66
use std::ops::{Index, IndexMut};
77
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -51,15 +51,31 @@ impl<T> IndexMut<Handle> for OwnedStore<T> {
5151
}
5252
}
5353

54+
// HACK(eddyb) deterministic `std::collections::hash_map::RandomState` replacement
55+
// that doesn't require adding any dependencies to `proc_macro` (like `rustc-hash`).
56+
#[derive(Clone)]
57+
struct NonRandomState;
58+
59+
impl BuildHasher for NonRandomState {
60+
type Hasher = std::collections::hash_map::DefaultHasher;
61+
#[inline]
62+
fn build_hasher(&self) -> Self::Hasher {
63+
Self::Hasher::new()
64+
}
65+
}
66+
5467
/// Like `OwnedStore`, but avoids storing any value more than once.
5568
pub(super) struct InternedStore<T: 'static> {
5669
owned: OwnedStore<T>,
57-
interner: HashMap<T, Handle>,
70+
interner: HashMap<T, Handle, NonRandomState>,
5871
}
5972

6073
impl<T: Copy + Eq + Hash> InternedStore<T> {
6174
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
62-
InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() }
75+
InternedStore {
76+
owned: OwnedStore::new(counter),
77+
interner: HashMap::with_hasher(NonRandomState),
78+
}
6379
}
6480

6581
pub(super) fn alloc(&mut self, x: T) -> Handle {

0 commit comments

Comments
 (0)