<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>freakingpenguin blog</title><id>https://freakingpenguin.com/feeds/tags/babel.xml</id><subtitle>Tag: babel</subtitle><updated>2024-03-06T00:00:00Z</updated><link href="https://freakingpenguin.com/feeds/tags/babel.xml" rel="self" /><link href="https://freakingpenguin.com" /><entry><title>Are bus errors still commonplace? Plus a bonus discussion on org-babel.</title><id>https://freakingpenguin.com/blog/are-bus-errors-still-commonplace-plus-a-bonus-discussion-on-org-babel.html</id><author><name>Richard Sent</name><email>richard@freakingpenguin.com</email></author><updated>2021-03-24T00:00:00Z</updated><link href="https://freakingpenguin.com/blog/are-bus-errors-still-commonplace-plus-a-bonus-discussion-on-org-babel.html" rel="alternate" /><content type="html">&lt;h1&gt;What is a bus error?&lt;/h1&gt;&lt;p&gt;I was reading through Peter Linden's &lt;em&gt;Expert C Programming&lt;/em&gt; when I
noticed an interesting example. On page 189, Peter talks about how one
can cause a bus error. I've never had a bus error occur before. Maybe
they're a solved problem?&lt;/p&gt;&lt;p&gt;Before we get too far ahead of ourselves, a bus error can occur when
we access a variable at an address that's not valid for that variable.
An address is not valid if the address is not evenly divisible by the
length of the variable. In other words...&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;/* sizeof (int) == 4 */&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;p1&lt;/span&gt; = *&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;5&lt;/span&gt;;
&lt;span class=&quot;syntax-comment&quot;&gt;/* Causes a bus error, 5 % 4 != 0 */&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;p2&lt;/span&gt; = *&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;32&lt;/span&gt;;
&lt;span class=&quot;syntax-comment&quot;&gt;/* No bus error, 32 % 4 == 0 */&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Realistically these programs would immediately seg fault as we
don't have access to arbitrary memory addresses. (Unless we were
working with embedded systems, perhaps...). To avoid this, we
can use a union.&lt;/p&gt;&lt;h1&gt;Peter Linden's Code&lt;/h1&gt;&lt;p&gt;Using the sample code in &lt;em&gt;Expert C Programming&lt;/em&gt;, pg.
189, I am going to see if it causes a bus error.&lt;/p&gt;&lt;p&gt;The address of the union must be divisible by 4 (or &lt;code&gt;sizeof int&lt;/code&gt;),
as it can store an integer. As long as &lt;code&gt;sizeof int &amp;gt; sizeof char&lt;/code&gt;
(or &lt;code&gt;sizeof int &amp;gt; 1&lt;/code&gt; as &lt;code&gt;sizeof char == 1&lt;/code&gt;), we can successfully
get our bus error.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;;
&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &amp;amp;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
,*&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;17&lt;/span&gt;;
&lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;*p %d\n&amp;quot;&lt;/span&gt;, *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;#+RESULTS:
: *p 17&lt;/p&gt;&lt;p&gt;Look at that! No problems.&lt;/p&gt;&lt;p&gt;x86 is very forgiving when it comes to misalignment errors. For the
most part, they just don't happen. This is great for us, but what if
we ported this code over to a platform that is less friendly, like
ARM?&lt;/p&gt;&lt;p&gt;Ideally, we want to see if a bus error &lt;em&gt;can&lt;/em&gt; occur in our code, so
that way we can avoid them during development, as opposed to fixing it
later.&lt;/p&gt;&lt;p&gt;Looking through the &lt;code&gt;gcc&lt;/code&gt; manual, I found a compile flag that will be
useful.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;code&gt;fsanitize=undefined&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector.
Various computations are instrumented to detect undefined behavior at
runtime.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;By adding the &lt;code&gt;-fsanitize=undefined&lt;/code&gt; compile flag, our program will
print a runtime error whenever one occurs.&lt;/p&gt;&lt;p&gt;There are similar flags, &lt;code&gt;-fsanitize=address&lt;/code&gt; and &lt;code&gt;-fsanitize=thread&lt;/code&gt;,
that can be useful for runtime error checking; look at the &lt;code&gt;gcc&lt;/code&gt;
manual for more information. I can combine options with commas, i.e.
&lt;code&gt;-fsanitize=address,thread,undefined&lt;/code&gt;.&lt;/p&gt;&lt;h1&gt;&lt;code&gt;-fsanitize=undefined&lt;/code&gt;&lt;/h1&gt;&lt;p&gt;There is one change that I need to make to the code. When a runtime
error occurs, the results are printed to &lt;code&gt;stderr&lt;/code&gt;. When we're looking
at our code through a terminal, &lt;code&gt;stderr&lt;/code&gt; and &lt;code&gt;stdout&lt;/code&gt; might seem like
the exact same thing.&lt;/p&gt;&lt;p&gt;I am not running this code through a terminal. I'm using &lt;code&gt;org-babel&lt;/code&gt;,
a very powerful tool for literate programming. If our
program runs successfully, &lt;code&gt;org-babel&lt;/code&gt; will tell us the results.&lt;/p&gt;&lt;p&gt;Unfortunately, these results don't include &lt;code&gt;stderr&lt;/code&gt;. In order to see
the runtime error occur, I need to close &lt;code&gt;stderr&lt;/code&gt;, then change
&lt;code&gt;stderr&lt;/code&gt;'s file descriptor to point to &lt;code&gt;stdout&lt;/code&gt;. This is what the
&lt;code&gt;dup2()&lt;/code&gt; function is doing.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;STDOUT_FILENO&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;STDERR_FILENO&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;

