]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/debug: provide Addr method for errors from SetPanicOnFault
authorKeith Randall <khr@golang.org>
Thu, 20 Aug 2020 21:22:30 +0000 (14:22 -0700)
committerKeith Randall <khr@golang.org>
Fri, 18 Sep 2020 17:21:06 +0000 (17:21 +0000)
When we're building a panic that's triggered by a memory fault when
SetPanicOnFault has been called, include an Addr method. This
method reports the address at which the fault occurred.

Fixes #37023

RELNOTE=yes

Change-Id: Idff144587d6b75070fdc861a36efec76f4ec7384
Reviewed-on: https://go-review.googlesource.com/c/go/+/249677
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Keith Randall <khr@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/runtime/debug/garbage.go
src/runtime/debug/panic_test.go [new file with mode: 0644]
src/runtime/error.go
src/runtime/os_plan9.go
src/runtime/panic.go
src/runtime/signal_unix.go
src/runtime/signal_windows.go

index 785e9d4598eac673dd4c6b32b28db7da6f1d73e8..e36e54f12d34d559f170531c453147dc6171011e 100644 (file)
@@ -139,6 +139,11 @@ func SetMaxThreads(threads int) int {
 // manipulation of memory may cause faults at non-nil addresses in less
 // dramatic situations; SetPanicOnFault allows such programs to request
 // that the runtime trigger only a panic, not a crash.
+// The runtime.Error that the runtime panics with may have an additional method:
+//     Addr() uintptr
+// If that method exists, it returns the memory address which triggered the fault.
+// The results of Addr are best-effort and the veracity of the result
+// may depend on the platform.
 // SetPanicOnFault applies only to the current goroutine.
 // It returns the previous setting.
 func SetPanicOnFault(enabled bool) bool {
diff --git a/src/runtime/debug/panic_test.go b/src/runtime/debug/panic_test.go
new file mode 100644 (file)
index 0000000..2aad418
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright 2020 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.
+
+// +build aix darwin dragonfly freebsd linux netbsd openbsd
+
+// TODO: test on Windows?
+
+package debug_test
+
+import (
+       "runtime/debug"
+       "syscall"
+       "testing"
+       "unsafe"
+)
+
+func TestPanicOnFault(t *testing.T) {
+       m, err := syscall.Mmap(-1, 0, 0x1000, syscall.PROT_READ /* Note: no PROT_WRITE */, syscall.MAP_SHARED|syscall.MAP_ANON)
+       if err != nil {
+               t.Fatalf("can't map anonymous memory: %s", err)
+       }
+       defer syscall.Munmap(m)
+       old := debug.SetPanicOnFault(true)
+       defer debug.SetPanicOnFault(old)
+       const lowBits = 0x3e7
+       defer func() {
+               r := recover()
+               if r == nil {
+                       t.Fatalf("write did not fault")
+               }
+               type addressable interface {
+                       Addr() uintptr
+               }
+               a, ok := r.(addressable)
+               if !ok {
+                       t.Fatalf("fault does not contain address")
+               }
+               want := uintptr(unsafe.Pointer(&m[lowBits]))
+               got := a.Addr()
+               if got != want {
+                       t.Fatalf("fault address %x, want %x", got, want)
+               }
+       }()
+       m[lowBits] = 1 // will fault
+}
index 386569bead6fc490d86539db32f74ec4560bdf42..9e6cdf35dd39add79a4a05a1a46c1055de0c7283 100644 (file)
@@ -77,6 +77,26 @@ func (e errorString) Error() string {
        return "runtime error: " + string(e)
 }
 
+type errorAddressString struct {
+       msg  string  // error message
+       addr uintptr // memory address where the error occurred
+}
+
+func (e errorAddressString) RuntimeError() {}
+
+func (e errorAddressString) Error() string {
+       return "runtime error: " + e.msg
+}
+
+// Addr returns the memory address where a fault occurred.
+// The address provided is best-effort.
+// The veracity of the result may depend on the platform.
+// Errors providing this method will only be returned as
+// a result of using runtime/debug.SetPanicOnFault.
+func (e errorAddressString) Addr() uintptr {
+       return e.addr
+}
+
 // plainError represents a runtime error described a string without
 // the prefix "runtime error: " after invoking errorString.Error().
 // See Issue #14965.
index 128c30adebadb2aaa685947ac1ed1631bf07e476..f3037a750867411b0c73276cfa5a93c76e5b85c2 100644 (file)
@@ -92,9 +92,12 @@ func sigpanic() {
                }
                addr := note[i:]
                g.sigcode1 = uintptr(atolwhex(addr))
-               if g.sigcode1 < 0x1000 || g.paniconfault {
+               if g.sigcode1 < 0x1000 {
                        panicmem()
                }
+               if g.paniconfault {
+                       panicmemAddr(g.sigcode1)
+               }
                print("unexpected fault address ", hex(g.sigcode1), "\n")
                throw("fault")
        case _SIGTRAP:
index 127843b081913a2f5a6d9ff2e04a5d2e87500fbe..6050a34d291c62aa8cad9a02844962ce551662a0 100644 (file)
@@ -212,6 +212,11 @@ func panicmem() {
        panic(memoryError)
 }
 
+func panicmemAddr(addr uintptr) {
+       panicCheck2("invalid memory address or nil pointer dereference")
+       panic(errorAddressString{msg: "invalid memory address or nil pointer dereference", addr: addr})
+}
+
 // Create a new deferred function fn with siz bytes of arguments.
 // The compiler turns a defer statement into a call to this.
 //go:nosplit
index 064a0ea100f5071ea5907dad234b04ddc43e8d0d..bbfc18e37b3bfce60d40f58692216edd04c865b7 100644 (file)
@@ -710,7 +710,7 @@ func sigpanic() {
                }
                // Support runtime/debug.SetPanicOnFault.
                if g.paniconfault {
-                       panicmem()
+                       panicmemAddr(g.sigcode1)
                }
                print("unexpected fault address ", hex(g.sigcode1), "\n")
                throw("fault")
@@ -720,7 +720,7 @@ func sigpanic() {
                }
                // Support runtime/debug.SetPanicOnFault.
                if g.paniconfault {
-                       panicmem()
+                       panicmemAddr(g.sigcode1)
                }
                print("unexpected fault address ", hex(g.sigcode1), "\n")
                throw("fault")
index d123276d3efbb62954fff02fb2f3d5e1c3d2126c..6d98d0259898f1f77c1edcd5f1f79a91236a8131 100644 (file)
@@ -242,9 +242,12 @@ func sigpanic() {
 
        switch g.sig {
        case _EXCEPTION_ACCESS_VIOLATION:
-               if g.sigcode1 < 0x1000 || g.paniconfault {
+               if g.sigcode1 < 0x1000 {
                        panicmem()
                }
+               if g.paniconfault {
+                       panicmemAddr(g.sigcode1)
+               }
                print("unexpected fault address ", hex(g.sigcode1), "\n")
                throw("fault")
        case _EXCEPTION_INT_DIVIDE_BY_ZERO: