Skip to content

Commit 646c280

Browse files
committed
[AIX] Lint on structs that have a different alignment in AIX's C ABI
1 parent 27f3361 commit 646c280

File tree

7 files changed

+394
-16
lines changed

7 files changed

+394
-16
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ lint_unused_result = unused result of type `{$ty}`
970970
971971
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
972972
973+
lint_uses_power_alignment = repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
974+
973975
lint_variant_size_differences =
974976
enum variant is more than three times larger ({$largest} bytes) than the next largest
975977

compiler/rustc_lint/src/lints.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,10 @@ pub(crate) struct OverflowingLiteral<'a> {
16921692
pub lit: String,
16931693
}
16941694

1695+
#[derive(LintDiagnostic)]
1696+
#[diag(lint_uses_power_alignment)]
1697+
pub(crate) struct UsesPowerAlignment;
1698+
16951699
#[derive(LintDiagnostic)]
16961700
#[diag(lint_unused_comparisons)]
16971701
pub(crate) struct UnusedComparisons;

compiler/rustc_lint/src/types.rs

+112-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::iter;
22
use std::ops::ControlFlow;
33

4-
use rustc_abi::{BackendRepr, ExternAbi, TagEncoding, Variants, WrappingRange};
4+
use rustc_abi::{BackendRepr, ExternAbi, TagEncoding, VariantIdx, Variants, WrappingRange};
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::DiagMessage;
77
use rustc_hir::{Expr, ExprKind, LangItem};
88
use rustc_middle::bug;
99
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
1010
use rustc_middle::ty::{
11-
self, AdtKind, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
11+
self, Adt, AdtKind, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
12+
TypeVisitableExt,
1213
};
1314
use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass};
1415
use rustc_span::def_id::LocalDefId;
@@ -23,7 +24,7 @@ use crate::lints::{
2324
AmbiguousWidePointerComparisonsAddrSuggestion, AtomicOrderingFence, AtomicOrderingLoad,
2425
AtomicOrderingStore, ImproperCTypes, InvalidAtomicOrderingDiag, InvalidNanComparisons,
2526
InvalidNanComparisonsSuggestion, UnpredictableFunctionPointerComparisons,
26-
UnpredictableFunctionPointerComparisonsSuggestion, UnusedComparisons,
27+
UnpredictableFunctionPointerComparisonsSuggestion, UnusedComparisons, UsesPowerAlignment,
2728
VariantSizeDifferencesDiag,
2829
};
2930
use crate::{LateContext, LateLintPass, LintContext, fluent_generated as fluent};
@@ -727,7 +728,43 @@ declare_lint! {
727728
"proper use of libc types in foreign item definitions"
728729
}
729730

730-
declare_lint_pass!(ImproperCTypesDefinitions => [IMPROPER_CTYPES_DEFINITIONS]);
731+
declare_lint! {
732+
/// In its platform C ABI, AIX uses the "power" (as in PowerPC) alignment
733+
/// rule (detailed in https://www.ibm.com/docs/en/xl-c-and-cpp-aix/16.1?topic=data-using-alignment-modes#alignment),
734+
/// which can also be set for XLC by `#pragma align(power)` or
735+
/// `-qalign=power`. Aggregates with a floating-point type as the
736+
/// recursively first field (as in "at offset 0") modify the layout of
737+
/// *subsequent* fields of the associated structs to use an alignment value
738+
/// where the floating-point type is aligned on a 4-byte boundary.
739+
///
740+
/// The power alignment rule for structs needed for C compatibility is
741+
/// unimplementable within `repr(C)` in the compiler without building in
742+
/// handling of references to packed fields and infectious nested layouts,
743+
/// so a warning is produced in these situations.
744+
///
745+
/// ### Example
746+
///
747+
/// ```rust
748+
/// pub struct Floats {
749+
/// a: f64,
750+
/// b: u8,
751+
/// c: f64,
752+
/// }
753+
/// ```
754+
///
755+
/// The power alignment rule specifies that the above struct has the
756+
/// following alignment:
757+
/// - offset_of!(Floats, a) == 0
758+
/// - offset_of!(Floats, b) == 8
759+
/// - offset_of!(Floats, c) == 12
760+
/// However, rust currently aligns `c` at offset_of!(Floats, c) == 16.
761+
/// Thus, a warning should be produced for the above struct in this case.
762+
USES_POWER_ALIGNMENT,
763+
Warn,
764+
"Structs do not follow the power alignment rule under repr(C)"
765+
}
766+
767+
declare_lint_pass!(ImproperCTypesDefinitions => [IMPROPER_CTYPES_DEFINITIONS, USES_POWER_ALIGNMENT]);
731768

