github.com/aloncn/graphics-go@v0.0.1/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  	// Create a temporary file.
    20  	tmp, err := ioutil.TempFile("", "go-memmovetest")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	_, err = tmp.Write(make([]byte, 65536))
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	defer os.Remove(tmp.Name())
    29  	defer tmp.Close()
    30  
    31  	// Set up mappings.
    32  	base, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    33  		0xa0<<32, 3<<30, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_PRIVATE|syscall.MAP_ANONYMOUS, ^uintptr(0), 0)
    34  	if errno != 0 {
    35  		t.Skipf("could not create memory mapping: %s", errno)
    36  	}
    37  	syscall.Syscall(syscall.SYS_MUNMAP, base, 3<<30, 0)
    38  
    39  	for off := uintptr(0); off < 3<<30; off += 65536 {
    40  		_, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    41  			base+off, 65536, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_FIXED, tmp.Fd(), 0)
    42  		if errno != 0 {
    43  			t.Fatalf("could not map a page at requested 0x%x: %s", base+off, errno)
    44  		}
    45  		defer syscall.Syscall(syscall.SYS_MUNMAP, base+off, 65536, 0)
    46  	}
    47  
    48  	var s []byte
    49  	sp := (*reflect.SliceHeader)(unsafe.Pointer(&s))
    50  	sp.Data = base
    51  	sp.Len, sp.Cap = 3<<30, 3<<30
    52  
    53  	n := copy(s[1:], s)
    54  	if n != 3<<30-1 {
    55  		t.Fatalf("copied %d bytes, expected %d", n, 3<<30-1)
    56  	}
    57  	n = copy(s, s[1:])
    58  	if n != 3<<30-1 {
    59  		t.Fatalf("copied %d bytes, expected %d", n, 3<<30-1)
    60  	}
    61  }