Skip to content

Commit 3296d5c

Browse files
committed
Add support for SHA256 source file hashing for LLVM 11+.
1 parent 5565241 commit 3296d5c

File tree

10 files changed

+59
-10
lines changed

10 files changed

+59
-10
lines changed

Cargo.lock

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,17 @@ dependencies = [
18831883
"opaque-debug 0.2.3",
18841884
]
18851885

1886+
[[package]]
1887+
name = "md-5"
1888+
version = "0.9.1"
1889+
source = "registry+https://github.com/rust-lang/crates.io-index"
1890+
checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15"
1891+
dependencies = [
1892+
"block-buffer 0.9.0",
1893+
"digest 0.9.0",
1894+
"opaque-debug 0.3.0",
1895+
]
1896+
18861897
[[package]]
18871898
name = "mdbook"
18881899
version = "0.4.3"
@@ -2411,7 +2422,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
24112422
dependencies = [
24122423
"maplit",
24132424
"pest",
2414-
"sha-1",
2425+
"sha-1 0.8.2",
24152426
]
24162427

24172428
[[package]]
@@ -3225,14 +3236,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32253236
checksum = "1c267f15c3cfc82a8a441d2bf86bcccf299d1eb625822468e3d8ee6f7c5a1c89"
32263237
dependencies = [
32273238
"cfg-if 0.1.10",
3228-
"md-5",
3239+
"md-5 0.8.0",
32293240
"rustc-ap-rustc_arena",
32303241
"rustc-ap-rustc_data_structures",
32313242
"rustc-ap-rustc_index",
32323243
"rustc-ap-rustc_macros",
32333244
"rustc-ap-rustc_serialize",
32343245
"scoped-tls",
3235-
"sha-1",
3246+
"sha-1 0.8.2",
32363247
"tracing",
32373248
"unicode-width",
32383249
]
@@ -4069,14 +4080,15 @@ name = "rustc_span"
40694080
version = "0.0.0"
40704081
dependencies = [
40714082
"cfg-if 0.1.10",
4072-
"md-5",
4083+
"md-5 0.9.1",
40734084
"rustc_arena",
40744085
"rustc_data_structures",
40754086
"rustc_index",
40764087
"rustc_macros",
40774088
"rustc_serialize",
40784089
"scoped-tls",
4079-
"sha-1",
4090+
"sha-1 0.9.1",
4091+
"sha2",
40804092
"tracing",
40814093
"unicode-width",
40824094
]
@@ -4422,6 +4434,19 @@ dependencies = [
44224434
"opaque-debug 0.2.3",
44234435
]
44244436

4437+
[[package]]
4438+
name = "sha-1"
4439+
version = "0.9.1"
4440+
source = "registry+https://github.com/rust-lang/crates.io-index"
4441+
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
4442+
dependencies = [
4443+
"block-buffer 0.9.0",
4444+
"cfg-if 0.1.10",
4445+
"cpuid-bool",
4446+
"digest 0.9.0",
4447+
"opaque-debug 0.3.0",
4448+
]
4449+
44254450
[[package]]
44264451
name = "sha2"
44274452
version = "0.9.1"

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ fn file_metadata_raw(
805805
let kind = match hash.kind {
806806
rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
807807
rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
808+
rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
808809
};
809810
(kind, hex_encode(hash.hash_bytes()))
810811
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ pub enum ChecksumKind {
557557
None,
558558
MD5,
559559
SHA1,
560+
SHA256,
560561
}
561562

562563
extern "C" {

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10681068
span_free_formats: bool = (false, parse_bool, [UNTRACKED],
10691069
"exclude spans when debug-printing compiler state (default: no)"),
10701070
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
1071-
"hash algorithm of source files in debug info (`md5`, or `sha1`)"),
1071+
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
10721072
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
10731073
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
10741074
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,

compiler/rustc_span/Cargo.toml

Lines changed: 3 additions & 2 deletions
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

Lines changed: 8 additions & 1 deletion
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

@@ -1040,6 +1041,7 @@ pub struct OffsetOverflowError;
10401041
pub enum SourceFileHashAlgorithm {
10411042
Md5,
10421043
Sha1,
1044+
Sha256,
10431045
}
10441046

10451047
impl FromStr for SourceFileHashAlgorithm {
@@ -1049,6 +1051,7 @@ impl FromStr for SourceFileHashAlgorithm {
10491051
match s {
10501052
"md5" => Ok(SourceFileHashAlgorithm::Md5),
10511053
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
1054+
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
10521055
_ => Err(()),
10531056
}
10541057
}
@@ -1061,7 +1064,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
10611064
#[derive(HashStable_Generic, Encodable, Decodable)]
10621065
pub struct SourceFileHash {
10631066
pub kind: SourceFileHashAlgorithm,
1064-
value: [u8; 20],
1067+
value: [u8; 32],
10651068
}
10661069

10671070
impl SourceFileHash {
@@ -1077,6 +1080,9 @@ impl SourceFileHash {
10771080
SourceFileHashAlgorithm::Sha1 => {
10781081
value.copy_from_slice(&Sha1::digest(data));
10791082
}
1083+
SourceFileHashAlgorithm::Sha256 => {
1084+
value.copy_from_slice(&Sha256::digest(data));
1085+
}
10801086
}
10811087
hash
10821088
}
@@ -1096,6 +1102,7 @@ impl SourceFileHash {
10961102
match self.kind {
10971103
SourceFileHashAlgorithm::Md5 => 16,
10981104
SourceFileHashAlgorithm::Sha1 => 20,
1105+
SourceFileHashAlgorithm::Sha256 => 32,
10991106
}
11001107
}
11011108
}

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

Lines changed: 1 addition & 1 deletion
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.
Lines changed: 7 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)