&lt;span class=&quot;syntax-special&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;;
&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &amp;amp;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
,*&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;17&lt;/span&gt;;
&lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;*p %d\n&amp;quot;&lt;/span&gt;, *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;p %lld\n&amp;quot;&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;#+RESULTS:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;/tmp/babel-YOFYnN/C-src-93AiCJ.c:17:6: runtime error: store to misaligned address 0x7ffec796bddd for type 'int', which requires 4 byte alignment
0x7ffec796bddd: note: pointer points here
 40 5a 14 84 55 00 00  e0 be 96 c7 fe 7f 00 00  00 f5 9a c3 4a 31 08 2e  00 00 00 00 00 00 00 00  25
             ^ 
/tmp/babel-YOFYnN/C-src-93AiCJ.c:18:3: runtime error: load of misaligned address 0x7ffec796bddd for type 'int', which requires 4 byte alignment
0x7ffec796bddd: note: pointer points here
 40 5a 14 84 11 00 00  00 be 96 c7 fe 7f 00 00  00 f5 9a c3 4a 31 08 2e  00 00 00 00 00 00 00 00  25
             ^ 
,*p 17
p 140732246965725&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And it works! We can now see the runtime error! We're trying to access
an integer at address 140732246965725, which is not divisible by 4
(AKA &lt;code&gt;sizeof int&lt;/code&gt;). Thus, a bus error occurs.&lt;/p&gt;&lt;h1&gt;Crash and burn programming&lt;/h1&gt;&lt;p&gt;Running code and printing out runtime errors is great. However, there's a
saying in programming called &amp;quot;Fail early, fail often&amp;quot;. What if we
don't just want an error message printed? What if, instead, we want
the program to immediately crash? After all, this is what would
actually happen if we were on a CPU architecture that couldn't handle
misaligned addresses.&lt;/p&gt;&lt;p&gt;I looked through the &lt;code&gt;gcc&lt;/code&gt; manual and saw the &lt;code&gt;-fno-sanitize-recover=all&lt;/code&gt;
option. Supposedly, it does the following:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;code&gt;-fsanitize-recover=all&lt;/code&gt; and &lt;code&gt;-fno-sanitize-recover=all&lt;/code&gt; is also
accepted, the former enables recovery for all sanitizers that
support it, the latter disables recovery for all sanitizers that
support it.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Let's try it! I'm going to add &lt;code&gt;-fno-sanitize-recover=all&lt;/code&gt; as a
compile flag. This should cause the program to immediately crash,
only printing the error message.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;STDOUT_FILENO&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;STDERR_FILENO&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;

&lt;span class=&quot;syntax-special&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;;
&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; *&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &amp;amp;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;u&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
,*&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;17&lt;/span&gt;;
&lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;p %d\n&amp;quot;&lt;/span&gt;, *&lt;span class=&quot;syntax-symbol&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Huh? Why wasn't the error message printed? Crashing the program is
what we wanted, but not without the error message! Without an error
message, all we're doing is making our program harder to debug.&lt;/p&gt;&lt;p&gt;Fortunately, this isn't our fault. The error message is actually being
printed, and it is being printed to &lt;code&gt;stdout&lt;/code&gt;. If we were running our
program in a terminal, we'd see the error message we expect.&lt;/p&gt;&lt;p&gt;Unfortunately, this is a limitation of &lt;code&gt;org-babel&lt;/code&gt;.
&lt;code&gt;-fno-sanitize-recover=all&lt;/code&gt; causes a nonzero exit code to be returned
on failure. &lt;code&gt;org-babel&lt;/code&gt; does not like nonzero exit codes and fails to
evaluate &lt;code&gt;stdout&lt;/code&gt; when this happens. It does evaluate &lt;code&gt;stderr&lt;/code&gt; when
the exit code is nonzero, but only to a separate temporary buffer. At
least this works outside of &lt;code&gt;org-babel&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;There's a (brief) discussion of this issue on the mailing list
&lt;a href=&quot;https://lists.gnu.org/archive/html/emacs-orgmode/2016-05/msg00204.html&quot;&gt;here&lt;/a&gt;.
Given that this thread is 5 years old, I'm not holding my breath for a
fix.&lt;/p&gt;&lt;p&gt;There is an easy solution for &lt;code&gt;sh&lt;/code&gt; scripts; just create a line at the
end with &lt;code&gt;:&lt;/code&gt;. Unfortunately since this is C, that's not really an
option.&lt;/p&gt;&lt;h1&gt;Wrapping it up&lt;/h1&gt;&lt;p&gt;The entire point of this endeavour is to try to make sure our code is
portable. When I write a program for one system, that program better
work on as many other systems as possible.&lt;/p&gt;&lt;p&gt;If any college students read this, professors don't like the &amp;quot;but it
worked on my machine!&amp;quot; excuse. (On the other hand, it takes one &lt;em&gt;mean&lt;/em&gt;
professor to test with a different architecture in order to if you were
careful about memory alignment. We can't predict everything!)&lt;/p&gt;&lt;p&gt;&lt;code&gt;-fsanitize=undefined&lt;/code&gt; is a great flag to add when compiling; it
catches more than just memory alignment! If you add the flag and
forget about it, you will at least get a warning when undefined
behavior occurs! I'd much rather have a program that doesn't work but
I know why then a program that doesn't work and I don't know why.&lt;/p&gt;</content></entry></feed>