-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationArea: Code generationC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team
Description
Current status: Clicky clicky
This compiles and prints "p is not null and 0x0":
pub mod bad {
#[allow(improper_ctypes)]
extern {
pub fn malloc(x: usize) -> &'static mut ();
}
#[no_mangle]
pub fn bar() {
let _m = malloc as unsafe extern "C" fn(usize) -> &'static mut ();
}
}
pub mod good {
extern {
fn malloc(x: usize) -> *const u8;
}
pub fn foo() {
unsafe {
let p = malloc(0x13371337deadbeef); // your computer doesn't have enough memory
if p.is_null() {
panic!("p is null");
} else {
panic!("p is not null and {:?}", p);
}
}
}
}
fn main() {
bad::bar();
good::foo();
}
The problem is that we have two declarations of the "malloc" symbol, but LLVM uses a global namespace for these. So during codegen, the 2nd declaration we generate overwrites the first. In this case, the "ill-typed" malloc declaration (bad::malloc
) comes last, up putting a nonnull
attribute on malloc
, which causes mod good
to be miscompiled.
Here's another example that does not involve malloc
. It does not get miscompiled currently, but it demonstrates the issue.
hanna-kruppe, Ixrec, michaelwoerister, kornelski, jplatte and 6 more
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationArea: Code generationC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language team