<?xml version="1.0"?>
<!-- name="generator" content="blosxom/2.0" -->
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Landon Fuller   </title>
		<link>http://landonf.bikemonkey.org</link>
		<description>Technical Minutiae</description>
		<language>en</language>
		<!-- <atom:link href="http://landonf.bikemonkey.org/index.rss" rel="self" type="application/rss+xml" /> -->
		<copyright>Copyright (c) 2008 Landon Fuller. All Rights Reserved.</copyright>
<item>
	<title>Reliable Crash Reporting</title>
	<link>http://landonf.bikemonkey.org/2011/09/14#Reliable_Crash_Reporting.20110912</link>
	<guid>http://landonf.bikemonkey.org/2011/09/14#Reliable_Crash_Reporting.20110912</guid>
	<pubDate>Wed, 14 Sep 2011 16:09 PDT</pubDate>
	<description>&lt;h3 class=&quot;story&quot; &gt; Introduction&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;&lt;a href=&quot;http://code.google.com/p/plcrashreporter/&quot;&gt;PLCrashReporter&lt;/a&gt; is a standalone open-source library for generating crash reports on iOS. When I first wrote the library in 2008, it was the only option for automatically generating and gathering crash reports from an iOS application. Apple's iOS crash reports were not available to developers, and existing crash reporters &amp;mdash; such as Google's excellent &lt;a href=&quot;http://code.google.com/p/google-breakpad/&quot;&gt;Breakpad&lt;/a&gt; &amp;mdash; were not supported on iOS (Breakpad still isn't). Since that time, quite a few crash reporters and crash reporting services have appeared: Apple now provides access to App Store crash reports, a number of 3rd-party products and services were built on PLCrashReporter (such as &lt;a href=&quot;http://www.hockeyapp.net/&quot;&gt;HockeyApp&lt;/a&gt;, &lt;a href=&quot;http://www.atlassian.com/en/jiramobileconnect&quot;&gt;JIRA Mobile Connect&lt;/a&gt;), and some services have chosen to write their own crash reporting library (&lt;a href=&quot;http://testflightapp.com&quot;&gt;TestFlight&lt;/a&gt;, &lt;a href=&quot;http://www.airbrakeapp.com/&quot;&gt;Airbrake&lt;/a&gt;, and others).&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Despite this obvious interest in adopting crash reporting from iOS developers, there has remained little understanding of the complexities and difficulties in implementing a reliable and safe crash reporter, and many of the custom crash reporting libraries have been implemented improperly. It's my intention to explore what makes crash reporting difficult (especially on iOS), and provide real-world examples of how an impoperly written crash reporter can fail &amp;mdash; sometimes with little fanfare, and sometimes with surprising consequences.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; A Hostile Environment&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Implementing a reliable and safe crash reporter on iOS presents a unique challenge: an application may not fork() additional processes, and &lt;em&gt;all handling of the crash must occur in the crashed process.&lt;/em&gt; If this sounds like a difficult proposition to you, you're right. Consider the state of a crashed process: the crashed thread abruptly stopped executing, memory might have been corrupted, data structures may be partially updated, and locks may be held by a paused thread. It's within this hostile environment that the crash reporter must reliably produce an accurate crash report.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To reliably execute within this hostile environment, code must be &quot;async-safe&quot;: it must not rely on external, potentially inconsistent state. More concretely, this means that a crash reporter must avoid APIs that have not been written explicitly to be executed with a signal handler, in a potentially crashed process. This excludes everything from malloc &amp;mdash; the heap may have been corrupted, or partially updated &amp;mdash; to Objective-C &amp;mdash; locks may be held in the runtime, or data structures might be partially initialized. In fact, there's so little that you can do safely within a signal handler, it's much easier to define what you *can* do safely &amp;mdash; a minimum number of system calls and APIs are defined to be async-safe, and those are the only APIs you can reliably call.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Thus, to be reliable and safe, a crash reporter must be written with async-safety in mind, eliminating or minimizing the risk inherent in operating within a hostile and corrupt environment. At this point, you might ask what I mean by &quot;reliable and safe&quot; &amp;mdash; after all, if the process has already crashed, what's the worst that could happen? It crashes again? In fact, there's quite a bit that can go wrong, largely depending on the non-async-safe APIs that an unreliable crash reporter might rely on.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; First, do no harm&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;The mission of a crash reporter is simple: report crashes, and provide enough information to debug them. As it turns out, if you know how to read the report, just about everything you need to debug nearly all crashes can be found in the backtraces, register state, and a bit of intuition about your own code (look for a future blog post on this subject).&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;However, a crash reporter should never make things worse &amp;mdash; I'll cover three major ways it can do that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relying on non-async-safe APIs: This can deadlock the process and cause a hang, resulting in a failure to report the crash. In the pathological case, this can result in corruption of users' data.&lt;/li&gt;
&lt;li&gt;Using overly simple APIs like &lt;a href=&quot;http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/backtrace.3.html&quot;&gt;backtrace(3)&lt;/a&gt;: Blinds the developer to specific problems --- such as a stack overflow -- by being unable to report them.&lt;/li&gt;
&lt;li&gt;Discarding crashed thread's register state: Providing incomplete crash reports makes it practically impossible to debug certain classes of crashes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;Let's explore these failure cases with some real world examples.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Failure Case: Async-Safety and Deadlocking Objective-C&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;One of the more likely failure modes when dealing with a non-async-safe APIs is a deadlock. Imagine that the application has just acquired a lock prior to crashing. If the
crash reporter's implementation then attempts to acquire the same lock, it will wait forever: the crashed thread is no longer running and will never release the lock.
When a deadlock like this occurs on iOS, the application will appear unresponsive for 10-20 seconds until the system watchdog terminates the process, or the user
force quits the application.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;It is possible to trigger such a deadlock simply by using Objective-C within the signal handler. The Objective-C runtime itself maintains a number of internal locks, and if a
thread happens to hold a runtime lock when a crash occurs, any use of Objective-C in the crash reporter itself will trigger a deadlock.  This is
dependent on timing, however &amp;mdash; for the purposes of a providing a simple test case, I've created a contrived example that will reliably demonstrate the deadlock on ARM and x86:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;
static void unsafe_signal_handler (int signo) {
    /* Attempt to use ObjC to fetch a backtrace. Will trigger deadlock. */
    [NSThread callStackSymbols];
    exit(1);
}
 
