runtime/debug: add SetPanicOnFault

SetPanicOnFault allows recovery from unexpected memory faults.
This can be useful if you are using a memory-mapped file
or probing the address space of the current program.

LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/66590044
This commit is contained in:
Russ Cox 2014-02-20 16:18:05 -05:00
parent 67c83db60d
commit e56c6e7535
14 changed files with 52 additions and 15 deletions

View file

@ -10,9 +10,11 @@ import (
"os"
"os/exec"
. "runtime"
"runtime/debug"
"strconv"
"strings"
"testing"
"unsafe"
)
var errf error
@ -131,3 +133,19 @@ func TestRuntimeGogoBytes(t *testing.T) {
func TestStopCPUProfilingWithProfilerOff(t *testing.T) {
SetCPUProfileRate(0)
}
func TestSetPanicOnFault(t *testing.T) {
old := debug.SetPanicOnFault(true)
defer debug.SetPanicOnFault(old)
defer func() {
if err := recover(); err == nil {
t.Fatalf("did not find error in recover")
}
}()
var p *int
p = (*int)(unsafe.Pointer(^uintptr(0)))
println(*p)
t.Fatalf("still here - should have faulted")
}