You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 28, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on May 28, 2025. It is now read-only.
Address lack of proper nil-initialization in WinObjC compiler #1965
There were some speculative patches for objc_msgSend, but it turns out that the compiler skips the send entirely if the target is known to be nil. We need to investigate either implementing a nil-initializer in the compiler, or make a thorough pass through the codebase to make sure we're not relying on this behavior for complex types, followed by educating the team against sending messages to nil.
For example; this call is returning a completely bogus CGRect when 'image' is nil:
Regarding the workaround, if layer is nil then CGRectEqualToRect compares garbage. The only result is a no-op on a nil anyways, unless the garbage contains a NaN value (highly improbable) which could result in an arithmethic exception...
In my code, I rely on sending messages to nil to work as defined (doing nothing and returning 0). If WinObjC does not behave the same, it would be a bug.
David Dunham (@alakoring) I think the title to this thread is poorly worded and the actual issue is how are the (function return) registers initialized when the receiver is nil. For a word length it is zero'd out but for anything greater than a word length (such as double or a struct where sizeof is greater than word length) the registers are not necessarily zero'd past a word length (32 bits in this case, or 64 bits on a 64bit implementation). This is the same way it works on iOS and Mac and its always been that way ever since Objective-C was invented. It is pretty standard that a pointer byte length (an id type) is also the same as a word length.
Dustin L. Howett (MSFT) (@DHowett-MSFT) : Jared (@jaredhms) wrote: "or make a thorough pass through the codebase to make sure we're not relying on this behavior for complex types". ... that is a good idea and is the main takeaway that I got from this thread. (in fact, the entire codebase can use a good cleaning and methodical going over).
Thanks, Ed VanVliet (@edvv). We've done a substantial amount of general 'code scrubbing' (and there's certainly more room for improvement), but I want to ensure that we address this specific problem in our codebase - especially if it's going to take a while to address it in the compiler.
This is still a bit confusing. It is my understanding that the compiler works correctly for x86 targets. That is it zeros out 32 bit in the return register when the receiver is nil. That is the way it is suppose to work. Thus you need to scrub your code anyways for returns > 32 bits.
The ARM target compile may be something different (?). From what I am reading the arm target assembly does not zero out 32 bit in the return register when the receiver is nil? - that definitely needs to be fixed in the compiler and not in a WinObjC scrub if that is the case.
Also, if the return is typed as a float (as opposed to double) then it shouldn't matter anyways because float is 32 bits.
Am I understanding this issue right?
I also am getting the impression that you want to extend the ObjC compiler to handle complex returns. Why is that? If the receiver type is "id" (an uncast type, i.e.: void *) then the return type is unknown and there is no way to know how much of the register to zero out; so it defaults to 32 bits.
From my historical understanding, when the receiver is nil then 32 bits (or 64 on 64 bit implementations) is zero'd in the return register just because that is the implicit function [method] prototype return value for ObjC. -- i.e.: Nothing fancy in attempting to zero out the entire return register, etc.
Ed VanVliet (@edvv) With our compiler/runtime, 32-bit return values that go through the general purpose registers are properly zeroed when the receiver is nil. This is totally expected and per-specification.
The x86 implementation of objc_msgSend explicitly clears the x87 floating point register as well, for the objc_msgSend_fpret case. This may not get hit, and that's explained below.
On ARM, we may have an issue (not yet determined) where the VFP register set used for returns (S0-S7 or D0-D3) is not cleared in objc_msgSend or an objc_msgSend_fpret equivalent. This doesn't exceptionally matter, agaim because of the below.
The kicker: the Objective-C code generator we're using (CGObjCGNU with its objfw personality) explicitly skips the objc_msgSend if the receiver is nil; this means the call site gains the responsibility to clear the VFP registers on ARM or the x87 fp stack on x86.
Since 32-bit non-floating-point return values are generally returned in the same register used for self, this is fine: eax and r0 both contain nil after we skip the call, and the world remains sane.
Now, code was added to CGObjCGNU that attempts to clear even complex floating point and struct returns. I can only imagine that this is because OS X 10.5 and beyond, as well as iOS 5 (or 6?), unofficially specify that struct returns from nil receivers are zero-valued!
In CGObjCMac, Clang emits a local default-constructed value with static storage duration (memset to 0, which fits in with other static initialization). If the receiver is nil, a memcpy is emitted from this local static value to the final return value. Therefore, it's safe to initialize even a complex struct or floating point value with the return value from a message send to a nil receiver.
It looks like CGObjCGNU tried to mimic this: there's a comment stating that it tries to do so, but we have not been able to successfully reproduce that behaviour. It seems broken, but not intentionally so.
Since the return type is known to the compiler, and there's precedent in both the mac and GNU code generators, this is a safe (albeit fairly new compared to the rest of Objective-C) convenience to provide.
Activity
Working around microsoft#1965 in UIImageSetLayerContents.
edvv commentedon Feb 14, 2017
Regarding the workaround, if layer is nil then CGRectEqualToRect compares garbage. The only result is a no-op on a nil anyways, unless the garbage contains a NaN value (highly improbable) which could result in an arithmethic exception...
Working around #1965 in UIImageSetLayerContents. (#1967)
Release 1702.1 (#1992)
alakoring commentedon Feb 20, 2017
In my code, I rely on sending messages to nil to work as defined (doing nothing and returning 0). If WinObjC does not behave the same, it would be a bug.
edvv commentedon Feb 20, 2017
David Dunham (@alakoring) I think the title to this thread is poorly worded and the actual issue is how are the (function return) registers initialized when the receiver is nil. For a word length it is zero'd out but for anything greater than a word length (such as double or a struct where sizeof is greater than word length) the registers are not necessarily zero'd past a word length (32 bits in this case, or 64 bits on a 64bit implementation). This is the same way it works on iOS and Mac and its always been that way ever since Objective-C was invented. It is pretty standard that a pointer byte length (an id type) is also the same as a word length.
alakoring commentedon Feb 20, 2017
Makes sense, thanks
DHowett-MSFT commentedon Feb 21, 2017
Ed VanVliet (@edvv) You're totally right. Thanks for the detailed write up.
In light of that, I'm duping this to #1365. There's more info about the compiler side of the bug and some attempted workarounds over there.
edvv commentedon Feb 22, 2017
Dustin L. Howett (MSFT) (@DHowett-MSFT) : Jared (@jaredhms) wrote: "or make a thorough pass through the codebase to make sure we're not relying on this behavior for complex types". ... that is a good idea and is the main takeaway that I got from this thread. (in fact, the entire codebase can use a good cleaning and methodical going over).
jaredhms commentedon Feb 22, 2017
Thanks, Ed VanVliet (@edvv). We've done a substantial amount of general 'code scrubbing' (and there's certainly more room for improvement), but I want to ensure that we address this specific problem in our codebase - especially if it's going to take a while to address it in the compiler.
Dustin L. Howett (MSFT) (@DHowett-MSFT), were you planning on pivoting #1365 to perform a thorough code inspection while we wait for a compiler fix?
edvv commentedon Feb 22, 2017
This is still a bit confusing. It is my understanding that the compiler works correctly for x86 targets. That is it zeros out 32 bit in the return register when the receiver is nil. That is the way it is suppose to work. Thus you need to scrub your code anyways for returns > 32 bits.
The ARM target compile may be something different (?). From what I am reading the arm target assembly does not zero out 32 bit in the return register when the receiver is nil? - that definitely needs to be fixed in the compiler and not in a WinObjC scrub if that is the case.
Also, if the return is typed as a float (as opposed to double) then it shouldn't matter anyways because float is 32 bits.
Am I understanding this issue right?
I also am getting the impression that you want to extend the ObjC compiler to handle complex returns. Why is that? If the receiver type is "id" (an uncast type, i.e.: void *) then the return type is unknown and there is no way to know how much of the register to zero out; so it defaults to 32 bits.
From my historical understanding, when the receiver is nil then 32 bits (or 64 on 64 bit implementations) is zero'd in the return register just because that is the implicit function [method] prototype return value for ObjC. -- i.e.: Nothing fancy in attempting to zero out the entire return register, etc.
DHowett-MSFT commentedon Feb 23, 2017
Ed VanVliet (@edvv) With our compiler/runtime, 32-bit return values that go through the general purpose registers are properly zeroed when the receiver is
nil. This is totally expected and per-specification.The x86 implementation of
objc_msgSendexplicitly clears the x87 floating point register as well, for theobjc_msgSend_fpretcase. This may not get hit, and that's explained below.On ARM, we may have an issue (not yet determined) where the VFP register set used for returns (S0-S7 or D0-D3) is not cleared in
objc_msgSendor anobjc_msgSend_fpretequivalent. This doesn't exceptionally matter, agaim because of the below.The kicker: the Objective-C code generator we're using (CGObjCGNU with its objfw personality) explicitly skips the
objc_msgSendif the receiver isnil; this means the call site gains the responsibility to clear the VFP registers on ARM or the x87 fp stack on x86.Since 32-bit non-floating-point return values are generally returned in the same register used for
self, this is fine:eaxandr0both containnilafter we skip the call, and the world remains sane.Now, code was added to CGObjCGNU that attempts to clear even complex floating point and struct returns. I can only imagine that this is because OS X 10.5 and beyond, as well as iOS 5 (or 6?), unofficially specify that struct returns from
nilreceivers are zero-valued!In CGObjCMac, Clang emits a local default-constructed value with static storage duration (memset to 0, which fits in with other static initialization). If the receiver is
nil, a memcpy is emitted from this local static value to the final return value. Therefore, it's safe to initialize even a complex struct or floating point value with the return value from a message send to a nil receiver.It looks like CGObjCGNU tried to mimic this: there's a comment stating that it tries to do so, but we have not been able to successfully reproduce that behaviour. It seems broken, but not intentionally so.
Since the return type is known to the compiler, and there's precedent in both the mac and GNU code generators, this is a safe (albeit fairly new compared to the rest of Objective-C) convenience to provide.
Finally, this is what I see on my Mac:
Code
And this reproduces on both 32-bit and 64-bit :)
edvv commentedon Feb 24, 2017
Dustin L. Howett (MSFT) (@DHowett-MSFT) Thank you for spending the time explaining it. Much appreciated.
Protecting UIButton's CGSize calculations against nil receivers; more…
Protecting UIButton's CGSize calculations against nil receivers; more…