sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | 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_error.h" |
| 27 | #include "drd_mutex.h" |
| 28 | #include "drd_suppression.h" |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 29 | #include "priv_drd_clientreq.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 30 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 31 | #include "pub_tool_libcassert.h" // tl_assert() |
| 32 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 33 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 34 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 35 | |
| 36 | |
| 37 | // Type definitions. |
| 38 | |
| 39 | struct mutex_info |
| 40 | { |
| 41 | Addr mutex; // Pointer to client mutex. |
| 42 | SizeT size; // Size in bytes of client-side object. |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 43 | MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | int recursion_count; // 0 if free, >= 1 if locked. |
| 45 | DrdThreadId owner; // owner if locked, last owner if free. |
| 46 | VectorClock vc; // vector clock associated with last unlock. |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | // Local variables. |
| 51 | |
| 52 | static Bool s_trace_mutex; |
| 53 | static ULong s_mutex_lock_count; |
| 54 | struct mutex_info s_mutex[256]; |
| 55 | |
| 56 | |
| 57 | // Function definitions. |
| 58 | |
| 59 | void mutex_set_trace(const Bool trace_mutex) |
| 60 | { |
| 61 | tl_assert(!! trace_mutex == trace_mutex); |
| 62 | s_trace_mutex = trace_mutex; |
| 63 | } |
| 64 | |
| 65 | static |
| 66 | void mutex_initialize(struct mutex_info* const p, |
| 67 | const Addr mutex, |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 68 | const SizeT size, |
| 69 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 70 | { |
| 71 | tl_assert(mutex != 0); |
| 72 | tl_assert(size > 0); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 73 | tl_assert(mutex_type == mutex_type_mutex |
| 74 | || mutex_type == mutex_type_spinlock); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | |
| 76 | p->mutex = mutex; |
| 77 | p->size = size; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 78 | p->mutex_type = mutex_type; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | p->recursion_count = 0; |
| 80 | p->owner = DRD_INVALID_THREADID; |
| 81 | vc_init(&p->vc, 0, 0); |
| 82 | } |
| 83 | |
| 84 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 85 | struct mutex_info* |
| 86 | mutex_get_or_allocate(const Addr mutex, |
| 87 | const SizeT size, |
| 88 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | { |
| 90 | int i; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 91 | |
| 92 | tl_assert(mutex_type == mutex_type_mutex |
| 93 | || mutex_type == mutex_type_spinlock); |
| 94 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 95 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 96 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 97 | if (s_mutex[i].mutex == mutex) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 98 | { |
| 99 | tl_assert(s_mutex[i].mutex_type == mutex_type); |
| 100 | tl_assert(s_mutex[i].size == size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 101 | return &s_mutex[i]; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 104 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 105 | { |
| 106 | if (s_mutex[i].mutex == 0) |
| 107 | { |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 108 | mutex_initialize(&s_mutex[i], mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 109 | drd_start_suppression(mutex, mutex + size, |
| 110 | mutex_get_typename(&s_mutex[i])); |
| 111 | return &s_mutex[i]; |
| 112 | } |
| 113 | } |
| 114 | tl_assert(0); |
| 115 | return 0; |
| 116 | } |
| 117 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 118 | struct mutex_info* |
| 119 | mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 120 | { |
| 121 | struct mutex_info* mutex_p; |
| 122 | |
| 123 | tl_assert(mutex_get(mutex) == 0); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 124 | tl_assert(mutex_type == mutex_type_mutex |
| 125 | || mutex_type == mutex_type_spinlock); |
| 126 | mutex_p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
| 128 | if (s_trace_mutex) |
| 129 | { |
| 130 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 131 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 132 | VG_(message)(Vg_DebugMsg, |
| 133 | "drd_post_mutex_init tid = %d/%d, %s 0x%lx", |
| 134 | vg_tid, drd_tid, |
| 135 | mutex_get_typename(mutex_p), |
| 136 | mutex); |
| 137 | } |
| 138 | |
| 139 | return mutex_p; |
| 140 | } |
| 141 | |
| 142 | void mutex_destroy(struct mutex_info* const p) |
| 143 | { |
| 144 | if (s_trace_mutex) |
| 145 | { |
| 146 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 147 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 148 | VG_(message)(Vg_DebugMsg, |
| 149 | "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx", |
| 150 | vg_tid, drd_tid, |
| 151 | mutex_get_typename(p), |
| 152 | p->mutex); |
| 153 | } |
| 154 | |
| 155 | drd_finish_suppression(p->mutex, p->mutex + p->size); |
| 156 | |
| 157 | vc_cleanup(&p->vc); |
| 158 | p->mutex = 0; |
| 159 | } |
| 160 | |
| 161 | struct mutex_info* mutex_get(const Addr mutex) |
| 162 | { |
| 163 | int i; |
| 164 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 165 | if (s_mutex[i].mutex == mutex) |
| 166 | return &s_mutex[i]; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 172 | * Note: this function must be called after pthread_mutex_lock() has been |
| 173 | * called, or a race condition is triggered ! |
| 174 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 175 | int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 176 | { |
| 177 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 178 | struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 179 | const DrdThreadId last_owner = p->owner; |
| 180 | |
| 181 | if (s_trace_mutex) |
| 182 | { |
| 183 | const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid); |
| 184 | VG_(message)(Vg_DebugMsg, |
| 185 | "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d", |
| 186 | tid, |
| 187 | drd_tid, |
| 188 | mutex_get_typename(p), |
| 189 | mutex, |
| 190 | p ? p->recursion_count : 0, |
| 191 | p ? p->owner : VG_INVALID_THREADID); |
| 192 | } |
| 193 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 194 | tl_assert(mutex_type == mutex_type_mutex |
| 195 | || mutex_type == mutex_type_spinlock); |
| 196 | tl_assert(p->mutex_type == mutex_type); |
| 197 | tl_assert(p->size == size); |
| 198 | |
| 199 | if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 200 | { |
| 201 | // TO DO: tell the user in a more friendly way that it is not allowed to |
| 202 | // lock spinlocks recursively. |
| 203 | tl_assert(0); |
| 204 | } |
| 205 | |
| 206 | if (p->recursion_count == 0) |
| 207 | { |
| 208 | p->owner = drd_tid; |
| 209 | s_mutex_lock_count++; |
| 210 | } |
| 211 | else if (p->owner != drd_tid) |
| 212 | { |
| 213 | VG_(message)(Vg_DebugMsg, |
| 214 | "The impossible happened: mutex 0x%lx is locked" |
| 215 | " simultaneously by two threads (recursion count %d," |
| 216 | " owners %d and %d) !", |
| 217 | p->mutex, p->recursion_count, p->owner, drd_tid); |
| 218 | tl_assert(0); |
| 219 | } |
| 220 | p->recursion_count++; |
| 221 | |
| 222 | if (p->recursion_count == 1) |
| 223 | { |
| 224 | if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID) |
| 225 | thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex)); |
| 226 | thread_new_segment(drd_tid); |
| 227 | } |
| 228 | |
| 229 | return p->recursion_count; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Update mutex_info state when unlocking the pthread_mutex_t mutex. |
| 234 | * Note: this function must be called before pthread_mutex_unlock() is called, |
| 235 | * or a race condition is triggered ! |
| 236 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 237 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 238 | * @param vc Pointer to the current vector clock of thread tid. |
| 239 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 240 | int mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 241 | { |
| 242 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
| 243 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid); |
| 244 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 245 | struct mutex_info* const p = mutex_get(mutex); |
| 246 | |
| 247 | if (s_trace_mutex) |
| 248 | { |
| 249 | VG_(message)(Vg_DebugMsg, |
| 250 | "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d", |
| 251 | vg_tid, drd_tid, |
| 252 | mutex_get_typename(p), |
| 253 | mutex, |
| 254 | p->recursion_count, |
| 255 | p->owner); |
| 256 | } |
| 257 | |
| 258 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 259 | tl_assert(p->mutex_type == mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 260 | tl_assert(p->owner != DRD_INVALID_THREADID); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 261 | tl_assert(mutex_type == mutex_type_mutex |
| 262 | || mutex_type == mutex_type_spinlock); |
| 263 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 264 | if (p->owner != drd_tid) |
| 265 | { |
| 266 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 267 | VG_(maybe_record_error)(vg_tid, |
| 268 | MutexErr, |
| 269 | VG_(get_IP)(vg_tid), |
| 270 | "Mutex not unlocked by owner thread", |
| 271 | &MEI); |
| 272 | } |
| 273 | p->recursion_count--; |
| 274 | tl_assert(p->recursion_count >= 0); |
| 275 | if (p->recursion_count == 0) |
| 276 | { |
| 277 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 278 | /* current vector clock of the thread such that it is available when */ |
| 279 | /* this mutex is locked again. */ |
| 280 | vc_copy(&p->vc, vc); |
| 281 | |
| 282 | thread_new_segment(drd_tid); |
| 283 | } |
| 284 | return p->recursion_count; |
| 285 | } |
| 286 | |
| 287 | const char* mutex_get_typename(struct mutex_info* const p) |
| 288 | { |
| 289 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 290 | |
| 291 | switch (p->mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 292 | { |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 293 | case mutex_type_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 294 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 295 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 296 | return "spinlock"; |
| 297 | default: |
| 298 | tl_assert(0); |
| 299 | } |
| 300 | return "?"; |
| 301 | } |
| 302 | |
| 303 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 304 | { |
| 305 | struct mutex_info* const p = mutex_get(mutex); |
| 306 | tl_assert(p); |
| 307 | if (p) |
| 308 | { |
| 309 | return (p->recursion_count > 0 && p->owner == tid); |
| 310 | } |
| 311 | return False; |
| 312 | } |
| 313 | |
| 314 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 315 | { |
| 316 | struct mutex_info* const p = mutex_get(mutex); |
| 317 | return p ? &p->vc : 0; |
| 318 | } |
| 319 | |
| 320 | int mutex_get_recursion_count(const Addr mutex) |
| 321 | { |
| 322 | struct mutex_info* const p = mutex_get(mutex); |
| 323 | tl_assert(p); |
| 324 | return p->recursion_count; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Call this function when thread threadid stops to exist, such that the |
| 329 | * "last owner" field can be cleared if it still refers to that thread. |
| 330 | * TO DO: print an error message if a thread exits while it still has some |
| 331 | * mutexes locked. |
| 332 | */ |
| 333 | void mutex_thread_delete(const DrdThreadId threadid) |
| 334 | { |
| 335 | int i; |
| 336 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 337 | { |
| 338 | struct mutex_info* const p = &s_mutex[i]; |
| 339 | if (p->mutex && p->owner == threadid) |
| 340 | { |
| 341 | p->owner = VG_INVALID_THREADID; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void mutex_stop_using_mem(const Addr a1, const Addr a2) |
| 347 | { |
| 348 | unsigned i; |
| 349 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 350 | { |
| 351 | if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2) |
| 352 | { |
| 353 | tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2); |
| 354 | mutex_destroy(&s_mutex[i]); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | ULong get_mutex_lock_count(void) |
| 360 | { |
| 361 | return s_mutex_lock_count; |
| 362 | } |