Example: provenance_union_punning_2_global_yx.c

up: index
prev: provenance_union_punning_2_global_xy.c
next: provenance_union_punning_2_auto_xy.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    #include <stdio.h>
    #include <string.h> 
    #include <inttypes.h>
    int y=2, x=1;
    typedef union { uintptr_t ui; int *p; } un;
    int main() {
      un u; 
      int *px = &x;
      uintptr_t i = (uintptr_t)px;
      i = i + sizeof(int);
      u.ui = i;
      int *p = u.p;
      int *q = &y;
      printf("Addresses: p=%p q=%p\n",(void*)p,(void*)q);
      if (memcmp(&p, &q, sizeof(p)) == 0) {
        *p = 11;  // does this have undefined behaviour?
        printf("x=%d y=%d *p=%d *q=%d\n",x,y,*p,*q);
      }
      return 0;
    }
[link to run test in Cerberus]

Experimental data (what does this mean?)

cerberus-concrete-PVI-plain Addresses: p=(@2, 0xffffffe4) q=(@2, 0xffffffe4)
x=1 y=11 *p=11 *q=11
cerberus-concrete-PVI-ae Undefined behaviour: the operand of the unary '*' operator has an invalid value at 16:5-7
cerberus-concrete-PVI-ae-udi Undefined behaviour: out of bounds pointer at memory store at 16:5-12
gcc-8.3-O0 Addresses: p=0x100001028 q=0x100001020
gcc-8.3-O2 Addresses: p=0x10000101c q=0x10000101c
x=1 y=2 *p=11 *q=2
gcc-8.3-O3 Addresses: p=0x10000101c q=0x10000101c
x=1 y=2 *p=11 *q=2
gcc-8.3-O2-no-strict-aliasing Addresses: p=0x10000101c q=0x10000101c
x=1 y=2 *p=11 *q=2
gcc-8.3-O3-no-strict-aliasing Addresses: p=0x10000101c q=0x10000101c
x=1 y=2 *p=11 *q=2
clang-7.0.1-O0 Addresses: p=0x100001020 q=0x100001018
clang-7.0.1-O2 Addresses: p=0x100001020 q=0x100001018
clang-7.0.1-O3 Addresses: p=0x100001020 q=0x100001018
clang-7.0.1-O2-no-strict-aliasing Addresses: p=0x100001020 q=0x100001018
clang-7.0.1-O3-no-strict-aliasing Addresses: p=0x100001020 q=0x100001018
icc-19-O0 Addresses: p=0x600b80 q=0x600b78
icc-19-O2 Addresses: p=0x6046c8 q=0x6046c0
icc-19-O3 Addresses: p=0x6046c8 q=0x6046c0
icc-19-O2-no-strict-aliasing Addresses: p=0x6046c8 q=0x6046c0
icc-19-O3-no-strict-aliasing Addresses: p=0x6046c8 q=0x6046c0