github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/test/checkbce.go (about)

     1  // +build amd64
     2  // errorcheck -0 -d=ssa/check_bce/debug=3
     3  
     4  package main
     5  
     6  func f0(a []int) {
     7  	a[0] = 1 // ERROR "Found IsInBounds$"
     8  	a[0] = 1
     9  	a[6] = 1 // ERROR "Found IsInBounds$"
    10  	a[6] = 1
    11  	a[5] = 1
    12  	a[5] = 1
    13  }
    14  
    15  func f1(a [256]int, i int) {
    16  	useInt(a[i])     // ERROR "Found IsInBounds$"
    17  	useInt(a[i%256]) // ERROR "Found IsInBounds$"
    18  	useInt(a[i&255])
    19  	useInt(a[i&17])
    20  
    21  	if 4 <= i && i < len(a) {
    22  		useInt(a[i])
    23  		useInt(a[i-1]) // ERROR "Found IsInBounds$"
    24  		// TODO: 'if 4 <= i && i < len(a)' gets rewritten to 'if uint(i - 4) < 256 - 4',
    25  		// which the bounds checker cannot yet use to infer that the next line doesn't need a bounds check.
    26  		useInt(a[i-4])
    27  	}
    28  }
    29  
    30  func f2(a [256]int, i uint) {
    31  	useInt(a[i]) // ERROR "Found IsInBounds$"
    32  	useInt(a[i%256])
    33  	useInt(a[i&255])
    34  	useInt(a[i&17])
    35  }
    36  
    37  func f3(a [256]int, i uint8) {
    38  	useInt(a[i])
    39  	useInt(a[i+10])
    40  	useInt(a[i+14])
    41  }
    42  
    43  func f4(a [27]int, i uint8) {
    44  	useInt(a[i%15])
    45  	useInt(a[i%19])
    46  	useInt(a[i%27])
    47  }
    48  
    49  func f5(a []int) {
    50  	if len(a) > 5 {
    51  		useInt(a[5])
    52  		useSlice(a[6:])
    53  		useSlice(a[:6])
    54  	}
    55  }
    56  
    57  func f6(a [32]int, b [64]int, i int) {
    58  	useInt(a[uint32(i*0x07C4ACDD)>>27])
    59  	useInt(b[uint64(i*0x07C4ACDD)>>58])
    60  	useInt(a[uint(i*0x07C4ACDD)>>59])
    61  
    62  	// The following bounds should not be removed because they can overflow.
    63  	useInt(a[uint32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$"
    64  	useInt(b[uint64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$"
    65  	useInt(a[int32(i*0x106297f105d0cc86)>>26])  // ERROR "Found IsInBounds$"
    66  	useInt(b[int64(i*0x106297f105d0cc86)>>57])  // ERROR "Found IsInBounds$"
    67  }
    68  
    69  func g1(a []int) {
    70  	for i := range a {
    71  		a[i] = i
    72  		useSlice(a[:i+1])
    73  		useSlice(a[:i])
    74  	}
    75  }
    76  
    77  func g2(a []int) {
    78  	useInt(a[3]) // ERROR "Found IsInBounds$"
    79  	useInt(a[2])
    80  	useInt(a[1])
    81  	useInt(a[0])
    82  }
    83  
    84  func g3(a []int) {
    85  	for i := range a[:256] { // ERROR "Found IsSliceInBounds$"
    86  		useInt(a[i]) // ERROR "Found IsInBounds$"
    87  	}
    88  	b := a[:256]
    89  	for i := range b {
    90  		useInt(b[i])
    91  	}
    92  }
    93  
    94  func g4(a [100]int) {
    95  	for i := 10; i < 50; i++ {
    96  		useInt(a[i-10])
    97  		useInt(a[i])
    98  		useInt(a[i+25])
    99  		useInt(a[i+50])
   100  
   101  		// The following are out of bounds.
   102  		useInt(a[i-11]) // ERROR "Found IsInBounds$"
   103  		useInt(a[i+51]) // ERROR "Found IsInBounds$"
   104  	}
   105  }
   106  
   107  //go:noinline
   108  func useInt(a int) {
   109  }
   110  
   111  //go:noinline
   112  func useSlice(a []int) {
   113  }
   114  
   115  func main() {
   116  }