Skip to content

lto = "thin" causes doctest to generate invalid code on rust-1.57 #92869

Closed
@trofi

Description

@trofi

It's a senf-contained example extracted from rayon-rs/rayon#911 where doctest tests mysteriously crash rav1e. This is what crashes for me:

// cat src/lib.rs
thread_local! {
    static THREAD_LOCAL_GLOBAL: std::cell::Cell<usize> = std::cell::Cell::new(0);
}

#[inline(never)]
fn set_state_func(t: &std::cell::Cell<usize>) {
    t.set(42);
}

#[inline(never)]
fn thread_func() {
    THREAD_LOCAL_GLOBAL.with(set_state_func);
}

/// # Examples
///
/// ```
/// use bug::do_bug;
///
/// # fn main() {
/// bug::do_bug()
/// # }
/// ```
#[inline(never)]
pub fn do_bug() {
  // to ease catching the test with gdb
  //std::thread::sleep(std::time::Duration::from_secs(10));

  for _ in 0..128 {
    std::thread::spawn(thread_func).join().unwrap();
  }
}

It needs the following Cargo.toml:

# cat Cargo.toml
[package]
name = "bug"
version = "0.1.0"
edition = "2018"

[profile.dev]
debug = true
incremental = true
lto = "thin"
opt-level = 2
debug-assertions = false
overflow-checks = false
codegen-units = 256

Running the test:

$ cargo test --doc --verbose
   Compiling bug v0.1.0 (/home/slyfox/dev/git/rav1e)
     Running `rustc --crate-name bug --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=2 -C linker-plugin-lto -C codegen-units=256 -C debuginfo=2 -C metadata=08626d1130101a14 -C extra-filename=-08626d1130101a14 --out-dir /home/slyfox/dev/git/rav1e/target/debug/deps -C incremental=/home/slyfox/dev/git/rav1e/target/debug/incremental -L dependency=/home/slyfox/dev/git/rav1e/target/debug/deps`
    Finished test [optimized + debuginfo] target(s) in 0.15s
   Doc-tests bug
     Running `rustdoc --edition=2018 --crate-type lib --crate-name bug --test /home/slyfox/dev/git/rav1e/src/lib.rs -L dependency=/home/slyfox/dev/git/rav1e/target/debug/deps -L dependency=/home/slyfox/dev/git/rav1e/target/debug/deps --extern bug=/home/slyfox/dev/git/rav1e/target/debug/deps/libbug-08626d1130101a14.rlib -C lto=thin --error-format human`

running 1 test
test src/lib.rs - do_bug (line 17) ... FAILED

failures:

---- src/lib.rs - do_bug (line 17) stdout ----
Test executable failed (terminated by signal).


failures:
    src/lib.rs - do_bug (line 17)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.35s

error: test failed, to rerun pass '--doc'

I expected to see this happen: test should pass and not crash.

Instead, this happened: test crashes mysteriously.

Meta

rustc --version --verbose:

rustc --version --verbose
rustc 1.57.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.57.0
LLVM version: 13.0.0
Backtrace

Thread 2 "rust_out" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ff5caf23640 (LWP 2280238)]
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ff5caf235f8 in ?? ()
#2  0x000000008498015f in ?? ()
#3  0x0000559d328efdd6 in core::ops::function::FnOnce::call_once<fn(), ()> ()
    at /build/rustc-1.57.0-src/library/core/src/ops/function.rs:227
#4  std::sys_common::backtrace::__rust_begin_short_backtrace<fn(), ()> (f=<optimized out>)
    at /build/rustc-1.57.0-src/library/std/src/sys_common/backtrace.rs:123
#5  0x0000559d328efe40 in std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<std::thread::{impl#0}::spawn_unchecked::{closure#1}::{closure#0}>, ()> (data=<optimized out>) at /build/rustc-1.57.0-src/library/std/src/panicking.rs:403
#6  std::panicking::try<(), core::panic::unwind_safe::AssertUnwindSafe<std::thread::{impl#0}::spawn_unchecked::{closure#1}::{closure#0}>> (f=...) at /build/rustc-1.57.0-src/library/std/src/panicking.rs:367
#7  0x0000559d328efa11 in std::thread::{impl#0}::spawn_unchecked::{closure#1}<fn(), ()> ()
    at /build/rustc-1.57.0-src/library/std/src/thread/mod.rs:482
#8  core::ops::function::FnOnce::call_once<std::thread::{impl#0}::spawn_unchecked::{closure#1}, ()> ()
    at /build/rustc-1.57.0-src/library/core/src/ops/function.rs:227
#9  0x0000559d329603d5 in std::sys::unix::thread::Thread::new::thread_start ()
#10 0x00007ff5cb0fbd40 in start_thread () from /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libpthread.so.0
#11 0x00007ff5cb02343f in clone () from /nix/store/s9qbqh7gzacs7h68b2jfmn9l6q4jwfjz-glibc-2.33-59/lib/libc.so.6

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-LTOArea: Link-time optimization (LTO)C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions