Loading...
--- libmalloc/libmalloc-792.41.1/src/sanitizer_malloc.c
+++ libmalloc/libmalloc-715.120.13/src/sanitizer_malloc.c
@@ -21,6 +21,10 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
+#include <malloc/_platform.h>
+#include <malloc_private.h>
+#include <stddef.h>
+
 #include "internal.h"
 
 #if CONFIG_SANITIZER
@@ -700,10 +704,9 @@
 	return size;
 }
 
-static void * __alloc_size(2) __sized_by_or_null(size)
+static void * __alloc_size(2)
 sanitizer_malloc_type_malloc_noalign_with_options(sanitizer_zone_t *zone,
-		size_t size, malloc_zone_malloc_options_t options,
-		malloc_type_id_t type_id)
+		size_t size, uint64_t options, malloc_type_id_t type_id)
 {
 	if (!size) {
 		size = 1;
@@ -732,7 +735,7 @@
 			// Dispatch directly with pass-thru options
 			ptr = DELEGATE(malloc_type_malloc_with_options, 0, size, options,
 					type_id);
-		} else if (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR) {
+		} else if (options & MALLOC_NP_OPTION_CLEAR) {
 			// Need fallback for this option
 			ptr = DELEGATE(malloc_type_calloc, 1, size, type_id);
 		} else {
@@ -750,21 +753,11 @@
 		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_ZONE_MALLOC_OPTION_CLEAR) {
+		if (options & MALLOC_NP_OPTION_CLEAR) {
 			// Need fallback for this option
 			ptr = DELEGATE(calloc, 1, size);
 		} else {
@@ -792,14 +785,14 @@
 	return ptr;
 }
 
-static void * __alloc_size(2) __sized_by_or_null(size)
+static void * __alloc_size(2)
 sanitizer_malloc(sanitizer_zone_t *zone, size_t size)
 {
 	return sanitizer_malloc_type_malloc_noalign_with_options(zone, size, 0,
 			malloc_get_tsd_type_id());
 }
 
-static void * __alloc_size(2) __sized_by_or_null(size)
+static void * __alloc_size(2)
 sanitizer_malloc_type_malloc(sanitizer_zone_t *zone, size_t size,
 		malloc_type_id_t type_id)
 {
@@ -807,7 +800,7 @@
 			type_id);
 }
 
-static void * __alloc_size(2,3) __sized_by_or_null(num_items * size)
+static void * __alloc_size(2,3)
 sanitizer_malloc_type_calloc(sanitizer_zone_t *zone, size_t num_items,
 		size_t size, malloc_type_id_t type_id)
 {
@@ -866,14 +859,14 @@
 }
 
 
-static void * __alloc_size(2,3) __sized_by_or_null(num_items * size)
+static void * __alloc_size(2,3)
 sanitizer_calloc(sanitizer_zone_t *zone, size_t num_items, size_t size)
 {
 	return sanitizer_malloc_type_calloc(zone, num_items, size,
 			malloc_get_tsd_type_id());
 }
 
-static void * __alloc_size(2) __sized_by_or_null(size)
+static void * __alloc_size(2)
 sanitizer_valloc(sanitizer_zone_t *zone, size_t size)
 {
 	if (!size) {
@@ -911,10 +904,6 @@
 static void
 sanitizer_free(sanitizer_zone_t *zone, void * __unsafe_indexable ptr)
 {
-	if (os_unlikely(!ptr)) {
-		return;
-	}
-
 	size_t size = 0;
 	if (zone->do_poisoning) {
 		size = DELEGATE(size, ptr);
@@ -924,7 +913,7 @@
 	place_into_quarantine(zone, ptr, size);
 }
 
-static void * __alloc_size(3) __sized_by_or_null(new_size)
+static void * __alloc_size(3)
 sanitizer_malloc_type_realloc(sanitizer_zone_t *zone,
 		void * __unsafe_indexable ptr, size_t new_size,
 		malloc_type_id_t type_id)
@@ -1002,7 +991,7 @@
 	return new_ptr;
 }
 
-static void * __alloc_size(3) __sized_by_or_null(new_size)
+static void * __alloc_size(3)
 sanitizer_realloc(sanitizer_zone_t *zone, void * __unsafe_indexable ptr, size_t new_size)
 {
 	return sanitizer_malloc_type_realloc(zone, ptr, new_size,
@@ -1022,7 +1011,7 @@
 #endif /* !MALLOC_TARGET_EXCLAVES */
 }
 
-static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+static void * __alloc_align(2) __alloc_size(3)
 sanitizer_malloc_type_memalign(sanitizer_zone_t *zone, size_t align,
 		size_t size, malloc_type_id_t type_id)
 {
@@ -1070,39 +1059,26 @@
 	return ptr;
 }
 
-static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+static void * __alloc_align(2) __alloc_size(3)
 sanitizer_memalign(sanitizer_zone_t *zone, size_t align, size_t size)
 {
 	return sanitizer_malloc_type_memalign(zone, align, size,
 			malloc_get_tsd_type_id());
 }
 
-static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+static void * __alloc_align(2) __alloc_size(3)
 sanitizer_malloc_type_malloc_with_options(sanitizer_zone_t *zone, size_t align,
-		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
+	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();
+	}
+
 
 	void *ptr;
 	if (!align) {
@@ -1110,26 +1086,18 @@
 			options, type_id);
 	} else {
 		ptr = sanitizer_malloc_type_memalign(zone, align, size, type_id);
-		if (ptr && (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR)) {
+		if (ptr && (options & MALLOC_NP_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)
+static void * __alloc_align(2) __alloc_size(3)
 sanitizer_malloc_with_options(sanitizer_zone_t *zone, size_t align, size_t size,
-		malloc_zone_malloc_options_t options)
+		uint64_t options)
 {
 	return sanitizer_malloc_type_malloc_with_options(zone, align, size, options,
 			malloc_get_tsd_type_id());