Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SipHasher to an isolated module. #8233

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::env;
use std::fmt;
use std::hash::{Hash, Hasher, SipHasher};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Arc;

Expand All @@ -11,7 +11,7 @@ use log::info;
use super::{BuildContext, CompileKind, Context, FileFlavor, Layout};
use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit};
use crate::core::{Target, TargetKind, Workspace};
use crate::util::{self, CargoResult};
use crate::util::{self, CargoResult, StableHasher};

/// The `Metadata` is a hash used to make unique file names for each unit in a
/// build. It is also use for symbol mangling.
Expand Down Expand Up @@ -481,7 +481,7 @@ fn compute_metadata(
if !should_use_metadata(bcx, unit) {
return None;
}
let mut hasher = SipHasher::new();
let mut hasher = StableHasher::new();

// This is a generic version number that can be changed to make
// backwards-incompatible changes to any file structures in the output
Expand Down Expand Up @@ -556,7 +556,7 @@ fn compute_metadata(
Some(Metadata(hasher.finish()))
}

fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut SipHasher) {
fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
let vers = &bcx.rustc().version;
if vers.pre.is_empty() || bcx.config.cli_unstable().separate_nightlies {
// For stable, keep the artifacts separate. This helps if someone is
Expand Down
1 change: 0 additions & 1 deletion src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(deprecated)]
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
Expand Down
24 changes: 24 additions & 0 deletions src/cargo/util/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Implementation of a hasher that produces the same values across releases.
//!
//! The hasher should be fast and have a low chance of collisions (but is not
//! sufficient for cryptographic purposes).
#![allow(deprecated)]

use std::hash::{Hasher, SipHasher};

pub struct StableHasher(SipHasher);

impl StableHasher {
pub fn new() -> StableHasher {
StableHasher(SipHasher::new())
}
}

impl Hasher for StableHasher {
fn finish(&self) -> u64 {
self.0.finish()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}
}
9 changes: 4 additions & 5 deletions src/cargo/util/hex.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(deprecated)]

use super::StableHasher;
use std::fs::File;
use std::hash::{Hash, Hasher, SipHasher};
use std::hash::{Hash, Hasher};
use std::io::Read;

pub fn to_hex(num: u64) -> String {
Expand All @@ -18,13 +17,13 @@ pub fn to_hex(num: u64) -> String {
}

pub fn hash_u64<H: Hash>(hashable: H) -> u64 {
let mut hasher = SipHasher::new();
let mut hasher = StableHasher::new();
hashable.hash(&mut hasher);
hasher.finish()
}

pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
let mut hasher = SipHasher::new_with_keys(0, 0);
let mut hasher = StableHasher::new();
let mut buf = [0; 64 * 1024];
loop {
let n = file.read(&mut buf)?;
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use self::errors::{CargoResult, CargoResultExt, CliResult, Test};
pub use self::errors::{CargoTestError, CliError, ProcessError};
pub use self::flock::{FileLock, Filesystem};
pub use self::graph::Graph;
pub use self::hasher::StableHasher;
pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::into_url::IntoUrl;
pub use self::into_url_with_base::IntoUrlWithBase;
Expand Down Expand Up @@ -39,6 +40,7 @@ pub mod diagnostic_server;
pub mod errors;
mod flock;
pub mod graph;
mod hasher;
pub mod hex;
pub mod important_paths;
pub mod into_url;
Expand Down
10 changes: 4 additions & 6 deletions src/cargo/util/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![allow(deprecated)] // for SipHasher

use std::collections::hash_map::{Entry, HashMap};
use std::env;
use std::hash::{Hash, Hasher, SipHasher};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Mutex;

Expand All @@ -11,7 +9,7 @@ use serde::{Deserialize, Serialize};

use crate::core::InternedString;
use crate::util::paths;
use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder};
use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder, StableHasher};

/// Information on the `rustc` executable
#[derive(Debug)]
Expand Down Expand Up @@ -222,7 +220,7 @@ impl Drop for Cache {
}

fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
let mut hasher = SipHasher::new();
let mut hasher = StableHasher::new();

let path = paths::resolve_executable(path)?;
path.hash(&mut hasher);
Expand Down Expand Up @@ -266,7 +264,7 @@ fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult<u64> {
}

fn process_fingerprint(cmd: &ProcessBuilder) -> u64 {
let mut hasher = SipHasher::new();
let mut hasher = StableHasher::new();
cmd.get_args().hash(&mut hasher);
let mut env = cmd.get_envs().iter().collect::<Vec<_>>();
env.sort_unstable();
Expand Down