Example: pointer_copy_memcpy.c

#include <stdio.h>
#include <string.h>
int x=1;
int main() {
int *p = &x;
int *q;
memcpy (&q, &p, sizeof p);
*q = 11; // is this free of undefined behaviour?
printf("*p=%d *q=%d\n",*p,*q);
}
[link to test in Cerberus and Compiler Explorer]

Experimental data (what does this mean?)

gcc-8.1-O0 *p=11 *q=11
gcc-8.1-O2 *p=11 *q=11
gcc-8.1-O3 *p=11 *q=11
gcc-8.1-O2-no-strict-aliasing *p=11 *q=11
gcc-8.1-O3-no-strict-aliasing *p=11 *q=11
clang-6.0-O0 *p=11 *q=11
clang-6.0-O2 *p=11 *q=11
clang-6.0-O3 *p=11 *q=11
clang-6.0-O2-no-strict-aliasing *p=11 *q=11
clang-6.0-O3-no-strict-aliasing *p=11 *q=11
clang-6.0-UBSAN *p=11 *q=11
clang-6.0-ASAN *p=11 *q=11
clang-6.0-MSAN *p=11 *q=11
icc-19-O0 *p=11 *q=11
icc-19-O2 *p=11 *q=11
icc-19-O3 *p=11 *q=11
icc-19-O2-no-strict-aliasing *p=11 *q=11
icc-19-O3-no-strict-aliasing *p=11 *q=11
cerberus-concrete BEGIN EXEC[0]
Defined {value: "Specified(0)", stdout: "*p=11 *q=11\n", blocked: "false"}
END EXEC[0]
Time spent: 0.030925 seconds
cerberus-symbolic exit codes: compile 0 / execute 1 cerberus: internal error, uncaught exception:
Failure("WIP: Symbolic memcpy")

gcc-4.9-shadowprov *p=11 *q=11
CHERI:MIPS-O0 *p=11 *q=11
CHERI:MIPS-O2 *p=11 *q=11
CHERI:MIPS-O2-no-strict-aliasing *p=11 *q=11
CHERI:CHERI-O0-uintcap-addr-exact-equals *p=11 *q=11
CHERI:CHERI-O2-uintcap-addr-exact-equals *p=11 *q=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-addr-exact-equals *p=11 *q=11
CHERI:CHERI-O0-uintcap-offset-exact-equals *p=11 *q=11
CHERI:CHERI-O2-uintcap-offset-exact-equals *p=11 *q=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset-exact-equals *p=11 *q=11
CHERI:CHERI-O0-uintcap-addr *p=11 *q=11
CHERI:CHERI-O2-uintcap-addr *p=11 *q=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-addr *p=11 *q=11
CHERI:CHERI-O0-uintcap-offset *p=11 *q=11
CHERI:CHERI-O2-uintcap-offset *p=11 *q=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset *p=11 *q=11
RV-Match exit codes: compile 0 / execute 1
ch2o Fatal error: exception Main.CH2O_error("variable `memcpy` not found")
Raised at file "bytes.ml", line 220, characters 25-34
Called from file "bytes.ml", line 248, characters 15-34
compcert-3.2 *p=11 *q=11
compcert-3.2-O *p=11 *q=11
compcert-3.2-interp Time 0: calling main()
--[step_internal_function]-->
Time 1: in function main, statement
p = &x;
memcpy(&q, &p, sizeof(int *));
*q = 11;
printf(__stringlit_1, *p, *q);
return 0;
--[step_seq]-->
Time 2: in function main, statement
p = &x;
memcpy(&q, &p, sizeof(int *));
*q = 11;
printf(__stringlit_1, *p, *q);
--[step_seq]-->
Time 3: in function main, statement p = &x;
--[step_do_1]-->
Time 4: in function main, expression p = &x
--[red_var_local]-->
Time 5: in function main, expression <loc p> = &x
--[red_var_global]-->
Time 6: in function main, expression <loc p> = &<loc x>
--[red_addrof]-->
Time 7: in function main, expression <loc p> = <ptr x>
--[red_assign]-->
Time 8: in function main, expression <ptr x>
--[step_do_2]-->
Time 9: in function main, statement /*skip*/;
--[step_skip_seq]-->
Time 10: in function main, statement
memcpy(&q, &p, sizeof(int *)); *q = 11; printf(__stringlit_1, *p, *q);
--[step_seq]-->
Time 11: in function main, statement memcpy(&q, &p, sizeof(int *));
--[step_do_1]-->
Time 12: in function main, expression memcpy(&q, &p, sizeof(int *))
--[red_var_global]-->
Time 13: in function main, expression <loc memcpy>(&q, &p, sizeof(int *))
--[red_rvalof]-->
Time 14: in function main, expression <ptr memcpy>(&q, &p, sizeof(int *))
--[red_var_local]-->
Time 15: in function main, expression
<ptr memcpy>(&<loc q>, &p, sizeof(int *))
--[red_addrof]-->
Time 16: in function main, expression
<ptr memcpy>(<ptr q>, &p, sizeof(int *))
--[red_var_local]-->
Time 17: in function main, expression
<ptr memcpy>(<ptr q>, &<loc p>, sizeof(int *))
--[red_addrof]-->
Time 18: in function main, expression
<ptr memcpy>(<ptr q>, <ptr p>, sizeof(int *))
--[red_sizeof]-->
Time 19: in function main, expression <ptr memcpy>(<ptr q>, <ptr p>, 4U)
--[red_call]-->
Time 20: calling memcpy(<ptr>, <ptr>, 4)
Stuck state: calling memcpy(<ptr>, <ptr>, 4)
ERROR: Undefined behavior
In file included from pointer_copy_memcpy.c:1:
In file included from /usr/include/stdio.h:64:
In file included from /usr/include/_stdio.h:68:
/usr/include/sys/cdefs.h:81:2: warning: "Unsupported compiler detected" [-W#warnings]
#warning "Unsupported compiler detected"
^
1 warning generated.