From 8dd4cf158160460009483f91db44c805ad152dce Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Wed, 4 Mar 2015 17:39:33 +0000 Subject: [PATCH] Fix DwarfExpression::AddMachineRegExpression so it doesn't read past the end of an expression that ends with DW_OP_plus. Caught by the ASAN build bots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231260 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/DebugInfo.h | 2 +- lib/CodeGen/AsmPrinter/DwarfExpression.cpp | 26 +++++++++++++--------- lib/IR/DebugInfo.cpp | 4 ++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/llvm/IR/DebugInfo.h b/include/llvm/IR/DebugInfo.h index e2189ae81c2..5ab8a297465 100644 --- a/include/llvm/IR/DebugInfo.h +++ b/include/llvm/IR/DebugInfo.h @@ -1031,7 +1031,7 @@ public: /// \brief Returns underlying MDExpression::element_iterator. const MDExpression::element_iterator &getBase() const { return I; } /// \brief Returns the next operand. - Operand getNext() const; + iterator getNext() const; }; /// \brief An iterator for DIExpression elements. diff --git a/lib/CodeGen/AsmPrinter/DwarfExpression.cpp b/lib/CodeGen/AsmPrinter/DwarfExpression.cpp index 86954e90b68..489e455c122 100644 --- a/lib/CodeGen/AsmPrinter/DwarfExpression.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfExpression.cpp @@ -196,11 +196,12 @@ bool DwarfExpression::AddMachineRegExpression(DIExpression Expr, unsigned MachineReg, unsigned PieceOffsetInBits) { auto I = Expr.begin(); - // Pattern-match combinations for which more efficient representations exist - // first. - if (I == Expr.end()) + auto E = Expr.end(); + if (I == E) return AddMachineRegPiece(MachineReg); + // Pattern-match combinations for which more efficient representations exist + // first. bool ValidReg = false; switch (*I) { case dwarf::DW_OP_bit_piece: { @@ -210,20 +211,23 @@ bool DwarfExpression::AddMachineRegExpression(DIExpression Expr, return AddMachineRegPiece(MachineReg, SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits)); } - case dwarf::DW_OP_plus: + case dwarf::DW_OP_plus: { // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset]. - if (I->getNext() == dwarf::DW_OP_deref) { + auto N = I->getNext(); + if ((N != E) && (*N == dwarf::DW_OP_deref)) { unsigned Offset = I->getArg(1); ValidReg = AddMachineRegIndirect(MachineReg, Offset); std::advance(I, 2); break; } else ValidReg = AddMachineRegPiece(MachineReg); - case dwarf::DW_OP_deref: - // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg]. - ValidReg = AddMachineRegIndirect(MachineReg); - ++I; - break; + } + case dwarf::DW_OP_deref: { + // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg]. + ValidReg = AddMachineRegIndirect(MachineReg); + ++I; + break; + } default: llvm_unreachable("unsupported operand"); } @@ -232,7 +236,7 @@ bool DwarfExpression::AddMachineRegExpression(DIExpression Expr, return false; // Emit remaining elements of the expression. - AddExpression(I, Expr.end(), PieceOffsetInBits); + AddExpression(I, E, PieceOffsetInBits); return true; } diff --git a/lib/IR/DebugInfo.cpp b/lib/IR/DebugInfo.cpp index d44d6c9e15b..3db74fcee58 100644 --- a/lib/IR/DebugInfo.cpp +++ b/lib/IR/DebugInfo.cpp @@ -183,9 +183,9 @@ uint64_t DIExpression::getBitPieceSize() const { return getElement(getNumElements()-1); } -DIExpression::Operand DIExpression::Operand::getNext() const { +DIExpression::iterator DIExpression::Operand::getNext() const { iterator it(I); - return *(++it); + return ++it; } //===----------------------------------------------------------------------===//