Loading...
--- libmalloc/libmalloc-374.60.3/src/malloc.c
+++ libmalloc/libmalloc-425.100.7/src/malloc.c
@@ -23,12 +23,12 @@
 
 #include "internal.h"
 
-#if TARGET_OS_IPHONE
+#if MALLOC_TARGET_IOS
 // malloc_report(ASL_LEVEL_INFO...) on iOS doesn't show up in the Xcode Console log of the device,
 // but ASL_LEVEL_NOTICE does.  So raising the log level is helpful.
 #undef ASL_LEVEL_INFO
 #define ASL_LEVEL_INFO ASL_LEVEL_NOTICE
-#endif // TARGET_OS_IPHONE
+#endif // MALLOC_TARGET_IOS
 
 #define USE_SLEEP_RATHER_THAN_ABORT 0
 
@@ -68,6 +68,10 @@
 
 static int malloc_check_sleep = 100; // default 100 second sleep
 static int malloc_check_abort = 0;   // default is to sleep, not abort
+static bool malloc_instrumented = false;
+static bool malloc_simple_stack_logging = false;
+#define MALLOC_SIMPLE_STACK_LOGGING_FLAGS \
+		(ASL_LEVEL_NOTICE | MALLOC_REPORT_NOPREFIX | MALLOC_REPORT_BACKTRACE | MALLOC_REPORT_NOWRITE)
 
 static struct _malloc_msl_symbols msl = {};
 
@@ -131,10 +135,24 @@
 MALLOC_NOEXPORT
 unsigned int hyper_shift;
 
+MALLOC_NOEXPORT
+size_t malloc_absolute_max_size;
+
 // Boot argument for max magazine control
 static const char max_magazines_boot_arg[] = "malloc_max_magazines";
 
 static const char large_expanded_cache_threshold_boot_arg[] = "malloc_large_expanded_cache_threshold";
+
+
+MALLOC_NOEXPORT
+unsigned malloc_zero_on_free_sample_period = 0; // 0 means don't sample
+
+static const char zero_on_free_sample_period_boot_arg[] = "malloc_zero_on_free_sample_period";
+
+MALLOC_NOEXPORT
+malloc_zero_policy_t malloc_zero_policy = MALLOC_ZERO_POLICY_DEFAULT;
+
+static const char zero_on_free_enabled_boot_arg[] = "malloc_zero_on_free_enabled";
 
 #if CONFIG_MEDIUM_ALLOCATOR
 static const char medium_enabled_boot_arg[] = "malloc_medium_zone";
@@ -153,8 +171,8 @@
 typedef void * (*dlopen_t) (const char * __path, int __mode);
 typedef void * (*dlsym_t) (void * __handle, const char * __symbol);
 
-static dlopen_t _dlopen = NULL;
-static dlsym_t _dlsym = NULL;
+static dlopen_t LIBMALLOC_FUNCTION_PTRAUTH(_dlopen) = NULL;
+static dlsym_t LIBMALLOC_FUNCTION_PTRAUTH(_dlsym) = NULL;
 #else
 #define _dlopen(...) NULL
 #define _dlsym(...) NULL
@@ -201,9 +219,9 @@
 }
 #endif /* TARGET_OS_OSX */
 
-
 #define LIBMALLOC_EXPERIMENT_FACTORS_KEY "MallocExperiment="
 #define LIBMALLOC_EXPERIMENT_DISABLE_MEDIUM (1ULL)
+#define LIBMALLOC_DEFERRED_RECLAIM_ENABLE "MallocDeferredReclaim=1"
 static void
 __malloc_init_experiments(const char *str)
 {
@@ -254,6 +272,32 @@
 		}
 	}
 
