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

Make struct layout not depend on unsizeable tail #112062

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ fn univariant(
if optimize && fields.len() > 1 {
let end = if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
let optimizing = &mut inverse_memory_index.raw[..end];
let fields_excluding_tail = &fields.raw[..end];

// If `-Z randomize-layout` was enabled for the type definition we can shuffle
// the field ordering to try and catch some code making assumptions about layouts
Expand All @@ -844,8 +845,11 @@ fn univariant(
}
// Otherwise we just leave things alone and actually optimize the type's fields
} else {
let max_field_align = fields.iter().map(|f| f.align().abi.bytes()).max().unwrap_or(1);
let largest_niche_size = fields
// To allow unsizing `&Foo<Type>` -> `&Foo<dyn Trait>`, the layout of the struct must
// not depend on the layout of the tail.
let max_field_align =
fields_excluding_tail.iter().map(|f| f.align().abi.bytes()).max().unwrap_or(1);
let largest_niche_size = fields_excluding_tail
Comment on lines +854 to +858
Copy link
Member

@the8472 the8472 May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are necessary to fix it but I'm not sure if they're sufficient.

If an unsizing coercion could change which niche is considered the largest or how many bytes it spans that could alter the chosen layout in LayoutCalculator::univariant.
I haven't constructed an example yet where that actually happens and I'm not sure if it can happen (changing available niches between coercions seems iffy). But if you want to be conservative then disabling the alt-layout branch for unsized structs would be a safe choice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a case that still fails with this PR:

#![feature(offset_of)]

#[derive(Clone)]
struct WideptrField<T: ?Sized> {
  first: usize,
  second: usize,
  niche: NicheAtEnd,
  tail: T,
}

#[derive(Clone)]
#[repr(C)]
struct NicheAtEnd {
  arr: [u8; 7],
  b: bool,
}

type Tail = [bool; 8];

fn main() {
  let sized = Box::new(WideptrField {
    first: 0xdead,
    second: 0xbeef,
    niche: NicheAtEnd {
      arr: [0; 7],
      b: false,
    },
    tail: [false; 8] as Tail,
  });

  let unsized_value: Box<WideptrField<dyn Send>> = sized.clone();

  println!("offset in niche, sized   = {}", core::mem::offset_of!(WideptrField<Tail>, niche));
  println!("offset in niche, unsized = {}", core::mem::offset_of!(WideptrField<dyn Send>, niche));
}

prints

offset in niche, sized   = 0
offset in niche, unsized = 16

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a change that fixes this case. Feel free to force-push over my commit if you already have your own.

.iter()
.filter_map(|f| f.largest_niche())
.map(|n| n.available(dl))
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/layout/issue-112048-unsizing-field-order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// run-pass

// Check that unsizing doesn't reorder fields.

#![allow(dead_code)]

use std::fmt::Debug;

#[derive(Debug)]
struct GcNode<T: ?Sized> {
gets_swapped_with_next: usize,
next: Option<&'static GcNode<dyn Debug>>,
tail: T,
}

fn main() {
let node: Box<GcNode<dyn Debug>> = Box::new(GcNode {
gets_swapped_with_next: 42,
next: None,
tail: Box::new(1),
});

assert_eq!(node.gets_swapped_with_next, 42);
assert!(node.next.is_none());
}