Loading...
--- libmalloc/libmalloc-657.80.3/src/sanitizer_malloc.c
+++ libmalloc/libmalloc-792.80.2/src/sanitizer_malloc.c
@@ -21,9 +21,6 @@
* @APPLE_LICENSE_HEADER_END@
*/
-#include <malloc/_platform.h>
-#include <stddef.h>
-
#include "internal.h"
#if CONFIG_SANITIZER
@@ -343,12 +340,16 @@
if (zone->debug) malloc_report(ASL_LEVEL_INFO, "evicting %p from quarantine, size = 0x%lx\n", iterator, iterator_size);
+ // Forge the pointer because it is only sized for quarantined_chunk_t
+ void *iterator_ptr = __unsafe_forge_bidi_indexable(void *, iterator,
+ iterator_size);
+
// Same as above, perform actual unpoisoning
if (zone->do_poisoning) {
- unpoison(zone, iterator, iterator_size);
+ unpoison(zone, iterator_ptr, iterator_size);
}
- DELEGATE(free_definite_size, iterator, iterator_size);
+ DELEGATE(free_definite_size, iterator_ptr, iterator_size);
iterator = next;
}
@@ -699,8 +700,10 @@
return size;
}
-static void * __alloc_size(2)
-sanitizer_malloc(sanitizer_zone_t *zone, size_t size)
+static void * __alloc_size(2) __sized_by_or_null(size)
+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)
{
if (!size) {
size = 1;
@@ -719,7 +722,60 @@
return NULL;
}
}
- void *ptr = DELEGATE(malloc, size);
+
+ void *ptr;
+#if MALLOC_TARGET_64BIT
+ malloc_type_descriptor_t type_desc = { .type_id = type_id };
+#endif // MALLOC_TARGET_64BIT
+ if (zone->wrapped_zone->version >= 16) {
+ if (zone->wrapped_zone->malloc_type_malloc_with_options) {
+ // 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) {
+ // Need fallback for this option
+ ptr = DELEGATE(malloc_type_calloc, 1, size, type_id);
+ } else {
+ // Remaining options already handled in parent, ignore them
+ ptr = DELEGATE(malloc_type_malloc, size, type_id);
+ }
+ } else if (zone->wrapped_zone->version >= 15 &&
+ zone->wrapped_zone->malloc_with_options) {
+ // Dispatch directly with type TSD and pass-thru options
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(type_desc);
+#endif // MALLOC_TARGET_64BIT
+ ptr = DELEGATE(malloc_with_options, 0, size, options);
+#if MALLOC_TARGET_64BIT
+ 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) {
+ // Need fallback for this option
+ ptr = DELEGATE(calloc, 1, size);
+ } else {
+ // Remaining options already handled in parent, ignore them
+ ptr = DELEGATE(malloc, size);
+ }
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(MALLOC_TYPE_DESCRIPTOR_NONE);
+#endif // MALLOC_TARGET_64BIT
+ }
+
#if !MALLOC_TARGET_EXCLAVES
record_alloc_stacktrace(zone->depo, zone->map, ptr, usr_size);
#endif /* !MALLOC_TARGET_EXCLAVES */
@@ -736,8 +792,24 @@
return ptr;
}
-static void * __alloc_size(2,3)
-sanitizer_calloc(sanitizer_zone_t *zone, size_t num_items, size_t size)
+static void * __alloc_size(2) __sized_by_or_null(size)
+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)
+sanitizer_malloc_type_malloc(sanitizer_zone_t *zone, size_t size,
+ malloc_type_id_t type_id)
+{
+ return sanitizer_malloc_type_malloc_noalign_with_options(zone, size, 0,
+ type_id);
+}
+
+static void * __alloc_size(2,3) __sized_by_or_null(num_items * size)
+sanitizer_malloc_type_calloc(sanitizer_zone_t *zone, size_t num_items,
+ size_t size, malloc_type_id_t type_id)
{
size_t usr_size;
if (!size || !num_items) {
@@ -760,7 +832,23 @@
return NULL;
}
}
- void *ptr = __unsafe_forge_bidi_indexable(void *, DELEGATE(calloc, num_items, size), usr_size);
+
+ void *ptr;
+ if (zone->wrapped_zone->version >= 16) {
+ ptr = __unsafe_forge_bidi_indexable(void *,
+ DELEGATE(malloc_type_calloc, num_items, size, type_id), usr_size);
+ } else {
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(
+ (malloc_type_descriptor_t){ .type_id = type_id });
+#endif // MALLOC_TARGET_64BIT
+ ptr = __unsafe_forge_bidi_indexable(void *,
+ DELEGATE(calloc, num_items, size), usr_size);
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(MALLOC_TYPE_DESCRIPTOR_NONE);
+#endif // MALLOC_TARGET_64BIT
+ }
+
if (zone->debug) malloc_report(ASL_LEVEL_INFO, "calloc(0x%lx, 0x%lx) = %p\n", num_items, size, ptr);
#if !MALLOC_TARGET_EXCLAVES
record_alloc_stacktrace(zone->depo, zone->map, ptr, usr_size);
@@ -777,7 +865,15 @@
return ptr;
}
-static void * __alloc_size(2)
+
+static void * __alloc_size(2,3) __sized_by_or_null(num_items * size)
+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)
sanitizer_valloc(sanitizer_zone_t *zone, size_t size)
{
if (!size) {
@@ -815,6 +911,10 @@
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);
@@ -824,8 +924,10 @@
place_into_quarantine(zone, ptr, size);
}
-static void * __alloc_size(3)
-sanitizer_realloc(sanitizer_zone_t *zone, void * __unsafe_indexable ptr, size_t new_size)
+static void * __alloc_size(3) __sized_by_or_null(new_size)
+sanitizer_malloc_type_realloc(sanitizer_zone_t *zone,
+ void * __unsafe_indexable ptr, size_t new_size,
+ malloc_type_id_t type_id)
{
if (new_size == 0) {
new_size = 1;
@@ -844,7 +946,20 @@
}
}
- void *new_ptr = DELEGATE(malloc, new_size);
+ void *new_ptr;
+ if (zone->wrapped_zone->version >= 16) {
+ new_ptr = DELEGATE(malloc_type_malloc, new_size, type_id);
+ } else {
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(
+ (malloc_type_descriptor_t){ .type_id = type_id });
+#endif // MALLOC_TARGET_64BIT
+ new_ptr = DELEGATE(malloc, new_size);
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(MALLOC_TYPE_DESCRIPTOR_NONE);
+#endif // MALLOC_TARGET_64BIT
+ }
+
#if !MALLOC_TARGET_EXCLAVES
record_alloc_stacktrace(zone->depo, zone->map, new_ptr, usr_new_size);
#endif /* !MALLOC_TARGET_EXCLAVES */
@@ -887,6 +1002,13 @@
return new_ptr;
}
+static void * __alloc_size(3) __sized_by_or_null(new_size)
+sanitizer_realloc(sanitizer_zone_t *zone, void * __unsafe_indexable ptr, size_t new_size)
+{
+ return sanitizer_malloc_type_realloc(zone, ptr, new_size,
+ malloc_get_tsd_type_id());
+}
+
static void
sanitizer_destroy(sanitizer_zone_t *zone)
{
@@ -900,8 +1022,9 @@
#endif /* !MALLOC_TARGET_EXCLAVES */
}
-static void * __alloc_align(2) __alloc_size(3)
-sanitizer_memalign(sanitizer_zone_t *zone, size_t alignment, size_t size)
+static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+sanitizer_malloc_type_memalign(sanitizer_zone_t *zone, size_t align,
+ size_t size, malloc_type_id_t type_id)
{
if (!size) {
size = 1;
@@ -916,11 +1039,25 @@
return NULL;
}
}
- void *ptr = DELEGATE(memalign, alignment, size);
+
+ void *ptr;
+ if (zone->wrapped_zone->version >= 16) {
+ ptr = DELEGATE(malloc_type_memalign, align, size, type_id);
+ } else {
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(
+ (malloc_type_descriptor_t){ .type_id = type_id });
+#endif // MALLOC_TARGET_64BIT
+ ptr = DELEGATE(memalign, align, size);
+#if MALLOC_TARGET_64BIT
+ malloc_set_tsd_type_descriptor(MALLOC_TYPE_DESCRIPTOR_NONE);
+#endif // MALLOC_TARGET_64BIT
+ }
+
#if !MALLOC_TARGET_EXCLAVES
record_alloc_stacktrace(zone->depo, zone->map, ptr, usr_size);
#endif /* !MALLOC_TARGET_EXCLAVES */
- if (zone->debug) malloc_report(ASL_LEVEL_INFO, "memalign(0x%lx, 0x%lx)\n", alignment, size);
+ if (zone->debug) malloc_report(ASL_LEVEL_INFO, "memalign(0x%lx, 0x%lx)\n", align, size);
if (ptr && zone->do_poisoning) {
// Recalculate the redzone size to include allocator padding
size_t actual_size = DELEGATE(size, ptr);
@@ -933,6 +1070,71 @@
return ptr;
}
+static void * __alloc_align(2) __alloc_size(3) __sized_by_or_null(size)
+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)
+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
+
+ void *ptr;
+ if (!align) {
+ ptr = sanitizer_malloc_type_malloc_noalign_with_options(zone, size,
+ options, type_id);
+ } else {
+ ptr = sanitizer_malloc_type_memalign(zone, align, size, type_id);
+ 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,
+ malloc_zone_malloc_options_t options)
+{
+ return sanitizer_malloc_type_malloc_with_options(zone, align, size, options,
+ malloc_get_tsd_type_id());
+}
+
static void
sanitizer_free_definite_size(sanitizer_zone_t *zone, void * __sized_by(size) ptr, size_t size)
{
@@ -1095,7 +1297,7 @@
}
g_crm_reader = NULL;
- memset(report, 0, sizeof(*report));
+ bzero(report, sizeof(*report));
report->fault_address = fault_address;
if (enumeration_context.found_range.address != 0) {
@@ -1191,7 +1393,7 @@
// Introspection
.zone_name = "SanitizerMallocZone",
- .version = 14,
+ .version = 16,
.introspect = &sanitizer_zone_introspect_template,
// Specialized operations
@@ -1200,6 +1402,15 @@
.pressure_relief = malloc_zone_pressure_relief_fallback,
.claimed_address = FN_PTR(sanitizer_claimed_address),
.try_free_default = NULL,
+ .malloc_with_options = FN_PTR(sanitizer_malloc_with_options),
+
+ // Typed operations
+ .malloc_type_malloc = FN_PTR(sanitizer_malloc_type_malloc),
+ .malloc_type_calloc = FN_PTR(sanitizer_malloc_type_calloc),
+ .malloc_type_realloc = FN_PTR(sanitizer_malloc_type_realloc),
+ .malloc_type_memalign = FN_PTR(sanitizer_malloc_type_memalign),
+ .malloc_type_malloc_with_options =
+ FN_PTR(sanitizer_malloc_type_malloc_with_options),
};