<html>
    <head>
      <base href="https://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 --- - Clang miscompiles placement-new"
   href="https://llvm.org/bugs/show_bug.cgi?id=28767">28767</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang miscompiles placement-new
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Keywords</th>
          <td>miscompilation
          </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>hstong@ca.ibm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the following program, the only iteration of the loop increments the int
object pointed-to by p, and then replaces said object via q using
placement-new.

Clang miscompiles at -O1 at least on x86_64-unknown-linux-gnu and
powerpc64le-unknown-linux-gnu.

Observation: passes with -fno-strict-aliasing.

Observation: -mllvm -print-before-all is indistinguishable between the
placement-new and the commented-out reinterpret_cast.

Online compiler: <a href="http://melpon.org/wandbox/permlink/h0aEUwT90gAnbGtv">http://melpon.org/wandbox/permlink/h0aEUwT90gAnbGtv</a>

### SOURCE (<stdin>):
void *operator new(decltype(sizeof 0), void *) noexcept;
extern "C" void abort();

typedef int A;
typedef float B;

B *qq;

void foo(A *p, A *q, long unk) {
   for (long i = 0; i < unk; ++i) {
      ++*p;
      qq = new (static_cast<void *>(&q[i])) B(42);
      // Note: the following is a TBAA violation.
      //qq = &(*reinterpret_cast<B *>(static_cast<void *>(&q[i])) = B(42));
   }
}

void (*volatile fp)(A *, A *, long);

int main(void) {
   union { A x; B f; } u = { 0 };
   fp = foo;
   fp(&u.x, &u.x, 1);
   if (*qq != 42) abort();
}


### COMMANDS TO REPRODUCE:
clang -O1 -o ./a.out -x c++ -std=c++11 -
./a.out


### EXPECTED OUTPUT:
(rc=0)


### ACTUAL OUTPUT:
Aborted (core dumped)
Return:  0x86:134


### COMPILER VERSION INFO (clang -v):
clang version 4.0.0 (trunk 276983) (llvm/trunk 276982)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/llvm-head/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.3
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64</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>