{"record":{"id":"bd9bf8c3836e495e","repo":"go-delve/delve","slug":"errstackunderflow","errorCode":"ErrStackUnderflow","errorMessage":"DWARF stack underflow","messagePattern":"DWARF stack underflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/dwarf/op/op.go","lineNumber":53,"sourceCode":"// Piece is a piece of memory stored either at an address or in a register.\ntype Piece struct {\n\tSize  int\n\tKind  PieceKind\n\tVal   uint64\n\tBytes []byte\n}\n\n// PieceKind describes the kind of a piece.\ntype PieceKind uint8\n\nconst (\n\tAddrPiece PieceKind = iota // The piece is stored in memory, Val is the address\n\tRegPiece                   // The piece is stored in a register, Val is the register number\n\tImmPiece                   // The piece is an immediate value, Val or Bytes is the value\n)\n\nvar (\n\tErrStackUnderflow        = errors.New(\"DWARF stack underflow\")\n\tErrStackIndexOutOfBounds = errors.New(\"DWARF stack index out of bounds\")\n\tErrMemoryReadUnavailable = errors.New(\"memory read unavailable\")\n)\n\nconst arbitraryExecutionLimitFactor = 10\n\n// ExecuteStackProgram executes a DWARF location expression and returns\n// either an address (int64), or a slice of Pieces for location expressions\n// that don't evaluate to an address (such as register and composite expressions).\nfunc ExecuteStackProgram(regs DwarfRegisters, instructions []byte, ptrSize int, readMemory ReadMemoryFunc) (int64, []Piece, error) {\n\tctxt := &context{\n\t\tbuf:            bytes.NewBuffer(instructions),\n\t\tprog:           instructions,\n\t\tstack:          make([]int64, 0, 3),\n\t\tDwarfRegisters: regs,\n\t\tptrSize:        ptrSize,\n\t\treadMemory:     readMemory,\n\t}","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/dwarf/op/op.go#L35-L71","documentation":"ErrStackUnderflow is a sentinel error indicating a DWARF location-expression stack operation ran out of operands. Operations like dup/drop/swap/rot (stack manipulation), unaryop and binaryop pop one or two values off the DWARF value stack; if the stack has too few values, this error is returned by ExecuteStackProgram.","triggerScenarios":"Executing a malformed or truncated DWARF location expression via op.ExecuteStackProgram where a dup, drop, swap, rot, unaryop, or binaryop opcode needs more stack operands than are present.","commonSituations":"Corrupted or hand-crafted DWARF debug info, compiler bugs emitting bad expression streams, or a mis-parsed byte slice passed as 'instructions' so the opcode stream is misaligned.","solutions":["Verify the binary's DWARF data is intact and produced by a standard toolchain (recompile with debug info)","Check that the byte slice passed as instructions is the raw DWARF expression, not a padded or offset slice","If you control the expression generation, validate that every producer opcode precedes its consumers","Catch errors.Is(err, op.ErrStackUnderflow) and fall back to reporting the variable as unavailable"],"exampleFix":"// before\nval, pieces, err := op.ExecuteStackProgram(regs, instr, ptrSize, nil)\n// after\nval, pieces, err := op.ExecuteStackProgram(regs, instr, ptrSize, nil)\nif errors.Is(err, op.ErrStackUnderflow) {\n    return 0, nil, fmt.Errorf(\"malformed DWARF expression: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"val, pieces, err := op.ExecuteStackProgram(regs, instr, ptrSize, mem)\nif errors.Is(err, op.ErrStackUnderflow) {\n    // malformed expression: report variable as unavailable\n}","preventionTips":["Ensure debug info comes from a standard, current toolchain","Never hand-truncate DWARF expression byte slices","Unit-test custom expressions with assertExprResult before shipping"],"tags":["dwarf","stack-underflow","expression-evaluation"],"backgroundTag":"dwarf-stack-underflow","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}