Loading...
libkern/c++/OSMetaClass.cpp xnu-12377.101.15 xnu-4903.270.47
--- xnu/xnu-12377.101.15/libkern/c++/OSMetaClass.cpp
+++ xnu/xnu-4903.270.47/libkern/c++/OSMetaClass.cpp
@@ -60,7 +60,6 @@
 #include <kern/thread_call.h>
 #include <kern/host.h>
 #include <mach/mach_interface.h>
-#include <stddef.h>
 
 #if PRAGMA_MARK
 #pragma mark Macros
@@ -129,7 +128,7 @@
 /*********************************************************************
 * Reserved vtable functions.
 *********************************************************************/
-#if defined(__arm64__) || defined(__arm__)
+#if SLOT_USED
 void
 OSMetaClassBase::_RESERVEDOSMetaClassBase0()
 {
@@ -145,14 +144,14 @@
 {
 	panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 2);
 }
+#endif /* SLOT_USED */
+
+// As these slots are used move them up inside the #if above
 void
 OSMetaClassBase::_RESERVEDOSMetaClassBase3()
 {
 	panic("OSMetaClassBase::_RESERVEDOSMetaClassBase%d called.", 3);
 }
-#endif /* defined(__arm64__) || defined(__arm__) */
-
-// As these slots are used move them up inside the #if above
 void
 OSMetaClassBase::_RESERVEDOSMetaClassBase4()
 {
@@ -170,14 +169,13 @@
 }
 #endif
 
+
 /*********************************************************************
 *********************************************************************/
 
 #if defined(__arm__) || defined(__arm64__)
 
-#if defined(HAS_APPLE_PAC)
-#include <ptrauth.h>
-#endif /* defined(HAS_APPLE_PAC) */
+
 
 /*
  *  IHI0059A "C++ Application Binary Interface Standard for the ARM 64 - bit Architecture":
@@ -196,15 +194,9 @@
  */
 
 OSMetaClassBase::_ptf_t
