Skip to content

Commit

Permalink
pkg/proc: pad variable mem in extractVarInfoFromEntry
Browse files Browse the repository at this point in the history
On 64 bit system, the byte size of th following struct is 16:
    type myStruct struct {
       a int
       b uint32
    }
But extractVarInfoFromEntry only allocates a mem of 12 bytes for it.
When calling method of this struct with the "call" command, it will
result in this error:
    write out of bounds

This patch extends the mem by adding padding bytes to the end of the
mem.

Fixes go-delve#3364.
  • Loading branch information
ZekeLu committed May 11, 2023
1 parent e95ae9c commit 5174462
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
15 changes: 14 additions & 1 deletion _fixtures/fncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ func (i Issue2698) String() string {
return fmt.Sprintf("%d %d %d %d", i.a, i.b, i.c, i.d)
}

type Issue3364 struct {
a int
b uint32
}

func (i Issue3364) String() string {
return fmt.Sprintf("%d %d", i.a, i.b)
}

func main() {
one, two := 1, 2
intslice := []int{1, 2, 3}
Expand All @@ -222,6 +231,10 @@ func main() {
c: 3,
d: 4,
}
issue3364 := Issue3364{
a: 1,
b: 2,
}

fn2clos := makeclos(pa)
fn2glob := call1
Expand All @@ -241,5 +254,5 @@ func main() {
d.Method()
d.Base.Method()
x.CallMe()
fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5), longstrs, regabistacktest, regabistacktest2, issue2698.String(), regabistacktest3, rast3, floatsum, ref)
fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5), longstrs, regabistacktest, regabistacktest2, issue2698.String(), issue3364.String(), regabistacktest3, rast3, floatsum, ref)
}
5 changes: 5 additions & 0 deletions pkg/proc/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,11 @@ func extractVarInfoFromEntry(tgt *Target, bi *BinaryInfo, image *Image, regs op.
}
}
if cmem != nil {
paddingBytes := int(t.Common().ByteSize) - len(cmem.data)
if paddingBytes > 0 && paddingBytes < bi.Arch.ptrSize {
padding := make([]byte, paddingBytes)
cmem.data = append(cmem.data, padding...)
}
mem = cmem
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/proc/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ func TestCallFunction(t *testing.T) {
{`regabistacktest("one", "two", "three", "four", "five", 4)`, []string{`:string:"onetwo"`, `:string:"twothree"`, `:string:"threefour"`, `:string:"fourfive"`, `:string:"fiveone"`, ":uint8:8"}, nil},
{`regabistacktest2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)`, []string{":int:3", ":int:5", ":int:7", ":int:9", ":int:11", ":int:13", ":int:15", ":int:17", ":int:19", ":int:11"}, nil},
{`issue2698.String()`, []string{`:string:"1 2 3 4"`}, nil},
{`issue3364.String()`, []string{`:string:"1 2"`}, nil},
{`regabistacktest3(rast3, 5)`, []string{`:[10]string:[10]string ["onetwo","twothree","threefour","fourfive","fivesix","sixseven","sevenheight","heightnine","nineten","tenone"]`, ":uint8:15"}, nil},
{`floatsum(1, 2)`, []string{":float64:3"}, nil},
}
Expand Down

0 comments on commit 5174462

Please sign in to comment.