| 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 |
"internal/asan" |
| 9 |
"os" |
| 10 |
"syscall" |
| 11 |
"testing" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
// TestMemmoveOverflow maps 3GB of memory and calls memmove on |
| 16 |
// the corresponding slice. |
| 17 |
func TestMemmoveOverflow(t *testing.T) { |
| 18 |
if asan.Enabled { |
| 19 |
t.Skip("appears to break asan and causes spurious failures") |
| 20 |
} |
| 21 |
|
| 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 |
} |