Loading...
--- libmalloc/libmalloc-409.40.6/src/magazine_malloc.c
+++ libmalloc/libmalloc-715.120.13/src/magazine_malloc.c
@@ -48,7 +48,7 @@
int max_magazines;
// Control whether medium is enabled at all when creating new magazine zones
-bool magazine_medium_enabled = true;
+bool magazine_medium_enabled = DEFAULT_MEDIUM_ALLOCATOR_ENABLED;
// Control the DRAM limit at which medium kicks in.
uint64_t magazine_medium_active_threshold = MEDIUM_ACTIVATION_THRESHOLD;
@@ -316,6 +316,12 @@
return szone_malloc_should_clear(szone, size, 0);
}
+static void *
+szone_malloc_type_malloc(szone_t *szone, size_t size, malloc_type_id_t type_id)
+{
+ return szone_malloc(szone, size);
+}
+
void *
szone_calloc(szone_t *szone, size_t num_items, size_t size)
{
@@ -324,6 +330,13 @@
return NULL;
}
return szone_malloc_should_clear(szone, total_bytes, 1);
+}
+
+static void *
+szone_malloc_type_calloc(szone_t *szone, size_t num_items, size_t size,
+ malloc_type_id_t type_id)
+{
+ return szone_calloc(szone, num_items, size);
}
void *
@@ -592,6 +605,13 @@
return new_ptr;
}
+static void *
+szone_malloc_type_realloc(szone_t *szone, void *ptr, size_t size,
+ malloc_type_id_t type_id)
+{
+ return szone_realloc(szone, ptr, size);
+}
+
void *
szone_memalign(szone_t *szone, size_t alignment, size_t size)
{
@@ -656,6 +676,13 @@
}
/* NOTREACHED */
__builtin_unreachable();
+}
+
+static void *
+szone_malloc_type_memalign(szone_t *szone, size_t align, size_t size,
+ malloc_type_id_t type_id)
+{
+ return szone_memalign(szone, align, size);
}
// Given a size, returns the number of pointers allocated capable of holding
@@ -904,7 +931,7 @@
return szone_check_all(szone, "");
}
-// To support the quarantine zone, we need to be able to perform zone enumeration across different
+// To support the sanitizer zone, we need to be able to perform zone enumeration across different
// architecture slices on macOS, because ReportCrash is always running as a native (arm64e) process,
// but we also need to be able to inspect x86_64 targets that are running under Rosetta. So the data
// layout and zone logic needs to match between x86_64 and arm64(e).
@@ -919,9 +946,7 @@
szone_t *szone;
kern_return_t err;
- if (!reader) {
- reader = _malloc_default_reader;
- }
+ reader = reader_or_in_memory_fallback(reader, task);
err = reader(task, zone_address, sizeof(szone_t), (void **)&szone);
if (err) {
@@ -1048,6 +1073,15 @@
zone_address, info[0], info[1], info[2], info[3], info[12]);
printer("\ttiny=%u(%u) small=%u(%u) large=%u(%u)\n", info[4],
info[5], info[6], info[7], info[8], info[9]);
+
+ // FIXME: The rest of the code here assumes that regions have their normal
+ // alignment, which isn't guaranteed when looking at regions mapped from
+ // other processes
+ if (!mach_task_is_self(task)) {
+ printer("(unable to safely further examine remote process)\n");
+ return;
+ }
+
// tiny
printer("%lu tiny regions:\n", mapped_szone->tiny_rack.num_regions);
if (mapped_szone->tiny_rack.num_regions_dealloc) {
@@ -1286,15 +1320,9 @@
static MALLOC_INLINE void
szone_force_lock_magazine(szone_t *szone, magazine_t *mag)
{
- while (1) {
- SZONE_MAGAZINE_PTR_LOCK(mag);
- if (!mag->alloc_underway) {
- return;
- }
-
- SZONE_MAGAZINE_PTR_UNLOCK(mag);
- yield();
- }
+ // Acquire the alloc lock first to avoid deadlocking with allocating threads
+ _malloc_lock_lock(&mag->magazine_alloc_lock);
+ SZONE_MAGAZINE_PTR_LOCK(mag);
}
static void
@@ -1335,16 +1363,19 @@
if (szone->is_medium_engaged) {
for (i = -1; i < szone->medium_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->medium_rack.magazines[i])));
+ _malloc_lock_unlock(&szone->medium_rack.magazines[i].magazine_alloc_lock);
}
}
#endif // CONFIG_MEDIUM_ALLOCATOR
for (i = -1; i < szone->small_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->small_rack.magazines[i])));
+ _malloc_lock_unlock(&szone->small_rack.magazines[i].magazine_alloc_lock);
}
for (i = -1; i < szone->tiny_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->tiny_rack.magazines[i])));
+ _malloc_lock_unlock(&szone->tiny_rack.magazines[i].magazine_alloc_lock);
}
}
@@ -1359,16 +1390,19 @@
if (szone->is_medium_engaged) {
for (i = -1; i < szone->medium_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_REINIT_LOCK((&(szone->medium_rack.magazines[i])));
+ _malloc_lock_init(&szone->medium_rack.magazines[i].magazine_alloc_lock);
}
}
#endif // CONFIG_MEDIUM_ALLOCATOR
for (i = -1; i < szone->small_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_REINIT_LOCK((&(szone->small_rack.magazines[i])));
+ _malloc_lock_init(&szone->small_rack.magazines[i].magazine_alloc_lock);
}
for (i = -1; i < szone->tiny_rack.num_magazines; ++i) {
SZONE_MAGAZINE_PTR_REINIT_LOCK((&(szone->tiny_rack.magazines[i])));
+ _malloc_lock_init(&szone->tiny_rack.magazines[i].magazine_alloc_lock);
}
}
@@ -1392,6 +1426,11 @@
return 1;
}
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->small_rack.magazines[i])));
+ tookLock = _malloc_lock_trylock(&szone->medium_rack.magazines[i].magazine_alloc_lock);
+ if (tookLock == 0) {
+ return 1;
+ }
+ _malloc_lock_unlock(&szone->medium_rack.magazines[i].magazine_alloc_lock);
}
}
#endif // CONFIG_MEDIUM_ALLOCATOR
@@ -1402,6 +1441,11 @@
return 1;
}
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->small_rack.magazines[i])));
+ tookLock = _malloc_lock_trylock(&szone->small_rack.magazines[i].magazine_alloc_lock);
+ if (tookLock == 0) {
+ return 1;
+ }
+ _malloc_lock_unlock(&szone->small_rack.magazines[i].magazine_alloc_lock);
}
for (i = -1; i < szone->tiny_rack.num_magazines; ++i) {
@@ -1410,6 +1454,11 @@
return 1;
}
SZONE_MAGAZINE_PTR_UNLOCK((&(szone->tiny_rack.magazines[i])));
+ tookLock = _malloc_lock_trylock(&szone->tiny_rack.magazines[i].magazine_alloc_lock);
+ if (tookLock == 0) {
+ return 1;
+ }
+ _malloc_lock_unlock(&szone->tiny_rack.magazines[i].magazine_alloc_lock);
}
return 0;
}
@@ -1433,7 +1482,7 @@
#endif // CONFIG_MEDIUM_ALLOCATOR
#endif // CONFIG_MADVISE_PRESSURE_RELIEF
-#if CONFIG_LARGE_CACHE && !CONFIG_DEFERRED_RECLAIM
+#if CONFIG_LARGE_CACHE && !CONFIG_MAGAZINE_DEFERRED_RECLAIM
if (large_cache_enabled && szone->flotsam_enabled) {
SZONE_LOCK(szone);
@@ -1467,7 +1516,7 @@
total += local_entry_cache[idx].size;
}
}
-#endif // CONFIG_LARGE_CACHE && !CONFIG_DEFERRED_RECLAIM
+#endif // CONFIG_LARGE_CACHE && !CONFIG_MAGAZINE_DEFERRED_RECLAIM
MAGMALLOC_PRESSURERELIEFEND((void *)szone, szone->basic_zone.zone_name, (int)goal, (int)total); // DTrace USDT Probe
MALLOC_TRACE(TRACE_malloc_memory_pressure | DBG_FUNC_END, (uint64_t)szone, goal, total, 0);
@@ -1562,7 +1611,7 @@
szone_statistics_task(task_t task, vm_address_t zone_address,
memory_reader_t reader, malloc_statistics_t *stats)
{
- reader = !reader && task == mach_task_self() ? _malloc_default_reader : reader;
+ reader = reader_or_in_memory_fallback(reader, task);
szone_t *szone;
kern_return_t err;
@@ -1729,7 +1778,7 @@
// Initialize the security token.
szone->cookie = (uintptr_t)malloc_entropy[0];
- szone->basic_zone.version = 13;
+ szone->basic_zone.version = 16;
szone->basic_zone.size = (void *)szone_size;
szone->basic_zone.malloc = (void *)szone_malloc;
szone->basic_zone.calloc = (void *)szone_calloc;
@@ -1746,6 +1795,11 @@
szone->basic_zone.claimed_address = (void *)szone_claimed_address;
szone->basic_zone.try_free_default = (void *)szone_try_free_default;
+ szone->basic_zone.malloc_type_malloc = (void *)szone_malloc_type_malloc;
+ szone->basic_zone.malloc_type_calloc = (void *)szone_malloc_type_calloc;
+ szone->basic_zone.malloc_type_realloc = (void *)szone_malloc_type_realloc;
+ szone->basic_zone.malloc_type_memalign = (void *)szone_malloc_type_memalign;
+
/* Set to zero once and for all as required by CFAllocator. */
szone->basic_zone.reserved1 = 0;
/* Set to zero once and for all as required by CFAllocator. */