Skip to content

Commit aa0b3c7

Browse files
committed
error on alignments greater than isize::MAX
1 parent 3678036 commit aa0b3c7

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ passes_remove_fields =
601601
passes_repr_align_function =
602602
`repr(align)` attributes on functions are unstable
603603
604+
passes_repr_align_greater_than_target_max =
605+
alignment must not be greater than `isize::MAX` bytes
606+
.note = `isize::MAX` is {$size} for the current target
607+
604608
passes_repr_conflicting =
605609
conflicting representation hints
606610

compiler/rustc_passes/src/check_attr.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_session::lint::builtin::{
3434
use rustc_session::parse::feature_err;
3535
use rustc_span::symbol::{Symbol, kw, sym};
3636
use rustc_span::{BytePos, DUMMY_SP, Span};
37+
use rustc_target::abi::Size;
3738
use rustc_target::spec::abi::Abi;
3839
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
3940
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
@@ -1783,7 +1784,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17831784
| Target::Union
17841785
| Target::Enum
17851786
| Target::Fn
1786-
| Target::Method(_) => continue,
1787+
| Target::Method(_) => {}
17871788
_ => {
17881789
self.dcx().emit_err(
17891790
errors::AttrApplication::StructEnumFunctionMethodUnion {
@@ -1793,6 +1794,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17931794
);
17941795
}
17951796
}
1797+
1798+
self.check_align_value(hint);
17961799
}
17971800
sym::packed => {
17981801
if target != Target::Struct && target != Target::Union {
@@ -1890,6 +1893,45 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18901893
}
18911894
}
18921895

1896+
fn check_align_value(&self, item: &MetaItemInner) {
1897+
match item.singleton_lit_list() {
1898+
Some((
1899+
_,
1900+
MetaItemLit {
1901+
kind: ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed), ..
1902+
},
1903+
)) => {
1904+
let val = literal.get() as u64;
1905+
if val > 2_u64.pow(29) {
1906+
// for values greater than 2^29, a different error will be emitted, make sure that happens
1907+
self.dcx().span_delayed_bug(
1908+
item.span(),
1909+
"alignment greater than 2^29 should be errored on elsewhere",
1910+
);
1911+
} else {
1912+
// only do this check when <= 2^29 to prevent duplicate errors:
1913+
// alignment greater than 2^29 not supported
1914+
// alignment is too large for the current target
1915+
1916+
let max =
1917+
Size::from_bits(self.tcx.sess.target.pointer_width).signed_int_max() as u64;
1918+
if val > max {
1919+
self.dcx().emit_err(errors::InvalidReprAlignForTarget {
1920+
span: item.span(),
1921+
size: max,
1922+
});
1923+
}
1924+
}
1925+
}
1926+
1927+
// if the attribute is malformed, singleton_lit_list may not be of the expected type or may be None
1928+
// but an error will have already been emitted, so this code should just skip such attributes
1929+
Some((_, _)) | None => {
1930+
self.dcx().span_delayed_bug(item.span(), "malformed repr(align(N))");
1931+
}
1932+
}
1933+
}
1934+
18931935
fn check_used(&self, attrs: &[Attribute], target: Target, target_span: Span) {
18941936
let mut used_linker_span = None;
18951937
let mut used_compiler_span = None;

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ pub(crate) struct ReprConflicting {
532532
pub hint_spans: Vec<Span>,
533533
}
534534

535+
#[derive(Diagnostic)]
536+
#[diag(passes_repr_align_greater_than_target_max, code = E0589)]
537+
#[note]
538+
pub(crate) struct InvalidReprAlignForTarget {
539+
#[primary_span]
540+
pub span: Span,
541+
pub size: u64,
542+
}
543+
535544
#[derive(LintDiagnostic)]
536545
#[diag(passes_repr_conflicting, code = E0566)]
537546
pub(crate) struct ReprConflictingLint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0589]: alignment must not be greater than `isize::MAX` bytes
2+
--> $DIR/repr_align_greater_usize.rs:22:8
3+
|
4+
LL | #[repr(align(32768))]
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `isize::MAX` is 32767 for the current target
8+
9+
error[E0589]: alignment must not be greater than `isize::MAX` bytes
10+
--> $DIR/repr_align_greater_usize.rs:25:8
11+
|
12+
LL | #[repr(align(65536))]
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: `isize::MAX` is 32767 for the current target
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0589`.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ revisions: msp430 aarch32
2+
//@[msp430] check-fail
3+
//@[msp430] needs-llvm-components: msp430
4+
//@[msp430] compile-flags: --target=msp430-none-elf
5+
//@[aarch32] build-pass
6+
//@[aarch32] needs-llvm-components: arm
7+
//@[aarch32] compile-flags: --target=thumbv7m-none-eabi
8+
9+
// We should fail to compute alignment for types aligned higher than usize::MAX.
10+
// We can't handle alignments that require all 32 bits, so this only affects 16-bit.
11+
12+
#![feature(lang_items, no_core)]
13+
#![no_core]
14+
#![crate_type = "lib"]
15+
16+
#[lang = "sized"]
17+
trait Sized {}
18+
19+
#[repr(align(16384))]
20+
struct Kitten;
21+
22+
#[repr(align(32768))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
23+
struct Cat;
24+
25+
#[repr(align(65536))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
26+
struct BigCat;

0 commit comments

Comments
 (0)