<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>I’m Bavarious, (sporadically) writing about Objective-C, Apple frameworks and OS X.</description><title>Objectivist-C</title><generator>Tumblr (3.0; @objectivistc)</generator><link>http://objectivistc.tumblr.com/</link><item><title>Example of copyfile() progress via COPYFILE_STATE_COPIED</title><description>&lt;p&gt;Since it looks like &lt;a href="https://www.google.com/search?q=%22copyfile_state_copied%22"&gt;the Web&lt;/a&gt; doesn’t have an example of monitoring progress of a &lt;code&gt;copyfile()&lt;/code&gt; operation via &lt;code&gt;COPYFILE_STATE_COPIED&lt;/code&gt;, here it goes. &lt;del&gt;Note that the progress callback is disabled in OS X v10.8 even though the man page mentions it.&lt;/del&gt; As explained by &lt;a href="https://twitter.com/halfliterate/status/301615797548249088"&gt;Doug&lt;/a&gt;, &lt;code&gt;copyfile()&lt;/code&gt;’s status callback can be used to monitor progress of single files and recursive copies. Check &lt;a href="https://github.com/rustle/RSTLCopyOperation/blob/master/Copy/RSTLCopyOperation.m"&gt;his example with &lt;code&gt;COPYFILE_STATE_STATUS_CB&lt;/code&gt; in an &lt;code&gt;NSOperation&lt;/code&gt; subclass&lt;/a&gt;.&lt;/p&gt;

&lt;script src="https://gist.github.com/bavarious/4742450.js" type="text/javascript"&gt;&lt;/script&gt;</description><link>http://objectivistc.tumblr.com/post/42611639154</link><guid>http://objectivistc.tumblr.com/post/42611639154</guid><pubDate>Fri, 08 Feb 2013 22:27:00 +0000</pubDate><category>objective-c copyfile</category></item><item><title>Execnames in DTrace</title><description>&lt;p&gt;A few weeks ago, &lt;a href="http://twitter.com/Catfish_Man"&gt;David Smith&lt;/a&gt; asked whether it was possible to obtain the executable name of a process (given its PID) in DTrace. There is no built-in function for that, and I suggested the following:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Since the &lt;code&gt;proc&lt;/code&gt; provider supplies probes for process creation and termination, use these probes to keep a map of PIDs to execnames for &lt;strong&gt;new&lt;/strong&gt; processes;&lt;/li&gt;
&lt;li&gt;For previous processes, use the output from &lt;code&gt;ps(1)&lt;/code&gt; and feed it to the DTrace script at startup time.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The DTrace fragments below keep a &lt;code&gt;pnames&lt;/code&gt; map (an associative array) mapping PIDs to execnames of &lt;strong&gt;new&lt;/strong&gt; processes. I’m using three probes: &lt;code&gt;proc:::create&lt;/code&gt; for forked processes, &lt;code&gt;proc:::exec&lt;/code&gt; for execed process and &lt;code&gt;proc:::exit&lt;/code&gt; for processes that have terminated.&lt;/p&gt;

