Skip to content

Commit e3cf7e5

Browse files
committed
rewrite foreign-double-unwind to rmake
1 parent b01a977 commit e3cf7e5

File tree

7 files changed

+147
-15
lines changed

7 files changed

+147
-15
lines changed

src/tools/run-make-support/src/external_deps/c_build.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::path::PathBuf;
22

33
use crate::artifact_names::static_lib_name;
4-
use crate::external_deps::cc::cc;
4+
use crate::external_deps::cc::{cc, cxx};
55
use crate::external_deps::llvm::llvm_ar;
66
use crate::path_helpers::path;
77
use crate::targets::is_msvc;
88

99
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
10+
/// Built from a C file.
1011
#[track_caller]
1112
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
1213
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
@@ -25,3 +26,24 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
2526
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
2627
path(lib_path)
2728
}
29+
30+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
31+
/// Built from a C++ file.
32+
#[track_caller]
33+
pub fn build_native_static_lib_cxx(lib_name: &str) -> PathBuf {
34+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
35+
let src = format!("{lib_name}.cpp");
36+
let lib_path = static_lib_name(lib_name);
37+
if is_msvc() {
38+
cxx().arg("-EHs").arg("-c").out_exe(&obj_file).input(src).run();
39+
} else {
40+
cxx().arg("-c").out_exe(&obj_file).input(src).run();
41+
};
42+
let obj_file = if is_msvc() {
43+
PathBuf::from(format!("{lib_name}.obj"))
44+
} else {
45+
PathBuf::from(format!("{lib_name}.o"))
46+
};
47+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
48+
path(lib_path)
49+
}

src/tools/run-make-support/src/external_deps/cc.rs

+59
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ pub fn cc() -> Cc {
1515
Cc::new()
1616
}
1717

18+
/// Construct a new platform-specific CXX compiler invocation.
19+
#[track_caller]
20+
pub fn cxx() -> Cc {
21+
Cc::cxx()
22+
}
23+
1824
/// A platform-specific C compiler invocation builder. The specific C compiler used is
1925
/// passed down from compiletest.
2026
#[derive(Debug)]
@@ -44,6 +50,21 @@ impl Cc {
4450
Self { cmd }
4551
}
4652

53+
/// Construct a new platform-specific C compiler invocation.
54+
#[track_caller]
55+
pub fn cxx() -> Self {
56+
let compiler = env_var("CXX");
57+
58+
let mut cmd = Command::new(compiler);
59+
60+
let default_cflags = env_var("CXX_DEFAULT_FLAGS");
61+
for flag in default_cflags.split(char::is_whitespace) {
62+
cmd.arg(flag);
63+
}
64+
65+
Self { cmd }
66+
}
67+
4768
/// Specify path of the input file.
4869
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
4970
self.cmd.arg(path.as_ref());
@@ -192,3 +213,41 @@ pub fn extra_cxx_flags() -> Vec<&'static str> {
192213
}
193214
}
194215
}
216+
217+
/// `EXTRARSCXXFLAGS`
218+
pub fn extra_rs_cxx_flags() -> Vec<&'static str> {
219+
// Adapted from tools.mk (trimmed):
220+
//
221+
// ```makefile
222+
// ifdef IS_WINDOWS
223+
// ifdef IS_MSVC
224+
// else
225+
// EXTRARSCXXFLAGS := -lstatic:-bundle=stdc++
226+
// endif
227+
// else
228+
// ifeq ($(UNAME),Darwin)
229+
// EXTRARSCXXFLAGS := -lc++
230+
// else
231+
// ifeq ($(UNAME),FreeBSD)
232+
// else
233+
// ifeq ($(UNAME),SunOS)
234+
// else
235+
// ifeq ($(UNAME),OpenBSD)
236+
// else
237+
// EXTRARSCXXFLAGS := -lstdc++
238+
// endif
239+
// endif
240+
// endif
241+
// endif
242+
// endif
243+
// ```
244+
if is_windows() {
245+
if is_msvc() { vec![] } else { vec!["-lstatic:-bundle=stdc++"] }
246+
} else {
247+
match &uname()[..] {
248+
"Darwin" => vec!["-lc++"],
249+
"FreeBSD" | "SunOS" | "OpenBSD" => vec![],
250+
_ => vec!["-lstdc++"],
251+
}
252+
}
253+
}

