Skip to content

Commit

Permalink
cmd/cgo/internal/testsanitizers: fix TSAN tests using setarch
Browse files Browse the repository at this point in the history
Some systems don't have permissions to run setarch, for example
when running in a docker container without the --privileged flag.

This change makes the tests skip the setarch command if it fails.

Fixes #70463

Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-asan-clang15
Change-Id: I02fbd423ba809f5229b8639c9abe6fd275f32558
Reviewed-on: https://go-review.googlesource.com/c/go/+/630096
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
qmuntal committed Nov 20, 2024
1 parent 2ad53d5 commit 5254e98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/cmd/cgo/internal/testsanitizers/cshared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ func TestShared(t *testing.T) {

cmdArgs := []string{dstBin}
if tc.sanitizer == "thread" && GOOS == "linux" {
// Disable ASLR for TSAN. See #59418.
arch, err := exec.Command("uname", "-m").Output()
// Disable ASLR for TSAN. See https://go.dev/issue/59418.
out, err := exec.Command("uname", "-m").Output()
if err != nil {
t.Fatalf("failed to run `uname -m`: %v", err)
}
cmdArgs = []string{"setarch", strings.TrimSpace(string(arch)), "-R", dstBin}
arch := strings.TrimSpace(string(out))
if _, err := exec.Command("setarch", arch, "-R", "true").Output(); err != nil {
// Some systems don't have permission to run `setarch`.
// See https://go.dev/issue/70463.
t.Logf("failed to run `setarch %s -R true`: %v", arch, err)
} else {
cmdArgs = []string{"setarch", arch, "-R", dstBin}
}
}
cmd = hangProneCmd(cmdArgs[0], cmdArgs[1:]...)
replaceEnv(cmd, "LD_LIBRARY_PATH", ".")
Expand Down
13 changes: 10 additions & 3 deletions src/cmd/cgo/internal/testsanitizers/tsan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ func TestTSAN(t *testing.T) {

cmdArgs := []string{outPath}
if goos == "linux" {
// Disable ASLR. See #59418.
arch, err := exec.Command("uname", "-m").Output()
// Disable ASLR for TSAN. See https://go.dev/issue/59418.
out, err := exec.Command("uname", "-m").Output()
if err != nil {
t.Fatalf("failed to run `uname -m`: %v", err)
}
cmdArgs = []string{"setarch", strings.TrimSpace(string(arch)), "-R", outPath}
arch := strings.TrimSpace(string(out))
if _, err := exec.Command("setarch", arch, "-R", "true").Output(); err != nil {
// Some systems don't have permission to run `setarch`.
// See https://go.dev/issue/70463.
t.Logf("failed to run `setarch %s -R true`: %v", arch, err)
} else {
cmdArgs = []string{"setarch", arch, "-R", outPath}
}
}
cmd := hangProneCmd(cmdArgs[0], cmdArgs[1:]...)
if tc.needsRuntime {
Expand Down

0 comments on commit 5254e98

Please sign in to comment.