Source file src/cmd/compile/internal/test/fixedbugs_test.go

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"internal/testenv"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  type T struct {
    16  	x [2]int64 // field that will be clobbered. Also makes type not SSAable.
    17  	p *byte    // has a pointer
    18  }
    19  
    20  //go:noinline
    21  func makeT() T {
    22  	return T{}
    23  }
    24  
    25  var g T
    26  
    27  var sink interface{}
    28  
    29  func TestIssue15854(t *testing.T) {
    30  	for i := 0; i < 10000; i++ {
    31  		if g.x[0] != 0 {
    32  			t.Fatalf("g.x[0] clobbered with %x\n", g.x[0])
    33  		}
    34  		// The bug was in the following assignment. The return
    35  		// value of makeT() is not copied out of the args area of
    36  		// stack frame in a timely fashion. So when write barriers
    37  		// are enabled, the marshaling of the args for the write
    38  		// barrier call clobbers the result of makeT() before it is
    39  		// read by the write barrier code.
    40  		g = makeT()
    41  		sink = make([]byte, 1000) // force write barriers to eventually happen
    42  	}
    43  }
    44  func TestIssue15854b(t *testing.T) {
    45  	const N = 10000
    46  	a := make([]T, N)
    47  	for i := 0; i < N; i++ {
    48  		a = append(a, makeT())
    49  		sink = make([]byte, 1000) // force write barriers to eventually happen
    50  	}
    51  	for i, v := range a {
    52  		if v.x[0] != 0 {
    53  			t.Fatalf("a[%d].x[0] clobbered with %x\n", i, v.x[0])
    54  		}
    55  	}
    56  }
    57  
    58  // Test that the generated assembly has line numbers (Issue #16214).
    59  func TestIssue16214(t *testing.T) {
    60  	testenv.MustHaveGoBuild(t)
    61  	dir := t.TempDir()
    62  
    63  	src := filepath.Join(dir, "x.go")
    64  	err := os.WriteFile(src, []byte(issue16214src), 0644)
    65  	if err != nil {
    66  		t.Fatalf("could not write file: %v", err)
    67  	}
    68  
    69  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=main", "-S", "-o", filepath.Join(dir, "out.o"), src)
    70  	out, err := cmd.CombinedOutput()
    71  	if err != nil {
    72  		t.Fatalf("go tool compile: %v\n%s", err, out)
    73  	}
    74  
    75  	if strings.Contains(string(out), "unknown line number") {
    76  		t.Errorf("line number missing in assembly:\n%s", out)
    77  	}
    78  }
    79  
    80  var issue16214src = `
    81  package main
    82  
    83  func Mod32(x uint32) uint32 {
    84  	return x % 3 // frontend rewrites it as HMUL with 2863311531, the LITERAL node has unknown Pos
    85  }
    86  `
    87  

View as plain text