A streamed IPS patch built its target as the source arrived: a second
buffer the size of the ROM, taken at open, every byte of the source
copied into it, then the records laid over it, and the source freed.
The target of an IPS patch is the source with the records on it, and
the source is the content buffer the load already owns.
patch_stream_finish_into() finishes a stream whose source the caller
owns (malloc'd, given to patch_stream_set_source()): an IPS patch puts
its records on that buffer in place, growing it with realloc() only
when the target is longer, and any other format hands back its target
in the buffer's place. On failure the buffer is still the unpatched
source, as before. Fed in place, IPS copies nothing while the source
arrives; a chunk fed from elsewhere copies what came before through to
the target and carries on as before. The target is no longer taken at
open but by the first copy-through, so the in-place path never takes
one. The content load finishes its patch with finish_into().
A 16 MiB ROM with a 2000-record IPS patch, first touch: 6.7 ms ->
0.05 ms to apply, and 16 MiB less heap during the load.
The patch_stream test finishes every in-place and fallback comparison
with finish_into() on a buffer of its own, and checks a 4 MiB IPS
source is patched where it lies without being moved or copied. Leaving
the grown tail unzeroed, skipping the records in place or in the
catch-up, copying nothing in the catch-up, or copying through while fed
in place each fail it.
The content load streams a soft patch alongside the read: each span the
transfer fills is fed to the applier as it arrives. BPS and xdelta may
copy from anywhere in the source, so both kept their own growing copy of
every byte fed (BPS in src_buf, xdelta in own_src), and UPS kept the
part it had not consumed yet in its carry. The transfer buffer already
holds the whole source in one allocation that never moves, and stays
alive, detached as the content, until the patch is finished - so that
copy is a second ROM in memory, written once and read back.
patch_stream_set_source() (and vcdiff_stream_set_source() under it)
tells the stream where the source it is about to be fed lies. Chunks
fed from there are read in place: no copy, no realloc as it grows, no
memmove of the UPS carry. A chunk fed from anywhere else falls back to
the old copy, starting with what came before it, so a caller that does
not keep the source whole still gets the right answer. IPS keeps no
source and is unchanged. content_file_pump_source() sets the transfer
buffer as the source.
A 32 MiB BPS hack patch: 32 MiB less heap during the load, and
143 -> 137 ms to apply.
The patch_stream test feeds each comparison as a copy, in place, or in
place then from another buffer (freed at once, so ASan catches a stream
still reading it), and checks with glibc's heap counters that a 4 MiB
BPS source and the xdelta source are no longer copied. Dropping the
bytes the fallback copies, restarting the UPS carry at 0, reading the
wrong byte in place, or ignoring the setter each fail it.
Every trial fed its source a byte, two and three at a time, sources of
up to 8 KiB among them: some two hundred million feed calls under ASan,
most of the test's time, reaching no record boundary the seven-byte
feed does not. Those three feed sizes now go over sources of up to
1025 bytes, past the 1024-byte edges; larger ones keep the 7, 64, 1024
and whole-buffer feeds.
Measured on two CPUs: 103 s before, 54 s after; 238510 comparisons of
the 280000, all matching.
The test ran under ASan + UBSan at -O0, the slowest step of
samples-tasks. -O1, as the other samples build, keeps every check and
runs faster; and the 2 MiB buffer each of the 20000 trials built its
patches in is allocated once, as each comparison reads its own
exact-size copy of the patch, where an overread is caught.
Measured on two CPUs: 138 s before, 103 s after, the same report.
Two real bugs went through this suite untouched - the UPS and the BPS
streaming appliers both accepted patches built for a different revision
of the content, where the whole-buffer appliers refuse. Neither was
found here. Both were found by applying a patch to a real ROM through a
real build.
The reason is structural, not a missing case. The oracle was a copy of
the whole-buffer logic with the checksum validation deliberately left
out, on the reasoning that byte-identical output was what the streaming
appliers owed. So the suite compared bytes, and on a patch whose
checksums are correct - which is everything the generator makes - both
sides agreed on the bytes while disagreeing on whether the patch should
have been applied at all. No number of additional generated patches
would have closed that, because the disagreement is invisible to a
byte comparison.
Put the validation back in the oracle and compare verdicts. Then add
the cases that need it: a valid patch applied to a source one byte
different from the one it was built against, which is the ordinary way
patching goes wrong and precisely what the formats carry a source
checksum to catch. Four hundred of them, UPS and BPS, where the only
correct answer is that both appliers refuse.
Checked against the bugs it exists for: with the checksum validation
removed from the appliers again, this section reports "oracle refused,
streamed accepted" and the suite fails. With it in place everything
passes, including the 300000 output comparisons, clean under address
and undefined-behaviour sanitizers.
Running the streaming appliers against a real ROM through a real build
turned up a claim of mine that was wrong. The commit that introduced
streaming asserted that a deliberately corrupt patch would leave the
content unpatched, and offered a truncated .ips as the check. It does
not, and the check was the one format where the claim does not hold.
BPS and UPS carry checksums, and xdelta is structured enough that
damage almost always violates it, so for those three a damaged patch is
refused: finish() fails, the caller keeps the unpatched buffer, and the
content runs unpatched. That was confirmed end to end - a bit-flipped
and a truncated .xdelta each had the streamed attempt refuse, the
whole-buffer pass refuse after it, and the content load with the
source ROM's own checksum.
IPS has no checksum, no length and no framing beyond a terminator, so a
truncated one is a shorter valid one as far as the format is concerned.
Both appliers apply its intact leading records and produce a partially
patched target. That is not a regression and not a decision taken in
the streaming appliers: the whole-buffer applier has always done it,
and the two were confirmed to produce byte-identical output for exactly
this input. It is a property of the format.
Write both down - in the header, where a caller decides what to do
about a failed patch, and in the test, which deliberately does not
assert refusal because the contract that holds for every format is the
weaker one: whatever the whole-buffer applier does with a patch, the
streaming one does too.
No functional change.
Adding .xdelta to the streaming appliers left this suite testing three
of the four formats. The gap is thin - the format is a shell delegating
to the VCDIFF decoder - but thin wiring is exactly what gets connected
wrongly and never noticed, since a format that silently fell back to
the whole-buffer pass would still produce correct content.
Cover it the way its shape allows. The decoder itself is verified
against the reference implementation in samples/encodings/vcdiff, so
what is checked here is the delegation: feeding through this module's
own API, in chunks, must reach that decoder and return what decoding
the whole buffer at once returns. Five chunk sizes from one byte up,
and a feed that stops short, which must be refused rather than answered
with a target built from a partial source.
The vector is a golden patch from the reference encoder rather than a
generated one, because there is no xdelta encoder in the tree to
generate with - the same reason the vcdiff sample carries golden
vectors. It exercises an edited span, a shifted region and a run, so
the patch carries some instruction variety rather than one flat copy.
The whole suite still reports 300000 comparisons for the other three
formats with zero mismatches, and is clean under address,undefined.
samples/ is where the tree's standalone regression tests live, each with
its own Makefile - samples/tasks/bps_patch is the directly analogous
case, covering the whole-buffer .bps applier this streaming one has to
reproduce. Move the differential harness there rather than leaving it in
a one-off tasks/test directory, and give it the same Makefile shape,
including the SANITIZER= hook.
The oracle's formatting is cleaned up in the process: it arrived as
compressed prototype code and built with -Wmisleading-indentation
warnings. It is the reference the streaming appliers are checked
against, so it should read as such.
No change to what is tested: 300000 comparisons (280000 IPS/UPS, 20000
BPS), zero mismatches, clean under address,undefined via the Makefile's
sanitizer path.