CFI: std::thread::spawn fails the CFI check on linux-gnu (__pthread_get_minstack called through dlsym)

Abierto
#166,068 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
43/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
linux, rust

Línea de trabajo

Start with library/std/src/sys/thread/unix.rs, especially min_stack_size near line 1076, where the backtrace shows the failing indirect call. Read how it resolves __pthread_get_minstack through dlsym and compare the reported function type with glibc's definition. Reproduce with the minimal std::thread::spawn example and the supplied CFI flags; done means the Linux GNU build runs without a CFI failure while preserving the musl behavior.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A-ABI A-control-flow-integrity A-thread C-bug O-linux-gnu requires-nightly T-libs

Discovered while building ClickHouse with CFI, which uses glibc (for now, we are in the process of switching to musl).

I tried the following project

src/main.rs:

fn main() {
    let thread = std::thread::spawn(|| 1);
    println!("{}", thread.join().unwrap());
}

Cargo.toml:

[package]
name = "minstack_cfi"
version = "0.1.0"
edition = "2021"

[profile.dev]
lto = "fat"
codegen-units = 1

Command:

RUSTFLAGS="-Clto -Ccodegen-units=1 -Zsanitizer=cfi" \
    cargo +nightly run -Zbuild-std --target x86_64-unknown-linux-gnu

I expected to see just 1, instead I get a SIGILL from CFI. With -Zsanitizer-cfi-diag -Zsanitizer-cfi-recover it reports:

library/std/src/lib.rs:1:1: runtime error: control flow integrity check for type fn(*const libc::pthread_attr_t) -> usize failed during indirect function call
nptl/nptl-stack.c:162:1: note: __pthread_get_minstack defined here
library/std/src/lib.rs:1:1: note: check failed in .../minstack_cfi, destination function located in /usr/lib/libc.so.6

If I build with musl it works fine:

$ RUSTFLAGS="-Clto -Ccodegen-units=1 -Zsanitizer=cfi -Ctarget-feature=-crt-static \
    -Clink-self-contained=yes -Clink-arg=-static -Crelocation-model=static \
    -Clinker=clang -Clink-arg=-fuse-ld=lld"     cargo +nightly run -Zbuild-std --target x86_64-unknown-linux-musl
   Compiling minstack_cfi v0.1.0 (/mnt/ch/ClickHouse/tmp/minstack_cfi)
warning: linker stderr: clang: argument unused during compilation: '-no-pie' [-Wunused-command-line-argument]
 |
 = note: `#[warn(linker_messages)]` on by default

warning: `minstack_cfi` (bin "minstack_cfi") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.18s
     Running `target/x86_64-unknown-linux-musl/debug/minstack_cfi`
1
Meta

rustc --version --verbose:

$ rustc +nightly --version --verbose
rustc 1.102.0-nightly (5f4effd57 2026-11-13)
binary: rustc
commit-hash: 5f4effd579500175af844241eb179e756190f1e7
commit-date: 2026-11-13
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.5

The lookup has been there since 2015, years before Rust had CFI so this is not a new issue.

Backtrace

(gdb) bt
#0  0x0000561956333632 in std::sys::thread::unix::min_stack_size (attr=0x7fffafd94f00) at src/sys/thread/unix.rs:1076
#1  0x00005619562eadf8 in std::sys::thread::unix::Thread::new (stack=2097152, init=0x56198b13bde0) at src/sys/thread/unix.rs:78
#2  0x0000561956364902 in std::thread::lifecycle::spawn_unchecked<minstack_cfi::main::{closure_env#0}, i32> (name=..., stack_size=..., no_hooks=false, scope_data=..., f=...)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/lifecycle.rs:116
#3  0x000056195636417a in std::thread::builder::Builder::spawn_unchecked<minstack_cfi::main::{closure_env#0}, i32> (self=..., f=...)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/builder.rs:261
#4  0x000056195636403a in std::thread::builder::Builder::spawn<minstack_cfi::main::{closure_env#0}, i32> (self=..., f=...)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/builder.rs:191
#5  0x00005619563628a6 in std::thread::functions::spawn<minstack_cfi::main::{closure_env#0}, i32> (f=...)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/functions.rs:131
#6  0x000056195636278f in minstack_cfi::main () at src/main.rs:2
#7  0x000056195636276f in core::ops::function::FnOnce::call_once<fn(), ()> ()
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250
#8  0x000056195636270e in std::sys::backtrace::__rust_begin_short_backtrace<fn(), ()> (f=0x561956362778 <__ubsan_check_cfi_icall_jt>)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:166
#9  0x00005619563626ed in std::rt::lang_start::{closure#0}<()> () at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:206
#10 0x000056195633ff92 in core::ops::function::impls::{impl#2}::call_once<(), (dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)>
    (self=..., args=()) at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:287
#11 0x00005619562c4b73 in std::panicking::catch_unwind::do_call<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> (
    data=0x7fffafd95660) at src/panicking.rs:573
#12 0x000056195636263b in __rust_try ()
#13 0x000056195628e453 in std::panicking::catch_unwind<i32, &(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe)> (f=...)
    at src/panicking.rs:541
#14 0x000056195628e33a in std::panic::catch_unwind<&(dyn core::ops::function::Fn<(), Output=i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe), i32> (f=...)
    at src/panic.rs:359
#15 0x00005619562d6653 in std::rt::lang_start_internal::{closure#0} () at src/rt.rs:175
#16 0x00005619562c4a91 in std::panicking::catch_unwind::do_call<std::rt::lang_start_internal::{closure_env#0}, isize> (data=0x7fffafd957c8) at src/panicking.rs:573
#17 0x000056195636263b in __rust_try ()
#18 0x000056195628e3b0 in std::panicking::catch_unwind<isize, std::rt::lang_start_internal::{closure_env#0}> (f=...) at src/panicking.rs:541
#19 0x000056195628e30a in std::panic::catch_unwind<std::rt::lang_start_internal::{closure_env#0}, isize> (f=...) at src/panic.rs:359
#20 0x000056195632c686 in std::rt::lang_start_internal (main=..., argc=1, argv=0x7fffafd95a78, sigpipe=0) at src/rt.rs:171
#21 0x00005619563626b6 in std::rt::lang_start<()> (main=0x561956362778 <__ubsan_check_cfi_icall_jt>, argc=1, argv=0x7fffafd95a78, sigpipe=0)
    at /home/raul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:205
#22 0x00005619563661bb in main ()
#23 0x00007f6590427781 in __libc_start_call_main (main=main@entry=0x5619563661a0 <main>, argc=argc@entry=1, argv=argv@entry=0x7fffafd95a78) at ../sysdeps/nptl/libc_start_call_main.h:59
#24 0x00007f65904278b9 in __libc_start_main_impl (main=0x5619563661a0 <main>, argc=1, argv=0x7fffafd95a78, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7fffafd95a68) at ../csu/libc-start.c:372
#25 0x000056195626c5a5 in _start ()

Lenguaje dominante
Rust
Estrellas
122k
Forks
16.7k
Merge medio
2 d 16 h
PR fusionados (30 d)
515

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.