Skip to content

Commit 99ec109

Browse files
authored
proc: fill bi member of constants (#4026)
Fill the bi field of constant variables when we create them. This is meant to address #4021 which is caused by trying to access a member field of a variable with a nil bi. I couldn't find a way to make a constant reach that point into the code but this still looks like the most likely explanation. Fixes #4021
1 parent 2ac3573 commit 99ec109

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

pkg/proc/breakpoints.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,11 +1014,11 @@ func (rbpi *returnBreakpointInfo) Collect(t *Target, thread Thread) []*Variable
10141014

10151015
g, err := GetG(thread)
10161016
if err != nil {
1017-
return returnInfoError("could not get g", err, thread.ProcessMemory())
1017+
return returnInfoError("could not get g", err, thread.BinInfo(), thread.ProcessMemory())
10181018
}
10191019
scope, err := GoroutineScope(t, thread)
10201020
if err != nil {
1021-
return returnInfoError("could not get scope", err, thread.ProcessMemory())
1021+
return returnInfoError("could not get scope", err, thread.BinInfo(), thread.ProcessMemory())
10221022
}
10231023
v, err := scope.evalAST(rbpi.retFrameCond)
10241024
if err != nil || v.Unreadable != nil || v.Kind != reflect.Bool {
@@ -1036,12 +1036,12 @@ func (rbpi *returnBreakpointInfo) Collect(t *Target, thread Thread) []*Variable
10361036
oldSP := uint64(rbpi.spOffset + int64(g.stack.hi))
10371037
err = fakeFunctionEntryScope(scope, rbpi.fn, oldFrameOffset, oldSP)
10381038
if err != nil {
1039-
return returnInfoError("could not read function entry", err, thread.ProcessMemory())
1039+
return returnInfoError("could not read function entry", err, thread.BinInfo(), thread.ProcessMemory())
10401040
}
10411041

10421042
vars, err := scope.Locals(0, "")
10431043
if err != nil {
1044-
return returnInfoError("could not evaluate return variables", err, thread.ProcessMemory())
1044+
return returnInfoError("could not evaluate return variables", err, thread.BinInfo(), thread.ProcessMemory())
10451045
}
10461046
vars = filterVariables(vars, func(v *Variable) bool {
10471047
return (v.Flags & VariableReturnArgument) != 0
@@ -1050,8 +1050,8 @@ func (rbpi *returnBreakpointInfo) Collect(t *Target, thread Thread) []*Variable
10501050
return vars
10511051
}
10521052

1053-
func returnInfoError(descr string, err error, mem MemoryReadWriter) []*Variable {
1054-
v := newConstant(constant.MakeString(fmt.Sprintf("%s: %v", descr, err.Error())), mem)
1053+
func returnInfoError(descr string, err error, bi *BinaryInfo, mem MemoryReadWriter) []*Variable {
1054+
v := newConstant(constant.MakeString(fmt.Sprintf("%s: %v", descr, err.Error())), bi, mem)
10551055
v.Name = "return value read error"
10561056
return []*Variable{v}
10571057
}

pkg/proc/eval.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,29 +1068,29 @@ func (stack *evalStack) executeOp() {
10681068
gvar := newVariable("curg", fakeAddressUnresolv, typ, scope.BinInfo, scope.Mem)
10691069
gvar.loaded = true
10701070
gvar.Flags = VariableFakeAddress
1071-
gvar.Children = append(gvar.Children, *newConstant(constant.MakeInt64(0), scope.Mem))
1071+
gvar.Children = append(gvar.Children, *newConstant(constant.MakeInt64(0), scope.BinInfo, scope.Mem))
10721072
gvar.Children[0].Name = "goid"
10731073
stack.push(gvar)
10741074
}
10751075

10761076
case *evalop.PushFrameoff:
1077-
stack.push(newConstant(constant.MakeInt64(scope.frameOffset), scope.Mem))
1077+
stack.push(newConstant(constant.MakeInt64(scope.frameOffset), scope.BinInfo, scope.Mem))
10781078

10791079
case *evalop.PushRangeParentOffset:
10801080
if scope.rangeFrames == nil {
10811081
stack.err = scope.setupRangeFrames()
10821082
}
10831083
if len(scope.rangeFrames) > 0 {
1084-
stack.push(newConstant(constant.MakeInt64(scope.rangeFrames[len(scope.rangeFrames)-2].FrameOffset()), scope.Mem))
1084+
stack.push(newConstant(constant.MakeInt64(scope.rangeFrames[len(scope.rangeFrames)-2].FrameOffset()), scope.BinInfo, scope.Mem))
10851085
} else {
1086-
stack.push(newConstant(constant.MakeInt64(0), scope.Mem))
1086+
stack.push(newConstant(constant.MakeInt64(0), scope.BinInfo, scope.Mem))
10871087
}
10881088

10891089
case *evalop.PushThreadID:
1090-
stack.push(newConstant(constant.MakeInt64(int64(scope.threadID)), scope.Mem))
1090+
stack.push(newConstant(constant.MakeInt64(int64(scope.threadID)), scope.BinInfo, scope.Mem))
10911091

10921092
case *evalop.PushConst:
1093-
stack.push(newConstant(op.Value, scope.Mem))
1093+
stack.push(newConstant(op.Value, scope.BinInfo, scope.Mem))
10941094

10951095
case *evalop.PushLocal:
10961096
found := stack.pushLocal(scope, op.Name, op.Frame)
@@ -1133,7 +1133,7 @@ func (stack *evalStack) executeOp() {
11331133

11341134
case *evalop.PushLen:
11351135
v := stack.peek()
1136-
stack.push(newConstant(constant.MakeInt64(v.Len), scope.Mem))
1136+
stack.push(newConstant(constant.MakeInt64(v.Len), scope.BinInfo, scope.Mem))
11371137

11381138
case *evalop.Select:
11391139
scope.evalStructSelector(op, stack)
@@ -1172,7 +1172,7 @@ func (stack *evalStack) executeOp() {
11721172
return
11731173
}
11741174
x.loadValue(loadFullValue)
1175-
stack.push(newConstant(x.Value, scope.Mem))
1175+
stack.push(newConstant(x.Value, scope.BinInfo, scope.Mem))
11761176

11771177
case *evalop.Pop:
11781178
stack.pop()
@@ -1335,7 +1335,7 @@ func (stack *evalStack) pushIdent(scope *EvalScope, name string) (found bool) {
13351335

13361336
switch name {
13371337
case "true", "false":
1338-
stack.push(newConstant(constant.MakeBool(name == "true"), scope.Mem))
1338+
stack.push(newConstant(constant.MakeBool(name == "true"), scope.BinInfo, scope.Mem))
13391339
return true
13401340
case "nil":
13411341
stack.push(nilVariable)
@@ -1846,18 +1846,18 @@ func capBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
18461846
}
18471847
fallthrough
18481848
case reflect.Array:
1849-
return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
1849+
return newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil
18501850
case reflect.Slice:
1851-
return newConstant(constant.MakeInt64(arg.Cap), arg.mem), nil
1851+
return newConstant(constant.MakeInt64(arg.Cap), arg.bi, arg.mem), nil
18521852
case reflect.Chan:
18531853
arg.loadValue(loadFullValue)
18541854
if arg.Unreadable != nil {
18551855
return nil, arg.Unreadable
18561856
}
18571857
if arg.Base == 0 {
1858-
return newConstant(constant.MakeInt64(0), arg.mem), nil
1858+
return newConstant(constant.MakeInt64(0), arg.bi, arg.mem), nil
18591859
}
1860-
return newConstant(arg.Children[1].Value, arg.mem), nil
1860+
return newConstant(arg.Children[1].Value, arg.bi, arg.mem), nil
18611861
default:
18621862
return nil, invalidArgErr
18631863
}
@@ -1881,25 +1881,25 @@ func lenBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
18811881
if arg.Unreadable != nil {
18821882
return nil, arg.Unreadable
18831883
}
1884-
return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
1884+
return newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil
18851885
case reflect.Chan:
18861886
arg.loadValue(loadFullValue)
18871887
if arg.Unreadable != nil {
18881888
return nil, arg.Unreadable
18891889
}
18901890
if arg.Base == 0 {
1891-
return newConstant(constant.MakeInt64(0), arg.mem), nil
1891+
return newConstant(constant.MakeInt64(0), arg.bi, arg.mem), nil
18921892
}
1893-
return newConstant(arg.Children[0].Value, arg.mem), nil
1893+
return newConstant(arg.Children[0].Value, arg.bi, arg.mem), nil
18941894
case reflect.Map:
18951895
it := arg.mapIterator(0)
18961896
if arg.Unreadable != nil {
18971897
return nil, arg.Unreadable
18981898
}
18991899
if it == nil {
1900-
return newConstant(constant.MakeInt64(0), arg.mem), nil
1900+
return newConstant(constant.MakeInt64(0), arg.bi, arg.mem), nil
19011901
}
1902-
return newConstant(constant.MakeInt64(arg.Len), arg.mem), nil
1902+
return newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil
19031903
default:
19041904
return nil, invalidArgErr
19051905
}
@@ -1970,7 +1970,7 @@ func imagBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
19701970
return nil, fmt.Errorf("invalid argument %s (type %s) to imag", astutil.ExprToString(nodeargs[0]), arg.TypeString())
19711971
}
19721972

1973-
return newConstant(constant.Imag(arg.Value), arg.mem), nil
1973+
return newConstant(constant.Imag(arg.Value), arg.bi, arg.mem), nil
19741974
}
19751975

19761976
func realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
@@ -1989,7 +1989,7 @@ func realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
19891989
return nil, fmt.Errorf("invalid argument %s (type %s) to real", astutil.ExprToString(nodeargs[0]), arg.TypeString())
19901990
}
19911991

1992-
return newConstant(constant.Real(arg.Value), arg.mem), nil
1992+
return newConstant(constant.Real(arg.Value), arg.bi, arg.mem), nil
19931993
}
19941994

