Skip to content

Commit 52405f7

Browse files
authored
Rollup merge of rust-lang#77950 - arlosi:sha256, r=eddyb
Add support for SHA256 source file hashing Adds support for `-Z src-hash-algorithm sha256`, which became available in LLVM 11. Using an older version of LLVM will cause an error `invalid checksum kind` if the hash algorithm is set to sha256. r? `@eddyb` cc rust-lang#70401 `@est31`
2 parents 0cd1516 + 3296d5c commit 52405f7

File tree

10 files changed

+59
-10
lines changed

10 files changed

+59
-10
lines changed

Cargo.lock

+30-5
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,17 @@ dependencies = [
19281928
"opaque-debug 0.2.3",
19291929
]
19301930

1931+
[[package]]
1932+
name = "md-5"
1933+
version = "0.9.1"
1934+
source = "registry+https://github.com/rust-lang/crates.io-index"
1935+
checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15"
1936+
dependencies = [
1937+
"block-buffer 0.9.0",
1938+
"digest 0.9.0",
1939+
"opaque-debug 0.3.0",
1940+
]
1941+
19311942
[[package]]
19321943
name = "mdbook"
19331944
version = "0.4.3"
@@ -2467,7 +2478,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
24672478
dependencies = [
24682479
"maplit",
24692480
"pest",
2470-
"sha-1",
2481+
"sha-1 0.8.2",
24712482
]
24722483

24732484
[[package]]
@@ -3281,14 +3292,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32813292
checksum = "1c267f15c3cfc82a8a441d2bf86bcccf299d1eb625822468e3d8ee6f7c5a1c89"
32823293
dependencies = [
32833294
"cfg-if 0.1.10",
3284-
"md-5",
3295+
"md-5 0.8.0",
32853296
"rustc-ap-rustc_arena",
32863297
"rustc-ap-rustc_data_structures",
32873298
"rustc-ap-rustc_index",
32883299
"rustc-ap-rustc_macros",
32893300
"rustc-ap-rustc_serialize",
32903301
"scoped-tls",
3291-
"sha-1",
3302+
"sha-1 0.8.2",
32923303
"tracing",
32933304
"unicode-width",
32943305
]
@@ -4138,14 +4149,15 @@ name = "rustc_span"
41384149
version = "0.0.0"
41394150
dependencies = [
41404151
"cfg-if 0.1.10",
4141-
"md-5",
4152+
"md-5 0.9.1",
41424153
"rustc_arena",
41434154
"rustc_data_structures",
41444155
"rustc_index",
41454156
"rustc_macros",
41464157
"rustc_serialize",
41474158
"scoped-tls",
4148-
"sha-1",
4159+
"sha-1 0.9.1",
4160+
"sha2",
41494161
"tracing",
41504162
"unicode-width",
41514163
]
@@ -4510,6 +4522,19 @@ dependencies = [
45104522
"opaque-debug 0.2.3",
45114523
]
45124524

