-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make naked functions incompatible with certain attributes #93809
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93d9578
Fix copy/paste errors in check_attr
bstrie a3b3782
Consolidate checking for attributes not compatible with #[naked]
bstrie 647ff96
Forbid #[target_feature] from being used on naked functions
bstrie 3f8c33c
Forbid testing attributes from being used with #[naked]
bstrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
`#[track_caller]` and `#[naked]` cannot both be applied to the same function. | ||
Functions marked with the `#[naked]` attribute are restricted in what other | ||
code generation attributes they may be marked with. | ||
|
||
The following code generation attributes are incompatible with `#[naked]`: | ||
|
||
* `#[inline]` | ||
* `#[track_caller]` | ||
* `#[target_feature]` | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0736 | ||
#[inline] | ||
#[naked] | ||
#[track_caller] | ||
fn foo() {} | ||
``` | ||
|
||
This is primarily due to ABI incompatibilities between the two attributes. | ||
See [RFC 2091] for details on this and other limitations. | ||
These incompatibilities are due to the fact that naked functions impose | ||
deliberately strict restrictions regarding the code that the compiler is | ||
allowed to produce for this function. | ||
|
||
See [the reference page for codegen attributes] for more information. | ||
|
||
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md | ||
[the reference page for codegen attributes]: https://doc.rust-lang.org/reference/attributes/codegen.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Testing attributes cannot be applied to functions marked with `#[naked]`. | ||
|
||
Erroneous code example: | ||
|
||
```ignore (requires test runner) | ||
#[test] | ||
#[should_panic] | ||
#[naked] | ||
fn foo() {} | ||
``` | ||
|
||
See [the reference page for codegen attributes] for more information. | ||
|
||
[the reference page for codegen attributes]: https://doc.rust-lang.org/reference/attributes/codegen.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// needs-asm-support | ||
|
||
#![feature(naked_functions)] | ||
#![crate_type = "lib"] | ||
|
||
use std::arch::asm; | ||
|
||
#[naked] | ||
pub unsafe extern "C" fn inline_none() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline] | ||
//~^ ERROR cannot use additional code generation attributes with `#[naked]` | ||
pub unsafe extern "C" fn inline_hint() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline(always)] | ||
//~^ ERROR cannot use additional code generation attributes with `#[naked]` | ||
pub unsafe extern "C" fn inline_always() { | ||
asm!("", options(noreturn)); | ||
} | ||
|
||
#[naked] | ||
#[inline(never)] | ||
//~^ ERROR cannot use additional code generation attributes with `#[naked]` | ||
pub unsafe extern "C" fn inline_never() { | ||
asm!("", options(noreturn)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0736]: cannot use additional code generation attributes with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:14:1 | ||
| | ||
LL | #[inline] | ||
| ^^^^^^^^^ | ||
|
||
error[E0736]: cannot use additional code generation attributes with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:21:1 | ||
| | ||
LL | #[inline(always)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0736]: cannot use additional code generation attributes with `#[naked]` | ||
--> $DIR/naked-functions-inline.rs:28:1 | ||
| | ||
LL | #[inline(never)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0736`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// ignore-arm | ||
// ignore-aarch64 | ||
// ignore-wasm | ||
// ignore-emscripten | ||
// ignore-mips | ||
// ignore-mips64 | ||
// ignore-powerpc | ||
// ignore-powerpc64 | ||
// ignore-powerpc64le | ||
// ignore-riscv64 | ||
// ignore-s390x | ||
// ignore-sparc | ||
// ignore-sparc64 | ||
// needs-asm-support | ||
|
||
#![feature(naked_functions)] | ||
#![crate_type = "lib"] | ||
|
||
use std::arch::asm; | ||
|
||
#[target_feature(enable = "sse2")] | ||
//~^ ERROR cannot use additional code generation attributes with `#[naked]` | ||
#[naked] | ||
pub unsafe extern "C" fn naked_target_feature() { | ||
asm!("", options(noreturn)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0736]: cannot use additional code generation attributes with `#[naked]` | ||
--> $DIR/naked-functions-target-feature.rs:21:1 | ||
| | ||
LL | #[target_feature(enable = "sse2")] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0736`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we point at
#[naked]
as well here, and maybe at the item name too?