github.com/primecitizens/pcz/std@v0.2.1/core/mem/move_linux_amd64_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2013 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build linux && amd64
     9  
    10  package mem_test
    11  
    12  import (
    13  	"os"
    14  	"syscall"
    15  	"testing"
    16  	"unsafe"
    17  )
    18  
    19  // TestMemmoveOverflow maps 3GB of memory and calls memmove on
    20  // the corresponding slice.
    21  func TestMemmoveOverflow(t *testing.T) {
    22  	t.Parallel()
    23  	// Create a temporary file.
    24  	tmp, err := os.CreateTemp("", "go-memmovetest")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	_, err = tmp.Write(make([]byte, 65536))
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	defer os.Remove(tmp.Name())
    33  	defer tmp.Close()
    34  
    35  	// Set up mappings.
    36  	base, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    37  		0xa0<<32, 3<<30, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_PRIVATE|syscall.MAP_ANONYMOUS, ^uintptr(0), 0)
    38  	if errno != 0 {
    39  		t.Skipf("could not create memory mapping: %s", errno)
    40  	}
    41  	syscall.Syscall(syscall.SYS_MUNMAP, base, 3<<30, 0)
    42  
    43  	for off := uintptr(0); off < 3<<30; off += 65536 {
    44  		_, _, errno := syscall.Syscall6(syscall.SYS_MMAP,
    45  			base+off, 65536, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_FIXED, tmp.Fd(), 0)
    46  		if errno != 0 {
    47  			t.Skipf("could not map a page at requested 0x%x: %s", base+off, errno)
    48  		}
    49  		defer syscall.Syscall(syscall.SYS_MUNMAP, base+off, 65536, 0)
    50  	}
    51  
    52  	s := unsafe.Slice((*byte)(unsafe.Pointer(base)), 3<<30)
    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  }