Skip to content

Commit 7ab1fd1

Browse files
authored
Rollup merge of #147243 - folkertdev:cmse-bail-impl-trait, r=davidtwco
cmse: disallow `impl Trait` in `cmse-nonsecure-entry` return types tracking issue: #75835 fixes #147242 Refactors some logic to be more robust in the future, and then disallows `impl Trait` as a return type for the cmse ABIs. The `is_valid_cmse_output_layout` function disallows `union` values like before. That is not entirely correct, but preserves the current behavior. Some additional logic is needed for `union` values (and any types where parts may be uninitialized) that I'll tackle in a later PR. can be reviewed commit-by-commit. r? types
2 parents bce59ab + fbdc685 commit 7ab1fd1

File tree

5 files changed

+93
-40
lines changed

5 files changed

+93
-40
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use rustc_abi::ExternAbi;
1+
use rustc_abi::{BackendRepr, ExternAbi, Float, Integer, Primitive, Scalar};
22
use rustc_errors::{DiagCtxtHandle, E0781, struct_span_code_err};
33
use rustc_hir::{self as hir, HirId};
44
use rustc_middle::bug;
5-
use rustc_middle::ty::layout::LayoutError;
6-
use rustc_middle::ty::{self, TyCtxt};
5+
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
6+
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
77

88
use crate::errors;
99

@@ -164,44 +164,48 @@ fn is_valid_cmse_output<'tcx>(
164164
) -> Result<bool, &'tcx LayoutError<'tcx>> {
165165
// this type is only used for layout computation, which does not rely on regions
166166
let fn_sig = tcx.instantiate_bound_regions_with_erased(fn_sig);
167+
let fn_sig = tcx.erase_and_anonymize_regions(fn_sig);
168+
let return_type = fn_sig.output();
169+
170+
// `impl Trait` is already disallowed with `cmse-nonsecure-call`, because that ABI is only
171+
// allowed on function pointers, and function pointers cannot contain `impl Trait` in their
172+
// signature.
173+
//
174+
// Here we explicitly disallow `impl Trait` in the `cmse-nonsecure-entry` return type too, to
175+
// prevent query cycles when calculating the layout. This ABI is meant to be used with
176+
// `#[no_mangle]` or similar, so generics in the type really don't make sense.
177+
//
178+
// see also https://github.com/rust-lang/rust/issues/147242.
179+
if return_type.has_opaque_types() {
180+
return Err(tcx.arena.alloc(LayoutError::TooGeneric(return_type)));
181+
}
167182

168183
let typing_env = ty::TypingEnv::fully_monomorphized();
184+
let layout = tcx.layout_of(typing_env.as_query_input(return_type))?;
185+
186+
Ok(is_valid_cmse_output_layout(layout))
187+
}
169188

170-
let mut ret_ty = fn_sig.output();
171-
let layout = tcx.layout_of(typing_env.as_query_input(ret_ty))?;
189+
/// Returns whether the output will fit into the available registers
190+
fn is_valid_cmse_output_layout<'tcx>(layout: TyAndLayout<'tcx>) -> bool {
172191
let size = layout.layout.size().bytes();
173192

174193
if size <= 4 {
175-
return Ok(true);
194+
return true;
176195
} else if size > 8 {
177-
return Ok(false);
196+
return false;
178197
}
179198

180-
// next we need to peel any repr(transparent) layers off
181-
'outer: loop {
182-
let ty::Adt(adt_def, args) = ret_ty.kind() else {
183-
break;
184-
};
185-
186-
if !adt_def.repr().transparent() {
187-
break;
188-
}
189-
190-
// the first field with non-trivial size and alignment must be the data
191-
for variant_def in adt_def.variants() {
192-
for field_def in variant_def.fields.iter() {
193-
let ty = field_def.ty(tcx, args);
194-
let layout = tcx.layout_of(typing_env.as_query_input(ty))?;
199+
// Accept scalar 64-bit types.
200+
let BackendRepr::Scalar(scalar) = layout.layout.backend_repr else {
201+
return false;
202+
};
195203

196-
if !layout.layout.is_1zst() {
197-
ret_ty = ty;
198-
continue 'outer;
199-
}
200-
}
201-
}
202-
}
204+
let Scalar::Initialized { value, .. } = scalar else {
205+
return false;
206+
};
203207

