<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - std::memcpy with incorrect align attribute on arguments introduces undefined behavior"
   href="https://bugs.llvm.org/show_bug.cgi?id=49708">49708</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::memcpy with incorrect align attribute on arguments introduces undefined behavior
          </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>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>gonzalo.gadeschi@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Example (<a href="https://clang.godbolt.org/z/7r8qndWzG">https://clang.godbolt.org/z/7r8qndWzG</a>):

#include <cstring>
void f(int* src, int* dst) {
  std::memcpy(src, dst, sizeof(int));
}

void g(char* a, char *b) {
    f((int*)a, (int*)b);
}

lowers to: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %6, i8* align 4 %8,
i64 4, i1 false)

The "align 4" in the pointer arguments of memcpy is incorrect.

No C++ code loads/stores from the src and dst pointers at type "int", and IIUC
std::memcpy(void*,void*,size) access its arguments at type "unsigned char*", so
TBAA does not allow inferring that the pointers are 4 byte aligned in the
memcpy.

As a consequence, the memcpy loads/stores with an incorrect alignment, and the
behavior is undefined.</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>