Skip to content

Commit 3bd754e

Browse files
committed
Merge tag '2.1.5.1'
# By DRC # Via DRC * tag '2.1.5.1': ChangeLog.md: Document d743a2c OSS-Fuzz: Bail out immediately on decomp failure SIMD/x86: Initialize simd_support before every use Build: Define THREAD_LOCAL even if !WITH_TURBOJPEG # Conflicts: # CMakeLists.txt
2 parents af265e7 + 8ecba36 commit 3bd754e

7 files changed

Lines changed: 156 additions & 20 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/ci export-ignore
44
/.gitattributes export-ignore
55
*.ppm binary
6+
/ChangeLog.md conflict-marker-size=8

CMakeLists.txt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if(CMAKE_EXECUTABLE_SUFFIX)
1010
endif()
1111

1212
project(mozjpeg C)
13-
set(VERSION 4.1.3)
13+
set(VERSION 4.1.4)
1414
set(COPYRIGHT_YEAR "1991-2023")
1515
string(REPLACE "." ";" VERSION_TRIPLET ${VERSION})
1616
list(GET VERSION_TRIPLET 0 VERSION_MAJOR)
@@ -496,19 +496,17 @@ if(NOT INLINE_WORKS)
496496
endif()
497497
message(STATUS "INLINE = ${INLINE} (FORCE_INLINE = ${FORCE_INLINE})")
498498

499-
if(WITH_TURBOJPEG)
500-
if(MSVC)
501-
set(THREAD_LOCAL "__declspec(thread)")
502-
else()
503-
set(THREAD_LOCAL "__thread")
504-
endif()
505-
check_c_source_compiles("${THREAD_LOCAL} int i; int main(void) { i = 0; return i; }" HAVE_THREAD_LOCAL)
506-
if(HAVE_THREAD_LOCAL)
507-
message(STATUS "THREAD_LOCAL = ${THREAD_LOCAL}")
508-
else()
509-
message(WARNING "Thread-local storage is not available. The TurboJPEG API library's global error handler will not be thread-safe.")
510-
unset(THREAD_LOCAL)
511-
endif()
499+
if(MSVC)
500+
set(THREAD_LOCAL "__declspec(thread)")
501+
else()
502+
set(THREAD_LOCAL "__thread")
503+
endif()
504+
check_c_source_compiles("${THREAD_LOCAL} int i; int main(void) { i = 0; return i; }" HAVE_THREAD_LOCAL)
505+
if(HAVE_THREAD_LOCAL)
506+
message(STATUS "THREAD_LOCAL = ${THREAD_LOCAL}")
507+
else()
508+
message(WARNING "Thread-local storage is not available. The TurboJPEG API library's global error handler will not be thread-safe.")
509+
unset(THREAD_LOCAL)
512510
endif()
513511

514512
if(UNIX AND NOT APPLE)

ChangeLog.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
2.1.5.1
2+
=======
3+
4+
### Significant changes relative to 2.1.5:
5+
6+
1. The SIMD dispatchers in libjpeg-turbo 2.1.4 and prior stored the list of
7+
supported SIMD instruction sets in a global variable, which caused an innocuous
8+
race condition whereby the variable could have been initialized multiple times
9+
if `jpeg_start_*compress()` was called simultaneously in multiple threads.
10+
libjpeg-turbo 2.1.5 included an undocumented attempt to fix this race condition
11+
by making the SIMD support variable thread-local. However, that caused another
12+
issue whereby, if `jpeg_start_*compress()` was called in one thread and
13+
`jpeg_read_*()` or `jpeg_write_*()` was called in a second thread, the SIMD
14+
support variable was never initialized in the second thread. On x86 systems,
15+
this led the second thread to incorrectly assume that AVX2 instructions were
16+
always available, and when it attempted to use those instructions on older x86
17+
CPUs that do not support them, an illegal instruction error occurred. The SIMD
18+
dispatchers now ensure that the SIMD support variable is initialized before
19+
dispatching based on its value.
20+
21+
122
2.1.5
223
=====
324