204-
Ok(ret_ty == tcx.types.i64 || ret_ty == tcx.types.u64 || ret_ty == tcx.types.f64)
208+
matches!(value, Primitive::Int(Integer::I64, _) | Primitive::Float(Float::F64))
205209
}
206210

207211
fn should_emit_generic_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError<'tcx>) -> bool {

tests/ui/cmse-nonsecure/cmse-nonsecure-call/generics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ struct Test<T: Copy> {
1414
f1: extern "cmse-nonsecure-call" fn<U: Copy>(U, u32, u32, u32) -> u64,
1515
//~^ ERROR cannot find type `U` in this scope
1616
//~| ERROR function pointer types may not have generic parameters
17-
f2: extern "cmse-nonsecure-call" fn(impl Copy, u32, u32, u32) -> u64,
17+
f2: extern "cmse-nonsecure-call" fn(impl Copy, u32, u32, u32) -> impl Copy,
1818
//~^ ERROR `impl Trait` is not allowed in `fn` pointer parameters
19+
//~| ERROR `impl Trait` is not allowed in `fn` pointer return types
1920
f3: extern "cmse-nonsecure-call" fn(T, u32, u32, u32) -> u64, //~ ERROR [E0798]
2021
f4: extern "cmse-nonsecure-call" fn(Wrapper<T>, u32, u32, u32) -> u64, //~ ERROR [E0798]
2122
}

