-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-DSTsArea: Dynamically-sized types (DSTs)Area: Dynamically-sized types (DSTs)A-craneliftThings relevant to the [future] cranelift backendThings relevant to the [future] cranelift backendC-bugCategory: This is a bug.Category: This is a bug.F-unsized_locals`#![feature(unsized_locals)]``#![feature(unsized_locals)]`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-highHigh priorityHigh 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.
Description
The following code fails on stable:
When compiled in release mode, the "optimized out"
assert is optimized out by LLVM even though it checks the exact same condition as the other assert, as can be verified by testing in debug mode.
Note that it's theoretically possible for the stack to be aligned correctly such that the bug is suppressed, but that's not likely. The 256's can be replaced with larger alignments if that happens.
The bug is caused by the impl of Box<dyn FnOnce>
copying self
to the stack with the default alignment of 16.
#![allow(dead_code)]
#[repr(align(256))]
struct A {
v: u8
}
impl A {
fn f(&self) -> *const A {
assert_eq!(self as *const A as usize % 256, 0, "optimized out");
self
}
}
fn f2(v: u8) -> Box<dyn FnOnce() -> *const A> {
let a = A { v };
Box::new(move || {
a.f()
})
}
#[test]
fn test() {
let addr = f2(0)();
assert_eq!(addr as usize % 256, 0, "addr: {:?}", addr);
}
qnighy and pro465
Metadata
Metadata
Assignees
Labels
A-DSTsArea: Dynamically-sized types (DSTs)Area: Dynamically-sized types (DSTs)A-craneliftThings relevant to the [future] cranelift backendThings relevant to the [future] cranelift backendC-bugCategory: This is a bug.Category: This is a bug.F-unsized_locals`#![feature(unsized_locals)]``#![feature(unsized_locals)]`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-highHigh priorityHigh 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.