Repository navigation
Description
Summary
The current migrator's zlib output (with build :zlib-migrate +
:zlib-promote, on top of #882/#884) is correct at -O0 but produces
Z_DATA_ERROR (-3, "invalid or corrupt zlib data") at -O1 when
decompressing through the std.zlib wrapper (inflate). Per CLAUDE.md this
is a real bug — latent UB the optimizer exposed, or a genuine miscompile.
$ ./out/bootstrap/bin/with-stage1 run -O0 test/behavior/behav_zlib_std.w
ok
$ ./out/bootstrap/bin/with-stage1 run -O1 test/behavior/behav_zlib_std.w
panic at test/behavior/behav_zlib_std.w:55:20: called unwrap on Err:
ZlibError { code: -3, message: invalid or corrupt zlib data }
Scope
- Only the re-migrated zlib. The committed
lib/std/zlib(migrated by a
pre-c-migrate: designated inits over payload-enum carriers (tag + anonymous union) are untranslatable #749 migrator) passesbehav_zlib_std.wat -O1 and ships today. The
regen was reverted for this reason; the migrate: prelude-collision libc call in a goto function emits unbound write_ (goto-CFG callee emission gap) #884 migrator fix and its
c-migrator test are kept (they don't touch shipped libraries). - Only the inflate path.
:zlib-test(the migratedexample+minigzip,
built at -O1) passes — those exercise gzip file I/O.behav_zlib_std.w
fails oninflate(the-3is Z_DATA_ERROR from decompression), so the
suspect modules areinflate.w/inftrees.w/inffast.w. - -O1-only, everywhere: reproduces under stage1
-O1and the release
compiler (both -O1); clean at -O0.
What changed in the re-migration
The newer migrator's inflate output differs from the committed version in
ways worth checking first as UB sources: the check checksum field of
inflate_state is escaped to check_ (prelude-collision rename), a
zlib_version global was added, and per-type __with_builtin_*_overflow
helpers are emitted. A wrong/half-renamed checksum field, or an
uninitialized/aliased field the optimizer reorders, would present exactly as
Z_DATA_ERROR.
How to run it down
with reduce on the migrated inflate.w/inftrees.w with the predicate
"run -O1 panics with -3", then compare the -O0 vs -O1 codegen for the
reduced function (--dump-mir / LLVM IR). Because -O0 is correct, diffing the
optimizer's effect on the reduced unit should localize it. The migrated
sources are reproducible via with build :zlib-migrate (needs #882+#884,
which are landed).
Why it matters
It is the last thing blocking #880's zlib re-migration (pcre2 is separately
blocked by #883). The migrator now produces correct-at-O0 zlib; this is the
gap to correct-at-O1.
Activity
ehartford commentedon Aug 22, 2026
Root-caused to #886 — reduced from 15 modules to a 7-line repro
Bisected the -O1 zlib miscompile module-by-module (rebuild +
run -O1 behav_zlib_std.wat each step):The failure is on
compress(deflate), not inflate — the corrupt compressedbytes make
inflatecorrectly return Z_DATA_ERROR. My earlier focus oninflate.w was misdirected.
trees.w's only meaningful change is the static Huffman tables. The newer
migrator emits the union fields as bare scalars —
ct_data_s { fc: 12, dl: 8 }— where the pre-#749 migrator emitted theexplicit
ct_data_s { fc: ct_data_s_fc { freq: 12 }, ... }. The bare form issilently miscompiled: a union-typed field given a bare scalar reads back 0.
Minimal repro (fails at -O0) is #886.
So #885 is a symptom of #886. It clears the moment #886 is fixed (the
explicit-member form already compiles correctly at -O1, which is why the old
committed zlib works). Keeping #885 open as the "re-migrated zlib is correct at
-O1" acceptance check for #880's zlib half; the actual fix lives in #886.
migrate: anonymous-union member init keeps the union wrapper (#886, #885
std/zlib: re-migrate with the anon-union init fix (#880, #885, #886)
ehartford commentedon Aug 23, 2026
Resolved by the #886 migrator fix (6dd5414 + ca91e09)
Root cause was the anonymous-union member-init flattening (#886):
lower_anon_member_arm_initgot the braced{12}(an InitListExpr) insteadof the scalar, bailed, and a text fallback dropped the union wrapper —
ct_data_s { fc: 12 }— which the compiler then mis-stored, corrupting zlib'sstatic Huffman tables → wrong compression → inflate's Z_DATA_ERROR at -O1.
Fixed: the arm-init now descends into the braced initializer. zlib re-migrated
and promoted;
behav_zlib_std.w(the -O1 failure) passes at both -O0 and-O1 against the re-migrated library, and
:zlib-testpasses. Battery green,pushed, reseeded. Closing.
sema: reject a bare value for a union-typed struct-literal field (#886)