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