From 88927fa935f085cdd071480d161ffd5dbecb86e6 Mon Sep 17 00:00:00 2001 From: Inflation <2375962+inflation@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:46:07 +0800 Subject: [PATCH] ci: Enable ASan on `libjxl` --- .github/workflows/ci.yml | 55 ++++++++-------------------- jpegxl-rs/src/encode/options.rs | 2 +- jpegxl-src/Cargo.toml | 5 ++- jpegxl-src/build.rs | 5 --- jpegxl-src/src/lib.rs | 65 +++++++++++++++++++++++++-------- jpegxl-sys/Cargo.toml | 2 +- jpegxl-sys/build.rs | 12 +++--- 7 files changed, 77 insertions(+), 69 deletions(-) delete mode 100644 jpegxl-src/build.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 89f4b06..a1ca0ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] include: - os: macos-13 - rustflags: "-Clink-args=-fapple-link-rtlib" + # rustflags: "-Clink-args=-fapple-link-rtlib" runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 @@ -31,18 +31,13 @@ jobs: uses: dtolnay/rust-toolchain@stable - name: Install cargo-nextest uses: taiki-e/install-action@nextest - - name: Dependency - run: cargo fetch - - uses: actions/cache@v4 - id: cache + - uses: Leafwing-Studios/cargo-cache@v2 with: - path: | - target/ - key: ${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }} + sweep-cache: true - name: Tests run: cargo nextest run --all-features --workspace - env: - RUSTFLAGS: "${{ matrix.rustflags }}" + # env: + # RUSTFLAGS: "${{ matrix.rustflags }}" coverage: name: Code Coverage @@ -60,14 +55,9 @@ jobs: uses: taiki-e/install-action@cargo-llvm-cov - name: Install cargo-nextest uses: taiki-e/install-action@nextest - - name: Dependency - run: cargo fetch - - uses: actions/cache@v4 - id: cache + - uses: Leafwing-Studios/cargo-cache@v2 with: - path: | - target/ - key: ${{ runner.os }}-cargo-cov-${{ hashFiles('**/Cargo.lock') }} + sweep-cache: true - name: Generate code coverage run: cargo +nightly llvm-cov nextest --all-features --workspace @@ -98,20 +88,15 @@ jobs: components: rust-src - name: Install cargo-nextest uses: taiki-e/install-action@nextest - - name: Dependency - run: cargo fetch - - uses: actions/cache@v4 - id: cache + - uses: Leafwing-Studios/cargo-cache@v2 with: - path: | - target/ - key: ${{ runner.os }}-cargo-sanitizer-${{ hashFiles('**/Cargo.lock') }} + sweep-cache: true - name: Test with Sanitizer run: cargo +nightly nextest run -Z build-std --target=x86_64-unknown-linux-gnu --all-features env: - RUSTFLAGS: "-Zsanitizer=${{matrix.sanitizer}}" - RUSTDOCFLAGS: "-Zsanitizer=${{matrix.sanitizer}}" + RUSTFLAGS: "-Zsanitizer=${{matrix.sanitizer}} --cfg=asan" + RUSTDOCFLAGS: "-Zsanitizer=${{matrix.sanitizer}} --cfg=asan" msrv: runs-on: ubuntu-latest @@ -119,14 +104,9 @@ jobs: - uses: actions/checkout@v4 with: submodules: recursive - - name: Dependency - run: cargo fetch - - uses: actions/cache@v4 - id: cache + - uses: Leafwing-Studios/cargo-cache@v2 with: - path: | - target/ - key: ${{ runner.os }}-cargo-msrv-${{ hashFiles('**/Cargo.lock') }} + sweep-cache: true - uses: taiki-e/install-action@cargo-hack - run: cargo hack check --rust-version --workspace --all-targets --ignore-private --all-features @@ -140,14 +120,9 @@ jobs: submodules: recursive - name: Install Rust run: rustup update stable - - name: Dependency - run: cargo fetch - - uses: actions/cache@v4 - id: cache + - uses: Leafwing-Studios/cargo-cache@v2 with: - path: | - target/ - key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }} + sweep-cache: true - name: Run Clippy with reviewdog uses: giraffate/clippy-action@v1.0.1 with: diff --git a/jpegxl-rs/src/encode/options.rs b/jpegxl-rs/src/encode/options.rs index d56138d..f7b5de1 100644 --- a/jpegxl-rs/src/encode/options.rs +++ b/jpegxl-rs/src/encode/options.rs @@ -59,7 +59,7 @@ impl From for JxlColorEncoding { api::JxlColorEncodingSetToLinearSRGB(color_encoding.as_mut_ptr(), false.into()); } SrgbLuma => { - api::JxlColorEncodingSetToSRGB(color_encoding.as_mut_ptr(), true.into()) + api::JxlColorEncodingSetToSRGB(color_encoding.as_mut_ptr(), true.into()); } LinearSrgbLuma => { api::JxlColorEncodingSetToLinearSRGB(color_encoding.as_mut_ptr(), true.into()); diff --git a/jpegxl-src/Cargo.toml b/jpegxl-src/Cargo.toml index 83352e3..05dfde9 100644 --- a/jpegxl-src/Cargo.toml +++ b/jpegxl-src/Cargo.toml @@ -35,7 +35,10 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] } +unexpected_cfgs = { level = "warn", check-cfg = [ + 'cfg(coverage_nightly)', + 'cfg(asan)', +] } [lints.clippy] pedantic = "warn" diff --git a/jpegxl-src/build.rs b/jpegxl-src/build.rs deleted file mode 100644 index 7c24c19..0000000 --- a/jpegxl-src/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let source = std::path::Path::new("libjxl"); - assert!(source.exists()); - assert!(source.is_dir()); -} diff --git a/jpegxl-src/src/lib.rs b/jpegxl-src/src/lib.rs index d46813e..a4ca6d6 100644 --- a/jpegxl-src/src/lib.rs +++ b/jpegxl-src/src/lib.rs @@ -30,8 +30,18 @@ fn source_dir() -> PathBuf { } #[cfg_attr(coverage_nightly, coverage(off))] + +/// Builds the JPEG XL library. +/// +/// # Panics +/// +/// This function will panic if the source directory does not exist or is not a directory. pub fn build() { let source = source_dir(); + assert!( + source.exists() && source.is_dir(), + "Source directory {source:?} does not exist" + ); let mut config = cmake::Config::new(source); config @@ -52,8 +62,23 @@ pub fn build() { config.env("CMAKE_BUILD_PARALLEL_LEVEL", format!("{p}")); } - #[cfg(target_os = "windows")] - { + if cfg!(asan) { + config + .env("SANITIZER", "asan") + .cflag("-g -DADDRESS_SANITIZER -fsanitize=address") + .cxxflag("-g -DADDRESS_SANITIZER -fsanitize=address") + .define("JPEGXL_ENABLE_TCMALLOC", "OFF"); + } + + if cfg!(windows) { + // For CMake pre-checking + let mut exeflags = "MSVCRTD.lib".to_string(); + if cfg!(asan) { + exeflags.push_str( + " clang_rt.asan_dynamic-x86_64.lib clang_rt.asan_dynamic_runtime_thunk-x86_64.lib", + ); + } + config .generator_toolset("ClangCL") .define( @@ -61,16 +86,29 @@ pub fn build() { "UseMultiToolTask=true;EnforceProcessCountAcrossBuilds=true", ) // Enable parallel builds .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded") - .define("CMAKE_EXE_LINKER_FLAGS", "MSVCRTD.lib") - .cflag("-Zl"); + .define("CMAKE_EXE_LINKER_FLAGS", exeflags) + .cflag("/Zl"); } - let mut prefix = config.build(); - prefix.push("lib"); - println!("cargo:rustc-link-search=native={}", prefix.display()); - prefix.pop(); - prefix.push("lib64"); - println!("cargo:rustc-link-search=native={}", prefix.display()); + let prefix = config.build(); + + let lib_dir = { + let mut lib_dir = prefix.join("lib"); + if lib_dir.exists() { + lib_dir + } else { + lib_dir.pop(); + lib_dir.push("lib64"); + if lib_dir.exists() { + lib_dir + } else { + panic!( + "Could not find the library directory, please check the files in {prefix:?}" + ); + } + } + }; + println!("cargo:rustc-link-search=native={}", lib_dir.display()); println!("cargo:rustc-link-lib=static=jxl"); println!("cargo:rustc-link-lib=static=jxl_cms"); @@ -81,12 +119,9 @@ pub fn build() { println!("cargo:rustc-link-lib=static=brotlienc"); println!("cargo:rustc-link-lib=static=brotlicommon"); - #[cfg(any(target_vendor = "apple", target_os = "freebsd"))] - { + if cfg!(any(target_vendor = "apple", target_os = "freebsd")) { println!("cargo:rustc-link-lib=c++"); - } - #[cfg(target_os = "linux")] - { + } else if cfg!(target_os = "linux") { println!("cargo:rustc-link-lib=stdc++"); } } diff --git a/jpegxl-sys/Cargo.toml b/jpegxl-sys/Cargo.toml index 03896d2..809e5de 100644 --- a/jpegxl-sys/Cargo.toml +++ b/jpegxl-sys/Cargo.toml @@ -41,5 +41,5 @@ pretty_assertions = "1.4.1" [features] default = [] -vendored = ["jpegxl-src"] +vendored = ["dep:jpegxl-src"] docs = [] diff --git a/jpegxl-sys/build.rs b/jpegxl-sys/build.rs index 803c34b..31620cb 100644 --- a/jpegxl-sys/build.rs +++ b/jpegxl-sys/build.rs @@ -17,10 +17,10 @@ //! Build script for jpegxl-sys. +use std::env; + fn main() { - #[cfg(all(not(feature = "vendored"), not(feature = "docs")))] - { - use std::env; + if cfg!(all(not(feature = "vendored"), not(feature = "docs"))) { let version = env!("CARGO_PKG_VERSION") .split('+') .nth(1) @@ -43,8 +43,8 @@ fn main() { panic!("Cannot find `libjxl_threads` with version >= {version}") }); } + } else { + #[cfg(feature = "vendored")] + jpegxl_src::build(); } - - #[cfg(feature = "vendored")] - jpegxl_src::build(); }