1 // errorcheck -0 -d=ssa/prove/debug=1
5 // Copyright 2016 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
13 func f0(a []int) int {
15 a[0] = 1 // ERROR "Proved IsInBounds$"
17 a[6] = 1 // ERROR "Proved IsInBounds$"
18 a[5] = 1 // ERROR "Proved IsInBounds$"
19 a[5] = 1 // ERROR "Proved IsInBounds$"
23 func f1(a []int) int {
27 a[0] = 1 // ERROR "Proved IsInBounds$"
28 a[0] = 1 // ERROR "Proved IsInBounds$"
30 a[6] = 1 // ERROR "Proved IsInBounds$"
31 a[5] = 1 // ERROR "Proved IsInBounds$"
32 a[5] = 1 // ERROR "Proved IsInBounds$"
36 func f1b(a []int, i int, j uint) int {
37 if i >= 0 && i < len(a) {
38 return a[i] // ERROR "Proved IsInBounds$"
40 if i >= 10 && i < len(a) {
41 return a[i] // ERROR "Proved IsInBounds$"
43 if i >= 10 && i < len(a) {
44 return a[i] // ERROR "Proved IsInBounds$"
46 if i >= 10 && i < len(a) {
47 return a[i-10] // ERROR "Proved IsInBounds$"
50 return a[j] // ERROR "Proved IsInBounds$"
55 func f1c(a []int, i int64) int {
56 c := uint64(math.MaxInt64 + 10) // overflows int
58 if i >= d && i < int64(len(a)) {
59 // d overflows, should not be handled.
65 func f2(a []int) int {
66 for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
68 a[i+1] = i // ERROR "Proved IsInBounds$"
73 func f3(a []uint) int {
74 for i := uint(0); i < uint(len(a)); i++ {
75 a[i] = i // ERROR "Proved IsInBounds$"
80 func f4a(a, b, c int) int {
82 if a == b { // ERROR "Disproved Eq64$"
85 if a > b { // ERROR "Disproved Less64$"
88 if a < b { // ERROR "Proved Less64$"
91 // We can't get to this point and prove knows that, so
92 // there's no message for the next (obvious) branch.
101 func f4b(a, b, c int) int {
104 if a == b { // ERROR "Proved Eq64$"
114 func f4c(a, b, c int) int {
117 if a != b { // ERROR "Disproved Neq64$"
127 func f4d(a, b, c int) int {
130 if a < b { // ERROR "Proved Less64$"
131 if a < c { // ERROR "Proved Less64$"
143 func f4e(a, b, c int) int {
145 if b > a { // ERROR "Proved Less64$"
153 func f4f(a, b, c int) int {
156 if b == a { // ERROR "Disproved Eq64$"
161 if b >= a { // ERROR "Proved Leq64$"
162 if b == a { // ERROR "Proved Eq64$"
172 func f5(a, b uint) int {
174 if a <= b { // ERROR "Proved Leq64U$"
182 // These comparisons are compile time constants.
183 func f6a(a uint8) int {
184 if a < a { // ERROR "Disproved Less8U$"
190 func f6b(a uint8) int {
191 if a < a { // ERROR "Disproved Less8U$"
197 func f6x(a uint8) int {
198 if a > a { // ERROR "Disproved Less8U$"
204 func f6d(a uint8) int {
205 if a <= a { // ERROR "Proved Leq8U$"
211 func f6e(a uint8) int {
212 if a >= a { // ERROR "Proved Leq8U$"
218 func f7(a []int, b int) int {
221 if b < len(a) { // ERROR "Proved Less64$"
222 a[b] = 5 // ERROR "Proved IsInBounds$"
228 func f8(a, b uint) int {
235 if a < b { // ERROR "Proved Less64U$"
241 func f9(a, b bool) int {
245 if a || b { // ERROR "Disproved Arg$"
251 func f10(a string) int {
253 // We optimize comparisons with small constant strings (see cmd/compile/internal/gc/walk.go),
254 // so this string literal must be long.
255 if a[:n>>1] == "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" {
261 func f11a(a []int, i int) {
263 useInt(a[i]) // ERROR "Proved IsInBounds$"
266 func f11b(a []int, i int) {
268 useSlice(a[i:]) // ERROR "Proved IsSliceInBounds$"
271 func f11c(a []int, i int) {
273 useSlice(a[:i]) // ERROR "Proved IsSliceInBounds$"
276 func f11d(a []int, i int) {
278 useInt(a[2*i+7]) // ERROR "Proved IsInBounds$"
281 func f12(a []int, b int) {
285 func f13a(a, b, c int, x bool) int {
288 if a < 12 { // ERROR "Disproved Less64$"
293 if a <= 12 { // ERROR "Disproved Leq64$"
298 if a == 12 { // ERROR "Disproved Eq64$"
303 if a >= 12 { // ERROR "Proved Leq64$"
308 if a > 12 { // ERROR "Proved Less64$"
317 func f13b(a int, x bool) int {
320 if a < -9 { // ERROR "Disproved Less64$"
325 if a <= -9 { // ERROR "Proved Leq64$"
330 if a == -9 { // ERROR "Proved Eq64$"
335 if a >= -9 { // ERROR "Proved Leq64$"
340 if a > -9 { // ERROR "Disproved Less64$"
349 func f13c(a int, x bool) int {
352 if a < 90 { // ERROR "Proved Less64$"
357 if a <= 90 { // ERROR "Proved Leq64$"
362 if a == 90 { // ERROR "Disproved Eq64$"
367 if a >= 90 { // ERROR "Disproved Leq64$"
372 if a > 90 { // ERROR "Disproved Less64$"
381 func f13d(a int) int {
383 if a < 9 { // ERROR "Proved Less64$"
390 func f13e(a int) int {
392 if a > 5 { // ERROR "Proved Less64$"
399 func f13f(a int64) int64 {
400 if a > math.MaxInt64 {
401 if a == 0 { // ERROR "Disproved Eq64$"
408 func f13g(a int) int {
415 if a == 3 { // ERROR "Proved Eq64$"
421 func f13h(a int) int {
424 if a == 2 { // ERROR "Proved Eq64$"
432 func f13i(a uint) int {
436 if a > 0 { // ERROR "Proved Less64U$"
442 func f14(p, q *int, a []int) {
443 // This crazy ordering usually gives i1 the lowest value ID,
444 // j the middle value ID, and i2 the highest value ID.
445 // That used to confuse CSE because it ordered the args
446 // of the two + ops below differently.
447 // That in turn foiled bounds check elimination.
452 useInt(a[i2+j]) // ERROR "Proved IsInBounds$"
455 func f15(s []int, x int) {
457 useSlice(s[:x]) // ERROR "Proved IsSliceInBounds$"
460 func f16(s []int) []int {
462 return s[:10] // ERROR "Proved IsSliceInBounds$"
468 for i := 0; i < len(b); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
469 // This tests for i <= cap, which we can only prove
470 // using the derived relation between len and cap.
471 // This depends on finding the contradiction, since we
472 // don't query this condition directly.
473 useSlice(b[:i]) // ERROR "Proved IsSliceInBounds$"
477 func f18(b []int, x int, y uint) {
481 if x > len(b) { // ERROR "Disproved Less64$"
484 if y > uint(len(b)) { // ERROR "Disproved Less64U$"
487 if int(y) > len(b) { // ERROR "Disproved Less64$"
492 func f19() (e int64, err error) {
493 // Issue 29502: slice[:0] is incorrectly disproved.
495 stack = append(stack, 123)
497 panic("too many elements")
499 last := len(stack) - 1
501 // Buggy compiler prints "Disproved Leq64" for the next line.
506 func sm1(b []int, x int) {
507 // Test constant argument to slicemask.
508 useSlice(b[2:8]) // ERROR "Proved slicemask not needed$"
509 // Test non-constant argument with known limits.
515 func lim1(x, y, z int) {
516 // Test relations between signed and unsigned limits.
518 if uint(x) > 5 { // ERROR "Proved Less64U$"
523 if uint(y) > 4 { // ERROR "Disproved Less64U$"
526 if uint(y) < 5 { // ERROR "Proved Less64U$"
531 if uint(z) > 4 { // Not provable without disjunctions.
537 // fence1–4 correspond to the four fence-post implications.
539 func fence1(b []int, x, y int) {
540 // Test proofs that rely on fence-post implications.
542 if x < y { // ERROR "Disproved Less64$"
547 // This eliminates the growslice path.
548 b = append(b, 1) // ERROR "Disproved Less64U$"
552 func fence2(x, y int) {
554 if x > y { // ERROR "Disproved Less64$"
560 func fence3(b, c []int, x, y int64) {
562 if x <= y { // Can't prove because x may have wrapped.
567 if x != math.MinInt64 && x-1 >= y {
568 if x <= y { // ERROR "Disproved Leq64$"
573 c[len(c)-1] = 0 // Can't prove because len(c) might be 0
575 if n := len(b); n > 0 {
576 b[n-1] = 0 // ERROR "Proved IsInBounds$"
580 func fence4(x, y int64) {
586 if y != math.MaxInt64 && x >= y+1 {
587 if x <= y { // ERROR "Disproved Leq64$"
593 // Check transitive relations
594 func trans1(x, y int64) {
597 if y > 2 { // ERROR "Proved Less64$"
601 if y > 5 { // ERROR "Proved Less64$"
608 if y > 10 { // ERROR "Proved Less64$"
615 func trans2(a, b []int, i int) {
616 if len(a) != len(b) {
621 _ = b[i] // ERROR "Proved IsInBounds$"
624 func trans3(a, b []int, i int) {
630 _ = b[i] // ERROR "Proved IsInBounds$"
633 func trans4(b []byte, x int) {
634 // Issue #42603: slice len/cap transitive relations.
640 _ = b[:2] // ERROR "Proved IsSliceInBounds$"
645 _ = b[:2] // ERROR "Proved IsSliceInBounds$"
649 // Derived from nat.cmp
650 func natcmp(x, y []uint) (r int) {
653 if m != n || m == 0 {
658 for i > 0 && // ERROR "Induction variable: limits \(0,\?\], increment 1$"
659 x[i] == // ERROR "Proved IsInBounds$"
660 y[i] { // ERROR "Proved IsInBounds$"
665 case x[i] < // todo, cannot prove this because it's dominated by i<=0 || x[i]==y[i]
666 y[i]: // ERROR "Proved IsInBounds$"
668 case x[i] > // ERROR "Proved IsInBounds$"
669 y[i]: // ERROR "Proved IsInBounds$"
675 func suffix(s, suffix string) bool {
676 // todo, we're still not able to drop the bound check here in the general case
677 return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
680 func constsuffix(s string) bool {
681 return suffix(s, "abc") // ERROR "Proved IsSliceInBounds$"
684 // oforuntil tests the pattern created by OFORUNTIL blocks. These are
685 // handled by addLocalInductiveFacts rather than findIndVar.
686 func oforuntil(b []int) {
690 println(b[i]) // ERROR "Induction variable: limits \[0,\?\), increment 1$" "Proved IsInBounds$"
698 func atexit(foobar []func()) {
699 for i := len(foobar) - 1; i >= 0; i-- { // ERROR "Induction variable: limits \[0,\?\], increment 1"
701 foobar = foobar[:i] // ERROR "IsSliceInBounds"
706 func make1(n int) []int {
708 for i := 0; i < n; i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1"
709 s[i] = 1 // ERROR "Proved IsInBounds$"
714 func make2(n int) []int {
716 for i := range s { // ERROR "Induction variable: limits \[0,\?\), increment 1"
717 s[i] = 1 // ERROR "Proved IsInBounds$"
722 // The range tests below test the index variable of range loops.
724 // range1 compiles to the "efficiently indexable" form of a range loop.
725 func range1(b []int) {
726 for i, v := range b { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
727 b[i] = v + 1 // ERROR "Proved IsInBounds$"
728 if i < len(b) { // ERROR "Proved Less64$"
731 if i >= 0 { // ERROR "Proved Leq64$"
737 // range2 elements are larger, so they use the general form of a range loop.
738 func range2(b [][32]int) {
739 for i, v := range b { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
740 b[i][0] = v[0] + 1 // ERROR "Proved IsInBounds$"
741 if i < len(b) { // ERROR "Proved Less64$"
744 if i >= 0 { // ERROR "Proved Leq64$"
750 // signhint1-2 test whether the hint (int >= 0) is propagated into the loop.
751 func signHint1(i int, data []byte) {
753 for i < len(data) { // ERROR "Induction variable: limits \[\?,\?\), increment 1$"
754 _ = data[i] // ERROR "Proved IsInBounds$"
760 func signHint2(b []byte, n int) {
765 for i := n; i <= 25; i++ { // ERROR "Induction variable: limits \[\?,25\], increment 1$"
766 b[i] = 123 // ERROR "Proved IsInBounds$"
770 // indexGT0 tests whether prove learns int index >= 0 from bounds check.
771 func indexGT0(b []byte, n int) {
775 for i := n; i <= 25; i++ { // ERROR "Induction variable: limits \[\?,25\], increment 1$"
776 b[i] = 123 // ERROR "Proved IsInBounds$"
780 // Induction variable in unrolled loop.
781 func unrollUpExcl(a []int) int {
783 for i = 0; i < len(a)-1; i += 2 { // ERROR "Induction variable: limits \[0,\?\), increment 2$"
784 x += a[i] // ERROR "Proved IsInBounds$"
793 // Induction variable in unrolled loop.
794 func unrollUpIncl(a []int) int {
796 for i = 0; i <= len(a)-2; i += 2 { // ERROR "Induction variable: limits \[0,\?\], increment 2$"
797 x += a[i] // ERROR "Proved IsInBounds$"
806 // Induction variable in unrolled loop.
807 func unrollDownExcl0(a []int) int {
809 for i = len(a) - 1; i > 0; i -= 2 { // ERROR "Induction variable: limits \(0,\?\], increment 2$"
810 x += a[i] // ERROR "Proved IsInBounds$"
811 x += a[i-1] // ERROR "Proved IsInBounds$"
819 // Induction variable in unrolled loop.
820 func unrollDownExcl1(a []int) int {
822 for i = len(a) - 1; i >= 1; i -= 2 { // ERROR "Induction variable: limits \(0,\?\], increment 2$"
823 x += a[i] // ERROR "Proved IsInBounds$"
824 x += a[i-1] // ERROR "Proved IsInBounds$"
832 // Induction variable in unrolled loop.
833 func unrollDownInclStep(a []int) int {
835 for i = len(a); i >= 2; i -= 2 { // ERROR "Induction variable: limits \[2,\?\], increment 2$"
836 x += a[i-1] // ERROR "Proved IsInBounds$"
837 x += a[i-2] // ERROR "Proved IsInBounds$"
845 // Not an induction variable (step too large)
846 func unrollExclStepTooLarge(a []int) int {
848 for i = 0; i < len(a)-1; i += 3 {
858 // Not an induction variable (step too large)
859 func unrollInclStepTooLarge(a []int) int {
861 for i = 0; i <= len(a)-2; i += 3 {
871 // Not an induction variable (min too small, iterating down)
872 func unrollDecMin(a []int) int {
874 for i = len(a); i >= math.MinInt64; i -= 2 {
878 if i == 1 { // ERROR "Disproved Eq64$"
884 // Not an induction variable (min too small, iterating up -- perhaps could allow, but why bother?)
885 func unrollIncMin(a []int) int {
887 for i = len(a); i >= math.MinInt64; i += 2 {
891 if i == 1 { // ERROR "Disproved Eq64$"
897 // The 4 xxxxExtNto64 functions below test whether prove is looking
898 // through value-preserving sign/zero extensions of index values (issue #26292).
900 // Look through all extensions
901 func signExtNto64(x []int, j8 int8, j16 int16, j32 int32) int {
905 if j8 >= 0 && j8 < 22 {
906 return x[j8] // ERROR "Proved IsInBounds$"
908 if j16 >= 0 && j16 < 22 {
909 return x[j16] // ERROR "Proved IsInBounds$"
911 if j32 >= 0 && j32 < 22 {
912 return x[j32] // ERROR "Proved IsInBounds$"
917 func zeroExtNto64(x []int, j8 uint8, j16 uint16, j32 uint32) int {
921 if j8 >= 0 && j8 < 22 {
922 return x[j8] // ERROR "Proved IsInBounds$"
924 if j16 >= 0 && j16 < 22 {
925 return x[j16] // ERROR "Proved IsInBounds$"
927 if j32 >= 0 && j32 < 22 {
928 return x[j32] // ERROR "Proved IsInBounds$"
933 // Process fence-post implications through 32to64 extensions (issue #29964)
934 func signExt32to64Fence(x []int, j int32) int {
938 if j > 0 && x[j-1] != 0 { // ERROR "Proved IsInBounds$"
944 func zeroExt32to64Fence(x []int, j uint32) int {
948 if j > 0 && x[j-1] != 0 { // ERROR "Proved IsInBounds$"
954 // Ensure that bounds checks with negative indexes are not incorrectly removed.
957 for i := -1; i <= 0; i++ { // ERROR "Induction variable: limits \[-1,0\], increment 1$"
961 func negIndex2(n int) {
965 for i := -1; i <= 0; i-- {
976 // Check that prove is zeroing these right shifts of positive ints by bit-width - 1.
977 // e.g (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) && ft.isNonNegative(n) -> 0
978 func sh64(n int64) int64 {
982 return n >> 63 // ERROR "Proved Rsh64x64 shifts to zero"
985 func sh32(n int32) int32 {
989 return n >> 31 // ERROR "Proved Rsh32x64 shifts to zero"
992 func sh32x64(n int32) int32 {
996 return n >> uint64(31) // ERROR "Proved Rsh32x64 shifts to zero"
999 func sh16(n int16) int16 {
1003 return n >> 15 // ERROR "Proved Rsh16x64 shifts to zero"
1006 func sh64noopt(n int64) int64 {
1007 return n >> 63 // not optimized; n could be negative
1010 // These cases are division of a positive signed integer by a power of 2.
1011 // The opt pass doesnt have sufficient information to see that n is positive.
1012 // So, instead, opt rewrites the division with a less-than-optimal replacement.
1013 // Prove, which can see that n is nonnegative, cannot see the division because
1014 // opt, an earlier pass, has already replaced it.
1015 // The fix for this issue allows prove to zero a right shift that was added as
1016 // part of the less-than-optimal reqwrite. That change by prove then allows
1017 // lateopt to clean up all the unnecessary parts of the original division
1018 // replacement. See issue #36159.
1019 func divShiftClean(n int) int {
1023 return n / int(8) // ERROR "Proved Rsh64x64 shifts to zero"
1026 func divShiftClean64(n int64) int64 {
1030 return n / int64(16) // ERROR "Proved Rsh64x64 shifts to zero"
1033 func divShiftClean32(n int32) int32 {
1037 return n / int32(16) // ERROR "Proved Rsh32x64 shifts to zero"
1040 // Bounds check elimination
1042 func sliceBCE1(p []string, h uint) string {
1047 i := h & uint(len(p)-1)
1048 return p[i] // ERROR "Proved IsInBounds$"
1051 func sliceBCE2(p []string, h int) string {
1055 i := h & (len(p) - 1)
1056 return p[i] // ERROR "Proved IsInBounds$"
1059 func and(p []byte) ([]byte, []byte) { // issue #52563
1060 const blocksize = 16
1061 fullBlocks := len(p) &^ (blocksize - 1)
1062 blk := p[:fullBlocks] // ERROR "Proved IsSliceInBounds$"
1063 rem := p[fullBlocks:] // ERROR "Proved IsSliceInBounds$"
1067 func rshu(x, y uint) int {
1069 if z <= x { // ERROR "Proved Leq64U$"
1075 func divu(x, y uint) int {
1077 if z <= x { // ERROR "Proved Leq64U$"
1083 func modu1(x, y uint) int {
1085 if z < y { // ERROR "Proved Less64U$"
1091 func modu2(x, y uint) int {
1093 if z <= x { // ERROR "Proved Leq64U$"
1099 func issue57077(s []int) (left, right []int) {
1100 middle := len(s) / 2
1101 left = s[:middle] // ERROR "Proved IsSliceInBounds$"
1102 right = s[middle:] // ERROR "Proved IsSliceInBounds$"
1106 func issue51622(b []byte) int {
1107 if len(b) >= 3 && b[len(b)-3] == '#' { // ERROR "Proved IsInBounds$"
1113 func issue45928(x int) {
1114 combinedFrac := x / (x | (1 << 31)) // ERROR "Proved Neq64$"
1115 useInt(combinedFrac)
1119 func useInt(a int) {
1123 func useSlice(a []int) {