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