Loading...
--- libmalloc/libmalloc-715.100.22/src/early_malloc.c
+++ libmalloc/libmalloc-646.0.13/src/early_malloc.c
@@ -95,7 +95,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,28 +642,33 @@
 mfm_initialize(void)
 {
 	struct mfm_arena *arena;
-	int debug_flags;
 #if MALLOC_TARGET_EXCLAVES
 	plat_map_t map = {0};
-	debug_flags = MALLOC_NO_POPULATE;
+#endif // MALLOC_TARGET_EXCLAVES
+
+
+#if MALLOC_TARGET_EXCLAVES
+	arena = mvm_allocate_pages_plat(MFM_ARENA_SIZE, 0, MALLOC_NO_POPULATE,
+			VM_MEMORY_MALLOC, mvm_plat_map(map));
 #else
-	int alloc_flags = 0;
-	debug_flags = DISABLE_ASLR | MALLOC_ADD_GUARD_PAGE_FLAGS;
+	/* 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
 
-
-	/* this is called early, which means the address space _does_ have 8M */
-	arena = mvm_allocate_pages_plat(MFM_ARENA_SIZE, 0, debug_flags,
-			VM_MEMORY_MALLOC, mvm_plat_map(map));
 	if (arena == NULL) {
 		MFM_INTERNAL_CRASH(arena, "failed to allocate memory");
 	}
 
 #if MALLOC_TARGET_EXCLAVES
 	/* populate the header up to the block storage */
-	mvm_madvise_plat(arena,
-			roundup(offsetof(struct mfm_arena, mfm_blocks), PAGE_SIZE),
-			MADV_FAULTABLE, MALLOC_ABORT_ON_ERROR, mvm_plat_map(map));
+	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");
+	}
 
 	arena->mfm_header.mfm_map = map;
 #else
@@ -673,7 +678,8 @@
 	 * 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;
-	alloc_flags |= VM_FLAGS_OVERWRITE | VM_MAKE_TAG(VM_MEMORY_MALLOC_TINY);
+	int 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,
@@ -763,7 +769,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);
@@ -793,9 +799,12 @@
 			const uintptr_t end = roundup((uintptr_t)ptr + alloc_size, PAGE_SIZE);
 			const size_t bytes = end - begin;
 			if (bytes) {
-				mvm_madvise_plat((void*)begin, bytes, MADV_FAULTABLE,
-						MALLOC_ABORT_ON_ERROR,
+				const uintptr_t addr = (uintptr_t)mvm_allocate_plat(begin,
+						bytes, 0, VM_FLAGS_FIXED, 0, 0,
 						mvm_plat_map(arena->mfm_header.mfm_map));
+				if (addr != begin) {
+					MFM_INTERNAL_CRASH(ptr, "populate of pages failed");
+				}
 			}
 #endif
 
@@ -863,7 +872,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);
 
@@ -888,6 +897,7 @@
 {
 	struct mfm_arena *arena = os_atomic_load(&mfm_arena, dependency);
 
+
 	return __mfm_address_owned(arena, ptr);
 }
 
@@ -924,7 +934,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");