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 5package runtime_test 6 7import ( 8 "internal/testenv" 9 "os/exec" 10 "runtime" 11 "strings" 12 "testing" 13) 14 15func TestCheckPtr(t *testing.T) { 16 if runtime.Compiler == "gccgo" { 17 t.Skip("gccgo does not have -d=checkptr") 18 } 19 t.Parallel() 20 testenv.MustHaveGoRun(t) 21 22 exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1") 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 testCases := []struct { 28 cmd string 29 want string 30 }{ 31 {"CheckPtrAlignmentPtr", "fatal error: checkptr: unsafe pointer conversion\n"}, 32 {"CheckPtrAlignmentNoPtr", ""}, 33 {"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"}, 34 {"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"}, 35 {"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"}, 36 } 37 38 for _, tc := range testCases { 39 tc := tc 40 t.Run(tc.cmd, func(t *testing.T) { 41 t.Parallel() 42 got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput() 43 if err != nil { 44 t.Log(err) 45 } 46 if tc.want == "" { 47 if len(got) > 0 { 48 t.Errorf("output:\n%s\nwant no output", got) 49 } 50 return 51 } 52 if !strings.HasPrefix(string(got), tc.want) { 53 t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want) 54 } 55 }) 56 } 57} 58