]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add tests for checkptr
authorMatthew Dempsky <mdempsky@google.com>
Thu, 9 Jan 2020 23:26:01 +0000 (15:26 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 10 Jan 2020 21:40:21 +0000 (21:40 +0000)
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 <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/runtime/checkptr_test.go [new file with mode: 0644]
src/runtime/testdata/testprog/checkptr.go [new file with mode: 0644]

diff --git a/src/runtime/checkptr_test.go b/src/runtime/checkptr_test.go
new file mode 100644 (file)
index 0000000..c5f22cc
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+       "internal/testenv"
+       "os/exec"
+       "strings"
+       "testing"
+)
+
+func TestCheckPtr(t *testing.T) {
+       t.Parallel()
+       testenv.MustHaveGoRun(t)
+
+       exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       testCases := []struct {
+               cmd  string
+               want string
+       }{
+               {"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
+               {"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
+               {"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
+               {"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
+       }
+
+       for _, tc := range testCases {
+               tc := tc
+               t.Run(tc.cmd, func(t *testing.T) {
+                       t.Parallel()
+                       got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
+                       if err != nil {
+                               t.Log(err)
+                       }
+                       if !strings.HasPrefix(string(got), tc.want) {
+                               t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
+                       }
+               })
+       }
+}
diff --git a/src/runtime/testdata/testprog/checkptr.go b/src/runtime/testdata/testprog/checkptr.go
new file mode 100644 (file)
index 0000000..177db38
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "unsafe"
+
+func init() {
+       register("CheckPtrAlignment", CheckPtrAlignment)
+       register("CheckPtrArithmetic", CheckPtrArithmetic)
+       register("CheckPtrSize", CheckPtrSize)
+       register("CheckPtrSmall", CheckPtrSmall)
+}
+
+func CheckPtrAlignment() {
+       var x [2]int64
+       p := unsafe.Pointer(&x[0])
+       sink2 = (*int64)(unsafe.Pointer(uintptr(p) + 1))
+}
+
+func CheckPtrArithmetic() {
+       var x int
+       i := uintptr(unsafe.Pointer(&x))
+       sink2 = (*int)(unsafe.Pointer(i))
+}
+
+func CheckPtrSize() {
+       p := new(int64)
+       sink2 = p
+       sink2 = (*[100]int64)(unsafe.Pointer(p))
+}
+
+func CheckPtrSmall() {
+       sink2 = unsafe.Pointer(uintptr(1))
+}