Add support for SHA256 source file hashing for LLVM 11+.

This commit is contained in:
Arlo Siemsen 2020-10-13 08:41:06 -07:00
parent 5565241f65
commit 3296d5ca7b
10 changed files with 59 additions and 10 deletions

View file

@ -1883,6 +1883,17 @@ dependencies = [
"opaque-debug 0.2.3", "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]] [[package]]
name = "mdbook" name = "mdbook"
version = "0.4.3" version = "0.4.3"
@ -2411,7 +2422,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
dependencies = [ dependencies = [
"maplit", "maplit",
"pest", "pest",
"sha-1", "sha-1 0.8.2",
] ]
[[package]] [[package]]
@ -3225,14 +3236,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c267f15c3cfc82a8a441d2bf86bcccf299d1eb625822468e3d8ee6f7c5a1c89" checksum = "1c267f15c3cfc82a8a441d2bf86bcccf299d1eb625822468e3d8ee6f7c5a1c89"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 0.1.10",
"md-5", "md-5 0.8.0",
"rustc-ap-rustc_arena", "rustc-ap-rustc_arena",
"rustc-ap-rustc_data_structures", "rustc-ap-rustc_data_structures",
"rustc-ap-rustc_index", "rustc-ap-rustc_index",
"rustc-ap-rustc_macros", "rustc-ap-rustc_macros",
"rustc-ap-rustc_serialize", "rustc-ap-rustc_serialize",
"scoped-tls", "scoped-tls",
"sha-1", "sha-1 0.8.2",
"tracing", "tracing",
"unicode-width", "unicode-width",
] ]
@ -4069,14 +4080,15 @@ name = "rustc_span"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "cfg-if 0.1.10",
"md-5", "md-5 0.9.1",
"rustc_arena", "rustc_arena",
"rustc_data_structures", "rustc_data_structures",
"rustc_index", "rustc_index",
"rustc_macros", "rustc_macros",
"rustc_serialize", "rustc_serialize",
"scoped-tls", "scoped-tls",
"sha-1", "sha-1 0.9.1",
"sha2",
"tracing", "tracing",
"unicode-width", "unicode-width",
] ]
@ -4422,6 +4434,19 @@ dependencies = [
"opaque-debug 0.2.3", "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]] [[package]]
name = "sha2" name = "sha2"
version = "0.9.1" version = "0.9.1"

View file

@ -805,6 +805,7 @@ fn file_metadata_raw(
let kind = match hash.kind { let kind = match hash.kind {
rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5, rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1, rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
}; };
(kind, hex_encode(hash.hash_bytes())) (kind, hex_encode(hash.hash_bytes()))
} }

View file

@ -557,6 +557,7 @@ pub enum ChecksumKind {
None, None,
MD5, MD5,
SHA1, SHA1,
SHA256,
} }
extern "C" { extern "C" {

View file

@ -648,6 +648,7 @@ enum class LLVMRustChecksumKind {
None, None,
MD5, MD5,
SHA1, SHA1,
SHA256,
}; };
static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) { static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
@ -658,6 +659,10 @@ static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
return DIFile::ChecksumKind::CSK_MD5; return DIFile::ChecksumKind::CSK_MD5;
case LLVMRustChecksumKind::SHA1: case LLVMRustChecksumKind::SHA1:
return DIFile::ChecksumKind::CSK_SHA1; return DIFile::ChecksumKind::CSK_SHA1;
#if (LLVM_VERSION_MAJOR >= 11)
case LLVMRustChecksumKind::SHA256:
return DIFile::ChecksumKind::CSK_SHA256;
#endif
default: default:
report_fatal_error("bad ChecksumKind."); report_fatal_error("bad ChecksumKind.");
} }

View file

@ -1068,7 +1068,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
span_free_formats: bool = (false, parse_bool, [UNTRACKED], span_free_formats: bool = (false, parse_bool, [UNTRACKED],
"exclude spans when debug-printing compiler state (default: no)"), "exclude spans when debug-printing compiler state (default: no)"),
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED], 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], strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"), "tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy, symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,

View file

@ -17,5 +17,6 @@ scoped-tls = "1.0"
unicode-width = "0.1.4" unicode-width = "0.1.4"
cfg-if = "0.1.2" cfg-if = "0.1.2"
tracing = "0.1" tracing = "0.1"
sha-1 = "0.8" sha-1 = "0.9"
md-5 = "0.8" sha2 = "0.9"
md-5 = "0.9"

View file

@ -59,6 +59,7 @@ use std::str::FromStr;
use md5::Md5; use md5::Md5;
use sha1::Digest; use sha1::Digest;
use sha1::Sha1; use sha1::Sha1;
use sha2::Sha256;
use tracing::debug; use tracing::debug;
@ -1040,6 +1041,7 @@ pub struct OffsetOverflowError;
pub enum SourceFileHashAlgorithm { pub enum SourceFileHashAlgorithm {
Md5, Md5,
Sha1, Sha1,
Sha256,
} }
impl FromStr for SourceFileHashAlgorithm { impl FromStr for SourceFileHashAlgorithm {
@ -1049,6 +1051,7 @@ impl FromStr for SourceFileHashAlgorithm {
match s { match s {
"md5" => Ok(SourceFileHashAlgorithm::Md5), "md5" => Ok(SourceFileHashAlgorithm::Md5),
"sha1" => Ok(SourceFileHashAlgorithm::Sha1), "sha1" => Ok(SourceFileHashAlgorithm::Sha1),
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
_ => Err(()), _ => Err(()),
} }
} }
@ -1061,7 +1064,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
#[derive(HashStable_Generic, Encodable, Decodable)] #[derive(HashStable_Generic, Encodable, Decodable)]
pub struct SourceFileHash { pub struct SourceFileHash {
pub kind: SourceFileHashAlgorithm, pub kind: SourceFileHashAlgorithm,
value: [u8; 20], value: [u8; 32],
} }
impl SourceFileHash { impl SourceFileHash {
@ -1077,6 +1080,9 @@ impl SourceFileHash {
SourceFileHashAlgorithm::Sha1 => { SourceFileHashAlgorithm::Sha1 => {
value.copy_from_slice(&Sha1::digest(data)); value.copy_from_slice(&Sha1::digest(data));
} }
SourceFileHashAlgorithm::Sha256 => {
value.copy_from_slice(&Sha256::digest(data));
}
} }
hash hash
} }
@ -1096,6 +1102,7 @@ impl SourceFileHash {
match self.kind { match self.kind {
SourceFileHashAlgorithm::Md5 => 16, SourceFileHashAlgorithm::Md5 => 16,
SourceFileHashAlgorithm::Sha1 => 20, SourceFileHashAlgorithm::Sha1 => 20,
SourceFileHashAlgorithm::Sha256 => 32,
} }
} }
} }

View file

@ -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. 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. By default, the compiler chooses the hash algorithm based on the target specification.

View file

@ -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

View file

@ -80,6 +80,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"cloudabi", "cloudabi",
"cmake", "cmake",
"compiler_builtins", "compiler_builtins",
"cpuid-bool",
"crc32fast", "crc32fast",
"crossbeam-deque", "crossbeam-deque",
"crossbeam-epoch", "crossbeam-epoch",
@ -160,6 +161,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"serde", "serde",
"serde_derive", "serde_derive",
"sha-1", "sha-1",
"sha2",
"smallvec", "smallvec",
"snap", "snap",
"stable_deref_trait", "stable_deref_trait",