-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchan.pml
More file actions
715 lines (644 loc) · 16.1 KB
/
chan.pml
File metadata and controls
715 lines (644 loc) · 16.1 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Promela model of pthreadChannel chan.c
* Copyright (C) 2026 G. David Butler <gdb@dbSystems.com>
* Generated with Claude Code (https://claude.ai/code)
*
* Models the core synchronization logic to verify:
* - Deadlock freedom
* - chanAll atomicity (all-or-nothing)
* - chanOne correct selection
* - Fairness (no starvation)
* - Shutdown propagation
*
* Build and run:
* spin -a chan.pml
* cc -DSAFETY -DXUSAFE -O2 -o pan pan.c
* ./pan
*
* For liveness/fairness checks:
* cc -O2 -o pan pan.c
* ./pan -a -f
*
* For specific LTL property:
* spin -a -N no_deadlock chan.pml
* cc -O2 -o pan pan.c
* ./pan -a
*/
/* Configuration - keep small for tractable state space */
#define NCHANS 2 /* number of channels */
#define NPROCS 2 /* number of worker processes */
#define NOPS 2 /* operations per process before done */
/* Channel states (models chanSs_t) */
#define ST_EMPTY 0 /* can put, cannot get */
#define ST_HAS_ITEM 1 /* can get, cannot put (single-item store) */
#define ST_SHUT 2 /* shutdown */
/* Operation types (models chanOp_t) */
#define OP_NOP 0
#define OP_SHT 1
#define OP_GET 2
#define OP_PUT 3
/* Operation status (models chanOs_t) */
#define OS_NOP 0
#define OS_SHT 1
#define OS_GET 2
#define OS_PUT 3
#define OS_TMO 4
/* chanAll status (models chanAl_t) */
#define AL_ERR 0
#define AL_EVT 1
#define AL_OP 2
#define AL_TMO 3
/*
* Channel structure
* Abstracts struct chan to essential state
*/
typedef Channel {
byte state; /* ST_EMPTY, ST_HAS_ITEM, ST_SHUT */
byte open_count; /* reference count */
byte get_waiters; /* count of threads waiting to get */
byte put_waiters; /* count of threads waiting to put */
byte lock; /* 0 = unlocked, N = locked by process N */
};
Channel channels[NCHANS];
/* Per-process signaling (models cpr_t simplified) */
chan wake[NPROCS] = [1] of { byte }; /* signal channel per process */
/* Statistics for verification */
byte total_gets = 0;
byte total_puts = 0;
byte total_ops = 0;
bool all_done = false;
/* Process completion tracking */
bool proc_done[NPROCS];
/*
* Lock acquisition - models pthread_mutex_lock
*/
inline lock_chan(ch) {
atomic {
(channels[ch].lock == 0) -> channels[ch].lock = _pid + 1
}
}
/*
* Lock release - models pthread_mutex_unlock
*/
inline unlock_chan(ch) {
channels[ch].lock = 0
}
/*
* Try lock - models pthread_mutex_trylock
* Sets 'got_lock' to result
*/
inline trylock_chan(ch, got_lock) {
atomic {
if
:: channels[ch].lock == 0 ->
channels[ch].lock = _pid + 1;
got_lock = 1
:: else ->
got_lock = 0
fi
}
}
/*
* Lock ladder for multiple channels
* Acquire locks in ascending order, retry on failure
* Models the trylock/yield loop in chanOne/chanAll
*/
inline lock_ladder(n, arr, success) {
byte ll_i;
byte ll_got;
bool ll_retry;
success = 0;
do
:: success == 0 ->
ll_retry = false;
ll_i = 0;
do
:: ll_i < n ->
if
:: ll_i == 0 ->
lock_chan(arr[ll_i]);
ll_i++
:: ll_i > 0 ->
trylock_chan(arr[ll_i], ll_got);
if
:: ll_got == 1 -> ll_i++
:: ll_got == 0 ->
/* Release all and retry - models sched_yield loop */
byte ll_j = 0;
do
:: ll_j < ll_i ->
unlock_chan(arr[ll_j]);
ll_j++
:: ll_j >= ll_i -> break
od;
ll_retry = true;
break
fi
fi
:: ll_i >= n -> break
od;
if
:: ll_retry == false -> success = 1
:: ll_retry == true -> skip /* retry the outer loop */
fi
:: success == 1 -> break
od
}
/*
* Release locks in descending order
*/
inline unlock_ladder(n, arr) {
byte ul_i = n;
do
:: ul_i > 0 ->
ul_i--;
unlock_chan(arr[ul_i])
:: ul_i == 0 -> break
od
}
/*
* Wake waiting threads on a channel
* Models the WAKE macro
*/
inline wake_getters(ch) {
/* In real impl, signal condition variable */
/* Here we just note waiters can proceed */
skip
}
inline wake_putters(ch) {
skip
}
/*
* chanOp on single channel (simplified)
* Models blocking Get or Put
*/
inline chan_op(ch, op, status) {
lock_chan(ch);
if
:: channels[ch].state == ST_SHUT ->
status = OS_SHT;
unlock_chan(ch)
:: op == OP_GET && channels[ch].state == ST_HAS_ITEM ->
channels[ch].state = ST_EMPTY;
status = OS_GET;
total_gets++;
wake_putters(ch);
unlock_chan(ch)
:: op == OP_PUT && channels[ch].state == ST_EMPTY ->
channels[ch].state = ST_HAS_ITEM;
status = OS_PUT;
total_puts++;
wake_getters(ch);
unlock_chan(ch)
:: op == OP_GET && channels[ch].state == ST_EMPTY ->
/* Must wait */
channels[ch].get_waiters++;
unlock_chan(ch);
/* Block until signaled or state changes */
do
:: true ->
lock_chan(ch);
if
:: channels[ch].state == ST_SHUT ->
channels[ch].get_waiters--;
status = OS_SHT;
unlock_chan(ch);
break
:: channels[ch].state == ST_HAS_ITEM ->
channels[ch].get_waiters--;
channels[ch].state = ST_EMPTY;
status = OS_GET;
total_gets++;
wake_putters(ch);
unlock_chan(ch);
break
:: else ->
unlock_chan(ch)
/* spin/wait */
fi
od
:: op == OP_PUT && channels[ch].state == ST_HAS_ITEM ->
/* Must wait */
channels[ch].put_waiters++;
unlock_chan(ch);
do
:: true ->
lock_chan(ch);
if
:: channels[ch].state == ST_SHUT ->
channels[ch].put_waiters--;
status = OS_SHT;
unlock_chan(ch);
break
:: channels[ch].state == ST_EMPTY ->
channels[ch].put_waiters--;
channels[ch].state = ST_HAS_ITEM;
status = OS_PUT;
total_puts++;
wake_getters(ch);
unlock_chan(ch);
break
:: else ->
unlock_chan(ch)
fi
od
fi
}
/*
* chanOne - operate on first available channel
* Models the select-style behavior
*
* arr: array of channel indices
* ops: array of operations (OP_GET or OP_PUT)
* n: count
* result_idx: which channel succeeded (0-based)
* result_status: operation status
*/
inline chan_one(n, arr, ops, result_idx, result_status) {
byte co_i;
byte co_j;
byte co_found;
bool co_success;
bool co_need_cleanup;
result_idx = 255;
result_status = OS_NOP;
co_need_cleanup = false;
/* First pass: check without blocking */
co_i = 0;
do
:: co_i < n ->
lock_chan(arr[co_i]);
if
:: channels[arr[co_i]].state == ST_SHUT ->
result_idx = co_i;
result_status = OS_SHT;
unlock_chan(arr[co_i]);
break
:: ops[co_i] == OP_GET && channels[arr[co_i]].state == ST_HAS_ITEM ->
channels[arr[co_i]].state = ST_EMPTY;
result_idx = co_i;
result_status = OS_GET;
total_gets++;
unlock_chan(arr[co_i]);
break
:: ops[co_i] == OP_PUT && channels[arr[co_i]].state == ST_EMPTY ->
channels[arr[co_i]].state = ST_HAS_ITEM;
result_idx = co_i;
result_status = OS_PUT;
total_puts++;
unlock_chan(arr[co_i]);
break
:: else ->
unlock_chan(arr[co_i]);
co_i++
fi
:: co_i >= n -> break
od;
/* If no immediate success, must wait (simplified) */
if
:: result_idx == 255 ->
/* Register as waiter on all channels */
co_i = 0;
do
:: co_i < n ->
lock_chan(arr[co_i]);
if
:: ops[co_i] == OP_GET -> channels[arr[co_i]].get_waiters++
:: ops[co_i] == OP_PUT -> channels[arr[co_i]].put_waiters++
:: else -> skip
fi;
unlock_chan(arr[co_i]);
co_i++
:: co_i >= n -> break
od;
/* Wait loop */
do
:: co_need_cleanup == false ->
co_i = 0;
do
:: co_i < n && co_need_cleanup == false ->
lock_chan(arr[co_i]);
if
:: channels[arr[co_i]].state == ST_SHUT ->
if
:: ops[co_i] == OP_GET -> channels[arr[co_i]].get_waiters--
:: ops[co_i] == OP_PUT -> channels[arr[co_i]].put_waiters--
:: else -> skip
fi;
result_idx = co_i;
result_status = OS_SHT;
unlock_chan(arr[co_i]);
co_need_cleanup = true;
break
:: ops[co_i] == OP_GET && channels[arr[co_i]].state == ST_HAS_ITEM ->
channels[arr[co_i]].get_waiters--;
channels[arr[co_i]].state = ST_EMPTY;
result_idx = co_i;
result_status = OS_GET;
total_gets++;
unlock_chan(arr[co_i]);
co_need_cleanup = true;
break
:: ops[co_i] == OP_PUT && channels[arr[co_i]].state == ST_EMPTY ->
channels[arr[co_i]].put_waiters--;
channels[arr[co_i]].state = ST_HAS_ITEM;
result_idx = co_i;
result_status = OS_PUT;
total_puts++;
unlock_chan(arr[co_i]);
co_need_cleanup = true;
break
:: else ->
unlock_chan(arr[co_i]);
co_i++
fi
:: co_i >= n || co_need_cleanup == true -> break
od
:: co_need_cleanup == true -> break
od;
/* Unregister from remaining channels */
co_j = 0;
do
:: co_j < n ->
if
:: co_j != result_idx ->
lock_chan(arr[co_j]);
if
:: ops[co_j] == OP_GET && channels[arr[co_j]].get_waiters > 0 ->
channels[arr[co_j]].get_waiters--
:: ops[co_j] == OP_PUT && channels[arr[co_j]].put_waiters > 0 ->
channels[arr[co_j]].put_waiters--
:: else -> skip
fi;
unlock_chan(arr[co_j])
:: else -> skip
fi;
co_j++
:: co_j >= n -> break
od
:: else -> skip
fi
}
/*
* chanAll - atomic all-or-nothing operation
* Models the core chanAll semantics
*
* Returns AL_OP if all succeeded, AL_EVT if any failed
*/
inline chan_all(n, arr, ops, result) {
byte ca_i;
byte ca_j;
bool ca_can_do;
bool ca_has_event;
bool ca_success;
result = AL_ERR;
/* Lock ladder - acquire all locks in order */
lock_ladder(n, arr, ca_success);
assert(ca_success == 1);
/* Check if all operations can proceed */
ca_can_do = true;
ca_has_event = false;
ca_i = 0;
do
:: ca_i < n ->
if
:: channels[arr[ca_i]].state == ST_SHUT ->
ca_has_event = true;
ca_can_do = false
:: ops[ca_i] == OP_GET && channels[arr[ca_i]].state != ST_HAS_ITEM ->
ca_can_do = false
:: ops[ca_i] == OP_PUT && channels[arr[ca_i]].state != ST_EMPTY ->
ca_can_do = false
:: else -> skip
fi;
ca_i++
:: ca_i >= n -> break
od;
if
:: ca_has_event ->
/* Event occurred (shutdown), report but don't operate */
result = AL_EVT;
unlock_ladder(n, arr)
:: ca_can_do == false && ca_has_event == false ->
/* Cannot proceed, must wait (simplified: just fail for non-blocking) */
/* In real impl, would register waiters and block */
result = AL_EVT; /* Treat as event for simplicity */
unlock_ladder(n, arr)
:: ca_can_do == true ->
/* All operations can proceed - do them atomically */
ca_i = 0;
do
:: ca_i < n ->
if
:: ops[ca_i] == OP_GET ->
assert(channels[arr[ca_i]].state == ST_HAS_ITEM);
channels[arr[ca_i]].state = ST_EMPTY;
total_gets++
:: ops[ca_i] == OP_PUT ->
assert(channels[arr[ca_i]].state == ST_EMPTY);
channels[arr[ca_i]].state = ST_HAS_ITEM;
total_puts++
:: else -> skip
fi;
ca_i++
:: ca_i >= n -> break
od;
result = AL_OP;
unlock_ladder(n, arr)
fi
}
/*
* chanShut - shutdown a channel
*/
inline chan_shut(ch) {
lock_chan(ch);
channels[ch].state = ST_SHUT;
/* Wake all waiters */
channels[ch].get_waiters = 0;
channels[ch].put_waiters = 0;
unlock_chan(ch)
}
/*
* Initialize channels
*/
init {
byte i;
/* Initialize all channels */
atomic {
i = 0;
do
:: i < NCHANS ->
channels[i].state = ST_EMPTY;
channels[i].open_count = 1;
channels[i].get_waiters = 0;
channels[i].put_waiters = 0;
channels[i].lock = 0;
i++
:: i >= NCHANS -> break
od;
i = 0;
do
:: i < NPROCS ->
proc_done[i] = false;
i++
:: i >= NPROCS -> break
od
};
/* Start worker processes */
atomic {
run worker(0);
run worker(1)
}
}
/*
* Worker process - alternates put then get (realistic usage)
* Each worker: put on "their" channel, get from "other" channel
* This models producer/consumer pairing
*/
proctype worker(byte id) {
byte op_count = 0;
byte status;
byte my_ch;
byte other_ch;
/* Worker 0 puts to channel 0, gets from channel 1 */
/* Worker 1 puts to channel 1, gets from channel 0 */
my_ch = id;
other_ch = 1 - id;
do
:: op_count < NOPS ->
/* First put, then get - ensures no livelock */
chan_op(my_ch, OP_PUT, status);
if
:: status == OS_PUT -> total_ops++
:: status == OS_SHT -> break
:: else -> skip
fi;
chan_op(other_ch, OP_GET, status);
if
:: status == OS_GET -> total_ops++
:: status == OS_SHT -> break
:: else -> skip
fi;
op_count++
:: op_count >= NOPS -> break
od;
proc_done[id] = true;
/* Check if all processes done */
if
:: proc_done[0] && proc_done[1] ->
all_done = true
:: else -> skip
fi
}
/*
* Test process for chanOne
*/
proctype test_chan_one() {
byte arr[2];
byte ops[2];
byte result_idx;
byte result_status;
arr[0] = 0;
arr[1] = 1;
ops[0] = OP_GET;
ops[1] = OP_PUT;
chan_one(2, arr, ops, result_idx, result_status);
assert(result_status != OS_NOP || channels[0].state == ST_SHUT || channels[1].state == ST_SHUT)
}
/*
* Test process for chanAll
*/
proctype test_chan_all() {
byte arr[2];
byte ops[2];
byte result;
byte old_state_0;
byte old_state_1;
arr[0] = 0;
arr[1] = 1;
ops[0] = OP_GET;
ops[1] = OP_PUT;
/* Record states before */
old_state_0 = channels[0].state;
old_state_1 = channels[1].state;
chan_all(2, arr, ops, result);
/* Verify atomicity: either both changed or neither */
if
:: result == AL_OP ->
/* Both operations succeeded */
assert(old_state_0 == ST_HAS_ITEM); /* was gettable */
assert(old_state_1 == ST_EMPTY); /* was puttable */
:: result == AL_EVT ->
/* Neither operation occurred (states unchanged by us) */
skip
:: else -> skip
fi
}
/*
* Producer process - only puts
*/
proctype producer(byte ch) {
byte status;
byte count = 0;
do
:: count < NOPS ->
chan_op(ch, OP_PUT, status);
if
:: status == OS_PUT -> count++
:: status == OS_SHT -> break
:: else -> skip
fi
:: count >= NOPS -> break
od
}
/*
* Consumer process - only gets
*/
proctype consumer(byte ch) {
byte status;
byte count = 0;
do
:: count < NOPS ->
chan_op(ch, OP_GET, status);
if
:: status == OS_GET -> count++
:: status == OS_SHT -> break
:: else -> skip
fi
:: count >= NOPS -> break
od
}
/*
* Shutdown process - shuts down a channel after delay
*/
proctype shutter(byte ch) {
byte i;
/* Let some operations happen first */
i = 0;
do
:: i < 3 -> i++
:: i >= 3 -> break
od;
chan_shut(ch)
}
/*
* LTL Properties
*/
/* No deadlock - SPIN checks this automatically */
/* Run with: ./pan -DSAFETY */
/* Progress: operations eventually complete */
ltl progress {
[] (total_ops < 255 -> <> (total_ops > 0))
}
/* Fairness: if a process is trying, it eventually succeeds */
/* (weak fairness - checked with ./pan -a -f) */
/* chanAll atomicity assertion is inline in chan_all */
/* Eventually all processes complete (under fairness) */
ltl termination {
<> all_done
}
/* Conservation: gets <= puts (can't get what wasn't put) */
/* Note: with initial empty channels, this should hold */
ltl conservation {
[] (total_gets <= total_puts + NCHANS)
}