xref: /xnu-10002.81.5/libkern/c++/OSSymbol.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2000-2016 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 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions #include <string.h>
33*5e3eaea3SApple OSS Distributions #include <sys/cdefs.h>
34*5e3eaea3SApple OSS Distributions 
35*5e3eaea3SApple OSS Distributions #include <kern/bits.h>
36*5e3eaea3SApple OSS Distributions #include <kern/locks.h>
37*5e3eaea3SApple OSS Distributions #include <kern/smr_hash.h>
38*5e3eaea3SApple OSS Distributions #include <kern/thread_call.h>
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions #if defined(__arm64__)
41*5e3eaea3SApple OSS Distributions #include <arm64/amcc_rorgn.h> /* rorgn_contains */
42*5e3eaea3SApple OSS Distributions #endif
43*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
44*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
45*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSLib.h>
46*5e3eaea3SApple OSS Distributions #include <os/cpp_util.h>
47*5e3eaea3SApple OSS Distributions #include <os/hash.h>
48*5e3eaea3SApple OSS Distributions #include <string.h>
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions static ZONE_DEFINE(OSSymbol_zone, "iokit.OSSymbol", sizeof(OSSymbol), ZC_NONE);
51*5e3eaea3SApple OSS Distributions static LCK_GRP_DECLARE(lock_group, "OSSymbolPool");
52*5e3eaea3SApple OSS Distributions 
53*5e3eaea3SApple OSS Distributions #pragma clang diagnostic push
54*5e3eaea3SApple OSS Distributions #pragma clang diagnostic ignored "-Winvalid-offsetof"
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions /*
57*5e3eaea3SApple OSS Distributions  * This implements a relativistic hash table, using <kern/smr.h> as underlying
58*5e3eaea3SApple OSS Distributions  * safe memory reclamation scheme.
59*5e3eaea3SApple OSS Distributions  *
60*5e3eaea3SApple OSS Distributions  * (https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf)
61*5e3eaea3SApple OSS Distributions  *
62*5e3eaea3SApple OSS Distributions  * One twist is that the OSSymbol_smr_free() callback must be
63*5e3eaea3SApple OSS Distributions  * preemption-disabled safe, which means the `kfree_data()` it calls _MUST_ be
64*5e3eaea3SApple OSS Distributions  * smaller than KALLOC_SAFE_ALLOC_SIZE. To deal with that, if a Symbol is made
65*5e3eaea3SApple OSS Distributions  * with a string that is much larger (should be rare), these go on a lock-based
66*5e3eaea3SApple OSS Distributions  * "huge" queue.
67*5e3eaea3SApple OSS Distributions  */
68*5e3eaea3SApple OSS Distributions class OSSymbolPool
69*5e3eaea3SApple OSS Distributions {
70*5e3eaea3SApple OSS Distributions 	/* empirically most devices have at least 10+k symbols */
71*5e3eaea3SApple OSS Distributions 	static constexpr uint32_t MIN_SIZE = 4096;
72*5e3eaea3SApple OSS Distributions 
73*5e3eaea3SApple OSS Distributions 	static inline smrh_key_t
OSSymbol_get_key(const OSSymbol * sym)74*5e3eaea3SApple OSS Distributions 	OSSymbol_get_key(const OSSymbol *sym)
75*5e3eaea3SApple OSS Distributions 	{
76*5e3eaea3SApple OSS Distributions 		return {
77*5e3eaea3SApple OSS Distributions 			       .smrk_string = sym->string,
78*5e3eaea3SApple OSS Distributions 			       .smrk_len    = (size_t)(sym->length - 1)
79*5e3eaea3SApple OSS Distributions 		};
80*5e3eaea3SApple OSS Distributions 	}
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions 	static uint32_t
OSSymbol_obj_hash(const struct smrq_slink * link,uint32_t seed)83*5e3eaea3SApple OSS Distributions 	OSSymbol_obj_hash(const struct smrq_slink *link, uint32_t seed)
84*5e3eaea3SApple OSS Distributions 	{
85*5e3eaea3SApple OSS Distributions 		OSSymbol *sym = __container_of(link, OSSymbol, hashlink);
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions 		return smrh_key_hash_str(OSSymbol_get_key(sym), seed);
88*5e3eaea3SApple OSS Distributions 	}
89*5e3eaea3SApple OSS Distributions 
90*5e3eaea3SApple OSS Distributions 	static bool
OSSymbol_obj_equ(const struct smrq_slink * link,smrh_key_t key)91*5e3eaea3SApple OSS Distributions 	OSSymbol_obj_equ(const struct smrq_slink *link, smrh_key_t key)
92*5e3eaea3SApple OSS Distributions 	{
93*5e3eaea3SApple OSS Distributions 		OSSymbol *sym = __container_of(link, OSSymbol, hashlink);
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions 		return smrh_key_equ_str(OSSymbol_get_key(sym), key);
96*5e3eaea3SApple OSS Distributions 	}
97*5e3eaea3SApple OSS Distributions 
98*5e3eaea3SApple OSS Distributions 	static bool
OSSymbol_obj_try_get(void * obj)99*5e3eaea3SApple OSS Distributions 	OSSymbol_obj_try_get(void *obj)
100*5e3eaea3SApple OSS Distributions 	{
101*5e3eaea3SApple OSS Distributions 		OSSymbol *sym = (OSSymbol *)obj;
102*5e3eaea3SApple OSS Distributions 
103*5e3eaea3SApple OSS Distributions 		return (sym->flags & kOSSSymbolPermanent) ||
104*5e3eaea3SApple OSS Distributions 		       sym->taggedTryRetain(nullptr);
105*5e3eaea3SApple OSS Distributions 	}
106*5e3eaea3SApple OSS Distributions 
107*5e3eaea3SApple OSS Distributions 	SMRH_TRAITS_DEFINE_STR(hash_traits, OSSymbol, hashlink,
108*5e3eaea3SApple OSS Distributions 	    .domain      = &smr_iokit,
109*5e3eaea3SApple OSS Distributions 	    .obj_hash    = OSSymbol_obj_hash,
110*5e3eaea3SApple OSS Distributions 	    .obj_equ     = OSSymbol_obj_equ,
111*5e3eaea3SApple OSS Distributions 	    .obj_try_get = OSSymbol_obj_try_get);
112*5e3eaea3SApple OSS Distributions 
113*5e3eaea3SApple OSS Distributions 	mutable lck_mtx_t _mutex;
114*5e3eaea3SApple OSS Distributions 	struct smr_hash   _hash;
115*5e3eaea3SApple OSS Distributions 	smrq_slist_head   _huge_head;
116*5e3eaea3SApple OSS Distributions 	thread_call_t     _tcall;
117*5e3eaea3SApple OSS Distributions 	uint32_t          _hugeCount = 0;
118*5e3eaea3SApple OSS Distributions 	bool              _tcallScheduled;
119*5e3eaea3SApple OSS Distributions 
120*5e3eaea3SApple OSS Distributions private:
121*5e3eaea3SApple OSS Distributions 
122*5e3eaea3SApple OSS Distributions 	inline void
lock() const123*5e3eaea3SApple OSS Distributions 	lock() const
124*5e3eaea3SApple OSS Distributions 	{
125*5e3eaea3SApple OSS Distributions 		lck_mtx_lock(&_mutex);
126*5e3eaea3SApple OSS Distributions 	}
127*5e3eaea3SApple OSS Distributions 
128*5e3eaea3SApple OSS Distributions 	inline void
unlock() const129*5e3eaea3SApple OSS Distributions 	unlock() const
130*5e3eaea3SApple OSS Distributions 	{
131*5e3eaea3SApple OSS Distributions 		lck_mtx_unlock(&_mutex);
132*5e3eaea3SApple OSS Distributions 	}
133*5e3eaea3SApple OSS Distributions 
134*5e3eaea3SApple OSS Distributions 	inline bool
shouldShrink() const135*5e3eaea3SApple OSS Distributions 	shouldShrink() const
136*5e3eaea3SApple OSS Distributions 	{
137*5e3eaea3SApple OSS Distributions 		/* shrink if there are more than 2 buckets per 1 symbol */
138*5e3eaea3SApple OSS Distributions 		return smr_hash_serialized_should_shrink(&_hash, MIN_SIZE, 2, 1);
139*5e3eaea3SApple OSS Distributions 	}
140*5e3eaea3SApple OSS Distributions 
141*5e3eaea3SApple OSS Distributions 	inline bool
shouldGrow() const142*5e3eaea3SApple OSS Distributions 	shouldGrow() const
143*5e3eaea3SApple OSS Distributions 	{
144*5e3eaea3SApple OSS Distributions 		/* shrink if there less more than 1 bucket per 4 symbol */
145*5e3eaea3SApple OSS Distributions 		return smr_hash_serialized_should_grow(&_hash, 1, 4);
146*5e3eaea3SApple OSS Distributions 	}
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions public:
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions 	static void rehash(thread_call_param_t, thread_call_param_t);
151*5e3eaea3SApple OSS Distributions 	inline static OSSymbolPool &instance() __pure2;
152*5e3eaea3SApple OSS Distributions 
OSSymbolPool()153*5e3eaea3SApple OSS Distributions 	OSSymbolPool()
154*5e3eaea3SApple OSS Distributions 	{
155*5e3eaea3SApple OSS Distributions 		lck_mtx_init(&_mutex, &lock_group, LCK_ATTR_NULL);
156*5e3eaea3SApple OSS Distributions 
157*5e3eaea3SApple OSS Distributions 		smr_hash_init(&_hash, MIN_SIZE);
158*5e3eaea3SApple OSS Distributions 		smrq_init(&_huge_head);
159*5e3eaea3SApple OSS Distributions 
160*5e3eaea3SApple OSS Distributions 		_tcall = thread_call_allocate_with_options(rehash, this,
161*5e3eaea3SApple OSS Distributions 		    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
162*5e3eaea3SApple OSS Distributions 	}
163*5e3eaea3SApple OSS Distributions 	OSSymbolPool(const OSSymbolPool &) = delete;
164*5e3eaea3SApple OSS Distributions 	OSSymbolPool(OSSymbolPool &&) = delete;
165*5e3eaea3SApple OSS Distributions 	OSSymbolPool &operator=(const OSSymbolPool &) = delete;
166*5e3eaea3SApple OSS Distributions 	OSSymbolPool &operator=(OSSymbolPool &&) = delete;
167*5e3eaea3SApple OSS Distributions 
168*5e3eaea3SApple OSS Distributions 	~OSSymbolPool() = delete;
169*5e3eaea3SApple OSS Distributions 
170*5e3eaea3SApple OSS Distributions 	OSSharedPtr<const OSSymbol> findSymbol(smrh_key_t key) const;
171*5e3eaea3SApple OSS Distributions 
172*5e3eaea3SApple OSS Distributions 	void insertSymbol(
173*5e3eaea3SApple OSS Distributions 		OSSharedPtr<OSSymbol> &sym,
174*5e3eaea3SApple OSS Distributions 		smrh_key_t key,
175*5e3eaea3SApple OSS Distributions 		bool makePermanent = false);
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions 	void removeSymbol(OSSymbol *sym);
178*5e3eaea3SApple OSS Distributions 
179*5e3eaea3SApple OSS Distributions 	void rehash();
180*5e3eaea3SApple OSS Distributions 
181*5e3eaea3SApple OSS Distributions 	void checkForPageUnload(void *startAddr, void *endAddr);
182*5e3eaea3SApple OSS Distributions };
183*5e3eaea3SApple OSS Distributions 
184*5e3eaea3SApple OSS Distributions static _Alignas(OSSymbolPool) uint8_t OSSymbolPoolStorage[sizeof(OSSymbolPool)];
185*5e3eaea3SApple OSS Distributions 
186*5e3eaea3SApple OSS Distributions OSSymbolPool &
instance()187*5e3eaea3SApple OSS Distributions OSSymbolPool::instance()
188*5e3eaea3SApple OSS Distributions {
189*5e3eaea3SApple OSS Distributions 	return reinterpret_cast<OSSymbolPool &>(OSSymbolPoolStorage);
190*5e3eaea3SApple OSS Distributions }
191*5e3eaea3SApple OSS Distributions 
192*5e3eaea3SApple OSS Distributions static inline bool
OSSymbol_is_huge(size_t size)193*5e3eaea3SApple OSS Distributions OSSymbol_is_huge(size_t size)
194*5e3eaea3SApple OSS Distributions {
195*5e3eaea3SApple OSS Distributions 	return size > KALLOC_SAFE_ALLOC_SIZE;
196*5e3eaea3SApple OSS Distributions }
197*5e3eaea3SApple OSS Distributions 
198*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
findSymbol(smrh_key_t key) const199*5e3eaea3SApple OSS Distributions OSSymbolPool::findSymbol(smrh_key_t key) const
200*5e3eaea3SApple OSS Distributions {
201*5e3eaea3SApple OSS Distributions 	OSSymbol *sym;
202*5e3eaea3SApple OSS Distributions 	OSSharedPtr<const OSSymbol> ret;
203*5e3eaea3SApple OSS Distributions 
204*5e3eaea3SApple OSS Distributions 	if (!OSSymbol_is_huge(key.smrk_len)) {
205*5e3eaea3SApple OSS Distributions 		char tmp_buf[128]; /* empirically all keys are < 110 bytes */
206*5e3eaea3SApple OSS Distributions 		char *copy_s = NULL;
207*5e3eaea3SApple OSS Distributions 
208*5e3eaea3SApple OSS Distributions 		/*
209*5e3eaea3SApple OSS Distributions 		 * rdar://105075708: the key might be in pageable memory,
210*5e3eaea3SApple OSS Distributions 		 * and smr_hash_get() disable preemption which prevents
211*5e3eaea3SApple OSS Distributions 		 * faulting the memory.
212*5e3eaea3SApple OSS Distributions 		 */
213*5e3eaea3SApple OSS Distributions 		if (key.smrk_len <= sizeof(tmp_buf)) {
214*5e3eaea3SApple OSS Distributions 			memcpy(tmp_buf, key.smrk_opaque, key.smrk_len);
215*5e3eaea3SApple OSS Distributions 			key.smrk_string = tmp_buf;
216*5e3eaea3SApple OSS Distributions 		} else {
217*5e3eaea3SApple OSS Distributions 			copy_s = (char *)kalloc_data(key.smrk_len,
218*5e3eaea3SApple OSS Distributions 			    Z_WAITOK_ZERO_NOFAIL);
219*5e3eaea3SApple OSS Distributions 			memcpy(copy_s, key.smrk_opaque, key.smrk_len);
220*5e3eaea3SApple OSS Distributions 			key.smrk_string = copy_s;
221*5e3eaea3SApple OSS Distributions 		}
222*5e3eaea3SApple OSS Distributions 		sym = smr_hash_get(&_hash, key, &hash_traits);
223*5e3eaea3SApple OSS Distributions 		if (copy_s) {
224*5e3eaea3SApple OSS Distributions 			kfree_data(copy_s, key.smrk_len);
225*5e3eaea3SApple OSS Distributions 		}
226*5e3eaea3SApple OSS Distributions 	} else {
227*5e3eaea3SApple OSS Distributions 		lock();
228*5e3eaea3SApple OSS Distributions 		sym = (OSSymbol *)__smr_hash_serialized_find(&_huge_head, key,
229*5e3eaea3SApple OSS Distributions 		    &hash_traits.smrht);
230*5e3eaea3SApple OSS Distributions 		if (sym && !OSSymbol_obj_try_get(sym)) {
231*5e3eaea3SApple OSS Distributions 			sym = NULL;
232*5e3eaea3SApple OSS Distributions 		}
233*5e3eaea3SApple OSS Distributions 		unlock();
234*5e3eaea3SApple OSS Distributions 	}
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions 	if (sym) {
237*5e3eaea3SApple OSS Distributions 		ret.reset(sym, OSNoRetain);
238*5e3eaea3SApple OSS Distributions 	}
239*5e3eaea3SApple OSS Distributions 
240*5e3eaea3SApple OSS Distributions 	return ret;
241*5e3eaea3SApple OSS Distributions }
242*5e3eaea3SApple OSS Distributions 
243*5e3eaea3SApple OSS Distributions void
insertSymbol(OSSharedPtr<OSSymbol> & symToInsert,smrh_key_t key,bool make_permanent)244*5e3eaea3SApple OSS Distributions OSSymbolPool::insertSymbol(
245*5e3eaea3SApple OSS Distributions 	OSSharedPtr<OSSymbol>  &symToInsert,
246*5e3eaea3SApple OSS Distributions 	smrh_key_t              key,
247*5e3eaea3SApple OSS Distributions 	bool                    make_permanent)
248*5e3eaea3SApple OSS Distributions {
249*5e3eaea3SApple OSS Distributions 	OSSymbol *sym;
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions 	/* make sure no one ever subclassed OSSymbols */
252*5e3eaea3SApple OSS Distributions 	zone_require(OSSymbol_zone, symToInsert.get());
253*5e3eaea3SApple OSS Distributions 
254*5e3eaea3SApple OSS Distributions 	symToInsert->flags |= kOSSSymbolHashed;
255*5e3eaea3SApple OSS Distributions 	if (make_permanent) {
256*5e3eaea3SApple OSS Distributions 		symToInsert->flags |= kOSSSymbolPermanent;
257*5e3eaea3SApple OSS Distributions 	}
258*5e3eaea3SApple OSS Distributions 
259*5e3eaea3SApple OSS Distributions 	lock();
260*5e3eaea3SApple OSS Distributions 
261*5e3eaea3SApple OSS Distributions 	if (!OSSymbol_is_huge(key.smrk_len)) {
262*5e3eaea3SApple OSS Distributions 		sym = smr_hash_serialized_get_or_insert(&_hash, key,
263*5e3eaea3SApple OSS Distributions 		    &symToInsert->hashlink, &hash_traits);
264*5e3eaea3SApple OSS Distributions 
265*5e3eaea3SApple OSS Distributions 		if (shouldGrow() && !_tcallScheduled &&
266*5e3eaea3SApple OSS Distributions 		    startup_phase >= STARTUP_SUB_THREAD_CALL) {
267*5e3eaea3SApple OSS Distributions 			_tcallScheduled = true;
268*5e3eaea3SApple OSS Distributions 			thread_call_enter(_tcall);
269*5e3eaea3SApple OSS Distributions 		}
270*5e3eaea3SApple OSS Distributions 	} else {
271*5e3eaea3SApple OSS Distributions 		sym = (OSSymbol *)__smr_hash_serialized_find(&_huge_head, key,
272*5e3eaea3SApple OSS Distributions 		    &hash_traits.smrht);
273*5e3eaea3SApple OSS Distributions 		if (!sym || !OSSymbol_obj_try_get(sym)) {
274*5e3eaea3SApple OSS Distributions 			smrq_serialized_insert_head(&_huge_head,
275*5e3eaea3SApple OSS Distributions 			    &symToInsert->hashlink);
276*5e3eaea3SApple OSS Distributions 			_hugeCount++;
277*5e3eaea3SApple OSS Distributions 			sym = NULL;
278*5e3eaea3SApple OSS Distributions 		}
279*5e3eaea3SApple OSS Distributions 	}
280*5e3eaea3SApple OSS Distributions 
281*5e3eaea3SApple OSS Distributions 	unlock();
282*5e3eaea3SApple OSS Distributions 
283*5e3eaea3SApple OSS Distributions 	if (sym) {
284*5e3eaea3SApple OSS Distributions 		symToInsert->flags &= ~(kOSSSymbolHashed | kOSSSymbolPermanent);
285*5e3eaea3SApple OSS Distributions 		symToInsert.reset(sym, OSNoRetain);
286*5e3eaea3SApple OSS Distributions 	}
287*5e3eaea3SApple OSS Distributions }
288*5e3eaea3SApple OSS Distributions 
289*5e3eaea3SApple OSS Distributions void
removeSymbol(OSSymbol * sym)290*5e3eaea3SApple OSS Distributions OSSymbolPool::removeSymbol(OSSymbol *sym)
291*5e3eaea3SApple OSS Distributions {
292*5e3eaea3SApple OSS Distributions 	lock();
293*5e3eaea3SApple OSS Distributions 
294*5e3eaea3SApple OSS Distributions 	assert(sym->flags & kOSSSymbolHashed);
295*5e3eaea3SApple OSS Distributions 	sym->flags &= ~kOSSSymbolHashed;
296*5e3eaea3SApple OSS Distributions 
297*5e3eaea3SApple OSS Distributions 	if (!OSSymbol_is_huge(sym->length)) {
298*5e3eaea3SApple OSS Distributions 		smr_hash_serialized_remove(&_hash, &sym->hashlink, &hash_traits);
299*5e3eaea3SApple OSS Distributions 
300*5e3eaea3SApple OSS Distributions 		if (shouldShrink() && !_tcallScheduled &&
301*5e3eaea3SApple OSS Distributions 		    startup_phase >= STARTUP_SUB_THREAD_CALL) {
302*5e3eaea3SApple OSS Distributions 			_tcallScheduled = true;
303*5e3eaea3SApple OSS Distributions 			thread_call_enter(_tcall);
304*5e3eaea3SApple OSS Distributions 		}
305*5e3eaea3SApple OSS Distributions 	} else {
306*5e3eaea3SApple OSS Distributions 		smrq_serialized_remove(&_huge_head, &sym->hashlink);
307*5e3eaea3SApple OSS Distributions 		_hugeCount--;
308*5e3eaea3SApple OSS Distributions 	}
309*5e3eaea3SApple OSS Distributions 
310*5e3eaea3SApple OSS Distributions 	unlock();
311*5e3eaea3SApple OSS Distributions }
312*5e3eaea3SApple OSS Distributions 
313*5e3eaea3SApple OSS Distributions void
rehash(thread_call_param_t arg0,thread_call_param_t arg1 __unused)314*5e3eaea3SApple OSS Distributions OSSymbolPool::rehash(thread_call_param_t arg0, thread_call_param_t arg1 __unused)
315*5e3eaea3SApple OSS Distributions {
316*5e3eaea3SApple OSS Distributions 	reinterpret_cast<OSSymbolPool *>(arg0)->rehash();
317*5e3eaea3SApple OSS Distributions }
318*5e3eaea3SApple OSS Distributions 
319*5e3eaea3SApple OSS Distributions void
rehash()320*5e3eaea3SApple OSS Distributions OSSymbolPool::rehash()
321*5e3eaea3SApple OSS Distributions {
322*5e3eaea3SApple OSS Distributions 	lock();
323*5e3eaea3SApple OSS Distributions 	_tcallScheduled = false;
324*5e3eaea3SApple OSS Distributions 
325*5e3eaea3SApple OSS Distributions 	if (shouldShrink()) {
326*5e3eaea3SApple OSS Distributions 		smr_hash_shrink_and_unlock(&_hash, &_mutex, &hash_traits);
327*5e3eaea3SApple OSS Distributions 	} else if (shouldGrow()) {
328*5e3eaea3SApple OSS Distributions 		smr_hash_grow_and_unlock(&_hash, &_mutex, &hash_traits);
329*5e3eaea3SApple OSS Distributions 	} else {
330*5e3eaea3SApple OSS Distributions 		unlock();
331*5e3eaea3SApple OSS Distributions 	}
332*5e3eaea3SApple OSS Distributions }
333*5e3eaea3SApple OSS Distributions 
334*5e3eaea3SApple OSS Distributions void
checkForPageUnload(void * startAddr,void * endAddr)335*5e3eaea3SApple OSS Distributions OSSymbolPool::checkForPageUnload(void *startAddr, void *endAddr)
336*5e3eaea3SApple OSS Distributions {
337*5e3eaea3SApple OSS Distributions 	OSSymbol *sym;
338*5e3eaea3SApple OSS Distributions 	char *s;
339*5e3eaea3SApple OSS Distributions 	bool mustSync = false;
340*5e3eaea3SApple OSS Distributions 
341*5e3eaea3SApple OSS Distributions 	lock();
342*5e3eaea3SApple OSS Distributions 	smr_hash_foreach(sym, &_hash, &hash_traits) {
343*5e3eaea3SApple OSS Distributions 		if (sym->string >= startAddr && sym->string < endAddr) {
344*5e3eaea3SApple OSS Distributions 			assert(sym->flags & kOSStringNoCopy);
345*5e3eaea3SApple OSS Distributions 
346*5e3eaea3SApple OSS Distributions 			s = (char *)kalloc_data(sym->length,
347*5e3eaea3SApple OSS Distributions 			    Z_WAITOK_ZERO);
348*5e3eaea3SApple OSS Distributions 			if (s) {
349*5e3eaea3SApple OSS Distributions 				memcpy(s, sym->string, sym->length);
350*5e3eaea3SApple OSS Distributions 				/*
351*5e3eaea3SApple OSS Distributions 				 * make sure the memcpy is visible for readers
352*5e3eaea3SApple OSS Distributions 				 * who dereference `string` below.
353*5e3eaea3SApple OSS Distributions 				 *
354*5e3eaea3SApple OSS Distributions 				 * We can't use os_atomic_store(&..., release)
355*5e3eaea3SApple OSS Distributions 				 * because OSSymbol::string is PACed
356*5e3eaea3SApple OSS Distributions 				 */
357*5e3eaea3SApple OSS Distributions 				os_atomic_thread_fence(release);
358*5e3eaea3SApple OSS Distributions 			}
359*5e3eaea3SApple OSS Distributions 			sym->string = s;
360*5e3eaea3SApple OSS Distributions 			sym->flags &= ~kOSStringNoCopy;
361*5e3eaea3SApple OSS Distributions 			mustSync = true;
362*5e3eaea3SApple OSS Distributions 		}
363*5e3eaea3SApple OSS Distributions 	}
364*5e3eaea3SApple OSS Distributions 
365*5e3eaea3SApple OSS Distributions 	unlock();
366*5e3eaea3SApple OSS Distributions 
367*5e3eaea3SApple OSS Distributions 	/* Make sure no readers can see stale pointers that we rewrote */
368*5e3eaea3SApple OSS Distributions 	if (mustSync) {
369*5e3eaea3SApple OSS Distributions 		smr_iokit_synchronize();
370*5e3eaea3SApple OSS Distributions 	}
371*5e3eaea3SApple OSS Distributions }
372*5e3eaea3SApple OSS Distributions 
373*5e3eaea3SApple OSS Distributions #pragma clang diagnostic pop /* -Winvalid-offsetof */
374*5e3eaea3SApple OSS Distributions 
375*5e3eaea3SApple OSS Distributions /*
376*5e3eaea3SApple OSS Distributions  *********************************************************************
377*5e3eaea3SApple OSS Distributions  * From here on we are actually implementing the OSSymbol class
378*5e3eaea3SApple OSS Distributions  *********************************************************************
379*5e3eaea3SApple OSS Distributions  */
380*5e3eaea3SApple OSS Distributions #define super OSString
381*5e3eaea3SApple OSS Distributions 
382*5e3eaea3SApple OSS Distributions OSDefineMetaClassWithInit(OSSymbol, OSString, OSSymbol::initialize());
383*5e3eaea3SApple OSS Distributions OSMetaClassConstructorInit(OSSymbol, OSString, OSSymbol::initialize());
384*5e3eaea3SApple OSS Distributions OSDefineBasicStructors(OSSymbol, OSString)
385*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 0);
386*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 1);
387*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 2);
388*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 3);
389*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 4);
390*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 5);
391*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 6);
392*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 7);
393*5e3eaea3SApple OSS Distributions 
394*5e3eaea3SApple OSS Distributions static void
OSSymbol_smr_free(void * sym,vm_size_t size __unused)395*5e3eaea3SApple OSS Distributions OSSymbol_smr_free(void *sym, vm_size_t size __unused)
396*5e3eaea3SApple OSS Distributions {
397*5e3eaea3SApple OSS Distributions 	reinterpret_cast<OSSymbol *>(sym)->smr_free();
398*5e3eaea3SApple OSS Distributions }
399*5e3eaea3SApple OSS Distributions 
400*5e3eaea3SApple OSS Distributions void
initialize()401*5e3eaea3SApple OSS Distributions OSSymbol::initialize()
402*5e3eaea3SApple OSS Distributions {
403*5e3eaea3SApple OSS Distributions 	zone_enable_smr(OSSymbol_zone, &smr_iokit, &OSSymbol_smr_free);
404*5e3eaea3SApple OSS Distributions 	new (OSSymbolPoolStorage) OSSymbolPool();
405*5e3eaea3SApple OSS Distributions }
406*5e3eaea3SApple OSS Distributions 
407*5e3eaea3SApple OSS Distributions bool
initWithCStringNoCopy(const char *)408*5e3eaea3SApple OSS Distributions OSSymbol::initWithCStringNoCopy(const char *)
409*5e3eaea3SApple OSS Distributions {
410*5e3eaea3SApple OSS Distributions 	return false;
411*5e3eaea3SApple OSS Distributions }
412*5e3eaea3SApple OSS Distributions bool
initWithCString(const char *)413*5e3eaea3SApple OSS Distributions OSSymbol::initWithCString(const char *)
414*5e3eaea3SApple OSS Distributions {
415*5e3eaea3SApple OSS Distributions 	return false;
416*5e3eaea3SApple OSS Distributions }
417*5e3eaea3SApple OSS Distributions bool
initWithString(const OSString *)418*5e3eaea3SApple OSS Distributions OSSymbol::initWithString(const OSString *)
419*5e3eaea3SApple OSS Distributions {
420*5e3eaea3SApple OSS Distributions 	return false;
421*5e3eaea3SApple OSS Distributions }
422*5e3eaea3SApple OSS Distributions 
423*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
withString(const OSString * aString)424*5e3eaea3SApple OSS Distributions OSSymbol::withString(const OSString *aString)
425*5e3eaea3SApple OSS Distributions {
426*5e3eaea3SApple OSS Distributions 	// This string may be a OSSymbol already, cheap check.
427*5e3eaea3SApple OSS Distributions 	if (OSDynamicCast(OSSymbol, aString)) {
428*5e3eaea3SApple OSS Distributions 		OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
429*5e3eaea3SApple OSS Distributions 		return aStringNew;
430*5e3eaea3SApple OSS Distributions 	} else if (((const OSSymbol *) aString)->flags & kOSStringNoCopy) {
431*5e3eaea3SApple OSS Distributions 		return OSSymbol::withCStringNoCopy(aString->getCStringNoCopy());
432*5e3eaea3SApple OSS Distributions 	} else {
433*5e3eaea3SApple OSS Distributions 		return OSSymbol::withCString(aString->getCStringNoCopy());
434*5e3eaea3SApple OSS Distributions 	}
435*5e3eaea3SApple OSS Distributions }
436*5e3eaea3SApple OSS Distributions 
437*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCString(const char * cString)438*5e3eaea3SApple OSS Distributions OSSymbol::withCString(const char *cString)
439*5e3eaea3SApple OSS Distributions {
440*5e3eaea3SApple OSS Distributions 	auto &pool = OSSymbolPool::instance();
441*5e3eaea3SApple OSS Distributions 	smrh_key_t key = {
442*5e3eaea3SApple OSS Distributions 		.smrk_string = cString,
443*5e3eaea3SApple OSS Distributions 		.smrk_len    = strnlen(cString, kMaxStringLength),
444*5e3eaea3SApple OSS Distributions 	};
445*5e3eaea3SApple OSS Distributions 	bool permanent = false;
446*5e3eaea3SApple OSS Distributions 
447*5e3eaea3SApple OSS Distributions 	if (key.smrk_len >= kMaxStringLength) {
448*5e3eaea3SApple OSS Distributions 		return nullptr;
449*5e3eaea3SApple OSS Distributions 	}
450*5e3eaea3SApple OSS Distributions 
451*5e3eaea3SApple OSS Distributions 	auto symbol = pool.findSymbol(key);
452*5e3eaea3SApple OSS Distributions 	if (__probable(symbol)) {
453*5e3eaea3SApple OSS Distributions 		return symbol;
454*5e3eaea3SApple OSS Distributions 	}
455*5e3eaea3SApple OSS Distributions 
456*5e3eaea3SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
457*5e3eaea3SApple OSS Distributions 	/*
458*5e3eaea3SApple OSS Distributions 	 * Empirically, symbols which string is from the rorgn part of the
459*5e3eaea3SApple OSS Distributions 	 * kernel are asked about all the time.
460*5e3eaea3SApple OSS Distributions 	 *
461*5e3eaea3SApple OSS Distributions 	 * Making them noCopy + permanent avoids a significant amount of
462*5e3eaea3SApple OSS Distributions 	 * useless refcounting traffic.
463*5e3eaea3SApple OSS Distributions 	 *
464*5e3eaea3SApple OSS Distributions 	 * On embedded, this policy causes about 200 extra symbols to be made
465*5e3eaea3SApple OSS Distributions 	 * from baseline (~6k), but avoiding the string copies saves about 60k.
466*5e3eaea3SApple OSS Distributions 	 */
467*5e3eaea3SApple OSS Distributions 	permanent = rorgn_contains((vm_offset_t)cString, key.smrk_len + 1, false);
468*5e3eaea3SApple OSS Distributions #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
469*5e3eaea3SApple OSS Distributions 
470*5e3eaea3SApple OSS Distributions 	/*
471*5e3eaea3SApple OSS Distributions 	 * can't use OSString::initWithCString* because it calls
472*5e3eaea3SApple OSS Distributions 	 * OSObject::init() which tries to enroll in IOTracking if it's on.
473*5e3eaea3SApple OSS Distributions 	 */
474*5e3eaea3SApple OSS Distributions 
475*5e3eaea3SApple OSS Distributions 	auto newSymb = OSMakeShared<OSSymbol>();
476*5e3eaea3SApple OSS Distributions 
477*5e3eaea3SApple OSS Distributions 	if (permanent) {
478*5e3eaea3SApple OSS Distributions 		newSymb->flags  = kOSStringNoCopy;
479*5e3eaea3SApple OSS Distributions 		newSymb->length = (uint32_t)(key.smrk_len + 1);
480*5e3eaea3SApple OSS Distributions 		newSymb->string = const_cast<char *>(cString);
481*5e3eaea3SApple OSS Distributions 		pool.insertSymbol(/* inout */ newSymb, key, permanent);
482*5e3eaea3SApple OSS Distributions 	} else if (char *s = (char *)kalloc_data(key.smrk_len + 1, Z_WAITOK_ZERO)) {
483*5e3eaea3SApple OSS Distributions 		memcpy(s, cString, key.smrk_len);
484*5e3eaea3SApple OSS Distributions 		newSymb->flags  = 0;
485*5e3eaea3SApple OSS Distributions 		newSymb->length = (uint32_t)(key.smrk_len + 1);
486*5e3eaea3SApple OSS Distributions 		newSymb->string = s;
487*5e3eaea3SApple OSS Distributions 		pool.insertSymbol(/* inout */ newSymb, key, permanent);
488*5e3eaea3SApple OSS Distributions 	} else {
489*5e3eaea3SApple OSS Distributions 		newSymb.reset();
490*5e3eaea3SApple OSS Distributions 	}
491*5e3eaea3SApple OSS Distributions 
492*5e3eaea3SApple OSS Distributions 	return os::move(newSymb); // return the newly created & inserted symbol.
493*5e3eaea3SApple OSS Distributions }
494*5e3eaea3SApple OSS Distributions 
495*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCStringNoCopy(const char * cString)496*5e3eaea3SApple OSS Distributions OSSymbol::withCStringNoCopy(const char *cString)
497*5e3eaea3SApple OSS Distributions {
498*5e3eaea3SApple OSS Distributions 	auto &pool = OSSymbolPool::instance();
499*5e3eaea3SApple OSS Distributions 	smrh_key_t key = {
500*5e3eaea3SApple OSS Distributions 		.smrk_string = cString,
501*5e3eaea3SApple OSS Distributions 		.smrk_len    = strnlen(cString, kMaxStringLength),
502*5e3eaea3SApple OSS Distributions 	};
503*5e3eaea3SApple OSS Distributions 	bool permanent = false;
504*5e3eaea3SApple OSS Distributions 
505*5e3eaea3SApple OSS Distributions 	if (key.smrk_len >= kMaxStringLength) {
506*5e3eaea3SApple OSS Distributions 		return nullptr;
507*5e3eaea3SApple OSS Distributions 	}
508*5e3eaea3SApple OSS Distributions 
509*5e3eaea3SApple OSS Distributions 	auto symbol = pool.findSymbol(key);
510*5e3eaea3SApple OSS Distributions 	if (__probable(symbol)) {
511*5e3eaea3SApple OSS Distributions 		return symbol;
512*5e3eaea3SApple OSS Distributions 	}
513*5e3eaea3SApple OSS Distributions 
514*5e3eaea3SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
515*5e3eaea3SApple OSS Distributions 	permanent = rorgn_contains((vm_offset_t)cString, key.smrk_len + 1, false);
516*5e3eaea3SApple OSS Distributions #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
517*5e3eaea3SApple OSS Distributions 
518*5e3eaea3SApple OSS Distributions 	auto newSymb = OSMakeShared<OSSymbol>();
519*5e3eaea3SApple OSS Distributions 
520*5e3eaea3SApple OSS Distributions 	/*
521*5e3eaea3SApple OSS Distributions 	 * can't use OSString::initWithCStringNoCopy because it calls
522*5e3eaea3SApple OSS Distributions 	 * OSObject::init() which tries to enrol in IOTracking if it's on.
523*5e3eaea3SApple OSS Distributions 	 */
524*5e3eaea3SApple OSS Distributions 	newSymb->flags  = kOSStringNoCopy;
525*5e3eaea3SApple OSS Distributions 	newSymb->length = (uint32_t)(key.smrk_len + 1);
526*5e3eaea3SApple OSS Distributions 	newSymb->string = const_cast<char *>(cString);
527*5e3eaea3SApple OSS Distributions 	pool.insertSymbol(/* inout */ newSymb, key, permanent);
528*5e3eaea3SApple OSS Distributions 
529*5e3eaea3SApple OSS Distributions 	return os::move(newSymb); // return the newly created & inserted symbol.
530*5e3eaea3SApple OSS Distributions }
531*5e3eaea3SApple OSS Distributions 
532*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForString(const OSString * aString)533*5e3eaea3SApple OSS Distributions OSSymbol::existingSymbolForString(const OSString *aString)
534*5e3eaea3SApple OSS Distributions {
535*5e3eaea3SApple OSS Distributions 	if (!aString) {
536*5e3eaea3SApple OSS Distributions 		return NULL;
537*5e3eaea3SApple OSS Distributions 	}
538*5e3eaea3SApple OSS Distributions 	if (OSDynamicCast(OSSymbol, aString)) {
539*5e3eaea3SApple OSS Distributions 		OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
540*5e3eaea3SApple OSS Distributions 		return aStringNew;
541*5e3eaea3SApple OSS Distributions 	}
542*5e3eaea3SApple OSS Distributions 
543*5e3eaea3SApple OSS Distributions 	smrh_key_t key = {
544*5e3eaea3SApple OSS Distributions 		.smrk_string = aString->getCStringNoCopy(),
545*5e3eaea3SApple OSS Distributions 		.smrk_len    = aString->getLength(),
546*5e3eaea3SApple OSS Distributions 	};
547*5e3eaea3SApple OSS Distributions 	return OSSymbolPool::instance().findSymbol(key);
548*5e3eaea3SApple OSS Distributions }
549*5e3eaea3SApple OSS Distributions 
550*5e3eaea3SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForCString(const char * cString)551*5e3eaea3SApple OSS Distributions OSSymbol::existingSymbolForCString(const char *cString)
552*5e3eaea3SApple OSS Distributions {
553*5e3eaea3SApple OSS Distributions 	smrh_key_t key = {
554*5e3eaea3SApple OSS Distributions 		.smrk_string = cString,
555*5e3eaea3SApple OSS Distributions 		.smrk_len    = strlen(cString),
556*5e3eaea3SApple OSS Distributions 	};
557*5e3eaea3SApple OSS Distributions 	return OSSymbolPool::instance().findSymbol(key);
558*5e3eaea3SApple OSS Distributions }
559*5e3eaea3SApple OSS Distributions 
560*5e3eaea3SApple OSS Distributions void
checkForPageUnload(void * startAddr,void * endAddr)561*5e3eaea3SApple OSS Distributions OSSymbol::checkForPageUnload(void *startAddr, void *endAddr)
562*5e3eaea3SApple OSS Distributions {
563*5e3eaea3SApple OSS Distributions 	OSSymbolPool::instance().checkForPageUnload(startAddr, endAddr);
564*5e3eaea3SApple OSS Distributions }
565*5e3eaea3SApple OSS Distributions 
566*5e3eaea3SApple OSS Distributions void
taggedRetain(const void * tag) const567*5e3eaea3SApple OSS Distributions OSSymbol::taggedRetain(const void *tag) const
568*5e3eaea3SApple OSS Distributions {
569*5e3eaea3SApple OSS Distributions 	if ((flags & kOSSSymbolPermanent) == 0) {
570*5e3eaea3SApple OSS Distributions 		super::taggedRetain(tag);
571*5e3eaea3SApple OSS Distributions 	}
572*5e3eaea3SApple OSS Distributions }
573*5e3eaea3SApple OSS Distributions 
574*5e3eaea3SApple OSS Distributions void
taggedRelease(const void * tag) const575*5e3eaea3SApple OSS Distributions OSSymbol::taggedRelease(const void *tag) const
576*5e3eaea3SApple OSS Distributions {
577*5e3eaea3SApple OSS Distributions 	if ((flags & kOSSSymbolPermanent) == 0) {
578*5e3eaea3SApple OSS Distributions 		super::taggedRelease(tag);
579*5e3eaea3SApple OSS Distributions 	}
580*5e3eaea3SApple OSS Distributions }
581*5e3eaea3SApple OSS Distributions 
582*5e3eaea3SApple OSS Distributions void
taggedRelease(const void * tag,const int when) const583*5e3eaea3SApple OSS Distributions OSSymbol::taggedRelease(const void *tag, const int when) const
584*5e3eaea3SApple OSS Distributions {
585*5e3eaea3SApple OSS Distributions 	if ((flags & kOSSSymbolPermanent) == 0) {
586*5e3eaea3SApple OSS Distributions 		super::taggedRelease(tag, when);
587*5e3eaea3SApple OSS Distributions 	}
588*5e3eaea3SApple OSS Distributions }
589*5e3eaea3SApple OSS Distributions 
590*5e3eaea3SApple OSS Distributions void *
operator new(size_t size __unused)591*5e3eaea3SApple OSS Distributions OSSymbol::operator new(size_t size __unused)
592*5e3eaea3SApple OSS Distributions {
593*5e3eaea3SApple OSS Distributions 	return zalloc_smr(OSSymbol_zone, Z_WAITOK_ZERO_NOFAIL);
594*5e3eaea3SApple OSS Distributions }
595*5e3eaea3SApple OSS Distributions 
596*5e3eaea3SApple OSS Distributions void
operator delete(void * mem,size_t size)597*5e3eaea3SApple OSS Distributions OSSymbol::operator delete(void *mem, size_t size)
598*5e3eaea3SApple OSS Distributions {
599*5e3eaea3SApple OSS Distributions 	/*
600*5e3eaea3SApple OSS Distributions 	 * OSSymbol dying is this sequence:
601*5e3eaea3SApple OSS Distributions 	 *
602*5e3eaea3SApple OSS Distributions 	 * OSSymbol::taggedRelease() hits 0,
603*5e3eaea3SApple OSS Distributions 	 * which calls OSSymbol::free(),
604*5e3eaea3SApple OSS Distributions 	 * which calls zfree_smr().
605*5e3eaea3SApple OSS Distributions 	 *
606*5e3eaea3SApple OSS Distributions 	 * At this stage, the memory of the OSSymbol is on a deferred
607*5e3eaea3SApple OSS Distributions 	 * reclamation queue.
608*5e3eaea3SApple OSS Distributions 	 *
609*5e3eaea3SApple OSS Distributions 	 * When the memory is being recycled by zalloc, OSSymbol::smr_free()
610*5e3eaea3SApple OSS Distributions 	 * is called which terminates with a delete call and only needs
611*5e3eaea3SApple OSS Distributions 	 * to zero said memory given that the memory has already been
612*5e3eaea3SApple OSS Distributions 	 * returned to the allocator.
613*5e3eaea3SApple OSS Distributions 	 */
614*5e3eaea3SApple OSS Distributions 	bzero(mem, size);
615*5e3eaea3SApple OSS Distributions }
616*5e3eaea3SApple OSS Distributions 
617*5e3eaea3SApple OSS Distributions void
smr_free()618*5e3eaea3SApple OSS Distributions OSSymbol::smr_free()
619*5e3eaea3SApple OSS Distributions {
620*5e3eaea3SApple OSS Distributions 	/*
621*5e3eaea3SApple OSS Distributions 	 * This is called when the object is getting reused
622*5e3eaea3SApple OSS Distributions 	 */
623*5e3eaea3SApple OSS Distributions 
624*5e3eaea3SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
625*5e3eaea3SApple OSS Distributions 		kfree_data(string, length);
626*5e3eaea3SApple OSS Distributions 	}
627*5e3eaea3SApple OSS Distributions 
628*5e3eaea3SApple OSS Distributions 	/*
629*5e3eaea3SApple OSS Distributions 	 * Note: we do not call super::free() on purpose because
630*5e3eaea3SApple OSS Distributions 	 *       it would call OSObject::free() which tries to support
631*5e3eaea3SApple OSS Distributions 	 *       iotracking. iotracking is fundamentally incompatible
632*5e3eaea3SApple OSS Distributions 	 *       with SMR, so we on purpose do not call into these.
633*5e3eaea3SApple OSS Distributions 	 *
634*5e3eaea3SApple OSS Distributions 	 *       to debug OSSymbol leaks etc, the zone logging feature
635*5e3eaea3SApple OSS Distributions 	 *       can be used instead on the iokit.OSSymbol zone.
636*5e3eaea3SApple OSS Distributions 	 */
637*5e3eaea3SApple OSS Distributions 	OSSymbol::gMetaClass.instanceDestructed();
638*5e3eaea3SApple OSS Distributions 
639*5e3eaea3SApple OSS Distributions 	delete this;
640*5e3eaea3SApple OSS Distributions }
641*5e3eaea3SApple OSS Distributions 
642*5e3eaea3SApple OSS Distributions void
free()643*5e3eaea3SApple OSS Distributions OSSymbol::free()
644*5e3eaea3SApple OSS Distributions {
645*5e3eaea3SApple OSS Distributions 	bool freeNow = true;
646*5e3eaea3SApple OSS Distributions 
647*5e3eaea3SApple OSS Distributions 	if (flags & kOSSSymbolHashed) {
648*5e3eaea3SApple OSS Distributions 		OSSymbolPool::instance().removeSymbol(this);
649*5e3eaea3SApple OSS Distributions 		freeNow = OSSymbol_is_huge(length);
650*5e3eaea3SApple OSS Distributions 	}
651*5e3eaea3SApple OSS Distributions 
652*5e3eaea3SApple OSS Distributions 	if (freeNow && !(flags & kOSStringNoCopy) && string) {
653*5e3eaea3SApple OSS Distributions 		/*
654*5e3eaea3SApple OSS Distributions 		 * If the element isn't in the hash, it was a failed insertion
655*5e3eaea3SApple OSS Distributions 		 * racing, and no one will every do a hazardous access,
656*5e3eaea3SApple OSS Distributions 		 * so we can clean up the string right away.
657*5e3eaea3SApple OSS Distributions 		 *
658*5e3eaea3SApple OSS Distributions 		 * If it is huge, then it is not looked up via SMR but under
659*5e3eaea3SApple OSS Distributions 		 * locks, so we can free right now (actually _must_ because
660*5e3eaea3SApple OSS Distributions 		 * this free is not preemption disabled safe and can't be done
661*5e3eaea3SApple OSS Distributions 		 * in smr_free())
662*5e3eaea3SApple OSS Distributions 		 */
663*5e3eaea3SApple OSS Distributions 		kfree_data(string, length);
664*5e3eaea3SApple OSS Distributions 		assert(string == nullptr); /* kfree_data nils out */
665*5e3eaea3SApple OSS Distributions 	}
666*5e3eaea3SApple OSS Distributions 
667*5e3eaea3SApple OSS Distributions 	(zfree_smr)(OSSymbol_zone, this);
668*5e3eaea3SApple OSS Distributions }
669*5e3eaea3SApple OSS Distributions 
670*5e3eaea3SApple OSS Distributions uint32_t
hash() const671*5e3eaea3SApple OSS Distributions OSSymbol::hash() const
672*5e3eaea3SApple OSS Distributions {
673*5e3eaea3SApple OSS Distributions 	assert(!OSSymbol_is_huge(length));
674*5e3eaea3SApple OSS Distributions 	return os_hash_jenkins(string, length - 1);
675*5e3eaea3SApple OSS Distributions }
676*5e3eaea3SApple OSS Distributions 
677*5e3eaea3SApple OSS Distributions bool
isEqualTo(const char * aCString) const678*5e3eaea3SApple OSS Distributions OSSymbol::isEqualTo(const char *aCString) const
679*5e3eaea3SApple OSS Distributions {
680*5e3eaea3SApple OSS Distributions 	return super::isEqualTo(aCString);
681*5e3eaea3SApple OSS Distributions }
682*5e3eaea3SApple OSS Distributions 
683*5e3eaea3SApple OSS Distributions bool
isEqualTo(const OSSymbol * aSymbol) const684*5e3eaea3SApple OSS Distributions OSSymbol::isEqualTo(const OSSymbol *aSymbol) const
685*5e3eaea3SApple OSS Distributions {
686*5e3eaea3SApple OSS Distributions 	return aSymbol == this;
687*5e3eaea3SApple OSS Distributions }
688*5e3eaea3SApple OSS Distributions 
689*5e3eaea3SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const690*5e3eaea3SApple OSS Distributions OSSymbol::isEqualTo(const OSMetaClassBase *obj) const
691*5e3eaea3SApple OSS Distributions {
692*5e3eaea3SApple OSS Distributions 	OSSymbol *  sym;
693*5e3eaea3SApple OSS Distributions 	OSString *  str;
694*5e3eaea3SApple OSS Distributions 
695*5e3eaea3SApple OSS Distributions 	if ((sym = OSDynamicCast(OSSymbol, obj))) {
696*5e3eaea3SApple OSS Distributions 		return isEqualTo(sym);
697*5e3eaea3SApple OSS Distributions 	} else if ((str = OSDynamicCast(OSString, obj))) {
698*5e3eaea3SApple OSS Distributions 		return super::isEqualTo(str);
699*5e3eaea3SApple OSS Distributions 	} else {
700*5e3eaea3SApple OSS Distributions 		return false;
701*5e3eaea3SApple OSS Distributions 	}
702*5e3eaea3SApple OSS Distributions }
703*5e3eaea3SApple OSS Distributions 
704*5e3eaea3SApple OSS Distributions unsigned int
bsearch(const void * key,const void * array,unsigned int arrayCount,size_t memberSize)705*5e3eaea3SApple OSS Distributions OSSymbol::bsearch(
706*5e3eaea3SApple OSS Distributions 	const void *  key,
707*5e3eaea3SApple OSS Distributions 	const void *  array,
708*5e3eaea3SApple OSS Distributions 	unsigned int  arrayCount,
709*5e3eaea3SApple OSS Distributions 	size_t        memberSize)
710*5e3eaea3SApple OSS Distributions {
711*5e3eaea3SApple OSS Distributions 	const void **p;
712*5e3eaea3SApple OSS Distributions 	unsigned int baseIdx = 0;
713*5e3eaea3SApple OSS Distributions 	unsigned int lim;
714*5e3eaea3SApple OSS Distributions 
715*5e3eaea3SApple OSS Distributions 	for (lim = arrayCount; lim; lim >>= 1) {
716*5e3eaea3SApple OSS Distributions 		p = (typeof(p))(((uintptr_t) array) + (baseIdx + (lim >> 1)) * memberSize);
717*5e3eaea3SApple OSS Distributions 		if (key == *p) {
718*5e3eaea3SApple OSS Distributions 			return baseIdx + (lim >> 1);
719*5e3eaea3SApple OSS Distributions 		}
720*5e3eaea3SApple OSS Distributions 		if (key > *p) {
721*5e3eaea3SApple OSS Distributions 			// move right
722*5e3eaea3SApple OSS Distributions 			baseIdx += (lim >> 1) + 1;
723*5e3eaea3SApple OSS Distributions 			lim--;
724*5e3eaea3SApple OSS Distributions 		}
725*5e3eaea3SApple OSS Distributions 		// else move left
726*5e3eaea3SApple OSS Distributions 	}
727*5e3eaea3SApple OSS Distributions 	// not found, insertion point here
728*5e3eaea3SApple OSS Distributions 	return baseIdx + (lim >> 1);
729*5e3eaea3SApple OSS Distributions }
730*5e3eaea3SApple OSS Distributions 
731*5e3eaea3SApple OSS Distributions #if DEBUG || DEVELOPMENT
732*5e3eaea3SApple OSS Distributions static int
iokit_symbol_basic_test(int64_t size,int64_t * out)733*5e3eaea3SApple OSS Distributions iokit_symbol_basic_test(int64_t size, int64_t *out)
734*5e3eaea3SApple OSS Distributions {
735*5e3eaea3SApple OSS Distributions 	OSSharedPtr<const OSSymbol> sym1;
736*5e3eaea3SApple OSS Distributions 	OSSharedPtr<const OSSymbol> sym2;
737*5e3eaea3SApple OSS Distributions 	char *data;
738*5e3eaea3SApple OSS Distributions 
739*5e3eaea3SApple OSS Distributions 	data = (char *)kalloc_data(size, Z_WAITOK);
740*5e3eaea3SApple OSS Distributions 	if (!data) {
741*5e3eaea3SApple OSS Distributions 		return ENOMEM;
742*5e3eaea3SApple OSS Distributions 	}
743*5e3eaea3SApple OSS Distributions 
744*5e3eaea3SApple OSS Distributions 	memset(data, 'A', size - 1);
745*5e3eaea3SApple OSS Distributions 	data[size - 1] = '\0';
746*5e3eaea3SApple OSS Distributions 
747*5e3eaea3SApple OSS Distributions 	sym1 = OSSymbol::withCString(data);
748*5e3eaea3SApple OSS Distributions 	if (sym1 == nullptr) {
749*5e3eaea3SApple OSS Distributions 		return ENOMEM;
750*5e3eaea3SApple OSS Distributions 	}
751*5e3eaea3SApple OSS Distributions 	assert(sym1->getLength() == size - 1);
752*5e3eaea3SApple OSS Distributions 
753*5e3eaea3SApple OSS Distributions 	sym2 = OSSymbol::withCString(data);
754*5e3eaea3SApple OSS Distributions 	assert(sym1 == sym2);
755*5e3eaea3SApple OSS Distributions 
756*5e3eaea3SApple OSS Distributions 	sym2.reset();
757*5e3eaea3SApple OSS Distributions 	sym1.reset();
758*5e3eaea3SApple OSS Distributions 
759*5e3eaea3SApple OSS Distributions 	*out = 1;
760*5e3eaea3SApple OSS Distributions 	return 0;
761*5e3eaea3SApple OSS Distributions }
762*5e3eaea3SApple OSS Distributions SYSCTL_TEST_REGISTER(iokit_symbol_basic, iokit_symbol_basic_test);
763*5e3eaea3SApple OSS Distributions #endif /* DEBUG || DEVELOPMENT */
764