+	flag = malloc_common_value_for_key_copy(bootargs,
+			zero_on_free_enabled_boot_arg, value_buf, sizeof(value_buf));
+	if (flag) {
+		const char *endp;
+		long value = malloc_common_convert_to_long(flag, &endp);
+		if (!*endp && (value == 0 || value == 1)) {
+			malloc_zero_policy = value ? MALLOC_ZERO_ON_FREE : MALLOC_ZERO_NONE;
+		} else {
+			malloc_report(ASL_LEVEL_ERR,
+					"malloc_zero_on_free_enabled must be 0 or 1 - ignored.\n");
+		}
+	}
+
+	flag = malloc_common_value_for_key_copy(bootargs,
+			zero_on_free_sample_period_boot_arg, value_buf, sizeof(value_buf));
+	if (flag) {
+		const char *endp;
+		long value = malloc_common_convert_to_long(flag, &endp);
+		if (!*endp && value >= 0) {
+			malloc_zero_on_free_sample_period = (unsigned int)value;
+		} else {
+			malloc_report(ASL_LEVEL_ERR,
+					"malloc_zero_on_free_sample_period must be positive - ignored.\n");
+		}
+	}
+
 #if CONFIG_MEDIUM_ALLOCATOR
 #if TARGET_OS_OSX
 #if defined(__x86_64__)
@@ -351,6 +395,20 @@
 		bootargs[len + 1] = '\0';
 	}
 
+#if CONFIG_FEATUREFLAGS_SIMPLE
+	bool zero_on_free_feature_enabled = os_feature_enabled_simple(libmalloc,
+			ZeroOnFree, MALLOC_ZERO_POLICY_DEFAULT == MALLOC_ZERO_ON_FREE);
+	bool policy_is_zero_on_free = (malloc_zero_policy == MALLOC_ZERO_ON_FREE);
+	if (zero_on_free_feature_enabled != policy_is_zero_on_free) {
+		malloc_zero_policy = zero_on_free_feature_enabled ?
+				MALLOC_ZERO_ON_FREE : MALLOC_ZERO_NONE;
+	}
+#endif
+
+	// Cache the calculation of this "constant", which unfortunately depends on
+	// runtime values of vm_kernel_page_size and vm_page_size
+	malloc_absolute_max_size = _MALLOC_ABSOLUTE_MAX_SIZE;
+
 	const char **p;
 	const char *malloc_experiments = NULL;
 	for (p = apple; p && *p; p++) {
@@ -365,6 +423,13 @@
 		if (strstr(*p, LIBMALLOC_EXPERIMENT_FACTORS_KEY) == *p) {
 			malloc_experiments = *p;
 		}
+#if CONFIG_DEFERRED_RECLAIM
+		if (strstr(*p, LIBMALLOC_DEFERRED_RECLAIM_ENABLE) == *p) {
+			// Turn on the large cache which will place
+			// its free entries in the deferred reclaim buffer
+			large_cache_enabled = 1;
+		}
+#endif /* CONFIG_DEFERRED_RECLAIM */
 	}
 	if (!_malloc_entropy_initialized) {
 		getentropy((void*)malloc_entropy, sizeof(malloc_entropy));
@@ -663,9 +728,9 @@
 	return !has_injected_zone0;
 }
 
-static inline malloc_zone_t *find_registered_zone(const void *, size_t *) __attribute__((always_inline));
+static inline malloc_zone_t *find_registered_zone(const void *, size_t *, bool) __attribute__((always_inline));
 static inline malloc_zone_t *
