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

     1  // Copyright 2026 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/buildcfg"
     9  	"internal/platform"
    10  	"internal/testenv"
    11  	"os"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  // TestABIWrapperNoRaceInstrumentation verifies that compiler-generated
    19  // ABI0->ABIInternal wrappers are not instrumented under -race.
    20  func TestABIWrapperNoRaceInstrumentation(t *testing.T) {
    21  	if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
    22  		t.Skipf("race detector not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
    23  	}
    24  	if !buildcfg.Experiment.RegabiWrappers {
    25  		t.Skipf("ABI wrappers not enabled on %s/%s", runtime.GOOS, runtime.GOARCH)
    26  	}
    27  	testenv.MustHaveGoBuild(t)
    28  
    29  	dir := t.TempDir()
    30  	src := filepath.Join(dir, "x.go")
    31  	const code = `package main
    32  
    33  import _ "unsafe"
    34  
    35  var sink int
    36  
    37  //go:linkname F
    38  func F(x int) int {
    39  	sink = x
    40  	return x + 1
    41  }
    42  `
    43  	if err := os.WriteFile(src, []byte(code), 0644); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-race", "-p=main", "-S", "-o", filepath.Join(dir, "x.o"), src)
    48  	outBytes, err := cmd.CombinedOutput()
    49  	if err != nil {
    50  		t.Fatalf("compile failed: %v\n%s", err, outBytes)
    51  	}
    52  	out := string(outBytes)
    53  
    54  	lines := strings.Split(out, "\n")
    55  	var sawInternal, sawWrapper bool
    56  	for i := 0; i < len(lines); i++ {
    57  		if !strings.HasPrefix(lines[i], "main.F STEXT") {
    58  			continue
    59  		}
    60  		end := i + 1
    61  		for end < len(lines) && strings.HasPrefix(lines[end], "\t") {
    62  			end++
    63  		}
    64  		body := strings.Join(lines[i:end], "\n")
    65  		hasRace := strings.Contains(body, "runtime.racefuncenter") || strings.Contains(body, "runtime.racefuncexit")
    66  		if strings.Contains(body, "ABIWRAPPER") {
    67  			sawWrapper = true
    68  			if hasRace {
    69  				t.Errorf("main.F ABI wrapper unexpectedly instrumented:\n%s", body)
    70  			}
    71  		} else {
    72  			sawInternal = true
    73  			if !hasRace {
    74  				t.Errorf("main.F (ABIInternal) not instrumented:\n%s", body)
    75  			}
    76  		}
    77  		i = end - 1
    78  	}
    79  	if !sawInternal {
    80  		t.Fatalf("no ABIInternal assembly found for main.F:\n%s", out)
    81  	}
    82  	if !sawWrapper {
    83  		t.Fatalf("no ABIWRAPPER assembly found for main.F:\n%s", out)
    84  	}
    85  }
    86  

View as plain text