Loading...
--- libmalloc/libmalloc-657.60.21/src/pgm_malloc.c
+++ libmalloc/libmalloc-521.100.59/src/pgm_malloc.c
@@ -25,6 +25,7 @@
 
 #include <TargetConditionals.h>
 #include <mach/mach_time.h>  // mach_absolute_time()
+#include <sys/codesign.h>  // csops()
 
 #include "internal.h"
 
@@ -84,6 +85,7 @@
 	uint32_t max_allocations;
 	uint32_t max_metadata;
 	uint32_t sample_counter_range;
+	uint32_t min_alignment;
 	bool debug;
 	uint64_t debug_log_throttle_ms;
 
@@ -312,21 +314,19 @@
 #pragma mark -
 #pragma mark Allocator Helpers
 
-// Darwin ABI requires 16 byte alignment.
-static const size_t k_min_alignment = 16;
-
 static bool
 is_power_of_2(size_t n) {
 	return __builtin_popcountl(n) == 1;
 }
 
 static size_t
-block_size(size_t size)
-{
+block_size(size_t size, size_t min_alignment)
+{
+	MALLOC_ASSERT(is_power_of_2(min_alignment));
 	if (size == 0) {
-		return k_min_alignment;
-	}
-	const size_t mask = (k_min_alignment - 1);
+		return min_alignment;
+	}
+	const size_t mask = (min_alignment - 1);
 	return (size + mask) & ~mask;
 }
 
@@ -425,14 +425,14 @@
 allocate(pgm_zone_t *zone, size_t size, size_t alignment)
 {
 	MALLOC_ASSERT(size <= PAGE_SIZE);
-	MALLOC_ASSERT(k_min_alignment <= alignment && alignment <= PAGE_SIZE);
+	MALLOC_ASSERT(zone->min_alignment <= alignment && alignment <= PAGE_SIZE);
 	MALLOC_ASSERT(is_power_of_2(alignment));
 
 	if (is_full(zone)) {
 		return (vm_address_t)NULL;
 	}
 
-	size = block_size(size);
+	size = block_size(size, zone->min_alignment);
 	uint32_t slot = choose_available_slot(zone);
 	uint32_t metadata = choose_metadata(zone, slot);
 	uint16_t offset = choose_offset_on_page(size, alignment, PAGE_SIZE);
@@ -511,7 +511,7 @@
 
 	vm_address_t new_addr;
 	if (sample && !is_full(zone)) {
-		new_addr = allocate(zone, new_size, k_min_alignment);
+		new_addr = allocate(zone, new_size, zone->min_alignment);
 		MALLOC_ASSERT(new_addr);
 	} else {
 		new_addr = (vm_address_t)DELEGATE(malloc, new_size);
@@ -577,14 +577,7 @@
 static void *
 pgm_malloc(pgm_zone_t *zone, size_t size)
 {
-	SAMPLED_ALLOCATE(size, k_min_alignment, malloc, size);
-	return ptr;
-}
-
-static void *
-pgm_malloc_type_malloc(pgm_zone_t *zone, size_t size, malloc_type_id_t type_id)
-{
-	SAMPLED_ALLOCATE(size, k_min_alignment, malloc_type_malloc, size, type_id);
+	SAMPLED_ALLOCATE(size, zone->min_alignment, malloc, size);
 	return ptr;
 }
 
@@ -595,22 +588,8 @@
 	if (os_unlikely(os_mul_overflow(num_items, size, &total_size))) {
 		return DELEGATE(calloc, num_items, size);
 	}
-	SAMPLED_ALLOCATE(total_size, k_min_alignment, calloc, num_items, size);
-	bzero(ptr, total_size);
-	return ptr;
-}
-
-static void *
-pgm_malloc_type_calloc(pgm_zone_t *zone, size_t num_items, size_t size,
-		malloc_type_id_t type_id)
-{
-	size_t total_size;
-	if (os_unlikely(os_mul_overflow(num_items, size, &total_size))) {
-		return DELEGATE(malloc_type_calloc, num_items, size, type_id);
-	}
-	SAMPLED_ALLOCATE(total_size, k_min_alignment, malloc_type_calloc, num_items,
-			size, type_id);
-	bzero(ptr, total_size);
+	SAMPLED_ALLOCATE(total_size, zone->min_alignment, calloc, num_items, size);
+	memset(ptr, 0, total_size);
 	return ptr;
 }
 
@@ -643,23 +622,6 @@
 	return new_ptr;
 }
 
