Skip to content

Commit 42ef16c

Browse files
fhahnBaiXilin
authored andcommitted
[TySan] Intercept malloc_size on Apple platforms. (llvm#122133)
After llvm#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: llvm#122133
1 parent 5653d3a commit 42ef16c

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_allocator_dlsym.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,19 @@ struct DlSymAllocator {
3636
static void *Allocate(uptr size_in_bytes, uptr align = kWordSize) {
3737
void *ptr = InternalAlloc(size_in_bytes, nullptr, align);
3838
CHECK(internal_allocator()->FromPrimary(ptr));
39-
Details::OnAllocate(ptr,
40-
internal_allocator()->GetActuallyAllocatedSize(ptr));
39+
Details::OnAllocate(ptr, GetSize(ptr));
4140
return ptr;
4241
}
4342

4443
static void *Callocate(usize nmemb, usize size) {
4544
void *ptr = InternalCalloc(nmemb, size);
4645
CHECK(internal_allocator()->FromPrimary(ptr));
47-
Details::OnAllocate(ptr,
48-
internal_allocator()->GetActuallyAllocatedSize(ptr));
46+
Details::OnAllocate(ptr, GetSize(ptr));
4947
return ptr;
5048
}
5149

5250
static void Free(void *ptr) {
53-
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
51+
uptr size = GetSize(ptr);
5452
Details::OnFree(ptr, size);
5553
InternalFree(ptr);
5654
}
@@ -63,7 +61,7 @@ struct DlSymAllocator {
6361
Free(ptr);
6462
return nullptr;
6563
}
66-
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
64+
uptr size = GetSize(ptr);
6765
uptr memcpy_size = Min(new_size, size);
6866
void *new_ptr = Allocate(new_size);
6967
if (new_ptr)
@@ -77,6 +75,10 @@ struct DlSymAllocator {
7775
return Realloc(ptr, count * size);
7876
}
7977

78+
static uptr GetSize(void *ptr) {
79+
return internal_allocator()->GetActuallyAllocatedSize(ptr);
80+
}
81+
8082
static void OnAllocate(const void *ptr, uptr size) {}
8183
static void OnFree(const void *ptr, uptr size) {}
8284
};

compiler-rt/lib/tysan/tysan_interceptors.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ INTERCEPTOR(void *, malloc, uptr size) {
108108
return res;
109109
}
110110

111+
#if SANITIZER_APPLE
112+
INTERCEPTOR(uptr, malloc_size, void *ptr) {
113+
if (DlsymAlloc::PointerIsMine(ptr))
114+
return DlsymAlloc::GetSize(ptr);
115+
return REAL(malloc_size)(ptr);
116+
}
117+
#endif
118+
111119
INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
112120
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
113121
return DlsymAlloc::Realloc(ptr, size);

0 commit comments

Comments
 (0)