Skip to content

migrate: SIGSEGV in with_cimport_macro_name on zlib deflate.c — MacroSession names[idx] holds a garbage pointer (0x2b) #882

Closed

Description

@ehartford

Repro

$ with build :zlib-reference            # fetches zlib-1.3.2 into out/
$ with migrate out/tmp/action-scratch/zlib-migrate/source/deflate.c --no-c-export -o deflate.w
$ echo $?
139

No diagnostic, no output file — the migrator dies silently with SIGSEGV.
Reproduces identically with the installed seed (v0.15.1.6-g4a83a556) and a
stage1 built from HEAD. with build :zlib-migrate therefore cannot re-migrate
zlib today; the committed lib/std/zlib/ was produced by an older migrator.

(Before the fix in #880's branch for the migrate-mode flag, zlib-migrate died
earlier on adler32.c with a loud "untranslatable function" error from the
c_import-only #799 callee filter — that one was the flag being unset on the
directory path and is fixed there. This crash is what's underneath.)

Location (lldb)

stop reason = EXC_BAD_ACCESS (code=1, address=0x2b)
frame #0: with-stage1`make_str
lr = with-stage1`ci_macro_is_fn_like_name + 116 [inlined] with_cimport_macro_name + 32
x0 = 0x2b   x1 = 0x2       (make_str(ptr, len): ptr is garbage, len 2)
  • src/compiler/ClangBridge.w:2574
    make_str(*(((*ms).names as i64 + idx as i64 * 8) as *const *const u8))
    — names[idx] is 0x2b, not a C string.
  • Caller: src/CImport.w:12646 ci_macro_is_fn_like_name iterating
    0..with_cimport_macro_count(session).

bt unwinds only one frame (fp chain clobbered) and fp - sp ≈ 120 KB, so
the faulting callee sits under a very large frame.

Suspects

MacroSession (ClangBridge.w:415) is hand-rolled: parallel with_alloc'd
arrays (names/values/locations/fn_like/system_flags/params/param_counts)
grown by macro_session_grow (:2170-2200, copies oc * 8 / oc * 4 then
frees the old arrays) and appended by macro_session_add_from_define_line
(:2282-2290) from the @[callconv("c")] collect_macro_def visitor. A
names[idx] slot holding a small integer means one of: the session pointer
is stale (session freed/reused — a UAF), count ran past what grow
actually copied, or an append wrote through a pointer the grow had already
freed.

How to run it down

--debug-alloc first (CLAUDE.md memory-bug workflow), then a watchpoint on
names[idx] for the idx that faults:

lldb -- ./out/release/bin/with migrate .../deflate.c --no-c-export -o /tmp/d.w
(lldb) b ClangBridge.w:2574
(lldb) run
(lldb) p idx ; p (*ms).count ; p (*ms).cap

deflate.c is the first zlib unit with a large macro surface (#defines with
parameters, local inline helpers); adler32/compress/crc32 migrate fine.

Why it matters

A silent rc=139 from with migrate is a no-silent-fallbacks violation on its
own, and it blocks re-migrating zlib for #880.

Activity

  1. ehartford commented on Aug 21, 2026

    @ehartford
    ContributorAuthor

    Fixed — two bugs, and zlib fully re-migrates (33a14ef, a3cd6a19, 1cf9807e)

    1. The SIGSEGV (the issue as filed)

    Root-caused with lldb exactly as the debugging notes suggested. The fault was
    make_str(names[idx]) walking a session that was not a MacroSession — its
    count field read as garbage (0x06d70010), so the idx < count guard passed.

    Migrate mode has two libclang sessions: the translation-unit session threaded
    through the cursor walk, and the separate macro session
    (g_migrate_macro_session). ci_lookup_macro_value (CImport.w:12622) already
    ignored its session param and used g_migrate_macro_session;
    ci_macro_is_fn_like_name trusted the passed session and so read the TU session
    as a MacroSession. Fixed to resolve the macro session the same way its sibling
    does. deflate.c was the first zlib unit whose initializers reference object-like
    macros.

    2. The loud failure underneath it

    With the segfault gone, zlib-migrate reached gzwrite.c and failed loudly:
    "unsupported assignment rhs in goto CFG (kind=117)" at
    writ = (int)write(state->fd, ...).

    write is a With prelude-collision name, so the migrator renames it to write_.
    The goto-CFG call lowerer passed that renamed spelling to the system-symbol
    allowlist, which keys on the original C name — so write_ looked like an
    unhandled filtered system symbol and bailed. A hand-declared write migrated
    fine; only the <unistd.h> system write triggered it. The normal DECL_REF
    path (CImport.w:8618) filters on the original name; the goto path now does too.

    Result

    All 15/15 zlib units migrate (was 4/15). :zlib-test passes ("migrated zlib
    example and minigzip tests pass"), and lib/std/zlib is re-promoted from the
    current migrator. tools/rt_decl_audit.w dropped 13 -> 7 (the zlib rows
    cleared).

    Closing once the battery + reseed lands. The remaining #880 work (pcre2) is
    blocked on #883, not this.

  2. ehartford commented on Aug 21, 2026

    @ehartford
    ContributorAuthor

    Correction: the SIGSEGV is fixed; the goto/emission part is #884

    Landed and pushed 33a14ef — the macro-session fix. deflate.c (and every
    zlib unit through the segfault) now migrates; the crash is gone. Battery green,
    reseeded.

    I initially bundled a second fix (goto-CFG call filter + zlib re-migration) and
    had to revert it: making the goto path allow the write call was
    necessary but not sufficient — the emitted callee write_ has no binding and
    use std.libc wasn't injected, so the regenerated zlib didn't compile
    (behav_zlib_std.w caught it). That turned a loud bail into silently-broken
    output, so it's out.

    The remaining work — the migrator can't migrate calls to prelude-collision
    libc functions (write/read/print/...) in either the normal or goto
    path, because the system-symbol filter is handed the renamed write_ — is
    tracked precisely in #884. That is the real remaining blocker for #880's
    zlib half (pcre2 is independently blocked by #883).

    This issue (the SIGSEGV) is resolved by 33a14ef.

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