[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
This commit is contained in:
Adrian Prantl 2021-12-10 09:15:54 -08:00
parent 5861cf77da
commit c7c84b9087
4 changed files with 66 additions and 22 deletions

View File

@ -789,7 +789,7 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
auto AddEntry = [&](const DbgValueLocEntry &Entry, auto AddEntry = [&](const DbgValueLocEntry &Entry,
DIExpressionCursor &Cursor) { DIExpressionCursor &Cursor) {
if (Entry.isLocation()) { if (Entry.isLocation()) {
if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, if (!DwarfExpr.addMachineRegExpression(TRI, Cursor,
Entry.getLoc().getReg())) Entry.getLoc().getReg()))
@ -798,11 +798,19 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
// If there is an expression, emit raw unsigned bytes. // If there is an expression, emit raw unsigned bytes.
DwarfExpr.addUnsignedConstant(Entry.getInt()); DwarfExpr.addUnsignedConstant(Entry.getInt());
} else if (Entry.isConstantFP()) { } 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(); APInt RawBytes = Entry.getConstantFP()->getValueAPF().bitcastToAPInt();
DwarfExpr.addUnsignedConstant(RawBytes); if (RawBytes.getBitWidth() > 64)
return false;
DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
} else if (Entry.isConstantInt()) { } else if (Entry.isConstantInt()) {
APInt RawBytes = Entry.getConstantInt()->getValue(); APInt RawBytes = Entry.getConstantInt()->getValue();
DwarfExpr.addUnsignedConstant(RawBytes); if (RawBytes.getBitWidth() > 64)
return false;
DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
} else if (Entry.isTargetIndexLocation()) { } else if (Entry.isTargetIndexLocation()) {
TargetIndexLocation Loc = Entry.getTargetIndexLocation(); TargetIndexLocation Loc = Entry.getTargetIndexLocation();
// TODO TargetIndexLocation is a target-independent. Currently only the // TODO TargetIndexLocation is a target-independent. Currently only the
@ -815,11 +823,12 @@ DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
return true; return true;
}; };
DwarfExpr.addExpression( if (!DwarfExpr.addExpression(
std::move(Cursor), std::move(Cursor),
[&](unsigned Idx, DIExpressionCursor &Cursor) -> bool { [&](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
return AddEntry(DVal->getLocEntries()[Idx], Cursor); return AddEntry(DVal->getLocEntries()[Idx], Cursor);
}); }))
return VariableDie;
// Now attach the location information to the DIE. // Now attach the location information to the DIE.
addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());

View File

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

View File

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

View File

@ -0,0 +1,33 @@
; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -o - %s \
; RUN: | llvm-dwarfdump - \
; RUN: | FileCheck %s
; CHECK: DW_TAG_variable
; CHECK-NOT: DW_AT_location
; CHECK-NEXT: DW_AT_name ("arr")
; CHECK-NOT: DW_AT_location
; CHECK: DW_TAG
define dso_local void @test() !dbg !4 {
entry:
call void @llvm.dbg.value(metadata !DIArgList(i128 0), metadata !7, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 127)), !dbg !12
ret void, !dbg !12
}
declare void @llvm.dbg.value(metadata, metadata, metadata)
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "test.c", directory: "")
!2 = !{i32 7, !"Dwarf Version", i32 4}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 1, type: !5, unit: !0)
!5 = !DISubroutineType(types: !6)
!6 = !{null}
!7 = !DILocalVariable(name: "arr", scope: !4, file: !1, line: 1, type: !8)
!8 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, size: 128, elements: !10)
!9 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!10 = !{!11}
!11 = !DISubrange(count: 16)
!12 = !DILocation(line: 1, column: 1, scope: !4)