Skip to content

Incorrect behavior on Windows with dylibs and ThinLTO #127979

Closed

Description

@janhohenheim

edit by @Noratrieb: MCVE: https://github.com/bash/rust-dylib-crash

I tried this code:
Clone https://github.com/TheBevyFlock/bevy_quickstart and run cargo run --release on Windows

I expected to see this happen: Literally anything else than the standard library hitting unreachable code.

Instead, this happened: We hit this line of unreachable code: https://github.com/rust-lang/rust/blob/22a5267c83a3e17f2b763279eb24bb632c45dc6b/library/std/src/sys/sync/once/futex.rs

Meta

rustc --version --verbose:

rustc 1.81.0-nightly (5affbb171 2024-07-18)
binary: rustc
commit-hash: 5affbb17153bc69a9d5d8d2faa4e399a014a211e
commit-date: 2024-07-18
host: x86_64-pc-windows-msvc
release: 1.81.0-nightly
LLVM version: 18.1.7

image

Backtrace

thread 'main' panicked at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e\library\std\src\sync\once.rs:217:20:
internal error: entered unreachable code: state is never set to invalid values
stack backtrace:
   0:     0x7ffae5f03e3d - std::backtrace_rs::backtrace::dbghelp64::trace
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
   1:     0x7ffae5f03e3d - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ffae5f03e3d - std::sys::backtrace::_print_fmt
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\sys\backtrace.rs:65
   3:     0x7ffae5f03e3d - std::sys::backtrace::impl$0::print::impl$0::fmt
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\sys\backtrace.rs:40
   4:     0x7ffae5f34639 - core::fmt::rt::Argument::fmt
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\core\src\fmt\rt.rs:173
   5:     0x7ffae5f34639 - core::fmt::write
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\core\src\fmt\mod.rs:1182
   6:     0x7ffae5efa941 - std::io::Write::write_fmt<std::sys::pal::windows::stdio::Stderr>
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\io\mod.rs:1827
   7:     0x7ffae5f06ed7 - std::panicking::default_hook::closure$1
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:269
   8:     0x7ffae5f06ac9 - std::panicking::default_hook
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:296
   9:     0x7ffae5f076e2 - std::panicking::rust_panic_with_hook
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:800
  10:     0x7ffae5f074ef - std::panicking::begin_panic_handler::closure$0
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:667
  11:     0x7ffae5f047af - std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\sys\backtrace.rs:168
  12:     0x7ffae5f07106 - std::panicking::begin_panic_handler
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:665
  13:     0x7ffae5f5d6b4 - core::panicking::panic_fmt
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\core\src\panicking.rs:74
  14:     0x7ff7dfc60e01 - main
  15:     0x7ff7dfc5e391 - main
  16:     0x7ff7dfc5cc95 - main
  17:     0x7ffade9fe3e2 - bevy_app::app::App::add_boxed_plugin::he4742dca059e0ec1
  18:     0x7ff7dfc42aff - main
  19:     0x7ff7dfc4104f - main
  20:     0x7ff7dfc4103e - main
  21:     0x7ffae5eeb969 - std::rt::lang_start_internal::closure$2
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\rt.rs:141
  22:     0x7ffae5eeb969 - std::panicking::try::do_call
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:557
  23:     0x7ffae5eeb969 - std::panicking::try
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:521
  24:     0x7ffae5eeb969 - std::panic::catch_unwind
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panic.rs:350
  25:     0x7ffae5eeb969 - std::rt::lang_start_internal
                               at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\rt.rs:141
  26:     0x7ff7dfc42cec - main
  27:     0x7ff7dfcd5ba0 - invoke_main
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  28:     0x7ff7dfcd5ba0 - __scrt_common_main_seh
                               at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  29:     0x7ffc426a257d - BaseThreadInitThunk
  30:     0x7ffc4366af28 - RtlUserThreadStart
