summaryrefslogtreecommitdiff
path: root/libgo/go/runtime/checkptr_test.go
blob: 0ca7b20cfd43b6507e0f39e44174554690e4ba6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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"
	"runtime"
	"strings"
	"testing"
)

func TestCheckPtr(t *testing.T) {
	if runtime.Compiler == "gccgo" {
		t.Skip("gccgo does not have -d=checkptr")
	}
	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
	}{
		{"CheckPtrAlignmentPtr", "fatal error: checkptr: unsafe pointer conversion\n"},
		{"CheckPtrAlignmentNoPtr", ""},
		{"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 tc.want == "" {
				if len(got) > 0 {
					t.Errorf("output:\n%s\nwant no output", got)
				}
				return
			}
			if !strings.HasPrefix(string(got), tc.want) {
				t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
			}
		})
	}
}