Fix: Handle inability to find return addr

Fixes a code path where stacktrace returns < 2 locations and
thread.ReturnAddress would panic. Now returns an error.
This commit is contained in:
Derek Parker 2015-08-10 08:55:57 -05:00
parent 8de1a307ee
commit f43007c55e
3 changed files with 45 additions and 31 deletions

@ -0,0 +1,15 @@
package main
import "sync"
func doSomething(wg *sync.WaitGroup) {
wg.Done()
}
func main() {
var wg sync.WaitGroup
wg.Add(2)
go doSomething(&wg)
go doSomething(&wg)
wg.Wait()
}

@ -2,7 +2,6 @@ package proc
import ( import (
"bytes" "bytes"
"encoding/binary"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -380,48 +379,22 @@ func TestRuntimeBreakpoint(t *testing.T) {
func TestFindReturnAddress(t *testing.T) { func TestFindReturnAddress(t *testing.T) {
withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) { withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
var ( start, _, err := p.goSymTable.LineToPC(fixture.Source, 24)
fdes = p.frameEntries
gsd = p.goSymTable
)
start, _, err := gsd.LineToPC(fixture.Source, 24)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = p.SetBreakpoint(start) _, err = p.SetBreakpoint(start)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = p.Continue() err = p.Continue()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
addr, err := p.CurrentThread.ReturnAddress()
regs, err := p.Registers()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fde, err := fdes.FDEForPC(start)
if err != nil {
t.Fatal(err)
}
ret := fde.ReturnAddressOffset(start)
if err != nil {
t.Fatal(err)
}
addr := uint64(int64(regs.SP()) + ret)
data, err := p.CurrentThread.readMemory(uintptr(addr), 8)
if err != nil {
t.Fatal(err)
}
addr = binary.LittleEndian.Uint64(data)
_, l, _ := p.goSymTable.PCToLine(addr) _, l, _ := p.goSymTable.PCToLine(addr)
if l != 40 { if l != 40 {
t.Fatalf("return address not found correctly, expected line 40") t.Fatalf("return address not found correctly, expected line 40")
@ -429,6 +402,25 @@ func TestFindReturnAddress(t *testing.T) {
}) })
} }
func TestFindReturnAddressTopOfStackFn(t *testing.T) {
withTestProcess("testreturnaddress", t, func(p *Process, fixture protest.Fixture) {
fnName := "runtime.rt0_go"
fn := p.goSymTable.LookupFunc(fnName)
if fn == nil {
t.Fatalf("could not find function %s", fnName)
}
if _, err := p.SetBreakpoint(fn.Entry); err != nil {
t.Fatal(err)
}
if err := p.Continue(); err != nil {
t.Fatal(err)
}
if _, err := p.CurrentThread.ReturnAddress(); err == nil {
t.Fatal("expected error to be returned")
}
})
}
func TestSwitchThread(t *testing.T) { func TestSwitchThread(t *testing.T) {
withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) { withTestProcess("testnextprog", t, func(p *Process, fixture protest.Fixture) {
// With invalid thread id // With invalid thread id

@ -1,6 +1,9 @@
package proc package proc
import "encoding/binary" import (
"encoding/binary"
"fmt"
)
// Takes an offset from RSP and returns the address of the // Takes an offset from RSP and returns the address of the
// instruction the current function is going to return to. // instruction the current function is going to return to.
@ -9,6 +12,9 @@ func (thread *Thread) ReturnAddress() (uint64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
if len(locations) < 2 {
return 0, fmt.Errorf("could not find return address for %s", locations[0].Fn.BaseName())
}
return locations[1].PC, nil return locations[1].PC, nil
} }
@ -75,7 +81,8 @@ func (dbp *Process) stacktrace(pc, sp uint64, depth int) ([]Location, error) {
break break
} }
locations = append(locations, Location{PC: ret, File: f, Line: l, Fn: fn}) locations = append(locations, Location{PC: ret, File: f, Line: l, Fn: fn})
if fn.Name == "runtime.goexit" { // Look for "top of stack" functions.
if fn.Name == "runtime.rt0_go" {
break break
} }
} }