Skip to content

Commit 97dfbc9

Browse files
Aaron1011JohnTitor
authored andcommitted
Apply fixes
Co-Authored-By: Yuki Okushi <huyuumi.dev@gmail.com>
1 parent 01c0b04 commit 97dfbc9

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/panic-implementation.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,49 +49,50 @@ The `extract` function converts the `panic_handler` attribute to a `panic_impl`
4949

5050
Now, we have a matching `panic_handler` lang item in the `libstd`. This function goes
5151
through the same process as the `extern { fn panic_impl }` definition in `libcore`, ending
52-
up with a symbol name of `rust_begin_unwind`. At link time, the symbol refernce in `libcore`
52+
up with a symbol name of `rust_begin_unwind`. At link time, the symbol reference in `libcore`
5353
will be resolved to the definition of `libstd` (the function called `begin_panic_handler` in the
5454
Rust source).
5555

5656
Thus, control flow will pass from libcore to std at runtime. This allows panics from `libcore`
57-
to go through the same infratructure that other panics use (panic hooks, unwinding, etc)
57+
to go through the same infrastructure that other panics use (panic hooks, unwinding, etc)
5858

5959
##### libstd implementation of panic!
6060

61-
This is where the actual panic-related logic begins. In `src/libstd/pancking.rs`,
61+
This is where the actual panic-related logic begins. In `src/libstd/panicking.rs`,
6262
control passes to `rust_panic_with_hook`. This method is responsible
6363
for invoking the global panic hook, and checking for double panics. Finally,
64-
we call ```__rust_start_panic```, which is provided by the panic runtime.
64+
we call `__rust_start_panic`, which is provided by the panic runtime.
6565

66-
The call to ```__rust_start_panic``` is very weird - it is passed a ```*mut &mut dyn BoxMeUp```,
66+
The call to `__rust_start_panic` is very weird - it is passed a `*mut &mut dyn BoxMeUp`,
6767
converted to an `usize`. Let's break this type down:
6868

6969
1. `BoxMeUp` is an internal trait. It is implemented for `PanicPayload`
7070
(a wrapper around the user-supplied payload type), and has a method
71-
```fn box_me_up(&mut self) -> *mut (dyn Any + Send)```.
72-
This method takes the user-provided payload (`T: Any + Send`), boxes it, and convertes the box to a raw pointer.
71+
`fn box_me_up(&mut self) -> *mut (dyn Any + Send)`.
72+
This method takes the user-provided payload (`T: Any + Send`),
73+
boxes it, and converts the box to a raw pointer.
7374

74-
2. When we call ```__rust_start_panic```, we have an `&mut dyn BoxMeUp`.
75+
2. When we call `__rust_start_panic`, we have an `&mut dyn BoxMeUp`.
7576
However, this is a fat pointer (twice the size of a `usize`).
7677
To pass this to the panic runtime across an FFI boundary, we take a mutable
7778
reference *to this mutable reference* (`&mut &mut dyn BoxMeUp`), and convert it to a raw pointer
7879
(`*mut &mut dyn BoxMeUp`). The outer raw pointer is a thin pointer, since it points to a `Sized`
7980
type (a mutable reference). Therefore, we can convert this thin pointer into a `usize`, which
8081
is suitable for passing across an FFI boundary.
8182

82-
Finally, we call ```__rust_start_panic``` with this `usize`. We have now entered the panic runtime.
83+
Finally, we call `__rust_start_panic` with this `usize`. We have now entered the panic runtime.
8384

8485
#### Step 2: The panic runtime
8586

8687
Rust provides two panic runtimes: `libpanic_abort` and `libpanic_unwind`. The user chooses
8788
between them at build time via their `Cargo.toml`
8889

89-
`libpanic_abort` is extremely simple: its implementation of ```__rust_start_panic``` just aborts,
90+
`libpanic_abort` is extremely simple: its implementation of `__rust_start_panic` just aborts,
9091
as you would expect.
9192

9293
`libpanic_unwind` is the more interesting case.
9394

94-
In its implementation of ```__rust_start_panic```, we take the `usize`, convert
95+
In its implementation of `__rust_start_panic`, we take the `usize`, convert
9596
it back to a `*mut &mut dyn BoxMeUp`, dereference it, and call `box_me_up`
9697
on the `&mut dyn BoxMeUp`. At this point, we have a raw pointer to the payload
9798
itself (a `*mut (dyn Send + Any)`): that is, a raw pointer to the actual value

0 commit comments

Comments
 (0)