[lsan][Darwin] Unconditionally strip high bits from potential pointers

The method cache stashes a mask in the high bits under some circumstances:
689525d556/runtime/objc-cache.mm (L589)

I'm hitting this now on macOS 13.4 arm64, so we can no longer rely on OBJC_FAST_IS_RW to identify potential pointers that need to be transformed

Differential Revision: https://reviews.llvm.org/D153471
This commit is contained in:
Leonard Grey 2023-06-21 17:23:04 -04:00
parent 0f6cf55567
commit ac604cc310

View File

@ -34,8 +34,6 @@
# else # else
# define OBJC_DATA_MASK 0x00007ffffffffff8UL # define OBJC_DATA_MASK 0x00007ffffffffff8UL
# endif # endif
// https://github.com/apple-oss-distributions/objc4/blob/8701d5672d3fd3cd817aeb84db1077aafe1a1604/runtime/objc-runtime-new.h#L139
# define OBJC_FAST_IS_RW 0x8000000000000000UL
# endif # endif
namespace __lsan { namespace __lsan {
@ -173,13 +171,11 @@ static uptr GetCallerPC(const StackTrace &stack) {
} }
# if SANITIZER_APPLE # if SANITIZER_APPLE
// Objective-C class data pointers are stored with flags in the low bits, so // Several pointers in the Objective-C runtime (method cache and class_rw_t,
// they need to be transformed back into something that looks like a pointer. // for example) are tagged with additional bits we need to strip.
static inline void *MaybeTransformPointer(void *p) { static inline void *TransformPointer(void *p) {
uptr ptr = reinterpret_cast<uptr>(p); uptr ptr = reinterpret_cast<uptr>(p);
if ((ptr & OBJC_FAST_IS_RW) == OBJC_FAST_IS_RW) return reinterpret_cast<void *>(ptr & OBJC_DATA_MASK);
ptr &= OBJC_DATA_MASK;
return reinterpret_cast<void *>(ptr);
} }
# endif # endif
@ -301,7 +297,7 @@ void ScanRangeForPointers(uptr begin, uptr end, Frontier *frontier,
for (; pp + sizeof(void *) <= end; pp += alignment) { for (; pp + sizeof(void *) <= end; pp += alignment) {
void *p = *reinterpret_cast<void **>(pp); void *p = *reinterpret_cast<void **>(pp);
# if SANITIZER_APPLE # if SANITIZER_APPLE
p = MaybeTransformPointer(p); p = TransformPointer(p);
# endif # endif
if (!MaybeUserPointer(reinterpret_cast<uptr>(p))) if (!MaybeUserPointer(reinterpret_cast<uptr>(p)))
continue; continue;