]> wimlib.net Git - wimlib/commitdiff
Align stack when entering wimlib on 32-bit x86
authorEric Biggers <ebiggers3@gmail.com>
Mon, 26 Jun 2023 01:06:41 +0000 (18:06 -0700)
committerEric Biggers <ebiggers3@gmail.com>
Mon, 26 Jun 2023 01:11:21 +0000 (18:11 -0700)
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

index 6c68865f1a7e7e8b54b8e42812aa21ca0ebac6f9..ef08e96e5ac1952e5ad6fce0f53973bce7362bf0 100644 (file)
 #include <time.h>
 
 #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