-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
269 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// compile-flags: -O | ||
// only-x86_64 | ||
|
||
#![crate_type = "rlib"] | ||
#![feature(asm_goto)] | ||
|
||
use std::arch::asm; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn panicky() {} | ||
|
||
struct Foo; | ||
|
||
impl Drop for Foo { | ||
fn drop(&mut self) { | ||
println!(); | ||
} | ||
} | ||
|
||
// CHECK-LABEL: @asm_goto | ||
#[no_mangle] | ||
pub unsafe fn asm_goto() { | ||
// CHECK: callbr void asm sideeffect alignstack inteldialect " | ||
// CHECK-NEXT: to label %[[FALLTHROUGHBB:[a-b0-9]+]] [label %[[JUMPBB:[a-b0-9]+]]] | ||
asm!("jmp {}", label {}); | ||
} | ||
|
||
// CHECK-LABEL: @asm_goto_with_outputs | ||
#[no_mangle] | ||
pub unsafe fn asm_goto_with_outputs() -> u64 { | ||
let out: u64; | ||
// CHECK: [[RES:%[0-9]+]] = callbr i64 asm sideeffect alignstack inteldialect " | ||
// CHECK-NEXT: to label %[[FALLTHROUGHBB:[a-b0-9]+]] [label %[[JUMPBB:[a-b0-9]+]]] | ||
asm!("{} /* {} */", out(reg) out, label { return 1; }); | ||
// CHECK: [[JUMPBB]]: | ||
// CHECK-NEXT: [[RET:%.+]] = phi i64 [ [[RES]], %[[FALLTHROUGHBB]] ], [ 1, %start ] | ||
// CHECK-NEXT: ret i64 [[RET]] | ||
out | ||
} | ||
|
||
// CHECK-LABEL: @asm_goto_noreturn | ||
#[no_mangle] | ||
pub unsafe fn asm_goto_noreturn() -> u64 { | ||
let out: u64; | ||
// CHECK: callbr void asm sideeffect alignstack inteldialect " | ||
// CHECK-NEXT: to label %unreachable [label %[[JUMPBB:[a-b0-9]+]]] | ||
asm!("jmp {}", label { return 1; }, options(noreturn)); | ||
// CHECK: [[JUMPBB]]: | ||
// CHECK-NEXT: ret i64 1 | ||
out | ||
} |
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,23 @@ | ||
warning: unreachable statement | ||
--> $DIR/goto.rs:99:9 | ||
| | ||
LL | / asm!( | ||
LL | | "jmp {}", | ||
LL | | label { | ||
LL | | return; | ||
LL | | }, | ||
LL | | options(noreturn) | ||
LL | | ); | ||
| |_________- any code following this expression is unreachable | ||
LL | unreachable!(); | ||
| ^^^^^^^^^^^^^^ unreachable statement | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/goto.rs:89:8 | ||
| | ||
LL | #[warn(unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: this warning originates in the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
warning: 1 warning emitted | ||
|
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,111 @@ | ||
// only-x86_64 | ||
// run-pass | ||
// needs-asm-support | ||
// revisions: mirunsafeck thirunsafeck | ||
// [thirunsafeck]compile-flags: -Z thir-unsafeck | ||
|
||
#![deny(unreachable_code)] | ||
#![feature(asm_goto)] | ||
|
||
use std::arch::asm; | ||
|
||
fn goto_fallthough() { | ||
unsafe { | ||
asm!( | ||
"/* {} */", | ||
label { | ||
unreachable!(); | ||
} | ||
) | ||
} | ||
} | ||
|
||
fn goto_jump() { | ||
unsafe { | ||
let mut value = false; | ||
asm!( | ||
"jmp {}", | ||
label { | ||
value = true; | ||
} | ||
); | ||
assert!(value); | ||
} | ||
} | ||
|
||
// asm goto with outputs cause miscompilation in LLVM. UB can be triggered | ||
// when outputs are used inside the label block when optimisation is enabled. | ||
// See: https://github.com/llvm/llvm-project/issues/74483 | ||
/* | ||
fn goto_out_fallthrough() { | ||
unsafe { | ||
let mut out: usize; | ||
asm!( | ||
"lea {}, [{} + 1]", | ||
"/* {} */", | ||
out(reg) out, | ||
in(reg) 0x12345678usize, | ||
label { | ||
unreachable!(); | ||
} | ||
); | ||
assert_eq!(out, 0x12345679); | ||
} | ||
} | ||
fn goto_out_jump() { | ||
unsafe { | ||
let mut value = false; | ||
let mut out: usize; | ||
asm!( | ||
"lea {}, [{} + 1]", | ||
"jmp {}", | ||
out(reg) out, | ||
in(reg) 0x12345678usize, | ||
label { | ||
value = true; | ||
assert_eq!(out, 0x12345679); | ||
} | ||
); | ||
assert!(value); | ||
} | ||
} | ||
*/ | ||
|
||
fn goto_noreturn() { | ||
unsafe { | ||
let a; | ||
asm!( | ||
"jmp {}", | ||
label { | ||
a = 1; | ||
}, | ||
options(noreturn) | ||
); | ||
assert_eq!(a, 1); | ||
} | ||
} | ||
|
||
#[warn(unreachable_code)] | ||
fn goto_noreturn_diverge() { | ||
unsafe { | ||
asm!( | ||
"jmp {}", | ||
label { | ||
return; | ||
}, | ||
options(noreturn) | ||
); | ||
unreachable!(); | ||
//~^ WARN unreachable statement | ||
} | ||
} | ||
|
||
fn main() { | ||
goto_fallthough(); | ||
goto_jump(); | ||
// goto_out_fallthrough(); | ||
// goto_out_jump(); | ||
goto_noreturn(); | ||
goto_noreturn_diverge(); | ||
} |
Oops, something went wrong.