Skip to content

Commit f82eb4d

Browse files
committed
Auto merge of #127926 - Oneirical:classical-orctestra, r=jieyouxu
Migrate `foreign-double-unwind`, `issue-36710` and `foreign-exceptions` `run-make` tests to rmake Part of #121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html). ~~This is expected to fail CI, I am getting a weird `core dumped` error on `foreign-double-unwind` locally. Posting this to start a discussion.~~ EDIT: I got it, `foreign_double_unwind` is *supposed* to fail in the execution stage, but this wasn't obvious, because the original test was just piping the stdout to `CGREP` (which silently ignores errors). try-job: aarch64-apple try-job: armhf-gnu try-job: test-various try-job: x86_64-msvc try-job: x86_64-gnu-llvm-18 try-job: i686-msvc try-job: dist-i586-gnu-i586-i686-musl
2 parents 5367673 + 46b4083 commit f82eb4d

File tree

12 files changed

+149
-48
lines changed

12 files changed

+149
-48
lines changed

Diff for: src/tools/run-make-support/src/external_deps/c_build.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::path::PathBuf;
22

33
use super::cygpath::get_windows_path;
44
use crate::artifact_names::{dynamic_lib_name, static_lib_name};
5-
use crate::external_deps::cc::cc;
5+
use crate::external_deps::cc::{cc, cxx};
66
use crate::external_deps::llvm::llvm_ar;
77
use crate::path_helpers::path;
88
use crate::targets::{is_darwin, is_msvc, is_windows};
99

1010
// FIXME(Oneirical): These native build functions should take a Path-based generic.
1111

1212
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
13+
/// Built from a C file.
1314
#[track_caller]
1415
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
1516
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
@@ -58,3 +59,24 @@ pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
5859
}
5960
path(lib_path)
6061
}
62+
63+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
64+
/// Built from a C++ file.
65+
#[track_caller]
66+
pub fn build_native_static_lib_cxx(lib_name: &str) -> PathBuf {
67+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
68+
let src = format!("{lib_name}.cpp");
69+
let lib_path = static_lib_name(lib_name);
70+
if is_msvc() {
71+
cxx().arg("-EHs").arg("-c").out_exe(&obj_file).input(src).run();
72+
} else {
73+
cxx().arg("-c").out_exe(&obj_file).input(src).run();
74+
};
75+
let obj_file = if is_msvc() {
76+
PathBuf::from(format!("{lib_name}.obj"))
77+
} else {
78+
PathBuf::from(format!("{lib_name}.o"))
79+
};
80+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
81+
path(lib_path)
82+
}

Diff for: src/tools/run-make-support/src/external_deps/rustc.rs

+59
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_darwin, 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,62 @@ 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+
// So this is a bit hacky: we can't use the DLL version of libstdc++ because
348+
// it pulls in the DLL version of libgcc, which means that we end up with 2
349+
// instances of the DW2 unwinding implementation. This is a problem on
350+
// i686-pc-windows-gnu because each module (DLL/EXE) needs to register its
351+
// unwind information with the unwinding implementation, and libstdc++'s
352+
// __cxa_throw won't see the unwinding info we registered with our statically
353+
// linked libgcc.
354+
//
355+
// Now, simply statically linking libstdc++ would fix this problem, except
356+
// that it is compiled with the expectation that pthreads is dynamically
357+
// linked as a DLL and will fail to link with a statically linked libpthread.
358+
//
359+
// So we end up with the following hack: we link use static:-bundle to only
360+
// link the parts of libstdc++ that we actually use, which doesn't include
361+
// the dependency on the pthreads DLL.
362+
if is_msvc() { None } else { Some("-lstatic:-bundle=stdc++") }
363+
} else if is_darwin() {
364+
Some("-lc++")
365+
} else {
366+
match &uname()[..] {
367+
"FreeBSD" | "SunOS" | "OpenBSD" => None,
368+
_ => Some("-lstdc++"),
369+
}
370+
};
371+
if let Some(flag) = flag {
372+
self.cmd.arg(flag);
373+
}
374+
self
375+
}
317376
}

