Source file src/runtime/checkptr_test.go

     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  	// This test requires rebuilding packages with -d=checkptr=1,
    16  	// so it's somewhat slow.
    17  	if testing.Short() {
    18  		t.Skip("skipping test in -short mode")
    19  	}
    20  
    21  	t.Parallel()
    22  	testenv.MustHaveGoRun(t)
    23  
    24  	exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	testCases := []struct {
    30  		cmd  string
    31  		want string
    32  	}{
    33  		{"CheckPtrAlignmentPtr", "fatal error: checkptr: misaligned pointer conversion\n"},
    34  		{"CheckPtrAlignmentNoPtr", ""},
    35  		{"CheckPtrAlignmentNilPtr", ""},
    36  		{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
    37  		{"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
    38  		{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
    39  		{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
    40  		{"CheckPtrSliceOK", ""},
    41  		{"CheckPtrSliceFail", "fatal error: checkptr: unsafe.Slice result straddles multiple allocations\n"},
    42  	}
    43  
    44  	for _, tc := range testCases {
    45  		tc := tc
    46  		t.Run(tc.cmd, func(t *testing.T) {
    47  			t.Parallel()
    48  			got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
    49  			if err != nil {
    50  				t.Log(err)
    51  			}
    52  			if tc.want == "" {
    53  				if len(got) > 0 {
    54  					t.Errorf("output:\n%s\nwant no output", got)
    55  				}
    56  				return
    57  			}
    58  			if !strings.HasPrefix(string(got), tc.want) {
    59  				t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestCheckPtr2(t *testing.T) {
    66  	// This test requires rebuilding packages with -d=checkptr=2,
    67  	// so it's somewhat slow.
    68  	if testing.Short() {
    69  		t.Skip("skipping test in -short mode")
    70  	}
    71  
    72  	t.Parallel()
    73  	testenv.MustHaveGoRun(t)
    74  
    75  	exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=2")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	testCases := []struct {
    81  		cmd  string
    82  		want string
    83  	}{
    84  		{"CheckPtrAlignmentNested", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
    85  	}
    86  
    87  	for _, tc := range testCases {
    88  		tc := tc
    89  		t.Run(tc.cmd, func(t *testing.T) {
    90  			t.Parallel()
    91  			got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
    92  			if err != nil {
    93  				t.Log(err)
    94  			}
    95  			if tc.want == "" {
    96  				if len(got) > 0 {
    97  					t.Errorf("output:\n%s\nwant no output", got)
    98  				}
    99  				return
   100  			}
   101  			if !strings.HasPrefix(string(got), tc.want) {
   102  				t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
   103  			}
   104  		})
   105  	}
   106  }
   107  

View as plain text