SGD5: a marker inside a record's packed bytes is not another record #273
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!273
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/273/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@SkippySteve on GitHub · 2026-11-24 01:58 UTC · original
SkippySteve/sgd5-nested-marker→main· +4677 −27 · 8 files · 72 commits · closed without mergingsgd5::recordsmatched every0x000DDCE7marker in the file, including ones inside an already-found record's packed bytes. Deflate stored blocks carry the container's bytes as they are, so a container that holds a record-shaped run of bytes was reported as an extra record (and the shader CLI would try to unpack it).The search now resumes after each record's packed bytes. A marker that does not form a record still advances by one byte, as before.
Game tests: every known build's hash is one
identify()can match (was #303)KNOWN_BUILDSis matched against the hex BLAKE3 thatrun.rscomputes, so an entry with a typo, a truncated hash, stray whitespace or a duplicate would silently never identify the build it names. Three tests incrates/victory-game/src/lib.rscheck that each entry is 64 lowercase hex digits with unique label and hash, thatidentify()finds each build by its own hash in either case, and that an unknown digest, an empty string or non-hex text identifies nothing. The list is empty today, so the first two guard future entries. Test-only; no game data.This PR now carries #303, which is closed in its favour.
A record's header cannot sit inside the previous record
After a record, the search resumed right at the end of its packed bytes, so a marker found there read its NUL, kind and
nfrom the last bytes of the previous record (and its name from further back). Bytes after the end of a deflate stream are not checked, so this produced a phantom record. The search now starts 6 bytes after the previous record (or the magic), and a record's header and name are read only from bytes after it.header_inside_a_record_is_not_another_record: before[Some("PS_first"), None], after[Some("PS_first")].empty_container_unpacksanddamaged_file_yields_disjoint_records(every single-byte change to a two-record file, 8 values each: no panic, records in order, inside the file, not overlapping).The shader survey does not count a stored container twice
Deflate stored blocks carry a record's container as it is, so
victory shader's rawDXBCscan (apps/victory/src/shader.rs,sources) found it inside the record's packed bytes and counted it again, next to the unpacked record.sgd5::Record::span(marker to end of packed stream) andsgd5::in_record(binary search over the records, which are in file order and disjoint) let the scan skip those offsets. Containers between or after records, and in files with no SGD5 magic, are still found as before.offsets_inside_records: a level-0 record holding aDXBCrun, thenDXBCbetween and after records. The stored one is inside a record; the magic, the gap, the tail and each record's header are not.A truncated record does not expose the records it stores
A record whose packed bytes run past the end of the file (a cut-off file) was dropped, but the search then went on one byte at a time through its bytes, so a record stored as is inside it (deflate stored blocks) was found and reported. The search now ends at such a record: every byte after its marker belongs to it. Sizes are added with
checked_add.truncated_record_hides_the_records_it_stores: the file cut right after the stored inner record. Before[PS_first, PS_inner], after[PS_first].A marker with sizes deflate cannot produce is not a record
Bytes between records (a container stored as is, say) that read as a marker with a huge packed size were taken for a truncated record, and a truncated record ends the search, so every record after them was lost. A record's packed size may now be at most what deflate can take for its stated unpacked size (9 bits a byte for fixed codes, which also covers stored blocks' 5 bytes per 65535) plus 4096 bytes of slack, since the stream's end is not checked.
marker_with_impossible_sizes_does_not_end_the_search:FFFFFFFF E7DC0D00 FFFFFFFF 10000000before a record. Before[], after[PS_after].packed_size_bound_holds_for_every_level: incompressible input of 0 to 200000 bytes at miniz levels 0 to 10 never exceeds the bound.A marker running past the end is truncated only if a deflate stream starts there
Bytes between records (a container stored as is, say) that read as a marker with sizes that fit the bound above, but whose packed bytes run past the end of the file, were still taken for a truncated record, which ends the search and loses every record after them. Such a record now counts as truncated only if its bytes so far are a deflate stream of the stated unpacked size: inflating them runs out of input mid-stream, or the stream ends having produced exactly that many bytes. Otherwise the marker is not a record and the search goes on one byte later. A truncated record that is a real one (a cut-off file) still ends the search as before.
marker_running_past_the_end_without_a_stream_does_not_end_the_search: a 64 KiB/64 KiB marker followed by a reserved deflate block type, between two records. Before[PS_first], after[PS_first, PS_after].truncated_stream_is_told_from_noise: every prefix of a 4000-byte stream at miniz levels 0, 1, 6 and 10 starts a stream; the whole stream does only for its own size; a reserved block type and a stored block whose length check fails do not.Seeded random files yield exactly the planted records
New
random_files_yield_exactly_the_planted_records(200 seeds): 1 to 6 records with gaps of noise and near-miss markers (sizes that disagree, sizes no deflate stream could have) between them, half of them stored as is (level 0) and holding a record and aDXBCmagic of their own.records()returns exactly the planted records, with their offsets, names and containers, andin_record()agrees with a linear scan of their spans at every offset. Reverting the skip over packed bytes or the packed-size bound makes it fail. Test-only.An empty name is no name
A length byte of 1 right before the NUL read as the name
"", so any01 00before a record's kind (noise, or a nameless record) named itSome(""), andvictory shaderlabelled it with nothing instead of?. Such a name is nowNone.empty_name_is_no_name: before[Some(""), Some("PS_named")], after[None, Some("PS_named")].archive_with_two_sgd5_files_yields_both: two SGD5 files in one archive, the second magic in a gap between them; both files' records are found. Test-only.The shader survey does not count a container kept in another one twice
victory shader's rawDXBCscan accepted every magic that parses, so a container kept in a part of another container stored as is (outside any SGD5 record) was counted a second time. Newvictory_shader::dxbc::find_allreturns containers stored as is, in file order, bounded to their stated size, and skips a magic that lies inside a found container's parts. Only parts count: a magic in junk right before a real container can read its size and part count from the real header and parse with no parts, and it must not hide the real one.shader.rssourcesuses it, passingsgd5::in_recordas the skip.victory-shadernow depends on the workspacememchr.container_in_a_part_is_not_another_container: a container holding another in anILDBpart, then one after a gap. Found: the outer and the last; skipping the outer exposes the inner.header_reading_into_a_container_does_not_hide_itandfinds_nothing_in_noise.cargo test -p victory-shader(59) passes;cargo xwin check/clippy -p victory --target x86_64-pc-windows-msvcare clean.Records and the raw scan, tested together as the survey runs them
victory shader'ssourcescombinessgd5::records,sgd5::in_recordanddxbc::find_all, but the CLI does not build on Linux, so nothing ran that combination. Newcrates/victory-game/tests/survey.rs(victory-shaderas a dev-dependency) does the same thingsourcesdoes:stored_record_container_is_counted_once: a level-0 record's container counts as the record only, not also as a raw container; the container between records is raw; without the SGD5 magic the stored container is found as is.random_archives_survey_each_container_once(200 seeds): another file's container before the magic, records (some stored as is, holding a container, a record and a container of their own), and gaps with noise, magics that do not parse, and containers (some keeping another in a part). Exactly the planted records and top-level raw containers are found, each once, and no raw container overlaps a record.|_| falseas the skip, or droppingfind_all's part check, makes them fail. Test-only; synthetic bytes.cargo test -p victory-game(20 + 2) andcargo clippy -p victory-game --all-targets -- -D warningspass.A marker with too few packed bytes for its size is not a record
A marker whose packed size is less than any deflate stream of its stated unpacked size needs (2 bytes, and no fewer than unpacked / 1032) was yielded as a record that never unpacks, and with any packed bytes its span covered the next record's length byte, so that record lost its name.
packed_size_lower_bound_holds_for_every_level(zeros up to 4 MiB at every miniz level),marker_with_too_few_packed_bytes_does_not_hide_the_next_record(before[PS_first, None, PS_after]or[PS_first, None, None], after[PS_first, PS_after]),smallest_containers_are_records.Bytes that are no deflate stream do not hide the record they cover
Bytes between records that read as a marker with sizes inside both bounds and inside the file were yielded as a corrupt record, and the search resumed after their packed bytes, so a real record whose header they covered was lost. If a record whose packed bytes are a deflate stream starts inside such a marker's span, it now wins and the marker is skipped. A corrupt record that covers no other record is still yielded (
unpack()isNone), sovictory shaderstill reports it. Streams are inflated only when a span covers another record's header.marker_without_a_stream_does_not_hide_the_record_it_covers: before[PS_first, None], after[PS_first, PS_after]. Disabling the check makes it fail.corrupt_record_is_still_a_record: a damaged record and the one after it are both yielded.cargo test -p victory-game(25 + 2) andcargo clippy -p victory-game --all-targets -- -D warningspass.Markers laid over one another are each looked at once
A marker whose span covers a stream record, but whose own bytes are no stream, is skipped and the search goes on one byte later. Each such marker scanned its whole span again and inflated every stream record it found, so a file with many markers that each cover the rest of the file took n² work or more: 8000 of them (150 KB) took 43 s.
records()now remembers which markers start stream records as the search moves forward through the file, so each marker is looked at once. The records found are unchanged (all earlier tests, including the random-file test, pass as before).nested_markers_without_streams_take_linear_work: 8000 such markers, then a real record. Before: 43 s (fails its 5 s bound); after: 0.03 s, and onlyPS_realis found.cargo test -p victory-game(26 + 2) andcargo clippy -p victory-game --all-targets -- -D warningspass.Chained markers do not make the stream check read the whole file
Every marker that might be a record has its bytes checked for the start of a deflate stream (when its packed bytes run past the end, when it covers another record, and for each record it covers). Stored blocks that each end where the next marker's stored block begins make every marker's bytes a valid stream through all the markers after it, so each check inflated the rest of the chain: 16000 markers (1 MB) took 2.5 s, 32000 (2 MB) took 9 s, growing as n².
starts_streamnow reads at most 4096 packed bytes and makes at most 4096 unpacked ones (PROBE). A stream that ends inside the probe must still have the stated size; one that goes on past it counts as a stream.chained_markers_inside_the_file_take_linear_work: 32000 markers whose packed bytes end at the end of the file. Before: 9 s (fails its 5 s bound); after: milliseconds, same result (one corrupt record spanning them).chained_markers_past_the_end_take_linear_work: the same chain with packed sizes past the end. Before: 9 s; after: milliseconds. Trade-off: the chain is valid deflate for far longer than the probe, so its first marker is now taken for a truncated record and ends the search (before, the record after the chain was found, after 9 s). Noise that is no deflate stream within 4 KiB still does not end the search (marker_running_past_the_end_without_a_stream_does_not_end_the_searchis unchanged).cargo test -p victory-game(28 + 2),cargo clippy -p victory-game --all-targets -- -D warningsandcargo fmt --checkpass.Linear-work tests count work instead of timing it
The three linear-work tests bounded
records()by wall clock (5 s), so a slow or loaded machine could fail them and a fast one could hide a regression. A test-only, per-thread counter instarts_streamnow records the stream checks made and the packed bytes they read, and the tests bound those: at most two checks per marker, each reading at mostPROBEbytes. The code under test is unchanged.Streamsmemo disabled,nested_markers_without_streams_take_linear_workmakes 31.6M checks for 8000 markers (bound 16002) and fails; with the probe removed, both chained-marker tests read about 2 MB per check and fail.cargo test -p victory-game(28 + 2),cargo clippy -p victory-game --all-targets -- -D warningsandcargo fmt --checkpass.Test
New
marker_inside_a_record_is_not_another_record: a synthetic record whose payload is another record, packed at level 0 (stored blocks), followed by a normal record. Before:[PS_outer, PS_inner, PS_after]; after:[PS_outer, PS_after].cargo test -p victory-game(20 tests) andcargo clippy -p victory-game --all-targets -- -D warningspass on Linux.victory(CLI) does not build on Linux onmain(victory-gpuhost_resource), so theshader.rschange was checked withcargo check -p victory --target x86_64-pc-windows-msvc.A printable byte before a long name is not its length byte
From 32 characters on, a name's length byte is printable, so
name_beforereads back through the bytes before it too (the previous record's packed tail, say). It took the earliest byte equal to its distance to the NUL, so a stray byte there (one in about 256 for names right after random bytes) made the name read as.PS_.... Shader names are identifiers, and a length byte for 32–46 characters is punctuation, so the earliest length byte whose name is all letters, digits and_now wins; failing that, the earliest, as before. (Taking the nearest instead would misread names of 48+ characters holding a character equal to its distance, like the4inPS_4_0_www….)printable_bytes_before_a_long_name_are_not_part_of_it: a 45-character name after/,0A,x.. Before:Some(".PS_DeferredLightingComposite_0011aabb_Wrapped"); after: the name. Also a 107-character name andPS_4_0_+ 48w, read whole.cargo test -p victory-game(29 + 2),cargo clippy -p victory-game --all-targets -- -D warningsandcargo fmt --checkpass.A name does not start with its length byte
A name of 47 to 56 characters has a digit (
0–9) for its length byte, so a stray byte right before it that equals its distance to the NUL (one in 256 after random bytes) read as a length byte whose name, the digit then the real name, is all letters, digits and_, and won as the earliest:0PS_www…. Names are identifiers, which do not start with a digit, so such a candidate is no longer taken for one. (Names of 64+ characters have a letter for a length byte, which this does not tell apart.)a_name_does_not_start_with_its_length_byte: names of 47, 49 and 56 characters after such a byte. Before:Some("0PS_…"); after: the name.cargo test -p victory-game(30 + 2),cargo clippy -p victory-game --all-targets -- -D warningsandcargo fmt -p victory-game --checkpass.Bytes without a stream over a cut-off record hide nothing
A marker with sizes that fit, followed by bytes that are no deflate stream, laid over the header of a record that the end of the file cuts off, was not counted as covering a record, because only whole records counted. So it was yielded as a corrupt record, and the search went on inside the cut-off record and found the records it stores as is. The whole file yields neither of them. A cut-off record whose bytes so far are a deflate stream now counts as covered, so the phantom is skipped and the cut-off record ends the search, as it does without the phantom.
marker_without_a_stream_over_a_cut_off_record_hides_nothing: whole file[PS_first, PS_outer]; cut after the stored inner record, before[PS_first, None, PS_inner], after[PS_first].cargo test -p victory-game(32 + 2),cargo clippy -p victory-game --all-targets -- -D warningsandcargo fmt -p victory-game --checkpass.Names of every length, the magic in a name, and records in an archive
Test-only: three hermetic synthetic tests in
crates/victory-game/src/sgd5.rsthat pin down behaviour the search already has.every_name_length_a_byte_can_state_is_read_whole: 254 back-to-back records named with 1 to 254 characters. Their length bytes run through control bytes, punctuation, digits, letters and bytes past 0x7F. Each name is read whole and each container unpacks.magic_in_a_name_is_part_of_the_name: records namedPS_SGD5_WrappedandSGD5after the file's magic. They keep their whole names, and the search does not restart at the magic in a name.records_in_an_archive_are_the_bare_files_shifted: the same three-record file after 0 to 40 bytes of zeros, printable length-byte look-alikes or marker bytes. It yields the same records, with every offset and span moved by the prefix length and the same names, kinds and containers.A length byte of 1 followed by the NUL read as the name "", so any `01 00` before a record's kind (noise, or a nameless record) gave it Some(""), and the shader survey labelled it with an empty name instead of "?". Such a name is now None. Also test that an archive holding two SGD5 files, the second magic in a gap, yields the records of both.@jeremygold02 on GitHub · 2026-11-24 04:11 UTC · original · edited 2026-11-24 04:13 UTC
Tested PR #273 at head
7c033d911d1deda65306692dc0e94beed1507b9b(state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at7c033d911d1deda65306692dc0e94beed1507b9b.cargo test --locked -p victory-game— passed (25 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out); cases includedsgd5::tests::marker_running_past_the_end_without_a_stream_does_not_end_the_search,sgd5::tests::reads_records,sgd5::tests::empty_container_unpacks,sgd5::tests::corrupt_stream_does_not_unpack, and 21 others.cargo test --locked -p victory-shader container_in_a_part_is_not_another_container— passed (1 passed; 0 failed; 71 filtered out):dxbc::tests::container_in_a_part_is_not_another_container.Behavior covered: SGD5 record spans remain disjoint, and the DXBC survey does not count containers inside packed records. Relevant code: sgd5.rs, dxbc.rs, shader.rs.
Pull request closed