<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Doing better</title>
	<atom:link href="http://www.longacre-scm.com/blog/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.longacre-scm.com/blog</link>
	<description>Opinions on CM, software development, and process automation from Longacre.</description>
	<lastBuildDate>Wed, 15 Dec 2010 04:21:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>You&apos;re doing it wrong:  GNot Invented Here</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/12/youre-doing-it-wrong-gnot-invented-here</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/12/youre-doing-it-wrong-gnot-invented-here#comments</comments>
		<pubDate>Wed, 15 Dec 2010 03:11:37 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Practice]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[make]]></category>
		<category><![CDATA[sunos]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/index.php/2010/12/youre-doing-it-wrong-gnot-invented-here</guid>
		<description><![CDATA[A while ago, I was working at a company in South Florida. And I happened to be reading the manual for their build tool, which was called &#8216;make&#8217;, and it mentioned a special syntax available for cases where more than one output file could be built by a single application of a rule.
The example most [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I was working at a company in South Florida. And I happened to be reading the manual for their build tool, which was called &#8216;make&#8217;, and it mentioned a special syntax available for cases where more than one output file could be built by a single application of a rule.</p>
<p>The example most programmers will understand today is with a yacc-like tool (or lex, for that matter):</p>
<p><tt>y.tab.c y.tab.h : whatever.yacc</tt></p>
<p><tt> &#160; &#160; &#160; &#160;$(YACC) $(YFLAGS) $&lt;</tt></p>
<p>If you&#8217;ve ever used yacc, or the GNU equivalent, <i>bison,</i> or any other code generator, you&#8217;ll understand the pattern. There&#8217;s a set of input files, usually either just one, or one plus some other files generated by a lexer generator. And the input files are read by the code generator tool, and the code generator tool produces a bunch of different output files <i>all at the same time.</i></p>
<p><span id="more-110"></span></p>
<p>The syntax that this &#8216;make&#8217; tool I was reading about used to indicate this was a plus sign:</p>
<p><tt>y.tab.c + y.tab.h : whatever.yacc</tt></p>
<p>And that plus sign was enough to let the &#8216;make&#8217; tool know what was going on with the simultaneous generation of both files.</p>
<p>The company in question was not a client. It was my second serious job after college. I started working there in the (late) 1980&#8217;s. And the &#8216;make&#8217; tool in question was <i>make,</i> as provided with SunOS 3 and 4. (<i>Not </i>Solaris. That came later.)</p>
<h2>Yeah, so?</h2>
<p>I told that story for a reason: to point out that this somewhat niche-market problem has been solved, in a widely-available system, using a trivial syntax enhancement. The solution has been around for more than 20 years. And GNU make still gets it wrong.</p>
<p>The latest GNU make version provides a feature that almost solves the same problem. Except that it doesn&#8217;t. And that &#8220;feature&#8221; is a hack.</p>
<p>It&#8217;s documented in the manual, but not mentioned in a header in the contents, so you have to just know, or have someone give you a link. Google won&#8217;t help you find it. (It&#8217;s <a href="http://www.gnu.org/software/make/manual/make.html#Pattern-Examples">here.</a>)</p>
<p>And of course the syntax used for the GNU make not-quite-but-maybe-almost hack is: two targets separated by spaces.</p>
<p>That&#8217;s right, the GNU version looks like:</p>
<p><tt>a b : input</tt></p>
<p>Which you might find confusing, because the GNU make syntax for two targets that <i>do not </i>all get regenerated at the same time, but instead require the recipe to be executed once for each output looks like:</p>
<p><tt>a b : input</tt></p>
<p>See the difference? Of course not, because at this level of abstraction there is no difference. The rules look exactly the same.</p>
<p>If you don&#8217;t know the secret &#8211; which is that the run-once or run-multiple-times behavior depends on whether the rule is a <i>pattern rule </i>or not &#8211; then you&#8217;re SOL, because there&#8217;s no visible indicator.</p>
<p>It&#8217;s worth pointing out that there&#8217;s a method to the madness of the GNU make weenies. The justification, if I were dumb enough to ask for one, would be something like this: the <i>only time </i>this kind of thing ever arises (in C) is when you&#8217;re using yacc or rpcgen. (Rpcgen is the reason the Sun guys added the rule, I&#8217;m fairly sure.) And yacc/rpcgen always get used in such a way that the filenames always have a common root. So making this incredibly lame hack makes total sense, because nobody ever does this, but if they did they&#8217;d be happy it was this way.</p>
<h2>Bah, humbug!</h2>
<p>I don&#8217;t do a lot of C programming any more. There are other languages that are generally much more suited to what I&#8217;m trying to do. Some of those languages are &#8220;static&#8221; languages, like C++, Java, and D. And some of those languages are &#8220;dynamic&#8221; languages, like sh, perl, and scheme.</p>
<p>But in <i>both </i>camps, you&#8217;ll find languages that use directory names and file names to reflect the classes, modules, packages, units, or whatever-it&#8217;s-called defined inside them.</p>
<p>This is important because if I&#8217;m going to generate code, I want to be free to generate code using file and directory names that are meaningful to, and constrained in, the context of the programming language I&#8217;m actually using.</p>
<p>I really, really, really don&#8217;t want to have to conform to a naming convention inspired by a tool I&#8217;m not using in a language I don&#8217;t care about that is the basis for a syntax hack in make!</p>
<p>Ultimately, I think this is an example of a couple of things. First, that the GNU make guys are suffering a vision problem because they&#8217;re focused on building a core set of applications, using a small set of languages.</p>
<p>Second, I think they&#8217;re afraid to admit that other people, when faced with a similar set of problems as them, have come up with useful solutions. Back when GNU make got started, SunOS was the dominant Unix. And the guys at Sun were doing a fine job of carrying forward the tool suite. But heavens! For GNU to copy a useful feature of a popular tool would be to admit that we aren&#8217;t the sole source of clever ideas. Oh, noes!</p>
<p>Thus, we have the open source curse: Gnot Invented Here syndrome. </p>
<p>And also thus &#8211; more of a pain in my tuchus, thus &#8211; we have a version of GNU make that forces me to jump through a bunch of hoops to accomplish something that I could have done in <i>two lines </i>back in 1988. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/12/youre-doing-it-wrong-gnot-invented-here/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crossing Over</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/10/crossing-over</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/10/crossing-over#comments</comments>
		<pubDate>Mon, 18 Oct 2010 05:08:48 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=106</guid>
		<description><![CDATA[Recently I&#8217;ve been doing a lot of work at a very low level. Not &#8220;should I use a primitive type or a class&#8221; low, but more like &#8220;how many cycles will this take?&#8221; low.
Today I was chatting with some folks on IRC, and the subject of binary searching came up. Now, I don&#8217;t know if [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been doing a lot of work at a very low level. Not &#8220;should I use a primitive type or a class&#8221; low, but more like &#8220;how many cycles will this take?&#8221; low.</p>
<p>Today I was chatting with some folks on IRC, and the subject of binary searching came up. Now, I don&#8217;t know if this is going to surprise you, but in the last few years there has been some &#8220;movement&#8221; in the performance arena as far as thing we just &#8220;know&#8221;. It turns out, for example, that the &#8220;qsort&#8221; function isn&#8217;t the fastest horse in the race any more. And it turns out that <em>bsearch</em> is getting a little long in the tooth, too.</p>
<p><em>&#8220;Mon dieu! How can this be?,&#8221;</em> you ask. Well, it&#8217;s templates.</p>
<p><span id="more-106"></span></p>
<p>The C++ guys apparently listened to Philip Greenspun, and decided that the way to better performance was to build a lisp interpreter into the compiler. That lisp interpreter is called &#8220;template metaprogramming&#8221; (TMP), and the languages like C++ that support TMP can use it in some surprising ways.</p>
<p>The key thing to realize here is that there&#8217;s a trade-off involved. The old C standard library functions are &#8211; no question &#8211; about as fast as they can be. But the TMP guys came to the realization that the library functions were just that: functions. And so calling qsort or bsearch means passing in a callback function that takes a couple of pointers and compares whatever they point to.</p>
<p>This is very generic, but it comes at a fairly high cost in terms of run-time. Calling a function to make a comparison is a huge performance lose, especially on a fast CPU with a good sized cache. All that stuff you&#8217;ve heard about cache misses and branch prediction and what-not? Well, it becomes relevant if you have to call through a pointer to the compare function, and it gets doubly relevant if your compare function turns around and calls some other function &#8211; like <em>strcmp.</em></p>
<p>So the template version of all this uses the same logic as the hoary old C standard library chestnuts, but it uses templates to dynamically build a special-purpose function for doing just exactly the Quicksort or binary search that you need to do right now.</p>
<p>The obvious down side? Each place where you invoke qsort or bsearch generates a different function. Big code bloat here, folks.</p>
<p>The up side? Breaking through the &#8220;call a function to compare two items&#8221; barrier. For small types &#8211; like int, float, etc. &#8211; and for &#8220;fully equipped&#8221; types like strings, there will be enough code laying around that the compiler can in-line some, if not most or all, of the comparison logic. Comparing strings will always require looping through arrays. But comparing two structs? That&#8217;s easy to inline. Comparing two ints? Please!</p>
<h2>Crossing Over</h2>
<p>So with this in mind, I set out to find the &#8220;cross over&#8221; point. Put simply, where do the performance curves of linear seach (lsearch) and binary search (bsearch) cross? That is, for N = how many items does it make more sense to use a binary search than to iterate?</p>
<p>Now, here&#8217;s what we know about bsearch: it has performance characteristic <em>O( log n ).</em>  And we know that lsearch has performance characteristic <em>O( n ).</em> But what does that mean, exactly?</p>
<p>First, every routine has some overhead. When a subroutine is called, you push some information on the stack, initialize some variables, and then do your work. And later, you takes stuff off the stack, free your resources, and return. That&#8217;s overhead.</p>
<p>Next, in the process of doing the actual work, you have to perform interim computations. For example, linear search basically requires some mechanism for iterating through the array you are searching. And that&#8217;s going to be a pointer or an integer or something. Updating that iterator is part of the structural cost of the algorithm. A bsearch algorithm typically involves computing (low + (high-low)/2). That&#8217;s the middle point for the search, and that&#8217;s a part of the structural cost.</p>
<p>Those structural costs can be high, sometimes. So saying that an O( log n ) function is faster than an O( n ) function automatically implies a caveat, <em>so long as n is big enough that the structural and overhead costs get washed out. </em>And that&#8217;s why I was curious about the cross-over number. Because with templates, the call-a-function-through-a-pointer structural cost is gone. And so my understanding of where the lines cross is no longer valid.</p>
<h2>Great! Another &#8216;D&#8217; in programming&#8230; </h2>
<p>The language in question isn&#8217;t C++, though. It&#8217;s D. Now, D is a lousy name for a programming language, just like C is. Because Google is not your friend if you&#8217;re searching for &#8220;D&#8221;. Nor is &#8220;file.d&#8221; going to be a win, either, because that&#8217;s the default name for dependency files in a lot of build systems. So D wasn&#8217;t the best possible choice of names, and it can be hard to find info about it. To save you some grief, have a look at http://www.digitalmars.com/d &#8211; that&#8217;s the &#8220;official&#8221; D site, if there is such a thing. (For myself, I&#8217;ve taken to using &#8220;PL/D&#8221; as an abbreviation.)</p>
<p>PL/D version 2.0 supports templates. So here&#8217;s bsearch, as a can-find type function:</p>
<pre>
bool bsearch( T, alias cmp = "a < b" )(T[ ] array, T key )
{
	int left = 0;
	int right = array.length;

	while( left + 1 < right ) {

		int middle = ( left + right ) / 2;
		T am = array[ middle ];

		if( binaryFun!( cmp )( key, am ) ) {

			right = middle;
		}
		else {

			left = middle;
		}
	}

	// return a[0] == key
	return ! binaryFun!( cmp )( key, array[ 0 ] )
		&#038;&#038; ! BinaryFun!( cmp )( array[ 0 ], key );
}
</pre>
<p>Now I know that this version doesn't do early exit. But it's a little more readable to folks who might be new to the whole template thing, especially with PL/D. One thing I like more about D than C++ is that templates are instantiated like foo!( T )( a, b ) instead of foo< T >( a, b ). If nothing else, it's more html friendly.<br />
I won't explain all the neat-o features of the language here - see the link above for that. I will point out that this version generates a binary function using "a < b" by default, and generates a direct call to that function in the comparisons. As a result of the direct (instead of indirect - through a pointer) function calls, the compiler can inline them, resulting in the comparison being made right in the function itself.<br />
The testbed was an array of randomly-generated integers. The driver code looked like this:</p>
<pre>
	// ========================
	Thread.sleep( 1 );
	timer.start();

	foreach (iter; 0 .. ITERATIONS )
		bsearch(a[], cast(int)(rand() % ELEMENTS));

	timer.stop();
	writefln( "Bsearch: %f usec (avg)", cast( float ) timer.microseconds / ITERATIONS );
	writefln( " .... Cycle: %f usec (avg)", ( timer.microseconds - overhead ) / ITERATIONS );

	// ========================
	Thread.sleep( 1 );
	timer.start();

	foreach (iter; 0 .. ITERATIONS ) {
		int key = rand() % ELEMENTS;
		csearch( &#038;key, &#038;a[ 0 ], ELEMENTS, int.sizeof, &#038;compare_ints );
	}

	timer.stop();
	writefln( "Csearch: %f usec (avg)", cast( float ) timer.microseconds / ITERATIONS );
	writefln( " .... Cycle: %f usec (avg)", ( timer.microseconds - overhead ) / ITERATIONS );
</pre>
<p>Where most things should be obvious, but I&#8217;ll point out that the Thread.sleep calls were for 100 nanoseconds each &#8211; just enough to give control back to Windows so it hopefully wouldn&#8217;t interrupt the loops. The &#8220;csearch&#8221; call in the second block is actually a call to the bsearch function in the C standard library &#8211; I created an alias for it since I already had a bsearch function.</p>
<p>Overall, for ELEMENTS = 1000, the cycle time for the template based bsearch was about 0.12 usec. The cycle time for the call-a-function C library version was about 0.165 usec. So for trivial comparisons, the template-based version gives a 25+% speed benefit &#8211; that&#8217;s why I was interested in running the tests!</p>
<p>Obviously, this number depends on the number of comparisons. For ELEMENTS = 100, you&#8217;re looking at log n = 7 instead of 10 comparisons &#8211; csearch should catch up a little bit. For ELEMENTS = 100,000, it would be 17 instead of 10 and presumably the template version would show even better performance.</p>
<h2>The Edge of Seventeen</h2>
<p>But that&#8217;s not why I started this. I wanted to know how well the template bsearch code would do against lsearch &#8211; linearly scanning the array. It seemed like a safe bet that 1,000 elements would be a win for the binary search&#8230;and it was. (<em>whew!</em>) But would the same be true for 100 elements? 10?</p>
<p>Well, for me, the number is 17. If ELEMENTS = 17, then lsearching the array (using a template based lsearch) is faster than bsearching the array (using a template based bsearch). (On the other hand, template based lsearch crosses over C-style bsearch at 61 items!)</p>
<p>Those performance numbers depend on a lot of things. Most importantly, they depend on the relative speed of your comparison code versus the structural code of the search function.</p>
<p>What does that mean? Well, take a look at the a program called git-bisect. This program is a shell script that uses a binary search to find a build failure. Bisect knows the list of changes that were made to your source code, so it can pull out a version, build it, test it, and decide whether the problem is in that version or elsewhere.</p>
<p>The issue, though, is that the &#8220;comparison function&#8221; involves pulling out your 30 million lines of code, running a build, and then running the test that identifies whether the bug is in this version or not. The build and test process takes so much longer than the &#8220;add two numbers and divide by two&#8221; of the binary search that it&#8217;s a no brainer &#8211; you&#8217;re almost always going to use the bsearch version.</p>
<p>But when the compare is a single CPU instruction, it&#8217;s a different story. In that case, the overhead of the bsearch pushes the crossover point far out past where it is in theory. If your compiler is smart enough, and when you&#8217;ve got enough memory to allow it to inline these kinds of functions&#8230;well, we have to draw a line somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/10/crossing-over/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You&apos;re doing it wrong: ergonomics</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/10/youre-doing-it-wrong-ergonomics</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/10/youre-doing-it-wrong-ergonomics#comments</comments>
		<pubDate>Fri, 08 Oct 2010 16:35:26 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/index.php/2010/10/youre-doing-it-wrong-ergonomics</guid>
		<description><![CDATA[Here&#8217;s a picture of my keyboard. It&#8217;s a Logitech Alto cordless keyboard, that came with a laptop stand. 

All in all, it&#8217;s a pretty nice keyboard, and the whole stand+keyboard thing has really worked out well for me. But I have to say to the folks at Logitech (and to every other keyboard maker out [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a picture of my keyboard. It&#8217;s a Logitech Alto cordless keyboard, that came with a laptop stand. </p>
<p><a href="http://www.longacre-scm.com/blog/wp-content/uploads/2010/10/alto.jpg"><img src="http://www.longacre-scm.com/blog/wp-content/uploads/2010/10/alto-150x150.jpg" alt="Logitech Alto cordless keyboard with laptop stand" title="Logitech Alto Keyboard+Stand" width="150" height="150" class="alignnone size-thumbnail wp-image-103" /></a></p>
<p>All in all, it&#8217;s a pretty nice keyboard, and the whole stand+keyboard thing has really worked out well for me. But I have to say to the folks at Logitech (and to <i>every other keyboard maker out there</i>), &#8220;You&#8217;re doing it wrong!&#8221;</p>
<h3>What does &#8216;cordless&#8217; mean?</h3>
<p>This is a <em>cordless</em> keyboard. What does that mean? Think about it. If you have a keyboard with no cord, where does it go? It goes in your lap, of course. Or on your knees, or maybe balanced on one arm of your office chair.</p>
<p>It doesn&#8217;t just sit on your desk in front of your laptop, looking cool with no cable. Wireless devices in general can be used farther away from the terminal, in stranger and more convenient and incredibly awkward locations. (In the middle of a power outage, I once had a customer using a cordless mouse on my back.)</p>
<p>And using a cordless keyboard as a &#8220;laptop&#8221; keyboard should mean certain things. In particular, it should mean that the design engineers spent some time optimizing the design for its likely use.</p>
<p>But clearly they haven&#8217;t. Because every cordless keyboard I&#8217;ve seen, until recently, looked like a slightly shrunken corded keyboard. &#8220;Wow! I left my enormous keyboard in my pants pocket, and mom put it through the wash. Look! The cord is gone and it shrunk by about an inch. Neat!&#8221;</p>
<p>Folks, the fact is that I will probably <em>never</em> use that numeric keypad on the side of your keyboard. I have a separate numeric keypad that I use for my expense reports. That&#8217;s a relatively new product, too, and it&#8217;s still not perfect (you need arrows and numbers at the same time when you&#8217;re doing spreadsheet work, idiots!) but that&#8217;s a different blog post.</p>
<p>So why does every &#8220;cordless&#8221; keyboard have a number pad? Because if it didn&#8217;t, someone might claim more keys than us? I have no idea why, but here&#8217;s the problem: that number pad unbalances the keyboard!</p>
<p>Do the math. The keypad takes up what, four inches of space on the right side of the keyboard? And so as a result, the center of balance of the keyboard is two inches farther off than it should be. Because that keyboard is going to be in somebody&#8217;s lap!</p>
<p>Talk to your ergonomics people. Most of your users have arms that are about the same length. And so the &#8220;natural&#8221; position for a keyboard is to have the left-hand keys and the right-hand keys about the same distance away from the left and right hands, respectively.</p>
<p>Putting the arrow keys and the numeric keypad and the whatever-other-stupid-things out there on the right side of the keyboard makes the thing harder and harder to balance on your lap. And it moves the actual keys that people type with farther and farther ti the left, away from the hands that want to use them.</p>
<p>So please, keyboard guys, build me a keyboard. And <em>twist</em> those keys. Don&#8217;t just give it a nice &#8220;wave&#8221; shape, because that crap only works on the desktop. A keyboard for my lap needs to admit that my belly will prevent my hands from being anywhere near the &#8220;center&#8221; of the keyboard. So twist those keys 40 degrees or so &#8211; you&#8217;ve got a lap, you do the math. But remember, only &#8220;good girls&#8221; keep their keyboard on their knees. And there&#8217;s about two girls, good or otherwise, in programming. The rest of us keep that keyboard on our thighs, so we can prop our feet on the desk.</p>
<p>Then <em>get rid</em> of the keypad, or at least make it a separate unit. (I&#8217;ve already bought one of those.) And move the arrow keys and all the other nonsense. Make &#8216;em close to the edge, or down where laptops put &#8216;em, or something. But they have to be easy to use!</p>
<p>What I&#8217;d like to see added is a scroll wheel. I write code for a living. And when I&#8217;m not writing code, I&#8217;m writing documents &#8211; design documents, trip reports, recommendations, and even the occasional blog post. So I spend a lot of time going up and down in documents. I don&#8217;t want a touchpad. I hate touchpads. But if you could take the scroll wheel from a mouse and put it in the middle of the keyboard (in the V gap where you twisted the keys apart) that would rock.</p>
<p>And finally, let&#8217;s get old-school. Remember the Gateway AnyKey keyboards? If you design guys still want to put some keys on, I&#8217;d like some more function keys. Those of us who live in IDEs like Visual Studio or Eclipse have got too many options. Put some G-keys on the left. Put some H-keys on the right. Whatever it takes. But remember to keep the center of balance the same. If the keyboard grows by an inch on the left, then grow it by an inch on the right, too!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/10/youre-doing-it-wrong-ergonomics/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Doxygen&#8217;s \test command with C++</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/08/using-doxygens-test-command-with-c</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/08/using-doxygens-test-command-with-c#comments</comments>
		<pubDate>Sat, 28 Aug 2010 21:19:39 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=95</guid>
		<description><![CDATA[I&#8217;m working on some C++ code that is documented using Doxygen. Nothing earth-shattering there.
But I&#8217;m doing unit testing, and writing unit tests. In this case, I&#8217;m using the boost C++ libraries. That means my tests don&#8217;t look like classes, the way they might look if I was using CppUnit or CxxUnit. Instead, they look like [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on some C++ code that is documented using <a href="http://www.doxygen.org">Doxygen.</a> Nothing earth-shattering there.</p>
<p>But I&#8217;m doing unit testing, and writing unit tests. In this case, I&#8217;m using the <a href="http://www.boost.org">boost C++ libraries.</a> That means my tests don&#8217;t look like classes, the way they might look if I was using CppUnit or CxxUnit. Instead, they look like macros:<br />
<code><br />
BOOST_AUTO_TEST_CASE( null_ctor ) {</p>
<p>	Arena &#038; sut = Simple::Arena();<br />
	BOOST_CHECK( sut.capacity() != 0 );<br />
}<br />
</code></p>
<p>The macro expansion is really quite clever, in a Lovecraftian &#8220;there are secrets man was not meant to know&#8221; fashion. And reading them has certainly improved my knowledge of C++. (But it also permanently lowered my SAN by a few points, I think.)</p>
<p>Anyway, Doxygen offers this command called <b>\test</b> that seems tailor-made for testing.</p>
<p>Well, it&#8217;s not. The command description is very &#8220;vague,&#8221; in that you use it to add entries (paragraphs) to the &#8220;test list.&#8221; </p>
<p>Some experimentation will show that the apparent use is to document something (a class, a function) and add a bunch of these \test entries, just like adding \param commands. And the entity you are documenting will then have a <b>Test:</b> section in its documentation.</p>
<p>This is totally not what I want. And I&#8217;m pretty sure it&#8217;s not what anybody doing TDD wants. What I want is to somehow tie my tests, which are in a separate location (different file, different class, different namespace, etc.), back to the class or function that I&#8217;m testing.</p>
<p>The best way I&#8217;ve found so far to accomplish this is to <em>lie</em> to Doxygen about what is being documented. Here&#8217;s my current scheme:<br />
<code><br />
#define BOOST_TEST_DYN_LINK<br />
#include &lt;boost/test/unit_test.hpp&gt;</p>
<p>#include &lt;Bronze/Memory/Simple/Arena.h&gt;</p>
<p>using namespace Bronze::Memory;</p>
<p>BOOST_AUTO_TEST_SUITE( Memory_SimpleArena )</p>
<p>/// \class Bronze::Memory::Simple::Arena<br />
/// \test \b null_ctor Confims that a SimpleArena no-args construction will<br />
/// use the default size, whatever that is. (1m)</p>
<p>BOOST_AUTO_TEST_CASE( null_ctor ) {</p>
<p>	Arena &#038; sut = Simple::Arena();<br />
	BOOST_CHECK( sut.capacity() != 0 );<br />
}<br />
</code></p>
<p>Adding the <tt>\class</tt> command tells Doxygen to re-open the documentation of the target class. The <tt>\test</tt> command is then associated with the &#8220;right&#8221; class, and I include the testcase name in bold as part of the test description. </p>
<p>This still doesn&#8217;t generate a link to the right source file (containing the test), and it doesn&#8217;t really know anything about the test case. So it&#8217;s not a perfect solution. But it does enable me to put my testcase documentation with my testcases, while having the documentation actually show up attached to the right class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/08/using-doxygens-test-command-with-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Which &#8216;which&#8217; is which?</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/08/which-which-is-which</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/08/which-which-is-which#comments</comments>
		<pubDate>Tue, 10 Aug 2010 22:13:27 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>
		<category><![CDATA[Practice]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=91</guid>
		<description><![CDATA[The &#8216;which&#8217; utility is one of those really useful commands that never seems to cross the bridge from Unix to Windows. The CMD.EXE special %$PATH:f syntax seems to promise some relief, but of course it&#8217;s never that simple &#8211; I at least want to type &#8220;which foo&#8221; rather than &#8220;which foo.exe&#8221;.
So here&#8217;s which.cmd &#8211; a [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8216;which&#8217; utility is one of those really useful commands that never seems to cross the bridge from Unix to Windows. The CMD.EXE special %$PATH:f syntax seems to promise some relief, but of course it&#8217;s never that simple &#8211; <strong>I</strong> at least want to type &#8220;which foo&#8221; rather than &#8220;which foo.exe&#8221;.</p>
<p>So here&#8217;s <code>which.cmd</code> &#8211; a script that tries to DWIW. </p>
<p>Note that this script is wrong in one key detail: it searches inside-out. That is, any &#8220;foo.COM&#8221; will take precedence over any &#8220;foo.EXE&#8221; even if the .exe version occurs earlier in the PATH.</p>
<pre>
@echo off
REM Copyright (c) 2010, Austin Hastings.
REM This file may be used for any purpose without restriction.

REM NOTE: This script does not handle multiple entries with different
REM extensions correctly. It returns the first matching EXTENSION, rather
REM than returning the first DIRECTORY with any EXTENSION.

REM Analogous to unix 'which' command, look for a matching runnable
REM in %PATH% and print the location.

SETLOCAL ENABLEDELAYEDEXPANSION

set check_extensions= ;%PATHEXT%

:ext_loop
if "%check_extensions%" == "" goto done

for /F "tokens=1* delims=;" %%E in ( "%check_extensions%" ) do (
	set check_extensions=%%F
	set target=%1%%E
	for %%W in (  !target! ) do set answer=%%~f$PATH:W
)

if "%answer%" == "" goto ext_loop

:done
if "%answer%" == "" goto not_found

@echo %answer%
goto exit

:not_found
@echo No runnable matching '%1' was found.

:exit
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/08/which-which-is-which/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add VC &#8220;branch&#8221; to your prompt</title>
		<link>http://www.longacre-scm.com/blog/index.php/2010/03/add-vc-branch-to-your-prompt</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2010/03/add-vc-branch-to-your-prompt#comments</comments>
		<pubDate>Wed, 31 Mar 2010 20:40:18 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=85</guid>
		<description><![CDATA[Inspired by a recent discussion on IRC, here&#8217;s another little piece of bash script goodness. This one goes in your startup files, and it integrates with your $PS1 prompt variable. The script adds your VC branch to the prompt. I&#8217;ve included SVN and GIT, and I&#8217;m sure you could trivially add CVS if need be. [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by a recent discussion on IRC, here&#8217;s another little piece of bash script goodness. This one goes in your startup files, and it integrates with your $PS1 prompt variable. The script adds your VC branch to the prompt. I&#8217;ve included SVN and GIT, and I&#8217;m sure you could trivially add CVS if need be. Other VC tools may not be such a good fit &#8212; in many cases, they encode project info as part of the path, so the standard &#8217;show the working directory&#8217; prompt is enough.</p>
<p>(On the other hand, if you&#8217;ve got one that needs special treatment, drop me a line and I&#8217;ll add it here.)</p>
<p><code>
<pre>
if which git > /dev/null
then
	parse_git_path() {
		if [[ -d .git ]]
		then
			git branch --no-color \
			| sed -rne '/^\*/s/^\* (.*)$/(git:\1) /p'
			return 0
		else
			return 1
		fi
	}
else
	parse_git_path() {
		return 1
	}
fi

if which svn > /dev/null
then
	parse_svn_path() {
		if [[ -d .svn ]]
		then
			local rroot=$(svn info | sed -ne 's/Repository Root: //p')
			svn info 		\
			| sed -rne "s#^URL: $rroot/(trunk|branches/[^/]*).*#(svn:^/\1) #p"
			return 0
		else
			return 1
		fi
	}
else
	parse_svn_path() {
		return 1
	}
fi

parse_vc_path() {
	parse_git_path \
	|| parse_svn_path
}

export PS1="\[\e]0;\$(parse_vc_path)\w\a\]${debian_chroot:+($debian_chroot)}\$(parse_vc_path)\w\$ "
</pre>
<p></code><br />
The key is that bash performs command substitution on PS1 each time it prints it, so you can have whatever command sequence you want get run. In this case, it&#8217;s a script that does whatever processing is needed in order to determine the &#8220;branch&#8221;, if any, that you&#8217;re on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2010/03/add-vc-branch-to-your-prompt/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some bash goodness</title>
		<link>http://www.longacre-scm.com/blog/index.php/2009/10/some-bash-goodness</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2009/10/some-bash-goodness#comments</comments>
		<pubDate>Wed, 28 Oct 2009 15:09:48 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/index.php/2009/10/some-bash-goodness</guid>
		<description><![CDATA[Here&#8217;s some bash goodness (well, not really) to make &#8216;less&#8217; a little bit more useful.
&#160;
less() {
 &#160; &#160; &#160; &#160;local -a args
&#160;
 &#160; &#160; &#160; &#160;for arg
 &#160; &#160; &#160; &#160;do
 &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;case &#8220;$arg&#8221; in
 &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;*:[[:digit:]]* )
 &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<div>Here&#8217;s some bash goodness (well, not really) to make &#8216;less&#8217; a little bit more useful.</div>
<div>&#160;</div>
<div>less() {</div>
<div> &#160; &#160; &#160; &#160;local -a args</div>
<div>&#160;</div>
<div> &#160; &#160; &#160; &#160;for arg</div>
<div> &#160; &#160; &#160; &#160;do</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;case &#8220;$arg&#8221; in</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;*:[[:digit:]]* )</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;line=${arg/#+(?):/}</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;args[${#args[*]}]=&#8221;+$line&#8221;</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;arg=${arg/%:+(?)/}</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;;;</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;esac</div>
<div>&#160;</div>
<div> &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;args[${#args[*]}]=$arg</div>
<div> &#160; &#160; &#160; &#160;done</div>
<div>&#160;</div>
<div> &#160; &#160; &#160; &#160;$( which less ) &#8220;${args[@]}&#8221;</div>
<div>}</div>
<div>&#160;</div>
<div>Basically, this shell function loads when you tell it to (in your .profile, likely), and it replaces the &#8216;less&#8217; command. When you type &#8216;less &#8230;&#8217; on the command line, the function runs.</div>
<div>&#160;</div>
<div>It scans through the args, looking for one like filename.c:24, and if it finds that kind of arg, it replaces it with +24 filename.c &#8212; translating the syntax used by a *lot* of compilers into the syntax used by less for opening a file and jumping directly to the line number.</div>
<div>&#160;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2009/10/some-bash-goodness/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why not 2 stacks?</title>
		<link>http://www.longacre-scm.com/blog/index.php/2009/06/why-not-2-stacks</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2009/06/why-not-2-stacks#comments</comments>
		<pubDate>Wed, 01 Jul 2009 00:05:13 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=76</guid>
		<description><![CDATA[I was googling around the other day for compiler wisdom when I stumbled on a computer security &#8220;research&#8221; paper. Some one or ones were postulating some theoretical result about vulnerability to buffer overflow attacks in blah, blah, blah special case. I didn&#8217;t read it, because that&#8217;s not what I was after.
But it made me think: [...]]]></description>
			<content:encoded><![CDATA[<p>I was googling around the other day for compiler wisdom when I stumbled on a computer security &#8220;research&#8221; paper. Some one or ones were postulating some theoretical result about vulnerability to buffer overflow attacks in <i>blah, blah, blah</i> special case. I didn&#8217;t read it, because that&#8217;s not what I was after.</p>
<p>But it made me think: how come Microsoft hasn&#8217;t just solved this problem already? Two &#8220;stacks&#8221; would wipe out the overflow attack problem at minimal cost.<br />
<span id="more-76"></span></p>
<h3>Back Story</h3>
<p>If you don&#8217;t know, here&#8217;s a very (!) short summary of the problem.</p>
<p>Computer software uses &#8220;subroutines&#8221; to do everything. In fact, even the &#8220;main&#8221; program is a subroutine. Because subroutines can call other subroutines, they can &#8220;stack up&#8221;. That is, &#8220;main&#8221; can call &#8220;sell ticket&#8221; which can call &#8220;print schedule&#8221; which can call &#8220;draw table&#8221;, etc.</p>
<h4>What&#8217;s a stack?</h4>
<p>In modern systems, there is no really good way to keep track of how many routines will get stacked up, a general-purpose data structure called a &#8220;stack&#8221; is used. This is so important that CPU&#8217;s explicitly support a single &#8220;call stack&#8221; with special CPU instructions like push and pop, special registers to access the stack, and all kinds of <i>incredibly expensive</i> stuff. </p>
<p>A stack works by &#8220;stacking&#8221; data, in much the same way they stack plates at a buffet restaurant (or cafeteria). The only data item you can get to is the one at the &#8220;top&#8221; of the stack. (The only plate you can take is the one on the top of the stack. See?) If you want to put on more data, it goes on the top of the stack, and the stack grows. If you want to take off data, you take it off the top of the stack, and the stack shrinks.</p>
<p>In this case, though, there are two ways to think about what &#8220;data&#8221; means. One way is to think of data as being individual units. One number goes on the stack, then another number goes on top of it, etc. But another way is to think of data &#8220;sets&#8221; going on the stack. If you think about the back button on your web browser, it acts like a stack. You click on a link, and a page gets <strong>pushed</strong> on to the &#8220;history&#8221; in the browser. You click &#8220;Back&#8221; and a page gets <strong>popped</strong> off the stack, leaving a different page visible in the browser. But these pages aren&#8217;t just a single number. There&#8217;s a URL, the page contents, maybe some form data, etc.</p>
<p>In terms of computer subroutines, each subroutine may have some &#8220;local data&#8221; that it needs to store. The URL of a web page, or the name of the current user, for example. And this data is expected to be used within the subroutine, maybe passed down into any sub-subroutines that get called, and then it will disappear when the subroutine finishes. </p>
<p>So a subroutine may have a bunch of local data, much like a web page in your browers. That can also be managed as part of a stack, using what is called a <em>stack frame</em>. A stack frame is a collection of data that all gets jammed onto the stack at one time, so the subroutine can use it. Again,&#8221;modern CPUs&#8221; (pretty much all of them built since the 1970&#8217;s) support this directly.</p>
<h4>How important is the stack?</h4>
<p>The de facto standard computer in the world today is the Intel x86 CPU of some kind: Pentium, Opteron, whatever you want to call it. Back when dinosaurs roamed the earth and nobody thought a 16-bit computer was worth the extra money over a 8-bit computer, the x86 family had about 30,000 transistors on board. Now, when people are arguing the relative merits of 32 versus 64 bit computing, the transistor count is about a billion.</p>
<p>The x86 family of computers work using &#8220;registers.&#8221; A register is a place in the center of the CPU where a number can be held while other stuff gets done to it. If you want to add two numbers, you use a register. If you want to store the result, you probably use another register. They are absolutely crucial to the design of the x86, and most other widespread CPUs. (There are alternative architectures: stack based and accumulator based CPUs. But nobody buys them except for special applications.)</p>
<p>The x86 registers back in the time of the 8088 and 8086 computer were called:</p>
<ul>
<li><tt>AX</tt> The <em>accumulator</em> register, best for math.</li>
<li><tt>BX</tt> The <em>base address</em> register, best for pointer references.</li>
<li><tt>CX</tt> The <em>counter</em> register, best for loop counts.</li>
<li><tt>DX</tt> The <em>data</em> register, used for multiply/divide operations, I/O operations, and indirect addressing.</li>
<li><tt>SI</tt> The <em>source index</em> register, for memory-to-memory block operations, like copying data.</li>
<li><tt>DI</tt> The <em>destination index</em> register, used for memory block operations, like SI.</li>
<li><tt>BP</tt> The <em>base pointer</em> register, used to point to the base of the stack frame.</li>
<li><tt>SP</tt> The <em>stack pointer</em> register, used to point to the current &#8220;top&#8221; of the stack.</li>
</ul>
<p>There were some additional registers, like IP, flags, CS, DS, ES, and SS. But they weren&#8217;t (and still aren&#8217;t) general purpose registers, can&#8217;t participate in computations, and generally don&#8217;t get modified much by programs. Because the registers listed above are <em>general purpose</em> registers, they can be used for things not listed above. For example, if you need to store a temporary intermediate result while you are computing some other value, you can always jam it into the DI register. (or SI, or DX, or whatever you like). But not BP or SP.</p>
<p>So back in the day, when we had to carve our own computers out of rocks using nothing buy a bronze chisel and a wooden mallet, BP and SP were given over to the stack frame. That&#8217;s two registers <em>out of eight</em> &#8212; 25%, for you liberal arts majors &#8212; assigned to do the job of keeping track of the stack.</p>
<h4>And now?</h4>
<p>When the 80&#215;86 family went 32-bit, the registers changed. The 32-bit registers were called &#8220;extended&#8221; registers, and so the names became: <tt>eax, ebx, ecx, edx, edi, esi, ebp, esp</tt>. </p>
<p>Now, with the advent of the 64-bit x86 family, the names have changed again: <tt>rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp.</tt> But in addition, AMD added 8 new registers to the x64 architecture, and Intel followed suit.</p>
<p>This means that if you&#8217;re using a 32-bit CPU, or a 64-bit CPU in compatibility mode, you&#8217;re using 8 registers, still, and 2 of them are dedicated to the stack, still. (25%, still, for you liberal arts majors.) If you&#8217;re using a 64-bit CPU in 64-bit mode, then you&#8217;ve got 16 registers (that&#8217;s 12.5% for all you Art History grads).</p>
<p>So the stack frame, and the stack pointer, and this whole stack &#8220;thang&#8221; are pretty important.</p>
<h3>What&#8217;s a buffer overflow attack?</h3>
<p>So there&#8217;s a stack, and your subroutines use it. The most obvious way they can use it is to store a &#8220;return address&#8221; on it. If you have a subroutine called sub_1, it might have the stack set up like this:</p>
<pre>
    :
  ????
  ????
  1234 < -- SP
</pre>
<p>Where the SP (stack pointer) points to 1234. What's 1234? Well, it's the return address -- an address is a location in computer memory -- where it will send program control when it finishes doing whatever it is doing.</p>
<p>And if the sub_1 routine calls a sub_2 routine, the stack will look like:
</pre>
<pre>
    :
  ????
  ????
  1234
  1299 < -- SP
</pre>
<p>When the CPU executes a <tt>call</tt> instruction, it pushes the return address (of the instruction right after the call) onto the stack automatically. (And yes, on Intel platforms the stack always goes down instead of up. Don't ask why, it doesn't matter.)</p>
<p>When the sub_2 routine returns control to sub_1, it pops the stack and uses the address that it gets to branch back.
</pre>
<pre>
    :
  ????
  ????
  1234 < -- SP
  1299
</pre>
<p>Of course the 1299 is still down there -- it doesn't get erased. Instead, the SP adds 4 (4 is the size of a return address on a 32-bit [4 byte] CPU) and we go on. Very efficient, very fast, and very automatic with all the special instruction support.</p>
<h4>Stack Frames, redux</h4>
<p>When the program has local data it needs to store, the situation gets a little more complex. The caller pushes a return address, then the callee (the local subroutine) moves the stack pointer a little bit more, to make room. If sub_2 needs 12 bytes of storage, the <em>preamble</em> of the subroutine looks like this:
</pre>
<pre>
   push(ebp)
   ebp = esp
   esp = esp - 12
</pre>
<p>A preamble is a standard set of code that gets executed at the top of every function. It is generated automatically by the compiler &#8212; programmers don&#8217;t have to think about inserting it.</p>
<p>When it comes time to clean up and leave the subroutine, the corresponding code is the <em>postamble</em> and it does something like:</p>
<pre>
   esp = esp + 12
   pop ebp
   return
</pre>
<p>(In fact, this is wrong. But the effect is the same and it&#8217;s easier for you to understand.)</p>
<p>Before the subroutine is called, the stack looks like:</p>
<pre>
    :
  ????
  ????
  1234   < -- SP
</pre>
<p>Then when the call happens, the stack looks like:
</pre>
<pre>
    :
  ????
  ????
  1234
  1299  < -- SP
</pre>
<p>Then when the preamble runs, we get:
</pre>
<pre>
    :
  ????
  ????
  1234
  1299
   BP'  < -- BP
   ?
   ?
   ?     <-- SP (12 bytes = 3 x 32bit words of "stack frame" storage)
</pre>
<p>Note that the new BP is pointing to the place on the stack where the previous BP was stored.</p>
<h4>So?</h4>
<p>So there's a stack frame, and it's a little complex, and it's sitting there on the stack. And in the middle of that stack is the "local data" the subroutine needs.</p>
<p>Sometimes the "local data" includes what is called a "buffer." A buffer is just a space to store a bunch of characters in a row. For example, if the program is going to ask me for my name and address, it needs a place to store however-many characters go into a name and address.</p>
<p>If the buffer is 12 bytes long, I could enter my name as "Austin" and there would be not problem. But if I entered "Austin Hastings" (14 letters, with the space) the buffer would not be long enough to store the name. The problem is that if I am allowed to just enter any old thing, I might deliberately do something destructive. Suppose we are using our previous example:
</pre>
<pre>
    :
  ????
  ????
  1234
  1299
   BP'  < -- BP
   ?
   ?
   ?     <-- SP (12 bytes = 3 x 32bit words of "stack frame" storage)
</pre>
<p>If I type my name as "Austin Hastings", the buffer overflows, like this:
</pre>
<pre>
    :
  ????
  ????
  1234
  1299
  ngs   < -- BP
  asti
  in H
  Aust  <-- SP
</pre>
<p>The old value of BP gets wiped out, and so the calling subroutine will have a bunch of bogus data (because it accesses all the local variables relative to where BP is pointing).</p>
<p>But if I type my name as "Austin Hastings.1215" look what happens to the return address that is supposed to point to the subroutine that called us:
</pre>
<pre>
    :
  ????
  ????
  1234
  1215    -- look here!
  ngs.   < -- BP
  asti
  in H
  Aust  <-- SP
</pre>
<p>See? The return address got changed by me typing in a special, too-long name. This is called a "buffer overflow attack", and if done right it can cause the subroutine to return control to a new program, written by the attacker.</p>
<h3>Why can Microsoft solve the problem?</h3>
<p>First, Microsoft writes Windows, the most attacked system in the world. And it writes the Microsoft C/C++ compiler, which is the tool of choice for compiling on Windows. Since anything Microsoft does will get copied by the Linux and Apple guys, this is a no-brainer. If Microsoft solves the problem, everyone will copy them and solve the problem for their own sites.</p>
<h3>Okay, <em>how</em> can Microsoft solve the problem?</h3>
<p>The solution to the "attack" part of the problem is to use two stacks. There are two registers already, and one of them rarely gets used (SP). So split them into two different stacks, and you eliminate the ability of an attacker to overwrite the return address (by separating the stack frame data from the "just-a-stack" data).</p>
<h4>Details</h4>
<p>Here's what the previous example would look like with two stacks.
</pre>
<pre>
  "Frames":          "Stack":
      :                1234
    ????               EBP(1)  < -- SP(2)
    ????  <-- BP(2)    1299
    ....               EBP(2)  <-- SP(3)
    ....
    ....  <-- BP(3)
</pre>
<p>Notice that I've drawn the buffer as dots, and whatever was in the caller's local data as question marks. Notice also that the important data -- the frame pointers and return addresses -- are in a totally different place from the stack frame data. This means that no buffer overflow attack can reach them. </p>
<h4>That's it?</h4>
<p>Yep. That's it.</p>
<p>Mind you, it's an expensive it. But not a horribly expensive one. Microsoft, and the GCC folks, and Intel, and anybody else that writes a compiler, would have to come up with a transition plan to take us from one system to another. It would probably easier for Microsoft, since they can deliver their system (Windows) all at once. This recompile would have to happen for almost every problem, so it might take a while for total coverage. But the "most attacked" programs are things like email servers, web servers, the PHP interpreter, etc. There is a pretty short list of those, and getting them secured might take an hour and a half, if everyone would work together.</p>
<h4>Any problems with this idea?</h4>
<p>First, it doesn't just magically make everything work. Some solutions, like W(+)E, try to do that. If you can just "solve it in hardware" then the problem goes away without messing around with that 23-year-old program that the boss uses but nobody has the source for.</p>
<p>Second, this approach doesn't require anybody to buy a new computer. (But keep reading!) So unless Microsoft forces an upgrade with Windows 7 or 8 or whatever (and when have they ever forced an upgrade?) there's no money in it for the CPU vendors.</p>
<p>Also, don't forget that this only protects against buffer overrun attacks that try to force code execution. It won't protect you from corruption of <em>data</em> that is in a stack frame buffer or local variable. Someone might try to rewrite their account balance, or change your password, or whatever. </p>
<h4>Checksum the Stack Frame</h4>
<p>There's a solution for that, too. And it could involve new hardware. Just create a "checksum" value for the stack frame, and put it right at the end of each subroutine's frame. That way, if an attacker overruns a buffer it will destroy the checksum. </p>
<p>Obviously I don't mean a real checksum. What I mean is something unpredictable, unlikely to occur at random, and easily checked. Most of the registers wouldn't work, since their values are generally predictable. It would make sense to create a dedicated register for this on the CPU -- which would also allow Intel to make some new hardware -- and push the "random" value on both the frame stack and the SP stack (for checking).</p>
<h3>If you're so damn smart, why aren't you rich?</h3>
<p>I used to do a lot of assembly programming. So I've always kind of wondered why this wasn't obvious to everybody else. And it may well be that there's some critical flaw in the logic. Probably having to do with interrupt-proofing the two-stack transition. </p>
<p>But maybe there's not. Maybe it's just obvious to me. In which case, I hereby demand that anyone who implements this must call it "Austin's Device". And if you implement the checksum idea, that shall be "Austin's Other Device." </p>
<p>But there's an old joke about an Economics professor and one of his grad students walking across the quad. And the grad student says, "Look, professor! There's a $100 bill lying on the ground."</p>
<p>To which the professor replies, "Don't be absurd. If there were really a $100 bill lying on the ground, someone would pick it up."</p>
<p>Maybe you know what's wrong with my clever device? If so, please let me know.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2009/06/why-not-2-stacks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Things left unsaid&#8230;</title>
		<link>http://www.longacre-scm.com/blog/index.php/2009/06/things-left-unsaid</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2009/06/things-left-unsaid#comments</comments>
		<pubDate>Sat, 27 Jun 2009 23:15:18 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[Off-topic]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/?p=69</guid>
		<description><![CDATA[In my copious spare time I&#8217;m working on a programming language called &#8220;Close.&#8221; It&#8217;s a C-like systems programming language targeted at the Parrot VM.
Now, I&#8217;m not a compiler guy. But I don&#8217;t have to be. Because Parrot comes with the Parrot Compiler Toolkit (PCT), a set of code that can render an Abstract Syntax Tree [...]]]></description>
			<content:encoded><![CDATA[<p>In my copious spare time I&#8217;m working on a programming language called &#8220;Close.&#8221; It&#8217;s a C-like systems programming language targeted at the <a href="http://www.parrot.org">Parrot VM.</a></p>
<p>Now, I&#8217;m not a compiler guy. But I don&#8217;t have to be. Because Parrot comes with the Parrot Compiler Toolkit (PCT), a set of code that can render an Abstract Syntax Tree (AST) into executable code. The result is that I built a grammar using Perl6, then wrote some action methods hooked to the grammar that built the AST. Then I stood back and let PCT do it&#8217;s voodoo. The time between starting the project and now has been about 3 weeks, and I was busy with other stuff for 30 percent or so of that.</p>
<p>Okay, that&#8217;s cool. Parrot+PCT rocks, as Allison Randal <a href="http://www.linux-mag.com/cache/7373/1.html">points out</a>. But it&#8217;s not my point. The reason I wrote Close was because people were still coding in PIR &#8212; Parrot&#8217;s version of assembly language &#8212; years after starting the project. D&#8217;oh! Everybody knows that the first thing you do with a new architecture is develop a C compiler.</p>
<p>Well, somebody had to do it. So I stepped up. I talked a little bit about it at the Parrot Virtual Machine Workshop last week in Pittsburgh, and some other people acted interested. As a result, it&#8217;s up on Google code at http://close.googlecode.com. But the key for me was a conversation I had with Uri Guttman in a hallway at <a href="http://yapc10.org/yn2009/">YAPC10</a> (the conference to which the PVMW was attached).</p>
<p>I showed Uri this code:</p>
<pre>
void test_foreach()
{
	pmc the_list = new ResizableStringArray;
	push the_list, "alpha", "beta", "gamma", "delta", "omega";

	pmc new_list = new ResizableStringArray;
	int count = 0;

	foreach (str i: the_list) {
		push new_list, i;
		++count;
	}

	ok(count, 5, "f/e Iterate over each element");

	count = 0;
	int pass = 1;

	foreach (i : new_list) {
		if (i != the_list[count++]) {
			pass = 0;
		}
	}

	ok(pass, "f/e Same items, same order");
}
</pre>
<p>And Uri read through it, figured out what I was doing, and said, &#8220;Well, I don&#8217;t like your coding style. And I really don&#8217;t like single letter variable names, even inside a loop.&#8221;</p>
<p>And my first reaction was &#8220;Up yours, bud!&#8221; But I swallowed that, and then it hit me: I won!</p>
<p>The whole point of implementing Close was to stop writing code that looks like this:</p>
<pre>
.sub 'uniquereg' :method
    .param string rtype
    unless rtype goto err_nortype
    if rtype == 'v' goto reg_void
    .local string reg
    reg = 'P'
    $I0 = index 'Ss~Nn+Ii', rtype
    if $I0 < 0 goto reg_psin
    reg = substr 'SSSNNNII', $I0, 1
  reg_psin:
    reg = concat '$', reg
    .tailcall self.'unique'(reg)
  reg_void:
    .return ('')
  err_nortype:
    self.'panic'('rtype not set')
.end
</pre>
<p>And so what occurred to me was that I showed Uri some code that <em>didn't</em> look like that. It looked like C. And so naturally he read it. And he commented on something that was relevant to him. Not "Gee, I can read your code!" but "Of course I can read your code -- it's C. But I don't like your curly braces."</p>
<p>Win. Win. Win. </p>
<p>Hearing what Uri did <strong>not</strong> say was the key.</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2009/06/things-left-unsaid/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Being a trust specialist</title>
		<link>http://www.longacre-scm.com/blog/index.php/2009/04/being-a-trust-specialist</link>
		<comments>http://www.longacre-scm.com/blog/index.php/2009/04/being-a-trust-specialist#comments</comments>
		<pubDate>Mon, 27 Apr 2009 07:25:57 +0000</pubDate>
		<dc:creator>Austin Hastings</dc:creator>
				<category><![CDATA[General CM]]></category>
		<category><![CDATA[Organizations]]></category>
		<category><![CDATA[Practice]]></category>
		<category><![CDATA[Software CM]]></category>
		<category><![CDATA[Theory]]></category>

		<guid isPermaLink="false">http://www.longacre-scm.com/blog/index.php/2009/04/being-a-trust-specialist</guid>
		<description><![CDATA[Most of the readers of this blog are CM specialists. Whether you&#8217;re a corporate CM librarian, or a build manager, you are focused on what the industry now calls &#8220;Application Lifecycle Management.&#8221; That&#8217;s an attempt to give a name to the collection of roles and functions we perform. It isn&#8217;t so much that the people [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the readers of this blog are CM specialists. Whether you&#8217;re a corporate CM librarian, or a build manager, you are focused on what the industry now calls &#8220;Application Lifecycle Management.&#8221; That&#8217;s an attempt to give a name to the collection of roles and functions we perform. It isn&#8217;t so much that the people are diversifying, as that the collection of tools that were all trying to fit under the &#8220;Configuration Management Tool&#8221; umbrella got too large. Change tracking? Sure, that&#8217;s a CM function. Version control? Yep. Requirements management? Well &#8230; okay. UML? Not so much. Trouble ticketing? Yeah, I guess. ITIL? Sure, why not?</p>
<p><a href="http://bradapp.blogspot.com/">Brad Appleton</a> is, in my opinion, one of the all-around smart guys in the CM space. His focus for the last few years has been on Agile CM, but his writings are applicable to anybody doing software development, for a <i>very </i>broad definition of software &#8211; most of what he writes is applicable to almost any kind of intellectual property. And recently Brad has been doing some blogging about books on various flavors of &#8216;trust. &#8216;</p>
<p>Trust, to me, is one of those core values for most CM specialists. The fact is that CM is a simple job. There are certain requirements, and once you meet them you get to go home. In that way it&#8217;s a lot like being a system administrator: is the system up, is everything working? Okay, go home. Now admittedly it can be pretty hard to meet some of those requirements. That&#8217;s why I didn&#8217;t say it was an easy job &#8211; just a simple one. But that&#8217;s where trust comes in. Because whether your shop is an agile shop or not, the CM guys are more affected by trust than any single other thing.<br />
<span id="more-65"></span></p>
<p>Let me repeat that: CM guys are more affected by trust than any single other thing.</p>
<p>I&#8217;m a CM consultant. I get called in by shops that either don&#8217;t have CM and want to build a team, or by shops that already HAVE a CM team but have experienced enough CM failures that they don&#8217;t trust them any more. Since most companies obtain headcount by calling up a body shop and ordering it by the pound (kilos for the EU, I guess), I don&#8217;t usually get called <i>first</i> for CM team setups. It&#8217;s usually only after there are some bodies on site, when it turns out that CM specialists that are great at keeping an existing system going don&#8217;t have so much experience in starting a new system up from scratch. So there&#8217;s usually some kind of mess on the floor when I show up.</p>
<p>What does that mean for you? Well, it means that if you see me at your shop, you should make sure your resume is up to date. (Sorry, but it&#8217;s true.) It also means that you can keep me, or somebody like me, from showing up if you concentrate on building trust with your customers.</p>
<p>Brad&#8217;s first blog post about trust was on Covey&#8217;s &#8220;The Speed of Trust,&#8221; and in that book there are five &#8220;rings of trust&#8221; outlined. The second and third rings, or types, are interpersonal trust and organizational trust. That&#8217;s where I live, and if you&#8217;re smart it&#8217;s where you spend time each day maintaining your relationships with your customers.</p>
<p>It&#8217;s a pretty simple set of questions, but it makes all the difference. </p>
<ol>
<li>Do the people you interact with understand what you need from them, and understand how to deliver it?</li>
<li>Do they understand what services you can provide to them, and how to request them? </li>
<li>Are all of these things practicable?</li>
<li>Do they perceive you as doing what is needed, on time, and without unnecessary oversight?</li>
<li>Do they perceive you as requesting things that are objective, and consistent, and in keeping with your and their function?</li>
<li>Do they believe the estimates and the explanations you give them?</li>
</ol>
<p>If you can answer yes to all of those, you&#8217;ll never work with me. That&#8217;s because your customers trust you. If you can&#8217;t &#8211; if your customers distrust you &#8211; then as the level of trust decreases, the likelihood of a &#8220;fixer&#8221; being called in for your team goes up. In the credit card business, your interest rate goes up. In the CM business, the level of interest in working with you goes down.</p>
<p>If you&#8217;re a CM manager for a military systems contractor, then you&#8217;ve got a CM plan. And you&#8217;ve got a set of prescribed deliverables. And if the developers &#8211; software, firmware, systems engineers, whatever &#8211; know how to deliver each release to you, and it&#8217;s an automated, mechanical, simple chore, then you&#8217;re halfway there. If your other customers &#8211; the clients, and the development team that need to roll back to an earlier release &#8211; can submit a request and quickly get some kind of link, or bundle, or set of typing instructions, then you&#8217;re all the way done.</p>
<p>If you&#8217;re a build guy, or a release guy, or a deployment guy for a software team &#8211; agile or not &#8211; then the activities might be different but the questions aren&#8217;t. Does the team know how to get you to do a build, or cut a release, or deploy a package? Are there any bogus hoops they have to jump through? Have you built any passive-aggressive walls, saying &#8220;we want the development team to do this thing which they really ought to trust us to do but they overrode us that one time and now we&#8217;re bitter and whiny and nobody loves us&#8221;? Can a developer quickly get to the codeface of some old branch from six months ago?</p>
<p>I guess the short form is this. Are you doing your job? Are you making it as easy as possible for your customers to ask you to do your job for them? Because in my experience &#8211; and I&#8217;ll admit my experiences are all negative &#8211; if you aren&#8217;t doing your job, or if you&#8217;re making your customers pay some kind of &#8220;tax&#8221; (time, whining, paperwork, hoop-jumping, sloth, inefficiency) then you&#8217;re going to lose trust. And once you lose trust, you&#8217;ll see a little bureaucracy get started. And when that doesn&#8217;t work (and it rarely does) you&#8217;ll get to meet me. I&#8217;ll be the guy &#8220;we brought in to help gets things caught up.&#8221;</p>
<p>This isn&#8217;t a threat, quite. Because I don&#8217;t know you. It&#8217;s a reflection of the world as I see it, because I&#8217;m like a specialist doctor &#8211; nobody makes an appointment with a gastroenterologist to talk about how healthy they are. In my case, nobody calls me up and says, &#8220;We&#8217;d like to pay you to fly out here and sit in a conference room and live in a hotel for two weeks so that we can tell you how smoothly our build and deployment works!&#8221; (Man, I would LOVE that.) But what I get instead is, &#8220;We need you to come and fix our build and deployment procedures. Our developers don&#8217;t trust the CM team, and we&#8217;ve had a couple of blown deployments in the last few months.&#8221;</p>
<p>So anyway, I guess the upshot of this blog post &#8211; and this is much too depressing to be a CM Journal article, so it&#8217;ll stay a blog post &#8211; is that you need to make sure your house is in order, trust-wise. Go read one or more of the books on Brad&#8217;s list. Because CM teams seem far more Boolean than development teams. Developers can say &#8220;It&#8217;ll be done soon,&#8221; while CM&#8217;ers have to say &#8220;it&#8217;s not done.&#8221; And saying &#8220;it&#8217;s not done&#8221; leads pretty directly to &#8220;you&#8217;re not hired.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.longacre-scm.com/blog/index.php/2009/04/being-a-trust-specialist/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

