Skip to content

Commit 0804e47

Browse files
committed
Add test for foreign double unwind
1 parent 42b41da commit 0804e47

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-include ../tools.mk
2+
3+
all: foo
4+
$(call RUN,foo) | $(CGREP) -v unreachable
5+
6+
foo: foo.rs $(call NATIVE_STATICLIB,foo)
7+
$(RUSTC) $< -lfoo $(EXTRARSCXXFLAGS)
8+
9+
$(TMPDIR)/libfoo.o: foo.cpp
10+
$(call COMPILE_OBJ_CXX,$@,$<)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <cstdio>
2+
#include <exception>
3+
4+
void println(const char* s) {
5+
puts(s);
6+
fflush(stdout);
7+
}
8+
9+
struct outer_exception {};
10+
struct inner_exception {};
11+
12+
extern "C" {
13+
void throw_cxx_exception() {
14+
if (std::uncaught_exception()) {
15+
println("throwing inner C++ exception");
16+
throw inner_exception();
17+
} else {
18+
println("throwing outer C++ exception");
19+
throw outer_exception();
20+
}
21+
}
22+
23+
void cxx_catch_callback(void (*cb)()) {
24+
try {
25+
cb();
26+
println("unreachable: callback returns");
27+
} catch (outer_exception) {
28+
println("unreachable: caught outer exception in catch (...)");
29+
} catch (inner_exception) {
30+
println("unreachable: caught inner exception in catch (...)");
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Tests that C++ double unwinding through Rust code will be properly guarded
2+
// against instead of exhibiting undefined behaviour.
3+
4+
#![feature(c_unwind)]
5+
6+
extern "C-unwind" {
7+
fn throw_cxx_exception();
8+
fn cxx_catch_callback(cb: extern "C-unwind" fn());
9+
}
10+
11+
struct ThrowOnDrop;
12+
13+
impl Drop for ThrowOnDrop {
14+
fn drop(&mut self) {
15+
unsafe { throw_cxx_exception() };
16+
}
17+
}
18+
19+
extern "C-unwind" fn test_double_unwind() {
20+
let _a = ThrowOnDrop;
21+
let _b = ThrowOnDrop;
22+
}
23+
24+
fn main() {
25+
unsafe { cxx_catch_callback(test_double_unwind) };
26+
}

0 commit comments

Comments
 (0)