-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathpthread_lib.c
1038 lines (847 loc) · 25.7 KB
/
pthread_lib.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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FUNCTION: pthread_mutexattr_settype */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int __VERIFIER_nondet_int(void);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
__CPROVER_HIDE:;
(void)attr;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
if(type==PTHREAD_MUTEX_RECURSIVE)
__CPROVER_set_must(attr, "mutexattr-recursive");
#else
(void)type;
#endif
int result=__VERIFIER_nondet_int();
return result;
}
/* FUNCTION: pthread_cancel */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int __VERIFIER_nondet_int(void);
int pthread_cancel(pthread_t thread)
{
__CPROVER_HIDE:;
(void)thread;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(&thread, "pthread-id"),
"pthread_cancel must be given valid thread ID");
#endif
int result=__VERIFIER_nondet_int();
return result;
}
/* FUNCTION: pthread_mutex_init */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_mutex_t_defined
#define __CPROVER_mutex_t_defined
#if defined __CYGWIN__ || defined __MINGW32__ || defined _WIN32
// on Windows, the mutexes are integers already
typedef pthread_mutex_t __CPROVER_mutex_t;
#else
typedef signed char __CPROVER_mutex_t;
#endif
#endif
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
void pthread_mutex_cleanup(void *p)
{
__CPROVER_HIDE:;
__CPROVER_assert(
__CPROVER_get_must(p, "mutex-destroyed"),
"mutex must be destroyed");
}
#endif
int pthread_mutex_init(
pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutexattr)
{
__CPROVER_HIDE:;
*((__CPROVER_mutex_t *)mutex)=0;
if(mutexattr!=0) (void)*mutexattr;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_cleanup(mutex, pthread_mutex_cleanup);
__CPROVER_set_must(mutex, "mutex-init");
__CPROVER_clear_may(mutex, "mutex-destroyed");
if(__CPROVER_get_must(mutexattr, "mutexattr-recursive"))
__CPROVER_set_must(mutex, "mutex-recursive");
#endif
return 0;
}
/* FUNCTION: pthread_mutex_lock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_mutex_t_defined
#define __CPROVER_mutex_t_defined
#if defined __CYGWIN__ || defined __MINGW32__ || defined _WIN32
// on Windows, the mutexes are integers already
typedef pthread_mutex_t __CPROVER_mutex_t;
#else
typedef signed char __CPROVER_mutex_t;
#endif
#endif
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
__CPROVER_HIDE:;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-init"),
"mutex must be initialized");
__CPROVER_assert(!__CPROVER_get_may(mutex, "mutex-destroyed"),
"mutex must not be destroyed");
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-recursive") ||
!__CPROVER_get_may(mutex, "mutex-locked"),
"attempt to lock non-recurisive locked mutex");
__CPROVER_set_must(mutex, "mutex-locked");
__CPROVER_set_may(mutex, "mutex-locked");
__CPROVER_assert(*((__CPROVER_mutex_t *)mutex)!=-1,
"mutex not initialised or destroyed");
#else
__CPROVER_atomic_begin();
__CPROVER_assume(!*((__CPROVER_mutex_t *)mutex));
*((__CPROVER_mutex_t *)mutex)=1;
__CPROVER_atomic_end();
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
#endif
return 0; // we never fail
}
/* FUNCTION: pthread_mutex_trylock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_mutex_t_defined
#define __CPROVER_mutex_t_defined
#if defined __CYGWIN__ || defined __MINGW32__ || defined _WIN32
// on Windows, the mutexes are integers already
typedef pthread_mutex_t __CPROVER_mutex_t;
#else
typedef signed char __CPROVER_mutex_t;
#endif
#endif
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
__CPROVER_HIDE:;
int return_value;
__CPROVER_atomic_begin();
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-init"),
"mutex must be initialized");
__CPROVER_assert(*((__CPROVER_mutex_t *)mutex)!=-1,
"mutex not initialised or destroyed");
#endif
if(*((__CPROVER_mutex_t *)mutex)==1)
{
// failed
return_value=1;
}
else
{
// ok
return_value=0;
*((__CPROVER_mutex_t *)mutex)=1;
}
__CPROVER_atomic_end();
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
return return_value;
}
/* FUNCTION: pthread_mutex_unlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_mutex_t_defined
#define __CPROVER_mutex_t_defined
#if defined __CYGWIN__ || defined __MINGW32__ || defined _WIN32
// on Windows, the mutexes are integers already
typedef pthread_mutex_t __CPROVER_mutex_t;
#else
typedef signed char __CPROVER_mutex_t;
#endif
#endif
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
__CPROVER_HIDE:;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-init"),
"mutex must be initialized");
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-locked"),
"mutex must be locked");
__CPROVER_assert(!__CPROVER_get_may(mutex, "mutex-destroyed"),
"mutex must not be destroyed");
__CPROVER_clear_may(mutex, "mutex-locked");
#else
// the fence must be before the unlock
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
__CPROVER_atomic_begin();
__CPROVER_assert(*((__CPROVER_mutex_t *)mutex)==1,
"must hold lock upon unlock");
*((__CPROVER_mutex_t *)mutex)=0;
__CPROVER_atomic_end();
#endif
return 0; // we never fail
}
/* FUNCTION: pthread_mutex_destroy */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_mutex_t_defined
#define __CPROVER_mutex_t_defined
#if defined __CYGWIN__ || defined __MINGW32__ || defined _WIN32
// on Windows, the mutexes are integers already
typedef pthread_mutex_t __CPROVER_mutex_t;
#else
typedef signed char __CPROVER_mutex_t;
#endif
#endif
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
__CPROVER_HIDE:;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-init"),
"mutex must be initialized");
__CPROVER_assert(!__CPROVER_get_may(mutex, "mutex-locked"),
"mutex must not be locked");
__CPROVER_assert(!__CPROVER_get_may(mutex, "mutex-destroyed"),
"mutex must not be destroyed");
__CPROVER_set_must(mutex, "mutex-destroyed");
__CPROVER_set_may(mutex, "mutex-destroyed");
#else
__CPROVER_assert(*((__CPROVER_mutex_t *)mutex)==0,
"lock held upon destroy");
*((__CPROVER_mutex_t *)mutex)=-1;
#endif
return 0;
}
/* FUNCTION: pthread_exit */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
// Clang refuses anything larger than SIZE_MAX/8, thus using - 4 in the shift
// expression below
__CPROVER_bool __CPROVER_threads_exited
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 4)];
__CPROVER_thread_local unsigned long __CPROVER_thread_id = 0;
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
__CPROVER_thread_local const void
*__CPROVER_thread_keys[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 4)];
__CPROVER_thread_local void (
*__CPROVER_thread_key_dtors[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 4)])(void *);
__CPROVER_thread_local unsigned long __CPROVER_next_thread_key = 0;
#endif
void pthread_exit(void *value_ptr)
{
__CPROVER_HIDE:;
if(value_ptr!=0) (void)*(char*)value_ptr;
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
for(unsigned long i = 0; i < __CPROVER_next_thread_key; ++i)
{
const void *key = __CPROVER_thread_keys[i];
__CPROVER_thread_keys[i] = 0;
if(__CPROVER_thread_key_dtors[i] && key)
__CPROVER_thread_key_dtors[i](key);
}
#endif
__CPROVER_threads_exited[__CPROVER_thread_id]=1;
__CPROVER_assume(0);
#ifdef LIBRARY_CHECK
__builtin_unreachable();
#endif
}
/* FUNCTION: pthread_join */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_ERRNO_H_INCLUDED
#include <errno.h>
#define __CPROVER_ERRNO_H_INCLUDED
#endif
// Clang refuses anything larger than SIZE_MAX/8, thus using - 4 in the shift
// expression below
__CPROVER_bool __CPROVER_threads_exited
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 4)];
#ifndef LIBRARY_CHECK
__CPROVER_thread_local unsigned long __CPROVER_thread_id = 0;
#endif
unsigned long __CPROVER_next_thread_id = 0;
int pthread_join(pthread_t thread, void **value_ptr)
{
__CPROVER_HIDE:;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(
__CPROVER_get_must(&thread, "pthread-id"),
"pthread_join must be given valid thread ID");
#endif
if((unsigned long)thread>__CPROVER_next_thread_id) return ESRCH;
if((unsigned long)thread==__CPROVER_thread_id) return EDEADLK;
if(value_ptr!=0) (void)**(char**)value_ptr;
__CPROVER_assume(__CPROVER_threads_exited[(unsigned long)thread]);
return 0;
}
/* FUNCTION: _pthread_join */
// This is for Apple
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_ERRNO_H_INCLUDED
#include <errno.h>
#define __CPROVER_ERRNO_H_INCLUDED
#endif
#ifdef __APPLE__
__CPROVER_bool __CPROVER_threads_exited
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 2)];
# ifndef LIBRARY_CHECK
__CPROVER_thread_local unsigned long __CPROVER_thread_id = 0;
unsigned long __CPROVER_next_thread_id = 0;
# endif
int _pthread_join(pthread_t thread, void **value_ptr)
{
__CPROVER_HIDE:;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(
__CPROVER_get_must(&thread, "pthread-id"),
"pthread_join must be given valid thread ID");
#endif
if((unsigned long)thread>__CPROVER_next_thread_id) return ESRCH;
if((unsigned long)thread==__CPROVER_thread_id) return EDEADLK;
if(value_ptr!=0) (void)**(char**)value_ptr;
__CPROVER_assume(__CPROVER_threads_exited[(unsigned long)thread]);
return 0;
}
#endif
/* FUNCTION: pthread_rwlock_destroy */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_destroy(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_assert(*((signed char *)lock)==0,
"rwlock held upon destroy");
*((signed char *)lock)=-1;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_set_must(lock, "rwlock_destroyed");
#endif
return 0;
}
/* FUNCTION: pthread_rwlock_init */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
void pthread_rwlock_cleanup(void *p)
{
__CPROVER_HIDE:;
__CPROVER_assert(__CPROVER_get_must(p, "rwlock_destroyed"),
"rwlock must be destroyed");
}
#endif
int pthread_rwlock_init(
pthread_rwlock_t *lock,
const pthread_rwlockattr_t *attr)
{
__CPROVER_HIDE:;
(*(signed char *)lock)=0;
if(attr!=0) (void)*attr;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_cleanup(lock, pthread_rwlock_cleanup);
#endif
return 0;
}
/* FUNCTION: pthread_rwlock_rdlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_atomic_begin();
__CPROVER_assert(*((signed char *)lock)!=-1,
"lock not initialised or destroyed");
__CPROVER_assume(!*((signed char *)lock));
*((signed char *)lock)=1;
__CPROVER_atomic_end();
return 0; // we never fail
}
/* FUNCTION: pthread_rwlock_tryrdlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_atomic_begin();
if((*(signed char *)lock &2)!=0) { __CPROVER_atomic_end(); return 1; }
(*(signed char *)lock)|=1;
__CPROVER_atomic_end();
return 0;
}
/* FUNCTION: pthread_rwlock_trywrlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_trywrlock(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_atomic_begin();
if(*(signed char *)lock) { __CPROVER_atomic_end(); return 1; }
(*(signed char *)lock)=2;
__CPROVER_atomic_end();
return 0;
}
/* FUNCTION: pthread_rwlock_unlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_unlock(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_assert(*((signed char *)lock)==1,
"must hold lock upon unlock");
// TODO: unlocks all held locks at once
*((signed char *)lock)=0;
return 0; // we never fail
}
/* FUNCTION: pthread_rwlock_wrlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_atomic_begin();
__CPROVER_assert(*((signed char *)lock)!=-1,
"lock not initialised or destroyed");
__CPROVER_assume(!*((signed char *)lock));
*((signed char *)lock)=2;
__CPROVER_atomic_end();
return 0; // we never fail
}
/* FUNCTION: __spawned_thread */
// Clang refuses anything larger than SIZE_MAX/8, thus using - 4 in the shift
// expression below
__CPROVER_bool __CPROVER_threads_exited
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 4)];
#ifndef LIBRARY_CHECK
__CPROVER_thread_local unsigned long __CPROVER_thread_id = 0;
#endif
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
__CPROVER_thread_local const void
*__CPROVER_thread_keys[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 2)];
__CPROVER_thread_local void (
*__CPROVER_thread_key_dtors[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 2)])(void *);
#endif
__CPROVER_thread_local unsigned long __CPROVER_next_thread_key = 0;
void __spawned_thread(
unsigned long this_thread_id,
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
void (**thread_key_dtors)(void *),
#endif
unsigned long next_thread_key,
void *(*start_routine)(void *),
void *arg)
{
__CPROVER_HIDE:;
__CPROVER_thread_id = this_thread_id;
__CPROVER_next_thread_key = next_thread_key;
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
for(unsigned long i = 0; i < __CPROVER_next_thread_key; ++i)
__CPROVER_thread_key_dtors[i] = thread_key_dtors[i];
#endif
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
// Clear all locked mutexes; locking must happen in same thread.
__CPROVER_clear_must(0, "mutex-locked");
__CPROVER_clear_may(0, "mutex-locked");
#endif
start_routine(arg);
__CPROVER_fence(
"WWfence",
"RRfence",
"RWfence",
"WRfence",
"WWcumul",
"RRcumul",
"RWcumul",
"WRcumul");
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
for(unsigned long i = 0; i < __CPROVER_next_thread_key; ++i)
{
const void *key = __CPROVER_thread_keys[i];
__CPROVER_thread_keys[i] = 0;
if(__CPROVER_thread_key_dtors[i] && key)
__CPROVER_thread_key_dtors[i](key);
}
#endif
__CPROVER_threads_exited[this_thread_id] = 1;
}
/* FUNCTION: pthread_create */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
# include <pthread.h>
# define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef LIBRARY_CHECK
unsigned long __CPROVER_next_thread_id = 0;
# if 0
__CPROVER_thread_local void (
*__CPROVER_thread_key_dtors[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 2)])(void *);
# endif
__CPROVER_thread_local unsigned long __CPROVER_next_thread_key = 0;
#endif
void __spawned_thread(
unsigned long this_thread_id,
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
void (**thread_key_dtors)(void *),
#endif
unsigned long next_thread_key,
void *(*start_routine)(void *),
void *arg);
int pthread_create(
pthread_t *thread, // must not be null
const pthread_attr_t *attr, // may be null
void *(*start_routine)(void *), // must not be null
void *arg) // may be null
{
__CPROVER_HIDE:;
unsigned long this_thread_id;
__CPROVER_atomic_begin();
this_thread_id=++__CPROVER_next_thread_id;
__CPROVER_atomic_end();
// pthread_t is a pointer type on some systems
*thread=(pthread_t)this_thread_id;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_set_must(thread, "pthread-id");
#endif
if(attr) (void)*attr;
unsigned long next_thread_key = __CPROVER_next_thread_key;
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
void (**thread_key_dtors)(void *) = __CPROVER_thread_key_dtors;
#endif
__CPROVER_ASYNC_1:
__spawned_thread(
this_thread_id,
#if 0
// Destructor support is disabled as it is too expensive due to its
// extensive use of shared variables.
thread_key_dtors,
#endif
next_thread_key,
start_routine,
arg);
return 0;
}
/* FUNCTION: pthread_cond_init */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{ __CPROVER_HIDE:
*((unsigned *)cond)=0;
if(attr) (void)*attr;
return 0;
}
/* FUNCTION: pthread_cond_signal */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_cond_signal(pthread_cond_t *cond)
{ __CPROVER_HIDE:
__CPROVER_atomic_begin();
(*((unsigned *)cond))++;
__CPROVER_atomic_end();
return 0;
}
/* FUNCTION: pthread_cond_broadcast */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_cond_broadcast(pthread_cond_t *cond)
{ __CPROVER_HIDE:
__CPROVER_atomic_begin();
*((unsigned *)cond)=(unsigned)-1;
__CPROVER_atomic_end();
return 0;
}
/* FUNCTION: pthread_cond_wait */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ __CPROVER_HIDE:
(void)*mutex;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-init"),
"mutex must be initialized");
__CPROVER_assert(__CPROVER_get_must(mutex, "mutex-locked"),
"mutex must be locked");
__CPROVER_assert(!__CPROVER_get_may(mutex, "mutex-destroyed"),
"mutex must not be destroyed");
__CPROVER_clear_may(mutex, "mutex-locked");
#endif
__CPROVER_atomic_begin();
if(*((unsigned *)cond))
(*((unsigned *)cond))--;
__CPROVER_atomic_end();
return 0; // we never fail
}
/* FUNCTION: pthread_spin_lock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
// no pthread_spinlock_t on the Mac
#ifndef __APPLE__
int pthread_spin_lock(pthread_spinlock_t *lock)
{
__CPROVER_HIDE:;
__CPROVER_atomic_begin();
__CPROVER_assume(!*((unsigned *)lock));
(*((unsigned *)lock))=1;
__CPROVER_atomic_end();
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
return 0;
}
#endif
/* FUNCTION: pthread_spin_unlock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
// no pthread_spinlock_t on the Mac
#ifndef __APPLE__
int pthread_spin_unlock(pthread_spinlock_t *lock)
{
__CPROVER_HIDE:;
// This is atomic_full_barrier() in glibc.
// The fence must be before the unlock.
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
*((unsigned *)lock) = 0;
return 0;
}
#endif
/* FUNCTION: pthread_spin_trylock */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
#ifndef __CPROVER_ERRNO_H_INCLUDED
#include <errno.h>
#define __CPROVER_ERRNO_H_INCLUDED
#endif
// no pthread_spinlock_t on the Mac
#ifndef __APPLE__
int pthread_spin_trylock(pthread_spinlock_t *lock)
{
__CPROVER_HIDE:;
int result;
__CPROVER_atomic_begin();
if(*((unsigned *)lock))
result=EBUSY;
else
{
result=0;
(*((unsigned *)lock))=1;
}
__CPROVER_atomic_end();
__CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence",
"WWcumul", "RRcumul", "RWcumul", "WRcumul");
return result;
}
#endif
/* FUNCTION: pthread_barrier_init */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int __VERIFIER_nondet_int(void);
// no pthread_barrier_t on the Mac
// slightly different declaration on OpenBSD
#if !defined(__APPLE__) && !defined(__OpenBSD__)
int pthread_barrier_init(
pthread_barrier_t *restrict barrier,
const pthread_barrierattr_t *restrict attr,
unsigned count)
{
__CPROVER_HIDE:;
(void)barrier;
(void)attr;
(void)count;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_set_must(barrier, "barrier-init");
__CPROVER_clear_may(barrier, "barrier-destroyed");
#endif
int result=__VERIFIER_nondet_int();
return result;
}
#endif
// pthread_barrier_init has a slightly different decl on OpenBSD
#if defined(__OpenBSD__)
int pthread_barrier_init(
pthread_barrier_t *restrict barrier,
pthread_barrierattr_t *restrict attr,
unsigned count)
{
__CPROVER_HIDE:;
(void)barrier;
(void)attr;
(void)count;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_set_must(barrier, "barrier-init");
__CPROVER_clear_may(barrier, "barrier-destroyed");
#endif
int result = __VERIFIER_nondet_int();
return result;
}
#endif
/* FUNCTION: pthread_barrier_destroy */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int __VERIFIER_nondet_int(void);
// no pthread_barrier_t on the Mac
#ifndef __APPLE__
int pthread_barrier_destroy(pthread_barrier_t *barrier)
{
__CPROVER_HIDE:;
(void)barrier;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(barrier, "barrier-init"),
"pthread barrier must be initialized");
__CPROVER_assert(!__CPROVER_get_may(barrier, "barrier-destroyed"),
"pthread barrier must not be destroyed");
__CPROVER_set_may(barrier, "barrier-destroyed");
#endif
int result=__VERIFIER_nondet_int();
return result;
}
#endif
/* FUNCTION: pthread_barrier_wait */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
int __VERIFIER_nondet_int(void);
// no pthread_barrier_t on the Mac
#ifndef __APPLE__
int pthread_barrier_wait(pthread_barrier_t *barrier)
{
__CPROVER_HIDE:;
(void)barrier;
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
__CPROVER_assert(__CPROVER_get_must(barrier, "barrier-init"),
"pthread barrier must be initialized");
__CPROVER_assert(!__CPROVER_get_may(barrier, "barrier-destroyed"),
"pthread barrier must not be destroyed");
#endif
int result=__VERIFIER_nondet_int();
return result;
}
#endif
/* FUNCTION: pthread_key_create */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
__CPROVER_thread_local const void *__CPROVER_thread_keys
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 1 - sizeof(void *))];
#ifndef LIBRARY_CHECK
# if 0
__CPROVER_thread_local void (
*__CPROVER_thread_key_dtors[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 2)])(void *);
# endif
__CPROVER_thread_local unsigned long __CPROVER_next_thread_key = 0;
#endif
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
{
__CPROVER_HIDE:;
__CPROVER_thread_keys[__CPROVER_next_thread_key] = 0;
#if 0
// Destructor support is disabled as it is too expensive due to its extensive
// use of shared variables.
__CPROVER_thread_key_dtors[__CPROVER_next_thread_key] = destructor;
#else
__CPROVER_precondition(destructor == 0, "destructors are not yet supported");
#endif
*key = __CPROVER_next_thread_key++;
return 0;
}
/* FUNCTION: pthread_key_delete */
#ifndef __CPROVER_PTHREAD_H_INCLUDED
#include <pthread.h>
#define __CPROVER_PTHREAD_H_INCLUDED
#endif
__CPROVER_thread_local const void *__CPROVER_thread_keys
[(__CPROVER_size_t)1 << (sizeof(__CPROVER_size_t) * 8 - 1 - sizeof(void *))];
int pthread_key_delete(pthread_key_t key)