fuzz/decompress.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C)2021 D. R. Commander. All Rights Reserved.
2+
* Copyright (C)2021, 2023 D. R. Commander. All Rights Reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -88,7 +88,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
8888
when using MemorySanitizer. */
8989
for (i = 0; i < w * h * tjPixelSize[pf]; i++)
9090
sum += dstBuf[i];
91-
}
91+
} else
92+
goto bailout;
9293

9394
free(dstBuf);
9495
dstBuf = NULL;

fuzz/decompress_yuv.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C)2021 D. R. Commander. All Rights Reserved.
2+
* Copyright (C)2021, 2023 D. R. Commander. All Rights Reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are met:
@@ -90,7 +90,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9090
when using MemorySanitizer. */
9191
for (i = 0; i < w * h * tjPixelSize[pf]; i++)
9292
sum += dstBuf[i];
93-
}
93+
} else
94+
goto bailout;
9495

9596
free(dstBuf);
9697
dstBuf = NULL;

simd/i386/jsimd.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* jsimd_i386.c
33
*
44
* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB
5-
* Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2022, D. R. Commander.
5+
* Copyright (C) 2009-2011, 2013-2014, 2016, 2018, 2022-2023, D. R. Commander.
66
* Copyright (C) 2015-2016, 2018, 2022, Matthieu Darbois.
77
*
88
* Based on the x86 SIMD extension for IJG JPEG library,
@@ -158,6 +158,9 @@ jsimd_rgb_ycc_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
158158
void (*sse2fct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
159159
void (*mmxfct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
160160

161+
if (simd_support == ~0U)
162+
init_simd();
163+
161164
switch (cinfo->in_color_space) {
162165
case JCS_EXT_RGB:
163166
avx2fct = jsimd_extrgb_ycc_convert_avx2;
@@ -217,6 +220,9 @@ jsimd_rgb_gray_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
217220
void (*sse2fct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
218221
void (*mmxfct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
219222

223+
if (simd_support == ~0U)
224+
init_simd();
225+
220226
switch (cinfo->in_color_space) {
221227
case JCS_EXT_RGB:
222228
avx2fct = jsimd_extrgb_gray_convert_avx2;
@@ -276,6 +282,9 @@ jsimd_ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
276282
void (*sse2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
277283
void (*mmxfct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
278284

285+
if (simd_support == ~0U)
286+
init_simd();
287+
279288
switch (cinfo->out_color_space) {
280289
case JCS_EXT_RGB:
281290
avx2fct = jsimd_ycc_extrgb_convert_avx2;
@@ -379,6 +388,9 @@ GLOBAL(void)
379388
jsimd_h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
380389
JSAMPARRAY input_data, JSAMPARRAY output_data)
381390
{
391+
if (simd_support == ~0U)
392+
init_simd();
393+
382394
if (simd_support & JSIMD_AVX2)
383395
jsimd_h2v2_downsample_avx2(cinfo->image_width, cinfo->max_v_samp_factor,
384396
compptr->v_samp_factor,
@@ -399,6 +411,9 @@ GLOBAL(void)
399411
jsimd_h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
400412
JSAMPARRAY input_data, JSAMPARRAY output_data)
401413
{
414+
if (simd_support == ~0U)
415+
init_simd();
416+
402417
if (simd_support & JSIMD_AVX2)
403418
jsimd_h2v1_downsample_avx2(cinfo->image_width, cinfo->max_v_samp_factor,
404419
compptr->v_samp_factor,
@@ -461,6 +476,9 @@ GLOBAL(void)
461476
jsimd_h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
462477
JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
463478
{
479+
if (simd_support == ~0U)
480+
init_simd();
481+
464482
if (simd_support & JSIMD_AVX2)
465483
jsimd_h2v2_upsample_avx2(cinfo->max_v_samp_factor, cinfo->output_width,
466484
input_data, output_data_ptr);
@@ -476,6 +494,9 @@ GLOBAL(void)
476494
jsimd_h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
477495
JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
478496
{
497+
if (simd_support == ~0U)
498+
init_simd();
499+
479500
if (simd_support & JSIMD_AVX2)
480501
jsimd_h2v1_upsample_avx2(cinfo->max_v_samp_factor, cinfo->output_width,
481502
input_data, output_data_ptr);
@@ -537,6 +558,9 @@ GLOBAL(void)
537558
jsimd_h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
538559
JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
539560
{
561+
if (simd_support == ~0U)
562+
init_simd();
563+
540564
if (simd_support & JSIMD_AVX2)
541565
jsimd_h2v2_fancy_upsample_avx2(cinfo->max_v_samp_factor,
542566
compptr->downsampled_width, input_data,
@@ -555,6 +579,9 @@ GLOBAL(void)
555579
jsimd_h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
556580
JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
557581
{
582+
if (simd_support == ~0U)
583+
init_simd();
584+
558585
if (simd_support & JSIMD_AVX2)
559586
jsimd_h2v1_fancy_upsample_avx2(cinfo->max_v_samp_factor,
560587
compptr->downsampled_width, input_data,
@@ -623,6 +650,9 @@ jsimd_h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
623650
void (*sse2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
624651
void (*mmxfct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
625652

653+
if (simd_support == ~0U)
654+
init_simd();
655+
626656
switch (cinfo->out_color_space) {
627657
case JCS_EXT_RGB:
628658
avx2fct = jsimd_h2v2_extrgb_merged_upsample_avx2;
@@ -681,6 +711,9 @@ jsimd_h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
681711
void (*sse2fct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
682712
void (*mmxfct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY);
683713

714+
if (simd_support == ~0U)
715+
init_simd();
716+
684717
switch (cinfo->out_color_space) {
685718
case JCS_EXT_RGB:
686719
avx2fct = jsimd_h2v1_extrgb_merged_upsample_avx2;
@@ -785,6 +818,9 @@ GLOBAL(void)
785818
jsimd_convsamp(JSAMPARRAY sample_data, JDIMENSION start_col,
786819
DCTELEM *workspace)
787820
{
821+
if (simd_support == ~0U)
822+
init_simd();
823+
788824
if (simd_support & JSIMD_AVX2)
789825
jsimd_convsamp_avx2(sample_data, start_col, workspace);
790826
else if (simd_support & JSIMD_SSE2)
@@ -797,6 +833,9 @@ GLOBAL(void)
797833
jsimd_convsamp_float(JSAMPARRAY sample_data, JDIMENSION start_col,
798834
FAST_FLOAT *workspace)
799835
{
836+
if (simd_support == ~0U)
837+
init_simd();
838+
800839
if (simd_support & JSIMD_SSE2)
801840
jsimd_convsamp_float_sse2(sample_data, start_col, workspace);
802841
else if (simd_support & JSIMD_SSE)
@@ -867,6 +906,9 @@ jsimd_can_fdct_float(void)
867906
GLOBAL(void)
868907
jsimd_fdct_islow(DCTELEM *data)
869908
{
909+
if (simd_support == ~0U)
910+
init_simd();
911+
870912
if (simd_support & JSIMD_AVX2)
871913
jsimd_fdct_islow_avx2(data);
872914
else if (simd_support & JSIMD_SSE2)
@@ -878,6 +920,9 @@ jsimd_fdct_islow(DCTELEM *data)
878920
GLOBAL(void)
879921
jsimd_fdct_ifast(DCTELEM *data)
880922
{
923+
if (simd_support == ~0U)
924+
init_simd();
925+
881926
if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_fdct_islow_sse2))
882927
jsimd_fdct_ifast_sse2(data);
883928
else
@@ -887,6 +932,9 @@ jsimd_fdct_ifast(DCTELEM *data)
887932
GLOBAL(void)
888933
jsimd_fdct_float(FAST_FLOAT *data)
889934
{
935+
if (simd_support == ~0U)
936+
init_simd();
937+
890938
if ((simd_support & JSIMD_SSE) && IS_ALIGNED_SSE(jconst_fdct_float_sse))
891939
jsimd_fdct_float_sse(data);
892940
else if (simd_support & JSIMD_3DNOW)
@@ -942,6 +990,9 @@ jsimd_can_quantize_float(void)
942990
GLOBAL(void)
943991
jsimd_quantize(JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
944992
{
993+
if (simd_support == ~0U)
994+
init_simd();
995+
945996
if (simd_support & JSIMD_AVX2)
946997
jsimd_quantize_avx2(coef_block, divisors, workspace);
947998
else if (simd_support & JSIMD_SSE2)
@@ -954,6 +1005,9 @@ GLOBAL(void)
9541005
jsimd_quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
9551006
FAST_FLOAT *workspace)
9561007
{
1008+
if (simd_support == ~0U)
1009+
init_simd();
1010+
9571011
if (simd_support & JSIMD_SSE2)
9581012
jsimd_quantize_float_sse2(coef_block, divisors, workspace);
9591013
else if (simd_support & JSIMD_SSE)
@@ -1017,6 +1071,9 @@ jsimd_idct_2x2(j_decompress_ptr cinfo, jpeg_component_info *compptr,
10171071
JCOEFPTR coef_block, JSAMPARRAY output_buf,
10181072
JDIMENSION output_col)
10191073
{
1074+
if (simd_support == ~0U)
1075+
init_simd();
1076+
10201077
if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_red_sse2))
10211078
jsimd_idct_2x2_sse2(compptr->dct_table, coef_block, output_buf,
10221079
output_col);
@@ -1029,6 +1086,9 @@ jsimd_idct_4x4(j_decompress_ptr cinfo, jpeg_component_info *compptr,
10291086
JCOEFPTR coef_block, JSAMPARRAY output_buf,
10301087
JDIMENSION output_col)
10311088
{
1089+
if (simd_support == ~0U)
1090+
init_simd();
1091+
10321092
if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_red_sse2))
10331093
jsimd_idct_4x4_sse2(compptr->dct_table, coef_block, output_buf,
10341094
output_col);
@@ -1123,6 +1183,9 @@ jsimd_idct_islow(j_decompress_ptr cinfo, jpeg_component_info *compptr,
11231183
JCOEFPTR coef_block, JSAMPARRAY output_buf,
11241184
JDIMENSION output_col)
11251185
{
1186+
if (simd_support == ~0U)
1187+
init_simd();
1188+
11261189
if (simd_support & JSIMD_AVX2)
11271190
jsimd_idct_islow_avx2(compptr->dct_table, coef_block, output_buf,
11281191
output_col);
@@ -1139,6 +1202,9 @@ jsimd_idct_ifast(j_decompress_ptr cinfo, jpeg_component_info *compptr,
11391202
JCOEFPTR coef_block, JSAMPARRAY output_buf,
11401203
JDIMENSION output_col)
11411204
{
1205+
if (simd_support == ~0U)
1206+
init_simd();
1207+
11421208
if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_ifast_sse2))
11431209
jsimd_idct_ifast_sse2(compptr->dct_table, coef_block, output_buf,
11441210
output_col);
@@ -1152,6 +1218,9 @@ jsimd_idct_float(j_decompress_ptr cinfo, jpeg_component_info *compptr,
11521218
JCOEFPTR coef_block, JSAMPARRAY output_buf,
11531219
JDIMENSION output_col)
11541220
{
1221+
if (simd_support == ~0U)
1222+
init_simd();
1223+
11551224
if ((simd_support & JSIMD_SSE2) && IS_ALIGNED_SSE(jconst_idct_float_sse2))
11561225
jsimd_idct_float_sse2(compptr->dct_table, coef_block, output_buf,
11571226
output_col);

0 commit comments

Comments
 (0)