Skip to content

Latest commit

 

History

History
44 lines (25 loc) · 2.66 KB

File metadata and controls

44 lines (25 loc) · 2.66 KB

Panic statement

We conducted a study to analyze the characteristics of panic-related assemblies in Rust and understand differences between unwind and abort, which are behavior on a panic.

Study results

  • The panic!() macro is expanded into a call to the core::panicking::panic_fmt() function. During this expansion, the core::panic::Location struct that contains the file name, line number and column number where the panic has occurred is passed as an argument.

  • We found that the panic-related assembly code contain calls to functions that do not return and calls to Window API including RtlFailFast() and _CxxThrowException().

  • Behavior on a panic includes release of resources, such as strings, when using the unwind option. In contrast, such resources are not released when using the abort option.

Details

Characteristics of panic-related assembly code

The panic!() macro is expanded into a call to the core::panicking::panic_fmt() function as shown below. Core::panicking::panic_fmt() is a function that serves as an entry point triggering a panic along with a panic message.

panic

This function receives a formatted panic message in its first argument and the core::panic::Location structure in its second argument. The core::panic::Location struct stores the paths, line numbers and column numbers in the source code where the panic has occurred and provides information that helps you identify the point to start processing relating to the panic from.

panic

However, when -Zlocation-detail=none, which is used for building minimized binaries, is applied, the core::panic::Location structure becomes as shown below, and panic-related information is removed.

panic

Differences between unwind and abort in assembly code

Rust specifies either unwind or abort as the behavior on panic. unwind is the default behavior that unwinds the stack when the program falls into a panic and terminates the program after completing necessary actions including release of resources. abort does not unwind the stack and immediately terminates the program. When unwind is functioning, code, including the drop_in_place function, as shown below is added that has the assembly release resources.

panic

Differences for 32-bit and size-minimized binaries

We could find the characteristics described in [Characteristics of panic-related assemblies](#Characteristics of panic-related assemblies) also in 32-bit binaries.

Size-minimized binaries dropped panic-related processing due to the effects of -Zbuild-std-features=panic_immediate_abort the build option specified.