Example: pointer_offset_from_int_subtraction_global_xy.c

up: index
prev: provenance_basic_using_uintptr_t_auto_yx.c
next: pointer_offset_from_int_subtraction_global_yx.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    #include <stdio.h>
    #include <string.h> 
    #include <stdint.h>
    #include <inttypes.h>
    int x=1, y=2;
    int main() {
      uintptr_t ux = (uintptr_t)&x;
      uintptr_t uy = (uintptr_t)&y;
      uintptr_t offset = uy - ux;
      printf("Addresses: &x=%"PRIuPTR" &y=%"PRIuPTR\
             " offset=%"PRIuPTR" \n",(unsigned long)ux,(unsigned long)uy,(unsigned long)offset);
      int *p = (int *)(ux + offset);
      int *q = &y;
      if (memcmp(&p, &q, sizeof(p)) == 0) {
        *p = 11; // is this free of UB?
        printf("x=%d y=%d *p=%d *q=%d\n",x,y,*p,*q); 
      }
    }
[link to run test in Cerberus]

Experimental data (what does this mean?)

cerberus-concrete-PVI-plain Addresses: &x=4294967268 &y=4294967264 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
cerberus-concrete-PVI-ae Addresses: &x=4294967268 &y=4294967264 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
cerberus-concrete-PVI-ae-udi Addresses: &x=4294967268 &y=4294967264 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
gcc-8.3-O0 Addresses: &x=4294971424 &y=4294971428 offset=4
x=1 y=11 *p=11 *q=11
gcc-8.3-O2 Addresses: &x=4294971420 &y=4294971416 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
gcc-8.3-O3 Addresses: &x=4294971420 &y=4294971416 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
gcc-8.3-O2-no-strict-aliasing Addresses: &x=4294971420 &y=4294971416 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
gcc-8.3-O3-no-strict-aliasing Addresses: &x=4294971420 &y=4294971416 offset=18446744073709551612
x=1 y=11 *p=11 *q=11
clang-7.0.1-O0 Addresses: &x=4294971416 &y=4294971420 offset=4
x=1 y=11 *p=11 *q=11
clang-7.0.1-O2 Addresses: &x=4294971416 &y=4294971420 offset=4
x=1 y=11 *p=11 *q=11
clang-7.0.1-O3 Addresses: &x=4294971416 &y=4294971420 offset=4
x=1 y=11 *p=11 *q=11
clang-7.0.1-O2-no-strict-aliasing Addresses: &x=4294971416 &y=4294971420 offset=4
x=1 y=11 *p=11 *q=11
clang-7.0.1-O3-no-strict-aliasing Addresses: &x=4294971416 &y=4294971420 offset=4
x=1 y=11 *p=11 *q=11
icc-19-O0 Addresses: &x=6294408 &y=6294412 offset=4
x=1 y=11 *p=11 *q=11
icc-19-O2 Addresses: &x=6309568 &y=6309572 offset=4
x=1 y=11 *p=11 *q=11
icc-19-O3 Addresses: &x=6309568 &y=6309572 offset=4
x=1 y=11 *p=11 *q=11
icc-19-O2-no-strict-aliasing Addresses: &x=6309568 &y=6309572 offset=4
x=1 y=11 *p=11 *q=11
icc-19-O3-no-strict-aliasing Addresses: &x=6309568 &y=6309572 offset=4
x=1 y=11 *p=11 *q=11