preface
In the last article we explored dynamic method resolution, so if we didn’t deal with unimplemented methods in dynamic method resolution, what else could we do? With questions we continue to analyze the source code, first to find the method will be log_and_fill_cache, we enter the method to view, as shown in the figure:
There are two main approacheslogMessageSend
andinsert
.insert
As I said earlier, it’s the insert method. Then thislogMessageSend
What does it do? We enter again, as shown below:
Comment this out in this methodcreate/open the log file
Create or open a log file, andobjcMsgLogFD
The value of- 1
, so this method must open the log file. butobjcMsgLogEnabled = false
, global searchobjcMsgLogEnabled
, the results are as follows:
After browsing found only in instrumentObjcMessageSends will assign objcMsgLogEnabled false value, as shown in figure:
So we have to expand instrumentObjcMessageSends, then can control objcMsgLogEnabled values in the external. As shown in figure:
Run the program again, crash, and head to folder TMP /msgSends (the folder created by logMessageSend).
forward
We through logMessageSend generated files found in the middle of the two resolveInstanceMethod insert forwardingTargetForSelector and methodSignatureForSelector when these two methods? We don’t know. A global search will also see only a few declarations and empty implementations, as shown below:
How do you find it? Open Developer Documentation as shown:
On the shortcut shift + command + 0, search forwardingTargetForSelector and methodSignatureForSelector.
1.forwardingTargetForSelector
ForwardingTargetForSelector search results are as follows:
Returns an object that receives an unrecognized message, which simply means that I didn’t implement the message, but I can specify an instance object to handle the message. . Let’s try it, as shown below:
Return in forwardingTargetForSelector LGPerson instances of an object, and then run, as shown in figure:
callPerson
The instancesayWork
It doesn’t crash, and it prints-[LGPerson sayWork]
“Which means we’ve managed to dump the potLGPerson
. This process of flipping is calledFast forward
2,methodSignatureForSelector
MethodSignatureForSelector search results:
Here does not seem to say the specific function, scroll down:
Returns an NSMethodSignature object that contains the description of the method identified by the given selector. Then click – methodSignatureForSelector: as shown in figure:
Returns an NSMethodSignature object that contains the description of the method identified by the given selector. Let’s try writing this method:
When running the program, it is found that it still crashes, as shown in the figure below:
But gomethodSignatureForSelector
How do you handle it without breaking down? Let’s move on to the description:
There is a related method for forwardInvocation, described as follows:
We see the contents of this Important, probably means to rewrite this method need to rewrite methodSignatureForSelector, we have been rewritten, so write to try this method, as shown in figure:
Then run the program, as shown:
We clearly see the sayWork printed in the forwardInvocation and once again we handled the crash handling of the unimplemented method. This method of handling messages that IMPs cannot find is called slow lookups.
conclusion
Combining the two previous articles on runtime and the nature of methods and dynamic method resolution, the complete message lookup process is complete.