2012-02-10 22:19:24 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2013-09-13 12:19:23 -06:00
|
|
|
. "runtime"
|
2014-02-20 14:18:05 -07:00
|
|
|
"runtime/debug"
|
2012-02-10 22:19:24 -07:00
|
|
|
"testing"
|
2014-02-20 14:18:05 -07:00
|
|
|
"unsafe"
|
2012-02-10 22:19:24 -07:00
|
|
|
)
|
|
|
|
|
2016-01-14 14:43:40 -07:00
|
|
|
func init() {
|
|
|
|
// We're testing the runtime, so make tracebacks show things
|
|
|
|
// in the runtime. This only raises the level, so it won't
|
|
|
|
// override GOTRACEBACK=crash from the user.
|
|
|
|
SetTracebackEnv("system")
|
|
|
|
}
|
|
|
|
|
2012-02-10 22:19:24 -07:00
|
|
|
var errf error
|
|
|
|
|
|
|
|
func errfn() error {
|
|
|
|
return errf
|
|
|
|
}
|
|
|
|
|
|
|
|
func errfn1() error {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkIfaceCmp100(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
if errfn() == io.EOF {
|
|
|
|
b.Fatal("bad comparison")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkIfaceCmpNil100(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
if errfn1() == nil {
|
|
|
|
b.Fatal("bad comparison")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 12:54:39 -07:00
|
|
|
|
|
|
|
func BenchmarkDefer(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
defer1()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defer1() {
|
|
|
|
defer func(x, y, z int) {
|
|
|
|
if recover() != nil || x != 1 || y != 2 || z != 3 {
|
|
|
|
panic("bad recover")
|
|
|
|
}
|
|
|
|
}(1, 2, 3)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDefer10(b *testing.B) {
|
|
|
|
for i := 0; i < b.N/10; i++ {
|
|
|
|
defer2()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defer2() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
defer func(x, y, z int) {
|
|
|
|
if recover() != nil || x != 1 || y != 2 || z != 3 {
|
|
|
|
panic("bad recover")
|
|
|
|
}
|
|
|
|
}(1, 2, 3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDeferMany(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
defer func(x, y, z int) {
|
|
|
|
if recover() != nil || x != 1 || y != 2 || z != 3 {
|
|
|
|
panic("bad recover")
|
|
|
|
}
|
|
|
|
}(1, 2, 3)
|
|
|
|
}
|
|
|
|
}
|
2013-09-13 12:19:23 -06:00
|
|
|
|
2014-01-06 10:53:55 -07:00
|
|
|
// golang.org/issue/7063
|
|
|
|
func TestStopCPUProfilingWithProfilerOff(t *testing.T) {
|
|
|
|
SetCPUProfileRate(0)
|
|
|
|
}
|
2014-02-20 14:18:05 -07:00
|
|
|
|
runtime: test malformed address fault and fix on OS X
The garbage collector poison pointers
(0x6969696969696969 and 0x6868686868686868)
are malformed addresses on amd64.
That is, they are not 48-bit addresses sign extended
to 64 bits. This causes a different kind of hardware fault
than the usual 'unmapped page' when accessing such
an address, and OS X 10.9.2 sends the resulting SIGSEGV
incorrectly, making it look like it was user-generated
rather than kernel-generated and does not include the
faulting address. This means that in GODEBUG=gcdead=1
mode, if there is a bug and something tries to dereference
a poisoned pointer, the runtime delivers the SIGSEGV to
os/signal and returns to the faulting code, which faults
again, causing the process to hang instead of crashing.
Fix by rewriting "user-generated" SIGSEGV on OS X to
look like a kernel-generated SIGSEGV with fault address
0xb01dfacedebac1e.
I chose that address because (1) when printed in hex
during a crash, it is obviously spelling out English text,
(2) there are no current Google hits for that pointer,
which will make its origin easy to find once this CL
is indexed, and (3) it is not an altogether inaccurate
description of the situation.
Add a test. Maybe other systems will break too.
LGTM=khr
R=golang-codereviews, khr
CC=golang-codereviews, iant, ken
https://golang.org/cl/83270049
2014-04-03 17:07:33 -06:00
|
|
|
// Addresses to test for faulting behavior.
|
|
|
|
// This is less a test of SetPanicOnFault and more a check that
|
|
|
|
// the operating system and the runtime can process these faults
|
|
|
|
// correctly. That is, we're indirectly testing that without SetPanicOnFault
|
|
|
|
// these would manage to turn into ordinary crashes.
|
|
|
|
// Note that these are truncated on 32-bit systems, so the bottom 32 bits
|
|
|
|
// of the larger addresses must themselves be invalid addresses.
|
|
|
|
// We might get unlucky and the OS might have mapped one of these
|
|
|
|
// addresses, but probably not: they're all in the first page, very high
|
|
|
|
// adderesses that normally an OS would reserve for itself, or malformed
|
|
|
|
// addresses. Even so, we might have to remove one or two on different
|
|
|
|
// systems. We will see.
|
|
|
|
|
|
|
|
var faultAddrs = []uint64{
|
|
|
|
// low addresses
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0xfff,
|
|
|
|
// high (kernel) addresses
|
|
|
|
// or else malformed.
|
|
|
|
0xffffffffffffffff,
|
|
|
|
0xfffffffffffff001,
|
2014-09-18 19:43:09 -06:00
|
|
|
0xffffffffffff0001,
|
|
|
|
0xfffffffffff00001,
|
runtime: test malformed address fault and fix on OS X
The garbage collector poison pointers
(0x6969696969696969 and 0x6868686868686868)
are malformed addresses on amd64.
That is, they are not 48-bit addresses sign extended
to 64 bits. This causes a different kind of hardware fault
than the usual 'unmapped page' when accessing such
an address, and OS X 10.9.2 sends the resulting SIGSEGV
incorrectly, making it look like it was user-generated
rather than kernel-generated and does not include the
faulting address. This means that in GODEBUG=gcdead=1
mode, if there is a bug and something tries to dereference
a poisoned pointer, the runtime delivers the SIGSEGV to
os/signal and returns to the faulting code, which faults
again, causing the process to hang instead of crashing.
Fix by rewriting "user-generated" SIGSEGV on OS X to
look like a kernel-generated SIGSEGV with fault address
0xb01dfacedebac1e.
I chose that address because (1) when printed in hex
during a crash, it is obviously spelling out English text,
(2) there are no current Google hits for that pointer,
which will make its origin easy to find once this CL
is indexed, and (3) it is not an altogether inaccurate
description of the situation.
Add a test. Maybe other systems will break too.
LGTM=khr
R=golang-codereviews, khr
CC=golang-codereviews, iant, ken
https://golang.org/cl/83270049
2014-04-03 17:07:33 -06:00
|
|
|
0xffffffffff000001,
|
|
|
|
0xfffffffff0000001,
|
|
|
|
0xffffffff00000001,
|
|
|
|
0xfffffff000000001,
|
|
|
|
0xffffff0000000001,
|
|
|
|
0xfffff00000000001,
|
|
|
|
0xffff000000000001,
|
|
|
|
0xfff0000000000001,
|
|
|
|
0xff00000000000001,
|
|
|
|
0xf000000000000001,
|
|
|
|
0x8000000000000001,
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:18:05 -07:00
|
|
|
func TestSetPanicOnFault(t *testing.T) {
|
|
|
|
old := debug.SetPanicOnFault(true)
|
|
|
|
defer debug.SetPanicOnFault(old)
|
|
|
|
|
2014-09-18 19:43:09 -06:00
|
|
|
nfault := 0
|
runtime: test malformed address fault and fix on OS X
The garbage collector poison pointers
(0x6969696969696969 and 0x6868686868686868)
are malformed addresses on amd64.
That is, they are not 48-bit addresses sign extended
to 64 bits. This causes a different kind of hardware fault
than the usual 'unmapped page' when accessing such
an address, and OS X 10.9.2 sends the resulting SIGSEGV
incorrectly, making it look like it was user-generated
rather than kernel-generated and does not include the
faulting address. This means that in GODEBUG=gcdead=1
mode, if there is a bug and something tries to dereference
a poisoned pointer, the runtime delivers the SIGSEGV to
os/signal and returns to the faulting code, which faults
again, causing the process to hang instead of crashing.
Fix by rewriting "user-generated" SIGSEGV on OS X to
look like a kernel-generated SIGSEGV with fault address
0xb01dfacedebac1e.
I chose that address because (1) when printed in hex
during a crash, it is obviously spelling out English text,
(2) there are no current Google hits for that pointer,
which will make its origin easy to find once this CL
is indexed, and (3) it is not an altogether inaccurate
description of the situation.
Add a test. Maybe other systems will break too.
LGTM=khr
R=golang-codereviews, khr
CC=golang-codereviews, iant, ken
https://golang.org/cl/83270049
2014-04-03 17:07:33 -06:00
|
|
|
for _, addr := range faultAddrs {
|
2014-09-18 19:43:09 -06:00
|
|
|
testSetPanicOnFault(t, uintptr(addr), &nfault)
|
|
|
|
}
|
|
|
|
if nfault == 0 {
|
|
|
|
t.Fatalf("none of the addresses faulted")
|
runtime: test malformed address fault and fix on OS X
The garbage collector poison pointers
(0x6969696969696969 and 0x6868686868686868)
are malformed addresses on amd64.
That is, they are not 48-bit addresses sign extended
to 64 bits. This causes a different kind of hardware fault
than the usual 'unmapped page' when accessing such
an address, and OS X 10.9.2 sends the resulting SIGSEGV
incorrectly, making it look like it was user-generated
rather than kernel-generated and does not include the
faulting address. This means that in GODEBUG=gcdead=1
mode, if there is a bug and something tries to dereference
a poisoned pointer, the runtime delivers the SIGSEGV to
os/signal and returns to the faulting code, which faults
again, causing the process to hang instead of crashing.
Fix by rewriting "user-generated" SIGSEGV on OS X to
look like a kernel-generated SIGSEGV with fault address
0xb01dfacedebac1e.
I chose that address because (1) when printed in hex
during a crash, it is obviously spelling out English text,
(2) there are no current Google hits for that pointer,
which will make its origin easy to find once this CL
is indexed, and (3) it is not an altogether inaccurate
description of the situation.
Add a test. Maybe other systems will break too.
LGTM=khr
R=golang-codereviews, khr
CC=golang-codereviews, iant, ken
https://golang.org/cl/83270049
2014-04-03 17:07:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:43:09 -06:00
|
|
|
func testSetPanicOnFault(t *testing.T, addr uintptr, nfault *int) {
|
2014-05-20 10:10:19 -06:00
|
|
|
if GOOS == "nacl" {
|
|
|
|
t.Skip("nacl doesn't seem to fault on high addresses")
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:18:05 -07:00
|
|
|
defer func() {
|
2014-09-18 19:43:09 -06:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
*nfault++
|
2014-02-20 14:18:05 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-18 19:43:09 -06:00
|
|
|
// The read should fault, except that sometimes we hit
|
|
|
|
// addresses that have had C or kernel pages mapped there
|
|
|
|
// readable by user code. So just log the content.
|
|
|
|
// If no addresses fault, we'll fail the test.
|
runtime: be very careful with bad pointer tests
Saw this on a test:
runtime: bad pointer in frame runtime_test.testSetPanicOnFault at 0xc20801c6b0: 0xfff
fatal error: bad pointer!
runtime stack:
...
copystack(0xc2081bf7a0, 0x1000)
/root/work/solaris-amd64-smartos-2dde8b453d26/go/src/runtime/stack.c:621 +0x173 fp=0xfffffd7ffd5ffee0 sp=0xfffffd7ffd5ffe20
runtime.newstack()
/root/work/solaris-amd64-smartos-2dde8b453d26/go/src/runtime/stack.c:774 +0x552 fp=0xfffffd7ffd5fff90 sp=0xfffffd7ffd5ffee0
runtime.morestack()
/root/work/solaris-amd64-smartos-2dde8b453d26/go/src/runtime/asm_amd64.s:324 +0x90 fp=0xfffffd7ffd5fff98 sp=0xfffffd7ffd5fff90
goroutine 163354 [stack growth]:
...
runtime.convT2E(0x587000, 0xc20807bea8, 0x0, 0x0)
/root/work/solaris-amd64-smartos-2dde8b453d26/go/src/runtime/iface.go:141 +0xd2 fp=0xc20801c678 sp=0xc20801c640
runtime_test.testSetPanicOnFault(0xc20822c510, 0xfff, 0xc20801c748)
/root/work/solaris-amd64-smartos-2dde8b453d26/go/src/runtime/runtime_test.go:211 +0xc6 fp=0xc20801c718 sp=0xc20801c678
...
This test is testing bad pointers. It loads the bad pointer into a pointer variable,
but before it gets a chance to dereference it, calls convT2E. That call causes a stack copy,
which exposes that live but bad pointer variable.
LGTM=dvyukov
R=golang-codereviews, dvyukov
CC=golang-codereviews
https://golang.org/cl/146880043
2014-09-21 00:31:11 -06:00
|
|
|
v := *(*byte)(unsafe.Pointer(addr))
|
|
|
|
t.Logf("addr %#x: %#x\n", addr, v)
|
2014-02-20 14:18:05 -07:00
|
|
|
}
|
2014-06-16 22:00:37 -06:00
|
|
|
|
|
|
|
func eqstring_generic(s1, s2 string) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// optimization in assembly versions:
|
|
|
|
// if s1.str == s2.str { return true }
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
if s1[i] != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqString(t *testing.T) {
|
|
|
|
// This isn't really an exhaustive test of eqstring, it's
|
|
|
|
// just a convenient way of documenting (via eqstring_generic)
|
|
|
|
// what eqstring does.
|
|
|
|
s := []string{
|
|
|
|
"",
|
|
|
|
"a",
|
|
|
|
"c",
|
|
|
|
"aaa",
|
|
|
|
"ccc",
|
|
|
|
"cccc"[:3], // same contents, different string
|
|
|
|
"1234567890",
|
|
|
|
}
|
|
|
|
for _, s1 := range s {
|
|
|
|
for _, s2 := range s {
|
|
|
|
x := s1 == s2
|
|
|
|
y := eqstring_generic(s1, s2)
|
|
|
|
if x != y {
|
|
|
|
t.Errorf(`eqstring("%s","%s") = %t, want %t`, s1, s2, x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 10:25:23 -07:00
|
|
|
|
|
|
|
func TestTrailingZero(t *testing.T) {
|
|
|
|
// make sure we add padding for structs with trailing zero-sized fields
|
|
|
|
type T1 struct {
|
|
|
|
n int32
|
|
|
|
z [0]byte
|
|
|
|
}
|
|
|
|
if unsafe.Sizeof(T1{}) != 8 {
|
|
|
|
t.Errorf("sizeof(%#v)==%d, want 8", T1{}, unsafe.Sizeof(T1{}))
|
|
|
|
}
|
|
|
|
type T2 struct {
|
|
|
|
n int64
|
|
|
|
z struct{}
|
|
|
|
}
|
2015-02-26 08:01:37 -07:00
|
|
|
if unsafe.Sizeof(T2{}) != 8+unsafe.Sizeof(Uintreg(0)) {
|
|
|
|
t.Errorf("sizeof(%#v)==%d, want %d", T2{}, unsafe.Sizeof(T2{}), 8+unsafe.Sizeof(Uintreg(0)))
|
2015-01-07 10:25:23 -07:00
|
|
|
}
|
|
|
|
type T3 struct {
|
|
|
|
n byte
|
|
|
|
z [4]struct{}
|
|
|
|
}
|
|
|
|
if unsafe.Sizeof(T3{}) != 2 {
|
|
|
|
t.Errorf("sizeof(%#v)==%d, want 2", T3{}, unsafe.Sizeof(T3{}))
|
|
|
|
}
|
|
|
|
// make sure padding can double for both zerosize and alignment
|
|
|
|
type T4 struct {
|
|
|
|
a int32
|
|
|
|
b int16
|
|
|
|
c int8
|
|
|
|
z struct{}
|
|
|
|
}
|
|
|
|
if unsafe.Sizeof(T4{}) != 8 {
|
|
|
|
t.Errorf("sizeof(%#v)==%d, want 8", T4{}, unsafe.Sizeof(T4{}))
|
|
|
|
}
|
|
|
|
// make sure we don't pad a zero-sized thing
|
|
|
|
type T5 struct {
|
|
|
|
}
|
|
|
|
if unsafe.Sizeof(T5{}) != 0 {
|
|
|
|
t.Errorf("sizeof(%#v)==%d, want 0", T5{}, unsafe.Sizeof(T5{}))
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 21:16:48 -07:00
|
|
|
|
|
|
|
func TestBadOpen(t *testing.T) {
|
|
|
|
if GOOS == "windows" || GOOS == "nacl" {
|
|
|
|
t.Skip("skipping OS that doesn't have open/read/write/close")
|
|
|
|
}
|
|
|
|
// make sure we get the correct error code if open fails. Same for
|
|
|
|
// read/write/close on the resulting -1 fd. See issue 10052.
|
|
|
|
nonfile := []byte("/notreallyafile")
|
|
|
|
fd := Open(&nonfile[0], 0, 0)
|
|
|
|
if fd != -1 {
|
|
|
|
t.Errorf("open(\"%s\")=%d, want -1", string(nonfile), fd)
|
|
|
|
}
|
|
|
|
var buf [32]byte
|
|
|
|
r := Read(-1, unsafe.Pointer(&buf[0]), int32(len(buf)))
|
|
|
|
if r != -1 {
|
|
|
|
t.Errorf("read()=%d, want -1", r)
|
|
|
|
}
|
|
|
|
w := Write(^uintptr(0), unsafe.Pointer(&buf[0]), int32(len(buf)))
|
|
|
|
if w != -1 {
|
|
|
|
t.Errorf("write()=%d, want -1", w)
|
|
|
|
}
|
|
|
|
c := Close(-1)
|
|
|
|
if c != -1 {
|
|
|
|
t.Errorf("close()=%d, want -1", c)
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 17:27:20 -06:00
|
|
|
|
|
|
|
func TestAppendGrowth(t *testing.T) {
|
|
|
|
var x []int64
|
|
|
|
check := func(want int) {
|
|
|
|
if cap(x) != want {
|
|
|
|
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(0)
|
|
|
|
want := 1
|
|
|
|
for i := 1; i <= 100; i++ {
|
|
|
|
x = append(x, 1)
|
|
|
|
check(want)
|
|
|
|
if i&(i-1) == 0 {
|
|
|
|
want = 2 * i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var One = []int64{1}
|
|
|
|
|
|
|
|
func TestAppendSliceGrowth(t *testing.T) {
|
|
|
|
var x []int64
|
|
|
|
check := func(want int) {
|
|
|
|
if cap(x) != want {
|
|
|
|
t.Errorf("len=%d, cap=%d, want cap=%d", len(x), cap(x), want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(0)
|
|
|
|
want := 1
|
|
|
|
for i := 1; i <= 100; i++ {
|
|
|
|
x = append(x, One...)
|
|
|
|
check(want)
|
|
|
|
if i&(i-1) == 0 {
|
|
|
|
want = 2 * i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|