Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bring GCC into an infinite loop?

Tags:

gcc

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?

like image 795
Karl von Moor Avatar asked Mar 11 '10 21:03

Karl von Moor


People also ask

Is it possible to have an infinite for loop?

A for loop is only another syntax for a while loop. Everything which is possible with one of them is also possible with the other one. Any for loop where the termination condition can never be met will be infinite: for($i = 0; $i > -1; $i++) { ... }

When would you need an infinite loop?

Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously, such as product demo s or in programming for embedded system s.

What structure should be used to create an infinite loop in a block based program?

Answer: while infinite_loop" is the correct answers. We use "while infinite_loop" to create an infinite loop. An infinite loop is a sequence of instructions that continues endlessly, unless an external intervention occurs.


3 Answers

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.

like image 101
pestilence669 Avatar answered Nov 11 '22 16:11

pestilence669


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.

EDIT: I just found a new way, which also crashes Comeau. This is a more satisfying answer, for now. Of course, it should also be fixed soon.

template< int n >
struct a { 
    a< n+1 > operator->() { return a< n+1 >(); }
};

int main() {
    a<0>()->x;
}
like image 20
Potatoswatter Avatar answered Nov 11 '22 16:11

Potatoswatter


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).

like image 4
Shiroko Avatar answered Nov 11 '22 14:11

Shiroko