From 3296d5ca7b41154359eaec76d9f77310816c9913 Mon Sep 17 00:00:00 2001 From: Arlo Siemsen Date: Tue, 13 Oct 2020 08:41:06 -0700 Subject: [PATCH] Add support for SHA256 source file hashing for LLVM 11+. --- Cargo.lock | 35 ++++++++++++++++--- .../src/debuginfo/metadata.rs | 1 + compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 5 +++ compiler/rustc_session/src/options.rs | 2 +- compiler/rustc_span/Cargo.toml | 5 +-- compiler/rustc_span/src/lib.rs | 9 ++++- .../src/compiler-flags/src-hash-algorithm.md | 2 +- .../src-hash-algorithm-sha256.rs | 7 ++++ src/tools/tidy/src/deps.rs | 2 ++ 10 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha256.rs diff --git a/Cargo.lock b/Cargo.lock index 9c48ff86769..faf3c2bca79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" @@ -2411,7 +2422,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d" dependencies = [ "maplit", "pest", - "sha-1", + "sha-1 0.8.2", ] [[package]] @@ -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", ] @@ -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", ] @@ -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" diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 987149cb4c2..caf941f8c45 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -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())) } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 4c1fee0106a..d3663ced24b 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -557,6 +557,7 @@ pub enum ChecksumKind { None, MD5, SHA1, + SHA256, } extern "C" { diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 9f8ea7f43d8..267ff5fa4cc 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -648,6 +648,7 @@ enum class LLVMRustChecksumKind { None, MD5, SHA1, + SHA256, }; static Optional fromRust(LLVMRustChecksumKind Kind) { @@ -658,6 +659,10 @@ static Optional 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."); } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 627adcceb3f..cd75c3672e3 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -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 = (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, diff --git a/compiler/rustc_span/Cargo.toml b/compiler/rustc_span/Cargo.toml index 1abfd50f003..08645990c48 100644 --- a/compiler/rustc_span/Cargo.toml +++ b/compiler/rustc_span/Cargo.toml @@ -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" diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 925f1bd33cb..ffa3d77ecb6 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -59,6 +59,7 @@ use std::str::FromStr; use md5::Md5; use sha1::Digest; use sha1::Sha1; +use sha2::Sha256; use tracing::debug; @@ -1040,6 +1041,7 @@ pub struct OffsetOverflowError; pub enum SourceFileHashAlgorithm { Md5, Sha1, + Sha256, } impl FromStr for SourceFileHashAlgorithm { @@ -1049,6 +1051,7 @@ impl FromStr for SourceFileHashAlgorithm { match s { "md5" => Ok(SourceFileHashAlgorithm::Md5), "sha1" => Ok(SourceFileHashAlgorithm::Sha1), + "sha256" => Ok(SourceFileHashAlgorithm::Sha256), _ => Err(()), } } @@ -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 { @@ -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 } @@ -1096,6 +1102,7 @@ impl SourceFileHash { match self.kind { SourceFileHashAlgorithm::Md5 => 16, SourceFileHashAlgorithm::Sha1 => 20, + SourceFileHashAlgorithm::Sha256 => 32, } } } diff --git a/src/doc/unstable-book/src/compiler-flags/src-hash-algorithm.md b/src/doc/unstable-book/src/compiler-flags/src-hash-algorithm.md index 5a7d0655a44..ff776741b21 100644 --- a/src/doc/unstable-book/src/compiler-flags/src-hash-algorithm.md +++ b/src/doc/unstable-book/src/compiler-flags/src-hash-algorithm.md @@ -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. diff --git a/src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha256.rs b/src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha256.rs new file mode 100644 index 00000000000..eaa9eafa1e8 --- /dev/null +++ b/src/test/codegen/src-hash-algorithm/src-hash-algorithm-sha256.rs @@ -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 diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index 0c52fee68a9..057b0884e28 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -80,6 +80,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[ "cloudabi", "cmake", "compiler_builtins", + "cpuid-bool", "crc32fast", "crossbeam-deque", "crossbeam-epoch", @@ -160,6 +161,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[ "serde", "serde_derive", "sha-1", + "sha2", "smallvec", "snap", "stable_deref_trait",