-#if defined(HAS_APPLE_PAC) && \
-        __has_feature(ptrauth_member_function_pointer_type_discrimination)
-OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase *self __attribute__((unused)),
-    void (OSMetaClassBase::*func)(void))
-#else
-OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase *self,
-    void (OSMetaClassBase::*func)(void))
-#endif
-{
+OSMetaClassBase::_ptmf2ptf(const OSMetaClassBase *self, void (OSMetaClassBase::*func)(void))
+{
+	typedef long int ptrdiff_t;
 	struct ptmf_t {
 		_ptf_t fPFN;
 		ptrdiff_t delta;
@@ -218,20 +210,6 @@
 	map.fIn = func;
 	pfn     = map.pTMF.fPFN;
 
-#if defined(HAS_APPLE_PAC) && \
-	__has_feature(ptrauth_member_function_pointer_type_discrimination)
-	// Authenticate 'pfn' using the member function pointer type discriminator
-	// and resign it as a C function pointer. 'pfn' can point to either a
-	// non-virtual function or a virtual member function thunk.
-	// It can also be NULL.
-	if (pfn) {
-		pfn = ptrauth_auth_and_resign(pfn, ptrauth_key_function_pointer,
-		    ptrauth_type_discriminator(__typeof__(func)),
-		    ptrauth_key_function_pointer,
-		    ptrauth_function_pointer_type_discriminator(_ptf_t));
-	}
-	return pfn;
-#else
 	if (map.pTMF.delta & 1) {
 		// virtual
 		union {
@@ -241,39 +219,12 @@
 		u.fObj = self;
 
 		// Virtual member function so dereference table
-#if defined(HAS_APPLE_PAC)
-		// The entity hash is stored in the top 32-bits of the vtable offset of a
-		// member function pointer.
-		uint32_t entity_hash = ((uintptr_t)pfn) >> 32;
-		pfn = (_ptf_t)(((uintptr_t) pfn) & 0xFFFFFFFF);
-
-#if __has_builtin(__builtin_get_vtable_pointer)
-		const _ptf_t *vtablep =
-		    (const _ptf_t *)__builtin_get_vtable_pointer(u.fObj);
-#else
-		// Authenticate the vtable pointer.
-		const _ptf_t *vtablep = ptrauth_auth_data(*u.vtablep,
-		    ptrauth_key_cxx_vtable_pointer, 0);
-#endif
-		// Calculate the address of the vtable entry.
-		_ptf_t *vtentryp = (_ptf_t *)(((uintptr_t)vtablep) + (uintptr_t)pfn);
-		// Load the pointer from the vtable entry.
-		pfn = *vtentryp;
-
-		// Finally, resign the vtable entry as a function pointer.
-		uintptr_t auth_data = ptrauth_blend_discriminator(vtentryp, entity_hash);
-		pfn = ptrauth_auth_and_resign(pfn, ptrauth_key_function_pointer,
-		    auth_data, ptrauth_key_function_pointer,
-		    ptrauth_function_pointer_type_discriminator(_ptf_t));
-#else /* defined(HAS_APPLE_PAC) */
 		pfn = *(_ptf_t *)(((uintptr_t)*u.vtablep) + (uintptr_t)pfn);
-#endif /* !defined(HAS_APPLE_PAC) */
 		return pfn;
 	} else {
 		// Not virtual, i.e. plain member func
 		return pfn;
 	}
-#endif
 }
 
 #endif /* defined(__arm__) || defined(__arm64__) */
@@ -292,32 +243,7 @@
 	const OSMetaClassBase * me,
 	const OSMetaClass     * toType)
 {
-	return (me)? me->metaCast(toType) : NULL;
-}
-
-/// A helper function to crash with a kernel panic.
-__attribute__((cold, not_tail_called, noreturn))
-static inline void
-panic_crash_fail_cast(const OSMetaClassBase *me,
-    const OSMetaClass *toType)
-{
-	panic("Unexpected cast fail: from %p to %p", me, toType);
-	__builtin_unreachable();
-}
-
-OSMetaClassBase *
-OSMetaClassBase::requiredMetaCast(
-	const OSMetaClassBase * me,
-	const OSMetaClass     * toType)
-{
-	if (!me) {
-		return NULL;
-	}
-	OSMetaClassBase *tmp = safeMetaCast(me, toType);
-	if (!tmp) {
-		panic_crash_fail_cast(me, toType);
-	}
-	return tmp;
+	return (me)? me->metaCast(toType) : 0;
 }
 
 /*********************************************************************
@@ -328,7 +254,7 @@
 	const OSMetaClassBase * typeinst)
 {
 	const OSMetaClass * toType = OSTypeIDInst(typeinst);
-	return typeinst && inst && (NULL != inst->metaCast(toType));
+	return typeinst && inst && (0 != inst->metaCast(toType));
 }
 
 /*********************************************************************
@@ -401,7 +327,7 @@
 OSMetaClassBase::metaCast(const OSString * toMetaStr) const
 {
 	const OSSymbol  * tempSymb = OSSymbol::withString(toMetaStr);
-	OSMetaClassBase * ret = NULL;
+	OSMetaClassBase * ret = 0;
 	if (tempSymb) {
 		ret = metaCast(tempSymb);
 		tempSymb->release();
@@ -415,7 +341,7 @@
 OSMetaClassBase::metaCast(const char * toMetaCStr) const
 {
 	const OSSymbol  * tempSymb = OSSymbol::withCString(toMetaCStr);
-	OSMetaClassBase * ret = NULL;
+	OSMetaClassBase * ret = 0;
 	if (tempSymb) {
 		ret = metaCast(tempSymb);
 		tempSymb->release();
@@ -433,16 +359,16 @@
 {
 public:
 	OSMetaClassMeta();
-	OSObject * alloc() const override;
+	OSObject * alloc() const;
 };
 OSMetaClassMeta::OSMetaClassMeta()
-	: OSMetaClass("OSMetaClass", NULL, sizeof(OSMetaClass))
+	: OSMetaClass("OSMetaClass", 0, sizeof(OSMetaClass))
 {
 }
 OSObject *
 OSMetaClassMeta::alloc() const
 {
-	return NULL;
+	return 0;
 }
 
 static OSMetaClassMeta sOSMetaClassMeta;
@@ -570,7 +496,6 @@
 * registration, and OSMetaClass::postModLoad(), which actually
 * records all the class/kext relationships of the new MetaClasses.
 *********************************************************************/
-
 OSMetaClass::OSMetaClass(
 	const char        * inClassName,
 	const OSMetaClass * inSuperClass,
@@ -580,7 +505,8 @@
 	classSize = inClassSize;
 	superClassLink = inSuperClass;
 
-	reserved = IOMallocType(ExpansionData);
+	reserved = IONew(ExpansionData, 1);
+	bzero(reserved, sizeof(ExpansionData));
 #if IOTRACKING
 	uint32_t numSiteQs = 0;
 	if ((this == &OSSymbol    ::gMetaClass)
@@ -617,48 +543,32 @@
 		// Grow stalled array if neccessary
 		if (sStalled->count >= sStalled->capacity) {
 			OSMetaClass **oldStalled = sStalled->classes;
-			int oldCount = sStalled->capacity;
-			int newCount = oldCount + kKModCapacityIncrement;
-
-			sStalled->classes = kalloc_type_tag(OSMetaClass *, newCount,
-			    Z_WAITOK_ZERO, VM_KERN_MEMORY_OSKEXT);
+			int oldSize = sStalled->capacity * sizeof(OSMetaClass *);
+			int newSize = oldSize
+			    + kKModCapacityIncrement * sizeof(OSMetaClass *);
+
+			sStalled->classes = (OSMetaClass **)kalloc_tag(newSize, VM_KERN_MEMORY_OSKEXT);
 			if (!sStalled->classes) {
 				sStalled->classes = oldStalled;
 				sStalled->result = kOSMetaClassNoTempData;
 				return;
 			}
 
-			sStalled->capacity = newCount;
-			memmove(sStalled->classes, oldStalled,
-			    sizeof(OSMetaClass *) * oldCount);
-			kfree_type(OSMetaClass *, oldCount, oldStalled);
-			OSMETA_ACCUMSIZE(sizeof(OSMetaClass *) * (newCount - oldCount));
+			sStalled->capacity += kKModCapacityIncrement;
+			memmove(sStalled->classes, oldStalled, oldSize);
+			kfree(oldStalled, oldSize);
+			OSMETA_ACCUMSIZE(((size_t)newSize) - ((size_t)oldSize));
 		}
 
 		sStalled->classes[sStalled->count++] = this;
 	}
 }
 
-OSMetaClass::OSMetaClass(
-	const char        * inClassName,
-	const OSMetaClass * inSuperClass,
-	unsigned int        inClassSize,
-	zone_t            * inZone,
-	const char        * zone_name,
-	zone_create_flags_t zflags) : OSMetaClass(inClassName, inSuperClass,
-	    inClassSize)
-{
-	if (!(kIOTracking & gIOKitDebug)) {
-		*inZone  = zone_create(zone_name, inClassSize,
-		    (zone_create_flags_t) (ZC_ZFREE_CLEARMEM | zflags));
-	}
-}
-
 /*********************************************************************
 *********************************************************************/
 OSMetaClass::~OSMetaClass()
 {
-	OSKext * myKext = reserved->kext; // do not release
+	OSKext * myKext = reserved ? reserved->kext : 0; // do not release
 
 	/* Hack alert: 'className' is a C string during early C++ init, and
 	 * is converted to a real OSSymbol only when we record the OSKext in
@@ -713,7 +623,7 @@
 #if IOTRACKING
 	IOTrackingQueueFree(reserved->tracking);
 #endif
-	IOFreeType(reserved, ExpansionData);
+	IODelete(reserved, ExpansionData, 1);
 }
 
 /*********************************************************************
@@ -782,21 +692,23 @@
 	IOLockLock(sStalledClassesLock);
 
 	assert(sStalled == NULL);
-	sStalled = kalloc_type(StalledData, Z_WAITOK_ZERO_NOFAIL);
-
-	sStalled->classes = kalloc_type_tag(OSMetaClass *,
-	    kKModCapacityIncrement, Z_WAITOK_ZERO, VM_KERN_MEMORY_OSKEXT);
-	if (!sStalled->classes) {
-		kfree_type(StalledData, sStalled);
-		return NULL;
-	}
-	OSMETA_ACCUMSIZE((kKModCapacityIncrement * sizeof(OSMetaClass *)) +
-	    sizeof(*sStalled));
-
-	sStalled->result   = kOSReturnSuccess;
-	sStalled->capacity = kKModCapacityIncrement;
-	sStalled->count    = 0;
-	sStalled->kextIdentifier = kextIdentifier;
+	sStalled = (StalledData *)kalloc_tag(sizeof(*sStalled), VM_KERN_MEMORY_OSKEXT);
+	if (sStalled) {
+		sStalled->classes = (OSMetaClass **)
+		    kalloc_tag(kKModCapacityIncrement * sizeof(OSMetaClass *), VM_KERN_MEMORY_OSKEXT);
+		if (!sStalled->classes) {
+			kfree(sStalled, sizeof(*sStalled));
+			return 0;
+		}
+		OSMETA_ACCUMSIZE((kKModCapacityIncrement * sizeof(OSMetaClass *)) +
+		    sizeof(*sStalled));
+
+		sStalled->result   = kOSReturnSuccess;
+		sStalled->capacity = kKModCapacityIncrement;
+		sStalled->count    = 0;
+		sStalled->kextIdentifier = kextIdentifier;
+		bzero(sStalled->classes, kKModCapacityIncrement * sizeof(OSMetaClass *));
+	}
 
 	// keep sStalledClassesLock locked until postModLoad
 
@@ -818,8 +730,8 @@
 OSMetaClass::postModLoad(void * loadHandle)
 {
 	OSReturn         result     = kOSReturnSuccess;
-	OSSymbol       * myKextName = NULL;// must release
-	OSKext         * myKext     = NULL;// must release
+	OSSymbol       * myKextName = 0;// must release
+	OSKext         * myKext     = 0;// must release
 
 	if (!sStalled || loadHandle != sStalled) {
 		result = kOSMetaClassInternal;
@@ -885,15 +797,15 @@
 					/* Log this error here so we can include the class name.
 					 * xxx - we should look up the other kext that defines the class
 					 */
-#if defined(XNU_TARGET_OS_OSX)
+#if CONFIG_EMBEDDED
+					panic(
+#else
 					OSKextLog(myKext, kOSMetaClassLogSpec,
-#else
-					panic(
-#endif /* defined(XNU_TARGET_OS_OSX) */
-					    "OSMetaClass: Kext %s class %s is a duplicate;"
-					    "kext %s already has a class by that name.",
-					    sStalled->kextIdentifier, (const char *)me->className,
-					    ((OSKext *)orig->reserved->kext)->getIdentifierCString());
+#endif /* CONFIG_EMBEDDED */
+						"OSMetaClass: Kext %s class %s is a duplicate;"
+						"kext %s already has a class by that name.",
+						sStalled->kextIdentifier, (const char *)me->className,
+						((OSKext *)orig->reserved->kext)->getIdentifierCString());
 					result = kOSMetaClassDuplicateClass;
 					break;
 				}
@@ -968,9 +880,9 @@
 	if (sStalled) {
 	        OSMETA_ACCUMSIZE(-(sStalled->capacity * sizeof(OSMetaClass *) +
 	            sizeof(*sStalled)));
-	        kfree_type(OSMetaClass *, sStalled->capacity, sStalled->classes);
-	        kfree_type(StalledData, sStalled);
-	        sStalled = NULL;
+	        kfree(sStalled->classes, sStalled->capacity * sizeof(OSMetaClass *));
+	        kfree(sStalled, sizeof(*sStalled));
+	        sStalled = 0;
 	}
 
 	IOLockUnlock(sStalledClassesLock);
@@ -1076,7 +988,7 @@
 			}
                         IOLockLock(sAllClassesLock);
                         reserved->instances->release();
-                        reserved->instances = NULL;
+                        reserved->instances = 0;
                         IOLockUnlock(sAllClassesLock);
 		}
 	}
