-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow foreign exceptions to unwind through Rust code
- Loading branch information
Showing
9 changed files
with
333 additions
and
256 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
Large diffs are not rendered by default.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-include ../tools.mk | ||
|
||
all: foo | ||
$(call RUN,foo) | ||
|
||
foo: foo.rs $(call NATIVE_STATICLIB,foo) | ||
$(RUSTC) $< -lfoo $(EXTRACXXFLAGS) | ||
|
||
$(TMPDIR)/libfoo.o: foo.cpp | ||
$(call COMPILE_OBJ_CXX,$@,$<) |
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,59 @@ | ||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
|
||
void println(const char* s) { | ||
puts(s); | ||
fflush(stdout); | ||
} | ||
|
||
struct exception {}; | ||
|
||
struct drop_check { | ||
bool* ok; | ||
~drop_check() { | ||
println("~drop_check"); | ||
|
||
if (ok) | ||
*ok = true; | ||
} | ||
}; | ||
|
||
extern "C" { | ||
void rust_catch_callback(void (*cb)(), bool* rust_ok); | ||
|
||
static void callback() { | ||
println("throwing C++ exception"); | ||
throw exception(); | ||
} | ||
|
||
void throw_cxx_exception() { | ||
bool rust_ok = false; | ||
try { | ||
rust_catch_callback(callback, &rust_ok); | ||
assert(false && "unreachable"); | ||
} catch (exception e) { | ||
println("caught C++ exception"); | ||
assert(rust_ok); | ||
return; | ||
} | ||
assert(false && "did not catch thrown C++ exception"); | ||
} | ||
|
||
void cxx_catch_callback(void (*cb)(), bool* cxx_ok) { | ||
drop_check x; | ||
x.ok = NULL; | ||
try { | ||
cb(); | ||
} catch (exception e) { | ||
assert(false && "shouldn't be able to catch a rust panic"); | ||
} catch (...) { | ||
println("caught foreign exception in catch (...)"); | ||
// Foreign exceptions are caught by catch (...). We only set the ok | ||
// flag if we successfully caught the panic. The destructor of | ||
// drop_check will then set the flag to true if it is executed. | ||
x.ok = cxx_ok; | ||
throw; | ||
} | ||
} | ||
} |
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,63 @@ | ||
// Tests that C++ exceptions can unwind through Rust code, run destructors and | ||
// are ignored by catch_unwind. Also tests that Rust panics can unwind through | ||
// C++ code. | ||
|
||
#![feature(unwind_attributes)] | ||
|
||
use std::panic::{catch_unwind, AssertUnwindSafe}; | ||
|
||
struct DropCheck<'a>(&'a mut bool); | ||
impl<'a> Drop for DropCheck<'a> { | ||
fn drop(&mut self) { | ||
println!("DropCheck::drop"); | ||
*self.0 = true; | ||
} | ||
} | ||
|
||
extern "C" { | ||
fn throw_cxx_exception(); | ||
|
||
#[unwind(allowed)] | ||
fn cxx_catch_callback(cb: extern "C" fn(), ok: *mut bool); | ||
} | ||
|
||
#[no_mangle] | ||
#[unwind(allowed)] | ||
extern "C" fn rust_catch_callback(cb: extern "C" fn(), rust_ok: &mut bool) { | ||
let _caught_unwind = catch_unwind(AssertUnwindSafe(|| { | ||
let _drop = DropCheck(rust_ok); | ||
cb(); | ||
unreachable!("should have unwound instead of returned"); | ||
})); | ||
unreachable!("catch_unwind should not have caught foreign exception"); | ||
} | ||
|
||
fn throw_rust_panic() { | ||
#[unwind(allowed)] | ||
extern "C" fn callback() { | ||
println!("throwing rust panic"); | ||
panic!(1234i32); | ||
} | ||
|
||
let mut dropped = false; | ||
let mut cxx_ok = false; | ||
let caught_unwind = catch_unwind(AssertUnwindSafe(|| { | ||
let _drop = DropCheck(&mut dropped); | ||
unsafe { | ||
cxx_catch_callback(callback, &mut cxx_ok); | ||
} | ||
unreachable!("should have unwound instead of returned"); | ||
})); | ||
println!("caught rust panic"); | ||
assert!(dropped); | ||
assert!(caught_unwind.is_err()); | ||
let panic_obj = caught_unwind.unwrap_err(); | ||
let panic_int = *panic_obj.downcast_ref::<i32>().unwrap(); | ||
assert_eq!(panic_int, 1234); | ||
assert!(cxx_ok); | ||
} | ||
|
||
fn main() { | ||
unsafe { throw_cxx_exception() }; | ||
throw_rust_panic(); | ||
} |