Skip to content

Commit

Permalink
runtime: make execution error panic values implement the Error interface
Browse files Browse the repository at this point in the history
Make execution panics implement Error as
mandated by https://golang.org/ref/spec#Run_time_panics,
instead of panics with strings.

Fixes #14965

Change-Id: I7827f898b9b9c08af541db922cc24fa0800ff18a
Reviewed-on: https://go-review.googlesource.com/21214
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
odeke-em authored and bradfitz committed Apr 10, 2016
1 parent 824d8c1 commit e4f1d9c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/runtime/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func makechan(t *chantype, size int64) *hchan {
throw("makechan: bad alignment")
}
if size < 0 || int64(uintptr(size)) != size || (elem.size > 0 && uintptr(size) > (_MaxMem-hchanSize)/elem.size) {
panic("makechan: size out of range")
panic(plainError("makechan: size out of range"))
}

var c *hchan
Expand Down Expand Up @@ -171,7 +171,7 @@ func chansend(t *chantype, c *hchan, ep unsafe.Pointer, block bool, callerpc uin

if c.closed != 0 {
unlock(&c.lock)
panic("send on closed channel")
panic(plainError("send on closed channel"))
}

if sg := c.recvq.dequeue(); sg != nil {
Expand Down Expand Up @@ -231,7 +231,7 @@ func chansend(t *chantype, c *hchan, ep unsafe.Pointer, block bool, callerpc uin
if c.closed == 0 {
throw("chansend: spurious wakeup")
}
panic("send on closed channel")
panic(plainError("send on closed channel"))
}
gp.param = nil
if mysg.releasetime > 0 {
Expand Down Expand Up @@ -302,13 +302,13 @@ func sendDirect(t *_type, sg *sudog, src unsafe.Pointer) {

func closechan(c *hchan) {
if c == nil {
panic("close of nil channel")
panic(plainError("close of nil channel"))
}

lock(&c.lock)
if c.closed != 0 {
unlock(&c.lock)
panic("close of closed channel")
panic(plainError("close of closed channel"))
}

if raceenabled {
Expand Down
46 changes: 46 additions & 0 deletions src/runtime/crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,52 @@ func TestGoexitInPanic(t *testing.T) {
}
}

// Issue 14965: Runtime panics should be of type runtime.Error
func TestRuntimePanicWithRuntimeError(t *testing.T) {
testCases := [...]func(){
0: func() {
var m map[uint64]bool
m[1234] = true
},
1: func() {
ch := make(chan struct{})
close(ch)
close(ch)
},
2: func() {
var ch = make(chan struct{})
close(ch)
ch <- struct{}{}
},
3: func() {
var s = make([]int, 2)
_ = s[2]
},
4: func() {
n := -1
_ = make(chan bool, n)
},
5: func() {
close((chan bool)(nil))
},
}

for i, fn := range testCases {
got := panicValue(fn)
if _, ok := got.(runtime.Error); !ok {
t.Errorf("test #%d: recovered value %v(type %T) does not implement runtime.Error", i, got, got)
}
}
}

func panicValue(fn func()) (recovered interface{}) {
defer func() {
recovered = recover()
}()
fn()
return
}

func TestPanicAfterGoexit(t *testing.T) {
// an uncaught panic should still work after goexit
output := runTestProg(t, "testprog", "PanicAfterGoexit")
Expand Down
13 changes: 12 additions & 1 deletion src/runtime/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func (e errorString) Error() string {
return "runtime error: " + string(e)
}

// plainError represents a runtime error described a string without
// the prefix "runtime error: " after invoking errorString.Error().
// See Issue #14965.
type plainError string

func (e plainError) RuntimeError() {}

func (e plainError) Error() string {
return string(e)
}

type stringer interface {
String() string
}
Expand Down Expand Up @@ -82,5 +93,5 @@ func printany(i interface{}) {

// called from generated code
func panicwrap(pkg, typ, meth string) {
panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
}
4 changes: 2 additions & 2 deletions src/runtime/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func makemap(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap {
}

if hint < 0 || int64(int32(hint)) != hint {
panic("makemap: size out of range")
panic(plainError("makemap: size out of range"))
// TODO: make hint an int, then none of this nonsense
}

Expand Down Expand Up @@ -428,7 +428,7 @@ func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe

func mapassign1(t *maptype, h *hmap, key unsafe.Pointer, val unsafe.Pointer) {
if h == nil {
panic("assignment to entry in nil map")
panic(plainError("assignment to entry in nil map"))
}
if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&t))
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/malloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ func newarray(typ *_type, n uintptr) unsafe.Pointer {
flags |= flagNoScan
}
if int(n) < 0 || (typ.size > 0 && n > _MaxMem/typ.size) {
panic("runtime: allocation size out of range")
panic(plainError("runtime: allocation size out of range"))
}
return mallocgc(typ.size*n, typ, flags)
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func badmcall2(fn func(*g)) {
}

func badreflectcall() {
panic("runtime: arg size to reflect.call more than 1GB")
panic(plainError("arg size to reflect.call more than 1GB"))
}

func lockedOSThread() bool {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ retc:
sclose:
// send on closed channel
selunlock(scases, lockorder)
panic("send on closed channel")
panic(plainError("send on closed channel"))
}

func (c *hchan) sortkey() uintptr {
Expand Down

0 comments on commit e4f1d9c

Please sign in to comment.