sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
| 4 | Copyright (C) 2006-2008 Bart Van Assche |
| 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 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 26 | #include "drd_clientobj.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 27 | #include "drd_error.h" |
| 28 | #include "drd_semaphore.h" |
| 29 | #include "drd_suppression.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +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 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 37 | // Local functions. |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 38 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 39 | static void semaphore_cleanup(struct semaphore_info* p); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | // Local variables. |
| 43 | |
| 44 | static Bool s_trace_semaphore; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 45 | static ULong s_semaphore_segment_creation_count; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | // Function definitions. |
| 49 | |
| 50 | void semaphore_set_trace(const Bool trace_semaphore) |
| 51 | { |
| 52 | s_trace_semaphore = trace_semaphore; |
| 53 | } |
| 54 | |
| 55 | static |
| 56 | void semaphore_initialize(struct semaphore_info* const p, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 57 | const Addr semaphore, const UWord value) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 58 | { |
| 59 | tl_assert(semaphore != 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 60 | tl_assert(p->a1 == semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 61 | tl_assert(p->type == ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 62 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 63 | p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 64 | p->value = value; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 65 | p->waiters = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 66 | p->last_sem_post_tid = DRD_INVALID_THREADID; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 67 | p->last_sem_post_segment = 0; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 68 | } |
| 69 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 70 | /** Free the memory that was allocated by semaphore_initialize(). Called by |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 71 | * clientobj_remove(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 72 | */ |
| 73 | static void semaphore_cleanup(struct semaphore_info* p) |
| 74 | { |
| 75 | if (p->waiters > 0) |
| 76 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 77 | SemaphoreErrInfo sei = { p->a1 }; |
| 78 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 79 | SemaphoreErr, |
| 80 | VG_(get_IP)(VG_(get_running_tid)()), |
| 81 | "Destruction of semaphore that is being waited" |
| 82 | " upon", |
| 83 | &sei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 84 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 85 | sg_put(p->last_sem_post_segment); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 88 | static |
| 89 | struct semaphore_info* |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 90 | semaphore_get_or_allocate(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 91 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 92 | struct semaphore_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 93 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 94 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 95 | p = &clientobj_get(semaphore, ClientSemaphore)->semaphore; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 96 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 97 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 98 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 99 | p = &clientobj_add(semaphore, ClientSemaphore)->semaphore; |
| 100 | semaphore_initialize(p, semaphore, 0); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 101 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 102 | return p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 103 | } |
| 104 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 105 | static struct semaphore_info* semaphore_get(const Addr semaphore) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 106 | { |
| 107 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 108 | return &clientobj_get(semaphore, ClientSemaphore)->semaphore; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** Called before sem_init(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 112 | struct semaphore_info* semaphore_init(const Addr semaphore, |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 113 | const Word pshared, const UWord value) |
| 114 | { |
| 115 | struct semaphore_info* p; |
| 116 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 117 | if (s_trace_semaphore) |
| 118 | { |
| 119 | VG_(message)(Vg_UserMsg, |
bart | 4a975e1 | 2008-03-30 13:28:33 +0000 | [diff] [blame] | 120 | "[%d/%d] semaphore_init 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 121 | VG_(get_running_tid)(), |
| 122 | thread_get_running_tid(), |
| 123 | semaphore); |
| 124 | } |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 125 | p = semaphore_get(semaphore); |
| 126 | if (p) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 127 | { |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 128 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 129 | SemaphoreErrInfo SEI = { semaphore }; |
| 130 | VG_(maybe_record_error)(vg_tid, |
| 131 | SemaphoreErr, |
| 132 | VG_(get_IP)(vg_tid), |
| 133 | "Semaphore reinitialization", |
| 134 | &SEI); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 135 | } |
bart | d9e39ec | 2008-06-28 15:03:26 +0000 | [diff] [blame] | 136 | else |
| 137 | { |
| 138 | p = semaphore_get_or_allocate(semaphore); |
| 139 | } |
| 140 | tl_assert(p); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 141 | p->value = value; |
| 142 | return p; |
| 143 | } |
| 144 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 145 | /** Called after sem_destroy(). */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 146 | void semaphore_destroy(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 147 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 148 | struct semaphore_info* p; |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 149 | |
| 150 | if (s_trace_semaphore) |
| 151 | { |
| 152 | VG_(message)(Vg_UserMsg, |
bart | 4a975e1 | 2008-03-30 13:28:33 +0000 | [diff] [blame] | 153 | "[%d/%d] semaphore_destroy 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 154 | VG_(get_running_tid)(), |
| 155 | thread_get_running_tid(), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 156 | semaphore); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 159 | p = semaphore_get(semaphore); |
| 160 | |
| 161 | if (p == 0) |
| 162 | { |
| 163 | GenericErrInfo GEI; |
| 164 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 165 | GenericErr, |
| 166 | VG_(get_IP)(VG_(get_running_tid)()), |
| 167 | "Not a semaphore", |
| 168 | &GEI); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | clientobj_remove(semaphore, ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 175 | /** Called before sem_wait(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 176 | void semaphore_pre_wait(const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 177 | { |
| 178 | struct semaphore_info* p; |
| 179 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 180 | p = semaphore_get_or_allocate(semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 181 | if (s_trace_semaphore) |
| 182 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 183 | VG_(message)(Vg_UserMsg, |
bart | 4a975e1 | 2008-03-30 13:28:33 +0000 | [diff] [blame] | 184 | "[%d/%d] semaphore_pre_wait 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 185 | VG_(get_running_tid)(), |
| 186 | thread_get_running_tid(), |
| 187 | semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 188 | } |
| 189 | tl_assert(p); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 190 | tl_assert((int)p->waiters >= 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 191 | p->waiters++; |
| 192 | tl_assert(p->waiters > 0); |
| 193 | } |
| 194 | |
| 195 | /** Called after sem_wait() finished. |
| 196 | * @note Do not rely on the value of 'waited' -- some glibc versions do |
| 197 | * not set it correctly. |
| 198 | */ |
| 199 | void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, |
| 200 | const Bool waited) |
| 201 | { |
| 202 | struct semaphore_info* p; |
| 203 | |
| 204 | p = semaphore_get(semaphore); |
| 205 | if (s_trace_semaphore) |
| 206 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 207 | VG_(message)(Vg_UserMsg, |
| 208 | "[%d/%d] semaphore_post_wait 0x%lx", |
| 209 | VG_(get_running_tid)(), |
| 210 | thread_get_running_tid(), |
| 211 | semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 212 | } |
| 213 | tl_assert(p->waiters > 0); |
| 214 | p->waiters--; |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 215 | tl_assert((int)p->waiters >= 0); |
| 216 | tl_assert((int)p->value >= 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 217 | if (p->value == 0) |
| 218 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 219 | SemaphoreErrInfo sei = { semaphore }; |
| 220 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 221 | SemaphoreErr, |
| 222 | VG_(get_IP)(VG_(get_running_tid)()), |
| 223 | "Invalid semaphore", |
| 224 | &sei); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 225 | return; |
| 226 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 227 | p->value--; |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 228 | tl_assert((int)p->value >= 0); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 229 | if (p->last_sem_post_tid != tid |
| 230 | && p->last_sem_post_tid != DRD_INVALID_THREADID) |
| 231 | { |
| 232 | tl_assert(p->last_sem_post_segment); |
| 233 | thread_combine_vc2(tid, &p->last_sem_post_segment->vc); |
| 234 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 235 | thread_new_segment(tid); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 236 | s_semaphore_segment_creation_count++; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** Called before sem_post(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 240 | void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 241 | { |
| 242 | struct semaphore_info* p; |
| 243 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 244 | if (s_trace_semaphore) |
| 245 | { |
| 246 | VG_(message)(Vg_UserMsg, |
bart | 4a975e1 | 2008-03-30 13:28:33 +0000 | [diff] [blame] | 247 | "[%d/%d] semaphore_post 0x%lx", |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 248 | VG_(get_running_tid)(), |
| 249 | thread_get_running_tid(), |
| 250 | semaphore); |
| 251 | } |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 252 | p = semaphore_get_or_allocate(semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 253 | p->value++; |
| 254 | if (p->value == 1) |
| 255 | { |
| 256 | p->last_sem_post_tid = tid; |
sewardj | c0be925 | 2008-02-11 11:00:51 +0000 | [diff] [blame] | 257 | thread_new_segment(tid); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 258 | thread_get_latest_segment(&p->last_sem_post_segment, tid); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 259 | s_semaphore_segment_creation_count++; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /** Called after sem_post() finished successfully. */ |
| 264 | void semaphore_post_post(const DrdThreadId tid, const Addr semaphore, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 265 | const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 266 | { |
bart | 25896d9 | 2008-02-17 09:21:05 +0000 | [diff] [blame] | 267 | /* Note: it is hard to implement the sem_post() wrapper correctly in */ |
| 268 | /* case sem_post() returns an error code. This is because handling this */ |
| 269 | /* case correctly requires restoring the vector clock associated with */ |
| 270 | /* the semaphore to its original value here. In order to do that without */ |
| 271 | /* introducing a race condition, extra locking has to be added around */ |
| 272 | /* each semaphore call. Such extra locking would have to be added in */ |
| 273 | /* drd_intercepts.c. However, it is hard to implement synchronization */ |
| 274 | /* in drd_intercepts.c in a portable way without calling already */ |
| 275 | /* redirected functions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void semaphore_thread_delete(const DrdThreadId threadid) |
| 279 | { } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 280 | |
| 281 | ULong get_semaphore_segment_creation_count(void) |
| 282 | { |
| 283 | return s_semaphore_segment_creation_count; |
| 284 | } |