-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add codegen option for branch protection and pointer authentication on AArch64 #88354
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
18 changes: 18 additions & 0 deletions
18
src/doc/unstable-book/src/compiler-flags/branch-protection.md
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,18 @@ | ||
# `branch-protection` | ||
|
||
This option lets you enable branch authentication instructions on AArch64. | ||
This option is ignored for non-AArch64 architectures. | ||
It takes some combination of the following values, separated by a `,`. | ||
|
||
- `pac-ret` - Enable pointer authentication for non-leaf functions. | ||
- `leaf` - Enable pointer authentication for all functions, including leaf functions. | ||
- `b-key` - Sign return addresses with key B, instead of the default key A. | ||
- `bti` - Enable branch target identification. | ||
|
||
`leaf` and `b-key` are only valid if `pac-ret` was previously specified. | ||
For example, `-Z branch-protection=bti,pac-ret,leaf` is valid, but | ||
`-Z branch-protection=bti,leaf,pac-ret` is not. | ||
|
||
Rust's standard library does not ship with BTI or pointer authentication enabled by default. | ||
In Cargo projects the standard library can be recompiled with pointer authentication using the nightly | ||
[build-std](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std) feature. |
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,22 @@ | ||
// Test that PAC instructions are emitted when branch-protection is specified. | ||
|
||
// min-llvm-version: 10.0.1 | ||
// assembly-output: emit-asm | ||
// compile-flags: --target aarch64-unknown-linux-gnu | ||
// compile-flags: -Z branch-protection=pac-ret,leaf | ||
// needs-llvm-components: aarch64 | ||
|
||
#![feature(no_core, lang_items)] | ||
#![no_std] | ||
#![no_core] | ||
#![crate_type = "lib"] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
// CHECK: hint #25 | ||
nagisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CHECK: hint #29 | ||
#[no_mangle] | ||
pub fn test() -> u8 { | ||
42 | ||
} |
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,41 @@ | ||
// Test that the correct module flags are emitted with different branch protection flags. | ||
|
||
// revisions: bti pac-ret leaf b-key | ||
// min-llvm-version: 12.0.0 | ||
// needs-llvm-components: aarch64 | ||
// [bti] compile-flags: -Z branch-protection=bti | ||
// [pac-ret] compile-flags: -Z branch-protection=pac-ret | ||
// [leaf] compile-flags: -Z branch-protection=pac-ret,leaf | ||
// [b-key] compile-flags: -Z branch-protection=pac-ret,b-key | ||
// compile-flags: --target aarch64-unknown-linux-gnu | ||
|
||
#![crate_type = "lib"] | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
|
||
#[lang="sized"] | ||
trait Sized { } | ||
|
||
// A basic test function. | ||
pub fn test() { | ||
} | ||
|
||
// bti: !"branch-target-enforcement", i32 1 | ||
// bti: !"sign-return-address", i32 0 | ||
// bti: !"sign-return-address-all", i32 0 | ||
// bti: !"sign-return-address-with-bkey", i32 0 | ||
|
||
// pac-ret: !"branch-target-enforcement", i32 0 | ||
// pac-ret: !"sign-return-address", i32 1 | ||
// pac-ret: !"sign-return-address-all", i32 0 | ||
// pac-ret: !"sign-return-address-with-bkey", i32 0 | ||
|
||
// leaf: !"branch-target-enforcement", i32 0 | ||
// leaf: !"sign-return-address", i32 1 | ||
// leaf: !"sign-return-address-all", i32 1 | ||
// leaf: !"sign-return-address-with-bkey", i32 0 | ||
|
||
// b-key: !"branch-target-enforcement", i32 0 | ||
// b-key: !"sign-return-address", i32 1 | ||
// b-key: !"sign-return-address-all", i32 0 | ||
// b-key: !"sign-return-address-with-bkey", i32 1 |
14 changes: 14 additions & 0 deletions
14
src/test/run-make-fulldeps/pointer-auth-link-with-c/Makefile
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 @@ | ||
-include ../tools.mk | ||
|
||
# only-aarch64 | ||
|
||
all: | ||
$(COMPILE_OBJ) $(TMPDIR)/test.o test.c | ||
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o | ||
$(RUSTC) -Z branch-protection=bti,pac-ret,leaf test.rs | ||
$(call RUN,test) | ||
|
||
$(COMPILE_OBJ) $(TMPDIR)/test.o test.c -mbranch-protection=bti+pac-ret+leaf | ||
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o | ||
$(RUSTC) -Z branch-protection=bti,pac-ret,leaf test.rs | ||
$(call RUN,test) |
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 @@ | ||
int foo() { return 0; } |
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,8 @@ | ||
#[link(name = "test")] | ||
extern "C" { | ||
fn foo() -> i32; | ||
} | ||
|
||
fn main() { | ||
unsafe {foo();} | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs
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 @@ | ||
// compile-flags: -Z branch-protection=leaf |
2 changes: 2 additions & 0 deletions
2
src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.stderr
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,2 @@ | ||
error: incorrect value `leaf` for debugging option `branch-protection` - a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf` was expected | ||
|
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.
This should ideally work even if
b-key
comes beforepac-ret
in the list of options. Same forleaf
.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.
Those are modifiers for
pac-ret
so to me it makes sense to put them after it. This is also what Clang requires (empirically). However, the main reason behind this is more practical: Making Rust flexible (i.e. treating these as an unordered set) is great, but we can't go back without breaking users' build scripts. Conversely, making Rust strict (like C compilers) makes it easier for us to match future changes to the C options, or to extend it in Rust-specific ways (currently unexplored).We can of course change this to suit Rust's practices if you think that's more appropriate, but I thought I should explain why it was done this way. What do you think?
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.
I'm okay with leaving this as an outstanding question for the tracking issue for this feature.