From 2588287deffd92c4ca0b5b39f92b9cca894a3983 Mon Sep 17 00:00:00 2001 From: Hugues de Valon Date: Wed, 30 Sep 2020 18:10:17 +0100 Subject: [PATCH] Add more tests and check for ABI Signed-off-by: Hugues de Valon --- compiler/rustc_error_codes/src/error_codes.rs | 1 + .../src/error_codes/E0775.md | 2 +- .../src/error_codes/E0776.md | 13 +++++++++++++ compiler/rustc_typeck/src/collect.rs | 9 +++++++++ .../language-features/cmse-nonsecure-entry.md | 6 ++++++ src/test/ui/cmse-nonsecure-entry/gate_test.rs | 11 +++++++++++ .../ui/cmse-nonsecure-entry/gate_test.stderr | 19 +++++++++++++++++++ .../params-on-registers.rs | 11 +++++++++++ .../cmse-nonsecure-entry/params-on-stack.rs | 3 +-- .../ui/cmse-nonsecure-entry/trustzone-only.rs | 1 - .../trustzone-only.stderr | 2 +- src/test/ui/cmse-nonsecure-entry/wrong-abi.rs | 10 ++++++++++ .../ui/cmse-nonsecure-entry/wrong-abi.stderr | 9 +++++++++ 13 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0776.md create mode 100644 src/test/ui/cmse-nonsecure-entry/gate_test.rs create mode 100644 src/test/ui/cmse-nonsecure-entry/gate_test.stderr create mode 100644 src/test/ui/cmse-nonsecure-entry/params-on-registers.rs create mode 100644 src/test/ui/cmse-nonsecure-entry/wrong-abi.rs create mode 100644 src/test/ui/cmse-nonsecure-entry/wrong-abi.stderr diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index c9753296732..8b21dacf787 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -459,6 +459,7 @@ E0771: include_str!("./error_codes/E0771.md"), E0773: include_str!("./error_codes/E0773.md"), E0774: include_str!("./error_codes/E0774.md"), E0775: include_str!("./error_codes/E0775.md"), +E0776: include_str!("./error_codes/E0776.md"), ; // E0006, // merged with E0005 // E0008, // cannot bind by-move into a pattern guard diff --git a/compiler/rustc_error_codes/src/error_codes/E0775.md b/compiler/rustc_error_codes/src/error_codes/E0775.md index f21ee73791c..9bafd52f75c 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0775.md +++ b/compiler/rustc_error_codes/src/error_codes/E0775.md @@ -7,7 +7,7 @@ Erroneous code example: #![feature(cmse_nonsecure_entry)] #[cmse_nonsecure_entry] -fn toto() {} +pub extern "C" fn entry_function() {} ``` To fix this error, compile your code for a Rust target that supports the diff --git a/compiler/rustc_error_codes/src/error_codes/E0776.md b/compiler/rustc_error_codes/src/error_codes/E0776.md new file mode 100644 index 00000000000..d65beebe07c --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0776.md @@ -0,0 +1,13 @@ +`#[cmse_nonsecure_entry]` functions require a C ABI + +Erroneous code example: + +```compile_fail,E0776 +#![feature(cmse_nonsecure_entry)] + +#[no_mangle] +#[cmse_nonsecure_entry] +pub fn entry_function(input: Vec) {} +``` + +To fix this error, declare your entry function with a C ABI, using `extern "C"`. diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 6c0d51ed717..9aca112a914 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2544,6 +2544,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { } else if tcx.sess.check_name(attr, sym::used) { codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED; } else if tcx.sess.check_name(attr, sym::cmse_nonsecure_entry) { + if tcx.fn_sig(id).abi() != abi::Abi::C { + struct_span_err!( + tcx.sess, + attr.span, + E0776, + "`#[cmse_nonsecure_entry]` requires C ABI" + ) + .emit(); + } if !tcx.sess.target.target.llvm_target.contains("thumbv8m") { struct_span_err!(tcx.sess, attr.span, E0775, "`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension") .emit(); diff --git a/src/doc/unstable-book/src/language-features/cmse-nonsecure-entry.md b/src/doc/unstable-book/src/language-features/cmse-nonsecure-entry.md index 9d69be3e69b..338fbc4b2bf 100644 --- a/src/doc/unstable-book/src/language-features/cmse-nonsecure-entry.md +++ b/src/doc/unstable-book/src/language-features/cmse-nonsecure-entry.md @@ -26,6 +26,12 @@ With this attribute, the compiler will do the following: information * use the `BXNS` instruction to return +Because the stack can not be used to pass parameters, there will be compilation +errors if: +* the total size of all parameters is too big (for example more than four 32 + bits integers) +* the entry function is not using a C ABI + The special symbol `__acle_se_` will be used by the linker to generate a secure gateway veneer. diff --git a/src/test/ui/cmse-nonsecure-entry/gate_test.rs b/src/test/ui/cmse-nonsecure-entry/gate_test.rs new file mode 100644 index 00000000000..02d5f20febc --- /dev/null +++ b/src/test/ui/cmse-nonsecure-entry/gate_test.rs @@ -0,0 +1,11 @@ +// gate-test-cmse_nonsecure_entry + +#[no_mangle] +#[cmse_nonsecure_entry] +//~^ ERROR [E0775] +//~| ERROR [E0658] +pub extern "C" fn entry_function(input: u32) -> u32 { + input + 6 +} + +fn main() {} diff --git a/src/test/ui/cmse-nonsecure-entry/gate_test.stderr b/src/test/ui/cmse-nonsecure-entry/gate_test.stderr new file mode 100644 index 00000000000..75a29b317df --- /dev/null +++ b/src/test/ui/cmse-nonsecure-entry/gate_test.stderr @@ -0,0 +1,19 @@ +error[E0658]: the `#[cmse_nonsecure_entry]` attribute is an experimental feature + --> $DIR/gate_test.rs:4:1 + | +LL | #[cmse_nonsecure_entry] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #75835 for more information + = help: add `#![feature(cmse_nonsecure_entry)]` to the crate attributes to enable + +error[E0775]: `#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension + --> $DIR/gate_test.rs:4:1 + | +LL | #[cmse_nonsecure_entry] + | ^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0658, E0775. +For more information about an error, try `rustc --explain E0658`. diff --git a/src/test/ui/cmse-nonsecure-entry/params-on-registers.rs b/src/test/ui/cmse-nonsecure-entry/params-on-registers.rs new file mode 100644 index 00000000000..a723eb73473 --- /dev/null +++ b/src/test/ui/cmse-nonsecure-entry/params-on-registers.rs @@ -0,0 +1,11 @@ +// build-pass +// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib +// only-thumbv8m.main-none-eabi +#![feature(cmse_nonsecure_entry)] +#![no_std] + +#[no_mangle] +#[cmse_nonsecure_entry] +pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32) -> u32 { + a + b + c + d +} diff --git a/src/test/ui/cmse-nonsecure-entry/params-on-stack.rs b/src/test/ui/cmse-nonsecure-entry/params-on-stack.rs index 14c334fd010..553d3a8cb0b 100644 --- a/src/test/ui/cmse-nonsecure-entry/params-on-stack.rs +++ b/src/test/ui/cmse-nonsecure-entry/params-on-stack.rs @@ -1,4 +1,3 @@ -// gate-test-cmse_nonsecure_entry // compile-flags: --target thumbv8m.main-none-eabi --crate-type lib // only-thumbv8m.main-none-eabi #![feature(cmse_nonsecure_entry)] @@ -6,6 +5,6 @@ #[no_mangle] #[cmse_nonsecure_entry] -pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32, e: u32) -> u32 { +pub extern "C" fn entry_function(a: u32, b: u32, c: u32, d: u32, e: u32) -> u32 { //~ ERROR a + b + c + d + e } diff --git a/src/test/ui/cmse-nonsecure-entry/trustzone-only.rs b/src/test/ui/cmse-nonsecure-entry/trustzone-only.rs index efb4eb09b68..3783e279402 100644 --- a/src/test/ui/cmse-nonsecure-entry/trustzone-only.rs +++ b/src/test/ui/cmse-nonsecure-entry/trustzone-only.rs @@ -1,4 +1,3 @@ -// gate-test-cmse_nonsecure_entry // ignore-thumbv8m.main-none-eabi #![feature(cmse_nonsecure_entry)] diff --git a/src/test/ui/cmse-nonsecure-entry/trustzone-only.stderr b/src/test/ui/cmse-nonsecure-entry/trustzone-only.stderr index d3e0c1e42eb..7e8862f9ab7 100644 --- a/src/test/ui/cmse-nonsecure-entry/trustzone-only.stderr +++ b/src/test/ui/cmse-nonsecure-entry/trustzone-only.stderr @@ -1,5 +1,5 @@ error[E0775]: `#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension - --> $DIR/trustzone-only.rs:6:1 + --> $DIR/trustzone-only.rs:5:1 | LL | #[cmse_nonsecure_entry] | ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/cmse-nonsecure-entry/wrong-abi.rs b/src/test/ui/cmse-nonsecure-entry/wrong-abi.rs new file mode 100644 index 00000000000..611c8643dcb --- /dev/null +++ b/src/test/ui/cmse-nonsecure-entry/wrong-abi.rs @@ -0,0 +1,10 @@ +// compile-flags: --target thumbv8m.main-none-eabi --crate-type lib +// only-thumbv8m.main-none-eabi +#![feature(cmse_nonsecure_entry)] +#![no_std] + +#[no_mangle] +#[cmse_nonsecure_entry] +pub fn entry_function(a: u32, b: u32, c: u32, d: u32) -> u32 { //~ ERROR [E0776] + a + b + c + d +} diff --git a/src/test/ui/cmse-nonsecure-entry/wrong-abi.stderr b/src/test/ui/cmse-nonsecure-entry/wrong-abi.stderr new file mode 100644 index 00000000000..d6967a11e6b --- /dev/null +++ b/src/test/ui/cmse-nonsecure-entry/wrong-abi.stderr @@ -0,0 +1,9 @@ +error[E0776]: `#[cmse_nonsecure_entry]` functions require C ABI + --> $DIR/wrong-abi.rs:7:1 + | +LL | #[cmse_nonsecure_entry] + | ^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0776`.