Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a2e905f

Browse files
committed
Correctly align offset for dst field projections
Fixes rust-lang#681
1 parent cd0e862 commit a2e905f

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

example/dst-field-align.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// run-pass
2+
#![allow(dead_code)]
3+
struct Foo<T: ?Sized> {
4+
a: u16,
5+
b: T
6+
}
7+
8+
trait Bar {
9+
fn get(&self) -> usize;
10+
}
11+
12+
impl Bar for usize {
13+
fn get(&self) -> usize { *self }
14+
}
15+
16+
struct Baz<T: ?Sized> {
17+
a: T
18+
}
19+
20+
struct HasDrop<T: ?Sized> {
21+
ptr: Box<usize>,
22+
data: T
23+
}
24+
25+
fn main() {
26+
// Test that zero-offset works properly
27+
let b : Baz<usize> = Baz { a: 7 };
28+
assert_eq!(b.a.get(), 7);
29+
let b : &Baz<dyn Bar> = &b;
30+
assert_eq!(b.a.get(), 7);
31+
32+
// Test that the field is aligned properly
33+
let f : Foo<usize> = Foo { a: 0, b: 11 };
34+
assert_eq!(f.b.get(), 11);
35+
let ptr1 : *const u8 = &f.b as *const _ as *const u8;
36+
37+
let f : &Foo<dyn Bar> = &f;
38+
let ptr2 : *const u8 = &f.b as *const _ as *const u8;
39+
assert_eq!(f.b.get(), 11);
40+
41+
// The pointers should be the same
42+
assert_eq!(ptr1, ptr2);
43+
44+
// Test that nested DSTs work properly
45+
let f : Foo<Foo<usize>> = Foo { a: 0, b: Foo { a: 1, b: 17 }};
46+
assert_eq!(f.b.b.get(), 17);
47+
let f : &Foo<Foo<dyn Bar>> = &f;
48+
assert_eq!(f.b.b.get(), 17);
49+
50+
// Test that get the pointer via destructuring works
51+
52+
let f : Foo<usize> = Foo { a: 0, b: 11 };
53+
let f : &Foo<dyn Bar> = &f;
54+
let &Foo { a: _, b: ref bar } = f;
55+
assert_eq!(bar.get(), 11);
56+
57+
// Make sure that drop flags don't screw things up
58+
59+
let d : HasDrop<Baz<[i32; 4]>> = HasDrop {
60+
ptr: Box::new(0),
61+
data: Baz { a: [1,2,3,4] }
62+
};
63+
assert_eq!([1,2,3,4], d.data.a);
64+
65+
let d : &HasDrop<Baz<[i32]>> = &d;
66+
assert_eq!(&[1,2,3,4], &d.data.a);
67+
}

src/value_and_place.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,54 @@ use crate::prelude::*;
33
fn codegen_field<'tcx>(
44
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
55
base: Value,
6+
extra: Option<Value>,
67
layout: TyLayout<'tcx>,
78
field: mir::Field,
89
) -> (Value, TyLayout<'tcx>) {
910
let field_offset = layout.fields.offset(field.index());
10-
let field_ty = layout.field(&*fx, field.index());
11-
if field_offset.bytes() > 0 {
12-
(
13-
fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
14-
field_ty,
15-
)
11+
let field_layout = layout.field(&*fx, field.index());
12+
13+
let simple = |fx: &mut FunctionCx<_>| {
14+
if field_offset.bytes() > 0 {
15+
(
16+
fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
17+
field_layout,
18+
)
19+
} else {
20+
(base, field_layout)
21+
}
22+
};
23+
24+
if let Some(extra) = extra {
25+
if !field_layout.is_unsized() {
26+
return simple(fx);
27+
}
28+
match field_layout.ty.sty {
29+
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(fx),
30+
ty::Adt(def, _) if def.repr.packed() => {
31+
assert_eq!(layout.align.abi.bytes(), 1);
32+
return simple(fx);
33+
}
34+
_ => {
35+
// We have to align the offset for DST's
36+
let unaligned_offset = field_offset.bytes();
37+
let (_, unsized_align) = crate::unsize::size_and_align_of_dst(fx, field_layout.ty, extra);
38+
39+
let one = fx.bcx.ins().iconst(pointer_ty(fx.tcx), 1);
40+
let align_sub_1 = fx.bcx.ins().isub(unsized_align, one);
41+
let and_lhs = fx.bcx.ins().iadd_imm(align_sub_1, unaligned_offset as i64);
42+
let zero = fx.bcx.ins().iconst(pointer_ty(fx.tcx), 0);
43+
let and_rhs = fx.bcx.ins().isub(zero, unsized_align);
44+
let offset = fx.bcx.ins().band(and_lhs, and_rhs);
45+
46+
(
47+
fx.bcx.ins().iadd(base, offset),
48+
field_layout,
49+
)
50+
}
51+
}
1652
} else {
17-
(base, field_ty)
53+
simple(fx)
1854
}
1955
}
2056

@@ -125,7 +161,7 @@ impl<'tcx> CValue<'tcx> {
125161
_ => bug!("place_field for {:?}", self),
126162
};
127163

128-
let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
164+
let (field_ptr, field_layout) = codegen_field(fx, base, None, layout, field);
129165
CValue::by_ref(field_ptr, field_layout)
130166
}
131167

@@ -431,7 +467,7 @@ impl<'tcx> CPlace<'tcx> {
431467
let layout = self.layout();
432468
let (base, extra) = self.to_addr_maybe_unsized(fx);
433469

434-
let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
470+
let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
435471
if field_layout.is_unsized() {
436472
CPlace::for_addr_with_extra(field_ptr, extra.unwrap(), field_layout)
437473
} else {

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ $RUSTC example/alloc_example.rs --crate-type bin
5050

5151
jit std_example example/std_example.rs
5252

53+
echo "[AOT] dst_field_align"
54+
$RUSTC example/dst-field-align.rs -Zmir-opt-level=2 --crate-name dst_field_align --crate-type bin
55+
./target/out/dst_field_align
56+
5357
echo "[AOT] std_example"
5458
$RUSTC example/std_example.rs --crate-type bin
5559
./target/out/std_example

0 commit comments

Comments
 (0)