Skip to content

Commit 96623ad

Browse files
authored
Fix panic_handler (#2)
- Last implementation caused undefined behavior by executing a nonexistent exception handler.
1 parent 39d1c00 commit 96623ad

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(core_intrinsics)]
22
#![feature(global_asm)]
33
#![feature(lang_items)]
4+
#![feature(llvm_asm)]
45
#![warn(rust_2018_idioms)]
56
#![allow(unused_attributes)]
67
#![no_std]
@@ -14,32 +15,32 @@ use core::panic::PanicInfo;
1415
/// This is the executable start function, which directly follows the entry point.
1516
#[cfg_attr(not(test), lang = "start")]
1617
#[cfg(not(test))]
17-
extern "C" fn start<T>(user_main: *const (), _argc: isize, _argv: *const *const u8) -> isize
18+
extern "C" fn start<T>(user_main: *const (), _argc: isize, _argv: *const *const u8) -> !
1819
where
1920
T: Termination,
2021
{
2122
let user_main: fn() -> T = unsafe { core::mem::transmute(user_main) };
22-
user_main().report() as isize
23+
user_main();
24+
25+
panic!("main() cannot return");
2326
}
2427

2528
/// Termination trait required for the start function.
2629
#[cfg_attr(not(test), lang = "termination")]
27-
trait Termination {
28-
fn report(self) -> i32;
29-
}
30+
trait Termination {}
3031

3132
/// This implementation does the bare minimum to satisfy the executable start function.
32-
impl Termination for () {
33-
fn report(self) -> i32 {
34-
0
35-
}
36-
}
33+
impl Termination for () {}
3734

3835
/// This function is called on panic.
3936
#[cfg_attr(not(test), panic_handler)]
4037
#[no_mangle]
4138
fn panic(_info: &PanicInfo<'_>) -> ! {
42-
core::intrinsics::abort();
39+
loop {
40+
// A loop without side effects may be optimized away by LLVM. This issue can be avoided with
41+
// a volatile no-op. See: https://github.com/rust-lang/rust/issues/28728
42+
unsafe { llvm_asm!("" :::: "volatile") };
43+
}
4344
}
4445

4546
/// Error handler personality language item (current no-op, to satisfy clippy).

src/platforms/n64/entrypoint.s

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ _start:
5050
li $t1, FS_START
5151
sw $t0, 0($t1)
5252

53-
// So if we want to get fancy, we can load a second stage here.
54-
// The second stage should contain an ELF parser and TLB Initialization.
55-
// The ELF (kernel image) should be loaded into virtual memory by the second
56-
// stage, and its entry point executed.
57-
5853
// Jump to Rust
5954
jal main
6055
nop
61-
62-
// Panic!
63-
// We can't do much here aside from looping
64-
1:
65-
j 1b
66-
nop

0 commit comments

Comments
 (0)