Container: functions of a shared-module library may share one empty metadata record
macOS 11's own Core Image libraries (file type 1, format 2.5; reims's 11.7.11 guests, 118977 and 320632 bytes) name one 8-byte private-metadata record, a size word and ENDT, from every function, over the one module they share; the parser refused the second function's entry with BadOffset (at 245 and 223), so the guest's Core Image got "Invalid library file". In a library whose functions share a module (Core Image kernel, dynamic), a function may now name exactly the record the previous one did when it holds no tags; a shared record holding a tag, and any sharing in an executable library, stay BadOffset, so parsing stays linear in the file size. Test: a 2.5 Core Image library sharing one empty private record parses with no private tags; the tagged and executable variants are refused. (cherry picked from commit 200ca7984d84dde9aba214115e4df5745ade5123)
This commit is contained in:
1 parent
563ddcdaab
commit
dcaf3085a0
2 files changed
+71
-7
No files matched your search
@@ -32,7 +32,10 @@
|
||||
//! toolchain lists as the symbols it exports (`TYPE` 3, each as `NAME`) over the library's one
|
||||
//! module, and a Core Image kernel library (`metallib -cikernel`), whose kernels it lists as
|
||||
//! extern functions (`TYPE` 5) over the one module `metal -fcikernel` compiled: their functions
|
||||
//! may share exactly one bitcode range, hashed once.
|
||||
//! may share exactly one bitcode range, hashed once. Their functions may also share one empty
|
||||
//! metadata record (its size word and `ENDT`, no tags), as macOS 11's own Core Image libraries
|
||||
//! (format 2.5) share one 8-byte private-metadata record among all their functions. A shared
|
||||
//! record holding any tag stays refused, so the result stays linear in the file size.
|
||||
//! - The extended header's `HDYN` and `ILST` tags each hold an `(offset, size)` of a region of
|
||||
//! the file. `HDYN`'s is a tag list: `NAME`, the library's install name (a dynamic library's
|
||||
//! `installName`; an executable's file name), and one `DYNL` per dynamic library it was linked
|
||||
@@ -397,17 +400,19 @@ pub(super) fn parse(bytes: &[u8]) -> Result<Metallib<'_>, Error> {
|
||||
}
|
||||
}
|
||||
|
||||
// Metadata records are parsed only after their offsets are known to be disjoint, so each
|
||||
// record's bytes are read (and its tags held) once.
|
||||
// Metadata records are parsed only after their offsets are known to be disjoint (or one
|
||||
// shared empty record), so each record's bytes are read (and its tags held) once.
|
||||
let public = records(
|
||||
file,
|
||||
header.public_metadata,
|
||||
parsed.iter().map(|p| (p.public_at, p.entry)),
|
||||
shared,
|
||||
)?;
|
||||
let private = records(
|
||||
file,
|
||||
header.private_metadata,
|
||||
parsed.iter().map(|p| (p.private_at, p.entry)),
|
||||
shared,
|
||||
)?;
|
||||
let mut functions = Vec::with_capacity(parsed.len());
|
||||
for ((p, public), private) in parsed.into_iter().zip(public).zip(private) {
|
||||
@@ -620,12 +625,15 @@ fn function<'a>(
|
||||
|
||||
/// The metadata records in `section` at the given `(offset, entry)` pairs, one per function and in
|
||||
/// the same order. Records are read in file order, and one that starts before the previous record
|
||||
/// ends (including a shared record) is `BadOffset` at the later function's entry. The records are
|
||||
/// therefore disjoint, and the total work and the returned tags are linear in the section size.
|
||||
/// ends (including a shared record) is `BadOffset` at the later function's entry, except that
|
||||
/// where `share_empty` (a library whose functions share one module) a function may name exactly
|
||||
/// the record the previous one did when that record holds no tags. The records are therefore
|
||||
/// disjoint or empty, and the total work and the returned tags are linear in the section size.
|
||||
fn records<'a>(
|
||||
file: &'a [u8],
|
||||
section: Section,
|
||||
offsets: impl Iterator<Item = (u64, u64)>,
|
||||
share_empty: bool,
|
||||
) -> Result<Vec<Vec<Tag<'a>>>, Error> {
|
||||
let mut order: Vec<(u64, u64, usize)> = offsets
|
||||
.enumerate()
|
||||
@@ -634,12 +642,18 @@ fn records<'a>(
|
||||
order.sort_unstable();
|
||||
let mut out: Vec<Option<Vec<Tag<'a>>>> = vec![None; order.len()];
|
||||
let mut end = 0;
|
||||
// The start of the last record read, where it held no tags.
|
||||
let mut empty_at = None;
|
||||
for (at, entry, i) in order {
|
||||
if share_empty && empty_at == Some(at) {
|
||||
continue; // The shared empty record: this function's metadata is no tags.
|
||||
}
|
||||
if at < end {
|
||||
return Err(container_error(ContainerError::BadOffset, entry));
|
||||
}
|
||||
let (list, list_end) = metadata(file, section, at, entry)?;
|
||||
end = list_end - section.offset;
|
||||
empty_at = list.is_empty().then_some(at);
|
||||
if let Some(slot) = out.get_mut(i) {
|
||||
*slot = Some(list);
|
||||
}
|
||||
|
||||
@@ -83,6 +83,9 @@ struct Builder {
|
||||
inclusive_md_size: bool,
|
||||
/// Every function's bitcode is the first function's, written once (a dynamic library).
|
||||
shared_bitcode: bool,
|
||||
/// Every function's private-metadata record is the first function's, written once (macOS 11's
|
||||
/// Core Image libraries, where it is empty).
|
||||
shared_private: bool,
|
||||
/// Extended-header tags holding the `(offset, size)` of a region written after the trailer,
|
||||
/// with the region's bytes (`HDYN`, `ILST`).
|
||||
regions: Vec<([u8; 4], Vec<u8>)>,
|
||||
@@ -98,6 +101,7 @@ impl Builder {
|
||||
trailer: vec![],
|
||||
inclusive_md_size: false,
|
||||
shared_bitcode: false,
|
||||
shared_private: false,
|
||||
regions: vec![],
|
||||
}
|
||||
}
|
||||
@@ -119,9 +123,16 @@ impl Builder {
|
||||
out.extend(list);
|
||||
out
|
||||
};
|
||||
let (pub_at, priv_at) = (public.len(), private.len());
|
||||
let pub_at = public.len();
|
||||
public.extend(md(&f.public));
|
||||
private.extend(md(&f.private));
|
||||
let priv_at = match (self.shared_private, private.is_empty()) {
|
||||
(true, false) => 0,
|
||||
_ => {
|
||||
let at = private.len();
|
||||
private.extend(md(&f.private));
|
||||
at
|
||||
}
|
||||
};
|
||||
let bc_at = match (self.shared_bitcode, bitcode.is_empty()) {
|
||||
(true, false) => 0,
|
||||
_ => {
|
||||
@@ -693,6 +704,45 @@ fn a_core_image_library_hash_is_kept_as_read() {
|
||||
);
|
||||
}
|
||||
|
||||
/// macOS 11's own Core Image libraries (format 2.5) name one empty private-metadata record (its
|
||||
/// size word and `ENDT`) from every function, over the one module they share. That shared empty
|
||||
/// record is read as no tags; a shared record holding a tag, or one shared in an executable
|
||||
/// library, stays refused.
|
||||
#[test]
|
||||
fn a_shared_module_library_shares_one_empty_metadata_record() {
|
||||
let names = ["tint", "shift", "invert"];
|
||||
let module = FnSpec::new("m", 5, 4).bitcode;
|
||||
let mut b = core_image(&names, sha256(&module));
|
||||
b.version = (2, 5);
|
||||
b.shared_private = true;
|
||||
let bytes = b.build();
|
||||
let lib = metallib(&bytes);
|
||||
let read: Vec<_> = lib.functions.iter().map(|f| f.name).collect();
|
||||
assert_eq!(read, names);
|
||||
for f in &lib.functions {
|
||||
assert!(f.private.is_empty(), "{}: no private tags", f.name);
|
||||
assert_eq!(f.bitcode, module.as_slice());
|
||||
}
|
||||
let refused = |b: &Builder, what: &str| {
|
||||
let err = parse(&b.build()).unwrap_err();
|
||||
assert_eq!(
|
||||
container_kind(&err),
|
||||
ContainerError::BadOffset,
|
||||
"{what}: {err:?}"
|
||||
);
|
||||
};
|
||||
let mut tagged = b.clone();
|
||||
tagged.functions[0].private.push((*b"PRIV", vec![9]));
|
||||
refused(&tagged, "a shared record holding a tag");
|
||||
let mut executable = b.clone();
|
||||
executable.file_type = 0;
|
||||
executable.shared_bitcode = false;
|
||||
refused(
|
||||
&executable,
|
||||
"a shared empty record in an executable library",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_versions_and_library_kinds() {
|
||||
let mut b = sample();
|
||||
|
||||
Reference in new issue
Block a user