1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::hash::{Hash, Hasher, BuildHasher};
12 use std::marker::PhantomData;
14 use blake2b::Blake2bHasher;
15 use rustc_serialize::leb128;
17 fn write_unsigned_leb128_to_buf(buf: &mut [u8; 16], value: u64) -> usize {
18 leb128::write_unsigned_leb128_to(value as u128, |i, v| buf[i] = v)
21 fn write_signed_leb128_to_buf(buf: &mut [u8; 16], value: i64) -> usize {
22 leb128::write_signed_leb128_to(value as i128, |i, v| buf[i] = v)
25 /// When hashing something that ends up affecting properties like symbol names. We
26 /// want these symbol names to be calculated independent of other factors like
27 /// what architecture you're compiling *from*.
29 /// The hashing just uses the standard `Hash` trait, but the implementations of
30 /// `Hash` for the `usize` and `isize` types are *not* architecture independent
31 /// (e.g. they has 4 or 8 bytes). As a result we want to avoid `usize` and
32 /// `isize` completely when hashing.
34 /// To do that, we encode all integers to be hashed with some
35 /// arch-independent encoding.
37 /// At the moment, we pass i8/u8 straight through and encode
38 /// all other integers using leb128.
40 /// This hasher currently always uses the stable Blake2b algorithm
41 /// and allows for variable output lengths through its type
43 pub struct StableHasher<W> {
46 width: PhantomData<W>,
49 impl<W: StableHasherResult> ::std::fmt::Debug for StableHasher<W> {
50 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
51 write!(f, "{:?}", self.state)
55 pub trait StableHasherResult: Sized {
56 fn finish(hasher: StableHasher<Self>) -> Self;
59 impl<W: StableHasherResult> StableHasher<W> {
60 pub fn new() -> Self {
62 state: Blake2bHasher::new(mem::size_of::<W>(), &[]),
68 pub fn finish(self) -> W {
73 impl StableHasherResult for [u8; 20] {
74 fn finish(mut hasher: StableHasher<Self>) -> Self {
75 let mut result: [u8; 20] = [0; 20];
76 result.copy_from_slice(hasher.state.finalize());
81 impl StableHasherResult for u128 {
82 fn finish(mut hasher: StableHasher<Self>) -> Self {
83 let hash_bytes: &[u8] = hasher.finalize();
84 assert!(hash_bytes.len() >= mem::size_of::<u128>());
87 ::std::ptr::read_unaligned(hash_bytes.as_ptr() as *const u128)
92 impl StableHasherResult for u64 {
93 fn finish(mut hasher: StableHasher<Self>) -> Self {
94 hasher.state.finalize();
99 impl<W> StableHasher<W> {
101 pub fn finalize(&mut self) -> &[u8] {
102 self.state.finalize()
106 pub fn bytes_hashed(&self) -> u64 {
111 fn write_uleb128(&mut self, value: u64) {
112 let mut buf = [0; 16];
113 let len = write_unsigned_leb128_to_buf(&mut buf, value);
114 self.state.write(&buf[..len]);
115 self.bytes_hashed += len as u64;
119 fn write_ileb128(&mut self, value: i64) {
120 let mut buf = [0; 16];
121 let len = write_signed_leb128_to_buf(&mut buf, value);
122 self.state.write(&buf[..len]);
123 self.bytes_hashed += len as u64;
127 // For the non-u8 integer cases we leb128 encode them first. Because small
128 // integers dominate, this significantly and cheaply reduces the number of
129 // bytes hashed, which is good because blake2b is expensive.
130 impl<W> Hasher for StableHasher<W> {
131 fn finish(&self) -> u64 {
132 panic!("use StableHasher::finish instead");
136 fn write(&mut self, bytes: &[u8]) {
137 self.state.write(bytes);
138 self.bytes_hashed += bytes.len() as u64;
142 fn write_u8(&mut self, i: u8) {
143 self.state.write_u8(i);
144 self.bytes_hashed += 1;
148 fn write_u16(&mut self, i: u16) {
149 self.write_uleb128(i as u64);
153 fn write_u32(&mut self, i: u32) {
154 self.write_uleb128(i as u64);
158 fn write_u64(&mut self, i: u64) {
159 self.write_uleb128(i);
163 fn write_usize(&mut self, i: usize) {
164 self.write_uleb128(i as u64);
168 fn write_i8(&mut self, i: i8) {
169 self.state.write_i8(i);
170 self.bytes_hashed += 1;
174 fn write_i16(&mut self, i: i16) {
175 self.write_ileb128(i as i64);
179 fn write_i32(&mut self, i: i32) {
180 self.write_ileb128(i as i64);
184 fn write_i64(&mut self, i: i64) {
185 self.write_ileb128(i);
189 fn write_isize(&mut self, i: isize) {
190 self.write_ileb128(i as i64);
195 /// Something that can provide a stable hashing context.
196 pub trait StableHashingContextProvider {
198 fn create_stable_hashing_context(&self) -> Self::ContextType;
201 impl<'a, T: StableHashingContextProvider> StableHashingContextProvider for &'a T {
202 type ContextType = T::ContextType;
204 fn create_stable_hashing_context(&self) -> Self::ContextType {
205 (**self).create_stable_hashing_context()
209 impl<'a, T: StableHashingContextProvider> StableHashingContextProvider for &'a mut T {
210 type ContextType = T::ContextType;
212 fn create_stable_hashing_context(&self) -> Self::ContextType {
213 (**self).create_stable_hashing_context()
217 /// Something that implements `HashStable<CTX>` can be hashed in a way that is
218 /// stable across multiple compilation sessions.
219 pub trait HashStable<CTX> {
220 fn hash_stable<W: StableHasherResult>(&self,
222 hasher: &mut StableHasher<W>);
225 /// Implement this for types that can be turned into stable keys like, for
226 /// example, for DefId that can be converted to a DefPathHash. This is used for
227 /// bringing maps into a predictable order before hashing them.
228 pub trait ToStableHashKey<HCX> {
229 type KeyType: Ord + Clone + Sized + HashStable<HCX>;
230 fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
233 // Implement HashStable by just calling `Hash::hash()`. This works fine for
234 // self-contained values that don't depend on the hashing context `CTX`.
235 macro_rules! impl_stable_hash_via_hash {
237 impl<CTX> HashStable<CTX> for $t {
239 fn hash_stable<W: StableHasherResult>(&self,
241 hasher: &mut StableHasher<W>) {
242 ::std::hash::Hash::hash(self, hasher);
248 impl_stable_hash_via_hash!(i8);
249 impl_stable_hash_via_hash!(i16);
250 impl_stable_hash_via_hash!(i32);
251 impl_stable_hash_via_hash!(i64);
252 impl_stable_hash_via_hash!(isize);
254 impl_stable_hash_via_hash!(u8);
255 impl_stable_hash_via_hash!(u16);
256 impl_stable_hash_via_hash!(u32);
257 impl_stable_hash_via_hash!(u64);
258 impl_stable_hash_via_hash!(usize);
260 impl_stable_hash_via_hash!(u128);
261 impl_stable_hash_via_hash!(i128);
263 impl_stable_hash_via_hash!(char);
264 impl_stable_hash_via_hash!(());
266 impl<CTX> HashStable<CTX> for f32 {
267 fn hash_stable<W: StableHasherResult>(&self,
269 hasher: &mut StableHasher<W>) {
270 let val: u32 = unsafe {
271 ::std::mem::transmute(*self)
273 val.hash_stable(ctx, hasher);
277 impl<CTX> HashStable<CTX> for f64 {
278 fn hash_stable<W: StableHasherResult>(&self,
280 hasher: &mut StableHasher<W>) {
281 let val: u64 = unsafe {
282 ::std::mem::transmute(*self)
284 val.hash_stable(ctx, hasher);
288 impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
289 fn hash_stable<W: StableHasherResult>(&self,
291 hasher: &mut StableHasher<W>) {
292 let (ref _0,) = *self;
293 _0.hash_stable(ctx, hasher);
297 impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) {
298 fn hash_stable<W: StableHasherResult>(&self,
300 hasher: &mut StableHasher<W>) {
301 let (ref _0, ref _1) = *self;
302 _0.hash_stable(ctx, hasher);
303 _1.hash_stable(ctx, hasher);
307 impl<T1, T2, T3, CTX> HashStable<CTX> for (T1, T2, T3)
308 where T1: HashStable<CTX>,
312 fn hash_stable<W: StableHasherResult>(&self,
314 hasher: &mut StableHasher<W>) {
315 let (ref _0, ref _1, ref _2) = *self;
316 _0.hash_stable(ctx, hasher);
317 _1.hash_stable(ctx, hasher);
318 _2.hash_stable(ctx, hasher);
322 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
323 default fn hash_stable<W: StableHasherResult>(&self,
325 hasher: &mut StableHasher<W>) {
326 self.len().hash_stable(ctx, hasher);
328 item.hash_stable(ctx, hasher);
333 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> {
335 fn hash_stable<W: StableHasherResult>(&self,
337 hasher: &mut StableHasher<W>) {
338 (&self[..]).hash_stable(ctx, hasher);
342 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> {
344 fn hash_stable<W: StableHasherResult>(&self,
346 hasher: &mut StableHasher<W>) {
347 (**self).hash_stable(ctx, hasher);
351 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> {
353 fn hash_stable<W: StableHasherResult>(&self,
355 hasher: &mut StableHasher<W>) {
356 (**self).hash_stable(ctx, hasher);
360 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
362 fn hash_stable<W: StableHasherResult>(&self,
364 hasher: &mut StableHasher<W>) {
365 (**self).hash_stable(ctx, hasher);
369 impl<CTX> HashStable<CTX> for str {
371 fn hash_stable<W: StableHasherResult>(&self,
373 hasher: &mut StableHasher<W>) {
374 self.len().hash(hasher);
375 self.as_bytes().hash(hasher);
380 impl<CTX> HashStable<CTX> for String {
382 fn hash_stable<W: StableHasherResult>(&self,
384 hasher: &mut StableHasher<W>) {
385 (&self[..]).hash_stable(hcx, hasher);
389 impl<HCX> ToStableHashKey<HCX> for String {
390 type KeyType = String;
392 fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
397 impl<CTX> HashStable<CTX> for bool {
399 fn hash_stable<W: StableHasherResult>(&self,
401 hasher: &mut StableHasher<W>) {
402 (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher);
407 impl<T, CTX> HashStable<CTX> for Option<T>
408 where T: HashStable<CTX>
411 fn hash_stable<W: StableHasherResult>(&self,
413 hasher: &mut StableHasher<W>) {
414 if let Some(ref value) = *self {
415 1u8.hash_stable(ctx, hasher);
416 value.hash_stable(ctx, hasher);
418 0u8.hash_stable(ctx, hasher);
423 impl<T1, T2, CTX> HashStable<CTX> for Result<T1, T2>
424 where T1: HashStable<CTX>,
428 fn hash_stable<W: StableHasherResult>(&self,
430 hasher: &mut StableHasher<W>) {
431 mem::discriminant(self).hash_stable(ctx, hasher);
433 Ok(ref x) => x.hash_stable(ctx, hasher),
434 Err(ref x) => x.hash_stable(ctx, hasher),
439 impl<'a, T, CTX> HashStable<CTX> for &'a T
440 where T: HashStable<CTX> + ?Sized
443 fn hash_stable<W: StableHasherResult>(&self,
445 hasher: &mut StableHasher<W>) {
446 (**self).hash_stable(ctx, hasher);
450 impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
452 fn hash_stable<W: StableHasherResult>(&self,
454 hasher: &mut StableHasher<W>) {
455 ::std::hash::Hash::hash(self, hasher);
459 impl<I: ::indexed_vec::Idx, T, CTX> HashStable<CTX> for ::indexed_vec::IndexVec<I, T>
460 where T: HashStable<CTX>,
462 fn hash_stable<W: StableHasherResult>(&self,
464 hasher: &mut StableHasher<W>) {
465 self.len().hash_stable(ctx, hasher);
467 v.hash_stable(ctx, hasher);
473 impl<I: ::indexed_vec::Idx, CTX> HashStable<CTX> for ::indexed_set::IdxSetBuf<I>
475 fn hash_stable<W: StableHasherResult>(&self,
477 hasher: &mut StableHasher<W>) {
478 self.words().hash_stable(ctx, hasher);
482 impl_stable_hash_via_hash!(::std::path::Path);
483 impl_stable_hash_via_hash!(::std::path::PathBuf);
485 impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
486 where K: ToStableHashKey<HCX> + Eq + Hash,
491 fn hash_stable<W: StableHasherResult>(&self,
493 hasher: &mut StableHasher<W>) {
494 hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key);
498 impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
499 where K: ToStableHashKey<HCX> + Eq + Hash,
502 fn hash_stable<W: StableHasherResult>(&self,
504 hasher: &mut StableHasher<W>) {
505 let mut keys: Vec<_> = self.iter()
506 .map(|k| k.to_stable_hash_key(hcx))
508 keys.sort_unstable();
509 keys.hash_stable(hcx, hasher);
513 impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
514 where K: ToStableHashKey<HCX>,
517 fn hash_stable<W: StableHasherResult>(&self,
519 hasher: &mut StableHasher<W>) {
520 let mut entries: Vec<_> = self.iter()
521 .map(|(k, v)| (k.to_stable_hash_key(hcx), v))
523 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
524 entries.hash_stable(hcx, hasher);
528 impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
529 where K: ToStableHashKey<HCX>,
531 fn hash_stable<W: StableHasherResult>(&self,
533 hasher: &mut StableHasher<W>) {
534 let mut keys: Vec<_> = self.iter()
535 .map(|k| k.to_stable_hash_key(hcx))
537 keys.sort_unstable();
538 keys.hash_stable(hcx, hasher);
542 pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>(
544 hasher: &mut StableHasher<W>,
545 map: &::std::collections::HashMap<K, V, R>,
546 to_stable_hash_key: F)
550 SK: HashStable<HCX> + Ord + Clone,
551 F: Fn(&K, &HCX) -> SK,
552 W: StableHasherResult,
554 let mut entries: Vec<_> = map.iter()
555 .map(|(k, v)| (to_stable_hash_key(k, hcx), v))
557 entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
558 entries.hash_stable(hcx, hasher);
562 /// A vector container that makes sure that its items are hashed in a stable
564 pub struct StableVec<T>(Vec<T>);
566 impl<T> StableVec<T> {
567 pub fn new(v: Vec<T>) -> Self {
572 impl<T> ::std::ops::Deref for StableVec<T> {
573 type Target = Vec<T>;
575 fn deref(&self) -> &Vec<T> {
580 impl<T, HCX> HashStable<HCX> for StableVec<T>
581 where T: HashStable<HCX> + ToStableHashKey<HCX>
583 fn hash_stable<W: StableHasherResult>(&self,
585 hasher: &mut StableHasher<W>) {
586 let StableVec(ref v) = *self;
588 let mut sorted: Vec<_> = v.iter()
589 .map(|x| x.to_stable_hash_key(hcx))
591 sorted.sort_unstable();
592 sorted.hash_stable(hcx, hasher);