Example: pointer_arith_algebraic_properties_3_auto.c

#include <stdio.h>
#include <inttypes.h>
int main() {
int y[2], x[2];
int *p=(int*)(
(((uintptr_t)&(x[0])) + ((uintptr_t)&(y[1])))
-((uintptr_t)&(y[0])) );
*p = 11; // is this free of undefined behaviour?
//(equivalent to the &x[0]+(&(y[1])-&(y[0])) version?)
printf("x[1]=%d *p=%d\n",x[1],*p);
return 0;
}
[link to test in Cerberus and Compiler Explorer]

Experimental data (what does this mean?)

gcc-8.1-O0 x[1]=11 *p=11
gcc-8.1-O2 x[1]=11 *p=11
gcc-8.1-O3 x[1]=11 *p=11
gcc-8.1-O2-no-strict-aliasing x[1]=11 *p=11
gcc-8.1-O3-no-strict-aliasing x[1]=11 *p=11
clang-6.0-O0 x[1]=11 *p=11
clang-6.0-O2 x[1]=11 *p=11
clang-6.0-O3 x[1]=11 *p=11
clang-6.0-O2-no-strict-aliasing x[1]=11 *p=11
clang-6.0-O3-no-strict-aliasing x[1]=11 *p=11
clang-6.0-UBSAN x[1]=11 *p=11
clang-6.0-ASAN x[1]=11 *p=11
clang-6.0-MSAN x[1]=11 *p=11
icc-19-O0 x[1]=11 *p=11
icc-19-O2 x[1]=11 *p=11
icc-19-O3 x[1]=11 *p=11
icc-19-O2-no-strict-aliasing x[1]=11 *p=11
icc-19-O3-no-strict-aliasing x[1]=11 *p=11
cerberus-concrete BEGIN EXEC[0]
Killed {msg: MerrAccess Store [pointer_arith_algebraic_properties_3_auto.c:8:3-10] OutOfBoundPtr}
END EXEC[0]
Time spent: 0.015064 seconds
cerberus-symbolic 
gcc-4.9-shadowprov x[1]=11 *p=11
CHERI:MIPS-O0 x[1]=11 *p=11
CHERI:MIPS-O2 x[1]=11 *p=11
CHERI:MIPS-O2-no-strict-aliasing x[1]=11 *p=11
CHERI:CHERI-O0-uintcap-addr-exact-equals exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-uintcap-addr-exact-equals exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-no-strict-aliasing-uintcap-addr-exact-equals exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O0-uintcap-offset-exact-equals x[1]=11 *p=11
CHERI:CHERI-O2-uintcap-offset-exact-equals x[1]=11 *p=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset-exact-equals x[1]=11 *p=11
CHERI:CHERI-O0-uintcap-addr exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-uintcap-addr exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-no-strict-aliasing-uintcap-addr exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O0-uintcap-offset x[1]=11 *p=11
CHERI:CHERI-O2-uintcap-offset x[1]=11 *p=11
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset x[1]=11 *p=11
RV-Match exit codes: compile 0 / execute 139 A pointer (or array subscript) outside the bounds of an object:
> in main at pointer_arith_algebraic_properties_3_auto.c:5:3

Undefined behavior (UB-CEA1):
see C11 section 6.5.6:8 http://rvdoc.org/C11/6.5.6
see C11 section J.2:1 item 46 http://rvdoc.org/C11/J.2
see CERT-C section ARR30-C http://rvdoc.org/CERT-C/ARR30-C
see CERT-C section ARR37-C http://rvdoc.org/CERT-C/ARR37-C
see CERT-C section STR31-C http://rvdoc.org/CERT-C/STR31-C
see MISRA-C section 8.18:1 http://rvdoc.org/MISRA-C/8.18
see MISRA-C section 8.1:3 http://rvdoc.org/MISRA-C/8.1

Computing pointer difference between two different objects:
> in main at pointer_arith_algebraic_properties_3_auto.c:5:3

