Skip to content

migrate: preamble declares with_alloc/free/memcpy family as *i8, diverging from the rt defs (*mut u8) #880

Closed

Description

@ehartford

Summary

The with migrate preamble is the source of the largest remaining
decl/def divergence class found by tools/rt_decl_audit.w (D30 R2c, #761):

src/CiMigrate.w:647  extern fn with_realloc(ptr: *i8, old_size: i64, new_size: i64) -> *i8
src/CiMigrate.w:648  extern fn with_free(ptr: *i8) -> Unit
src/CiMigrate.w:649  extern fn with_memcpy(dst: *i8, src: *i8, n: i64) -> *i8
src/CiMigrate.w:650  extern fn with_memmove(dst: *i8, src: *i8, n: i64) -> *i8
src/CiMigrate.w:651  extern fn with_memset(ptr: *i8, c: i32, n: i64) -> *i8
src/CiMigrate.w:652  extern fn with_memcmp(a: *i8, b: *i8, n: i64) -> i32

against the rt definitions, all *mut u8 / *const u8:

rt/rt_core.w:1402  pub fn with_realloc(ptr: *mut u8, old_size: i64, new_size: i64) -> *mut u8
rt/rt_core.w:1405  pub fn with_free(ptr: *mut u8) -> Unit
rt/rt_core.w:1417  pub fn with_memcpy(dst: *mut u8, src: *const u8, n: i64) -> *mut u8
rt/rt_core.w:1421  pub fn with_memmove(dst: *mut u8, src: *const u8, n: i64) -> *mut u8
rt/rt_core.w:1430  pub fn with_memset(dst: *mut u8, c: i32, n: i64) -> *mut u8
rt/rt_core.w:1434  pub fn with_memcmp(a: *const u8, b: *const u8, n: i64) -> i32

Every migrated library inherits the wrong shape: lib/std/re/defs.w:99-105
(pcre2) carries these decls verbatim, and ~217 call sites across 15
lib/std/re/pcre2_*.w files cast as *i8 to match. lib/std/zlib/defs.w
has the same preamble. Also in the same preamble: with_bswap16/32/64
declared u16/u32/u64 against i16/i32/i64 defs
(rt/rt_core.w:3603-3605).

Why it is a migrator bug, not a sweep

Per the C-library-migration methodology the fix goes in the migrator and the
libraries are re-migrated; hand-editing 217 generated call sites is the
anti-pattern. And the migrator itself already disagrees with itself — three
paths derive the shape independently:

path free shape
preamble src/CiMigrate.w:648 *i8
structural lowering src/CImport.w:15921-15925 casts arg as *i8
text mapper ci_map_libc_call src/CImport.w:15943 as *mut u8

That is a per-path ABI derivation inside the migrator — the same shape D6
forbids in codegen (FnAbi is the one source of truth). The preamble should
be the one place the runtime-extern shapes live, spelled from the rt defs, and
both lowering paths should read it.

Interaction with #761

#839's declare/define unification reconciles these at LLVM level today, so
nothing fails at link. But the contracts are still derived twice, which is
#761's corruption class, and the *i8/*mut u8 mismatch will be a §16.3e
error once R4 lands (an extern fn whose declared pointee type disagrees
with the def's). tools/rt_decl_audit.w is the exit gate: this issue closes
when it reports zero divergences for the alloc/mem/bswap families.

Fix shape

  1. Correct the preamble strings in src/CiMigrate.w to the rt shapes.
  2. Make the structural free/malloc lowering (CImport.w:15880-15930)
    and ci_map_libc_call emit *mut u8 / *const u8 casts (memcpy/memcmp
    src args are *const).
  3. Re-migrate pcre2 and zlib (with migrate), validate via each library's
    own migrated test suite (pcre2test, zlib example).
  4. Re-run tools/rt_decl_audit.w — the alloc/mem/bswap rows must be gone.

Activity

  1. ehartford commented on Aug 21, 2026

    @ehartford
    ContributorAuthor

    Migrator half landed (a12ea56 + 0120a80, battery in flight)

    • ci_rt_ptr_mut() / ci_rt_ptr_const() in src/CiMigrate.w are the one
      spelling; preamble, both structural IR paths, the text-mapper helpers, and
      CiPrint's struct init/copy read them. No literal *i8 emission remains.
    • with_bswap16/32/64 rt defs flipped to unsigned (zero callers; the bodies
      converted on entry anyway), so the preamble's u16/u32/u64 is now truth.
    • Found by running the directory migration: g_ci_translate_migrate_mode was
      set only on the single-file CLI path. migrate_c_directory — what the
      pcre2/zlib build targets use — ran every mode-gated rule with the flag OFF.
      That's what produced the "untranslatable adler32_combine: calls
      non-emitted adler32_combine_" failure (the c_import-only Windows: c_import behavior tests fail on MSVC headers #799 filter).
      Flag now owned by ci_migrate_file_inner for every caller.
    • c-migrator-basic-tests + c-migrator-core-tests green under stage1; the
      regenerated direct_runtime_memory_calls.w carries *mut u8 / *const u8
      and runs to exit 0.
    • pcre2 build graph was missing the pcre2-migrate → pcre2-build → pcre2-test
      producer edges (zlib's chain had them); added.

    Re-migration of the libraries is blocked — two pre-existing bugs

    library blocker
    zlib #882 — migrator SIGSEGV on deflate.c (ClangBridge.w:2574, MacroSession.names[idx] = 0x2b). adler32/compress/crc32 now migrate clean with the flag fix; deflate is the first unit with a large macro surface. Reproduces on the installed seed.
    pcre2 #883 — std.tls handshake fails against every host (bisected to before today), so pcre2-reference cannot fetch the tarball. The action framework scrubs its scratch dir before each run, so hand-seeding the archive is not a lever.

    Until those land, lib/std/re/defs.w (10 rows) and lib/std/zlib/defs.w
    (3 rows) keep the old shape and tools/rt_decl_audit.w stays at 13. When
    pcre2 is re-migrated, rt/regex_runtime.w has five with_free(x as *i8)
    casts that must flip to *mut u8 in the same commit (they currently satisfy
    the old decl; I tried flipping them early and backed it out for that reason).

    Steps 3–4 of the fix shape in the description remain open; this issue stays
    open until the audit reads zero for the alloc/mem/bswap families.

  2. ehartford commented on Aug 23, 2026

    @ehartford
    ContributorAuthor

    zlib half done (ca91e09)

    lib/std/zlib is re-migrated with the current migrator and validated:
    :zlib-test passes and behav_zlib_std.w passes at -O0 and -O1. The three
    migrator bugs that blocked it are fixed — #882 (SIGSEGV), #884
    (prelude-collision libc calls), #886 (anon-union init). tools/rt_decl_audit.w
    is at 7 divergences, all lib/std/re/defs.w (pcre2).

    Remaining: the pcre2 half is blocked on #883 (std.tls P-256 ECDH — the
    handshake can't fetch the tarball). Once #883 lands, re-migrate pcre2 the same
    way and the audit should reach 0.

  3. ehartford commented on Aug 29, 2026

    @ehartford
    ContributorAuthor

    Migration-correctness campaign COMPLETE: upstream RunTest fully green

    with build :pcre2-test now reports VERIFIED: migrated pcre2test passes upstream RunTest for the 8-bit corpus — all groups 0–29 plus heap (was 14 failing groups when the RunTest gate was first run properly).

    Five migrator fixes, every one a variant of a single defect family — the migrator recovers a C string from the wrong libclang text form and keeps only part of it:

    Commit Defect Cleared
    adfbcc94 macro-expanded adjacent-string concat kept only the first macro's string (spelling-location text) Test 7 + all pso_list verb names
    ea8b20a7 #-stringize token (XSTRING(PCRE2_CODE_UNIT_WIDTH)) dropped by the plain literal join Test 22 (UTF-8 → UTF-)
    d735f80b macro-likeness detector scanned string CONTENT — all-caps words in error messages false-flagged, forcing a re-derivation that truncated the error tables at the first interior /* */ comment → pcre2_get_error_message = −29 Tests 0, 2, 5, 6, 9, 14, 15, 16, 18, 20
    8e0daae0 STRING_UTFn_RIGHTPAR = "<concat>, <length>" macro form; peel the trailing , <n> and expand the string part Test 10 ((*UTF8))
    b60edabc fn-like macro argument (PUTCHARS(STR_STAR_NUL)) — the cursor extent maps to the macro definition sites in the header, so token/source text is a slice of pcre2_internal.h's #define lines; prefer the invocation-argument expansion Test 24 ((*NUL), :])

    Method note: the breakthrough (d735f80, ten tests at once) came from a one-shot pipeline dump — one instrumented migrate printing every string-pipeline stage's length — after blind print-rebuild cycles had failed; the truncation was a post-expansion clobber, invisible to any single-stage read.

    Battery (build/:fixpoint/:test) for the 5-commit batch in progress; then reseed → :pcre2-promote → rt_decl_audit → 0.

  4. ehartford commented on Aug 30, 2026

    @ehartford
    ContributorAuthor

    #880 COMPLETE — all acceptance criteria met

    • Re-migration: 34 modules, cohesive compile 0 errors, produced by the seeded compiler.
    • Validated by pcre2's own test suite: VERIFIED: migrated pcre2test passes upstream RunTest for the 8-bit corpus — all groups 0–29 plus heap (from 14 failing groups at the campaign's start).
    • Promoted: 87df3b31 replaces the 2026-05-19 lib/std/re with the re-migrated zone; the embedded regex runtime (:regex-runtime-ir) builds from it and std.regex works end-to-end.
    • rt_decl_audit → 0: 0 divergent decls / 524 rt defs / 829 extern with_* decls.
    • Full battery green (build, fixpoint, test incl. audit:all, move-audit, drop-audit) and reseeded.

    The promote also surfaced and fixed two latent compiler-side issues:

    • f5355773/963abb56: the regex zone is compiled --no-prelude for the embedded runtime — the zone's shared defs now carry self-contained c_void/unreachable, and try_eval_const_llvm gained the missing TY_EXTERN_FN/TY_FN case (one callout: null field was silently demoting whole global initializers to runtime-init — BSS zeros in a standalone object, so every std.regex match returned PCRE2_ERROR_HEAPLIMIT while the identical code worked in-unit).
    • 1cbcece3: object-symbol fixtures adapted to the promoted API's honest unsafe fn contracts.
  5. ehartford commented on Aug 30, 2026

    @ehartford
    ContributorAuthor

    All criteria verified above; closing.

  6. 5 remaining items

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