Loading...
--- libmalloc/libmalloc-425.100.7/src/nanov2_malloc.c
+++ libmalloc/libmalloc-521.120.7/src/nanov2_malloc.c
@@ -1517,9 +1517,7 @@
 	kern_return_t kr;
 	bitarray_t slots;
 
-	if (!reader) {
-		reader = nano_common_default_reader;
-	}
+	reader = reader_or_in_memory_fallback(reader, task);
 
 	kr = reader(task, zone_address, sizeof(nanozonev2_t), (void **)&nanozone);
 	if (kr) {
@@ -1624,7 +1622,7 @@
 							bitarray_zap(slots, log_size, next_slot);
 							void *ptr = nanov2_slot_in_block_ptr(blockp, size_class, next_slot);
 							nanov2_free_slot_t *slotp = NANOV2_ZONE_PTR_TO_MAPPED_PTR(nanov2_free_slot_t *, ptr, ptr_offset);
-							next_slot = slotp->next_slot;
+							next_slot = (uint16_t)slotp->next_slot;
 							free_list_count++;
 						}
 						// Add a range for each slot that is not on the freelist,
@@ -1643,7 +1641,7 @@
 							ranges[range_count].size = slot_size;
 							range_count++;
 						}
-						free(slots);
+						_free(slots);
 					}
 					if (range_count) {
 						// Notify the in-use pointers that we found.
@@ -1960,7 +1958,7 @@
 		malloc_statistics_t *stats)
 {
 	printer = printer ? printer : nanov2_null_printer;
-	reader = !reader && task == mach_task_self() ? _malloc_default_reader : reader;
+	reader = reader_or_in_memory_fallback(reader, task);
 
 	kern_return_t err;
 
@@ -2292,6 +2290,7 @@
 
 #if OS_VARIANT_NOTRESOLVED
 
+#if CONFIG_NANO_RESERVE_REGIONS
 // Update protection for region to DEFAULT
 static bool
 nanov2_unprotect_region(nanov2_region_t *region)
@@ -2319,7 +2318,7 @@
 
 	return result;
 }
-
+#else
 // Attempts to allocate VM space for a region at a given address and returns
 // whether the allocation succeeded.
 static bool
@@ -2333,6 +2332,7 @@
 			(uint64_t)region, result, 0, 0);
 	return result;
 }
+#endif // CONFIG_NANO_RESERVE_REGIONS
 
 // Allocates a new region adjacent to the current one. If the allocation fails,
 // keep sliding up by the size of a region until we either succeed or run out of
@@ -2424,7 +2424,8 @@
 	uint64_t guard = *(uint64_t *)corrupt_slot;
 	malloc_zone_error(MALLOC_ABORT_ON_CORRUPTION, true,
 			"Heap corruption detected, free list is damaged at %p\n"
-			"*** Incorrect guard value: %lu\n", corrupt_slot, guard);
+			"*** Incorrect guard value: %llu\n", corrupt_slot,
+			(unsigned long long)guard);
 	__builtin_unreachable();
 }
 
@@ -2489,7 +2490,7 @@
 		slot = old_meta_view.meta.next_slot - 1; // meta.next_slot is 1-based.
 		ptr = nanov2_slot_in_block_ptr(blockp, size_class, slot);
 		nanov2_free_slot_t *slotp = (nanov2_free_slot_t *)ptr;
-		new_meta.next_slot = slot_full ? SLOT_FULL : slotp->next_slot;
+		new_meta.next_slot = slot_full ? SLOT_FULL : (uint16_t)slotp->next_slot;
 	}
 
 	// Write the updated meta data; try again if we raced with another thread.
@@ -3096,11 +3097,9 @@
 malloc_zone_t *
 nanov2_create_zone(malloc_zone_t *helper_zone, unsigned debug_flags)
 {
-	// Note: It is important that nanov2_create_zone resets _malloc_engaged_nano
-	// if it is unable to enable the nanozone (and chooses not to abort). As
-	// several functions rely on _malloc_engaged_nano to determine if they
-	// should manipulate the nanozone, and these should not run if we failed
-	// to create the zone.
+	// Note: It is not necessary that nanov2_create_zone resets _malloc_engaged_nano
+	// if it is unable to enable the nanozone - functions that need to determine
+	// whether the nanozone is preset should test initial_nano_zone.
 	MALLOC_ASSERT(_malloc_engaged_nano == NANO_V2);
 
 	// Get memory for the zone and disable Nano if we fail.