diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index 6316512ec..7deb9822c 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -255,61 +255,6 @@ jobs: # fi # RUST_BACKTRACE=1 cargo fuzz run --no-default-features --features="$features" $target -- -max_total_time=10 # done - # - # link-c-dynamic-library: - # name: dynamic library - # strategy: - # matrix: - # include: - # - target: x86_64-unknown-linux-gnu - # features: - # - '' - # runs-on: ubuntu-latest - # steps: - # - name: Checkout sources - # uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - # with: - # persist-credentials: false - # submodules: true - # - name: Install rust toolchain - # uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248 - # with: - # toolchain: stable - # targets: ${{matrix.target}} - # - name: "cdylib: default settings" - # working-directory: libz-rs-sys-cdylib - # env: - # LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" - # run: | - # cargo build --release --target ${{matrix.target}} - # cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/libz_rs.so - # ./zpipe < Cargo.toml | ./zpipe -d > out.txt - # cmp -s Cargo.toml out.txt - # - name: "cdylib: rust-allocator" - # env: - # LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" - # working-directory: libz-rs-sys-cdylib - # run: | - # cargo build --release --target ${{matrix.target}} --no-default-features --features="rust-allocator" - # cc -o zpipe zpipe.c target/${{matrix.target}}/release/deps/libz_rs.so - # ./zpipe < Cargo.toml | ./zpipe -d > out.txt - # cmp -s Cargo.toml out.txt - # - name: "cdylib: no_std" - # env: - # LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" - # working-directory: libz-rs-sys-cdylib - # run: | - # cargo build --release --target ${{matrix.target}} --no-default-features - # cc -o zpipe_no_std zpipe_no_std.c target/${{matrix.target}}/release/deps/libz_rs.so - # ./zpipe_no_std < Cargo.toml | ./zpipe_no_std -d > out.txt - # cmp -s Cargo.toml out.txt - # - name: "cdylib: custom-prefix" - # working-directory: libz-rs-sys-cdylib - # env: - # LIBZ_RS_SYS_PREFIX: "MY_CUSTOM_PREFIX_" - # run: | - # cargo build --release --target ${{matrix.target}} --features=custom-prefix - # objdump -tT target/${{matrix.target}}/release/deps/libz_rs.so | grep -q "MY_CUSTOM_PREFIX_uncompress" || (echo "symbol not found!" && exit 1) wasm32: name: "wasm32" @@ -401,16 +346,7 @@ jobs: cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../ ./bzpipe < Cargo.toml | ./bzpipe -d > out.txt cmp -s Cargo.toml out.txt - - name: "cdylib: rust-allocator" - env: - LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" - working-directory: libbzip2-rs-sys-cdylib - run: | - cargo build --release --target ${{matrix.target}} --no-default-features --features="rust-allocator" - cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../ - ./bzpipe < Cargo.toml | ./bzpipe -d > out.txt - cmp -s Cargo.toml out.txt - - name: "cdylib: no_std" + - name: "cdylib: no stdio" env: LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" working-directory: libbzip2-rs-sys-cdylib @@ -424,7 +360,7 @@ jobs: LD_LIBRARY_PATH: "target/${{matrix.target}}/release/deps" working-directory: libbzip2-rs-sys-cdylib run: | - cargo build --release --target ${{matrix.target}} --no-default-features --features="c-allocator" + cargo build --release --target ${{matrix.target}} --no-default-features cc -o bzpipe bzpipe.c target/${{matrix.target}}/release/deps/libbz2_rs.so -I ../ ./bzpipe < Cargo.toml | ./bzpipe -d > out.txt cmp -s Cargo.toml out.txt diff --git a/libbzip2-rs-sys-cdylib/Cargo.lock b/libbzip2-rs-sys-cdylib/Cargo.lock index 2bf3797f9..94b82a2f2 100644 --- a/libbzip2-rs-sys-cdylib/Cargo.lock +++ b/libbzip2-rs-sys-cdylib/Cargo.lock @@ -7,6 +7,7 @@ name = "libbz2-rs-sys-cdylib" version = "0.0.0" dependencies = [ "libbzip2-rs-sys", + "libc", ] [[package]] diff --git a/libbzip2-rs-sys-cdylib/Cargo.toml b/libbzip2-rs-sys-cdylib/Cargo.toml index af115967e..ba6484dca 100644 --- a/libbzip2-rs-sys-cdylib/Cargo.toml +++ b/libbzip2-rs-sys-cdylib/Cargo.toml @@ -12,17 +12,18 @@ rust-version = "1.82" # MSRV [lib] name = "bz2_rs" # turns into e.g. `libbz2_rs.so` -crate-type=["cdylib"] +crate-type = ["cdylib"] +test = false [features] -default = ["c-allocator", "libbzip2-rs-sys/stdio", "libbzip2-rs-sys/std"] # when used as a cdylib crate, use the c allocator -c-allocator = ["libbzip2-rs-sys/c-allocator"] # by default, use malloc/free for memory allocation -rust-allocator = ["libbzip2-rs-sys/rust-allocator", "libbzip2-rs-sys/std"] # by default, use the rust global alloctor for memory allocation +default = ["stdio"] +stdio = ["libbzip2-rs-sys/stdio"] custom-prefix = ["libbzip2-rs-sys/custom-prefix"] # use the LIBBZIP2_RS_SYS_PREFIX to prefix all exported symbols capi = [] [dependencies] -libbzip2-rs-sys = { version = "0.0.0", path = "../libbzip2-rs-sys", default-features = false } +libbzip2-rs-sys = { version = "0.0.0", path = "../libbzip2-rs-sys", default-features = false, features = ["c-allocator"] } +libc = "0.2" [package.metadata.capi.library] version = "1.0.9" # the bzip2 api version we match @@ -34,3 +35,10 @@ enabled = false [package.metadata.capi.pkg_config] name = "libbz2_rs" filename = "libbz2_rs" + +# no_std requires panic=abort +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" diff --git a/libbzip2-rs-sys-cdylib/bzpipe.c b/libbzip2-rs-sys-cdylib/bzpipe.c index 33103904d..54f14b968 100644 --- a/libbzip2-rs-sys-cdylib/bzpipe.c +++ b/libbzip2-rs-sys-cdylib/bzpipe.c @@ -6,6 +6,7 @@ extern void bz_internal_error(int errcode) { fprintf(stderr, "bzip2 hit internal error code: %d\n", errcode); + exit(1); } #ifdef NO_STD diff --git a/libbzip2-rs-sys-cdylib/src/lib.rs b/libbzip2-rs-sys-cdylib/src/lib.rs index 4c833f413..b6c7b0953 100644 --- a/libbzip2-rs-sys-cdylib/src/lib.rs +++ b/libbzip2-rs-sys-cdylib/src/lib.rs @@ -1,3 +1,48 @@ +#![no_std] + extern crate libbzip2_rs_sys; +use core::panic::PanicInfo; pub use libbzip2_rs_sys::*; + +#[cfg(feature = "stdio")] +struct StderrWritter; + +#[cfg(feature = "stdio")] +impl core::fmt::Write for StderrWritter { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + use core::ffi::c_void; + use libc::write; + + unsafe { + write(2, s.as_ptr() as *const c_void, s.len()); + } + Ok(()) + } +} + +#[panic_handler] +fn panic_handler(_info: &PanicInfo) -> ! { + #[cfg(feature = "stdio")] + { + use core::fmt::Write; + use libc::exit; + + let _ = StderrWritter.write_str("libbzip2-rs: internal error:\n"); + let _ = StderrWritter.write_fmt(format_args!("{}", _info.message())); + + unsafe { + exit(3); + } + } + + #[cfg(not(feature = "stdio"))] + { + extern "C" { + fn bz_internal_error(errcode: core::ffi::c_int); + } + + unsafe { bz_internal_error(-1) } + loop {} + } +} diff --git a/libbzip2-rs-sys/Cargo.toml b/libbzip2-rs-sys/Cargo.toml index ad15cd317..b4d4229a7 100644 --- a/libbzip2-rs-sys/Cargo.toml +++ b/libbzip2-rs-sys/Cargo.toml @@ -4,7 +4,7 @@ readme = "README.md" edition.workspace = true [features] -default = ["std", "c-allocator", "stdio"] +default = ["std", "stdio"] c-allocator = ["dep:libc"] # use a malloc-based C allocator (rust is picked over c if both are configured) rust-allocator = [] # use the rust global allocator (rust is picked over c if both are configured) std = ["rust-allocator"] diff --git a/libbzip2-rs-sys/src/lib.rs b/libbzip2-rs-sys/src/lib.rs index d56a4d077..eaef371b3 100644 --- a/libbzip2-rs-sys/src/lib.rs +++ b/libbzip2-rs-sys/src/lib.rs @@ -84,37 +84,40 @@ pub(crate) use libbzip2_rs_sys_version; // --- assert failure logic macro_rules! assert_h { - ($condition:expr, $errcode:expr) => {{ - #[cfg(feature = "stdio")] + ($condition:expr, $errcode:expr) => { if !$condition { - #[cfg(feature = "std")] - std::eprint!("{}", $crate::AssertFail($errcode)); - #[cfg(feature = "std")] - std::process::exit(3); - - #[cfg(not(feature = "std"))] - panic!("{}", $crate::AssertFail($errcode)); - } - - #[cfg(not(feature = "stdio"))] - if !$condition { - $crate::no_stdio_internal_error($errcode) + $crate::handle_assert_failure($errcode) } - }}; + }; } -#[cfg(not(feature = "stdio"))] -pub(crate) fn no_stdio_internal_error(errcode: c_int) { - extern "C" { - fn bz_internal_error(errcode: c_int); +#[cold] +fn handle_assert_failure(errcode: c_int) -> ! { + #[cfg(feature = "stdio")] + { + #[cfg(feature = "std")] + std::eprint!("{}", AssertFail(errcode)); + #[cfg(feature = "std")] + std::process::exit(3); + + #[cfg(not(feature = "std"))] + panic!("{}", AssertFail(errcode)); } - unsafe { bz_internal_error(errcode) } + #[cfg(not(feature = "stdio"))] + { + extern "C" { + fn bz_internal_error(errcode: c_int); + } + + unsafe { bz_internal_error(errcode) } + loop {} + } } -pub(crate) use assert_h; +use assert_h; -pub(crate) struct AssertFail(i32); +struct AssertFail(i32); impl core::fmt::Display for AssertFail { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {