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
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
. "runtime"
|
2014-02-20 14:18:05 -07:00
|
|
|
"runtime/debug"
|
2013-09-13 12:19:23 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// The profiling signal handler needs to know whether it is executing runtime.gogo.
|
|
|
|
// The constant RuntimeGogoBytes in arch_*.h gives the size of the function;
|
|
|
|
// we don't have a way to obtain it from the linker (perhaps someday).
|
|
|
|
// Test that the constant matches the size determined by 'go tool nm -S'.
|
|
|
|
// The value reported will include the padding between runtime.gogo and the
|
|
|
|
// next function in memory. That's fine.
|
|
|
|
func TestRuntimeGogoBytes(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "go-build")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temp directory: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
out, err := exec.Command("go", "build", "-o", dir+"/hello", "../../../test/helloworld.go").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("building hello world: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
|
2013-12-16 10:59:30 -07:00
|
|
|
out, err = exec.Command("go", "tool", "nm", "-size", dir+"/hello").CombinedOutput()
|
2013-09-13 12:19:23 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("go tool nm: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
|
|
f := strings.Fields(line)
|
|
|
|
if len(f) == 4 && f[3] == "runtime.gogo" {
|
|
|
|
size, _ := strconv.Atoi(f[1])
|
|
|
|
if GogoBytes() != int32(size) {
|
|
|
|
t.Fatalf("RuntimeGogoBytes = %d, should be %d", GogoBytes(), size)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("go tool nm did not report size for runtime.gogo")
|
|
|
|
}
|
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,
|
|
|
|
// no 0xffffffffffff0001; 0xffff0001 is mapped for 32-bit user space on OS X
|
2014-04-09 08:02:55 -06:00
|
|
|
// no 0xfffffffffff00001; 0xfff00001 is mapped for 32-bit user space sometimes on Linux
|
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) {
|
2014-02-27 09:26:26 -07:00
|
|
|
// This currently results in a fault in the signal trampoline on
|
|
|
|
// dragonfly/386 - see issue 7421.
|
|
|
|
if GOOS == "dragonfly" && GOARCH == "386" {
|
|
|
|
t.Skip("skipping test on dragonfly/386")
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:18:05 -07:00
|
|
|
old := debug.SetPanicOnFault(true)
|
|
|
|
defer debug.SetPanicOnFault(old)
|
|
|
|
|
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 {
|
|
|
|
testSetPanicOnFault(t, uintptr(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetPanicOnFault(t *testing.T, addr uintptr) {
|
2014-02-20 14:18:05 -07:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Fatalf("did not find error in recover")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var p *int
|
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
|
|
|
p = (*int)(unsafe.Pointer(addr))
|
2014-02-20 14:18:05 -07:00
|
|
|
println(*p)
|
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
|
|
|
t.Fatalf("still here - should have faulted on address %#x", addr)
|
2014-02-20 14:18:05 -07:00
|
|
|
}
|