[PATCH] D49234: [DebugInfo] Refactor DWARFDie::iterator to prevent UB

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 12 07:07:42 PDT 2018


JDevlieghere created this revision.
JDevlieghere added reviewers: dblaikie, thegameg.
Herald added a subscriber: aprantl.

The DWARFDie is a lightweight utility wrapper that stores a pointer to a compile unit and a debug info entry. Currently, its iterator (used for walking over its children) stores a DWARFDie and returns a const reference when dereferencing it. However, when the iterator is modified (by incrementing or decrementing it), this reference becomes invalid. This was happening when calling reverse on it, because the std::reverse_iterator is keeping a temporary copy of the iterator (see https://en.cppreference.com/w/cpp/iterator/reverse_iterator for a good illustration).

  reference operator*() const {_Iter __tmp = current; return *--__tmp;}

When dereferencing the reverse iterator, we decrement and return a reference to a DWARFDie stored in the stack frame of this function, resulting in UB at runtime.

This patch changes the DWARFDie::iterator into something that looks like an iterator, but actually isn't. When dereferencing it, we return a copy of the DWARFDie instead. This means we can't have `operator->`, but at least we don't risk invalidating the iterator by modifying it.


Repository:
  rL LLVM

https://reviews.llvm.org/D49234

Files:
  llvm/include/llvm/DebugInfo/DWARF/DWARFDie.h
  llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49234.155167.patch
Type: text/x-patch
Size: 4092 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180712/dbc222fd/attachment.bin>


More information about the llvm-commits mailing list