-static void *
-pgm_malloc_type_realloc(pgm_zone_t *zone, void *ptr, size_t new_size,
-		malloc_type_id_t type_id)
-{
-	if (os_unlikely(!ptr)) {
-		return pgm_malloc_type_malloc(zone, new_size, type_id);
-	}
-	boolean_t sample = should_sample(zone, new_size);
-	if (os_likely(!sample)) {
-		DELEGATE_UNGUARDED(ptr, malloc_type_realloc, ptr, new_size, type_id);
-	}
-	lock(zone);
-	void *new_ptr = (void *)reallocate(zone, (vm_address_t)ptr, new_size, sample);
-	unlock(zone);
-	return new_ptr;
-}
-
 static void my_vm_deallocate(vm_address_t addr, size_t size);
 static void
 pgm_destroy(pgm_zone_t *zone)
@@ -672,27 +634,15 @@
 static void *
 pgm_memalign(pgm_zone_t *zone, size_t alignment, size_t size)
 {
-	if (os_unlikely(alignment > PAGE_SIZE)) {
+	// Delegate for (alignment > page size) and invalid alignment sizes.
+	if (alignment > PAGE_SIZE || !is_power_of_2(alignment) || alignment < sizeof(void *)) {
 		return DELEGATE(memalign, alignment, size);
 	}
-	size_t adj_alignment = MAX(alignment, k_min_alignment);
+	size_t adj_alignment = MAX(alignment, zone->min_alignment);
 	SAMPLED_ALLOCATE(size, adj_alignment, memalign, alignment, size);
 	return ptr;
 }
 
-static void *
-pgm_malloc_type_memalign(pgm_zone_t *zone, size_t alignment, size_t size,
-		malloc_type_id_t type_id)
-{
-	if (os_unlikely(alignment > PAGE_SIZE)) {
-		return DELEGATE(malloc_type_memalign, alignment, size, type_id);
-	}
-	size_t adj_alignment = MAX(alignment, k_min_alignment);
-	SAMPLED_ALLOCATE(size, adj_alignment, malloc_type_memalign, alignment, size,
-			type_id);
-	return ptr;
-}
-
 static void
 pgm_free_definite_size(pgm_zone_t *zone, void *ptr, size_t size)
 {
@@ -702,40 +652,40 @@
 static boolean_t
 pgm_claimed_address(pgm_zone_t *zone, void *ptr)
 {
-	DELEGATE_UNGUARDED(ptr, claimed_address, ptr);
-	return true;
+	return is_guarded(zone, (vm_address_t)ptr);
 }
 
 static void *
 pgm_malloc_with_options(pgm_zone_t *zone, size_t align, size_t size,
 		uint64_t options)
 {
-	if (os_unlikely(align > PAGE_SIZE)) {
+	if (os_unlikely(should_sample(zone, size))) {
+		size_t adj_alignment = MAX(align, zone->min_alignment);
+		lock(zone);
+		void *ptr = (void *)allocate(zone, size, adj_alignment);
+		unlock(zone);
+		if (ptr) {
+			if (options & MALLOC_NP_OPTION_CLEAR) {
+				bzero(ptr, size);
+			}
+			return ptr;
+		}
+		// If the allocaiton fails, fall back to the wrapped zone
+	}
+
+	if (zone->wrapped_zone->version >= 15) {
 		return DELEGATE(malloc_with_options, align, size, options);
-	}
-	size_t adj_alignment = MAX(align, k_min_alignment);
-	SAMPLED_ALLOCATE(size, adj_alignment, malloc_with_options, align, size, options);
-	if (options & MALLOC_NP_OPTION_CLEAR) {
-		bzero(ptr, size);
-	}
-	return ptr;
-}
-
-static void *
-pgm_malloc_type_malloc_with_options(pgm_zone_t *zone, size_t align, size_t size,
-		uint64_t options, malloc_type_id_t type_id)
-{
-	if (os_unlikely(align > PAGE_SIZE)) {
-		return DELEGATE(malloc_type_malloc_with_options, align, size, options,
-				type_id);
-	}
-	size_t adj_alignment = MAX(align, k_min_alignment);
-	SAMPLED_ALLOCATE(size, adj_alignment, malloc_type_malloc_with_options,
-			align, size, options, type_id);
-	if (options & MALLOC_NP_OPTION_CLEAR) {
-		bzero(ptr, size);
-	}
-	return ptr;
+	} else if (align) {
+		void *ptr = DELEGATE(memalign, align, size);
+		if (ptr && (options & MALLOC_NP_OPTION_CLEAR)) {
+			bzero(ptr, size);
+		}
+		return ptr;
+	} else if (options & MALLOC_NP_OPTION_CLEAR) {
+		return DELEGATE(calloc, 1, size);
+	} else {
+		return DELEGATE(malloc, size);
+	}
 }
 
 #pragma mark -
@@ -751,7 +701,8 @@
 			(zone->max_allocations <= zone->max_metadata / 2) &&  // choose_metadata() relies on max_allocations << max_metadata
 			(zone->max_metadata <= zone->num_slots) &&
 			(zone->num_slots <= k_max_slots) &&
-			(zone->sample_counter_range > 0);
+			(zone->sample_counter_range > 0) &&
+			(zone->min_alignment == 1 || zone->min_alignment == 16);  // strict alignment || Darwin ABI alignment
 }
 
 static bool
@@ -787,9 +738,9 @@
 	return (slot->state <= ss_freed) &&
 			(slot->metadata < zone->num_metadata) &&
 			(slot->size <= PAGE_SIZE) &&
-			(slot->size == block_size(slot->size)) &&
+			(slot->size == block_size(slot->size, zone->min_alignment)) &&
 			(slot->offset <= PAGE_SIZE) &&
-			(slot->offset % k_min_alignment == 0) &&
+			(slot->offset % zone->min_alignment == 0) &&
 			((size_t)slot->offset + slot->size <= PAGE_SIZE);
 }
 
