<?xml version="1.0"?>
<!-- RSS generated by my own two hands and bloggerizer.pl -->
<rss version="2.0">
  <channel>
    <title>Hamster Emporium</title>
    <link>http://www.sealiesoftware.com/blog/</link>
    <description>Greg Parker's web log</description>
    <item>
      <title>[objc explain]: objc_msgSend_vtable</title>
      <link>http://www.sealiesoftware.com/blog/archive/2011/06/17/objc_explain_objc_msgSend_vtable.html</link>
      <pubDate>17 Jun 2011 16:42 PDT</pubDate>
      <description>    &lt;p&gt;
      &lt;code&gt;objc_msgSend_vtable&lt;/code&gt; is a version
      of &lt;code&gt;objc_msgSend&lt;/code&gt; used to optimize a few of the most
      commonly called methods. 
    &lt;p&gt;
      Most Objective-C methods are dispatched using a hash table lookup
      inside &lt;code&gt;objc_msgSend&lt;/code&gt;. On x86_64, a few selectors can be
      dispatched using a C++-style virtual table: an array lookup, not a
      hash table. 
    &lt;p&gt;
      The compiler knows which selectors are optimized by the runtime. It
      compiles the call site differently,
      calling &lt;code&gt;objc_msgSend_fixup&lt;/code&gt; via a function pointer. At
      runtime, &lt;code&gt;objc_msgSend_fixup&lt;/code&gt; replaces the function
      pointer with one of the &lt;code&gt;objc_msgSend_vtable&lt;/code&gt; functions,
      if the called selector is one of the optimized selectors. 
    &lt;p&gt;
      C++ vtables are notoriously fragile: the array offsets for each
      virtual method are hardcoded into the generated code. Objective-C's
      vtables are not fragile. Each vtable is built at runtime and updated
      when method lists change. In theory even the set of optimized
      methods could be changed. The non-fragile flexibility costs an extra
      memory load during dispatch. 
    &lt;p&gt;
      Dispatch via vtable is faster than a hash table, but would consume
      tremendous amounts of memory if used everywhere. Objective-C's
      vtable implementation limits its use to a few selectors that are (1)
      implemented everywhere, but (2) rarely overridden. That means most
      classes share their superclass's vtable, which keeps memory costs
      low. 
    &lt;p&gt;
      A crash in any &lt;code&gt;objc_msgSend_vtable&lt;/code&gt; function should be
      debugged exactly like a crash in &lt;code&gt;objc_msgSend&lt;/code&gt;
      itself. They both crash for all of the same reasons, like incorrect
      memory management or memory smashers. 
    &lt;p&gt;
      Currently, the runtime uses sixteen
      different &lt;code&gt;objc_msgSend_vtable&lt;/code&gt; functions, one for each
      slot in the sixteen-entry vtable. 
    &lt;/p&gt;
    &lt;table border=1 cellspacing=0 cellpadding=4&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable0&lt;/code&gt;&lt;td&gt;&lt;code&gt;allocWithZone:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable1&lt;/code&gt;&lt;td&gt;&lt;code&gt;alloc&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable2&lt;/code&gt;&lt;td&gt;&lt;code&gt;class&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable3&lt;/code&gt;&lt;td&gt;&lt;code&gt;self&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable4&lt;/code&gt;&lt;td&gt;&lt;code&gt;isKindOfClass:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable5&lt;/code&gt;&lt;td&gt;&lt;code&gt;respondsToSelector:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable6&lt;/code&gt;&lt;td&gt;&lt;code&gt;isFlipped&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable7&lt;/code&gt;&lt;td&gt;&lt;code&gt;length&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable8&lt;/code&gt;&lt;td&gt;&lt;code&gt;objectForKey:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable9&lt;/code&gt;&lt;td&gt;&lt;code&gt;count&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable10&lt;/code&gt;&lt;td&gt;&lt;code&gt;objectAtIndex:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable11&lt;/code&gt;&lt;td&gt;&lt;code&gt;isEqualToString:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable12&lt;/code&gt;&lt;td&gt;&lt;code&gt;isEqual:&lt;/code&gt;
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable13&lt;/code&gt;&lt;td&gt;&lt;code&gt;retain&lt;/code&gt; (non-GC)&lt;br&gt;&lt;code&gt;hash&lt;/code&gt; (GC)
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable14&lt;/code&gt;&lt;td&gt;&lt;code&gt;release&lt;/code&gt; (non-GC)&lt;br&gt;&lt;code&gt;addObject:&lt;/code&gt; (GC)
      &lt;tr&gt;&lt;td&gt;&lt;code&gt;objc_msgSend_vtable15&lt;/code&gt;&lt;td&gt;&lt;code&gt;autorelease&lt;/code&gt; (non-GC)&lt;br&gt;&lt;code&gt;countByEnumeratingWithState:objects:count:&lt;/code&gt; (GC)
    &lt;/table&gt;
    &lt;p&gt;
       The vtable's contents differ for GC and non-GC, for obvious
       reasons. &lt;code&gt;-isFlipped&lt;/code&gt; is part of
       NSView. &lt;code&gt;-countByEnumeratingWithState:objects:count:&lt;/code&gt; is
       the fast enumeration implementation, including &lt;code&gt;for (x in
       y)&lt;/code&gt;. Together these methods make up roughly 30-50% of calls
       in typical Objective-C applications. 
