File tree 2 files changed +82
-0
lines changed 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
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 \n want output starting with: %s" , got , tc .want )
43
+ }
44
+ })
45
+ }
46
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments