Loading...
src/sanitizer_malloc.c libmalloc-715.140.5 libmalloc-792.80.2
--- libmalloc/libmalloc-715.140.5/src/sanitizer_malloc.c
+++ libmalloc/libmalloc-792.80.2/src/sanitizer_malloc.c
@@ -21,10 +21,6 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#include <malloc/_platform.h>
-#include <malloc_private.h>
-#include <stddef.h>
-
 #include "internal.h"
 
 #if CONFIG_SANITIZER
@@ -706,7 +702,8 @@
 
 static void * __alloc_size(2) __sized_by_or_null(size)
 sanitizer_malloc_type_malloc_noalign_with_options(sanitizer_zone_t *zone,
-		size_t size, uint64_t options, malloc_type_id_t type_id)
+		size_t size, malloc_zone_malloc_options_t options,
+		malloc_type_id_t type_id)
 {
 	if (!size) {
 		size = 1;
@@ -735,7 +732,7 @@
 			// Dispatch directly with pass-thru options
 			ptr = DELEGATE(malloc_type_malloc_with_options, 0, size, options,
 					type_id);
-		} else if (options & MALLOC_NP_OPTION_CLEAR) {
+		} else if (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR) {
 			// Need fallback for this option
 			ptr = DELEGATE(malloc_type_calloc, 1, size, type_id);
 		} else {
@@ -753,11 +750,21 @@
 		malloc_set_tsd_type_descriptor(MALLOC_TYPE_DESCRIPTOR_NONE);
 #endif // MALLOC_TARGET_64BIT
 	} else {
+		const malloc_zone_malloc_options_t known_options =
+				MALLOC_ZONE_MALLOC_OPTION_CLEAR
+				;
+		if (options & ~known_options) {
+			malloc_zone_error(MALLOC_ABORT_ON_ERROR, true,
+					"sanitizer_malloc_with_options: unsupported options 0x%llx\n",
+					options);
+			__builtin_trap();
+		}
+
 		// Set the type TSD and check the options
 #if MALLOC_TARGET_64BIT
 		malloc_set_tsd_type_descriptor(type_desc);
 #endif // MALLOC_TARGET_64BIT
-		if (options & MALLOC_NP_OPTION_CLEAR) {
+		if (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR) {
 			// Need fallback for this option
 			ptr = DELEGATE(calloc, 1, size);
 		} else {
@@ -1072,17 +1079,30 @@
 
 static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
 sanitizer_malloc_type_malloc_with_options(sanitizer_zone_t *zone, size_t align,
-	size_t size, uint64_t options, malloc_type_id_t type_id)
-{
-	const malloc_options_np_t known_options = MALLOC_NP_OPTION_CLEAR
-			;
-	if (options & ~known_options) {
-		malloc_zone_error(MALLOC_ABORT_ON_ERROR, true,
-				"sanitizer_malloc_with_options: unsupported options 0x%llx\n",
-				options);
-		__builtin_trap();
-	}
-
+		size_t size, malloc_zone_malloc_options_t options,
+		malloc_type_id_t type_id)
+{
+#if CONFIG_MTE
+	// rdar://140822174
+	// When dyld interposition or a wrapper zone that does not support
+	// forwarding malloc options is enabled, we need to set a flag in
+	// the TSD to preserve the semantics of canonical tagging.
+	bool use_tsd_fallback =
+			(options & MALLOC_ZONE_MALLOC_OPTION_CANONICAL_TAG) &&
+			(zone->wrapped_zone->version < 15 ||
+			!zone->wrapped_zone->malloc_with_options);
+#if !MALLOC_TARGET_EXCLAVES
+	malloc_thread_options_t opts;
+	if (use_tsd_fallback) {
+		opts = malloc_get_thread_options();
+		malloc_thread_options_t newopts = opts;
+		newopts.ReservedFlag = true;
+		_malloc_set_thread_options(newopts);
+	}
+#else
+	MALLOC_ASSERT(!use_tsd_fallback);
+#endif // MALLOC_TARGET_EXCLAVES
+#endif // CONFIG_MTE
 
 	void *ptr;
 	if (!align) {
@@ -1090,18 +1110,26 @@
 			options, type_id);
 	} else {
 		ptr = sanitizer_malloc_type_memalign(zone, align, size, type_id);
-		if (ptr && (options & MALLOC_NP_OPTION_CLEAR)) {
+		if (ptr && (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR)) {
 			bzero(ptr, size);
 		}
 	}
 
+#if CONFIG_MTE
+#if !MALLOC_TARGET_EXCLAVES
+	// Restore the saved TSD flags
+	if (use_tsd_fallback) {
+		_malloc_set_thread_options(opts);
+	}
+#endif // MALLOC_TARGET_EXCLAVES
+#endif // CONFIG_MTE
 
 	return ptr;
 }
 
 static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
 sanitizer_malloc_with_options(sanitizer_zone_t *zone, size_t align, size_t size,
-		uint64_t options)
+		malloc_zone_malloc_options_t options)
 {
 	return sanitizer_malloc_type_malloc_with_options(zone, align, size, options,
 			malloc_get_tsd_type_id());