reflect: when StructOf overflows computing size/offset, panic

Fixes #52740

Change-Id: I849e585deb77cfcfc1b517be4a171eb29b30c5f3
Reviewed-on: https://go-review.googlesource.com/c/go/+/412214
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Keith Randall 2022-06-14 14:38:56 -07:00
parent e1e66a03a6
commit c22a6c3b90
2 changed files with 95 additions and 1 deletions

View file

@ -5891,6 +5891,87 @@ func TestStructOfDifferentPkgPath(t *testing.T) {
})
}
func TestStructOfTooLarge(t *testing.T) {
t1 := TypeOf(byte(0))
t2 := TypeOf(int16(0))
t4 := TypeOf(int32(0))
t0 := ArrayOf(0, t1)
// 2^64-3 sized type (or 2^32-3 on 32-bit archs)
bigType := StructOf([]StructField{
{Name: "F1", Type: ArrayOf(int(^uintptr(0)>>1), t1)},
{Name: "F2", Type: ArrayOf(int(^uintptr(0)>>1-1), t1)},
})
type test struct {
shouldPanic bool
fields []StructField
}
tests := [...]test{
{
shouldPanic: false, // 2^64-1, ok
fields: []StructField{
{Name: "F1", Type: bigType},
{Name: "F2", Type: ArrayOf(2, t1)},
},
},
{
shouldPanic: true, // overflow in total size
fields: []StructField{
{Name: "F1", Type: bigType},
{Name: "F2", Type: ArrayOf(3, t1)},
},
},
{
shouldPanic: true, // overflow while aligning F2
fields: []StructField{
{Name: "F1", Type: bigType},
{Name: "F2", Type: t4},
},
},
{
shouldPanic: true, // overflow while adding trailing byte for zero-sized fields
fields: []StructField{
{Name: "F1", Type: bigType},
{Name: "F2", Type: ArrayOf(2, t1)},
{Name: "F3", Type: t0},
},
},
{
shouldPanic: true, // overflow while aligning total size
fields: []StructField{
{Name: "F1", Type: t2},
{Name: "F2", Type: bigType},
},
},
}
for i, tt := range tests {
func() {
defer func() {
err := recover()
if !tt.shouldPanic {
if err != nil {
t.Errorf("test %d should not panic, got %s", i, err)
}
return
}
if err == nil {
t.Errorf("test %d expected to panic", i)
return
}
s := fmt.Sprintf("%s", err)
if s != "reflect.StructOf: struct size would exceed virtual address space" {
t.Errorf("test %d wrong panic message: %s", i, s)
return
}
}()
_ = StructOf(tt.fields)
}()
}
}
func TestChanOf(t *testing.T) {
// check construction and use of type not in binary
type T string