Source file test/fixedbugs/issue45851.go

     1  // run
     2  
     3  // Copyright 2021 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  // This tickles a stack-allocation bug when the register ABI is enabled.
     8  // The original report was from cue, internal/core/adt/equality.go,
     9  // function equalVertex.
    10  
    11  // In the failing case, something bad gets passed to equalTerminal.
    12  
    13  package main
    14  
    15  import "fmt"
    16  
    17  type Kind uint16
    18  type Flag uint16
    19  
    20  const (
    21  	allKinds Kind = 1
    22  	TopKind  Kind = (allKinds - 1)
    23  )
    24  type Value interface {
    25  	Kind() Kind
    26  }
    27  type Vertex struct {
    28  	BaseValue Value
    29  	name string
    30  }
    31  func (v *Vertex) Kind() Kind {
    32  	return TopKind
    33  }
    34  
    35  func main() {
    36  	vA := &Vertex{name:"vA",}
    37  	vB := &Vertex{name:"vB",}
    38  	vX := &Vertex{name:"vX",}
    39  	vA.BaseValue = vX
    40  	vB.BaseValue = vX
    41  	_ = equalVertex(vA, vB, Flag(1))
    42  }
    43  
    44  var foo string
    45  
    46  //go:noinline
    47  func (v *Vertex) IsClosedStruct() bool {
    48  	return true
    49  }
    50  
    51  func equalVertex(x *Vertex, v Value, flags Flag) bool {
    52  	y, ok := v.(*Vertex)
    53  	if !ok {
    54  		return false
    55  	}
    56  	v, ok1 := x.BaseValue.(Value)
    57  	w, ok2 := y.BaseValue.(Value)
    58  	if !ok1 && !ok2 {
    59  		return true // both are struct or list.
    60  	}
    61  	return equalTerminal(v, w, flags)
    62  }
    63  
    64  //go:noinline
    65  func equalTerminal(x Value, y Value, flags Flag) bool {
    66  	foo = fmt.Sprintf("EQclosed %s %s %d\n", x.(*Vertex).name, y.(*Vertex).name, flags)
    67  	return true
    68  }
    69  

View as plain text