std/jpeg: cap the implicit-zero bitstream padding

Updates https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=59966

name                                             old speed     new speed     delta

wuffs_jpeg_decode_19k_8bpp/clang11               241MB/s ± 0%  237MB/s ± 0%  -1.57%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/clang11  213MB/s ± 0%  212MB/s ± 0%  -0.43%  (p=0.016 n=5+4)
wuffs_jpeg_decode_30k_24bpp_sequential/clang11   387MB/s ± 0%  386MB/s ± 0%  -0.08%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/clang11              341MB/s ± 0%  340MB/s ± 0%  -0.50%  (p=0.016 n=4+5)
wuffs_jpeg_decode_552k_24bpp_420/clang11         356MB/s ± 0%  353MB/s ± 0%  -0.89%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/clang11         484MB/s ± 0%  480MB/s ± 0%  -0.96%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/clang11            364MB/s ± 0%  360MB/s ± 0%  -1.12%  (p=0.008 n=5+5)

wuffs_jpeg_decode_19k_8bpp/gcc10                 244MB/s ± 0%  247MB/s ± 0%  +0.88%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/gcc10    254MB/s ± 0%  254MB/s ± 0%  -0.20%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/gcc10     508MB/s ± 0%  512MB/s ± 0%  +0.61%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/gcc10                434MB/s ± 0%  436MB/s ± 0%  +0.39%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/gcc10           456MB/s ± 0%  458MB/s ± 0%  +0.41%  (p=0.016 n=4+5)
wuffs_jpeg_decode_552k_24bpp_444/gcc10           481MB/s ± 0%  483MB/s ± 0%  +0.49%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/gcc10              463MB/s ± 0%  466MB/s ± 0%  +0.57%  (p=0.016 n=4+5)
This commit is contained in:
Nigel Tao committed 2023-06-23 21:38:01 +10:00
1 parent 66ef485efa
commit 49f50cca91
2 files changed
+22 -2

No files matched your search

+13 -1
View File
@@ -271,6 +271,11 @@ pub struct decoder? implements base.image_decoder(
bitstream_ri : base.u32[..= 0x800],
bitstream_wi : base.u32[..= 0x800],
// bitstream_padding is the number (per restart) of implicit zeroes
// remaining at the end of the bitstream. Its initial 12345 value is
// arbitrary.
bitstream_padding : base.u32[..= 12345],
quant_tables : array[4] array[64] base.u8,
saved_quant_tables : array[4] array[64] base.u8,
@@ -1161,6 +1166,7 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
this.bitstream_n_bits = 0
this.bitstream_ri = 0
this.bitstream_wi = 0
this.bitstream_padding = 12345
this.fill_bitstream!(src: args.src)
my = 0
@@ -1197,6 +1203,8 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
this.fill_bitstream!(src: args.src)
if bitstream_length < (this.bitstream_wi ~mod- this.bitstream_ri) {
break.fill_bitstream
} else if this.bitstream_padding == 0 {
return "#bad SOS marker"
}
yield? base."$short read"
} endwhile.fill_bitstream
@@ -1223,6 +1231,7 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
this.bitstream_n_bits = 0
this.bitstream_ri = 0
this.bitstream_wi = 0
this.bitstream_padding = 12345
}
}
@@ -1520,10 +1529,13 @@ pri func decoder.fill_bitstream!(src: base.io_reader) {
if args.src.length() > 1 {
if (args.src.peek_u8() >= 0xFF) and ((args.src.peek_u16le() >> 8) > 0) {
new_wi = wi.min(no_more_than: 0x800 - 264) + 264
new_wi = new_wi.min(no_more_than: wi + this.bitstream_padding)
if wi < new_wi {
assert new_wi > wi via "a > b: b < a"()
this.bitstream_padding ~sat-= new_wi - wi
this.bitstream_buffer[wi .. new_wi].bulk_memset!(byte_value: 0)
wi = new_wi
}
wi = new_wi
}
}