Example: provenance_via_io_percentp_global.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
int x=1;
int main() {
int *p = &x;
FILE *f = fopen(
"provenance_via_io_percentp_global.tmp","w+b");
printf("Addresses: p=%p\n",(void*)p);
// print pointer address to a file
fprintf(f,"%p\n",(void*)p);
rewind(f);
void *rv;
int n = fscanf(f,"%p\n",&rv);
int *r = (int *)rv;
if (n != 1) exit(EXIT_FAILURE);
printf("Addresses: r=%p\n",(void*)r);
// are r and p now equivalent?
*r=12; // is this free of undefined behaviour?
_Bool b1 = (r==p); // do they compare equal?
_Bool b2 = (0==memcmp(&r,&p,sizeof(r)));//same reps?
printf("x=%i *r=%i b1=%s b2=%s\n",x,*r,
b1?"true":"false",b2?"true":"false");
}
[link to test in Cerberus and Compiler Explorer]

Experimental data (what does this mean?)

gcc-8.1-O0 Addresses: p=0x600cf0
Addresses: r=0x600cf0
x=12 *r=12 b1=true b2=true
gcc-8.1-O2 Addresses: p=0x600c38
Addresses: r=0x600c38
x=12 *r=12 b1=true b2=true
gcc-8.1-O3 Addresses: p=0x600c38
Addresses: r=0x600c38
x=12 *r=12 b1=true b2=true
gcc-8.1-O2-no-strict-aliasing Addresses: p=0x600c38
Addresses: r=0x600c38
x=12 *r=12 b1=true b2=true
gcc-8.1-O3-no-strict-aliasing Addresses: p=0x600c38
Addresses: r=0x600c38
x=12 *r=12 b1=true b2=true
clang-6.0-O0 Addresses: p=0x601068
Addresses: r=0x601068
x=12 *r=12 b1=true b2=true
clang-6.0-O2 Addresses: p=0x601060
Addresses: r=0x601060
x=12 *r=12 b1=true b2=true
clang-6.0-O3 Addresses: p=0x601060
Addresses: r=0x601060
x=12 *r=12 b1=true b2=true
clang-6.0-O2-no-strict-aliasing Addresses: p=0x601060
Addresses: r=0x601060
x=12 *r=12 b1=true b2=true
clang-6.0-O3-no-strict-aliasing Addresses: p=0x601060
Addresses: r=0x601060
x=12 *r=12 b1=true b2=true
clang-6.0-UBSAN Addresses: p=0x632b80
Addresses: r=0x632b80
x=12 *r=12 b1=true b2=true
clang-6.0-ASAN Addresses: p=0x716b80
Addresses: r=0x716b80
x=12 *r=12 b1=true b2=true
clang-6.0-MSAN Addresses: p=0x6b7b00
Addresses: r=0x6b7b00
x=12 *r=12 b1=true b2=true
icc-19-O0 Addresses: p=0x600e70
Addresses: r=0x600e70
x=12 *r=12 b1=true b2=true
icc-19-O2 Addresses: p=0x6046e0
Addresses: r=0x6046e0
x=12 *r=12 b1=true b2=true
icc-19-O3 Addresses: p=0x6046e0
Addresses: r=0x6046e0
x=12 *r=12 b1=true b2=true
icc-19-O2-no-strict-aliasing Addresses: p=0x6046e0
Addresses: r=0x6046e0
x=12 *r=12 b1=true b2=true
icc-19-O3-no-strict-aliasing Addresses: p=0x6046e0
Addresses: r=0x6046e0
x=12 *r=12 b1=true b2=true
cerberus-concrete exit codes: compile 0 / execute 1 provenance_via_io_percentp_global.c:17:20: error: [desug] use of undeclared identifier '__cerbvar_EXIT_FAILURE'. (§6.5.1#2)
if (n != 1) exit(EXIT_FAILURE);
^
cerberus-symbolic exit codes: compile 0 / execute 1 provenance_via_io_percentp_global.c:17:20: error: [desug] use of undeclared identifier '__cerbvar_EXIT_FAILURE'. (§6.5.1#2)
if (n != 1) exit(EXIT_FAILURE);
^
gcc-4.9-shadowprov exit codes: compile 0 / execute 134
CHERI:MIPS-O0 Addresses: p=0x30020
Addresses: r=0x30020
x=12 *r=12 b1=true b2=true
CHERI:MIPS-O2 Addresses: p=0x30020
Addresses: r=0x30020
x=12 *r=12 b1=true b2=true
CHERI:MIPS-O2-no-strict-aliasing Addresses: p=0x30020
Addresses: r=0x30020
x=12 *r=12 b1=true b2=true
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 exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-uintcap-offset-exact-equals exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset-exact-equals exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
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 exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-uintcap-offset exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
CHERI:CHERI-O2-no-strict-aliasing-uintcap-offset exit codes: compile 0 / execute -1 Terminated with signal 34: In-address space security exception
RV-Match exit codes: compile 0 / execute 139 Addresses: p=(nil)
Addresses: r=(nil)
Dereferencing a null pointer:
> in main at provenance_via_io_percentp_global.c:20: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 provenance_via_io_percentp_global.c:4:10: fatal error: inttypes.h: No such file or directory
#include <inttypes.h>
^~~~~~~~~~~~
compilation terminated.
compcert-3.2 Addresses: p=0x601068
Addresses: r=0x601068
x=12 *r=12 b1=true b2=true
compcert-3.2-O Addresses: p=0x601068
Addresses: r=0x601068
x=12 *r=12 b1=true b2=true
compcert-3.2-interp Time 0: calling main()
--[step_internal_function]-->
Time 1: in function main, statement
p = &x;
f = fopen(__stringlit_2, __stringlit_1);
printf(__stringlit_3, (void *) p);
fprintf(f, __stringlit_4, (void *) p);
rewind(f);
n = fscanf(f, __stringlit_4, &rv);
r = (int *) rv;
if (n != 1) {
exit(1);
}
printf(__stringlit_5, (void *) r);
*r = 12;
b1 = r == p;
b2 = 0 == memcmp(&r, &p, sizeof(int *));
printf(__stringlit_8, x, *r,
b1 ? __stringlit_7 : __stringlit_6,
b2 ? __stringlit_7 : __stringlit_6);
return 0;
--[step_seq]-->
Time 2: in function main, statement
p = &x;
f = fopen(__stringlit_2, __stringlit_1);
printf(__stringlit_3, (void *) p);
fprintf(f, __stringlit_4, (void *) p);
rewind(f);
n = fscanf(f, __stringlit_4, &rv);
r = (int *) rv;
if (n != 1) {
exit(1);
}
printf(__stringlit_5, (void *) r);
*r = 12;
b1 = r == p;
b2 = 0 == memcmp(&r, &p, sizeof(int *));
printf(__stringlit_8, x, *r,
b1 ? __stringlit_7 : __stringlit_6,
b2 ? __stringlit_7 : __stringlit_6);
--[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
f = fopen(__stringlit_2, __stringlit_1);
printf(__stringlit_3, (void *) p);
fprintf(f, __stringlit_4, (void *) p);
rewind(f);
n = fscanf(f, __stringlit_4, &rv);
r = (int *) rv;
if (n != 1) {
exit(1);
}
printf(__stringlit_5, (void *) r);
*r = 12;
b1 = r == p;
b2 = 0 == memcmp(&r, &p, sizeof(int *));
printf(__stringlit_8, x, *r,
b1 ? __stringlit_7 : __stringlit_6,
b2 ? __stringlit_7 : __stringlit_6);
--[step_seq]-->
Time 11: in function main, statement f = fopen(__stringlit_2, __stringlit_1);
--[step_do_1]-->
Time 12: in function main, expression f = fopen(__stringlit_2, __stringlit_1)
--[red_var_local]-->
Time 13: in function main, expression
<loc f> = fopen(__stringlit_2, __stringlit_1)
--[red_var_global]-->
Time 14: in function main, expression
<loc f> = <loc fopen>(__stringlit_2, __stringlit_1)
--[red_rvalof]-->
Time 15: in function main, expression
<loc f> = <ptr fopen>(__stringlit_2, __stringlit_1)
--[red_var_global]-->
Time 16: in function main, expression
<loc f> = <ptr fopen>(<loc __stringlit_2>, __stringlit_1)
--[red_rvalof]-->
Time 17: in function main, expression
<loc f> = <ptr fopen>(<ptr __stringlit_2>, __stringlit_1)
--[red_var_global]-->
Time 18: in function main, expression
<loc f> = <ptr fopen>(<ptr __stringlit_2>, <loc __stringlit_1>)
--[red_rvalof]-->
Time 19: in function main, expression
<loc f> = <ptr fopen>(<ptr __stringlit_2>, <ptr __stringlit_1>)
--[red_call]-->
Time 20: calling fopen(<ptr __stringlit_2>, <ptr __stringlit_1>)
Stuck state: calling fopen(<ptr __stringlit_2>, <ptr __stringlit_1>)
ERROR: Undefined behavior
In file included from provenance_via_io_percentp_global.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.