Apply llvm fix for assertion failure compiling recent libc++

Merge commit c7c84b90879f from llvm git (by Adrian Prantl):

  [DwarfDebug] Refuse to emit DW_OP_LLVM_arg values wider than 64 bits

  DwarfExpression::addUnsignedConstant(const APInt &Value) only supports
  wider-than-64-bit values when it is used to emit a top-level DWARF
  expression representing the location of a variable. Before this change,
  it was possible to call addUnsignedConstant on >64 bit values within a
  subexpression when substituting DW_OP_LLVM_arg values.

  This can trigger an assertion failure (e.g. PR52584, PR52333) when it
  happens in a fragment (DW_OP_LLVM_fragment) expression, as
  addUnsignedConstant on >64 bit values splits the constant into separate
  DW_OP_pieces, which modifies DwarfExpression::OffsetInBits.

  This change papers over the assertion errors by bailing on overly wide
  DW_OP_LLVM_arg values. A more comprehensive fix might be to be to split
  wide values into pointer-sized fragments.

  [0] https://github.com/llvm/llvm-project/blob/e71fa03/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp#L799-L805

  Patch by Ricky Zhou!

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

MFC after:	3 days
This commit is contained in:
Dimitry Andric 2022-01-29 22:28:12 +01:00
parent 79afae3b3f
commit 9738bc28ab
3 changed files with 33 additions and 22 deletions

View File

@ -779,7 +779,7 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
auto AddEntry = [&](const DbgValueLocEntry &Entry,
DIExpressionCursor &Cursor) {
DIExpressionCursor &Cursor) {
if (Entry.isLocation()) {
if (!DwarfExpr.addMachineRegExpression(TRI, Cursor,
Entry.getLoc().getReg()))
@ -788,11 +788,19 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
// If there is an expression, emit raw unsigned bytes.
DwarfExpr.addUnsignedConstant(Entry.getInt());
} else if (Entry.isConstantFP()) {
// DwarfExpression does not support arguments wider than 64 bits
// (see PR52584).
// TODO: Consider chunking expressions containing overly wide
// arguments into separate pointer-sized fragment expressions.
APInt RawBytes = Entry.getConstantFP()->getValueAPF().bitcastToAPInt();
DwarfExpr.addUnsignedConstant(RawBytes);
if (RawBytes.getBitWidth() > 64)
return false;
DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
} else if (Entry.isConstantInt()) {
APInt RawBytes = Entry.getConstantInt()->getValue();
DwarfExpr.addUnsignedConstant(RawBytes);
if (RawBytes.getBitWidth() > 64)
return false;
DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
} else if (Entry.isTargetIndexLocation()) {
TargetIndexLocation Loc = Entry.getTargetIndexLocation();
// TODO TargetIndexLocation is a target-independent. Currently only the
@ -805,11 +813,12 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
return true;
};
DwarfExpr.addExpression(
std::move(Cursor),
[&](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
return AddEntry(DVal->getLocEntries()[Idx], Cursor);
});
if (!DwarfExpr.addExpression(
std::move(Cursor),
[&](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
return AddEntry(DVal->getLocEntries()[Idx], Cursor);
}))
return VariableDie;
// Now attach the location information to the DIE.
addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());

View File

@ -463,15 +463,14 @@ static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
return true;
}
void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
unsigned FragmentOffsetInBits) {
void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor) {
addExpression(std::move(ExprCursor),
[](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
llvm_unreachable("unhandled opcode found in expression");
});
}
void DwarfExpression::addExpression(
bool DwarfExpression::addExpression(
DIExpressionCursor &&ExprCursor,
llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg) {
// Entry values can currently only cover the initial register location,
@ -496,7 +495,7 @@ void DwarfExpression::addExpression(
case dwarf::DW_OP_LLVM_arg:
if (!InsertArg(Op->getArg(0), ExprCursor)) {
LocationKind = Unknown;
return;
return false;
}
break;
case dwarf::DW_OP_LLVM_fragment: {
@ -527,7 +526,7 @@ void DwarfExpression::addExpression(
setSubRegisterPiece(0, 0);
// Reset the location description kind.
LocationKind = Unknown;
return;
return true;
}
case dwarf::DW_OP_plus_uconst:
assert(!isRegisterLocation());
@ -630,6 +629,8 @@ void DwarfExpression::addExpression(
if (isImplicitLocation() && !isParameterValue())
// Turn this into an implicit location description.
addStackValue();
return true;
}
/// add masking operations to stencil out a subregister.

View File

@ -340,16 +340,17 @@ class DwarfExpression {
/// create one if necessary.
unsigned getOrCreateBaseType(unsigned BitSize, dwarf::TypeKind Encoding);
/// Emit all remaining operations in the DIExpressionCursor. The
/// cursor must not contain any DW_OP_LLVM_arg operations.
void addExpression(DIExpressionCursor &&Expr);
/// Emit all remaining operations in the DIExpressionCursor.
///
/// \param FragmentOffsetInBits If this is one fragment out of multiple
/// locations, this is the offset of the
/// fragment inside the entire variable.
void addExpression(DIExpressionCursor &&Expr,
unsigned FragmentOffsetInBits = 0);
void
addExpression(DIExpressionCursor &&Expr,
llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg);
/// DW_OP_LLVM_arg operations are resolved by calling (\p InsertArg).
//
/// \return false if any call to (\p InsertArg) returns false.
bool addExpression(
DIExpressionCursor &&Expr,
llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg);
/// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
/// the fragment described by \c Expr.