<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 --- - Copy construction performs unsolicited reads of volatile derived-class members in padding"
   href="https://llvm.org/bugs/show_bug.cgi?id=25474">25474</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Copy construction performs unsolicited reads of volatile derived-class members in padding
          </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>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>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>It is observed that Clang generates reads into the tail padding of non-POD
classes, which is unsafe because said tail padding may (in the complete object)
be associated with volatile members.

The following code demonstrates the issue on typical x86 Linux systems. A
segmentation fault does not occur when the members of the A subobject are
"manually" copied, but the code generated by Clang for the copy construction
causes a segmentation fault.

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

### REPRODUCTION SCRIPT:
clang++ -x c++ -std=c++11 -<<'EOF' &&
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <new>

constexpr long pgsz = 4096;

struct A {
  ~A() {}
  struct AA {
    alignas(pgsz * 2) char aa[pgsz * 2];
  } aa;
  struct AB {
    char ab[pgsz];
  } ab;
};

struct B : A {
  struct BA {
    volatile char ba[pgsz];
  } ba;
};

static_assert(sizeof(A) == pgsz * 4, "");
static_assert(sizeof(B) == sizeof(A), "");

int main(void) {
  void *paLHS, *pbLHS;
  if (posix_memalign(&paLHS, alignof(A), sizeof(A)))
    abort();
  if (posix_memalign(&pbLHS, alignof(B), sizeof(B)))
    abort();
  B &bLHS = *new (pbLHS) B;

  int fd = open("pgszX6", O_RDONLY);
  if (fd < 0)
    abort();

  void *pb1 = mmap(0, pgsz * 6, PROT_READ, MAP_PRIVATE, fd, 0);
  if (!pb1)
    abort();

  void *pb0 = reinterpret_cast<void *>(
      (reinterpret_cast<intptr_t>(pb1) + (pgsz * 2 - 1)) / (pgsz * 2) *
      (pgsz * 2));
  B *pb = new (pb0) B;

  if (mprotect(&pb->ba, pgsz, PROT_NONE))
    abort();

  fprintf(stderr, "Page size: %ld\n", sysconf(_SC_PAGESIZE));

  fprintf(stderr, "About to perform assignment member-by-member...\n");
  bLHS.aa = pb->aa;
  bLHS.ab = pb->ab;

  fprintf(stderr, "About to perform copy construction...\n");
  new (paLHS) A(*static_cast<A *>(pb));
}
EOF
dd ibs=$(( 4096 * 6 )) count=1 if=/dev/zero of=pgszX6 && ./a.out


### ACTUAL OUTPUT:
Page size: 4096
About to perform assignment member-by-member...
About to perform copy construction...
prog.sh: line 64:    30 Segmentation fault      ./a.out


### EXPECTED OUTPUT (no segmentation fault):
Page size: 4096
About to perform assignment member-by-member...
About to perform copy construction...


### COMPILER VERSION INFO (clang++ -v):
clang version 3.8.0 (trunk 252469)
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>