Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
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

Closed

Description

@jaredhms

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:

CGRect stretch = [image _imageStretch];
if (!CGRectEqualToRect([layer contentsCenter], stretch)) {
     [layer setContentsCenter:stretch];
}

0:007:x86> dd &stretch
034ff37c  034ff398 09b41e9c 0ae98df8 c68a9ac4

Activity

  1. added a commit that references this issue on Feb 14, 2017
  2. edvv commented on Feb 14, 2017

    @edvv

    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...

  3. added a commit that references this issue on Feb 14, 2017
  4. alakoring commented on Feb 20, 2017

    @alakoring

    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.

  5. edvv commented on Feb 20, 2017

    @edvv

    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.

  6. alakoring commented on Feb 20, 2017

    @alakoring

    Makes sense, thanks

  7. DHowett-MSFT commented on Feb 21, 2017

    @DHowett-MSFT

    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.

  8. edvv commented on Feb 22, 2017

    @edvv

    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).

  9. jaredhms commented on Feb 22, 2017

    @jaredhms
    ContributorAuthor

    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?

  10. edvv commented on Feb 22, 2017

    @edvv

    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.

  11. DHowett-MSFT commented on Feb 23, 2017

    @DHowett-MSFT

    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.

    Finally, this is what I see on my Mac:

    Code

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    struct LargeStruct {
            void* pointers[100];
            float floats[2];
            double doubles[4];
    };
    
    __attribute__((objc_root_class))
    @interface AnObjectForFun
    @end
    
    @implementation AnObjectForFun
    + (void)load {
    }
    
    + (void)initialize {
    }
    
    + (instancetype)val {
            return 0x00;
    }
    
    - (struct LargeStruct)get {
            struct LargeStruct mine;
            memset(&mine, 0xcc, sizeof(mine));
            return mine;
    }
    @end
    
    int main(int argc, char** argv, char** envp) {
            struct LargeStruct whatever;
            memset(&whatever, 0xdd, sizeof(whatever));
            whatever = [[AnObjectForFun val] get];
            fprintf(stderr, "done\n");
    }
        frame #0: 0x00001eca zr32`main(argc=1, argv=0xbffffa7c, envp=0xbffffa84) + 330 at zero_return.m:36
       33           struct LargeStruct whatever;
       34           memset(&whatever, 0xdd, sizeof(whatever));
       35           whatever = [[AnObjectForFun val] get];
    -> 36           fprintf(stderr, "done\n");
       37   }
    (lldb) p whatever
    (LargeStruct) $0 = {
      pointers = {
        [0] = 0x00000000
    ...
        [99] = 0x00000000
      }
      floats = ([0] = 0, [1] = 0)
      doubles = ([0] = 0, [1] = 0, [2] = 0, [3] = 0)
    }
    

    And this reproduces on both 32-bit and 64-bit :)

  12. edvv commented on Feb 24, 2017

    @edvv

    Dustin L. Howett (MSFT) (@DHowett-MSFT) Thank you for spending the time explaining it. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions