[PR #5110] [MERGED] Read LZ4 region chunks as lz4-java reads them (#5064, point 7) #6079

Closed
opened 2026-11-03 06:11:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5110
Author: @k3yh
Created: 10/31/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @ludovic111

Base: main ← Head: fix/lz4-java-decoder


📝 Commits (3)

  • 0cffa9f Read LZ4 region chunks as lz4-java reads them (#5064)
  • 0e3d146 Merge main: lz4-java's decompressor inside the block-at-a-time decode
  • 5e8c60d Merge origin/main (with #5251's build fix)

📊 Changes

1 file changed (+119 additions, -4 deletions)

View changed files

📝 crates/horizon-world/src/compression.rs (+119 -4)

📄 Description

🤖 Opened by k3yh's AI agent (Claude Code, Claude Opus 5.5), not by k3yh personally.

What and why

Refs #5064, point 7 only. Points 3, 5, 6 and 8 are in #5090, #5085, #5077 and #5103. Points 1, 2 and 4 are not touched.

Vanilla 26.3 reads an LZ4 region chunk (RegionFileVersion.VERSION_LZ4) through lz4-java 1.10.1's LZ4BlockInputStream. That class decompresses each block with LZ4JavaSafeFastDecompressor, a pure-Java decoder that gives the same result on every machine, and throws "Stream is corrupted" unless the decompressor read exactly the block's compressed length. I checked both in the jar's bytecode with javap.

That decompressor has its own end conditions:

  • A sequence whose literals leave less than 8 bytes of output, or less than 8 bytes of input past them, must be the last one, and its literals must end exactly at the end of the output.
  • An offset of 0 fills the match with zeros.

Horizon decoded blocks with lz4_flex, which disagrees in both directions:

  • It loaded streams lz4-java refuses, with whatever they decoded to. The issue's repro is one: its last match ends 2 bytes before the end.
  • It refused streams lz4-java loads, such as one with an offset of 0.

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Change

compression.rs:

  • decompress_lz4_java ports LZ4JavaSafeFastDecompressor.decompress. It returns how many input bytes it read, or None where lz4-java throws LZ4Exception.
  • decode_lz4 feeds it from a buffer kept as LZ4BlockInputStream keeps compressedBuffer:
    • Each 21-byte header is read into the start of the buffer.
    • The buffer grows to max(compressed, len * 3 / 2), as a fresh zeroed array, when a block doesn't fit.
    • The block is read over its start.
    • The decompressor sees the whole buffer, so like lz4-java's it may read past the block.
  • decode_lz4 requires the decompressor to have read exactly the block, as refill does. The xxHash check is unchanged.
  • lz4_flex is still the compressor.

Vanilla reference

  • RegionFileVersion.VERSION_LZ4 → new LZ4BlockInputStream(InputStream) → LZ4Factory.fastestInstance().fastDecompressor(), which is LZ4JavaSafeFastDecompressor here. Checked with a small Java program on lz4-java 1.10.1: the factory reports LZ4Factory:JNI, fast=net.jpountz.lz4.LZ4JavaSafeFastDecompressor.
  • LZ4BlockInputStream.refill: compressedBuffer growth, decompress(compressedBuffer, 0, buffer, 0, originalLen) != compressedLen → "Stream is corrupted".
  • LZ4JavaSafeFastDecompressor.decompress(byte[], int, int, byte[], int, int).

Test

compression::tests::lz4_blocks_are_read_as_lz4_java_reads_them:

  • The issue's stream is refused. lz4-java says "Malformed input at 25"; main returned 39 bytes.
  • A short block whose literal has less than 8 bytes after it is refused. lz4-java refuses it; main returned 0303030303.
  • A block with an offset of 0 decodes to 16 zeros. lz4-java gives that; main refused it.
  • The test fails with main's decoder and passes here. All horizon-world tests pass, including all_codecs_roundtrip_and_enforce_limits.

Differential check against lz4-java 1.10.1: the same streams went through LZ4BlockInputStream and through Compression::Lz4.decode, and I compared the results (error, or the SHA-256 of the output) stream by stream.

corpus (20000 streams each) main agrees this branch agrees
Valid streams from LZ4BlockOutputStream (block sizes 64–65536, 1–3000 bytes), randomly damaged: bytes flipped, cut short, tails overwritten 20000 20000
Hand-built blocks (random literals, matches, offsets including 0 and out of range, odd endings, junk after), 1–3 per stream, each with the correct checksum for its plain decoding 16052 (2027 lz4-java refuses but main loads, 1921 the other way round) 20000

The first corpus barely tells the decoders apart, since a damaged block nearly always fails its checksum in both. The second puts every block past the checksum, so only the decoders' rules decide.

Overlap with #5077 (point 6, also mine): both change decode_lz4. #5077 makes it write into an output: &mut Vec<u8> and truncate a block that fails. Whichever merges second needs a hand merge there: keep this PR's decompress_lz4_java call and buffer, and #5077's output parameter and truncation. I'll do it.

Self-check

pr_ready (tools/pr_ready.py --quick --base 3c68c05d0 on DESKTOP-S9O4938, against 3c68c05d0 at 3c68c05d0, 2 min 34 s)

Check Result
Scope ✅ 1 files, +117 −4
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ❌ 495 run, FAILED (errors=2, skipped=1) (50 s)
rustfmt ✅ (7 s)
clippy ✅ (16 s)
tests: horizon-world ✅ 317 passed (1 min 18 s)
doc tests ✅ 0 passed (1 s)
content root tests ➖ content root untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The two Python errors come from files this PR doesn't touch, and both also failed on main on this machine:

  • test_port_ledger cannot import tests.test_class_index.
  • test_perf_bench's windows_version() gets nothing from cmd /C ver on a Spanish Windows.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/5110 **Author:** [@k3yh](https://github.com/k3yh) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/lz4-java-decoder` --- ### 📝 Commits (3) - [`0cffa9f`](https://github.com/horizon-rust/horizon/commit/0cffa9fa03e27dd3018c015cd8a9945154e7e475) Read LZ4 region chunks as lz4-java reads them (#5064) - [`0e3d146`](https://github.com/horizon-rust/horizon/commit/0e3d14647002ad30a8a5d4ae792dd6f992560912) Merge main: lz4-java's decompressor inside the block-at-a-time decode - [`5e8c60d`](https://github.com/horizon-rust/horizon/commit/5e8c60d4d34eca83451fdce160f59f6d75b67325) Merge origin/main (with #5251's build fix) ### 📊 Changes **1 file changed** (+119 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-world/src/compression.rs` (+119 -4) </details> ### 📄 Description > 🤖 Opened by k3yh's AI agent (Claude Code, Claude Opus 5.5), not by k3yh personally. ## What and why Refs #5064, point 7 only. Points 3, 5, 6 and 8 are in #5090, #5085, #5077 and #5103. Points 1, 2 and 4 are not touched. Vanilla 26.3 reads an LZ4 region chunk (`RegionFileVersion.VERSION_LZ4`) through lz4-java 1.10.1's `LZ4BlockInputStream`. That class decompresses each block with `LZ4JavaSafeFastDecompressor`, a pure-Java decoder that gives the same result on every machine, and throws "Stream is corrupted" unless the decompressor read exactly the block's compressed length. I checked both in the jar's bytecode with `javap`. That decompressor has its own end conditions: - A sequence whose literals leave less than 8 bytes of output, or less than 8 bytes of input past them, must be the last one, and its literals must end exactly at the end of the output. - An offset of 0 fills the match with zeros. Horizon decoded blocks with `lz4_flex`, which disagrees in both directions: - It **loaded** streams lz4-java refuses, with whatever they decoded to. The issue's repro is one: its last match ends 2 bytes before the end. - It **refused** streams lz4-java loads, such as one with an offset of 0. ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [x] bug: a crash or a breakage - [ ] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Change `compression.rs`: - `decompress_lz4_java` ports `LZ4JavaSafeFastDecompressor.decompress`. It returns how many input bytes it read, or `None` where lz4-java throws `LZ4Exception`. - `decode_lz4` feeds it from a buffer kept as `LZ4BlockInputStream` keeps `compressedBuffer`: - Each 21-byte header is read into the start of the buffer. - The buffer grows to `max(compressed, len * 3 / 2)`, as a fresh zeroed array, when a block doesn't fit. - The block is read over its start. - The decompressor sees the whole buffer, so like lz4-java's it may read past the block. - `decode_lz4` requires the decompressor to have read exactly the block, as `refill` does. The xxHash check is unchanged. - `lz4_flex` is still the compressor. ## Vanilla reference - `RegionFileVersion.VERSION_LZ4` → `new LZ4BlockInputStream(InputStream)` → `LZ4Factory.fastestInstance().fastDecompressor()`, which is `LZ4JavaSafeFastDecompressor` here. Checked with a small Java program on lz4-java 1.10.1: the factory reports `LZ4Factory:JNI`, `fast=net.jpountz.lz4.LZ4JavaSafeFastDecompressor`. - `LZ4BlockInputStream.refill`: `compressedBuffer` growth, `decompress(compressedBuffer, 0, buffer, 0, originalLen) != compressedLen` → "Stream is corrupted". - `LZ4JavaSafeFastDecompressor.decompress(byte[], int, int, byte[], int, int)`. ## Test `compression::tests::lz4_blocks_are_read_as_lz4_java_reads_them`: - The issue's stream is refused. lz4-java says "Malformed input at 25"; main returned 39 bytes. - A short block whose literal has less than 8 bytes after it is refused. lz4-java refuses it; main returned `0303030303`. - A block with an offset of 0 decodes to 16 zeros. lz4-java gives that; main refused it. - The test fails with main's decoder and passes here. All `horizon-world` tests pass, including `all_codecs_roundtrip_and_enforce_limits`. **Differential check against lz4-java 1.10.1:** the same streams went through `LZ4BlockInputStream` and through `Compression::Lz4.decode`, and I compared the results (error, or the SHA-256 of the output) stream by stream. | corpus (20000 streams each) | main agrees | this branch agrees | |---|---|---| | Valid streams from `LZ4BlockOutputStream` (block sizes 64–65536, 1–3000 bytes), randomly damaged: bytes flipped, cut short, tails overwritten | 20000 | 20000 | | Hand-built blocks (random literals, matches, offsets including 0 and out of range, odd endings, junk after), 1–3 per stream, each with the **correct** checksum for its plain decoding | **16052** (2027 lz4-java refuses but main loads, 1921 the other way round) | **20000** | The first corpus barely tells the decoders apart, since a damaged block nearly always fails its checksum in both. The second puts every block past the checksum, so only the decoders' rules decide. **Overlap with #5077 (point 6, also mine):** both change `decode_lz4`. #5077 makes it write into an `output: &mut Vec<u8>` and truncate a block that fails. Whichever merges second needs a hand merge there: keep this PR's `decompress_lz4_java` call and buffer, and #5077's output parameter and truncation. I'll do it. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base 3c68c05d0` on DESKTOP-S9O4938, against 3c68c05d0 at 3c68c05d0, 2 min 34 s) | Check | Result | | --- | --- | | Scope | ✅ 1 files, +117 −4 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ❌ 495 run, FAILED (errors=2, skipped=1) (50 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (16 s) | | tests: horizon-world | ✅ 317 passed (1 min 18 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The two Python errors come from files this PR doesn't touch, and both also failed on main on this machine: - `test_port_ledger` cannot import `tests.test_class_index`. - `test_perf_bench`'s `windows_version()` gets nothing from `cmd /C ver` on a Spanish Windows. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:11:28 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#6079
No description provided.