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

fix detecting references to packed unsized fields #115583

Merged
merged 2 commits into from
Oct 7, 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
Next Next commit
fix detecting references to packed unsized fields
  • Loading branch information
RalfJung committed Sep 5, 2023
commit 8b3435c10f458b840d9cba31f2f35586e9b31189
4 changes: 3 additions & 1 deletion compiler/rustc_const_eval/src/util/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 22,11 @@ where

let ty = place.ty(local_decls, tcx).ty;
match tcx.layout_of(param_env.and(ty)) {
Ok(layout) if layout.align.abi <= pack => {
Ok(layout) if layout.align.abi <= pack && layout.is_sized() => {
// If the packed alignment is greater or equal to the field alignment, the type won't be
// further disaligned.
// However we need to ensure the field is sized; for unsized fields, `layout.align` is
// just an approximation.
debug!(
"is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
place,
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/lint/unaligned_references.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,6 @@
use std::mem::ManuallyDrop;
use std::fmt::Debug;

#[repr(packed)]
pub struct Good {
data: u64,
Expand Down Expand Up @@ -27,6 30,16 @@ impl Foo for Packed2 {
}
}

// Test for #115396
fn packed_dyn() {
#[repr(packed)]
struct Unaligned<T: ? Sized>(ManuallyDrop<T>);

let ref local = Unaligned(ManuallyDrop::new([3, 5, 8u64]));
let foo: &Unaligned<dyn Debug> = &*local;
println!("{:?}", &*foo.0); //~ ERROR reference to packed field
}

fn main() {
unsafe {
let good = Good { data: 0, ptr: &0, data2: [0, 0], aligned: [0; 32] };
Expand Down
32 changes: 21 additions & 11 deletions tests/ui/lint/unaligned_references.stderr
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:25:13
--> $DIR/unaligned_references.rs:28:13
|
LL | &self.x;
| ^^^^^^^
Expand All @@ -9,7 9,17 @@ LL | &self.x;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:34:17
--> $DIR/unaligned_references.rs:40:24
|
LL | println!("{:?}", &*foo.0);
| ^^^^^
|
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:47:17
|
LL | let _ = &good.ptr;
| ^^^^^^^^^
Expand All @@ -19,7 29,7 @@ LL | let _ = &good.ptr;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:35:17
--> $DIR/unaligned_references.rs:48:17
|
LL | let _ = &good.data;
| ^^^^^^^^^^
Expand All @@ -29,7 39,7 @@ LL | let _ = &good.data;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:37:17
--> $DIR/unaligned_references.rs:50:17
|
LL | let _ = &good.data as *const _;
| ^^^^^^^^^^
Expand All @@ -39,7 49,7 @@ LL | let _ = &good.data as *const _;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:38:27
--> $DIR/unaligned_references.rs:51:27
|
LL | let _: *const _ = &good.data;
| ^^^^^^^^^^
Expand All @@ -49,7 59,7 @@ LL | let _: *const _ = &good.data;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:40:17
--> $DIR/unaligned_references.rs:53:17
|
LL | let _ = good.data.clone();
| ^^^^^^^^^
Expand All @@ -59,7 69,7 @@ LL | let _ = good.data.clone();
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:42:17
--> $DIR/unaligned_references.rs:55:17
|
LL | let _ = &good.data2[0];
| ^^^^^^^^^^^^^^
Expand All @@ -69,7 79,7 @@ LL | let _ = &good.data2[0];
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:51:17
--> $DIR/unaligned_references.rs:64:17
|
LL | let _ = &packed2.x;
| ^^^^^^^^^^
Expand All @@ -79,7 89,7 @@ LL | let _ = &packed2.x;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:90:20
--> $DIR/unaligned_references.rs:103:20
|
LL | let _ref = &m1.1.a;
| ^^^^^^^
Expand All @@ -89,7 99,7 @@ LL | let _ref = &m1.1.a;
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error[E0793]: reference to packed field is unaligned
--> $DIR/unaligned_references.rs:93:20
--> $DIR/unaligned_references.rs:106:20
|
LL | let _ref = &m2.1.a;
| ^^^^^^^
Expand All @@ -98,6 108,6 @@ LL | let _ref = &m2.1.a;
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0793`.
Loading