Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust musl build segfaults on startup when linked with LLD 12 #86712

Open
rocallahan opened this issue Jun 29, 2021 · 9 comments
Open

Rust musl build segfaults on startup when linked with LLD 12 #86712

rocallahan opened this issue Jun 29, 2021 · 9 comments
Assignees
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. O-musl Target: The musl libc P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rocallahan
Copy link

Steps to reproduce:

$ git clone https://github.com/rocallahan/rust-musl-segfault.git
$ cd rust-musl-segfault
$ cargo +nightly build --target x86_64-unknown-linux-musl
...
$ target/x86_64-unknown-linux-musl/debug/test_crate
Segmentation fault (core dumped)

Since the code is literally

use nix::sys::stat::Mode;

fn main() {
}

I expected it to not segfault.

@rocallahan rocallahan added the C-bug Category: This is a bug. label Jun 29, 2021
@rocallahan
Copy link
Author

$ ld --version
LLD 12.0.0 (compatible with GNU linkers)

If I actually use ld.bfd the problem goes away, so this is quite possibly an LLD bug, though it might not be.

@rocallahan
Copy link
Author

The segfault is in musl's __libc_sigaction. The segfault is at this instruction:

=> 0x00007f844d139e29 <+142>:	mov    %eax,-0x4be2f(%rip)        # 0x7f844d0ee000

0x7f844d0ee000 is unmapped memory. Looks like a linker relocation points somewhere wild.

@rocallahan
Copy link
Author

If I remove the use statement the problem goes away.

@12101111
Copy link
Contributor

12101111 commented Jul 8, 2021

I can reproduce this issue, the output will segfault when

  1. use x86_64-unknown-linux-musl target
  2. crt-static is enabled -C target-feature=+crt-static (It enabled by default on musl )
  3. Use lld as linker ( Either use ld.lld directly -Clink-self-contained=yes -Clinker=ld.lld or use clang and linker args -Clink-self-contained=no -Clinker=clang -Clink-arg=-fuse-ld=bfd)
  4. In debug mode ( no --release or -Copt-level > 0 )

The bisect command is

export RUSTFLAGS="-Ctarget-feature=+crt-static -Clinker=clang -Clink-arg=-fuse-ld=lld"
cargo bisect-rustc --start 2020-08-07 --end 2020-08-09 -- run

The bisect result is:

get_commits_between returning commits, len: 13
  commit[0] 2020-08-07UTC: Auto merge of #75255 - davidtwco:polymorphisation-symbol-mangling-v0-upvar-closures, r=lcnr
  commit[1] 2020-08-07UTC: Auto merge of #75071 - ssomers:btree_cleanup_5, r=Mark-Simulacrum
  commit[2] 2020-08-08UTC: Auto merge of #75048 - eggyal:force-no-tco-start-backtrace-frame, r=Mark-Simulacrum
  commit[3] 2020-08-08UTC: Auto merge of #74877 - lcnr:min_const_generics, r=oli-obk
  commit[4] 2020-08-08UTC: Auto merge of #75276 - JohnTitor:rollup-rz4hs0w, r=JohnTitor
  commit[5] 2020-08-08UTC: Auto merge of #74932 - nnethercote:rm-ast-session-globals, r=petrochenkov
  commit[6] 2020-08-08UTC: Auto merge of #75257 - ssomers:btree_74762_again, r=Mark-Simulacrum
  commit[7] 2020-08-08UTC: Auto merge of #75282 - RalfJung:miri-black-box, r=oli-obk
  commit[8] 2020-08-08UTC: Auto merge of #74289 - lzutao:unroll, r=LukasKalbertodt
  commit[9] 2020-08-08UTC: Auto merge of #74533 - nikic:issue-74425, r=eddyb
  commit[10] 2020-08-08UTC: Auto merge of #75260 - davidtwco:polymorphization-promoted-substs, r=lcnr
  commit[11] 2020-08-08UTC: Auto merge of #75163 - canova:map_into_keys_values, r=dtolnay
  commit[12] 2020-08-08UTC: Auto merge of #75271 - cuviper:array-iter, r=LukasKalbertodt
ERROR: no CI builds available between 09f4c9f5082f78b0adcee83d3ab4337e000cd28e and ceedf1d5febd65b012b8bcd513d70a0a6a091210 within last 167 days

My environment:

> clang -v
clang version 12.0.0
Target: x86_64-gentoo-linux-musl
Thread model: posix
InstalledDir: /usr/lib/llvm/12/bin
> ld.lld --version
LLD 12.0.0 (compatible with GNU linkers)

@rustbot label: +A-linkage +O-musl +regression-from-stable-to-stable

@rustbot rustbot added A-linkage Area: linking into static, shared libraries and binaries O-musl Target: The musl libc regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 8, 2021
@apiraino
Copy link
Contributor

thanks @12101111 for the additional information. Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-high +T-compiler

@rustbot rustbot added P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 11, 2021
@pnkfelix pnkfelix self-assigned this Nov 18, 2022
@wesleywiser
Copy link
Member

Testing with our build of lld 15 on nightly and this seems to work fine now:

$ rustc +nightly --version -v
rustc 1.67.0-nightly (77e57db38 2022-10-30)
binary: rustc
commit-hash: 77e57db384aca99444c3b5f6a9c86bc58a804d89
commit-date: 2022-10-30
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
$ git clone https://github.com/rocallahan/rust-musl-segfault.git
$ cd rust-musl-segfault/
$ RUSTFLAGS="-Ctarget-feature=+crt-static -Clinker=rust-lld" cargo +nightly build --target x86_64-unknown-linux-musl
$ ./target/x86_64-unknown-linux-musl/debug/test_crate
$ echo $?
0

@pnkfelix
Copy link
Member

Indeed, this seems it was "fixed" (or perhaps hidden) sometime in late August 2021:

% ND=nightly-2021-08-21 ; rustup update $ND ; rustup target add --toolchain $ND x86_64-unknown-linux-musl ; RUSTFLAGS="-Ctarget-feature=+crt-static -Clinker=rust-lld" cargo +$ND build --target x86_64-unknown-linux-musl && ./target/x86_64-unknown-linux-musl/debug/test_crate
info: syncing channel updates for 'nightly-2021-08-21-x86_64-unknown-linux-gnu'

  nightly-2021-08-21-x86_64-unknown-linux-gnu unchanged - rustc 1.56.0-nightly (a0035916e 2021-08-20)

info: checking for self-updates
info: component 'rust-std' for target 'x86_64-unknown-linux-musl' is up to date
   Compiling bitflags v1.2.1
   Compiling libc v0.2.97
   Compiling nix v0.17.0
   Compiling cfg-if v0.1.10
   Compiling void v1.0.2
   Compiling test_crate v0.1.0 (/media/pnkfelix/Rust/issue_86712/rust-musl-segfault)
warning: unused import: `nix::sys::stat::Mode`
 --> src/main.rs:1:5
  |
1 | use nix::sys::stat::Mode;
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `test_crate` (bin "test_crate") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 3.64s
Segmentation fault (core dumped)
15-15-24 issue_86712/rust-musl-segfault (git:main) [ERROR#139] % ND=nightly-2021-08-22 ; rustup update $ND ; rustup target add --toolchain $ND x86_64-unknown-linux-musl ; RUSTFLAGS="-Ctarget-feature=+crt-static -Clinker=rust-lld" cargo +$ND build --target x86_64-unknown-linux-musl && ./target/x86_64-unknown-linux-musl/debug/test_crate
info: syncing channel updates for 'nightly-2021-08-22-x86_64-unknown-linux-gnu'

  nightly-2021-08-22-x86_64-unknown-linux-gnu unchanged - rustc 1.56.0-nightly (d3e2578c3 2021-08-21)

info: checking for self-updates
info: component 'rust-std' for target 'x86_64-unknown-linux-musl' is up to date
   Compiling libc v0.2.97
   Compiling bitflags v1.2.1
   Compiling nix v0.17.0
   Compiling cfg-if v0.1.10
   Compiling void v1.0.2
   Compiling test_crate v0.1.0 (/media/pnkfelix/Rust/issue_86712/rust-musl-segfault)
warning: unused import: `nix::sys::stat::Mode`
 --> src/main.rs:1:5
  |
1 | use nix::sys::stat::Mode;
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `test_crate` (bin "test_crate") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 3.60s
%

@pnkfelix
Copy link
Member

pnkfelix commented Dec 13, 2022

I'm going to guess this was fixed by the LLVM 13 upgrade that took place with #87570 (on 2021-08-21).

@pnkfelix
Copy link
Member

So I think the only thing that's blocking this ticket being closed is a regression test. Marking accordingly.

@rustbot label: E-needs-test

@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 13, 2022
@JohnTitor JohnTitor added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. O-musl Target: The musl libc P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants