Windows TLS - Only register the `atexit` hook when `cleanup` can be unloaded Followup to rust-lang/rust#148799 - One of the motivations was to remove the "Synchronization in thread-local destructors" limitation (see the [docs](https://doc.rust-lang.org/std/thread/struct.LocalKey.html#synchronization-in-thread-local-destructors)). However, it is still _possible_ to deadlock on Windows with a destructor that uses a `join`, but **only** during an application shutdown. (Edited. The original PR leaked the dtors when a DLL was unloaded.) This is because calling `FlsFree` from the `atexit` hook _from the main executable_ can deadlock if other threads are also running FLS destructors. This PR tweaks the implementation to only register the hook if `cleanup` can be unloaded, meaning we are loaded as a DLL. This has an added benefit of not triggering `FlsFree` at all during normal application shutdown, which is the most common case and is now handled more naturally by the Windows runtime (which calls the FLS hooks without holding the load-lock). Checking if the `cleanup` hook is unload-able is done by checking if it is in the same module as the main exe `GetModuleHandleExW(FLAG_FROM_ADDRESS, cleanup) == GetModuleHandleW(ptr::null())`. <details> <summary>Join-on-Drop on Process Exit Deadlock Example</summary> ```rust use std:🧵:JoinHandle; use std::time::Duration; struct JoinOnDrop(Option<JoinHandle<()>>); impl Drop for JoinOnDrop { fn drop(&mut self) { println!("Joining thread... ({:?})", std:🧵:current()); self.0.take().unwrap().join().unwrap(); println!("Thread joined"); } } thread_local! { static HANDLE: JoinOnDrop = { let thread = std:🧵:spawn(|| { println!("Starting..."); std:🧵:sleep(Duration::from_secs(3)); println!("Done ({:?})", std:🧵:current()); }); JoinOnDrop(Some(thread)) }; } fn main() { let thread = std:🧵:spawn(|| { HANDLE.with(|_| { println!("Some other thread ({:?})", std:🧵:current()); }) }); // ***Here***, because we do not join beforehand, the drops are called from atexit which is done while in loader-lock, so the the binary will deadlock. // thread.join().unwrap(); std:🧵:sleep(Duration::from_secs(1)); println!("Done ({:?})", std:🧵:current()); } ``` </details> r? @ChrisDenton
The run-make test suite
The run-make test suite contains tests which are the most flexible out of all the rust-lang/rust test suites. run-make tests can basically contain arbitrary code, and are supported by the run_make_support library.
Infrastructure
A run-make test is a test recipe source file rmake.rs accompanied by its parent directory (e.g. tests/run-make/foo/rmake.rs is the foo run-make test).
The implementation for collecting and building the rmake.rs recipes are in src/tools/compiletest/src/runtest.rs, in run_rmake_test.
The setup for the rmake.rs can be summarized as a 3-stage process:
-
First, we build the
run_make_supportlibrary in bootstrap as a tool lib. -
Then, we compile the
rmake.rs"recipe" linking the support library and its dependencies in, and provide a bunch of env vars. We setup a directory structure withinbuild/<target>/test/run-make/<test-name>/ rmake.exe # recipe binary rmake_out/ # sources from test sources copied overand copy non-
rmake.rsinput support files over tormake_out/. The support library is made available as an extern prelude. -
Finally, we run the recipe binary and set
rmake_out/as the working directory.
External dependencies
compiletest passes tool paths and target-specific flags to rmake.rs through
environment variables. Prefer using the helpers in run_make_support over
reading these variables directly. The helpers keep command construction
consistent across hosts and targets, and avoid relying on whichever tools happen
to be first in PATH.
Commonly used helpers include:
rustc()andrustdoc()for the compiler and rustdoc under test, fromRUSTCandRUSTDOC.cc()andcxx()for the target C and C++ compilers, fromCC/CXXplusCC_DEFAULT_FLAGS/CXX_DEFAULT_FLAGS.gcc()for tests that specifically needgcc; unlikecc(), this assumes a suitablegccis available inPATHand does not addCC_DEFAULT_FLAGS.llvm_ar(),llvm_nm(),llvm_objdump(),llvm_readobj(), and similar LLVM tools fromLLVM_BIN_DIR;llvm_filecheck()usesLLVM_FILECHECK.python_command()for the Python interpreter selected by bootstrap, fromPYTHON.clang()for tests that explicitly require clang-based testing, fromCLANG.cargo()for in-tree cargo, fromCARGO; this is only available in therun-make-cargoandbuild-stdsuites, not in plainrun-make.htmldocck()for the in-treesrc/etc/htmldocck.pyscript, invoked throughpython_command().
Some of these variables are only set when the configured builder can provide the
corresponding tool. Tests that require optional tools or LLVM target components
should declare the matching compiletest directive, such as
//@ needs-force-clang-based-tests or //@ needs-llvm-components: x86, instead
of failing later in the recipe. If a test needs a tool that is not represented by
run_make_support::external_deps, add a small helper there rather than open
coding the lookup in individual tests.