File: evoting.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 (421 lines) | stat: -rw-r--r-- 11,621 bytes parent folder | download | duplicates (4)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* evoting.c - An example program of electronic voting in C */
/* copyright 2004-2009 David MENTRE */

/* this program is under GNU GPL license */

/* This program presents a prototype of electronic machine written in C.
   Its intent is to implement the core algorithms as a reference for
   further analysis */

/* TODO:

*/

#define _GNU_SOURCE
#define __SINGLE_THREAD__

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>


#if 0
#include "check_specs.h"
#if 0
#include <C:/Frama-C/share/frama-c/jessie/jessie_prolog.h>
#else
#include </usr/local/share/frama-c/jessie/jessie_prolog.h>
#endif
#endif

#include "jessie_prolog.h"

#define MAX_CANDIDATES 20
#define MAX_STRING_SIZE 255
#define MAX_VOTES_PER_CANDIDATE 100000

char *candidates[MAX_CANDIDATES];
int num_candidates;
int number_of_votes;
int counters[MAX_CANDIDATES];

FILE *log_desc;

/*@ assigns \nothing;
    ensures \result <= a && \result <= b;
    ensures \result == a || \result == b;
 */
int min(int a, int b)
{
  return (a)<(b)?(a):(b);
}

#ifdef NO_FRAMA_C
/*@ requires valid_string(str);
    assigns \nothing;
 */
void print_string(char *str)
{
  printf(str);
}

/*@ assigns \nothing;
 */
void print_int(int n)
{
  printf("%d", n);
}
#else
/* to avoid overhead of Frama-C regarding static C strings */
#define print_string(s)
#define print_int(n)
#endif

/*@ requires choice >= 0 && choice <= num_candidates;
    assigns \nothing;
 */
void print_candidate(int choice)
{
  print_int(choice);
  print_string("-'");
  print_string(candidates[choice]);
  print_string("'");
}

/*@ requires valid_string(filename);
    assigns candidates[..];
    ensures \result > 1 && \result < MAX_CANDIDATES;
    ensures \forall integer i;
                0 <= i < \result ==> valid_string(candidates[i]);
 */
int read_candidates(char *filename)
{
  int i;
  FILE *f;
  char buf[MAX_STRING_SIZE];
  char *s;

  f = fopen(filename, "r");
  if (f == NULL) {
    perror("unable to open candidates file");
    exit(1);
  }

  candidates[0] = strndup("None of those candidates", MAX_STRING_SIZE);

  i = 1;
  s = fgets(buf, MAX_STRING_SIZE, f); 
  /*@ loop invariant 1 <= i && i < MAX_CANDIDATES;
      loop invariant \forall integer j;
                           0 <= j < i ==> valid_string(candidates[j]);
      loop invariant s == NULL || valid_string(buf);
   */
  while (i < MAX_CANDIDATES - 1 && s != NULL) {
    int len, copied_len;

    len = strlen(buf);
    if (len > 0) {
      copied_len = min(MAX_STRING_SIZE, len - 1); /* do not copy newline */
      candidates[i] = strndup(buf, copied_len);
      i++;
    }
    s = fgets(buf, MAX_STRING_SIZE, f); 
  }

  if (i <= 1) {
    print_string("Not enough candidates\n");
    exit(1);
  }

  fclose(f);

  return i;
}

/*@ assigns \nothing; 
 */
void print_candidate_array(void)
{
  int i;

  for (i = 0; i < num_candidates; i++) {
    print_string("   ");
    print_int(i);
    print_string(". ");
    print_string(candidates[i]);
    print_string("\n");
  }
}

/*@ requires \valid(argv +(0..1));
    requires valid_string(argv[1]);
    assigns num_candidates, candidates[..];
    ensures num_candidates > 1 && num_candidates < MAX_CANDIDATES;
    ensures \forall integer i;
                0 <= i < num_candidates ==> valid_string(candidates[i]);
 */
void vote_setup(char **argv)
{
  print_string("**** Vote Setup ****\n");
  print_string("* reading candidates file '");
  print_string(argv[1]);
  print_string("'\n");

  num_candidates = read_candidates(argv[1]);

  print_int(num_candidates);
  print_string(" candidates:\n");
  print_candidate_array();
}

/*@ requires \valid(stdin);
    assigns \nothing;
    ensures \result >= 0 && \result < num_candidates;
 */
int get_vote(void)
{
  char buf[MAX_STRING_SIZE];
  char *s = NULL;

  while (1) {
    print_string("Choose a candidate:\n");
    print_candidate_array();
    print_string("Your choice (0-");
    print_int(num_candidates - 1);
    print_string("):");
    s = fgets(buf, MAX_STRING_SIZE, stdin);

    if (s != NULL) {
      int choice;

      choice = atoi(buf);
      if (choice >= 0 && choice < num_candidates) {
        print_string("Are you sure your vote ");
	print_candidate(choice);
	print_string(" is correct? (y/n)");
        s = fgets(buf, MAX_STRING_SIZE, stdin);
        if (s != NULL && strncmp(buf, "y", 1) == 0) {
          return choice;
        } else {
          print_string("Vote canceled, redo\n");
        }
      } else {
        print_string("Invalid choice (");
	print_int(choice);
	print_string("), redo\n");
      }
    } else {
      print_string("bad input, redo\n");
    }
  }
}

/*@ requires number_of_votes == 0;
    requires \forall integer i; 0 <= i < MAX_CANDIDATES ==> counters[i] == 0;
    requires 1 < num_candidates && num_candidates < MAX_CANDIDATES;
    requires \valid(stdin);
    assigns counters[..];
    assigns number_of_votes;
    ensures number_of_votes < MAX_CANDIDATES * MAX_VOTES_PER_CANDIDATE;
    ensures number_of_votes >= 0;
    ensures \forall integer i;
        0 <= i < num_candidates ==> counters[i] < MAX_VOTES_PER_CANDIDATE;
    ensures \forall integer i;
        0 <= i < num_candidates ==> counters[i] >= 0;
    ensures \forall integer i;
        num_candidates <= i < MAX_CANDIDATES ==> counters[i] == 0;
    ensures num_candidates < MAX_CANDIDATES;
 */
//     ensures number_of_votes == \sum(0, num_candidates,
//                                         \lambda integer i; counters[i]);
void voting(void)
{
  char buf[MAX_STRING_SIZE];
  char *s = "";

  print_string("**** Voting ****\n");
  /*@ loop invariant \forall integer i; 
         0 <= i < MAX_CANDIDATES ==> 0 <= counters[i]
                                     && counters[i] < MAX_VOTES_PER_CANDIDATE;
      loop invariant 0 <= number_of_votes;
      loop invariant number_of_votes < MAX_CANDIDATES * MAX_VOTES_PER_CANDIDATE;
      loop invariant \forall integer i; 
         num_candidates <= i < MAX_CANDIDATES ==> counters[i] == 0;
      loop invariant \valid(stdin);
   */
  while (number_of_votes < num_candidates * MAX_VOTES_PER_CANDIDATE) {
    print_string("* Do you want to Vote or Stop the vote (v/\"end of vote\")?");
    s = fgets(buf, MAX_STRING_SIZE, stdin);

    if (s != NULL && strncmp(buf, "end of vote", 11) == 0) {
      return;
    }
    
    if (s != NULL && strncmp(buf, "v", 1) == 0) {
      int chosen_vote;

      chosen_vote = get_vote();
      // Stop votes when we have reach the max of vote per candidate
      if (counters[chosen_vote] < MAX_VOTES_PER_CANDIDATE - 1) {
	counters[chosen_vote]++;
	number_of_votes++;
	print_string("Vote stored: ");
	print_candidate(chosen_vote);
	print_string("\n");
      } else
	return;
    }
  }
}

/* FIXME: in following function, we don't consider the case when two
          candidates have the same counters.
 */
/*@ requires 1 < num_candidates && num_candidates < MAX_CANDIDATES;
    assigns \nothing;
    ensures \result >= 1 && \result < num_candidates;
    ensures \forall integer i; 
               1 <= i < num_candidates ==> counters[\result] >= counters[i];
 */
int compute_winner(void)
{
  int i, winner;

  winner = 1; /* "No vote" is NOT taken into account */
  /*@ loop invariant 2 <= i && i < MAX_CANDIDATES;
      loop invariant \forall integer j;
                         1 <= j < i ==> counters[winner] >= counters[j];
      loop invariant winner >= 1 && winner < num_candidates;
   */
  for (i = 2; i < num_candidates; i++) {
    if (counters[i] > counters[winner]) { winner = i; }
  }
  return winner;
}

