| 1 | /*****************************************************************************
|
|---|
| 2 | * SOURCE: This is a translation of a Pthread program from
|
|---|
| 3 | * the Pthread benchmarks of SV-COMP 2014.
|
|---|
| 4 | * https://svn.sosy-lab.org/software/sv-benchmarks/tags/svcomp14/
|
|---|
| 5 | * FILE: fib_bench_false.cvl
|
|---|
| 6 | * DESCRIPTION:
|
|---|
| 7 | * Simple program of threads interacting which causes an error when
|
|---|
| 8 | * their total reaches a certain value. The fix simply involves changing the output
|
|---|
| 9 | * for which an error will be caused; specifically, here error is caused when i or j
|
|---|
| 10 | * >=144, the fix has i or j > 144.
|
|---|
| 11 | * Command line execution:
|
|---|
| 12 | * civl verify -inputSIZE=5 queue_false.cvl
|
|---|
| 13 | ******************************************************************************/
|
|---|
| 14 |
|
|---|
| 15 | #include "pthread.cvh"
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <assert.h>
|
|---|
| 18 | #include <civlc.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 |
|
|---|
| 21 | $input int SIZE;
|
|---|
| 22 | #define EMPTY (-1)
|
|---|
| 23 | #define FULL (-2)
|
|---|
| 24 | #define FALSE (0)
|
|---|
| 25 | #define TRUE (1)
|
|---|
| 26 |
|
|---|
| 27 | typedef struct {
|
|---|
| 28 | int element[SIZE];
|
|---|
| 29 | int head;
|
|---|
| 30 | int tail;
|
|---|
| 31 | int amount;
|
|---|
| 32 | } QType;
|
|---|
| 33 |
|
|---|
| 34 | int stored_elements[SIZE];
|
|---|
| 35 | _Bool enqueue_flag, dequeue_flag;
|
|---|
| 36 | pthread_mutex_t mutex;
|
|---|
| 37 | QType queue;
|
|---|
| 38 |
|
|---|
| 39 | int init(QType *q)
|
|---|
| 40 | {
|
|---|
| 41 | q->head=0;
|
|---|
| 42 | q->tail=0;
|
|---|
| 43 | q->amount=0;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | int empty(QType *q)
|
|---|
| 47 | {
|
|---|
| 48 | if (q->head == q->tail)
|
|---|
| 49 | {
|
|---|
| 50 | printf("queue is empty\n");
|
|---|
| 51 | return EMPTY;
|
|---|
| 52 | }
|
|---|
| 53 | else
|
|---|
| 54 | return 0;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | int full(QType *q)
|
|---|
| 58 | {
|
|---|
| 59 | if (q->amount == SIZE)
|
|---|
| 60 | {
|
|---|
| 61 | printf("queue is full\n");
|
|---|
| 62 | return FULL;
|
|---|
| 63 | }
|
|---|
| 64 | else
|
|---|
| 65 | return 0;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | int enqueue(QType *q, int x)
|
|---|
| 69 | {
|
|---|
| 70 | q->element[q->tail] = x;
|
|---|
| 71 | q->amount++;
|
|---|
| 72 | if (q->tail == SIZE)
|
|---|
| 73 | {
|
|---|
| 74 | q->tail = 1;
|
|---|
| 75 | }
|
|---|
| 76 | else
|
|---|
| 77 | {
|
|---|
| 78 | q->tail++;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | return 0;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | int dequeue(QType *q)
|
|---|
| 85 | {
|
|---|
| 86 | int x;
|
|---|
| 87 |
|
|---|
| 88 | x = q->element[q->head];
|
|---|
| 89 | q->amount--;
|
|---|
| 90 | if (q->head == SIZE)
|
|---|
| 91 | {
|
|---|
| 92 | q->head = 1;
|
|---|
| 93 | }
|
|---|
| 94 | else
|
|---|
| 95 | q->head++;
|
|---|
| 96 |
|
|---|
| 97 | return x;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void *t1(void *arg)
|
|---|
| 101 | {
|
|---|
| 102 | int value, i;
|
|---|
| 103 |
|
|---|
| 104 | pthread_mutex_lock(&mutex);
|
|---|
| 105 | value = 0;
|
|---|
| 106 | int tmp = enqueue(&queue,value);
|
|---|
| 107 | printf("0");
|
|---|
| 108 | if (tmp) {
|
|---|
| 109 | $assert($false);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | stored_elements[0]=value;
|
|---|
| 113 | int tmp1 = empty(&queue);
|
|---|
| 114 | if (tmp1) {
|
|---|
| 115 | $assert($false);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | pthread_mutex_unlock(&mutex);
|
|---|
| 119 |
|
|---|
| 120 | for(i=0; i<(SIZE-1); i++)
|
|---|
| 121 | {
|
|---|
| 122 | pthread_mutex_lock(&mutex);
|
|---|
| 123 | printf("1");
|
|---|
| 124 | if (enqueue_flag)
|
|---|
| 125 | {
|
|---|
| 126 | value = 1;
|
|---|
| 127 | enqueue(&queue,value);
|
|---|
| 128 | stored_elements[i+1]=value;
|
|---|
| 129 | enqueue_flag=FALSE;
|
|---|
| 130 | dequeue_flag=TRUE;
|
|---|
| 131 | }
|
|---|
| 132 | pthread_mutex_unlock(&mutex);
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | void *t2(void *arg)
|
|---|
| 137 | {
|
|---|
| 138 | int i;
|
|---|
| 139 |
|
|---|
| 140 | for(i=0; i<SIZE; i++)
|
|---|
| 141 | {
|
|---|
| 142 | pthread_mutex_lock(&mutex);
|
|---|
| 143 | if (dequeue_flag)
|
|---|
| 144 | printf("2");
|
|---|
| 145 | {
|
|---|
| 146 | int tmp = dequeue(&queue);
|
|---|
| 147 | if (!tmp==stored_elements[i]) {
|
|---|
| 148 | $assert($false);
|
|---|
| 149 | }
|
|---|
| 150 | dequeue_flag=FALSE;
|
|---|
| 151 | enqueue_flag=TRUE;
|
|---|
| 152 | }
|
|---|
| 153 | pthread_mutex_unlock(&mutex);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | void main()
|
|---|
| 159 | {
|
|---|
| 160 | $proc id1, id2;
|
|---|
| 161 |
|
|---|
| 162 | enqueue_flag=TRUE;
|
|---|
| 163 | dequeue_flag=FALSE;
|
|---|
| 164 |
|
|---|
| 165 | pthread_mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT);
|
|---|
| 166 | init(&queue);
|
|---|
| 167 | int tmp = empty(&queue);
|
|---|
| 168 | if (!tmp==EMPTY) {
|
|---|
| 169 | $assert($false);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | pthread_create(&id1, NULL, t1, &queue);
|
|---|
| 173 | pthread_create(&id2, NULL, t2, &queue);
|
|---|
| 174 |
|
|---|
| 175 | pthread_join(id1, NULL);
|
|---|
| 176 | pthread_join(id2, NULL);
|
|---|
| 177 | }
|
|---|