Loading...
--- libmalloc/libmalloc-792.80.2/src/malloc_exclaves.c
+++ libmalloc/libmalloc-521.120.7/src/malloc_exclaves.c
@@ -27,9 +27,6 @@
 
 #define MAX_MALLOC_ZONES 2
 
-#define DEFAULT_MALLOC_ZONE_STRING "DefaultXzoneZone"
-#define DEFAULT_SANITIZER_ZONE_STRING "DefaultWrapperSanitizerZone"
-
 MALLOC_NOEXPORT
 unsigned int phys_ncpus = 0;
 
@@ -41,12 +38,6 @@
 malloc_zone_t ** __unsafe_indexable malloc_zones = _malloc_zones;
 
 bool malloc_sanitizer_enabled = false;
-
-#if CONFIG_MTE
-bool malloc_has_sec_transition = false;
-uint32_t malloc_sec_transition_policy = 0;
-#endif
-
 #if __LIBLIBC_F_ASAN_INSTRUMENTATION
 static struct malloc_sanitizer_poison malloc_poison_default = {
 	.heap_allocate_poison = __asan_poison_heap_memory_alloc,
@@ -62,7 +53,7 @@
 malloc_zero_policy_t malloc_zero_policy = MALLOC_ZERO_POLICY_DEFAULT;
 
 static inline malloc_zone_t *
-_find_registered_zone(const void * __unsafe_indexable ptr, size_t *returned_size,
+find_registered_zone(const void * __unsafe_indexable ptr, size_t *returned_size,
 	bool known_non_default)
 {
 	malloc_zone_t *zone;
@@ -96,13 +87,6 @@
 	return zone;
 }
 
-malloc_zone_t *
-find_registered_zone(const void * __unsafe_indexable ptr, size_t *returned_size,
-	bool known_non_default)
-{
-	return _find_registered_zone(ptr, returned_size, known_non_default);
-}
-
 /*********  Creation and destruction    ************/
 
 static void
@@ -150,22 +134,15 @@
 	logical_ncpus = _liblibc_plat_num_cpus;
 	phys_ncpus = _liblibc_plat_num_cpus;
 
-#if CONFIG_MTE
-	malloc_has_sec_transition = xrt__has_sec_transition();
-#endif
-
 	const unsigned malloc_debug_flags = MALLOC_ABORT_ON_CORRUPTION |
 			MALLOC_ABORT_ON_ERROR;
-	malloc_zone_t *xzone = xzm_main_malloc_zone_create(malloc_debug_flags,
-			NULL, args, NULL);
-	_malloc_zone_register(xzone, true);
-	malloc_set_zone_name(xzone, DEFAULT_MALLOC_ZONE_STRING);
+	mfm_initialize();
+	_malloc_zone_register(xzm_main_malloc_zone_create(malloc_debug_flags,
+			NULL, args, NULL), true);
 
 #if __LIBLIBC_F_ASAN_INSTRUMENTATION
 	if ((malloc_sanitizer_enabled = sanitizer_should_enable())) {
-		malloc_zone_t *sanitizer = sanitizer_create_zone(xzone);
-		_malloc_zone_register(sanitizer, true);
-		malloc_set_zone_name(sanitizer, DEFAULT_SANITIZER_ZONE_STRING);
+		_malloc_zone_register(sanitizer_create_zone(_malloc_zones[0]), true);
 	}
 #endif // __LIBLIBC_F_ASAN_INSTRUMENTATION
 }
@@ -177,7 +154,7 @@
 
 /*********  Block creation and manipulation ************/
 
-void * __sized_by_or_null(size)
+void *
 _malloc_zone_malloc(malloc_zone_t *zone, size_t size, malloc_zone_options_t mzo)
 {
 	// This and similar conditionals are commented out to avoid compiler
@@ -192,13 +169,13 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(size)
+void *
 malloc_zone_malloc(malloc_zone_t *zone, size_t size)
 {
 	return _malloc_zone_malloc(zone, size, MZ_NONE);
 }
 
-void * __sized_by_or_null(num_items * size)
+void *
 _malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size,
 		malloc_zone_options_t mzo)
 {
@@ -214,13 +191,13 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(num_items * size)
+void *
 malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
 {
 	return _malloc_zone_calloc(zone, num_items, size, MZ_NONE);
 }
 
-void * __sized_by_or_null(size)
+void *
 _malloc_zone_valloc(malloc_zone_t *zone, size_t size, malloc_zone_options_t mzo)
 {
 	// if (os_unlikely(malloc_too_large(size))) {
@@ -237,13 +214,13 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(size)
+void *
 malloc_zone_valloc(malloc_zone_t *zone, size_t size)
 {
 	return _malloc_zone_valloc(zone, size, MZ_NONE);
 }
 
-void * __sized_by_or_null(size)
+void *
 _malloc_zone_realloc(malloc_zone_t *zone, void * __unsafe_indexable ptr,
 		size_t size, malloc_type_descriptor_t type_desc)
 {
@@ -255,7 +232,7 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(size)
+void *
 malloc_zone_realloc(malloc_zone_t *zone, void * __unsafe_indexable ptr,
 		size_t size)
 {
@@ -282,11 +259,11 @@
 	if (!ptr) {
 		return NULL;
 	} else {
-		return _find_registered_zone(ptr, NULL, false);
-	}
-}
-
-void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+		return find_registered_zone(ptr, NULL, false);
+	}
+}
+
+void * __alloc_align(2) __alloc_size(3)
 _malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size,
 	malloc_zone_options_t mzo, malloc_type_descriptor_t type_desc)
 {
@@ -299,7 +276,7 @@
 
 	// excludes 0 == alignment
 	// relies on sizeof(void *) being a power of two.
-	if (alignment < MALLOC_ZONE_MALLOC_DEFAULT_ALIGN ||
+	if (alignment < sizeof(void *) ||
 			0 != (alignment & (alignment - 1))) {
 		err = EINVAL;
 		goto out;
@@ -326,7 +303,7 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(size)
+void *
 malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size)
 {
 	return _malloc_zone_memalign(zone, alignment, size, MZ_NONE,
@@ -334,13 +311,12 @@
 }
 
 MALLOC_NOINLINE
-void * __sized_by_or_null(size)
-malloc_zone_malloc_with_options(malloc_zone_t *zone, size_t align,
-		size_t size, malloc_zone_malloc_options_t options)
-{
-	if (align != MALLOC_ZONE_MALLOC_DEFAULT_ALIGN &&
-			(os_unlikely((align != 0) && (!powerof2(align) ||
-			((size & (align-1)) != 0))))) { // equivalent to (size % align != 0)
+void *
+malloc_zone_malloc_with_options_np(malloc_zone_t *zone, size_t align,
+		size_t size, malloc_options_np_t options)
+{
+	if (os_unlikely((align != 0) && (!powerof2(align) ||
+			((size & (align-1)) != 0)))) { // equivalent to (size % align != 0)
 		return NULL;
 	}
 
@@ -348,29 +324,21 @@
 		zone = malloc_zones[0];
 	}
 
-	if (zone->version >= 15 && zone->malloc_with_options) {
+	if (zone->version >= 15) {
 		return zone->malloc_with_options(zone, align, size, options);
 	}
 
-	if (align > MALLOC_ZONE_MALLOC_DEFAULT_ALIGN) {
+	if (align) {
 		void *ptr = zone->memalign(zone, align, size);
-		if (ptr && (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR)) {
+		if (ptr && (options & MALLOC_NP_OPTION_CLEAR)) {
 			memset(ptr, 0, size);
 		}
 		return ptr;
-	} else if (options & MALLOC_ZONE_MALLOC_OPTION_CLEAR) {
+	} else if (options & MALLOC_NP_OPTION_CLEAR) {
 		return zone->calloc(zone, 1, size);
 	} else {
 		return zone->malloc(zone, size);
 	}
-}
-
-MALLOC_NOINLINE
-void * __sized_by_or_null(size)
-malloc_zone_malloc_with_options_np(malloc_zone_t *zone, size_t align,
-		size_t size, malloc_options_np_t options)
-{
-	return malloc_zone_malloc_with_options(zone, align, size, options);
 }
 
 boolean_t
@@ -396,7 +364,7 @@
 malloc_set_zone_name(malloc_zone_t *z, const char *name)
 {
 	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);
@@ -432,9 +400,11 @@
 		return;
 	}
 
-	zone = _find_registered_zone(ptr, &size, known_non_default);
+	zone = find_registered_zone(ptr, &size, known_non_default);
 	if (!zone) {
-		malloc_report_pointer_was_not_allocated(MALLOC_REPORT_CRASH, ptr);
+		malloc_report(MALLOC_REPORT_DEBUG | MALLOC_REPORT_NOLOG,
+			"*** error for object %p: pointer being freed was not allocated\n",
+			ptr);
 	} else if (zone->free_definite_size) {
 		malloc_zone_free_definite_size(zone,
 			__unsafe_forge_bidi_indexable(void *, ptr, size), size);
@@ -445,23 +415,27 @@
 
 /*********    Generic ANSI callouts    ************/
 
-void * __sized_by_or_null(size)
+// FIXME: Return NULL instead but we need bounds checking support
+// rdar://75598414 (Support __counted_by_or_null and __sized_by_or_null)
+#define NONZERO_SZ(sz) ((sz) ? (sz) : 1)
+
+void *
 malloc(size_t size)
 {
-	return malloc_zone_malloc(_malloc_zones[0], size);
-}
-
-void * __sized_by_or_null(size)
+	return malloc_zone_malloc(_malloc_zones[0], NONZERO_SZ(size));
+}
+
+void *
 aligned_alloc(size_t alignment, size_t size)
 {
-	return _malloc_zone_memalign(_malloc_zones[0], alignment, size,
+	return _malloc_zone_memalign(_malloc_zones[0], alignment, NONZERO_SZ(size),
 		MZ_POSIX | MZ_C11, MALLOC_TYPE_DESCRIPTOR_NONE);
 }
 
-void * __sized_by_or_null(num_items * size)
+void *
 calloc(size_t num_items, size_t size)
 {
-	return malloc_zone_calloc(_malloc_zones[0], num_items, size);
+	return malloc_zone_calloc(_malloc_zones[0], num_items, NONZERO_SZ(size));
 }
 
 void
@@ -485,7 +459,7 @@
 	return _free(ptr);
 }
 
-void * __sized_by_or_null(new_size)
+void *
 _realloc(void * __unsafe_indexable in_ptr, size_t new_size)
 {
 	void * __bidi_indexable retval = NULL;
@@ -502,12 +476,13 @@
 	// size."  So we only free the original memory if the allocation succeeds.
 	old_ptr = (new_size == 0) ? NULL : in_ptr;
 	if (!old_ptr) {
-		retval = malloc_zone_malloc(_malloc_zones[0], new_size);
-	} else {
-		zone = _find_registered_zone(old_ptr, NULL, false);
+		retval = malloc_zone_malloc(_malloc_zones[0], NONZERO_SZ(new_size));
+	} else {
+		zone = find_registered_zone(old_ptr, NULL, false);
 		if (!zone) {
-			malloc_report_pointer_was_not_allocated(MALLOC_REPORT_CRASH,
-					in_ptr);
+			malloc_report(MALLOC_REPORT_CRASH,
+				"*** error for object %p: pointer being realloc'd was not allocated\n",
+				in_ptr);
 		} else {
 			retval = malloc_zone_realloc(zone, old_ptr, new_size);
 		}
@@ -521,33 +496,28 @@
 	return retval;
 }
 
-void * __sized_by_or_null(new_size)
+void *
 realloc(void * __unsafe_indexable in_ptr, size_t new_size)
 {
 	return _realloc(in_ptr, new_size);
 }
 
-void * __sized_by_or_null(new_size)
+void *
 reallocf(void * __unsafe_indexable in_ptr, size_t new_size)
 {
 	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;
 }
 
-void * __sized_by_or_null(size)
+void *
 valloc(size_t size)
 {
-	return _malloc_zone_valloc(_malloc_zones[0], size, MZ_POSIX);
+	return _malloc_zone_valloc(_malloc_zones[0], NONZERO_SZ(size), MZ_POSIX);
 }
 
 size_t
@@ -559,7 +529,7 @@
 		return size;
 	}
 
-	(void)_find_registered_zone(ptr, &size, false);
+	(void)find_registered_zone(ptr, &size, false);
 	return size;
 }
 
@@ -578,13 +548,13 @@
 
 	/* POSIX is silent on NULL == memptr !?! */
 
-	retval = malloc_zone_memalign(_malloc_zones[0], alignment, size);
+	retval = malloc_zone_memalign(_malloc_zones[0], alignment, NONZERO_SZ(size));
 	if (retval == NULL) {
 		// To avoid testing the alignment constraints redundantly, we'll rely on
 		// the test made in malloc_zone_memalign to vet each request. Only if
 		// that test fails and returns NULL, do we arrive here to detect the
 		// bogus alignment and give the required EINVAL return.
-		if (alignment < MALLOC_ZONE_MALLOC_DEFAULT_ALIGN || // excludes 0 == alignment
+		if (alignment < sizeof(void *) ||             // excludes 0 == alignment
 				0 != (alignment & (alignment - 1))) { // relies on sizeof(void *)
 													  // being a power of two.
 			return EINVAL;
@@ -621,7 +591,7 @@
 	return false;
 }
 
-void * __sized_by_or_null(nmemb * size)
+void *
 reallocarray(void * in_ptr, size_t nmemb, size_t size)
 {
 	size_t alloc_size;
@@ -632,7 +602,7 @@
 	return realloc(in_ptr, alloc_size);
 }
 
-void * __sized_by_or_null(nmemb * size)
+void *
 reallocarrayf(void * in_ptr, size_t nmemb, size_t size)
 {
 	size_t alloc_size;
@@ -716,6 +686,5 @@
 void
 malloc_zero_on_free_disable(void)
 {
-	malloc_zone_error(MALLOC_ABORT_ON_ERROR, false,
-			"xzone cannot disable zero on free");
-}
+	malloc_zero_policy = MALLOC_ZERO_NONE;
+}