<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64397>64397</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
__builtin_memmove restrictions not enforced; gives different behavior at compile time and run time
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
JohelEGP
</td>
</tr>
</table>
<pre>
See <https://godbolt.org/z/3ffdPTPMY>.
```C++
#include <stdio.h>
struct B {
B() = default;
int x = 0;
short pad = 0;
};
struct D : B {
constexpr D(int y) : y(y) {}
short y;
};
static_assert(__is_trivially_copyable(D));
static_assert(sizeof(D) == sizeof(B));
constexpr int test() {
D a = {1};
D b = {2};
__builtin_memmove((B*)&b, (B*)&a, 8);
return b.y;
}
int main() {
static int global = test();
int local = test();
printf("global = %d, local = %d\n", global, local);
}
```
> This prints: `global = 2, local = 1`
>
> During constant-evaluation, `__builtin_memmove` seems to ignore the number of bytes passed and just always copy the non-padding bytes. During runtime, it copies padding bytes also. This is confusing, and maybe unintentional. [The documentation says](https://clang.llvm.org/docs/LanguageExtensions.html#memory-builtins) "Constant evaluation support is only provided when the source and destination are pointers to arrays with the same trivially copyable element type, and the given size is an exact multiple of the element size that is no greater than the number of elements accessible through the source and destination operands." But these restrictions are not enforced at compile time; instead, it behaves as above. Compare to `__builtin_memcmp`, which does enforce its documented restrictions.
@philnik777 This might be relevant to your recent `std::copy` improvements.
Source: Private communications.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VsmO4zYQ_Rr6UmhBprwefGjb7QBBAjQwc8nJoMSSxAlFClzcrfn6oCjvnRlAsCnW9urxsSDhvWoM4obNt2y-n4gYWus2f9oW9dsf75PSymHzDRFYsWtD6D0rXhk_MH5orCytDpl1DeOHn4wfirqW79_f__6HFW8Zy_csf2WLfHx2jG_pGXd5oUylo0xpfZDKZi0r3s7W9OuDi1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