forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#88759 - Amanieu:panic_in_drop, r=nagisa,eddyb
Add -Z panic-in-drop={unwind,abort} command-line option This PR changes `Drop` to abort if an unwinding panic attempts to escape it, making the process abort instead. This has several benefits: - The current behavior when unwinding out of `Drop` is very unintuitive and easy to miss: unwinding continues, but the remaining drops in scope are simply leaked. - A lot of unsafe code doesn't expect drops to unwind, which can lead to unsoundness: - servo/rust-smallvec#14 - bluss/arrayvec#3 - There is a code size and compilation time cost to this: LLVM needs to generate extra landing pads out of all calls in a drop implementation. This can compound when functions are inlined since unwinding will then continue on to process drops in the callee, which can itself unwind, etc. - Initial measurements show a 3% size reduction and up to 10% compilation time reduction on some crates (`syn`). One thing to note about `-Z panic-in-drop=abort` is that *all* crates must be built with this option for it to be sound since it makes the compiler assume that dropping `Box<dyn Any>` will never unwind. cc rust-lang/lang-team#97
- Loading branch information
Showing
11 changed files
with
119 additions
and
17 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
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
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,54 @@ | ||
// compile-flags: -Z panic-in-drop=abort -O | ||
|
||
// Ensure that unwinding code paths are eliminated from the output after | ||
// optimization. | ||
|
||
#![crate_type = "lib"] | ||
use std::any::Any; | ||
use std::mem::forget; | ||
|
||
pub struct ExternDrop; | ||
impl Drop for ExternDrop { | ||
#[inline(always)] | ||
fn drop(&mut self) { | ||
// This call may potentially unwind. | ||
extern "Rust" { | ||
fn extern_drop(); | ||
} | ||
unsafe { | ||
extern_drop(); | ||
} | ||
} | ||
} | ||
|
||
struct AssertNeverDrop; | ||
impl Drop for AssertNeverDrop { | ||
#[inline(always)] | ||
fn drop(&mut self) { | ||
// This call should be optimized away as unreachable. | ||
extern "C" { | ||
fn should_not_appear_in_output(); | ||
} | ||
unsafe { | ||
should_not_appear_in_output(); | ||
} | ||
} | ||
} | ||
|
||
// CHECK-LABEL: normal_drop | ||
// CHECK-NOT: should_not_appear_in_output | ||
#[no_mangle] | ||
pub fn normal_drop(x: ExternDrop) { | ||
let guard = AssertNeverDrop; | ||
drop(x); | ||
forget(guard); | ||
} | ||
|
||
// CHECK-LABEL: indirect_drop | ||
// CHECK-NOT: should_not_appear_in_output | ||
#[no_mangle] | ||
pub fn indirect_drop(x: Box<dyn Any>) { | ||
let guard = AssertNeverDrop; | ||
drop(x); | ||
forget(guard); | ||
} |