Loading...
--- libmalloc/libmalloc-715.120.13/src/malloc.c
+++ libmalloc/libmalloc-646.0.13/src/malloc.c
@@ -161,10 +161,10 @@
 MALLOC_NOEXPORT
 unsigned int logical_ncpus;
 
-#if CONFIG_CLUSTER_AWARE
+#if CONFIG_MAGAZINE_PER_CLUSTER
 MALLOC_NOEXPORT
 unsigned int ncpuclusters;
-#endif // CONFIG_CLUSTER_AWARE
+#endif // CONFIG_MAGAZINE_PER_CLUSTER
 
 MALLOC_NOEXPORT
 unsigned int hyper_shift;
@@ -511,27 +511,10 @@
 		{ "wifip2pd",            MALLOC_PROCESS_WIFIP2PD, },
 		{ "wifianalyticsd",      MALLOC_PROCESS_WIFIANALYTICSD, },
 
-#if TARGET_OS_VISION
-		{ "presenced",           MALLOC_PROCESS_PRESENCED, },
-		{ "FaceTime",            MALLOC_PROCESS_FACETIME, },
-		{ "managedassetsd",      MALLOC_PROCESS_MANAGEDASSETSD },
-		{ "polarisd",            MALLOC_PROCESS_POLARISD },
-		{ "arkitd",              MALLOC_PROCESS_ARKITD, },
-		{ "backboardd",          MALLOC_PROCESS_BACKBOARDD, },
-		{ "wakeboardd",          MALLOC_PROCESS_WAKEBOARDD, },
-		{ "realitycamerad",      MALLOC_PROCESS_REALITYCAMERAD, },
-#endif // TARGET_OS_VISION
+		{ "mds_stores",          MALLOC_PROCESS_MDS_STORES },
 
 		{ "AegirPoster",         MALLOC_PROCESS_AEGIRPOSTER, },
 		{ "CollectionsPoster",   MALLOC_PROCESS_COLLECTIONSPOSTER, },
