From 4848859f9723f16fbaf0a43497392cbc2d58f938 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 2 Feb 2021 13:08:21 +1100 Subject: [PATCH] Tweak std/deflate decode_huffman_fast asserts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wuffs_deflate_decode_1k_full_init/clang9 95.0MB/s ± 0% 92.9MB/s ± 0% -2.25% (p=0.008 n=5+5) wuffs_deflate_decode_1k_part_init/clang9 108MB/s ± 0% 108MB/s ± 0% +0.34% (p=0.008 n=5+5) wuffs_deflate_decode_10k_full_init/clang9 141MB/s ± 0% 165MB/s ± 1% +16.77% (p=0.008 n=5+5) wuffs_deflate_decode_10k_part_init/clang9 143MB/s ± 0% 169MB/s ± 0% +17.73% (p=0.008 n=5+5) wuffs_deflate_decode_100k_just_one_read/clang9 185MB/s ± 0% 181MB/s ± 0% -2.09% (p=0.008 n=5+5) wuffs_deflate_decode_100k_many_big_reads/clang9 156MB/s ± 1% 151MB/s ± 0% -3.12% (p=0.008 n=5+5) wuffs_deflate_decode_1k_full_init/gcc10 98.6MB/s ± 0% 97.5MB/s ± 0% -1.05% (p=0.016 n=4+5) wuffs_deflate_decode_1k_part_init/gcc10 112MB/s ± 0% 111MB/s ± 0% -1.05% (p=0.008 n=5+5) wuffs_deflate_decode_10k_full_init/gcc10 183MB/s ± 0% 181MB/s ± 1% -0.73% (p=0.008 n=5+5) wuffs_deflate_decode_10k_part_init/gcc10 186MB/s ± 0% 185MB/s ± 0% -0.80% (p=0.008 n=5+5) wuffs_deflate_decode_100k_just_one_read/gcc10 213MB/s ± 0% 216MB/s ± 0% +1.55% (p=0.008 n=5+5) wuffs_deflate_decode_100k_many_big_reads/gcc10 174MB/s ± 0% 179MB/s ± 0% +2.79% (p=0.008 n=5+5) --- lang/check/bounds.go | 14 +++++++------- release/c/wuffs-unsupported-snapshot.c | 3 ++- std/deflate/decode_huffman_fast.wuffs | 16 +++++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 423860df..48399997 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1210,17 +1210,17 @@ func (q *checker) canUndoByte(recv *a.Expr) error { func (q *checker) canLimitedCopyU32FromHistoryFast(recv *a.Expr, args []*a.Node) error { // As per cgen's io-private.h, there are three pre-conditions: - // - n <= this.length() + // - upTo <= this.length() // - distance > 0 // - distance <= this.history_length() if len(args) != 2 { - return fmt.Errorf("check: internal error: inconsistent copy_n_from_history_fast arguments") + return fmt.Errorf("check: internal error: inconsistent limited_copy_u32_from_history_fast arguments") } - n := args[0].AsArg().Value() + upTo := args[0].AsArg().Value() distance := args[1].AsArg().Value() - // Check "n <= this.length()". + // Check "upTo <= this.length()". check0: for { for _, x := range q.facts { @@ -1228,13 +1228,13 @@ check0: continue } - // Check that the LHS is "n as base.u64". + // Check that the LHS is "upTo as base.u64". lhs := x.LHS().AsExpr() if lhs.Operator() != t.IDXBinaryAs { continue } llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr() - if !llhs.Eq(n) || !lrhs.Eq(typeExprU64) { + if !llhs.Eq(upTo) || !lrhs.Eq(typeExprU64) { continue } @@ -1249,7 +1249,7 @@ check0: break check0 } - return fmt.Errorf("check: could not prove n <= %s.length()", recv.Str(q.tm)) + return fmt.Errorf("check: could not prove (up_to as base.u64) <= %s.length()", recv.Str(q.tm)) } // Check "distance > 0". diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index f53bafe6..3616613f 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -23030,10 +23030,11 @@ wuffs_deflate__decoder__decode_huffman_fast( if (v_length == 0) { goto label__loop__continue; } - if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)(iop_a_dst - io0_a_dst))) { + if ((((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)(iop_a_dst - io0_a_dst))) || (((uint64_t)(v_length)) > ((uint64_t)(io2_a_dst - iop_a_dst)))) { status = wuffs_base__make_status(wuffs_deflate__error__internal_error_inconsistent_distance); goto exit; } + } else { } wuffs_base__io_writer__limited_copy_u32_from_history_fast( &iop_a_dst, io0_a_dst, io2_a_dst, v_length, (v_dist_minus_1 + 1)); diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index a3d439b7..99f47436 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -216,18 +216,20 @@ pri func decoder.decode_huffman_fast!(dst: base.io_writer, src: base.io_reader) continue.loop } - if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() { + if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or + ((length as base.u64) > args.dst.length()) { return "#internal error: inconsistent distance" } + } else { + assert (length as base.u64) <= 258 + assert args.dst.length() >= 258 + // We can therefore prove: + assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 258) } // Once again, redundant but explicit assertions. - assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() - assert args.dst.length() >= 258 - - // We can therefore prove: assert (dist_minus_1 + 1) > 0 - assert (length as base.u64) <= 258 - assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 258) + assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() + assert (length as base.u64) <= args.dst.length() // Copy from args.dst. args.dst.limited_copy_u32_from_history_fast!(