Loading...
libkern/c++/OSRuntime.cpp xnu-4570.71.2 xnu-1504.7.4
--- xnu/xnu-4570.71.2/libkern/c++/OSRuntime.cpp
+++ xnu/xnu-1504.7.4/libkern/c++/OSRuntime.cpp
@@ -33,7 +33,6 @@
 #include <libkern/c++/OSKext.h>
 #include <libkern/c++/OSLib.h>
 #include <libkern/c++/OSSymbol.h>
-#include <IOKit/IOKitDebug.h>
 
 #include <sys/cdefs.h>
 
@@ -74,6 +73,7 @@
         }                                                     \
     } while (0)
 
+
 #if PRAGMA_MARK
 #pragma mark kern_os Allocator Package
 #endif /* PRAGMA_MARK */
@@ -87,28 +87,36 @@
 extern int debug_iomalloc_size;
 #endif
 
+struct _mhead {
+    size_t  mlen;
+    char    dat[0];
+};
+
 /*********************************************************************
 *********************************************************************/
 void *
 kern_os_malloc(size_t size)
 {
-    void *mem;
+    struct _mhead * mem;
+    size_t          memsize = sizeof (*mem) + size ;
+
     if (size == 0) {
         return (0);
     }
 
-    mem = kallocp_tag_bt((vm_size_t *)&size, VM_KERN_MEMORY_LIBKERN);
+    mem = (struct _mhead *)kalloc(memsize);
     if (!mem) {
         return (0);
     }
 
 #if OSALLOCDEBUG
-    OSAddAtomic(size, &debug_iomalloc_size);
+    debug_iomalloc_size += memsize;
 #endif
 
-    bzero(mem, size);
-
-    return mem;
+    mem->mlen = memsize;
+    bzero(mem->dat, size);
+
+    return mem->dat;
 }
 
 /*********************************************************************
@@ -116,13 +124,24 @@
 void
 kern_os_free(void * addr)
 {
-    size_t size;
-    size = kalloc_size(addr);
+    struct _mhead * hdr;
+
+    if (!addr) {
+        return;
+    }
+
+    hdr = (struct _mhead *)addr; 
+    hdr--;
+
 #if OSALLOCDEBUG
-	OSAddAtomic(-size, &debug_iomalloc_size);
+    debug_iomalloc_size -= hdr->mlen;
 #endif
 
-    kfree_addr(addr);
+#if 0
+    memset((vm_offset_t)hdr, 0xbb, hdr->mlen);
+#else
+    kfree(hdr, hdr->mlen);
+#endif
 }
 
 /*********************************************************************
@@ -132,40 +151,60 @@
     void   * addr,
     size_t   nsize)
 {
-    void            *nmem;
-    size_t          osize;
+    struct _mhead * ohdr;
+    struct _mhead * nmem;
+    size_t          nmemsize, osize;
 
     if (!addr) {
         return (kern_os_malloc(nsize));
     }
 
-    osize = kalloc_size(addr);
+    ohdr = (struct _mhead *)addr;
+    ohdr--;
+    osize = ohdr->mlen - sizeof(*ohdr);
     if (nsize == osize) {
         return (addr);
     }
 
     if (nsize == 0) {
-        kfree_addr(addr);
+        kern_os_free(addr);
         return (0);
     }
 
-    nmem = kallocp_tag_bt((vm_size_t *)&nsize, VM_KERN_MEMORY_LIBKERN);
+    nmemsize = sizeof (*nmem) + nsize ;
+    nmem = (struct _mhead *) kalloc(nmemsize);
     if (!nmem){
-        kfree_addr(addr);
+        kern_os_free(addr);
         return (0);
     }
 
 #if OSALLOCDEBUG
-    OSAddAtomic((nsize - osize), &debug_iomalloc_size);
+    debug_iomalloc_size += (nmemsize - ohdr->mlen);
 #endif
 
+    nmem->mlen = nmemsize;
     if (nsize > osize) {
-        (void)memset((char *)nmem + osize, 0, nsize - osize);
-    }
-    (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
-    kfree_addr(addr);
-
-    return (nmem);
+        (void) memset(&nmem->dat[osize], 0, nsize - osize);
+    }
+    (void)memcpy(nmem->dat, ohdr->dat, (nsize > osize) ? osize : nsize);
+    kfree(ohdr, ohdr->mlen);
+
+    return (nmem->dat);
+}
+
+/*********************************************************************
+*********************************************************************/
+size_t
+kern_os_malloc_size(void * addr)
+{
+    struct _mhead * hdr;
+
+    if (!addr) {
+        return(0);
+    }
+
+    hdr = (struct _mhead *) addr; hdr--;
+    return hdr->mlen - sizeof (struct _mhead);
 }
 
 #if PRAGMA_MARK
