gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/gohacks/gohacks_test.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gohacks
    16  
    17  import (
    18  	"io/ioutil"
    19  	"math/rand"
    20  	"os"
    21  	"runtime"
    22  	"runtime/debug"
    23  	"testing"
    24  	"time"
    25  	"unsafe"
    26  
    27  	"golang.org/x/sys/unix"
    28  )
    29  
    30  func randBuf(size int) []byte {
    31  	b := make([]byte, size)
    32  	for i := range b {
    33  		b[i] = byte(rand.Intn(256))
    34  	}
    35  	return b
    36  }
    37  
    38  // Size of a page in bytes. Cloned from hostarch.PageSize to avoid a circular
    39  // dependency.
    40  const pageSize = 4096
    41  
    42  func testCopy(dst, src []byte) (panicked bool) {
    43  	defer func() {
    44  		if r := recover(); r != nil {
    45  			panicked = true
    46  		}
    47  	}()
    48  	debug.SetPanicOnFault(true)
    49  	copy(dst, src)
    50  	return panicked
    51  }
    52  
    53  func TestSegVOnMemmove(t *testing.T) {
    54  	// Test that SIGSEGVs received by runtime.memmove when *not* doing
    55  	// CopyIn or CopyOut work gets propagated to the runtime.
    56  	const bufLen = pageSize
    57  	a, err := unix.Mmap(-1, 0, bufLen, unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
    58  	if err != nil {
    59  		t.Fatalf("Mmap failed: %v", err)
    60  
    61  	}
    62  	defer unix.Munmap(a)
    63  	b := randBuf(bufLen)
    64  
    65  	if !testCopy(b, a) {
    66  		t.Fatalf("testCopy didn't panic when it should have")
    67  	}
    68  
    69  	if !testCopy(a, b) {
    70  		t.Fatalf("testCopy didn't panic when it should have")
    71  	}
    72  }
    73  
    74  func TestSigbusOnMemmove(t *testing.T) {
    75  	// Test that SIGBUS received by runtime.memmove when *not* doing
    76  	// CopyIn or CopyOut work gets propagated to the runtime.
    77  	const bufLen = pageSize
    78  	f, err := ioutil.TempFile("", "sigbus_test")
    79  	if err != nil {
    80  		t.Fatalf("TempFile failed: %v", err)
    81  	}
    82  	os.Remove(f.Name())
    83  	defer f.Close()
    84  
    85  	a, err := unix.Mmap(int(f.Fd()), 0, bufLen, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
    86  	if err != nil {
    87  		t.Fatalf("Mmap failed: %v", err)
    88  
    89  	}
    90  	defer unix.Munmap(a)
    91  	b := randBuf(bufLen)
    92  
    93  	if !testCopy(b, a) {
    94  		t.Fatalf("testCopy didn't panic when it should have")
    95  	}
    96  
    97  	if !testCopy(a, b) {
    98  		t.Fatalf("testCopy didn't panic when it should have")
    99  	}
   100  }
   101  
   102  func TestNanotime(t *testing.T) {
   103  	// Verify that nanotime increases over time.
   104  	nano1 := Nanotime()
   105  	time.Sleep(10 * time.Millisecond)
   106  	nano2 := Nanotime()
   107  	if nano2 <= nano1 {
   108  		t.Errorf("runtime.nanotime() did not increase after 10ms: %d vs %d", nano1, nano2)
   109  	}
   110  }
   111  
   112  // +checkescape:heap
   113  //
   114  //go:noinline
   115  func NoescapeAlloc() unsafe.Pointer {
   116  	// This is obviously quite dangerous, and we presumably return a pointer to a
   117  	// 16-byte object that is allocated on the local stack. This pointer should
   118  	// never be used for anything (or saved anywhere). The function is exported
   119  	// and marked as noinline in order to ensure that it is still defined as is.
   120  	var m [16]byte // 16-byte object.
   121  	return Noescape(unsafe.Pointer(&m))
   122  }
   123  
   124  // ptrs is used to ensure that when the compiler is analyzing TestNoescape, it
   125  // cannot simply eliminate the entire relevant block of code, realizing that it
   126  // does not have any side effects. This is much harder with a global, unless
   127  // the compiler implements whole program analysis.
   128  var ptrs [1024]uintptr
   129  
   130  func TestNoescape(t *testing.T) {
   131  	var (
   132  		beforeStats runtime.MemStats
   133  		afterStats  runtime.MemStats
   134  	)
   135  
   136  	// Ensure referenced objects don't escape.
   137  	runtime.ReadMemStats(&beforeStats)
   138  	for i := 0; i < len(ptrs); i++ {
   139  		ptrs[i] = uintptr(NoescapeAlloc())
   140  	}
   141  	runtime.ReadMemStats(&afterStats)
   142  
   143  	// Count the mallocs to check if it escaped.
   144  	if afterStats.Mallocs-beforeStats.Mallocs >= uint64(len(ptrs)) {
   145  		t.Errorf("Noescape did not prevent escapes to the heap")
   146  	}
   147  
   148  	// Use ptrs to ensure the loop above isn't optimized out. As noted above,
   149  	// this is already quite difficult with the global, but we may as well make
   150  	// it slightly harder by introducing a sanity check for the values here.
   151  	for _, p := range ptrs {
   152  		if p == 0 {
   153  			t.Errorf("got nil ptr, expected non-nil")
   154  		}
   155  	}
   156  }