mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
std/brotli: fix long zero runs in the fast Huffman tree encoding
build_and_store_huffman_tree_fast_generic indexed the 704-entry ZERO_REPS and NON_ZERO_REPS tables with reps & 511, so a run of more than 511 equal code lengths (e.g. unused command codes between 75 and 688 in a quality 2 metablock of more than 128 commands) was written wrongly and the stream did not decode. Found by the differential fuzzer (quality 2, lgwin 10).
This commit is contained in:
3 files changed
+52
-4
No files matched your search
@@ -85619,7 +85619,7 @@ wuffs_brotli__encoder__build_and_store_huffman_tree_fast_generic(
|
||||
}
|
||||
v_i += v_reps;
|
||||
if (v_value == 0u) {
|
||||
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__ZERO_REPS_DEPTH[(v_reps & 511u)], WUFFS_BROTLI__ZERO_REPS_BITS[(v_reps & 511u)], a_s);
|
||||
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__ZERO_REPS_DEPTH[wuffs_base__u32__min(v_reps, 703u)], WUFFS_BROTLI__ZERO_REPS_BITS[wuffs_base__u32__min(v_reps, 703u)], a_s);
|
||||
} else {
|
||||
if (v_prev_value != v_value) {
|
||||
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__CODE_LENGTH_DEPTH[(v_value & 15u)], WUFFS_BROTLI__CODE_LENGTH_BITS[(v_value & 15u)], a_s);
|
||||
@@ -85632,7 +85632,7 @@ wuffs_brotli__encoder__build_and_store_huffman_tree_fast_generic(
|
||||
}
|
||||
} else {
|
||||
v_reps -= 3u;
|
||||
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__NON_ZERO_REPS_DEPTH[(v_reps & 511u)], WUFFS_BROTLI__NON_ZERO_REPS_BITS[(v_reps & 511u)], a_s);
|
||||
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__NON_ZERO_REPS_DEPTH[wuffs_base__u32__min(v_reps, 703u)], WUFFS_BROTLI__NON_ZERO_REPS_BITS[wuffs_base__u32__min(v_reps, 703u)], a_s);
|
||||
}
|
||||
v_prev_value = v_value;
|
||||
}
|
||||
|
||||
@@ -397,8 +397,12 @@ pri func encoder.build_and_store_huffman_tree_fast_generic!(histogram_total: bas
|
||||
k ~mod+= 1
|
||||
}
|
||||
i ~mod+= reps
|
||||
// Mirrors BrotliBuildAndStoreHuffmanTreeFast's kZeroRepsDepth[reps]
|
||||
// and kNonZeroRepsDepth[reps] (704 entries each): a run can be
|
||||
// longer than 511, e.g. the zeros between command codes 75 and
|
||||
// 688.
|
||||
if value == 0 {
|
||||
this.write_bits!(n_bits: ZERO_REPS_DEPTH[reps & 511], bits: ZERO_REPS_BITS[reps & 511], s: args.s)
|
||||
this.write_bits!(n_bits: ZERO_REPS_DEPTH[reps.min(no_more_than: 703)], bits: ZERO_REPS_BITS[reps.min(no_more_than: 703)], s: args.s)
|
||||
} else {
|
||||
if prev_value <> value {
|
||||
this.write_bits!(n_bits: CODE_LENGTH_DEPTH[value & 15], bits: CODE_LENGTH_BITS[value & 15], s: args.s)
|
||||
@@ -411,7 +415,7 @@ pri func encoder.build_and_store_huffman_tree_fast_generic!(histogram_total: bas
|
||||
}
|
||||
} else {
|
||||
reps -= 3
|
||||
this.write_bits!(n_bits: NON_ZERO_REPS_DEPTH[reps & 511], bits: NON_ZERO_REPS_BITS[reps & 511], s: args.s)
|
||||
this.write_bits!(n_bits: NON_ZERO_REPS_DEPTH[reps.min(no_more_than: 703)], bits: NON_ZERO_REPS_BITS[reps.min(no_more_than: 703)], s: args.s)
|
||||
}
|
||||
prev_value = value
|
||||
}
|
||||
|
||||
@@ -1322,6 +1322,39 @@ test_wuffs_brotli_encode_round_trip_synthetic() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// brotli_enc_long_zero_run fills data[:9200] with 6300 pseudo-random bytes
|
||||
// followed by a copy of their first 2900 bytes, with one byte in every 21,
|
||||
// 15, 13 or 11 changed. At quality 2 (BrotliStoreMetaBlockFast with more than
|
||||
// 128 commands) that makes command codes 72 to 75 (last distance, one literal
|
||||
// and a 10 to 20 byte copy) and 688 (6300 literals and an 80 byte copy), so
|
||||
// the complex Huffman code for the commands has a run of 612 zero code
|
||||
// lengths, more than the 511 that the encoder's repetition tables once had.
|
||||
static void //
|
||||
brotli_enc_long_zero_run(uint8_t* data) {
|
||||
uint32_t x = 1;
|
||||
for (int i = 0; i < 6300; i++) {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
data[i] = (uint8_t)x;
|
||||
}
|
||||
memcpy(data + 6300, data, 2900);
|
||||
static const int steps[4] = {21, 15, 13, 11};
|
||||
for (int i = 80, k = 0; i < 2900; i += steps[k++ & 3]) {
|
||||
data[6300 + i] ^= 0x55;
|
||||
}
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_long_zero_run() {
|
||||
CHECK_FOCUS(__func__);
|
||||
uint8_t data[9200];
|
||||
brotli_enc_long_zero_run(data);
|
||||
brotli_enc_config c = make_brotli_enc_config(2, 16);
|
||||
return do_test_brotli_encode_round_trip("long zero run", &c, data,
|
||||
sizeof data);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_metadata_round_trip() {
|
||||
CHECK_FOCUS(__func__);
|
||||
@@ -2219,6 +2252,15 @@ test_mimic_brotli_encode_parity_q10_params() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_mimic_brotli_encode_long_zero_run() {
|
||||
CHECK_FOCUS(__func__);
|
||||
uint8_t data[9200];
|
||||
brotli_enc_long_zero_run(data);
|
||||
brotli_enc_config c = make_brotli_enc_config(2, 16);
|
||||
return do_test_mimic_brotli_encode("long zero run", &c, data, sizeof data);
|
||||
}
|
||||
|
||||
// test_mimic_brotli_encode_metadata_stale_pointer checks a libbrotlienc bug
|
||||
// that Wuffs does not mirror. At quality 1, a metadata block whose body goes
|
||||
// through the C code's 16-byte tiny_buf_ (dst has no room) leaves next_out_
|
||||
@@ -4618,6 +4660,7 @@ proc g_tests[] = {
|
||||
test_wuffs_brotli_encode_interface,
|
||||
test_wuffs_brotli_encode_large_window_far,
|
||||
test_wuffs_brotli_encode_large_window_files,
|
||||
test_wuffs_brotli_encode_long_zero_run,
|
||||
test_wuffs_brotli_encode_metadata_round_trip,
|
||||
test_wuffs_brotli_encode_parameters,
|
||||
test_wuffs_brotli_encode_round_trip_corpus,
|
||||
@@ -4631,6 +4674,7 @@ proc g_tests[] = {
|
||||
test_mimic_brotli_encode_dictionary_rejections,
|
||||
test_mimic_brotli_encode_large_window_far,
|
||||
test_mimic_brotli_encode_large_window_files,
|
||||
test_mimic_brotli_encode_long_zero_run,
|
||||
test_mimic_brotli_encode_metadata_stale_pointer,
|
||||
test_mimic_brotli_encode_parity_corpus,
|
||||
test_mimic_brotli_encode_parity_grid,
|
||||
|
||||
Reference in new issue
Block a user