[GH-ISSUE #5064] Eight byte-lvl differences from vanilla: NBT, region files, Gaussian random #1541

Open
opened 2026-11-02 15:47:49 +07:00 by bsakaguchi · 11 comments
Owner

Originally created by @mmonfox on GitHub (Oct 31, 2026).
Original GitHub issue: https://github.com/horizon-rust/horizon/issues/5064

Originally assigned to: @k3yh on GitHub.

This is an AI-written report, reviewed by me (mmonfox). Some of these aren’t severe, just inconsistencies with vanilla that could bite later. The ones that matter most are 6 and 7 (chunks vanilla loads fail to load; corrupt LZ4 is accepted), then 3 and 5 (files vanilla reads or saves fail in Horizon).

We ran Horizon (main at 9d979ca) against golden outputs from the real 26.3 server classes on JDK 25 and found eight byte-level differences, each with a minimal reproduction below.

Vanilla results come from the 26.3 classes on JDK 25. Hex is raw bytes.

1. Gaussian draws are 1 ulp off on x86-64

About 0.1% of next_gaussian results differ from vanilla in the last bit, in every random source. The polar method's log goes to Rust's f64::ln on x86-64, which differs in the last bit from HotSpot's Math.log intrinsic.

  • Repro (horizon_core::random): let mut r = XoroshiroRandom::new(-4064299097425015850); r.next_int(); r.next_int(); let mut f = r.fork(); f.next_long(); f.next_int_bound(1000); f.next_gaussian()
  • Vanilla: 0x3fd46420f960f7c7. Horizon: 0x3fd46420f960f7c6.
  • Why it matters: anything that draws a Gaussian gets a different value from vanilla about once in a thousand draws, and later state that depends on it drifts from there.

HotSpot's Math.log gives identical bits on Intel (i5-13420H) and AMD (Zen 1) across JDK 17, 21 and 25 on 1M inputs, so vanilla's Gaussians don't depend on the CPU; Rust's f64::ln differs from both.

2. Compound tags are written in sorted key order

nbt::Compound is a BTreeMap, so write_named and write_network emit keys in UTF-16 order. Vanilla emits them in java.util.HashMap iteration order.

  • Repro (horizon_world::nbt): read_named then write_named on 0a 0000 01 0001 5a 01 01 0001 61 02 00 ({Z:1b, a:2b}).
  • Vanilla: 0a 0000 01 0001 61 02 01 0001 5a 01 00 (a first). Horizon: 0a 0000 01 0001 5a 01 01 0001 61 02 00 (Z first).
  • Why it matters: saved chunks, level.dat and NBT packets differ byte for byte from vanilla's whenever a compound has two or more keys. That breaks byte-level comparison and anything that hashes NBT.

3. The disk root name is decoded instead of skipped

read_named decodes the root tag's name as modified UTF-8 and fails on invalid bytes or a short name. Vanilla skips the name bytes without decoding them, stopping at end of input.

  • Repro: nbt::read_named(&hex("0a0002ffff00"), limits). A second case: 4a1f01.
  • Vanilla: 0a0002ffff00 loads an empty compound and consumes 6 bytes. 4a1f01 fails as a format error (unknown type 0x4a). Horizon: 0a0002ffff00 returns "invalid Java modified UTF-8". 4a1f01 returns "truncated input".
  • Why it matters: a file whose root name is malformed but whose payload is valid loads in vanilla and fails in Horizon, so the world or data file is lost.

4. The network writer canonicalises NaN

write_network replaces every float or double NaN with the canonical NaN. Vanilla's network path writes the stored bits unchanged. Only the disk path canonicalises.

  • Repro: nbt::read_network(&hex("057f800001"), limits), then write_network.
  • Vanilla: 05 7f800001. Horizon: 05 7fc00000.
  • Why it matters: NBT sent to clients (item components, entity data) is not bit-identical to vanilla's when it holds NaN payloads.

5. The disk writer fails on strings longer than 65,535 encoded bytes

write_named returns an error when any key or string value encodes to more than 65,535 bytes. Vanilla's disk writer writes an empty string in its place and finishes the tree. Only the network writer fails.

  • Repro: build Tag::compound([("s", Tag::String(JavaString(vec![0u16; 32768])))]) (each U+0000 encodes to 2 bytes, 65,536 in all), then nbt::write_named with empty name.
  • Vanilla: 0a 0000 08 0001 73 0000 00. Horizon: Err("modified UTF-8 exceeds 65535 bytes").
  • Why it matters: a chunk or player file holding such a string (book text, a command block, a modded tag) fails to save in Horizon, where vanilla saves the rest of the data.

6. Chunks with bytes after the root, or a damaged stream tail, fail to load

ChunkRecord::nbt decompresses the whole stream first and rejects trailing bytes ("trailing bytes after region chunk NBT"). Vanilla parses the root straight from the stream and ignores whatever follows it, including a corrupt zlib or LZ4 tail.

  • Repro (horizon_world::region): a region file r.0.0.mca of 12,288 bytes. Header slot 0 = 00000201, the rest of both header sectors zero. Sector 2 starts 00000006 03 0a000000ff, padded with zeros. Call RegionFile::open(..)?.read(ChunkPos::new(0,0))?.unwrap().nbt(Limits::saved()).
  • Vanilla: loads the empty compound (4 bytes read). Horizon: Err("trailing bytes after region chunk NBT").
  • Why it matters: chunks vanilla loads fine are treated as unreadable. In our hidden set this was about 20% of damaged-tail and junk-after chunks.

7. The LZ4 decoder accepts a stream vanilla rejects

Compression::Lz4.decode returns data for an LZ4Block stream that lz4-java refuses. Vanilla delivers 0 bytes and raises an LZ4 error.

  • Repro (horizon_world::compression): Compression::Lz4.decode(&hex("4c5a34426c6f636b2620000000270000002817d90bb112cd89673ffd3d11ad97420600708664345bbd2cb70b00248fc80b0020a52b4c5a34426c6f636b16000000000000000000000000"), usize::MAX).
  • Vanilla: error, 0 bytes. Horizon: Ok, 39 bytes.
  • Why it matters: corrupt LZ4 chunks load with wrong contents instead of being treated as damaged.

8. A chunk write does not rewrite the whole region header

RegionFile::write and clear write only the touched slot's offset and timestamp. Vanilla rewrites both header sectors from memory, so entries it dropped as invalid when opening (sector < 2, count 0, past end of file) become zero on disk.

  • Repro: a file r.0.0.mca of 8,192 zero bytes, except slot 5 = 00000101 at offset 20 (sector 1, an invalid entry). Call RegionFile::open_writable(path, false) and write any chunk to (0,0).
  • Vanilla: offset 20 holds 00000000 afterwards. Horizon: offset 20 still holds 00000101.
  • Why it matters: region files differ from vanilla's, and the stale invalid entries stay in the file for other tools to read.
Originally created by @mmonfox on GitHub (Oct 31, 2026). Original GitHub issue: https://github.com/horizon-rust/horizon/issues/5064 Originally assigned to: @k3yh on GitHub. This is an AI-written report, reviewed by me (mmonfox). Some of these aren’t severe, just inconsistencies with vanilla that could bite later. The ones that matter most are 6 and 7 (chunks vanilla loads fail to load; corrupt LZ4 is accepted), then 3 and 5 (files vanilla reads or saves fail in Horizon). We ran Horizon (main at 9d979ca) against golden outputs from the real 26.3 server classes on JDK 25 and found eight byte-level differences, each with a minimal reproduction below. Vanilla results come from the 26.3 classes on JDK 25. Hex is raw bytes. ## 1. Gaussian draws are 1 ulp off on x86-64 About 0.1% of `next_gaussian` results differ from vanilla in the last bit, in every random source. The polar method's `log` goes to Rust's `f64::ln` on x86-64, which differs in the last bit from HotSpot's `Math.log` intrinsic. - Repro (`horizon_core::random`): `let mut r = XoroshiroRandom::new(-4064299097425015850); r.next_int(); r.next_int(); let mut f = r.fork(); f.next_long(); f.next_int_bound(1000); f.next_gaussian()` - Vanilla: `0x3fd46420f960f7c7`. Horizon: `0x3fd46420f960f7c6`. - Why it matters: anything that draws a Gaussian gets a different value from vanilla about once in a thousand draws, and later state that depends on it drifts from there. HotSpot's Math.log gives identical bits on Intel (i5-13420H) and AMD (Zen 1) across JDK 17, 21 and 25 on 1M inputs, so vanilla's Gaussians don't depend on the CPU; Rust's f64::ln differs from both. ## 2. Compound tags are written in sorted key order `nbt::Compound` is a `BTreeMap`, so `write_named` and `write_network` emit keys in UTF-16 order. Vanilla emits them in `java.util.HashMap` iteration order. - Repro (`horizon_world::nbt`): `read_named` then `write_named` on `0a 0000 01 0001 5a 01 01 0001 61 02 00` (`{Z:1b, a:2b}`). - Vanilla: `0a 0000 01 0001 61 02 01 0001 5a 01 00` (`a` first). Horizon: `0a 0000 01 0001 5a 01 01 0001 61 02 00` (`Z` first). - Why it matters: saved chunks, level.dat and NBT packets differ byte for byte from vanilla's whenever a compound has two or more keys. That breaks byte-level comparison and anything that hashes NBT. ## 3. The disk root name is decoded instead of skipped `read_named` decodes the root tag's name as modified UTF-8 and fails on invalid bytes or a short name. Vanilla skips the name bytes without decoding them, stopping at end of input. - Repro: `nbt::read_named(&hex("0a0002ffff00"), limits)`. A second case: `4a1f01`. - Vanilla: `0a0002ffff00` loads an empty compound and consumes 6 bytes. `4a1f01` fails as a format error (unknown type 0x4a). Horizon: `0a0002ffff00` returns "invalid Java modified UTF-8". `4a1f01` returns "truncated input". - Why it matters: a file whose root name is malformed but whose payload is valid loads in vanilla and fails in Horizon, so the world or data file is lost. ## 4. The network writer canonicalises NaN `write_network` replaces every float or double NaN with the canonical NaN. Vanilla's network path writes the stored bits unchanged. Only the disk path canonicalises. - Repro: `nbt::read_network(&hex("057f800001"), limits)`, then `write_network`. - Vanilla: `05 7f800001`. Horizon: `05 7fc00000`. - Why it matters: NBT sent to clients (item components, entity data) is not bit-identical to vanilla's when it holds NaN payloads. ## 5. The disk writer fails on strings longer than 65,535 encoded bytes `write_named` returns an error when any key or string value encodes to more than 65,535 bytes. Vanilla's disk writer writes an empty string in its place and finishes the tree. Only the network writer fails. - Repro: build `Tag::compound([("s", Tag::String(JavaString(vec![0u16; 32768])))])` (each U+0000 encodes to 2 bytes, 65,536 in all), then `nbt::write_named` with empty name. - Vanilla: `0a 0000 08 0001 73 0000 00`. Horizon: `Err("modified UTF-8 exceeds 65535 bytes")`. - Why it matters: a chunk or player file holding such a string (book text, a command block, a modded tag) fails to save in Horizon, where vanilla saves the rest of the data. ## 6. Chunks with bytes after the root, or a damaged stream tail, fail to load `ChunkRecord::nbt` decompresses the whole stream first and rejects trailing bytes ("trailing bytes after region chunk NBT"). Vanilla parses the root straight from the stream and ignores whatever follows it, including a corrupt zlib or LZ4 tail. - Repro (`horizon_world::region`): a region file `r.0.0.mca` of 12,288 bytes. Header slot 0 = `00000201`, the rest of both header sectors zero. Sector 2 starts `00000006 03 0a000000ff`, padded with zeros. Call `RegionFile::open(..)?.read(ChunkPos::new(0,0))?.unwrap().nbt(Limits::saved())`. - Vanilla: loads the empty compound (4 bytes read). Horizon: `Err("trailing bytes after region chunk NBT")`. - Why it matters: chunks vanilla loads fine are treated as unreadable. In our hidden set this was about 20% of damaged-tail and junk-after chunks. ## 7. The LZ4 decoder accepts a stream vanilla rejects `Compression::Lz4.decode` returns data for an LZ4Block stream that lz4-java refuses. Vanilla delivers 0 bytes and raises an LZ4 error. - Repro (`horizon_world::compression`): `Compression::Lz4.decode(&hex("4c5a34426c6f636b2620000000270000002817d90bb112cd89673ffd3d11ad97420600708664345bbd2cb70b00248fc80b0020a52b4c5a34426c6f636b16000000000000000000000000"), usize::MAX)`. - Vanilla: error, 0 bytes. Horizon: `Ok`, 39 bytes. - Why it matters: corrupt LZ4 chunks load with wrong contents instead of being treated as damaged. ## 8. A chunk write does not rewrite the whole region header `RegionFile::write` and `clear` write only the touched slot's offset and timestamp. Vanilla rewrites both header sectors from memory, so entries it dropped as invalid when opening (sector < 2, count 0, past end of file) become zero on disk. - Repro: a file `r.0.0.mca` of 8,192 zero bytes, except slot 5 = `00000101` at offset 20 (sector 1, an invalid entry). Call `RegionFile::open_writable(path, false)` and write any chunk to (0,0). - Vanilla: offset 20 holds `00000000` afterwards. Horizon: offset 20 still holds `00000101`. - Why it matters: region files differ from vanilla's, and the stale invalid entries stay in the file for other tools to read.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: /claim

Point 6 only (a chunk with bytes or a damaged stream after its root). Points 1–5, 7 and 8 are free for anyone.

<!-- gh-comment-id:6542281730 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: /claim Point 6 only (a chunk with bytes or a damaged stream after its root). Points 1–5, 7 and 8 are free for anyone.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: taking point 5 as well (a string over 65535 encoded bytes makes the save fail). It goes in its own PR, separate from #5077. Points 1–4, 7 and 8 are still free for anyone.

<!-- gh-comment-id:6542467187 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: taking point 5 as well (a string over 65535 encoded bytes makes the save fail). It goes in its own PR, separate from #5077. Points 1–4, 7 and 8 are still free for anyone.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: taking point 3 as well (a root name that isn't modified UTF-8 makes the file fail to load). It goes in its own PR. Points 1, 2, 4, 7 and 8 are still free for anyone.

<!-- gh-comment-id:6542602901 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: taking point 3 as well (a root name that isn't modified UTF-8 makes the file fail to load). It goes in its own PR. Points 1, 2, 4, 7 and 8 are still free for anyone.
Author
Owner

@mmonfox commented on GitHub (Oct 31, 2026):

@k3yh thoughts on this report versus what other reports? (Human here!)

<!-- gh-comment-id:6542614959 --> @mmonfox commented on GitHub (Oct 31, 2026): @k3yh thoughts on this report versus what other reports? (Human here!)
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

8. @k3yh thoughts on this report versus what other reports? (Human here!)

@mmonfox
Finally something that gives specific and useful info. Only recommendation would be that if you find that many issues, you should open it in different "issues", not all in one so other devs can claim those and this one wont be that crowded (human here, hehe)

<!-- gh-comment-id:6542693042 --> @k3yh commented on GitHub (Oct 31, 2026): > 8\. [@k3yh](https://github.com/k3yh) thoughts on this report versus what other reports? (Human here!) @mmonfox Finally something that gives specific and useful info. Only recommendation would be that if you find that many issues, you should open it in different "issues", not all in one so other devs can claim those and this one wont be that crowded (human here, hehe)
Author
Owner

@mmonfox commented on GitHub (Oct 31, 2026):

@k3yh Thanks, will keep that in mind. I'm writing server implementation myself, so I ran some tests on Horizon server side too, it's crazy that there are even differences how world generates between AMD and Intel lol... I'll do run some tests here from time to time to see the "worst offenders" as I continue developing plugin&mod ready server

<!-- gh-comment-id:6542790595 --> @mmonfox commented on GitHub (Oct 31, 2026): @k3yh Thanks, will keep that in mind. I'm writing server implementation myself, so I ran some tests on Horizon server side too, it's crazy that there are even differences how world generates between AMD and Intel lol... I'll do run some tests here from time to time to see the "worst offenders" as I continue developing plugin&mod ready server
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: taking point 8 as well (a chunk write doesn't rewrite the whole region header). It goes in its own PR. Points 1, 2, 4 and 7 are still free for anyone.

<!-- gh-comment-id:6542871741 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: taking point 8 as well (a chunk write doesn't rewrite the whole region header). It goes in its own PR. Points 1, 2, 4 and 7 are still free for anyone.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: taking point 7 as well (LZ4 blocks read differently from lz4-java). It goes in its own PR. It touches the same decode_lz4 as #5077, which the PR explains. Points 1, 2 and 4 are still free for anyone.

<!-- gh-comment-id:6543097304 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: taking point 7 as well (LZ4 blocks read differently from lz4-java). It goes in its own PR. It touches the same `decode_lz4` as #5077, which the PR explains. Points 1, 2 and 4 are still free for anyone.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: on point 2 (compound key order), I'd suggest not matching it, and I'm not taking it.

Vanilla's CompoundTag keeps its tags in a java.util.HashMap: new HashMap<>(), Maps.newHashMap() on load, new HashMap<>(Map) on copy. Its iteration order depends on each key's String.hashCode(), on the table size, and on the insertion order of keys that land in the same bucket. The table size depends on the map's history: how many keys it has ever held, and whether it was copied (the HashMap(Map) constructor sizes it differently). Horizon's Compound is a BTreeMap used in about 200 places. It keeps neither that history nor insertion order, so matching the order would mean changing the type everywhere and emulating HashMap's internals.

It doesn't change what a game does: vanilla reads keys in any order, and item component hashing (HashOps) doesn't depend on map order. Only byte-level comparison of files and NBT packets differs. Leaving it to the maintainers to decide.

<!-- gh-comment-id:6543478374 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: on point 2 (compound key order), I'd suggest not matching it, and I'm not taking it. Vanilla's `CompoundTag` keeps its tags in a `java.util.HashMap`: `new HashMap<>()`, `Maps.newHashMap()` on load, `new HashMap<>(Map)` on copy. Its iteration order depends on each key's `String.hashCode()`, on the table size, and on the insertion order of keys that land in the same bucket. The table size depends on the map's history: how many keys it has ever held, and whether it was copied (the `HashMap(Map)` constructor sizes it differently). Horizon's `Compound` is a `BTreeMap` used in about 200 places. It keeps neither that history nor insertion order, so matching the order would mean changing the type everywhere and emulating `HashMap`'s internals. It doesn't change what a game does: vanilla reads keys in any order, and item component hashing (`HashOps`) doesn't depend on map order. Only byte-level comparison of files and NBT packets differs. Leaving it to the maintainers to decide.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: taking point 4 as well (network NBT canonicalizes NaNs). It goes in its own PR. Point 1 is still free for anyone; point 2 is explained above.

<!-- gh-comment-id:6543495107 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: taking point 4 as well (network NBT canonicalizes NaNs). It goes in its own PR. Point 1 is still free for anyone; point 2 is explained above.
Author
Owner

@k3yh commented on GitHub (Oct 31, 2026):

🤖 Agent: measurements on point 1, not a fix. Point 1 is left to the maintainers to choose a route.

marsaglia_polar calls jmath::log. On AArch64 that is fdlibm, which matches vanilla there. On x86-64 it is f64::ln, the platform libm (MSVC's UCRT on Windows, glibc on Linux). Vanilla on x86-64 uses HotSpot's Intel LIBM dlog stub instead.

Setup: 200,000 polar-method radii (s = x² + y², from SplittableRandom(1)), Windows 11, Ryzen 7 9800X3D, JDK 25.0.5, compared bit for bit:

differs from vanilla Math.log not correctly rounded*
Horizon's jmath::log on x86-64 (UCRT ln) 299 (0.15%) 298
Math.log (HotSpot x86-64 stub) – 3 (0.0015%)
StrictMath.log (fdlibm, = Horizon's AArch64 path, 0 differences) 14,650 (7.3%) ~7%

*Checked against ln at 60 significant digits (Python decimal). In the 299 disagreements, Math.log is the correctly rounded one in 297.

So:

  • Vanilla itself differs between x86-64 and AArch64, by 7% of logs.
  • On x86-64, Horizon depends on the OS's libm. That would also explain different worlds on different machines, as mmonfox saw on AMD and Intel, though I could only test one AMD machine on Windows.
  • The HotSpot stub is correctly rounded for nearly every argument.

Two routes I see:

  1. A correctly rounded log in pure Rust, for example a port of CORE-MATH's cr_log, which is MIT. It would match vanilla x86-64 on 199,997 of these 200,000, and give the same result on every OS and CPU. It could be checked against Math.log on millions of arguments.
  2. A port of HotSpot's x86-64 dlog stub. That is an exact match, but it is OpenJDK HotSpot code under GPLv2, so it is a licensing question for Horizon.

jmath's sin, cos, tan, exp and pow take the platform libm on x86-64 too; I haven't measured those.

<!-- gh-comment-id:6543615459 --> @k3yh commented on GitHub (Oct 31, 2026): 🤖 Agent: measurements on point 1, not a fix. Point 1 is left to the maintainers to choose a route. `marsaglia_polar` calls `jmath::log`. On AArch64 that is fdlibm, which matches vanilla there. On x86-64 it is `f64::ln`, the platform libm (MSVC's UCRT on Windows, glibc on Linux). Vanilla on x86-64 uses HotSpot's Intel LIBM `dlog` stub instead. Setup: 200,000 polar-method radii (`s = x² + y²`, from `SplittableRandom(1)`), Windows 11, Ryzen 7 9800X3D, JDK 25.0.5, compared bit for bit: | | differs from vanilla `Math.log` | not correctly rounded* | |---|---|---| | Horizon's `jmath::log` on x86-64 (UCRT `ln`) | 299 (0.15%) | 298 | | `Math.log` (HotSpot x86-64 stub) | – | **3** (0.0015%) | | `StrictMath.log` (fdlibm, = Horizon's AArch64 path, 0 differences) | 14,650 (7.3%) | ~7% | \*Checked against `ln` at 60 significant digits (Python `decimal`). In the 299 disagreements, `Math.log` is the correctly rounded one in 297. So: - Vanilla itself differs between x86-64 and AArch64, by 7% of logs. - On x86-64, Horizon depends on the OS's libm. That would also explain different worlds on different machines, as mmonfox saw on AMD and Intel, though I could only test one AMD machine on Windows. - The HotSpot stub is correctly rounded for nearly every argument. Two routes I see: 1. **A correctly rounded `log` in pure Rust**, for example a port of CORE-MATH's `cr_log`, which is MIT. It would match vanilla x86-64 on 199,997 of these 200,000, and give the same result on every OS and CPU. It could be checked against `Math.log` on millions of arguments. 2. **A port of HotSpot's x86-64 `dlog` stub.** That is an exact match, but it is OpenJDK HotSpot code under GPLv2, so it is a licensing question for Horizon. `jmath`'s `sin`, `cos`, `tan`, `exp` and `pow` take the platform libm on x86-64 too; I haven't measured those.
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#1541
No description provided.