Skip to content

codegen/-O1: re-migrated zlib inflate path decompresses wrong at -O1 (Z_DATA_ERROR); -O0 fine #885

Closed

Description

@ehartford

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

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

  1. ehartford commented on Aug 22, 2026

    @ehartford
    ContributorAuthor

    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.w at each step):

    • committed zlib: passes -O1
    • NEW defs+inflate: passes
    • NEW inftrees+inffast+zutil: passes
    • NEW deflate+trees: fails → NEW deflate alone: passes → trees.w is it

    The failure is on compress (deflate), not inflate — the corrupt compressed
    bytes make inflate correctly return Z_DATA_ERROR. My earlier focus on
    inflate.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 the
    explicit ct_data_s { fc: ct_data_s_fc { freq: 12 }, ... }. The bare form is
    silently 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.

  2. ehartford commented on Aug 23, 2026

    @ehartford
    ContributorAuthor

    Resolved by the #886 migrator fix (6dd5414 + ca91e09)

    Root cause was the anonymous-union member-init flattening (#886):
    lower_anon_member_arm_init got the braced {12} (an InitListExpr) instead
    of the scalar, bailed, and a text fallback dropped the union wrapper —
    ct_data_s { fc: 12 } — which the compiler then mis-stored, corrupting zlib's
    static 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-test passes. Battery green,
    pushed, reseeded. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions