forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#87324 - asquared31415:named-asm-labels, r=Ama…
…nieu Lint against named asm labels This adds a deny-by-default lint to prevent the use of named labels in inline `asm!`. Without a solution to rust-lang#81088 about whether the compiler should rewrite named labels or a special syntax for labels, a lint against them should prevent users from writing assembly that could break for internal compiler reasons, such as inlining or anything else that could change the number of actual inline assembly blocks emitted. This does **not** resolve the issue with rewriting labels, that still needs a decision if the compiler should do any more work to try to make them work.
- Loading branch information
Showing
8 changed files
with
520 additions
and
5 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
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,130 @@ | ||
// only-x86_64 | ||
|
||
#![feature(asm, global_asm)] | ||
|
||
#[no_mangle] | ||
pub static FOO: usize = 42; | ||
|
||
fn main() { | ||
unsafe { | ||
// Basic usage | ||
asm!("bar: nop"); //~ ERROR avoid using named labels | ||
|
||
// No following asm | ||
asm!("abcd:"); //~ ERROR avoid using named labels | ||
|
||
// Multiple labels on one line | ||
asm!("foo: bar1: nop"); | ||
//~^ ERROR avoid using named labels | ||
|
||
// Multiple lines | ||
asm!("foo1: nop", "nop"); //~ ERROR avoid using named labels | ||
asm!("foo2: foo3: nop", "nop"); | ||
//~^ ERROR avoid using named labels | ||
asm!("nop", "foo4: nop"); //~ ERROR avoid using named labels | ||
asm!("foo5: nop", "foo6: nop"); | ||
//~^ ERROR avoid using named labels | ||
//~| ERROR avoid using named labels | ||
|
||
// Statement separator | ||
asm!("foo7: nop; foo8: nop"); | ||
//~^ ERROR avoid using named labels | ||
asm!("foo9: nop; nop"); //~ ERROR avoid using named labels | ||
asm!("nop; foo10: nop"); //~ ERROR avoid using named labels | ||
|
||
// Escaped newline | ||
asm!("bar2: nop\n bar3: nop"); | ||
//~^ ERROR avoid using named labels | ||
asm!("bar4: nop\n nop"); //~ ERROR avoid using named labels | ||
asm!("nop\n bar5: nop"); //~ ERROR avoid using named labels | ||
asm!("nop\n bar6: bar7: nop"); | ||
//~^ ERROR avoid using named labels | ||
|
||
// Raw strings | ||
asm!( | ||
r" | ||
blah2: nop | ||
blah3: nop | ||
" | ||
); | ||
//~^^^^ ERROR avoid using named labels | ||
|
||
asm!( | ||
r###" | ||
nop | ||
nop ; blah4: nop | ||
"### | ||
); | ||
//~^^^ ERROR avoid using named labels | ||
|
||
// Non-labels | ||
// should not trigger lint, but may be invalid asm | ||
asm!("ab cd: nop"); | ||
|
||
// `blah:` does not trigger because labels need to be at the start | ||
// of the statement, and there was already a non-label | ||
asm!("1bar: blah: nop"); | ||
|
||
// Only `blah1:` should trigger | ||
asm!("blah1: 2bar: nop"); //~ ERROR avoid using named labels | ||
|
||
// Duplicate labels | ||
asm!("def: def: nop"); //~ ERROR avoid using named labels | ||
asm!("def: nop\ndef: nop"); //~ ERROR avoid using named labels | ||
asm!("def: nop; def: nop"); //~ ERROR avoid using named labels | ||
|
||
// Trying to break parsing | ||
asm!(":"); | ||
asm!("\n:\n"); | ||
asm!("::::"); | ||
|
||
// 0x3A is a ':' | ||
asm!("fooo\u{003A} nop"); //~ ERROR avoid using named labels | ||
asm!("foooo\x3A nop"); //~ ERROR avoid using named labels | ||
|
||
// 0x0A is a newline | ||
asm!("fooooo:\u{000A} nop"); //~ ERROR avoid using named labels | ||
asm!("foooooo:\x0A nop"); //~ ERROR avoid using named labels | ||
|
||
// Intentionally breaking span finding | ||
// equivalent to "ABC: nop" | ||
asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70"); //~ ERROR avoid using named labels | ||
|
||
// Non-label colons - should pass | ||
// (most of these are stolen from other places) | ||
asm!("{:l}", in(reg) 0i64); | ||
asm!("{:e}", in(reg) 0f32); | ||
asm!("mov rax, qword ptr fs:[0]"); | ||
|
||
// Comments | ||
asm!( | ||
r" | ||
ab: nop // ab: does foo | ||
// cd: nop | ||
" | ||
); | ||
//~^^^^ ERROR avoid using named labels | ||
|
||
// Tests usage of colons in non-label positions | ||
asm!(":lo12:FOO"); // this is apparently valid aarch64 | ||
// is there an example that is valid x86 for this test? | ||
asm!(":bbb nop"); | ||
|
||
// Test include_str in asm | ||
asm!(include_str!("named-asm-labels.s")); //~ ERROR avoid using named labels | ||
|
||
// Test allowing or warning on the lint instead | ||
#[allow(named_asm_labels)] | ||
{ | ||
asm!("allowed: nop"); // Should not emit anything | ||
} | ||
|
||
#[warn(named_asm_labels)] | ||
{ | ||
asm!("warned: nop"); //~ WARNING avoid using named labels | ||
} | ||
} | ||
} | ||
|
||
// Don't trigger on global asm | ||
global_asm!("aaaaaaaa: nop"); |
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,5 @@ | ||
lab1: nop | ||
// do more things | ||
lab2: nop // does bar | ||
// a: b | ||
lab3: nop; lab4: nop |
Oops, something went wrong.