-find_registered_zone(const void *ptr, size_t *returned_size)
+find_registered_zone(const void *ptr, size_t *returned_size, bool known_non_default)
 {
 	// Returns a zone which contains ptr, else NULL
 
@@ -695,7 +760,7 @@
 	// We assume that the initial zones will never be unregistered concurrently while this code is running so we can have
 	// a fast path without synchronization.  Callers who really do unregister these (to install their own default zone) need
 	// to ensure they establish their zone setup during initialization and before entering a multi-threaded environment.
-	for (uint32_t i = 0; i < initial_num_zones; i++) {
+	for (uint32_t i = known_non_default ? 1 : 0; i < initial_num_zones; i++) {
 		zone = malloc_zones[i];
 		size = zone->size(zone, ptr);
 
@@ -929,6 +994,17 @@
 
 	set_flags_from_environment(); // will only set flags up to two times
 
+	if (malloc_tracing_enabled || malloc_simple_stack_logging) {
+		// Note: although malloc_tracing_enabled is exported "for performance
+		// tools", it does not appear in any API or SPI headers and is not
+		// referenced by any indexed performance tools, so we're going to go
+		// ahead and stop respecting dynamic programmatic enablement via direct
+		// setting of the global by other code
+		//
+		// TODO: it may be possible to fold malloc_check_start into this as well
+		malloc_instrumented = true;
+	}
+
 #if CONFIG_QUARANTINE
 	malloc_quarantine_enabled = quarantine_should_enable();
 #endif
@@ -937,9 +1013,15 @@
 	// TODO: envp should be passed down from Libsystem
 	const char **envp = (const char **)*_NSGetEnviron();
 	
-	// Force magazine_malloc then quarantine is enabled, to avoid speculative
-	// out-of-bounds use-after-free reads that nano/nanov2 performs.
-	if (!malloc_quarantine_enabled) {
+	// Force magazine_malloc when:
+	// - quarantine is enabled, to avoid speculative out-of-bounds
+	//   use-after-free reads that nano/nanov2 performs, OR
+	// - MallocScribble is enabled, which nanov2 does not implement
+	// - MallocCheckZeroOnFreeCorruption sampling is enabled, which nanov2 does
+	//   not implement
+	if (!malloc_quarantine_enabled &&
+			!(malloc_debug_flags & MALLOC_DO_SCRIBBLE) &&
+			!malloc_zero_on_free_sample_period) {
 		nano_common_init(envp, apple, bootargs);
 	}
 #endif // CONFIG_NANOZONE
@@ -973,6 +1055,15 @@
 #endif
 
 	initial_num_zones = malloc_num_zones;
+
+#if CONFIG_DEFERRED_RECLAIM
+	if (large_cache_enabled) {
+		kern_return_t kr = mvm_deferred_reclaim_init();
+		if (kr != KERN_SUCCESS) {
+			MALLOC_REPORT_FATAL_ERROR(kr, "Unable to set up reclaim buffer.\n");
+		}
+	}
+#endif /* CONFIG_DEFERRED_RECLAIM */
 
 #if CONFIG_MEDIUM_ALLOCATOR
 	uint64_t memsize = platform_hw_memsize();
@@ -1114,6 +1205,16 @@
 		malloc_debug_flags = MALLOC_ABORT_ON_CORRUPTION;
 	}
 #endif
+
+#if TARGET_OS_OSX
+	// rdar://99288027
+	if (!dyld_program_sdk_at_least(dyld_platform_version_macOS_13_0)) {
+		if (malloc_zero_policy == MALLOC_ZERO_ON_FREE) {
+			malloc_zero_policy = MALLOC_ZERO_ON_ALLOC;
+		}
+	}
+#else // TARGET_OS_OSX
+#endif // TARGET_OS_OSX
 
 	/*
 	 * Given that all environment variables start with "Malloc" we optimize by scanning quickly
@@ -1183,6 +1284,9 @@
 	if (getenv("MallocTracing")) {
 		malloc_tracing_enabled = true;
 	}
+	if (getenv("MallocSimpleStackLogging")) {
+		malloc_simple_stack_logging = true;
+	}
 
 #if __LP64__
 /* initialization above forces MALLOC_ABORT_ON_CORRUPTION of 64-bit processes */
@@ -1293,7 +1397,10 @@
 			aggressive_madvise_enabled = (value == 1);
 #endif // CONFIG_AGGRESSIVE_MADVISE
 #if CONFIG_LARGE_CACHE
-			large_cache_enabled = (value == 0);
+			// Disable the large cache in space efficient mode
+			if (value != 0){
+				large_cache_enabled = 0;
+			}
 #endif // CONFIG_LARGE_CACHE
 			malloc_space_efficient_enabled = (value == 1);
 			// consider disabling medium magazine if aggressive madvise is not sufficient
@@ -1403,6 +1510,40 @@
 		}
 	}
 #endif // CONFIG_RECIRC_DEPOT
+
+	flag = getenv("MallocZeroOnFree");
+	if (flag) {
+		const char *endp;
+		long value = malloc_common_convert_to_long(flag, &endp);
+		if (!*endp && endp != flag && (value == 0 || value == 1)) {
+			malloc_zero_policy = value ? MALLOC_ZERO_ON_FREE : MALLOC_ZERO_NONE;
+		} else {
+			malloc_report(ASL_LEVEL_ERR, "MallocZeroOnFree must be 0 or 1.\n");
+		}
+	}
+
+	flag = getenv("MallocZeroOnAlloc");
+	if (flag) {
+		const char *endp;
+		long value = malloc_common_convert_to_long(flag, &endp);
+		if (!*endp && endp != flag && (value == 0 || value == 1)) {
+			malloc_zero_policy = value ? MALLOC_ZERO_ON_ALLOC : MALLOC_ZERO_NONE;
+		} else {
+			malloc_report(ASL_LEVEL_ERR, "MallocZeroOnAlloc must be 0 or 1.\n");
+		}
+	}
+
+	flag = getenv("MallocCheckZeroOnFreeCorruption");
+	if (flag) {
+		int value = (int)strtol(flag, NULL, 0);
+		if (value > 0) {
+			malloc_zero_on_free_sample_period = value;
+		} else {
+			malloc_report(ASL_LEVEL_ERR,
+					"malloc_zero_on_free_sample_period must be positive - ignored.\n");
+		}
+	}
+
 	if (getenv("MallocHelp")) {
 		malloc_report(ASL_LEVEL_INFO,
 				"environment variables that can be set for debug:\n"
@@ -1423,6 +1564,8 @@
 				"  MallocCorruptionAbort is always set on 64-bit processes\n"
 				"- MallocErrorAbort to abort on any malloc error, including out of memory\n"\
 				"- MallocTracing to emit kdebug trace points on malloc entry points\n"\
+				"- MallocZeroOnFree to enable or disable zero-on-free behavior (for debugging only)\n"\
+				"- MallocCheckZeroOnFreeCorruption to enable zero-on-free corruption detection\n"\
 				"- MallocHelp - this help!\n");
 	}
 }
