From 2f867a63daf99dc27830d4442a574a790e02f27e Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 6 Jan 2011 16:48:42 +0000 Subject: [PATCH] Correctly disassemble truncated asm. Patch by Richard Simth. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122962 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/X86/Disassembler/X86DisassemblerDecoder.c | 6 ++++-- test/MC/Disassembler/X86/truncated-input.txt | 4 ++++ tools/llvm-mc/Disassembler.cpp | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 test/MC/Disassembler/X86/truncated-input.txt diff --git a/lib/Target/X86/Disassembler/X86DisassemblerDecoder.c b/lib/Target/X86/Disassembler/X86DisassemblerDecoder.c index 1fd66853f75..0af510c4cec 100644 --- a/lib/Target/X86/Disassembler/X86DisassemblerDecoder.c +++ b/lib/Target/X86/Disassembler/X86DisassemblerDecoder.c @@ -511,7 +511,8 @@ static int getIDWithAttrMask(uint16_t* instructionID, insn->opcode); if (hasModRMExtension) { - readModRM(insn); + if (readModRM(insn)) + return -1; *instructionID = decode(insn->opcodeType, instructionClass, @@ -860,7 +861,8 @@ static int readModRM(struct InternalInstruction* insn) { if (insn->consumedModRM) return 0; - consumeByte(insn, &insn->modRM); + if (consumeByte(insn, &insn->modRM)) + return -1; insn->consumedModRM = TRUE; mod = modFromModRM(insn->modRM); diff --git a/test/MC/Disassembler/X86/truncated-input.txt b/test/MC/Disassembler/X86/truncated-input.txt new file mode 100644 index 00000000000..34cf0382a74 --- /dev/null +++ b/test/MC/Disassembler/X86/truncated-input.txt @@ -0,0 +1,4 @@ +# RUN: llvm-mc --disassemble %s -triple=x86_64-apple-darwin9 |& FileCheck %s + +# CHECK: warning +0x00 diff --git a/tools/llvm-mc/Disassembler.cpp b/tools/llvm-mc/Disassembler.cpp index e5c31ca598e..c29d82a2cb3 100644 --- a/tools/llvm-mc/Disassembler.cpp +++ b/tools/llvm-mc/Disassembler.cpp @@ -44,7 +44,7 @@ public: uint64_t getExtent() const { return Bytes.size(); } int readByte(uint64_t Addr, uint8_t *Byte) const { - if (Addr > getExtent()) + if (Addr >= getExtent()) return -1; *Byte = Bytes[Addr].first; return 0;