rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.75k stars 12.65k forks source link

Miri sometimes reports memory leak in thread-local storage on *-pc-windows-gnu #123583

Closed RalfJung closed 5 months ago

RalfJung commented 6 months ago

I've only seen this once, even though the test should be covered by miri-test-libstd every day:

  error: memory leaked: alloc26870878 (Rust heap, size: 8, align: 4), allocated here:
  Error:     --> /checkout/library/alloc/src/alloc.rs:100:9
       |
  100  |         __rust_alloc(layout.size(), layout.align())
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |
       = note: BACKTRACE:
       = note: inside `realstd::alloc::alloc` at /checkout/library/alloc/src/alloc.rs:100:9: 100:52
       = note: inside `realstd::alloc::Global::alloc_impl` at /checkout/library/alloc/src/alloc.rs:183:73: 183:86
       = note: inside `<realstd::alloc::Global as core::alloc::Allocator>::allocate` at /checkout/library/alloc/src/alloc.rs:243:9: 243:39
       = note: inside `alloc_crate::alloc::exchange_malloc` at /checkout/library/alloc/src/alloc.rs:332:11: 332:34
       = note: inside `realstd::boxed::Box::<realstd::sys::thread_local::os_local::Value<u8>>::new` at /checkout/library/alloc/src/boxed.rs:218:9: 218:20
       = note: inside `realstd::thread::local_impl::Key::<u8>::try_initialize::<{closure@/checkout/library/std/src/sys/thread_local/os_local.rs:28:27: 28:34}>` at /checkout/library/std/src/sys/thread_local/os_local.rs:145:37: 145:94
       = note: inside `realstd::thread::local_impl::Key::<u8>::get::<{closure@/checkout/library/std/src/sys/thread_local/os_local.rs:28:27: 28:34}>` at /checkout/library/std/src/sys/thread_local/os_local.rs:127:18: 127:43
  note: inside `sync::reentrant_lock::current_thread_unique_ptr::X::__getit`
      --> /checkout/library/std/src/sys/thread_local/os_local.rs:28:17
       |
  11   |   pub macro thread_local_inner {
       |   ----------------------------
       |   |
       |   in this expansion of `$crate::thread::local_impl::thread_local_inner!` (#2)
       |   in this expansion of `$crate::thread::local_impl::thread_local_inner!` (#3)
  ...
  28   | /                 __KEY.get(move || {
  29   | |                     if let $crate::option::Option::Some(init) = _init {
  30   | |                         if let $crate::option::Option::Some(value) = init.take() {
  31   | |                             return value;
  ...    |
  36   | |                     __init()
  37   | |                 })
       | |__________________^
  ...
  82   |               $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*);
       |               ------------------------------------------------------------------- in this macro invocation (#3)
       |
      ::: library/std/src/sync/reentrant_lock.rs:318:5
       |
  318  |       thread_local! { static X: u8 = const { 0 } }
       |       -------------------------------------------- in this macro invocation (#1)
       |
      ::: library/std/src/thread/local.rs:183:1
       |
  183  | / macro_rules! thread_local {
  184  | |     // empty (base case for the recursion)
  185  | |     () => {};
  ...    |
  193  | |         $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
       | |         ----------------------------------------------------------------------------------------- in this macro invocation (#2)
  ...    |
  205  | |     );
  206  | | }
       | |_- in this expansion of `thread_local!` (#1)
       = note: inside `realstd::thread::LocalKey::<u8>::try_with::<{closure@library/std/src/sync/reentrant_lock.rs:319:12: 319:15}, usize>` at /checkout/library/std/src/thread/local.rs:283:32: 283:50
       = note: inside `realstd::thread::LocalKey::<u8>::with::<{closure@library/std/src/sync/reentrant_lock.rs:319:12: 319:15}, usize>` at /checkout/library/std/src/thread/local.rs:260:9: 260:25
  note: inside `sync::reentrant_lock::current_thread_unique_ptr`
      --> library/std/src/sync/reentrant_lock.rs:319:5
       |
  319  |     X.with(|x| <*const _>::addr(x))
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  note: inside `sync::reentrant_lock::ReentrantLock::<()>::lock`
      --> library/std/src/sync/reentrant_lock.rs:184:27
       |
  184  |         let this_thread = current_thread_unique_ptr();
       |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  note: inside `sync::reentrant_lock::tests::smoke`
      --> library/std/src/sync/reentrant_lock/tests.rs:10:17
       |
  10   |         let a = l.lock();
       |                 ^^^^^^^^
  note: inside closure
      --> library/std/src/sync/reentrant_lock/tests.rs:7:11
       |
  6    | #[test]
       | ------- in this procedural macro expansion
  7    | fn smoke() {
       |           ^
       |
      ::: /checkout/library/core/src/macros/mod.rs:1636:5
       |
  1636 |     pub macro test($item:item) {
       |     -------------- in this expansion of `#[test]`

Cc @joboet @m-ou-se @Amanieu

RalfJung commented 6 months ago

Or maybe this is caused by Miri not properly running the on-thread-exit hook on Windows? The support for that is a bit fragile as Miri does not support these magic linker sections we rely on. Instead Miri hard-codes the path to this static and calls it. But with the test crate we may have that static twice (once in std and once in the test crate) and only one of them gets called...

However I would expect that this logic means that the thread_local impl from the real std is used, so the one from the test crate should not matter.

Maybe we should stop using this hack and implement support for this magic linker section instead. I just don't know if there's a good way to ask the compiler for "all static in a particular link_section across all crates".

RalfJung commented 6 months ago

I tried to figure out how TLS is currently implemented on Windows, but I got lost in the dark forest that is std::sys.^^ I can't figure out where this call (imp::create) would go on Windows; nothing in the Windows folder declares a create function.

EDIT: Oh, looks like sys_common::thread_local_key is Unix-only. I got fooled by the "common" in the name and it not being in pal.

ChrisDenton commented 6 months ago

The Windows specific bits happen here: https://github.com/rust-lang/rust/blob/e43aef0ef9cc9d2d12c138b36fd817f7c41d0152/library/std/src/sys/pal/windows/thread_local_key.rs

Note though that the StaticKey (and related) bits aren't actually used if #[cfg(target_thread_local)].

RalfJung commented 6 months ago

The bug was reported on i686-pc-windows-gnu. Not sure if that's currently target_thread_local or not... I think it is? Only the 32bit MSVC target is not? But I keep forgetting which one exactly it is so I may be wrong.

RalfJung commented 6 months ago

However I think even with target_thread_local, dtors are handled the same way -- via thread::local_impl::Key::<T>::register_dtor.

ChrisDenton commented 6 months ago

32-bit msvc is currently not target_thread_local. However, that may change soon.

register_keyless_dtor is used when target_thread_local is enabled, otherwise register_dtor is used. Both ultimately use the special .CRT$XLB linker magic.

RalfJung commented 6 months ago

Ah, register_dtor is actually register_keyless_dtor.

pub use super::thread_local_key::register_keyless_dtor as register_dtor;

This is such a maze...^^

ChrisDenton commented 6 months ago

You're not the first to notice that: https://github.com/rust-lang/rust/issues/110897

mati865 commented 6 months ago

The bug was reported on i686-pc-windows-gnu. Not sure if that's currently target_thread_local or not... I think it is? Only the 32bit MSVC target is not? But I keep forgetting which one exactly it is so I may be wrong.

All tier 1 windows-gnu targets have target_thread_local disabled. There are similar tier 2 targets windows-gnullvm which have working TLS and have target_thread_local enabled.

RalfJung commented 6 months ago

Okay, thanks for clarifying.

Is there an issue tracking why the windows-gnu targets can't use target_thread_local?

mati865 commented 6 months ago

Some of binaries produced by tests inside ui/ crash with some code that I can'it recall 😉 That could be discrepancy between our default LLVM backend which uses Windows native TLS and GNU tools which use emuTLS but I didn't dedicated enough time to it and I'm not planning currently.

joboet commented 6 months ago

I tried enabling native TLS in #102243, the result being access violations when running rustc. Things may have changed since then, though.

mati865 commented 6 months ago

I've tried it multiple times over the years and the result was always the same, this time it could work if emutls is enabled as well: https://github.com/rust-lang/rust/pull/117873 but I don't have high hopes here and I think it'd still require some fixes.

EDIT: As I anticipated it needs more work:

Details ``` $ ./x.py t tests/ui ... failures: ---- [ui] tests\ui\extern\issue-64655-extern-rust-must-allow-unwind.rs#fat0 stdout ---- error in revision `fat0`: test compilation failed although it shouldn't! status: exit code: 1 command: PATH="H:\projects\rust\build\x86_64-pc-windows-gnu\stage1\bin;H:\projects\rust\build\x86_64-pc-windows-gnu\stage0-bootstrap-tools\x86_64-pc-windows-gnu\release\deps;H:\projects\rust\build\x86_64-pc-windows-gnu\stage0\bin;C:\Users\mateusz\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\mateusz\bin;C:\Program Files\Alacritty;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet;C:\Program Files\NVIDIA Corporation\Nsight Compute 2023.1.1;C:\Program Files\WezTerm;C:\Program Files\Intel\PresentMon\PresentMonApplication;C:\Program Files\NVIDIA Corporation\NVIDIA App\NvDLISR;C:\Program Files\Git\cmd;C:\Users\mateusz\.cargo\bin;C:\Users\mateusz\AppData\Local\Microsoft\WindowsApps;C:\Users\mateusz\AppData\Local\JetBrains\Toolbox\scripts;C:\Users\mateusz\AppData\Local\GitHubDesktop\bin;C:\Users\mateusz\AppData\Local\Programs\Microsoft VS Code\bin;D:\msys64\mingw64\bin;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\bin\\rustc.exe" "H:\\projects\\rust\\tests\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=C:\\Users\\mateusz\\.cargo" "--sysroot" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1" "--target=x86_64-pc-windows-gnu" "--cfg" "fat0" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-o" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.fat0\\a.exe" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\native\\rust-test-helpers" "-Clinker=d:/msys64/mingw64/bin/gcc.exe" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.fat0\\auxiliary" "-C" "opt-level=0" "-C" "lto=fat" stdout: none --- stderr ------------------------------- error: linking with `d:/msys64/mingw64/bin/gcc.exe` failed: exit code: 1 | = note: "d:/msys64/mingw64/bin/gcc.exe" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\mateusz\\AppData\\Local\\Temp\\rustceEqcfE\\symbols.o" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.fat0\\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\native\\rust-test-helpers" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.fat0\\auxiliary" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\mateusz\\AppData\\Local\\Temp\\rustceEqcfE\\libstd-418d4ec99f519a29.rlib" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-4d92a5d2ada47422.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-lsynchronization" "-lkernel32" "-lws2_32" "-lkernel32" "-lntdll" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-lmingwex" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-o" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\extern\\issue-64655-extern-rust-must-allow-unwind.fat0\\a.exe" "-Wl,--gc-sections" "-no-pie" "-Wl,--strip-debug" "-nodefaultlibs" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o" = note: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0x718): undefined reference to `std::sys_common::thread_info::THREAD_INFO::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0x720): undefined reference to `std::sys_common::thread_info::THREAD_INFO::__getit::STATE' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0x728): undefined reference to `std::sync::reentrant_lock::current_thread_unique_ptr::X::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0x730): undefined reference to `std::panicking::panic_count::LOCAL_PANIC_COUNT::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0xa28): undefined reference to `std::io::stdio::OUTPUT_CAPTURE::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0xb30): undefined reference to `std::sys::pal::windows::thread_local_key::DESTRUCTORS' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0xc28): undefined reference to `std::hash::random::RandomState::new::KEYS::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0xc30): undefined reference to `std::sync::mpmc::context::Context::with::CONTEXT::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\extern\issue-64655-extern-rust-must-allow-unwind.fat0\a.issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0.rcgu.o:issue_64655_extern_rust_must_allow_unwind.56e668df17fe0d16-cgu.0:(.data+0xdb0): undefined reference to `std::sync::mpmc::waker::current_thread_id::DUMMY::__getit::__KEY' collect2.exe: error: ld returned 1 exit status = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified = note: use the `-l` flag to specify native libraries to link error: aborting due to 1 previous error ------------------------------------------ ---- [ui] tests\ui\issues\issue-44056.rs stdout ---- error: test compilation failed although it shouldn't! status: exit code: 1 command: PATH="H:\projects\rust\build\x86_64-pc-windows-gnu\stage1\bin;H:\projects\rust\build\x86_64-pc-windows-gnu\stage0-bootstrap-tools\x86_64-pc-windows-gnu\release\deps;H:\projects\rust\build\x86_64-pc-windows-gnu\stage0\bin;C:\Users\mateusz\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\mateusz\bin;C:\Program Files\Alacritty;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet;C:\Program Files\NVIDIA Corporation\Nsight Compute 2023.1.1;C:\Program Files\WezTerm;C:\Program Files\Intel\PresentMon\PresentMonApplication;C:\Program Files\NVIDIA Corporation\NVIDIA App\NvDLISR;C:\Program Files\Git\cmd;C:\Users\mateusz\.cargo\bin;C:\Users\mateusz\AppData\Local\Microsoft\WindowsApps;C:\Users\mateusz\AppData\Local\JetBrains\Toolbox\scripts;C:\Users\mateusz\AppData\Local\GitHubDesktop\bin;C:\Users\mateusz\AppData\Local\Programs\Microsoft VS Code\bin;D:\msys64\mingw64\bin;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\bin\\rustc.exe" "H:\\projects\\rust\\tests\\ui\\issues\\issue-44056.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=C:\\Users\\mateusz\\.cargo" "--sysroot" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1" "--target=x86_64-pc-windows-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--out-dir" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\issues\\issue-44056" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\native\\rust-test-helpers" "-Clinker=d:/msys64/mingw64/bin/gcc.exe" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\issues\\issue-44056\\auxiliary" "-Ctarget-feature=+avx" "-Clto" stdout: none --- stderr ------------------------------- error: linking with `d:/msys64/mingw64/bin/gcc.exe` failed: exit code: 1 | = note: "d:/msys64/mingw64/bin/gcc.exe" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\mateusz\\AppData\\Local\\Temp\\rustcz2km9U\\symbols.o" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\issues\\issue-44056\\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\native\\rust-test-helpers" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\issues\\issue-44056\\auxiliary" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\mateusz\\AppData\\Local\\Temp\\rustcz2km9U\\libstd-418d4ec99f519a29.rlib" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-4d92a5d2ada47422.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-lsynchronization" "-lkernel32" "-lws2_32" "-lkernel32" "-lntdll" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-lmingwex" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-o" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\test\\ui\\issues\\issue-44056\\issue-44056.exe" "-Wl,--gc-sections" "-no-pie" "-Wl,--strip-debug" "-nodefaultlibs" "H:\\projects\\rust\\build\\x86_64-pc-windows-gnu\\stage1\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o" = note: D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0x718): undefined reference to `std::sys_common::thread_info::THREAD_INFO::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0x720): undefined reference to `std::sys_common::thread_info::THREAD_INFO::__getit::STATE' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0x728): undefined reference to `std::sync::reentrant_lock::current_thread_unique_ptr::X::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0x730): undefined reference to `std::panicking::panic_count::LOCAL_PANIC_COUNT::__getit::VAL' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0xa28): undefined reference to `std::io::stdio::OUTPUT_CAPTURE::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0xb30): undefined reference to `std::sys::pal::windows::thread_local_key::DESTRUCTORS' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0xc28): undefined reference to `std::hash::random::RandomState::new::KEYS::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0xc30): undefined reference to `std::sync::mpmc::context::Context::with::CONTEXT::__getit::__KEY' D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: H:\projects\rust\build\x86_64-pc-windows-gnu\test\ui\issues\issue-44056\issue-44056.issue_44056.fdfc3584e7394587-cgu.0.rcgu.o:issue_44056.fdfc3584e7394587-cgu.0:(.data+0xdb0): undefined reference to `std::sync::mpmc::waker::current_thread_id::DUMMY::__getit::__KEY' collect2.exe: error: ld returned 1 exit status = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified = note: use the `-l` flag to specify native libraries to link error: aborting due to 1 previous error ------------------------------------------ failures: [ui] tests\ui\extern\issue-64655-extern-rust-must-allow-unwind.rs#fat0 [ui] tests\ui\issues\issue-44056.rs ``` ```diff $ git diff diff --git a/compiler/rustc_target/src/spec/base/windows_gnu.rs b/compiler/rustc_target/src/spec/base/windows_gnu.rs index 25f02dc1451..3b2d174071a 100644 --- a/compiler/rustc_target/src/spec/base/windows_gnu.rs +++ b/compiler/rustc_target/src/spec/base/windows_gnu.rs @@ -1,4 +1,4 @@ -use crate::spec::LinkSelfContainedDefault; +use crate::spec::{LinkSelfContainedDefault, TlsModel}; use crate::spec::{add_link_args, crt_objects}; use crate::spec::{cvs, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions}; use std::borrow::Cow; @@ -40,6 +40,7 @@ pub fn opts() -> TargetOptions { // // See https://github.com/rust-lang/rust/pull/47483 for some more details. "-lmsvcrt", + "-lmingwex", // This one is required for newer mingw-w64 versions "-luser32", "-lkernel32", ]; @@ -103,6 +104,8 @@ pub fn opts() -> TargetOptions { // output DWO, despite using DWARF, doesn't use ELF.. debuginfo_kind: DebuginfoKind::Pdb, supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]), + has_thread_local: true, + tls_model: TlsModel::Emulated, ..Default::default() } } ```
matthiaskrgr commented 5 months ago

Seems to randomly happen in CI as well https://rust-lang.zulipchat.com/#narrow/stream/269128-miri/topic/memleak https://github.com/rust-lang/rust/pull/124248#issuecomment-2068731334

RalfJung commented 5 months ago

So, it was not some issue with the global destructor after all. And it seems to always hit only the reentrant lock, even though that is not the only thread-local we have.

Maybe there's an actual race condition leading to occasional memory leaks here?

RalfJung commented 5 months ago

Minimal reproducer so far:

use std::thread;

pub(crate) fn current_thread_unique_ptr() {
    thread_local! { static X: u8 = const { 0 } }
    X.with(|_x| {})
}

fn main() {
    let j2 = thread::spawn(current_thread_unique_ptr);
    current_thread_unique_ptr();
    j2.join().unwrap();
}

The leak only occurs on the windows-gnu targets.

ChrisDenton commented 5 months ago

Presumably this is due to the TLS dtor when not has_thread_local.

All the msvc targets currently use the #[cfg(target_thread_local)] code.

RalfJung commented 5 months ago

Meanwhile, https://github.com/rust-lang/rust/pull/124270 should work around the CI issue.

RalfJung commented 5 months ago

FWIW the issue reproduces with and without const { ... }.

RalfJung commented 5 months ago

Hm, that's very strange... supposedly there's Box::new being called here to create a Box with content std::sys::thread_local::os_local::Value<u8>:

https://github.com/rust-lang/rust/blob/45ca53f9d867087fdf8fa7371b9f4f8b38a01a41/library/std/src/sys/thread_local/os_local.rs#L62-L71

... there's no call to Box there that I can see?!?

The span specifically points at the move ||.

RalfJung commented 5 months ago

Oh never mind, I copied the span from the closure not the stacktrace.^^

It's this Box:

https://github.com/rust-lang/rust/blob/45ca53f9d867087fdf8fa7371b9f4f8b38a01a41/library/std/src/sys/thread_local/os_local.rs#L145

RalfJung commented 5 months ago

One key ingredient to the leak seems to be not loading the latest value here:

https://github.com/rust-lang/rust/blob/5934aaaa973349c0ff6050e88d6a66a0fe196dda/library/std/src/sys/pal/windows/thread_local_key.rs#L314

Cc @joboet

RalfJung commented 5 months ago

Ah, I think I got it. :) https://github.com/rust-lang/rust/pull/124281

matthiaskrgr commented 5 months ago

🏅