File: loops.c

package info (click to toggle)
frama-c 20140301%2Bneon%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,548 kB
  • ctags: 28,183
  • sloc: ml: 181,252; ansic: 13,776; makefile: 2,452; sh: 1,085; lisp: 178
file content (212 lines) | stat: -rw-r--r-- 6,913 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* run.config
   OPT: -check -deps -lib-entry -main f1 -slice-pragma f1 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main f1 -slice-assert f1 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main f2 -slice-pragma f2 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main f2 -slice-assert f2 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main test_infinite_loop_3 -slice-value G -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main test_infinite_loop_4 -slice-value G -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main test_infinite_loop_5 -slice-value G -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main loop -slice-value Z  -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-calls loop -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-pragma loop -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-assert loop -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main loop -slice-rd Y -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main loop -slice-rd Z -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main loop -slice-wr Y -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -main loop -slice-wr Z  -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main stop_f1 -slice-pragma stop_f1 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main stop_f1 -slice-assert stop_f1 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main stop_f2 -slice-pragma stop_f2 -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main stop_f2 -slice-assert stop_f2  -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-value Z  -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-rd Y -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-rd Z -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-wr Y -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -slice-wr Z -journal-disable -then-on 'Slicing export' -print
   OPT: -check -deps -lib-entry -main alarm -slice-threat alarm -journal-disable -then-on 'Slicing export' -print


 */

int f1 (int c) {
  int x = 0, s = 0;
  if (c) {
    while(1) { /* infinite loop */
      s++;
      //@ assert s > 0 ;
      }
    //@ assert \false ;
    }
  else
    x = 1;
   //@ slice pragma stmt;
  x ++;
  return x;
}

void f2 (int c) {
  int x1 = 0, x2 = 0;

  while (1) {
    if (c)
      x1++;
    else
      x2++;
    //@slice pragma expr x1;
    //@ assert x2 > 0 ;
  }
}

/*-------------------------------------------*/
void stop(void) __attribute__ ((noreturn)) ;

int stop_f1 (int c) {
  int x = 0, s = 0;
  if (c) {
    while(s < c) {
      s++;
      //@ assert s > 0 ;
      }
    stop () ; /* never returns */
    }
  else
    x = 1;
   //@ slice pragma stmt;
  x ++;
  return x;
}

void stop_f2 (int c) {
  int x1 = 0, x2 = 0;

  while (x1+x2 < c + 10) {
    if (c)
      x1++;
    else
      x2++;
    //@slice pragma expr x1;
    //@ assert x2 > 0 ;
    stop () ; /* never loops nor returns */
    x1++;     /* dead code */
    //@ assert \false ;
  }
}
/*-------------------------------------------*/
int G ;
void test_infinite_loop_3 (int ctrl1, int ctrl2,
                           int no_ctrl,
                           int data1, int data2,
                           int no_data) {
  G = 0 ;
  if (ctrl1) {
    G = data1 ;
    if (no_ctrl) { /* Don't control an assignment of G
                    * which leads to the return */
      G = no_data ; /* Don't affect the final value of G
                     * because the assignement
                     * does not lead to the return */
      while (1)
        G = no_data ; /* Don't affect the final value of G
                       * because the assignement
                       * does not lead to the return */
      G = no_data ; /* Don't affect the final value of G
                     * because the assignement
                     * is dead code */
      }
    if (ctrl2)
      G = data2 ;
    }
  return;
}

void test_infinite_loop_4 (int ctrl1, int ctrl2, int no_ctrl,
                           int data1, int data2, int no_data) {
  G = 0 ;
  while (ctrl1) {
    G += data1 ;
    if (no_ctrl) { /* Don't control an assignment of G
                    * which leads to the return */
      G += no_data ; /* Don't affect the final value of G
                     * because the assignement
                     * does not lead to  the return */
      while (1)
        G += no_data ;  /* Don't affect the final value of G
                       * because the assignement
                       * does not lead to the return */
      G += no_data ; /* Don't affect the final value of G
                      * because the assignement
                      * is dead code */
      }
    if (ctrl2)
      G += data2 ;
    }
  return;
}

void test_infinite_loop_5 (int ctrl1, int ctrl2, int no_ctrl,
                           int data1, int data2, int no_data) {
  G = 0 ;
  while (ctrl1) {
    G += data1 ;
    if (no_ctrl) { /* Don't control the final value of G.
                    * It only controls the terminaison of the function.
                    */
      G += no_data ;   /* Don't affect ... */
      while (1)
        G += no_data ; /* Don't affect ... */
      G += no_data ;   /* Don't affect ...  dead code */
      }
    else /* <-- This is the difference with test_infinite_loop_4.
          * It is only a syntaxical difference,
          * and not a semantical difference
          * since the previous statement "G += no_data" is dead.
          */
      if (ctrl2)
        G += data2 ;
    }
  return;
}
/*-------------------------------------------*/
int C1 = 1, C2 = 1 ;
int X, Y, Z ;

void loop (int cond) {
  if (cond) {
    int c = 0 ;
    /*@ loop pragma WIDEN_HINTS X, 10, 100 ; */ while (1) {
      //@ slice pragma ctrl ;
      if (c) {
        X++;
        Y = Z ;
        }
      c=1;
      //@ assert  c==1 ;
      }
    }
  Z = Y ; // dead code with -main main
}
/*---------------------*/
/*@ assigns *p \from p, y, Z ;
 */
void may_write_Y_from_Z (int * p, int y) ;
void test_assigns (int * p, int y) { if (y < Z) *p = y + Z; }
/*---------------------*/
void main (int y) {
  int no_ctrl = 1 ;
  Z = 0;
  if (no_ctrl)
    Z = X ;
  may_write_Y_from_Z (&Y, y) ;
  if (C1) {
    int cond = C2 ;
    loop (cond) ;
    }
}
/*-------------------------------------------*/

void alarm() {
  int i = 1;
  volatile int j = 3;
  //@ assert i == 1;
  j++;
}