package sanitizers_test
import (
+ "bytes"
"fmt"
"internal/platform"
"internal/testenv"
)
func TestASAN(t *testing.T) {
- testenv.MustHaveGoBuild(t)
- testenv.MustHaveCGO(t)
- goos, err := goEnv("GOOS")
- if err != nil {
- t.Fatal(err)
- }
- goarch, err := goEnv("GOARCH")
- if err != nil {
- t.Fatal(err)
- }
- // The asan tests require support for the -asan option.
- if !platform.ASanSupported(goos, goarch) {
- t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
- }
- // The current implementation is only compatible with the ASan library from version
- // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
- // -asan option must use a compatible version of ASan library, which requires that
- // the gcc version is not less than 7 and the clang version is not less than 9,
- // otherwise a segmentation fault will occur.
- if !compilerRequiredAsanVersion(goos, goarch) {
- t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
- }
+ config := mustHaveASAN(t)
t.Parallel()
- requireOvercommit(t)
- config := configure("address")
- config.skipIfCSanitizerBroken(t)
-
mustRun(t, config.goCmd("build", "std"))
cases := []struct {
}
func TestASANLinkerX(t *testing.T) {
+ // Test ASAN with linker's -X flag (see issue 56175).
+ config := mustHaveASAN(t)
+
+ t.Parallel()
+
+ dir := newTempDir(t)
+ defer dir.RemoveAll(t)
+
+ var ldflags string
+ for i := 1; i <= 10; i++ {
+ ldflags += fmt.Sprintf("-X=main.S%d=%d -X=cmd/cgo/internal/testsanitizers/testdata/asan_linkerx/p.S%d=%d ", i, i, i, i)
+ }
+
+ // build the binary
+ outPath := dir.Join("main.exe")
+ cmd := config.goCmd("build", "-ldflags="+ldflags, "-o", outPath)
+ cmd.Dir = srcPath("asan_linkerx")
+ mustRun(t, cmd)
+
+ // run the binary
+ mustRun(t, hangProneCmd(outPath))
+}
+
+// Issue 66966.
+func TestASANFuzz(t *testing.T) {
+ config := mustHaveASAN(t)
+
+ t.Parallel()
+
+ dir := newTempDir(t)
+ defer dir.RemoveAll(t)
+
+ cmd := config.goCmd("test", "-fuzz=Fuzz", srcPath("asan_fuzz_test.go"))
+ t.Logf("%v", cmd)
+ out, err := cmd.CombinedOutput()
+ t.Logf("%s", out)
+ if err == nil {
+ t.Error("expected fuzzing failure")
+ }
+ if bytes.Contains(out, []byte("AddressSanitizer")) {
+ t.Error(`output contains "AddressSanitizer", but should not`)
+ }
+}
+
+func mustHaveASAN(t *testing.T) *config {
testenv.MustHaveGoBuild(t)
testenv.MustHaveCGO(t)
- // Test ASAN with linker's -X flag (see issue 56175).
goos, err := goEnv("GOOS")
if err != nil {
t.Fatal(err)
if err != nil {
t.Fatal(err)
}
- // The asan tests require support for the -asan option.
if !platform.ASanSupported(goos, goarch) {
t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
}
+
+ // The current implementation is only compatible with the ASan library from version
+ // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
+ // -asan option must use a compatible version of ASan library, which requires that
+ // the gcc version is not less than 7 and the clang version is not less than 9,
+ // otherwise a segmentation fault will occur.
if !compilerRequiredAsanVersion(goos, goarch) {
t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
}
- t.Parallel()
requireOvercommit(t)
+
config := configure("address")
config.skipIfCSanitizerBroken(t)
- dir := newTempDir(t)
- defer dir.RemoveAll(t)
-
- var ldflags string
- for i := 1; i <= 10; i++ {
- ldflags += fmt.Sprintf("-X=main.S%d=%d -X=cmd/cgo/internal/testsanitizers/testdata/asan_linkerx/p.S%d=%d ", i, i, i, i)
- }
-
- // build the binary
- outPath := dir.Join("main.exe")
- cmd := config.goCmd("build", "-ldflags="+ldflags, "-o", outPath)
- cmd.Dir = srcPath("asan_linkerx")
- mustRun(t, cmd)
-
- // run the binary
- mustRun(t, hangProneCmd(outPath))
+ return config
}