4525+
[[package]]
4526+
name = "sha-1"
4527+
version = "0.9.1"
4528+
source = "registry+https://github.com/rust-lang/crates.io-index"
4529+
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
4530+
dependencies = [
4531+
"block-buffer 0.9.0",
4532+
"cfg-if 0.1.10",
4533+
"cpuid-bool",
4534+
"digest 0.9.0",
4535+
"opaque-debug 0.3.0",
4536+
]
4537+
45134538
[[package]]
45144539
name = "sha2"
45154540
version = "0.9.1"

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ fn file_metadata_raw(
801801
let kind = match hash.kind {
802802
rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
803803
rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
804+
rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
804805
};
805806
(kind, hex_encode(hash.hash_bytes()))
806807
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ pub enum ChecksumKind {
558558
None,
559559
MD5,
560560
SHA1,
561+
SHA256,
561562
}
562563

563564
extern "C" {

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ enum class LLVMRustChecksumKind {
648648
None,
649649
MD5,
650650
SHA1,
651+
SHA256,
651652
};
652653

653654
static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
@@ -658,6 +659,10 @@ static Optional<DIFile::ChecksumKind> fromRust(LLVMRustChecksumKind Kind) {
658659
return DIFile::ChecksumKind::CSK_MD5;
659660
case LLVMRustChecksumKind::SHA1:
660661
return DIFile::ChecksumKind::CSK_SHA1;
662+
#if (LLVM_VERSION_MAJOR >= 11)
663+
case LLVMRustChecksumKind::SHA256:
664+
return DIFile::ChecksumKind::CSK_SHA256;
665+
#endif
661666
default:
662667
report_fatal_error("bad ChecksumKind.");
663668
}

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10761076
span_free_formats: bool = (false, parse_bool, [UNTRACKED],
10771077
"exclude spans when debug-printing compiler state (default: no)"),
10781078
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
1079-
"hash algorithm of source files in debug info (`md5`, or `sha1`)"),
1079+
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
10801080
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
10811081
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
10821082
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,

compiler/rustc_span/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ scoped-tls = "1.0"
1717
unicode-width = "0.1.4"
1818
cfg-if = "0.1.2"
1919
tracing = "0.1"
20-
sha-1 = "0.8"
21-
md-5 = "0.8"
20+
sha-1 = "0.9"
21+
sha2 = "0.9"
22+
md-5 = "0.9"

compiler/rustc_span/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use std::str::FromStr;
5959
use md5::Md5;
6060
use sha1::Digest;
6161
use sha1::Sha1;
62+
use sha2::Sha256;
6263

6364
use tracing::debug;
6465

@@ -1034,6 +1035,7 @@ pub struct OffsetOverflowError;
10341035
pub enum SourceFileHashAlgorithm {
10351036
Md5,
10361037
Sha1,
1038+
Sha256,
10371039
}
10381040

10391041
impl FromStr for SourceFileHashAlgorithm {
@@ -1043,6 +1045,7 @@ impl FromStr for SourceFileHashAlgorithm {
10431045
match s {
10441046
"md5" => Ok(SourceFileHashAlgorithm::Md5),
10451047
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
1048+
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
10461049
_ => Err(()),
10471050
}
10481051
}
@@ -1055,7 +1058,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
10551058
#[derive(HashStable_Generic, Encodable, Decodable)]
10561059
pub struct SourceFileHash {
10571060
pub kind: SourceFileHashAlgorithm,
1058-
value: [u8; 20],
1061+
value: [u8; 32],
10591062
}
10601063

10611064
impl SourceFileHash {
@@ -1071,6 +1074,9 @@ impl SourceFileHash {
10711074
SourceFileHashAlgorithm::Sha1 => {
10721075
value.copy_from_slice(&Sha1::digest(data));
10731076
}
1077+
SourceFileHashAlgorithm::Sha256 => {
1078+
value.copy_from_slice(&Sha256::digest(data));
1079+
}
10741080
}
10751081
hash
10761082
}
@@ -1090,6 +1096,7 @@ impl SourceFileHash {
10901096
match self.kind {
10911097
SourceFileHashAlgorithm::Md5 => 16,
10921098
SourceFileHashAlgorithm::Sha1 => 20,
1099+
SourceFileHashAlgorithm::Sha256 => 32,
10931100
}
10941101
}
10951102
}

src/doc/unstable-book/src/compiler-flags/src-hash-algorithm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ The tracking issue for this feature is: [#70401](https://github.com/rust-lang/ru
66

77
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.
88

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

1111
By default, the compiler chooses the hash algorithm based on the target specification.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: -g -Z src-hash-algorithm=sha256
2+
// min-llvm-version: 11.0
3+
4+
#![crate_type = "lib"]
5+
6+
pub fn test() {}
7+
// CHECK: checksumkind: CSK_SHA256

src/tools/tidy/src/deps.rs

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
8080
"cloudabi",
8181
"cmake",
8282
"compiler_builtins",
83+
"cpuid-bool",
8384
"crc32fast",
8485
"crossbeam-deque",
8586
"crossbeam-epoch",
@@ -160,6 +161,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
160161
"serde",
161162
"serde_derive",
162163
"sha-1",
164+
"sha2",
163165
"smallvec",
164166
"snap",
165167
"stable_deref_trait",

0 commit comments

Comments
 (0)