Loading...
--- libmalloc/libmalloc-374.120.1/src/base.h
+++ libmalloc/libmalloc-715.140.5/src/base.h
@@ -24,6 +24,12 @@
 #ifndef __BASE_H
 #define __BASE_H
 
+#include <stddef.h>
+#include "platform.h"
+
+#include <malloc/_ptrcheck.h>
+__ptrcheck_abi_assume_single()
+
 #ifndef __has_extension
 #define __has_extension(x) 0
 #endif
@@ -52,7 +58,7 @@
 		MALLOC_FATAL_ERROR((cause), message); \
 })
 
-#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
+#if __has_include(<machine/cpu_capabilities.h>) && (defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__))
 #   define __APPLE_API_PRIVATE
 #   include <machine/cpu_capabilities.h>
 #   if defined(__i386__) || defined(__x86_64__)
@@ -61,7 +67,7 @@
 #      define _COMM_PAGE_VERSION_REQD 3
 #   endif
 #   undef __APPLE_API_PRIVATE
-#else
+#elif __has_include(<sys/sysctl.h>)
 #   include <sys/sysctl.h>
 #endif
 
@@ -71,14 +77,7 @@
 #   define MALLOC_CACHE_LINE 128
 #   define MALLOC_NANO_CACHE_LINE 64
 #elif defined(__arm__) || defined(__arm64__)
-#	if TARGET_OS_OSX || TARGET_OS_DRIVERKIT || TARGET_OS_SIMULATOR
-// To make zone structure layout match to support introspecting a Rosetta process
-// from a native process or vice versa on macOS. See comment in
-// quarantine_diagnose_fault_from_crash_reporter.
-#   	define MALLOC_CACHE_LINE 128
-#	else
-#   	define MALLOC_CACHE_LINE 64
-#	endif
+# 	define MALLOC_CACHE_LINE 128
 #   define MALLOC_NANO_CACHE_LINE 64
 #else
 #   define MALLOC_CACHE_LINE 32
@@ -95,11 +94,31 @@
 #define MALLOC_PACKED __attribute__((packed))
 #define MALLOC_USED __attribute__((used))
 #define MALLOC_UNUSED __attribute__((unused))
+#define MALLOC_NORETURN __attribute__((noreturn))
+#define MALLOC_COLD __attribute__((cold))
+#define MALLOC_NOESCAPE __attribute__((noescape))
+#define MALLOC_PRESERVE_MOST __attribute__((preserve_most))
+#define MALLOC_FALLTHROUGH __attribute__((fallthrough))
 #define CHECK_MAGAZINE_PTR_LOCKED(szone, mag_ptr, fun) {}
+
+#if __has_feature(bounds_safety)
+#define __malloc_bidi_indexable __bidi_indexable
+#else
+#define __malloc_bidi_indexable
+#endif
 
 #define SCRIBBLE_BYTE 0xaa /* allocated scribble */
 #define SCRABBLE_BYTE 0x55 /* free()'d scribble */
 #define SCRUBBLE_BYTE 0xdd /* madvise(..., MADV_FREE) scriblle */
+
+#undef KiB
+#undef MiB
+#undef GiB
+#undef TiB
+#define KiB(x) ((uint64_t)(x) << 10)
+#define MiB(x) ((uint64_t)(x) << 20)
+#define GiB(x) ((uint64_t)(x) << 30)
+#define TiB(x) ((uint64_t)(x) << 40)
 
 #define NDEBUG 1
 #define trunc_page_quanta(x) trunc_page((x))
@@ -121,6 +140,37 @@
 #define trunc_large_page_quanta(x) ((x) & (~large_vm_page_quanta_mask))
 #define round_large_page_quanta(x) (trunc_large_page_quanta((x) + large_vm_page_quanta_mask))
 
+/*
+ * MALLOC_ABSOLUTE_MAX_SIZE - There are many instances of addition to a
+ * user-specified size_t, which can cause overflow (and subsequent crashes)
+ * for values near SIZE_T_MAX.  Rather than add extra "if" checks everywhere
+ * this occurs, it is easier to just set an absolute maximum request size,
+ * and immediately return an error if the requested size exceeds this maximum.
+ * Of course, values less than this absolute max can fail later if the value
+ * is still too large for the available memory.  The largest value added
+ * seems to be large_vm_page_quanta_size (in the macro round_large_page_quanta()), so to be safe, we set
+ * the maximum to be 2 * PAGE_SIZE less than SIZE_T_MAX.
+ *
+ * This value needs to be calculated at runtime, so we'll cache it rather than
+ * recalculate on each use.
+ */
+#define _MALLOC_ABSOLUTE_MAX_SIZE (SIZE_T_MAX - (2 * large_vm_page_quanta_size))
+
+#if defined(MALLOC_BUILDING_XCTESTS)
+#define malloc_absolute_max_size _MALLOC_ABSOLUTE_MAX_SIZE
+#else
+extern size_t malloc_absolute_max_size; // caches the definition above
+#endif
+
+#if !MALLOC_TARGET_EXCLAVES
+#define malloc_too_large(n) ((n) > malloc_absolute_max_size)
+#else
+#define malloc_too_large(n) (0)
+#endif // !MALLOC_TARGET_EXCLAVES
+
+#if MALLOC_TARGET_EXCLAVES && !defined(MAX)
+#define MAX(a, b) (((a)>(b))?(a):(b))
+#endif // MALLOC_TARGET_EXCLAVES && !defined(MAX)
 
 // add a guard page before each VM region to help debug
 #define MALLOC_ADD_PRELUDE_GUARD_PAGE (1 << 0)
@@ -144,6 +194,26 @@
 #define MALLOC_PURGEABLE (1 << 7)
 // call abort() on malloc errors, but not on out of memory.
 #define MALLOC_ABORT_ON_CORRUPTION (1 << 8)
+// don't populate the mapping for this allocation
+#define MALLOC_NO_POPULATE (1 << 9)
+// enable faulting anywhere within this allocation
+#define MALLOC_CAN_FAULT (1 << 12)
+
+// See malloc_implementation.h
+// MALLOC_MSL_LITE_WRAPPED_ZONE_FLAGS == (1 << 10)
+
+
+/*
+ * These commpage routines provide fast access to the logical cpu number
+ * of the calling processor assuming no pre-emption occurs.
+ */
+
+extern unsigned int hyper_shift;
+extern unsigned int logical_ncpus;
+extern unsigned int phys_ncpus;
+#if CONFIG_CLUSTER_AWARE
+extern unsigned int ncpuclusters;
+#endif // CONFIG_CLUSTER_AWARE
 
 /*
  * msize - a type to refer to the number of quanta of a tiny or small
@@ -158,6 +228,6 @@
 typedef struct rack_s rack_t;
 typedef struct magazine_s magazine_t;
 typedef int mag_index_t;
-typedef void *region_t;
+typedef void * __single region_t;
 
 #endif // __BASE_H