From 4cdfe1f65507e3647b37f11c19202c99cce11f77 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sun, 25 Jun 2023 18:06:41 -0700 Subject: [PATCH] Align stack when entering wimlib on 32-bit x86 This may resolve https://wimlib.net/forums/viewtopic.php?t=669. The reported crash happened in sha1_blocks_x86_avx_bmi2() on the first instruction that writes to the stack with vmovqda. That suggests the stack was misaligned. If this indeed fixes the bug, then it has actually always existed but it got exposed by new code in v1.14.0. --- include/wimlib.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/wimlib.h b/include/wimlib.h index 6c68865f..ef08e96e 100644 --- a/include/wimlib.h +++ b/include/wimlib.h @@ -401,10 +401,23 @@ #include #ifdef BUILDING_WIMLIB + /* + * On i386, gcc assumes that the stack is 16-byte aligned at function entry. + * However, some compilers (e.g. MSVC) and programming languages (e.g. Delphi) + * only guarantee 4-byte alignment when calling functions. This is mainly an + * issue on Windows, but it can occur on Linux too. Work around this ABI + * incompatibility by realigning the stack pointer when entering the library. + * This prevents crashes in SSE/AVX code. + */ +# if defined(__GNUC__) && defined(__i386__) +# define WIMLIB_ALIGN_STACK __attribute__((force_align_arg_pointer)) +# else +# define WIMLIB_ALIGN_STACK +# endif # ifdef _WIN32 -# define WIMLIBAPI __declspec(dllexport) +# define WIMLIBAPI __declspec(dllexport) WIMLIB_ALIGN_STACK # else -# define WIMLIBAPI __attribute__((visibility("default"))) +# define WIMLIBAPI __attribute__((visibility("default"))) WIMLIB_ALIGN_STACK # endif #else # define WIMLIBAPI -- 2.43.0