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

View as plain text