732769
#[derive(Clone, Copy)]
733770
pub(crate) enum CItemKind {
@@ -1539,6 +1576,71 @@ impl ImproperCTypesDefinitions {
15391576
vis.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, true, false);
15401577
}
15411578
}
1579+
1580+
fn check_arg_for_power_alignment<'tcx>(
1581+
&mut self,
1582+
cx: &LateContext<'tcx>,
1583+
ty: Ty<'tcx>,
1584+
) -> bool {
1585+
// Structs (under repr(C)) follow the power alignment rule if:
1586+
// - the first field of the struct is a floating-point type that
1587+
// is greater than 4-bytes, or
1588+
// - the first field of the struct is an aggregate whose
1589+
// recursively first field is a floating-point type greater than
1590+
// 4 bytes.
1591+
if cx.tcx.sess.target.os != "aix" {
1592+
return false;
1593+
}
1594+
if ty.is_floating_point() && ty.primitive_size(cx.tcx).bytes() > 4 {
1595+
return true;
1596+
} else if let Adt(adt_def, _) = ty.kind()
1597+
&& adt_def.is_struct()
1598+
{
1599+
let struct_variant = adt_def.variant(VariantIdx::ZERO);
1600+
// Within a nested struct, all fields are examined to correctly
1601+
// report if any fields after the nested struct within the
1602+
// original struct are misaligned.
1603+
for struct_field in &struct_variant.fields {
1604+
let field_ty = cx.tcx.type_of(struct_field.did).instantiate_identity();
1605+
if self.check_arg_for_power_alignment(cx, field_ty) {
1606+
return true;
1607+
}
1608+
}
1609+
}
1610+
return false;
1611+
}
1612+
1613+
fn check_struct_for_power_alignment<'tcx>(
1614+
&mut self,
1615+
cx: &LateContext<'tcx>,
1616+
item: &'tcx hir::Item<'tcx>,
1617+
) {
1618+
let adt_def = cx.tcx.adt_def(item.owner_id.to_def_id());
1619+
if adt_def.repr().c()
1620+
&& !adt_def.repr().packed()
1621+
&& cx.tcx.sess.target.os == "aix"
1622+
&& !adt_def.all_fields().next().is_none()
1623+
{
1624+
let struct_variant_data = item.expect_struct().0;
1625+
for (index, ..) in struct_variant_data.fields().iter().enumerate() {
1626+
// Struct fields (after the first field) are checked for the
1627+
// power alignment rule, as fields after the first are likely
1628+
// to be the fields that are misaligned.
1629+
if index != 0 {
1630+
let first_field_def = struct_variant_data.fields()[index];
1631+
let def_id = first_field_def.def_id;
1632+
let ty = cx.tcx.type_of(def_id).instantiate_identity();
1633+
if self.check_arg_for_power_alignment(cx, ty) {
1634+
cx.emit_span_lint(
1635+
USES_POWER_ALIGNMENT,
1636+
first_field_def.span,
1637+
UsesPowerAlignment,
1638+
);
1639+
}
1640+
}
1641+
}
1642+
}
1643+
}
15421644
}
15431645

