From d2b6665e33944eb04ea699124f15850ba22a144a Mon Sep 17 00:00:00 2001 From: Jeremy Morse Date: Mon, 1 Jul 2019 09:38:23 +0000 Subject: [DebugInfo] Avoid adding too much indirection to pointer-valued variables This patch addresses PR41675, where a stack-pointer variable is dereferenced too many times by its location expression, presenting a value on the stack as the pointer to the stack. The difference between a stack *pointer* DBG_VALUE and one that refers to a value on the stack, is currently the indirect flag. However the DWARF backend will also try to guess whether something is a memory location or not, based on whether there is any computation in the location expression. By simply prepending the stack offset to existing expressions, we can accidentally convert a register location into a memory location, which introduces a suprise (and unintended) dereference. The solution is to add DW_OP_stack_value whenever we add a DIExpression computation to a stack *pointer*. It's an implicit location computed on the expression stack, thus needs to be flagged as a stack_value. For the edge case where the offset is zero and the location could be a register location, DIExpression::prepend will still generate opcodes, and thus DW_OP_stack_value must still be added. Differential Revision: https://reviews.llvm.org/D63429 llvm-svn: 364736 --- llvm/lib/CodeGen/PrologEpilogInserter.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'llvm/lib/CodeGen/PrologEpilogInserter.cpp') diff --git a/llvm/lib/CodeGen/PrologEpilogInserter.cpp b/llvm/lib/CodeGen/PrologEpilogInserter.cpp index a7dbc044b9f..8e31c070714 100644 --- a/llvm/lib/CodeGen/PrologEpilogInserter.cpp +++ b/llvm/lib/CodeGen/PrologEpilogInserter.cpp @@ -1200,6 +1200,16 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF, MI.getOperand(0).setIsDebug(); const DIExpression *DIExpr = MI.getDebugExpression(); + + // If we have a direct DBG_VALUE, and its location expression isn't + // currently complex, then adding an offset will morph it into a + // complex location that is interpreted as being a memory address. + // This changes a pointer-valued variable to dereference that pointer, + // which is incorrect. Fix by adding DW_OP_stack_value. + unsigned PrependFlags = DIExpression::ApplyOffset; + if (!MI.isIndirectDebugValue() && !DIExpr->isComplex()) + PrependFlags |= DIExpression::StackValue; + // If we have DBG_VALUE that is indirect and has a Implicit location // expression need to insert a deref before prepending a Memory // location expression. Also after doing this we change the DBG_VALUE @@ -1211,8 +1221,7 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF, // Make the DBG_VALUE direct. MI.getOperand(1).ChangeToRegister(0, false); } - DIExpr = - DIExpression::prepend(DIExpr, DIExpression::ApplyOffset, Offset); + DIExpr = DIExpression::prepend(DIExpr, PrependFlags, Offset); MI.getOperand(3).setMetadata(DIExpr); continue; } -- cgit v1.2.3