Merge pull request 'fix(windows): ensure memset_explicit symbol is properly exported for MinGW' (#119) from fix/cargo-config-sodium into beta
Some checks failed
Release Beta / autotag (push) Successful in 9s
Release Beta / changelog (push) Successful in 1m32s
Test / frontend-tests (push) Successful in 1m46s
Test / frontend-typecheck (push) Successful in 1m57s
Release Beta / build-macos-arm64 (push) Successful in 8m52s
Release Beta / build-linux-amd64 (push) Successful in 11m41s
Release Beta / build-windows-amd64 (push) Failing after 11m49s
Release Beta / build-linux-arm64 (push) Successful in 14m11s
Test / rust-fmt-check (push) Failing after 18m49s
Test / rust-clippy (push) Successful in 20m19s
Test / rust-tests (push) Successful in 22m38s

Reviewed-on: #119
This commit is contained in:
sarman 2026-06-19 14:54:49 +00:00
commit 036af81147
2 changed files with 25 additions and 7 deletions

View File

@ -6,14 +6,19 @@ fn main() {
println!("cargo:rerun-if-changed=.git/refs/tags"); println!("cargo:rerun-if-changed=.git/refs/tags");
// Compile memset_explicit shim for Windows MinGW // Compile memset_explicit shim for Windows MinGW
if std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "windows" // libsodium-sys-stable uses memset_explicit which isn't available in MinGW
&& std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default() == "gnu" 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() cc::Build::new()
.file("memset_s_shim.c") .file("memset_s_shim.c")
.define("WIN32", None)
.define("__WIN32__", None)
.compile("memset_shim"); .compile("memset_shim");
println!("cargo:rerun-if-changed=memset_s_shim.c"); 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"); println!("cargo:rustc-link-lib=static=memset_shim");
} }

View File

@ -2,16 +2,29 @@
* *
* libsodium-sys-stable expects memset_explicit which isn't available * libsodium-sys-stable expects memset_explicit which isn't available
* in the MinGW runtime. This provides a compatible implementation. * 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 <string.h> #include <string.h>
#include <stddef.h>
#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 #else
#define EXPORT #define EXPORT __attribute__((visibility("default")))
#endif #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) { EXPORT void memset_explicit(void *dest, int val, size_t n) {
volatile unsigned char *p = (volatile unsigned char *)dest; volatile unsigned char *p = (volatile unsigned char *)dest;
while (n--) { while (n--) {