error: process didn't exit successfully: `target\release\bevy_quickstart.exe` (exit code: 101)

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 19, 2024
  2. workingjubilee commented on Jul 19, 2024

    @workingjubilee
    Member

    @janhohenheim Can you bisect this to a specific commit?

    https://github.com/rust-lang/cargo-bisect-rustc

  3. added
    O-windowsOperating system: Windows
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Jul 19, 2024
  4. workingjubilee commented on Jul 19, 2024

    @workingjubilee
    Member

    Also, can you specify your Windows version? Just in case.

  5. janhohenheim commented on Jul 19, 2024

    @janhohenheim
    ContributorAuthor

    @workingjubilee sure, will do later. First, I'm trying to narrow the example a bit down.

  6. tgross35 commented on Jul 19, 2024

    @tgross35
    Member

    For future reference, permalink at that version https://github.com/rust-lang/rust/blob/22a5267c83a3e17f2b763279eb24bb632c45dc6b/library/std/src/sys/sync/once/futex.rs

    You should try the nightly too, @ChrisDenton recently did some changes there that may have happened to fix things

  7. janhohenheim commented on Jul 19, 2024

    @janhohenheim
    ContributorAuthor

    @tgross35 thanks for the hint, updated the description :)
    Nightly is hitting the same thing

  8. janhohenheim commented on Jul 19, 2024

    @janhohenheim
    ContributorAuthor

    Alright, added my Windows system information and linked to a smaller example. Will run cargo-bisect now.

  9. tgross35 commented on Jul 19, 2024

    @tgross35
  10. janhohenheim commented on Jul 19, 2024

    @janhohenheim
    ContributorAuthor

    @tgross35 oh sorry, I have "simplified" the example too much. That's my bad. Can you try with https://github.com/TheBevyFlock/bevy_quickstart ? Just running cargo run --release should produce the described result.

  11. tgross35 commented on Jul 19, 2024

    @tgross35
    Member

    Ah, yeah reproduced with the quickstart

  12. tgross35 commented on Jul 19, 2024

    @tgross35
    Member

    Are you running a bisect, or still trying to minimize? Unfortunate that this takes almost 3 minutes to compile

  13. tgross35 commented on Jul 19, 2024

    @tgross35
    Member

    Relevant bit with RUST_BACKTRACE=full

      11:     0x7ffa909b47af - std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                                   at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\sys\backtrace.rs:168
      12:     0x7ffa909b7106 - std::panicking::begin_panic_handler
                                   at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:665
      13:     0x7ffa90a0d6b4 - core::panicking::panic_fmt
                                   at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\core\src\panicking.rs:74
      14:     0x7ff6d1364176 - _guard_xfg_dispatch_icall_nop
      15:     0x7ff6d1366803 - _guard_xfg_dispatch_icall_nop
      16:     0x7ff6d133c711 - <bevy_quickstart::game::assets::HandleMap<bevy_quickstart::game::assets::ImageKey> as bevy_ecs::world::FromWorld>::from_world::h409ade5f5f444d1b
      17:     0x7ff6d134fd90 - <bevy_quickstart::AppPlugin as bevy_app::plugin::Plugin>::build::h2e1d5ae2b1f18546
      18:     0x7ffa44ad6b47 - bevy_app::app::App::add_boxed_plugin::h671372d46ee73030
      19:     0x7ff6d135f121 - <bevy_quickstart::AppSet as bevy_ecs::schedule::set::SystemSet>::dyn_hash::h07f7da5483ff379d
      20:     0x7ff6d135e4fa - <bevy_quickstart::AppSet as bevy_ecs::schedule::set::SystemSet>::dyn_hash::h07f7da5483ff379d
      21:     0x7ff6d135e4e9 - <bevy_quickstart::AppSet as bevy_ecs::schedule::set::SystemSet>::dyn_hash::h07f7da5483ff379d
      22:     0x7ffa9099b969 - std::rt::lang_start_internal::closure$2
                                   at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\rt.rs:141
      23:     0x7ffa9099b969 - std::panicking::try::do_call
                                   at /rustc/5affbb17153bc69a9d5d8d2faa4e399a014a211e/library\std\src\panicking.rs:557
    
  14. 45 remaining items

  15. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 20, 2024
  16. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 20, 2024
  17. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 20, 2024
  18. removed
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Jul 20, 2024
  19. apiraino commented on Jul 22, 2024

    @apiraino
    Contributor

    WG-prioritization assigning priority (Zulip discussion).

    @rustbot label -I-prioritize +P-high

  20. added
    P-highHigh priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 22, 2024
  21. Zoxc commented on Aug 12, 2024

    @Zoxc
    Contributor

    #122790 fixes @bash's minimized example by reverting #103353.

  22. Zoxc commented on Aug 15, 2024

    @Zoxc
    Contributor

    I see that Cargo was incorrectly passing -C linker-plugin-lto when using ThinLTO. That doesn't work with dylibs and we apparently don't correctly check that no dylibs are involved. With that addressed and #122790 and #129079 applied the bevy-template repo works with both rust-lld and link.

  23. Noratrieb commented on May 25, 2025

    @Noratrieb
    Member

    @janhohenheim can you confirm that the issue is now fixed?

  24. changed the title [-] unreachable!("state is never set to invalid values") is reached[/-] [+]Incorrect behavior on Windows with dylibs and ThinLTO[/+] on May 25, 2025
  25. janhohenheim commented on May 26, 2025

    @janhohenheim
    ContributorAuthor

    @Noratrieb I can try over the next few days, but from a quick glance at the discussion, it certainly looks fixed :)

  26. janhohenheim commented on Jul 17, 2025

    @janhohenheim
    ContributorAuthor

    LGTM :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-windowsOperating system: WindowsP-highHigh priorityS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions