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

Segfault in argv::iter when linked with musl #3

Closed
nikclayton-dfinity opened this issue Oct 7, 2020 · 6 comments · Fixed by #4
Closed

Segfault in argv::iter when linked with musl #3

nikclayton-dfinity opened this issue Oct 7, 2020 · 6 comments · Fixed by #4

Comments

@nikclayton-dfinity
Copy link

argv segfaults in unsafe code when compiled using musl instead of glibc.

Reproduction code

Cargo.toml:

[package]
name = "argv_musl_bug"
version = "0.1.0"
edition = "2018"

[dependencies]
argv = "0.1"

src/main.rs:

fn main() {
    for arg in argv::iter() {
        println!("{}", arg.to_string_lossy());
    }

    println!("After argument parsing, exiting");
}

Results

Works: On a Mac:

% uname -a
Darwin Niks-MBP 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64 x86_64 i386 MacBookPro15,1 Darwin

% cargo --version
cargo 1.45.1

% cargo run
[...]
After argument parsing, exiting

Works: In the official Rust docker container:

Note: Run docker from the crate's root, this will mount the crate's files at /crate in the container.

% docker run -it --mount type=bind,source=$(pwd),target=/crate rust:1.46
root% uname -a
Linux 8928eecdcc46 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 GNU/Linux

root% cd /crate

root% rm -rf target # just in case it's there from the Mac build

root% cargo run
[...]
After argument parsing, exiting

Fails: Static linking against musl

Still in the docker container from the previous step, follow the instructions at https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html to add the musl target

root% rustup target add x86_64-unknown-linux-musl
info: downloading component 'rust-std' for 'x86_64-unknown-linux-musl'
info: installing component 'rust-std' for 'x86_64-unknown-linux-musl'
info: Defaulting to 500.0 MiB unpack ram
 15.3 MiB /  15.3 MiB (100 %)   9.7 MiB/s in  1s ETA:  0s

Then try and build and run that target.

root% cargo run --target x86_64-unknown-linux-musl
   Compiling argv v0.1.2
   Compiling argv_musl_bug v0.1.0 (/crate)
    Finished dev [unoptimized + debuginfo] target(s) in 5.49s
     Running `target/x86_64-unknown-linux-musl/debug/argv_musl_bug`
Segmentation fault

Setting RUST_BACKTRACE=full doesn't work, you need to install gdb and run it under there, so:

root% apt-get update
[...]

root% apt-get install gdb
[...]

root% gdb target/x86_64-unknown-linux-musl/debug/argv_musl_bug
[...]
(gdb) run
Starting program: /crate/target/x86_64-unknown-linux-musl/debug/argv_musl_bug
warning: Error disabling address space randomization: Operation not permitted

Program received signal SIGSEGV, Segmentation fault.
0x00007f5406f1e02f in <argv::impl::Iter as core::iter::traits::iterator::Iterator>::next (
    self=0x7ffdf6ea4b48)
    at /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/argv-0.1.2/src/lib.rs:119
119	                let ptr = unsafe { *self.next };
(gdb) bt
#0  0x00007f5406f1e02f in <argv::impl::Iter as core::iter::traits::iterator::Iterator>::next (
    self=0x7ffdf6ea4b48)
    at /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/argv-0.1.2/src/lib.rs:119
#1  0x00007f5406f1de9f in <argv::Iter as core::iter::traits::iterator::Iterator>::next (
    self=0x7ffdf6ea4b48)
    at /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/argv-0.1.2/src/lib.rs:56
#2  0x00007f5406f1da83 in argv_musl_bug::main () at src/main.rs:2

This obviously affects anything that tries to use argv, including gflags.

@nikclayton-dfinity
Copy link
Author

Based on golang/go#13492, musl appears to believe that libraries don't get access to argv/argc, and the equivalent of main() should pass the values to the library.

@nikclayton-dfinity
Copy link
Author

@dtolnay
Copy link
Owner

dtolnay commented Oct 7, 2020

Thanks! I published 0.1.3 with a fix.

@nikclayton-dfinity
Copy link
Author

nikclayton-dfinity commented Oct 7, 2020

Thanks, that was quick. Please can you also publish a new version of gflags that bumps the argv dependency (which is currently pinned to 1.1.1).

dtolnay added a commit to dtolnay/gflags that referenced this issue Oct 7, 2020
@dtolnay
Copy link
Owner

dtolnay commented Oct 7, 2020

Done.

@nikclayton-dfinity
Copy link
Author

That is great -- thanks again.

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

Successfully merging a pull request may close this issue.

2 participants