-
Notifications
You must be signed in to change notification settings - Fork 118
/
runtime.c
849 lines (740 loc) · 18.3 KB
/
runtime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#include "objc/runtime.h"
#include "selector.h"
#include "class.h"
#include "protocol.h"
#include "ivar.h"
#include "method.h"
#include "lock.h"
#include "dtable.h"
#include "gc_ops.h"
/* Make glibc export strdup() */
#if defined __GLIBC__
#define __USE_BSD 1
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define CHECK_ARG(arg) if (0 == arg) { return 0; }
static inline void safe_remove_from_subclass_list(Class cls);
PRIVATE void objc_resolve_class(Class);
void objc_send_initialize(id object);
/**
* Calls C++ destructors in the correct order.
*/
PRIVATE void call_cxx_destruct(id obj)
{
static SEL cxx_destruct;
if (NULL == cxx_destruct)
{
cxx_destruct = sel_registerName(".cxx_destruct");
}
// Don't call object_getClass(), because we want to get hidden classes too
Class cls = classForObject(obj);
while (cls)
{
// If we're deallocating a class with a hidden class, then the
// `.cxx_destruct` method may deallocate the class.
Class currentClass = cls;
cls = cls->super_class;
if (currentClass->cxx_destruct)
{
currentClass->cxx_destruct(obj, cxx_destruct);
}
}
}
static void call_cxx_construct_for_class(Class cls, id obj)
{
static SEL cxx_construct;
if (NULL == cxx_construct)
{
cxx_construct = sel_registerName(".cxx_construct");
}
if (cls->super_class)
{
call_cxx_construct_for_class(cls->super_class, obj);
}
if (cls->cxx_construct)
{
cls->cxx_construct(obj, cxx_construct);
}
}
PRIVATE void call_cxx_construct(id obj)
{
call_cxx_construct_for_class(classForObject(obj), obj);
}
/**
* Looks up the instance method in a specific class, without recursing into
* superclasses.
*/
static Method class_getInstanceMethodNonrecursive(Class aClass, SEL aSelector)
{
for (struct objc_method_list *methods = aClass->methods;
methods != NULL ; methods = methods->next)
{
for (int i=0 ; i<methods->count ; i++)
{
Method method = method_at_index(methods, i);
if (sel_isEqual(method->selector, aSelector))
{
return method;
}
}
}
return NULL;
}
BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment,
const char *types)
{
CHECK_ARG(cls);
CHECK_ARG(name);
CHECK_ARG(types);
// You can't add ivars to initialized classes. Note: We can't use the
// resolved flag here because class_getInstanceVariable() sets it.
if (objc_test_class_flag(cls, objc_class_flag_initialized))
{
return NO;
}
if (class_getInstanceVariable(cls, name) != NULL)
{
return NO;
}
struct objc_ivar_list *ivarlist = cls->ivars;
if (NULL == ivarlist)
{
cls->ivars = malloc(sizeof(struct objc_ivar_list) + sizeof(struct objc_ivar));
cls->ivars->size = sizeof(struct objc_ivar);
cls->ivars->count = 1;
}
else
{
ivarlist->count++;
// objc_ivar_list contains one ivar. Others follow it.
cls->ivars = realloc(ivarlist, sizeof(struct objc_ivar_list) +
(ivarlist->count) * sizeof(struct objc_ivar));
}
Ivar ivar = ivar_at_index(cls->ivars, cls->ivars->count - 1);
ivar->name = strdup(name);
ivar->type = strdup(types);
ivarSetAlign(ivar, alignment);
// Round up the offset of the ivar so it is correctly aligned.
long offset = cls->instance_size;
if (alignment != 0)
{
offset >>= alignment;
if (offset << alignment != cls->instance_size)
{
offset++;
}
offset <<= alignment;
}
ivar->offset = (int*)(uintptr_t)offset;
// Increase the instance size to make space for this.
cls->instance_size = offset + size;
return YES;
}
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
{
CHECK_ARG(cls);
CHECK_ARG(name);
CHECK_ARG(imp);
CHECK_ARG(types);
const char *methodName = sel_getName(name);
struct objc_method_list *methods;
for (methods=cls->methods; methods!=NULL ; methods=methods->next)
{
for (int i=0 ; i<methods->count ; i++)
{
Method method = method_at_index(methods, i);
if (strcmp(sel_getName(method->selector), methodName) == 0)
{
return NO;
}
}
}
methods = malloc(sizeof(struct objc_method_list) + sizeof(struct objc_method));
methods->next = cls->methods;
methods->size = sizeof(struct objc_method);
cls->methods = methods;
methods->count = 1;
struct objc_method *m0 = method_at_index(methods, 0);
m0->selector = sel_registerTypedName_np(methodName, types);
m0->types = strdup(types);
m0->imp = imp;
if (classHasDtable(cls))
{
add_method_list_to_class(cls, methods);
}
return YES;
}
BOOL class_addProtocol(Class cls, Protocol *protocol)
{
CHECK_ARG(cls);
CHECK_ARG(protocol);
if (class_conformsToProtocol(cls, protocol)) { return NO; }
struct objc_protocol_list *protocols =
malloc(sizeof(struct objc_protocol_list) + sizeof(Protocol*));
if (protocols == NULL) { return NO; }
protocols->next = cls->protocols;
protocols->count = 1;
protocols->list[0] = protocol;
cls->protocols = protocols;
return YES;
}
Ivar * class_copyIvarList(Class cls, unsigned int *outCount)
{
if (outCount != NULL)
{
*outCount = 0x0;
}
CHECK_ARG(cls);
struct objc_ivar_list *ivarlist = NULL;
unsigned int count = 0;
unsigned int index;
Ivar *list;
if (Nil != cls)
{
ivarlist = cls->ivars;
}
if (ivarlist != NULL)
{
count = ivarlist->count;
}
if (outCount != NULL)
{
*outCount = count;
}
if (count == 0)
{
return NULL;
}
list = malloc((count + 1) * sizeof(struct objc_ivar *));
list[count] = NULL;
count = 0;
for (index = 0; index < ivarlist->count; index++)
{
list[count++] = ivar_at_index(ivarlist, index);
}
return list;
}
Method * class_copyMethodList(Class cls, unsigned int *outCount)
{
if (outCount != NULL)
{
*outCount = 0x0;
}
CHECK_ARG(cls);
unsigned int count = 0;
Method *list;
struct objc_method_list *methods;
if (cls != NULL)
{
for (methods = cls->methods; methods != NULL; methods = methods->next)
{
count += methods->count;
}
}
if (outCount != NULL)
{
*outCount = count;
}
if (count == 0)
{
return NULL;
}
list = malloc((count + 1) * sizeof(struct objc_method *));
list[count] = NULL;
count = 0;
for (methods = cls->methods; methods != NULL; methods = methods->next)
{
unsigned int index;
for (index = 0; index < methods->count; index++)
{
list[count++] = method_at_index(methods, index);
}
}
return list;
}
Protocol*__unsafe_unretained* class_copyProtocolList(Class cls, unsigned int *outCount)
{
if (outCount != NULL)
{
*outCount = 0x0;
}
CHECK_ARG(cls);
struct objc_protocol_list *protocolList = NULL;
struct objc_protocol_list *list;
unsigned int count = 0;
Protocol **protocols;
if (Nil != cls)
{
protocolList = cls->protocols;
}
for (list = protocolList; list != NULL; list = list->next)
{
count += list->count;
}
if (outCount != NULL)
{
*outCount = count;
}
if (count == 0)
{
return NULL;
}
protocols = malloc((count + 1) * sizeof(Protocol *));
protocols[count] = NULL;
count = 0;
for (list = protocolList; list != NULL; list = list->next)
{
memcpy(&protocols[count], list->list, list->count * sizeof(Protocol *));
count += list->count;
}
return protocols;
}
id class_createInstance(Class cls, size_t extraBytes)
{
CHECK_ARG(cls);
if (sizeof(id) == 4)
{
if (cls == SmallObjectClasses[0])
{
return (id)1;
}
}
else
{
for (int i=0 ; i<4 ; i++)
{
if (cls == SmallObjectClasses[i])
{
return (id)(uintptr_t)((i<<1)+1);
}
}
}
if (Nil == cls) { return nil; }
// Don't try to allocate an object of size 0, because there's no space for
// its isa pointer!
if (cls->instance_size < sizeof(Class)) { return nil; }
id obj = gc->allocate_class(cls, extraBytes);
obj->isa = cls;
checkARCAccessorsSlow(cls);
call_cxx_construct(obj);
return obj;
}
id object_copy(id obj, size_t size)
{
Class cls = object_getClass(obj);
id cpy = class_createInstance(cls, size - class_getInstanceSize(cls));
memcpy(((char*)cpy + sizeof(id)), ((char*)obj + sizeof(id)), size - sizeof(id));
return cpy;
}
id object_dispose(id obj)
{
call_cxx_destruct(obj);
gc->free_object(obj);
return nil;
}
Method class_getInstanceMethod(Class aClass, SEL aSelector)
{
CHECK_ARG(aClass);
CHECK_ARG(aSelector);
// If the class has a dtable installed, then we can use the fast path
if (classHasInstalledDtable(aClass))
{
// Do a dtable lookup to find out which class the method comes from.
struct objc_slot2 *slot = objc_get_slot2(aClass, aSelector, NULL);
if (NULL == slot)
{
slot = objc_get_slot2(aClass, sel_registerName(sel_getName(aSelector)), NULL);
if (NULL == slot)
{
return NULL;
}
}
// Slots are the same as methods.
return (struct objc_method*)slot;
}
Method m = class_getInstanceMethodNonrecursive(aClass, aSelector);
if (NULL != m)
{
return m;
}
return class_getInstanceMethod(class_getSuperclass(aClass), aSelector);
}
Method class_getClassMethod(Class aClass, SEL aSelector)
{
return class_getInstanceMethod(object_getClass((id)aClass), aSelector);
}
Ivar class_getClassVariable(Class cls, const char* name)
{
// Note: We don't have compiler support for cvars in ObjC
return class_getInstanceVariable(object_getClass((id)cls), name);
}
size_t class_getInstanceSize(Class cls)
{
if (Nil == cls) { return 0; }
return cls->instance_size;
}
Ivar class_getInstanceVariable(Class cls, const char *name)
{
if (name != NULL)
{
while (cls != Nil)
{
struct objc_ivar_list *ivarlist = cls->ivars;
if (ivarlist != NULL)
{
for (int i = 0; i < ivarlist->count; i++)
{
Ivar ivar = ivar_at_index(ivarlist, i);
if (strcmp(ivar->name, name) == 0)
{
return ivar;
}
}
}
cls = class_getSuperclass(cls);
}
}
return NULL;
}
// The format of the char* is undocumented. This function is only ever used in
// conjunction with class_setIvarLayout().
const char *class_getIvarLayout(Class cls)
{
CHECK_ARG(cls);
return (char*)cls->ivars;
}
const char * class_getName(Class cls)
{
if (Nil == cls) { return "nil"; }
return cls->name;
}
int class_getVersion(Class theClass)
{
CHECK_ARG(theClass);
return theClass->version;
}
const char *class_getWeakIvarLayout(Class cls)
{
assert(0 && "Weak ivars not supported");
return NULL;
}
BOOL class_isMetaClass(Class cls)
{
CHECK_ARG(cls);
return objc_test_class_flag(cls, objc_class_flag_meta);
}
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
{
if (Nil == cls) { return (IMP)0; }
SEL sel = sel_registerTypedName_np(sel_getName(name), types);
Method method = class_getInstanceMethodNonrecursive(cls, sel);
if (method == NULL)
{
class_addMethod(cls, sel, imp, types);
return NULL;
}
IMP old = (IMP)method->imp;
method->imp = imp;
return old;
}
void class_setIvarLayout(Class cls, const char *layout)
{
if ((Nil == cls) || (NULL == layout)) { return; }
struct objc_ivar_list *list = (struct objc_ivar_list*)layout;
size_t listsize = sizeof(struct objc_ivar_list) +
sizeof(struct objc_ivar) * (list->count);
cls->ivars = malloc(listsize);
memcpy(cls->ivars, list, listsize);
}
__attribute__((deprecated))
Class class_setSuperclass(Class cls, Class newSuper)
{
CHECK_ARG(cls);
CHECK_ARG(newSuper);
Class oldSuper;
if (Nil == cls) { return Nil; }
{
LOCK_RUNTIME_FOR_SCOPE();
oldSuper = cls->super_class;
if (oldSuper == newSuper) { return newSuper; }
safe_remove_from_subclass_list(cls);
objc_resolve_class(newSuper);
cls->super_class = newSuper;
// The super class's subclass list is used in certain method resolution scenarios.
cls->sibling_class = cls->super_class->subclass_list;
cls->super_class->subclass_list = cls;
if (UNLIKELY(class_isMetaClass(cls)))
{
// newSuper is presumably a metaclass. Its isa will therefore be the appropriate root metaclass.
cls->isa = newSuper->isa;
}
else
{
Class meta = cls->isa, newSuperMeta = newSuper->isa;
// Update the metaclass's superclass.
safe_remove_from_subclass_list(meta);
objc_resolve_class(newSuperMeta);
meta->super_class = newSuperMeta;
meta->isa = newSuperMeta->isa;
// The super class's subclass list is used in certain method resolution scenarios.
meta->sibling_class = newSuperMeta->subclass_list;
newSuperMeta->subclass_list = meta;
}
LOCK_FOR_SCOPE(&initialize_lock);
if (!objc_test_class_flag(cls, objc_class_flag_initialized))
{
// Uninitialized classes don't have dtables to update
// and don't need their superclasses initialized.
return oldSuper;
}
}
objc_send_initialize((id)newSuper); // also initializes the metaclass
objc_update_dtable_for_new_superclass(cls->isa, newSuper->isa);
objc_update_dtable_for_new_superclass(cls, newSuper);
return oldSuper;
}
void class_setVersion(Class theClass, int version)
{
if (Nil == theClass) { return; }
theClass->version = version;
}
void class_setWeakIvarLayout(Class cls, const char *layout)
{
assert(0 && "Not implemented");
}
const char * ivar_getName(Ivar ivar)
{
CHECK_ARG(ivar);
return ivar->name;
}
ptrdiff_t ivar_getOffset(Ivar ivar)
{
CHECK_ARG(ivar);
return *ivar->offset;
}
const char * ivar_getTypeEncoding(Ivar ivar)
{
CHECK_ARG(ivar);
return ivar->type;
}
void method_exchangeImplementations(Method m1, Method m2)
{
if (NULL == m1 || NULL == m2) { return; }
IMP tmp = (IMP)m1->imp;
m1->imp = m2->imp;
m2->imp = tmp;
}
IMP method_getImplementation(Method method)
{
if (NULL == method) { return (IMP)NULL; }
return (IMP)method->imp;
}
SEL method_getName(Method method)
{
if (NULL == method) { return (SEL)NULL; }
return (SEL)method->selector;
}
IMP method_setImplementation(Method method, IMP imp)
{
if (NULL == method) { return (IMP)NULL; }
IMP old = (IMP)method->imp;
method->imp = imp;
return old;
}
id objc_getRequiredClass(const char *name)
{
CHECK_ARG(name);
id cls = objc_getClass(name);
if (nil == cls)
{
abort();
}
return cls;
}
PRIVATE void freeMethodLists(Class aClass)
{
struct objc_method_list *methods = aClass->methods;
while(methods != NULL)
{
for (int i=0 ; i<methods->count ; i++)
{
free((void*)method_at_index(methods, i)->types);
}
struct objc_method_list *current = methods;
methods = methods->next;
free(current);
}
}
PRIVATE void freeIvarLists(Class aClass)
{
struct objc_ivar_list *ivarlist = aClass->ivars;
if (NULL == ivarlist) { return; }
if (ivarlist->count > 0)
{
// For dynamically created classes, ivar offset variables are allocated
// as a contiguous range starting with the first one.
free(ivar_at_index(ivarlist, 0)->offset);
}
for (int i=0 ; i<ivarlist->count ; i++)
{
Ivar ivar = ivar_at_index(ivarlist, i);
free((void*)ivar->type);
free((void*)ivar->name);
}
free(ivarlist);
}
/*
* Removes a class from the subclass list found on its super class.
* Must be called with the objc runtime mutex locked.
*/
static inline void safe_remove_from_subclass_list(Class cls)
{
// If this class hasn't been added to the class hierarchy, then this is easy
if (!objc_test_class_flag(cls, objc_class_flag_resolved)) { return; }
Class sub = cls->super_class->subclass_list;
if (sub == cls)
{
cls->super_class->subclass_list = cls->sibling_class;
}
else
{
while (sub != NULL)
{
if (sub->sibling_class == cls)
{
sub->sibling_class = cls->sibling_class;
break;
}
sub = sub->sibling_class;
}
}
}
void objc_disposeClassPair(Class cls)
{
if (0 == cls) { return; }
Class meta = ((id)cls)->isa;
// Remove from the runtime system so nothing tries updating the dtable
// while we are freeing the class.
{
LOCK_RUNTIME_FOR_SCOPE();
safe_remove_from_subclass_list(meta);
safe_remove_from_subclass_list(cls);
class_table_remove(cls);
}
// Free the method and ivar lists.
freeMethodLists(cls);
freeMethodLists(meta);
freeIvarLists(cls);
if (cls->dtable != uninstalled_dtable)
{
free_dtable(cls->dtable);
}
if (meta->dtable != uninstalled_dtable)
{
free_dtable(meta->dtable);
}
// Free the class and metaclass
gc->free(meta);
gc->free(cls);
}
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
{
// Check the class doesn't already exist.
if (nil != objc_lookUpClass(name)) { return Nil; }
Class newClass = gc->malloc(sizeof(struct objc_class) + extraBytes);
if (Nil == newClass) { return Nil; }
// Create the metaclass
Class metaClass = gc->malloc(sizeof(struct objc_class));
if (Nil == superclass)
{
/*
* Metaclasses of root classes are precious little flowers and work a
* little differently:
*/
metaClass->isa = metaClass;
metaClass->super_class = newClass;
}
else
{
// Initialize the metaclass
// Set the meta-metaclass pointer to the name. The runtime will fix this
// in objc_resolve_class().
// If the superclass is not yet resolved, then we need to look it up
// via the class table.
metaClass->isa = superclass->isa;
metaClass->super_class = superclass->isa;
}
metaClass->name = strdup(name);
metaClass->info = objc_class_flag_meta | objc_class_flag_user_created;
metaClass->dtable = uninstalled_dtable;
metaClass->instance_size = sizeof(struct objc_class);
// Set up the new class
newClass->isa = metaClass;
newClass->super_class = superclass;
newClass->name = strdup(name);
newClass->info = objc_class_flag_user_created;
newClass->dtable = uninstalled_dtable;
newClass->abi_version = 2;
metaClass->abi_version = 2;
if (Nil == superclass)
{
newClass->instance_size = sizeof(struct objc_class*);
}
else
{
newClass->instance_size = superclass->instance_size;
}
return newClass;
}
void *object_getIndexedIvars(id obj)
{
CHECK_ARG(obj);
size_t size = classForObject(obj)->instance_size;
if ((0 == size) && class_isMetaClass(classForObject(obj)))
{
size = sizeof(struct objc_class);
}
return ((char*)obj) + size;
}
Class object_getClass(id obj)
{
CHECK_ARG(obj);
Class isa = classForObject(obj);
while ((Nil != isa) && objc_test_class_flag(isa, objc_class_flag_hidden_class))
{
isa = isa->super_class;
}
return isa;
}
Class object_setClass(id obj, Class cls)
{
CHECK_ARG(obj);
// If this is a small object, then don't set its class.
if (isSmallObject(obj)) { return classForObject(obj); }
Class oldClass = obj->isa;
obj->isa = cls;
return oldClass;
}
const char *object_getClassName(id obj)
{
CHECK_ARG(obj);
return class_getName(object_getClass(obj));
}
void objc_registerClassPair(Class cls)
{
if (cls->ivars != NULL)
{
int *ptrs = calloc(cls->ivars->count, sizeof(int));
for (int i=0 ; i<cls->ivars->count ; i++)
{
ptrs[i] = (int)(intptr_t)ivar_at_index(cls->ivars, i)->offset;
ivar_at_index(cls->ivars, i)->offset = &ptrs[i];
}
}
LOCK_RUNTIME_FOR_SCOPE();
class_table_insert(cls);
objc_resolve_class(cls);
}