...

Source file src/runtime/checkptr_test.go

Documentation: runtime

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

View as plain text