-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy path05-adapted_from_01_09_array.c
122 lines (97 loc) · 2.42 KB
/
05-adapted_from_01_09_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// PARAM: --enable ana.int.interval --set ana.base.arrays.domain partitioned
#include<stdio.h>
#include <goblint.h>
int fun_5() { return 5; }
int fun_6() { return 6; }
int fun_5b() { return 5; }
struct kala {
int a[5];
};
struct kass {
int v;
};
int main () {
int i,t, k1,k2,top;
int a[] = {2,2,2};
int b[2], c[3];
int (*f[2])(void) = {fun_5, fun_6};
int (*g[2])(void) = {fun_5, fun_5b};
int (*fp)(void);
int *ip;
int (*iap)[];
// really really top
if (i) top = (int) ⊤
else top = 5;
__goblint_check(a[0] == 2);
__goblint_check(a[1] == 2);
__goblint_check(a[2] == 2);
// writing to unknown index:
// NB! We assume the index is in bounds!
if (k1) i=0; else i=1;
a[i] = 0;
__goblint_check(a[0] == 0); // UNKNOWN
__goblint_check(a[1] == 0); // UNKNOWN
__goblint_check(a[2] == 0); // FAIL
// reading from unknown index:
b[0] = 2; b[1] = 2;
__goblint_check(b[i] == 2);
b[0] = 3;
__goblint_check(b[i] == 2); // UNKNOWN
// function arrays
t = f[i]();
__goblint_check(t == 5); // UNKNOWN
t = g[i]();
__goblint_check(t == 5);
// array has set of addresses:
if (k2) f[i] = fun_5b;
t = f[1]();
__goblint_check(t == 5); // UNKNOWN
// now we collect all the sets:
fp = f[i];
t = fp();
__goblint_check(t == 5); // UNKNOWN
fp = g[i];
t = fp();
__goblint_check(t == 5);
// NASTY ARRAY OPS:
c[0] = 5; c[1] = 5; c[2] = 5;
// this is not usual: a pointer to an array (easy!)
iap = &c;
t = (*iap)[2];
__goblint_check(t == 5);
// Typical C: a pointer to first element of array (difficult!)
ip = c; // this means &c[0]
// dereferencing...
__goblint_check(*ip == 5);
// pointing into the array
ip = &c[1];
__goblint_check(*ip == 5);
// and some pointer arithmetic (tests are meaningless)
*ip = 6;
ip++;
__goblint_check(*ip == 5);
// Now testing arrays inside structs.
struct kala x;
ip = x.a;
x.a[0] = 7;
__goblint_check(*ip == 7);
// (typeless) Top index
__goblint_check(x.a[top] == 7); // UNKNOWN
// And finally array of structs
struct kala xs[5];
xs[0] = x;
ip = &xs[0].a[0];
struct kass k[1];
k[0].v = 42;
__goblint_check(k[0].v == 42);
// multi-dim arrays
int ma[1][1];
ma[0][0] = 42;
__goblint_check(ma[0][0] == 42);
//i = hash("kala");
//printf("Hash value: %d", i);
// NB arrays must be in bounds... otherwise everything fails!
// It's not possible to analyze this:
// a[3] = 666;
return 0;
}