Someone Set us Up the Compiler Bomb

Despite the general public’s hijacking of the word “hacker,” we don’t advocate doing disruptive things. However, studying code exploits can often be useful both as an academic exercise and to understand what kind of things your systems might experience in the wild. [Code Explainer] takes apart a compiler bomb in a recent blog post.

If you haven’t heard of a compiler bomb, perhaps you’ve heard of a zip bomb. This is a small zip file that “explodes” into a very large file. A compiler bomb is a small piece of C code that will blow up a compiler — in this case, specifically, gcc. [Code Explainer] didn’t create the bomb though, that credit goes to [Digital Trauma].

We aren’t sure what practical use this would have, but it did illustrate a few interesting points. First, the code itself is simple and probably surprises you that it would even work:

main[-1u]={1};

The linker apparently doesn’t care that main is actually a function. You could argue that it doesn’t work because it blows up the compiler, but if you restrict the size to fit in your available memory, it will create an executable. Of course, that executable won’t actually run, but still.

To us, this seems less of an exploit as a strange bug in the linker. The reason it works is that the compiler runs out of memory during the link phase. We didn’t try it, but we wondered if –no-keep-memory or some other option could help. After all, you could imagine the linker being smart enough to initialize the static array in a “streaming” way and not trying to build it all in memory. It just doesn’t.

If you are more interested in making gcc produce smaller executables, we don’t blame you.

15 thoughts on “Someone Set us Up the Compiler Bomb

  1. For a more interesting program, see
    https://en.wikibooks.org/wiki/C%2B%2B_Programming/Templates/Template_Meta-Programming#History_of_TMP

    It causes the *compiler* to compute prime numbers *during compilation* in the form of an error message. Naturally it does not actually finish compiling.

    Most interestingly, the C++ template designers at first refused to believe that their creation was Turing-complete. This program rubbed their faces in it, and demonstrated they did not understand their creation. Now is something is so complex that the creators can’t understand it, what chance to mere mortals have?

    If C++ is the answer, you need to change the question.

    1. Modern C++ (fourteen) is actually a really different beast – you have to treat it differently and there’s a good language in there. It shares a room with “old” C++ but you can work around it. If it were taught “right”, from fresh, I think a lot of the kickback against C++ would disappear….

      1. Ah, an optimist; you must be young.

        Tools that require workarounds are waving a big red flag warning that Something Is Fundamentally Wrong.

        Workarounds won’t be used. If they can, people will tend to workaround using a tool properly. Fools are damn ingenious.

        1. If you seriously believe other languages don’t have debilitation quirks all over you haven’t been much around. It’s just a matter of choosing you poison, and I preffer the chainsaw and machete as I can accomplish just about any task with it, really fast and very efficient (though there might be some collateral damage). Tomy toys and padded screwdrivers are for wussies.

          1. Actually it neither fast nor efficient to get most working programs.

            As someone that has used C professionally since 1982 (and C++ since 1987), I was gobsmacked at how soon and how many high quality plug-and-play libraries became available for Java within 9 months of its release. In contrast C++ had dismally failed to manage that in 10 years of its release.

    2. C++ is a tool and if you don’t like it, that’s ok. However, there’s nothing mystical about metaprogramming because it’s been around forever. But if you are all about C, then I got some bad news about the preprocessor: it too is Turing-complete. Lots of things are Turing-complete, even Conway’s Game of Life.

      Honestly, just get over yourself.

      1. You’ve missed the point. C++ is so overly complex that even its designers refused to believe how complex it had become – that’s a sign that it is out of control of even the *experts*, let alone mere mortals.

        As C.A.R. Hoare famously put it, “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”
        https://en.wikiquote.org/wiki/C._A._R._Hoare

  2. This article is using the terms “compiler” and “linker” interchangably. Which one, exactly, does it break?

    Makes perfect sense to me how it would break a linker, but can someone explain to me why the static initializer is needed?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.