Skip to content

Commit

Permalink
Mark all extern functions as nounwind
Browse files Browse the repository at this point in the history
Unwinding across an FFI boundary is undefined behaviour, so we can mark
all external function as nounwind. The obvious exception are those
functions that actually perform the unwinding.
  • Loading branch information
dotdash committed Sep 14, 2015
1 parent e4e67bd commit 3ef75d5
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Expand Up @@ -79,6 +79,7 @@
#![feature(optin_builtin_traits)]
#![feature(reflect)]
#![feature(rustc_attrs)]
#![feature(unwind_attributes)]
#![cfg_attr(stage0, feature(simd))]
#![cfg_attr(not(stage0), feature(repr_simd, platform_intrinsics))]
#![feature(staged_api)]
Expand Down
1 change: 1 addition & 0 deletions src/libcore/panicking.rs
Expand Up @@ -62,6 +62,7 @@ pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
#[allow(improper_ctypes)]
extern {
#[lang = "panic_fmt"]
#[unwind]
fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32) -> !;
}
let (file, line) = *file_line;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_trans/trans/foreign.rs
Expand Up @@ -211,6 +211,7 @@ pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);

let llfn = get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), name, cc, llfn_ty, fty);
attributes::unwind(llfn, false);
add_argument_attributes(&tys, llfn);
attributes::from_fn_attrs(ccx, attrs, llfn);
llfn
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Expand Up @@ -243,6 +243,7 @@
#![feature(unique)]
#![feature(unsafe_no_drop_flag, filling_drop)]
#![feature(decode_utf16)]
#![feature(unwind_attributes)]
#![feature(vec_push_all)]
#![feature(vec_resize)]
#![feature(wrapping)]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/common/libunwind.rs
Expand Up @@ -124,10 +124,12 @@ extern "C" {
// iOS on armv7 uses SjLj exceptions and requires to link
// against corresponding routine (..._SjLj_...)
#[cfg(not(all(target_os = "ios", target_arch = "arm")))]
#[unwind]
pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception)
-> _Unwind_Reason_Code;

#[cfg(all(target_os = "ios", target_arch = "arm"))]
#[unwind]
fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception)
-> _Unwind_Reason_Code;

Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/common/unwind/mod.rs
Expand Up @@ -192,6 +192,7 @@ fn rust_panic(cause: Box<Any + Send + 'static>) -> ! {
#[cfg(not(test))]
/// Entry point of panic from the libcore crate.
#[lang = "panic_fmt"]
#[unwind]
pub extern fn rust_begin_unwind(msg: fmt::Arguments,
file: &'static str, line: u32) -> ! {
begin_unwind_fmt(msg, &(file, line))
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/common/unwind/seh.rs
Expand Up @@ -62,6 +62,7 @@ static PANIC_DATA: StaticKey = StaticKey::new(None);

// This function is provided by kernel32.dll
extern "system" {
#[unwind]
fn RaiseException(dwExceptionCode: DWORD,
dwExceptionFlags: DWORD,
nNumberOfArguments: DWORD,
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/common/unwind/seh64_gnu.rs
Expand Up @@ -93,6 +93,7 @@ pub enum EXCEPTION_DISPOSITION {

// From kernel32.dll
extern "system" {
#[unwind]
fn RaiseException(dwExceptionCode: DWORD,
dwExceptionFlags: DWORD,
nNumberOfArguments: DWORD,
Expand Down Expand Up @@ -198,6 +199,7 @@ unsafe extern fn rust_eh_personality(

#[lang = "eh_unwind_resume"]
#[cfg(not(test))]
#[unwind]
unsafe extern fn rust_eh_unwind_resume(panic_ctx: LPVOID) {
let params = [panic_ctx as ULONG_PTR];
RaiseException(RUST_PANIC,
Expand Down
23 changes: 23 additions & 0 deletions src/test/codegen/extern-functions.rs
@@ -0,0 +1,23 @@
// Copyright 2015 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.

// compile-flags: -C no-prepopulate-passes

#![feature(unwind_attributes)]

extern {
// CHECK: Function Attrs: nounwind
// CHECK-NEXT: declare void @extern_fn
fn extern_fn();
// CHECK-NOT: Function Attrs: nounwind
// CHECK: declare void @unwinding_extern_fn
#[unwind]
fn unwinding_extern_fn();
}

0 comments on commit 3ef75d5

Please sign in to comment.