Source file test/fixedbugs/issue26407.go

     1  // run
     2  
     3  // Copyright 2018 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Issue 26407: ensure that stack variables which have
     8  // had their address taken and then used in a comparison,
     9  // but are otherwise unused, are cleared.
    10  
    11  package main
    12  
    13  func main() {
    14  	poison()
    15  	test()
    16  }
    17  
    18  //go:noinline
    19  func poison() {
    20  	// initialise the stack with invalid pointers
    21  	var large [256]uintptr
    22  	for i := range large {
    23  		large[i] = 1
    24  	}
    25  	use(large[:])
    26  }
    27  
    28  //go:noinline
    29  func test() {
    30  	a := 2
    31  	x := &a
    32  	if x != compare(&x) {
    33  		panic("not possible")
    34  	}
    35  }
    36  
    37  //go:noinline
    38  func compare(x **int) *int {
    39  	var y *int
    40  	if x == &y {
    41  		panic("not possible")
    42  	}
    43  	// grow the stack to trigger a check for invalid pointers
    44  	grow()
    45  	if x == &y {
    46  		panic("not possible")
    47  	}
    48  	return *x
    49  }
    50  
    51  //go:noinline
    52  func grow() {
    53  	var large [1 << 16]uintptr
    54  	use(large[:])
    55  }
    56  
    57  //go:noinline
    58  func use(_ []uintptr) { }
    59  

View as plain text