rust-lang / rust-lang/rust

LTO turns weak linkage into a miscompile (identified via `gettid` segfault on x86_64-unknown-linux-musl)

Open
#154,439 27 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-linkage A-LLVM A-LTO C-bug F-linkage I-prioritize regression-from-stable-to-stable WG-llvm
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

View all comments

This is a very strange one. Since 289fe36d373 (I bisected; cc @tgross35), my program that calls libc's gettid segfaults when built for x86_64-unknown-linux-musl. However, I can only reproduce this when the call to gettid is in a library crate, and only when it has a certain name, and only when not using -C target-feature=-crt-static, only when using LTO.

Code
dep.rs
unsafe extern "C" {
    safe fn gettid() -> i32;
}

pub fn print_tid() {
    println!("{}", gettid());
}
main.rs
fn main() {
    vmm::print_tid();
}
Reproduction commands
rustc --crate-name vmm --edition=2024 dep.rs --crate-type lib -C opt-level=3 -C linker-plugin-lto --target x86_64-unknown-linux-musl -C linker=/nix/store/nz3n5wy8zadq73kkql2xn485l7xdmv46-x86_64-unknown-linux-musl-gcc-wrapper-15.2.0/bin/x86_64-unknown-linux-musl-cc
rustc --edition=2024 main.rs --crate-type bin -C opt-level=3 -C lto --target x86_64-unknown-linux-musl -C linker=/nix/store/nz3n5wy8zadq73kkql2xn485l7xdmv46-x86_64-unknown-linux-musl-gcc-wrapper-15.2.0/bin/x86_64-unknown-linux-musl-cc --extern vmm=libvmm.rlib
./main

Happy to provide LLVM IR or anything else that helps!

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

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 by running the supplied dep.rs and main.rs reproduction commands for x86_64-unknown-linux-musl, comparing LTO with and without -C target-feature=-crt-static. Inspect regression commit 289fe36d373 and the generated LLVM IR around the gettid call and weak linkage. Done means the reproducer no longer segfaults under LTO and the regression is covered by a compiler test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.