[TySan] Intercept malloc_size on Apple platforms. (#122133)

After https://github.com/llvm/llvm-project/pull/120563 malloc_size also
needs intercepting on Apple platforms, otherwise all type-sanitized
binaries crash on startup with an objc error:
realized class 0x12345 has corrupt data pointer: malloc_size(0x567) = 0

PR: https://github.com/llvm/llvm-project/pull/122133
This commit is contained in:
Florian Hahn 2025-01-09 20:55:36 +00:00 committed by GitHub
parent 84087226fa
commit e8c8543a1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -36,21 +36,19 @@ struct DlSymAllocator {
static void *Allocate(uptr size_in_bytes, uptr align = kWordSize) {
void *ptr = InternalAlloc(size_in_bytes, nullptr, align);
CHECK(internal_allocator()->FromPrimary(ptr));
Details::OnAllocate(ptr,
internal_allocator()->GetActuallyAllocatedSize(ptr));
Details::OnAllocate(ptr, GetSize(ptr));
return ptr;
}
static void *Callocate(usize nmemb, usize size) {
void *ptr = InternalCalloc(nmemb, size);
CHECK(internal_allocator()->FromPrimary(ptr));
Details::OnAllocate(ptr,
internal_allocator()->GetActuallyAllocatedSize(ptr));
Details::OnAllocate(ptr, GetSize(ptr));
return ptr;
}
static void Free(void *ptr) {
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
uptr size = GetSize(ptr);
Details::OnFree(ptr, size);
InternalFree(ptr);
}
@ -63,7 +61,7 @@ struct DlSymAllocator {
Free(ptr);
return nullptr;
}
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
uptr size = GetSize(ptr);
uptr memcpy_size = Min(new_size, size);
void *new_ptr = Allocate(new_size);
if (new_ptr)
@ -77,6 +75,10 @@ struct DlSymAllocator {
return Realloc(ptr, count * size);
}
static uptr GetSize(void *ptr) {
return internal_allocator()->GetActuallyAllocatedSize(ptr);
}
static void OnAllocate(const void *ptr, uptr size) {}
static void OnFree(const void *ptr, uptr size) {}
};

View File

@ -108,6 +108,14 @@ INTERCEPTOR(void *, malloc, uptr size) {
return res;
}
#if SANITIZER_APPLE
INTERCEPTOR(uptr, malloc_size, void *ptr) {
if (DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::GetSize(ptr);
return REAL(malloc_size)(ptr);
}
#endif
INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::Realloc(ptr, size);