-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Limit maximum alignment in #[repr(align)] to 4K #70193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -908,11 +908,12 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> { | |
let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> { | ||
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node { | ||
if literal.is_power_of_two() { | ||
// rustc::ty::layout::Align restricts align to <= 2^29 | ||
if *literal <= 1 << 29 { | ||
// Many targets don't support global variables | ||
// with alignment greater than the page size. | ||
if *literal <= 4096 { | ||
Ok(*literal as u32) | ||
} else { | ||
Err("larger than 2^29") | ||
Err("larger than 4096") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we land this, then it would be good to mention what this magic number is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, let's add a regression test at |
||
} | ||
} else { | ||
Err("not a power of two") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,9 +67,9 @@ struct AlignContainsPacked4C { | |
|
||
// The align limit was originally smaller (2^15). | ||
// Check that it works with big numbers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Irrelevant for crater run but this comment is now wrong. |
||
#[repr(align(0x10000))] | ||
#[repr(align(0x1000))] | ||
struct AlignLarge { | ||
stuff: [u8; 0x10000], | ||
stuff: [u8; 0x1000], | ||
} | ||
|
||
union UnionContainsAlign { | ||
|
@@ -233,13 +233,13 @@ pub fn main() { | |
assert!(is_aligned_to(&a, 16)); | ||
|
||
let mut large = box AlignLarge { | ||
stuff: [0; 0x10000], | ||
stuff: [0; 0x1000], | ||
}; | ||
large.stuff[0] = 132; | ||
*large.stuff.last_mut().unwrap() = 102; | ||
assert_eq!(large.stuff[0], 132); | ||
assert_eq!(large.stuff.last(), Some(&102)); | ||
assert_eq!(mem::align_of::<AlignLarge>(), 0x10000); | ||
assert_eq!(mem::align_of_val(&*large), 0x10000); | ||
assert!(is_aligned_to(&*large, 0x10000)); | ||
assert_eq!(mem::align_of::<AlignLarge>(), 0x1000); | ||
assert_eq!(mem::align_of_val(&*large), 0x1000); | ||
assert!(is_aligned_to(&*large, 0x1000)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And also page size is always at last 4096, I take it? That is left implicit here, right now the comment doesn't entirely match what the code does.