| 1 |
// Copyright 2020 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 runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/testenv" |
| 9 |
"os/exec" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCheckPtr(t *testing.T) { |
| 15 |
// This test requires rebuilding packages with -d=checkptr=1, |
| 16 |
// so it's somewhat slow. |
| 17 |
if testing.Short() { |
| 18 |
t.Skip("skipping test in -short mode") |
| 19 |
} |
| 20 |
|
| 21 |
t.Parallel() |
| 22 |
testenv.MustHaveGoRun(t) |
| 23 |
|
| 24 |
exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1") |
| 25 |
if err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
|
| 29 |
testCases := []struct { |
| 30 |
cmd string |
| 31 |
want string |
| 32 |
}{ |
| 33 |
{"CheckPtrAlignmentPtr", "fatal error: checkptr: misaligned pointer conversion\n"}, |
| 34 |
{"CheckPtrAlignmentNoPtr", ""}, |
| 35 |
{"CheckPtrAlignmentNilPtr", ""}, |
| 36 |
{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, |
| 37 |
{"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, |
| 38 |
{"CheckPtrArithmeticUnsafeAdd", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, |
| 39 |
{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"}, |
| 40 |
{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"}, |
| 41 |
{"CheckPtrSliceOK", ""}, |
| 42 |
{"CheckPtrSliceFail", "fatal error: checkptr: unsafe.Slice result straddles multiple allocations\n"}, |
| 43 |
{"CheckPtrStringOK", ""}, |
| 44 |
{"CheckPtrStringFail", "fatal error: checkptr: unsafe.String result straddles multiple allocations\n"}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tc := range testCases { |
| 48 |
t.Run(tc.cmd, func(t *testing.T) { |
| 49 |
t.Parallel() |
| 50 |
got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput() |
| 51 |
if err != nil { |
| 52 |
t.Log(err) |
| 53 |
} |
| 54 |
if tc.want == "" { |
| 55 |
if len(got) > 0 { |
| 56 |
t.Errorf("output:\n%s\nwant no output", got) |
| 57 |
} |
| 58 |
return |
| 59 |
} |
| 60 |
if !strings.HasPrefix(string(got), tc.want) { |
| 61 |
t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want) |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestCheckPtr2(t *testing.T) { |
| 68 |
// This test requires rebuilding packages with -d=checkptr=2, |
| 69 |
// so it's somewhat slow. |
| 70 |
if testing.Short() { |
| 71 |
t.Skip("skipping test in -short mode") |
| 72 |
} |
| 73 |
|
| 74 |
t.Parallel() |
| 75 |
testenv.MustHaveGoRun(t) |
| 76 |
|
| 77 |
exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=2") |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
|
| 82 |
testCases := []struct { |
| 83 |
cmd string |
| 84 |
want string |
| 85 |
}{ |
| 86 |
{"CheckPtrAlignmentNested", "fatal error: checkptr: converted pointer straddles multiple allocations\n"}, |
| 87 |
} |
| 88 |
|
| 89 |
for _, tc := range testCases { |
| 90 |
t.Run(tc.cmd, func(t *testing.T) { |
| 91 |
t.Parallel() |
| 92 |
got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput() |
| 93 |
if err != nil { |
| 94 |
t.Log(err) |
| 95 |
} |
| 96 |
if tc.want == "" { |
| 97 |
if len(got) > 0 { |
| 98 |
t.Errorf("output:\n%s\nwant no output", got) |
| 99 |
} |
| 100 |
return |
| 101 |
} |
| 102 |
if !strings.HasPrefix(string(got), tc.want) { |
| 103 |
t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want) |
| 104 |
} |
| 105 |
}) |
| 106 |
} |
| 107 |
} |