Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust + MSVC + LLD = Segfault on attempt to access TLS #76398

Closed
petrochenkov opened this issue Sep 6, 2020 · 4 comments
Closed

Rust + MSVC + LLD = Segfault on attempt to access TLS #76398

petrochenkov opened this issue Sep 6, 2020 · 4 comments
Labels
A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows

Comments

@petrochenkov
Copy link
Contributor

petrochenkov commented Sep 6, 2020

How to reproduce:

"<build>\x86_64-pc-windows-msvc\stage1\bin\rustc.exe" -Clinker=lld hello.rs

where hello.rs is

// The important thing here is accessing thread-local `static LOCAL_STDERR` in libstd.
fn main() {
    eprint!("Hello");
}
  • rustc must be built locally, the issue doesn't reproduce on nightly rustc, I don't know due to which differences.
  • lld can be either rust-lld shipped with rustc, or lld from a msys2 package, the issue reproduces on both, and it doesn't happen without lld.
  • the produced executable must be run using a path, not just an executable name, an absolute path like "C:\msys64\home\me\hello.exe" works for sure. UPD: The program invocation string should just be long enough, not necessarily a path.
  • libstd must be build with opt-level = 3 (the default), lowering the opt level to improve debugging removes the issue :( but full debuginfo can still be enabled.
  • The exception and backtrace look like this - rustbuild: Remove one LLD workaround #76127 (comment)

I'll try to step through disassembly and look what happens in more detail tomorrow.

@petrochenkov
Copy link
Contributor Author

petrochenkov commented Sep 6, 2020

I would be great if someone else could reproduce this issue, to make sure it's not caused by something specific to my system. (The issue not being reproducible on nightly rustc is the most suspicious part here.)

My config.toml doesn't seem to contain anything unusual.

[llvm]
ninja = true
targets = "X86"
experimental-targets = ""

[build]
build = "x86_64-pc-windows-msvc"

[rust]
debuginfo-level = 1
debuginfo-level-std = 2

@petrochenkov petrochenkov added A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Sep 6, 2020
@petrochenkov
Copy link
Contributor Author

Hmm, the cause of the crash seems to be vmovaps with an insufficiently aligned operand.

Exception thrown at 0x00007FF78DAA3474 in 777.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
00007FF78DAA3474 C5 FC 29 88 40 00 00 00 vmovaps     ymmword ptr [rax+40h],ymm1
rax+40h	0x0000009d751b2bd0	unsigned __int64
(rax+40h) % 32	0x0000000000000010	unsigned __int64 <-- vmovaps requires 32-bit operand alignment

"Access violation reading location 0xFFFFFFFFFFFFFFFF" in this case is an extremely misleading exception message, because the instruction not only doesn't access that address, it doesn't even read memory.

@petrochenkov
Copy link
Contributor Author

rustc must be built locally, the issue doesn't reproduce on nightly rustc, I don't know due to which differences.

I forgot about this, but I actually have -Ctarget-cpu=native set globally. And vmovaps is a relatively new instruction. Perhaps that's the difference, need to check.

@petrochenkov
Copy link
Contributor Author

So, -Ctarget-cpu=native is indeed the reason and this is a duplicate of #72145.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-thread-locals Area: Thread local storage (TLS) C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows
Projects
None yet
Development

No branches or pull requests

1 participant