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 |
| 75 | * drd_clientobj_remove(). |
| 76 | */ |
| 77 | static void semaphore_cleanup(struct semaphore_info* p) |
| 78 | { |
| 79 | if (p->waiters > 0) |
| 80 | { |
| 81 | VG_(message)(Vg_UserMsg, "Error: destroying semaphore while %d threads are" |
| 82 | "still waiting on the semaphore.\n", p->waiters); |
| 83 | } |
| 84 | vc_cleanup(&p->vc); |
| 85 | } |
| 86 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 87 | static |
| 88 | struct semaphore_info* |
| 89 | semaphore_get_or_allocate(const Addr semaphore, const SizeT size) |
| 90 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 91 | struct semaphore_info *p; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 92 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 93 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 94 | p = &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore; |
| 95 | if (p == 0) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 96 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 97 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 98 | p = &drd_clientobj_add(semaphore, semaphore + size, |
| 99 | ClientSemaphore)->semaphore; |
| 100 | semaphore_initialize(p, semaphore, size, 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 | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 105 | struct semaphore_info* semaphore_get(const Addr semaphore) |
| 106 | { |
| 107 | tl_assert(offsetof(DrdClientobj, semaphore) == 0); |
| 108 | return &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore; |
| 109 | } |
| 110 | |
| 111 | /** Called before sem_init(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 112 | struct semaphore_info* semaphore_init(const Addr semaphore, const SizeT size, |
| 113 | const Word pshared, const UWord value) |
| 114 | { |
| 115 | struct semaphore_info* p; |
| 116 | |
| 117 | tl_assert(semaphore_get(semaphore) == 0); |
| 118 | p = semaphore_get_or_allocate(semaphore, size); |
| 119 | p->value = value; |
| 120 | return p; |
| 121 | } |
| 122 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 123 | /** Called after sem_destroy(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 124 | void semaphore_destroy(struct semaphore_info* const p) |
| 125 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 126 | drd_clientobj_remove(p->a1, ClientSemaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 129 | /** Called before sem_wait(). */ |
| 130 | void semaphore_pre_wait(const Addr semaphore, const SizeT size) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 131 | { |
| 132 | struct semaphore_info* p; |
| 133 | |
| 134 | p = semaphore_get_or_allocate(semaphore, size); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 135 | if (s_trace_semaphore) |
| 136 | { |
| 137 | VG_(message)(Vg_UserMsg, "semaphore_pre_wait(0x%lx, %d)", semaphore, size); |
| 138 | } |
| 139 | tl_assert(p); |
| 140 | tl_assert(p->waiters >= 0); |
| 141 | p->waiters++; |
| 142 | tl_assert(p->waiters > 0); |
| 143 | } |
| 144 | |
| 145 | /** Called after sem_wait() finished. |
| 146 | * @note Do not rely on the value of 'waited' -- some glibc versions do |
| 147 | * not set it correctly. |
| 148 | */ |
| 149 | void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, |
| 150 | const Bool waited) |
| 151 | { |
| 152 | struct semaphore_info* p; |
| 153 | |
| 154 | p = semaphore_get(semaphore); |
| 155 | if (s_trace_semaphore) |
| 156 | { |
| 157 | VG_(message)(Vg_UserMsg, "semaphore_post_wait(0x%lx, %d)", semaphore); |
| 158 | } |
| 159 | tl_assert(p->waiters > 0); |
| 160 | p->waiters--; |
| 161 | tl_assert(p->waiters >= 0); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 162 | tl_assert(p->value >= 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame^] | 163 | if (p->value == 0) |
| 164 | { |
| 165 | VG_(message)(Vg_UserMsg, "Invalid semaphore 0x%lx", semaphore); |
| 166 | return; |
| 167 | } |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 168 | p->value--; |
| 169 | tl_assert(p->value >= 0); |
| 170 | if (p->last_sem_post_tid != tid) |
| 171 | thread_combine_vc2(tid, &p->vc); |
| 172 | thread_new_segment(tid); |
| 173 | } |
| 174 | |
| 175 | /** Called before sem_post(). */ |
| 176 | void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore, |
| 177 | const SizeT size) |
| 178 | { |
| 179 | struct semaphore_info* p; |
| 180 | |
| 181 | p = semaphore_get_or_allocate(semaphore, size); |
| 182 | p->value++; |
| 183 | if (p->value == 1) |
| 184 | { |
| 185 | p->last_sem_post_tid = tid; |
sewardj | c0be925 | 2008-02-11 11:00:51 +0000 | [diff] [blame] | 186 | thread_new_segment(tid); |
bart | 9cdaf1e | 2008-02-24 18:29:43 +0000 | [diff] [blame] | 187 | vc_assign(&p->vc, thread_get_vc(tid)); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | /** Called after sem_post() finished successfully. */ |
| 192 | void semaphore_post_post(const DrdThreadId tid, const Addr semaphore, |
sewardj | e3b57aa | 2008-01-18 07:42:01 +0000 | [diff] [blame] | 193 | const SizeT size, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 194 | { |
bart | 25896d9 | 2008-02-17 09:21:05 +0000 | [diff] [blame] | 195 | /* Note: it is hard to implement the sem_post() wrapper correctly in */ |
| 196 | /* case sem_post() returns an error code. This is because handling this */ |
| 197 | /* case correctly requires restoring the vector clock associated with */ |
| 198 | /* the semaphore to its original value here. In order to do that without */ |
| 199 | /* introducing a race condition, extra locking has to be added around */ |
| 200 | /* each semaphore call. Such extra locking would have to be added in */ |
| 201 | /* drd_intercepts.c. However, it is hard to implement synchronization */ |
| 202 | /* in drd_intercepts.c in a portable way without calling already */ |
| 203 | /* redirected functions. */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void semaphore_thread_delete(const DrdThreadId threadid) |
| 207 | { } |