@@ -1030,8 +981,7 @@
 #pragma mark -
 #pragma mark Zone Templates
 
-#define PGM_ZONE_VERSION 16
-#define MIN_WRAPPED_ZONE_VERSION 16
+#define PGM_ZONE_VERSION 15
 
 // Suppress warning: incompatible function pointer types
 #define FN_PTR(fn) (void *)(&fn)
@@ -1099,18 +1049,10 @@
 	// Specialized operations
 	.memalign = FN_PTR(pgm_memalign),
 	.free_definite_size = FN_PTR(pgm_free_definite_size),
-	.pressure_relief = malloc_zone_pressure_relief_fallback,
+	.pressure_relief = NULL,
 	.claimed_address = FN_PTR(pgm_claimed_address),
 	.try_free_default = NULL,
 	.malloc_with_options = FN_PTR(pgm_malloc_with_options),
-
-	// Typed entrypoints
-	.malloc_type_malloc = FN_PTR(pgm_malloc_type_malloc),
-	.malloc_type_calloc = FN_PTR(pgm_malloc_type_calloc),
-	.malloc_type_realloc = FN_PTR(pgm_malloc_type_realloc),
-	.malloc_type_memalign = FN_PTR(pgm_malloc_type_memalign),
-	.malloc_type_malloc_with_options =
-			FN_PTR(pgm_malloc_type_malloc_with_options),
 };
 
 
@@ -1151,6 +1093,7 @@
 	bool internal_build;
 	bool MallocProbGuard_is_set;
 	bool MallocProbGuard;
+	bool MallocProbGuardViaLaunchd;
 } g_env;
 
 void
@@ -1164,16 +1107,20 @@
 		g_env.MallocProbGuard_is_set = true;
 		g_env.MallocProbGuard = env_bool("MallocProbGuard");
 	}
+	if (env_bool("MallocProbGuardViaLaunchd")) {
+		g_env.MallocProbGuardViaLaunchd = true;
+	}
 }
 
 static bool
 is_platform_binary(void)
 {
-#if CONFIG_CHECK_PLATFORM_BINARY
-	return malloc_is_platform_binary;
-#else
-	return _malloc_is_platform_binary();
-#endif
+	uint32_t flags = 0;
+	int err = csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags));
+	if (err) {
+		return false;
+	}
+	return (flags & CS_PLATFORM_BINARY);
 }
 
 extern bool main_image_has_section(const char* segname, const char *sectname);
@@ -1183,10 +1130,16 @@
 	if (!internal_build && !is_platform_binary()) {
 		return false;
 	}