&lt;p&gt;Since it is possible that a child process was forked and not execed, copy the parent process execname to the child process:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;proc:::create
{
    this-&amp;gt;pid = args[0]-&amp;gt;pr_pid;
    this-&amp;gt;ppid = args[0]-&amp;gt;pr_ppid;

    pnames[this-&amp;gt;pid] = pnames[this-&amp;gt;ppid];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the child process was execed after fork, use the child execname instead of the parent execname:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;proc:::exec
{
    this-&amp;gt;pname = basename(args[0]);
    pnames[pid] = this-&amp;gt;pname;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When a process exits, keep its execname and append &lt;code&gt;(exited)&lt;/code&gt; for debugging purposes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;proc:::exit
{
    pnames[pid] = strjoin(pnames[pid], " (exited)");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For existing processes, run &lt;code&gt;ps&lt;/code&gt; and convert its output to a series of DTrace statements that add PID → execname mappings to &lt;code&gt;pnames&lt;/code&gt; like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ps -axco "pid= comm="
| awk '{printf "pnames[%s]", $1; $1=""; sub(/^ /, "", $0); printf " = \x22%s\x22;\n", $0}'
&amp;gt; tmp/pnames.h
; sudo dtrace -C -I/tmp -s someDTraceScript.d
&lt;/code&gt;&lt;/pre&gt;

&lt;ol&gt;&lt;li&gt;The first line (&lt;code&gt;ps&lt;/code&gt;) outputs a list of running processes with two columns: PID and command name;&lt;/li&gt;
&lt;li&gt;The second line (&lt;code&gt;awk&lt;/code&gt;) writes &lt;code&gt;pnames[PID] = "execname"";&lt;/code&gt; for each process listed in step 1;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The third line (&lt;code&gt;&amp;gt;&lt;/code&gt;) stores the output from step 2 in /tmp/pnames.h;&lt;/p&gt;

&lt;p&gt;Sample contents of /tmp/pnames.h:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pnames[1] = "launchd";
pnames[11] = "UserEventAgent";
pnames[12] = "kextd";
pnames[13] = "taskgated";
pnames[14] = "notifyd";
pnames[15] = "securityd";
pnames[16] = "diskarbitrationd";
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The fourth line runs &lt;code&gt;dtrace&lt;/code&gt; with the C preprocessor (&lt;code&gt;-C&lt;/code&gt;), specifying &lt;code&gt;/tmp&lt;/code&gt; as a directory to search for include files.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Using /tmp/pnames.h, the file created in step 3, is easy enough:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;dtrace:::BEGIN
{
#include "pnames.h"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is not a perfect solution, though: processes created between steps 1 and 4 may not be stored in /tmp/pnames.h or the &lt;code&gt;pnames&lt;/code&gt; associative array, but it should be a good enough solution for many cases.&lt;/p&gt;

&lt;p&gt;The following DTrace script, signals.d, shows the use of the &lt;code&gt;pnames&lt;/code&gt; map to list the names of the sender process and the receiver process in a signal operation. It uses predicates to pattern-match the &lt;code&gt;pid_t&lt;/code&gt; argument of &lt;code&gt;kill(2)&lt;/code&gt; and print different messages accordingly. For debugging purposes, it also prints a message whenever a process is forked, execed or terminated. Even if you’re not particularly interested in tracing, you may find out something interesting about your system behaviour. Check it out!&lt;/p&gt;

&lt;script src="https://gist.github.com/bavarious/4726504.js" type="text/javascript"&gt;&lt;/script&gt;</description><link>http://objectivistc.tumblr.com/post/42457883511</link><guid>http://objectivistc.tumblr.com/post/42457883511</guid><pubDate>Wed, 06 Feb 2013 22:55:00 +0000</pubDate><category>dtrace</category></item><item><title>Stack-trace-dumping regular-expression-based symbolic breakpoints in LLDB</title><description>&lt;p&gt;Even though LLDB supports regular expressions when setting breakpoints, Xcode (as of 4.5.2) does not. Regular expressions can be useful when one’s trying to examine how a certain class is used overall.&lt;/p&gt;

&lt;p&gt;In order to use a regular expression in a breakpoint, use the &lt;code&gt;--func-regex&lt;/code&gt; option. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lldb) breakpoint set --func-regex NSLayoutManager
Breakpoint created: 2: regex = 'NSLayoutManager', locations = 312, resolved = 312
(lldb)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Breakpoint number 2 matches NSLayoutManager in 312 locations in this example. Inspecting all locations is easy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lldb) breakpoint list 2
…
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;LLDB stopping at each NSLayoutManager method quickly becomes annoying. In general, if I’m using a regular expression, I want to examine the stack trace to identify which methods have led to the execution of each location and automatically continue execution after the breakpoint is hit. This is easily accomplished in Xcode by editing a breakpoint, setting the Debugger Command action to &lt;code&gt;bt&lt;/code&gt; and checking the Automatically continue after evaluating checkbox.&lt;/p&gt;

&lt;p&gt;Since we’re not using Xcode, here’s how to obtain the same behaviour in LLDB. Recall that the example created a breakpoint numbered 2. We’ll add a three-line command to that breakpoint:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lldb) breakpoint command add 2
Enter your debugger command(s).  Type 'DONE' to end.
&amp;gt; script print "----------"
&amp;gt; bt
&amp;gt; continue
&amp;gt; DONE
(lldb)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Voilà! Now we can resume program execution by issuing &lt;code&gt;continue&lt;/code&gt; and LLDB will gladly dump a stack trace whenever a method/function whose name matches &lt;code&gt;NSLayoutManager&lt;/code&gt; is executed. Note that this breakpoint does not match methods declared in superclasses unless they have been overridden by &lt;code&gt;NSLayoutManager&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to be more strict and, say, avoid C functions whose names contain &lt;code&gt;NSLayoutManager&lt;/code&gt;, e.g. &lt;code&gt;NSLayoutManagerLogDebug&lt;/code&gt;, add &lt;code&gt;[&lt;/code&gt; to the regular expression since Objective-C methods are represented by &lt;code&gt;-[…]&lt;/code&gt; or &lt;code&gt;+[…]&lt;/code&gt;. Recall that &lt;code&gt;[&lt;/code&gt; is a special character in POSIX regular expressions, so it needs to be escaped:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(lldb) breakpoint set --func-regex "\[NSLayoutManager"
Breakpoint created: 2: regex = '\[NSLayoutManager', locations = 311, resolved = 311
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Tailor the regular expression to suit your needs, be prepared for many stack traces in the console log, and happy debugging!&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/40854305239</link><guid>http://objectivistc.tumblr.com/post/40854305239</guid><pubDate>Fri, 18 Jan 2013 18:22:00 +0000</pubDate><category>xcode</category><category>lldb</category></item><item><title>YOU MUST EXEC, a Core Foundation fork safety tale</title><description>&lt;p&gt;Mike Ash has recently published a post about &lt;a href="http://www.mikeash.com/pyblog/friday-qa-2012-01-20-fork-safety.html"&gt;fork safety&lt;/a&gt;. This promptly reminded me of an error message that I, like possibly other developers jumping from Unix into Cocoa, stumbled upon when trying to use &lt;code&gt;fork()&lt;/code&gt; in Mac OS X:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A symbol in all caps is hard to miss or forget.&lt;/p&gt;

&lt;p&gt;What’s happening here is that Core Foundation (aka CF) has detected that a process used a fork-unsafe CF operation, &lt;code&gt;fork()&lt;/code&gt;ed, didn’t &lt;code&gt;exec()&lt;/code&gt;, and now a descendant process is trying to use some fork-unsafe CF functions. But how does CF detect this?&lt;/p&gt;

&lt;p&gt;First, consider the following scenarios.&lt;/p&gt;

&lt;p&gt;Scenario #1&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;A process is created from an executable file that is linked against Core Foundation&lt;/li&gt;
&lt;li&gt;The process executes a fork-unsafe CF operation (for example, using &lt;code&gt;CFRunLoop&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;The process spawns a child process via &lt;code&gt;fork()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The child process tries to execute a fork-unsafe CF operation, and CF complains.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Scenario #2&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;A process is created from an executable file that is linked against Core Foundation&lt;/li&gt;
&lt;li&gt;The process executes fork-safe CF operations&lt;/li&gt;
&lt;li&gt;The process spawns a child process via &lt;code&gt;fork()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The child process tries to execute a fork-unsafe CF operation. CF doesn’t complain because this is the first time an unsafe operation is being executed.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Scenario #3, or #2 with more descendants&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;A process is created from an executable file that is linked against Core Foundation&lt;/li&gt;
&lt;li&gt;The process executes fork-safe CF operations&lt;/li&gt;
&lt;li&gt;The process spawns a child process via &lt;code&gt;fork()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The child process tries to execute a fork-unsafe CF operation. CF doesn’t complain because this is the first time an unsafe operation is being executed.&lt;/li&gt;
&lt;li&gt;The child process spawns another (let’s call it grandchild) process via &lt;code&gt;fork()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The grandchild process tries to execute a fork-unsafe CF operation. CF complains because an ancestor process (in this case, the child process) executed an unsafe operation, so the grandchild process is wading into dangerous waters.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;In summary, from a Core Foundation perspective, there’s potential for trouble when a fork-unsafe CF operation is being executed in a process with an ancestor that has already executed a fork-unsafe CF operation. This can be controlled by two variables:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;p&gt;Whether the current process or any ancestor process has executed a fork-unsafe CF operation. This should be passed along to its children so that they’re able to set the variable below;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whether an ancestor process has executed a fork-unsafe CF operation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Core Foundation does the following:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;p&gt;At startup, &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation = false&lt;/code&gt; and &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation = false&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever a fork-unsafe CF operation is about to be executed, set &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation = true&lt;/code&gt; and test whether &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation == true&lt;/code&gt;, in which case, show an error message and bail out&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever a child process is created via &lt;code&gt;fork()&lt;/code&gt;, the child process tests whether &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation == true&lt;/code&gt; — since variables are copied from the parent process to the child process, at this moment &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation&lt;/code&gt; is a copy of the parent process value. If it’s &lt;code&gt;true&lt;/code&gt;, set &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation = true&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It’s important to note that CF deals with fork-unsafe operations &lt;strong&gt;from a CF perspective only&lt;/strong&gt;. Whether the child process might be executing fork-unsafe operations in the general case is left to the programmer.&lt;/p&gt;

&lt;hr&gt;&lt;p&gt;And now, the implementation details.&lt;/p&gt;

&lt;p&gt;POSIX provides a &lt;a href="http://pubs.opengroup.org/onlinepubs/007904975/functions/pthread_atfork.html"&gt;&lt;code&gt;pthread_atfork()&lt;/code&gt;&lt;/a&gt; function to register callbacks that are called before and after &lt;code&gt;fork()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;prepare&lt;/code&gt; handler is called before &lt;code&gt;fork()&lt;/code&gt;, and the &lt;code&gt;parent&lt;/code&gt; and &lt;code&gt;child&lt;/code&gt; handlers are called after &lt;code&gt;fork()&lt;/code&gt; in the parent and child process, respectively.&lt;/p&gt;

&lt;p&gt;Core Foundation uses atfork handlers to implement the test for fork-unsafe operations previously described:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;p&gt;The library has two variables with static storage duration. Instead of being called &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation&lt;/code&gt; and &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation&lt;/code&gt;, they’re called &lt;code&gt;__CF120290&lt;/code&gt; and &lt;code&gt;__CF120293&lt;/code&gt;, respectively. They’re initialised with &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Core Foundation is a dynamic library and uses the &lt;code&gt;-init&lt;/code&gt; linker parameter to specify a symbol that is run as the first initialiser. The corresponding initialiser function is &lt;code&gt;CFInitialize()&lt;/code&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;CFInitialize()&lt;/code&gt; registers a child atfork handler via &lt;code&gt;pthread_atfork()&lt;/code&gt;. It’s called, erm, &lt;code&gt;__01123__()&lt;/code&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever a fork-unsafe CF operation is about to be executed, set &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation = true&lt;/code&gt; and test whether &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation == true&lt;/code&gt;. If it is, call &lt;code&gt;__THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__()&lt;/code&gt;, which prints the warning messages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever a child process is created via &lt;code&gt;fork()&lt;/code&gt;, the atfork child handler registered by CF is executed. It tests whether &lt;code&gt;current_or_ancestor_process_has_executed_fork_unsafe_CF_operation == true&lt;/code&gt; and, if it is it sets &lt;code&gt;ancestor_process_has_executed_fork_unsafe_CF_operation = true&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;If you want to see some code, go check &lt;a href="http://opensource.apple.com/source/CF/CF-635.15/CFRuntime.c"&gt;CFRuntime.c&lt;/a&gt; and &lt;a href="http://opensource.apple.com/source/CF/CF-635.15/CFInternal.h"&gt;CFInternal.h&lt;/a&gt;. Although the open source edition of Core Foundation does not necessarily reflect the actual code in Core Foundation, it should be similar enough.&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/16187948939</link><guid>http://objectivistc.tumblr.com/post/16187948939</guid><pubDate>Fri, 20 Jan 2012 21:46:00 +0000</pubDate></item><item><title>Testing closures (aka Blocks) for equality</title><description>&lt;p&gt;(In which &lt;a href="https://twitter.com/bbum"&gt;@bbum&lt;/a&gt; slaps me around a bit with a large trout. Or a leg of lamb.)&lt;/p&gt;

&lt;p&gt;A couple of days ago, &lt;a href="https://twitter.com/kongtomorrow"&gt;@kongtomorrow&lt;/a&gt; &lt;a href="https://twitter.com/#!/kongtomorrow/status/142824067978895360"&gt;asked on Twitter&lt;/a&gt; whether there were a function to test the equality of two closures:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Is there a func like Block_equals such that Block_equals(block, Block_copy(block)) is true? I could have sworn Block_equals existed…&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There doesn’t seem to exist such a function in either Block.h (public closure functions) or Block_private.h (private closure functions). In this post, I show an implementation of testing closure equality. Bear in mind that &lt;strong&gt;I am using private details about how closures are currently implemented. This might change in the future and thou shalt never, ever use this information in shipped applications.&lt;/strong&gt; That said, it should work with current compiler &amp;amp; runtime if you need to test closure equality in, for example, unit tests.&lt;/p&gt;

&lt;h4&gt;The Problem&lt;/h4&gt;

&lt;p&gt;Let’s say you’re writing unit tests for an API: you start by writing a closure literal, hand it to the API and, after a few hoops, you want to assert that a certain closure reference is the same as the original closure literal you’ve written.&lt;/p&gt;

&lt;p&gt;If the closure literal has static storage, you can simply compare the corresponding pointers since they’ll always be the same. This won’t work for context-capturing, automatic storage (stack) closures that have been copied to the heap, though.&lt;/p&gt;

&lt;p&gt;The following code illustrates this problem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;typedef void (^closure)(void);

int main(void) {
    int answer = 42;
    closure c = ^{ printf("Hello, I'm a closure: %d\n", answer); };

    closure c0 = Block_copy(c);
    closure c1 = Block_copy(c);
    closure c00 = Block_copy(c0);

    printf("closure = copy(closure)? %d\n", c == c0);
    printf("copy(closure) = another copy(closure)? %d\n", c0 == c1);
    printf("copy(closure) = copy(copy(closure))? %d\n", c0 == c00);

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The program generates the following output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ clang BVBlocksTest.c &amp;amp;&amp;amp; ./a.out
closure = copy(closure)? 0
copy(closure) = another copy(closure)? 0
copy(closure) = copy(copy(closure))? 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, a stack closure has different address from its heap copy, which is expected. Different copies of the same stack closure are also placed in different addresses. A copy of a copy, on the other hand, has the same address — it’s the same (heap) closure and its reference count has been incremented.&lt;/p&gt;

&lt;p&gt;Conceptually, they are all the same closure, though.&lt;/p&gt;

&lt;p&gt;Even though one could restrict oneself to never use a stack closure as a basis for comparison, there might be situations where this is needed.&lt;/p&gt;

&lt;h4&gt;A Solution&lt;/h4&gt;

&lt;p&gt;When the compiler finds a closure literal in a program, e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void (^c)(void) = ^{ printf("Hey\n"); };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;it emits code that includes:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;A structure that describes the closure;&lt;/li&gt;
&lt;li&gt;A function containing the code inside the closure.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;All of this is transparent and there’s no public API to introspect it.&lt;/p&gt;

&lt;p&gt;For the purpose of this post, I use two important facts (which, mind you, are true for the current compiler &amp;amp; runtime but may change in the future):&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Although different closures may have different underlying structures, they all share the same first members;&lt;/li&gt;
&lt;li&gt;There’s a one-to-one relation between closure literals and the underlying functions containing the closure code.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;In &lt;a href="http://www.opensource.apple.com/source/libclosure/"&gt;libclosure&lt;/a&gt;, there’s a file called &lt;a href="http://www.opensource.apple.com/source/libclosure/libclosure-53/BlockImplementation.txt"&gt;BlockImplementation.txt&lt;/a&gt; which describes the ABI used by Apple’s implementation of closures. In particular, it specifies the following structure for closure literals:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;struct {
    void *isa;
    int flags;
    int reserved; 
    void (*invoke)(void *, ...);
    …
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The actual structure varies according to the details of the closure literal but the members listed above are always present. Of particular interest is the &lt;code&gt;invoke&lt;/code&gt; member: it’s a pointer to the function that contains the code specified inside the closure literal.&lt;/p&gt;

&lt;p&gt;With this information, writing a function that tests for closure equality shouldn’t be hard: given two closures, they are equal if their underlying functions are the same:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bool Block_equals(const void *b0, const void *b1) {
    // The following header is (at the moment) shared by all closure literals
    typedef struct {
        void *isa;
        int flags;
        int reserved;
        void *invoke;
    } Block_header;

    const Block_header *h0 = b0;
    const Block_header *h1 = b1;

    // We consider two closures to be the same if their underlying functions
    // are located at the same memory address, i.e., they're the same functions
    return h0-&amp;gt;invoke == h1-&amp;gt;invoke;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The actual code needs to take care of a couple of issues: adjusting types, including ARC bridge casts if needed, and testing for NULL pointers. I’ve published it on GitHub: &lt;a href="https://github.com/bavarious/BVBlock_equals"&gt;&lt;a href="https://github.com/bavarious/BVBlock_equals"&gt;https://github.com/bavarious/BVBlock_equals&lt;/a&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once again, don’t use this in shipped applications. This solution uses details about how the compiler emits code for closures according to the current ABI and it might break in the future. If &lt;a href="https://twitter.com/bbum"&gt;@bbum&lt;/a&gt; ever finds out you’ve used this code in the wild, he’ll slap you around a bit with a large trout. Or a leg of lamb.&lt;/p&gt;

&lt;hr&gt;&lt;p&gt;&lt;strong&gt;Discussion:&lt;/strong&gt; Psy on Freenode and &lt;a href="http://twitter.com/ahruman"&gt;@ahruman&lt;/a&gt; on Twitter  pointed out that this equality test doesn’t take into account the values of imported variables. &lt;a href="http://twitter.com/#!/ahruman/status/143251990305185793"&gt;Here’s @ahruman’s example&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;&lt;code&gt;IntBlock foo(int x) { return ^{ return x; } };
BVBlock_equals(foo(1), foo(2)); // Should be false. ;-)
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case, the two closures have the same closure literal so they are equal in that sense. However, since the values of imported variables aren’t the same, one could argue that they’re in fact different.&lt;/p&gt;

&lt;p&gt;Maybe my function should be called &lt;code&gt;BVBlock_literal_equals()&lt;/code&gt; instead!&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/13708678183</link><guid>http://objectivistc.tumblr.com/post/13708678183</guid><pubDate>Sun, 04 Dec 2011 02:49:00 +0000</pubDate><category>objective-c</category><category>blocks</category><category>closures</category></item><item><title>CFLite, Core Foundation’s little cousin</title><description>&lt;p&gt;&lt;a href="http://opensource.apple.com"&gt;Apple Open Source&lt;/a&gt; is the Web site where Apple publish releases of part of the software that’s bundled in Mac OS X, iOS and the developer tools. One of the available open source components is CFLite, a subset of Core Foundation. In this post, I describe how I’ve built CFLite on Mac OS X and implemented a minor optimisation.&lt;/p&gt;

&lt;h2&gt;Obtaining CFLite&lt;/h2&gt;

&lt;p&gt;Mac OS X open source releases are grouped into OS versions. Lion is currently &lt;a href="http://opensource.apple.com/release/mac-os-x-1072/"&gt;v10.7.2&lt;/a&gt; but its corresponding CF version, CF-635.15, is not available yet. I’ve used &lt;a href="http://opensource.apple.com/tarballs/CF/CF-635.tar.gz"&gt;CF-635 from v10.7.1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;CFLite is licensed under &lt;a href="http://opensource.apple.com/license/apsl/"&gt;Apple’s APSL licence&lt;/a&gt;. Bear in mind that APSL is not as developer-friendly as MIT, BSD or Apache licences. Some other open source components are not APSL-licensed, e.g. libauto (Apache), libclosure (MIT), libdispatch (Apache).&lt;/p&gt;

&lt;h2&gt;Building CFLite&lt;/h2&gt;

&lt;p&gt;README_CFLITE states that the default makefile can be used to build CoreFoundation.Framework on Mac OS X:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;% make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Well, not outside of Apple.&lt;/p&gt;

&lt;p&gt;There are a number of private header files being included in the source files. Commenting them out leads to undefined symbols and removing the corresponding function calls (or even entire functions) may be necessary. This means that the local build will lack some functionality but it should be enough to do some simple tests.&lt;/p&gt;

&lt;p&gt;It’s also necessary to install ICU, International Components for Unicode. Even though there’s a /usr/lib/libicucore.A.dylib file, I wasn’t able to find the corresponding header files under /usr/include or /Developer/SDKs/MacOSX10.7.sdk/usr/include. Luckily ICU is also available at &lt;a href="http://opensource.apple.com/tarballs/ICU/ICU-461.13.tar.gz"&gt;Apple Open Source&lt;/a&gt; and can be built and installed under /usr/local.&lt;/p&gt;

&lt;p&gt;After some rather blunt patching in my eagerness to build CFLite, I came up with &lt;a href="http://cl.ly/2f1P3N3b1k1H0W2t093V/CF-635.patch"&gt;this patch&lt;/a&gt; to make CF-635 build on Mac OS X v10.7.2 with ICU-461.13. If you can improve this patch, let me know.&lt;/p&gt;

&lt;h2&gt;Testing CFLite&lt;/h2&gt;

&lt;p&gt;Makefile builds CFLite as a framework, CoreFoundation.framework, which gets installed under &lt;code&gt;../CF-Root&lt;/code&gt;. In order to make sure that programs are linked against CFLite instead of system Core Foundation, set the &lt;code&gt;DYLD_FRAMEWORK_PATH&lt;/code&gt; environment variable to that &lt;code&gt;CF-Root&lt;/code&gt; directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export DYLD_FRAMEWORK_PATH=/path/to/CF-Root
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You may create a small test program, say:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;CoreFoundation/CoreFoundation.h&amp;gt;

int main(void) {
    CFStringRef s = CFSTR("hey there");
    CFShowStr(s);
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;build it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ clang testcf.c -framework CoreFoundation -o testcf
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and test it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./testcf
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to be sure that the program is using CFLite instead of Core Foundation, you may set the &lt;code&gt;DYLD_PRINT_LIBRARIES&lt;/code&gt; environment variable. This tells dyld, the dynamic linker, to output the location of all libraries that are loaded when running a program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export DYLD_PRINT_LIBRARIES=1
$ ./testcf
…
dyld: loaded: /path/to/CF-Root/CoreFoundation.framework/Versions/A/CoreFoundation
…
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You may unset that variable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ unset DYLD_PRINT_LIBRARIES
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;after having confirmed that dyld is in fact using CFLite.&lt;/p&gt;

&lt;p&gt;Note that a program cannot be linked against both CFLite and system Core Foundation, and CFLite won’t work with Foundation.&lt;/p&gt;

&lt;h2&gt;Mmm, code&lt;/h2&gt;

&lt;p&gt;CFLite being open is useful because it allows developers on other platforms to use a subset of Core Foundation — for example, to parse property lists on Linux. Moreover, it’s handy to be able to read source code and (hopefully!) understand what happens under the hood in Cocoa applications, like &lt;a href="http://ridiculousfish.com/blog/posts/array.html"&gt;the array that wasn’t&lt;/a&gt;. It can also be used to debug some obscure problem or help security researchers spot potential vulnerabilities in Core Foundation.&lt;/p&gt;

&lt;p&gt;And, if one’s feeling adventurous, one might even find opportunities for optimisation.&lt;/p&gt;

&lt;p&gt;Whilst browsing CFLite, I stumbled upon a lookup table in CFURL.c:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;static const unsigned char sURLValidCharacters[] = {
    /* ' '  32 */   0,
    /* '!'  33 */   VALID | UNRESERVED | PATHVALID ,
    /* '"'  34 */   0,
    /* '#'  35 */   0,
    /* '$'  36 */   VALID | PATHVALID ,
    …
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, I like lookup tables. I especially like lookup tables indexed by characters since they look rather neat when initialised with C99 designators:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int array[128] = {
    ['a'] = 1,
    ['b'] = 5,
    ['c'] = 13
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The initialisation of &lt;code&gt;sURLValidCharacters[]&lt;/code&gt; can be rewritten with designators like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;static const unsigned char sURLValidCharacters[128] = {
    [' '] = 0,
    ['!'] = VALID | UNRESERVED | PATHVALID ,
    ['"'] = 0,
    ['#'] = 0,
    ['$'] = VALID | PATHVALID ,
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and it’s possible to drop the 0 assignments because an object with static storage which hasn’t been explicitly initialised by a designator is automatically zeroed out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;static const unsigned char sURLValidCharacters[128] = {
    ['!'] = VALID | UNRESERVED | PATHVALID ,
    ['$'] = VALID | PATHVALID ,
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One may prefer to keep the 0 assignments to make it clear that a character doesn’t have any of the mask bits set.&lt;/p&gt;

&lt;p&gt;The table had to grow 32 bytes because &lt;code&gt;' '&lt;/code&gt; is equivalent to 32, so we end up reserving space for the control characters whose corresponding codes are less than 32. Which, mind you, can be a good thing.&lt;/p&gt;

&lt;p&gt;Here’s one of the functions that use that lookup table:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CF_INLINE Boolean isURLLegalCharacter(UniChar ch) {
    return ( ( 32 &amp;lt;= ch ) &amp;amp;&amp;amp; ( ch &amp;lt;= 127 ) ) ? ( sURLValidCharacters[ ch - 32 ] &amp;amp; VALID ) : false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The original lookup table starts with the space character, code 32. This means that &lt;code&gt;isURLLegalCharacter()&lt;/code&gt; must subtract 32 (&lt;code&gt;ch - 32&lt;/code&gt;) from the character code and it must make sure that this subtraction doesn’t result in a negative value (&lt;code&gt;32 &amp;lt;= ch&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;However, since the lookup table using designators includes all codes from 0 to 127, &lt;code&gt;isURLLegalCharacter()&lt;/code&gt; can be simplified to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CF_INLINE Boolean isURLLegalCharacter(UniChar ch) {
    return ( ch &amp;lt;= 127 ) ? ( sURLValidCharacters[ ch ] &amp;amp; VALID ) : false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One less comparison, no need to use logical and in the conditional operator condition, no subtraction — at the cost of 32 extra bytes in the lookup table. This same optimisation can be applied to the other functions that use &lt;code&gt;sURLValidCharacters&lt;/code&gt;. By doing so, &lt;code&gt;CFURLCreateWithString()&lt;/code&gt; is about 25% faster in my tests (~130-character valid URLs). Your mileage may vary.&lt;/p&gt;

&lt;hr&gt;&lt;p&gt;Thanks to &lt;a href="http://twitter.com/Catfish_Man"&gt;David Smith&lt;/a&gt; for the inspiration to play with CFLite and his comments on an early draft, and to &lt;a href="http://landonf.bikemonkey.org"&gt;Landon Fuller&lt;/a&gt; for calling my attention to the fact that Apple have not used APSL in some recent open source components.&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/12003006590</link><guid>http://objectivistc.tumblr.com/post/12003006590</guid><pubDate>Thu, 27 Oct 2011 22:06:17 +0100</pubDate></item><item><title>Would you please crash my out of scope stack closure?</title><description>&lt;p&gt;Programmers who’ve used Apple/Objective-C closures are probably aware that a literal closure should be copied from the stack to the heap via &lt;code&gt;Block_copy()&lt;/code&gt; or &lt;code&gt;-copy&lt;/code&gt; if it is to be used outside its enclosing block. The &lt;a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/bxUsing.html"&gt;documentation&lt;/a&gt; says so, &lt;a href="http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/"&gt;Bill Bumgarner has a post explaining it&lt;/a&gt;, and &lt;a href="http://opensource.apple.com/source/libdispatch/libdispatch-187.5/dispatch/queue.h"&gt;dispatch/queue.h&lt;/a&gt; even has a warning for this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// The declaration of a block allocates storage on the stack. 
// Therefore, this is an invalid construct:

dispatch_block_t block;

if (x) {
    block = ^{ printf("true\n"); };
} else {
    block = ^{ printf("false\n"); };
}
block(); // unsafe!!!

// What is happening behind the scenes:

if (x) {
    struct Block __tmp_1 = ...; // setup details
    block = &amp;amp;__tmp_1;
} else {
    struct Block __tmp_2 = ...; // setup details
    block = &amp;amp;__tmp_2;
}

// As the example demonstrates, the address of a stack variable is 
// escaping the scope in which it is allocated. That is a classic C bug.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, this sample program based on the warning on dispatch/queue.h doesn’t crash:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main(void) {
    void (^closure)(void);

    int n;
    scanf("%d", &amp;amp;n);

    if (n) {
        closure = ^{ printf("non-zero\n"); };
    }
    else {
        closure = ^{ printf("zero\n"); };
    }

    closure();

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let’s investigate why.&lt;/p&gt;

&lt;h3&gt;Terminology&lt;/h3&gt;

&lt;p&gt;In C, a &lt;em&gt;block&lt;/em&gt; is a &lt;code&gt;{}&lt;/code&gt;-delimited group of declarations and statements that form a syntactical unit. Identifiers declared inside a block have scope/visibility limited to that block and the objects corresponding to automatic/local variables have storage duration/lifetime limited to that block. Even though C99 uses the term &lt;em&gt;scope&lt;/em&gt; for visibility only, it’s not uncommon for scope to mean lifetime scope as well.&lt;/p&gt;

&lt;p&gt;Since &lt;em&gt;block&lt;/em&gt; already has a meaning in C, I prefer to use the term &lt;em&gt;closure&lt;/em&gt; for Apple/Objective-C blocks, much like &lt;a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1451.pdf"&gt;Apple’s N1451 submission of closures to ISO/IEC JTC1/SC22/WG14&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Global and stack closures&lt;/h3&gt;

&lt;p&gt;A global closure is a closure that exists throughout the execution of the program, e.g . &lt;code&gt;static&lt;/code&gt; or global (file-level visibility) ones — in C99 terminology, that’s called &lt;em&gt;static storage duration&lt;/em&gt; — and therefore it does not need to be copied to the heap. In the code below, all closures are global:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int someInt = 21;
void (^closure0)(void) = ^{ printf("hey there\n"); };
void (^closure1)(void) = ^{ printf("it's %d\n", someInt); };

void someFunction() {
    static void (^closure2)(void) = ^{ printf("hola"); };
    closure0();
    closure1();
    closure2();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When generating code, the compiler sets the &lt;code&gt;isa&lt;/code&gt; pointer of the corresponding closure structures to &lt;code&gt;_NSConcreteGlobalBlock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, if a closure doesn’t need to capture its local context then the compiler turns it into a global closure. &lt;a href="http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/"&gt;Bill writes&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;(…) when the compiler detects that a block is effectively constant because it captures no state, the compiler will create a static global for said block and copying is a no-op. Implementation detail. Now you know, but forget it when writing code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void someFunction() {
    void (^closure)(void) = ^{ printf("salut\n"); };
    closure();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Apple Clang 2.1 considers that closure as being global:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;leaq    ___block_literal_global(%rip), %rax
…
___block_literal_global:
    .quad   __NSConcreteGlobalBlock
…
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we want to crash a stack closure being referenced outside of its scope, we’d better make sure it is truly a stack one. Making it reference a variable in the current context should do the trick.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

void someFunction(int n) {
    void (^closure)(void) = ^{ printf("%d\n", n); };
    closure();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;yields:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;leaq    ___block_descriptor_tmp(%rip), %rcx
leaq    ___someFunction_block_invoke_0(%rip), %rdx
movq    __NSConcreteStackBlock@GOTPCREL(%rip), %rsi
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;so we should be good to go.&lt;/p&gt;

&lt;h3&gt;Stack frame and object lifetime&lt;/h3&gt;

&lt;p&gt;In a C function or Objective-C method, its &lt;a href="http://en.wikipedia.org/wiki/Call_stack#Structure"&gt;stack frame&lt;/a&gt; is a stack memory range where local/automatic variables used throughout the function are stored. When the function returns, its stack frame shouldn’t be considered to keep valid objects used in that function.&lt;/p&gt;

&lt;p&gt;Something similar happens to blocks in C. A local, non-&lt;code&gt;static&lt;/code&gt; variable has its lifetime restricted to its enclosing block. In C99 terminology, the object associated to such a variable has &lt;em&gt;automatic storage duration.&lt;/em&gt; Attempting to reference said object outside of its enclosing block, i.e., after it’s reached the end of its lifetime, is an undefined behaviour.&lt;/p&gt;

&lt;p&gt;Consider the following program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;time.h&amp;gt;

int main(void) {
    void *p0, *p1;

    // Block 0
    {
        time_t n0 = time(NULL);
        p0 = &amp;amp;n0;
    }

    // Block 1
    {
        time_t n1 = time(NULL);
        p1 = &amp;amp;n1;
    }

    printf("p0 = %p\np1 = %p\n", p0, p1);

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Variables &lt;code&gt;n0&lt;/code&gt; and &lt;code&gt;n1&lt;/code&gt; have scope (visibility) limited to their enclosing blocks and, because of their automatic storage duration, the lifetime of the corresponding objects is limited to their enclosing blocks as well. When the program leaves Block 0, the function shouldn’t rely on the memory address of &lt;code&gt;n0&lt;/code&gt; pointing to a valid object. In fact, the compiler may choose to reuse the memory used by Block 0 — conceptually, we could consider that all automatic variables inside Block 0 are pushed onto the stack at the beginning of the block and then popped out of the stack at the end of the block.&lt;/p&gt;

&lt;p&gt;I get the following output when running the program above after compiling it with GCC 4.6 (binaries available at the &lt;a href="http://hpc.sourceforge.net"&gt;HPC project&lt;/a&gt;) with an optimisation level (&lt;code&gt;-O&lt;/code&gt;) greater than 0:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./a.out
p0 = 0x7fff6f8a9c08
p1 = 0x7fff6f8a9c08
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;thus GCC 4.6 is effectively reusing the memory used by Block 0.&lt;/p&gt;

&lt;p&gt;When compiling the same program with GCC 4.6, &lt;code&gt;-O0&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./a.out
p0 = 0x7fff6f0cbbf0
p1 = 0x7fff6f0cbbe8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;addresses don’t get reused.&lt;/p&gt;

&lt;p&gt;With both Apple Clang 2.1 and LLVM-GCC 4.2.1, addresses don’t get reused regardless of &lt;code&gt;-O&lt;/code&gt; optimisation level. Specifying a block for &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;for&lt;/code&gt; structures seemingly doesn’t change that.&lt;/p&gt;

&lt;h3&gt;(Not) Making a stack closure crash&lt;/h3&gt;

&lt;p&gt;In summary, there are two circumstances that influence an out of scope stack closure crash inside a single function:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;&lt;p&gt;It’s not enough for the closure to be literal and non-&lt;code&gt;static&lt;/code&gt; for it to be a stack closure: it must capture and reference a variable in the current context. This is valid as of Apple Clang 2.1 but it may change in the future. Being an implementation detail, you shouldn’t rely on it;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The stack memory used by a block must be reused and rewritten with data that do not represent a valid closure.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The problem with item 2 is that neither Apple Clang 2.1 nor LLVM-GCC 4.2.1 seem to reuse the stack memory used by a block whose execution has ended. This means that every object inside a function will be assigned a unique memory address, hence it will never be overwritten inside that function unless the programmer explicitly does that. And whilst stock GCC 4.6 is more aggressive and can reuse memory used inside a block, it doesn’t support closures.&lt;/p&gt;

&lt;p&gt;If you have an example where Clang emits conservative stack usage code, &lt;a href="http://twitter.com/bavarious"&gt;drop me a line&lt;/a&gt;! I’m also curious as to which combination of GCC flags applied to &lt;code&gt;-O0&lt;/code&gt; makes it emit conservative stacks. &lt;code&gt;-fconserve-stack&lt;/code&gt; is not enough by itself.&lt;/p&gt;

&lt;hr&gt;&lt;p&gt;Thanks to Sedate Alien for the &lt;a href="http://stackoverflow.com/q/7439295/557219"&gt;inspiration for this post&lt;/a&gt;.&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/10523983325</link><guid>http://objectivistc.tumblr.com/post/10523983325</guid><pubDate>Thu, 22 Sep 2011 19:06:00 +0100</pubDate><category>objective-c</category></item><item><title>A CHOCKING MYSTERY: po [NSNumber numberWithBool:NO] outputs 1</title><description>&lt;p&gt;&lt;a href="https://twitter.com/chockenberry"&gt;Craig Hockenberry&lt;/a&gt; posted &lt;a href="https://twitter.com/chockenberry/status/103220126320566272"&gt;this&lt;/a&gt; on Twitter:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;(gdb) po [NSNumber numberWithBool:NO]&lt;/p&gt;
  
  &lt;p&gt;1&lt;/p&gt;
  
  &lt;p&gt;Does not inspire confidence…&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://twitter.com/ahruman"&gt;Jens Ayton&lt;/a&gt; cleverly &lt;a href="https://twitter.com/ahruman/status/103220283216891904"&gt;noted&lt;/a&gt; that &lt;code&gt;NO&lt;/code&gt; is a macro, so where would GDB be getting a value from?&lt;/p&gt;

&lt;p&gt;A few comments ensued, including speculations about tagged pointers, until &lt;a href="https://twitter.com/0xced"&gt;Cédric Luthi&lt;/a&gt; mentioned &lt;a href="https://twitter.com/0xced/status/103223380265799680"&gt;how to correctly handle it&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;That’s unfortunate but Foundation exports YES and NO symbols, the correct way to use it is *((char*)NO)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I wasn’t aware of that, so I decided to take a look.&lt;/p&gt;

&lt;p&gt;In Objective-C source code using Apple’s runtime, the &lt;code&gt;BOOL&lt;/code&gt; type is defined as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;typedef signed char     BOOL;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with corresponding boolean values:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#define YES             (BOOL)1
#define NO              (BOOL)0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means that a boolean value is a signed, 1-byte character, with 1 representing a true value and 0 representing a false value (actually, the C language allows any non-zero value to represent a true value).&lt;/p&gt;

&lt;p&gt;In source code, any reference to &lt;code&gt;YES&lt;/code&gt;  or &lt;code&gt;NO&lt;/code&gt; is converted to &lt;code&gt;(BOOL)1&lt;/code&gt; or &lt;code&gt;(BOOL)0&lt;/code&gt; by the preprocessor.&lt;/p&gt;

&lt;p&gt;However, GDB does not invoke the preprocessor. This means that in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;po [NSNumber numberWithBool:NO]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;NO&lt;/code&gt; must be obtained from somewhere else.&lt;/p&gt;

&lt;p&gt;In fact:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p NO
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;outputs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$1 = {&amp;lt;text variable, no debug info&amp;gt;} 0x9374bc6a73a0a1 &amp;lt;NO&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;hence there’s a symbol called &lt;code&gt;NO&lt;/code&gt; that was loaded onto the address 0x9374bc6a73a0a1. Since the address is different from 0, &lt;code&gt;+numberWithBool:&lt;/code&gt; returns an &lt;code&gt;NSNumber&lt;/code&gt; representing a true value.&lt;/p&gt;

&lt;p&gt;Inspecting the Foundation framework, we can see:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ nm Foundation | grep NO
…
00000000002630a1 S _NO
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;that Foundation indeed exports a symbol called &lt;code&gt;NO&lt;/code&gt;. The &lt;code&gt;S&lt;/code&gt; flag means that the symbol is in a section other than text, data or bss. Running:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ nm -m Foundation | grep NO
…
00000000002630a1 (__TEXT,__const) external _NO
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;shows that &lt;code&gt;NO&lt;/code&gt; is in the &lt;code&gt;(__TEXT,__const)&lt;/code&gt; section. Inspecting the contents of that section:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ nm -s __TEXT __const Foundation | sort
…
00000000002630a0 S _YES
00000000002630a1 S _NO
00000000002630a2 s ___NSOperationPrios
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;we can see that the symbols &lt;code&gt;YES&lt;/code&gt; and &lt;code&gt;NO&lt;/code&gt; use only one byte. This matches the previous definition that &lt;code&gt;BOOL&lt;/code&gt; is one-byte long, so it’s reasonable to expect that those symbols point to a 1-byte memory region whose contents are either 1 or 0.&lt;/p&gt;

&lt;p&gt;If we cast the &lt;code&gt;NO&lt;/code&gt; symbol as a pointer to &lt;code&gt;signed char&lt;/code&gt; and then dereference it to obtain its value, we get:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p *((signed char *)NO)
$1 = 0 '\0'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As expected, we get 1 for &lt;code&gt;YES&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p *((signed char *)YES)
$1 = 1 '\001'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So the correct way to obtain &lt;code&gt;NO&lt;/code&gt;, with the same value and representation used in an Objective-C program, is to use the expression:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;*((signed char *)NO)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;as pointed out by &lt;a href="https://twitter.com/0xced"&gt;Cédric Luthi&lt;/a&gt;. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) po [NSNumber numberWithBool:*((signed char *)NO)]
0
(gdb) po [NSNumber numberWithBool:*((signed char *)YES)]
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Even though this mimics what happens in Objective-C source code, it’s not strictly necessary. Knowing that &lt;code&gt;NO&lt;/code&gt; is 0 and &lt;code&gt;YES&lt;/code&gt; is 1 allows us to write the much simpler expressions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) po [NSNumber numberWithBool:0]
0
(gdb) po [NSNumber numberWithBool:1]
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;without having to reference the symbols exported by Foundation.&lt;/p&gt;

&lt;p&gt;Alternatively, Core Foundation exports two constants — &lt;code&gt;kCFBooleanTrue&lt;/code&gt; and &lt;code&gt;kCFBooleanFalse&lt;/code&gt; — that represent the two possible boxed boolean values:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) po (id)kCFBooleanFalse
0
(gdb) po (id)kCFBooleanTrue
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In fact, whenever Foundation/Core Foundation returns a boxed boolean value, it returns either &lt;code&gt;kCFBooleanTrue&lt;/code&gt; or &lt;code&gt;kCFBooleanFalse&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p/a (int)[NSNumber numberWithBool:0]
$1 = 0x7f270f20
(gdb) p/a (int)kCFBooleanFalse
$2 = 0x7f270f20
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(gdb) p/a (int)[NSNumber numberWithBool:1]
$1 = 0x7f270eb0
(gdb) p/a (int)kCFBooleanTrue
$2 = 0x7f270eb0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note how the objects returned by &lt;code&gt;+numberWithBool:&lt;/code&gt; have the same addresses as those Core Foundation constants. After Core Foundation is loaded and throughout the execution of a program, there are exactly two &lt;code&gt;NSNumber&lt;/code&gt; objects representing true and false.&lt;/p&gt;

&lt;p&gt;Also note that the addresses are even integers, hence they aren’t tagged pointers. Even though they are of type &lt;code&gt;NSNumber&lt;/code&gt;, they are not treated as (fast-pathed) integers by Core Foundation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; Since GDB optimises its symbol lookup table, code that doesn’t reference &lt;code&gt;NSNumber&lt;/code&gt; might yield a &lt;code&gt;No symbol "NSNumber" in current context.&lt;/code&gt; error message. If that happens, replace &lt;code&gt;NSNumber&lt;/code&gt; with &lt;code&gt;NSClassFromString(@"NSNumber")&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;po [NSClassFromString(@"NSNumber") numberWithBool:NO]
&lt;/code&gt;&lt;/pre&gt;</description><link>http://objectivistc.tumblr.com/post/8992822737</link><guid>http://objectivistc.tumblr.com/post/8992822737</guid><pubDate>Tue, 16 Aug 2011 12:10:00 +0100</pubDate><category>objective-c</category></item><item><title>Tagged pointers and fast-pathed CFNumber integers in Lion</title><description>&lt;p&gt;Tagged pointers are a new feature in Lion and the OS X v10.7 SDK. They provide an alternative representation of objects based on the fact that not every integer can represent the memory address of an arbitrary Objective-C object since allocators return 16-byte aligned addresses.&lt;/p&gt;

&lt;p&gt;In Lion, the Objective-C runtime and Core Foundation tag pointers as follows:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;p&gt;Every tagged pointer has its lowest bit set, hence tagged pointers are odd integers;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next 3 bits (from lowest to highest) define the tagged object class. At the moment, there are classes for integers, managed objects, and dates;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next 4 bits are for type information specific to the tagged object class.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Thus the lowest eight bits in the memory address are used as tag metadata, and the remaining 24 or 56 bits are used as payload.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CFNumber&lt;/code&gt; (and, consequently, &lt;code&gt;NSNumber&lt;/code&gt;) takes advantage of tagged pointers for integers — if an integer can fit in the payload of a tagged pointer then no actual &lt;code&gt;CFNumber&lt;/code&gt; is created. Instead, the memory address represents the integer number itself according to the following layout:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   6         5         4         3         2         1         0
3210987654321098765432109876543210987654321098765432109876543210
........................................................xxxx0011
|                                                       |   |  +-- (1 bit) always 1 for tagged pointers
|                                                       |   +----- (3 bits) 001 is the tagged object class for integers
|                                                       +--------- (4 bits) for integers, xxxx is either:
|                                                                           0000 for 8-bit integers,
|                                                                           0100 for 16-bit integers,
|                                                                           1000 for 32-bit integers,
|                                                                           1100 for 64-bit integers
+------------------------------------------------------------------ (56 bits) payload with the actual integer value
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Given a tagged pointer, obtaining its underlying integer value is as simple (and fast) as a shift right by 8 bits of data that’s already stored in a register. Preliminary tests by &lt;a href="http://nszombie.com"&gt;Joshua Weinberg&lt;/a&gt; yielded a speedup of 2.42 in both creating &lt;code&gt;NSNumber&lt;/code&gt; objects and reading its integer value.&lt;/p&gt;

&lt;p&gt;Since there are no actual &lt;code&gt;CFNumber/NSNumber&lt;/code&gt; objects, there’s no need for memory management.&lt;/p&gt;

&lt;p&gt;In order for a tagged pointer to behave as an object, the Objective-C runtime keeps an isa table (&lt;code&gt;_objc_tagged_isa_table[]&lt;/code&gt;) that maps the four lowest bits of a tagged pointer (effectively, its tagged object class) to the isa pointer that’s expected for Objective-C objects. This table is used by &lt;code&gt;object_getClass()&lt;/code&gt; (and its corresponding private function) so that the correct isa is returned when querying tagged pointers. Note that directly accessing the isa pointer via &lt;code&gt;-&amp;gt;isa&lt;/code&gt; will break in this case. Developers should use &lt;code&gt;object_getClass()&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;Warning: this post is based upon CF-635. It is internal information, and subject to change in future releases.&lt;/p&gt;

&lt;p&gt;/Bavarious&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/7872364181</link><guid>http://objectivistc.tumblr.com/post/7872364181</guid><pubDate>Thu, 21 Jul 2011 04:59:00 +0100</pubDate><category>objective-c</category></item><item><title>Name spaces in Objective-C</title><description>&lt;p&gt;One thing that might not be immediately clear for newcomers to Objective-C is the name spaces for the varying Objective-C constructs. For instance, is it possible for a class to have the same name as a protocol?&lt;/p&gt;

&lt;p&gt;Let’s start by enumerating some of the &lt;em&gt;elements that can be named&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;C types&lt;/li&gt;
&lt;li&gt;C variables&lt;/li&gt;
&lt;li&gt;C functions&lt;/li&gt;
&lt;li&gt;C structure members&lt;/li&gt;
&lt;li&gt;Objective-C classes&lt;/li&gt;
&lt;li&gt;Objective-C class methods&lt;/li&gt;
&lt;li&gt;Objective-C instance methods&lt;/li&gt;
&lt;li&gt;Objective-C instance variables&lt;/li&gt;
&lt;li&gt;Objective-C declared properties&lt;/li&gt;
&lt;li&gt;Objective-C protocols&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;C elements&lt;/h3&gt;

&lt;p&gt;Since Objective-C is a strict superset of C, the standard rules for C name spaces apply. Types, variables, and functions share the same name space if they are in the same syntactic context (e.g. global vs. block). For instance, it is not possible to have a global function with the same name as a globally defined type. However, it is possible to have a global function with the same name as a type declared inside that function since they are in different contexts.&lt;/p&gt;

&lt;p&gt;The following code is valid C:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int main()
{
    typedef int main;
    {
        main main = 42;
        printf("%d\n", main);
    }
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;whereas the following code isn’t:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int main()
{
    typedef int main;
    main main = 42;
    printf("%d\n", main);
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;because the type &lt;code&gt;main&lt;/code&gt; is in the same syntactic context as the local variable &lt;code&gt;main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each C structure (or union) has its own name space.&lt;/p&gt;

&lt;h3&gt;Objective-C Elements&lt;/h3&gt;

&lt;p&gt;An Objective-C class is a C type, hence the same rules for C types apply. The following code isn’t valid:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;typedef char *Name;

@interface Name : NSObject
@end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;since both the type and the class are defined in the same syntactic context, whereas the following code is valid:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@interface Name : NSObject
@end

int main ()
{
    typedef char *Name;
    Name name = "objc";
    printf("%s\n", name);
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;Name&lt;/code&gt; can refer to both the global Objective-C class and the C &lt;code&gt;typedef char *&lt;/code&gt;, and the compiler uses whatever is closest to the syntactic context where the identifier is used.&lt;/p&gt;

&lt;p&gt;Other Objective-C elements are not C types. Protocols and categories have their own name space and, for a given class, instance variables, declared properties, class methods, and instance methods have each their own name space. This means that it is possible to have a protocol, a category, a class method, an instance method, a declared property, and an instance variable with the same name. This also means that each of these elements can have the same name as an Objective-C class or C elements.&lt;/p&gt;

&lt;p&gt;The following code — where a protocol, a class, an instance variable, a declared property, a class method, and an instance method share the same name — is valid:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@protocol Name
@end

@interface Name &amp;lt;Name&amp;gt; {
    Name *Name;
}
@property (nonatomic, assign) Name *Name;
- (Name *)Name;
+ (Name *)Name;
@end
&lt;/code&gt;&lt;/pre&gt;

&lt;hr&gt;&lt;p&gt;&lt;em&gt;Props to David Smith for pointing out that class methods and instance methods are in different name spaces, and to Daniel DeCovnick for pointing out that categories are also in a different name space.&lt;/em&gt;&lt;/p&gt;</description><link>http://objectivistc.tumblr.com/post/3340816080</link><guid>http://objectivistc.tumblr.com/post/3340816080</guid><pubDate>Thu, 17 Feb 2011 06:00:00 +0000</pubDate><category>objective-c</category></item></channel></rss>