</description>
    </item>
    <item>
      <title>Dr. Gregory Parker, Department of Diagnostic Engineering</title>
      <link>http://www.sealiesoftware.com/blog/archive/2010/09/01/Dr_Gregory_Parker_Department_of_Diagnostic_Engineering.html</link>
      <pubDate>01 Sep 2010 3:15 PDT</pubDate>
      <description>    &lt;p&gt;
      Last week, &lt;a href="http://twitter.com/rballard"&gt;Rick Ballard&lt;/a&gt; came by my office for a consult. He had caught Xcode at a crash in &lt;code&gt;objc_msgSend()&lt;/code&gt;. The crash looked like an intermittent problem that had been plaguing Xcode for months. So he called the local expert on debugging &lt;code&gt;objc_msgSend()&lt;/code&gt;. Dr. Gregory Parker, Department of Diagnostic Engineering.
    &lt;p&gt;
      The good news was that Rick's crash was reliably reproducible. Running tests on a live patient is better than performing an autopsy on a dead one. The bad news was that the obvious debugging tools had not helped. &lt;code&gt;NSZombieEnabled&lt;/code&gt; and &lt;code&gt;guardmalloc&lt;/code&gt; had turned up nothing, and &lt;code&gt;AUTO_USE_GUARDS=YES&lt;/code&gt; (the GC equivalent of &lt;code&gt;guardmalloc&lt;/code&gt;) just thrashed the machine for two hours before running out of address space.
    &lt;p&gt;
      &lt;a href="http://sealiesoftware.com/blog/archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html"&gt;So you crashed in &lt;code&gt;objc_msgSend()&lt;/code&gt;&lt;/a&gt;. The selector was &lt;code&gt;-isAbsolutePath&lt;/code&gt;, which was reasonable but meant the debugger's backtrace was missing a frame. &lt;code&gt;objc_msgSend()&lt;/code&gt; had read the class from the object, read the method cache from the class, read a method from the method cache, and crashed while trying to read the &lt;code&gt;IMP&lt;/code&gt; from the method. Theory: either one of those data structures had been hit by a memory smasher, or the original object was bogus but happened to have dereferenceable pointers in the right places to survive that long. The method cache's mask was invalid - it should have been of the form 2&lt;sup&gt;n&lt;/sup&gt;-1 - so the failure must have been at or before that point in the chain.
    &lt;p&gt;
      The object pointer itself looked plausible. Theory: the object was valid, but a previous object at the same location had been used after being freed. We had the great luxury of a reproducible crash, so we turned on &lt;code&gt;MallocStackLoggingNoCompact&lt;/code&gt; and ran it again. That memory had only been used for one object, and it had not been deallocated. So the evidence did not support the use-after-free theory. But the history showed that the object had been allocated as an &lt;code&gt;NSPathStore2&lt;/code&gt; - an internal subclass of &lt;code&gt;NSString&lt;/code&gt; for file pathnames - which matched the selector &lt;code&gt;-isAbsolutePath&lt;/code&gt; and matched the call site's expectations. The theory that the object pointer was valid looked good.
    &lt;p&gt;
      The object pointer was good, and the method cache was not: the failure was on the chain between them. The contents of the object looked good. The bytes looked like alternating zero and ASCII, which is a dead giveaway for the UTF-16 used inside &lt;code&gt;NSString&lt;/code&gt;. The string value decoded as &lt;code&gt;@"/Xcode4/usr/bin/llvm-gcc"&lt;/code&gt;, which made sense in the call site's context.
    &lt;p&gt;
      The object's &lt;code&gt;isa&lt;/code&gt; pointer was not so good. Its value was &lt;code&gt;0xa0050000&lt;/code&gt;. This was not class &lt;code&gt;NSPathStore2&lt;/code&gt; or any other class. &lt;code&gt;vmmap&lt;/code&gt; showed it to be in Foundation's data segment, and &lt;code&gt;otool&lt;/code&gt; showed it was specifically in Foundation's constant CF strings. But instead of pointing to the start of some string, it pointed to the middle of a string object. That string object was &lt;code&gt;@"tzm-Latn"&lt;/code&gt;: some localization thingy, perhaps? Theory: some bug had replaced this object's isa pointer with a pointer to the middle of an unrelated localization string object. This did not sound like a good theory.
    &lt;p&gt;
      Go back to the board. Symptom: the object was allocated as an &lt;code&gt;NSPathStore2&lt;/code&gt;. Symptom: the object's &lt;code&gt;isa&lt;/code&gt; pointer is now &lt;code&gt;0xa0050000&lt;/code&gt;, which is not &lt;code&gt;NSPathStore2&lt;/code&gt;. What should the &lt;code&gt;isa&lt;/code&gt; pointer's value have been? &lt;code&gt;otool&lt;/code&gt; and &lt;code&gt;objc_getClass()&lt;/code&gt; agreed: the correct &lt;code&gt;isa&lt;/code&gt; pointer should have been &lt;code&gt;0xa005f198&lt;/code&gt;. &lt;code&gt;0xa0050000&lt;/code&gt; is suspiciously similar. Theory: something had cleared two bytes of this object, leaving a nonsense &lt;code&gt;isa&lt;/code&gt; pointer. &lt;code&gt;@"tzm-Latn"&lt;/code&gt; was a red herring.
    &lt;p&gt;
      Aha! This is 32-bit i386. Little endian. The pointer &lt;code&gt;0xa005f198&lt;/code&gt; is stored backwards in memory: &lt;code&gt;0x98 0xf1 0x05 0xa0&lt;/code&gt;. Clearing the least-significant bytes of the &lt;code&gt;isa&lt;/code&gt; pointer meant clearing bytes 0 and 1 of the object, not bytes 2 and 3. Damage to bytes 0 and 1 is exactly what you'd expect from a two-byte overrun of the object preceding this one in memory. Theory: the bug was in that preceding object, and this &lt;code&gt;NSPathStore2&lt;/code&gt; object was an innocent victim.
    &lt;p&gt;
      &lt;code&gt;malloc_history&lt;/code&gt; works with a pointer to the middle of an allocation, too. We plugged in &lt;code&gt;object-1&lt;/code&gt; and got back an instance of &lt;code&gt;DVTSourceModelItem&lt;/code&gt;, not deallocated. Rick recognized this as part of Xcode's indexer, which was always running in another thread at the time of the crash. A buffer overrun from the &lt;code&gt;DVTSourceModelItem&lt;/code&gt; object fit the symptoms.
    &lt;p&gt;
      But where was the buffer? I had expected an overrun in some heap-allocated C array, not an ordinary object. Nor did &lt;code&gt;DVTSourceModelItem&lt;/code&gt; have any C arrays in its instance variables. 
    &lt;p&gt;
      Theory: the compiler or runtime had allocated too little memory for the instance of class &lt;code&gt;DVTSourceModelItem&lt;/code&gt;, and ordinary ivar access had overrun that allocation. It was a long shot, but easy to test. &lt;code&gt;malloc_size()&lt;/code&gt; and &lt;code&gt;class_getInstanceSize()&lt;/code&gt; and an eyeball count of ivars all agreed that the object was 32 bytes. Theory disproved.
    &lt;p&gt;
      We tested the overrun theory again. Add an unused ivar to the end of &lt;code&gt;DVTSourceModelItem&lt;/code&gt;, recompile, and run it. No crash. Remove the ivar. Crash. The extra ivar "fixed" the bug. The buffer overrun theory still fit the evidence, but we couldn't find it. 
    &lt;p&gt;
      No more ideas. We needed data. Debugger watchpoints were out: there were thousands of instances of &lt;code&gt;DVTSourceModelItem&lt;/code&gt;, and we couldn't watch two bytes after each of them. We were not yet desperate enough to try brute force code inspection. &lt;code&gt;AUTO_USE_GUARDS=YES&lt;/code&gt; could catch it, if it didn't fall over first. Since we had a suspect in mind, we could play the &lt;code&gt;guardmalloc&lt;/code&gt; trick ourselves with a narrower target. Override &lt;code&gt;+[DVTSourceModelItem alloc]&lt;/code&gt;, &lt;code&gt;mprotect()&lt;/code&gt; the page after the allocation, and cross our fingers really hard hoping that it still reproduced after changing the timing so much.
    &lt;p&gt;
      Bang! It crashed (good) somewhere new (also good). &lt;code&gt;DVTSourceModelItem&lt;/code&gt; &lt;code&gt;-init&lt;/code&gt; was writing to one of its own instance variables. The ivar was a bit in a bitfield, and that bitfield was at the end of the ivar list. 
    &lt;p&gt;
      Disassemble. The generated code read 4 bytes around the bit into a register, change the bit in that register, and wrote the 4 bytes back to memory. That's typical for a bitfield. The unexpected part was that the 4 bytes spanned the last two bytes of the object and the first two bytes after the object. That's a bug. Most of the time the out of bounds access is invalid - it reads two bytes it shouldn't, and writes back the same value. But if there's another thread it can crash:
      &lt;table border=1 cellspacing=0 cellpadding=4&gt;
	&lt;tr&gt;&lt;td align=center&gt;Thread 1&lt;/td&gt;&lt;td align=center&gt;Thread 2&lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;reads four bytes, including two&lt;br&gt;bytes outside the object&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;allocates a new object&lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;writes an isa pointer&lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;writes four bytes, clobbering the&lt;br&gt;new value written by Thread 2&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;crashes&lt;/td&gt;&lt;/tr&gt;
      &lt;/table&gt;
    &lt;p&gt;
      Theory: a compiler bug generated bad code for &lt;code&gt;DVTSourceModelItem&lt;/code&gt;'s bitfield ivar, causing a read-modify-write out of bounds by two bytes, which corrupted memory in other threads. Test: try a different compiler. &lt;code&gt;DVTSourceModelItem.m&lt;/code&gt; was built with &lt;code&gt;clang&lt;/code&gt;, so we recompiled with &lt;code&gt;llvm-gcc&lt;/code&gt;. No crash, and the disassembly looked correct. Compile with &lt;code&gt;clang&lt;/code&gt; again, crash again.
    &lt;p&gt;
      Diagnosis: &lt;code&gt;clang&lt;/code&gt; compiler bug in bitfield ivars. The patient's symptoms were treated with an extra ivar in &lt;code&gt;DVTSourceModelItem&lt;/code&gt; until a compiler transplant could be performed. 
    &lt;p&gt;
      Elapsed time: about three hours. Too long for an episode of a TV procedural drama, unfortunately. 
    &lt;/p&gt;
