Skip to content

Commit 5701093

Browse files
committed
Auto merge of rust-lang#118344 - saethlin:rmeta-header-pos, r=WaffleLapkin
Use a u64 for the rmeta root position Waffle noticed this in rust-lang#117301 (comment) We've upgraded the other file offsets to u64, and this one only costs 4 bytes per file. Also the way the truncation was being done before was extremely easy to miss, I sure missed it! It's not clear to me if not having this change effectively made the other upgrades from u32 to u64 ineffective, but we can have it now. r? `@WaffleLapkin`
2 parents 8a37655 + 79bdd24 commit 5701093

File tree

6 files changed

+46
-44
lines changed

6 files changed

+46
-44
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a
158158
file.symbols().find(|sym| sym.name() == Ok(AIX_METADATA_SYMBOL_NAME))
159159
{
160160
let offset = metadata_symbol.address() as usize;
161-
if offset < 4 {
161+
if offset < 8 {
162162
return Err(format!("Invalid metadata symbol offset: {offset}"));
163163
}
164164
// The offset specifies the location of rustc metadata in the comment section.
165-
// The metadata is preceded by a 4-byte length field.
166-
let len = u32::from_be_bytes(info_data[(offset - 4)..offset].try_into().unwrap()) as usize;
165+
// The metadata is preceded by a 8-byte length field.
166+
let len = u64::from_le_bytes(info_data[(offset - 8)..offset].try_into().unwrap()) as usize;
167167
if offset + len > (info_data.len() as usize) {
168168
return Err(format!(
169169
"Metadata at offset {offset} with size {len} is beyond .info section"
@@ -479,8 +479,8 @@ pub fn create_wrapper_file(
479479
file.section_mut(section).flags =
480480
SectionFlags::Xcoff { s_flags: xcoff::STYP_INFO as u32 };
481481

482-
let len = data.len() as u32;
483-
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
482+
let len = data.len() as u64;
483+
let offset = file.append_section_data(section, &len.to_le_bytes(), 1);
484484
// Add a symbol referring to the data in .info section.
485485
file.add_symbol(Symbol {
486486
name: AIX_METADATA_SYMBOL_NAME.into(),
@@ -524,7 +524,7 @@ pub fn create_compressed_metadata_file(
524524
symbol_name: &str,
525525
) -> Vec<u8> {
526526
let mut packed_metadata = rustc_metadata::METADATA_HEADER.to_vec();
527-
packed_metadata.write_all(&(metadata.raw_data().len() as u32).to_be_bytes()).unwrap();
527+
packed_metadata.write_all(&(metadata.raw_data().len() as u64).to_le_bytes()).unwrap();
528528
packed_metadata.extend(metadata.raw_data());
529529

530530
let Some(mut file) = create_object_file(sess) else {
@@ -599,12 +599,12 @@ pub fn create_compressed_metadata_file_for_xcoff(
599599
section: SymbolSection::Section(data_section),
600600
flags: SymbolFlags::None,
601601
});
602-
let len = data.len() as u32;
603-
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
602+
let len = data.len() as u64;
603+
let offset = file.append_section_data(section, &len.to_le_bytes(), 1);
604604
// Add a symbol referring to the rustc metadata.
605605
file.add_symbol(Symbol {
606606
name: AIX_METADATA_SYMBOL_NAME.into(),
607-
value: offset + 4, // The metadata is preceded by a 4-byte length field.
607+
value: offset + 8, // The metadata is preceded by a 8-byte length field.
608608
size: 0,
609609
kind: SymbolKind::Unknown,
610610
scope: SymbolScope::Dynamic,

compiler/rustc_metadata/src/locator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,8 @@ fn get_metadata_section<'p>(
783783
loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
784784
// The header is uncompressed
785785
let header_len = METADATA_HEADER.len();
786-
// header + u32 length of data
787-
let data_start = header_len + 4;
786+
// header + u64 length of data
787+
let data_start = header_len + 8;
788788

789789
debug!("checking {} bytes of metadata-version stamp", header_len);
790790
let header = &buf[..cmp::min(header_len, buf.len())];
@@ -797,13 +797,13 @@ fn get_metadata_section<'p>(
797797

798798
// Length of the compressed stream - this allows linkers to pad the section if they want
799799
let Ok(len_bytes) =
800-
<[u8; 4]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())])
800+
<[u8; 8]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())])
801801
else {
802802
return Err(MetadataError::LoadFailure(
803803
"invalid metadata length found".to_string(),
804804
));
805805
};
806-
let compressed_len = u32::from_be_bytes(len_bytes) as usize;
806+
let compressed_len = u64::from_le_bytes(len_bytes) as usize;
807807

808808
// Header is okay -> inflate the actual metadata
809809
let compressed_bytes = buf.slice(|buf| &buf[data_start..(data_start + compressed_len)]);

compiler/rustc_metadata/src/rmeta/decoder.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -690,28 +690,25 @@ impl MetadataBlob {
690690
}
691691

692692
pub(crate) fn get_rustc_version(&self) -> String {
693-
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap())
693+
LazyValue::<String>::from_position(NonZeroUsize::new(METADATA_HEADER.len() + 8).unwrap())
694694
.decode(self)
695695
}
696696

697-
pub(crate) fn get_header(&self) -> CrateHeader {
698-
let slice = &self.blob()[..];
697+
fn root_pos(&self) -> NonZeroUsize {
699698
let offset = METADATA_HEADER.len();
699+
let pos_bytes = self.blob()[offset..][..8].try_into().unwrap();
700+
let pos = u64::from_le_bytes(pos_bytes);
701+
NonZeroUsize::new(pos as usize).unwrap()
702+
}
700703

701-
let pos_bytes = slice[offset..][..4].try_into().unwrap();
702-
let pos = u32::from_be_bytes(pos_bytes) as usize;
703-
704-
LazyValue::<CrateHeader>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
704+
pub(crate) fn get_header(&self) -> CrateHeader {
705+
let pos = self.root_pos();
706+
LazyValue::<CrateHeader>::from_position(pos).decode(self)
705707
}
706708

707709
pub(crate) fn get_root(&self) -> CrateRoot {
708-
let slice = &self.blob()[..];
709-
let offset = METADATA_HEADER.len();
710-
711-
let pos_bytes = slice[offset..][..4].try_into().unwrap();
712-
let pos = u32::from_be_bytes(pos_bytes) as usize;
713-
714-
LazyValue::<CrateRoot>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
710+
let pos = self.root_pos();
711+
LazyValue::<CrateRoot>::from_position(pos).decode(self)
715712
}
716713

717714
pub(crate) fn list_crate_metadata(

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
21902190
encoder.emit_raw_bytes(METADATA_HEADER);
21912191

21922192
// Will be filled with the root position after encoding everything.
2193-
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
2193+
encoder.emit_raw_bytes(&0u64.to_le_bytes());
21942194

21952195
let source_map_files = tcx.sess.source_map().files();
21962196
let source_file_cache = (source_map_files[0].clone(), 0);
@@ -2246,7 +2246,7 @@ fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Erro
22462246
// Encode the root position.
22472247
let header = METADATA_HEADER.len();
22482248
file.seek(std::io::SeekFrom::Start(header as u64))?;
2249-
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])?;
2249+
file.write_all(&pos.to_le_bytes())?;
22502250

22512251
// Return to the position where we are before writing the root position.
22522252
file.seek(std::io::SeekFrom::Start(pos_before_seek))?;

compiler/rustc_metadata/src/rmeta/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
5757
/// Metadata encoding version.
5858
/// N.B., increment this if you change the format of metadata such that
5959
/// the rustc version can't be found to compare with `rustc_version()`.
60-
const METADATA_VERSION: u8 = 8;
60+
const METADATA_VERSION: u8 = 9;
6161

6262
/// Metadata header which includes `METADATA_VERSION`.
6363
///
6464
/// This header is followed by the length of the compressed data, then
65-
/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian
65+
/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian
6666
/// unsigned integer, and further followed by the rustc version string.
6767
pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
6868

src/tools/rust-analyzer/crates/proc-macro-api/src/version.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
8585
}
8686

8787
/// Check the version of rustc that was used to compile a proc macro crate's
88-
///
8988
/// binary file.
89+
///
9090
/// A proc macro crate binary's ".rustc" section has following byte layout:
9191
/// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes
9292
/// * ff060000 734e6150 is followed, it's the snappy format magic bytes,
@@ -96,8 +96,8 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
9696
/// The bytes you get after decompressing the snappy format portion has
9797
/// following layout:
9898
/// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes(again)
99-
/// * [crate root bytes] next 4 bytes is to store crate root position,
100-
/// according to rustc's source code comment
99+
/// * [crate root bytes] next 8 bytes (4 in old versions) is to store
100+
/// crate root position, according to rustc's source code comment
101101
/// * [length byte] next 1 byte tells us how many bytes we should read next
102102
/// for the version string's utf8 bytes
103103
/// * [version string bytes encoded in utf8] <- GET THIS BOI
@@ -119,14 +119,19 @@ pub fn read_version(dylib_path: &AbsPath) -> io::Result<String> {
119119
}
120120
let version = u32::from_be_bytes([dot_rustc[4], dot_rustc[5], dot_rustc[6], dot_rustc[7]]);
121121
// Last supported version is:
122-
// https://github.com/rust-lang/rust/commit/0696e79f2740ad89309269b460579e548a5cd632
123-
let snappy_portion = match version {
124-
5 | 6 => &dot_rustc[8..],
122+
// https://github.com/rust-lang/rust/commit/b94cfefc860715fb2adf72a6955423d384c69318
123+
let (snappy_portion, bytes_before_version) = match version {
124+
5 | 6 => (&dot_rustc[8..], 13),
125125
7 | 8 => {
126126
let len_bytes = &dot_rustc[8..12];
127127
let data_len = u32::from_be_bytes(len_bytes.try_into().unwrap()) as usize;
128-
&dot_rustc[12..data_len + 12]
128+
(&dot_rustc[12..data_len + 12], 13)
129129
}
130+
9 => {
131+
let len_bytes = &dot_rustc[8..16];
132+
let data_len = u64::from_le_bytes(len_bytes.try_into().unwrap()) as usize;
133+
(&dot_rustc[16..data_len + 12], 17)
134+
}
130135
_ => {
131136
return Err(io::Error::new(
132137
io::ErrorKind::InvalidData,
@@ -142,15 +147,15 @@ pub fn read_version(dylib_path: &AbsPath) -> io::Result<String> {
142147
Box::new(SnapDecoder::new(snappy_portion))
143148
};
144149

145-
// the bytes before version string bytes, so this basically is:
150+
// We're going to skip over the bytes before the version string, so basically:
146151
// 8 bytes for [b'r',b'u',b's',b't',0,0,0,5]
147-
// 4 bytes for [crate root bytes]
152+
// 4 or 8 bytes for [crate root bytes]
148153
// 1 byte for length of version string
149-
// so 13 bytes in total, and we should check the 13th byte
154+
// so 13 or 17 bytes in total, and we should check the last of those bytes
150155
// to know the length
151-
let mut bytes_before_version = [0u8; 13];
152-
uncompressed.read_exact(&mut bytes_before_version)?;
153-
let length = bytes_before_version[12];
156+
let mut bytes = [0u8; 17];
157+
uncompressed.read_exact(&mut bytes[..bytes_before_version])?;
158+
let length = bytes[bytes_before_version - 1];
154159

155160
let mut version_string_utf8 = vec![0u8; length as usize];
156161
uncompressed.read_exact(&mut version_string_utf8)?;

0 commit comments

Comments
 (0)