+#if TARGET_OS_OSX
 	uint32_t activation_rate = (internal_build ? 250 : 1000);
 	if (rand_uniform(activation_rate) != 0) {
 		return false;
 	}
+#else
+	if (!g_env.MallocProbGuardViaLaunchd) {
+		return false;
+	}
+#endif
 	if (main_image_has_section("__DATA", "__pgm_opt_out")) {
 		return false;
 	}
@@ -1221,6 +1174,8 @@
 		}
 #elif PGM_ALLOW_NON_INTERNAL_ACTIVATION
 		return true;
+#elif TARGET_OS_DRIVERKIT
+		// Never enable for DriverKit
 #else
 		if (internal_build) {
 			return true;
@@ -1278,13 +1233,15 @@
 	uint32_t sample_rate = env_uint("MallocProbGuardSampleRate", choose_sample_rate());
 	// Approximate a (1 / sample_rate) chance for sampling; 1 means "always sample".
 	zone->sample_counter_range = (sample_rate != 1) ? (2 * sample_rate) : 1;
+	bool strict_alignment = env_var("MallocProbGuardStrictAlignment") ? env_bool("MallocProbGuardStrictAlignment") : FEATURE_FLAG(ProbGuardStrictAlignment, false);
+	zone->min_alignment = (strict_alignment && MALLOC_TARGET_64BIT) ? 1 : 16;  // Darwin ABI requires 16 byte alignment.
 	zone->debug = env_bool("MallocProbGuardDebug");
 	zone->debug_log_throttle_ms = env_uint("MallocProbGuardDebugLogThrottleInMillis", 1000);
 
 	if (zone->debug) {
 		malloc_report(ASL_LEVEL_INFO,
-				"ProbGuard configuration: %u kB budget, 1/%u sample rate, %u/%u/%u allocations/metadata/slots\n",
-				memory_budget_in_kb, sample_rate, zone->max_allocations, zone->max_metadata, zone->num_slots);
+				"ProbGuard configuration: %u kB budget, 1/%u sample rate, %u/%u/%u allocations/metadata/slots, strict alignment: %d\n",
+				memory_budget_in_kb, sample_rate, zone->max_allocations, zone->max_metadata, zone->num_slots, strict_alignment);
 	}
 	if (!check_configuration(zone)) {
 		MALLOC_REPORT_FATAL_ERROR(0, "ProbGuard: bad configuration");
@@ -1306,18 +1263,8 @@
 disable_unsupported_apis(malloc_zone_t *pgm_zone, const malloc_zone_t *wrapped_zone)
 {
 	#define DISABLE_UNSUPPORTED(api) if (!wrapped_zone->api) pgm_zone->api = NULL;
-
-	// In practice, there are no zones we support wrapping right now that don't
-	// have these entrypoints
 	DISABLE_UNSUPPORTED(memalign)
-	DISABLE_UNSUPPORTED(malloc_type_memalign)
 	DISABLE_UNSUPPORTED(free_definite_size)
-	DISABLE_UNSUPPORTED(claimed_address)
-
-	// These ones are actually load-bearing: the nano and scalable zones do not
-	// implement these entrypoints
-	DISABLE_UNSUPPORTED(malloc_with_options)
-	DISABLE_UNSUPPORTED(malloc_type_malloc_with_options)
 }
 
 static void
@@ -1351,7 +1298,12 @@
 malloc_zone_t *
 pgm_create_zone(malloc_zone_t *wrapped_zone)
 {
-	MALLOC_ASSERT(wrapped_zone->version >= MIN_WRAPPED_ZONE_VERSION);
+	// The PGM zone includes the `malloc_introspection_t::zone_type` field
+	// (version 14) so we need to support the "malloc()/calloc() set errno to
+	// ENOMEM on failure" behavior (version 13).  Require wrapped zone to be at
+	// least version 13.
+	MALLOC_STATIC_ASSERT(PGM_ZONE_VERSION == 15, "PGM zone version");
+	MALLOC_ASSERT(wrapped_zone->version >= 13);
 
 	pgm_zone_t *zone = (pgm_zone_t *)my_vm_map(sizeof(pgm_zone_t), VM_PROT_READ_WRITE, VM_MEMORY_MALLOC);
 	setup_zone(zone, wrapped_zone);