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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected]

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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to