[llvm-bugs] [Bug 28767] New: Clang miscompiles placement-new

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jul 29 04:57:32 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28767

            Bug ID: 28767
           Summary: Clang miscompiles placement-new
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Keywords: miscompilation
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: hstong at ca.ibm.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

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: http://melpon.org/wandbox/permlink/h0aEUwT90gAnbGtv

### 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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160729/07d078f4/attachment.html>


More information about the llvm-bugs mailing list