Skip to content

A non-capturing closure inside a generic function coerced to extern "C" fn crashes when C calls it #1135

Closed

Description

@ehartford

Spec §16.6: named functions and non-capturing closures coerce to extern "C" fn when their signature matches. Inside a generic function the coerced closure is miscompiled: the callee runs the closure thunk (__closure + 8, EXC_BAD_ACCESS at address 0x5) instead of a plain C-ABI entry, so the migrated c-algorithms sorted array segfaults on its first comparison. The same closure in a non-generic function works.

Repro (from the c-algorithms-phase1 worktree; std.c_algorithms.sortedarray is the migrated corpus under lib/std/c_algorithms):

use std.builtins.print_i32
use std.c_algorithms.defs
use std.c_algorithms.sortedarray
extern fn with_alloc(size: i64) -> *mut u8
fn box_in[T](value: T) -> *mut c_void:
    let ptr = unsafe { with_alloc(sizeof[T]() as i64) } as *mut T
    unsafe { *ptr = value }
    ptr as *mut c_void
fn comparator[T: Ord](sample: &T) -> unsafe extern "C" fn(*mut c_void, *mut c_void) -> c_int:
    (a, b) => (unsafe { *(a as *const T) as &T }).cmp(*(unsafe { *(b as *const T) as &T })) as c_int
fn main:
    let seed = 0
    let array = sortedarray_new(0 as c_uint, comparator(&seed))
    for v in [5, 1, 3]: assert(unsafe { sortedarray_insert(array, box_in(v)) } != 0)
    print_i32(unsafe { *(sortedarray_get(array, 1 as c_uint) as *const i32) })

Expected: prints 3. Actual: exit 139.

* thread #1, stop reason = EXC_BAD_ACCESS (code=1, address=0x5)
    frame #0: p1bin`__closure + 8

Control: the same closure inside a non-generic fn comparator() -> unsafe extern "C" fn(...) prints 3. A plain named fn (fn cmp_i32(a: *mut c_void, b: *mut c_void) -> c_int) passed directly also works.

Related silent acceptance: T.engine_cmp (a trait static method referenced through the type parameter as a function value) is accepted by Sema, produces no MIR operand for the argument, and the engine jumps to a stack address (EXC_BAD_ACCESS at 0x16fdfe7a0). Either the spelling is rejected with a diagnostic or it lowers to the monomorphized function.

Found while writing the Phase 1 c-algorithms facades (docs/stdlib_sourcing_plan.md); the facades route comparisons through one non-generic C trampoline and a With-ABI closure stored beside each value until this is fixed.

Activity

  1. ehartford commented on Oct 24, 2026

    @ehartford
    ContributorAuthor

    No longer reproduces (verified on main 23dd9db with the issue's own repro and with qsort through the same shape: the generic closure sorts). Pinned by test/behavior/behav_1135_generic_closure_to_c.w in #2614. Note for anyone re-probing: the sample fixes T, so an isize sample over i32 values reads eight bytes and orders garbage (that is the program's mistake, not the compiler's; with analyze ... matrix shows the callee as isize.cmp).

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