pkg/proc: Judge the validity of addr ranges when disasm. (#1872)

Avoid panic if start addr is greater than end addr when disasm.
This commit is contained in:
chainhelen 2020-02-19 10:46:03 -06:00 committed by GitHub
parent 8db632e55b
commit 4f04b81c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

@ -1,5 +1,7 @@
package proc package proc
import "fmt"
// AsmInstruction represents one assembly instruction. // AsmInstruction represents one assembly instruction.
type AsmInstruction struct { type AsmInstruction struct {
Loc Location Loc Location
@ -101,6 +103,9 @@ func checkPrologue(s []AsmInstruction, prologuePattern opcodeSeq) bool {
// will evaluate the argument of the CALL instruction using the thread's registers. // will evaluate the argument of the CALL instruction using the thread's registers.
// Be aware that the Bytes field of each returned instruction is a slice of a larger array of size startAddr - endAddr. // Be aware that the Bytes field of each returned instruction is a slice of a larger array of size startAddr - endAddr.
func Disassemble(mem MemoryReadWriter, regs Registers, breakpoints *BreakpointMap, bi *BinaryInfo, startAddr, endAddr uint64) ([]AsmInstruction, error) { func Disassemble(mem MemoryReadWriter, regs Registers, breakpoints *BreakpointMap, bi *BinaryInfo, startAddr, endAddr uint64) ([]AsmInstruction, error) {
if startAddr > endAddr {
return nil, fmt.Errorf("start address(%x) should be less than end address(%x)", startAddr, endAddr)
}
return disassemble(mem, regs, breakpoints, bi, startAddr, endAddr, false) return disassemble(mem, regs, breakpoints, bi, startAddr, endAddr, false)
} }

@ -961,6 +961,11 @@ func TestDisasm(t *testing.T) {
pcstart := d1[0].Loc.PC pcstart := d1[0].Loc.PC
pcend := d1[len(d1)-1].Loc.PC + uint64(len(d1[len(d1)-1].Bytes)) pcend := d1[len(d1)-1].Loc.PC + uint64(len(d1[len(d1)-1].Bytes))
// start address should be less than end address
_, err = c.DisassembleRange(api.EvalScope{-1, 0, 0}, pcend, pcstart, api.IntelFlavour)
assertError(err, t, "DisassembleRange()")
d2, err := c.DisassembleRange(api.EvalScope{-1, 0, 0}, pcstart, pcend, api.IntelFlavour) d2, err := c.DisassembleRange(api.EvalScope{-1, 0, 0}, pcstart, pcend, api.IntelFlavour)
assertNoError(err, t, "DisassembleRange()") assertNoError(err, t, "DisassembleRange()")