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