19951995
func minBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
@@ -2121,7 +2121,7 @@ func (scope *EvalScope) evalIndex(op *evalop.Index, stack *evalStack) {
21212121
s := constant.StringVal(idxev.Value)
21222122
thc, err := totalHitCountByName(scope.target.Breakpoints().Logical, s)
21232123
if err == nil {
2124-
stack.push(newConstant(constant.MakeUint64(thc), scope.Mem))
2124+
stack.push(newConstant(constant.MakeUint64(thc), scope.BinInfo, scope.Mem))
21252125
}
21262126
stack.err = err
21272127
return
@@ -2137,7 +2137,7 @@ func (scope *EvalScope) evalIndex(op *evalop.Index, stack *evalStack) {
21372137
}
21382138
thc, err := totalHitCountByID(scope.target.Breakpoints().Logical, int(n))
21392139
if err == nil {
2140-
stack.push(newConstant(constant.MakeUint64(thc), scope.Mem))
2140+
stack.push(newConstant(constant.MakeUint64(thc), scope.BinInfo, scope.Mem))
21412141
}
21422142
stack.err = err
21432143
return
@@ -2371,7 +2371,7 @@ func (scope *EvalScope) evalUnary(op *evalop.Unary, stack *evalStack) {
23712371
stack.push(r)
23722372
return
23732373
}
2374-
stack.push(newConstant(rc, xv.mem))
2374+
stack.push(newConstant(rc, xv.bi, xv.mem))
23752375
}
23762376

