[osx] don't crash debugallocator on 0-sized allocations

OSX's malloc zones facility is calling 'usable size' before each (!)
free and uses size to choose between our allocator and something
else. And returning 0 breaks this choice. Which happens if we got
0-sized malloc request, which our tests exercise couple times. So we
don't let debug allocator on OSX to have 0 sized chunks. This unbreaks
debug allocation tests on OSX.
This commit is contained in:
Aliaksey Kandratsenka 2023-06-25 23:37:10 -04:00
parent b0c2ab298a
commit 11d4253ead
2 changed files with 12 additions and 0 deletions

View File

@ -1034,6 +1034,15 @@ void __malloctrace_write(const char *buf, size_t size) {
// General debug allocation/deallocation
static inline void* DebugAllocate(size_t size, int type) {
#if defined(__APPLE__)
// OSX malloc zones integration has some odd behavior. When
// GetAllocatedSize returns 0 it appears to assume something wrong
// about the pointer. And since in debug allocator we can return 0
// if original size was also 0, lets avoid this case. But only on
// OSX. It weakens debug checks a bit, but it unbreaks some tests
// (around realloc/free of 0-sized chunks).
if (size == 0) size = 1;
#endif
MallocBlock* ptr = MallocBlock::Allocate(size, type);
if (ptr == NULL) return NULL;
MALLOC_TRACE("malloc", size, ptr->data_addr());

View File

@ -264,6 +264,9 @@ TEST(DebugAllocationTest, GetAllocatedSizeTest) {
// exactly requested size, since debug_allocation doesn't allow users
// to write more than that.
for (int i = 0; i < 10; ++i) {
#ifdef __APPLE__
if (i == 0) continue;
#endif
void *p = noopt(malloc(i));
EXPECT_EQ(i, MallocExtension::instance()->GetAllocatedSize(p));
free(p);