@@ -186,41 +225,6 @@
 typedef void (*structor_t)(void);
 
 /*********************************************************************
-*********************************************************************/
-static boolean_t
-sectionIsDestructor(kernel_section_t * section)
-{
-    boolean_t result;
-
-    result = !strncmp(section->sectname, SECT_MODTERMFUNC,
-        sizeof(SECT_MODTERMFUNC) - 1);
-#if !__LP64__
-    result = result || !strncmp(section->sectname, SECT_DESTRUCTOR, 
-        sizeof(SECT_DESTRUCTOR) - 1);
-#endif
-
-    return result;
-}
-
-/*********************************************************************
-*********************************************************************/
-static boolean_t
-sectionIsConstructor(kernel_section_t * section)
-{
-    boolean_t result;
-
-    result = !strncmp(section->sectname, SECT_MODINITFUNC,
-        sizeof(SECT_MODINITFUNC) - 1);
-#if !__LP64__
-    result = result || !strncmp(section->sectname, SECT_CONSTRUCTOR, 
-        sizeof(SECT_CONSTRUCTOR) - 1);
-#endif
-
-    return result;
-}
-
-
-/*********************************************************************
 * OSRuntimeUnloadCPPForSegment()
 *
 * Given a pointer to a mach object segment, iterate the segment to
@@ -245,7 +249,9 @@
          section != 0;
          section = nextsect(segment, section)) {
 
-        if (sectionIsDestructor(section)) {
+        if (strncmp(section->sectname, SECT_DESTRUCTOR, 
+            sizeof(SECT_DESTRUCTOR)) == 0) {
+
             structor_t * destructors = (structor_t *)section->addr;
 
             if (destructors) {
@@ -267,7 +273,7 @@
         } /* if (strncmp...) */
     } /* for (section...) */
 
-    OSSafeReleaseNULL(theKext);
+    OSSafeRelease(theKext);
     return;
 }
 
@@ -352,7 +358,7 @@
     }
     result = KMOD_RETURN_SUCCESS;
 finish:
-    OSSafeReleaseNULL(theKext);
+    OSSafeRelease(theKext);
     return result;
 }
 
@@ -373,7 +379,7 @@
     kernel_segment_command_t * segment         = NULL;  // do not free
     kernel_segment_command_t * failure_segment = NULL;  // do not free
 
-    if (!kmodInfo || !kmodInfo->address) {
+    if (!kmodInfo || !kmodInfo->address || !kmodInfo->name) {
         result = kOSKextReturnInvalidArgument;
         goto finish;
     }
@@ -416,7 +422,9 @@
              section != NULL;
              section = nextsect(segment, section)) {
 
-            if (sectionIsConstructor(section)) {
+            if (strncmp(section->sectname, SECT_CONSTRUCTOR, 
+                sizeof(SECT_CONSTRUCTOR)) == 0) {
+
                 structor_t * constructors = (structor_t *)section->addr;
 
                 if (constructors) {
@@ -485,7 +493,7 @@
         theKext->setCPPInitialized(true);
     }
 finish:
-    OSSafeReleaseNULL(theKext);
+    OSSafeRelease(theKext);
     return result;
 }
 
@@ -498,15 +506,17 @@
 
 /*********************************************************************
 *********************************************************************/
+extern lck_spin_t  gOSObjectTrackLock;
 extern lck_grp_t * IOLockGroup;
 extern kmod_info_t g_kernel_kmod_info;
 
 void OSlibkernInit(void)
 {
+    lck_spin_init(&gOSObjectTrackLock, IOLockGroup, LCK_ATTR_NULL);
+ 
     // This must be called before calling OSRuntimeInitializeCPP.
     OSMetaClassBase::initialize();
     
-    g_kernel_kmod_info.address = (vm_address_t) &_mh_execute_header;
     if (kOSReturnSuccess != OSRuntimeInitializeCPP(&g_kernel_kmod_info, 0)) {
         panic("OSRuntime: C++ runtime failed to initialize.");
     }
@@ -526,9 +536,6 @@
 *********************************************************************/
 void *
 operator new(size_t size)
-#if __cplusplus >= 201103L
-								noexcept
-#endif
 {
     void * result;
 
@@ -538,9 +545,6 @@
 
 void
 operator delete(void * addr)
-#if __cplusplus >= 201103L
-								noexcept
-#endif
 {
     kern_os_free(addr);
     return;
@@ -548,9 +552,6 @@
 
 void *
 operator new[](unsigned long sz)
-#if __cplusplus >= 201103L
-								noexcept
-#endif
 {
     if (sz == 0) sz = 1;
     return kern_os_malloc(sz);
@@ -558,9 +559,6 @@
 
 void
 operator delete[](void * ptr)
-#if __cplusplus >= 201103L
-								noexcept
-#endif
 {
     if (ptr) {
         kern_os_free(ptr);