-
-#if TARGET_OS_OSX
-		{ "GroupSessionService", MALLOC_PROCESS_GROUPSESSIONSERVICE, },
-		{ "IMTranscoderAgent", MALLOC_PROCESS_IMTRANSCODERAGENT, },
-		{ "Messages", MALLOC_PROCESS_MESSAGES, },
-		{ "Screen Sharing", MALLOC_PROCESS_SCREENSHARING, },
-		{ "keychainsharingmessagingd", MALLOC_PROCESS_KEYCHAINSHARINGMESSAGINGD, },
-#endif // TARGET_OS_OSX
 	};
 
 	if (getpid() == 1) {
@@ -576,11 +559,10 @@
 #endif
 
 static bool
-_malloc_check_secure_allocator_process_enablement(
-		malloc_process_identity_t identity)
+_malloc_check_secure_allocator_process_enablement(void)
 {
 	// launchd is special because the feature flag check can't work for it
-	if (identity == MALLOC_PROCESS_LAUNCHD) {
+	if (malloc_process_identity == MALLOC_PROCESS_LAUNCHD) {
 		return MALLOC_SECURE_ALLOCATOR_LAUNCHD_ENABLED_DEFAULT;
 	}
 
@@ -593,7 +575,7 @@
 					(darwin_default), (simulator_default))
 
 
-	switch (identity) {
+	switch (malloc_process_identity) {
 	ENABLEMENT_CASE(LOGD, true);
 	ENABLEMENT_CASE(NOTIFYD, true);
 
@@ -618,7 +600,11 @@
 	ENABLEMENT_CASE(QUICKLOOK_PREVIEW, true);
 	ENABLEMENT_CASE(QUICKLOOK_THUMBNAIL, true);
 
+#if TARGET_OS_OSX
+	ENABLEMENT_CASE_FF(MTLCOMPILERSERVICE, MTLCompilerService, false, false);
+#else
 	ENABLEMENT_CASE(MTLCOMPILERSERVICE, true);
+#endif
 
 	ENABLEMENT_CASE(CALLSERVICESD, true);
 	ENABLEMENT_CASE(MAILD, true);
@@ -645,32 +631,14 @@
 	ENABLEMENT_CASE(SAFARI_SUPPORT, true);
 #endif
 
-#if TARGET_OS_VISION
-	ENABLEMENT_CASE(PRESENCED, true);
-	ENABLEMENT_CASE(FACETIME, true);
-	ENABLEMENT_CASE(MANAGEDASSETSD, true);
-	ENABLEMENT_CASE(POLARISD, true);
-
-	ENABLEMENT_CASE_FF(ARKITD, arkitd, false, false);
-	ENABLEMENT_CASE_FF(BACKBOARDD, backboardd, false, false);
-	ENABLEMENT_CASE_FF(WAKEBOARDD, wakeboardd, false, false);
-	ENABLEMENT_CASE_FF(REALITYCAMERAD, realitycamerad, false, false);
-#endif
-
 	ENABLEMENT_CASE_FF(AEGIRPOSTER, aegirposter, false, false);
 	ENABLEMENT_CASE_FF(COLLECTIONSPOSTER, CollectionsPoster, false, false);
-
-#if TARGET_OS_OSX
-	ENABLEMENT_CASE(GROUPSESSIONSERVICE, true);
-	ENABLEMENT_CASE(IMTRANSCODERAGENT, true);
-	ENABLEMENT_CASE(KEYCHAINSHARINGMESSAGINGD, true);
-	ENABLEMENT_CASE(MESSAGES, true);
-	ENABLEMENT_CASE(SCREENSHARING, true);
-#endif
 
 #if TARGET_OS_OSX
 	ENABLEMENT_CASE(VTDECODERXPCSERVICE, true);
 #endif
+
+	ENABLEMENT_CASE(MDS_STORES, true);
 
 	default:
 		return false;
@@ -694,18 +662,17 @@
 	bool secure_allocator = false;
 #if CONFIG_MALLOC_PROCESS_IDENTITY
 	if (malloc_process_identity != MALLOC_PROCESS_NONE) {
-		secure_allocator =
-				_malloc_check_secure_allocator_process_enablement(malloc_process_identity);
+		secure_allocator = _malloc_check_secure_allocator_process_enablement();
 	} else
 #endif // CONFIG_MALLOC_PROCESS_IDENTITY
 	{
-#if MALLOC_TARGET_IOS_ONLY || TARGET_OS_VISION
+#if MALLOC_TARGET_IOS_ONLY
 		secure_allocator = malloc_secure_feature_enabled(
 				SecureAllocator_SystemWide, true, true);
 #else
 		secure_allocator = malloc_secure_feature_enabled(
 				SecureAllocator_SystemWide, false, false);
-#endif	// MALLOC_TARGET_IOS_ONLY || TARGET_OS_VISION
+#endif	// MALLOC_TARGET_IOS_ONLY
 	}
 
 #if TARGET_OS_OSX && !TARGET_CPU_ARM64
@@ -769,13 +736,12 @@
 	}
 #endif
 
-	// TODO: envp should be passed down from Libsystem
-	const char **envp = (const char **)*_NSGetEnviron();
-
-	bool allow_internal_security = _malloc_allow_internal_security_policy(envp);
+#if CONFIG_CHECK_SECURITY_POLICY
+	bool allow_internal_security = _malloc_allow_internal_security_policy();
 	if (allow_internal_security != malloc_internal_security_policy) {
 		malloc_internal_security_policy = allow_internal_security;
 	}
+#endif
 
 #if CONFIG_MALLOC_PROCESS_IDENTITY
 	_malloc_check_process_identity(apple);
@@ -797,13 +763,13 @@
 		if (strstr(*p, LIBMALLOC_EXPERIMENT_FACTORS_KEY) == *p) {
 			malloc_experiments = *p;
 		}
-#if CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#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_MAGAZINE_DEFERRED_RECLAIM */
+#endif /* CONFIG_DEFERRED_RECLAIM */
 	}
 	if (!_malloc_entropy_initialized) {
 		getentropy((void*)malloc_entropy, sizeof(malloc_entropy));
@@ -1356,11 +1322,11 @@
 {
 	phys_ncpus = *(uint8_t *)(uintptr_t)_COMM_PAGE_PHYSICAL_CPUS;
 	logical_ncpus = *(uint8_t *)(uintptr_t)_COMM_PAGE_LOGICAL_CPUS;
-#if CONFIG_CLUSTER_AWARE
+#if CONFIG_MAGAZINE_PER_CLUSTER
 	{
 		ncpuclusters = *(uint8_t *)(uintptr_t)_COMM_PAGE_CPU_CLUSTERS;
 	}
-#endif // CONFIG_CLUSTER_AWARE
+#endif // CONFIG_MAGAZINE_PER_CLUSTER
 
 	if (0 != (logical_ncpus % phys_ncpus)) {
 		MALLOC_REPORT_FATAL_ERROR(logical_ncpus % phys_ncpus,
@@ -1397,11 +1363,11 @@
 		max_medium_magazines = max_magazines;
 	}
 
-	// Don't enable for pre-AMP iOS hardware, which we identify as "iOS, plain
-	// arm64"
-#if CONFIG_XZM_CLUSTER_AWARE && \
-		(MALLOC_TARGET_IOS_ONLY || MALLOC_TARGET_DK_IOS) && \
-		defined(__arm64__) && !defined(__arm64e__)
+	// The "single-cluster" concept doesn't apply to macOS: there is no
+	// non-Intel real hardware without multiple AMP clusters, so this will only
+	// happen under virtualization, and for that case we'd prefer the
+	// straight-to-pcpu behaviour rather than falling back to the old allocator.
+#if CONFIG_MAGAZINE_PER_CLUSTER && !(TARGET_OS_OSX || MALLOC_TARGET_DK_OSX)
 	if (ncpuclusters == 1) {
 #if CONFIG_FEATUREFLAGS_SIMPLE
 		// Not with the other feature flag checks because ncpuclusters needs to
@@ -1415,27 +1381,22 @@
 			malloc_xzone_enabled = false;
 		}
 	}
-#endif
+#endif // CONFIG_MAGAZINE_PER_CLUSTER && !(TARGET_OS_OSX || MALLOC_TARGET_DK_OSX)
 
 	_malloc_detect_interposition();
 
 
 	set_flags_from_environment();
-
-	if (malloc_report_config) {
-		malloc_report(ASL_LEVEL_INFO, "Internal Security Policy: %d\n",
-				malloc_internal_security_policy);
-	}
 
 
 #if CONFIG_SANITIZER
 	malloc_sanitizer_enabled = sanitizer_should_enable();
 #endif
 	
+#if CONFIG_NANOZONE
 	// TODO: envp should be passed down from Libsystem
 	const char **envp = (const char **)*_NSGetEnviron();
-
-#if CONFIG_NANOZONE
+	
 	// Disable nano when:
 	// - sanitizer is enabled, to avoid speculative out-of-bounds
 	//   use-after-free reads that nano/nanov2 performs, OR
@@ -1447,8 +1408,6 @@
 			!malloc_zero_on_free_sample_period) {
 		nano_common_init(envp, apple, bootargs);
 	}
-#else // CONFIG_NANOZONE
-	(void)envp;
 #endif // CONFIG_NANOZONE
 
 	bool nano_on_xzone = false;
@@ -1498,27 +1457,21 @@
 
 	initial_num_zones = malloc_num_zones;
 
-#if CONFIG_MAGAZINE_DEFERRED_RECLAIM
-	mach_vm_reclaim_error_t vmdr_kr = VM_RECLAIM_SUCCESS;
+#if CONFIG_DEFERRED_RECLAIM
+	kern_return_t vmdr_kr = KERN_SUCCESS;
 	if (large_cache_enabled) {
-		const bool xzone_deferred_reclaim =
-				CONFIG_XZM_DEFERRED_RECLAIM && initial_xzone_zone;
-		if (xzone_deferred_reclaim) {
+		if (initial_xzone_zone) {
 			// xzone_malloc will own the deferred_reclaim buffer
 			large_cache_enabled = false;
 		} else {
 			vmdr_kr = mvm_deferred_reclaim_init();
-			if (vmdr_kr != VM_RECLAIM_SUCCESS) {
+			if (vmdr_kr != KERN_SUCCESS) {
 				large_cache_enabled = false;
-				malloc_report(ASL_LEVEL_ERR, "Unable to set up "
-						"reclaim buffer, disabling "
-						"large cache [%d] %s\n",
-						err_get_code(vmdr_kr),
-						mach_error_string(vmdr_kr));
+				malloc_report(ASL_LEVEL_ERR, "Unable to set up reclaim buffer (%d) - disabling large cache\n", vmdr_kr);
 			}
 		}
 	}
-#endif // CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#endif /* CONFIG_DEFERRED_RECLAIM */
 
 	if (malloc_report_config && initial_scalable_zone) {
 		bool scribble = !!(malloc_debug_flags & MALLOC_DO_SCRIBBLE);
@@ -1526,15 +1479,15 @@
 				"\tMax Magazines: %d\n"
 				"\tMedium Enabled: %d\n"
 				"\tAggressive Madvise: %d\n"
-#if CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#if CONFIG_DEFERRED_RECLAIM
 				"\tLarge Cache: %d%s\n"
-#endif // CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#endif // CONFIG_DEFERRED_RECLAIM
 				"\tScribble: %d\n",
 				max_magazines, magazine_medium_enabled,
 				aggressive_madvise_enabled,
-#if CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#if CONFIG_DEFERRED_RECLAIM
 				vmdr_kr ?: large_cache_enabled, vmdr_kr ? " (ERROR)" : "",
-#endif // CONFIG_MAGAZINE_DEFERRED_RECLAIM
+#endif // CONFIG_DEFERRED_RECLAIM
 				scribble);
 	}
 
@@ -2823,7 +2776,8 @@
 		if ((malloc_debug_flags & (MALLOC_ABORT_ON_CORRUPTION | MALLOC_ABORT_ON_ERROR))) {
 			flags = MALLOC_REPORT_CRASH | MALLOC_REPORT_NOLOG;
 		}
-		malloc_report_pointer_was_not_allocated(flags, ptr);
+		malloc_report(flags,
+				"*** error for object %p: pointer being freed was not allocated\n", ptr);
 	} else if (zone->version >= 6 && zone->free_definite_size) {
 		malloc_zone_free_definite_size(zone, ptr, size);
 	} else {
@@ -2928,7 +2882,8 @@
 			if (malloc_debug_flags & abort_flags) {
 				flags = MALLOC_REPORT_CRASH | MALLOC_REPORT_NOLOG;
 			}
-			malloc_report_pointer_was_not_allocated(flags, in_ptr);
+			malloc_report(flags, "*** error for object %p: "
+					"pointer being realloc'd was not allocated\n", in_ptr);
 		} else {
 			retval = _malloc_zone_realloc(zone, in_ptr, new_size,
 					malloc_callsite_fallback_type_descriptor());
@@ -2957,12 +2912,7 @@
 	void *ptr = realloc(in_ptr, new_size);
 
 	if (!ptr && in_ptr && new_size != 0) {
-		// Save and restore `errno`, because `realloc` will set it to ENOMEM
-		// on allocation failure, but it could be overwritten if `free` calls
-		// into a library function that also modifies `errno`
-		errno_t error = errno;
 		free(in_ptr);
-		errno = error;
 	}
 
 	return ptr;
@@ -3139,16 +3089,6 @@
 		// There's no reasonable way to have the fallback callsite type
 		// descriptor work here.  That's okay, as it's uncommon and SPI, so its
 		// callers should be built with TMO.
-		const malloc_options_np_t known_options = MALLOC_NP_OPTION_CLEAR
-				;
-		if (options & ~known_options) {
-			malloc_zone_error(MALLOC_ABORT_ON_ERROR, true,
-					"malloc_zone_malloc_with_options: unsupported options 0x%llx\n",
-					options);
-			__builtin_trap();
-		}
-
-
 		if (align) {
 			ptr = malloc_zone_memalign(zone, align, size);
 			if (ptr && (options & MALLOC_NP_OPTION_CLEAR)) {
@@ -3159,7 +3099,6 @@
 		} else {
 			ptr = malloc_zone_malloc(zone, size);
 		}
-
 	} else {
 		MALLOC_TRACE(TRACE_malloc_options | DBG_FUNC_START, (uintptr_t)zone,
 				align, size, 0);
@@ -3329,11 +3268,11 @@
 void
 malloc_memory_event_handler(unsigned long event)
 {
-#if CONFIG_MADVISE_PRESSURE_RELIEF || (CONFIG_LARGE_CACHE && !CONFIG_MAGAZINE_DEFERRED_RECLAIM)
+#if CONFIG_MADVISE_PRESSURE_RELIEF || (CONFIG_LARGE_CACHE && !CONFIG_DEFERRED_RECLAIM)
 	if (event & MALLOC_MEMORYSTATUS_MASK_PRESSURE_RELIEF) {
 		malloc_zone_pressure_relief(0, 0);
 	}
-#endif /* CONFIG_MADVISE_PRESSURE_RELIEF || (CONFIG_LARGE_CACHE && !CONFIG_MAGAZINE_DEFERRED_RECLAIM) */
+#endif /* CONFIG_MADVISE_PRESSURE_RELIEF || (CONFIG_LARGE_CACHE && !CONFIG_DEFERRED_RECLAIM) */
 
 	if ((event & NOTE_MEMORYSTATUS_MSL_STATUS) != 0 && (event & ~NOTE_MEMORYSTATUS_MSL_STATUS) == 0) {
 		malloc_register_stack_logger();
@@ -3893,12 +3832,6 @@
 #endif
 }
 
-bool
-malloc_allows_internal_security_4test(void)
-{
-	return malloc_internal_security_policy;
-}
-
 /*****************	OBSOLETE ENTRY POINTS	********************/
 
 #ifndef PHASE_OUT_OLD_MALLOC
@@ -3941,6 +3874,13 @@
 #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)
 {
@@ -3951,12 +3891,6 @@
 void
 malloc_set_thread_options(malloc_thread_options_t opts)
 {
-	if (os_unlikely(opts.ReservedFlag)) {
-		malloc_zone_error(MALLOC_ABORT_ON_ERROR, true,
-				"malloc_zone_malloc_with_options: reserved TSD bit set\n");
-		__builtin_trap();
-	}
-
 	// Canonicalize options
 	if (opts.DisableExpensiveDebuggingOptions) {
 		opts.DisableProbabilisticGuardMalloc = true;
@@ -3965,7 +3899,8 @@
 
 	pgm_thread_set_disabled(opts.DisableProbabilisticGuardMalloc);
 
-	_malloc_set_thread_options(opts);
+	th_opts_t x = {.options = opts};
+	_pthread_setspecific_direct(__TSD_MALLOC_THREAD_OPTIONS, x.storage);
 }
 
 #pragma mark -