Undefined behavior (UB-CEA5):
see C11 section 6.5.6:9 http://rvdoc.org/C11/6.5.6
see C11 section J.2:1 item 48 http://rvdoc.org/C11/J.2
see CERT-C section ARR36-C http://rvdoc.org/CERT-C/ARR36-C
see MISRA-C section 8.18:2 http://rvdoc.org/MISRA-C/8.18
see MISRA-C section 8.1:3 http://rvdoc.org/MISRA-C/8.1

Conversion from an integer to non-null pointer:
> in main at pointer_arith_algebraic_properties_3_auto.c:5:3

Implementation defined behavior (IMPL-CCV13):
see C11 section 6.3.2.3:5 http://rvdoc.org/C11/6.3.2.3
see CERT section INT36-C http://rvdoc.org/CERT/INT36-C

Indeterminate value used in an expression:
> in main at pointer_arith_algebraic_properties_3_auto.c:5:3

Undefined behavior (UB-CEE2):
see C11 section 6.2.4 http://rvdoc.org/C11/6.2.4
see C11 section 6.7.9 http://rvdoc.org/C11/6.7.9
see C11 section 6.8 http://rvdoc.org/C11/6.8
see C11 section J.2:1 item 11 http://rvdoc.org/C11/J.2
see CERT-C section EXP33-C http://rvdoc.org/CERT-C/EXP33-C
see MISRA-C section 8.9:1 http://rvdoc.org/MISRA-C/8.9
see MISRA-C section 8.1:3 http://rvdoc.org/MISRA-C/8.1

Dereferencing a null pointer:
> in main at pointer_arith_algebraic_properties_3_auto.c:8:3

Undefined behavior (UB-CER3):
see C11 section 6.5.3.2:4 http://rvdoc.org/C11/6.5.3.2
see C11 section J.2:1 item 43 http://rvdoc.org/C11/J.2
see CERT-C section EXP34-C http://rvdoc.org/CERT-C/EXP34-C
see MISRA-C section 8.1:3 http://rvdoc.org/MISRA-C/8.1

Execution failed (configuration dumped)
ch2o pointer_arith_algebraic_properties_3_auto.c:2:10: fatal error: inttypes.h: No such file or directory
#include <inttypes.h>
^~~~~~~~~~~~
compilation terminated.
compcert-3.2 x[1]=11 *p=11
compcert-3.2-O x[1]=11 *p=11
compcert-3.2-interp Time 0: calling main()
--[step_internal_function]-->
Time 1: in function main, statement
p = (int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.);
*p = 11;
printf(__stringlit_1, *(. + 1), *p);
return 0;
return 0;
--[step_seq]-->
Time 2: in function main, statement
p = (int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.);
*p = 11;
printf(__stringlit_1, *(. + 1), *p);
return 0;
--[step_seq]-->
Time 3: in function main, statement
p = (int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.);
--[step_do_1]-->
Time 4: in function main, expression
p = (int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_var_local]-->
Time 5: in function main, expression
<loc p> =
(int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_var_local]-->
Time 6: in function main, expression
<loc p> =
(int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_rvalof]-->
Time 7: in function main, expression
<loc p> =
(int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_binop]-->
Time 8: in function main, expression
<loc p> =
(int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_deref]-->
Time 9: in function main, expression
<loc p> =
(int *) ((unsigned int) &. + (unsigned int) &. - (unsigned int) &*.)
--[red_addrof]-->
Time 10: in function main, expression
<loc p> =
(int *) ((unsigned int) <ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_cast]-->
Time 11: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_var_local]-->
Time 12: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_rvalof]-->
Time 13: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_binop]-->
Time 14: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_deref]-->
Time 15: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) &. - (unsigned int) &*.)
--[red_addrof]-->
Time 16: in function main, expression
<loc p> = (int *) (<ptr x> + (unsigned int) <ptr y+4> - (unsigned int) &*.)
--[red_cast]-->
Time 17: in function main, expression
<loc p> = (int *) (<ptr x> + <ptr y+4> - (unsigned int) &*.)
Stuck state: in function main, expression
<loc p> = (int *) (<ptr x> + <ptr y+4> - (unsigned int) &*(y + 0))
Stuck subexpression: <ptr x> + <ptr y+4>
ERROR: Undefined behavior
In file included from pointer_arith_algebraic_properties_3_auto.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.