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.
15 func TestCheckPtr(t
*testing
.T
) {
16 if runtime
.Compiler
== "gccgo" {
17 t
.Skip("gccgo does not have -d=checkptr")
20 // This test requires rebuilding packages with -d=checkptr=1,
21 // so it's somewhat slow.
23 t
.Skip("skipping test in -short mode")
27 testenv
.MustHaveGoRun(t
)
29 exe
, err
:= buildTestProg(t
, "testprog", "-gcflags=all=-d=checkptr=1")
34 testCases
:= []struct {
38 {"CheckPtrAlignmentPtr", "fatal error: checkptr: misaligned pointer conversion\n"},
39 {"CheckPtrAlignmentNoPtr", ""},
40 {"CheckPtrAlignmentNilPtr", ""},
41 {"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
42 {"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
43 {"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
44 {"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
45 {"CheckPtrSliceOK", ""},
46 {"CheckPtrSliceFail", "fatal error: checkptr: unsafe.Slice result straddles multiple allocations\n"},
49 for _
, tc
:= range testCases
{
51 t
.Run(tc
.cmd
, func(t
*testing
.T
) {
53 got
, err
:= testenv
.CleanCmdEnv(exec
.Command(exe
, tc
.cmd
)).CombinedOutput()
59 t
.Errorf("output:\n%s\nwant no output", got
)
63 if !strings
.HasPrefix(string(got
), tc
.want
) {
64 t
.Errorf("output:\n%s\n\nwant output starting with: %s", got
, tc
.want
)
70 func TestCheckPtr2(t
*testing
.T
) {
71 // This test requires rebuilding packages with -d=checkptr=2,
72 // so it's somewhat slow.
74 t
.Skip("skipping test in -short mode")
78 testenv
.MustHaveGoRun(t
)
80 exe
, err
:= buildTestProg(t
, "testprog", "-gcflags=all=-d=checkptr=2")
85 testCases
:= []struct {
89 {"CheckPtrAlignmentNested", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
92 for _
, tc
:= range testCases
{
94 t
.Run(tc
.cmd
, func(t
*testing
.T
) {
96 got
, err
:= testenv
.CleanCmdEnv(exec
.Command(exe
, tc
.cmd
)).CombinedOutput()
102 t
.Errorf("output:\n%s\nwant no output", got
)
106 if !strings
.HasPrefix(string(got
), tc
.want
) {
107 t
.Errorf("output:\n%s\n\nwant output starting with: %s", got
, tc
.want
)