rust-lang / rust-lang/rust

`#[thread_local]` alignment broken on Windows, GNU ABI

Open
#135,719 19 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-thread-locals C-bug F-thread_local I-unsound O-windows-gnu requires-nightly T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(thread_local)]

#[repr(align(256))]
struct HighAlignment(u8);

#[thread_local]
static EXAMPLE1: HighAlignment = const { HighAlignment(0) };
#[thread_local]
static EXAMPLE2: HighAlignment = const { HighAlignment(0) };
#[thread_local]
static EXAMPLE3: HighAlignment = const { HighAlignment(0) };

fn main() {
    println!(
        "{:?}, {:?}, {:?}",
        &EXAMPLE1 as *const HighAlignment,
        &EXAMPLE2 as *const HighAlignment,
        &EXAMPLE3 as *const HighAlignment
    );
}

I expected to see this happen: three 0x100-aligned pointers.

Instead, this happened: metal pipe falling

  • x86_64, GNU ABI, real Windows: 0x1f39e953850, 0x1f39e953950, 0x1f39e953a50 (unaligned)
  • x86_64, MSVC ABI, real Windows: 0x1c287893900, 0x1c287893a00, 0x1c287893b00 (aligned)
  • i686, GNU ABI, real Windows: 0xf9a5f8, 0xf9a6f8, 0xf9a7f8 (unaligned)
  • i686, MSVC ABI, real Windows: 0x6d2f00, 0x6d3000, 0x6d3100 (aligned)
  • x86_64, GNU ABI, Wine: 0x232c80, 0x232d80, 0x232e80 (unaligned)
  • x86_64, MSVC ABI, Wine: 0x1320e0, 0x1321e0, 0x1322e0 (unaligned)
  • i686, GNU ABI, Wine: 0x341f28, 0x342028, 0x342128 (unaligned)
  • i686, MSVC ABI, Wine: 0x34f010, 0x34f110, 0x34f210 (unaligned)

So either this affects all of Windows and I just got lucky twice, or this is two bugs in a trenchcoat, one affecting GNU ABI and another one in Wine. Not sure which.

Notably, checking mixed-alignment thread locals, like u8, u8, u128, u128, u128, shows that the variables are correctly aligned with respect to each other within TLS, but the TLS base itself seems to be unaligned.

Tentatively marking as I-unsound because of unaligned references bad, feel free to retag.

Meta

rustc --version --verbose:

rustc 1.86.0-nightly (419b3e2d3 2025-01-15)
binary: rustc
commit-hash: 419b3e2d3e350822550eee0e82eeded4d324d584
commit-date: 2025-01-15
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6

wine --version: wine-9.22 (Staging)

Tracking issue for visibility: #29594

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied #[thread_local] reproducer and compare its pointer alignment across the listed GNU and MSVC ABI, Windows, and Wine configurations. Trace the Windows TLS alignment behavior implicated by the results; done means the three thread-local pointers satisfy the requested 0x100 alignment in the affected configurations, with regression coverage for the observed cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.