/
hg_intercepts.c
2460 lines (2070 loc) · 73.6 KB
/
hg_intercepts.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
/*--------------------------------------------------------------------*/
/*--- pthread intercepts for thread checking. ---*/
/*--- hg_intercepts.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Helgrind, a Valgrind tool for detecting errors
in threaded programs.
Copyright (C) 2007-2011 OpenWorks LLP
info@open-works.co.uk
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
Neither the names of the U.S. Department of Energy nor the
University of California nor the names of its contributors may be
used to endorse or promote products derived from this software
without prior written permission.
*/
/* RUNS ON SIMULATED CPU
Interceptors for pthread_* functions, so that tc_main can see
significant thread events.
Important: when adding a function wrapper to this file, remember to
add a test case to tc20_verifywrap.c. A common cause of failure is
for wrappers to not engage on different distros, and
tc20_verifywrap essentially checks that each wrapper is really
doing something.
*/
// DDD: for Darwin, need to have non-"@*"-suffixed versions for all pthread
// functions that currently have them.
// Note also, in the comments and code below, all Darwin symbols start
// with a leading underscore, which is not shown either in the comments
// nor in the redirect specs.
#include "pub_tool_basics.h"
#include "pub_tool_redir.h"
#include "valgrind.h"
#include "helgrind.h"
#include "config.h"
#define TRACE_PTH_FNS 0
#define TRACE_QT4_FNS 0
/*----------------------------------------------------------------*/
/*--- ---*/
/*----------------------------------------------------------------*/
#define PTH_FUNC(ret_ty, f, args...) \
ret_ty I_WRAP_SONAME_FNNAME_ZZ(VG_Z_LIBPTHREAD_SONAME,f)(args); \
ret_ty I_WRAP_SONAME_FNNAME_ZZ(VG_Z_LIBPTHREAD_SONAME,f)(args)
// Do a client request. These are macros rather than a functions so
// as to avoid having an extra frame in stack traces.
// NB: these duplicate definitions in helgrind.h. But here, we
// can have better typing (Word etc) and assertions, whereas
// in helgrind.h we can't. Obviously it's important the two
// sets of definitions are kept in sync.
// nuke the previous definitions
#undef DO_CREQ_v_W
#undef DO_CREQ_v_WW
#undef DO_CREQ_W_WW
#undef DO_CREQ_v_WWW
#define DO_CREQ_v_W(_creqF, _ty1F,_arg1F) \
do { \
Word _arg1; \
assert(sizeof(_ty1F) == sizeof(Word)); \
_arg1 = (Word)(_arg1F); \
VALGRIND_DO_CLIENT_REQUEST_STMT((_creqF), \
_arg1, 0,0,0,0); \
} while (0)
#define DO_CREQ_v_WW(_creqF, _ty1F,_arg1F, _ty2F,_arg2F) \
do { \
Word _arg1, _arg2; \
assert(sizeof(_ty1F) == sizeof(Word)); \
assert(sizeof(_ty2F) == sizeof(Word)); \
_arg1 = (Word)(_arg1F); \
_arg2 = (Word)(_arg2F); \
VALGRIND_DO_CLIENT_REQUEST_STMT((_creqF), \
_arg1,_arg2,0,0,0); \
} while (0)
#define DO_CREQ_W_WW(_resF, _creqF, _ty1F,_arg1F, \
_ty2F,_arg2F) \
do { \
Word _res, _arg1, _arg2; \
assert(sizeof(_ty1F) == sizeof(Word)); \
assert(sizeof(_ty2F) == sizeof(Word)); \
_arg1 = (Word)(_arg1F); \
_arg2 = (Word)(_arg2F); \
_res = VALGRIND_DO_CLIENT_REQUEST_EXPR(2, \
(_creqF), \
_arg1,_arg2,0,0,0); \
_resF = _res; \
} while (0)
#define DO_CREQ_v_WWW(_creqF, _ty1F,_arg1F, \
_ty2F,_arg2F, _ty3F, _arg3F) \
do { \
Word _arg1, _arg2, _arg3; \
assert(sizeof(_ty1F) == sizeof(Word)); \
assert(sizeof(_ty2F) == sizeof(Word)); \
assert(sizeof(_ty3F) == sizeof(Word)); \
_arg1 = (Word)(_arg1F); \
_arg2 = (Word)(_arg2F); \
_arg3 = (Word)(_arg3F); \
VALGRIND_DO_CLIENT_REQUEST_STMT((_creqF), \
_arg1,_arg2,_arg3,0,0); \
} while (0)
#define DO_PthAPIerror(_fnnameF, _errF) \
do { \
char* _fnname = (char*)(_fnnameF); \
long _err = (long)(int)(_errF); \
char* _errstr = lame_strerror(_err); \
DO_CREQ_v_WWW(_VG_USERREQ__HG_PTH_API_ERROR, \
char*,_fnname, \
long,_err, char*,_errstr); \
} while (0)
/* Needed for older glibcs (2.3 and older, at least) who don't
otherwise "know" about pthread_rwlock_anything or about
PTHREAD_MUTEX_RECURSIVE (amongst things). */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
/* A lame version of strerror which doesn't use the real libc
strerror_r, since using the latter just generates endless more
threading errors (glibc goes off and does tons of crap w.r.t.
locales etc) */
static char* lame_strerror ( long err )
{ switch (err) {
case EPERM: return "EPERM: Operation not permitted";
case ENOENT: return "ENOENT: No such file or directory";
case ESRCH: return "ESRCH: No such process";
case EINTR: return "EINTR: Interrupted system call";
case EBADF: return "EBADF: Bad file number";
case EAGAIN: return "EAGAIN: Try again";
case ENOMEM: return "ENOMEM: Out of memory";
case EACCES: return "EACCES: Permission denied";
case EFAULT: return "EFAULT: Bad address";
case EEXIST: return "EEXIST: File exists";
case EINVAL: return "EINVAL: Invalid argument";
case EMFILE: return "EMFILE: Too many open files";
case ENOSYS: return "ENOSYS: Function not implemented";
case EOVERFLOW: return "EOVERFLOW: Value too large "
"for defined data type";
case EBUSY: return "EBUSY: Device or resource busy";
case ETIMEDOUT: return "ETIMEDOUT: Connection timed out";
case EDEADLK: return "EDEADLK: Resource deadlock would occur";
case EOPNOTSUPP: return "EOPNOTSUPP: Operation not supported on "
"transport endpoint"; /* honest, guv */
default: return "tc_intercepts.c: lame_strerror(): "
"unhandled case -- please fix me!";
}
}
/*----------------------------------------------------------------*/
/*--- pthread_create, pthread_join, pthread_exit ---*/
/*----------------------------------------------------------------*/
static void* mythread_wrapper ( void* xargsV )
{
volatile Word* xargs = (volatile Word*) xargsV;
void*(*fn)(void*) = (void*(*)(void*))xargs[0];
void* arg = (void*)xargs[1];
pthread_t me = pthread_self();
/* Tell the tool what my pthread_t is. */
DO_CREQ_v_W(_VG_USERREQ__HG_SET_MY_PTHREAD_T, pthread_t,me);
/* allow the parent to proceed. We can't let it proceed until
we're ready because (1) we need to make sure it doesn't exit and
hence deallocate xargs[] while we still need it, and (2) we
don't want either parent nor child to proceed until the tool has
been notified of the child's pthread_t.
Note that parent and child access args[] without a lock,
effectively using args[2] as a spinlock in order to get the
parent to wait until the child passes this point. The parent
disables checking on xargs[] before creating the child and
re-enables it once the child goes past this point, so the user
never sees the race. The previous approach (suppressing the
resulting error) was flawed, because it could leave shadow
memory for args[] in a state in which subsequent use of it by
the parent would report further races. */
xargs[2] = 0;
/* Now we can no longer safely use xargs[]. */
return (void*) fn( (void*)arg );
}
//-----------------------------------------------------------
// glibc: pthread_create@GLIBC_2.0
// glibc: pthread_create@@GLIBC_2.1
// glibc: pthread_create@@GLIBC_2.2.5
// darwin: pthread_create
// darwin: pthread_create_suspended_np (trapped)
//
/* ensure this has its own frame, so as to make it more distinguishable
in suppressions */
__attribute__((noinline))
static int pthread_create_WRK(pthread_t *thread, const pthread_attr_t *attr,
void *(*start) (void *), void *arg)
{
int ret;
OrigFn fn;
volatile Word xargs[3];
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_create wrapper"); fflush(stderr);
}
xargs[0] = (Word)start;
xargs[1] = (Word)arg;
xargs[2] = 1; /* serves as a spinlock -- sigh */
/* Disable checking on the spinlock and the two words used to
convey args to the child. Basically we need to make it appear
as if the child never accessed this area, since merely
suppressing the resulting races does not address the issue that
that piece of the parent's stack winds up in the "wrong" state
and therefore may give rise to mysterious races when the parent
comes to re-use this piece of stack in some other frame. */
VALGRIND_HG_DISABLE_CHECKING(&xargs, sizeof(xargs));
CALL_FN_W_WWWW(ret, fn, thread,attr,mythread_wrapper,&xargs[0]);
if (ret == 0) {
/* we have to wait for the child to notify the tool of its
pthread_t before continuing */
while (xargs[2] != 0) {
/* Do nothing. We need to spin until the child writes to
xargs[2]. However, that can lead to starvation in the
child and very long delays (eg, tc19_shadowmem on
ppc64-linux Fedora Core 6). So yield the cpu if we can,
to let the child run at the earliest available
opportunity. */
sched_yield();
}
} else {
DO_PthAPIerror( "pthread_create", ret );
}
/* Reenable checking on the area previously used to communicate
with the child. */
VALGRIND_HG_ENABLE_CHECKING(&xargs, sizeof(xargs));
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: pth_create -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucreateZAZa, // pthread_create@*
pthread_t *thread, const pthread_attr_t *attr,
void *(*start) (void *), void *arg) {
return pthread_create_WRK(thread, attr, start, arg);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucreate, // pthread_create
pthread_t *thread, const pthread_attr_t *attr,
void *(*start) (void *), void *arg) {
return pthread_create_WRK(thread, attr, start, arg);
}
PTH_FUNC(int, pthreadZucreateZuZa, // pthread_create_*
pthread_t *thread, const pthread_attr_t *attr,
void *(*start) (void *), void *arg) {
// trap anything else
assert(0);
}
#else
# error "Unsupported OS"
#endif
//-----------------------------------------------------------
// glibc: pthread_join
// darwin: pthread_join
// darwin: pthread_join$NOCANCEL$UNIX2003
// darwin pthread_join$UNIX2003
__attribute__((noinline))
static int pthread_join_WRK(pthread_t thread, void** value_pointer)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_join wrapper"); fflush(stderr);
}
CALL_FN_W_WW(ret, fn, thread,value_pointer);
/* At least with NPTL as the thread library, this is safe because
it is guaranteed (by NPTL) that the joiner will completely gone
before pthread_join (the original) returns. See email below.*/
if (ret == 0 /*success*/) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_JOIN_POST, pthread_t,thread);
} else {
DO_PthAPIerror( "pthread_join", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: pth_join -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZujoin, // pthread_join
pthread_t thread, void** value_pointer) {
return pthread_join_WRK(thread, value_pointer);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZujoinZa, // pthread_join*
pthread_t thread, void** value_pointer) {
return pthread_join_WRK(thread, value_pointer);
}
#else
# error "Unsupported OS"
#endif
/* Behaviour of pthread_join on NPTL:
Me:
I have a question re the NPTL pthread_join implementation.
Suppose I am the thread 'stayer'.
If I call pthread_join(quitter), is it guaranteed that the
thread 'quitter' has really exited before pthread_join returns?
IOW, is it guaranteed that 'quitter' will not execute any further
instructions after pthread_join returns?
I believe this is true based on the following analysis of
glibc-2.5 sources. However am not 100% sure and would appreciate
confirmation.
'quitter' will be running start_thread() in nptl/pthread_create.c
The last action of start_thread() is to exit via
__exit_thread_inline(0), which simply does sys_exit
(nptl/pthread_create.c:403)
'stayer' meanwhile is waiting for lll_wait_tid (pd->tid)
(call at nptl/pthread_join.c:89)
As per comment at nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h:536,
lll_wait_tid will not return until kernel notifies via futex
wakeup that 'quitter' has terminated.
Hence pthread_join cannot return until 'quitter' really has
completely disappeared.
Drepper:
> As per comment at nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h:536,
> lll_wait_tid will not return until kernel notifies via futex
> wakeup that 'quitter' has terminated.
That's the key. The kernel resets the TID field after the thread is
done. No way the joiner can return before the thread is gone.
*/
/*----------------------------------------------------------------*/
/*--- pthread_mutex_t functions ---*/
/*----------------------------------------------------------------*/
/* Handled: pthread_mutex_init pthread_mutex_destroy
pthread_mutex_lock
pthread_mutex_trylock pthread_mutex_timedlock
pthread_mutex_unlock
*/
//-----------------------------------------------------------
// glibc: pthread_mutex_init
// darwin: pthread_mutex_init
PTH_FUNC(int, pthreadZumutexZuinit, // pthread_mutex_init
pthread_mutex_t *mutex,
pthread_mutexattr_t* attr)
{
int ret;
long mbRec;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxinit %p", mutex); fflush(stderr);
}
mbRec = 0;
if (attr) {
int ty, zzz;
zzz = pthread_mutexattr_gettype(attr, &ty);
if (zzz == 0 && ty == PTHREAD_MUTEX_RECURSIVE)
mbRec = 1;
}
CALL_FN_W_WW(ret, fn, mutex,attr);
if (ret == 0 /*success*/) {
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_MUTEX_INIT_POST,
pthread_mutex_t*,mutex, long,mbRec);
} else {
DO_PthAPIerror( "pthread_mutex_init", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: mxinit -> %d >>\n", ret);
}
return ret;
}
//-----------------------------------------------------------
// glibc: pthread_mutex_destroy
// darwin: pthread_mutex_destroy
PTH_FUNC(int, pthreadZumutexZudestroy, // pthread_mutex_destroy
pthread_mutex_t *mutex)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxdestroy %p", mutex); fflush(stderr);
}
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_DESTROY_PRE,
pthread_mutex_t*,mutex);
CALL_FN_W_W(ret, fn, mutex);
if (ret != 0) {
DO_PthAPIerror( "pthread_mutex_destroy", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: mxdestroy -> %d >>\n", ret);
}
return ret;
}
//-----------------------------------------------------------
// glibc: pthread_mutex_lock
// darwin: pthread_mutex_lock
PTH_FUNC(int, pthreadZumutexZulock, // pthread_mutex_lock
pthread_mutex_t *mutex)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxlock %p", mutex); fflush(stderr);
}
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE,
pthread_mutex_t*,mutex, long,0/*!isTryLock*/);
CALL_FN_W_W(ret, fn, mutex);
/* There's a hole here: libpthread now knows the lock is locked,
but the tool doesn't, so some other thread could run and detect
that the lock has been acquired by someone (this thread). Does
this matter? Not sure, but I don't think so. */
if (ret == 0 /*success*/) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
pthread_mutex_t*,mutex);
} else {
DO_PthAPIerror( "pthread_mutex_lock", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: mxlock -> %d >>\n", ret);
}
return ret;
}
//-----------------------------------------------------------
// glibc: pthread_mutex_trylock
// darwin: pthread_mutex_trylock
//
// pthread_mutex_trylock. The handling needed here is very similar
// to that for pthread_mutex_lock, except that we need to tell
// the pre-lock creq that this is a trylock-style operation, and
// therefore not to complain if the lock is nonrecursive and
// already locked by this thread -- because then it'll just fail
// immediately with EBUSY.
PTH_FUNC(int, pthreadZumutexZutrylock, // pthread_mutex_trylock
pthread_mutex_t *mutex)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxtrylock %p", mutex); fflush(stderr);
}
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE,
pthread_mutex_t*,mutex, long,1/*isTryLock*/);
CALL_FN_W_W(ret, fn, mutex);
/* There's a hole here: libpthread now knows the lock is locked,
but the tool doesn't, so some other thread could run and detect
that the lock has been acquired by someone (this thread). Does
this matter? Not sure, but I don't think so. */
if (ret == 0 /*success*/) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
pthread_mutex_t*,mutex);
} else {
if (ret != EBUSY)
DO_PthAPIerror( "pthread_mutex_trylock", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: mxtrylock -> %d >>\n", ret);
}
return ret;
}
//-----------------------------------------------------------
// glibc: pthread_mutex_timedlock
// darwin: (doesn't appear to exist)
//
// pthread_mutex_timedlock. Identical logic to pthread_mutex_trylock.
PTH_FUNC(int, pthreadZumutexZutimedlock, // pthread_mutex_timedlock
pthread_mutex_t *mutex,
void* timeout)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxtimedlock %p %p", mutex, timeout);
fflush(stderr);
}
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE,
pthread_mutex_t*,mutex, long,1/*isTryLock-ish*/);
CALL_FN_W_WW(ret, fn, mutex,timeout);
/* There's a hole here: libpthread now knows the lock is locked,
but the tool doesn't, so some other thread could run and detect
that the lock has been acquired by someone (this thread). Does
this matter? Not sure, but I don't think so. */
if (ret == 0 /*success*/) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
pthread_mutex_t*,mutex);
} else {
if (ret != ETIMEDOUT)
DO_PthAPIerror( "pthread_mutex_timedlock", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " :: mxtimedlock -> %d >>\n", ret);
}
return ret;
}
//-----------------------------------------------------------
// glibc: pthread_mutex_unlock
// darwin: pthread_mutex_unlock
PTH_FUNC(int, pthreadZumutexZuunlock, // pthread_mutex_unlock
pthread_mutex_t *mutex)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_mxunlk %p", mutex); fflush(stderr);
}
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE,
pthread_mutex_t*,mutex);
CALL_FN_W_W(ret, fn, mutex);
if (ret == 0 /*success*/) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_POST,
pthread_mutex_t*,mutex);
} else {
DO_PthAPIerror( "pthread_mutex_unlock", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " mxunlk -> %d >>\n", ret);
}
return ret;
}
/*----------------------------------------------------------------*/
/*--- pthread_cond_t functions ---*/
/*----------------------------------------------------------------*/
/* Handled: pthread_cond_wait pthread_cond_timedwait
pthread_cond_signal pthread_cond_broadcast
pthread_cond_destroy
Unhandled: pthread_cond_init
-- is this important?
*/
//-----------------------------------------------------------
// glibc: pthread_cond_wait@GLIBC_2.2.5
// glibc: pthread_cond_wait@@GLIBC_2.3.2
// darwin: pthread_cond_wait
// darwin: pthread_cond_wait$NOCANCEL$UNIX2003
// darwin: pthread_cond_wait$UNIX2003
//
__attribute__((noinline))
static int pthread_cond_wait_WRK(pthread_cond_t* cond,
pthread_mutex_t* mutex)
{
int ret;
OrigFn fn;
unsigned long mutex_is_valid;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_cond_wait %p %p", cond, mutex);
fflush(stderr);
}
/* Tell the tool a cond-wait is about to happen, so it can check
for bogus argument values. In return it tells us whether it
thinks the mutex is valid or not. */
DO_CREQ_W_WW(mutex_is_valid,
_VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE,
pthread_cond_t*,cond, pthread_mutex_t*,mutex);
assert(mutex_is_valid == 1 || mutex_is_valid == 0);
/* Tell the tool we're about to drop the mutex. This reflects the
fact that in a cond_wait, we show up holding the mutex, and the
call atomically drops the mutex and waits for the cv to be
signalled. */
if (mutex_is_valid) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE,
pthread_mutex_t*,mutex);
}
CALL_FN_W_WW(ret, fn, cond,mutex);
/* these conditionals look stupid, but compare w/ same logic for
pthread_cond_timedwait below */
if (ret == 0 && mutex_is_valid) {
/* and now we have the mutex again */
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
pthread_mutex_t*,mutex);
}
if (ret == 0 && mutex_is_valid) {
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_COND_WAIT_POST,
pthread_cond_t*,cond, pthread_mutex_t*,mutex);
}
if (ret != 0) {
DO_PthAPIerror( "pthread_cond_wait", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " cowait -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucondZuwaitZAZa, // pthread_cond_wait@*
pthread_cond_t* cond, pthread_mutex_t* mutex) {
return pthread_cond_wait_WRK(cond, mutex);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucondZuwaitZa, // pthread_cond_wait*
pthread_cond_t* cond, pthread_mutex_t* mutex) {
return pthread_cond_wait_WRK(cond, mutex);
}
#else
# error "Unsupported OS"
#endif
//-----------------------------------------------------------
// glibc: pthread_cond_timedwait@@GLIBC_2.3.2
// glibc: pthread_cond_timedwait@GLIBC_2.2.5
// glibc: pthread_cond_timedwait@GLIBC_2.0
// darwin: pthread_cond_timedwait
// darwin: pthread_cond_timedwait$NOCANCEL$UNIX2003
// darwin: pthread_cond_timedwait$UNIX2003
// darwin: pthread_cond_timedwait_relative_np (trapped)
//
__attribute__((noinline))
static int pthread_cond_timedwait_WRK(pthread_cond_t* cond,
pthread_mutex_t* mutex,
struct timespec* abstime)
{
int ret;
OrigFn fn;
unsigned long mutex_is_valid;
Bool abstime_is_valid;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_cond_timedwait %p %p %p",
cond, mutex, abstime);
fflush(stderr);
}
/* Tell the tool a cond-wait is about to happen, so it can check
for bogus argument values. In return it tells us whether it
thinks the mutex is valid or not. */
DO_CREQ_W_WW(mutex_is_valid,
_VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE,
pthread_cond_t*,cond, pthread_mutex_t*,mutex);
assert(mutex_is_valid == 1 || mutex_is_valid == 0);
abstime_is_valid = abstime->tv_nsec >= 0 && abstime->tv_nsec < 1000000000;
/* Tell the tool we're about to drop the mutex. This reflects the
fact that in a cond_wait, we show up holding the mutex, and the
call atomically drops the mutex and waits for the cv to be
signalled. */
if (mutex_is_valid && abstime_is_valid) {
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE,
pthread_mutex_t*,mutex);
}
CALL_FN_W_WWW(ret, fn, cond,mutex,abstime);
if (!abstime_is_valid && ret != EINVAL) {
DO_PthAPIerror("Bug in libpthread: pthread_cond_timedwait "
"invalid abstime did not cause"
" EINVAL", ret);
}
if ((ret == 0 || ret == ETIMEDOUT) && mutex_is_valid) {
/* and now we have the mutex again */
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
pthread_mutex_t*,mutex);
}
if (ret == 0 && mutex_is_valid) {
DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_COND_WAIT_POST,
pthread_cond_t*,cond, pthread_mutex_t*,mutex);
}
if (ret != 0 && ret != ETIMEDOUT) {
DO_PthAPIerror( "pthread_cond_timedwait", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " cotimedwait -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucondZutimedwaitZAZa, // pthread_cond_timedwait@*
pthread_cond_t* cond, pthread_mutex_t* mutex,
struct timespec* abstime) {
return pthread_cond_timedwait_WRK(cond, mutex, abstime);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucondZutimedwait, // pthread_cond_timedwait
pthread_cond_t* cond, pthread_mutex_t* mutex,
struct timespec* abstime) {
return pthread_cond_timedwait_WRK(cond, mutex, abstime);
}
PTH_FUNC(int, pthreadZucondZutimedwaitZDZa, // pthread_cond_timedwait$*
pthread_cond_t* cond, pthread_mutex_t* mutex,
struct timespec* abstime) {
return pthread_cond_timedwait_WRK(cond, mutex, abstime);
}
PTH_FUNC(int, pthreadZucondZutimedwaitZuZa, // pthread_cond_timedwait_*
pthread_cond_t* cond, pthread_mutex_t* mutex,
struct timespec* abstime) {
assert(0);
}
#else
# error "Unsupported OS"
#endif
//-----------------------------------------------------------
// glibc: pthread_cond_signal@GLIBC_2.0
// glibc: pthread_cond_signal@GLIBC_2.2.5
// glibc: pthread_cond_signal@@GLIBC_2.3.2
// darwin: pthread_cond_signal
// darwin: pthread_cond_signal_thread_np (don't intercept this)
//
__attribute__((noinline))
static int pthread_cond_signal_WRK(pthread_cond_t* cond)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_cond_signal %p", cond);
fflush(stderr);
}
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_COND_SIGNAL_PRE,
pthread_cond_t*,cond);
CALL_FN_W_W(ret, fn, cond);
if (ret != 0) {
DO_PthAPIerror( "pthread_cond_signal", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " cosig -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucondZusignalZAZa, // pthread_cond_signal@*
pthread_cond_t* cond) {
return pthread_cond_signal_WRK(cond);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucondZusignal, // pthread_cond_signal
pthread_cond_t* cond) {
return pthread_cond_signal_WRK(cond);
}
#else
# error "Unsupported OS"
#endif
//-----------------------------------------------------------
// glibc: pthread_cond_broadcast@GLIBC_2.0
// glibc: pthread_cond_broadcast@GLIBC_2.2.5
// glibc: pthread_cond_broadcast@@GLIBC_2.3.2
// darwin: pthread_cond_broadcast
//
// Note, this is pretty much identical, from a dependency-graph
// point of view, with cond_signal, so the code is duplicated.
// Maybe it should be commoned up.
//
__attribute__((noinline))
static int pthread_cond_broadcast_WRK(pthread_cond_t* cond)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_cond_broadcast %p", cond);
fflush(stderr);
}
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_COND_BROADCAST_PRE,
pthread_cond_t*,cond);
CALL_FN_W_W(ret, fn, cond);
if (ret != 0) {
DO_PthAPIerror( "pthread_cond_broadcast", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " cobro -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucondZubroadcastZAZa, // pthread_cond_broadcast@*
pthread_cond_t* cond) {
return pthread_cond_broadcast_WRK(cond);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucondZubroadcast, // pthread_cond_broadcast
pthread_cond_t* cond) {
return pthread_cond_broadcast_WRK(cond);
}
#else
# error "Unsupported OS"
#endif
//-----------------------------------------------------------
// glibc: pthread_cond_destroy@@GLIBC_2.3.2
// glibc: pthread_cond_destroy@GLIBC_2.2.5
// glibc: pthread_cond_destroy@GLIBC_2.0
// darwin: pthread_cond_destroy
//
__attribute__((noinline))
static int pthread_cond_destroy_WRK(pthread_cond_t* cond)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_cond_destroy %p", cond);
fflush(stderr);
}
DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_COND_DESTROY_PRE,
pthread_cond_t*,cond);
CALL_FN_W_W(ret, fn, cond);
if (ret != 0) {
DO_PthAPIerror( "pthread_cond_destroy", ret );
}
if (TRACE_PTH_FNS) {
fprintf(stderr, " codestr -> %d >>\n", ret);
}
return ret;
}
#if defined(VGO_linux)
PTH_FUNC(int, pthreadZucondZudestroyZAZa, // pthread_cond_destroy@*
pthread_cond_t* cond) {
return pthread_cond_destroy_WRK(cond);
}
#elif defined(VGO_darwin)
PTH_FUNC(int, pthreadZucondZudestroy, // pthread_cond_destroy
pthread_cond_t* cond) {
return pthread_cond_destroy_WRK(cond);
}
#else
# error "Unsupported OS"
#endif
/*----------------------------------------------------------------*/
/*--- pthread_barrier_t functions ---*/
/*----------------------------------------------------------------*/
#if defined(HAVE_PTHREAD_BARRIER_INIT)
/* Handled: pthread_barrier_init
pthread_barrier_wait
pthread_barrier_destroy
Unhandled: pthread_barrierattr_destroy
pthread_barrierattr_getpshared
pthread_barrierattr_init
pthread_barrierattr_setpshared
-- are these important?
*/
//-----------------------------------------------------------
// glibc: pthread_barrier_init
// darwin: (doesn't appear to exist)
PTH_FUNC(int, pthreadZubarrierZuinit, // pthread_barrier_init
pthread_barrier_t* bar,
pthread_barrierattr_t* attr, unsigned long count)
{
int ret;
OrigFn fn;
VALGRIND_GET_ORIG_FN(fn);
if (TRACE_PTH_FNS) {
fprintf(stderr, "<< pthread_barrier_init %p %p %lu",
bar, attr, count);