On Nov 15, 2011, at 10:54 AM, Torsten Curdt wrote:

> But then again the compiler would know about these implementations.

No, it wouldn’t. The compiler has no idea how NSDictionary or NSWindow are 
implemented. All it knows about them is what’s given in their header files. 
(Worse, even if it did grope into the framework’s binary code to decompile the 
implementation, that implementation is guaranteed to change in the next OS 
release, and that might include the size of the instance data.)

The fragile base class problem in a nutshell:

- I’m generating 32-bit Mac OS machine code to read instance variable 
“self->_foo” in a method of class Bar.
- I have an imaginary internal struct that defines the data layout of Bar. It 
looks like:
        struct BarInstanceData {
                Class isa;
                BarParentInstanceData inheritedData;
                int _foo;
        };
- The compiler can now interpret “self->_foo” as a regular C struct access and 
emits an instruction that loads an int from a hardcoded offset from the 
register holding ‘self’. Let’s say the offset is 48.
- In the next release of the OS, one of the base classes of Bar has added some 
instance variables, adding 8 bytes to its instance size.
- This means that at runtime the true offset of self->_foo is now 48+8 = 56.
- Unfortunately the old offset 48 is baked into the machine code of the 
app/library containing class Bar.
- This means that the implementation of Bar will read and write the wrong 
locations. Kaboom.

Note that if the compiler can’t work out the instance size of all the base 
classes of Bar, it can’t work out the size of that BarParentInstanceData struct 
in step 2, meaning it can’t compile Bar.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to