| 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 |
"fmt" |
| 9 |
"os" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestShared(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
requireOvercommit(t) |
| 17 |
|
| 18 |
GOOS, err := goEnv("GOOS") |
| 19 |
if err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} |
| 22 |
|
| 23 |
GOARCH, err := goEnv("GOARCH") |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
|
| 28 |
libExt := "so" |
| 29 |
if GOOS == "darwin" { |
| 30 |
libExt = "dylib" |
| 31 |
} |
| 32 |
|
| 33 |
cases := []struct { |
| 34 |
src string |
| 35 |
sanitizer string |
| 36 |
}{ |
| 37 |
{ |
| 38 |
src: "msan_shared.go", |
| 39 |
sanitizer: "memory", |
| 40 |
}, |
| 41 |
{ |
| 42 |
src: "tsan_shared.go", |
| 43 |
sanitizer: "thread", |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tc := range cases { |
| 48 |
tc := tc |
| 49 |
name := strings.TrimSuffix(tc.src, ".go") |
| 50 |
//The memory sanitizer tests require support for the -msan option. |
| 51 |
if tc.sanitizer == "memory" && !mSanSupported(GOOS, GOARCH) { |
| 52 |
t.Logf("skipping %s test on %s/%s; -msan option is not supported.", name, GOOS, GOARCH) |
| 53 |
continue |
| 54 |
} |
| 55 |
t.Run(name, func(t *testing.T) { |
| 56 |
t.Parallel() |
| 57 |
config := configure(tc.sanitizer) |
| 58 |
config.skipIfCSanitizerBroken(t) |
| 59 |
|
| 60 |
dir := newTempDir(t) |
| 61 |
defer dir.RemoveAll(t) |
| 62 |
|
| 63 |
lib := dir.Join(fmt.Sprintf("lib%s.%s", name, libExt)) |
| 64 |
mustRun(t, config.goCmd("build", "-buildmode=c-shared", "-o", lib, srcPath(tc.src))) |
| 65 |
|
| 66 |
cSrc := dir.Join("main.c") |
| 67 |
if err := os.WriteFile(cSrc, cMain, 0600); err != nil { |
| 68 |
t.Fatalf("failed to write C source file: %v", err) |
| 69 |
} |
| 70 |
|
| 71 |
dstBin := dir.Join(name) |
| 72 |
cmd, err := cc(config.cFlags...) |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
cmd.Args = append(cmd.Args, config.ldFlags...) |
| 77 |
cmd.Args = append(cmd.Args, "-o", dstBin, cSrc, lib) |
| 78 |
mustRun(t, cmd) |
| 79 |
|
| 80 |
cmd = hangProneCmd(dstBin) |
| 81 |
replaceEnv(cmd, "LD_LIBRARY_PATH", ".") |
| 82 |
mustRun(t, cmd) |
| 83 |
}) |
| 84 |
} |
| 85 |
} |