https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66016
Bug ID: 66016 Summary: Accessing nil Func's name results in crash Product: gcc Version: 5.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: go Assignee: ian at airs dot com Reporter: jcajka at redhat dot com CC: cmang at google dot com Target Milestone: --- Created attachment 35462 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35462&action=edit patch Follow up to bug 65798 and https://bugzilla.redhat.com/show_bug.cgi?id=1212472#c2. Comment #4 from RHBZ: Hm..., it doesn't fix it. I have messed it up a bit..., problem might not be the runtime.Caller...( as the rejected function name part was working around the problem). I have prepare small program triggering the crash outside docker. package main import "runtime" import "fmt" func main() { for i := 1;i <= 5;i++ { pc, fname, line,ok := runtime.Caller(i) fmt.Printf("%x",pc) fmt.Print(" ",fname," ",line," ",ok,"\n") fn := runtime.FuncForPC(pc) fmt.Print(fn.Name(),"\n\n") } } Crashes with gcc-go: 7fefdc22b2c6 ../../../libgo/runtime/proc.c 550 true runtime_main 7fefdc228f8b ../../../libgo/runtime/proc.c 235 true kickoff 7fefdb20ffff 0 true panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0] goroutine 16 [running]: main.main /root/test.go:12 goroutine 18 [finalizer wait]: created by runtime_createfing ../../../libgo/runtime/mgc0.c:2572 but works fine with golang(built with --ldflags "-linkmode external"): ./test 4125e3 /usr/lib/golang/src/runtime/proc.go 63 true runtime.main 4377e1 /usr/lib/golang/src/runtime/asm_amd64.s 2232 true runtime.goexit 0 0 false 0 0 false 0 0 false There is difference(probably expected?, this confused me...), in number of callers. But also libgo seems to behave differently than golang when accessing nil Func's Name, as ommiting/replacing the fn.Name() with just fn yields with gcc-go: 7feb8a48e2c6 ../../../libgo/runtime/proc.c 550 true &{{}} 7feb8a48bf8b ../../../libgo/runtime/proc.c 235 true &{{}} 7feb89472fff 0 true <nil> 0 0 false <nil> 0 0 false <nil> (Also tried just fn := runtime.FuncForPC(0) fmt.Print(fn.Name(),"\n\n") which works with golang, but fails with gcc-go) I have checked golang's code and indeed it does checking for nil. (symtab.go) func cfuncname(f *_func) *byte { if f == nil || f.nameoff == 0 { return nil } datap := findmoduledatap(f.entry) // inefficient return (*byte)(unsafe.Pointer(&datap.pclntable[f.nameoff])) } but libgo doesn't: (caller.c) String runtime_funcname_go (Func *f) { return f->name; } changing it: @@ -231,7 +231,13 @@ String runtime_funcname_go (Func *f) String runtime_funcname_go (Func *f) { - return f->name; + String str; + if (!f) + { + runtime_memclr (&str, sizeof str); + return str; + } + else return f->name; } seems to fix the crash(tested on x86, will do ppc). Also it seems that Entry() is not protected in both golang and libgo. I would expect same behavior as in Name(). Will open golang ticket. I hope now it is finally correct fix(or at least at right spot)... Patch in attachment.