src/tools/run-make-support/src/external_deps/rustc.rs

+43
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::command::Command;
55
use crate::env::env_var;
66
use crate::path_helpers::cwd;
77
use crate::util::set_host_rpath;
8+
use crate::{is_msvc, is_windows, uname};
89

910
/// Construct a new `rustc` invocation. This will automatically set the library
1011
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -314,4 +315,46 @@ impl Rustc {
314315
self.cmd.arg(format!("-Clinker-flavor={linker_flavor}"));
315316
self
316317
}
318+
319+
/// `EXTRARSCXXFLAGS`
320+
pub fn extra_rs_cxx_flags(&mut self) -> &mut Self {
321+
// Adapted from tools.mk (trimmed):
322+
//
323+
// ```makefile
324+
// ifdef IS_WINDOWS
325+
// ifdef IS_MSVC
326+
// else
327+
// EXTRARSCXXFLAGS := -lstatic:-bundle=stdc++
328+
// endif
329+
// else
330+
// ifeq ($(UNAME),Darwin)
331+
// EXTRARSCXXFLAGS := -lc++
332+
// else
333+
// ifeq ($(UNAME),FreeBSD)
334+
// else
335+
// ifeq ($(UNAME),SunOS)
336+
// else
337+
// ifeq ($(UNAME),OpenBSD)
338+
// else
339+
// EXTRARSCXXFLAGS := -lstdc++
340+
// endif
341+
// endif
342+
// endif
343+
// endif
344+
// endif
345+
// ```
346+
let flag = if is_windows() {
347+
if is_msvc() { None } else { Some("-lstatic:-bundle=stdc++") }
348+
} else {
349+
match &uname()[..] {
350+
"Darwin" => Some("-lc++"),
351+
"FreeBSD" | "SunOS" | "OpenBSD" => None,
352+
_ => Some("-lstdc++"),
353+
}
354+
};
355+
if let Some(flag) = flag {
356+
self.cmd.arg(flag);
357+
}
358+
self
359+
}
317360
}

src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use wasmparser;
3737
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};
3838

3939
// These rely on external dependencies.
40-
pub use c_build::build_native_static_lib;
40+
pub use c_build::{build_native_static_lib, build_native_static_lib_cxx};
4141
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
4242
pub use clang::{clang, Clang};
4343
pub use htmldocck::htmldocck;

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ run-make/extern-fn-with-union/Makefile
2929
run-make/extern-multiple-copies/Makefile
3030
run-make/extern-multiple-copies2/Makefile
3131
run-make/fmt-write-bloat/Makefile
32-
run-make/foreign-double-unwind/Makefile
3332
run-make/foreign-exceptions/Makefile
3433
run-make/foreign-rust-exceptions/Makefile
3534
run-make/incr-add-rust-src-component/Makefile

tests/run-make/foreign-double-unwind/Makefile

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// When using foreign function interface (FFI) with C++, it is possible
2+
// to run into a "double unwind" if either both Rust and C++ run into a panic
3+
// and exception at the same time, or C++ encounters two exceptions. In this case,
4+
// one of the panic unwinds would be leaked and the other would be kept, leading
5+
// to undefined behaviour. After this was fixed in #92911, this test checks that
6+
// the keyword "unreachable" indicative of this bug triggering in this specific context
7+
// does not appear after successfully compiling and executing the program.
8+
// See https://github.com/rust-lang/rust/pull/92911
9+
10+
//@ needs-unwind
11+
// Reason: this test exercises panic unwinding
12+
//@ ignore-cross-compile
13+
// Reason: the compiled binary is executed
14+
15+
use run_make_support::{build_native_static_lib_cxx, run, rustc};
16+
17+
fn main() {
18+
build_native_static_lib_cxx("foo");
19+
rustc().input("foo.rs").arg("-lfoo").extra_rs_cxx_flags().run();
20+
run("foo").assert_stdout_not_contains("unreachable");
21+
}

0 commit comments

Comments
 (0)