@@ -1433,17 +1576,19 @@
 	malloc_zone_t *zone;
 
 	/* start_size doesn't actually appear to be used, but we test anyway. */
-	if (start_size > MALLOC_ABSOLUTE_MAX_SIZE) {
+	if (start_size > malloc_absolute_max_size) {
 		return NULL;
 	}
 
 	zone = create_scalable_zone(start_size, flags | malloc_debug_flags);
 	malloc_zone_register(zone);
 
+#if CONFIG_PGM_WRAP_CUSTOM_ZONES
 	if (enable_pgm()) {
 		zone = pgm_create_zone(zone);
 		malloc_zone_register(zone);
 	}
+#endif
 	return zone;
 }
 
@@ -1570,27 +1715,10 @@
 	malloc_check_start += malloc_check_each;
 }
 
-__options_decl(malloc_zone_options_t, unsigned, {
-	MZ_NONE  = 0x0,
-	MZ_POSIX = 0x1,
-	MZ_C11   = 0x2,
-});
-
-static inline void
-malloc_set_errno_fast(malloc_zone_options_t mzo, int err)
-{
-	if (mzo & MZ_POSIX) {
-#if TARGET_OS_SIMULATOR
-		errno = err;
-#else
-		(*_pthread_errno_address_direct()) = err;
-#endif
-	}
-}
-
 MALLOC_NOINLINE
 static void *
-_malloc_zone_malloc(malloc_zone_t *zone, size_t size, malloc_zone_options_t mzo)
+_malloc_zone_malloc_instrumented_or_legacy(malloc_zone_t *zone, size_t size,
+		malloc_zone_options_t mzo)
 {
 	MALLOC_TRACE(TRACE_malloc | DBG_FUNC_START, (uintptr_t)zone, size, 0, 0);
 
@@ -1599,14 +1727,19 @@
 	if (malloc_check_start) {
 		internal_check();
 	}
-	if (size > MALLOC_ABSOLUTE_MAX_SIZE) {
+	if (size > malloc_absolute_max_size) {
 		goto out;
 	}
 
-	ptr = zone->malloc(zone, size);		// if lite zone is passed in then we still call the lite methods
+	ptr = zone->malloc(zone, size);
 
 	if (os_unlikely(malloc_logger)) {
 		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
+	}
+
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "malloc (%p/%llu): ",
+				ptr, (unsigned long long)size);
 	}
 
 	MALLOC_TRACE(TRACE_malloc | DBG_FUNC_END, (uintptr_t)zone, size, (uintptr_t)ptr, 0);
@@ -1617,10 +1750,64 @@
 	return ptr;
 }
 
