-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent unwinding past FFI boundaries #46833
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ use rustc::mir::visit::{MutVisitor, TyContext}; | |
use rustc::ty::{self, Ty, TyCtxt}; | ||
use rustc::ty::subst::Substs; | ||
use rustc::util::nodemap::NodeMap; | ||
use rustc_back::PanicStrategy; | ||
use rustc_const_eval::pattern::{BindingMode, PatternKind}; | ||
use rustc_data_structures::indexed_vec::{IndexVec, Idx}; | ||
use shim; | ||
|
@@ -353,6 +354,27 @@ macro_rules! unpack { | |
}; | ||
} | ||
|
||
fn needs_abort_block<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, | ||
fn_id: ast::NodeId, | ||
abi: Abi) | ||
-> bool { | ||
|
||
// Not callable from C, so we can safely unwind through these | ||
if abi == Abi::Rust || abi == Abi::RustCall { return false; } | ||
|
||
// We never unwind, so it's not relevant to stop an unwind | ||
if tcx.sess.panic_strategy() != PanicStrategy::Unwind { return false; } | ||
|
||
// We cannot add landing pads, so don't add one | ||
if tcx.sess.no_landing_pads() { return false; } | ||
|
||
// This is a special case: some functions have a C abi but are meant to | ||
// unwind anyway. Don't stop them. | ||
if tcx.has_attr(tcx.hir.local_def_id(fn_id), "unwind") { return false; } | ||
|
||
return true; | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
/// the main entry point for building MIR for a function | ||
|
||
|
@@ -383,6 +405,11 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>, | |
let source_info = builder.source_info(span); | ||
let call_site_s = (call_site_scope, source_info); | ||
unpack!(block = builder.in_scope(call_site_s, LintLevel::Inherited, block, |builder| { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stray newline |
||
if needs_abort_block(tcx, fn_id, abi) { | ||
builder.schedule_abort(); | ||
} | ||
|
||
let arg_scope_s = (arg_scope, source_info); | ||
unpack!(block = builder.in_scope(arg_scope_s, LintLevel::Inherited, block, |builder| { | ||
builder.args_and_body(block, &arguments, arg_scope, &body.value) | ||
|
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
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,43 @@ | ||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Since we mark some ABIs as "nounwind" to LLVM, we must make sure that | ||
// we never unwind through them. | ||
|
||
// ignore-emscripten no processes | ||
|
||
use std::{env, panic}; | ||
use std::io::prelude::*; | ||
use std::io; | ||
use std::process::{Command, Stdio}; | ||
|
||
extern "C" fn panic_in_ffi() { | ||
panic!("Test"); | ||
} | ||
|
||
fn test() { | ||
let _ = panic::catch_unwind(|| { panic_in_ffi(); }); | ||
// The process should have aborted by now. | ||
io::stdout().write(b"This should never be printed.\n"); | ||
let _ = io::stdout().flush(); | ||
} | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() > 1 && args[1] == "test" { | ||
return test(); | ||
} | ||
|
||
let mut p = Command::new(&args[0]) | ||
.stdout(Stdio::piped()) | ||
.stdin(Stdio::piped()) | ||
.arg("test").spawn().unwrap(); | ||
assert!(!p.wait().unwrap().success()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this to
should_abort_on_panic
instead?