This is an archive of the discontinued LLVM Phabricator instance.

DWARFDebugLine.cpp: Format unknown line number standard opcodes
ClosedPublic

Authored by hubert.reinterpretcast on Jan 7 2020, 3:23 PM.

Details

Summary

This patch implements formatv() formatting for dwarf::LineNumberOps and makes use of it for the llvm-dwarfdump --debug-line dump.

Previously, unknown line number standard opcodes would lead to undefined behaviour. The code would attempt to format the data pointer of an empty StringRef (a null pointer) using %s. According to the description for format(), use of that interface carries the "risk of printf". Passing a null pointer in place of an array to a C library function results in undefined behaviour.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJan 7 2020, 3:23 PM

I think a better solution is to print something more meaningful. For example, "DW_LNS_unknown_<value>" or something similar. You can see examples of that with the DW_AT_unknown_* printing in debug-abbrev.s. I don't think that will be significantly more complex and will provide more user-friendly output. See also my inline comment about where to put the fix.

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
697

If you put the fix in LNStandardString instead, this line will also print more user-friendly output in verbose mode (a test should be added for it, if you do).

DWARFDebugLine.cpp: Format unknown line number standard opcodes

I'd still really like to see a test that tests the other code path that tries to print DW_LNS_* (see the inline comment). The obvious thing to me to do would be to update the unit test "ParserPrintsStandardOpcodesWhenRequested", but it would require changes to the DwarfGenerator to create a custom prologue. If you don't want to do this, that's okay, as the code path is already untested.

hubert.reinterpretcast marked 2 inline comments as done.Jan 9 2020, 7:31 AM

I think a better solution is to print something more meaningful. For example, "DW_LNS_unknown_<value>" or something similar. You can see examples of that with the DW_AT_unknown_* printing in debug-abbrev.s. I don't think that will be significantly more complex and will provide more user-friendly output.

Thanks for the pointer. I have set up "DW_LNS_unknown_<value>" printing based on the DW_AT_unknown_* printing.

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
697

I looked into this, and LNStandardString is part of a family of functions where returning an empty StringRef is the documented interface for unknown values.

operator<< is okay with empty StringRefs, and the opcode value is already printed. Changing the output here does not really fit with this patch.

hubert.reinterpretcast marked an inline comment as done.Jan 9 2020, 7:47 AM

I'd still really like to see a test that tests the other code path that tries to print DW_LNS_* (see the inline comment). The obvious thing to me to do would be to update the unit test "ParserPrintsStandardOpcodesWhenRequested", but it would require changes to the DwarfGenerator to create a custom prologue. If you don't want to do this, that's okay, as the code path is already untested.

Yes, that is rather out-of-the-way for what this patch is doing. My understanding is that you would be okay with letting this patch land?

jhenderson accepted this revision.Jan 9 2020, 8:06 AM

I'd still really like to see a test that tests the other code path that tries to print DW_LNS_* (see the inline comment). The obvious thing to me to do would be to update the unit test "ParserPrintsStandardOpcodesWhenRequested", but it would require changes to the DwarfGenerator to create a custom prologue. If you don't want to do this, that's okay, as the code path is already untested.

Yes, that is rather out-of-the-way for what this patch is doing. My understanding is that you would be okay with letting this patch land?

Just wanted to suggest the idea, but what you've done is good on its own, thanks. LGTM.

This revision is now accepted and ready to land.Jan 9 2020, 8:06 AM
hubert.reinterpretcast retitled this revision from DWARFDebugLine.cpp: Format NULL as "(null)" to avoid UB to DWARFDebugLine.cpp: Format unknown line number standard opcodes.Jan 15 2020, 7:45 AM
hubert.reinterpretcast edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.