Source file
src/runtime/memmove_linux_amd64_test.go
1
2
3
4
5 package runtime_test
6
7 import (
8 "os"
9 "reflect"
10 "syscall"
11 "testing"
12 "unsafe"
13 )
14
15
16
17 func TestMemmoveOverflow(t *testing.T) {
18 t.Parallel()
19
20 tmp, err := os.CreateTemp("", "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
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.Skipf("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 }
62
View as plain text