forked from gnustep/libobjc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmsg2.c
493 lines (453 loc) · 14 KB
/
sendmsg2.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
#include "objc/runtime.h"
#include "lock.h"
#include "dtable.h"
#include "selector.h"
#include "loader.h"
#include "objc/hooks.h"
#include <stdint.h>
#include <stdio.h>
void objc_send_initialize(id object);
static long long nil_method(id self, SEL _cmd) { return 0; }
static long double nil_method_D(id self, SEL _cmd) { return 0; }
static double nil_method_d(id self, SEL _cmd) { return 0; }
static float nil_method_f(id self, SEL _cmd) { return 0; }
static struct objc_slot nil_slot_v1 = { Nil, Nil, 0, 1, (IMP)nil_method };
static struct objc_slot nil_slot_D_v1 = { Nil, Nil, 0, 1, (IMP)nil_method_D };
static struct objc_slot nil_slot_d_v1 = { Nil, Nil, 0, 1, (IMP)nil_method_d };
static struct objc_slot nil_slot_f_v1 = { Nil, Nil, 0, 1, (IMP)nil_method_f };
static struct objc_method nil_slot = { (IMP)nil_method, NULL, NULL };
static struct objc_method nil_slot_D = { (IMP)nil_method_D, NULL, NULL };
static struct objc_method nil_slot_d = { (IMP)nil_method_d, NULL, NULL };
static struct objc_method nil_slot_f = { (IMP)nil_method_f, NULL, NULL };
static struct objc_slot2* objc_slot_lookup(id *receiver, SEL selector);
// Default implementations of the two new hooks. Return NULL.
static id objc_proxy_lookup_null(id receiver, SEL op) { return nil; }
static struct objc_slot *objc_msg_forward3_null(id receiver, SEL op) { return &nil_slot_v1; }
id (*objc_proxy_lookup)(id receiver, SEL op) = objc_proxy_lookup_null;
struct objc_slot *(*__objc_msg_forward3)(id receiver, SEL op) = objc_msg_forward3_null;
static IMP forward2(id self, SEL _cmd)
{
return __objc_msg_forward3(self, _cmd)->method;
}
IMP (*__objc_msg_forward2)(id, SEL) = forward2;
__thread struct objc_method uncacheable_slot = { (IMP)nil_method, NULL, NULL };
__thread struct objc_slot uncacheable_slot_v1 = { Nil, Nil, 0, 0, (IMP)nil_method };
#ifndef NO_SELECTOR_MISMATCH_WARNINGS
static IMP objc_selector_type_mismatch(Class cls, SEL
selector, struct objc_slot2 *result)
{
fprintf(stderr, "Calling [%s %c%s] with incorrect signature. "
"Method has %s (%s), selector has %s\n",
cls->name,
class_isMetaClass(cls) ? '+' : '-',
sel_getName(selector),
sel_getType_np(((struct objc_method*)result)->selector),
((struct objc_method*)result)->types,
sel_getType_np(selector));
return result->method;
}
#else
static IMP objc_selector_type_mismatch(Class cls, SEL
selector, struct objc_slot2 *result)
{
return result->method;
}
#endif
IMP (*_objc_selector_type_mismatch2)(Class cls, SEL
selector, struct objc_slot2 *result) = objc_selector_type_mismatch;
struct objc_slot *(*_objc_selector_type_mismatch)(Class cls, SEL
selector, struct objc_slot *result);
static IMP call_mismatch_hook(Class cls, SEL sel, struct objc_slot2 *slot)
{
if (_objc_selector_type_mismatch &&
(!_objc_selector_type_mismatch2 ||
(_objc_selector_type_mismatch2 == objc_selector_type_mismatch)))
{
struct objc_slot fwdslot;
fwdslot.types = ((struct objc_method*)slot)->types;
fwdslot.selector = sel;
fwdslot.method = slot->method;
struct objc_slot *slot_v1 = _objc_selector_type_mismatch(cls, sel, &uncacheable_slot_v1);
return slot_v1->method;
}
return _objc_selector_type_mismatch2(cls, sel, slot);
}
static
// Uncomment for debugging
//__attribute__((noinline))
__attribute__((always_inline))
struct objc_slot2 *objc_msg_lookup_internal(id *receiver, SEL selector, uint64_t *version)
{
if (version)
{
#ifdef NO_SAFE_CACHING
// Always write 0 to version, marking the slot as uncacheable.
*version = 0;
#else
*version = objc_method_cache_version;
#endif
}
Class class = classForObject((*receiver));
retry:;
struct objc_slot2 * result = objc_dtable_lookup(class->dtable, selector->index);
if (UNLIKELY(0 == result))
{
dtable_t dtable = dtable_for_class(class);
/* Install the dtable if it hasn't already been initialized. */
if (dtable == uninstalled_dtable)
{
objc_send_initialize(*receiver);
dtable = dtable_for_class(class);
result = objc_dtable_lookup(dtable, selector->index);
}
else
{
// Check again incase another thread updated the dtable while we
// weren't looking
result = objc_dtable_lookup(dtable, selector->index);
}
if (0 == result)
{
if ((result = objc_dtable_lookup(dtable, get_untyped_idx(selector))))
{
#ifndef NO_SAFE_CACHING
if (version)
{
*version = 0;
}
#endif
uncacheable_slot.imp = call_mismatch_hook(class, selector, result);
result = (struct objc_slot2*)&uncacheable_slot;
}
id newReceiver = objc_proxy_lookup(*receiver, selector);
// If some other library wants us to play forwarding games, try
// again with the new object.
if (nil != newReceiver)
{
*receiver = newReceiver;
return objc_slot_lookup(receiver, selector);
}
if (0 == result)
{
#ifndef NO_SAFE_CACHING
if (version)
{
*version = 0;
}
#endif
uncacheable_slot.imp = __objc_msg_forward2(*receiver, selector);
result = (struct objc_slot2*)&uncacheable_slot;
}
}
}
return result;
}
PRIVATE IMP slowMsgLookup(id *receiver, SEL cmd)
{
// By the time we've got here, the assembly version of this function has
// already done the nil checks.
return objc_msg_lookup_internal(receiver, cmd, NULL)->method;
}
PRIVATE void logInt(void *a)
{
fprintf(stderr, "Value: %p\n", a);
}
/**
* New Objective-C lookup function. This permits the lookup to modify the
* receiver and also supports multi-dimensional dispatch based on the sender.
*/
struct objc_slot *objc_msg_lookup_sender(id *receiver, SEL selector, id sender)
{
// Returning a nil slot allows the caller to cache the lookup for nil too,
// although this is not particularly useful because the nil method can be
// inlined trivially.
if (UNLIKELY(*receiver == nil))
{
// Return the correct kind of zero, depending on the type encoding.
if (selector->types)
{
const char *t = selector->types;
// Skip type qualifiers
while ('r' == *t || 'n' == *t || 'N' == *t || 'o' == *t ||
'O' == *t || 'R' == *t || 'V' == *t || 'A' == *t)
{
t++;
}
switch (selector->types[0])
{
case 'D': return &nil_slot_D_v1;
case 'd': return &nil_slot_d_v1;
case 'f': return &nil_slot_f_v1;
}
}
return &nil_slot_v1;
}
struct objc_slot2 *slot = objc_msg_lookup_internal(receiver, selector, NULL);
uncacheable_slot_v1.owner = Nil;
uncacheable_slot_v1.types = sel_getType_np(((struct objc_method*)slot)->selector);
uncacheable_slot_v1.selector = selector;
uncacheable_slot_v1.method = slot->method;
return &uncacheable_slot_v1;
}
static struct objc_slot2* objc_slot_lookup(id *receiver, SEL selector)
{
// Returning a nil slot allows the caller to cache the lookup for nil too,
// although this is not particularly useful because the nil method can be
// inlined trivially.
if (UNLIKELY(*receiver == nil))
{
// Return the correct kind of zero, depending on the type encoding.
if (selector->types)
{
const char *t = selector->types;
// Skip type qualifiers
while ('r' == *t || 'n' == *t || 'N' == *t || 'o' == *t ||
'O' == *t || 'R' == *t || 'V' == *t || 'A' == *t)
{
t++;
}
switch (selector->types[0])
{
case 'D': return (struct objc_slot2*)&nil_slot_D;
case 'd': return (struct objc_slot2*)&nil_slot_d;
case 'f': return (struct objc_slot2*)&nil_slot_f;
}
}
return (struct objc_slot2*)&nil_slot;
}
return objc_msg_lookup_internal(receiver, selector, NULL);
}
struct objc_slot2 *objc_slot_lookup_version(id *receiver, SEL selector, uint64_t *version)
{
// Returning a nil slot allows the caller to cache the lookup for nil too,
// although this is not particularly useful because the nil method can be
// inlined trivially.
if (UNLIKELY(*receiver == nil))
{
#ifndef NO_SAFE_CACHING
if (version)
{
*version = 0;
}
#endif
// Return the correct kind of zero, depending on the type encoding.
if (selector->types)
{
const char *t = selector->types;
// Skip type qualifiers
while ('r' == *t || 'n' == *t || 'N' == *t || 'o' == *t ||
'O' == *t || 'R' == *t || 'V' == *t || 'A' == *t)
{
t++;
}
switch (selector->types[0])
{
case 'D': return (struct objc_slot2*)&nil_slot_D;
case 'd': return (struct objc_slot2*)&nil_slot_d;
case 'f': return (struct objc_slot2*)&nil_slot_f;
}
}
return (struct objc_slot2*)&nil_slot;
}
return objc_msg_lookup_internal(receiver, selector, version);
}
IMP objc_msg_lookup2(id *receiver, SEL selector)
{
return objc_slot_lookup(receiver, selector)->method;
}
struct objc_slot2 *objc_slot_lookup_super2(struct objc_super *super, SEL selector)
{
id receiver = super->receiver;
if (receiver)
{
Class class = super->class;
struct objc_slot2 * result = objc_dtable_lookup(dtable_for_class(class),
selector->index);
if (0 == result)
{
Class class = classForObject(receiver);
// Dtable should always be installed in the superclass in
// Objective-C, but may not be for other languages (Python).
if (dtable_for_class(class) == uninstalled_dtable)
{
if (class_isMetaClass(class))
{
objc_send_initialize(receiver);
}
else
{
objc_send_initialize((id)class);
}
objc_send_initialize((id)class);
return objc_slot_lookup_super2(super, selector);
}
uncacheable_slot.imp = __objc_msg_forward2(receiver, selector);
return (struct objc_slot2*)&uncacheable_slot;
}
return result;
}
return (struct objc_slot2*)&nil_slot;
}
OBJC_PUBLIC
struct objc_slot *objc_slot_lookup_super(struct objc_super *super, SEL selector)
{
id receiver = super->receiver;
if (receiver)
{
Class class = super->class;
struct objc_slot2 * result = objc_dtable_lookup(dtable_for_class(class),
selector->index);
if (0 == result)
{
Class class = classForObject(receiver);
// Dtable should always be installed in the superclass in
// Objective-C, but may not be for other languages (Python).
if (dtable_for_class(class) == uninstalled_dtable)
{
if (class_isMetaClass(class))
{
objc_send_initialize(receiver);
}
else
{
objc_send_initialize((id)class);
}
objc_send_initialize((id)class);
return objc_slot_lookup_super(super, selector);
}
uncacheable_slot_v1.owner = Nil;
uncacheable_slot_v1.types = sel_getType_np(selector);
uncacheable_slot_v1.selector = selector;
uncacheable_slot_v1.method = __objc_msg_forward2(receiver, selector);
return &uncacheable_slot_v1;
}
uncacheable_slot_v1.owner = Nil;
uncacheable_slot_v1.types = sel_getType_np(((struct objc_method*)result)->selector);
uncacheable_slot_v1.selector = selector;
uncacheable_slot_v1.method = result->method;
return &uncacheable_slot_v1;
}
return &nil_slot_v1;
}
/**
* looks up a slot without invoking any forwarding mechanisms
*/
struct objc_slot2 *objc_get_slot2(Class cls, SEL selector, uint64_t *version)
{
#ifndef NO_SAFE_CACHING
if (version)
{
*version = objc_method_cache_version;
}
#endif
struct objc_slot2 * result = objc_dtable_lookup(cls->dtable, selector->index);
if (0 == result)
{
void *dtable = dtable_for_class(cls);
/* Install the dtable if it hasn't already been initialized. */
if (dtable == uninstalled_dtable)
{
dtable = dtable_for_class(cls);
result = objc_dtable_lookup(dtable, selector->index);
}
else
{
// Check again incase another thread updated the dtable while we
// weren't looking
result = objc_dtable_lookup(dtable, selector->index);
}
if (NULL == result)
{
if ((result = objc_dtable_lookup(dtable, get_untyped_idx(selector))))
{
#ifndef NO_SAFE_CACHING
if (version)
{
*version = 0;
}
#endif
uncacheable_slot.imp = call_mismatch_hook(cls, selector, result);
result = (struct objc_slot2*)&uncacheable_slot;
}
}
}
return result;
}
struct objc_slot *objc_get_slot(Class cls, SEL selector)
{
struct objc_slot2 *result = objc_get_slot2(cls, selector, NULL);
if (result == NULL)
{
return NULL;
}
uncacheable_slot_v1.owner = Nil;
// Don't leak extended type encodings!
uncacheable_slot_v1.types = sel_getType_np(((struct objc_method*)result)->selector);
uncacheable_slot_v1.selector = selector;
uncacheable_slot_v1.method = result->method;
return &uncacheable_slot_v1;
}
////////////////////////////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////////////////////////////
BOOL class_respondsToSelector(Class cls, SEL selector)
{
if (0 == selector || 0 == cls) { return NO; }
return NULL != objc_get_slot2(cls, selector, NULL);
}
IMP class_getMethodImplementation(Class cls, SEL name)
{
if ((Nil == cls) || (NULL == name)) { return (IMP)0; }
struct objc_slot2 * slot = objc_get_slot2(cls, name, NULL);
return NULL != slot ? slot->method : __objc_msg_forward2(nil, name);
}
IMP class_getMethodImplementation_stret(Class cls, SEL name)
{
return class_getMethodImplementation(cls, name);
}
////////////////////////////////////////////////////////////////////////////////
// Legacy compatibility
////////////////////////////////////////////////////////////////////////////////
#ifndef NO_LEGACY
/**
* Legacy message lookup function.
*/
BOOL __objc_responds_to(id object, SEL sel)
{
return class_respondsToSelector(classForObject(object), sel);
}
IMP get_imp(Class cls, SEL selector)
{
return class_getMethodImplementation(cls, selector);
}
/**
* Message send function that only ever worked on a small subset of compiler /
* architecture combinations.
*/
void *objc_msg_sendv(void)
{
fprintf(stderr, "objc_msg_sendv() never worked correctly. Don't use it.\n");
abort();
}
#endif
/**
* Legacy message lookup function. Does not support fast proxies or safe IMP
* caching.
*/
IMP objc_msg_lookup(id receiver, SEL selector)
{
if (nil == receiver) { return (IMP)nil_method; }
id self = receiver;
struct objc_slot2 * slot = objc_msg_lookup_internal(&self, selector, NULL);
// If the receiver is changed by the lookup mechanism then we have to fall
// back to old-style forwarding.
if (self != receiver)
{
return __objc_msg_forward2(receiver, selector);
}
return slot->method;
}
IMP objc_msg_lookup_super(struct objc_super *super, SEL selector)
{
return objc_slot_lookup_super2(super, selector)->method;
}