Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / drd / drd_hb.c
blob819b3c04836ca05ffe1c1356736618ec107c270b
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2017 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
21 The GNU General Public License is contained in the file COPYING.
25 #include "drd_clientobj.h"
26 #include "drd_hb.h"
27 #include "drd_error.h"
28 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
29 #include "pub_tool_libcassert.h" /* tl_assert() */
30 #include "pub_tool_libcprint.h" /* VG_(printf)() */
31 #include "pub_tool_machine.h" /* VG_(get_IP)() */
32 #include "pub_tool_mallocfree.h" /* VG_(malloc)(), VG_(free)()*/
33 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
36 /* Type definitions. */
38 /** Per-thread hb information. */
39 struct hb_thread_info
41 UWord tid; // A DrdThreadId declared as UWord because
42 // this member variable is the key of an OSet.
43 Segment* sg; // Segment created before most recent
44 // ANNOTATE_HAPPENS_BEFORE().
48 /* Local functions. */
50 static void DRD_(hb_cleanup)(struct hb_info* p);
53 /* Local variables. */
55 static Bool DRD_(s_trace_hb);
58 /* Function definitions. */
60 void DRD_(hb_set_trace)(const Bool trace_hb)
62 DRD_(s_trace_hb) = trace_hb;
65 /**
66 * Initialize the structure *p with the specified thread ID.
68 static
69 void DRD_(hb_thread_initialize)(struct hb_thread_info* const p,
70 const DrdThreadId tid)
72 p->tid = tid;
73 p->sg = 0;
76 /**
77 * Deallocate the memory that is owned by members of struct hb_thread_info.
79 static void DRD_(hb_thread_destroy)(struct hb_thread_info* const p)
81 tl_assert(p);
82 DRD_(sg_put)(p->sg);
85 static
86 void DRD_(hb_initialize)(struct hb_info* const p, const Addr hb)
88 tl_assert(hb != 0);
89 tl_assert(p->a1 == hb);
90 tl_assert(p->type == ClientHbvar);
92 p->cleanup = (void(*)(DrdClientobj*))(DRD_(hb_cleanup));
93 p->delete_thread = 0;
94 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.hb",
95 VG_(free));
98 /**
99 * Free the memory that was allocated by hb_initialize(). Called by
100 * DRD_(clientobj_remove)().
102 static void DRD_(hb_cleanup)(struct hb_info* p)
104 struct hb_thread_info* r;
106 tl_assert(p);
107 VG_(OSetGen_ResetIter)(p->oset);
108 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
109 DRD_(hb_thread_destroy)(r);
110 VG_(OSetGen_Destroy)(p->oset);
114 * Report that the synchronization object at address 'addr' is of the
115 * wrong type.
117 static void wrong_type(const Addr addr)
119 GenericErrInfo gei = {
120 .tid = DRD_(thread_get_running_tid)(),
121 .addr = addr,
123 VG_(maybe_record_error)(VG_(get_running_tid)(),
124 GenericErr,
125 VG_(get_IP)(VG_(get_running_tid)()),
126 "wrong type of synchronization object",
127 &gei);
130 struct hb_info* DRD_(hb_get_or_allocate)(const Addr hb)
132 struct hb_info *p;
134 tl_assert(offsetof(DrdClientobj, hb) == 0);
135 p = &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
136 if (p)
137 return p;
139 if (DRD_(clientobj_present)(hb, hb + 1))
141 wrong_type(hb);
142 return 0;
145 p = &(DRD_(clientobj_add)(hb, ClientHbvar)->hb);
146 DRD_(hb_initialize)(p, hb);
147 return p;
150 struct hb_info* DRD_(hb_get)(const Addr hb)
152 tl_assert(offsetof(DrdClientobj, hb) == 0);
153 return &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
156 /** Called because of a happens-before annotation. */
157 void DRD_(hb_happens_before)(const DrdThreadId tid, Addr const hb)
159 const ThreadId vg_tid = VG_(get_running_tid)();
160 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
161 const UWord word_tid = tid;
162 struct hb_info* p;
163 struct hb_thread_info* q;
165 p = DRD_(hb_get_or_allocate)(hb);
166 if (DRD_(s_trace_hb))
167 DRD_(trace_msg)("[%u] happens_before 0x%lx",
168 DRD_(thread_get_running_tid)(), hb);
170 if (!p)
171 return;
173 /* Allocate the per-thread data structure if necessary. */
174 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
175 if (!q)
177 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
178 DRD_(hb_thread_initialize)(q, tid);
179 VG_(OSetGen_Insert)(p->oset, q);
180 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
184 * Store a pointer to the latest segment of the current thread in the
185 * per-thread data structure.
187 DRD_(thread_get_latest_segment)(&q->sg, tid);
188 DRD_(thread_new_segment)(drd_tid);
191 /** Called because of a happens-after annotation. */
192 void DRD_(hb_happens_after)(const DrdThreadId tid, const Addr hb)
194 struct hb_info* p;
195 struct hb_thread_info* q;
196 VectorClock old_vc;
198 p = DRD_(hb_get_or_allocate)(hb);
200 if (DRD_(s_trace_hb))
201 DRD_(trace_msg)("[%u] happens_after 0x%lx",
202 DRD_(thread_get_running_tid)(), hb);
204 if (!p)
205 return;
207 DRD_(thread_new_segment)(tid);
210 * Combine all vector clocks that were stored because of happens-before
211 * annotations with the vector clock of the current thread.
213 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
214 VG_(OSetGen_ResetIter)(p->oset);
215 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
217 if (q->tid != tid)
219 tl_assert(q->sg);
220 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), &q->sg->vc);
223 DRD_(thread_update_conflict_set)(tid, &old_vc);
224 DRD_(vc_cleanup)(&old_vc);
227 /** Called because of a happens-done annotation. */
228 void DRD_(hb_happens_done)(const DrdThreadId tid, const Addr hb)
230 struct hb_info* p;
232 if (DRD_(s_trace_hb))
233 DRD_(trace_msg)("[%u] happens_done 0x%lx",
234 DRD_(thread_get_running_tid)(), hb);
236 p = DRD_(hb_get)(hb);
237 if (!p)
239 GenericErrInfo gei = {
240 .tid = DRD_(thread_get_running_tid)(),
241 .addr = hb,
243 VG_(maybe_record_error)(VG_(get_running_tid)(),
244 GenericErr,
245 VG_(get_IP)(VG_(get_running_tid)()),
246 "missing happens-before annotation",
247 &gei);
248 return;
251 DRD_(clientobj_remove)(p->a1, ClientHbvar);