From 69535280c6ac2d19e03ecb11034b36c806df2f5d Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Thu, 29 Aug 2024 16:35:42 +0200 Subject: [PATCH] fix: issue while creating statically linked binary that depends on `musl` instead of `glibc`. Closes #181 --- .cargo/config.toml | 20 ++++++++++++++++++++ lib/src/wasm/mod.rs | 12 ++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..35f09c87 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,20 @@ +# Special configuration for linking with musl instead of glibc. +# See: https://github.com/VirusTotal/yara-x/issues/181 +# +[target.x86_64-unknown-linux-musl] +# Relocation model is "pic" by default (i.e: position independent code), +# Here we force the "static" (i.e: non-relocatable code) model. Without this +# the produced binaries crash with SIGSEGV as described in: +# https://github.com/rust-lang/rust/issues/74757 +# https://github.com/rust-lang/rust/issues/81987 +rustflags = ["-C", "relocation-model=static"] +# This is not necessary when building from a host where gcc already links +# against musl by default, and it can be commented out (e.g: when building +# from a Alpine Linux host). But in most cases what we want is cross-compiling +# a host with a standard gcc that links against glibc. In such cases from we +# must link using musl-gcc. +# +# In Debian/Ubuntu you need to install the musl-tools package in order to +# build YARA-X with musl. This package contains musl-gcc, which is a gcc +# wrapper that creates binaries that depends on musl instead of glibc. +linker = "musl-gcc" diff --git a/lib/src/wasm/mod.rs b/lib/src/wasm/mod.rs index 83ed643b..45eb3f41 100644 --- a/lib/src/wasm/mod.rs +++ b/lib/src/wasm/mod.rs @@ -707,8 +707,20 @@ pub(crate) struct WasmSymbols { lazy_static! { pub(crate) static ref CONFIG: Config = { let mut config = Config::default(); + // Wasmtime produces a nasty warning when linked against musl. The + // warning can be fixed by disabling native unwind information. + // + // More details: + // + // https://github.com/bytecodealliance/wasmtime/issues/8897 + // https://github.com/VirusTotal/yara-x/issues/181 + // + #[cfg(target_env = "musl")] + config.native_unwind_info(false); + config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); config.epoch_interruption(true); + config }; pub(crate) static ref ENGINE: Engine = Engine::new(&CONFIG).unwrap();