Skip to content

Commit 9c9f2c3

Browse files
committed
Fix segfault related to __builtin_unreachable with inline asm
1 parent 9b2d8e5 commit 9c9f2c3

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/asm.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
575575
}
576576
if dest.is_none() && options.contains(InlineAsmOptions::NORETURN) {
577577
let builtin_unreachable = self.context.get_builtin_function("__builtin_unreachable");
578-
let builtin_unreachable: RValue<'gcc> =
579-
unsafe { std::mem::transmute(builtin_unreachable) };
580-
self.call(self.type_void(), None, None, builtin_unreachable, &[], None, None);
578+
self.llbb().add_eval(None, self.context.new_call(None, builtin_unreachable, &[]));
581579
}
582580

583581
// Write results to outputs.

tests/run/unreachable-function.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Compiler:
2+
//
3+
// Run-time:
4+
// status: 0
5+
6+
use std::arch::asm;
7+
8+
fn exit_syscall(status: i32) -> ! {
9+
#[cfg(target_arch = "x86_64")]
10+
unsafe {
11+
asm!(
12+
"syscall",
13+
in("rax") 60,
14+
in("rdi") status,
15+
options(noreturn)
16+
);
17+
}
18+
19+
#[cfg(not(target_arch = "x86_64"))]
20+
std::process::exit(1);
21+
}
22+
23+
fn main() {
24+
// Used to crash with rustc_codegen_gcc.
25+
exit_syscall(0);
26+
std::process::exit(1);
27+
}

0 commit comments

Comments
 (0)