Skip to content

c_import: a str/&str lent to a translated inline C function's const char * parameter is not marshalled — str fails in codegen, &str passes the header address (D47 §16.3c) #1589

Closed

Description

@ehartford

Repro

use c_import("unsigned long strlen(const char *s);\ntypedef struct box { int n; } box;\nstatic inline unsigned long my_len(box *b, const char *s) { b->n = b->n + 1; return strlen(s); }\n")
fn main:
    var bx = box { n: 0 }
    let b = &raw mut bx
    let s = "hello"
    print(f"{unsafe { my_len(b, s) }}")        // str argument
    let v: &str = s
    print(f"{unsafe { my_len(b, v) }}")        // &str argument

Expected

Both print 5: §16.3c / D47 — "a const char * parameter is lent a NUL-terminated str" — and Sema accepts both arguments (try_ci_coercion, the callee is a c_import symbol).

Actual

  • The str argument fails in codegen: error: wrong argument type actual=str expected=ptr fn=main / code generation failed.
  • The &str argument compiles and passes the address of the str header as the char *: C reads the pointer bytes as text. Observed as a facade constructor Note.note_new(text: &str, id) forwarding text to a static inline note *note_new(const char *text, int id): the note's copy of "hello" reads H-, and the program later hits an invalid free.

Cause: CodegenDispatch.w mir_eval_call_operand_info marshals a str operand through with_cstr_lend only when is_c_abi_arg != 0, i.e. an extern callee; a translated inline C function is an ordinary With fn with a *const i8 parameter, so the operand takes the generic pointer path. Sema and codegen disagree on the same call: the coercion is accepted for every ci_syms member, lowered for externs only.

Two related translator findings on the same shape:

  • static inline unsigned long my_len(const char *s) { return strlen(s); } translates to a body return unsafe { strlen(__param_s) }, and strlen over a const char * input is a safe call, so the check fails with "unsafe block contains no unsafe operations".
  • static inline int first(const char *s) { return s[0]; } translates to a safe fn first(__param_s: *const i8) whose body indexes the raw pointer: "safe function relies on caller-guaranteed raw pointer validity for parameter '__param_s'". The translator classifies the function safe from its const char * parameter and then emits a body that dereferences it.

Found while writing modeled C stage 7 fixtures (test/behavior/c_facade_text.h); the fixtures avoid inline bodies with const char * parameters until this lands. Route: with analyze <repro> audit:calls should flag the caller/callee marshalling disagreement; then the is_c_abi_arg derivation in CodegenDispatch.

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Not fixed by #1716: its pinned test passed by coincidence. strlen received the address of the str header and counted that pointer's non-zero low bytes (usually 5 = len("hello"), sometimes 3 under ASLR), on main and on the branch alike. Root cause: CodegenDispatch keys the D47 C-string lend on abi.convention == FN_ABI_C, while Sema accepts the coercion for every c_import-origin callee (try_ci_coercion / ci_syms); a translated inline function has the With convention. Fix in its own PR (ABI-path change, alone in its battery), with a 13-byte test no pointer's byte count can satisfy.

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