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