Skip to content

Commit

Permalink
[RISCV] Fix AddressSanitizer heap-buffer-overflow in disassembling
Browse files Browse the repository at this point in the history
Summary:
RISCVDisassembler should check number of bytes available before reading them.
Crash noticed when enabling -DLLVM_USE_SANITIZER=Address.

This bug was uncovered by a LLVM MC Disassembler Protocol Buffer Fuzzer for the RISC-V assembly language.

Reviewers: asb

Reviewed By: asb

Subscribers: rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, mgrang, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, asb

Differential Revision: https://reviews.llvm.org/D51708

llvm-svn: 341686
  • Loading branch information
Ana Pazos committed Sep 7, 2018
1 parent f06ffee commit b97d189
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,19 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,

// It's a 32 bit instruction if bit 0 and 1 are 1.
if ((Bytes[0] & 0x3) == 0x3) {
if (Bytes.size() < 4) {
Size = 0;
return MCDisassembler::Fail;
}
Insn = support::endian::read32le(Bytes.data());
LLVM_DEBUG(dbgs() << "Trying RISCV32 table :\n");
Result = decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
Size = 4;
} else {
if (Bytes.size() < 2) {
Size = 0;
return MCDisassembler::Fail;
}
Insn = support::endian::read16le(Bytes.data());

if (!STI.getFeatureBits()[RISCV::Feature64Bit]) {
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/MC/Disassembler/RISCV/fuzzer-invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RUN: not llvm-mc -disassemble -triple=riscv32 < %s 2>&1 | FileCheck %s
# RUN: not llvm-mc -disassemble -triple=riscv64 < %s 2>&1 | FileCheck %s
#
# Test generated by a LLVM MC Disassembler Protocol Buffer Fuzzer
# for the RISC-V assembly language.

[0xf9 0x95 0xab 0x99]
# CHECK: warning: invalid instruction encoding
3 changes: 3 additions & 0 deletions llvm/test/MC/Disassembler/RISCV/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if not 'RISCV' in config.root.targets:
config.unsupported = True

0 comments on commit b97d189

Please sign in to comment.