forwprop: Also dce from added statements from gimple_simplify
[official-gcc.git] / libgo / go / runtime / checkptr_test.go
blob584913993d1e0f0f03ceed7ee0c4affd25486d24
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.
5 package runtime_test
7 import (
8 "internal/testenv"
9 "os/exec"
10 "runtime"
11 "strings"
12 "testing"
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.
22 if testing.Short() {
23 t.Skip("skipping test in -short mode")
26 t.Parallel()
27 testenv.MustHaveGoRun(t)
29 exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
30 if err != nil {
31 t.Fatal(err)
34 testCases := []struct {
35 cmd string
36 want string
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 {
50 tc := tc
51 t.Run(tc.cmd, func(t *testing.T) {
52 t.Parallel()
53 got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
54 if err != nil {
55 t.Log(err)
57 if tc.want == "" {
58 if len(got) > 0 {
59 t.Errorf("output:\n%s\nwant no output", got)
61 return
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.
73 if testing.Short() {
74 t.Skip("skipping test in -short mode")
77 t.Parallel()
78 testenv.MustHaveGoRun(t)
80 exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=2")
81 if err != nil {
82 t.Fatal(err)
85 testCases := []struct {
86 cmd string
87 want string
89 {"CheckPtrAlignmentNested", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
92 for _, tc := range testCases {
93 tc := tc
94 t.Run(tc.cmd, func(t *testing.T) {
95 t.Parallel()
96 got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
97 if err != nil {
98 t.Log(err)
100 if tc.want == "" {
101 if len(got) > 0 {
102 t.Errorf("output:\n%s\nwant no output", got)
104 return
106 if !strings.HasPrefix(string(got), tc.want) {
107 t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)