[PATCH] D105404: [lld/mac] Fix function offset on 1st-level unwind table sentinel

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 3 19:32:40 PDT 2021


thakis created this revision.
thakis added a reviewer: lld-macho.
Herald added a reviewer: int3.
Herald added a reviewer: gkm.
Herald added a project: lld-macho.
thakis requested review of this revision.

Two bugs:

1. This tries to take the address of the last symbol plus the length of the last symbol. However, the sorted vector is cuPtrVector, not cuVector. Also, cuPtrVector has tombstone values removed and cuVector doesn't. If there was a stripped value at the end, the "last" element's value was UINT64_MAX, which meant the sentinel value was one less than the length of that "last" dead symbol.

2. We have to subtract in.header->addr. For 64-bit binaries that's (1 << 32) and functionAddress is 32-bit so this is a no-op, but for 32-bit binaries the sentinel's value was too large.

I believe this has no effect in practice since the first-level
binary search code in libunwind (in UnwindCursor.hpp) does:

  uint32_t low = 0;
  uint32_t high = sectionHeader.indexCount();
  uint32_t last = high - 1;
  while (low < high) {
    uint32_t mid = (low + high) / 2;
      if ((mid == last) ||
          (topIndex.functionOffset(mid + 1) > targetFunctionOffset)) {
        low = mid;
        break;
      } else {
      low = mid + 1;
    }

So the address of the last entry in the first-level table isn't really
checked -- except for the very end, but the check against `last` means
we just run the loop once more than necessary. But it makes `unwinddump` output
look less confusing, and it's what it looks was the intention here.

(No test since I can't think of a way to make FileCheck check that one
number is larger than another. But the included assert covers at least
the first bug.)


https://reviews.llvm.org/D105404

Files:
  lld/MachO/UnwindInfoSection.cpp
  lld/test/MachO/compact-unwind.s


Index: lld/test/MachO/compact-unwind.s
===================================================================
--- lld/test/MachO/compact-unwind.s
+++ lld/test/MachO/compact-unwind.s
@@ -43,10 +43,14 @@
 # CHECK:        Personality functions: (count = 2)
 # CHECK-DAG:     personality[{{[0-9]+}}]: 0x{{0*}}[[#MY_PERSONALITY-BASE]]
 # CHECK-DAG:     personality[{{[0-9]+}}]: 0x{{0*}}[[#GXX_PERSONALITY-BASE]]
+# CHECK:      Top level indices: (count = 2)
+# CHECK-DAG:    [0]: function offset={{.*}}, 2nd level page offset=[[#%x,PAGEOFF:]], LSDA offset=
+# CHECK-DAG:    [1]: function offset={{.*}}, 2nd level page offset=0x00000000, LSDA offset=
 # CHECK:        LSDA descriptors:
 # CHECK-DAG:     function offset=0x[[#%.8x,FOO-BASE]],  LSDA offset=0x[[#%.8x,EXCEPTION0-BASE]]
 # CHECK-DAG:     function offset=0x[[#%.8x,MAIN-BASE]], LSDA offset=0x[[#%.8x,EXCEPTION1-BASE]]
 # CHECK:        Second level indices:
+# CHECK-NEXT:   Second level index[0]: offset in section={{.*}}, base function offset=0x[[#.8x,PAGEOFF]]
 # CHECK-DAG:     function offset=0x[[#%.8x,MAIN-BASE]], encoding
 # CHECK-DAG:     function offset=0x[[#%.8x,FOO-BASE]], encoding
 # CHECK-DAG:     function offset=0x[[#%.8x,BAZ-BASE]], encoding
Index: lld/MachO/UnwindInfoSection.cpp
===================================================================
--- lld/MachO/UnwindInfoSection.cpp
+++ lld/MachO/UnwindInfoSection.cpp
@@ -568,8 +568,10 @@
     l2PagesOffset += SECOND_LEVEL_PAGE_BYTES;
   }
   // Level-1 sentinel
-  const CompactUnwindEntry<Ptr> &cuEnd = cuVector.back();
-  iep->functionOffset = cuEnd.functionAddress + cuEnd.functionLength;
+  const CompactUnwindEntry<Ptr> &cuEnd = *cuPtrVector.back();
+  assert(cuEnd.functionAddress != static_cast<Ptr>(UINT64_MAX));
+  iep->functionOffset =
+      cuEnd.functionAddress - in.header->addr + cuEnd.functionLength;
   iep->secondLevelPagesSectionOffset = 0;
   iep->lsdaIndexArraySectionOffset =
       lsdaOffset +


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105404.356361.patch
Type: text/x-patch
Size: 1943 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210704/d44da2b8/attachment.bin>


More information about the llvm-commits mailing list