diff --git a/internal/cgen/base/io-private.h b/internal/cgen/base/io-private.h index 4b0b8a92..e132c24e 100644 --- a/internal/cgen/base/io-private.h +++ b/internal/cgen/base/io-private.h @@ -204,6 +204,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history( // above, but has stronger pre-conditions. // // The caller needs to prove that: +// - length >= 1 // - length <= (io2_w - *ptr_iop_w) // - distance >= 1 // - distance <= (*ptr_iop_w - io0_w) @@ -240,6 +241,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast( // - copying l=3, d=2 produces [17, 18, 17] and the cusp is (17, 18). // // The caller needs to prove that: +// - length >= 1 // - length <= (io2_w - *ptr_iop_w) // - distance >= 1 // - distance <= (*ptr_iop_w - io0_w) @@ -276,6 +278,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp( // In terms of advancing *ptr_iop_w, length is not rounded up. // // The caller needs to prove that: +// - length >= 1 // - (length + 8) <= (io2_w - *ptr_iop_w) // - distance == 1 // - distance <= (*ptr_iop_w - io0_w) @@ -317,6 +320,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_8_byte_chunks_dista // In terms of advancing *ptr_iop_w, length is not rounded up. // // The caller needs to prove that: +// - length >= 1 // - (length + 8) <= (io2_w - *ptr_iop_w) // - distance >= 8 // - distance <= (*ptr_iop_w - io0_w) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 4f31b651..06092ba7 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -576,6 +576,7 @@ var funcsOther = [...]string{ "io_writer.limited_copy_u32_from_slice!(up_to: u32, s: roslice u8) u32", // TODO: this should have explicit pre-conditions: + // - up_to >= 1 // - up_to <= this.length() // - distance >= 1 // - distance <= this.history_length() @@ -583,6 +584,7 @@ var funcsOther = [...]string{ "io_writer.limited_copy_u32_from_history_fast!(up_to: u32, distance: u32) u32", // TODO: this should have explicit pre-conditions: + // - up_to >= 1 // - up_to <= this.length() // - distance >= 1 // - distance <= this.history_length() @@ -590,6 +592,7 @@ var funcsOther = [...]string{ "io_writer.limited_copy_u32_from_history_fast_return_cusp!(up_to: u32, distance: u32) u32[..= 0xFFFF]", // TODO: this should have explicit pre-conditions: + // - up_to >= 1 // - (up_to + 8) <= this.length() // - distance >= 8 // - distance <= this.history_length() @@ -597,6 +600,7 @@ var funcsOther = [...]string{ "io_writer.limited_copy_u32_from_history_8_byte_chunks_fast!(up_to: u32, distance: u32) u32", // TODO: this should have explicit pre-conditions: + // - up_to >= 1 // - (up_to + 8) <= this.length() // - distance == 1 // - distance <= this.history_length() diff --git a/lang/check/bounds.go b/lang/check/bounds.go index de538319..f6cf6072 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1337,6 +1337,7 @@ func (q *checker) canUndoByte(recv *a.Expr) error { func (q *checker) canLimitedCopyU32FromHistoryFast(recv *a.Expr, args []*a.Node, adj *big.Int, minDistance *big.Int, exactDistance *big.Int) error { // As per cgen's io-private.h, there are three pre-conditions: + // - upTo >= one // - (upTo + adj) <= this.length() // - either (distance >= minDistance) or (distance == exactDistance), // depending on which of minDistance and exactDistance is non-nil. @@ -1350,6 +1351,24 @@ func (q *checker) canLimitedCopyU32FromHistoryFast(recv *a.Expr, args []*a.Node, upTo := args[0].AsArg().Value() distance := args[1].AsArg().Value() + // Check "upto >= one". +check9: // TODO: renumber. + for { + for _, x := range q.facts { + if x.Operator() != t.IDXBinaryGreaterEq { + continue + } + if lhs := x.LHS().AsExpr(); !lhs.Eq(upTo) { + continue + } + if rcv := x.RHS().AsExpr().ConstValue(); (rcv == nil) || (rcv.Sign() <= 0) { + continue + } + break check9 + } + return fmt.Errorf("check: could not prove %s >= 1", upTo.Str(q.tm)) + } + // Check "upTo <= this.length()". check0: for { diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 879caced..ce73612d 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -14827,6 +14827,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history( // above, but has stronger pre-conditions. // // The caller needs to prove that: +// - length >= 1 // - length <= (io2_w - *ptr_iop_w) // - distance >= 1 // - distance <= (*ptr_iop_w - io0_w) @@ -14863,6 +14864,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast( // - copying l=3, d=2 produces [17, 18, 17] and the cusp is (17, 18). // // The caller needs to prove that: +// - length >= 1 // - length <= (io2_w - *ptr_iop_w) // - distance >= 1 // - distance <= (*ptr_iop_w - io0_w) @@ -14899,6 +14901,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp( // In terms of advancing *ptr_iop_w, length is not rounded up. // // The caller needs to prove that: +// - length >= 1 // - (length + 8) <= (io2_w - *ptr_iop_w) // - distance == 1 // - distance <= (*ptr_iop_w - io0_w) @@ -14940,6 +14943,7 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_8_byte_chunks_dista // In terms of advancing *ptr_iop_w, length is not rounded up. // // The caller needs to prove that: +// - length >= 1 // - (length + 8) <= (io2_w - *ptr_iop_w) // - distance >= 8 // - distance <= (*ptr_iop_w - io0_w) diff --git a/std/deflate/decode_huffman_bmi2.wuffs b/std/deflate/decode_huffman_bmi2.wuffs index 8bdb9ca6..519fb0da 100644 --- a/std/deflate/decode_huffman_bmi2.wuffs +++ b/std/deflate/decode_huffman_bmi2.wuffs @@ -146,6 +146,7 @@ pri func decoder.decode_huffman_bmi2!(dst: base.io_writer, src: base.io_reader) bits >>= table_entry_n_bits n_bits ~mod-= table_entry_n_bits } + assert length >= 1 // Decode a dcode symbol from H-D. table_entry = this.huffs[1][bits & dmask] @@ -190,6 +191,7 @@ pri func decoder.decode_huffman_bmi2!(dst: base.io_writer, src: base.io_reader) // decode_huffman_slow. while true, pre args.dst.length() >= 266, + pre length >= 1, { // We can therefore prove: assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266) @@ -227,6 +229,7 @@ pri func decoder.decode_huffman_bmi2!(dst: base.io_writer, src: base.io_reader) // No need to copy from args.dst. continue.loop } + assert length >= 1 if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or ((length as base.u64) > args.dst.length()) or @@ -237,6 +240,7 @@ pri func decoder.decode_huffman_bmi2!(dst: base.io_writer, src: base.io_reader) // Once again, redundant but explicit assertions. assert (dist_minus_1 + 1) >= 1 assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() + assert length >= 1 assert (length as base.u64) <= args.dst.length() assert ((length + 8) as base.u64) <= args.dst.length() diff --git a/std/deflate/decode_huffman_fast32.wuffs b/std/deflate/decode_huffman_fast32.wuffs index 04fab25a..b557a42c 100644 --- a/std/deflate/decode_huffman_fast32.wuffs +++ b/std/deflate/decode_huffman_fast32.wuffs @@ -201,6 +201,7 @@ pri func decoder.decode_huffman_fast32!(dst: base.io_writer, src: base.io_reader } else { assert args.src.length() >= 6 } + assert length >= 1 // Ensure that we have at least 15 bits of input. if n_bits < 15 { @@ -295,6 +296,7 @@ pri func decoder.decode_huffman_fast32!(dst: base.io_writer, src: base.io_reader // decode_huffman_slow. while true, pre args.dst.length() >= 266, + pre length >= 1, { // We can therefore prove: assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266) @@ -332,6 +334,7 @@ pri func decoder.decode_huffman_fast32!(dst: base.io_writer, src: base.io_reader // No need to copy from args.dst. continue.loop } + assert length >= 1 if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or ((length as base.u64) > args.dst.length()) or @@ -342,6 +345,7 @@ pri func decoder.decode_huffman_fast32!(dst: base.io_writer, src: base.io_reader // Once again, redundant but explicit assertions. assert (dist_minus_1 + 1) >= 1 assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() + assert length >= 1 assert (length as base.u64) <= args.dst.length() assert ((length + 8) as base.u64) <= args.dst.length() diff --git a/std/deflate/decode_huffman_fast64.wuffs b/std/deflate/decode_huffman_fast64.wuffs index 66222252..868b2287 100644 --- a/std/deflate/decode_huffman_fast64.wuffs +++ b/std/deflate/decode_huffman_fast64.wuffs @@ -145,6 +145,7 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader bits >>= table_entry_n_bits n_bits ~mod-= table_entry_n_bits } + assert length >= 1 // Decode a dcode symbol from H-D. table_entry = this.huffs[1][bits & dmask] @@ -189,6 +190,7 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader // decode_huffman_slow. while true, pre args.dst.length() >= 266, + pre length >= 1, { // We can therefore prove: assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266) @@ -226,6 +228,7 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader // No need to copy from args.dst. continue.loop } + assert length >= 1 if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or ((length as base.u64) > args.dst.length()) or @@ -236,6 +239,7 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader // Once again, redundant but explicit assertions. assert (dist_minus_1 + 1) >= 1 assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length() + assert length >= 1 assert (length as base.u64) <= args.dst.length() assert ((length + 8) as base.u64) <= args.dst.length()