Loading...
--- xnu/xnu-1504.7.4/libkern/c++/OSRuntime.cpp
+++ xnu/xnu-4570.71.2/libkern/c++/OSRuntime.cpp
@@ -33,6 +33,7 @@
#include <libkern/c++/OSKext.h>
#include <libkern/c++/OSLib.h>
#include <libkern/c++/OSSymbol.h>
+#include <IOKit/IOKitDebug.h>
#include <sys/cdefs.h>
@@ -73,7 +74,6 @@
} \
} while (0)
-
#if PRAGMA_MARK
#pragma mark kern_os Allocator Package
#endif /* PRAGMA_MARK */
@@ -87,36 +87,28 @@
extern int debug_iomalloc_size;
#endif
-struct _mhead {
- size_t mlen;
- char dat[0];
-};
-
/*********************************************************************
*********************************************************************/
void *
kern_os_malloc(size_t size)
{
- struct _mhead * mem;
- size_t memsize = sizeof (*mem) + size ;
-
+ void *mem;
if (size == 0) {
return (0);
}
- mem = (struct _mhead *)kalloc(memsize);
+ mem = kallocp_tag_bt((vm_size_t *)&size, VM_KERN_MEMORY_LIBKERN);
if (!mem) {
return (0);
}
#if OSALLOCDEBUG
- debug_iomalloc_size += memsize;
-#endif
-
- mem->mlen = memsize;
- bzero(mem->dat, size);
-
- return mem->dat;
+ OSAddAtomic(size, &debug_iomalloc_size);
+#endif
+
+ bzero(mem, size);
+
+ return mem;
}
/*********************************************************************
@@ -124,24 +116,13 @@
void
kern_os_free(void * addr)
{
- struct _mhead * hdr;
-
- if (!addr) {
- return;
- }
-
- hdr = (struct _mhead *)addr;
- hdr--;
-
+ size_t size;
+ size = kalloc_size(addr);
#if OSALLOCDEBUG
- debug_iomalloc_size -= hdr->mlen;
-#endif
-
-#if 0
- memset((vm_offset_t)hdr, 0xbb, hdr->mlen);
-#else
- kfree(hdr, hdr->mlen);
-#endif
+ OSAddAtomic(-size, &debug_iomalloc_size);
+#endif
+
+ kfree_addr(addr);
}
/*********************************************************************
@@ -151,60 +132,40 @@
void * addr,
size_t nsize)
{
- struct _mhead * ohdr;
- struct _mhead * nmem;
- size_t nmemsize, osize;
+ void *nmem;
+ size_t osize;
if (!addr) {
return (kern_os_malloc(nsize));
}
- ohdr = (struct _mhead *)addr;
- ohdr--;
- osize = ohdr->mlen - sizeof(*ohdr);
+ osize = kalloc_size(addr);
if (nsize == osize) {
return (addr);
}
if (nsize == 0) {
- kern_os_free(addr);
+ kfree_addr(addr);
return (0);
}
- nmemsize = sizeof (*nmem) + nsize ;
- nmem = (struct _mhead *) kalloc(nmemsize);
+ nmem = kallocp_tag_bt((vm_size_t *)&nsize, VM_KERN_MEMORY_LIBKERN);
if (!nmem){
- kern_os_free(addr);
+ kfree_addr(addr);
return (0);
}
#if OSALLOCDEBUG
- debug_iomalloc_size += (nmemsize - ohdr->mlen);
-#endif
-
- nmem->mlen = nmemsize;
+ OSAddAtomic((nsize - osize), &debug_iomalloc_size);
+#endif
+
if (nsize > osize) {
- (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);
+ (void)memset((char *)nmem + osize, 0, nsize - osize);
+ }
+ (void)memcpy(nmem, addr, (nsize > osize) ? osize : nsize);
+ kfree_addr(addr);
+
+ return (nmem);
}
#if PRAGMA_MARK
@@ -223,6 +184,41 @@
#endif
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()
@@ -249,9 +245,7 @@
section != 0;
section = nextsect(segment, section)) {
- if (strncmp(section->sectname, SECT_DESTRUCTOR,
- sizeof(SECT_DESTRUCTOR)) == 0) {
-
+ if (sectionIsDestructor(section)) {
structor_t * destructors = (structor_t *)section->addr;
if (destructors) {
@@ -273,7 +267,7 @@
} /* if (strncmp...) */
} /* for (section...) */
- OSSafeRelease(theKext);
+ OSSafeReleaseNULL(theKext);
return;
}
@@ -358,7 +352,7 @@
}
result = KMOD_RETURN_SUCCESS;
finish:
- OSSafeRelease(theKext);
+ OSSafeReleaseNULL(theKext);
return result;
}
@@ -379,7 +373,7 @@
kernel_segment_command_t * segment = NULL; // do not free
kernel_segment_command_t * failure_segment = NULL; // do not free
- if (!kmodInfo || !kmodInfo->address || !kmodInfo->name) {
+ if (!kmodInfo || !kmodInfo->address) {
result = kOSKextReturnInvalidArgument;
goto finish;
}
@@ -422,9 +416,7 @@
section != NULL;
section = nextsect(segment, section)) {
- if (strncmp(section->sectname, SECT_CONSTRUCTOR,
- sizeof(SECT_CONSTRUCTOR)) == 0) {
-
+ if (sectionIsConstructor(section)) {
structor_t * constructors = (structor_t *)section->addr;
if (constructors) {
@@ -493,7 +485,7 @@
theKext->setCPPInitialized(true);
}
finish:
- OSSafeRelease(theKext);
+ OSSafeReleaseNULL(theKext);
return result;
}
@@ -506,17 +498,15 @@
/*********************************************************************
*********************************************************************/
-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.");
}
@@ -536,6 +526,9 @@
*********************************************************************/
void *
operator new(size_t size)
+#if __cplusplus >= 201103L
+ noexcept
+#endif
{
void * result;
@@ -545,6 +538,9 @@
void
operator delete(void * addr)
+#if __cplusplus >= 201103L
+ noexcept
+#endif
{
kern_os_free(addr);
return;
@@ -552,6 +548,9 @@
void *
operator new[](unsigned long sz)
+#if __cplusplus >= 201103L
+ noexcept
+#endif
{
if (sz == 0) sz = 1;
return kern_os_malloc(sz);
@@ -559,6 +558,9 @@
void
operator delete[](void * ptr)
+#if __cplusplus >= 201103L
+ noexcept
+#endif
{
if (ptr) {
kern_os_free(ptr);