int main(int argc, char *argv[]) {
    /* Remove this line to test your own crash reporter */
    signal(SIGSEGV, unsafe_signal_handler);
 
    /* Some random data */
    void *cache[] = {
        NULL, NULL, NULL
    };
 
    void *displayStrings[6] = {
        &quot;This little piggy went to the market&quot;,
        &quot;This little piggy stayed at home&quot;,
        cache, 
	&quot;This little piggy had roast beef.&quot;,
        &quot;This little piggy had none.&quot;,
        &quot;And this little piggy went 'Wee! Wee! Wee!' all the way home&quot;,
    };
    
    /* A corrupted/under-retained/re-used piece of memory */
    struct {
        void *isa;
    } corruptObj;
    corruptObj.isa = displayStrings;
 
    /* Message an invalid/corrupt object. This will deadlock crash reporters
     * using Objective-C. */
    [(id)&amp;corruptObj class];
     
    return 0;
}&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;If you run this code on iOS or the simulator, you'll reliably trigger a deadlock in any crash reporter using Objective-C in its signal handler. While this specific
example is somewhat contrived for the sake of serving as reliable test case, &lt;em&gt;any&lt;/em&gt; use of Objective-C or any other non-async-safe function in
a signal handler has the potential to trigger such a deadlock, and should be avoided.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Failure Case: Async-Safety and Data Corruption&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;The risk of data corruption is a far more potent concern than a deadlock. There are a surprising number of ways that this can occur &amp;mdash; but what might be the most likely (and dangerous)
mechanism to trigger data corruption is the reentrant running of the application's event loop.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Some crash reporters attempt to submit the crash report over the network immediately upon program termination. This introduces an interesting failure mode: spinning the runloop
to handle network traffic may also trigger execution of the application's own code, and the application is then free to attempt to write potentially corrupt user data.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Consider a Core Data-based application, in which a model object is updated, and then saved:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;person.name = name;
person.age = age; // a crash occurs here
person.birthday = birthday;
[context save: NULL];&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;At the time of the crash, the managed object context contains a partially updated record &amp;mdash; certainly not something you want saved to the database. However, if the crash reporter
then proceeds to reentrantly run the application's runloop, any network connections, timers, or other pending runloop dispatches in your application will also be run. If the
application code dispatched from the runloop contains a call to -[NSManagedObjectContext save:], you'll write a partially updated record to the database, corrupting the user's data.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;This approach of executing non-reentrant, non-async-safe code from a crash reporter is particularly dangerous. To avoid this, the signal handler can not make use of higher-level networking APIs
at crash time, and crash report implementations must not attempt to submit a crash report until the application has started again.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If added to your UIApplicationDelegate, the following code will print a message to the console if your crash reporter spins the runloop after a crash has occurred:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@&quot;APPLICATION CODE IS RUNNING - Crash reporter is spinning runloop&quot;);
});
*((int *)NULL) = 5; // trigger a crash&lt;/pre&gt;

&lt;h3 class=&quot;story&quot; &gt; Failure Case: Stack Traces and Stack Overflow&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;There are two ways to implement backtrace support in a crash reporter (but only one of them is reliable):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use high-level APIs such as backtrace(3) or -[NSThread callStackSymbols] to fetch a stack trace for the current thread&lt;/li&gt;
&lt;li&gt;Implement custom stack walking to support fetching a backtrace for all threads (including the crashed thread)&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;The first solution is unreliable for a number of reasons, but the failure mode I'll be addressing here is a stack
overflow. In the case of a stack overflow, a crash reporter that makes use of backtrace(3) or similar APIs will
be entirely unable to report the crash. Here's a code example that will accidentally trigger an overflow:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;/* A small typo can trigger infinite recursion ... */
NSArray *resultMessages = [NSMutableArray arrayWithObject: @&quot;Error message!&quot;];
NSMutableArray *results = [[NSMutableArray alloc] init];
 
for (NSObject *result in resultMessages)
    [results addObject: results]; // Whoops!
 
NSLog(@&quot;Results: %@&quot;, results);&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;If the crash reporter makes use of backtrace(3), this crash will not be reported. The reason is straight-forward:
These backtrace APIs must execute on on the crashed thread's stack, but that stack just overflowed. There is is no
stack space within which the crash reporter's signal handler can be run.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;However, if the crash reporter uses &lt;a href=&quot;http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/sigaltstack.2.html&quot;&gt;sigaltstack()&lt;/a&gt; to correctly execute on a different stack, the backtrace will be empty &amp;mdash; the signal
handler is running on a new stack!&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;In PLCrashReporter, this was solved by implementing custom stack walking for the supported platforms. This requires more complexity,
but in addition to supporting the generation of reports in the case of stack overflow, also allows the crash reporter
to provide stack traces for all running threads.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Conclusion&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Implementing a reliable crash reporter is difficult, and these are only a brief overview of the potential pitfalls and complexities involved. I
think Mike Ash best described the complexity of signal handlers in &lt;a href=&quot;http://mikeash.com/pyblog//friday-qa-2011-04-01-signal-handling.html&quot;&gt;Friday Q&amp;A&lt;/a&gt;:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;There is very little that you can do safely. There is so little that I'm
not even going to discuss how to get anything done, because it's so impractical
to do so, and instead will simply tell you to avoid using signal handlers
unless you really know what you're doing and you enjoy pain.&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;While I can't claim that PLCrashReporter is perfect, great effort (and pain) have been expended in ensuring its reliability and correctness.
If you're considering implementing your own ad-hoc reporter, I'd highly recommend reviewing the design decisions
made in both Google Breakpad and PLCrashReporter, both of which are liberally licensed and may be included in any commercial and/or closed-source
product.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;As a developer considering the use of a crash reporter in your application, I hope this overview will provide a little more insight into
their function (and design complexities), as well as providing you with some tools to evaluate the efficacy of the available solutions -- complex
failure cases are often the time when you need accurate, reliable crash reporting the most.&lt;/p&gt;