+static void *
+_malloc_zone_malloc(malloc_zone_t *zone, size_t size, malloc_zone_options_t mzo)
+{
+	if (zone == default_zone && !lite_zone) {
+		// Eagerly resolve the virtual default zone to make the zone version
+		// check accurate
+		zone = malloc_zones[0];
+	}
+
+	if (os_unlikely(malloc_instrumented || malloc_check_start ||
+				malloc_logger || zone->version < 13)) {
+		return _malloc_zone_malloc_instrumented_or_legacy(zone, size, mzo);
+	}
+
+	if (os_unlikely(size > malloc_absolute_max_size)) {
+		malloc_set_errno_fast(mzo, ENOMEM);
+		return NULL;
+	}
+
+	// zone versions >= 13 set errno on failure so we can tail-call
+	return zone->malloc(zone, size);
+}
+
 void *
 malloc_zone_malloc(malloc_zone_t *zone, size_t size)
 {
 	return _malloc_zone_malloc(zone, size, MZ_NONE);
+}
+
+MALLOC_NOINLINE
+static void *
+_malloc_zone_calloc_instrumented_or_legacy(malloc_zone_t *zone,
+		size_t num_items, size_t size, malloc_zone_options_t mzo)
+{
+	MALLOC_TRACE(TRACE_calloc | DBG_FUNC_START, (uintptr_t)zone, num_items, size, 0);
+
+	void *ptr;
+	if (malloc_check_start) {
+		internal_check();
+	}
+
+	ptr = zone->calloc(zone, num_items, size);
+
+	if (os_unlikely(malloc_logger)) {
+		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE | MALLOC_LOG_TYPE_CLEARED, (uintptr_t)zone,
+				(uintptr_t)(num_items * size), 0, (uintptr_t)ptr, 0);
+	}
+
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "calloc (%p/%llu*%llu): ",
+				ptr, (unsigned long long)num_items, (unsigned long long)size);
+	}
+
+	MALLOC_TRACE(TRACE_calloc | DBG_FUNC_END, (uintptr_t)zone, num_items, size, (uintptr_t)ptr);
+	if (os_unlikely(ptr == NULL)) {
+		malloc_set_errno_fast(mzo, ENOMEM);
+	}
+	return ptr;
 }
 
 MALLOC_NOINLINE
@@ -1628,25 +1815,19 @@
 _malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size,
 		malloc_zone_options_t mzo)
 {
-	MALLOC_TRACE(TRACE_calloc | DBG_FUNC_START, (uintptr_t)zone, num_items, size, 0);
-
-	void *ptr;
-	if (malloc_check_start) {
-		internal_check();
-	}
-
-	ptr = zone->calloc(zone, num_items, size);
-
-	if (os_unlikely(malloc_logger)) {
-		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE | MALLOC_LOG_TYPE_CLEARED, (uintptr_t)zone,
-				(uintptr_t)(num_items * size), 0, (uintptr_t)ptr, 0);
-	}
-
-	MALLOC_TRACE(TRACE_calloc | DBG_FUNC_END, (uintptr_t)zone, num_items, size, (uintptr_t)ptr);
-	if (os_unlikely(ptr == NULL)) {
-		malloc_set_errno_fast(mzo, ENOMEM);
-	}
-	return ptr;
+	if (zone == default_zone && !lite_zone) {
+		// Eagerly resolve the virtual default zone to make the zone version
+		// check accurate
+		zone = malloc_zones[0];
+	}
+
+	if (os_unlikely(malloc_instrumented || malloc_check_start ||
+				malloc_logger || zone->version < 13)) {
+		return _malloc_zone_calloc_instrumented_or_legacy(zone, num_items, size, mzo);
+	}
+
+	// zone versions >= 13 set errno on failure so we can tail-call
+	return zone->calloc(zone, num_items, size);
 }
 
 void *
@@ -1665,7 +1846,7 @@
 	if (malloc_check_start) {
 		internal_check();
 	}
-	if (size > MALLOC_ABSOLUTE_MAX_SIZE) {
+	if (size > malloc_absolute_max_size) {
 		goto out;
 	}
 
@@ -1673,6 +1854,11 @@
 
 	if (os_unlikely(malloc_logger)) {
 		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
+	}
+
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "valloc (%p/%llu): ",
+				ptr, (unsigned long long)size);
 	}
 
 	MALLOC_TRACE(TRACE_valloc | DBG_FUNC_END, (uintptr_t)zone, size, (uintptr_t)ptr, 0);
@@ -1698,7 +1884,7 @@
 	if (malloc_check_start) {
 		internal_check();
 	}
