-
Notifications
You must be signed in to change notification settings - Fork 13.3k
rustc: don't use Abi::ScalarPair for unions when padding is involved. #62298
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// run-pass | ||
|
||
// Test that unions don't lose padding bytes (i.e. not covered by any leaf field). | ||
|
||
#![feature(core_intrinsics, test, transparent_unions)] | ||
|
||
extern crate test; | ||
|
||
#[repr(transparent)] | ||
#[derive(Copy, Clone)] | ||
union U<T: Copy> { _x: T, y: () } | ||
|
||
impl<T: Copy> U<T> { | ||
fn uninit() -> Self { | ||
U { y: () } | ||
} | ||
|
||
unsafe fn write(&mut self, i: usize, v: u8) { | ||
(self as *mut _ as *mut u8).add(i).write(v); | ||
} | ||
|
||
#[inline(never)] | ||
unsafe fn read_rust(self, i: usize) -> u8 { | ||
test::black_box((&self as *const _ as *const u8).add(i)).read() | ||
} | ||
|
||
#[inline(never)] | ||
unsafe extern "C" fn read_c(self, i: usize) -> u8 { | ||
test::black_box((&self as *const _ as *const u8).add(i)).read() | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Default)] | ||
struct Options { | ||
demote_c_to_warning: bool, | ||
} | ||
|
||
unsafe fn check_at<T: Copy>(i: usize, v: u8, opts: Options) { | ||
let mut u = U::<T>::uninit(); | ||
u.write(i, v); | ||
let msg = |abi: &str| format!( | ||
"check_at::<{}>: {} ABI failed at byte {}", | ||
std::intrinsics::type_name::<T>(), | ||
abi, | ||
i, | ||
); | ||
if u.read_rust(i) != v { | ||
panic!(msg("Rust")); | ||
} | ||
if u.read_c(i) != v { | ||
if opts.demote_c_to_warning { | ||
eprintln!("warning: {}", msg("C")); | ||
} else { | ||
panic!(msg("C")); | ||
} | ||
} | ||
} | ||
|
||
fn check_all<T: Copy>(opts: Options) { | ||
for i in 0..std::mem::size_of::<T>() { | ||
unsafe { | ||
check_at::<T>(i, 100 + i as u8, opts); | ||
} | ||
} | ||
} | ||
|
||
#[repr(C, align(16))] | ||
#[derive(Copy, Clone)] | ||
struct Align16<T>(T); | ||
|
||
#[repr(C)] | ||
#[derive(Copy, Clone)] | ||
struct Pair<A, B>(A, B); | ||
|
||
fn main() { | ||
// NOTE(eddyb) we can't error for the `extern "C"` ABI in these cases, as | ||
// the x86_64 SysV calling convention will drop padding bytes inside unions. | ||
check_all::<Align16<u8>>(Options { demote_c_to_warning: true }); | ||
check_all::<Align16<f32>>(Options { demote_c_to_warning: true }); | ||
check_all::<Align16<f64>>(Options { demote_c_to_warning: true }); | ||
|
||
check_all::<(u8, u32)>(Options::default()); | ||
check_all::<(u8, u64)>(Options::default()); | ||
|
||
check_all::<Pair<u8, u32>>(Options::default()); | ||
check_all::<Pair<u8, u64>>(Options::default()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there no
.stderr
file then if we expect warnings to be shown?Also, would be nice to still test the first N bytes that we assume not to be part of the padding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we capture runtime stderr, only rustc stderr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to mention that only some targets will hit that.