Skip to content

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,28 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
size = cmp::max(size, field.size);
}

size = size.align_to(align.abi);

if let Abi::ScalarPair(a, b) = &abi {
// Only use `ScalarPair` if there is no padding between
// `a` and `b`, or after `b`, to ensure that every byte
// of a `#[repr(transparent)]` `union` is covered by
// either a byte of `a` or one of `b`.
let a_size = a.value.size(dl);
let b_offset = a_size.align_to(b.value.align(dl).abi);
let has_inner_padding = a_size < b_offset;
let has_trailing_padding = b_offset + b.value.size(dl) < size;
if has_inner_padding || has_trailing_padding {
abi = Abi::Aggregate { sized: true };
}
}

return Ok(tcx.intern_layout(LayoutDetails {
variants: Variants::Single { index },
fields: FieldPlacement::Union(variants[index].len()),
abi,
align,
size: size.align_to(align.abi)
size,
}));
}

Expand Down
87 changes: 87 additions & 0 deletions src/test/ui/union/union-padding-preserved.rs
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.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

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());
}