xref: /xnu-11215/libkern/c++/OSRuntime.cpp (revision 94d3b452)
1c1dac77fSApple OSS Distributions /*
23ca3bd55SApple OSS Distributions  * Copyright (c) 2000,2008-2009 Apple Inc. All rights reserved.
3c1dac77fSApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
25c1dac77fSApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions  */
28c1dac77fSApple OSS Distributions /*
293ca3bd55SApple OSS Distributions  * Copyright (c) 1997 Apple Inc.
30c1dac77fSApple OSS Distributions  *
31c1dac77fSApple OSS Distributions  */
32c1dac77fSApple OSS Distributions #include <libkern/c++/OSMetaClass.h>
333ca3bd55SApple OSS Distributions #include <libkern/c++/OSKext.h>
34c1dac77fSApple OSS Distributions #include <libkern/c++/OSLib.h>
35c1dac77fSApple OSS Distributions #include <libkern/c++/OSSymbol.h>
360f3703acSApple OSS Distributions #include <IOKit/IOKitDebug.h>
37c1dac77fSApple OSS Distributions 
38c1dac77fSApple OSS Distributions #include <sys/cdefs.h>
39bb611c8fSApple OSS Distributions #if defined(HAS_APPLE_PAC)
40bb611c8fSApple OSS Distributions #include <ptrauth.h>
41bb611c8fSApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x)       ((uintptr_t) ptrauth_strip(ptrauth_nop_cast(void *, (x)), ptrauth_key_function_pointer))
42bb611c8fSApple OSS Distributions #else  /* defined(HAS_APPLE_PAC) */
43bb611c8fSApple OSS Distributions #define PTRAUTH_STRIP_STRUCTOR(x)       ((uintptr_t) (x))
44bb611c8fSApple OSS Distributions #endif /* !defined(HAS_APPLE_PAC) */
45c1dac77fSApple OSS Distributions 
46c1dac77fSApple OSS Distributions __BEGIN_DECLS
47c1dac77fSApple OSS Distributions 
48c1dac77fSApple OSS Distributions #include <string.h>
49c1dac77fSApple OSS Distributions #include <mach/mach_types.h>
503ca3bd55SApple OSS Distributions #include <libkern/kernel_mach_header.h>
51cc9a6355SApple OSS Distributions #include <libkern/prelink.h>
52c1dac77fSApple OSS Distributions #include <stdarg.h>
53c1dac77fSApple OSS Distributions 
54a5e72196SApple OSS Distributions #if KASAN
55a5e72196SApple OSS Distributions #include <san/kasan.h>
56a5e72196SApple OSS Distributions #endif
57a5e72196SApple OSS Distributions 
58*94d3b452SApple OSS Distributions #if CONFIG_SPTM
59*94d3b452SApple OSS Distributions #include <arm64/sptm/sptm.h>
60*94d3b452SApple OSS Distributions #endif
61aca3beaaSApple OSS Distributions 
623ca3bd55SApple OSS Distributions #if PRAGMA_MARK
633ca3bd55SApple OSS Distributions #pragma mark Constants &c.
643ca3bd55SApple OSS Distributions #endif /* PRAGMA_MARK */
653ca3bd55SApple OSS Distributions OSKextLogSpec kOSRuntimeLogSpec =
663ca3bd55SApple OSS Distributions     kOSKextLogErrorLevel |
673ca3bd55SApple OSS Distributions     kOSKextLogLoadFlag |
683ca3bd55SApple OSS Distributions     kOSKextLogKextBookkeepingFlag;
693ca3bd55SApple OSS Distributions 
703ca3bd55SApple OSS Distributions #if PRAGMA_MARK
713ca3bd55SApple OSS Distributions #pragma mark Logging Bootstrap
723ca3bd55SApple OSS Distributions #endif /* PRAGMA_MARK */
733ca3bd55SApple OSS Distributions /*********************************************************************
743ca3bd55SApple OSS Distributions * kern_os Logging Bootstrap
753ca3bd55SApple OSS Distributions *
763ca3bd55SApple OSS Distributions * We can't call in to OSKext until the kernel's C++ environment is up
773ca3bd55SApple OSS Distributions * and running, so let's mask those references with a check variable.
783ca3bd55SApple OSS Distributions * We print unconditionally if C++ isn't up, but if that's the case
793ca3bd55SApple OSS Distributions * we've generally hit a serious error in kernel init!
803ca3bd55SApple OSS Distributions *********************************************************************/
813ca3bd55SApple OSS Distributions static bool gKernelCPPInitialized = false;
823ca3bd55SApple OSS Distributions 
833ca3bd55SApple OSS Distributions #define OSRuntimeLog(kext, flags, format, args ...)            \
843ca3bd55SApple OSS Distributions     do {                                                      \
853ca3bd55SApple OSS Distributions 	if (gKernelCPPInitialized) {                          \
863ca3bd55SApple OSS Distributions 	    OSKextLog((kext), (flags), (format), ## args);  \
873ca3bd55SApple OSS Distributions 	} else {                                              \
883ca3bd55SApple OSS Distributions 	    printf((format), ## args);                        \
893ca3bd55SApple OSS Distributions 	}                                                     \
903ca3bd55SApple OSS Distributions     } while (0)
913ca3bd55SApple OSS Distributions 
923ca3bd55SApple OSS Distributions #if PRAGMA_MARK
93cc9a6355SApple OSS Distributions #pragma mark Libkern Init
943ca3bd55SApple OSS Distributions #endif /* PRAGMA_MARK */
953ca3bd55SApple OSS Distributions /*********************************************************************
96cc9a6355SApple OSS Distributions * Libkern Init
973ca3bd55SApple OSS Distributions *********************************************************************/
983ca3bd55SApple OSS Distributions 
99fad439e7SApple OSS Distributions #if __GNUC__ >= 3
100a5e72196SApple OSS Distributions void __dead2
__cxa_pure_virtual(void)101a5e72196SApple OSS Distributions __cxa_pure_virtual( void )
102a5e72196SApple OSS Distributions {
103a5e72196SApple OSS Distributions 	panic("%s", __FUNCTION__);
104a5e72196SApple OSS Distributions }
105fad439e7SApple OSS Distributions #else
106a5e72196SApple OSS Distributions void __dead2
__pure_virtual(void)107a5e72196SApple OSS Distributions __pure_virtual( void )
108a5e72196SApple OSS Distributions {
109a5e72196SApple OSS Distributions 	panic("%s", __FUNCTION__);
110a5e72196SApple OSS Distributions }
111fad439e7SApple OSS Distributions #endif
112c1dac77fSApple OSS Distributions 
113cc9a6355SApple OSS Distributions extern lck_grp_t * IOLockGroup;
114cc9a6355SApple OSS Distributions extern kmod_info_t g_kernel_kmod_info;
115*94d3b452SApple OSS Distributions #if CONFIG_SPTM
116*94d3b452SApple OSS Distributions extern kmod_info_t g_sptm_kmod_info, g_txm_kmod_info;
117*94d3b452SApple OSS Distributions #endif /* CONFIG_SPTM */
118cc9a6355SApple OSS Distributions 
119cc9a6355SApple OSS Distributions enum {
120cc9a6355SApple OSS Distributions 	kOSSectionNamesDefault     = 0,
121cc9a6355SApple OSS Distributions 	kOSSectionNamesBuiltinKext = 1,
122cc9a6355SApple OSS Distributions 	kOSSectionNamesCount       = 2,
123cc9a6355SApple OSS Distributions };
124cc9a6355SApple OSS Distributions enum {
125cc9a6355SApple OSS Distributions 	kOSSectionNameInitializer = 0,
126cc9a6355SApple OSS Distributions 	kOSSectionNameFinalizer   = 1,
127cc9a6355SApple OSS Distributions 	kOSSectionNameCount       = 2
128cc9a6355SApple OSS Distributions };
129cc9a6355SApple OSS Distributions 
130cc9a6355SApple OSS Distributions static const char *
131cc9a6355SApple OSS Distributions     gOSStructorSectionNames[kOSSectionNamesCount][kOSSectionNameCount] = {
132cc9a6355SApple OSS Distributions 	{ SECT_MODINITFUNC, SECT_MODTERMFUNC },
133cc9a6355SApple OSS Distributions 	{ kBuiltinInitSection, kBuiltinTermSection }
134cc9a6355SApple OSS Distributions };
135cc9a6355SApple OSS Distributions 
136a5e72196SApple OSS Distributions void
OSlibkernInit(void)137a5e72196SApple OSS Distributions OSlibkernInit(void)
138cc9a6355SApple OSS Distributions {
139cc9a6355SApple OSS Distributions 	// This must be called before calling OSRuntimeInitializeCPP.
140cc9a6355SApple OSS Distributions 	OSMetaClassBase::initialize();
141cc9a6355SApple OSS Distributions 
142cc9a6355SApple OSS Distributions 	g_kernel_kmod_info.address = (vm_address_t) &_mh_execute_header;
143*94d3b452SApple OSS Distributions #if CONFIG_SPTM
144*94d3b452SApple OSS Distributions 	g_sptm_kmod_info.address = (vm_offset_t)SPTMArgs->debug_header->image[DEBUG_HEADER_ENTRY_SPTM];
145*94d3b452SApple OSS Distributions 	g_txm_kmod_info.address = (vm_offset_t)SPTMArgs->debug_header->image[DEBUG_HEADER_ENTRY_TXM];
146*94d3b452SApple OSS Distributions #endif /* CONFIG_SPTM */
147aca3beaaSApple OSS Distributions 
148cc9a6355SApple OSS Distributions 	if (kOSReturnSuccess != OSRuntimeInitializeCPP(NULL)) {
149cc9a6355SApple OSS Distributions 		// &g_kernel_kmod_info, gOSSectionNamesStandard, 0, 0)) {
150cc9a6355SApple OSS Distributions 		panic("OSRuntime: C++ runtime failed to initialize.");
151cc9a6355SApple OSS Distributions 	}
152cc9a6355SApple OSS Distributions 
153cc9a6355SApple OSS Distributions 	gKernelCPPInitialized = true;
154cc9a6355SApple OSS Distributions 
155cc9a6355SApple OSS Distributions 	return;
156cc9a6355SApple OSS Distributions }
157cc9a6355SApple OSS Distributions 
158cc9a6355SApple OSS Distributions __END_DECLS
159cc9a6355SApple OSS Distributions 
160cc9a6355SApple OSS Distributions #if PRAGMA_MARK
161cc9a6355SApple OSS Distributions #pragma mark C++ Runtime Load/Unload
162cc9a6355SApple OSS Distributions #endif /* PRAGMA_MARK */
163cc9a6355SApple OSS Distributions /*********************************************************************
164cc9a6355SApple OSS Distributions * kern_os C++ Runtime Load/Unload
165cc9a6355SApple OSS Distributions *********************************************************************/
166cc9a6355SApple OSS Distributions 
167c1dac77fSApple OSS Distributions typedef void (*structor_t)(void);
168c1dac77fSApple OSS Distributions 
169cc9a6355SApple 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)170cc9a6355SApple OSS Distributions OSRuntimeCallStructorsInSection(
171cc9a6355SApple OSS Distributions 	OSKext                   * theKext,
172cc9a6355SApple OSS Distributions 	kmod_info_t              * kmodInfo,
173cc9a6355SApple OSS Distributions 	void                     * metaHandle,
1743ca3bd55SApple OSS Distributions 	kernel_segment_command_t * segment,
175cc9a6355SApple OSS Distributions 	const char               * sectionName,
176cc9a6355SApple OSS Distributions 	uintptr_t                  textStart,
177cc9a6355SApple OSS Distributions 	uintptr_t                  textEnd)
1783ca3bd55SApple OSS Distributions {
179cc9a6355SApple OSS Distributions 	kernel_section_t * section;
180cc9a6355SApple OSS Distributions 	bool result = TRUE;
181c1dac77fSApple OSS Distributions 
182c1dac77fSApple OSS Distributions 	for (section = firstsect(segment);
183cc9a6355SApple OSS Distributions 	    section != NULL;
184a5e72196SApple OSS Distributions 	    section = nextsect(segment, section)) {
185a5e72196SApple OSS Distributions 		if (strncmp(section->sectname, sectionName, sizeof(section->sectname) - 1)) {
186a5e72196SApple OSS Distributions 			continue;
187a5e72196SApple OSS Distributions 		}
188bb611c8fSApple OSS Distributions 		if (section->size == 0) {
189bb611c8fSApple OSS Distributions 			continue;
190bb611c8fSApple OSS Distributions 		}
191c1dac77fSApple OSS Distributions 
192cc9a6355SApple OSS Distributions 		structor_t * structors = (structor_t *)section->addr;
193a5e72196SApple OSS Distributions 		if (!structors) {
194a5e72196SApple OSS Distributions 			continue;
195a5e72196SApple OSS Distributions 		}
196c1dac77fSApple OSS Distributions 
197cc9a6355SApple OSS Distributions 		structor_t structor;
198bb611c8fSApple OSS Distributions 		uintptr_t value;
199bb611c8fSApple OSS Distributions 		unsigned long num_structors = section->size / sizeof(structor_t);
200cc9a6355SApple OSS Distributions 		unsigned int hit_null_structor = 0;
201bb611c8fSApple OSS Distributions 		unsigned long firstIndex = 0;
202c1dac77fSApple OSS Distributions 
203a5e72196SApple OSS Distributions 		if (textStart) {
204cc9a6355SApple OSS Distributions 			// bsearch for any in range
205bb611c8fSApple OSS Distributions 			unsigned long baseIdx;
206bb611c8fSApple OSS Distributions 			unsigned long lim;
207cc9a6355SApple OSS Distributions 			firstIndex = num_structors;
208a5e72196SApple OSS Distributions 			for (lim = num_structors, baseIdx = 0; lim; lim >>= 1) {
209bb611c8fSApple OSS Distributions 				structor = structors[baseIdx + (lim >> 1)];
210bb611c8fSApple OSS Distributions 				if (!structor) {
211a5e72196SApple OSS Distributions 					panic("%s: null structor", kmodInfo->name);
212a5e72196SApple OSS Distributions 				}
213bb611c8fSApple OSS Distributions 				value = PTRAUTH_STRIP_STRUCTOR(structor);
214a5e72196SApple OSS Distributions 				if ((value >= textStart) && (value < textEnd)) {
215cc9a6355SApple OSS Distributions 					firstIndex = (baseIdx + (lim >> 1));
216cc9a6355SApple OSS Distributions 					// scan back for the first in range
217a5e72196SApple OSS Distributions 					for (; firstIndex; firstIndex--) {
218bb611c8fSApple OSS Distributions 						structor = structors[firstIndex - 1];
219bb611c8fSApple OSS Distributions 						value = PTRAUTH_STRIP_STRUCTOR(structor);
220a5e72196SApple OSS Distributions 						if ((value < textStart) || (value >= textEnd)) {
221a5e72196SApple OSS Distributions 							break;
222a5e72196SApple OSS Distributions 						}
223cc9a6355SApple OSS Distributions 					}
224cc9a6355SApple OSS Distributions 					break;
225cc9a6355SApple OSS Distributions 				}
226a5e72196SApple OSS Distributions 				if (textStart > value) {
227cc9a6355SApple OSS Distributions 					// move right
228cc9a6355SApple OSS Distributions 					baseIdx += (lim >> 1) + 1;
229cc9a6355SApple OSS Distributions 					lim--;
230cc9a6355SApple OSS Distributions 				}
231cc9a6355SApple OSS Distributions 				// else move left
232cc9a6355SApple OSS Distributions 			}
233cc9a6355SApple OSS Distributions 			baseIdx = (baseIdx + (lim >> 1));
234cc9a6355SApple OSS Distributions 		}
235cc9a6355SApple OSS Distributions 		for (;
236cc9a6355SApple OSS Distributions 		    (firstIndex < num_structors)
237cc9a6355SApple OSS Distributions 		    && (!metaHandle || OSMetaClass::checkModLoad(metaHandle));
238a5e72196SApple OSS Distributions 		    firstIndex++) {
239a5e72196SApple OSS Distributions 			if ((structor = structors[firstIndex])) {
240bb611c8fSApple OSS Distributions 				value = PTRAUTH_STRIP_STRUCTOR(structor);
241bb611c8fSApple OSS Distributions 				if ((textStart && (value < textStart))
242bb611c8fSApple OSS Distributions 				    || (textEnd && (value >= textEnd))) {
243a5e72196SApple OSS Distributions 					break;
244cc9a6355SApple OSS Distributions 				}
245a5e72196SApple OSS Distributions 				(*structor)();
246a5e72196SApple OSS Distributions 			} else if (!hit_null_structor) {
247cc9a6355SApple OSS Distributions 				hit_null_structor = 1;
248cc9a6355SApple OSS Distributions 				OSRuntimeLog(theKext, kOSRuntimeLogSpec,
249cc9a6355SApple OSS Distributions 				    "Null structor in kext %s segment %s!",
250cc9a6355SApple OSS Distributions 				    kmodInfo->name, section->segname);
251c1dac77fSApple OSS Distributions 			}
252c1dac77fSApple OSS Distributions 		}
253a5e72196SApple OSS Distributions 		if (metaHandle) {
254a5e72196SApple OSS Distributions 			result = OSMetaClass::checkModLoad(metaHandle);
255a5e72196SApple OSS Distributions 		}
256cc9a6355SApple OSS Distributions 		break;
257cc9a6355SApple OSS Distributions 	} /* for (section...) */
258a5e72196SApple OSS Distributions 	return result;
2593ca3bd55SApple OSS Distributions }
2603ca3bd55SApple OSS Distributions 
2613ca3bd55SApple OSS Distributions /*********************************************************************
2623ca3bd55SApple OSS Distributions *********************************************************************/
2633ca3bd55SApple OSS Distributions kern_return_t
OSRuntimeFinalizeCPP(OSKext * theKext)2643ca3bd55SApple OSS Distributions OSRuntimeFinalizeCPP(
265cc9a6355SApple OSS Distributions 	OSKext                   * theKext)
266c1dac77fSApple OSS Distributions {
2673ca3bd55SApple OSS Distributions 	kern_return_t              result = KMOD_RETURN_FAILURE;
2683ca3bd55SApple OSS Distributions 	void                     * metaHandle = NULL;// do not free
269cc9a6355SApple OSS Distributions 	kernel_mach_header_t     * header;
270cc9a6355SApple OSS Distributions 	kernel_segment_command_t * segment;
271cc9a6355SApple OSS Distributions 	kmod_info_t              * kmodInfo;
272cc9a6355SApple OSS Distributions 	const char              ** sectionNames;
273cc9a6355SApple OSS Distributions 	uintptr_t                  textStart;
274cc9a6355SApple OSS Distributions 	uintptr_t                  textEnd;
275c1dac77fSApple OSS Distributions 
276cc9a6355SApple OSS Distributions 	textStart    = 0;
277cc9a6355SApple OSS Distributions 	textEnd      = 0;
278cc9a6355SApple OSS Distributions 	sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
279cc9a6355SApple OSS Distributions 	if (theKext) {
280cc9a6355SApple OSS Distributions 		if (!theKext->isCPPInitialized()) {
2813ca3bd55SApple OSS Distributions 			result = KMOD_RETURN_SUCCESS;
2823ca3bd55SApple OSS Distributions 			goto finish;
2833ca3bd55SApple OSS Distributions 		}
284cc9a6355SApple OSS Distributions 		kmodInfo = theKext->kmod_info;
285cc9a6355SApple OSS Distributions 		if (!kmodInfo || !kmodInfo->address) {
286cc9a6355SApple OSS Distributions 			result = kOSKextReturnInvalidArgument;
287cc9a6355SApple OSS Distributions 			goto finish;
288cc9a6355SApple OSS Distributions 		}
289cc9a6355SApple OSS Distributions 		header = (kernel_mach_header_t *)kmodInfo->address;
290cc9a6355SApple OSS Distributions 		if (theKext->flags.builtin) {
291cc9a6355SApple OSS Distributions 			header       = (kernel_mach_header_t *)g_kernel_kmod_info.address;
292cc9a6355SApple OSS Distributions 			textStart    = kmodInfo->address;
293cc9a6355SApple OSS Distributions 			textEnd      = textStart + kmodInfo->size;
294cc9a6355SApple OSS Distributions 			sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
295cc9a6355SApple OSS Distributions 		}
296cc9a6355SApple OSS Distributions 	} else {
297cc9a6355SApple OSS Distributions 		kmodInfo = &g_kernel_kmod_info;
298cc9a6355SApple OSS Distributions 		header   = (kernel_mach_header_t *)kmodInfo->address;
299cc9a6355SApple OSS Distributions 	}
3003ca3bd55SApple OSS Distributions 
3013ca3bd55SApple OSS Distributions 	/* OSKext checks for this condition now, but somebody might call
3023ca3bd55SApple OSS Distributions 	 * this function directly (the symbol is exported....).
3033ca3bd55SApple OSS Distributions 	 */
3043ca3bd55SApple OSS Distributions 	if (OSMetaClass::modHasInstance(kmodInfo->name)) {
3053ca3bd55SApple OSS Distributions 		// xxx - Don't log under errors? this is more of an info thing
3063ca3bd55SApple OSS Distributions 		OSRuntimeLog(theKext, kOSRuntimeLogSpec,
3073ca3bd55SApple OSS Distributions 		    "Can't tear down kext %s C++; classes have instances:",
3083ca3bd55SApple OSS Distributions 		    kmodInfo->name);
3093ca3bd55SApple OSS Distributions 		OSKext::reportOSMetaClassInstances(kmodInfo->name, kOSRuntimeLogSpec);
3103ca3bd55SApple OSS Distributions 		result = kOSMetaClassHasInstances;
3113ca3bd55SApple OSS Distributions 		goto finish;
3123ca3bd55SApple OSS Distributions 	}
3133ca3bd55SApple OSS Distributions 
3143ca3bd55SApple OSS Distributions 	/* Tell the meta class system that we are starting to unload.
3153ca3bd55SApple OSS Distributions 	 * metaHandle isn't actually needed on the finalize path,
3163ca3bd55SApple OSS Distributions 	 * so we don't check it here, even though OSMetaClass::postModLoad() will
3173ca3bd55SApple OSS Distributions 	 * return a failure (it only does actual work on the init path anyhow).
3183ca3bd55SApple OSS Distributions 	 */
3193ca3bd55SApple OSS Distributions 	metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
320cc9a6355SApple OSS Distributions 
321cc9a6355SApple OSS Distributions 	OSSymbol::checkForPageUnload((void *)kmodInfo->address,
322cc9a6355SApple OSS Distributions 	    (void *)(kmodInfo->address + kmodInfo->size));
323cc9a6355SApple OSS Distributions 
324cc9a6355SApple OSS Distributions 	header = (kernel_mach_header_t *)kmodInfo->address;
325cc9a6355SApple OSS Distributions 	segment = firstsegfromheader(header);
326cc9a6355SApple OSS Distributions 
327cc9a6355SApple OSS Distributions 	for (segment = firstsegfromheader(header);
328a5e72196SApple OSS Distributions 	    segment != NULL;
329cc9a6355SApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
330cc9a6355SApple OSS Distributions 		OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
331cc9a6355SApple OSS Distributions 		    sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
332cc9a6355SApple OSS Distributions 	}
333cc9a6355SApple OSS Distributions 
334c1dac77fSApple OSS Distributions 	(void)OSMetaClass::postModLoad(metaHandle);
335c1dac77fSApple OSS Distributions 
3363ca3bd55SApple OSS Distributions 	if (theKext) {
3373ca3bd55SApple OSS Distributions 		theKext->setCPPInitialized(false);
3383ca3bd55SApple OSS Distributions 	}
3393ca3bd55SApple OSS Distributions 	result = KMOD_RETURN_SUCCESS;
3403ca3bd55SApple OSS Distributions finish:
3413ca3bd55SApple OSS Distributions 	return result;
342c1dac77fSApple OSS Distributions }
343c1dac77fSApple OSS Distributions 
344bb611c8fSApple OSS Distributions #if defined(HAS_APPLE_PAC)
345e6231be0SApple OSS Distributions #if !KASAN
346e6231be0SApple OSS Distributions /*
347e6231be0SApple OSS Distributions  * Place this function in __KLD,__text on non-kasan builds so it gets unmapped
348e6231be0SApple OSS Distributions  * after CTRR lockdown.
349e6231be0SApple OSS Distributions  */
350e6231be0SApple OSS Distributions __attribute__((noinline, section("__KLD,__text")))
351e6231be0SApple OSS Distributions #endif
352e6231be0SApple OSS Distributions static void
OSRuntimeSignStructorsInSegment(kernel_segment_command_t * segment)353bb611c8fSApple OSS Distributions OSRuntimeSignStructorsInSegment(kernel_segment_command_t *segment)
354bb611c8fSApple OSS Distributions {
355bb611c8fSApple OSS Distributions 	kernel_section_t         * section;
356bb611c8fSApple OSS Distributions 	structor_t               * structors;
357bb611c8fSApple OSS Distributions 	volatile structor_t                 structor;
358bb611c8fSApple OSS Distributions 	size_t                     idx, num_structors;
359bb611c8fSApple OSS Distributions 
360bb611c8fSApple OSS Distributions 	for (section = firstsect(segment);
361bb611c8fSApple OSS Distributions 	    section != NULL;
362bb611c8fSApple OSS Distributions 	    section = nextsect(segment, section)) {
363bb611c8fSApple OSS Distributions 		if ((S_MOD_INIT_FUNC_POINTERS != (SECTION_TYPE & section->flags))
364bb611c8fSApple OSS Distributions 		    && (S_MOD_TERM_FUNC_POINTERS != (SECTION_TYPE & section->flags))) {
365bb611c8fSApple OSS Distributions 			continue;
366bb611c8fSApple OSS Distributions 		}
367bb611c8fSApple OSS Distributions 		structors = (structor_t *)section->addr;
368bb611c8fSApple OSS Distributions 		if (!structors) {
369bb611c8fSApple OSS Distributions 			continue;
370bb611c8fSApple OSS Distributions 		}
371bb611c8fSApple OSS Distributions 		num_structors = section->size / sizeof(structor_t);
372bb611c8fSApple OSS Distributions 		for (idx = 0; idx < num_structors; idx++) {
373bb611c8fSApple OSS Distributions 			structor = structors[idx];
374bb611c8fSApple OSS Distributions 			if (NULL == structor) {
375bb611c8fSApple OSS Distributions 				continue;
376bb611c8fSApple OSS Distributions 			}
377bb611c8fSApple OSS Distributions 			structor = ptrauth_strip(structor, ptrauth_key_function_pointer);
378bb611c8fSApple OSS Distributions 			structor = ptrauth_sign_unauthenticated(structor, ptrauth_key_function_pointer, ptrauth_function_pointer_type_discriminator(void (*)(void)));
379bb611c8fSApple OSS Distributions 			structors[idx] = structor;
380bb611c8fSApple OSS Distributions 		}
381bb611c8fSApple OSS Distributions 	} /* for (section...) */
382bb611c8fSApple OSS Distributions }
383bb611c8fSApple OSS Distributions #endif
384bb611c8fSApple OSS Distributions 
385bb611c8fSApple OSS Distributions /*********************************************************************
386bb611c8fSApple OSS Distributions *********************************************************************/
387bb611c8fSApple OSS Distributions void
OSRuntimeSignStructors(kernel_mach_header_t * header __unused)388bb611c8fSApple OSS Distributions OSRuntimeSignStructors(
389bb611c8fSApple OSS Distributions 	kernel_mach_header_t * header __unused)
390bb611c8fSApple OSS Distributions {
391bb611c8fSApple OSS Distributions #if defined(HAS_APPLE_PAC)
392bb611c8fSApple OSS Distributions 
393bb611c8fSApple OSS Distributions 	kernel_segment_command_t * segment;
394bb611c8fSApple OSS Distributions 
395bb611c8fSApple OSS Distributions 	for (segment = firstsegfromheader(header);
396bb611c8fSApple OSS Distributions 	    segment != NULL;
397bb611c8fSApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
398bb611c8fSApple OSS Distributions 		OSRuntimeSignStructorsInSegment(segment);
399bb611c8fSApple OSS Distributions 	} /* for (segment...) */
400bb611c8fSApple OSS Distributions #endif /* !defined(XXX) && defined(HAS_APPLE_PAC) */
401bb611c8fSApple OSS Distributions }
402bb611c8fSApple OSS Distributions 
403bb611c8fSApple OSS Distributions /*********************************************************************
404bb611c8fSApple OSS Distributions *********************************************************************/
405bb611c8fSApple OSS Distributions void
OSRuntimeSignStructorsInFileset(kernel_mach_header_t * fileset_header __unused)406bb611c8fSApple OSS Distributions OSRuntimeSignStructorsInFileset(
407bb611c8fSApple OSS Distributions 	kernel_mach_header_t * fileset_header __unused)
408bb611c8fSApple OSS Distributions {
409bb611c8fSApple OSS Distributions #if defined(HAS_APPLE_PAC)
410bb611c8fSApple OSS Distributions 	struct load_command *lc;
411bb611c8fSApple OSS Distributions 
412bb611c8fSApple OSS Distributions 	lc = (struct load_command *)((uintptr_t)fileset_header + sizeof(*fileset_header));
413bb611c8fSApple OSS Distributions 	for (uint32_t i = 0; i < fileset_header->ncmds; i++,
414bb611c8fSApple OSS Distributions 	    lc = (struct load_command *)((uintptr_t)lc + lc->cmdsize)) {
415bb611c8fSApple OSS Distributions 		if (lc->cmd == LC_FILESET_ENTRY) {
416bb611c8fSApple OSS Distributions 			struct fileset_entry_command *fse;
417bb611c8fSApple OSS Distributions 			kernel_mach_header_t *mh;
418bb611c8fSApple OSS Distributions 
419bb611c8fSApple OSS Distributions 			fse = (struct fileset_entry_command *)(uintptr_t)lc;
420bb611c8fSApple OSS Distributions 			mh = (kernel_mach_header_t *)((uintptr_t)fse->vmaddr);
421bb611c8fSApple OSS Distributions 			OSRuntimeSignStructors(mh);
422bb611c8fSApple OSS Distributions 		} else if (lc->cmd == LC_SEGMENT_64) {
423bb611c8fSApple OSS Distributions 			/*
424bb611c8fSApple OSS Distributions 			 * Slide/adjust all LC_SEGMENT_64 commands in the fileset
425bb611c8fSApple OSS Distributions 			 * (and any sections in those segments)
426bb611c8fSApple OSS Distributions 			 */
427bb611c8fSApple OSS Distributions 			kernel_segment_command_t *seg;
428bb611c8fSApple OSS Distributions 			seg = (kernel_segment_command_t *)(uintptr_t)lc;
429bb611c8fSApple OSS Distributions 			OSRuntimeSignStructorsInSegment(seg);
430bb611c8fSApple OSS Distributions 		}
431bb611c8fSApple OSS Distributions 	}
432bb611c8fSApple OSS Distributions 
433bb611c8fSApple OSS Distributions #endif /* defined(HAS_APPLE_PAC) */
434bb611c8fSApple OSS Distributions }
435bb611c8fSApple OSS Distributions 
4363ca3bd55SApple OSS Distributions /*********************************************************************
4373ca3bd55SApple OSS Distributions *********************************************************************/
4383ca3bd55SApple OSS Distributions kern_return_t
OSRuntimeInitializeCPP(OSKext * theKext)4393ca3bd55SApple OSS Distributions OSRuntimeInitializeCPP(
440cc9a6355SApple OSS Distributions 	OSKext                   * theKext)
441c1dac77fSApple OSS Distributions {
4423ca3bd55SApple OSS Distributions 	kern_return_t              result          = KMOD_RETURN_FAILURE;
4433ca3bd55SApple OSS Distributions 	kernel_mach_header_t     * header          = NULL;
4443ca3bd55SApple OSS Distributions 	void                     * metaHandle      = NULL;// do not free
4453ca3bd55SApple OSS Distributions 	bool                       load_success    = true;
4463ca3bd55SApple OSS Distributions 	kernel_segment_command_t * segment         = NULL;// do not free
4473ca3bd55SApple OSS Distributions 	kernel_segment_command_t * failure_segment = NULL; // do not free
448cc9a6355SApple OSS Distributions 	kmod_info_t             *  kmodInfo;
449cc9a6355SApple OSS Distributions 	const char              ** sectionNames;
450cc9a6355SApple OSS Distributions 	uintptr_t                  textStart;
451cc9a6355SApple OSS Distributions 	uintptr_t                  textEnd;
452c1dac77fSApple OSS Distributions 
453cc9a6355SApple OSS Distributions 	textStart    = 0;
454cc9a6355SApple OSS Distributions 	textEnd      = 0;
455cc9a6355SApple OSS Distributions 	sectionNames = gOSStructorSectionNames[kOSSectionNamesDefault];
456cc9a6355SApple OSS Distributions 	if (theKext) {
457cc9a6355SApple OSS Distributions 		if (theKext->isCPPInitialized()) {
4583ca3bd55SApple OSS Distributions 			result = KMOD_RETURN_SUCCESS;
4593ca3bd55SApple OSS Distributions 			goto finish;
4603ca3bd55SApple OSS Distributions 		}
4613ca3bd55SApple OSS Distributions 
462cc9a6355SApple OSS Distributions 		kmodInfo = theKext->kmod_info;
463cc9a6355SApple OSS Distributions 		if (!kmodInfo || !kmodInfo->address) {
464cc9a6355SApple OSS Distributions 			result = kOSKextReturnInvalidArgument;
465cc9a6355SApple OSS Distributions 			goto finish;
466cc9a6355SApple OSS Distributions 		}
4673ca3bd55SApple OSS Distributions 		header = (kernel_mach_header_t *)kmodInfo->address;
4683ca3bd55SApple OSS Distributions 
469cc9a6355SApple OSS Distributions 		if (theKext->flags.builtin) {
470cc9a6355SApple OSS Distributions 			header       = (kernel_mach_header_t *)g_kernel_kmod_info.address;
471cc9a6355SApple OSS Distributions 			textStart    = kmodInfo->address;
472cc9a6355SApple OSS Distributions 			textEnd      = textStart + kmodInfo->size;
473cc9a6355SApple OSS Distributions 			sectionNames = gOSStructorSectionNames[kOSSectionNamesBuiltinKext];
474cc9a6355SApple OSS Distributions 		}
475cc9a6355SApple OSS Distributions 	} else {
476cc9a6355SApple OSS Distributions 		kmodInfo = &g_kernel_kmod_info;
477cc9a6355SApple OSS Distributions 		header   = (kernel_mach_header_t *)kmodInfo->address;
478cc9a6355SApple OSS Distributions 	}
479cc9a6355SApple OSS Distributions 
4803ca3bd55SApple OSS Distributions 	/* Tell the meta class system that we are starting the load
4813ca3bd55SApple OSS Distributions 	 */
4823ca3bd55SApple OSS Distributions 	metaHandle = OSMetaClass::preModLoad(kmodInfo->name);
483c1dac77fSApple OSS Distributions 	assert(metaHandle);
4843ca3bd55SApple OSS Distributions 	if (!metaHandle) {
4853ca3bd55SApple OSS Distributions 		goto finish;
4863ca3bd55SApple OSS Distributions 	}
487c1dac77fSApple OSS Distributions 
4883ca3bd55SApple OSS Distributions 	/* NO GOTO PAST HERE. */
489c1dac77fSApple OSS Distributions 
4903ca3bd55SApple OSS Distributions 	/* Scan the header for all constructor sections, in any
491c1dac77fSApple OSS Distributions 	 * segment, and invoke the constructors within those sections.
492c1dac77fSApple OSS Distributions 	 */
493c1dac77fSApple OSS Distributions 	for (segment = firstsegfromheader(header);
4943ca3bd55SApple OSS Distributions 	    segment != NULL && load_success;
495a5e72196SApple OSS Distributions 	    segment = nextsegfromheader(header, segment)) {
496c1dac77fSApple OSS Distributions 		/* Record the current segment in the event of a failure.
497c1dac77fSApple OSS Distributions 		 */
498c1dac77fSApple OSS Distributions 		failure_segment = segment;
499cc9a6355SApple OSS Distributions 		load_success = OSRuntimeCallStructorsInSection(
500cc9a6355SApple OSS Distributions 			theKext, kmodInfo, metaHandle, segment,
501cc9a6355SApple OSS Distributions 			sectionNames[kOSSectionNameInitializer],
502cc9a6355SApple OSS Distributions 			textStart, textEnd);
503c1dac77fSApple OSS Distributions 	} /* for (segment...) */
504c1dac77fSApple OSS Distributions 
5053ca3bd55SApple OSS Distributions 	/* We failed so call all of the destructors. We must do this before
5063ca3bd55SApple OSS Distributions 	 * calling OSMetaClass::postModLoad() as the OSMetaClass destructors
5073ca3bd55SApple OSS Distributions 	 * will alter state (in the metaHandle) used by that function.
5083ca3bd55SApple OSS Distributions 	 */
509c1dac77fSApple OSS Distributions 	if (!load_success) {
5103ca3bd55SApple OSS Distributions 		/* Scan the header for all destructor sections, in any
511c1dac77fSApple OSS Distributions 		 * segment, and invoke the constructors within those sections.
512c1dac77fSApple OSS Distributions 		 */
513c1dac77fSApple OSS Distributions 		for (segment = firstsegfromheader(header);
514a5e72196SApple OSS Distributions 		    segment != failure_segment && segment != NULL;
5153ca3bd55SApple OSS Distributions 		    segment = nextsegfromheader(header, segment)) {
516cc9a6355SApple OSS Distributions 			OSRuntimeCallStructorsInSection(theKext, kmodInfo, NULL, segment,
517cc9a6355SApple OSS Distributions 			    sectionNames[kOSSectionNameFinalizer], textStart, textEnd);
518c1dac77fSApple OSS Distributions 		} /* for (segment...) */
519c1dac77fSApple OSS Distributions 	}
520c1dac77fSApple OSS Distributions 
5213ca3bd55SApple OSS Distributions 	/* Now, regardless of success so far, do the post-init registration
5223ca3bd55SApple OSS Distributions 	 * and cleanup. If we had to call the unloadCPP function, static
5233ca3bd55SApple OSS Distributions 	 * destructors have removed classes from the stalled list so no
5243ca3bd55SApple OSS Distributions 	 * metaclasses will actually be registered.
5253ca3bd55SApple OSS Distributions 	 */
5263ca3bd55SApple OSS Distributions 	result = OSMetaClass::postModLoad(metaHandle);
5273ca3bd55SApple OSS Distributions 
5283ca3bd55SApple OSS Distributions 	/* If we've otherwise been fine up to now, but OSMetaClass::postModLoad()
5293ca3bd55SApple OSS Distributions 	 * fails (typically due to a duplicate class), tear down all the C++
5303ca3bd55SApple OSS Distributions 	 * stuff from the kext. This isn't necessary for libkern/OSMetaClass stuff,
5313ca3bd55SApple OSS Distributions 	 * but may be necessary for other C++ code. We ignore the return value
5323ca3bd55SApple OSS Distributions 	 * because it's only a fail when there are existing instances of libkern
5333ca3bd55SApple OSS Distributions 	 * classes, and there had better not be any created on the C++ init path.
5343ca3bd55SApple OSS Distributions 	 */
5353ca3bd55SApple OSS Distributions 	if (load_success && result != KMOD_RETURN_SUCCESS) {
536cc9a6355SApple OSS Distributions 		(void)OSRuntimeFinalizeCPP(theKext); //kmodInfo, sectionNames, textStart, textEnd);
537c1dac77fSApple OSS Distributions 	}
538c1dac77fSApple OSS Distributions 
5393ca3bd55SApple OSS Distributions 	if (theKext && load_success && result == KMOD_RETURN_SUCCESS) {
5403ca3bd55SApple OSS Distributions 		theKext->setCPPInitialized(true);
5413ca3bd55SApple OSS Distributions 	}
5423ca3bd55SApple OSS Distributions finish:
5433ca3bd55SApple OSS Distributions 	return result;
5443ca3bd55SApple OSS Distributions }
545e13b1fa5SApple OSS Distributions 
5463ca3bd55SApple OSS Distributions /*********************************************************************
547a5e72196SApple OSS Distributions *   Unload a kernel segment.
5483ca3bd55SApple OSS Distributions *********************************************************************/
5493ca3bd55SApple OSS Distributions 
550cc9a6355SApple OSS Distributions void
OSRuntimeUnloadCPPForSegment(kernel_segment_command_t * segment)551cc9a6355SApple OSS Distributions OSRuntimeUnloadCPPForSegment(
552cc9a6355SApple OSS Distributions 	kernel_segment_command_t * segment)
553c1dac77fSApple OSS Distributions {
554cc9a6355SApple OSS Distributions 	OSRuntimeCallStructorsInSection(NULL, &g_kernel_kmod_info, NULL, segment,
555cc9a6355SApple OSS Distributions 	    gOSStructorSectionNames[kOSSectionNamesDefault][kOSSectionNameFinalizer], 0, 0);
5563ca3bd55SApple OSS Distributions }
557c1dac77fSApple OSS Distributions 
5583ca3bd55SApple OSS Distributions #if PRAGMA_MARK
5593ca3bd55SApple OSS Distributions #pragma mark C++ Allocators & Deallocators
5603ca3bd55SApple OSS Distributions #endif /* PRAGMA_MARK */
5613ca3bd55SApple OSS Distributions /*********************************************************************
5623ca3bd55SApple OSS Distributions * C++ Allocators & Deallocators
5633ca3bd55SApple OSS Distributions *********************************************************************/
5645c2921b0SApple OSS Distributions __typed_allocators_ignore_push
5655c2921b0SApple OSS Distributions 
5663ca3bd55SApple OSS Distributions void *
operator new(size_t size)5673ca3bd55SApple OSS Distributions operator new(size_t size)
568c1dac77fSApple OSS Distributions {
569bb611c8fSApple OSS Distributions 	assert(size);
570e7776783SApple OSS Distributions 	return kheap_alloc(KERN_OS_MALLOC, size,
571e7776783SApple OSS Distributions 	           Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
572c1dac77fSApple OSS Distributions }
573c1dac77fSApple OSS Distributions 
5743ca3bd55SApple OSS Distributions void
operator delete(void * addr)5753ca3bd55SApple OSS Distributions operator delete(void * addr)
5760f3703acSApple OSS Distributions #if __cplusplus >= 201103L
5770f3703acSApple OSS Distributions noexcept
5780f3703acSApple OSS Distributions #endif
579c1dac77fSApple OSS Distributions {
580bb611c8fSApple OSS Distributions 	kheap_free_addr(KERN_OS_MALLOC, addr);
5813ca3bd55SApple OSS Distributions 	return;
582c1dac77fSApple OSS Distributions }
583c1dac77fSApple OSS Distributions 
5843ca3bd55SApple OSS Distributions void *
operator new[](unsigned long size)585e7776783SApple OSS Distributions operator new[](unsigned long size)
5863ca3bd55SApple OSS Distributions {
587e7776783SApple OSS Distributions 	return kheap_alloc(KERN_OS_MALLOC, size,
588e7776783SApple OSS Distributions 	           Z_VM_TAG_BT(Z_WAITOK_ZERO, VM_KERN_MEMORY_LIBKERN));
5893ca3bd55SApple OSS Distributions }
5903ca3bd55SApple OSS Distributions 
5913ca3bd55SApple OSS Distributions void
operator delete[](void * ptr)5923ca3bd55SApple OSS Distributions operator delete[](void * ptr)
5930f3703acSApple OSS Distributions #if __cplusplus >= 201103L
5940f3703acSApple OSS Distributions noexcept
5950f3703acSApple OSS Distributions #endif
5963ca3bd55SApple OSS Distributions {
5973ca3bd55SApple OSS Distributions 	if (ptr) {
598a5e72196SApple OSS Distributions #if KASAN
599a5e72196SApple OSS Distributions 		/*
600a5e72196SApple OSS Distributions 		 * Unpoison the C++ array cookie inserted (but not removed) by the
601a5e72196SApple OSS Distributions 		 * compiler on new[].
602a5e72196SApple OSS Distributions 		 */
603a5e72196SApple OSS Distributions 		kasan_unpoison_cxx_array_cookie(ptr);
604a5e72196SApple OSS Distributions #endif
605bb611c8fSApple OSS Distributions 		kheap_free_addr(KERN_OS_MALLOC, ptr);
6063ca3bd55SApple OSS Distributions 	}
6073ca3bd55SApple OSS Distributions 	return;
6083ca3bd55SApple OSS Distributions }
6093ca3bd55SApple OSS Distributions 
610a5e72196SApple OSS Distributions #if __cplusplus >= 201103L
611a5e72196SApple OSS Distributions 
612a5e72196SApple OSS Distributions void
operator delete(void * addr,size_t sz)613a5e72196SApple OSS Distributions operator delete(void * addr, size_t sz) noexcept
614a5e72196SApple OSS Distributions {
615bb611c8fSApple OSS Distributions 	kheap_free(KERN_OS_MALLOC, addr, sz);
616a5e72196SApple OSS Distributions }
617a5e72196SApple OSS Distributions 
618a5e72196SApple OSS Distributions void
operator delete[](void * addr,size_t sz)619a5e72196SApple OSS Distributions operator delete[](void * addr, size_t sz) noexcept
620a5e72196SApple OSS Distributions {
621a5e72196SApple OSS Distributions 	if (addr) {
622bb611c8fSApple OSS Distributions 		kheap_free(KERN_OS_MALLOC, addr, sz);
623a5e72196SApple OSS Distributions 	}
624a5e72196SApple OSS Distributions }
625a5e72196SApple OSS Distributions 
6265c2921b0SApple OSS Distributions __typed_allocators_ignore_pop
6275c2921b0SApple OSS Distributions 
628a5e72196SApple OSS Distributions #endif /* __cplusplus >= 201103L */
629a5e72196SApple OSS Distributions 
6303ca3bd55SApple OSS Distributions /* PR-6481964 - The compiler is going to check for size overflows in calls to
6313ca3bd55SApple OSS Distributions  * new[], and if there is an overflow, it will call __throw_length_error.
6323ca3bd55SApple OSS Distributions  * This is an unrecoverable error by the C++ standard, so we must panic here.
6333ca3bd55SApple OSS Distributions  *
6343ca3bd55SApple OSS Distributions  * We have to put the function inside the std namespace because of how the
6353ca3bd55SApple OSS Distributions  * compiler expects the name to be mangled.
6363ca3bd55SApple OSS Distributions  */
6373ca3bd55SApple OSS Distributions namespace std {
638a5e72196SApple OSS Distributions void __dead2
__throw_length_error(const char * msg __unused)6393ca3bd55SApple OSS Distributions __throw_length_error(const char *msg __unused)
6403ca3bd55SApple OSS Distributions {
6413ca3bd55SApple OSS Distributions 	panic("Size of array created by new[] has overflowed");
6423ca3bd55SApple OSS Distributions }
6433ca3bd55SApple OSS Distributions };
644