tests/ui/cmse-nonsecure/cmse-nonsecure-call/generics.stderr

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,33 @@ LL | struct Test<T: Copy, U> {
2525
error[E0562]: `impl Trait` is not allowed in `fn` pointer parameters
2626
--> $DIR/generics.rs:17:41
2727
|
28-
LL | f2: extern "cmse-nonsecure-call" fn(impl Copy, u32, u32, u32) -> u64,
28+
LL | f2: extern "cmse-nonsecure-call" fn(impl Copy, u32, u32, u32) -> impl Copy,
2929
| ^^^^^^^^^
3030
|
3131
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
3232

33+
error[E0562]: `impl Trait` is not allowed in `fn` pointer return types
34+
--> $DIR/generics.rs:17:70
35+
|
36+
LL | f2: extern "cmse-nonsecure-call" fn(impl Copy, u32, u32, u32) -> impl Copy,
37+
| ^^^^^^^^^
38+
|
39+
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
40+
3341
error[E0798]: function pointers with the `"cmse-nonsecure-call"` ABI cannot contain generics in their type
34-
--> $DIR/generics.rs:19:9
42+
--> $DIR/generics.rs:20:9
3543
|
3644
LL | f3: extern "cmse-nonsecure-call" fn(T, u32, u32, u32) -> u64,
3745
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3846

3947
error[E0798]: function pointers with the `"cmse-nonsecure-call"` ABI cannot contain generics in their type
40-
--> $DIR/generics.rs:20:9
48+
--> $DIR/generics.rs:21:9
4149
|
4250
LL | f4: extern "cmse-nonsecure-call" fn(Wrapper<T>, u32, u32, u32) -> u64,
4351
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4452

4553
error[E0798]: return value of `"cmse-nonsecure-call"` function too large to pass via registers
46-
--> $DIR/generics.rs:26:71
54+
--> $DIR/generics.rs:27:71
4755
|
4856
LL | type WithTraitObject = extern "cmse-nonsecure-call" fn(&dyn Trait) -> &dyn Trait;
4957
| ^^^^^^^^^^ this type doesn't fit in the available registers
@@ -52,7 +60,7 @@ LL | type WithTraitObject = extern "cmse-nonsecure-call" fn(&dyn Trait) -> &dyn
5260
= note: the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
5361

5462
error[E0798]: return value of `"cmse-nonsecure-call"` function too large to pass via registers
55-
--> $DIR/generics.rs:30:60
63+
--> $DIR/generics.rs:31:60
5664
|
5765
LL | extern "cmse-nonsecure-call" fn(&'static dyn Trait) -> &'static dyn Trait;
5866
| ^^^^^^^^^^^^^^^^^^ this type doesn't fit in the available registers
@@ -61,7 +69,7 @@ LL | extern "cmse-nonsecure-call" fn(&'static dyn Trait) -> &'static dyn Tra
6169
= note: the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
6270

6371
error[E0798]: return value of `"cmse-nonsecure-call"` function too large to pass via registers
64-
--> $DIR/generics.rs:37:60
72+
--> $DIR/generics.rs:38:60
6573
|
6674
LL | extern "cmse-nonsecure-call" fn(WrapperTransparent) -> WrapperTransparent;
6775
| ^^^^^^^^^^^^^^^^^^ this type doesn't fit in the available registers
@@ -70,12 +78,12 @@ LL | extern "cmse-nonsecure-call" fn(WrapperTransparent) -> WrapperTranspare
7078
= note: the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
7179

7280
error[E0045]: C-variadic functions with the "cmse-nonsecure-call" calling convention are not supported
73-
--> $DIR/generics.rs:40:20
81+
--> $DIR/generics.rs:41:20
7482
|
7583
LL | type WithVarArgs = extern "cmse-nonsecure-call" fn(u32, ...);
7684
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C-variadic function must have a compatible calling convention
7785

78-
error: aborting due to 9 previous errors
86+
error: aborting due to 10 previous errors
7987

8088
Some errors have detailed explanations: E0045, E0412, E0562, E0798.
8189
For more information about an error, try `rustc --explain E0045`.

tests/ui/cmse-nonsecure/cmse-nonsecure-entry/generics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ extern "cmse-nonsecure-entry" fn wrapped_trait_object(x: WrapperTransparent) ->
6565
//~^ ERROR return value of `"cmse-nonsecure-entry"` function too large to pass via registers [E0798]
6666
x
6767
}
68+
69+
extern "cmse-nonsecure-entry" fn return_impl_trait(_: impl Copy) -> impl Copy {
70+
//~^ ERROR functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
71+
//~| ERROR functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
72+
0u128
73+
}
74+
75+
extern "cmse-nonsecure-entry" fn return_impl_trait_nested(v: (impl Copy, i32)) -> (impl Copy, i32) {
76+
//~^ ERROR functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
77+
//~| ERROR functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
78+
v
79+
}

tests/ui/cmse-nonsecure/cmse-nonsecure-entry/generics.stderr

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain gen
1616
LL | extern "cmse-nonsecure-entry" fn impl_trait(_: impl Copy, _: u32, _: u32, _: u32) -> u64 {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19+
error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
20+
--> $DIR/generics.rs:69:1
21+
|
22+
LL | extern "cmse-nonsecure-entry" fn return_impl_trait(_: impl Copy) -> impl Copy {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
26+
--> $DIR/generics.rs:69:1
27+
|
28+
LL | extern "cmse-nonsecure-entry" fn return_impl_trait(_: impl Copy) -> impl Copy {
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
32+
33+
error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
34+
--> $DIR/generics.rs:75:1
35+
|
36+
LL | extern "cmse-nonsecure-entry" fn return_impl_trait_nested(v: (impl Copy, i32)) -> (impl Copy, i32) {
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
40+
--> $DIR/generics.rs:75:1
41+
|
42+
LL | extern "cmse-nonsecure-entry" fn return_impl_trait_nested(v: (impl Copy, i32)) -> (impl Copy, i32) {
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
46+
1947
error[E0798]: functions with the `"cmse-nonsecure-entry"` ABI cannot contain generics in their type
2048
--> $DIR/generics.rs:14:5
2149
|
@@ -61,6 +89,6 @@ LL | extern "cmse-nonsecure-entry" fn wrapped_trait_object(x: WrapperTransparent
6189
= note: functions with the `"cmse-nonsecure-entry"` ABI must pass their result via the available return registers
6290
= note: the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
6391

64-
error: aborting due to 7 previous errors
92+
error: aborting due to 11 previous errors
6593

6694
For more information about this error, try `rustc --explain E0798`.

0 commit comments

Comments
 (0)