forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#126922 - asquared31415:asm_binary_label, r=estebank add lint for inline asm labels that look like binary fixes rust-lang#94426 Due to a bug/feature in LLVM, labels composed of only the digits `0` and `1` can sometimes be confused with binary literals, even if a binary literal would not be valid in that position. This PR adds detection for such labels and also as a drive-by change, adds a note to cases such as `asm!(include_str!("file"))` that the label that it found came from an expansion of a macro, it wasn't found in the source code. I expect this PR to upset some people that were using labels `0:` or `1:` without issue because they never hit the case where LLVM got it wrong, but adding a heuristic to the lint to prevent this is not feasible - it would involve writing a whole assembly parser for every target that we have assembly support for. [zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/238009-t-compiler.2Fmeetings/topic/.5Bweekly.5D.202024-06-20/near/445870628) r? ``@estebank``
- Loading branch information
Showing
8 changed files
with
397 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,17 @@ | ||
//@ needs-asm-support | ||
//@ only-x86_64 | ||
|
||
// tests that labels containing only the digits 0 and 1 are rejected | ||
// uses of such labels can sometimes be interpreted as a binary literal | ||
|
||
use std::arch::{asm, global_asm}; | ||
|
||
fn main() { | ||
unsafe { | ||
asm!("0: jmp 0b"); //~ ERROR avoid using labels containing only the digits | ||
asm!("1: jmp 1b"); //~ ERROR avoid using labels containing only the digits | ||
asm!("10: jmp 10b"); //~ ERROR avoid using labels containing only the digits | ||
asm!("01: jmp 01b"); //~ ERROR avoid using labels containing only the digits | ||
asm!("1001101: jmp 1001101b"); //~ ERROR avoid using labels containing only the digits | ||
} | ||
} |
This file contains 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,43 @@ | ||
error: avoid using labels containing only the digits `0` and `1` in inline assembly | ||
--> $DIR/binary_asm_labels.rs:11:15 | ||
| | ||
LL | asm!("0: jmp 0b"); | ||
| ^ use a different label that doesn't start with `0` or `1` | ||
| | ||
= note: an LLVM bug makes these labels ambiguous with a binary literal number | ||
= note: `#[deny(binary_asm_labels)]` on by default | ||
|
||
error: avoid using labels containing only the digits `0` and `1` in inline assembly | ||
--> $DIR/binary_asm_labels.rs:12:15 | ||
| | ||
LL | asm!("1: jmp 1b"); | ||
| ^ use a different label that doesn't start with `0` or `1` | ||
| | ||
= note: an LLVM bug makes these labels ambiguous with a binary literal number | ||
|
||
error: avoid using labels containing only the digits `0` and `1` in inline assembly | ||
--> $DIR/binary_asm_labels.rs:13:15 | ||
| | ||
LL | asm!("10: jmp 10b"); | ||
| ^^ use a different label that doesn't start with `0` or `1` | ||
| | ||
= note: an LLVM bug makes these labels ambiguous with a binary literal number | ||
|
||
error: avoid using labels containing only the digits `0` and `1` in inline assembly | ||
--> $DIR/binary_asm_labels.rs:14:15 | ||
| | ||
LL | asm!("01: jmp 01b"); | ||
| ^^ use a different label that doesn't start with `0` or `1` | ||
| | ||
= note: an LLVM bug makes these labels ambiguous with a binary literal number | ||
|
||
error: avoid using labels containing only the digits `0` and `1` in inline assembly | ||
--> $DIR/binary_asm_labels.rs:15:15 | ||
| | ||
LL | asm!("1001101: jmp 1001101b"); | ||
| ^^^^^^^ use a different label that doesn't start with `0` or `1` | ||
| | ||
= note: an LLVM bug makes these labels ambiguous with a binary literal number | ||
|
||
error: aborting due to 5 previous errors | ||
|
Oops, something went wrong.