Skip to content

Fix JPEG XL crash with upstream MinGW GCC (#211) - #212

Merged
danoli3 merged 4 commits into
masterfrom
fix/211-jxl-mingw-crash
Oct 26, 2026
Merged

danoli3 merged 4 commits into
masterfrom
fix/211-jxl-mingw-crash

Conversation

@danoli3

@danoli3 danoli3 commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Fixes #211.

Cause

GCC targeting 64-bit Windows cannot realign the stack past 16 bytes (GCC bug 54412), but it still spills AVX registers with aligned vmovaps/vmovdqa. Those instructions fault when the stack slot is not 32-byte aligned.

libjxl builds an 8x upsampling stage on every decode (PassesDecoderState::Init, dec_cache.h:174), so every JPEG XL load runs jxl::N_AVX2::UpsamplingStage's constructor. Built with mingw GCC at -O0, that constructor stores to -192(%rbp) with vmovdqa, and %rbp there is only 16-byte aligned. Optimized builds have fewer such stores, but still crash.

Fix

On MinGW GCC, CMake passes -Wa,-muse-unaligned-vector-move (binutils 2.38+) after checking the assembler accepts it. The assembler then emits unaligned moves, which are correct at any alignment. It applies to FreeImage's own sources (LibWebP and OpenEXR also have AVX2 paths) and to every bundled codec through CMAKE_C_FLAGS/CMAKE_CXX_FLAGS. On stage_upsampling.cc this takes the aligned vector moves from 5765 to 0.

Why CI did not catch it

  • No job built the bundled codecs with MinGW GCC.
  • MSYS2's GCC is patched to default to unaligned vector moves (0200-add-m-no-align-vector-insn-option-for-i386.patch), so MSYS2 jobs cannot reproduce this. The first version of the new job used MSYS2 and passed without the fix.
  • Stack alignment inside the decoder depends only on the caller's depth, so repeating a decode from one call site never changes it.

Changes

  • CMakeLists.txt: the assembler flag for MinGW GCC.
  • bundled-codecs.yml: windows-mingw-gcc builds the bundled codecs with the runner's unpatched mingw-builds GCC 15.2 (C:\mingw64), Debug and Release. windows-msys2-gcc stays as build coverage. ctest stops any test after 300 s.
  • TestAPI/jxl/: lossy, progressive, 2x/4x upsampled and alpha-upsampled fixtures with a reference PNG and a README giving the cjxl commands.
  • JpegXLTest decodes the fixtures with the stack offset in 16-byte steps, and checks alpha-weighted error against the reference.

Verification

Run Commit Upstream GCC Debug Upstream GCC Release
40316312424 95bdd7a, no fix JpegXLTest SegFault JpegXLTest SegFault
40317425254 a41c2df, fix pass pass

All 18 jobs in the fixed run pass.

Possibly related

cmake.yml's windows-mingw job uses the same unpatched toolchain and forces BUILD_OPENEXR=OFF because the test binary hung before main(). MSYS2 never hit that hang. It may be the same bug, but the new job builds OpenEXR and did not hang even without the fix, so this PR leaves that workaround alone.

Issue #211 crashed in libjxl's AVX2 upsampling stage on MinGW GCC, and no
CI job built the bundled codecs with that toolchain. Add a windows-mingw-gcc
job (UCRT64 Debug/Release, MINGW64 Debug static). Add lossy, progressive and
upsampled JXL fixtures so JpegXLTest also runs VarDCT and the upsampling
stages' SIMD row code.
The first MinGW GCC run passed without the #211 fix: alignment inside the
decoder depends only on the caller's stack depth, so repeating a decode from
the same frame repeats the same alignment. Offset the stack by 16-byte steps
so the fixtures cover every 32- and 64-byte alignment class.
MSYS2 patches its GCC to default to unaligned vector moves
(0200-add-m-no-align-vector-insn-option-for-i386), so the MSYS2 jobs passed
without the fix. Build the bundled codecs with the preinstalled mingw-builds
GCC in C:\mingw64 for Debug and Release, and keep one MSYS2 job as build
coverage. Cap ctest at 300 s per test so a hang fails fast.
GCC targeting Windows cannot realign the stack past 16 bytes (GCC bug
54412) but still spills AVX registers with vmovaps/vmovdqa. libjxl's
N_AVX2::UpsamplingStage constructor, built at -O0, stored to a 16-byte
aligned slot and faulted. Pass -Wa,-muse-unaligned-vector-move when the
assembler supports it, as MSYS2 does for its packages. The bundled codecs
inherit it through CMAKE_C_FLAGS/CMAKE_CXX_FLAGS.
@danoli3
danoli3 force-pushed the fix/211-jxl-mingw-crash branch from a41c2df to 903e9c7 Compare October 26, 2026 12:31
@danoli3
danoli3 merged commit 8fea9cd into master Oct 26, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3.19.27 libFreeImage crashes

1 participant