Skip to content

Commit 5d00751

Browse files
committed
runtime: add tests for checkptr
We had a few test cases to make sure checkptr didn't have certain false positives, but none to test for any true positives. This CL fixes that. Updates #22218. Change-Id: I24c02e469a4af43b1748829a9df325ce510f7cc4 Reviewed-on: https://go-review.googlesource.com/c/go/+/214238 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent ae0b735 commit 5d00751

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/runtime/checkptr_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
t.Parallel()
16+
testenv.MustHaveGoRun(t)
17+
18+
exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
23+
testCases := []struct {
24+
cmd string
25+
want string
26+
}{
27+
{"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
28+
{"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
29+
{"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
30+
{"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
31+
}
32+
33+
for _, tc := range testCases {
34+
tc := tc
35+
t.Run(tc.cmd, func(t *testing.T) {
36+
t.Parallel()
37+
got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
38+
if err != nil {
39+
t.Log(err)
40+
}
41+
if !strings.HasPrefix(string(got), tc.want) {
42+
t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
43+
}
44+
})
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 main
6+
7+
import "unsafe"
8+
9+
func init() {
10+
register("CheckPtrAlignment", CheckPtrAlignment)
11+
register("CheckPtrArithmetic", CheckPtrArithmetic)
12+
register("CheckPtrSize", CheckPtrSize)
13+
register("CheckPtrSmall", CheckPtrSmall)
14+
}
15+
16+
func CheckPtrAlignment() {
17+
var x [2]int64
18+
p := unsafe.Pointer(&x[0])
19+
sink2 = (*int64)(unsafe.Pointer(uintptr(p) + 1))
20+
}
21+
22+
func CheckPtrArithmetic() {
23+
var x int
24+
i := uintptr(unsafe.Pointer(&x))
25+
sink2 = (*int)(unsafe.Pointer(i))
26+
}
27+
28+
func CheckPtrSize() {
29+
p := new(int64)
30+
sink2 = p
31+
sink2 = (*[100]int64)(unsafe.Pointer(p))
32+
}
33+
34+
func CheckPtrSmall() {
35+
sink2 = unsafe.Pointer(uintptr(1))
36+
}

0 commit comments

Comments
 (0)