-
-
Notifications
You must be signed in to change notification settings - Fork 39
Ensure that #[repr(packed)] cannot be used on #[pin_projectable] structs #34
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
Merged
+180
−37
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6db8e8b
Ensure that #[repr(packed)] cannot be used on #[pin_projectable] structs
Aaron1011 484e554
Allow clippy::use_self
Aaron1011 799aeb0
Properly generate tuple index
Aaron1011 bbbda4c
Run 'cargo fmt'
Aaron1011 db3031c
Add another clippy allow
Aaron1011 8f6d7fd
Apply suggestions from code review
taiki-e 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| #![warn(unsafe_code)] | ||
| #![warn(rust_2018_idioms)] | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::cell::Cell; | ||
|
|
||
| // Ensure that the compiler doesn't copy the fields | ||
| // of #[repr(packed)] types during drop, if the field has alignment 1 | ||
| // (that is, any reference to the field is guaranteed to have proper alignment) | ||
| // We are currently unable to statically prevent the usage of #[pin_projectable] | ||
| // on #[repr(packed)] types composed entirely of fields of alignment 1. | ||
| // This shouldn't lead to undefined behavior, as long as the compiler doesn't | ||
| // try to move the field anyway during drop. | ||
| // | ||
| // This tests validates that the compiler is doing what we expect. | ||
| #[test] | ||
| fn weird_repr_packed() { | ||
| // We keep track of the field address during | ||
| // drop using a thread local, to avoid changing | ||
| // the layout of our #[repr(packed)] type | ||
taiki-e marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| thread_local! { | ||
| static FIELD_ADDR: Cell<usize> = Cell::new(0); | ||
| } | ||
|
|
||
| #[repr(packed)] | ||
| struct Foo { | ||
| field: u8, | ||
| } | ||
|
|
||
| impl Drop for Foo { | ||
| fn drop(&mut self) { | ||
| FIELD_ADDR.with(|f| { | ||
| f.set(&self.field as *const u8 as usize); | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| let field_addr = { | ||
| // We let this field drop by going out of scope, | ||
| // rather than explicitly calling drop(foo). | ||
| // Calling drop(foo) causes 'foo' to be moved | ||
| // into the 'drop' function, resulinting a different | ||
taiki-e marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // address | ||
taiki-e marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let foo = Foo { field: 27 }; | ||
| let field_addr = &foo.field as *const u8 as usize; | ||
| field_addr | ||
| }; | ||
| assert_eq!(field_addr, FIELD_ADDR.with(|f| f.get())); | ||
| } | ||
This file contains hidden or 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,14 @@ | ||
| // force-host | ||
| // no-prefer-dynamic | ||
|
|
||
| #![crate_type = "proc-macro"] | ||
|
|
||
| extern crate proc_macro; | ||
|
|
||
| use proc_macro::TokenStream; | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn hidden_repr(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| format!("#[repr({})] {}", attr, item).parse().unwrap() | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,26 +1,14 @@ | ||
| error: pin_projectable may not be used on #[repr(packed)] types | ||
| --> $DIR/packed.rs:8:8 | ||
| --> $DIR/packed.rs:9:8 | ||
| | | ||
| 8 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| 9 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| | ^^^^^^ | ||
|
|
||
| error: pin_projectable may not be used on #[repr(packed)] types | ||
| --> $DIR/packed.rs:15:8 | ||
| --> $DIR/packed.rs:16:8 | ||
| | | ||
| 15 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| 16 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| | ^^^^^^ | ||
|
|
||
| error: pin_projectable may not be used on #[repr(packed)] types | ||
| --> $DIR/packed.rs:23:8 | ||
| | | ||
| 23 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| | ^^^^^^ | ||
|
|
||
| error: pin_projectable may not be used on #[repr(packed)] types | ||
| --> $DIR/packed.rs:28:8 | ||
| | | ||
| 28 | #[repr(packed, C)] //~ ERROR may not be used on #[repr(packed)] type | ||
| | ^^^^^^ | ||
|
|
||
| error: aborting due to 4 previous errors | ||
| error: aborting due to 2 previous errors | ||
|
|
This file contains hidden or 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,16 @@ | ||
| // compile-fail | ||
| // aux-build:sneaky_macro.rs | ||
|
|
||
| #[macro_use] | ||
| extern crate sneaky_macro; | ||
|
|
||
| use pin_project::pin_projectable; | ||
|
|
||
| #[pin_projectable] //~ ERROR borrow of packed field is unsafe and requires unsafe function or block | ||
| #[hidden_repr(packed)] | ||
| struct Bar { | ||
| #[pin] | ||
| field: u32 | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or 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,17 @@ | ||
| error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) | ||
| --> $DIR/packed_sneaky.rs:9:1 | ||
| | | ||
| 9 | #[pin_projectable] //~ ERROR borrow of packed field is unsafe and requires unsafe function or block | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| note: lint level defined here | ||
| --> $DIR/packed_sneaky.rs:9:1 | ||
| | | ||
| 9 | #[pin_projectable] //~ ERROR borrow of packed field is unsafe and requires unsafe function or block | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043> | ||
| = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior | ||
|
|
||
| error: aborting due to previous error | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.