diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 019ec0758d6..4ab56699922 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -306,7 +306,13 @@ pub fn create_compressed_metadata_file( symbol_name: &str, ) -> Vec { let mut compressed = rustc_metadata::METADATA_HEADER.to_vec(); + // Our length will be backfilled once we're done writing + compressed.write_all(&[0; 4]).unwrap(); FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap(); + let meta_len = rustc_metadata::METADATA_HEADER.len(); + let data_len = (compressed.len() - meta_len - 4) as u32; + compressed[meta_len..meta_len + 4].copy_from_slice(&data_len.to_be_bytes()); + let Some(mut file) = create_object_file(sess) else { return compressed.to_vec(); }; diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 755a2425350..c48e681eb94 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -789,6 +789,9 @@ fn get_metadata_section<'p>( loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?; // The header is uncompressed let header_len = METADATA_HEADER.len(); + // header + u32 length of data + let data_start = header_len + 4; + debug!("checking {} bytes of metadata-version stamp", header_len); let header = &buf[..cmp::min(header_len, buf.len())]; if header != METADATA_HEADER { @@ -798,8 +801,14 @@ fn get_metadata_section<'p>( ))); } + // Length of the compressed stream - this allows linkers to pad the section if they want + let Ok(len_bytes) = <[u8; 4]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())]) else { + return Err(MetadataError::LoadFailure("invalid metadata length found".to_string())); + }; + let compressed_len = u32::from_be_bytes(len_bytes) as usize; + // Header is okay -> inflate the actual metadata - let compressed_bytes = &buf[header_len..]; + let compressed_bytes = &buf[data_start..(data_start + compressed_len)]; debug!("inflating {} bytes of compressed metadata", compressed_bytes.len()); // Assume the decompressed data will be at least the size of the compressed data, so we // don't have to grow the buffer as much. diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index a7ec2d790b7..1d2541a6788 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -55,13 +55,13 @@ pub(crate) fn rustc_version() -> String { /// Metadata encoding version. /// N.B., increment this if you change the format of metadata such that /// the rustc version can't be found to compare with `rustc_version()`. -const METADATA_VERSION: u8 = 6; +const METADATA_VERSION: u8 = 7; /// Metadata header which includes `METADATA_VERSION`. /// -/// This header is followed by the position of the `CrateRoot`, -/// which is encoded as a 32-bit big-endian unsigned integer, -/// and further followed by the rustc version string. +/// This header is followed by the length of the compressed data, then +/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian +/// unsigned integer, and further followed by the rustc version string. pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION]; /// A value of type T referred to by its absolute position