Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a
file.symbols().find(|sym| sym.name() == Ok(AIX_METADATA_SYMBOL_NAME))
{
let offset = metadata_symbol.address() as usize;
if offset < 8 {
// The offset specifies the location of rustc metadata in the .info section of XCOFF.
// Each string stored in .info section of XCOFF is preceded by a 4-byte length field.
if offset < 4 {
return Err(format!("Invalid metadata symbol offset: {offset}"));
}
// The offset specifies the location of rustc metadata in the comment section.
// The metadata is preceded by a 8-byte length field.
let len = u64::from_le_bytes(info_data[(offset - 8)..offset].try_into().unwrap()) as usize;
// XCOFF format uses big-endian byte order.
let len = u32::from_be_bytes(info_data[(offset - 4)..offset].try_into().unwrap()) as usize;
if offset + len > (info_data.len() as usize) {
return Err(format!(
"Metadata at offset {offset} with size {len} is beyond .info section"
Expand Down Expand Up @@ -478,9 +479,12 @@ pub fn create_wrapper_file(
file.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
file.section_mut(section).flags =
SectionFlags::Xcoff { s_flags: xcoff::STYP_INFO as u32 };

let len = data.len() as u64;
let offset = file.append_section_data(section, &len.to_le_bytes(), 1);
// Encode string stored in .info section of XCOFF.
// FIXME: The length of data here is not guaranteed to fit in a u32.
// We may have to split the data into multiple pieces in order to
// store in .info section.
let len: u32 = data.len().try_into().unwrap();
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
// Add a symbol referring to the data in .info section.
file.add_symbol(Symbol {
name: AIX_METADATA_SYMBOL_NAME.into(),
Expand Down Expand Up @@ -599,12 +603,12 @@ pub fn create_compressed_metadata_file_for_xcoff(
section: SymbolSection::Section(data_section),
flags: SymbolFlags::None,
});
let len = data.len() as u64;
let offset = file.append_section_data(section, &len.to_le_bytes(), 1);
let len = data.len() as u32;
Copy link
Member

@saethlin saethlin Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length of data here is not guaranteed to fit in a u32. Ideally we'd produce a compile error here, but it's not clear to me if we have access to a TyCtxt to do that. At the very least we should use .try_into().unwrap() to ICE early instead of producing a truncated metadata section.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least there is a session:

return create_compressed_metadata_file_for_xcoff(file, &packed_metadata, symbol_name);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzEq even though we panic above, this should also be .try_into().unwrap()

let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
// Add a symbol referring to the rustc metadata.
file.add_symbol(Symbol {
name: AIX_METADATA_SYMBOL_NAME.into(),
value: offset + 8, // The metadata is preceded by a 8-byte length field.
value: offset + 4, // The metadata is preceded by a 4-byte length field.
size: 0,
kind: SymbolKind::Unknown,
scope: SymbolScope::Dynamic,
Expand Down