fix(cli): hash only the embedded DXBC container
Validate and use the declared DXBC byte extent when scanning an embedded shader. Keep the borrowed slice and offset, and leave packed inputs unchanged. Tested on Windows with the focused embedded-container regression and a synthetic CLI run.
This commit is contained in:
1 parent
c437b0594b
commit
b1369afb40
1 file changed
+28
-4
@@ -43,15 +43,39 @@ pub(crate) fn sources(data: &[u8]) -> Vec<Source<'_>> {
|
||||
out.extend(sgd5::records(data).map(Source::Packed));
|
||||
}
|
||||
let raw = memchr::memmem::find_iter(data, b"DXBC")
|
||||
.filter(|&offset| Container::parse(&data[offset..]).is_ok())
|
||||
.map(|offset| Source::Raw {
|
||||
offset,
|
||||
data: &data[offset..],
|
||||
.filter_map(|offset| {
|
||||
let tail = &data[offset..];
|
||||
// DXBC total size, validated below. Hash the container, not the
|
||||
// rest of its carrier, so offline warming uses the runtime key.
|
||||
let size = u32::from_le_bytes(tail.get(0x18..0x1c)?.try_into().ok()?) as usize;
|
||||
let data = tail.get(..size)?;
|
||||
Container::parse(data).ok()?;
|
||||
Some(Source::Raw { offset, data })
|
||||
});
|
||||
out.extend(raw);
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::sources;
|
||||
|
||||
#[test]
|
||||
fn embedded_containers_keep_runtime_hashes() {
|
||||
// A 32-byte DXBC with no parts; the scan should not add carrier bytes.
|
||||
let shader = b"DXBC\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x20\0\0\0\0\0\0\0";
|
||||
let first = [b"MZ".as_slice(), shader, b"first suffix"].concat();
|
||||
let second = [b"PE header".as_slice(), shader, b"other suffix", b"DXBC"].concat();
|
||||
let runtime_hash = blake3::hash(shader);
|
||||
for (carrier, label) in [(&first, "@0x2"), (&second, "@0x9")] {
|
||||
let entries = sources(carrier);
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].label(), label);
|
||||
assert_eq!(blake3::hash(&entries[0].bytes().unwrap()), runtime_hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Totals {
|
||||
containers: usize,
|
||||
|
||||
Reference in new issue
Block a user