Skip to content

Commit 290ac8d

Browse files
committed
Limit maximum alignment in #[repr(align)]
1 parent f4c675c commit 290ac8d

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/librustc_attr/builtin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,12 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
908908
let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
909909
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
910910
if literal.is_power_of_two() {
911-
// rustc::ty::layout::Align restricts align to <= 2^29
912-
if *literal <= 1 << 29 {
911+
// Many targets don't support global variables
912+
// with alignment greater than the page size.
913+
if *literal <= 4096 {
913914
Ok(*literal as u32)
914915
} else {
915-
Err("larger than 2^29")
916+
Err("larger than 4096")
916917
}
917918
} else {
918919
Err("not a power of two")

src/test/ui/structs-enums/align-struct.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ struct AlignContainsPacked4C {
6767

6868
// The align limit was originally smaller (2^15).
6969
// Check that it works with big numbers.
70-
#[repr(align(0x10000))]
70+
#[repr(align(0x1000))]
7171
struct AlignLarge {
72-
stuff: [u8; 0x10000],
72+
stuff: [u8; 0x1000],
7373
}
7474

7575
union UnionContainsAlign {
@@ -233,13 +233,13 @@ pub fn main() {
233233
assert!(is_aligned_to(&a, 16));
234234

235235
let mut large = box AlignLarge {
236-
stuff: [0; 0x10000],
236+
stuff: [0; 0x1000],
237237
};
238238
large.stuff[0] = 132;
239239
*large.stuff.last_mut().unwrap() = 102;
240240
assert_eq!(large.stuff[0], 132);
241241
assert_eq!(large.stuff.last(), Some(&102));
242-
assert_eq!(mem::align_of::<AlignLarge>(), 0x10000);
243-
assert_eq!(mem::align_of_val(&*large), 0x10000);
244-
assert!(is_aligned_to(&*large, 0x10000));
242+
assert_eq!(mem::align_of::<AlignLarge>(), 0x1000);
243+
assert_eq!(mem::align_of_val(&*large), 0x1000);
244+
assert!(is_aligned_to(&*large, 0x1000));
245245
}

0 commit comments

Comments
 (0)