Skip to content

Commit fbdc685

Browse files
committed
cmse: disallow impl Trait in cmse-nonsecure-entry return types
1 parent a7bd285 commit fbdc685

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_errors::{DiagCtxtHandle, E0781, struct_span_code_err};
33
use rustc_hir::{self as hir, HirId};
44
use rustc_middle::bug;
55
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
6-
use rustc_middle::ty::{self, TyCtxt};
6+
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
77

88
use crate::errors;
99

@@ -165,9 +165,23 @@ fn is_valid_cmse_output<'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);
167167
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+
}
168182

169183
let typing_env = ty::TypingEnv::fully_monomorphized();
170-
let layout = tcx.layout_of(typing_env.as_query_input(fn_sig.output()))?;
184+
let layout = tcx.layout_of(typing_env.as_query_input(return_type))?;
171185

172186
Ok(is_valid_cmse_output_layout(layout))
173187
}

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)