So I have a method that has 5 arguments. As expected, the registers state right before it's called:
$rdi: The receiver
$rsi: the selector for the method
$rdx: first arg
$rcx: second arg
$r8: third arg
$r9: fourth arg
$r10 fifth arg
Within the method, the first thing it does is call another objective-c method
This in turn calls objc_msgSend (see offset +58):
MyApp`-[GTMOAuth2WindowController webView:resource:willSendRequest:redirectResponse:fromDataSource:]:
0x10044a1a0 <+0>: pushq %rbp
0x10044a1a1 <+1>: movq %rsp, %rbp
0x10044a1a4 <+4>: subq $0x40, %rsp
0x10044a1a8 <+8>: movq 0x10(%rbp), %rax
0x10044a1ac <+12>: movq %rdi, -0x10(%rbp)
0x10044a1b0 <+16>: movq %rsi, -0x18(%rbp)
0x10044a1b4 <+20>: movq %rdx, -0x20(%rbp)
0x10044a1b8 <+24>: movq %rcx, -0x28(%rbp)
0x10044a1bc <+28>: movq %r8, -0x30(%rbp)
0x10044a1c0 <+32>: movq %r9, -0x38(%rbp)
0x10044a1c4 <+36>: movq %rax, -0x40(%rbp)
0x10044a1c8 <+40>: movq -0x10(%rbp), %rax
0x10044a1cc <+44>: movq -0x38(%rbp), %rdx
0x10044a1d0 <+48>: movq 0x2ffda9(%rip), %rsi ; "handleCookiesForResponse:"
0x10044a1d7 <+55>: movq %rax, %rdi
0x10044a1da <+58>: callq 0x1005839a2 ; symbol stub for: objc_msgSend
which then goes to the instructions for objc_msgSend:
libobjc.A.dylib`objc_msgSend:
-> 0x7fff9084a0c0 <+0>: testq %rdi, %rdi
0x7fff9084a0c3 <+3>: je 0x7fff9084a140 ; <+128>
0x7fff9084a0c6 <+6>: testb $0x1, %dil
0x7fff9084a0ca <+10>: jne 0x7fff9084a14b ; <+139>
0x7fff9084a0cd <+13>: movabsq $0x7ffffffffff8, %r11
0x7fff9084a0d7 <+23>: andq (%rdi), %r11
0x7fff9084a0da <+26>: movq %rsi, %r10
0x7fff9084a0dd <+29>: andl 0x18(%r11), %r10d
and I sometimes crash on offset +29, when the cpu tries to dereference the %r11 register.
My question is, why is objc_msgSend dereferencing that register? According to the System V ABI that is a scratch register. But it's dereferenced everytime objc_msgSend, and I can't really figure out what it's used for.
My crash is happening when there is an invalid pointer in %r11
It looks like at +23, the %rdi register (pointer to receiver) is dereferenced and andq'd with %r11, but I don't get what that does. But perhaps if the receiver was deallocated here, %r11 would be filled with junk?
This theory is corroborated by this assembly source w/ comments
Where I think it states the %r11 is used for the isa property
"class = self->isa".
Which would mean that the object is being released because the isa property is junked
If that were the case, how could I protect against this?
Would a check to see if( self ) before calling objc_msgSend suffice?