Skip to content

Commit

Permalink
Add support for SHA256 source file hashing for LLVM 11+.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlosi committed Oct 14, 2020
1 parent 5565241 commit 3296d5c
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 10 deletions.
35 changes: 30 additions & 5 deletions Cargo.lock
Expand Up @@ -1883,6 +1883,17 @@ dependencies = [
"opaque-debug 0.2.3",
]

[[package]]
name = "md-5"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15"
dependencies = [
"block-buffer 0.9.0",
"digest 0.9.0",
"opaque-debug 0.3.0",
]

[[package]]
name = "mdbook"
version = "0.4.3"
Expand Down Expand Up @@ -2411,7 +2422,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
dependencies = [
"maplit",
"pest",
"sha-1",
"sha-1 0.8.2",
]

[[package]]
Expand Down Expand Up @@ -3225,14 +3236,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c267f15c3cfc82a8a441d2bf86bcccf299d1eb625822468e3d8ee6f7c5a1c89"
dependencies = [
"cfg-if 0.1.10",
"md-5",
"md-5 0.8.0",
"rustc-ap-rustc_arena",
"rustc-ap-rustc_data_structures",
"rustc-ap-rustc_index",
"rustc-ap-rustc_macros",
"rustc-ap-rustc_serialize",
"scoped-tls",
"sha-1",
"sha-1 0.8.2",
"tracing",
"unicode-width",
]
Expand Down Expand Up @@ -4069,14 +4080,15 @@ name = "rustc_span"
version = "0.0.0"
dependencies = [
"cfg-if 0.1.10",
"md-5",
"md-5 0.9.1",
"rustc_arena",
"rustc_data_structures",
"rustc_index",
"rustc_macros",
"rustc_serialize",
"scoped-tls",
"sha-1",
"sha-1 0.9.1",
"sha2",
"tracing",
"unicode-width",
]
Expand Down Expand Up @@ -4422,6 +4434,19 @@ dependencies = [
"opaque-debug 0.2.3",
]

[[package]]
name = "sha-1"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 0.1.10",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
]

[[package]]
name = "sha2"
version = "0.9.1"
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Expand Up @@ -805,6 +805,7 @@ fn file_metadata_raw(
let kind = match hash.kind {
rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
};
(kind, hex_encode(hash.hash_bytes()))
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Expand Up @@ -557,6 +557,7 @@ pub enum ChecksumKind {
None,
MD5,
SHA1,
SHA256,
}

extern "C" {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Expand Up @@ -648,6 +648,7 @@ enum class LLVMRustChecksumKind {
None,
MD5,
SHA1,
SHA256,
};

static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
Expand All @@ -658,6 +659,10 @@ static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
return DIFile::ChecksumKind::CSK_MD5;
case LLVMRustChecksumKind::SHA1:
return DIFile::ChecksumKind::CSK_SHA1;
#if (LLVM_VERSION_MAJOR >= 11)
case LLVMRustChecksumKind::SHA256:
return DIFile::ChecksumKind::CSK_SHA256;
#endif
default:
report_fatal_error("bad ChecksumKind.");
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Expand Up @@ -1068,7 +1068,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
span_free_formats: bool = (false, parse_bool, [UNTRACKED],
"exclude spans when debug-printing compiler state (default: no)"),
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
"hash algorithm of source files in debug info (`md5`, or `sha1`)"),
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_span/Cargo.toml
Expand Up @@ -17,5 +17,6 @@ scoped-tls = "1.0"
unicode-width = "0.1.4"
cfg-if = "0.1.2"
tracing = "0.1"
sha-1 = "0.8"
md-5 = "0.8"
sha-1 = "0.9"
sha2 = "0.9"
md-5 = "0.9"
9 changes: 8 additions & 1 deletion compiler/rustc_span/src/lib.rs
Expand Up @@ -59,6 +59,7 @@ use std::str::FromStr;
use md5::Md5;
use sha1::Digest;
use sha1::Sha1;
use sha2::Sha256;

use tracing::debug;

Expand Down Expand Up @@ -1040,6 +1041,7 @@ pub struct OffsetOverflowError;
pub enum SourceFileHashAlgorithm {
Md5,
Sha1,
Sha256,
}

impl FromStr for SourceFileHashAlgorithm {
Expand All @@ -1049,6 +1051,7 @@ impl FromStr for SourceFileHashAlgorithm {
match s {
"md5" => Ok(SourceFileHashAlgorithm::Md5),
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
_ => Err(()),
}
}
Expand All @@ -1061,7 +1064,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
#[derive(HashStable_Generic, Encodable, Decodable)]
pub struct SourceFileHash {
pub kind: SourceFileHashAlgorithm,
value: [u8; 20],
value: [u8; 32],
}

impl SourceFileHash {
Expand All @@ -1077,6 +1080,9 @@ impl SourceFileHash {
SourceFileHashAlgorithm::Sha1 => {
value.copy_from_slice(&Sha1::digest(data));
}
SourceFileHashAlgorithm::Sha256 => {
value.copy_from_slice(&Sha256::digest(data));
}
}
hash
}
Expand All @@ -1096,6 +1102,7 @@ impl SourceFileHash {
match self.kind {
SourceFileHashAlgorithm::Md5 => 16,
SourceFileHashAlgorithm::Sha1 => 20,
SourceFileHashAlgorithm::Sha256 => 32,
}
}
}
Expand Down
Expand Up @@ -6,6 +6,6 @@ The tracking issue for this feature is: [#70401](https://github.com/rust-lang/ru

The `-Z src-hash-algorithm` compiler flag controls which algorithm is used when hashing each source file. The hash is stored in the debug info and can be used by a debugger to verify the source code matches the executable.

Supported hash algorithms are: `md5`, and `sha1`. Note that not all hash algorithms are supported by all debug info formats.
Supported hash algorithms are: `md5`, `sha1`, and `sha256`. Note that not all hash algorithms are supported by all debug info formats.

By default, the compiler chooses the hash algorithm based on the target specification.
@@ -0,0 +1,7 @@
// compile-flags: -g -Z src-hash-algorithm=sha256
// min-llvm-version: 11.0

#![crate_type = "lib"]

pub fn test() {}
// CHECK: checksumkind: CSK_SHA256
2 changes: 2 additions & 0 deletions src/tools/tidy/src/deps.rs
Expand Up @@ -80,6 +80,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"cloudabi",
"cmake",
"compiler_builtins",
"cpuid-bool",
"crc32fast",
"crossbeam-deque",
"crossbeam-epoch",
Expand Down Expand Up @@ -160,6 +161,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"serde",
"serde_derive",
"sha-1",
"sha2",
"smallvec",
"snap",
"stable_deref_trait",
Expand Down

0 comments on commit 3296d5c

Please sign in to comment.