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  	"bytes"
     9  	"internal/platform"
    10  	"internal/testenv"
    11  	"os"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  type T struct {
    19  	x [2]int64 // field that will be clobbered. Also makes type not SSAable.
    20  	p *byte    // has a pointer
    21  }
    22  
    23  //go:noinline
    24  func makeT() T {
    25  	return T{}
    26  }
    27  
    28  var g T
    29  
    30  var sink any
    31  
    32  func TestIssue15854(t *testing.T) {
    33  	for i := 0; i < 10000; i++ {
    34  		if g.x[0] != 0 {
    35  			t.Fatalf("g.x[0] clobbered with %x\n", g.x[0])
    36  		}
    37  		// The bug was in the following assignment. The return
    38  		// value of makeT() is not copied out of the args area of
    39  		// stack frame in a timely fashion. So when write barriers
    40  		// are enabled, the marshaling of the args for the write
    41  		// barrier call clobbers the result of makeT() before it is
    42  		// read by the write barrier code.
    43  		g = makeT()
    44  		sink = make([]byte, 1000) // force write barriers to eventually happen
    45  	}
    46  }
    47  func TestIssue15854b(t *testing.T) {
    48  	const N = 10000
    49  	a := make([]T, N)
    50  	for i := 0; i < N; i++ {
    51  		a = append(a, makeT())
    52  		sink = make([]byte, 1000) // force write barriers to eventually happen
    53  	}
    54  	for i, v := range a {
    55  		if v.x[0] != 0 {
    56  			t.Fatalf("a[%d].x[0] clobbered with %x\n", i, v.x[0])
    57  		}
    58  	}
    59  }
    60  
    61  // Test that the generated assembly has line numbers (Issue #16214).
    62  func TestIssue16214(t *testing.T) {
    63  	testenv.MustHaveGoBuild(t)
    64  	dir := t.TempDir()
    65  
    66  	src := filepath.Join(dir, "x.go")
    67  	err := os.WriteFile(src, []byte(issue16214src), 0644)
    68  	if err != nil {
    69  		t.Fatalf("could not write file: %v", err)
    70  	}
    71  
    72  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=main", "-S", "-o", filepath.Join(dir, "out.o"), src)
    73  	out, err := cmd.CombinedOutput()
    74  	if err != nil {
    75  		t.Fatalf("go tool compile: %v\n%s", err, out)
    76  	}
    77  
    78  	if strings.Contains(string(out), "unknown line number") {
    79  		t.Errorf("line number missing in assembly:\n%s", out)
    80  	}
    81  }
    82  
    83  var issue16214src = `
    84  package main
    85  
    86  func Mod32(x uint32) uint32 {
    87  	return x % 3 // frontend rewrites it as HMUL with 2863311531, the LITERAL node has unknown Pos
    88  }
    89  `
    90  
    91  // Test that building and running a program with -race -gcflags='all=-N -l'
    92  // does not crash. This is a regression test for #77597 where generic functions
    93  // from NoRaceFunc packages (like internal/runtime/atomic) were incorrectly
    94  // getting racefuncenter/racefuncexit instrumentation when instantiated in
    95  // other packages with optimizations disabled.
    96  func TestIssue77597(t *testing.T) {
    97  	if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
    98  		t.Skipf("race detector not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
    99  	}
   100  	testenv.MustHaveGoBuild(t)
   101  	testenv.MustHaveGoRun(t)
   102  	testenv.MustHaveCGO(t)
   103  
   104  	dir := t.TempDir()
   105  	src := filepath.Join(dir, "main.go")
   106  	err := os.WriteFile(src, []byte(issue77597src), 0644)
   107  	if err != nil {
   108  		t.Fatalf("could not write file: %v", err)
   109  	}
   110  
   111  	cmd := testenv.Command(t, testenv.GoToolPath(t), "run", "-race", "-gcflags=all=-N -l", src)
   112  	out, err := cmd.CombinedOutput()
   113  	if err != nil {
   114  		// For details, please refer to CL 160919.
   115  		unsupportedVMA := []byte("unsupported VMA range")
   116  		if bytes.Contains(out, unsupportedVMA) {
   117  			t.Skipf("skipped due to unsupported VMA on %s/%s", runtime.GOOS, runtime.GOARCH)
   118  		} else {
   119  			t.Fatalf("program failed: %v\n%s", err, out)
   120  		}
   121  	}
   122  }
   123  
   124  var issue77597src = `
   125  package main
   126  
   127  import "fmt"
   128  
   129  func main() {
   130  	fmt.Println("OK")
   131  }
   132  `
   133  

View as plain text