</description>
    </item>
    <item>
      <title>TargetConditionals.h</title>
      <link>http://www.sealiesoftware.com/blog/archive/2010/8/16/TargetConditionalsh.html</link>
      <pubDate>16 Aug 2010 14:30 PDT</pubDate>
      <description>    &lt;br&gt;
    &lt;table border=1 cellspacing=0 cellpadding=4&gt;
      &lt;th&gt;&lt;td align=center&gt;Mac OS X&lt;/td&gt;&lt;td align=center&gt;iOS device&lt;/td&gt;&lt;td align=center&gt;iOS simulator&lt;/td&gt;&lt;/th&gt;
      &lt;tr&gt;&lt;td&gt;TARGET_OS_MAC&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;TARGET_OS_IPHONE&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;TARGET_OS_EMBEDDED&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;TARGET_IPHONE_SIMULATOR&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td bgcolor=#eeffee&gt;1&lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
    &lt;br&gt;
</description>
    </item>
    <item>
      <title>Do-it-yourself Objective-C weak import</title>
      <link>http://www.sealiesoftware.com/blog/archive/2010/4/8/Do-it-yourself_Objective-C_weak_import.html</link>
      <pubDate>8 Apr 2010 22:23 PDT</pubDate>
      <description>      &lt;h4&gt;WARNING  DANGER  HAZARD  BEWARE  EEK&lt;/h4&gt;
      &lt;p&gt;
	The scheme described herein is &lt;b&gt;UNTESTED&lt;/b&gt; and probably
	&lt;b&gt;BUGGY&lt;/b&gt;. Use at your own risk.  
      &lt;/p&gt;
      &lt;h4&gt;Executive summary&lt;/h4&gt;
      &lt;p&gt;
	The Objective-C runtime supports &lt;a href="http://sealiesoftware.com/blog/archive/2009/09/09/objc_explain_Weak-import_classes.html"&gt;weak-imported classes&lt;/a&gt; back to
	iPhone OS 3.1. An app could use a class added in iPhone OS 3.2 or
	4.0 and still run on 3.1. The app would check if &lt;code&gt;[SomeClass
	class]&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt; and act accordingly.
      &lt;/p&gt;
      &lt;p&gt;
	Unfortunately, the compilers and class declarations in framework
	headers do not support weak import yet. But you may be able to use
	weak linking anyway, by adding the right incantations yourself.
      &lt;/p&gt;
      &lt;p&gt;
	To &lt;b&gt;use&lt;/b&gt; a class &lt;code&gt;SomeClass&lt;/code&gt; that is unavailable
	on some of your app's deployment targets, write this in
	&lt;b&gt;every&lt;/b&gt; file that uses the class:&lt;pre&gt;
    asm(".weak_reference _OBJC_CLASS_$_SomeClass");&lt;/pre&gt;
	To &lt;b&gt;subclass&lt;/b&gt; a class &lt;code&gt;SomeClass&lt;/code&gt; that is unavailable 
	on some of your app's deployment targets, write this in 
	the file containing your subclass's
	&lt;code&gt;@implementation&lt;/code&gt;:&lt;pre&gt;
    asm(".weak_reference _OBJC_CLASS_$_SomeClass");
    asm(".weak_reference _OBJC_METACLASS_$_SomeClass");&lt;/pre&gt;
	This will not work for apps running on iPhone OS 3.0 or
	older. Only iPhone OS 3.1 and newer has any hope of success. Of
	course, since this is &lt;b&gt;UNTESTED&lt;/b&gt; it may not work there either.
      &lt;/p&gt;
      &lt;h4&gt;How it works&lt;/h4&gt;
      &lt;p&gt;
	Say you're writing a game, and want to use the hypothetical
	&lt;code&gt;UIDancePad&lt;/code&gt; class added to iPhone OS 3.2. (Do not
	dance on iPad.) When you use class &lt;code&gt;UIDancePad&lt;/code&gt; in your
	code, the compiler emits a C symbol pointing to the class:&lt;pre&gt;
    .long _OBJC_CLASS_$_UIDancePad&lt;/pre&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Since &lt;code&gt;UIDancePad&lt;/code&gt; is in a framework instead of your
	code, the symbol remains undefined in your executable, as shown by
	&lt;code&gt;`nm -m`&lt;/code&gt;:&lt;pre&gt;
    (undefined) external _OBJC_CLASS_$_UIDancePad (from DanceKit)&lt;/pre&gt;
      &lt;/p&gt;
      &lt;p&gt;
	When you run on iPhone OS 3.2, everything works great: the dynamic
	loader opens your executable and DanceKit, and binds your
	undefined symbol to their class definition.
      &lt;/p&gt;
      &lt;p&gt;
	Things don't go so well on iPhone OS 3.1. DanceKit exists but does
	not define &lt;code&gt;UIDancePad&lt;/code&gt;. The dynamic loader is unable
	to resolve 
	your undefined symbol, and the process halts:&lt;pre&gt;
    dyld: Symbol not found: _OBJC_CLASS_$_UIDancePad
        Referenced from: /path/to/YourApp
        Expected in: /path/to/DanceKit&lt;/pre&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Weak import solves this. The compiled symbol reference is now a
	weak one:&lt;pre&gt;
    .weak_reference _OBJC_CLASS_$_UIDancePad
    .long _OBJC_CLASS_$_UIDancePad

    (undefined) weak external _OBJC_CLASS_$_UIDancePad (from DanceKit)&lt;/pre&gt;
      &lt;/p&gt;
      &lt;p&gt;
	The dynamic loader shrugs its shoulders if a weak reference cannot
	be resolved, and sets the pointer to &lt;code&gt;NULL&lt;/code&gt;. The
	Objective-C runtime sees the &lt;code&gt;NULL&lt;/code&gt; pointer and fixes
	up the rest of the metadata as if &lt;code&gt;UIDancePad&lt;/code&gt; never
	existed. 
      &lt;/p&gt;
      &lt;p&gt;
	As mentioned above, the compiler and framework header support is
	not yet in place. The incantations simply add the
	assembler directives that the compiler does not yet know how to
	emit:&lt;pre&gt;
    asm(".weak_reference _OBJC_CLASS_$_UIDancePad");&lt;/pre&gt;
      &lt;/p&gt;
      &lt;p&gt;
	Et voil&amp;agrave;: weak import of an Objective-C class. Well, maybe. I have
	only tested this on toy examples, none of which got anywhere close
	to any version of iPhone OS. Coder beware!
      &lt;/p&gt;
      &lt;p&gt;
	(What about the &lt;code&gt;_OBJC_METACLASS&lt;/code&gt; symbol, you ask? When
	you subclass a class, your subclass's
	metaclass's superclass pointer points to the subclass's superclass's
	metaclass. In other words, your subclass's
	&lt;code&gt;@implementation&lt;/code&gt; points to both its superclass and its
	superclass's &lt;a
	href="http://sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html"&gt;metaclass&lt;/a&gt;. That
	requires two symbols: one for the class and one for the
	metaclass. When you simply use a class without subclassing it, you
	don't need the metaclass pointer.)
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Weak-import classes</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/09/objc_explain_Weak-import_classes.html</link>
      <pubDate>09 Sep 2009 13:30 PDT</pubDate>
      <description>      &lt;p&gt;
	Weak-import classes are a useful new Objective-C feature that you
	can't use yet. 
      &lt;/p&gt;
      &lt;p&gt;
	Weak import is a solution when you want to use something from a
	framework, but still need to be compatible with older versions of
	the framework that didn't support it yet. Using weak import you
	can test if the feature exists at runtime before you try to use
	it. 
      &lt;/p&gt;
      &lt;p&gt;
	Objective-C has not previously supported weak import for
	classes. Instead you had to use clumsy runtime introspection to
	check whether a class was available, store a pointer to that class
	in a variable, and use that variable when you wanted to send a
	message to the class. Even worse, there was no reasonable way to
	create your own subclass of a superclass that might be
	unavailable. Some developers put the subclass in a separate
	library that was not loaded until after checking that the
	superclass was present, but even that trick is not allowed on
	iPhone OS. 
      &lt;/p&gt;
      &lt;p&gt;
	Weak import for C functions works by checking the weak-imported
	function pointer's value before calling it:
      &lt;/p&gt;
      &lt;pre&gt;    if (NSNewFunction != NULL) {
        NSNewFunction(...);
    } else {
        // NSNewFunction not supported on this system
    }&lt;/pre&gt;
      &lt;p&gt;
	The same mechanism is a natural fit with Objective-C classes and 
	Objective-C's handling of messages to nil. These constructs are
	much nicer than &lt;code&gt;NSClassFromString()&lt;/code&gt; or a separate 
	&lt;code&gt;NSBundle&lt;/code&gt;. 
      &lt;/p&gt;
      &lt;pre&gt;    if ([NSNewClass class] != nil) {
        [NSNewClass doSomething];
    } else {
        // NSNewClass is unavailable on this system
    }&lt;/pre&gt;
      &lt;pre&gt;    @interface MySubclass : NSNewClass ... @end
    MySubclass *obj = [[MySubclass alloc] init];
    if (!obj) {
        // MySubclass (or a superclass thereof) is unavailable on this system
    }&lt;/pre&gt;
      &lt;p&gt;
	Weak import of Objective-C classes is now available. But you can't
	use it yet. First, it's only supported today on iPhone OS 3.1; 
	it's expected to arrive in a future Mac OS. 
      &lt;/p&gt;
      &lt;p&gt;
	Second, there's nothing you can do with weak import until the
	first OS update &lt;i&gt;after&lt;/i&gt; iPhone OS 3.1. Then you could write an app
	that adopted new features in that future version, and used weak import
	to be compatible with 3.1. (It still could not run on 3.0 or 2.x,
	because those systems lack the runtime machinery to process the
	weak import references.)
      &lt;/p&gt;
      &lt;p&gt;
	Weak import for Objective-C did not make Snow Leopard for
	scheduling reasons. Assuming it ships in Mac OS X 10.7 Cat Name
	Forthcoming, you won't be able to use it until Mac OS X 10.8
	LOLcat. 
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>Colorized keyboard backlight</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/05/Colorized_keyboard_backlight.html</link>
      <pubDate>05 Sep 2009 1:15 PDT</pubDate>
      <description>    &lt;p&gt;
      I use my MacBook Pro for astronomy. The backlit keyboard would be
      great in the dark, but its white light is bad for night
      vision. &lt;a href="/keyboard/index.html"&gt;This mod&lt;/a&gt; makes it red, or any other color you want.
    &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Selector uniquing in the dyld shared cache</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/09/01/objc_explain_Selector_uniquing_in_the_dyld_shared_cache.html</link>
      <pubDate>01 Sep 2009 2:10 PDT</pubDate>
      <description>      &lt;p&gt;
	Mac OS X Snow Leopard cuts in half the launch-time overhead of
	starting the Objective-C runtime, and simultaneously saves a few
	hundred KB of memory per app. This comes for free to every app,
	courtesy of one of the few pieces of Mac OS X that lives below
	even the Objective-C runtime: dyld.
      &lt;/p&gt;
&lt;h4&gt;dyld and the shared cache&lt;/h4&gt;
      &lt;p&gt;
	dyld is the dynamic loader and linker. When your process starts,
	dyld loads your executable and its shared libraries into memory,
	links the cross-library C function and variable references
	together, and starts execution on its way towards
	&lt;code&gt;main()&lt;/code&gt;. 
      &lt;/p&gt;
      &lt;p&gt;
	In theory a shared library could be different every time your
	program is run. In practice, you get the same version of
	the shared libraries almost every time you run, and so does every
	other process on the system. The system takes advantage of this by
	building the dyld shared cache. The shared cache contains a copy
	of many system libraries, with most of dyld's linking and loading
	work done in advance. Every process can then share that shared
	cache, saving memory and launch time. 
      &lt;/p&gt;
      &lt;p&gt;
	(Incidentally, the shared cache beats the pants off the pre-Leopard
	prebinding system that was supposed to achieve the same
	optimizations. Remember the post-install "Optimizing System
	Performance" step that often took longer than the install itself?
	That was prebinding being updated. Rebuilding the shared cache is 
	so blazingly fast that the installer doesn't bother to report it
	anymore.) 
      &lt;/p&gt;
      &lt;h4&gt;Objective-C selector uniquing&lt;/h4&gt;
      &lt;p&gt;
	Leopard's dyld shared cache is great for C code, but it didn't do
	anything to help Objective-C's startup overhead. The single
	biggest launch cost for Objective-C is selector uniquing. The app
	and every shared library contain their own copies of selector
	names like "alloc" and "init". The runtime needs to choose a
	single canonical SEL pointer value for each selector name, and
	then update the metadata for every call site and method list to 
	use the blessed unique value. This means building a big hash table
	(memory), calling &lt;code&gt;strcmp()&lt;/code&gt; a lot (time), and
	modifying copy-on-write metadata (more memory). 
      &lt;/p&gt;
      &lt;p&gt;
	There are tens of thousands of unique selectors present in a
	typical process. If you run &lt;code&gt;`strings
	/usr/lib/libobjc.dylib`&lt;/code&gt; on Leopard you can see the
	thirty-thousand-line 
	&lt;a
	href="http://www.opensource.apple.com/source/objc4/objc4-371/runtime/objc-sel-table.h"&gt;built-in
	selector table&lt;/a&gt; that was a previous attempt to reduce the
	memory 
	cost. Even so the cost goes up with every new class and
	method added to Cocoa.framework; left unchecked, an identical app
	would take longer to launch and use more memory after every OS
	upgrade. 
      &lt;/p&gt;
      &lt;p&gt;
	The obvious solution? Do the work of selector uniquing in the dyld
	shared cache. Build a selector table into the shared cache itself,
	and update the selector references in the cached copy of the
	shared libraries. Then you save memory because every process
	shares the same selector table, and save time because the runtime
	does not need to rebuild it during every app launch. The runtime
	only needs to fix the selector references from the app itself. The
	catch? 
	Selectors are too dynamic to be implemented as C symbols, so the
	shared cache construction tool needed to be taught how to read and
	write Objective-C's metadata. 
      &lt;/p&gt;
      &lt;h4&gt;Optimization WIN&lt;/h4&gt;
      &lt;p&gt;
	Snow Leopard's dyld shared cache uniques Objective-C selectors,
	and Snow Leopard's Objective-C runtime recognizes when the
	selectors in a shared library are already uniqued courtesy of the
	shared cache. About half of the runtime's initialization time is
	eliminated, making warm app launch several tenths of a second
	faster. Typical memory savings is 200-500 KB per process, 
	adding up to a few megabytes system-wide. When this optimization
	ships on the iPhone OS side, it's estimated to save 1 MB on a 128
	MB device. The iPhone performance team would pay any number of
	arms and legs for that kind of gain. 
      &lt;/p&gt;
      &lt;p&gt;
	You can watch the system in action with various debugging flags.
      &lt;/p&gt;
      &lt;p&gt;
      &lt;code&gt;$ sudo /usr/bin/update_dyld_shared_cache -debug -verify&lt;br&gt;
[...]&lt;br&gt;
update_dyld_shared_cache: for x86_64, uniquing objc selectors&lt;br&gt;
update_dyld_shared_cache: for x86_64, found 68761 unique objc selectors&lt;br&gt;
update_dyld_shared_cache: for x86_64, 541736/590908 bytes (91%) used in	libobjc unique selector section&lt;br&gt;
update_dyld_shared_cache: for x86_64, updated 205230 selector references&lt;br&gt;
&lt;/code&gt;
      &lt;/p&gt;
      &lt;p&gt;
      &lt;code&gt;$ OBJC_PRINT_PREOPTIMIZATION=YES /usr/bin/defaults&lt;br&gt;
objc[424]: PREOPTIMIZATION: selector preoptimization ENABLED (version 3)&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /usr/lib/libobjc.A.dylib&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata&lt;br&gt;
objc[424]: PREOPTIMIZATION: honoring preoptimized selectors in /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation&lt;br&gt;
&lt;/code&gt;
&lt;/p&gt;
      &lt;p&gt;
	You can estimate the memory savings with the
	&lt;code&gt;&lt;a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/allmemory.1.html"&gt;allmemory&lt;/a&gt;&lt;/code&gt; tool. Record post-launch memory usage of an
	app run with and without environment variable
	&lt;code&gt;OBJC_DISABLE_PREOPTIMIZATION=YES&lt;/code&gt;. Look for the count
	of dirty pages; each dirty page is 4 KB eaten by that
	process. With 64-bit TextEdit I see the dirty page count jump from
	725 to 1069 after disabling the optimization. This is an
	overestimate - many of those pages would have been not-dirty in
	Leopard because of the old built-in selector table - but it does
	show the magnitude of the win.
      &lt;/p&gt;
      &lt;p&gt;
	The Objective-C runtime does more than just selector uniquing
	during launch. Future improvements to the dyld shared cache may
	precompute some of that other work, to further improve launch
	time, save memory, and reduce the cost of linking to Objective-C
	code that you don't actually use. But selector uniquing as seen in
	Snow Leopard is by far the biggest bang for the buck.
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: Thread-local garbage collection</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/08/28/objc_explain_Thread-local_garbage_collection.html</link>
      <pubDate>28 Aug 2009 13:00 PDT</pubDate>
      <description>      &lt;p&gt;
	Mac OS X Snow Leopard introduces thread-local collection, a big
	enhancement to the Objective-C garbage collector. Thread-local
	collection is a more efficient way to reclaim much of the
	garbage in most programs. It also scales better to more threads
	and more cores than the other algorithms used by the Objective-C
	GC.
      &lt;/p&gt;
      &lt;h4&gt;A brief history of garbage collection&lt;/h4&gt;
      &lt;p&gt;
	The simplest algorithm used by the Objective-C GC is &lt;b&gt;full
	collection&lt;/b&gt;. The GC scans all live objects in the entire heap, 
	discovers (approximately) all dead objects, and reclaims
	them. This is slow, especially if you have a large
	population of not-dead objects, but does find all possible
	garbage. Historically this is mostly 1960-era technology, except
	for the machinery that allows other threads to run mostly
	unhindered while the scan completes. 
      &lt;/p&gt;
      &lt;p&gt;
	The second algorithm used by the Objective-C GC is &lt;b&gt;generational
	collection&lt;/b&gt;. This takes advantage of the &lt;i&gt;generational
	hypothesis&lt;/i&gt;: most objects die young. The heap is divided into
	at least two generations: new objects and old objects. After
	allocating some amount of new objects, the collector runs a
	generational collection. First, it scans only the "new" objects
	and any "old" objects into which a pointer to a "new" object was
	stored. Then the now-dead "new" objects are reclaimed, and the
	surviving "new" objects are aged, moving them to the next 
	generation. The advantage of generational GC is that it collects
	lots of garbage (most objects die young) with much less work (it
	does not need to scan most of the "old" objects). Full collections
	are still needed to reclaim objects that survive infancy and die
	later, but run less often. Generational collection is 1980-era
	technology. 
      &lt;/p&gt; 
      &lt;h4&gt;Thread-local collection&lt;/h4&gt;
      &lt;p&gt;
	The third algorithm is the new &lt;b&gt;thread-local collection&lt;/b&gt;. TLC
	is similar to generational collection: it scans and reclaims a
	subset of objects, trying to get lots of bang for the collector's
	buck. The &lt;i&gt;thread-local&lt;/i&gt; hypothesis: most objects die without
	being reachable by any other thread. Newly-allocated objects are
	marked thread-local to the allocating thread. If a thread-local
	object becomes accessible to another thread (for example, a
	pointer to it is written into a global variable), then it has
	"escaped" and is moved
	out of the thread-local set. In a thread-local collection, a
	thread scans its own stack and its set of thread-local objects, and
	reclaims the dead objects.
      &lt;/p&gt;
      &lt;p&gt;
	The advantage of thread-local collection is that it requires no
	synchronization with other threads. Normally, a thread performing
	GC work needs to coordinate with the other threads. For example,
	the other threads change a pointer variable after the GC thread
	has looked at it, or start pointing to an object that the GC
	thread thinks is dead. Thread-local collection avoids these
	complications. The thread-local objects are
	by definition reachable only by one thread. The other threads
	have no way acquire a pointer to any of those objects, or change 
	pointer
	values inside them. A thread performing thread-local collection
	can work quickly on its own, without interference from other threads.
      &lt;/p&gt;
      &lt;p&gt;
	Having each thread "clean up after itself" reduces 
	bottlenecks in the collector that will only get worse as threads
	and cores increase. It's trivial to run thread-local
	collections on multiple threads simultaneously. And it's very fast
	because the only memory to scan is the thread's own stack and its 
	surviving thread-local objects. The pause time for a thread during
	generational or full GC is almost as big as the pause time for
	that thread to run a thread-local collection - but TLC can then
	immediately reclaim some garbage, 
	whereas the other algorithms need to do much more work and
	coordinate with all of the other threads 
	before they can actually reclaim anything.
      &lt;/p&gt;
      &lt;h4&gt;How you can help&lt;/h4&gt;
      &lt;p&gt;
	Thread-local collection works best when objects remain unreachable
	to other threads. In the Objective-C collector, this means
	avoiding CFRetain() of temporary objects when possible. A
	CFRetained pointer could go anywhere behind the collector's back,
	bypassing the write barrier that the collector uses to keep track
	of escaping objects. (This is one place that Snow Leopard leaves
	room for improvement: the system frameworks often allocate objects
	with a CF retain count of one and immediately release them, making
	them ineligible for thread-local collection.) Other ways for an
	object to escape thread-local collection include storing a pointer
	into a global variable; storing a pointer into some other object
	that itself is not thread-local; and making a weak reference or
	associated reference to the object.
      &lt;/p&gt;
      &lt;p&gt;
	If your thread has just created and discarded a lot of temporary
	objects, you can give the collector a hint that now might be a
	good time to run. &lt;code&gt;-[NSGarbageCollector
	collectIfNeeded]&lt;/code&gt; and &lt;code&gt;-[NSAutoreleasePool
	drain]&lt;/code&gt; are two such hints. These may run a thread-local
	collection first, and may follow up with generational or full
	collection. 
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>[objc explain]: So you crashed in objc_msgSend(): iPhone Edition</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/06/08/objc_explain_So_you_crashed_in_objc_msgSend_iPhone_Edition.html</link>
      <pubDate>08 Jun 2009 23:40 PDT</pubDate>
      <description>      &lt;p&gt;
	&lt;a
	href="http://sealiesoftware.com/blog/archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html"&gt;So
	you crashed in objc_msgSend()&lt;/a&gt; has been updated with register
	usage for iPhone's ARM processor. The table now looks like this:
	&lt;table border=1 cellspacing=0 cellpadding=4&gt;
	  &lt;th&gt;&lt;td align=center colspan=2&gt;&lt;code&gt;objc_msgSend&lt;/code&gt;&lt;br&gt;&lt;code&gt;objc_msgSend_fpret&lt;/code&gt;&lt;/td&gt;
	    &lt;td align=center colspan=2&gt;&lt;code&gt;objc_msgSend_stret&lt;/code&gt;&lt;/td&gt;&lt;/th&gt;
	  &lt;tr&gt;&lt;td align=center&gt;&amp;nbsp;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;receiver&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;SEL&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;receiver&lt;/code&gt;&lt;/td&gt;&lt;td align=center&gt;&lt;code&gt;SEL&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;i386&lt;/td&gt;&lt;td align=center&gt;eax*&lt;/td&gt;&lt;td align=center&gt;ecx&lt;/td&gt;
	    &lt;td align=center&gt;eax*&lt;/td&gt;&lt;td align=center&gt;ecx&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;x86_64&lt;/td&gt;&lt;td align=center&gt;rdi&lt;/td&gt;&lt;td align=center&gt;rsi&lt;/td&gt;
	    &lt;td align=center&gt;rsi&lt;/td&gt;&lt;td align=center&gt;rdx&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;ppc&lt;/td&gt;&lt;td align=center&gt;r3&lt;/td&gt;&lt;td align=center&gt;r4&lt;/td&gt;
	    &lt;td align=center&gt;r4&lt;/td&gt;&lt;td align=center&gt;r5&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;ppc64&lt;/td&gt;&lt;td align=center&gt;r3&lt;/td&gt;&lt;td align=center&gt;r4&lt;/td&gt;
	    &lt;td align=center&gt;r4&lt;/td&gt;&lt;td align=center&gt;r5&lt;/td&gt;&lt;/tr&gt;
	  &lt;tr&gt;&lt;td&gt;arm&lt;/td&gt;&lt;td align=center&gt;r0&lt;/td&gt;&lt;td align=center&gt;r1&lt;/td&gt;
	    &lt;td align=center&gt;r1&lt;/td&gt;&lt;td align=center&gt;r2&lt;/td&gt;&lt;/tr&gt;
	&lt;/table&gt;
      &lt;/p&gt;
</description>
    </item>
    <item>
      <title>Valgrind for Mac OS X goes mainline</title>
      <link>http://www.sealiesoftware.com/blog/archive/2009/06/03/Valgrind_for_Mac_OS_X_goes_mainline.html</link>
      <pubDate>03 Jun 2009 19:20 PDT</pubDate>
      <description>      &lt;p&gt;
	Thanks to the heroic work of Nicholas Nethercote and Julian
	Seward, the Mac OS X port of Valgrind is &lt;a
	href="http://blog.mozilla.com/nnethercote/2009/05/28/mac-os-x-now-supported-on-the-valgrind-trunk/"&gt;now
	available on Valgrind's trunk&lt;/a&gt;. This is a big step forward
	towards an official &lt;a
	href="http://valgrind.org"&gt;Valgrind&lt;/a&gt; release with Mac OS X support. 
      &lt;/p&gt;
      &lt;p&gt;
	For those of you with &lt;a
	href="http://www.apple.com/macosx/snowleopard/"&gt;Snow Leopard&lt;/a&gt;
	seeds, Valgrind won't 
	work. Valgrind operates at the low-level unsupported guts of the
	kernel/Libc interface. When the kernel and Libc change, Valgrind
	needs to adapt or die. Valgrind support for Snow Leopard will not
	be available until the open-source release of Snow Leopard's
	kernel and Libc at the earliest, which in turn is not before Snow
	Leopard itself ships.  
      &lt;/p&gt;
</description>
    </item>
  </channel>
</rss>