</description>
</item>
<item>
	<title>Implementing imp_implementationWithBlock()</title>
	<link>http://landonf.bikemonkey.org/2011/04/14#imp_implementationWithBlock.20110413</link>
	<guid>http://landonf.bikemonkey.org/2011/04/14#imp_implementationWithBlock.20110413</guid>
	<pubDate>Thu, 14 Apr 2011 06:46 PDT</pubDate>
	<description>&lt;p class=&quot;story&quot;&gt;In iOS 4.3, Apple introduced a new API to support the use of blocks as Objective-C method implementations. The API provides similar functionality as Mike Ash's &lt;a href=&quot;https://github.com/mikeash/MABlockClosure&quot;&gt;MABlockClosure&lt;/a&gt;, which uses &lt;a href=&quot;http://sources.redhat.com/libffi/&quot;&gt;libffi&lt;/a&gt; (or &lt;a href=&quot;https://github.com/landonf/libffi-ios&quot;&gt;libffi-ios&lt;/a&gt;) to implement support for generating arbitrary function pointers that dispatch to a block.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;However, Apple's new API differs from MABlockClosure in a few important ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only Obj-C IMP-compatible function pointers may be generated.&lt;/li&gt;
&lt;li&gt;No knowledge of the block's type signature is required (with some caveats we'll discuss below).&lt;/li&gt;
&lt;li&gt;The trampoline is lightweight (just a few instructions), this is faster than libffi-based dispatch as used by MABlockClosure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;Today, I'll be discussing how block-based message dispatch is implemented by Apple, and how you can implement your own similar, custom trampolines on iOS and Mac OS X. Additionally, I've posted &lt;a href=&quot;https://github.com/landonf/plblockimp&quot;&gt;PLBlockIMP&lt;/a&gt; on github. This project provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An open-source implementation of imp_implementationWithBlock() for Mac OS X 10.6+ and iOS 4.0+ (ARM, i386, and x86-64).&lt;/li&gt;
&lt;li&gt;Generalized code to support automated generation of arbitrary trampoline pages on Darwin, based on the trampoline allocator I wrote for &lt;a href=&quot;https://github.com/landonf/libffi-ios&quot;&gt;libffi-ios&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;In addition to this article, Bill Bumgarner has written &lt;a href=&quot;http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementationwithblock/&quot;&gt;an excellent introduction&lt;/a&gt; to imp_implementationWithBlock, and if you need a refresher in objective message dispatch, Mike Ash has a more in-depth explanation &lt;a href=&quot;http://www.mikeash.com/pyblog/friday-qa-2009-03-20-objective-c-messaging.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;This work has been funded by my employer, &lt;a href=&quot;http://www.plausible.coop&quot;&gt;Plausible Labs&lt;/a&gt;. We specialize in Mac OS X, iOS, and Android development and we're &lt;a href=&quot;mailto:contact@plausible.coop&quot;&gt;available for hire&lt;/a&gt;.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Creating Trampolines without Writable Code&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;The implementation of imp_implementationWithBlock() relies on trampolines to convert between Objective-C method calls and block dispatch. Trampolines are small pieces of code that, when called, perform some intermediary operations and then jump to the actual target destination. When you call imp_implementationWithBlock(), a function pointer to a trampoline is returned; it's this trampoline's responsibility to modify the function arguments and then jump to the actual code corresponding to the block's implementation.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Trampolines often require more information than can be derived from their function parameters -- such is the case with our IMP trampolines, which must have a pointer to the target block that they should call. Historically, this type of trampoline has generally been implemented through the use of writable code pages; the instructions are written to a PROT_EXEC|PROT_WRITE page at runtime, with any additional context information included directly in the generated code.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Unfortunately, iOS has instituted a restriction on the use of writable, executable pages (although there are &lt;a href=&quot;https://gist.github.com/855607&quot;&gt;signs&lt;/a&gt; that this &lt;em&gt;may&lt;/em&gt; eventually be lifted), necessitating the use of an alternative mechanism for implementing trampoline-specific context data. While iOS does not allow the use of writable code, we can leverage a combination of &lt;a  href=&quot;http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/vm_remap.html&quot;&gt;vm_remap()&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Addressing_mode#PC-relative_2&quot;&gt;PC-relative addressing&lt;/a&gt; to implement configurable trampolines without writable code.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;On Darwin, vm_remap() provides support for mapping an existing code page at new address, while retaining the existing page protections; using vm_remap(), we can create multiple copies of existing, executable code, placed at arbitrary addresses. If we generate a template page filled with trampolines at build time, we can create arbitrary duplicates of that page at runtime. This allows us to allocate an arbitrary number of trampolines using that template without requiring writable code:&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;&lt;div style=&quot;width: 497px; margin: 0 auto 2em auto; text-align: center&quot;&gt;
    &lt;img src=&quot;/static/blog/code/objc/blockimp_vm_remap.png&quot; /&gt;
    &lt;p&gt;
        &lt;strong&gt;Figure 1: vm_remap()&lt;/strong&gt;
    &lt;/p&gt;
&lt;/div&gt;&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;However, executable trampoline allocation only solves half the problem -- we still need a way to configure each trampoline.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;The solution is PC-relative addressing. The processor's program counter register indicates the address of the currently executing instruction; PC-relative addressing uses the program counter to address memory &lt;em&gt;relative&lt;/em&gt; to the currently executing instruction. When we remap our trampolines and then jump to them, each trampoline is executing at a unique address. If we then map a writable data page &lt;strong&gt;next to&lt;/strong&gt; our trampoline page, we can use PC-relative addressing to load per-trampoline data from adjacent writable data page:&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;&lt;div style=&quot;width: 497px; margin: 0 auto 2em auto; text-align: center&quot;&gt;
    &lt;img src=&quot;/static/blog/code/objc/blockimp_vm_remap_data.png&quot; /&gt;
    &lt;p&gt;
        &lt;strong&gt;Figure 2: vm_remap() with writable data pages&lt;/strong&gt;
    &lt;/p&gt;
&lt;/div&gt;&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Once a full page of trampolines are allocated, we simply need to provide the individual trampolines on request, and allocate additional pages if the pool of trampolines is exhausted. A full implementation of a trampoline allocator is available in PLBlockIMP; refer to pl_trampoline_alloc(), pl_trampoline_data_ptr(), and pl_trampoline_free().&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To save space within our trampoline page, each individual trampoline saves the PC register, and then jumps to a common implementation at the start of the trampoline page. The ARM implementation of the individual trampoline stub is two instructions:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;mov r12, pc
b _block_tramp_dispatch;&lt;/pre&gt;

