<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Infinite loops in generated assembly when using -D_FORTIFY_SOURCE=2 and -O1"
   href="http://llvm.org/bugs/show_bug.cgi?id=16821">16821</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Infinite loops in generated assembly when using -D_FORTIFY_SOURCE=2 and -O1
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>zandobersek@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=10999" name="attach_10999" title="Test case">attachment 10999</a> <a href="attachment.cgi?id=10999&action=edit" title="Test case">[details]</a></span>
Test case

Attached is a reduced and simplified test case (originating from WebKit) that
loops infinitely when compiled compiled with Clang and specifying both
-D_FORTIFY_SOURCE=2 and -O1 on the command line.

Breaking the loop under GDB stops in the randomValue function and disassembly
sure enough shows the problem:
-----
Program received signal SIGINT, Interrupt.
0x0000000000400620 in randomValue(unsigned char*, unsigned long) ()
(gdb) disassemble 
Dump of assembler code for function _Z11randomValuePhm:
   0x0000000000400600 <+0>:    push   %rax
   0x0000000000400601 <+1>:    mov    $0x4006f4,%edi
   0x0000000000400606 <+6>:    xor    %esi,%esi
   0x0000000000400608 <+8>:    xor    %edx,%edx
   0x000000000040060a <+10>:    xor    %eax,%eax
   0x000000000040060c <+12>:    callq  0x4004f0 <open@plt>
   0x0000000000400611 <+17>:    test   %eax,%eax
   0x0000000000400613 <+19>:    js     0x400622 <_Z11randomValuePhm+34>
   0x0000000000400615 <+21>:    data32 nopw %cs:0x0(%rax,%rax,1)
=> 0x0000000000400620 <+32>:    jmp    0x400620 <_Z11randomValuePhm+32>
   0x0000000000400622 <+34>:    callq  0x4005f0 <WTFCrash>
End of assembler dump.
-----

The clang command used:
clang++ -D_FORTIFY_SOURCE=2 -O2 -stdlib=libstdc++ -o test test.cpp

Notes:
- setting _FORTIFY_SOURCE to 0 or disabling any optimizations fixes the
problem,
- the problem persists when specifying any other non-null level of
optimization,
- I'm using Clang 3.4 (trunk) on Ubuntu 13.04, hence also the libstdc++ choice,
- I've experienced the same problem with older versions of Clang, from at least
3.0 onwards.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>