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

segment fault in alpine linux #51

Open
plusls opened this issue Mar 21, 2024 · 4 comments
Open

segment fault in alpine linux #51

plusls opened this issue Mar 21, 2024 · 4 comments

Comments

@plusls
Copy link

plusls commented Mar 21, 2024

error: failed to run custom build command for `xxxxv0.1.0 (/xxxx)`
Caused by:
  process didn't exit successfully: `/builds/xxxx/target/release/build/xxxx-c3fa4b775c30545d/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)
  --- stdout
  cargo:rerun-if-changed=/builds/xxxx/custom_mime

And in my gdb I found that the reference of magic_open is null, I have no ideal why.

alpine env:
based on rust:1.76-alpine

apk add musl-dev file-dev
@robo9k
Copy link
Owner

robo9k commented Mar 22, 2024

Hej @plusls, could you add some more detail about your setup?

Which version of magic-sys are you using?
Are you using magic-sys directly or via the magic crate and which version?
Which version of the musl-dev and file-dev packages did you end up with?
How are you building your project, did you set any environment variables to configure magic-sys?
What did you debug with gdb, your build-script? When/where is magic_open set to null?

@plusls
Copy link
Author

plusls commented Mar 25, 2024

I use the rust-magic-sys 0.3.0, and I try to use it in docker image rust:alpine,with deps

apk add musl-dev file-dev

deps version:

  • file-dev: file-dev-5.45-r1 x86_64
  • musl-dev: musl-dev-1.2.4_git20230717-r4

I direct build my program with cargo build --release, without other envirom var.

my code:

fn main() {
    unsafe {magic_sys::magic_open(0)};
    println!("Hello, world!");
}

And I saw that

image

I found that it success link to libmagic

image

I don't know why, the value in got table of magic_open is null ptr, I debug it with gdb and reverse it with ida pro

@plusls
Copy link
Author

plusls commented Apr 1, 2024

any progress? @robo9k

@robo9k
Copy link
Owner

robo9k commented Apr 1, 2024

I can reproduce this, but since I'm usually using neither Alpine nor MUSL I don't really know why the shared libmagic library is not loaded, even though it is being linked against.

You can fix this by "dynamically linking" to libc (but this seems to link to libgcc_s, compare with your ldd screenshot):

$ RUSTFLAGS="-C target-feature=-crt-static" cargo build

This pretty much seems to be the same as https://users.rust-lang.org/t/segfault-when-linking-against-shared-libraries-on-alpine/98629 and even the gdb backtrace is as unhelpful there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants