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

segmentation fault #80

Closed
CandySunPlus opened this issue Jul 25, 2021 · 2 comments
Closed

segmentation fault #80

CandySunPlus opened this issue Jul 25, 2021 · 2 comments

Comments

@CandySunPlus
Copy link

segmentation fault in Alpine Linux
code:

use libusb1_sys as ffi;

pub fn main() {
    let version = unsafe { &*ffi::libusb_get_version() };

    println!(
        "libusb v{}.{}.{}.{}",
        version.major, version.minor, version.micro, version.nano
    );
}

build log:

$ cargo run -vv
   Compiling cc v1.0.69
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=cc CARGO_MANIFEST_DIR=/home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/cc-1.0.69 CARGO_PKG_AUTHORS='Alex Crichton <alex@alexcrichton.com>' CARGO_PKG_DESCRIPTION='A build-time dependency for Cargo build scripts to assist in invoking the native
C compiler to compile native C code into a static archive to be linked into Rust
code.
' CARGO_PKG_HOMEPAGE='https://github.com/alexcrichton/cc-rs' CARGO_PKG_LICENSE=MIT/Apache-2.0 CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=cc CARGO_PKG_REPOSITORY='https://github.com/alexcrichton/cc-rs' CARGO_PKG_VERSION=1.0.69 CARGO_PKG_VERSION_MAJOR=1 CARGO_PKG_VERSION_MINOR=0 CARGO_PKG_VERSION_PATCH=69 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' rustc --crate-name cc --edition=2018 /home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/cc-1.0.69/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=bc48367fbd4943a1 -C extra-filename=-bc48367fbd4943a1 --out-dir /home/niksun/development/rust/usb/target/debug/deps -L dependency=/home/niksun/development/rust/usb/target/debug/deps --cap-lints warn`
   Compiling libc v0.2.98
   Compiling pkg-config v0.3.19
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=build_script_build CARGO_MANIFEST_DIR=/home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.98 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.98 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=98 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' rustc --crate-name build_script_build /home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.98/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=5a8c16f1ed0a03b2 -C extra-filename=-5a8c16f1ed0a03b2 --out-dir /home/niksun/development/rust/usb/target/debug/build/libc-5a8c16f1ed0a03b2 -L dependency=/home/niksun/development/rust/usb/target/debug/deps --cap-lints warn`
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=pkg_config CARGO_MANIFEST_DIR=/home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/pkg-config-0.3.19 CARGO_PKG_AUTHORS='Alex Crichton <alex@alexcrichton.com>' CARGO_PKG_DESCRIPTION='A library to run the pkg-config system tool at build time in order to be used in
Cargo build scripts.
' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT/Apache-2.0 CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=pkg-config CARGO_PKG_REPOSITORY='https://github.com/rust-lang/pkg-config-rs' CARGO_PKG_VERSION=0.3.19 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=3 CARGO_PKG_VERSION_PATCH=19 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' rustc --crate-name pkg_config /home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/pkg-config-0.3.19/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=12760bb814148db2 -C extra-filename=-12760bb814148db2 --out-dir /home/niksun/development/rust/usb/target/debug/deps -L dependency=/home/niksun/development/rust/usb/target/debug/deps --cap-lints warn`
     Running `/home/niksun/development/rust/usb/target/debug/build/libc-5a8c16f1ed0a03b2/build-script-build`
