github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/fixedbugs_test.go (about)

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