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