-	if (size > MALLOC_ABSOLUTE_MAX_SIZE) {
+	if (size > malloc_absolute_max_size) {
 		return NULL;
 	}
 
@@ -1708,6 +1894,12 @@
 		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_DEALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone,
 				(uintptr_t)ptr, (uintptr_t)size, (uintptr_t)new_ptr, 0);
 	}
+
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "realloc (%p->%p/%llu): ",
+				ptr, new_ptr, (unsigned long long)size);
+	}
+
 	MALLOC_TRACE(TRACE_realloc | DBG_FUNC_END, (uintptr_t)zone, (uintptr_t)ptr, size, (uintptr_t)new_ptr);
 	return new_ptr;
 }
@@ -1720,6 +1912,9 @@
 	if (os_unlikely(malloc_logger)) {
 		malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)ptr, 0, 0, 0);
 	}
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "malloc_zone_free (%p): ", ptr);
+	}
 	if (malloc_check_start) {
 		internal_check();
 	}
@@ -1734,6 +1929,10 @@
 
 	if (os_unlikely(malloc_logger)) {
 		malloc_logger(MALLOC_LOG_TYPE_DEALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)ptr, 0, 0, 0);
+	}
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "free (%p/%llu): ",
+				ptr, (unsigned long long)size);
 	}
 	if (malloc_check_start) {
 		internal_check();
@@ -1748,7 +1947,7 @@
 	if (!ptr) {
 		return NULL;
 	} else {
-		return find_registered_zone(ptr, NULL);
+		return find_registered_zone(ptr, NULL, false);
 	}
 }
 
@@ -1767,7 +1966,7 @@
 	if (malloc_check_start) {
 		internal_check();
 	}