/*@ requires 1 < num_candidates && num_candidates < MAX_CANDIDATES;
    requires \forall integer i; 0 <= i < MAX_CANDIDATES
                ==> counters[i] < MAX_VOTES_PER_CANDIDATE && counters[i] >= 0;
    assigns \nothing;
 */
void compute_results(void)
{
  int total = 0;
  int i, winner, valid_total;

  print_string("**** Result ****\n");

  /*@ loop invariant 0 <= i && i <= num_candidates;
      loop invariant 0 <= counters[i] < MAX_VOTES_PER_CANDIDATE;
      loop invariant total >= 0;
      loop invariant total <= i * MAX_VOTES_PER_CANDIDATE;
      loop invariant \forall integer j; 0<=j<i ==> total >= counters[j];
  */
  for (i = 0; i < num_candidates; i++) {
    total += counters[i];
  }
  //@ assert total < MAX_CANDIDATES * MAX_VOTES_PER_CANDIDATE;
  //@ assert total >= counters[0];
  //@ assert counters[0] >= 0;
  valid_total = total - counters[0];

  //@ loop invariant 0 <= i;
  for (i = 0; i < num_candidates; i++) {
    print_int(counters[i]);
    print_string(" vote(s): ");
    print_candidate(i);
    print_string("\n");
  }

/*   for (i = 0; i < num_candidates; i++) { */
/*     dual_log("  %d. %s % 5d vote(s) (%.03f %%)\n", */
/*            i, candidates[i], counters[i], */
/*            (double)counters[i] / (double)valid_total * 100.0); */
/*   } */

  print_string("Total number of votes: ");
  print_int(total);
  print_string("\n");

  print_string("Total number of valid votes: ");
  print_int(valid_total);
  print_string("\n");

  winner = compute_winner();
  print_string("* Winner: ");
  print_int(winner);
  print_string("-");
  print_string(candidates[winner]);
  print_string("\n");
}

/*@ requires \valid(stdin);
    requires \valid(argv +(0..argc-1));
    requires valid_string(argv[1]);
    assigns log_desc;
    assigns num_candidates, candidates[..];
    assigns number_of_votes;
    assigns counters[..];
 */
int main(int argc, char **argv)
{
  int i;

  log_desc = fopen("c-evote.log", "w");
  if (log_desc == NULL) {
    perror("unable to open log file c-evote.log");
    exit(1);
  }

  print_string("**** Start of vote program (C version) ****\n");

  if (argc != 2) {
    fputs("bad number of arguments\n", stderr);
    fputs("usage: c-evoting candidates.txt\n", stderr);
    exit(1);
  }

  // clean-up all global data structures
  number_of_votes = 0;
  /*@ loop invariant \forall integer j; 0 <= j < i ==> counters[j] == 0;
      loop invariant 0 <= i && i <= MAX_CANDIDATES;
      loop invariant \valid(argv +(0..argc-1));
      loop invariant argc == 2;
      loop invariant valid_string(argv[1]);
   */
  for (i = 0; i < MAX_CANDIDATES; i++)
    counters[i] = 0;

  vote_setup(argv);
  // We have at least the "None of those candidates" option for voters
  //@ assert num_candidates > 1;

  // All candidates have a valid description string
  /*@ assert \forall integer i;
              0 <= i < num_candidates ==> valid_string(candidates[i]);
  */

  // All candidates have their counters set to zero and no vote registered
  //@ assert \forall integer i; 0 <= i < MAX_CANDIDATES ==> counters[i] == 0;
  //@ assert number_of_votes == 0;
  voting();
  // All candidates have their counters potentially incremented
  //@ assert \forall integer i; 0 <= i < num_candidates ==> counters[i] >= 0;

  // FIXME: How to specify that nothing is changed after voting?
  compute_results();

  /* free data structures */
  //@ assert num_candidates < MAX_CANDIDATES;
  //@ loop invariant 0 <= i && i <= MAX_CANDIDATES;
  for (i = 0; i < num_candidates; i++)
    free(candidates[i]);

  print_string("**** Stop of vote program (C version) ****\n");

  fclose(log_desc);
  return 0;
}