23772377
func negotiateType(op token.Token, xv, yv *Variable) (godwarf.Type, error) {
@@ -2502,7 +2502,7 @@ func (scope *EvalScope) evalBinary(binop *evalop.Binary, stack *evalStack) {
25022502
stack.err = err
25032503
return
25042504
}
2505-
stack.push(newConstant(constant.MakeBool(v), xv.mem))
2505+
stack.push(newConstant(constant.MakeBool(v), xv.bi, xv.mem))
25062506

25072507
default:
25082508
if xv.Kind == reflect.String {
@@ -2528,7 +2528,7 @@ func (scope *EvalScope) evalBinary(binop *evalop.Binary, stack *evalStack) {
25282528
}
25292529

25302530
if typ == nil {
2531-
stack.push(newConstant(rc, xv.mem))
2531+
stack.push(newConstant(rc, xv.bi, xv.mem))
25322532
return
25332533
}
25342534

pkg/proc/fncall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func (scope *EvalScope) evalCallInjectionStart(op *evalop.CallInjectionStart, st
390390
p.fncallForG[scope.g.ID].startThreadID = thread.ThreadID()
391391

392392
stack.fncallPush(&fncall)
393-
stack.push(newConstant(constant.MakeBool(!fncall.hasDebugPinner && (fncall.fn == nil || fncall.receiver != nil || fncall.closureAddr != 0)), scope.Mem))
393+
stack.push(newConstant(constant.MakeBool(!fncall.hasDebugPinner && (fncall.fn == nil || fncall.receiver != nil || fncall.closureAddr != 0)), scope.BinInfo, scope.Mem))
394394
stack.callInjectionContinue = true
395395
}
396396

pkg/proc/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func setAsyncPreemptOff(p *Target, v int64) {
356356
p.asyncPreemptChanged = true
357357
p.asyncPreemptOff, _ = constant.Int64Val(asyncpreemptoffv.Value)
358358

359-
err = scope.setValue(asyncpreemptoffv, newConstant(constant.MakeInt64(v), scope.Mem), "")
359+
err = scope.setValue(asyncpreemptoffv, newConstant(constant.MakeInt64(v), scope.BinInfo, scope.Mem), "")
360360
if err != nil {
361361
logger.Warnf("could not set asyncpreemptoff %v", err)
362362
}

pkg/proc/variables.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ func newVariable(name string, addr uint64, dwarfType godwarf.Type, bi *BinaryInf
776776

777777
var constantMaxInt64 = constant.MakeInt64(1<<63 - 1)
778778

779-
func newConstant(val constant.Value, mem MemoryReadWriter) *Variable {
780-
v := &Variable{Value: val, mem: mem, loaded: true}
779+
func newConstant(val constant.Value, bi *BinaryInfo, mem MemoryReadWriter) *Variable {
780+
v := &Variable{Value: val, mem: mem, loaded: true, bi: bi}
781781
switch val.Kind() {
782782
case constant.Int:
783783
v.Kind = reflect.Int
@@ -2197,47 +2197,47 @@ func (v *Variable) registerVariableTypeConv(newtyp string) (*Variable, error) {
21972197
var child *Variable
21982198
switch newtyp {
21992199
case "int8":
2200-
child = newConstant(constant.MakeInt64(int64(int8(v.reg.Bytes[i]))), v.mem)
2200+
child = newConstant(constant.MakeInt64(int64(int8(v.reg.Bytes[i]))), v.bi, v.mem)
22012201
child.Kind = reflect.Int8
22022202
n = 1
22032203
case "int16":
2204-
child = newConstant(constant.MakeInt64(int64(int16(binary.LittleEndian.Uint16(v.reg.Bytes[i:])))), v.mem)
2204+
child = newConstant(constant.MakeInt64(int64(int16(binary.LittleEndian.Uint16(v.reg.Bytes[i:])))), v.bi, v.mem)
22052205
child.Kind = reflect.Int16
22062206
n = 2
22072207
case "int32":
2208-
child = newConstant(constant.MakeInt64(int64(int32(binary.LittleEndian.Uint32(v.reg.Bytes[i:])))), v.mem)
2208+
child = newConstant(constant.MakeInt64(int64(int32(binary.LittleEndian.Uint32(v.reg.Bytes[i:])))), v.bi, v.mem)
22092209
child.Kind = reflect.Int32
22102210
n = 4
22112211
case "int64":
2212-
child = newConstant(constant.MakeInt64(int64(binary.LittleEndian.Uint64(v.reg.Bytes[i:]))), v.mem)
2212+
child = newConstant(constant.MakeInt64(int64(binary.LittleEndian.Uint64(v.reg.Bytes[i:]))), v.bi, v.mem)
22132213
child.Kind = reflect.Int64
22142214
n = 8
22152215
case "uint8":
2216-
child = newConstant(constant.MakeUint64(uint64(v.reg.Bytes[i])), v.mem)
2216+
child = newConstant(constant.MakeUint64(uint64(v.reg.Bytes[i])), v.bi, v.mem)
22172217
child.Kind = reflect.Uint8
22182218
n = 1
22192219
case "uint16":
2220-
child = newConstant(constant.MakeUint64(uint64(binary.LittleEndian.Uint16(v.reg.Bytes[i:]))), v.mem)
2220+
child = newConstant(constant.MakeUint64(uint64(binary.LittleEndian.Uint16(v.reg.Bytes[i:]))), v.bi, v.mem)
22212221
child.Kind = reflect.Uint16
22222222
n = 2
22232223
case "uint32":
2224-
child = newConstant(constant.MakeUint64(uint64(binary.LittleEndian.Uint32(v.reg.Bytes[i:]))), v.mem)
2224+
child = newConstant(constant.MakeUint64(uint64(binary.LittleEndian.Uint32(v.reg.Bytes[i:]))), v.bi, v.mem)
22252225
child.Kind = reflect.Uint32
22262226
n = 4
22272227
case "uint64":
2228-
child = newConstant(constant.MakeUint64(binary.LittleEndian.Uint64(v.reg.Bytes[i:])), v.mem)
2228+
child = newConstant(constant.MakeUint64(binary.LittleEndian.Uint64(v.reg.Bytes[i:])), v.bi, v.mem)
22292229
child.Kind = reflect.Uint64
22302230
n = 8
22312231
case "float32":
22322232
a := binary.LittleEndian.Uint32(v.reg.Bytes[i:])
22332233
x := *(*float32)(unsafe.Pointer(&a))
2234-
child = newConstant(constant.MakeFloat64(float64(x)), v.mem)
2234+
child = newConstant(constant.MakeFloat64(float64(x)), v.bi, v.mem)
22352235
child.Kind = reflect.Float32
22362236
n = 4
22372237
case "float64":
22382238
a := binary.LittleEndian.Uint64(v.reg.Bytes[i:])
22392239
x := *(*float64)(unsafe.Pointer(&a))
2240-
child = newConstant(constant.MakeFloat64(x), v.mem)
2240+
child = newConstant(constant.MakeFloat64(x), v.bi, v.mem)
22412241
child.Kind = reflect.Float64
22422242
n = 8
22432243
default:
@@ -2253,7 +2253,7 @@ func (v *Variable) registerVariableTypeConv(newtyp string) (*Variable, error) {
22532253
}
22542254
n = n / 8
22552255
}
2256-
child = newConstant(constant.MakeString(fmt.Sprintf("%x", v.reg.Bytes[i:][:n])), v.mem)
2256+
child = newConstant(constant.MakeString(fmt.Sprintf("%x", v.reg.Bytes[i:][:n])), v.bi, v.mem)
22572257
}
22582258
v.Children = append(v.Children, *child)
22592259
}

0 commit comments

Comments
 (0)