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_malloc_wrappers.h" |
| 28 | #include "drd_mutex.h" // struct mutex_info |
| 29 | #include "drd_suppression.h" // drd_start_suppression() |
| 30 | #include "pub_drd_bitmap.h" // LHS_W, ... |
| 31 | #include "pub_tool_vki.h" |
| 32 | #include "pub_tool_basics.h" |
| 33 | #include "pub_tool_libcassert.h" // tl_assert() |
| 34 | #include "pub_tool_libcbase.h" // strlen() |
| 35 | #include "pub_tool_libcfile.h" // VG_(get_startup_wd)() |
| 36 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 37 | #include "pub_tool_machine.h" |
| 38 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 39 | #include "pub_tool_tooliface.h" // VG_(needs_tool_errors)() |
| 40 | |
| 41 | |
| 42 | typedef enum { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 43 | ConflictingAccessSupp |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | } DRD_SuppKind; |
| 45 | |
| 46 | |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 47 | /* Describe a data address range [a,a+len[ as good as possible, for error */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | /* messages, putting the result in ai. */ |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 49 | static |
| 50 | void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 51 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 52 | Addr data; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 54 | if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 55 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 56 | ai->akind = eMallocd; |
| 57 | ai->rwoffset = a - data; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 58 | } |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 59 | else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 60 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 61 | ai->akind = eUnknown; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 62 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 63 | } |
| 64 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | static |
| 66 | void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri) |
| 67 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 68 | AddrInfo ai; |
| 69 | Char descr1[256]; |
| 70 | Char descr2[256]; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 72 | tl_assert(dri); |
| 73 | tl_assert(dri->addr); |
| 74 | tl_assert(dri->size > 0); |
bart | b515eb1 | 2008-03-07 18:52:38 +0000 | [diff] [blame] | 75 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 76 | descr1[0] = 0; |
| 77 | descr2[0] = 0; |
| 78 | VG_(get_data_description)(descr1, descr2, sizeof(descr1), dri->addr); |
| 79 | if (descr1[0] == 0) |
| 80 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 81 | describe_malloced_addr(dri->addr, dri->size, &ai); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 82 | } |
| 83 | VG_(message)(Vg_UserMsg, |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 84 | "Conflicting %s by thread %d/%d at 0x%08lx size %ld", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 85 | dri->access_type == eStore ? "store" : "load", |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 86 | DrdThreadIdToVgThreadId(dri->tid), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 87 | dri->tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 88 | dri->addr, |
| 89 | dri->size); |
| 90 | VG_(pp_ExeContext)(VG_(get_error_where)(err)); |
| 91 | if (descr1[0]) |
| 92 | { |
| 93 | VG_(message)(Vg_UserMsg, "%s", descr1); |
| 94 | VG_(message)(Vg_UserMsg, "%s", descr2); |
| 95 | } |
| 96 | else if (ai.akind == eMallocd && ai.lastchange) |
| 97 | { |
| 98 | VG_(message)(Vg_UserMsg, |
| 99 | "Address 0x%lx is at offset %ld from 0x%lx." |
| 100 | " Allocation context:", |
| 101 | dri->addr, ai.rwoffset, dri->addr - ai.rwoffset); |
| 102 | VG_(pp_ExeContext)(ai.lastchange); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | VG_(message)(Vg_UserMsg, "Allocation context: unknown."); |
| 107 | } |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 108 | thread_report_conflicting_segments(dri->tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 109 | dri->addr, dri->size, dri->access_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2) |
| 113 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 114 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void drd_tool_error_pp(Error* const e) |
| 118 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 119 | switch (VG_(get_error_kind)(e)) |
| 120 | { |
| 121 | case DataRaceErr: { |
| 122 | drd_report_data_race2(e, VG_(get_error_extra)(e)); |
| 123 | break; |
| 124 | } |
| 125 | case MutexErr: { |
| 126 | MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e)); |
| 127 | tl_assert(p); |
| 128 | VG_(message)(Vg_UserMsg, |
| 129 | "%s: mutex 0x%lx, recursion count %d, owner %d.", |
| 130 | VG_(get_error_string)(e), |
| 131 | p->mutex, |
| 132 | p->recursion_count, |
| 133 | p->owner); |
| 134 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 135 | break; |
| 136 | } |
| 137 | case CondErr: { |
| 138 | CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e)); |
| 139 | VG_(message)(Vg_UserMsg, |
| 140 | "%s: cond 0x%lx", |
bart | be8a12c | 2008-03-17 18:36:55 +0000 | [diff] [blame^] | 141 | VG_(get_error_string)(e), |
| 142 | cdei->cond); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 143 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 144 | break; |
| 145 | } |
| 146 | case CondRaceErr: { |
| 147 | CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e)); |
| 148 | VG_(message)(Vg_UserMsg, |
| 149 | "Race condition: condition variable 0x%lx has been" |
| 150 | " signalled but the associated mutex 0x%lx is not locked" |
| 151 | " by the signalling thread", |
| 152 | cei->cond, cei->mutex); |
| 153 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 154 | break; |
| 155 | } |
| 156 | case CondDestrErr: { |
| 157 | CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e)); |
| 158 | VG_(message)(Vg_UserMsg, |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 159 | "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d", |
bart | be8a12c | 2008-03-17 18:36:55 +0000 | [diff] [blame^] | 160 | VG_(get_error_string)(e), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 161 | cdi->cond, cdi->mutex, |
| 162 | DrdThreadIdToVgThreadId(cdi->tid), cdi->tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 163 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 164 | break; |
| 165 | } |
| 166 | case SemaphoreErr: { |
| 167 | SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e)); |
| 168 | tl_assert(sei); |
| 169 | VG_(message)(Vg_UserMsg, |
| 170 | "%s: semaphore 0x%lx", |
| 171 | VG_(get_error_string)(e), |
| 172 | sei->semaphore); |
| 173 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 174 | break; |
| 175 | } |
| 176 | case BarrierErr: { |
| 177 | BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e)); |
| 178 | tl_assert(sei); |
| 179 | VG_(message)(Vg_UserMsg, |
| 180 | "%s: barrier 0x%lx", |
| 181 | VG_(get_error_string)(e), |
| 182 | sei->barrier); |
| 183 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 184 | break; |
| 185 | } |
| 186 | case RwlockErr: { |
| 187 | RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e)); |
| 188 | tl_assert(p); |
| 189 | VG_(message)(Vg_UserMsg, |
| 190 | "%s: rwlock 0x%lx.", |
| 191 | VG_(get_error_string)(e), |
| 192 | p->rwlock); |
| 193 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 194 | break; |
| 195 | } |
| 196 | case GenericErr: { |
| 197 | //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e)); |
| 198 | VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e)); |
| 199 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 200 | break; |
| 201 | } |
| 202 | default: |
| 203 | VG_(message)(Vg_UserMsg, |
| 204 | "%s", |
| 205 | VG_(get_error_string)(e)); |
| 206 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 207 | break; |
| 208 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static UInt drd_tool_error_update_extra(Error* e) |
| 212 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 213 | switch (VG_(get_error_kind)(e)) |
| 214 | { |
| 215 | case DataRaceErr: |
| 216 | return sizeof(DataRaceErrInfo); |
| 217 | case MutexErr: |
| 218 | return sizeof(MutexErrInfo); |
| 219 | case CondErr: |
| 220 | return sizeof(CondErrInfo); |
| 221 | case CondRaceErr: |
| 222 | return sizeof(CondRaceErrInfo); |
| 223 | case CondDestrErr: |
| 224 | return sizeof(CondDestrErrInfo); |
| 225 | case SemaphoreErr: |
| 226 | return sizeof(SemaphoreErrInfo); |
| 227 | case BarrierErr: |
| 228 | return sizeof(BarrierErrInfo); |
| 229 | case RwlockErr: |
| 230 | return sizeof(RwlockErrInfo); |
| 231 | case GenericErr: |
| 232 | return sizeof(GenericErrInfo); |
| 233 | default: |
| 234 | tl_assert(False); |
| 235 | break; |
| 236 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static Bool drd_tool_error_recog(Char* const name, Supp* const supp) |
| 240 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 241 | SuppKind skind; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 242 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 243 | if (VG_(strcmp)(name, "ConflictingAccess") == 0) |
| 244 | skind = ConflictingAccessSupp; |
| 245 | else |
| 246 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 247 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 248 | VG_(set_supp_kind)(supp, skind); |
| 249 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp) |
| 253 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 254 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static Bool drd_tool_error_matches(Error* const e, Supp* const supp) |
| 258 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 259 | switch (VG_(get_supp_kind)(supp)) |
| 260 | { |
| 261 | } |
| 262 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static Char* drd_tool_error_name(Error* e) |
| 266 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 267 | switch (VG_(get_error_kind)(e)) |
| 268 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 269 | case DataRaceErr: return "ConflictingAccess"; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 270 | case MutexErr: return "MutexErr"; |
| 271 | case CondErr: return "CondErr"; |
| 272 | case CondRaceErr: return "CondRaceErr"; |
| 273 | case CondDestrErr: return "CondDestrErr"; |
| 274 | case SemaphoreErr: return "SemaphoreErr"; |
| 275 | case BarrierErr: return "BarrierErr"; |
| 276 | case RwlockErr: return "RwlockErr"; |
| 277 | case GenericErr: return "GenericErr"; |
| 278 | default: |
| 279 | tl_assert(0); |
| 280 | } |
| 281 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void drd_tool_error_print_extra(Error* e) |
| 285 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 286 | switch (VG_(get_error_kind)(e)) |
| 287 | { |
| 288 | // VG_(printf)(" %s\n", VG_(get_error_string)(err)); |
| 289 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void drd_register_error_handlers(void) |
| 293 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 294 | // Tool error reporting. |
| 295 | VG_(needs_tool_errors)(drd_tool_error_eq, |
| 296 | drd_tool_error_pp, |
| 297 | True, |
| 298 | drd_tool_error_update_extra, |
| 299 | drd_tool_error_recog, |
| 300 | drd_tool_error_read_extra, |
| 301 | drd_tool_error_matches, |
| 302 | drd_tool_error_name, |
| 303 | drd_tool_error_print_extra); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 304 | } |