From 5c4fb0580262ff7c11c468bfccc8344402b3c88a Mon Sep 17 00:00:00 2001 From: Shaun Arman Date: Fri, 19 Jun 2026 09:44:17 -0500 Subject: [PATCH] fix(windows): ensure memset_explicit symbol is properly exported for MinGW - Add __MINGW32__ detection in memset_s_shim.c for proper EXPORT macro - Add WIN32 and __WIN32__ defines when compiling the shim in build.rs - The memset_explicit symbol is required by libsodium-sys-stable but not available in MinGW runtime - The shim is compiled as a static library and linked before libsodium --- src-tauri/build.rs | 13 +++++++++---- src-tauri/memset_s_shim.c | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src-tauri/build.rs b/src-tauri/build.rs index 595b3cc8..ec9a663f 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -6,14 +6,19 @@ fn main() { println!("cargo:rerun-if-changed=.git/refs/tags"); // Compile memset_explicit shim for Windows MinGW - if std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "windows" - && std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default() == "gnu" - { + // libsodium-sys-stable uses memset_explicit which isn't available in MinGW + let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); + let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); + + if target_os == "windows" && target_env == "gnu" { cc::Build::new() .file("memset_s_shim.c") + .define("WIN32", None) + .define("__WIN32__", None) .compile("memset_shim"); println!("cargo:rerun-if-changed=memset_s_shim.c"); - // Explicitly link the shim library + // Link the shim library - must be before libsodium in link order + // The shim provides memset_explicit which libsodium expects println!("cargo:rustc-link-lib=static=memset_shim"); } diff --git a/src-tauri/memset_s_shim.c b/src-tauri/memset_s_shim.c index 864ab8ae..ba35dc89 100644 --- a/src-tauri/memset_s_shim.c +++ b/src-tauri/memset_s_shim.c @@ -2,16 +2,29 @@ * * libsodium-sys-stable expects memset_explicit which isn't available * in the MinGW runtime. This provides a compatible implementation. + * + * This shim is compiled as part of the build process when targeting + * Windows with MinGW (x86_64-w64-mingw32). */ #include +#include -#ifdef _WIN32 -#define EXPORT __declspec(dllexport) +/* + * Windows symbol export macros. + * __declspec(dllexport) ensures the symbol is visible in the compiled object. + */ +#if defined(_WIN32) || defined(__WIN32__) || defined(__MINGW32__) + #define EXPORT __declspec(dllexport) #else -#define EXPORT + #define EXPORT __attribute__((visibility("default"))) #endif +/* + * memset_explicit implementation - a secure memory clearing function. + * Uses volatile to prevent compiler optimization from removing the memset. + * This matches the signature expected by libsodium. + */ EXPORT void memset_explicit(void *dest, int val, size_t n) { volatile unsigned char *p = (volatile unsigned char *)dest; while (n--) { -- 2.45.2