Skip to content

Commit 8fea9cd

Browse files
authored
Merge pull request #212 from danoli3/fix/211-jxl-mingw-crash
Fix JPEG XL crash with upstream MinGW GCC (#211)
2 parents 82a2ac3 + 903e9c7 commit 8fea9cd

11 files changed

Lines changed: 165 additions & 3 deletions

File tree

‎.github/workflows/bundled-codecs.yml‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,81 @@ jobs:
7878
run: cmake --build build-runtime --config ${{ matrix.config }}
7979
- name: Run tests
8080
run: ctest --test-dir build-runtime -C ${{ matrix.config }} --output-on-failure
81+
windows-mingw-gcc:
82+
# Issue #211: GCC targeting Windows cannot realign the stack for AVX
83+
# spills, so libjxl's AVX2 decoder crashed under MinGW GCC while MSVC,
84+
# clang and Linux GCC were fine. Debug matters: -O0 spills every vector
85+
# temporary, which is where the reported crash came from.
86+
# This must be an upstream GCC. MSYS2 patches its GCC to default to
87+
# unaligned vector moves (0200-add-m-no-align-vector-insn-option-for-i386),
88+
# so the windows-msys2-gcc job below cannot reproduce the bug. The
89+
# runner's preinstalled C:\mingw64 is unpatched mingw-builds GCC, like
90+
# the WinLibs/mingw-builds toolchains users download.
91+
name: Windows MinGW GCC bundled codecs (${{ matrix.config }})
92+
runs-on: windows-latest
93+
strategy:
94+
fail-fast: false
95+
matrix:
96+
config: [Debug, Release]
97+
defaults:
98+
run:
99+
shell: pwsh
100+
env:
101+
MINGW: C:/mingw64/bin
102+
steps:
103+
- uses: actions/checkout@v4
104+
- name: Show the upstream GCC in use
105+
run: |
106+
& "$env:MINGW/gcc.exe" --version
107+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
108+
& "$env:MINGW/gcc.exe" -Q --help=target | Select-String "align-vector"
109+
"$env:MINGW" | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
110+
"CMAKE_BUILD_PARALLEL_LEVEL=$([Environment]::ProcessorCount)" >> $env:GITHUB_ENV
111+
- name: Configure bundled codecs
112+
run: >-
113+
cmake -S . -B build -G "MinGW Makefiles"
114+
"-DCMAKE_C_COMPILER=$env:MINGW/gcc.exe" "-DCMAKE_CXX_COMPILER=$env:MINGW/g++.exe"
115+
-DCMAKE_BUILD_TYPE=${{ matrix.config }} -DCMAKE_CXX_STANDARD=17
116+
-DBUILD_TESTS=ON -DFREEIMAGE_STATIC=OFF
117+
-DBUILD_LIBHEIF=ON -DBUILD_LIBJXL=ON
118+
-DUSE_SYSTEM_LIBHEIF=OFF -DUSE_SYSTEM_LIBJXL=OFF
119+
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
120+
- name: Build
121+
run: cmake --build build
122+
- name: Test
123+
run: ctest --test-dir build --output-on-failure --timeout 300
124+
windows-msys2-gcc:
125+
# Build coverage for MSYS2's own GCC with the bundled codecs. See the job
126+
# above for why this one cannot catch #211.
127+
name: Windows MSYS2 GCC bundled codecs (MINGW64, Debug, static)
128+
runs-on: windows-latest
129+
defaults:
130+
run:
131+
shell: msys2 {0}
132+
steps:
133+
- uses: actions/checkout@v4
134+
- name: Set up MSYS2 (MINGW64)
135+
uses: msys2/setup-msys2@v2
136+
with:
137+
msystem: MINGW64
138+
update: true
139+
install: >-
140+
mingw-w64-x86_64-toolchain
141+
mingw-w64-x86_64-cmake
142+
mingw-w64-x86_64-ninja
143+
perl
144+
- name: Configure bundled codecs
145+
run: >-
146+
cmake -S . -B build -G Ninja
147+
-DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_STANDARD=17
148+
-DBUILD_TESTS=ON -DFREEIMAGE_STATIC=ON
149+
-DBUILD_LIBHEIF=ON -DBUILD_LIBJXL=ON
150+
-DUSE_SYSTEM_LIBHEIF=OFF -DUSE_SYSTEM_LIBJXL=OFF
151+
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
152+
- name: Build
153+
run: cmake --build build -j$(nproc)
154+
- name: Test
155+
run: ctest --test-dir build --output-on-failure --timeout 300
81156
linux-x86:
82157
runs-on: ubuntu-24.04
83158
steps:

‎CMakeLists.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ elseif(NOT FREEIMAGE_MSVC_RUNTIME STREQUAL "Default")
6161
message(STATUS "FREEIMAGE_MSVC_RUNTIME is only applied by MSVC-compatible toolchains")
6262
endif()
6363

64+
# GCC targeting Windows cannot realign the stack past 16 bytes (GCC bug
65+
# 54412), yet still spills AVX/AVX-512 registers with aligned vmovaps/vmovdqa.
66+
# Runtime-dispatched AVX2 code (Highway/libjxl, LibWebP, OpenEXR) then crashes
67+
# whenever a frame lands off a 32-byte boundary - issue #211, which was
68+
# jxl::N_AVX2::UpsamplingStage's constructor in an unoptimized build. Have the
69+
# assembler emit unaligned moves instead, as MSYS2 does for all its packages.
70+
# Set before the bundled codecs, which inherit CMAKE_C_FLAGS/CMAKE_CXX_FLAGS.
71+
if(MINGW AND CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
72+
include(CheckCSourceCompiles)
73+
set(CMAKE_REQUIRED_FLAGS "-Wa,-muse-unaligned-vector-move")
74+
check_c_source_compiles("int main(void) { return 0; }" FREEIMAGE_HAVE_UNALIGNED_VECTOR_MOVE)
75+
unset(CMAKE_REQUIRED_FLAGS)
76+
if(FREEIMAGE_HAVE_UNALIGNED_VECTOR_MOVE)
77+
string(APPEND CMAKE_C_FLAGS " -Wa,-muse-unaligned-vector-move")
78+
string(APPEND CMAKE_CXX_FLAGS " -Wa,-muse-unaligned-vector-move")
79+
else()
80+
message(WARNING "This MinGW assembler lacks -muse-unaligned-vector-move "
81+
"(binutils 2.38+). AVX2 code paths may crash on misaligned stack frames.")
82+
endif()
83+
endif()
84+
6485
set(FREEIMAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
6586
set(FREEIMAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
6687
set(FREEIMAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})

‎TestAPI/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ if(BUILD_TESTS AND BUILD_LIBJXL)
148148
add_custom_command(TARGET JpegXLTest POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
149149
$<TARGET_FILE:FreeImage> $<TARGET_FILE_DIR:JpegXLTest>)
150150
endif()
151-
add_test(NAME JpegXLTest COMMAND JpegXLTest)
151+
add_test(NAME JpegXLTest COMMAND JpegXLTest "${CMAKE_CURRENT_SOURCE_DIR}/jxl")
152152
set_tests_properties(JpegXLTest PROPERTIES TIMEOUT 60)
153153
endif()
154154

‎TestAPI/jxl/README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# JPEG XL decode fixtures
2+
3+
`JpegXLTest` decodes these and compares them with `reference.png` (a 60x54
4+
RGBA downscale of `TestAPI/sample.png`). They cover decoder paths that the
5+
lossless roundtrips in `testJpegXL.cpp` do not: VarDCT, progressive DC and
6+
the SIMD upsampling stages (issue #211).
7+
8+
Generated with `cjxl` 0.12.0:
9+
10+
```sh
11+
sips -z 54 60 ../sample.png --out reference.png
12+
cjxl reference.png lossy.jxl -d 1 -e 3
13+
cjxl reference.png upsampled2x.jxl -d 1 -e 3 --resampling=2 --ec_resampling=2
14+
cjxl reference.png upsampled4x.jxl -d 2 -e 3 --resampling=4 --ec_resampling=4
15+
cjxl reference.png alpha-upsampled.jxl -d 1 -e 3 --resampling=1 --ec_resampling=2
16+
cjxl reference.png progressive.jxl -d 1 -e 3 -p --progressive_dc=1
17+
```
18+
19+
If you regenerate them, recheck the error thresholds in `testJpegXL.cpp`.

‎TestAPI/jxl/alpha-upsampled.jxl‎

3.84 KB
Binary file not shown.

‎TestAPI/jxl/lossy.jxl‎

5.17 KB
Binary file not shown.

‎TestAPI/jxl/progressive.jxl‎

6.03 KB
Binary file not shown.

‎TestAPI/jxl/reference.png‎

9.75 KB
Loading

‎TestAPI/jxl/upsampled2x.jxl‎

2.37 KB
Binary file not shown.

‎TestAPI/jxl/upsampled4x.jxl‎

1.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)