Skip to content

Name collision between extern "C" functions can cause undefined behavior in safe code #24561

Closed
@dgrunwald

Description

@dgrunwald

I'm generating extern "C" functions using macros. Since it's currently not possible to use $concat_ident for function names, I can't give these functions unique names.

I was hoping for rust's name mangling to take care of the issue; but unfortunately this doesn't work if the macro is used twice in the same function. This usually results in internal compiler errors for me, but on the playpen machine it's possible to produce undefined behavior in safe code:

fn main() {
    {
        extern "C" fn test(a: &mut i32) {
            println!("a: {:?}", a);
            *a = 1;
        }
    }
    {
        extern "C" fn test(b: &i32) {
            println!("b: {:?}", b);
        }
        test(&1);
    }
}

Expected output: b: 1
Actual output: a: 1
playpen: application terminated abnormally with signal 4 (Illegal instruction)

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions