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, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_clientobj.h"
25 #include "drd_error.h"
26 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
27 #include "pub_tool_libcassert.h" /* tl_assert() */
28 #include "pub_tool_libcprint.h" /* VG_(printf)() */
29 #include "pub_tool_machine.h" /* VG_(get_IP)() */
30 #include "pub_tool_mallocfree.h" /* VG_(malloc)(), VG_(free)()*/
31 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
34 /* Type definitions. */
36 /** Per-thread hb information. */
39 UWord tid
; // A DrdThreadId declared as UWord because
40 // this member variable is the key of an OSet.
41 Segment
* sg
; // Segment created before most recent
42 // ANNOTATE_HAPPENS_BEFORE().
46 /* Local functions. */
48 static void DRD_(hb_cleanup
)(struct hb_info
* p
);
51 /* Local variables. */
53 static Bool
DRD_(s_trace_hb
);
56 /* Function definitions. */
58 void DRD_(hb_set_trace
)(const Bool trace_hb
)
60 DRD_(s_trace_hb
) = trace_hb
;
64 * Initialize the structure *p with the specified thread ID.
67 void DRD_(hb_thread_initialize
)(struct hb_thread_info
* const p
,
68 const DrdThreadId tid
)
75 * Deallocate the memory that is owned by members of struct hb_thread_info.
77 static void DRD_(hb_thread_destroy
)(struct hb_thread_info
* const p
)
84 void DRD_(hb_initialize
)(struct hb_info
* const p
, const Addr hb
)
87 tl_assert(p
->a1
== hb
);
88 tl_assert(p
->type
== ClientHbvar
);
90 p
->cleanup
= (void(*)(DrdClientobj
*))(DRD_(hb_cleanup
));
92 p
->oset
= VG_(OSetGen_Create
)(0, 0, VG_(malloc
), "drd.hb",
97 * Free the memory that was allocated by hb_initialize(). Called by
98 * DRD_(clientobj_remove)().
100 static void DRD_(hb_cleanup
)(struct hb_info
* p
)
102 struct hb_thread_info
* r
;
105 VG_(OSetGen_ResetIter
)(p
->oset
);
106 for ( ; (r
= VG_(OSetGen_Next
)(p
->oset
)) != 0; )
107 DRD_(hb_thread_destroy
)(r
);
108 VG_(OSetGen_Destroy
)(p
->oset
);
112 * Report that the synchronization object at address 'addr' is of the
115 static void wrong_type(const Addr addr
)
117 GenericErrInfo gei
= {
118 .tid
= DRD_(thread_get_running_tid
)(),
121 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
123 VG_(get_IP
)(VG_(get_running_tid
)()),
124 "wrong type of synchronization object",
128 struct hb_info
* DRD_(hb_get_or_allocate
)(const Addr hb
)
132 tl_assert(offsetof(DrdClientobj
, hb
) == 0);
133 p
= &(DRD_(clientobj_get
)(hb
, ClientHbvar
)->hb
);
137 if (DRD_(clientobj_present
)(hb
, hb
+ 1))
143 p
= &(DRD_(clientobj_add
)(hb
, ClientHbvar
)->hb
);
144 DRD_(hb_initialize
)(p
, hb
);
148 struct hb_info
* DRD_(hb_get
)(const Addr hb
)
150 tl_assert(offsetof(DrdClientobj
, hb
) == 0);
151 return &(DRD_(clientobj_get
)(hb
, ClientHbvar
)->hb
);
154 /** Called because of a happens-before annotation. */
155 void DRD_(hb_happens_before
)(const DrdThreadId tid
, Addr
const hb
)
157 const ThreadId vg_tid
= VG_(get_running_tid
)();
158 const DrdThreadId drd_tid
= DRD_(VgThreadIdToDrdThreadId
)(vg_tid
);
159 const UWord word_tid
= tid
;
161 struct hb_thread_info
* q
;
163 p
= DRD_(hb_get_or_allocate
)(hb
);
164 if (DRD_(s_trace_hb
))
165 DRD_(trace_msg
)("[%u] happens_before 0x%lx",
166 DRD_(thread_get_running_tid
)(), hb
);
171 /* Allocate the per-thread data structure if necessary. */
172 q
= VG_(OSetGen_Lookup
)(p
->oset
, &word_tid
);
175 q
= VG_(OSetGen_AllocNode
)(p
->oset
, sizeof(*q
));
176 DRD_(hb_thread_initialize
)(q
, tid
);
177 VG_(OSetGen_Insert
)(p
->oset
, q
);
178 tl_assert(VG_(OSetGen_Lookup
)(p
->oset
, &word_tid
) == q
);
182 * Store a pointer to the latest segment of the current thread in the
183 * per-thread data structure.
185 DRD_(thread_get_latest_segment
)(&q
->sg
, tid
);
186 DRD_(thread_new_segment
)(drd_tid
);
189 /** Called because of a happens-after annotation. */
190 void DRD_(hb_happens_after
)(const DrdThreadId tid
, const Addr hb
)
193 struct hb_thread_info
* q
;
196 p
= DRD_(hb_get_or_allocate
)(hb
);
198 if (DRD_(s_trace_hb
))
199 DRD_(trace_msg
)("[%u] happens_after 0x%lx",
200 DRD_(thread_get_running_tid
)(), hb
);
205 DRD_(thread_new_segment
)(tid
);
208 * Combine all vector clocks that were stored because of happens-before
209 * annotations with the vector clock of the current thread.
211 DRD_(vc_copy
)(&old_vc
, DRD_(thread_get_vc
)(tid
));
212 VG_(OSetGen_ResetIter
)(p
->oset
);
213 for ( ; (q
= VG_(OSetGen_Next
)(p
->oset
)) != 0; )
218 DRD_(vc_combine
)(DRD_(thread_get_vc
)(tid
), &q
->sg
->vc
);
221 DRD_(thread_update_conflict_set
)(tid
, &old_vc
);
222 DRD_(vc_cleanup
)(&old_vc
);
225 /** Called because of a happens-done annotation. */
226 void DRD_(hb_happens_done
)(const DrdThreadId tid
, const Addr hb
)
230 if (DRD_(s_trace_hb
))
231 DRD_(trace_msg
)("[%u] happens_done 0x%lx",
232 DRD_(thread_get_running_tid
)(), hb
);
234 p
= DRD_(hb_get
)(hb
);
237 GenericErrInfo gei
= {
238 .tid
= DRD_(thread_get_running_tid
)(),
241 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
243 VG_(get_IP
)(VG_(get_running_tid
)()),
244 "missing happens-before annotation",
249 DRD_(clientobj_remove
)(p
->a1
, ClientHbvar
);