@@ -1105,7 +1017,7 @@
 
         maxDepth = sDeepestClass;
         if (maxDepth > kLocalDepth) {
-                nextIndex = IONewData(typeof(nextIndex[0]), maxDepth);
+                nextIndex = IONew(typeof(nextIndex[0]), maxDepth);
                 sets      = IONew(typeof(sets[0]), maxDepth);
 	}
         done = false;
@@ -1137,7 +1049,7 @@
 		}
 	}while (!done);
         if (maxDepth > kLocalDepth) {
-                IODeleteData(nextIndex, typeof(nextIndex[0]), maxDepth);
+                IODelete(nextIndex, typeof(nextIndex[0]), maxDepth);
                 IODelete(sets, typeof(sets[0]), maxDepth);
 	}
 }
@@ -1160,7 +1072,7 @@
 	void * context)
 {
         OSMetaClass  * meta;
-        OSOrderedSet * set = NULL;
+        OSOrderedSet * set = 0;
 
         IOLockLock(sAllClassesLock);
         if (sAllClassesDict
@@ -1232,10 +1144,10 @@
 const OSMetaClass *
 OSMetaClass::getMetaClassWithName(const OSSymbol * name)
 {
-        OSMetaClass * retMeta = NULL;
+        OSMetaClass * retMeta = 0;
 
         if (!name) {
-                return NULL;
+                return 0;
 	}
 
         IOLockLock(sAllClassesLock);
@@ -1255,10 +1167,10 @@
         const OSMetaClass * meta;
 
         if (!name) {
-                return NULL;
-	}
-
-        meta = NULL;
+                return 0;
+	}
+
+        meta = 0;
         IOLockLock(sAllClassesLock);
         if (sAllClassesDict) {
                 meta = (OSMetaClass *) sAllClassesDict->getObject(name);
@@ -1287,7 +1199,7 @@
         const OSMetaClass * meta;
         OSObject          * result;
 
-        result = NULL;
+        result = 0;
         meta = copyMetaClassWithName(name);
         if (meta) {
                 result = meta->alloc();
@@ -1327,7 +1239,7 @@
 	const OSSymbol        * name,
 	const OSMetaClassBase * in)
 {
-        OSMetaClassBase * result = NULL;
+        OSMetaClassBase * result = 0;
 
         const OSMetaClass * const meta = getMetaClassWithName(name);
 
@@ -1393,12 +1305,11 @@
 		}
 	}
 
-        return NULL;
-}
-
-/*********************************************************************
-*********************************************************************/
-__dead2
+        return 0;
+}
+
+/*********************************************************************
+*********************************************************************/
 void
 OSMetaClass::reservedCalled(int ind) const
 {
@@ -1416,23 +1327,16 @@
 }
 
 /*********************************************************************
+* xxx - I want to rename this :-/
 *********************************************************************/
 const OSSymbol *
 OSMetaClass::getKmodName() const
 {
-        OSKext * myKext = reserved ? reserved->kext : NULL;
+        OSKext * myKext = reserved ? reserved->kext : 0;
         if (myKext) {
                 return myKext->getIdentifier();
 	}
         return OSSymbol::withCStringNoCopy("unknown");
-}
-
-/*********************************************************************
-*********************************************************************/
-OSKext *
-OSMetaClass::getKext() const
-{
-        return reserved ? reserved->kext : NULL;
 }
 
 /*********************************************************************
@@ -1478,8 +1382,8 @@
 OSDictionary *
 OSMetaClass::getClassDictionary()
 {
-        panic("OSMetaClass::getClassDictionary() is obsoleted.");
-        return NULL;
+        panic("OSMetaClass::getClassDictionary() is obsoleted.\n");
+        return 0;
 }
 
 /*********************************************************************
@@ -1487,7 +1391,7 @@
 bool
 OSMetaClass::serialize(__unused OSSerialize * s) const
 {
-        panic("OSMetaClass::serialize(): Obsoleted");
+        panic("OSMetaClass::serialize(): Obsoleted\n");
         return false;
 }
 
@@ -1545,15 +1449,12 @@
 
 #if IOTRACKING
 
-__typed_allocators_ignore_push
-
 void *
 OSMetaClass::trackedNew(size_t size)
 {
         IOTracking * mem;
 
-        mem = (typeof(mem))kheap_alloc(KHEAP_DEFAULT, size + sizeof(IOTracking),
-            Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
+        mem = (typeof(mem))kalloc_tag_bt(size + sizeof(IOTracking), VM_KERN_MEMORY_LIBKERN);
         assert(mem);
         if (!mem) {
                 return mem;
@@ -1572,11 +1473,9 @@
 {
         IOTracking * mem = (typeof(mem))instance; mem--;
 
-        kheap_free(KHEAP_DEFAULT, mem, size + sizeof(IOTracking));
+        kfree(mem, size + sizeof(IOTracking));
         OSIVAR_ACCUMSIZE(-size);
 }
-
-__typed_allocators_ignore_pop
 
 void
 OSMetaClass::trackedInstance(OSObject * instance) const