&lt;h3 class=&quot;story&quot; &gt; IMP-&gt;Block Dispatch (self, _cmd, and Block)&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;As noted in Bill Bumgarner's &lt;a href=&quot;http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementationwithblock/&quot;&gt;article&lt;/a&gt; on imp_implementationWithBlock, every Objective-C method has two implicit, pointer-sized arguments at the head of the method's argument list: self, and _cmd. Likewise, block implementations also have an implicit first argument; the block literal, which maintains the block's reference count, bock descriptor, references to captured variables, and other block data.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;When called, a trampoline returned by imp_implementationWithBlock() is responsible for re-ordering its arguments to match those required by the block's implementation: the 'self' argument must be moved to second argument slot (overwriting _cmd), and the block literal moved to the (now vacated) first argument slot.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;However, there is one wrinkle that Bill didn't touch on: structure return values. On the current architectures supported by Darwin, functions that return structures by value &lt;em&gt;may&lt;/em&gt; have an additional pointer at the start of their argument list. This pointer is used to provide the address on the caller's stack at which the structure return value should be written.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;In the case where structure return (stret) calling conventions are used, the structure return pointer in the first argument slot must remain unmodified, the block literal argument must be in the &lt;em&gt;second&lt;/em&gt; argument slot, and the self pointer in the &lt;em&gt;third&lt;/em&gt;. This requires that imp_implementationWithBlock() provide two different trampoline implementations to support both calling conventions, and that the requisite trampoline type for a block be determined when imp_implementationWithBlock is called.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;There is no way to determine from a raw function pointer whether a function requires stret calling conventions -- to work around this, Apple's compilers set an additional flag, BLOCK_USE_STRET, when emitting a block that requires the stret calling conventions. This flag may be used to easily determine the necessarily trampoline type for a block.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;As described in the previous section, the individual trampolines save their PC address and then immediately jump to a shared implementation at the start of the trampoline page. That shared implementation re-orders the existing arguments, loads the block literal from its PC-relative configuration data, and then jumps to the block's implementation -- on ARM, our non-stret shared implementation looks like this:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;_block_tramp_dispatch:
    # trampoline address+8 is in r12 -- calculate our config page address
    sub r12, #0x8
    sub r12, #0x1000
    
    # Set the 'self' argument as the second argument
    mov r1, r0
    
    # Load the block pointer as the first argument
    ldr r0, [r12]
 
    # Jump to the block pointer
    ldr pc, [r0, #0xc]&lt;/pre&gt;

&lt;h3 class=&quot;story&quot; &gt; Conclusion&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;While I still have my fingers crossed for PROT_EXEC|PROT_WRITE pages on iOS, vm_remap()-based trampolines can serve as a viable replacement for some tasks. If you have further questions, or ideas for other neat projects worth tackling, feel free to drop me an e-mail.&lt;/p&gt;

</description>
</item>
<item>
	<title>Work with Plausible Labs</title>
	<link>http://landonf.bikemonkey.org/2010/02/18#Work_with_Plausible_Labs.20100218</link>
	<guid>http://landonf.bikemonkey.org/2010/02/18#Work_with_Plausible_Labs.20100218</guid>
	<pubDate>Thu, 18 Feb 2010 10:26 PST</pubDate>
	<description>&lt;p class=&quot;story&quot;&gt;When I started &lt;a href=&quot;http://www.plausiblelabs.com/about/&quot;&gt;Plausible Labs&lt;/a&gt; in
2008, we were working out of our homes, paying our rent out of savings while we figured
out how to bootstrap worker-owned &lt;a href=&quot;http://www.plausiblelabs.com/about/&quot;&gt;software
cooperative&lt;/a&gt; without external funding.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;A year or so later, I'm part of a three member team. We are all still working out of a home, but now
it's a loft in the Mission with proper office space, Ikea desks, and even a few Aeron chairs.
We've got the issue of self-sufficiency figured out, and now we're looking to the future -- how
we'd like to grow our organization, what sort of software projects we'd like to tackle (both in-house
and on contract), and how the co-operative experiment will scale past a few members.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;We're ready to take on more (and larger) projects, and we’d like to find the right engineer for a
contract-to-hire role to help us out. We’re particularly keen in finding someone genuinely
interested in joining the co-operative, and open to working with us on either a part-time or
full-time basis.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If you'd be interested in working with us, you can find the job posting
&lt;a href=&quot;http://www.plausiblelabs.com/blog/?p=23&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

</description>
</item>
<item>
	<title>OpenJDK 7 on Leopard PPC</title>
	<link>http://landonf.bikemonkey.org/2009/12/16#OpenJDK_7_PPC.20091205</link>
	<guid>http://landonf.bikemonkey.org/2009/12/16#OpenJDK_7_PPC.20091205</guid>
	<pubDate>Wed, 16 Dec 2009 17:29 PST</pubDate>
	<description>&lt;h3 class=&quot;story&quot; &gt; Introduction&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Thanks to the &lt;a href=&quot;http://gbenson.net/?p=166&quot;&gt;work of Gary Benson&lt;/a&gt; on implementing
and merging the &lt;a href=&quot;http://openjdk.java.net/projects/zero/&quot;&gt;Zero-Assembler Project&lt;/a&gt;,
and Greg Lewis' work bringing it to &lt;a href=&quot;http://openjdk.java.net/projects/zero/&quot;&gt;OpenJDK BSD Port&lt;/a&gt;,
it's now possible to bootstrap OpenJDK 7 on Mac OS X 10.5/PPC.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Gary Benson's Zero Assembler provides a portable implementation of the JVM intepreter that -- unlike
the existing JVM implementations -- relies on very little assembler to provide an acceptably performing but
highly portable VM, opening the door to supporting Mac OS X PPC with very little additional work.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;I've committed the few small fixes to get OpenJDK running on Mac OS X 10.5/PPC, and have bootstrapped an initial OpenJDK 7
binary using Havard Eidnes's &lt;a href=&quot;http://freebsd.monkey.org/freebsd-java/200802/msg00002.html&quot;&gt;bootstrap scripts&lt;/a&gt;.  
Bootstrapping the initial VM running is sufficiently involved that I would recommend using my
binaries (&lt;a href=&quot;http://hg.bikemonkey.org/archive/openjdk7_darwin/openjdk7-macppc-2009-12-16-b4.tar.bz2&quot;&gt;openjdk7-macppc-2009-12-16-b4.tar.bz2&lt;/a&gt;).&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Source Access &lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;OpenJDK uses &lt;a href=&quot;http://www.selenic.com/mercurial/wiki/&quot;&gt;Mercurial&lt;/a&gt; with the Forest extension.
Before checking out the BSD sources, you will need to install and configure Mercurial. See the
&lt;a href=&quot;http://openjdk.java.net/guide/repositories.html#installConfig&quot;&gt;OpenJDK Developer's Guide&lt;/a&gt; for more
information.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To check out the BSD-Port forest:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;hg fclone http://hg.openjdk.java.net/bsd-port/bsd-port&lt;/pre&gt;

&lt;h3 class=&quot;story&quot; &gt; Building on Leopard/PPC&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Building OpenJDK requires Java 6 or OpenJDK 7 -- on PPC, you will need to download or build
an OpenJDK 7 bootstrap VM (&lt;a href=&quot;http://hg.bikemonkey.org/archive/openjdk7_darwin/openjdk7-macppc-2009-12-16-b4.tar.bz2&quot;&gt;openjdk7-macppc-2009-12-16-b4.tar.bz2&lt;/a&gt;).&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To build the JDK in build/bsd-ppc/j2sdk-image:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;make \
CC=gcc-4.0 \
CXX=g++-4.0 \
ALT_BOOTDIR=/usr/local/openjdk7-macppc-2009-12-16-b4
ANT_HOME=/usr/share/ant \
ALT_FREETYPE_HEADERS_PATH=/usr/X11/include \
ALT_FREETYPE_LIB_PATH=/usr/X11/lib \
ALT_CUPS_HEADERS_PATH=/usr/include \
ALT_CACERTS_FILE=/System/Library/Frameworks/JavaVM.framework/Home/lib/security/cacerts \
LIBFFI_CFLAGS=&quot;-I/usr/include/ffi&quot; \
NO_DOCS=true \
ZERO_BUILD=true \
ZERO_ENDIANNESS=big \
ZERO_LIBARCH=ppc \
ZERO_ARCHDEF=PPC \
ZERO_ARCHFLAG=-m32&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;Be sure to set ALT_BOOTDIR to the path of your installed openjdk7-macppc-2009-12-16-b4 bootstrap JDK.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Once built, you should now have a JDK in build/bsd-ppc/j2sdk-image:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;landonf@onefish:~/openjdk-ppc/bsd-port&gt; ./build/bsd-ppc/j2sdk-image/bin/java -version
openjdk version &quot;1.7.0-internal&quot;
OpenJDK Runtime Environment (build 1.7.0-internal-landonf_2009_12_16_12_54-b00)
OpenJDK Zero VM (build 17.0-b05, interpreted mode)&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;For more information or assistance, please refer to the OpenJDK BSD-Port &lt;a href=&quot;http://wikis.sun.com/display/OpenJDK/BSDPort&quot;&gt;wiki&lt;/a&gt; and
&lt;a href=&quot;http://mail.openjdk.java.net/mailman/listinfo/bsd-port-dev&quot;&gt;mailing list&lt;/a&gt;. My testing has been very limited -- if you run into issues, please report
them on the development mailing list.&lt;/p&gt;

</description>
</item>
<item>
	<title>Using Blocks: Understanding the Memory Management Rules</title>
	<link>http://landonf.bikemonkey.org/2009/07/11#Using_Blocks_2.20090710</link>
	<guid>http://landonf.bikemonkey.org/2009/07/11#Using_Blocks_2.20090710</guid>
	<pubDate>Sat, 11 Jul 2009 18:09 PDT</pubDate>
	<description>&lt;h3 class=&quot;story&quot; &gt; Introduction&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;I just finished uploading &lt;a href=&quot;http://code.google.com/p/plblocks/&quot;&gt;PLBlocks&lt;/a&gt; 1.0-beta2, which
adds support for iPhoneOS 2.2, iPhone 3GS armv7-optimized binaries, and host support for PPC systems
(&lt;a href=&quot;http://www.plausiblelabs.com/blog/?p=11&quot;&gt;release announcement&lt;/a&gt;). This seems like a good time a good time to continue with my ad-hoc
series on developing with Blocks (&lt;a href=&quot;http://landonf.bikemonkey.org/code/iphone/Using_Blocks_1.20090704.html&quot;&gt;previous post&lt;/a&gt;).&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;I've fielded a number of questions regarding the memory management rules surrounding blocks; This article
should help you understand the basic rules necessary to use blocks successfully.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;I won't delve too deep into the describing the actual block implementation,
as that's a topic well-covered by Clang's &lt;a href=&quot;http://clang.llvm.org/docs/BlockImplementation.txt&quot;&gt;Block Implementation Specification&lt;/a&gt;
Even if you don't have time to read the implementation specification, it's important to note that blocks are really just a few
functions and structures implemented for you by the compiler, coupled with a runtime to help with managing setup and tear down.
You could implement the same thing yourself in pure C -- but it would be so verbose as to not be useful.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; The Memory Management Rules&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Apple provides a set of straight-forward &lt;a href=&quot;http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html&quot;&gt;memory management rules&lt;/a&gt;
for Objective-C. I've endeavoured to provide an equivalent set of rules for blocks in C and Objective-C.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;This is the single fundamental memory management rule:&lt;/p&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;To take ownership of a block, you must copy that block via -copy or Block_copy(). You
are responsible for relinquishing ownership of blocks you own using -release or Block_release().&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;The following rules derive from the fundamental rule, or cope with edge cases:&lt;/p&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;Block_copy() must be paired with Block_release(); likewise, the Objective-C -copy message must be paired with an Objective-C -release message.&lt;/li&gt;
&lt;li&gt;By default, variables captured by a block are copied, and global variables are referenced directly. If a captured variable is marked with the __block storage specifier, the variable will be indirectly referenced and thus may be modified from within a block.&lt;/li&gt;
&lt;li&gt;
    When a block is copied (or released):
    &lt;ul class=&quot;story&quot;&gt;
        &lt;li&gt;Any referenced (captured) Objective-C objects will be retained (or released).&lt;/li&gt;
        &lt;li&gt;Any captured blocks will be copied (or released).&lt;/li&gt;
    &lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class=&quot;story&quot; &gt; Block Ownership: Copy vs. Retain&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;All blocks have a valid class pointer, and appear to be Objective-C objects. According to the standard Objective-C
memory management, to assume ownership of an object you must simply retain it. However, blocks
differ from the standard Objective-C objects in at least one fundamental and very important way:
blocks defined within a function are allocated on the stack, and will persist only until your
function returns.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;This is why it is necessary to copy a block, rather than retain it: If the block is stack allocated, it must
be copied to a heap allocation to persist beyond the lifetime of the current stack frame. Once copied,
the block will be heap allocated and persist like any other standard Objective-C object.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Thus, to ensure that you do not attempt to maintain ownership of a stack allocated block, you must &lt;em&gt;always&lt;/em&gt;
copy a block if you wish to reference it past the lifetime of your current function. As an optimization, if
a block has already been copied to the heap copying will simply increment the block's reference count.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;It should also be noted that there is one very real benefit to stack allocation of blocks: they are very cheap,
and unless copied, require no allocations or cleanup.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Captured Variables: Copy vs. Reference&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;By default, local variables (variables of the &quot;auto&quot; storage class) are captured by your block
as const copies, while global variables are accessed directly. The runtime automatically handles
reference counting of captured blocks and Objective-C objects.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To allow you modify a local variable (rather than its copy), Apple has added the __block storage specifier. Any __block variables are
accessed by referenced from within a block. This is best explained by example:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;__block int i = 0;
int j = 0;
executeBlock(^{
    // Increments the stack allocated 'i' variable by 1.
    i++;&lt;/pre&gt;

&lt;blockquote class=&quot;story&quot;&gt;&lt;p class=&quot;story&quot;&gt;// 'j' is a const copy, and can not be modified from within the block
    / j++
});&lt;/p&gt;&lt;/blockquote&gt;

&lt;p class=&quot;story&quot;&gt;If you copy a block, any captured __block variables will also be copied to the heap.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Further Reading&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;I hope these brief explanations have provided a reasonable introduction to memory management with blocks. For additional details, you may
wish to review Jim Dovey's post on the &lt;a href=&quot;http://alanquatermain.net/post/138827791/blocks-episode-2-life-cycles&quot;&gt;life-cycle of blocks&lt;/a&gt;,
and join the &lt;a href=&quot;http://groups.google.com/group/plblocks-devel&quot;&gt;PLBlocks Mailing List&lt;/a&gt; for further discussion.&lt;/p&gt;

</description>
</item>
<item>
	<title>Blocks Examples: NSOperationQueue and UIActionSheet  </title>
	<link>http://landonf.bikemonkey.org/2009/07/04#Using_Blocks_1.20090704</link>
	<guid>http://landonf.bikemonkey.org/2009/07/04#Using_Blocks_1.20090704</guid>
	<pubDate>Sat, 04 Jul 2009 18:25 PDT</pubDate>
	<description>&lt;h3 class=&quot;story&quot; &gt; Introduction&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;On Friday, &lt;a href=&quot;http://www.plausiblelabs.com&quot;&gt;Plausible Labs&lt;/a&gt; released &lt;a href=&quot;http://code.google.com/p/plblocks/&quot;&gt;PLBlocks&lt;/a&gt;, an
SDK that allows you to immediately begin experimenting with blocks (also known as closures) on
Mac OS X 10.5 and iPhone OS 3.0.  While the &lt;a href=&quot;http://www.plausiblelabs.com/blog/?p=8&quot;&gt;original announcement&lt;/a&gt; included a very brief
introduction to blocks, I thought it might be worthwhile to provide some concrete examples of using blocks in your own code.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Example Code&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;All the sample code and implementation classes for this article are available
via my &lt;a href=&quot;http://github.com/landonf/block_samples/tree/master&quot;&gt;block_samples&lt;/a&gt; github repository.
You may download the repository as an archive (no git required) by pressing the &quot;download&quot; button next
to the repository name.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To get started with the PLBlocks SDK, check out the download and installation instructions on the
&lt;a href=&quot;http://code.google.com/p/plblocks/#Download&quot;&gt;project page&lt;/a&gt;. Please note
that PLBlocks is still in beta.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; NSOperationQueue&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;On Mac OS X and iPhoneOS, there are a variety of ways to schedule operations on a background thread.
One method that's often used is calling -[NSObject performSelectorInBackground:withObject:] to execute
a method in the background, and then -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]
to provide the results to the primary thread.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;This works, but it's not as convenient as it could be:&lt;/p&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;Every background task requires at least one new method definition.&lt;/li&gt;
&lt;li&gt;Methods can only accept one (or two) arguments.&lt;/li&gt;
&lt;li&gt;There's no easy way to provide the background method with additional state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;Using blocks -- and a few small extensions to NSOperationQueue and NSThread -- we can
instead encapsulate this full exchange in one method, using a block to run
the background operation, and a nested block to handle the response directly on the main
thread:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Allocated here for succinctness.
    NSOperationQueue *q = [[NSOperationQueue alloc] init];
 
    /* Data to process */
    NSData *data = [@&quot;Hello, I'm a Block!&quot; dataUsingEncoding: NSUTF8StringEncoding];
 
    /* Push an expensive computation to the operation queue, and then
     * display the response to the user on the main thread. */
    [q addOperationWithBlock: ^{
        /* Perform expensive processing with data on our background thread */
        NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
         
        /* This is the &quot;expensive&quot; part =) */
        sleep(5);
         
        /* Inform the user of the result on the main thread, where it's safe to play with the UI. */
        [[NSThread mainThread] performBlock: ^{
            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
             
            [alert addButtonWithTitle: @&quot;OK&quot;];
            [alert setMessageText: [NSString stringWithFormat: @&quot;Processing completed: %@&quot;, string]];
            [alert runModal];
        }];
 
        /* We don't need to hold a string reference anymore */
        [string release];
    }];
}&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;The first block is scheduled to run on the NSOperationQueue, and inside contains an additional nested block. When the operation has
completed, it schedules its nested block to run on the main thread, where the result can be presented to the user,
or passed on for further processing.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;You can find the full NSOperationQueue and NSThread extensions -- including example usage -- 
&lt;a href=&quot;http://github.com/landonf/block_samples/tree/master/NSOperationBlocks&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; UIActionSheet&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;If you've done any iPhone development, you'll know that using
&lt;a href=&quot;http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIActionSheet_Class/Reference/Reference.html&quot;&gt;UIActionSheet&lt;/a&gt;
is a bit complicated -- more so if you want to share an action sheet implementation across view controllers,
or display multiple UIActionSheets from a single view controller.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If you've used UIActionSheet in the past, you've had to do the following:&lt;/p&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;Create a UIActionSheet instance in your view controller.&lt;/li&gt;
&lt;li&gt;Add the UIActionSheetDelegate protocol to your view controller.&lt;/li&gt;
&lt;li&gt;Add the -[UIActionSheetDelegate actionSheet:clickedButtonAtIndex:] delegate method.&lt;/li&gt;
&lt;li&gt;Match the button index and source action sheet to your intended action, fetch any state you need, and execute.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;Using blocks, we can significant reduce the effort required to define and use a UIActionSheet. Instead of defining a delegate, and matching
index values to your button actions, we can simply pass a block that implements the button's action directly:&lt;/p&gt;

&lt;pre class=&quot;story&quot; &gt;- (void) displaySheet {
    PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @&quot;Destination&quot;];
    
    /* A re-usable block that simply displays an alert message */
    void (^alert)(NSString *) = ^(NSString *message) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @&quot;Destination Selected&quot;
                                                        message: message
                                                       delegate: nil
                                              cancelButtonTitle: @&quot;OK&quot;
                                              otherButtonTitles: nil];
        
        [alert show];
        [alert release];
    };
    
    [sheet addButtonWithTitle: @&quot;Work&quot; block: ^{
        alert(@&quot;Work selected&quot;);
    }];
    
    [sheet addButtonWithTitle: @&quot;Home&quot; block: ^{
        alert(@&quot;Home selected&quot;);
    }];
    
    [sheet addButtonWithTitle: @&quot;School&quot; block: ^{
        alert(@&quot;School selected&quot;);
    }];
    
    [sheet setCancelButtonWithTitle: @&quot;Cancel&quot; block: ^{}];
 
    [sheet showInView: self.window];
    [sheet release];
}&lt;/pre&gt;

&lt;p class=&quot;story&quot;&gt;That's it -- there is nothing else. The blocks used for each button automatically have access to the enclosing
method's variables, and we even use another block (&lt;em&gt;alert&lt;/em&gt;) to avoid retyping the UIAlertView boilerplate
or cluttering our class with an alert method.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;You can find the full UIActionSheet wrapper -- including example usage -- 
&lt;a href=&quot;http://github.com/landonf/block_samples/tree/master/UIActionSheetBlocks&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Conclusion&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;I hope you'll find these examples useful in experimenting with and incorporating blocks into your own software. There are quite a few
other ways that blocks can be leveraged to decrease code size and complexity, and I'll plan on writing future articles on the subject.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If you'd like to discuss blocks, the PLBlocks implementation, or having any other
questions, feel free to join the &lt;a href=&quot;http://groups.google.com/group/plblocks-devel&quot;&gt;PLBlocks mailing list.&lt;/a&gt;&lt;/p&gt;

</description>
</item>
<item>
	<title>PLBlocks: Blocks for iPhoneOS 3.0 and Mac OS X 10.5</title>
	<link>http://landonf.bikemonkey.org/2009/07/02#Blocks_For_iPhone.20090703</link>
	<guid>http://landonf.bikemonkey.org/2009/07/02#Blocks_For_iPhone.20090703</guid>
	<pubDate>Thu, 02 Jul 2009 21:50 PDT</pubDate>
	<description>&lt;p class=&quot;story&quot;&gt;Over the weekend (and for some of the week) I've been working on
back-porting block support from the &lt;a href=&quot;http://www.apple.com/macosx/&quot;&gt;Snow Leopard&lt;/a&gt;
toolchain, with the goal of leveraging blocks for our iPhone and Mac OS X 10.5
development at Plausible Labs.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If you're unfamiliar with blocks, they're an implementation of closures for C,
Objective-C, and provisionally C++. The full &lt;a href=&quot;http://clang.llvm.org/docs/BlockLanguageSpec.txt&quot;&gt;language specification&lt;/a&gt;
and &lt;a href=&quot;http://clang.llvm.org/docs/BlockImplementation.txt&quot;&gt;ABI specification&lt;/a&gt; are available from the LLVM/Clang project.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;I've now published an initial beta release with support for iPhone OS 3.0 (armv6),
and Mac OS X 10.5 (i386, x86-64, ppc) -- you can find the full announcement -- including
download and usage instructions -- &lt;a href=&quot;http://www.plausiblelabs.com/blog/?p=8&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
</item>
<item>
	<title>New Plausible Labs Website</title>
	<link>http://landonf.bikemonkey.org/2009/05/20#Plausible_New_Website.20090521</link>
	<guid>http://landonf.bikemonkey.org/2009/05/20#Plausible_New_Website.20090521</guid>
	<pubDate>Wed, 20 May 2009 17:55 PDT</pubDate>
	<description>&lt;p class=&quot;story&quot;&gt;We just released a new design for the &lt;a href=&quot;http://www.plausiblelabs.com&quot;&gt;Plausible Labs&lt;/a&gt; website, home
of the fine worker-owned cooperative that keeps me gainfully employed so that I can
spend my free time (such as it exists) working on open source software.&lt;/p&gt;

</description>
</item>
<item>
	<title>Critical Mac OS X Java Vulnerabilities</title>
	<link>http://landonf.bikemonkey.org/2009/05/19#CVE-2008-5353.20090519</link>
	<guid>http://landonf.bikemonkey.org/2009/05/19#CVE-2008-5353.20090519</guid>
	<pubDate>Tue, 19 May 2009 14:42 PDT</pubDate>
	<description>&lt;h3 class=&quot;story&quot; &gt; Introduction&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Five months ago, &lt;a href=&quot;http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5353&quot;&gt;CVE-2008-5353&lt;/a&gt; and
&lt;a href=&quot;http://java.sun.com/javase/6/webnotes/6u11.html&quot;&gt;other vulnerabilities&lt;/a&gt; were publicly disclosed, and fixed by Sun.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;CVE-2008-5353 allows malicious code to escape the Java sandbox and run arbitrary
commands with the permissions of the executing user. This may result in untrusted Java applets
executing arbitrary code merely by visiting a web page hosting the applet. The issue
is trivially exploitable.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;Unfortunately, these vulnerabilities remain in Apple's shipping JVMs, as well as Soylatte 1.0.3. As
Soylatte does not provide browser plugins, the impact of the vulnerability is reduced. The recent
release of OpenJDK6/Mac OS X is not affected by CVE-2008-5353.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Work-Arounds&lt;/h3&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;Mac OS X users should disable Java applets in their browsers and disable 'Open &quot;safe&quot; files after downloading' in Safari.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://landonf.bikemonkey.org/static/soylatte&quot;&gt;Soylatte&lt;/a&gt; users running untrusted code should upgrade to an OpenJDK6-based release, where possible. No future releases of the JRL-based Soylatte branch are planned at this time. If this is an issue for you, please feel free to &lt;a href=&quot;mailto:landonf@macports.org&quot;&gt;contact me&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class=&quot;story&quot; &gt; Patch&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;&lt;strong&gt;Update 06-15-2009:&lt;/strong&gt; Apple has released Java for Mac OS X 10.5 Update 4, which
contains a fix for this issue.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;To update your system, run &quot;Software Update&quot; from the Apple menu.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;&lt;strong&gt;Note&lt;/strong&gt;: Safari users should leave 'Open &quot;safe&quot; files after download' permanently disabled. Similarly critical vulnerabilities unrelated to Java remain in Safari's handling of &quot;Safe&quot; files.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Proof of Concept&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Unfortunately, it seems that many Mac OS X security issues are ignored if the severity of the issue
is not adequately demonstrated. Due to the fact that an exploit for this issue is available in the wild,
and the vulnerability has been public knowledge for six months, I have decided to release a my own
proof of concept to demonstrate the issue.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;If you visit the following page, &quot;/usr/bin/say&quot; will be executed on your system by a Java applet,
with your current user permissions. &lt;a href=&quot;http://landonf.bikemonkey.org/static/moab-tests/CVE-2008-5353/hello.html&quot;&gt;This link
will execute code on your system with your current user permissions.&lt;/a&gt; The proof of concept runs
on fully-patched PowerPC and Intel Mac OS X systems.&lt;/p&gt;

&lt;h3 class=&quot;story&quot; &gt; Credit&lt;/h3&gt;

&lt;p class=&quot;story&quot;&gt;Thanks to Jeffrey Czerniak for bringing this issue to my attention.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;&lt;strong&gt;Update:&lt;/strong&gt; Julien Tinnes e-mailed with a link to his in-depth discussion regarding the vulnerability &lt;a href=&quot;http://blog.cr0.org/2009/05/write-once-own-everyone.html&quot;&gt;available here.&lt;/a&gt;&lt;/p&gt;

</description>
</item>
<item>
	<title>OpenJDK 6 for Mac OS X</title>
	<link>http://landonf.bikemonkey.org/2009/05/17#OpenJDK6_MacPorts.20090516</link>
	<guid>http://landonf.bikemonkey.org/2009/05/17#OpenJDK6_MacPorts.20090516</guid>
	<pubDate>Sun, 17 May 2009 15:19 PDT</pubDate>
	<description>&lt;p class=&quot;story&quot;&gt;As part of the OpenJDK project, various organizations have been working on &lt;a href=&quot;http://openjdk.java.net/projects/jdk6/&quot;&gt;OpenJDK 6&lt;/a&gt;, a freely distributable
Java 6 implementation based on the open source OpenJDK 7 code base. Most Linux distributions are now shipping OpenJDK 6 binaries.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;&lt;a href=&quot;http://landonf.bikemonkey.org/static/soylatte&quot;&gt;Soylatte&lt;/a&gt; (Java 6 Port for Mac OS X) was originally based on the BSD port of
the &lt;a href=&quot;http://www.java.net/jrl.csp&quot;&gt;JRL&lt;/a&gt; licensed Java 6 code base, which significantly constrains end-user usage
and distribution rights. With Sun's approved re-licensing of the BSD changes for use in OpenJDK 7, a backport to OpenJDK 6 was made possible.&lt;/p&gt;

&lt;p class=&quot;story&quot;&gt;I've added support for OpenJDK 6 on Mac OS X, based on Brian Gardner's &lt;a href=&quot;http://lists.freebsd.org/pipermail/freebsd-java/2009-February/007887.html&quot;&gt;work&lt;/a&gt; backporting the OpenJDK 7 BSD changes to
OpenJDK6/FreeBSD. Unlike the legacy Soylatte builds, OpenJDK 6 is:&lt;/p&gt;

&lt;ul class=&quot;story&quot;&gt;
&lt;li&gt;Open source and freely redistributable under the &lt;a href=&quot;http://www.openjdk.org/legal/gplv2+ce.html&quot;&gt;GPLv2 w/ ClassPath Exception license&lt;/a&gt; -- you may bundle the JDK/JRE with commerical products.&lt;/li&gt;
&lt;li&gt;More up-to-date -- the current build of OpenJDK6/Mac OS X should be runtime equivalent to Java 6 Update 11 (Sun JRL source releases are sporadic).&lt;/li&gt;
&lt;li&gt;Available as a binary package as well as installable via MacPorts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p class=&quot;story&quot;&gt;The initial beta release is available for testing via the &lt;a href=&quot;http://www.macports.org&quot;&gt;MacPorts&lt;/a&gt; openjdk6 port (Leopard only),
or as a binary from the &lt;a href=&quot;http://landonf.bikemonkey.org/static/soylatte&quot;&gt;Soylatte&lt;/a&gt; web page (Leopard/Tiger, untested on Tiger).
My ability to provide 10.4 support is constrained without access to a 10.4 machine, and any testing/development assistance is most welcome.&lt;/p&gt;

</description>
</item>
</channel>
</rss>

