0

I have been wondering for a couple of days if NSInvocation should need the NSMethodSignature. Lets say we want to write our own NSInvocation, my requirements would be as so:

  1. I need a selector SEL
  2. The target object to call the selector on
  3. The argument array

Then i would get the IMP out from the target and the SEL, and pass the argument as parameters.

So, my question is, why do we need an NSMethodSignature to construct and use an NSInvocation?

Note: I do know that by having only a SEL and a target, we dont have the arguments and return type for this method, but why would we care about the types of the args and returns?

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56

2 Answers2

3

Each type in C has a different size. (Even the same type can have different sizes on different systems, but we'll ignore that for now.) int can have 32 or 64 bits, depending on the system. double takes 64 bits. char represents 8 bits (but might be passed as a regular int depending on the system's passing convention). And finally and most importantly, struct types have various sizes, depending on how many elements are in it and each of their sizes; there is no bound to how big it can be. So it is impossible to pass arguments the same way regardless of type. Therefore, how the calling function arranges the arguments, and how the called function interprets its arguments, must be dependent on the function's signature. (You cannot have a type-agnostic "argument array"; what would be the size of the array elements?) When normal function calls are compiled, the compiler knows the signature at compile time, and can arrange it correctly according to calling convention. But NSInvocation is for managing an invocation at runtime. Therefore, it needs a representation of the method signature to work.

There are several things that the NSInvocation can do. Each of those things requires knowledge of the number and types (at least the sizes of the types) of the parameters:

  1. When a message is sent to an object that does not have a method for it, the runtime constructs an NSInvocation object and passes it to -forwardInvocation:. The NSInvocation object contains a copy of all the arguments that are passed, since it can be stored and invoked later. Therefore, the runtime needs to know, at the very least, how big the parameters in total are, in order to copy the right amount of data from registers and/or stack (depending on how arguments are arranged in the calling convention) into the NSInvocation object.
  2. When you have an NSInvocation object, you can query for the value of the i'th argument, using -getArgument:atIndex:. You can also set/change the value for the i'th argument, using -setArgument:atIndex:. This requires it to know 1) Where in its data buffer the i'th parameter starts; this requires knowing how big the previous parameters are, and 2) how big the i'th parameter is, so that it can copy the right amount of data (if it copies too little, it will have a corrupt value; if it copies too much, say, when you do getArgument, it can overwrite the buffer you gave it; or when you do setArgument, overwrite other arguments).
  3. You can have it do -retainArguments, which causes it to retain all arguments of object pointer type. This requires it to distinguish between object pointer types and other types, so the type information must include not only the size.
  4. You can invoke the NSInvocation, which causes it to construct and execute the call to the method. This requires it to know, at the very least, how much data to copy from its buffer into the registers/stack to place all the data where the function will expect it. This requires knowing at least the combined size of all parameters, and probably also needs to know the sizes of individual parameters, so that the divide between parameters on registers and parameters on stack can be figured out correctly.
  5. You can get the return value of the call using -getReturnValue:; this has similar issues to the getting of arguments above.

    • A thing not mentioned above is that the return type may also have a great effect on the calling mechanism. On x86 and ARM, the common architectures for Objective-C, when the return type is a struct type, the calling convention is very different -- effectively an additional (first) parameter is added before all the regular parameters, which is a pointer to the space that the struct result should be written. This is instead of the regular calling convention where the result is returned in a register. (In PowerPC I believe that double return type is also treated specially.) So knowing the return type is essentially for constructing and invoking the NSInvocation.
newacct
  • 119,665
  • 29
  • 163
  • 224
  • So what you are saying is that internally NSInvocation does not use objc_msgSend, it uses calls the fptr directly and it copies the args to the registers etc... – Omar Abdelhafith Sep 06 '13 at 09:38
  • But why we pass it the `SEL`, the target and the `NSMethodSignature`, when we know that we could get the signature just by doing `[target methodSignatureForSelector:SEL]`, we could just pass the SEL and target and still get the method sig at runtime. what am i missing here? – Omar Abdelhafith Sep 06 '13 at 09:39
  • @OmarAbdelhafith: No, it does use `objc_msgSend()`. But in order to correctly use `objc_msgSend()`, you must also know the signature -- you must pretend that `objc_msgSend` has the type of the function pointer of the function you are calling. Because `objc_msgSend()` is simply a trampoline that replaces itself with the called function, leaving all the arguments in the registers and stack intact. – newacct Sep 06 '13 at 09:50
  • @OmarAbdelhafith: When the runtime creates an `NSInvocation` from a call, it does use `-methodSignatureForSelector:`. When you create an `NSInvocation` yourself, you are not required to specify the target right away. You could set the target after setting other arguments. So you need the signature in the beginning. I guess you might say it should require the target to create it. I am trying to think whether there are cases when you want to use a different signature than is returned by `[target methodSignatureForSelector:SEL]` but I can't think of any right now. – newacct Sep 06 '13 at 10:02
  • i think i can think of one now, its when the target has no implementation for the selector, the forwarding mechanism will need a signature to create an NSInvocation to call on another target, do you think that this applies? – Omar Abdelhafith Sep 06 '13 at 10:05
  • @OmarAbdelhafith: No, because even when you have no implementation for the selector, if you want to be able to handle it with `-forwardInvocation:`, you must still return the right signature in `-methodSignatureForSelector:`. In other words, if you override `-forwardInvocation:`, you must also override `-methodSignatureForSelector:` to return the proper signature. If `-methodSignatureForSelector:` returns `nil` , the runtime won't even bother calling `-forwardInvocation:` – newacct Sep 06 '13 at 10:17
1

NSMethodSignature is required for the message sending and forwarding mechanism to work properly for invocations. NSMethodSignature and NSInvocation were built as a wrapper around __builtin_call(), which is both architecture dependent, and extremely conservative about the stack space a given function requires. Hence, when invocations are invoked, __builtin_call() gets all the information it needs from the method signature, and can fail gracefully by throwing the call out to the forwarding mechanism knowing that it, too, receives the proper information about how the stack should look for re-invocation.

That being said, you cannot make a primitive NSInvocation without a method signature without modifying the C language to support converting arrays to VARARGS seeing as objc_msgSend() and its cousins won't allow it. Even if you could get around that, you'd need to calculate the size of the arguments and the return type (not too hard, but if you're wrong, you're wrong big time), and manage a proper call to __builtin_call(), which would require an intimate knowledge of the message sending architecture or an ffi (which probably drops down to __builtin_call() anyhow).

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • But why we pass it the SEL, the target and the NSMethodSignature, when we know that we could get the signature just by doing [target methodSignatureForSelector:SEL], we could just pass the SEL and target and still get the method sig at runtime. what am i missing here? – Omar Abdelhafith Sep 06 '13 at 09:43