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