Skip to content

Commit 1868143

Browse files
committed
Auto merge of #160114 - windsornguyen:fix/musl-lto-weak-gettid, r=<try>
Avoid weak-linking `gettid` on musl try-job: test-various
2 parents c9ff496 + 74fb8fb commit 1868143

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

library/std/src/sys/thread/unix.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ pub fn current_os_id() -> Option<u64> {
335335
// The OS thread ID is used rather than `pthread_self` so as to match what will be displayed
336336
// for process inspection (debuggers, trace, `top`, etc.).
337337
cfg_select! {
338+
all(target_os = "linux", target_env = "musl") => {
339+
// Under fat LTO, weak-linking `gettid` can turn strong declarations weak.
340+
// musl provides `gettid`, so call it directly. See #154439.
341+
// SAFETY: FFI call with no preconditions.
342+
let id: libc::pid_t = unsafe { libc::gettid() };
343+
Some(id as u64)
344+
}
338345
// Most platforms have a function returning a `pid_t` or int, which is an `i32`.
339346
any(target_os = "android", target_os = "linux") => {
340347
use crate::sys::pal::weak::syscall;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This crate must sort after `std` to reproduce #154439.
2+
3+
//@ no-prefer-dynamic
4+
//@ compile-flags: -Copt-level=3
5+
6+
#![crate_type = "rlib"]
7+
8+
unsafe extern "C" {
9+
safe fn gettid() -> i32;
10+
}
11+
12+
pub fn tid() -> i32 {
13+
gettid()
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Regression test for std weakening a strong `gettid` reference under fat LTO (#154439).
2+
3+
//@ run-pass
4+
//@ only-linux
5+
//@ aux-build: strong-gettid-ref.rs
6+
//@ no-prefer-dynamic
7+
//@ compile-flags: -Copt-level=3 -Clto=fat -Ctarget-feature=+crt-static
8+
9+
extern crate strong_gettid_ref;
10+
11+
fn main() {
12+
// The main thread's TID equals its PID.
13+
assert_eq!(strong_gettid_ref::tid() as u32, std::process::id());
14+
}

0 commit comments

Comments
 (0)