bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
| 2 | /* |
| 3 | This file is part of drd, a thread error detector. |
| 4 | |
| 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_clientobj.h" |
| 27 | #include "drd_hb.h" |
| 28 | #include "drd_error.h" |
| 29 | #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */ |
| 30 | #include "pub_tool_libcassert.h" /* tl_assert() */ |
| 31 | #include "pub_tool_libcprint.h" /* VG_(printf)() */ |
| 32 | #include "pub_tool_machine.h" /* VG_(get_IP)() */ |
| 33 | #include "pub_tool_mallocfree.h" /* VG_(malloc)(), VG_(free)()*/ |
| 34 | #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */ |
| 35 | |
| 36 | |
| 37 | /* Type definitions. */ |
| 38 | |
| 39 | /** Per-thread hb information. */ |
| 40 | struct hb_thread_info |
| 41 | { |
| 42 | UWord tid; // A DrdThreadId declared as UWord because |
| 43 | // this member variable is the key of an OSet. |
| 44 | Segment* sg; // Segment created before most recent |
| 45 | // ANNOTATE_HAPPENS_BEFORE(). |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | /* Local functions. */ |
| 50 | |
| 51 | static void DRD_(hb_cleanup)(struct hb_info* p); |
| 52 | |
| 53 | |
| 54 | /* Local variables. */ |
| 55 | |
| 56 | static Bool DRD_(s_trace_hb); |
| 57 | |
| 58 | |
| 59 | /* Function definitions. */ |
| 60 | |
| 61 | void DRD_(hb_set_trace)(const Bool trace_hb) |
| 62 | { |
| 63 | DRD_(s_trace_hb) = trace_hb; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Initialize the structure *p with the specified thread ID. |
| 68 | */ |
| 69 | static |
| 70 | void DRD_(hb_thread_initialize)(struct hb_thread_info* const p, |
| 71 | const DrdThreadId tid) |
| 72 | { |
| 73 | p->tid = tid; |
| 74 | p->sg = 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Deallocate the memory that is owned by members of struct hb_thread_info. |
| 79 | */ |
| 80 | static void DRD_(hb_thread_destroy)(struct hb_thread_info* const p) |
| 81 | { |
| 82 | tl_assert(p); |
| 83 | DRD_(sg_put)(p->sg); |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | void DRD_(hb_initialize)(struct hb_info* const p, const Addr hb) |
| 88 | { |
| 89 | tl_assert(hb != 0); |
| 90 | tl_assert(p->a1 == hb); |
| 91 | tl_assert(p->type == ClientHbvar); |
| 92 | |
| 93 | p->cleanup = (void(*)(DrdClientobj*))(DRD_(hb_cleanup)); |
| 94 | p->delete_thread = 0; |
| 95 | p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.hb", |
| 96 | VG_(free)); |
| 97 | p->done = False; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Free the memory that was allocated by hb_initialize(). Called by |
| 102 | * DRD_(clientobj_remove)(). |
| 103 | */ |
| 104 | static void DRD_(hb_cleanup)(struct hb_info* p) |
| 105 | { |
| 106 | struct hb_thread_info* r; |
| 107 | |
| 108 | tl_assert(p); |
| 109 | VG_(OSetGen_ResetIter)(p->oset); |
| 110 | for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 111 | DRD_(hb_thread_destroy)(r); |
| 112 | VG_(OSetGen_Destroy)(p->oset); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Report that the synchronization object at address 'addr' is of the |
| 117 | * wrong type. |
| 118 | */ |
| 119 | static void wrong_type(const Addr addr) |
| 120 | { |
| 121 | GenericErrInfo gei = { |
| 122 | .tid = DRD_(thread_get_running_tid)(), |
| 123 | .addr = addr, |
| 124 | }; |
| 125 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 126 | GenericErr, |
| 127 | VG_(get_IP)(VG_(get_running_tid)()), |
| 128 | "wrong type of synchronization object", |
| 129 | &gei); |
| 130 | } |
| 131 | |
| 132 | struct hb_info* DRD_(hb_get_or_allocate)(const Addr hb) |
| 133 | { |
| 134 | struct hb_info *p; |
| 135 | |
| 136 | tl_assert(offsetof(DrdClientobj, hb) == 0); |
| 137 | p = &(DRD_(clientobj_get)(hb, ClientHbvar)->hb); |
| 138 | if (p) |
| 139 | return p; |
| 140 | |
| 141 | if (DRD_(clientobj_present)(hb, hb + 1)) |
| 142 | { |
| 143 | wrong_type(hb); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | p = &(DRD_(clientobj_add)(hb, ClientHbvar)->hb); |
| 148 | DRD_(hb_initialize)(p, hb); |
| 149 | return p; |
| 150 | } |
| 151 | |
| 152 | struct hb_info* DRD_(hb_get)(const Addr hb) |
| 153 | { |
| 154 | tl_assert(offsetof(DrdClientobj, hb) == 0); |
| 155 | return &(DRD_(clientobj_get)(hb, ClientHbvar)->hb); |
| 156 | } |
| 157 | |
| 158 | /** Called because of a happens-before annotation. */ |
| 159 | void DRD_(hb_happens_before)(const DrdThreadId tid, Addr const hb) |
| 160 | { |
| 161 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 162 | const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
| 163 | const UWord word_tid = tid; |
| 164 | struct hb_info* p; |
| 165 | struct hb_thread_info* q; |
| 166 | |
| 167 | p = DRD_(hb_get_or_allocate)(hb); |
| 168 | if (DRD_(s_trace_hb)) |
| 169 | { |
| 170 | VG_(message)(Vg_UserMsg, |
| 171 | "[%d] happens_before 0x%lx\n", |
| 172 | DRD_(thread_get_running_tid)(), |
| 173 | hb); |
| 174 | } |
| 175 | |
| 176 | if (!p) |
| 177 | return; |
| 178 | |
| 179 | if (p->done) |
| 180 | { |
| 181 | GenericErrInfo gei = { |
| 182 | .tid = DRD_(thread_get_running_tid)(), |
| 183 | .addr = hb, |
| 184 | }; |
| 185 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 186 | GenericErr, |
| 187 | VG_(get_IP)(VG_(get_running_tid)()), |
| 188 | "happens-before after happens-after", |
| 189 | &gei); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | /* Allocate the per-thread data structure if necessary. */ |
| 194 | q = VG_(OSetGen_Lookup)(p->oset, &word_tid); |
| 195 | if (!q) |
| 196 | { |
| 197 | q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q)); |
| 198 | DRD_(hb_thread_initialize)(q, tid); |
| 199 | VG_(OSetGen_Insert)(p->oset, q); |
| 200 | tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Store a pointer to the latest segment of the current thread in the |
| 205 | * per-thread data structure. |
| 206 | */ |
| 207 | DRD_(thread_get_latest_segment)(&q->sg, tid); |
| 208 | DRD_(thread_new_segment)(drd_tid); |
| 209 | } |
| 210 | |
| 211 | /** Called because of a happens-after annotation. */ |
| 212 | void DRD_(hb_happens_after)(const DrdThreadId tid, const Addr hb) |
| 213 | { |
| 214 | struct hb_info* p; |
| 215 | struct hb_thread_info* q; |
| 216 | VectorClock old_vc; |
| 217 | |
| 218 | p = DRD_(hb_get)(hb); |
| 219 | |
| 220 | if (DRD_(s_trace_hb)) |
| 221 | { |
| 222 | VG_(message)(Vg_UserMsg, "[%d] happens_after 0x%lx\n", |
| 223 | DRD_(thread_get_running_tid)(), hb); |
| 224 | } |
| 225 | |
| 226 | if (!p) |
| 227 | { |
| 228 | GenericErrInfo gei = { |
| 229 | .tid = DRD_(thread_get_running_tid)(), |
| 230 | .addr = hb, |
| 231 | }; |
| 232 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 233 | GenericErr, |
| 234 | VG_(get_IP)(VG_(get_running_tid)()), |
| 235 | "missing happens-before annotation", |
| 236 | &gei); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | p->done = True; |
| 241 | |
bart | 9b41809 | 2010-03-07 18:39:33 +0000 | [diff] [blame^] | 242 | DRD_(thread_new_segment)(tid); |
| 243 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 244 | /* |
| 245 | * Combine all vector clocks that were stored because of happens-before |
| 246 | * annotations with the vector clock of the current thread. |
| 247 | */ |
| 248 | DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc); |
| 249 | VG_(OSetGen_ResetIter)(p->oset); |
| 250 | for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; ) |
| 251 | { |
| 252 | if (q->tid != tid) |
| 253 | { |
| 254 | tl_assert(q->sg); |
| 255 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, &q->sg->vc); |
| 256 | } |
| 257 | } |
| 258 | DRD_(thread_update_conflict_set)(tid, &old_vc); |
| 259 | DRD_(vc_cleanup)(&old_vc); |
| 260 | } |
| 261 | |
| 262 | /** Called because of a happens-done annotation. */ |
| 263 | void DRD_(hb_happens_done)(const DrdThreadId tid, const Addr hb) |
| 264 | { |
| 265 | struct hb_info* p; |
| 266 | |
| 267 | if (DRD_(s_trace_hb)) |
| 268 | { |
| 269 | VG_(message)(Vg_UserMsg, "[%d] happens_done 0x%lx\n", |
| 270 | DRD_(thread_get_running_tid)(), hb); |
| 271 | } |
| 272 | |
| 273 | p = DRD_(hb_get)(hb); |
| 274 | if (!p) |
| 275 | { |
| 276 | GenericErrInfo gei = { |
| 277 | .tid = DRD_(thread_get_running_tid)(), |
| 278 | .addr = hb, |
| 279 | }; |
| 280 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 281 | GenericErr, |
| 282 | VG_(get_IP)(VG_(get_running_tid)()), |
| 283 | "missing happens-before annotation", |
| 284 | &gei); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | DRD_(clientobj_remove)(p->a1, ClientHbvar); |
| 289 | } |