github.com/fenixara/go@v0.0.0-20170127160404-96ea0918e670/src/runtime/memmove_linux_amd64_test.go (about)

     1  // Copyright 2013 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  	"io/ioutil"
     9  	"os"
    10  	"reflect"
    11  	"syscall"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  // TestMemmoveOverflow maps 3GB of memory and calls memmove on
    17  // the corresponding slice.
    18  func TestMemmoveOverflow(t *testing.T) {
    19  	t.Parallel()
    20  	// Create a temporary file.
    21  	tmp, err := ioutil.TempFile("", "go-memmovetest")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	_, err = tmp.Write(make([]byte, 65536))
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	defer os.Remove(tmp.Name())
    30  	defer tmp.Close()
    31  
    32  	// Set up mappings.
    33  	base, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    34  		0xa0<<32, 3<<30, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_PRIVATE|syscall.MAP_ANONYMOUS, ^uintptr(0), 0)
    35  	if errno != 0 {
    36  		t.Skipf("could not create memory mapping: %s", errno)
    37  	}
    38  	syscall.Syscall(syscall.SYS_MUNMAP, base, 3<<30, 0)
    39  
    40  	for off := uintptr(0); off < 3<<30; off += 65536 {
    41  		_, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    42  			base+off, 65536, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_FIXED, tmp.Fd(), 0)
    43  		if errno != 0 {
    44  			t.Skipf("could not map a page at requested 0x%x: %s", base+off, errno)
    45  		}
    46  		defer syscall.Syscall(syscall.SYS_MUNMAP, base+off, 65536, 0)
    47  	}
    48  
    49  	var s []byte
    50  	sp := (*reflect.SliceHeader)(unsafe.Pointer(&s))
    51  	sp.Data = base
    52  	sp.Len, sp.Cap = 3<<30, 3<<30
    53  
    54  	n := copy(s[1:], s)
    55  	if n != 3<<30-1 {
    56  		t.Fatalf("copied %d bytes, expected %d", n, 3<<30-1)
    57  	}
    58  	n = copy(s, s[1:])
    59  	if n != 3<<30-1 {
    60  		t.Fatalf("copied %d bytes, expected %d", n, 3<<30-1)
    61  	}
    62  }