2010-06-03 15:17:12 +00:00
|
|
|
#include "objc/runtime.h"
|
2009-09-20 15:48:57 +00:00
|
|
|
#include "lock.h"
|
2010-06-03 15:17:12 +00:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "selector.h"
|
|
|
|
#include "loader.h"
|
|
|
|
#include "objc/hooks.h"
|
2009-09-20 15:48:57 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <dlfcn.h>
|
2010-06-03 15:17:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void objc_send_initialize(id object);
|
2009-09-20 15:48:57 +00:00
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
static id nil_method(id self, SEL _cmd) { return nil; }
|
|
|
|
|
2011-04-22 12:13:28 +00:00
|
|
|
static struct objc_slot nil_slot = { Nil, Nil, 0, 1, (IMP)nil_method };
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
typedef struct objc_slot *Slot_t;
|
|
|
|
|
|
|
|
Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
|
|
|
|
|
|
|
|
// Default implementations of the two new hooks. Return NULL.
|
|
|
|
static id objc_proxy_lookup_null(id receiver, SEL op) { return nil; }
|
2009-11-14 18:09:51 +00:00
|
|
|
static Slot_t objc_msg_forward3_null(id receiver, SEL op) { return &nil_slot; }
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
id (*objc_proxy_lookup)(id receiver, SEL op) = objc_proxy_lookup_null;
|
2010-07-21 12:26:49 +00:00
|
|
|
Slot_t (*__objc_msg_forward3)(id receiver, SEL op) = objc_msg_forward3_null;
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2011-04-06 18:17:18 +00:00
|
|
|
#ifdef NO_SELECTOR_MISMATCH_WARNINGS
|
2010-09-01 12:48:39 +00:00
|
|
|
static struct objc_slot* objc_selector_type_mismatch(Class cls, SEL
|
|
|
|
selector, Slot_t result)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Calling [%s %c%s] with incorrect signature. "
|
|
|
|
"Method has %s, selector has %s\n",
|
|
|
|
cls->name,
|
|
|
|
class_isMetaClass(cls) ? '+' : '-',
|
|
|
|
sel_getName(selector),
|
|
|
|
result->types,
|
|
|
|
sel_getType_np(selector));
|
|
|
|
return result;
|
|
|
|
}
|
2011-04-06 18:17:18 +00:00
|
|
|
#else
|
|
|
|
static struct objc_slot* objc_selector_type_mismatch(Class cls, SEL
|
|
|
|
selector, Slot_t result)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2010-09-01 12:48:39 +00:00
|
|
|
struct objc_slot* (*_objc_selector_type_mismatch)(Class cls, SEL
|
|
|
|
selector, struct objc_slot *result) = objc_selector_type_mismatch;
|
2010-07-21 12:26:49 +00:00
|
|
|
static
|
|
|
|
// Uncomment for debugging
|
2010-09-16 04:57:56 +00:00
|
|
|
//__attribute__((noinline))
|
|
|
|
__attribute__((always_inline))
|
2010-07-21 12:26:49 +00:00
|
|
|
Slot_t objc_msg_lookup_internal(id *receiver,
|
|
|
|
SEL selector,
|
|
|
|
id sender)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-08-31 20:40:08 +00:00
|
|
|
retry:;
|
2010-08-15 11:17:00 +00:00
|
|
|
Slot_t result = objc_dtable_lookup((*receiver)->isa->dtable,
|
2011-03-15 17:27:26 +00:00
|
|
|
selector->index);
|
2009-09-08 16:18:59 +00:00
|
|
|
if (0 == result)
|
|
|
|
{
|
2010-06-03 15:17:12 +00:00
|
|
|
Class class = (*receiver)->isa;
|
2010-08-15 11:17:00 +00:00
|
|
|
dtable_t dtable = dtable_for_class(class);
|
2009-09-08 16:18:59 +00:00
|
|
|
/* Install the dtable if it hasn't already been initialized. */
|
2011-04-14 16:44:34 +00:00
|
|
|
if (dtable == uninstalled_dtable)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-03 15:17:12 +00:00
|
|
|
objc_send_initialize(*receiver);
|
2010-02-25 16:34:05 +00:00
|
|
|
dtable = dtable_for_class(class);
|
2011-03-15 17:27:26 +00:00
|
|
|
result = objc_dtable_lookup(dtable, selector->index);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check again incase another thread updated the dtable while we
|
|
|
|
// weren't looking
|
2011-03-15 17:27:26 +00:00
|
|
|
result = objc_dtable_lookup(dtable, selector->index);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
if (0 == result)
|
|
|
|
{
|
2011-04-15 12:08:49 +00:00
|
|
|
if (!isSelRegistered(selector))
|
2010-08-31 20:40:08 +00:00
|
|
|
{
|
|
|
|
objc_register_selector(selector);
|
|
|
|
// This should be a tail call, but GCC is stupid and won't let
|
|
|
|
// us tail call an always_inline function.
|
|
|
|
goto retry;
|
|
|
|
}
|
2010-08-15 11:17:00 +00:00
|
|
|
if ((result = objc_dtable_lookup(dtable, get_untyped_idx(selector))))
|
2010-06-29 12:42:20 +00:00
|
|
|
{
|
2010-09-01 12:48:39 +00:00
|
|
|
return _objc_selector_type_mismatch((*receiver)->isa, selector,
|
|
|
|
result);
|
2010-06-29 12:42:20 +00:00
|
|
|
}
|
2009-09-20 15:48:57 +00:00
|
|
|
id newReceiver = objc_proxy_lookup(*receiver, selector);
|
2010-07-21 12:26:49 +00:00
|
|
|
// If some other library wants us to play forwarding games, try
|
|
|
|
// again with the new object.
|
2009-09-20 15:48:57 +00:00
|
|
|
if (nil != newReceiver)
|
|
|
|
{
|
|
|
|
*receiver = newReceiver;
|
|
|
|
return objc_msg_lookup_sender(receiver, selector, sender);
|
|
|
|
}
|
|
|
|
if (0 == result)
|
|
|
|
{
|
2010-07-21 12:26:49 +00:00
|
|
|
result = __objc_msg_forward3(*receiver, selector);
|
2009-09-20 15:48:57 +00:00
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Slot_t (*objc_plane_lookup)(id *receiver, SEL op, id sender) =
|
|
|
|
objc_msg_lookup_internal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New Objective-C lookup function. This permits the lookup to modify the
|
|
|
|
* receiver and also supports multi-dimensional dispatch based on the sender.
|
|
|
|
*/
|
|
|
|
Slot_t 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(*receiver == nil)
|
|
|
|
{
|
|
|
|
return &nil_slot;
|
|
|
|
}
|
|
|
|
|
2010-03-13 18:42:02 +00:00
|
|
|
/*
|
|
|
|
* The self pointer is invalid in some code. This test is disabled until
|
|
|
|
* we can guarantee that it is not (e.g. with GCKit)
|
2009-09-08 16:18:59 +00:00
|
|
|
if (__builtin_expect(sender == nil
|
|
|
|
||
|
2010-06-03 15:17:12 +00:00
|
|
|
(sender->isa->info & (*receiver)->isa->info & _CLS_PLANE_AWARE),1))
|
2010-03-13 18:42:02 +00:00
|
|
|
*/
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
|
|
|
return objc_msg_lookup_internal(receiver, selector, sender);
|
|
|
|
}
|
|
|
|
// If we are in plane-aware code
|
|
|
|
void *senderPlaneID = *((void**)sender - 1);
|
|
|
|
void *receiverPlaneID = *((void**)receiver - 1);
|
|
|
|
if (senderPlaneID == receiverPlaneID)
|
|
|
|
{
|
|
|
|
return objc_msg_lookup_internal(receiver, selector, sender);
|
|
|
|
}
|
|
|
|
return objc_plane_lookup(receiver, selector, sender);
|
|
|
|
}
|
2009-09-20 15:48:57 +00:00
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
Slot_t objc_slot_lookup_super(struct objc_super *super, SEL selector)
|
2010-05-01 13:54:47 +00:00
|
|
|
{
|
2010-06-03 15:17:12 +00:00
|
|
|
id receiver = super->receiver;
|
2010-05-01 13:54:47 +00:00
|
|
|
if (receiver)
|
|
|
|
{
|
|
|
|
Class class = super->class;
|
2010-08-15 11:17:00 +00:00
|
|
|
Slot_t result = objc_dtable_lookup(dtable_for_class(class),
|
2011-03-15 17:27:26 +00:00
|
|
|
selector->index);
|
2010-05-01 13:54:47 +00:00
|
|
|
if (0 == result)
|
|
|
|
{
|
|
|
|
// Dtable should always be installed in the superclass
|
2011-04-14 16:44:34 +00:00
|
|
|
assert(dtable_for_class(class) != uninstalled_dtable);
|
2010-05-01 13:54:47 +00:00
|
|
|
result = &nil_slot;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return &nil_slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Profiling
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-09-20 15:48:57 +00:00
|
|
|
/**
|
|
|
|
* Mutex used to protect non-thread-safe parts of the profiling subsystem.
|
|
|
|
*/
|
|
|
|
static mutex_t profileLock;
|
|
|
|
/**
|
|
|
|
* File used for writing the profiling symbol table.
|
|
|
|
*/
|
2010-05-01 13:54:47 +00:00
|
|
|
static FILE *profileSymbols;
|
2009-09-20 15:48:57 +00:00
|
|
|
/**
|
|
|
|
* File used for writing the profiling data.
|
|
|
|
*/
|
2010-05-01 13:54:47 +00:00
|
|
|
static FILE *profileData;
|
2009-09-20 15:48:57 +00:00
|
|
|
|
|
|
|
struct profile_info
|
|
|
|
{
|
|
|
|
const char *module;
|
|
|
|
int32_t callsite;
|
|
|
|
IMP method;
|
|
|
|
};
|
|
|
|
|
2011-04-14 16:44:34 +00:00
|
|
|
static void profile_init(void)
|
2009-09-20 15:48:57 +00:00
|
|
|
{
|
|
|
|
INIT_LOCK(profileLock);
|
|
|
|
profileSymbols = fopen("objc_profile.symbols", "a");
|
|
|
|
profileData = fopen("objc_profile.data", "a");
|
|
|
|
// Write markers indicating a new run.
|
|
|
|
fprintf(profileSymbols, "=== NEW TRACE ===\n");
|
2010-04-29 17:52:10 +00:00
|
|
|
struct profile_info profile_data = { 0, 0, 0 };
|
2009-09-20 15:48:57 +00:00
|
|
|
fwrite(&profile_data, sizeof(profile_data), 1, profileData);
|
|
|
|
}
|
2010-04-29 17:52:10 +00:00
|
|
|
|
|
|
|
void objc_profile_write_symbols(char **symbols)
|
|
|
|
{
|
|
|
|
if (NULL == profileData)
|
|
|
|
{
|
2011-04-14 16:44:34 +00:00
|
|
|
LOCK_RUNTIME_FOR_SCOPE();
|
2010-04-29 17:52:10 +00:00
|
|
|
if (NULL == profileData)
|
|
|
|
{
|
2011-04-14 16:44:34 +00:00
|
|
|
profile_init();
|
2010-04-29 17:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LOCK(&profileLock);
|
|
|
|
while(*symbols)
|
|
|
|
{
|
|
|
|
char *address = *(symbols++);
|
|
|
|
char *symbol = *(symbols++);
|
|
|
|
fprintf(profileSymbols, "%zx %s\n", (size_t)address, symbol);
|
|
|
|
}
|
|
|
|
UNLOCK(&profileLock);
|
|
|
|
fflush(profileSymbols);
|
|
|
|
}
|
|
|
|
|
2009-09-20 15:48:57 +00:00
|
|
|
/**
|
|
|
|
* Profiling version of the slot lookup. This takes a unique ID for the module
|
|
|
|
* and the callsite as extra arguments. The type of the receiver and the
|
|
|
|
* address of the resulting function are then logged to a file. These can then
|
|
|
|
* be used to determine whether adding slot caching is worthwhile, and whether
|
|
|
|
* any of the resulting methods should be speculatively inlined.
|
|
|
|
*/
|
2010-05-01 16:53:15 +00:00
|
|
|
void objc_msg_profile(id receiver, IMP method,
|
|
|
|
const char *module, int32_t callsite)
|
2009-09-20 15:48:57 +00:00
|
|
|
{
|
|
|
|
// Initialize the logging lazily. This prevents us from wasting any memory
|
|
|
|
// when we are not profiling.
|
2010-04-29 17:52:10 +00:00
|
|
|
if (NULL == profileData)
|
2009-09-20 15:48:57 +00:00
|
|
|
{
|
2011-04-14 16:44:34 +00:00
|
|
|
LOCK_RUNTIME_FOR_SCOPE();
|
2010-04-29 17:52:10 +00:00
|
|
|
if (NULL == profileData)
|
2009-09-20 15:48:57 +00:00
|
|
|
{
|
2011-04-14 16:44:34 +00:00
|
|
|
profile_init();
|
2009-09-20 15:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 17:52:10 +00:00
|
|
|
struct profile_info profile_data = { module, callsite, method };
|
2009-09-20 15:48:57 +00:00
|
|
|
fwrite(&profile_data, sizeof(profile_data), 1, profileData);
|
|
|
|
}
|
2010-06-03 15:17:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks up a slot without invoking any forwarding mechanisms
|
|
|
|
*/
|
|
|
|
Slot_t objc_get_slot(Class cls, SEL selector)
|
|
|
|
{
|
2011-03-15 17:27:26 +00:00
|
|
|
Slot_t result = objc_dtable_lookup(cls->dtable, selector->index);
|
2010-06-03 15:17:12 +00:00
|
|
|
if (0 == result)
|
|
|
|
{
|
|
|
|
void *dtable = dtable_for_class(cls);
|
|
|
|
/* Install the dtable if it hasn't already been initialized. */
|
2011-04-14 16:44:34 +00:00
|
|
|
if (dtable == uninstalled_dtable)
|
2010-06-03 15:17:12 +00:00
|
|
|
{
|
|
|
|
dtable = dtable_for_class(cls);
|
2011-03-15 17:27:26 +00:00
|
|
|
result = objc_dtable_lookup(dtable, selector->index);
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check again incase another thread updated the dtable while we
|
|
|
|
// weren't looking
|
2011-03-15 17:27:26 +00:00
|
|
|
result = objc_dtable_lookup(dtable, selector->index);
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
2010-06-29 12:42:20 +00:00
|
|
|
if (NULL == result)
|
|
|
|
{
|
2011-04-15 12:08:49 +00:00
|
|
|
if (!isSelRegistered(selector))
|
2010-08-31 20:40:08 +00:00
|
|
|
{
|
|
|
|
objc_register_selector(selector);
|
|
|
|
return objc_get_slot(cls, selector);
|
|
|
|
}
|
2010-08-15 11:17:00 +00:00
|
|
|
if ((result = objc_dtable_lookup(dtable, get_untyped_idx(selector))))
|
2010-06-29 12:42:20 +00:00
|
|
|
{
|
2010-09-01 12:48:39 +00:00
|
|
|
return _objc_selector_type_mismatch(cls, selector, result);
|
2010-06-29 12:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Public API
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
BOOL class_respondsToSelector(Class cls, SEL selector)
|
|
|
|
{
|
2011-02-12 11:03:41 +00:00
|
|
|
if (0 == selector || 0 == cls) { return NO; }
|
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
return NULL != objc_get_slot(cls, selector);
|
|
|
|
}
|
|
|
|
|
2011-02-20 00:00:12 +00:00
|
|
|
IMP class_getMethodImplementation(Class cls, SEL name)
|
|
|
|
{
|
2011-02-20 19:12:39 +00:00
|
|
|
if ((Nil == cls) || (NULL == name)) { return (IMP)0; }
|
2011-02-20 00:00:12 +00:00
|
|
|
Slot_t slot = objc_get_slot(cls, name);
|
|
|
|
return NULL != slot ? slot->method : __objc_msg_forward2(nil, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMP class_getMethodImplementation_stret(Class cls, SEL name)
|
|
|
|
{
|
|
|
|
return class_getMethodImplementation(cls, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Legacy compatibility
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-14 16:44:34 +00:00
|
|
|
#ifndef NO_LEGACY
|
2010-06-03 15:17:12 +00:00
|
|
|
/**
|
|
|
|
* Legacy message lookup function.
|
|
|
|
*/
|
|
|
|
BOOL __objc_responds_to(id object, SEL sel)
|
|
|
|
{
|
|
|
|
return class_respondsToSelector(object->isa, sel);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMP get_imp(Class cls, SEL selector)
|
|
|
|
{
|
2011-02-20 00:00:12 +00:00
|
|
|
return class_getMethodImplementation(cls, selector);
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
2011-04-14 16:44:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2010-06-03 15:17:12 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
Slot_t slot = objc_msg_lookup_internal(&self, selector, nil);
|
|
|
|
if (self != receiver)
|
|
|
|
{
|
2010-07-21 12:26:49 +00:00
|
|
|
slot = __objc_msg_forward3(receiver, selector);
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
|
|
|
return slot->method;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMP objc_msg_lookup_super(struct objc_super *super, SEL selector)
|
|
|
|
{
|
|
|
|
return objc_slot_lookup_super(super, selector)->method;
|
|
|
|
}
|