Skip to content

Commit

Permalink
fix: invalid frame pointer]
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Nov 20, 2024
1 parent 51b58f4 commit b353e75
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/proc/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (s *HeapScope) parseSegment(name string, md *region) *segment {
// which is extremely complex to handle and is not conducive to the maintenance of the project.
// Therefore, Goref adopts a conservative scanning scheme.
// NOTE: This may lead to scanning an additional portion of memory.
func (s *HeapScope) stackPtrMask(frames []proc.Stackframe) []*framePointerMask {
func (s *HeapScope) stackPtrMask(start, end Address, frames []proc.Stackframe) []*framePointerMask {
var frPtrMasks []*framePointerMask
for i := range frames {
pc := frames[i].Regs.PC()
Expand All @@ -565,6 +565,10 @@ func (s *HeapScope) stackPtrMask(frames []proc.Stackframe) []*framePointerMask {
}
sp := Address(frames[i].Regs.SP())
fp := Address(frames[i].Regs.FrameBase)
if fp <= sp || fp > end || sp < start {
// invalid frame pointer
continue
}
ptrMask := make([]uint64, CeilDivide(fp.Sub(sp)/8, 64))
for i := range ptrMask {
ptrMask[i] = ^uint64(0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/proc/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func ObjectReference(t *proc.Target, filename string) error {
threadID = gr.Thread.ThreadID()
}
sf, _ := proc.GoroutineStacktrace(t, gr, 1024, 0)
s.g.init(Address(lo), Address(hi), s.stackPtrMask(sf))
s.g.init(Address(lo), Address(hi), s.stackPtrMask(Address(lo), Address(hi), sf))
if len(sf) > 0 {
for i := range sf {
ms := myEvalScope{EvalScope: *proc.FrameToScope(t, t.Memory(), gr, threadID, sf[i:]...)}
Expand Down
54 changes: 54 additions & 0 deletions testdata/mockleak/creator/creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package creator

type InnerMessage struct {
msgs []string
}

type MyChan struct {
cchan chan *InnerMessage
}

type SubRequest struct {
E map[string]string
F map[int64]*MyChan
}

type Request struct {
A *int64
B *string
C []string
D *SubRequest
X []*SubRequest
}

func Create() interface{} {
buf := make([]byte, 1024)
str := string(buf)
return &Request{
A: new(int64),
B: &str,
C: []string{string(buf), string(buf), string(buf)},
D: &SubRequest{
E: map[string]string{
string(buf): string(buf),
},
F: map[int64]*MyChan{
123456: {
cchan: make(chan *InnerMessage, 100),
},
},
},
X: []*SubRequest{
{
E: map[string]string{
string(buf): string(buf),
},
F: map[int64]*MyChan{
123456: {
cchan: make(chan *InnerMessage, 100),
},
},
},
},
}
}
12 changes: 12 additions & 0 deletions testdata/mockleak/holder/holder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package holder

func ReceiveChan() chan interface{} {
ch := make(chan interface{}, 100)
go func() {
var cached []interface{}
for v := range ch {
cached = append(cached, v)
}
}()
return ch
}
22 changes: 22 additions & 0 deletions testdata/mockleak/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"
"net/http"
_ "net/http/pprof"
"time"

"github.com/cloudwego/goref/testdata/mockleak/creator"
"github.com/cloudwego/goref/testdata/mockleak/holder"
)

func main() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
receiver := holder.ReceiveChan()
for i := 0; i < 100000; i++ {
receiver <- creator.Create()
}
time.Sleep(1 * time.Minute)
}

0 comments on commit b353e75

Please sign in to comment.