15441646
/// `ImproperCTypesDefinitions` checks items outside of foreign items (e.g. stuff that isn't in
@@ -1562,8 +1664,13 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
15621664
}
15631665
// See `check_fn`..
15641666
hir::ItemKind::Fn { .. } => {}
1667+
// Structs are checked based on if they follow the power alignment
1668+
// rule (under repr(C)).
1669+
hir::ItemKind::Struct(..) => {
1670+
self.check_struct_for_power_alignment(cx, item);
1671+
}
15651672
// See `check_field_def`..
1566-
hir::ItemKind::Union(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) => {}
1673+
hir::ItemKind::Union(..) | hir::ItemKind::Enum(..) => {}
15671674
// Doesn't define something that can contain a external type to be checked.
15681675
hir::ItemKind::Impl(..)
15691676
| hir::ItemKind::TraitAlias(..)
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//@ check-pass
2+
//@ compile-flags: --target powerpc64-ibm-aix
3+
//@ needs-llvm-components: powerpc
4+
//@ add-core-stubs
5+
#![feature(no_core)]
6+
#![no_core]
7+
#![no_std]
8+
9+
extern crate minicore;
10+
use minicore::*;
11+
12+
#[warn(uses_power_alignment)]
13+
14+
#[repr(C)]
15+
pub struct Floats {
16+
a: f64,
17+
b: u8,
18+
c: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
19+
d: f32,
20+
}
21+
22+
pub struct Floats2 {
23+
a: f64,
24+
b: u32,
25+
c: f64,
26+
}
27+
28+
#[repr(C)]
29+
pub struct Floats3 {
30+
a: f32,
31+
b: f32,
32+
c: i64,
33+
}
34+
35+
#[repr(C)]
36+
pub struct Floats4 {
37+
a: u64,
38+
b: u32,
39+
c: f32,
40+
}
41+
42+
#[repr(C)]
43+
pub struct Floats5 {
44+
a: f32,
45+
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
46+
c: f32,
47+
}
48+
49+
#[repr(C)]
50+
pub struct FloatAgg1 {
51+
x: Floats,
52+
y: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
53+
}
54+
55+
#[repr(C)]
56+
pub struct FloatAgg2 {
57+
x: i64,
58+
y: Floats, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
59+
}
60+
61+
#[repr(C)]
62+
pub struct FloatAgg3 {
63+
x: FloatAgg1,
64+
// NOTE: the "power" alignment rule is infectious to nested struct fields.
65+
y: FloatAgg2, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
66+
z: FloatAgg2, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
67+
}
68+
69+
#[repr(C)]
70+
pub struct FloatAgg4 {
71+
x: FloatAgg1,
72+
y: FloatAgg2, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
73+
}
74+
75+
#[repr(C)]
76+
pub struct FloatAgg5 {
77+
x: FloatAgg1,
78+
y: FloatAgg2, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
79+
z: FloatAgg3, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
80+
}
81+
82+
#[repr(C)]
83+
pub struct FloatAgg6 {
84+
x: i64,
85+
y: Floats, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
86+
z: u8,
87+
}
88+
89+
#[repr(C)]
90+
pub struct FloatAgg7 {
91+
x: i64,
92+
y: Floats, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
93+
z: u8,
94+
zz: f32,
95+
}
96+
97+
#[repr(C)]
98+
pub struct A {
99+
d: f64,
100+
}
101+
#[repr(C)]
102+
pub struct B {
103+
a: A,
104+
f: f32,
105+
d: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
106+
}
107+
#[repr(C)]
108+
pub struct C {
109+
c: u8,
110+
b: B, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
111+
}
112+
#[repr(C)]
113+
pub struct D {
114+
x: f64,
115+
}
116+
#[repr(C)]
117+
pub struct E {
118+
x: i32,
119+
d: D, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
120+
}
121+
#[repr(C)]
122+
pub struct F {
123+
a: u8,
124+
b: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
125+
}
126+
#[repr(C)]
127+
pub struct G {
128+
a: u8,
129+
b: u8,
130+
c: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
131+
d: f32,
132+
e: f64, //~ WARNING repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
133+
}
134+
// Should not warn on #[repr(packed)].
135+
#[repr(packed)]
136+
pub struct H {
137+
a: u8,
138+
b: u8,
139+
c: f64,
140+
d: f32,
141+
e: f64,
142+
}
143+
#[repr(C, packed)]
144+
pub struct I {
145+
a: u8,
146+
b: u8,
147+
c: f64,
148+
d: f32,
149+
e: f64,
150+
}
151+
152+
fn main() { }

0 commit comments

Comments
 (0)