<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Trivial Bugs</title>
	<atom:link href="http://trivialbugs.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://trivialbugs.wordpress.com</link>
	<description>Join me here in search of solutions to interesting software problems.</description>
	<lastBuildDate>Sun, 02 May 2010 09:14:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='trivialbugs.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Trivial Bugs</title>
		<link>http://trivialbugs.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://trivialbugs.wordpress.com/osd.xml" title="Trivial Bugs" />
	<atom:link rel='hub' href='http://trivialbugs.wordpress.com/?pushpress=hub'/>
		<item>
		<title>if (&lt;linker symbol&gt;)</title>
		<link>http://trivialbugs.wordpress.com/2010/05/02/if-linker-symbol/</link>
		<comments>http://trivialbugs.wordpress.com/2010/05/02/if-linker-symbol/#comments</comments>
		<pubDate>Sun, 02 May 2010 09:00:51 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[linker]]></category>
		<category><![CDATA[RTOS]]></category>
		<category><![CDATA[symbol]]></category>

		<guid isPermaLink="false">http://trivialbugs.com/?p=120</guid>
		<description><![CDATA[A beautiful bug came to my attention several months ago. An RTOS was being ported from a proprietary compiler to GCC. I fixed a myriad of issues with inline assembly and other quirks, and RTOS was beginning to come to life. But for some reason it worked unimaginably slow &#8212; to the point of being [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=120&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A beautiful bug came to my attention several months ago.</p>
<p>An RTOS was being ported from a proprietary compiler to GCC. I fixed a myriad of issues with inline assembly and other quirks, and RTOS was beginning to come to life. But for some reason it worked unimaginably slow &#8212; to the point of being indistinguishable from halt.</p>
<p>Investigation revealed that all the CPU did was checking board&#8217;s SDRAM and checking it successfully, but over and over again. As the board had 256MB of RAM, every iteration took a while.</p>
<p>The RAM check was a part of RTOS&#8217; main loop and was guarded like so</p>
<p><code><br />
&lt;somewhere in a header file&gt;:<br />
<span style="color:#800000;">extern uchar __VERIFY_ENABLE[];<br />
#define VERIFY_ENABLE ((uint_32)__VERIFY_ENABLE)</span></p>
<p>&lt;somewhere in source file&gt;:<br />
<span style="color:#800000;">if (VERIFY_ENABLE) {<br />
&nbsp;&nbsp;&lt;check RAM&gt;;<br />
}</span><br />
</code></p>
<p>So where is the bug? Being user-friendly, RTOS developers provided pre-built RTOS binaries that are ready to be linked into final binary image. They also allowed users to disable or enable the check of RAM by setting <code><span style="color:#800000;">__VERIFY_ENABLE</span></code> symbol in the linker script to zero or non-zero value. Neat, isn&#8217;t it? Obviously, for a board with lots of RAM, the check should not be enabled, as that would slow the system to a crawl.</p>
<p>And this arrangement worked just fine with a proprietary compiler. GCC, however, being a smart compiler, inferred that as <code><span style="color:#800000;">__VERIFY_ENABLE</span></code> was an address of an array, it could not possibly be NULL. Consequently, <code><span style="color:#800000;">if ((uint_32)__VERIFY_ENABLE != NULL)</span></code> was always true, so GCC removed the condition in the <code>if</code>-statement. Hurray for optimizations!</p>
<p>Fixing this problem required changing just several bytes. While GCC could prove that <code><span style="color:#800000;">__VERIFY_ENABLE</span></code> was not zero, it couldn&#8217;t prove that <code><span style="color:#800000;">__VERIFY_ENABLE</span><code> was not one. Substituting comparison of <code><span style="color:#800000;">__VERIFY_ENABLE</span></code> from <code>"<span style="color:#800000;">!= NULL</span></code> to "<code><span style="color:#800000;">== 1</span></code>" solved the problem.</p>
<p><code><span style="color:#800000;">#define VERIFY_ENABLE ((uint_32)__VERIFY_ENABLE == 1)</span></code></p>
<p>It's that simple. I added these few bytes and the RTOS came finally to life. It turned out this bug was the last one blocking the RTOS from working.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/120/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=120&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2010/05/02/if-linker-symbol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing Compare-And-Swap</title>
		<link>http://trivialbugs.wordpress.com/2009/12/10/implementing-compare-and-swap/</link>
		<comments>http://trivialbugs.wordpress.com/2009/12/10/implementing-compare-and-swap/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 09:11:54 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[GLIBC]]></category>

		<guid isPermaLink="false">http://trivialbugs.com/?p=110</guid>
		<description><![CDATA[Do you know how to implement a compare-and-swap instruction (CAS) in an instruction set (ISA) which has only a test-and-set (TAS) for an atomic read-modify-write? Take into account that the implementation should be (a) fast and (b) behave as closely to a real CPU instruction as possible, since the final product needs to be integrated [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=110&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Do you know how to implement a compare-and-swap instruction (CAS) in an instruction set (ISA) which has only a test-and-set (TAS) for an atomic read-modify-write?</p>
<p>Take into account that the implementation should be</p>
<p>(a) fast</p>
<p>and</p>
<p>(b) behave as closely to a real CPU instruction as possible,</p>
<p>since the final product needs to be integrated into GLIBC.</p>
<p>I know a couple of solutions to this problem, but I&#8217;m wondering if you know better ways to do CAS which you&#8217;d like to share in comments to this post. One approach I tackled is quite complicated and involves kernel magic. The other, having an unfortunate side-effect, is not quite acceptable for general purpose use in GLIBC.</p>
<p>So what are your ideas? I promise to share mine in detail in the next post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=110&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/12/10/implementing-compare-and-swap/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Function pointers</title>
		<link>http://trivialbugs.wordpress.com/2009/11/26/function-pointers/</link>
		<comments>http://trivialbugs.wordpress.com/2009/11/26/function-pointers/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 19:18:35 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[debug]]></category>

		<guid isPermaLink="false">http://trivialbugs.com/?p=81</guid>
		<description><![CDATA[Here is a story about a sneaky bug that got in the way of a hacker, who had to do some pretty important stuff in assembly. Being a rational hacker, he didn&#8217;t start working from scratch, but turned to libraries. Say, he used a function from a dlopen()&#8216;ed library and called it from the assembly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=81&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is a story about a sneaky bug that got in the way of a <span style="color:#ff0000;"><span style="color:#000000;">hacke<span style="color:#000000;">r</span></span><span style="color:#000000;">,</span></span><span style="color:#000000;"> w</span>ho had to do some pretty important stuff in assembly.</p>
<p>Being a rational hacker, he didn&#8217;t start working from scratch, but turned to libraries. Say, he used a function from a <span style="color:#800000;">dlopen()</span>&#8216;ed library and called it from the assembly code. In C code he wrote:</p>
<pre style="padding-left:30px;"><span style="color:#800000;">void *dostuff_ptr;</span></pre>
<pre style="padding-left:30px;"><span style="color:#800000;">...</span></pre>
<pre style="padding-left:30px;"><span style="color:#800000;">dostuff_ptr = dlsym ("dostuff");</span></pre>
<p>In the assembly he then casually called <span style="color:#800000;">dostuff_ptr</span> to do the required pretty important stuff.</p>
<p>Being a smart hacker, he decided to re-implement the library function in assembly to show everyone his talent.  And he declared a pointer to his implementation:</p>
<pre style="padding-left:30px;"><span style="color:#800000;">extern void *mydostuff;</span></pre>
<p>Being a user-friendly hacker, he allowed users to choose between the library&#8217;s implementation of <span style="color:#800000;">dostuff()</span> and his own by conditionally assigning somewhere in the code <span style="color:#800000;">dostuff_ptr</span> to the right address:</p>
<pre style="padding-left:30px;"><span style="color:#800000;">dostuff_ptr = use_mydostuff_p  ?  mydostuff : dlsym ();</span></pre>
<p>How surprised was the hacker to see that once <span style="color:#800000;">use_mydostuff_p</span> was <span style="color:#800000;">true</span> things stopped working.</p>
<p>To fix the problem he launched a debugger and spent some time investigating code.  As you know, the amount of time it takes to debug a problem is very much dependent on how much you trust your toolchain. Well, our talented hacker was working on a new feature which affected the toolchain, and his level of trust was minimal.  So he spent a lot of time debugging, debugging and debugging again.</p>
<p>Anyway, he eventually discovered that <span style="color:#800000;">dostuff_ptr = mydostuff;</span> puts a bogus value into <span style="color:#800000;">dostuff_ptr</span>. After taking a close look at the value in <span style="color:#800000;">dostuff_ptr</span> he noticed that the value in <span style="color:#800000;">dostuff_ptr</span> is a binary code of the first instructions of <span style="color:#800000;">mydostuff()</span>.  It turned out instead of loading the <em>address</em> of <span style="color:#800000;">mydostuff()</span> the code loaded <em>contents</em> of <span style="color:#800000;">mydostuff()</span> into <span style="color:#800000;">dostuff_ptr</span>.  <span style="color:#ff0000;"><span style="color:#000000;">Well, why did this happen to our good hacker?</span> </span>He simply did not define <span style="color:#800000;">mydostuff</span> correctly!  Instead of</p>
<pre style="padding-left:30px;"><span style="color:#800000;">extern void *mydostuff;</span></pre>
<p>it should&#8217;ve been</p>
<pre style="text-align:justify;padding-left:30px;"><span style="color:#800000;">extern void mydostuff (void);</span></pre>
<p>!</p>
<p>The end.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=81&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/11/26/function-pointers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging GLIBC</title>
		<link>http://trivialbugs.wordpress.com/2009/11/01/debugging-glibc/</link>
		<comments>http://trivialbugs.wordpress.com/2009/11/01/debugging-glibc/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 12:25:47 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[GLIBC]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://trivialbugs.com/?p=57</guid>
		<description><![CDATA[When I first started working on GLIBC development, one of the challenges was to understand how to debug GLIBC.  All the usual attempts to compile GLIBC without optimizations ended up with GLIBC grumpily complaining that it cannot be compiled that way.  The development FAQ says In the early startup of the dynamic loader (_dl_start), before relocation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=57&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When I first started working on GLIBC development, one of the challenges was to understand how to debug GLIBC.  All the usual attempts to compile GLIBC without optimizations ended up with GLIBC grumpily complaining that it cannot be compiled that way.  The development FAQ says</p>
<blockquote><p>In the early startup of the dynamic loader (_dl_start), before relocation of the PLT, you cannot make function calls. You must inline the functions you will use during early startup, or call compiler<br />
builtins (__builtin_*).</p>
<p>Without optimizations enabled GNU CC will not inline functions. The early startup of the dynamic loader will make function calls via an unrelocated PLT and crash.</p></blockquote>
<p>OK, fair enough. Can the optimized code be perhaps then debugged, since after all GCC generates adequate debug information even when optimizations are enabled?  Well, not really.  GLIBC is a carefully written piece of software and people who wrote it (a) know quite a lot about compilers and (b) use that knowledge to squeeze every drop of optimizations out of the code.  So though the resulting debug information is usually enough for users to get a sensible backtrace and report bugs to the developers, it is almost useless <span style="color:#000000;">f</span><span style="color:#ff0000;"><span style="color:#000000;">or single-stepping </span><span style="color:#000000;"><span style="color:#000000;">i</span>n a debugger.</span></span></p>
<p>The two main problems with single-stepping optimized GLIBC are (1) line numbers and (2) <span style="color:#000000;">optimized out </span>variables.  The line numbers at <span style="color:#800000;">-O2</span> get <span style="color:#000000;">screwed up</span> beyond repair.   For example, when you try to single step the runtime linker while it is loading dependencies and resolving symbols, the debugger will be thrown between source files and functions at every other line.  So to make the situation more bearable you need to add <span style="color:#800000;">-fno-schedule-insns -fno-schedule-insns2</span> to the build flags.</p>
<p>I don&#8217;t really know how to <em>properly</em> fix problem (2) with variables being optimized out.  What I usually do is just recompile the only file I&#8217;m interested in without optimizations and relink GLIBC.  Normal GLIBC development rarely requires single-stepping the runtime linker before it self-relocates, so try, it might work for you too!</p>
<p>My recipe for debuggable GLIBC is the following:</p>
<div>
<ol>
<li>Build GLIBC, save the log.</li>
<li>Find the command line in the log that compiles the file you&#8217;re interested in.  Note: most files are compiled into <span style="color:#0000ff;"><em>file</em></span>.o and <span style="color:#0000ff;"><em>file</em></span>.os; the first one goes into the static library, and the second &#8212; into the shared.  If you&#8217;re not sure which one you need, it&#8217;s probably the .os version.</li>
<li>Go to the <span style="text-decoration:underline;">source</span> directory in which the file is located.  It is the subdirectory in the source tree which stands right before the <em><span style="color:#0000ff;">file</span></em>.os in the output destination.  It took me several builds to understand that after you run <span style="color:#800000;">make</span> in the build tree the first command  is <span style="color:#800000;">cd &lt;src_dir&gt;</span> &#8212; funny, eh?</li>
<li>Add <span style="color:#800000;">-E -dD</span> to the end of the compile line and change the output destination to <span style="color:#000000;">a directory outside both build and source trees.  This way you will get the </span>preprocessed source.  Take a look at it.  Most likely you&#8217;d want to remove the original line information from it (<span style="color:#800000;">sed &#8220;/#\ .*/d&#8221; &lt; <span style="color:#0000ff;"><em>file</em></span>.i &gt; <span style="color:#0000ff;"><em>file</em></span>.c</span>) and reformat it (<span style="color:#800000;">indent &lt; <em><span style="color:#0000ff;">file</span></em>.c &gt; <span style="color:#0000ff;"><em>file</em></span>2.c</span>) to unravel all the hidden <span style="color:#000000;">sorcery</span> in GLIBC macros.</li>
<li>Remove the <span style="color:#800000;">-include libc-symbols.h</span> from the command line (it&#8217;s the header through which that &#8220;glibc cannot be compiled without optimization&#8221; error gets in!) and remove <span style="color:#800000;">-O2</span> from the optimization flags.</li>
<li>Recompile <span style="color:#0000ff;"><em>file</em></span>.</li>
<li>Rerun <span style="color:#800000;">make</span> to relink the libraries, the runtime linker and the utilities.  Check the log to make sure only things that were intended for update were updated.</li>
<li>If the <span style="color:#800000;">make</span> process didn&#8217;t notice that <em><span style="color:#0000ff;">file</span></em><span style="color:#800000;">.o</span> was changed, force rebuild by removing the top-level binary, e.g., remove <span style="color:#800000;">libc-pic.a</span> to force rebuild of <span style="color:#800000;">libc.so</span>.</li>
</ol>
</div>
<p>Voilà!  <span style="color:#000000;">Debug at will!</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=57&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/11/01/debugging-glibc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging TLS storage</title>
		<link>http://trivialbugs.wordpress.com/2009/10/14/debugging-tls-storage/</link>
		<comments>http://trivialbugs.wordpress.com/2009/10/14/debugging-tls-storage/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 16:14:43 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://trivialbugs.wordpress.com/?p=40</guid>
		<description><![CDATA[For background information on TLS check out Wikipedia. Some time ago I completed implementing TLS/NPTL support for m68k/ColdFire architecture. As a final touch I added support to GDB/gdbserver to handle thread-local variables and used the following simple test  to check that gdb/gdbserver handles TLS properly: static int __thread foo; int main () { foo = 123; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=40&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>For background information on TLS check out <a href="http://en.wikipedia.org/wiki/Thread-local_storage" target="_blank">Wikipedia</a>.</p></blockquote>
<p>Some time ago I completed implementing TLS/NPTL support for m68k/ColdFire architecture. As a final touch I added support to GDB/gdbserver to handle thread-local variables and used the following simple test  to check that gdb/gdbserver handles TLS properly:</p>
<pre style="padding-left:30px;"><span style="color:#800000;">static int __thread foo;</span><span style="color:#800000;">
int</span><span style="color:#800000;">
main ()
</span><span style="color:#800000;">{</span><span style="color:#800000;">
  foo = 123;</span><span style="color:#800000;">
  return foo;
}</span></pre>
<p>Compiled as:</p>
<pre style="padding-left:30px;"><span style="color:#800000;">gcc -g -o test test.c</span></pre>
<p>Then I set a breakpoint in <span style="color:#800000;">main() </span>and tried printing out the value of <span style="color:#800000;">foo</span> in a debugger. This didn&#8217;t work; as a matter of fact, you can try the above on any architecture with TLS/NPTL support and GDB will just bail out with error that it can&#8217;t get to the TLS storage.</p>
<p>After a couple of hours of debugging GDB (which entails running gdbserver under gdbserver and gdb under gdb) I determined that gdbserver doesn&#8217;t activate TLS handling because it doesn&#8217;t find &#8216;nptl-version&#8217; symbol.  Following this lead it was easy to find out that:</p>
<p>a) half of the TLS debugging support is in GLIBC&#8217;s libthread_db and this library is linked into gdbserver to provide interface between the debugger and GLIBC;</p>
<p>b) the other half is in GLIBC&#8217;s libpthread.so, including the &#8216;nptl-version&#8217; symbol;</p>
<p>c) gdbserver is expecting the &#8216;nptl-version&#8217; symbol to be defined in the program space, since it reaches the libpthread.so&#8217;s part of the TLS/NPTL debugging support only via getting symbols from the program space.</p>
<p>Therefore, the problem with my original testcase turned out to be in not linking it against libpthread.so.  Rebuilding<span style="color:#000000;"> the testcase wi</span>th</p>
<pre style="padding-left:30px;"><span style="color:#800000;">gcc -g -o test -lpthread test.c</span></pre>
<p>solved the problem.  M68k/ColdFire TLS/NPTL support was implemented.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=40&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/10/14/debugging-tls-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>id-shared libraries and symbol overriding</title>
		<link>http://trivialbugs.wordpress.com/2009/09/30/id-shared-libraries/</link>
		<comments>http://trivialbugs.wordpress.com/2009/09/30/id-shared-libraries/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 16:00:45 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[uClinux]]></category>

		<guid isPermaLink="false">http://trivialbugs.wordpress.com/?p=22</guid>
		<description><![CDATA[uClinux id-shared libraries and custom &#8216;new&#8217; uClinux is a flavor of Linux that is designed to run on MMU-less processors, usually, those are microcontrollers with RAM and flash onboard.  That&#8217;s not the topic of today discussion though; the centerpiece today is how one manages to use libraries in flat memory model and what implications may [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=22&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">uClinux id-shared libraries and custom &#8216;new&#8217;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">uClinux is a flavor of Linux that is designed to run on MMU-less processors,</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">usually, those are microcontrollers with RAM and flash onboard.  That&#8217;s not</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">the topic of today discussion though; the centerpiece today is how one manages</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">to use libraries in flat memory model and what implications may jump out of the</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">corner and bite you in the arse.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">uClinux flat memory model means that all the code share the same address space.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Among other things this has an implication that all the applications should be</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">mapped at different addresses.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Another fact about uClinux is that binaries are usually in the bFLT format;</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">this format is very minimalistic and doesn&#8217;t allow run-time relocations.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">This last detail makes sharing of libraries on such a system kind of a trick.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">The explanation of how id-shared libraries work is a topic for another discussion.  The fact is that the library&#8217;s code stay intact once the library is loaded.  &#8217;Intact&#8217; is this context mainly means the library is not being relocated for</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">every application that uses it.  This makes symbol preemption within the</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">library impossible; i.e., one cannot define a-best-of-the-best implementation</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">of library&#8217;s function A and make the library use it.  The example I&#8217;ve</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">recently came about defines C++ operator &#8216;new&#8217; in an application and expects</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">libstdc++ library to use it for internal memory allocation.  Well, that didn&#8217;t</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">quite happen.</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">Let this be a warning to developers who write not-very-big applications that</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">may be useful to use in a washing machine or a toaster.  A C++ timer</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">application that allocates a string to show something on a display comes to mind <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">First of all, for applications to be able to access libraries they</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">[the libraries] should be mapped at some constant addresses.  This is where</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">&#8216;id&#8217; in &#8216;id-shared&#8217; comes into play.  On such a system each library is compiled</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">as a id=1, &#8230;, id=N library and the kernel then loads the first library at</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">constant_offset_1, &#8230;, and the Nth library in constant_offset_N.  The offsets</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:0;width:1px;height:1px;">are magic numbers that do not change overtime.<span style="font-family:Helvetica, 'Times New Roman', 'Bitstream Charter', Times, serif;line-height:normal;font-size:12px;">Some time ago I stumbled upon a bug called ______. The C++ operator was defined as &#8216;new&#8217; in an application and was expected to be used by libstdc++ library for internal memory allocation. Well, that didn&#8217;t happen. </span></div>
<p><span style="font-family:Helvetica, 'Times New Roman', 'Bitstream Charter', Times, serif;font-weight:normal;line-height:normal;font-size:12px;">S</span>ome time ago I stumbled upon a bug in a C++ application.  A C++ &#8216;new&#8217; operator was defined in an application and was expected to override the default definition in the libstdc++ library, even the library&#8217;s internal references.  Well, that didn&#8217;t happen.</p>
<p>Instead the libstdc++ library was merrily using its default memory allocator, ignoring the override.  For some reason symbol preemption was not working for the shared library.  Interestingly, the erratic behavior appeared only on a uClinux system.</p>
<blockquote><p>uClinux is a flavor of Linux designed to run on MMU-less processors.  uClinux has a flat memory model that obligates all code to share the same address space.  In uClinux binaries are usually in the bFLT format &#8212; a very minimalist format that doesn&#8217;t allow run-time relocations.</p></blockquote>
<p>The last peculiarity of uClinux turned out to be responsible for the problem.  As no run-time relocations are allowed in the bFLT format there is no way for an executable to tailor the library code to its needs.  This is quite a reasonable constraint given that all the executables share the address space and the libraries.</p>
<p>Well, this is one of those bugs that we just have to live with.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=22&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/09/30/id-shared-libraries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>ACM-ICPC challenge: Factorial Frequencies</title>
		<link>http://trivialbugs.wordpress.com/2009/09/18/acm-icpc_ffrequencies/</link>
		<comments>http://trivialbugs.wordpress.com/2009/09/18/acm-icpc_ffrequencies/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 07:43:49 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[ACM-ICPC]]></category>

		<guid isPermaLink="false">http://trivialbugs.wordpress.com/?p=7</guid>
		<description><![CDATA[To make my leisure time more challenging I have decided to start solving at least one ACM-ICPC contest problem a week. Last Thursday I embraced factorial frequencies problem dating way back to 1993. Problem gist: In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=7&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To make my leisure time more challenging I have decided to start solving at least one <a href="http://cm.baylor.edu/ICPCWiki/Wiki.jsp?page=Problems" target="_blank">ACM-ICPC contest problem</a> a week. Last Thursday I embraced <a href="http://www.karrels.org/Ed/ACM/reg93/prob_a.html" target="_blank">factorial frequencies problem</a> dating way back to 1993.</p>
<p>Problem gist:</p>
<blockquote><p>In an attempt to bolster her sagging palm-reading business, Madam  Phoenix has decided to offer several numerological treats to her  customers.  She has been able to convince them that the frequency  of occurrence of the digits in the decimal representation of  factorials bear witness to their futures.  Unlike palm-reading,  however, she can&#8217;t just conjure up these frequencies, so she has  employed you to determine these values.</p>
<p>Recall that the definition of n! (that is, n factorial) is just 1×2×3×&#8230;×n.  As she expects to use the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.</p>
<p>The input data for the program is simply a list of integers for which  the digit counts are desired.  All of these input values will be less  than or equal to 366 and greater than 0, except for the last integer,  which will be zero.  Don&#8217;t bother to process this zero value; just  stop your program at that point.  The output format isn&#8217;t too  critical.</p></blockquote>
<p>Well, I wanted the solution to be elegant. And what could be more elegant than prime numbers! So I decided to do a prime factorization of the factorial. The evident first step involved counting the zeroes at the factorial&#8217;s end, which was pretty easy. The number of zeroes equals to the exponent of &#8217;5&#8242; in the prime factorization, as you can get a &#8217;10&#8242; only by multiplying a &#8217;5&#8242; by a &#8217;2&#8242;.  It is unfortunate though that the number of &#8217;2&#8242;s in factorization is certainly greater then the number of &#8217;5&#8242;s.</p>
<p>Done with zeroes, I understood that I had no idea how to count the other digits without calculating the whole number of the factorial &#8212; which is what I tried elegantly to avoid in the first place. OK then. Perhaps, this number stripped of zeroes can now fit into the 64 bits and the usual CPU arithmetic can be applied?  Umm, but no.  There are 366/2 = 183 &#8217;2&#8242;s in the factorization of 366! and only 366/5 = 72 &#8217;5&#8242;s.  Therefore, we are left with 111 unaccountable &#8217;2&#8242;s, not to mention all the other primes.  This will by no means not fit into 64 bits.</p>
<p>Hmm&#8230; So I was left with the easy solution of just computing the factorial in decimal arithmetic. It&#8217;s just a 700-digit number after all.  In no time I wrote a program, and it did the job.  The end.</p>
<p>Hey, do you see any better solutions?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=7&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/09/18/acm-icpc_ffrequencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
		<item>
		<title>Signal frame ABI</title>
		<link>http://trivialbugs.wordpress.com/2009/09/05/signalframeabi/</link>
		<comments>http://trivialbugs.wordpress.com/2009/09/05/signalframeabi/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 13:07:41 +0000</pubDate>
		<dc:creator>Maxim Kuvyrkov</dc:creator>
				<category><![CDATA[Trivial Bugs]]></category>
		<category><![CDATA[ABI]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[signal unwinding]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Signal frame ABI is a special intimate relationship between the kernel and the unwinding library. Software developers don&#8217;t usually have the time to wonder how the signals are processed and the control is transfered back and forth between the user-space signal handler and the kernel. Myself included&#8230; till I had several GLIBC tests fail. Then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=1&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong> </strong></p>
<p><strong> </strong></p>
<p>Signal frame ABI is a special intimate relationship between the kernel and the unwinding library. Software developers don&#8217;t usually have the time to wonder how the signals are processed and the control is transfered back and forth between the user-space signal handler and the kernel. Myself included&#8230; till I had several GLIBC tests fail. Then I started to wonder.</p>
<p>Well, there is no magic in the kernel&#8217;s transferring control to user-space &#8212; it&#8217;s the kernel, the master, the tux, it can do whatever it wants (designed to want)! But what happens after the signal handler returns? Moreover, what will happen if for various reasons we need to show the full backtrace or unwind?</p>
<p>Have you heard about &#8216;syscall trampoline&#8217;? It is a blob the kernel arranges to be placed somewhere and then points the return address of the signal handler to it. The blob is then processed just like any other syscall asking the kernel to do a favor to the user space.</p>
<p>How do we unwind from a signal? We just need to peek into the code where the control would be returned, and if it resembles the trampoline, then the frame must be that of a signal!</p>
<p>Great! Now imagine what happens if the trampoline code gets changed in the kernel. The trampoline that no header will agree to have and that has to be, therefore, hardcoded in the unwinding library. Everything breaks: the unwinding code stops recognizing the signal frames, we loose our precious backtrace and exception cleanup handlers. What we get instead is several hours of exciting debugging.</p>
<p>Having found the bug, I leaped with joy and simply added another trampoline blob to the library.</p>
<p>What a good hunt it was.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/trivialbugs.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/trivialbugs.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/trivialbugs.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=trivialbugs.wordpress.com&amp;blog=9338632&amp;post=1&amp;subd=trivialbugs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://trivialbugs.wordpress.com/2009/09/05/signalframeabi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c2f9d4a3850f2eefbe9edf25e80fd7d3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">trivialbugs</media:title>
		</media:content>
	</item>
	</channel>
</rss>
