W32 - GTK4 crashes due to the use of SSE2 with unaligned memory
@ruslanizhb
Submitted by LRN Link to original bug (#784956)
Description
GTK4 compiles, but crashes reliably. Investigation shows that the culprit is SSE2-enabled graphene library, which tries to operate on __m128 values stored in memory that is not aligned to 16-byte boundaries.
There are two cases of this crash.
One is the unaligned stack-allocated graphene variables. This happens because on i686 Windows the stack is not 16-aligned, and/or this alignment is not preserved across W32 API calls (thus, any callbacks will break the alignment; gcc doesn't do any runtime alignment checks and assumes that the alignment is the same everywhere, it's faster that way). This can be fixed by compiling with -mstackrealign or by explicitly aligning the stack of each function that allocates any variable that is used with SIMD instructions.
I have no knowledge of x86_64 Windows stack alignment. It's possible that it's sufficiently aligned and does not require -mstackrealign (or explicit alignment modifiers), but i have no way to test this and be 100%-sure.
Another is the unaligned heap-allocated graphene variables. This happens because i686 MS CRT malloc() (and therefore g_malloc()) does not align the memory it allocates to 16-byte boundaries (x86_64 MS CRT malloc() does).
Since it might not be desirable to add aligned memory allocation functions to glib (see bug 784870), these functions could be added to GSK or GDK, and used privately by GSK and GTK. There are, so far, only two known instances of this kind of crash: GskRenderNode and GtkSnapshot.
Version: 3.91.x