Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Can bypass #[repr(packed)] checking since rust 1.57 #342

Closed
macpp opened this issue Mar 12, 2022 · 2 comments
Closed

Can bypass #[repr(packed)] checking since rust 1.57 #342

macpp opened this issue Mar 12, 2022 · 2 comments
Labels
C-bug Category: related to a bug. C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream) I-unsound A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness

Comments

@macpp
Copy link

macpp commented Mar 12, 2022

Since rust 1.57 it is possible to put proc macro attributes after derive (see rust-lang/rust#81119)
so given proc macro crate like that:

use proc_macro::TokenStream;
use syn::{parse_macro_input, parse_quote, ItemStruct};
use quote::quote;

#[proc_macro_attribute]
pub fn add_repr(_:TokenStream, item: TokenStream) -> TokenStream {
    let mut item = parse_macro_input!(item as ItemStruct);
    item.attrs.push(parse_quote!{#[add_repr_hidden]});
    quote!{#item}.into()
}

#[proc_macro_attribute]
pub fn add_repr_hidden(_:TokenStream, item: TokenStream) -> TokenStream {
    let mut item = parse_macro_input!(item as ItemStruct);
    item.attrs.push(parse_quote!{#[repr(packed)]});
    quote!{#item}.into()
}

and following usage :

#[pin_project]
#[add_repr]
pub struct Test {
    x: i32
}

__PinProjectInternalDerive won't notice that at the end of the expansion there is #[repr(packed)] on struct
Of course, --cap-lints=warn is still required to actually compile it
I'm not sure how probable it is to trigger this problem in real code base..

@taiki-e taiki-e added the I-unsound A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Mar 12, 2022
@taiki-e taiki-e changed the title Can bypass #[repr(packed)] checking Can bypass #[repr(packed)] checking since rust 1.57 Mar 12, 2022
@taiki-e taiki-e added C-bug Category: related to a bug. C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream) S-blocked Status: Blocked on something else labels Mar 12, 2022
@taiki-e
Copy link
Owner

taiki-e commented Mar 12, 2022

As well as taiki-e/pin-project-lite#26, it should eventually be fixed by unaligned_references becoming a hard error.

bors bot added a commit that referenced this issue Mar 12, 2022
343: Add test for issue 342 r=taiki-e a=taiki-e

Add test for #342

Co-authored-by: Taiki Endo <te316e89@gmail.com>
@taiki-e
Copy link
Owner

taiki-e commented Feb 1, 2023

unaligned_references is now a hard error 🎉: rust-lang/rust#102513

@taiki-e taiki-e closed this as completed Feb 1, 2023
@taiki-e taiki-e removed the S-blocked Status: Blocked on something else label Feb 1, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: related to a bug. C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream) I-unsound A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
Projects
None yet
Development

No branches or pull requests

2 participants