]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testsanitizers/msan_test.go
misc/cgo/test: add asan and msan arena tests
[gostls13.git] / misc / cgo / testsanitizers / msan_test.go
1 // Copyright 2017 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 sanitizers_test
6
7 import (
8         "strings"
9         "testing"
10 )
11
12 func TestMSAN(t *testing.T) {
13         goos, err := goEnv("GOOS")
14         if err != nil {
15                 t.Fatal(err)
16         }
17         goarch, err := goEnv("GOARCH")
18         if err != nil {
19                 t.Fatal(err)
20         }
21         // The msan tests require support for the -msan option.
22         if !mSanSupported(goos, goarch) {
23                 t.Skipf("skipping on %s/%s; -msan option is not supported.", goos, goarch)
24         }
25
26         t.Parallel()
27         requireOvercommit(t)
28         config := configure("memory")
29         config.skipIfCSanitizerBroken(t)
30
31         mustRun(t, config.goCmd("build", "std"))
32
33         cases := []struct {
34                 src         string
35                 wantErr     bool
36                 experiments []string
37         }{
38                 {src: "msan.go"},
39                 {src: "msan2.go"},
40                 {src: "msan2_cmsan.go"},
41                 {src: "msan3.go"},
42                 {src: "msan4.go"},
43                 {src: "msan5.go"},
44                 {src: "msan6.go"},
45                 {src: "msan7.go"},
46                 {src: "msan8.go"},
47                 {src: "msan_fail.go", wantErr: true},
48                 // This may not always fail specifically due to MSAN. It may sometimes
49                 // fail because of a fault. However, we don't care what kind of error we
50                 // get here, just that we get an error. This is an MSAN test because without
51                 // MSAN it would not fail deterministically.
52                 {src: "arena_fail.go", wantErr: true, experiments: []string{"arenas"}},
53         }
54         for _, tc := range cases {
55                 tc := tc
56                 name := strings.TrimSuffix(tc.src, ".go")
57                 t.Run(name, func(t *testing.T) {
58                         t.Parallel()
59
60                         dir := newTempDir(t)
61                         defer dir.RemoveAll(t)
62
63                         outPath := dir.Join(name)
64                         mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
65
66                         cmd := hangProneCmd(outPath)
67                         if tc.wantErr {
68                                 out, err := cmd.CombinedOutput()
69                                 if err != nil {
70                                         return
71                                 }
72                                 t.Fatalf("%#q exited without error; want MSAN failure\n%s", strings.Join(cmd.Args, " "), out)
73                         }
74                         mustRun(t, cmd)
75                 })
76         }
77 }