Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C-cmse-nonsecure-call: improved error messages #127814

Merged
merged 11 commits into from
Jul 19, 2024

Conversation

folkertdev
Copy link
Contributor

@folkertdev folkertdev commented Jul 16, 2024

tracking issue: #81391
issue for the error messages (partially implemented by this PR): #81347
related, in that it also deals with CMSE: #127766

When using the C-cmse-nonsecure-call ABI, both the arguments and return value must be passed via registers. Previously, when violating this constraint, an ugly LLVM error would be shown. Now, the rust compiler itself will print a pretty message and link to more information.

when the `C-cmse-nonsecure-call` ABI is used, arguments and return values must be passed via registers. Failing to do so (i.e. spilling to the stack) causes an LLVM error down the line, but now rustc will properly emit an error a bit earlier in the chain
@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2024

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 16, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2024

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

@folkertdev folkertdev changed the title C cmse nonsecure call error messages C-cmse-nonsecure-call: improved error messages Jul 16, 2024
Copy link
Member

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diagnostic error explanation looks good to me.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 16, 2024

Can you swap the commits so we can see the diagnostics as a diff on your actual commit? Or are the LLVM diagnostics too annoying?

@tdittr
Copy link
Contributor

tdittr commented Jul 16, 2024

Can you swap the commits so we can see the diagnostics as a diff on your actual commit? Or are the LLVM diagnostics too annoying?

We did consider this, but LLVM will only return the first error it encounters. Also rebasing this is not straight forward, since we moved some of the tests which git rebase really does not seem to like.

@rust-log-analyzer

This comment has been minimized.

folkertdev and others added 2 commits July 16, 2024 18:38
the error is only generated for functions that are actually codegen'd
@tdittr
Copy link
Contributor

tdittr commented Jul 16, 2024

For the name of the new mod we were not sure, so we just picked one, suggestions are very welcome.

Comment on lines 7 to 10
/// Check conditions on inputs and outputs that the cmse ABIs impose: arguments and results MUST be
/// returned via registers (i.e. MUST NOT spill to the stack). LLVM will also validate these
/// conditions, but by checking them here rustc can emit nicer error messages.
pub fn validate_cmse_abi<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the near future, this will also validate the C-cmse-nonsecure-entry abi which will be added by #127766.

@rust-log-analyzer

This comment has been minimized.

@folkertdev
Copy link
Contributor Author

folkertdev commented Jul 16, 2024

CI fails with

failures:

---- /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md - Rust_Compiler_Error_Index::E0798 (line 18059) stdout ----
Test compiled successfully, but it's marked `compile_fail`.
---- /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md - Rust_Compiler_Error_Index::E0798 (line 18073) stdout ----
Test compiled successfully, but it's marked `compile_fail`.

failures:
    /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md - Rust_Compiler_Error_Index::E0798 (line 18059)
    /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md - Rust_Compiler_Error_Index::E0798 (line 18073)

test result: FAILED. 1041 passed; 2 failed; 55 ignored; 0 measured; 0 filtered out; finished in 8.37s

How can I run those tests locally?

EDIT: ./x.py test --stage 1 src/tools/error_index_generator seems to do it but is slow. using stage 0 is faster but does that do everything?

@folkertdev
Copy link
Contributor Author

Something that stops working with the logic as implemented (and I think that is correct) is using a repr(Rust) union that is effectively a foundational type.

https://godbolt.org/z/oaTdf9cYE

My reasoning is that repr(Rust) is unstable, and so while it is currently given to LLVM as a foundational type, that is not guaranteed. We're future-proofing the implementation here by not allowing it. Using a union with just one foundational field does not work in C. repr(transparent) on unions seems far away from being stabilized, but if it guarantees that a one-field union has the same representation as that field then that would work.

@rustbot
Copy link
Collaborator

rustbot commented Jul 18, 2024

HIR ty lowering was modified

cc @fmease

@folkertdev
Copy link
Contributor Author

we've now implemented the logic in hir_analyze, removed code from codegen_ssa, and error when generics are used in function pointer types with the C-cmse-nonsecure-call abi

@rust-log-analyzer

This comment has been minimized.

it was moved to hir_analysis in the previous commit
@folkertdev folkertdev force-pushed the c-cmse-nonsecure-call-error-messages branch from 096cc19 to 6b6b842 Compare July 18, 2024 10:42
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2024
@folkertdev
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2024
let mut accum = 0u64;

for arg_def in fn_sig.inputs().iter() {
for (index, arg_def) in fn_sig.inputs().iter().enumerate() {
let layout = tcx.layout_of(ParamEnv::reveal_all().and(*arg_def.skip_binder()))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at this point it may be easier to just emit the errors in is_valid_cmse_inputs and _output directly. Then you can also report a "generics not allowed" error with a span pointing at the offending thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have 2 reasons not to do that

  • arg_def doesn't have any span information. it's effectively just a Ty
  • in the (near, hopefully) future, the C-cmse-nonsecure-entry abi will require the same validation, and we'd like to reuse this code, so passing in BareFn-specific data will not work

@oli-obk
Copy link
Contributor

oli-obk commented Jul 19, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 19, 2024

📌 Commit c2894a4 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 19, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#127295 (CFI: Support provided methods on traits)
 - rust-lang#127814 (`C-cmse-nonsecure-call`: improved error messages)
 - rust-lang#127949 (fix: explain E0120 better cover cases when its raised)
 - rust-lang#127966 (Use structured suggestions for unconstrained generic parameters on impl blocks)
 - rust-lang#127976 (Lazy type aliases: Diagostics: Detect bivariant ty params that are only used recursively)
 - rust-lang#127978 (Avoid ref when using format! for perf)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3b20150 into rust-lang:master Jul 19, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2024
Rollup merge of rust-lang#127814 - folkertdev:c-cmse-nonsecure-call-error-messages, r=oli-obk

`C-cmse-nonsecure-call`: improved error messages

tracking issue: rust-lang#81391
issue for the error messages (partially implemented by this PR): rust-lang#81347
related, in that it also deals with CMSE: rust-lang#127766

When using the `C-cmse-nonsecure-call` ABI, both the arguments and return value must be passed via registers. Previously, when violating this constraint, an ugly LLVM error would be shown. Now, the rust compiler itself will print a pretty message and link to more information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants