tikv / pprof-rs

A Rust CPU profiler implemented with the help of backtrace-rs
Apache License 2.0
1.3k stars 99 forks source link

Test under musl failed for pthread_getname_np not found #142

Open Xuanwo opened 2 years ago

Xuanwo commented 2 years ago

It's pretty wired. I run nm libc.a |grep pthread|grep getname in the alpine, and got:

0000000000000000 T pthread_getname_np

But I came across the same issue you faced. I will approve and merge this PR to make it work under musl (and I'll investigate further why it cannot link with this symbol)

_Originally posted by @YangKeao in https://github.com/tikv/pprof-rs/pull/140#discussion_r906205050_

YangKeao commented 2 years ago

If we find it always failed (and prepare a minimal reproducible example), we can report this problem to the libc crate.

Xuanwo commented 2 years ago

The minimal reproducible example:

pub const MAX_THREAD_NAME: usize = 16;

fn main() {
    let current_thread = unsafe { libc::pthread_self() };
    let mut name = [0_i8; MAX_THREAD_NAME];
    let name_ptr = &mut name as *mut libc::c_char;
    let result = unsafe { libc::pthread_getname_np(current_thread, name_ptr, MAX_THREAD_NAME) };
    println!("got {result}: {name:?}");
}

Works under gnu:

:( cargo run
   Compiling libc v0.2.126
   Compiling libc_test v0.1.0 (/home/xuanwo/Code/xuanwo/rust-playground/libc_test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.61s
     Running `/home/xuanwo/Code/xuanwo/rust-playground/target/debug/libc_test`
got 0: [108, 105, 98, 99, 95, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0]

But failed under musl:

:) cargo run --target x86_64-unknown-linux-musl
   Compiling libc v0.2.126
   Compiling libc_test v0.1.0 (/home/xuanwo/Code/xuanwo/rust-playground/libc_test)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.14v1ez4oz89gvn8d.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.19bwhm6xg8cfwxd8.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.1o3c9hbz320wtmxb.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.23xrez8oelkz64yl.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.36lh7jgovr639e3i.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.3dtua2jmdq2gb0u8.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.3g02hgl841r9d3cs.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.3tzxoq9ztmu74bj5.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.3uuiq6dh4mmnnewr.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.3ylq0z44hxbeahss.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.47dk7yv41ygtfk2b.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.4c35m1x8k7mhb7yw.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.4j16ufoq71st6gmk.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.4khuk9o7i2l0ozkb.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.4s3i1di0iezubtv9.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.58clh0y01wpq6290.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.5ek2fbend7kd7pcv.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.5zm65zipw2ifa29.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.q413du7u7x3x7l0.rcgu.o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.550zlew0kqjetrp4.rcgu.o" "-Wl,--as-needed" "-L" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps" "-L" "/home/xuanwo/Code/xuanwo/rust-playground/target/debug/deps" "-L" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/liblibc-ed02612f02b4d710.rlib" "-Wl,--start-group" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-f4c4d46e2c92f7e8.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-5718f6ffddf5ee32.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-0d1808e3fa0f4972.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-867572e7d2b1a37a.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-8936f277d6f129b4.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libmemchr-95bdf332c919ec2d.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-de517dbe5e03ebcd.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-d17001cfd16861a0.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-f1d0433ba63a9c44.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-99b2117164d979d6.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-fc9f890fbcf45c23.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-239281accac99750.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-bd7bf2dac51cd3a1.rlib" "-lunwind" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-2fbf9aa7168ff1c5.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-9e3ec3da6f4fc496.rlib" "-lc" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-65234c84e13bcb9c.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-3fb701dea1065ada.rlib" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-3001270ca6eb694f.rlib" "-Wl,--end-group" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-4e10c91f6e889509.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-nostartfiles" "-L" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "-o" "/home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: /usr/bin/ld: /home/xuanwo/Code/xuanwo/rust-playground/target/x86_64-unknown-linux-musl/debug/deps/libc_test-45164251b81adc9b.1o3c9hbz320wtmxb.rcgu.o: in function `libc_test::main':
          /home/xuanwo/Code/xuanwo/rust-playground/libc_test/src/main.rs:7: undefined reference to `pthread_getname_np'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `libc_test` due to previous error

I'm confirmed that musl already have pthread_getname_np:

xuanwo/rust-playground/libc_test is 📦 v0.1.0 via 🦀 v1.60.0
:( paru -Q musl
musl 1.2.3-1
xuanwo/rust-playground/libc_test is 📦 v0.1.0 via 🦀 v1.60.0
:) nm /usr/lib/musl/lib/libc.a |grep pthread|grep getname
nm: cache.lo: no symbols
nm: __invtrigl.lo: no symbols
nm: ceill.lo: no symbols
nm: expm1l.lo: no symbols
nm: truncl.lo: no symbols
pthread_getname_np.lo:
0000000000000000 T pthread_getname_np
nm: tls.lo: no symbols
Xuanwo commented 2 years ago

It looks like libc itself doesn't have musl tests covered...

https://github.com/rust-lang/libc/blob/ce3d1a2b86d4856376ea7025838569145eb9dd26/.github/workflows/main.yml#L17-L20

colincross commented 2 years ago

There is test coverage for musl in rust-libc, but it doesn't help much in this case because the pthread_getname_np test is disabled until the test environment is updated to an newer version of musl.

Your test code works for me, although my musl rust setup is not completely standard:

   Compiling libc v0.2.126
   Compiling hello_world v0.1.0 (/tmp/rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.92s
     Running `target/x86_64-unknown-linux-musl/debug/hello_world`
got 0: [104, 101, 108, 108, 111, 95, 119, 111, 114, 108, 100, 0, 0, 0, 0, 0]
$ nm libc.a | grep pthread_getname_np
pthread_getname_np.o:
0000000000000000 T pthread_getname_np

Is it possible your libc.a is coming from somewhere else besides /usr/lib/musl/lib/libc.a?

Xuanwo commented 2 years ago

There is test coverage for musl in rust-libc, but it doesn't help much in this case because the pthread_getname_np test is disabled until the test environment is updated to an newer version of musl.

Thanks for explaining.

Is it possible your libc.a is coming from somewhere else besides /usr/lib/musl/lib/libc.a?

Nice catch.

Based on failed log, rustc tried to link with musl bundled by rust:

/home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/libc.a
:) nm /home/xuanwo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/libc.a | grep pthread_getname_np

@BusyJay also confirmed that rust has it's own bundled musl: https://github.com/tikv/jemallocator/issues/30#issuecomment-1167816106

Xuanwo commented 2 years ago

Oh, I found this workaround:

RUSTFLAGS="-C target-feature=-crt-static" cargo run --target x86_64-unknown-linux-musl