[libc 0.2.98] cargo:rerun-if-changed=build.rs
[libc 0.2.98] cargo:rustc-cfg=freebsd11
[libc 0.2.98] cargo:rustc-cfg=libc_priv_mod_use
[libc 0.2.98] cargo:rustc-cfg=libc_union
[libc 0.2.98] cargo:rustc-cfg=libc_const_size_of
[libc 0.2.98] cargo:rustc-cfg=libc_align
[libc 0.2.98] cargo:rustc-cfg=libc_core_cvoid
[libc 0.2.98] cargo:rustc-cfg=libc_packedN
[libc 0.2.98] cargo:rustc-cfg=libc_cfg_target_vendor
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=libc CARGO_MANIFEST_DIR=/home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.98 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.98 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=98 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' OUT_DIR=/home/niksun/development/rust/usb/target/debug/build/libc-25c8dc8919ea7310/out rustc --crate-name libc /home/niksun/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/libc-0.2.98/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=49e285bc42127cb4 -C extra-filename=-49e285bc42127cb4 --out-dir /home/niksun/development/rust/usb/target/debug/deps -L dependency=/home/niksun/development/rust/usb/target/debug/deps --cap-lints warn --cfg freebsd11 --cfg libc_priv_mod_use --cfg libc_union --cfg libc_const_size_of --cfg libc_align --cfg libc_core_cvoid --cfg libc_packedN --cfg libc_cfg_target_vendor`
   Compiling libusb1-sys v0.5.0 (/home/niksun/development/rust/rusb/libusb1-sys)
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=build_script_build CARGO_MANIFEST_DIR=/home/niksun/development/rust/rusb/libusb1-sys CARGO_PKG_AUTHORS='David Cuddeback <david.cuddeback@gmail.com>:Ilya Averyanov <a1ien.n3t@gmail.com>' CARGO_PKG_DESCRIPTION='FFI bindings for libusb.' CARGO_PKG_HOMEPAGE='https://github.com/a1ien/rusb' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libusb1-sys CARGO_PKG_REPOSITORY='https://github.com/a1ien/rusb.git' CARGO_PKG_VERSION=0.5.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=5 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' rustc --crate-name build_script_build --edition=2018 /home/niksun/development/rust/rusb/libusb1-sys/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=110a414e8f76d54a -C extra-filename=-110a414e8f76d54a --out-dir /home/niksun/development/rust/usb/target/debug/build/libusb1-sys-110a414e8f76d54a -C incremental=/home/niksun/development/rust/usb/target/debug/incremental -L dependency=/home/niksun/development/rust/usb/target/debug/deps --extern cc=/home/niksun/development/rust/usb/target/debug/deps/libcc-bc48367fbd4943a1.rlib --extern pkg_config=/home/niksun/development/rust/usb/target/debug/deps/libpkg_config-12760bb814148db2.rlib`
     Running `/home/niksun/development/rust/usb/target/debug/build/libusb1-sys-110a414e8f76d54a/build-script-build`
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=LIBUSB_1.0_NO_PKG_CONFIG
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=SYSROOT
[libusb1-sys 0.5.0] cargo:rustc-link-search=native=/usr/lib
[libusb1-sys 0.5.0] cargo:rustc-link-lib=usb-1.0
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_PATH
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
[libusb1-sys 0.5.0] cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
[libusb1-sys 0.5.0] cargo:include=/usr/include/libusb-1.0
[libusb1-sys 0.5.0] cargo:version_number=1.0.24
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=libusb1_sys CARGO_MANIFEST_DIR=/home/niksun/development/rust/rusb/libusb1-sys CARGO_PKG_AUTHORS='David Cuddeback <david.cuddeback@gmail.com>:Ilya Averyanov <a1ien.n3t@gmail.com>' CARGO_PKG_DESCRIPTION='FFI bindings for libusb.' CARGO_PKG_HOMEPAGE='https://github.com/a1ien/rusb' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libusb1-sys CARGO_PKG_REPOSITORY='https://github.com/a1ien/rusb.git' CARGO_PKG_VERSION=0.5.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=5 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' OUT_DIR=/home/niksun/development/rust/usb/target/debug/build/libusb1-sys-4d7d9807ec56a82d/out rustc --crate-name libusb1_sys --edition=2018 /home/niksun/development/rust/rusb/libusb1-sys/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=146a826da8a6d5f3 -C extra-filename=-146a826da8a6d5f3 --out-dir /home/niksun/development/rust/usb/target/debug/deps -C incremental=/home/niksun/development/rust/usb/target/debug/incremental -L dependency=/home/niksun/development/rust/usb/target/debug/deps --extern libc=/home/niksun/development/rust/usb/target/debug/deps/liblibc-49e285bc42127cb4.rmeta -L native=/usr/lib -l usb-1.0`
   Compiling usb v0.1.0 (/home/niksun/development/rust/usb)
     Running `CARGO=/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/bin/cargo CARGO_BIN_NAME=usb CARGO_CRATE_NAME=usb CARGO_MANIFEST_DIR=/home/niksun/development/rust/usb CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=usb CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/niksun/development/rust/usb/target/debug/deps:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib:/home/niksun/.rustup/toolchains/stable-x86_64-unknown-linux-musl/lib' rustc --crate-name usb --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=94c3ab235d114386 -C extra-filename=-94c3ab235d114386 --out-dir /home/niksun/development/rust/usb/target/debug/deps -C incremental=/home/niksun/development/rust/usb/target/debug/incremental -L dependency=/home/niksun/development/rust/usb/target/debug/deps --extern libusb1_sys=/home/niksun/development/rust/usb/target/debug/deps/liblibusb1_sys-146a826da8a6d5f3.rlib -L native=/usr/lib`
    Finished dev [unoptimized + debuginfo] target(s) in 7.95s
     Running `target/debug/usb`
[1]    24324 segmentation fault  cargo run -vv

ldd:

ldd target/debug/usb
        /lib/ld-musl-x86_64.so.1 (0x7fc934ac4000)
        libusb-1.0.so.0 => /usr/lib/libusb-1.0.so.0 (0x7fc934a42000)
        libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7fc934ac4000)
@a1ien
Copy link
Owner

a1ien commented Jul 26, 2021

Hmm... I am not sure that is rusb problem. I jut do this test

  1. run docker docker run -v pwd:/build --rm -it rust:alpine
  2. install some package apk add libusb-dev pkgconfig gcc musl-dev
  3. compile simple test. gcc ver.c pkg-config --libs --cflags libusb-1.0`
#include "libusb.h"
#include <stdio.h>

int test_init_and_exit(void)
{
	for (int i = 0; i < 10000; ++i) {
		libusb_context *ctx = NULL;
		int r;

		r = libusb_init(&ctx);
		if (r != LIBUSB_SUCCESS) {
			printf(
				"Failed to init libusb on iteration %d: %d\n",
				i, r);
			return -1;
		}
		libusb_exit(ctx);
	}

	return 0;
}

int main(int argc, char** argv)
{
   const struct libusb_version* version;
    version = libusb_get_version();

    printf("Using libusb v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
	test_init_and_exit();
}

Failed to init libusb on iteration 0: -99

@a1ien
Copy link
Owner

a1ien commented Jul 26, 2021

The workaround describe here sfackler/rust-openssl#1462 (comment)

The system-installed copy of OpenSSL dynamically links to the system MUSL, but Rust will by default statically link to a bundled MUSL, and that causes things to break when calling into OpenSSL.

You should either build an OpenSSL that also statically links to MUSL (e.g. with the vendored cargo feature), or tell Rust to also dynamically link to the system musl by setting RUSTFLAGS=-Ctarget-feature=-crt-static.

@a1ien a1ien closed this as completed Jul 26, 2021
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