[llvm-bugs] [Bug 49708] New: std::memcpy with incorrect align attribute on arguments introduces undefined behavior

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 24 01:49:26 PDT 2021


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

            Bug ID: 49708
           Summary: std::memcpy with incorrect align attribute on
                    arguments introduces undefined behavior
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: gonzalo.gadeschi at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Example (https://clang.godbolt.org/z/7r8qndWzG):

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

-- 
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/20210324/917b7aa9/attachment.html>


More information about the llvm-bugs mailing list