https://llvm.org/bugs/show_bug.cgi?id=25474
Bug ID: 25474 Summary: Copy construction performs unsolicited reads of volatile derived-class members in padding Product: clang Version: trunk Hardware: All OS: Linux Status: NEW Severity: normal Priority: P Component: C++ Assignee: unassignedclangb...@nondot.org Reporter: hst...@ca.ibm.com CC: dgre...@apple.com, llvm-bugs@lists.llvm.org Classification: Unclassified 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: http://melpon.org/wandbox/permlink/91iSbt94RCoqH9d8 ### 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 -- You are receiving this mail because: You are on the CC list for the bug.
_______________________________________________ llvm-bugs mailing list llvm-bugs@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs