Skip to content

migrate: prelude-collision libc call in a goto function emits unbound write_ (goto-CFG callee emission gap) #884

Closed

Description

@ehartford

Summary

A libc function whose name collides with the With prelude (write, read,
print, ...) called from a goto-containing function migrates to a call
that references the renamed spelling (write_) with no binding for it,
and without the use std.libc the older migrator injected. The output does
not compile: error: undefined variable ... write_.

This is distinct from #882 (the macro-session SIGSEGV, fixed by 33a14ef). It
surfaced while re-migrating zlib for #880: with the segfault fixed, gz_comp
reached the goto-CFG value lowerer.

Repro

#include <unistd.h>
struct S { int fd; const void *next_in; unsigned avail_in; };
int gz_comp(struct S *state, unsigned put) {
    int writ = 0;
loop:
    if (state->avail_in == 0) goto done;
    writ = (int)write(state->fd, state->next_in, put);   // <-- goto path
    if (writ < 0) goto done;
    state->avail_in -= (unsigned)writ;
    goto loop;
done:
    return writ;
}

What happens today (at HEAD, 33a14ef)

The goto-CFG call lowerer's final branch calls
ci_note_filtered_system_symbol_ref_at(session, callee_cursor, callee_text, FN)
with callee_text == "write_" — the renamed spelling. The allowlist
(ci_libc_symbol_kind_mask) keys on the original name write, so write_
is treated as an unhandled filtered system symbol and the whole function bails
loudly: unsupported assignment rhs in goto CFG (kind=117). Honest, but
gz_comp is untranslatable, so zlib-migrate stops at gzwrite.c.

The two-part fix (a partial one is worse — see below)

  1. Filter on the original name. The normal DECL_REF path
    (CImport.w:8618) filters on with_ci_cursor_spelling (the original). The
    goto path should recover the original callee name
    (ci_call_callee_name(callee_cursor)) and filter on that, so write is
    allowed and noted (ci_migrate_note_libc_symbol), which drives the
    use std.libc injection (ci_migrate_insert_libc_use).

  2. Emit a resolvable callee. Even with (1), the emitted call still names
    write_, which nothing defines — std.libc exports write
    (lib/std/libc.w:89), not write_. The pre-c-migrate: designated inits over payload-enum carriers (tag + anonymous union) are untranslatable #749 migrator emitted the
    original write(...) and relied on use std.libc (see the old committed
    lib/std/zlib/gzwrite.w gz_comp: write((unsafe *__param_state).fd, ...)
    with use std.libc). So the goto call to an allowed libc function must
    emit the original name (resolving through std.libc), not the
    ci_migrate_c_function_name-renamed write_.

Do not land (1) without (2). I tried (1) alone: it makes the migrator
"succeed" and emit write_(...) with no binding and no use std.libc, i.e.
silently-uncompilable output — a no-silent-fallbacks regression strictly worse
than the current loud bail. (That attempt was reverted; only the #882 segfault
fix is at HEAD.)

Blocks

#880's zlib re-migration. With both parts, all 15/15 zlib units migrate and
:zlib-test passes (verified during the reverted attempt — the emission was
the last gap, and the use std.libc + original-name form is what the working
old output uses). pcre2 is independently blocked by #883.

