xref: /xnu-10002.81.5/libkern/c++/OSRuntime.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2000,2008-2009 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions /*
29*5e3eaea3SApple OSS Distributions  * Copyright (c) 1997 Apple Inc.
30*5e3eaea3SApple OSS Distributions  *
31*5e3eaea3SApple OSS Distributions  */
32*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
33*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSKext.h>
34*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSLib.h>
35*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
36*5e3eaea3SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions #include <sys/cdefs.h>
39*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
40*5e3eaea3SApple OSS Distributions #include <ptrauth.h>
41*5e3eaea3SApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x)       ((uintptr_t) ptrauth_strip(ptrauth_nop_cast(void *, (x)), ptrauth_key_function_pointer))
42*5e3eaea3SApple OSS Distributions #else  /* defined(HAS_APPLE_PAC) */
43*5e3eaea3SApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x)       ((uintptr_t) (x))
44*5e3eaea3SApple OSS Distributions #endif /* !defined(HAS_APPLE_PAC) */
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions __BEGIN_DECLS
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions #include <string.h>
49*5e3eaea3SApple OSS Distributions #include <mach/mach_types.h>
50*5e3eaea3SApple OSS Distributions #include <libkern/kernel_mach_header.h>
51*5e3eaea3SApple OSS Distributions #include <libkern/prelink.h>
52*5e3eaea3SApple OSS Distributions #include <stdarg.h>
53*5e3eaea3SApple OSS Distributions 
54*5e3eaea3SApple OSS Distributions #if KASAN
55*5e3eaea3SApple OSS Distributions #include <san/kasan.h>
56*5e3eaea3SApple OSS Distributions #endif
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions #if PRAGMA_MARK
60*5e3eaea3SApple OSS Distributions #pragma mark Constants &c.
61*5e3eaea3SApple OSS Distributions #endif /* PRAGMA_MARK */
62*5e3eaea3SApple OSS Distributions OSKextLogSpec kOSRuntimeLogSpec =
63*5e3eaea3SApple OSS Distributions     kOSKextLogErrorLevel |
64*5e3eaea3SApple OSS Distributions     kOSKextLogLoadFlag |
65*5e3eaea3SApple OSS Distributions     kOSKextLogKextBookkeepingFlag;
66*5e3eaea3SApple OSS Distributions 
67*5e3eaea3SApple OSS Distributions #if PRAGMA_MARK
68*5e3eaea3SApple OSS Distributions #pragma mark Logging Bootstrap
69*5e3eaea3SApple OSS Distributions #endif /* PRAGMA_MARK */
70*5e3eaea3SApple OSS Distributions /*********************************************************************
71*5e3eaea3SApple OSS Distributions * kern_os Logging Bootstrap
72*5e3eaea3SApple OSS Distributions *
73*5e3eaea3SApple OSS Distributions * We can't call in to OSKext until the kernel's C++ environment is up
74*5e3eaea3SApple OSS Distributions * and running, so let's mask those references with a check variable.
75*5e3eaea3SApple OSS Distributions * We print unconditionally if C++ isn't up, but if that's the case
76*5e3eaea3SApple OSS Distributions * we've generally hit a serious error in kernel init!
77*5e3eaea3SApple OSS Distributions *********************************************************************/
78*5e3eaea3SApple OSS Distributions static bool gKernelCPPInitialized = false;
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions #define OSRuntimeLog(kext, flags, format, args ...)            \
81*5e3eaea3SApple OSS Distributions     do {                                                      \
82*5e3eaea3SApple OSS Distributions 	if (gKernelCPPInitialized) {                          \
83*5e3eaea3SApple OSS Distributions 	    OSKextLog((kext), (flags), (format), ## args);  \
84*5e3eaea3SApple OSS Distributions 	} else {                                              \
85*5e3eaea3SApple OSS Distributions 	    printf((format), ## args);                        \
86*5e3eaea3SApple OSS Distributions 	}                                                     \
87*5e3eaea3SApple OSS Distributions     } while (0)
88*5e3eaea3SApple OSS Distributions 
89*5e3eaea3SApple OSS Distributions #if PRAGMA_MARK
90*5e3eaea3SApple OSS Distributions #pragma mark Libkern Init
91*5e3eaea3SApple OSS Distributions #endif /* PRAGMA_MARK */
92*5e3eaea3SApple OSS Distributions /*********************************************************************
93*5e3eaea3SApple OSS Distributions * Libkern Init
94*5e3eaea3SApple OSS Distributions *********************************************************************/
95*5e3eaea3SApple OSS Distributions 
96*5e3eaea3SApple OSS Distributions #if __GNUC__ >= 3
97*5e3eaea3SApple OSS Distributions void __dead2
__cxa_pure_virtual(void)98*5e3eaea3SApple OSS Distributions __cxa_pure_virtual( void )
99*5e3eaea3SApple OSS Distributions {
100*5e3eaea3SApple OSS Distributions 	panic("%s", __FUNCTION__);
101*5e3eaea3SApple OSS Distributions }
102*5e3eaea3SApple OSS Distributions #else
103*5e3eaea3SApple OSS Distributions void __dead2
__pure_virtual(void)104*5e3eaea3SApple OSS Distributions __pure_virtual( void )
105*5e3eaea3SApple OSS Distributions {
106*5e3eaea3SApple OSS Distributions 	panic("%s", __FUNCTION__);
107*5e3eaea3SApple OSS Distributions }
108*5e3eaea3SApple OSS Distributions #endif
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions extern lck_grp_t * IOLockGroup;
111*5e3eaea3SApple OSS Distributions extern kmod_info_t g_kernel_kmod_info;
112*5e3eaea3SApple OSS Distributions 
113*5e3eaea3SApple OSS Distributions enum {
114*5e3eaea3SApple OSS Distributions 	kOSSectionNamesDefault     = 0,
115*5e3eaea3SApple OSS Distributions 	kOSSectionNamesBuiltinKext = 1,
116*5e3eaea3SApple OSS Distributions 	kOSSectionNamesCount       = 2,
117*5e3eaea3SApple OSS Distributions };
118*5e3eaea3SApple OSS Distributions enum {
119*5e3eaea3SApple OSS Distributions 	kOSSectionNameInitializer = 0,
120*5e3eaea3SApple OSS Distributions 	kOSSectionNameFinalizer   = 1,
121*5e3eaea3SApple OSS Distributions 	kOSSectionNameCount       = 2
122*5e3eaea3SApple OSS Distributions };
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions static const char *
125*5e3eaea3SApple OSS Distributions     gOSStructorSectionNames[kOSSectionNamesCount][kOSSectionNameCount] = {
126*5e3eaea3SApple OSS Distributions 	{ SECT_MODINITFUNC, SECT_MODTERMFUNC },
127*5e3eaea3SApple OSS Distributions 	{ kBuiltinInitSection, kBuiltinTermSection }
128*5e3eaea3SApple OSS Distributions };
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions void
OSlibkernInit(void)131*5e3eaea3SApple OSS Distributions OSlibkernInit(void)
132*5e3eaea3SApple OSS Distributions {
133*5e3eaea3SApple OSS Distributions 	// This must be called before calling OSRuntimeInitializeCPP.
134*5e3eaea3SApple OSS Distributions 	OSMetaClassBase::initialize();
135*5e3eaea3SApple OSS Distributions 
136*5e3eaea3SApple OSS Distributions 	g_kernel_kmod_info.address = (vm_address_t) &_mh_execute_header;
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions 	if (kOSReturnSuccess != OSRuntimeInitializeCPP(NULL)) {
139*5e3eaea3SApple OSS Distributions 		// &g_kernel_kmod_info, gOSSectionNamesStandard, 0, 0)) {
140*5e3eaea3SApple OSS Distributions 		panic("OSRuntime: C++ runtime failed to initialize.");
141*5e3eaea3SApple OSS Distributions 	}
142*5e3eaea3SApple OSS Distributions 
143*5e3eaea3SApple OSS Distributions 	gKernelCPPInitialized = true;
144*5e3eaea3SApple OSS Distributions 
145*5e3eaea3SApple OSS Distributions 	return;
146*5e3eaea3SApple OSS Distributions }
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions __END_DECLS
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions #if PRAGMA_MARK
151*5e3eaea3SApple OSS Distributions #pragma mark C++ Runtime Load/Unload
152*5e3eaea3SApple OSS Distributions #endif /* PRAGMA_MARK */
153*5e3eaea3SApple OSS Distributions /*********************************************************************
154*5e3eaea3SApple OSS Distributions * kern_os C++ Runtime Load/Unload
155*5e3eaea3SApple OSS Distributions *********************************************************************/
156*5e3eaea3SApple OSS Distributions 
157*5e3eaea3SApple OSS Distributions typedef void (*structor_t)(void);
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions static bool
OSRuntimeCallStructorsInSection(OSKext * theKext,kmod_info_t * kmodInfo,void * metaHandle,kernel_segment_command_t * segment,const char * sectionName,uintptr_t textStart,uintptr_t textEnd)160*5e3eaea3SApple OSS Distributions OSRuntimeCallStructorsInSection(
161*5e3eaea3SApple OSS Distributions 	OSKext                   * theKext,
162*5e3eaea3SApple OSS Distributions 	kmod_info_t              * kmodInfo,
163*5e3eaea3SApple OSS Distributions 	void                     * metaHandle,
164*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * segment,
165*5e3eaea3SApple OSS Distributions 	const char               * sectionName,
166*5e3eaea3SApple OSS Distributions 	uintptr_t                  textStart,
167*5e3eaea3SApple OSS Distributions 	uintptr_t                  textEnd)
168*5e3eaea3SApple OSS Distributions {
169*5e3eaea3SApple OSS Distributions 	kernel_section_t * section;
170*5e3eaea3SApple OSS Distributions 	bool result = TRUE;
171*5e3eaea3SApple OSS Distributions 
172*5e3eaea3SApple OSS Distributions 	for (section = firstsect(segment);
173*5e3eaea3SApple OSS Distributions 	    section != NULL;
174*5e3eaea3SApple OSS Distributions 	    section = nextsect(segment, section)) {
175*5e3eaea3SApple OSS Distributions 		if (strncmp(section->sectname, sectionName, sizeof(section->sectname) - 1)) {
176*5e3eaea3SApple OSS Distributions 			continue;
177*5e3eaea3SApple OSS Distributions 		}
178*5e3eaea3SApple OSS Distributions 		if (section->size == 0) {
179*5e3eaea3SApple OSS Distributions 			continue;
180*5e3eaea3SApple OSS Distributions 		}
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions 		structor_t * structors = (structor_t *)section->addr;
183*5e3eaea3SApple OSS Distributions 		if (!structors) {
184*5e3eaea3SApple OSS Distributions 			continue;
185*5e3eaea3SApple OSS Distributions 		}
186*5e3eaea3SApple OSS Distributions 
187*5e3eaea3SApple OSS Distributions 		structor_t structor;
188*5e3eaea3SApple OSS Distributions 		uintptr_t value;
189*5e3eaea3SApple OSS Distributions 		unsigned long num_structors = section->size / sizeof(structor_t);
190*5e3eaea3SApple OSS Distributions 		unsigned int hit_null_structor = 0;
191*5e3eaea3SApple OSS Distributions 		unsigned long firstIndex = 0;
192*5e3eaea3SApple OSS Distributions 
193*5e3eaea3SApple OSS Distributions 		if (textStart) {
194*5e3eaea3SApple OSS Distributions 			// bsearch for any in range
195*5e3eaea3SApple OSS Distributions 			unsigned long baseIdx;
196*5e3eaea3SApple OSS Distributions 			unsigned long lim;
197*5e3eaea3SApple OSS Distributions 			firstIndex = num_structors;
198*5e3eaea3SApple OSS Distributions 			for (lim = num_structors, baseIdx = 0; lim; lim >>= 1) {
199*5e3eaea3SApple OSS Distributions 				structor = structors[baseIdx + (lim >> 1)];
200*5e3eaea3SApple OSS Distributions 				if (!structor) {
201*5e3eaea3SApple OSS Distributions 					panic("%s: null structor", kmodInfo->name);
202*5e3eaea3SApple OSS Distributions 				}
203*5e3eaea3SApple OSS Distributions 				value = PTRAUTH_STRIP_STRUCTOR(structor);
204*5e3eaea3SApple OSS Distributions 				if ((value >= textStart) && (value < textEnd)) {
205*5e3eaea3SApple OSS Distributions 					firstIndex = (baseIdx + (lim >> 1));
206*5e3eaea3SApple OSS Distributions 					// scan back for the first in range
207*5e3eaea3SApple OSS Distributions 					for (; firstIndex; firstIndex--) {
208*5e3eaea3SApple OSS Distributions 						structor = structors[firstIndex - 1];
209*5e3eaea3SApple OSS Distributions 						value = PTRAUTH_STRIP_STRUCTOR(structor);
210*5e3eaea3SApple OSS Distributions 						if ((value < textStart) || (value >= textEnd)) {
211*5e3eaea3SApple OSS Distributions 							break;
212*5e3eaea3SApple OSS Distributions 						}
213*5e3eaea3SApple OSS Distributions 					}
214*5e3eaea3SApple OSS Distributions 					break;
215*5e3eaea3SApple OSS Distributions 				}
216*5e3eaea3SApple OSS Distributions 				if (textStart > value) {
217*5e3eaea3SApple OSS Distributions 					// move right
218*5e3eaea3SApple OSS Distributions 					baseIdx += (lim >> 1) + 1;
219*5e3eaea3SApple OSS Distributions 					lim--;
220*5e3eaea3SApple OSS Distributions 				}
221*5e3eaea3SApple OSS Distributions 				// else move left
222*5e3eaea3SApple OSS Distributions 			}
223*5e3eaea3SApple OSS Distributions 			baseIdx = (baseIdx + (lim >> 1));
224*5e3eaea3SApple OSS Distributions 		}
225*5e3eaea3SApple OSS Distributions 		for (;
226*5e3eaea3SApple OSS Distributions 		    (firstIndex < num_structors)
227*5e3eaea3SApple OSS Distributions 		    && (!metaHandle || OSMetaClass::checkModLoad(metaHandle));
228*5e3eaea3SApple OSS Distributions 		    firstIndex++) {
229*5e3eaea3SApple OSS Distributions 			if ((structor = structors[firstIndex])) {
230*5e3eaea3SApple OSS Distributions 				value = PTRAUTH_STRIP_STRUCTOR(structor);
231*5e3eaea3SApple OSS Distributions 				if ((textStart && (value < textStart))
232*5e3eaea3SApple OSS Distributions 				    || (textEnd && (value >= textEnd))) {
233*5e3eaea3SApple OSS Distributions 					break;
234*5e3eaea3SApple OSS Distributions 				}
235*5e3eaea3SApple OSS Distributions 				(*structor)();
236*5e3eaea3SApple OSS Distributions 			} else if (!hit_null_structor) {
237*5e3eaea3SApple OSS Distributions 				hit_null_structor = 1;
238*5e3eaea3SApple OSS Distributions 				OSRuntimeLog(theKext, kOSRuntimeLogSpec,
239*5e3eaea3SApple OSS Distributions 				    "Null structor in kext %s segment %s!",
240*5e3eaea3SApple OSS Distributions 				    kmodInfo->name, section->segname);
241*5e3eaea3SApple OSS Distributions 			}
242*5e3eaea3SApple OSS Distributions 		}
243*5e3eaea3SApple OSS Distributions 		if (metaHandle) {
244*5e3eaea3SApple OSS Distributions 			result = OSMetaClass::checkModLoad(metaHandle);
245*5e3eaea3SApple OSS Distributions 		}
246*5e3eaea3SApple OSS Distributions 		break;
247*5e3eaea3SApple OSS Distributions 	} /* for (section...) */
248*5e3eaea3SApple OSS Distributions 	return result;
249*5e3eaea3SApple OSS Distributions }
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions /*********************************************************************
252*5e3eaea3SApple OSS Distributions *********************************************************************/
253*5e3eaea3SApple OSS Distributions kern_return_t
OSRuntimeFinalizeCPP(OSKext * theKext)254*5e3eaea3SApple OSS Distributions OSRuntimeFinalizeCPP(
255*5e3eaea3SApple OSS Distributions 	OSKext                   * theKext)
256*5e3eaea3SApple OSS Distributions {
257*5e3eaea3SApple OSS Distributions 	kern_return_t              result = KMOD_RETURN_FAILURE;
258*5e3eaea3SApple OSS Distributions 	void                     * metaHandle = NULL;// do not free
259*5e3eaea3SApple OSS Distributions 	kernel_mach_header_t     * header;
260*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * segment;
261*5e3eaea3SApple OSS Distributions 	kmod_info_t              * kmodInfo;
262*5e3eaea3SApple OSS Distributions 	const char              ** sectionNames;
263*5e3eaea3SApple OSS Distributions 	uintptr_t                  textStart;
264*5e3eaea3SApple OSS Distributions 	uintptr_t                  textEnd;
265*5e3eaea3SApple OSS Distributions 
266*5e3eaea3SApple OSS Distributions 	textStart    = 0;
267*5e3eaea3SApple OSS Distributions 	textEnd      = 0;
268*5e3eaea3SApple OSS Distributions 	sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
269*5e3eaea3SApple OSS Distributions 	if (theKext) {
270*5e3eaea3SApple OSS Distributions 		if (!theKext->isCPPInitialized()) {
271*5e3eaea3SApple OSS Distributions 			result = KMOD_RETURN_SUCCESS;
272*5e3eaea3SApple OSS Distributions 			goto finish;
273*5e3eaea3SApple OSS Distributions 		}
274*5e3eaea3SApple OSS Distributions 		kmodInfo = theKext->kmod_info;
275*5e3eaea3SApple OSS Distributions 		if (!kmodInfo || !kmodInfo->address) {
276*5e3eaea3SApple OSS Distributions 			result = kOSKextReturnInvalidArgument;
277*5e3eaea3SApple OSS Distributions 			goto finish;
278*5e3eaea3SApple OSS Distributions 		}
279*5e3eaea3SApple OSS Distributions 		header = (kernel_mach_header_t *)kmodInfo->address;
280*5e3eaea3SApple OSS Distributions 		if (theKext->flags.builtin) {
281*5e3eaea3SApple OSS Distributions 			header       = (kernel_mach_header_t *)g_kernel_kmod_info.address;
282*5e3eaea3SApple OSS Distributions 			textStart    = kmodInfo->address;
283*5e3eaea3SApple OSS Distributions 			textEnd      = textStart + kmodInfo->size;
284*5e3eaea3SApple OSS Distributions 			sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
285*5e3eaea3SApple OSS Distributions 		}
286*5e3eaea3SApple OSS Distributions 	} else {
287*5e3eaea3SApple OSS Distributions 		kmodInfo = &g_kernel_kmod_info;
288*5e3eaea3SApple OSS Distributions 		header   = (kernel_mach_header_t *)kmodInfo->address;
289*5e3eaea3SApple OSS Distributions 	}
290*5e3eaea3SApple OSS Distributions 
291*5e3eaea3SApple OSS Distributions 	/* OSKext checks for this condition now, but somebody might call
292*5e3eaea3SApple OSS Distributions 	 * this function directly (the symbol is exported....).
293*5e3eaea3SApple OSS Distributions 	 */
294*5e3eaea3SApple OSS Distributions 	if (OSMetaClass::modHasInstance(kmodInfo->name)) {
295*5e3eaea3SApple OSS Distributions 		// xxx - Don't log under errors? this is more of an info thing
296*5e3eaea3SApple OSS Distributions 		OSRuntimeLog(theKext, kOSRuntimeLogSpec,
297*5e3eaea3SApple OSS Distributions 		    "Can't tear down kext %s C++; classes have instances:",
298*5e3eaea3SApple OSS Distributions 		    kmodInfo->name);
299*5e3eaea3SApple OSS Distributions 		OSKext::reportOSMetaClassInstances(kmodInfo->name, kOSRuntimeLogSpec);
300*5e3eaea3SApple OSS Distributions 		result = kOSMetaClassHasInstances;
301*5e3eaea3SApple OSS Distributions 		goto finish;
302*5e3eaea3SApple OSS Distributions 	}
303*5e3eaea3SApple OSS Distributions 
304*5e3eaea3SApple OSS Distributions 	/* Tell the meta class system that we are starting to unload.
305*5e3eaea3SApple OSS Distributions 	 * metaHandle isn't actually needed on the finalize path,
306*5e3eaea3SApple OSS Distributions 	 * so we don't check it here, even though OSMetaClass::postModLoad() will
307*5e3eaea3SApple OSS Distributions 	 * return a failure (it only does actual work on the init path anyhow).
308*5e3eaea3SApple OSS Distributions 	 */
309*5e3eaea3SApple OSS Distributions 	metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
310*5e3eaea3SApple OSS Distributions 
311*5e3eaea3SApple OSS Distributions 	OSSymbol::checkForPageUnload((void *)kmodInfo->address,
312*5e3eaea3SApple OSS Distributions 	    (void *)(kmodInfo->address + kmodInfo->size));
313*5e3eaea3SApple OSS Distributions 
314*5e3eaea3SApple OSS Distributions 	header = (kernel_mach_header_t *)kmodInfo->address;
315*5e3eaea3SApple OSS Distributions 	segment = firstsegfromheader(header);
316*5e3eaea3SApple OSS Distributions 
317*5e3eaea3SApple OSS Distributions 	for (segment = firstsegfromheader(header);
318*5e3eaea3SApple OSS Distributions 	    segment != NULL;
319*5e3eaea3SApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
320*5e3eaea3SApple OSS Distributions 		OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
321*5e3eaea3SApple OSS Distributions 		    sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
322*5e3eaea3SApple OSS Distributions 	}
323*5e3eaea3SApple OSS Distributions 
324*5e3eaea3SApple OSS Distributions 	(void)OSMetaClass::postModLoad(metaHandle);
325*5e3eaea3SApple OSS Distributions 
326*5e3eaea3SApple OSS Distributions 	if (theKext) {
327*5e3eaea3SApple OSS Distributions 		theKext->setCPPInitialized(false);
328*5e3eaea3SApple OSS Distributions 	}
329*5e3eaea3SApple OSS Distributions 	result = KMOD_RETURN_SUCCESS;
330*5e3eaea3SApple OSS Distributions finish:
331*5e3eaea3SApple OSS Distributions 	return result;
332*5e3eaea3SApple OSS Distributions }
333*5e3eaea3SApple OSS Distributions 
334*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
335*5e3eaea3SApple OSS Distributions #if !KASAN
336*5e3eaea3SApple OSS Distributions /*
337*5e3eaea3SApple OSS Distributions  * Place this function in __KLD,__text on non-kasan builds so it gets unmapped
338*5e3eaea3SApple OSS Distributions  * after CTRR lockdown.
339*5e3eaea3SApple OSS Distributions  */
340*5e3eaea3SApple OSS Distributions __attribute__((noinline, section("__KLD,__text")))
341*5e3eaea3SApple OSS Distributions #endif
342*5e3eaea3SApple OSS Distributions static void
OSRuntimeSignStructorsInSegment(kernel_segment_command_t * segment)343*5e3eaea3SApple OSS Distributions OSRuntimeSignStructorsInSegment(kernel_segment_command_t *segment)
344*5e3eaea3SApple OSS Distributions {
345*5e3eaea3SApple OSS Distributions 	kernel_section_t         * section;
346*5e3eaea3SApple OSS Distributions 	structor_t               * structors;
347*5e3eaea3SApple OSS Distributions 	volatile structor_t                 structor;
348*5e3eaea3SApple OSS Distributions 	size_t                     idx, num_structors;
349*5e3eaea3SApple OSS Distributions 
350*5e3eaea3SApple OSS Distributions 	for (section = firstsect(segment);
351*5e3eaea3SApple OSS Distributions 	    section != NULL;
352*5e3eaea3SApple OSS Distributions 	    section = nextsect(segment, section)) {
353*5e3eaea3SApple OSS Distributions 		if ((S_MOD_INIT_FUNC_POINTERS != (SECTION_TYPE & section->flags))
354*5e3eaea3SApple OSS Distributions 		    && (S_MOD_TERM_FUNC_POINTERS != (SECTION_TYPE & section->flags))) {
355*5e3eaea3SApple OSS Distributions 			continue;
356*5e3eaea3SApple OSS Distributions 		}
357*5e3eaea3SApple OSS Distributions 		structors = (structor_t *)section->addr;
358*5e3eaea3SApple OSS Distributions 		if (!structors) {
359*5e3eaea3SApple OSS Distributions 			continue;
360*5e3eaea3SApple OSS Distributions 		}
361*5e3eaea3SApple OSS Distributions 		num_structors = section->size / sizeof(structor_t);
362*5e3eaea3SApple OSS Distributions 		for (idx = 0; idx < num_structors; idx++) {
363*5e3eaea3SApple OSS Distributions 			structor = structors[idx];
364*5e3eaea3SApple OSS Distributions 			if (NULL == structor) {
365*5e3eaea3SApple OSS Distributions 				continue;
366*5e3eaea3SApple OSS Distributions 			}
367*5e3eaea3SApple OSS Distributions 			structor = ptrauth_strip(structor, ptrauth_key_function_pointer);
368*5e3eaea3SApple OSS Distributions 			structor = ptrauth_sign_unauthenticated(structor, ptrauth_key_function_pointer, ptrauth_function_pointer_type_discriminator(void (*)(void)));
369*5e3eaea3SApple OSS Distributions 			structors[idx] = structor;
370*5e3eaea3SApple OSS Distributions 		}
371*5e3eaea3SApple OSS Distributions 	} /* for (section...) */
372*5e3eaea3SApple OSS Distributions }
373*5e3eaea3SApple OSS Distributions #endif
374*5e3eaea3SApple OSS Distributions 
375*5e3eaea3SApple OSS Distributions /*********************************************************************
376*5e3eaea3SApple OSS Distributions *********************************************************************/
377*5e3eaea3SApple OSS Distributions void
OSRuntimeSignStructors(kernel_mach_header_t * header __unused)378*5e3eaea3SApple OSS Distributions OSRuntimeSignStructors(
379*5e3eaea3SApple OSS Distributions 	kernel_mach_header_t * header __unused)
380*5e3eaea3SApple OSS Distributions {
381*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
382*5e3eaea3SApple OSS Distributions 
383*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * segment;
384*5e3eaea3SApple OSS Distributions 
385*5e3eaea3SApple OSS Distributions 	for (segment = firstsegfromheader(header);
386*5e3eaea3SApple OSS Distributions 	    segment != NULL;
387*5e3eaea3SApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
388*5e3eaea3SApple OSS Distributions 		OSRuntimeSignStructorsInSegment(segment);
389*5e3eaea3SApple OSS Distributions 	} /* for (segment...) */
390*5e3eaea3SApple OSS Distributions #endif /* !defined(XXX) && defined(HAS_APPLE_PAC) */
391*5e3eaea3SApple OSS Distributions }
392*5e3eaea3SApple OSS Distributions 
393*5e3eaea3SApple OSS Distributions /*********************************************************************
394*5e3eaea3SApple OSS Distributions *********************************************************************/
395*5e3eaea3SApple OSS Distributions void
OSRuntimeSignStructorsInFileset(kernel_mach_header_t * fileset_header __unused)396*5e3eaea3SApple OSS Distributions OSRuntimeSignStructorsInFileset(
397*5e3eaea3SApple OSS Distributions 	kernel_mach_header_t * fileset_header __unused)
398*5e3eaea3SApple OSS Distributions {
399*5e3eaea3SApple OSS Distributions #if defined(HAS_APPLE_PAC)
400*5e3eaea3SApple OSS Distributions 	struct load_command *lc;
401*5e3eaea3SApple OSS Distributions 
402*5e3eaea3SApple OSS Distributions 	lc = (struct load_command *)((uintptr_t)fileset_header + sizeof(*fileset_header));
403*5e3eaea3SApple OSS Distributions 	for (uint32_t i = 0; i < fileset_header->ncmds; i++,
404*5e3eaea3SApple OSS Distributions 	    lc = (struct load_command *)((uintptr_t)lc + lc->cmdsize)) {
405*5e3eaea3SApple OSS Distributions 		if (lc->cmd == LC_FILESET_ENTRY) {
406*5e3eaea3SApple OSS Distributions 			struct fileset_entry_command *fse;
407*5e3eaea3SApple OSS Distributions 			kernel_mach_header_t *mh;
408*5e3eaea3SApple OSS Distributions 
409*5e3eaea3SApple OSS Distributions 			fse = (struct fileset_entry_command *)(uintptr_t)lc;
410*5e3eaea3SApple OSS Distributions 			mh = (kernel_mach_header_t *)((uintptr_t)fse->vmaddr);
411*5e3eaea3SApple OSS Distributions 			OSRuntimeSignStructors(mh);
412*5e3eaea3SApple OSS Distributions 		} else if (lc->cmd == LC_SEGMENT_64) {
413*5e3eaea3SApple OSS Distributions 			/*
414*5e3eaea3SApple OSS Distributions 			 * Slide/adjust all LC_SEGMENT_64 commands in the fileset
415*5e3eaea3SApple OSS Distributions 			 * (and any sections in those segments)
416*5e3eaea3SApple OSS Distributions 			 */
417*5e3eaea3SApple OSS Distributions 			kernel_segment_command_t *seg;
418*5e3eaea3SApple OSS Distributions 			seg = (kernel_segment_command_t *)(uintptr_t)lc;
419*5e3eaea3SApple OSS Distributions 			OSRuntimeSignStructorsInSegment(seg);
420*5e3eaea3SApple OSS Distributions 		}
421*5e3eaea3SApple OSS Distributions 	}
422*5e3eaea3SApple OSS Distributions 
423*5e3eaea3SApple OSS Distributions #endif /* defined(HAS_APPLE_PAC) */
424*5e3eaea3SApple OSS Distributions }
425*5e3eaea3SApple OSS Distributions 
426*5e3eaea3SApple OSS Distributions /*********************************************************************
427*5e3eaea3SApple OSS Distributions *********************************************************************/
428*5e3eaea3SApple OSS Distributions kern_return_t
OSRuntimeInitializeCPP(OSKext * theKext)429*5e3eaea3SApple OSS Distributions OSRuntimeInitializeCPP(
430*5e3eaea3SApple OSS Distributions 	OSKext                   * theKext)
431*5e3eaea3SApple OSS Distributions {
432*5e3eaea3SApple OSS Distributions 	kern_return_t              result          = KMOD_RETURN_FAILURE;
433*5e3eaea3SApple OSS Distributions 	kernel_mach_header_t     * header          = NULL;
434*5e3eaea3SApple OSS Distributions 	void                     * metaHandle      = NULL;// do not free
435*5e3eaea3SApple OSS Distributions 	bool                       load_success    = true;
436*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * segment         = NULL;// do not free
437*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * failure_segment = NULL; // do not free
438*5e3eaea3SApple OSS Distributions 	kmod_info_t             *  kmodInfo;
439*5e3eaea3SApple OSS Distributions 	const char              ** sectionNames;
440*5e3eaea3SApple OSS Distributions 	uintptr_t                  textStart;
441*5e3eaea3SApple OSS Distributions 	uintptr_t                  textEnd;
442*5e3eaea3SApple OSS Distributions 
443*5e3eaea3SApple OSS Distributions 	textStart    = 0;
444*5e3eaea3SApple OSS Distributions 	textEnd      = 0;
445*5e3eaea3SApple OSS Distributions 	sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
446*5e3eaea3SApple OSS Distributions 	if (theKext) {
447*5e3eaea3SApple OSS Distributions 		if (theKext->isCPPInitialized()) {
448*5e3eaea3SApple OSS Distributions 			result = KMOD_RETURN_SUCCESS;
449*5e3eaea3SApple OSS Distributions 			goto finish;
450*5e3eaea3SApple OSS Distributions 		}
451*5e3eaea3SApple OSS Distributions 
452*5e3eaea3SApple OSS Distributions 		kmodInfo = theKext->kmod_info;
453*5e3eaea3SApple OSS Distributions 		if (!kmodInfo || !kmodInfo->address) {
454*5e3eaea3SApple OSS Distributions 			result = kOSKextReturnInvalidArgument;
455*5e3eaea3SApple OSS Distributions 			goto finish;
456*5e3eaea3SApple OSS Distributions 		}
457*5e3eaea3SApple OSS Distributions 		header = (kernel_mach_header_t *)kmodInfo->address;
458*5e3eaea3SApple OSS Distributions 
459*5e3eaea3SApple OSS Distributions 		if (theKext->flags.builtin) {
460*5e3eaea3SApple OSS Distributions 			header       = (kernel_mach_header_t *)g_kernel_kmod_info.address;
461*5e3eaea3SApple OSS Distributions 			textStart    = kmodInfo->address;
462*5e3eaea3SApple OSS Distributions 			textEnd      = textStart + kmodInfo->size;
463*5e3eaea3SApple OSS Distributions 			sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
464*5e3eaea3SApple OSS Distributions 		}
465*5e3eaea3SApple OSS Distributions 	} else {
466*5e3eaea3SApple OSS Distributions 		kmodInfo = &g_kernel_kmod_info;
467*5e3eaea3SApple OSS Distributions 		header   = (kernel_mach_header_t *)kmodInfo->address;
468*5e3eaea3SApple OSS Distributions 	}
469*5e3eaea3SApple OSS Distributions 
470*5e3eaea3SApple OSS Distributions 	/* Tell the meta class system that we are starting the load
471*5e3eaea3SApple OSS Distributions 	 */
472*5e3eaea3SApple OSS Distributions 	metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
473*5e3eaea3SApple OSS Distributions 	assert(metaHandle);
474*5e3eaea3SApple OSS Distributions 	if (!metaHandle) {
475*5e3eaea3SApple OSS Distributions 		goto finish;
476*5e3eaea3SApple OSS Distributions 	}
477*5e3eaea3SApple OSS Distributions 
478*5e3eaea3SApple OSS Distributions 	/* NO GOTO PAST HERE. */
479*5e3eaea3SApple OSS Distributions 
480*5e3eaea3SApple OSS Distributions 	/* Scan the header for all constructor sections, in any
481*5e3eaea3SApple OSS Distributions 	 * segment, and invoke the constructors within those sections.
482*5e3eaea3SApple OSS Distributions 	 */
483*5e3eaea3SApple OSS Distributions 	for (segment = firstsegfromheader(header);
484*5e3eaea3SApple OSS Distributions 	    segment != NULL && load_success;
485*5e3eaea3SApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
486*5e3eaea3SApple OSS Distributions 		/* Record the current segment in the event of a failure.
487*5e3eaea3SApple OSS Distributions 		 */
488*5e3eaea3SApple OSS Distributions 		failure_segment = segment;
489*5e3eaea3SApple OSS Distributions 		load_success = OSRuntimeCallStructorsInSection(
490*5e3eaea3SApple OSS Distributions 			theKext, kmodInfo, metaHandle, segment,
491*5e3eaea3SApple OSS Distributions 			sectionNames[kOSSectionNameInitializer],
492*5e3eaea3SApple OSS Distributions 			textStart, textEnd);
493*5e3eaea3SApple OSS Distributions 	} /* for (segment...) */
494*5e3eaea3SApple OSS Distributions 
495*5e3eaea3SApple OSS Distributions 	/* We failed so call all of the destructors. We must do this before
496*5e3eaea3SApple OSS Distributions 	 * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
497*5e3eaea3SApple OSS Distributions 	 * will alter state (in the metaHandle) used by that function.
498*5e3eaea3SApple OSS Distributions 	 */
499*5e3eaea3SApple OSS Distributions 	if (!load_success) {
500*5e3eaea3SApple OSS Distributions 		/* Scan the header for all destructor sections, in any
501*5e3eaea3SApple OSS Distributions 		 * segment, and invoke the constructors within those sections.
502*5e3eaea3SApple OSS Distributions 		 */
503*5e3eaea3SApple OSS Distributions 		for (segment = firstsegfromheader(header);
504*5e3eaea3SApple OSS Distributions 		    segment != failure_segment && segment != NULL;
505*5e3eaea3SApple OSS Distributions 		    segment = nextsegfromheader(header, segment)) {
506*5e3eaea3SApple OSS Distributions 			OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
507*5e3eaea3SApple OSS Distributions 			    sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
508*5e3eaea3SApple OSS Distributions 		} /* for (segment...) */
509*5e3eaea3SApple OSS Distributions 	}
510*5e3eaea3SApple OSS Distributions 
511*5e3eaea3SApple OSS Distributions 	/* Now, regardless of success so far, do the post-init registration
512*5e3eaea3SApple OSS Distributions 	 * and cleanup. If we had to call the unloadCPP function, static
513*5e3eaea3SApple OSS Distributions 	 * destructors have removed classes from the stalled list so no
514*5e3eaea3SApple OSS Distributions 	 * metaclasses will actually be registered.
515*5e3eaea3SApple OSS Distributions 	 */
516*5e3eaea3SApple OSS Distributions 	result = OSMetaClass::postModLoad(metaHandle);
517*5e3eaea3SApple OSS Distributions 
518*5e3eaea3SApple OSS Distributions 	/* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
519*5e3eaea3SApple OSS Distributions 	 * fails (typically due to a duplicate class), tear down all the C++
520*5e3eaea3SApple OSS Distributions 	 * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
521*5e3eaea3SApple OSS Distributions 	 * but may be necessary for other C++ code. We ignore the return value
522*5e3eaea3SApple OSS Distributions 	 * because it's only a fail when there are existing instances of libkern
523*5e3eaea3SApple OSS Distributions 	 * classes, and there had better not be any created on the C++ init path.
524*5e3eaea3SApple OSS Distributions 	 */
525*5e3eaea3SApple OSS Distributions 	if (load_success && result != KMOD_RETURN_SUCCESS) {
526*5e3eaea3SApple OSS Distributions 		(void)OSRuntimeFinalizeCPP(theKext); //kmodInfo, sectionNames, textStart, textEnd);
527*5e3eaea3SApple OSS Distributions 	}
528*5e3eaea3SApple OSS Distributions 
529*5e3eaea3SApple OSS Distributions 	if (theKext && load_success && result == KMOD_RETURN_SUCCESS) {
530*5e3eaea3SApple OSS Distributions 		theKext->setCPPInitialized(true);
531*5e3eaea3SApple OSS Distributions 	}
532*5e3eaea3SApple OSS Distributions finish:
533*5e3eaea3SApple OSS Distributions 	return result;
534*5e3eaea3SApple OSS Distributions }
535*5e3eaea3SApple OSS Distributions 
536*5e3eaea3SApple OSS Distributions /*********************************************************************
537*5e3eaea3SApple OSS Distributions *   Unload a kernel segment.
538*5e3eaea3SApple OSS Distributions *********************************************************************/
539*5e3eaea3SApple OSS Distributions 
540*5e3eaea3SApple OSS Distributions void
OSRuntimeUnloadCPPForSegment(kernel_segment_command_t * segment)541*5e3eaea3SApple OSS Distributions OSRuntimeUnloadCPPForSegment(
542*5e3eaea3SApple OSS Distributions 	kernel_segment_command_t * segment)
543*5e3eaea3SApple OSS Distributions {
544*5e3eaea3SApple OSS Distributions 	OSRuntimeCallStructorsInSection(NULL, &g_kernel_kmod_info, NULL, segment,
545*5e3eaea3SApple OSS Distributions 	    gOSStructorSectionNames[kOSSectionNamesDefault][kOSSectionNameFinalizer], 0, 0);
546*5e3eaea3SApple OSS Distributions }
547*5e3eaea3SApple OSS Distributions 
548*5e3eaea3SApple OSS Distributions #if PRAGMA_MARK
549*5e3eaea3SApple OSS Distributions #pragma mark C++ Allocators & Deallocators
550*5e3eaea3SApple OSS Distributions #endif /* PRAGMA_MARK */
551*5e3eaea3SApple OSS Distributions /*********************************************************************
552*5e3eaea3SApple OSS Distributions * C++ Allocators & Deallocators
553*5e3eaea3SApple OSS Distributions *********************************************************************/
554*5e3eaea3SApple OSS Distributions __typed_allocators_ignore_push
555*5e3eaea3SApple OSS Distributions 
556*5e3eaea3SApple OSS Distributions void *
operator new(size_t size)557*5e3eaea3SApple OSS Distributions operator new(size_t size)
558*5e3eaea3SApple OSS Distributions {
559*5e3eaea3SApple OSS Distributions 	assert(size);
560*5e3eaea3SApple OSS Distributions 	return kheap_alloc(KERN_OS_MALLOC, size,
561*5e3eaea3SApple OSS Distributions 	           Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
562*5e3eaea3SApple OSS Distributions }
563*5e3eaea3SApple OSS Distributions 
564*5e3eaea3SApple OSS Distributions void
operator delete(void * addr)565*5e3eaea3SApple OSS Distributions operator delete(void * addr)
566*5e3eaea3SApple OSS Distributions #if __cplusplus >= 201103L
567*5e3eaea3SApple OSS Distributions noexcept
568*5e3eaea3SApple OSS Distributions #endif
569*5e3eaea3SApple OSS Distributions {
570*5e3eaea3SApple OSS Distributions 	kheap_free_addr(KERN_OS_MALLOC, addr);
571*5e3eaea3SApple OSS Distributions 	return;
572*5e3eaea3SApple OSS Distributions }
573*5e3eaea3SApple OSS Distributions 
574*5e3eaea3SApple OSS Distributions void *
operator new[](unsigned long size)575*5e3eaea3SApple OSS Distributions operator new[](unsigned long size)
576*5e3eaea3SApple OSS Distributions {
577*5e3eaea3SApple OSS Distributions 	return kheap_alloc(KERN_OS_MALLOC, size,
578*5e3eaea3SApple OSS Distributions 	           Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
579*5e3eaea3SApple OSS Distributions }
580*5e3eaea3SApple OSS Distributions 
581*5e3eaea3SApple OSS Distributions void
operator delete[](void * ptr)582*5e3eaea3SApple OSS Distributions operator delete[](void * ptr)
583*5e3eaea3SApple OSS Distributions #if __cplusplus >= 201103L
584*5e3eaea3SApple OSS Distributions noexcept
585*5e3eaea3SApple OSS Distributions #endif
586*5e3eaea3SApple OSS Distributions {
587*5e3eaea3SApple OSS Distributions 	if (ptr) {
588*5e3eaea3SApple OSS Distributions #if KASAN
589*5e3eaea3SApple OSS Distributions 		/*
590*5e3eaea3SApple OSS Distributions 		 * Unpoison the C++ array cookie inserted (but not removed) by the
591*5e3eaea3SApple OSS Distributions 		 * compiler on new[].
592*5e3eaea3SApple OSS Distributions 		 */
593*5e3eaea3SApple OSS Distributions 		kasan_unpoison_cxx_array_cookie(ptr);
594*5e3eaea3SApple OSS Distributions #endif
595*5e3eaea3SApple OSS Distributions 		kheap_free_addr(KERN_OS_MALLOC, ptr);
596*5e3eaea3SApple OSS Distributions 	}
597*5e3eaea3SApple OSS Distributions 	return;
598*5e3eaea3SApple OSS Distributions }
599*5e3eaea3SApple OSS Distributions 
600*5e3eaea3SApple OSS Distributions #if __cplusplus >= 201103L
601*5e3eaea3SApple OSS Distributions 
602*5e3eaea3SApple OSS Distributions void
operator delete(void * addr,size_t sz)603*5e3eaea3SApple OSS Distributions operator delete(void * addr, size_t sz) noexcept
604*5e3eaea3SApple OSS Distributions {
605*5e3eaea3SApple OSS Distributions 	kheap_free(KERN_OS_MALLOC, addr, sz);
606*5e3eaea3SApple OSS Distributions }
607*5e3eaea3SApple OSS Distributions 
608*5e3eaea3SApple OSS Distributions void
operator delete[](void * addr,size_t sz)609*5e3eaea3SApple OSS Distributions operator delete[](void * addr, size_t sz) noexcept
610*5e3eaea3SApple OSS Distributions {
611*5e3eaea3SApple OSS Distributions 	if (addr) {
612*5e3eaea3SApple OSS Distributions 		kheap_free(KERN_OS_MALLOC, addr, sz);
613*5e3eaea3SApple OSS Distributions 	}
614*5e3eaea3SApple OSS Distributions }
615*5e3eaea3SApple OSS Distributions 
616*5e3eaea3SApple OSS Distributions __typed_allocators_ignore_pop
617*5e3eaea3SApple OSS Distributions 
618*5e3eaea3SApple OSS Distributions #endif /* __cplusplus >= 201103L */
619*5e3eaea3SApple OSS Distributions 
620*5e3eaea3SApple OSS Distributions /* PR-6481964 - The compiler is going to check for size overflows in calls to
621*5e3eaea3SApple OSS Distributions  * new[], and if there is an overflow, it will call __throw_length_error.
622*5e3eaea3SApple OSS Distributions  * This is an unrecoverable error by the C++ standard, so we must panic here.
623*5e3eaea3SApple OSS Distributions  *
624*5e3eaea3SApple OSS Distributions  * We have to put the function inside the std namespace because of how the
625*5e3eaea3SApple OSS Distributions  * compiler expects the name to be mangled.
626*5e3eaea3SApple OSS Distributions  */
627*5e3eaea3SApple OSS Distributions namespace std {
628*5e3eaea3SApple OSS Distributions void __dead2
__throw_length_error(const char * msg __unused)629*5e3eaea3SApple OSS Distributions __throw_length_error(const char *msg __unused)
630*5e3eaea3SApple OSS Distributions {
631*5e3eaea3SApple OSS Distributions 	panic("Size of array created by new[] has overflowed");
632*5e3eaea3SApple OSS Distributions }
633*5e3eaea3SApple OSS Distributions };
634