tags:

views:

474

answers:

7

Is it possible to bring GCC into an infinite loop by inputting strange source code? And if yes, how? Maybe one could do something with Template Metaprogramming?

A: 
Mimisbrunnr
That would be easy to test, wouldn't it?
anon
No it detects the circular dependency.
NomeN
gcc will eventually fail with a `#include nested too deeply` error, so not an infinite loop. A quick test shows this to be the case. gcc does not detect the circular dependency (gcc on OS X Snow Leopard), but it is not infinite
Brandon Bodnár
good point - not infinite. I'm not used to letting that test run long enough to dump
Mimisbrunnr
I thought it might also, so I was already testing it when I saw your post. :)
Brandon Bodnár
A few megabytes of circular #includes might be enough to get it to thrash for a looong time, could bring the machine down if `ulimit` isn't set or get it to hang for cosmic time.
Potatoswatter
+2  A: 

It may be possible. But most compilers (and most standardised languages) have limits on things like recursion depths in templates or include files, at which point the compiler should bail out with a diagnostic. Compilers that don't do this are not normally popular with users.

anon
A: 

Don't know about gcc, but old pcc used to go into an infinite loop compiling some kinds of infinite loops (the ones that compiled down to _x: jmp _x).

Joshua
+10  A: 

Yes.

Almost every computer program has loop termination problems. I'm thinking that GCC, however, would run out of RAM before an infinite loop ever becomes obvious. There aren't many "free" operations in its design.

The parser & preprocessor wouldn't create problems. I'm willing to bet that you could target the optimizer, which would likely have more implementation faults. It would be less about the language and more about exploiting a flaw you could discover from the source code. i.e. the exploit would be non-obvious.

UPDATE

In this particular case, my theory seems correct. The compiler keeps allocating RAM and the optimizer does seem to be vulnerable. The answer is yes. Yes you can.

Pestilence
Comment of `/* Theoretically possible, but *highly* unlikely. */` in that bug report is worth a thousand pictures.
Roger Pate
A: 

Bentley writes in his book "Programming Pearls" that the following code resulted in an infinite loop during optimized compilation:

void traverse(node* p) {
  traverse(p->left);
  traverse(p->right);
}

He says "the optimizer tried to convert the tail recursion into a loop, and died when it could find a test to terminated the loop." (p.139) He doesn't report the exact compiler version where that happened. I assume newer compilers detect the case.

No infinite loop during compilation on my compiler (GCC 4.x) :(
Polybos
+5  A: 

Bugs are particularly transient, for example @Pestilence's answer was found in GCC 4.4.0 and fixed in 4.4.1. For a list of current ways to bring GCC to an infinite loop, check their Bugzilla.

Potatoswatter
I like that answer. I'll wait if there will be some more, but probably I'll accept it.
Polybos
+4  A: 

Since C++ template metaprogramming is in fact Turing complete you can make a never ending compilation.

For example:

template<typename T>
struct Loop {
   typedef typename Loop<Loop<T> >::Temp Temp;
};

int main(int, char**) {
   Loop<int> n;
   return 0;
}

However, like the answer before me. gcc has a flag to stop this from continuing endlessly (Much like a stack overflow in an infinite recursion).

Shiroko
Hmm. Last time I check `man gcc` it had option specifing maximum size of 'stack'. So it is impossible as it is no way of working around the finity of iterations.
Maciej Piechotka
``template instantiation depth exceeds maximum of 500`
Polybos