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"
24 #include "drd_error.h"
25 #include "drd_suppression.h"
26 #include "pub_tool_basics.h"
27 #include "pub_tool_libcassert.h"
28 #include "pub_tool_libcbase.h"
29 #include "pub_tool_libcprint.h" // VG_(message)()
30 #include "pub_tool_mallocfree.h"
31 #include "pub_tool_options.h" // VG_(clo_backtrace_size)
32 #include "pub_tool_oset.h"
33 #include "pub_tool_stacktrace.h"
34 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
37 /* Local variables. */
39 static OSet
* s_clientobj_set
;
40 static Bool s_trace_clientobj
;
43 /* Local functions. */
45 static Bool
clientobj_remove_obj(DrdClientobj
* const p
);
48 /* Function definitions. */
50 void DRD_(clientobj_set_trace
)(const Bool trace
)
52 s_trace_clientobj
= trace
;
55 /** Initialize the client object set. */
56 void DRD_(clientobj_init
)(void)
58 tl_assert(s_clientobj_set
== 0);
59 s_clientobj_set
= VG_(OSetGen_Create
)(0, 0, VG_(malloc
),
60 "drd.clientobj.ci.1", VG_(free
));
64 * Free the memory allocated for the client object set.
66 * @pre Client object set is empty.
68 void DRD_(clientobj_cleanup
)(void)
70 tl_assert(s_clientobj_set
);
71 tl_assert(VG_(OSetGen_Size
)(s_clientobj_set
) == 0);
72 VG_(OSetGen_Destroy
)(s_clientobj_set
);
77 * Return the data associated with the client object at client address addr.
78 * Return 0 if there is no client object in the set with the specified start
81 DrdClientobj
* DRD_(clientobj_get_any
)(const Addr addr
)
83 return VG_(OSetGen_Lookup
)(s_clientobj_set
, &addr
);
87 * Return the data associated with the client object at client address addr
88 * and that has object type t. Return 0 if there is no client object in the
89 * set with the specified start address.
91 DrdClientobj
* DRD_(clientobj_get
)(const Addr addr
, const ObjType t
)
94 p
= VG_(OSetGen_Lookup
)(s_clientobj_set
, &addr
);
95 if (p
&& p
->any
.type
== t
)
100 /** Return true if and only if the address range of any client object overlaps
101 * with the specified address range.
103 Bool
DRD_(clientobj_present
)(const Addr a1
, const Addr a2
)
108 VG_(OSetGen_ResetIter
)(s_clientobj_set
);
109 for ( ; (p
= VG_(OSetGen_Next
)(s_clientobj_set
)) != 0; )
111 if (a1
<= p
->any
.a1
&& p
->any
.a1
< a2
)
120 * Add state information for the client object at client address addr and
121 * of type t. Suppress data race reports on the address range [addr,addr+size[.
123 * @pre No other client object is present in the address range [addr,addr+size[.
125 DrdClientobj
* DRD_(clientobj_add
)(const Addr a1
, const ObjType t
)
129 tl_assert(! DRD_(clientobj_present
)(a1
, a1
+ 1));
130 tl_assert(VG_(OSetGen_Lookup
)(s_clientobj_set
, &a1
) == 0);
132 if (s_trace_clientobj
)
133 DRD_(trace_msg
)("Adding client object 0x%lx of type %d", a1
, (Int
)t
);
135 p
= VG_(OSetGen_AllocNode
)(s_clientobj_set
, sizeof(*p
));
136 VG_(memset
)(p
, 0, sizeof(*p
));
139 p
->any
.first_observed_at
= VG_(record_ExeContext
)(VG_(get_running_tid
)(), 0);
140 VG_(OSetGen_Insert
)(s_clientobj_set
, p
);
141 tl_assert(VG_(OSetGen_Lookup
)(s_clientobj_set
, &a1
) == p
);
142 if (t
== ClientHbvar
)
143 DRD_(mark_hbvar
)(a1
);
145 DRD_(start_suppression
)(a1
, a1
+ 1, "clientobj");
150 * Remove the information that was stored about the client object.
152 * @param[in] addr Address of the client object in the client address space.
153 * @param[in] t Type of the client object.
155 Bool
DRD_(clientobj_remove
)(const Addr addr
, const ObjType t
)
159 p
= VG_(OSetGen_Lookup
)(s_clientobj_set
, &addr
);
161 tl_assert(p
->any
.type
== t
);
162 return clientobj_remove_obj(p
);
166 * Remove the information that was stored about the client object p.
168 * @note The order of operations below is important. The client object is
169 * removed from the client object set after the cleanup function has been
170 * called such that if the cleanup function can still use the function
171 * DRD_(clientobj_get_any)(). This happens e.g. in the function
172 * first_observed() in drd_error.c.
174 static Bool
clientobj_remove_obj(DrdClientobj
* const p
)
178 if (s_trace_clientobj
) {
179 DRD_(trace_msg
)("Removing client object 0x%lx of type %d", p
->any
.a1
,
182 VG_(get_and_pp_StackTrace
)(VG_(get_running_tid
)(),
183 VG_(clo_backtrace_size
));
187 tl_assert(p
->any
.cleanup
);
188 (*p
->any
.cleanup
)(p
);
189 VG_(OSetGen_Remove
)(s_clientobj_set
, &p
->any
.a1
);
190 VG_(OSetGen_FreeNode
)(s_clientobj_set
, p
);
195 * Clean up all client objects p for which their start address p->any.a1 fits
196 * inside the address range [ a1, a2 [.
198 * @note The implementation of this function relies on the fact that the
199 * data in s_clientobj_set is sorted on the start address of client objects.
201 void DRD_(clientobj_stop_using_mem
)(const Addr a1
, const Addr a2
)
206 tl_assert(s_clientobj_set
);
208 if (! DRD_(range_contains_suppression_or_hbvar
)(a1
, a2
))
211 VG_(OSetGen_ResetIterAt
)(s_clientobj_set
, &a1
);
212 for ( ; (p
= VG_(OSetGen_Next
)(s_clientobj_set
)) != 0 && p
->any
.a1
< a2
; )
214 tl_assert(a1
<= p
->any
.a1
);
215 removed_at
= p
->any
.a1
;
216 clientobj_remove_obj(p
);
218 * The above call removes an element from the oset and hence
219 * invalidates the iterator. Restore the iterator.
221 VG_(OSetGen_ResetIterAt
)(s_clientobj_set
, &removed_at
);
226 * Delete the per-thread information stored in client objects for the
229 void DRD_(clientobj_delete_thread
)(const DrdThreadId tid
)
233 VG_(OSetGen_ResetIter
)(s_clientobj_set
);
234 for ( ; (p
= VG_(OSetGen_Next
)(s_clientobj_set
)) != 0; )
236 if (p
->any
.delete_thread
)
238 (*p
->any
.delete_thread
)(p
, tid
);
243 const HChar
* DRD_(clientobj_type_name
)(const ObjType t
)
247 case ClientMutex
: return "mutex";
248 case ClientCondvar
: return "cond";
249 case ClientHbvar
: return "order annotation";
250 case ClientSemaphore
: return "semaphore";
251 case ClientBarrier
: return "barrier";
252 case ClientRwlock
: return "rwlock";