Skip to content

Commit b6a9396

Browse files
committed
[NFC][libunwind] Fix uintptr_t vs size_t confusion for lengths
These two are not conceptually the same; the former is a pointer shoved in an integer, the latter is an offset or length. On the architectures supported by libunwind, these two have the same underlying type, namely unsigned int on ILP32, unsigned long on LP64 and unsigned long long on LLP64. However, on CHERI, and thus Arm's Morello, they are not the same, as pointers are hardware capabilities that carry additional metadata including bounds and permissions, which is preserved in uintptr_t but not in size_t. Thus, fix all length variables to be of type size_t not uintptr_t, as we have done downstream for a while in CHERI LLVM but did not get round to upstreaming. Note that dyld_unwind_sections is currently defined in Apple's headers as genuinely using uintptr_t to represent lengths. This is a bad API and should be fixed, which would be totally API and ABI compatible due to size_t and uintptr_t being the same type on all supported Apple systems, but our definition is left matching theirs until such a time as they fix their bogus types. This is intended to be an NFC change on all architectures supported by LLVM upstream, only being a functional change for CHERI downstream in CHERI LLVM.
1 parent e3dd47f commit b6a9396

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

libunwind/src/AddressSpace.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,23 @@ struct UnwindInfoSections {
121121
uintptr_t dso_base;
122122
#endif
123123
#if defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
124-
uintptr_t text_segment_length;
124+
size_t text_segment_length;
125125
#endif
126126
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
127127
uintptr_t dwarf_section;
128-
uintptr_t dwarf_section_length;
128+
size_t dwarf_section_length;
129129
#endif
130130
#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
131131
uintptr_t dwarf_index_section;
132-
uintptr_t dwarf_index_section_length;
132+
size_t dwarf_index_section_length;
133133
#endif
134134
#if defined(_LIBUNWIND_SUPPORT_COMPACT_UNWIND)
135135
uintptr_t compact_unwind_section;
136-
uintptr_t compact_unwind_section_length;
136+
size_t compact_unwind_section_length;
137137
#endif
138138
#if defined(_LIBUNWIND_ARM_EHABI)
139139
uintptr_t arm_section;
140-
uintptr_t arm_section_length;
140+
size_t arm_section_length;
141141
#endif
142142
};
143143

@@ -430,7 +430,7 @@ static bool checkForUnwindInfoSegment(const Elf_Phdr *phdr, size_t image_base,
430430
// .eh_frame_hdr records the start of .eh_frame, but not its size.
431431
// Rely on a zero terminator to find the end of the section.
432432
cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
433-
cbdata->sects->dwarf_section_length = UINTPTR_MAX;
433+
cbdata->sects->dwarf_section_length = SIZE_MAX;
434434
return true;
435435
}
436436
}
@@ -506,22 +506,22 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
506506
info.dso_base = (uintptr_t)dyldInfo.mh;
507507
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
508508
info.dwarf_section = (uintptr_t)dyldInfo.dwarf_section;
509-
info.dwarf_section_length = dyldInfo.dwarf_section_length;
509+
info.dwarf_section_length = (size_t)dyldInfo.dwarf_section_length;
510510
#endif
511511
info.compact_unwind_section = (uintptr_t)dyldInfo.compact_unwind_section;
512-
info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
512+
info.compact_unwind_section_length = (size_t)dyldInfo.compact_unwind_section_length;
513513
return true;
514514
}
515515
#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
516516
info.dso_base = 0;
517517
// Bare metal is statically linked, so no need to ask the dynamic loader
518-
info.dwarf_section_length = (uintptr_t)(&__eh_frame_end - &__eh_frame_start);
518+
info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start);
519519
info.dwarf_section = (uintptr_t)(&__eh_frame_start);
520520
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
521521
(void *)info.dwarf_section, (void *)info.dwarf_section_length);
522522
#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
523523
info.dwarf_index_section = (uintptr_t)(&__eh_frame_hdr_start);
524-
info.dwarf_index_section_length = (uintptr_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start);
524+
info.dwarf_index_section_length = (size_t)(&__eh_frame_hdr_end - &__eh_frame_hdr_start);
525525
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
526526
(void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
527527
#endif
@@ -530,7 +530,7 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
530530
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
531531
// Bare metal is statically linked, so no need to ask the dynamic loader
532532
info.arm_section = (uintptr_t)(&__exidx_start);
533-
info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
533+
info.arm_section_length = (size_t)(&__exidx_end - &__exidx_start);
534534
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
535535
(void *)info.arm_section, (void *)info.arm_section_length);
536536
if (info.arm_section && info.arm_section_length)
@@ -584,7 +584,7 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
584584
int length = 0;
585585
info.arm_section =
586586
(uintptr_t)dl_unwind_find_exidx((_Unwind_Ptr)targetAddr, &length);
587-
info.arm_section_length = (uintptr_t)length * sizeof(EHABIIndexEntry);
587+
info.arm_section_length = (size_t)length * sizeof(EHABIIndexEntry);
588588
if (info.arm_section && info.arm_section_length)
589589
return true;
590590
#elif defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)

libunwind/src/DwarfParser.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class CFI_Parser {
151151
};
152152

153153
static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
154-
uintptr_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
154+
size_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
155155
CIE_Info *cieInfo);
156156
static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
157157
FDE_Info *fdeInfo, CIE_Info *cieInfo,
@@ -230,11 +230,11 @@ const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
230230
/// Scan an eh_frame section to find an FDE for a pc
231231
template <typename A>
232232
bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
233-
uintptr_t sectionLength, pint_t fdeHint,
233+
size_t sectionLength, pint_t fdeHint,
234234
FDE_Info *fdeInfo, CIE_Info *cieInfo) {
235235
//fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
236236
pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
237-
const pint_t ehSectionEnd = (sectionLength == UINTPTR_MAX)
237+
const pint_t ehSectionEnd = (sectionLength == SIZE_MAX)
238238
? static_cast<pint_t>(-1)
239239
: (ehSectionStart + sectionLength);
240240
while (p < ehSectionEnd) {

0 commit comments

Comments
 (0)