Diff for: src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub use wasmparser;
4444
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};
4545

4646
// These rely on external dependencies.
47-
pub use c_build::{build_native_dynamic_lib, build_native_static_lib};
4847
pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc};
48+
pub use c_build::{build_native_dynamic_lib, build_native_static_lib, build_native_static_lib_cxx};
4949
pub use clang::{clang, Clang};
5050
pub use htmldocck::htmldocck;
5151
pub use llvm::{

Diff for: src/tools/tidy/src/allowed_run_make_makefiles.txt

-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ run-make/dep-info-spaces/Makefile
99
run-make/dep-info/Makefile
1010
run-make/emit-to-stdout/Makefile
1111
run-make/extern-fn-reachable/Makefile
12-
run-make/foreign-double-unwind/Makefile
13-
run-make/foreign-exceptions/Makefile
1412
run-make/incr-add-rust-src-component/Makefile
15-
run-make/issue-36710/Makefile
1613
run-make/issue-84395-lto-embed-bitcode/Makefile
1714
run-make/issue-88756-default-output/Makefile
1815
run-make/jobserver-error/Makefile
File renamed without changes.
File renamed without changes.

Diff for: tests/run-make/cpp-global-destructors/rmake.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Some start files were missed when originally writing the logic to swap in musl start files.
2+
// This caused #36710. After the fix in #50105, this test checks that linking to C++ code
3+
// with global destructors works.
4+
// See https://github.com/rust-lang/rust/pull/50105
5+
6+
//@ ignore-cross-compile
7+
// Reason: the compiled binary is executed
8+
9+
//@ ignore-none
10+
// Reason: no-std is not supported.
11+
//@ ignore-wasm32
12+
//@ ignore-wasm64
13+
// Reason: compiling C++ to WASM may cause problems.
14+
15+
// Neither of these are tested in full CI.
16+
//@ ignore-nvptx64-nvidia-cuda
17+
// Reason: can't find crate "std"
18+
//@ ignore-sgx
19+
20+
use run_make_support::{build_native_static_lib_cxx, run, rustc};
21+
22+
fn main() {
23+
build_native_static_lib_cxx("foo");
24+
rustc().input("foo.rs").arg("-lfoo").extra_rs_cxx_flags().run();
25+
run("foo");
26+
}

Diff for: tests/run-make/foreign-double-unwind/Makefile

-12
This file was deleted.

Diff for: tests/run-make/foreign-double-unwind/rmake.rs

+21
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_fail, 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_fail("foo").assert_stdout_not_contains("unreachable");
21+
}

Diff for: tests/run-make/foreign-exceptions/Makefile

-12
This file was deleted.

Diff for: tests/run-make/foreign-exceptions/rmake.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This test was created to check that compilation and execution still works
2+
// after the addition of a new feature, in #65646: the ability to unwind panics
3+
// and exceptions back and forth between Rust and C++. This is a basic smoke test,
4+
// this feature being broken in quiet or subtle ways could still result in this test
5+
// passing.
6+
// See https://github.com/rust-lang/rust/pull/65646
7+
8+
//@ needs-unwind
9+
// Reason: this test exercises panic unwinding
10+
//@ ignore-cross-compile
11+
// Reason: the compiled binary is executed
12+
13+
use run_make_support::{build_native_static_lib_cxx, run, rustc};
14+
15+
fn main() {
16+
build_native_static_lib_cxx("foo");
17+
rustc().input("foo.rs").arg("-lfoo").extra_rs_cxx_flags().run();
18+
run("foo");
19+
}

Diff for: tests/run-make/issue-36710/Makefile

-19
This file was deleted.

0 commit comments

Comments
 (0)