Skip to content

C migrator: a declaration-only unprototyped function (int f();) is emitted variadic — wrong ABI in migrated code #1848

Closed

Description

@ehartford

Found while fixing #1831. The C migrator emits a declaration-only unprototyped function (int knr();, no definition in the unit) as a variadic extern, so every migrated call uses the variadic convention — on darwin arm64 the arguments go on the stack while the definition reads x0/x1.

// use.c
int knr();
int twice(void) { return knr(4, 2) * 2; }

with migrate use.c →

extern fn knr(...) -> c_int
...
    return (unsafe { knr(4, 2) } * 2)

C11 6.5.2.2p6: an unprototyped call passes its default-promoted arguments with the fixed-argument convention. #1831 fixed c_import: its generated text states the fact in a manifest line (// @with-cimport-unprototyped|knr) that only the Frontend reads while parsing c_import output, and it sets NK_EXTERN_FN flag bit 1; Sema records each call's promoted argument types and codegen builds the call's FnAbi from them. Migrated source is ordinary user source, so it has no spelling for this.

CiMigrate.w's declaration-only branch (fn_cursor < 0 or not a definition) still takes with_cimport_fn_is_variadic, which is true for every FunctionNoProto type.

Recommended fix: the migrator sees every call site's promoted argument types in clang's AST (the ImplicitCastExpr on each argument). When every call in the unit agrees, emit the prototype C actually uses, extern fn knr(a0: c_int, a1: c_int) -> c_int; when they disagree, or the function is used as a value, fail the migration loudly naming the function (No Silent Fallbacks). Never emit (...).

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