-	if (size > MALLOC_ABSOLUTE_MAX_SIZE) {
+	if (size > malloc_absolute_max_size) {
 		goto out;
 	}
 	if (alignment < sizeof(void *) ||			  // excludes 0 == alignment
@@ -1787,6 +1986,10 @@
 
 	if (os_unlikely(malloc_logger)) {
 		malloc_logger(MALLOC_LOG_TYPE_ALLOCATE | MALLOC_LOG_TYPE_HAS_ZONE, (uintptr_t)zone, (uintptr_t)size, 0, (uintptr_t)ptr, 0);
+	}
+	if (os_unlikely(malloc_simple_stack_logging)) {
+		malloc_report(MALLOC_SIMPLE_STACK_LOGGING_FLAGS, "memalign (%p/%llu,%llu): ",
+				ptr, (unsigned long long)alignment, (unsigned long long)size);
 	}
 
 	MALLOC_TRACE(TRACE_memalign | DBG_FUNC_END, (uintptr_t)zone, alignment, size, (uintptr_t)ptr);
@@ -1894,7 +2097,7 @@
 {
 	mprotect(z, sizeof(malloc_zone_t), PROT_READ | PROT_WRITE);
 	if (z->zone_name) {
-		malloc_zone_t *old_zone = find_registered_zone(z->zone_name, NULL);
+		malloc_zone_t *old_zone = find_registered_zone(z->zone_name, NULL, false);
 		if (old_zone) {
 			malloc_zone_free(old_zone, (char *)z->zone_name);
 		}
@@ -1921,30 +2124,8 @@
 	return zone->zone_name;
 }
 
-
-/*********	Generic ANSI callouts	************/
-
-void *
-malloc(size_t size)
-{
-	return _malloc_zone_malloc(default_zone, size, MZ_POSIX);
-}
-
-void *
-aligned_alloc(size_t alignment, size_t size)
-{
-	return _malloc_zone_memalign(default_zone, alignment, size,
-	    MZ_POSIX | MZ_C11);
-}
-
-void *
-calloc(size_t num_items, size_t size)
-{
-	return _malloc_zone_calloc(default_zone, num_items, size, MZ_POSIX);
-}
-
 void
-free(void *ptr)
+find_zone_and_free(void *ptr, bool known_non_default)
 {
 	malloc_zone_t *zone;
 	size_t size;
@@ -1952,7 +2133,7 @@
 		return;
 	}
 
-	zone = find_registered_zone(ptr, &size);
+	zone = find_registered_zone(ptr, &size, known_non_default);
 	if (!zone) {
 		int flags = MALLOC_REPORT_DEBUG | MALLOC_REPORT_NOLOG;
 		if ((malloc_debug_flags & (MALLOC_ABORT_ON_CORRUPTION | MALLOC_ABORT_ON_ERROR))) {
@@ -1965,6 +2146,50 @@
 	} else {
 		malloc_zone_free(zone, ptr);
 	}
+}
+
+
+/*********	Generic ANSI callouts	************/
+
+void *
+malloc(size_t size)
+{
+	return _malloc_zone_malloc(default_zone, size, MZ_POSIX);
+}
+
+void *
+aligned_alloc(size_t alignment, size_t size)
+{
+	return _malloc_zone_memalign(default_zone, alignment, size,
+	    MZ_POSIX | MZ_C11);
+}
+
+void *
+calloc(size_t num_items, size_t size)
+{
+	return _malloc_zone_calloc(default_zone, num_items, size, MZ_POSIX);
+}
+
+void
+free(void *ptr)
+{
+	if (!ptr) {
+		return;
+	}
+
+	if (os_unlikely(malloc_instrumented ||
+				malloc_check_start ||
+				malloc_logger ||
+				lite_zone ||
+				malloc_num_zones == 0 ||
+				malloc_zones[0]->version < 13 ||
+				!malloc_zones[0]->try_free_default)) {
+		find_zone_and_free(ptr, false);
+		return;
+	}
+
+	malloc_zone_t *zone0 = malloc_zones[0];
+	zone0->try_free_default(zone0, ptr);
 }
 
 void *
@@ -1986,7 +2211,7 @@
 	if (!old_ptr) {
 		retval = malloc_zone_malloc(default_zone, new_size);
 	} else {
-		zone = find_registered_zone(old_ptr, NULL);
+		zone = find_registered_zone(old_ptr, NULL, false);
 		if (!zone) {
 			int flags = MALLOC_REPORT_DEBUG | MALLOC_REPORT_NOLOG;
 			if (malloc_debug_flags & (MALLOC_ABORT_ON_CORRUPTION | MALLOC_ABORT_ON_ERROR)) {
@@ -2027,7 +2252,7 @@
 		return size;
 	}
 
-	(void)find_registered_zone(ptr, &size);
+	(void)find_registered_zone(ptr, &size, false);
 	return size;
 }
 
@@ -2153,7 +2378,7 @@
 	 * and only search those.
 	 */
 	size_t size = 0;
-	malloc_zone_t *zone = find_registered_zone(ptr, &size);
+	malloc_zone_t *zone = find_registered_zone(ptr, &size, false);
 
 	/* FIXME: would really like a zone->introspect->flags->purgeable check, but haven't determined
 	 * binary compatibility impact of changing the introspect struct yet. */
@@ -2655,6 +2880,12 @@
 	}
 }
 
+void
+malloc_zero_on_free_disable(void)
+{
+	malloc_zero_policy = MALLOC_ZERO_NONE;
+}
+
 /*****************	OBSOLETE ENTRY POINTS	********************/
 
 #if PHASE_OUT_OLD_MALLOC
@@ -2688,6 +2919,38 @@
 {
 	malloc_report(ASL_LEVEL_ERR, "*** OBSOLETE: malloc_debug()\n");
 	return 0;
+}
+
+#pragma mark -
+#pragma mark Malloc Thread Options
+
+typedef union {
+	malloc_thread_options_t options;
+	void *storage;
+} th_opts_t;
+
+MALLOC_STATIC_ASSERT(sizeof(th_opts_t) == sizeof(void *), "Options fit into pointer bits");
+
+malloc_thread_options_t
+malloc_get_thread_options(void)
+{
+	th_opts_t x = {.storage = _pthread_getspecific_direct(__TSD_MALLOC_THREAD_OPTIONS)};
+	return x.options;
+}
+
+void
+malloc_set_thread_options(malloc_thread_options_t opts)
+{
+	// Canonicalize options
+	if (opts.DisableExpensiveDebuggingOptions) {
+		opts.DisableProbabilisticGuardMalloc = true;
+		opts.DisableMallocStackLogging = true;
+	}
+
+	pgm_thread_set_disabled(opts.DisableProbabilisticGuardMalloc);
+
+	th_opts_t x = {.options = opts};
+	_pthread_setspecific_direct(__TSD_MALLOC_THREAD_OPTIONS, x.storage);
 }
 
 #pragma mark -