Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dgrunwald opened this issue Apr 18, 2015 · 5 comments
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@dgrunwald
Copy link
Contributor

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)

@alexcrichton
Copy link
Member

Hm this sounds like a bug, the compiler should be giving each of these extern functions a different symbol...

@nagisa
Copy link
Member

nagisa commented Apr 18, 2015

On the nightlies it is an ICE:

error: internal compiler error: symbol `_ZN4main4test10__rust_abiE` already defined
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Apr 18, 2015
@ruuda
Copy link
Contributor

ruuda commented May 8, 2015

Not sure if it is the same bug, but this also ICEs rustc 1.1.0-nightly (7bd71637c 2015-05-06) (built 2015-05-06):

fn main() {}

mod foo {
    extern {
        fn main();
    }
}

@mattico
Copy link
Contributor

mattico commented Feb 24, 2016

I believe I'm having the same issue with nightly-2016-02-22. I posted a backtrace over here SkylerLipthay/interpolate_idents#7 and I'll be trying to debug this sometime soon. Unless the issue is very simple I won't have the ability to fix it, but I should be able to identify what the problem is.

@dgrunwald
Copy link
Contributor Author

Closing in favor of #26201, that issue has a more up-to-date title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

6 participants