Loading...
--- libmalloc/libmalloc-646.0.13/src/early_malloc.c
+++ libmalloc/libmalloc-792.1.1/src/early_malloc.c
@@ -47,7 +47,9 @@
 #if MALLOC_TARGET_EXCLAVES || MALLOC_TARGET_EXCLAVES_INTROSPECTOR
 #define MFM_ARENA_SIZE      (1ul << 20)
 #else
-#define MFM_ARENA_SIZE      (8ul << 20)
+// Originally this was set to 8M: we shrank it down to 4M to accomodate for
+// the guarded range reservation (as the arena was heavily underused anyway).
+#define MFM_ARENA_SIZE      (4ul << 20)
 #endif /* MALLOC_TARGET_EXCLAVES || MALLOC_TARGET_EXCLAVES_INTROSPECTOR */
 #define MFM_QUANTUM         16ul
 #define MFM_SIZE_CLASSES    (__builtin_ctz(MFM_ALLOC_SIZE_MAX / MFM_QUANTUM) + 1)
@@ -95,7 +97,7 @@
 	void *__ptrauth(ptrauth_key_process_dependent_data, true,
 			ptrauth_string_discriminator("mfmb_next"),
 			"authenticates-null-values")
-	                        mfmb_next;
+							mfmb_next;
 #else
 	uint64_t                mfmb_next;
 #endif
@@ -642,33 +644,28 @@
 mfm_initialize(void)
 {
 	struct mfm_arena *arena;
+	int debug_flags;
 #if MALLOC_TARGET_EXCLAVES
 	plat_map_t map = {0};
+	debug_flags = MALLOC_NO_POPULATE;
+#else
+	int alloc_flags = 0;
+	debug_flags = DISABLE_ASLR | MALLOC_ADD_GUARD_PAGE_FLAGS;
 #endif // MALLOC_TARGET_EXCLAVES
 
 
-#if MALLOC_TARGET_EXCLAVES
-	arena = mvm_allocate_pages_plat(MFM_ARENA_SIZE, 0, MALLOC_NO_POPULATE,
+	/* this is called early, which means the address space _does_ have 4M */
+	arena = mvm_allocate_pages_plat(MFM_ARENA_SIZE, 0, debug_flags,
 			VM_MEMORY_MALLOC, mvm_plat_map(map));
-#else
-	/* this is called early, which means the address space _does_ have 8M */
-	arena = mvm_allocate_pages_plat(MFM_ARENA_SIZE, 0,
-			DISABLE_ASLR | MALLOC_ADD_GUARD_PAGE_FLAGS, VM_MEMORY_MALLOC,
-			NULL);
-#endif // MALLOC_TARGET_EXCLAVES
-
 	if (arena == NULL) {
 		MFM_INTERNAL_CRASH(arena, "failed to allocate memory");
 	}
 
 #if MALLOC_TARGET_EXCLAVES
 	/* populate the header up to the block storage */
-	const uintptr_t addr = (uintptr_t)mvm_allocate_plat((uintptr_t)arena,
-			roundup(offsetof(struct mfm_arena, mfm_blocks), PAGE_SIZE), 0,
-			VM_FLAGS_FIXED, 0, 0, mvm_plat_map(map));
-	if (addr != (uintptr_t)arena) {
-		MFM_INTERNAL_CRASH(addr, "populate of header failed");
-	}
+	mvm_madvise_plat(arena,
+			roundup(offsetof(struct mfm_arena, mfm_blocks), PAGE_SIZE),
+			MADV_FAULTABLE, MALLOC_ABORT_ON_ERROR, mvm_plat_map(map));
 
 	arena->mfm_header.mfm_map = map;
 #else
@@ -678,8 +675,7 @@
 	 * originally because the kernel would have placed it in the heap range */
 	mach_vm_address_t vm_addr = (mach_vm_address_t)arena;
 	mach_vm_size_t vm_size = (mach_vm_size_t)MFM_ARENA_SIZE;
-	int alloc_flags = VM_FLAGS_OVERWRITE | VM_MAKE_TAG(VM_MEMORY_MALLOC_TINY);
-
+	alloc_flags |= VM_FLAGS_OVERWRITE | VM_MAKE_TAG(VM_MEMORY_MALLOC_TINY);
 
 	kern_return_t kr = mach_vm_map(mach_task_self(), &vm_addr, vm_size,
 			/* mask */ 0, alloc_flags, MEMORY_OBJECT_NULL, /* offset */ 0,
@@ -769,7 +765,7 @@
 			if (blk_size > size) {
 				__mfm_block_mark_start(arena, blk_index + size);
 				__mfm_free_block(arena, blk_index + size,
-				    blk_size - size);
+					blk_size - size);
 			}
 
 			__mfm_block_mark_allocated(arena, blk_index, size);
@@ -799,12 +795,9 @@
 			const uintptr_t end = roundup((uintptr_t)ptr + alloc_size, PAGE_SIZE);
 			const size_t bytes = end - begin;
 			if (bytes) {
-				const uintptr_t addr = (uintptr_t)mvm_allocate_plat(begin,
-						bytes, 0, VM_FLAGS_FIXED, 0, 0,
+				mvm_madvise_plat((void*)begin, bytes, MADV_FAULTABLE,
+						MALLOC_ABORT_ON_ERROR,
 						mvm_plat_map(arena->mfm_header.mfm_map));
-				if (addr != begin) {
-					MFM_INTERNAL_CRASH(ptr, "populate of pages failed");
-				}
 			}
 #endif
 
@@ -872,7 +865,7 @@
 	}
 
 	if (index + size < arena->mfmh_bump &&
-	    !__mfm_block_is_allocated(arena, index + size)) {
+			!__mfm_block_is_allocated(arena, index + size)) {
 		size_t next  = index + size;
 		size_t nsize = __mfm_block_size(arena, next);
 
@@ -897,7 +890,6 @@
 {
 	struct mfm_arena *arena = os_atomic_load(&mfm_arena, dependency);
 
-
 	return __mfm_address_owned(arena, ptr);
 }
 
@@ -934,7 +926,7 @@
 			size_t size  = __mfm_block_size(arena, index);
 
 			P("  [%p, %p) size=%zd\n",
-			    blk, blk + size, size * MFM_QUANTUM);
+					blk, blk + size, size * MFM_QUANTUM);
 		}
 	}
 	P("\n");