Skip to content

Commit 632bfe4

Browse files
dvyukovwheatman
authored andcommitted
runtime: fix go of nil func value
Currently runtime derefences nil with m->locks>0, which causes unrecoverable fatal error. Panic instead. Fixes golang#8045. LGTM=rsc R=golang-codereviews, rsc CC=golang-codereviews, khr https://golang.org/cl/97620043
1 parent edb34a9 commit 632bfe4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/pkg/runtime/crash_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ func TestGoexitCrash(t *testing.T) {
161161
}
162162
}
163163

164+
func TestGoNil(t *testing.T) {
165+
output := executeTest(t, goNilSource, nil)
166+
want := "go of nil func value"
167+
if !strings.Contains(output, want) {
168+
t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
169+
}
170+
}
171+
164172
const crashSource = `
165173
package main
166174
@@ -346,3 +354,16 @@ func main() {
346354
runtime.Goexit()
347355
}
348356
`
357+
358+
const goNilSource = `
359+
package main
360+
361+
func main() {
362+
defer func() {
363+
recover()
364+
}()
365+
var f func()
366+
go f()
367+
select{}
368+
}
369+
`

src/pkg/runtime/proc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@ runtime·newproc1(FuncVal *fn, byte *argp, int32 narg, int32 nret, void *callerp
18161816
int32 siz;
18171817

18181818
//runtime·printf("newproc1 %p %p narg=%d nret=%d\n", fn->fn, argp, narg, nret);
1819+
if(fn == nil) {
1820+
m->throwing = -1; // do not dump full stacks
1821+
runtime·throw("go of nil func value");
1822+
}
18191823
m->locks++; // disable preemption because it can be holding p in a local var
18201824
siz = narg + nret;
18211825
siz = (siz+7) & ~7;

0 commit comments

Comments
 (0)