Activity

  1. ehartford commented on Aug 21, 2026

    @ehartford
    ContributorAuthor

    Broader than goto — the normal path fails too, and it's a #749 regression

    A one-function non-goto repro fails migration at HEAD:

    #include <unistd.h>
    int wr(int fd, const void *buf, unsigned long n) {
        return (int)write(fd, buf, n);
    }
    
    migrate: unsupported filtered system function 'write_' from .../unistd.h:508:10;
    add an explicit binding or extend std.libc
    

    So this is NOT specific to the goto-CFG lowerer — the migrator cannot migrate
    any call to a prelude-collision libc function (write, read, print,
    eprint, ewrite, and the assert/check/... family) regardless of control
    flow. The error here fires while processing the declaration of write
    (unistd.h:508), i.e. a filter site is handed the renamed write_ and rejects
    it because the allowlist keys on write.

    The committed lib/std/zlib works only because it was migrated before
    #749 added the write -> write_ prelude-collision rename (ci_escape_reserved,
    CImport.w:5257-5260). The old output calls write(...) with use std.libc;
    the current migrator produces write_ and bails.

    Corrected fix scope

    Every place that passes a name to ci_note_filtered_system_symbol_ref /
    _ref_at (and any decl-emission filter for these symbols) must key on the
    original C name, and the emitted call must name the original (resolving
    through std.libc, which exports write/etc.), not the write_ rename. This
    needs a sweep of the filter/emission sites in both the normal and goto paths,
    plus a c-migrator-tests case (write/read call → compiles + runs), then the
    zlib re-migration.

    Not goto-CFG-local; retitling in spirit to "migrator cannot migrate calls to
    prelude-collision libc functions (write/read/print/...)".

  2. ehartford commented on Aug 22, 2026

    @ehartford
    ContributorAuthor

    Fixed (d3c1df7 + 18c22226, battery in flight)

    Root cause was the CALL emission, not just the filter: ci_escape_reserved
    (#749) renames a prelude-collision name to write_ for definitions, and the
    call emission used the same renamed spelling — so the allowlist (keyed on the
    original) rejected write_, and any emitted write_(...) referenced a name
    nothing defines. Both the normal and goto paths were affected (the value-IR
    lowerer routes its callee through the same DECL_REF emission at CImport.w:8610).

    Fix: ci_migrate_call_callee_name returns the original name for an allowed
    libc function (else ci_migrate_c_function_name); the DECL_REF emission uses
    it. A CALL then resolves through std.libc (auto-imported by
    ci_migrate_insert_libc_use), whose write(fd, buf, count) is selected over
    the prelude's write(&str) by arity. write is in fact the only name that is
    both a prelude-collision and a libc function, so the change is tightly scoped.

    Verified before committing (the lesson from the earlier reverted attempt — a
    migrator fix isn't done until the output compiles + the library's suite
    passes):

    • plain and goto functions calling write migrate to write(...) + use std.libc and check clean;
    • new c-migrator-core test prelude_collision_libc_call asserts the
      original name, no write_, the import, and a clean check;
    • full zlib re-migrates 15/15, :zlib-test passes, and behav_zlib_std.w
      passes against the promoted library.

    lib/std/zlib is re-promoted; tools/rt_decl_audit.w drops 13 -> 7 (zlib
    cleared). Closing once the battery + reseed lands. The remaining 7 audit rows
    are pcre2 (#883).

  3. ehartford commented on Aug 22, 2026

    @ehartford
    ContributorAuthor

    Split: migrator fix lands; the zlib re-migration is blocked by an -O1 bug

    The migrator fix + c-migrator test (d3c1df7) is kept and batterying now — it's
    validated independently (both repros migrate to write(...) + use std.libc
    and check clean; the new prelude_collision_libc_call test asserts it).

    The zlib re-promotion (18c22226) was reverted: the re-migrated zlib is
    correct at -O0 but decompresses wrong at -O1 (Z_DATA_ERROR through the
    std.zlib inflate path), while the committed/old zlib is fine at -O1. That's a
    separate compiler/-O1 miscompile filed as #885. :zlib-test
    (example/minigzip) passed at -O1, so it's specific to the inflate modules the
    wrapper exercises.

    So this issue (the migrator can't emit prelude-collision libc calls) is fixed
    by d3c1df7. #880's zlib re-migration now needs #885 (the -O1 inflate bug), and
    pcre2 needs #883.

  4. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-08-31: CLOSE (overcome by events). Fixed by d3c1df7 "migrate: calls to prelude-collision libc functions keep the original name (#884)" — re-migrated lib/std/zlib/gzwrite.w has use std.libc, calls write(...) by its original name (zero write_ occurrences), and the zlib re-migration this blocked landed (ca91e09).

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