Skip to content

c_import("stdlib.h") no longer exposes malloc/realloc when clang_bridge.o is rebuilt by stage2 #74

Closed

Description

@ehartford

Summary

When clang_bridge.o is rebuilt by stage2 rather than copied from the seed runtime, c_import("stdlib.h") no longer exposes allocator symbols like malloc and realloc.

Spec reference: docs/with-specification.md §16.1 c_import: imported C functions are made available as With symbols and are callable directly.

Repro

Minimal shape:

use c_import("stdlib.h")

fn main:
    let raw = malloc(8)
    let _ = realloc(raw.unwrap(), 0)

Current stage2 behavior:

error: undefined variable
  | malloc
error: undefined variable
  | realloc

Representative failing tests:

  • test/behavior/behav_c_import_nullable_ptr_none.w
  • test/behavior/behav_raw_ptr_arithmetic.w
  • test/behavior/issue44_opaque_struct_call.w
  • test/behavior/behav_imported_alias_struct_field.w
  • test/behavior/issue59_demo_binding_order_independent.w
  • test/behavior/issue59_demo_pair_asserts.w
  • test/behavior/issue59_demo_pair_asserts_dump_mir.w
  • test/behavior/issue59_demo_pair_print.w
  • test/behavior/issue59_demo_view_dtype_roundtrip.w

Expected

malloc and realloc imported from stdlib.h should resolve as callable With symbols.

Actual

Stage2 reports undefined variable for imported allocator functions.

Notes

This only surfaced once stage2 actually rebuilt rt/clang_bridge.w and produced a fresh clang_bridge.o; the same tests had been masked when a seed-built bridge object was copied into place.

Likely subsystem: c_import translation / symbol emission in the stage2-built clang_bridge path.

Activity

  1. added
    bug
    on Apr 6, 2026
  2. ehartford commented on Apr 6, 2026

    @ehartford
    ContributorAuthor

    Fixed by 56f189a.

    Root cause: @[callconv("c")] stored the raw string-literal token text, including quotes, so codegen saw "c" instead of c and skipped the C ABI lowering path for defined functions. That left stage2-built clang_bridge.w visitor callbacks taking CXCursor by value instead of the indirect C ABI form, so clang_visitChildren passed garbage cursors and declaration collection returned zero results.

    The fix normalizes callconv metadata in the parser, keeps codegen defensive by stripping accidental quotes, and preserves the large-struct C ABI lowering path for defined callconv/C-export functions.

    Verified with:

    • make build
    • make selfcheck
    • make fixpoint
    • a minimal @[callconv("c")] large-struct IR probe
    • a stage2-built clang_bridge.o probe against stdlib.h and malloc/_malloc.h
    • the original malloc c_import repro with an empty cache

    Separate follow-up for cache invalidation: #79.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions