bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 4 | |
| 5 | Copyright (C) 2006-2009 Bart Van Assche <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_bitmap.h" |
| 27 | #include "drd_thread_bitmap.h" |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 28 | #include "drd_vc.h" /* DRD_(vc_snprint)() */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 29 | |
| 30 | /* Include several source files here in order to allow the compiler to */ |
| 31 | /* do more inlining. */ |
| 32 | #include "drd_bitmap.c" |
| 33 | #include "drd_load_store.h" |
| 34 | #include "drd_segment.c" |
| 35 | #include "drd_thread.c" |
| 36 | #include "drd_vc.c" |
| 37 | #include "libvex_guest_offsets.h" |
| 38 | |
| 39 | |
| 40 | /* STACK_POINTER_OFFSET: VEX register offset for the stack pointer register. */ |
| 41 | #if defined(VGA_x86) |
| 42 | #define STACK_POINTER_OFFSET OFFSET_x86_ESP |
| 43 | #elif defined(VGA_amd64) |
| 44 | #define STACK_POINTER_OFFSET OFFSET_amd64_RSP |
| 45 | #elif defined(VGA_ppc32) |
| 46 | #define STACK_POINTER_OFFSET ((OFFSET_ppc32_GPR0 + OFFSET_ppc32_GPR2) / 2) |
| 47 | #elif defined(VGA_ppc64) |
| 48 | #define STACK_POINTER_OFFSET ((OFFSET_ppc64_GPR0 + OFFSET_ppc64_GPR2) / 2) |
| 49 | #else |
| 50 | #error Unknown architecture. |
| 51 | #endif |
| 52 | |
| 53 | |
| 54 | /* Local variables. */ |
| 55 | |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 56 | static Bool s_check_stack_accesses = False; |
| 57 | static Bool s_first_race_only = False; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | /* Function definitions. */ |
| 61 | |
| 62 | Bool DRD_(get_check_stack_accesses)() |
| 63 | { |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 64 | return s_check_stack_accesses; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void DRD_(set_check_stack_accesses)(const Bool c) |
| 68 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 69 | tl_assert(c == False || c == True); |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 70 | s_check_stack_accesses = c; |
| 71 | } |
| 72 | |
| 73 | Bool DRD_(get_first_race_only)() |
| 74 | { |
| 75 | return s_first_race_only; |
| 76 | } |
| 77 | |
| 78 | void DRD_(set_first_race_only)(const Bool fro) |
| 79 | { |
| 80 | tl_assert(fro == False || fro == True); |
| 81 | s_first_race_only = fro; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 84 | void DRD_(trace_mem_access)(const Addr addr, const SizeT size, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 85 | const BmAccessTypeT access_type) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 86 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 87 | if (DRD_(is_any_traced)(addr, addr + size)) |
| 88 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 89 | char* vc; |
| 90 | |
| 91 | vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(DRD_(thread_get_running_tid)())); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 92 | VG_(message)(Vg_UserMsg, |
| 93 | "%s 0x%lx size %ld (vg %d / drd %d / vc %s)", |
| 94 | access_type == eLoad |
| 95 | ? "load " |
| 96 | : access_type == eStore |
| 97 | ? "store" |
| 98 | : access_type == eStart |
| 99 | ? "start" |
| 100 | : access_type == eEnd |
| 101 | ? "end " |
| 102 | : "????", |
| 103 | addr, |
| 104 | size, |
| 105 | VG_(get_running_tid)(), |
| 106 | DRD_(thread_get_running_tid)(), |
| 107 | vc); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 108 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 109 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 110 | VG_(clo_backtrace_size)); |
| 111 | tl_assert(DRD_(DrdThreadIdToVgThreadId)(DRD_(thread_get_running_tid)()) |
| 112 | == VG_(get_running_tid)()); |
| 113 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size) |
| 117 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 118 | return DRD_(trace_mem_access)(addr, size, eLoad); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static VG_REGPARM(2) void drd_trace_mem_store(const Addr addr,const SizeT size) |
| 122 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 123 | return DRD_(trace_mem_access)(addr, size, eStore); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void drd_report_race(const Addr addr, const SizeT size, |
| 127 | const BmAccessTypeT access_type) |
| 128 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 129 | DataRaceErrInfo drei; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 130 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 131 | drei.tid = DRD_(thread_get_running_tid)(); |
| 132 | drei.addr = addr; |
| 133 | drei.size = size; |
| 134 | drei.access_type = access_type; |
| 135 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 136 | DataRaceErr, |
| 137 | VG_(get_IP)(VG_(get_running_tid)()), |
| 138 | "Conflicting accesses", |
| 139 | &drei); |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 140 | |
| 141 | if (s_first_race_only) |
| 142 | { |
| 143 | DRD_(start_suppression)(addr, addr + size, "first race only"); |
| 144 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 145 | } |
| 146 | |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 147 | VG_REGPARM(2) void DRD_(trace_load)(Addr addr, SizeT size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 148 | { |
| 149 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 150 | /* The assert below has been commented out because of performance reasons.*/ |
| 151 | tl_assert(thread_get_running_tid() |
| 152 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 155 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 156 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 157 | || ! DRD_(thread_address_on_stack)(addr)) |
| 158 | && bm_access_load_triggers_conflict(addr, addr + size) |
| 159 | && ! DRD_(is_suppressed)(addr, addr + size)) |
| 160 | { |
| 161 | drd_report_race(addr, size, eLoad); |
| 162 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static VG_REGPARM(1) void drd_trace_load_1(Addr addr) |
| 166 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 167 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 168 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 169 | || ! DRD_(thread_address_on_stack)(addr)) |
| 170 | && bm_access_load_1_triggers_conflict(addr) |
| 171 | && ! DRD_(is_suppressed)(addr, addr + 1)) |
| 172 | { |
| 173 | drd_report_race(addr, 1, eLoad); |
| 174 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static VG_REGPARM(1) void drd_trace_load_2(Addr addr) |
| 178 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 179 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 180 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 181 | || ! DRD_(thread_address_on_stack)(addr)) |
| 182 | && bm_access_load_2_triggers_conflict(addr) |
| 183 | && ! DRD_(is_suppressed)(addr, addr + 2)) |
| 184 | { |
| 185 | drd_report_race(addr, 2, eLoad); |
| 186 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static VG_REGPARM(1) void drd_trace_load_4(Addr addr) |
| 190 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 191 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 192 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 193 | || ! DRD_(thread_address_on_stack)(addr)) |
| 194 | && bm_access_load_4_triggers_conflict(addr) |
| 195 | && ! DRD_(is_suppressed)(addr, addr + 4)) |
| 196 | { |
| 197 | drd_report_race(addr, 4, eLoad); |
| 198 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static VG_REGPARM(1) void drd_trace_load_8(Addr addr) |
| 202 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 203 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 204 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 205 | || ! DRD_(thread_address_on_stack)(addr)) |
| 206 | && bm_access_load_8_triggers_conflict(addr) |
| 207 | && ! DRD_(is_suppressed)(addr, addr + 8)) |
| 208 | { |
| 209 | drd_report_race(addr, 8, eLoad); |
| 210 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 211 | } |
| 212 | |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 213 | VG_REGPARM(2) void DRD_(trace_store)(Addr addr, SizeT size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 214 | { |
| 215 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 216 | /* The assert below has been commented out because of performance reasons.*/ |
| 217 | tl_assert(thread_get_running_tid() |
| 218 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 221 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 222 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 223 | || ! DRD_(thread_address_on_stack)(addr)) |
| 224 | && bm_access_store_triggers_conflict(addr, addr + size) |
| 225 | && ! DRD_(is_suppressed)(addr, addr + size)) |
| 226 | { |
| 227 | drd_report_race(addr, size, eStore); |
| 228 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static VG_REGPARM(1) void drd_trace_store_1(Addr addr) |
| 232 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 233 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 234 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 235 | || ! DRD_(thread_address_on_stack)(addr)) |
| 236 | && bm_access_store_1_triggers_conflict(addr) |
| 237 | && ! DRD_(is_suppressed)(addr, addr + 1)) |
| 238 | { |
| 239 | drd_report_race(addr, 1, eStore); |
| 240 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static VG_REGPARM(1) void drd_trace_store_2(Addr addr) |
| 244 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 245 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 246 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 247 | || ! DRD_(thread_address_on_stack)(addr)) |
| 248 | && bm_access_store_2_triggers_conflict(addr) |
| 249 | && ! DRD_(is_suppressed)(addr, addr + 2)) |
| 250 | { |
| 251 | drd_report_race(addr, 2, eStore); |
| 252 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static VG_REGPARM(1) void drd_trace_store_4(Addr addr) |
| 256 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 257 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 258 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 259 | || ! DRD_(thread_address_on_stack)(addr)) |
| 260 | && bm_access_store_4_triggers_conflict(addr) |
| 261 | && ! DRD_(is_suppressed)(addr, addr + 4)) |
| 262 | { |
| 263 | drd_report_race(addr, 4, eStore); |
| 264 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static VG_REGPARM(1) void drd_trace_store_8(Addr addr) |
| 268 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 269 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 270 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 271 | || ! DRD_(thread_address_on_stack)(addr)) |
| 272 | && bm_access_store_8_triggers_conflict(addr) |
| 273 | && ! DRD_(is_suppressed)(addr, addr + 8)) |
| 274 | { |
| 275 | drd_report_race(addr, 8, eStore); |
| 276 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Return true if and only if addr_expr matches the pattern (SP) or |
| 281 | * <offset>(SP). |
| 282 | */ |
| 283 | static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr) |
| 284 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 285 | Bool result = False; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 286 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 287 | if (addr_expr->tag == Iex_RdTmp) |
| 288 | { |
| 289 | int i; |
| 290 | for (i = 0; i < bb->stmts_size; i++) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 291 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 292 | if (bb->stmts[i] |
| 293 | && bb->stmts[i]->tag == Ist_WrTmp |
| 294 | && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp) |
| 295 | { |
| 296 | IRExpr* e = bb->stmts[i]->Ist.WrTmp.data; |
| 297 | if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET) |
| 298 | { |
| 299 | result = True; |
| 300 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 301 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 302 | //ppIRExpr(e); |
| 303 | //VG_(printf)(" (%s)\n", result ? "True" : "False"); |
| 304 | break; |
| 305 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 306 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 307 | } |
| 308 | return result; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void instrument_load(IRSB* const bb, |
| 312 | IRExpr* const addr_expr, |
| 313 | const HWord size) |
| 314 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 315 | IRExpr* size_expr; |
| 316 | IRExpr** argv; |
| 317 | IRDirty* di; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 318 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 319 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
| 320 | { |
| 321 | addStmtToIRSB(bb, |
| 322 | IRStmt_Dirty( |
| 323 | unsafeIRDirty_0_N(/*regparms*/2, |
| 324 | "drd_trace_load", |
| 325 | VG_(fnptr_to_fnentry) |
| 326 | (drd_trace_mem_load), |
| 327 | mkIRExprVec_2(addr_expr, |
| 328 | mkIRExpr_HWord(size))))); |
| 329 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 330 | |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 331 | if (! s_check_stack_accesses && is_stack_access(bb, addr_expr)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 332 | return; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 333 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 334 | switch (size) |
| 335 | { |
| 336 | case 1: |
| 337 | argv = mkIRExprVec_1(addr_expr); |
| 338 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 339 | "drd_trace_load_1", |
| 340 | VG_(fnptr_to_fnentry)(drd_trace_load_1), |
| 341 | argv); |
| 342 | break; |
| 343 | case 2: |
| 344 | argv = mkIRExprVec_1(addr_expr); |
| 345 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 346 | "drd_trace_load_2", |
| 347 | VG_(fnptr_to_fnentry)(drd_trace_load_2), |
| 348 | argv); |
| 349 | break; |
| 350 | case 4: |
| 351 | argv = mkIRExprVec_1(addr_expr); |
| 352 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 353 | "drd_trace_load_4", |
| 354 | VG_(fnptr_to_fnentry)(drd_trace_load_4), |
| 355 | argv); |
| 356 | break; |
| 357 | case 8: |
| 358 | argv = mkIRExprVec_1(addr_expr); |
| 359 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 360 | "drd_trace_load_8", |
| 361 | VG_(fnptr_to_fnentry)(drd_trace_load_8), |
| 362 | argv); |
| 363 | break; |
| 364 | default: |
| 365 | size_expr = mkIRExpr_HWord(size); |
| 366 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 367 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 368 | "drd_trace_load", |
| 369 | VG_(fnptr_to_fnentry)(DRD_(trace_load)), |
| 370 | argv); |
| 371 | break; |
| 372 | } |
| 373 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void instrument_store(IRSB* const bb, |
| 377 | IRExpr* const addr_expr, |
| 378 | const HWord size) |
| 379 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 380 | IRExpr* size_expr; |
| 381 | IRExpr** argv; |
| 382 | IRDirty* di; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 383 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 384 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
| 385 | { |
| 386 | addStmtToIRSB(bb, |
| 387 | IRStmt_Dirty( |
| 388 | unsafeIRDirty_0_N(/*regparms*/2, |
| 389 | "drd_trace_store", |
| 390 | VG_(fnptr_to_fnentry) |
| 391 | (drd_trace_mem_store), |
| 392 | mkIRExprVec_2(addr_expr, |
| 393 | mkIRExpr_HWord(size))))); |
| 394 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 395 | |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 396 | if (! s_check_stack_accesses && is_stack_access(bb, addr_expr)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 397 | return; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 398 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 399 | switch (size) |
| 400 | { |
| 401 | case 1: |
| 402 | argv = mkIRExprVec_1(addr_expr); |
| 403 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 404 | "drd_trace_store_1", |
| 405 | VG_(fnptr_to_fnentry)(drd_trace_store_1), |
| 406 | argv); |
| 407 | break; |
| 408 | case 2: |
| 409 | argv = mkIRExprVec_1(addr_expr); |
| 410 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 411 | "drd_trace_store_2", |
| 412 | VG_(fnptr_to_fnentry)(drd_trace_store_2), |
| 413 | argv); |
| 414 | break; |
| 415 | case 4: |
| 416 | argv = mkIRExprVec_1(addr_expr); |
| 417 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 418 | "drd_trace_store_4", |
| 419 | VG_(fnptr_to_fnentry)(drd_trace_store_4), |
| 420 | argv); |
| 421 | break; |
| 422 | case 8: |
| 423 | argv = mkIRExprVec_1(addr_expr); |
| 424 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 425 | "drd_trace_store_8", |
| 426 | VG_(fnptr_to_fnentry)(drd_trace_store_8), |
| 427 | argv); |
| 428 | break; |
| 429 | default: |
| 430 | size_expr = mkIRExpr_HWord(size); |
| 431 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 432 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 433 | "drd_trace_store", |
| 434 | VG_(fnptr_to_fnentry)(DRD_(trace_store)), |
| 435 | argv); |
| 436 | break; |
| 437 | } |
| 438 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 439 | } |
| 440 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 441 | IRSB* DRD_(instrument)(VgCallbackClosure* const closure, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 442 | IRSB* const bb_in, |
| 443 | VexGuestLayout* const layout, |
| 444 | VexGuestExtents* const vge, |
| 445 | IRType const gWordTy, |
| 446 | IRType const hWordTy) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 447 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 448 | IRDirty* di; |
| 449 | Int i; |
| 450 | IRSB* bb; |
| 451 | IRExpr** argv; |
| 452 | Bool instrument = True; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 453 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 454 | /* Set up BB */ |
| 455 | bb = emptyIRSB(); |
| 456 | bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv); |
| 457 | bb->next = deepCopyIRExpr(bb_in->next); |
| 458 | bb->jumpkind = bb_in->jumpkind; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 459 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 460 | for (i = 0; i < bb_in->stmts_used; i++) |
| 461 | { |
| 462 | IRStmt* const st = bb_in->stmts[i]; |
| 463 | tl_assert(st); |
| 464 | if (st->tag == Ist_NoOp) |
| 465 | continue; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 466 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 467 | switch (st->tag) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 468 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 469 | /* Note: the code for not instrumenting the code in .plt */ |
| 470 | /* sections is only necessary on CentOS 3.0 x86 (kernel 2.4.21 */ |
| 471 | /* + glibc 2.3.2 + NPTL 0.60 + binutils 2.14.90.0.4). */ |
| 472 | /* This is because on this platform dynamic library symbols are */ |
| 473 | /* relocated in another way than by later binutils versions. The */ |
| 474 | /* linker e.g. does not generate .got.plt sections on CentOS 3.0. */ |
| 475 | case Ist_IMark: |
| 476 | instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr) |
| 477 | != Vg_SectPLT; |
| 478 | addStmtToIRSB(bb, st); |
| 479 | break; |
| 480 | |
| 481 | case Ist_MBE: |
| 482 | switch (st->Ist.MBE.event) |
| 483 | { |
| 484 | case Imbe_Fence: |
| 485 | break; /* not interesting */ |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 486 | default: |
| 487 | tl_assert(0); |
| 488 | } |
| 489 | addStmtToIRSB(bb, st); |
| 490 | break; |
| 491 | |
| 492 | case Ist_Store: |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 493 | if (instrument && /* ignore stores resulting from st{d,w}cx. */ |
| 494 | st->Ist.Store.resSC == IRTemp_INVALID) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 495 | { |
| 496 | instrument_store(bb, |
| 497 | st->Ist.Store.addr, |
| 498 | sizeofIRType(typeOfIRExpr(bb->tyenv, |
| 499 | st->Ist.Store.data))); |
| 500 | } |
| 501 | addStmtToIRSB(bb, st); |
| 502 | break; |
| 503 | |
| 504 | case Ist_WrTmp: |
| 505 | if (instrument) |
| 506 | { |
| 507 | const IRExpr* const data = st->Ist.WrTmp.data; |
| 508 | if (data->tag == Iex_Load) |
| 509 | { |
| 510 | instrument_load(bb, |
| 511 | data->Iex.Load.addr, |
| 512 | sizeofIRType(data->Iex.Load.ty)); |
| 513 | } |
| 514 | } |
| 515 | addStmtToIRSB(bb, st); |
| 516 | break; |
| 517 | |
| 518 | case Ist_Dirty: |
| 519 | if (instrument) |
| 520 | { |
| 521 | IRDirty* d = st->Ist.Dirty.details; |
| 522 | IREffect const mFx = d->mFx; |
| 523 | switch (mFx) { |
| 524 | case Ifx_None: |
| 525 | break; |
| 526 | case Ifx_Read: |
| 527 | case Ifx_Write: |
| 528 | case Ifx_Modify: |
| 529 | tl_assert(d->mAddr); |
| 530 | tl_assert(d->mSize > 0); |
| 531 | argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize)); |
| 532 | if (mFx == Ifx_Read || mFx == Ifx_Modify) { |
| 533 | di = unsafeIRDirty_0_N( |
| 534 | /*regparms*/2, |
| 535 | "drd_trace_load", |
| 536 | VG_(fnptr_to_fnentry)(DRD_(trace_load)), |
| 537 | argv); |
| 538 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 539 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 540 | if (mFx == Ifx_Write || mFx == Ifx_Modify) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 541 | { |
| 542 | di = unsafeIRDirty_0_N( |
| 543 | /*regparms*/2, |
| 544 | "drd_trace_store", |
| 545 | VG_(fnptr_to_fnentry)(DRD_(trace_store)), |
| 546 | argv); |
| 547 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 548 | } |
| 549 | break; |
| 550 | default: |
| 551 | tl_assert(0); |
| 552 | } |
| 553 | } |
| 554 | addStmtToIRSB(bb, st); |
| 555 | break; |
| 556 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 557 | case Ist_CAS: |
| 558 | if (instrument) |
| 559 | { |
bart | a14e328 | 2009-07-11 14:35:59 +0000 | [diff] [blame^] | 560 | /* |
| 561 | * Treat compare-and-swap as a read. By handling atomic |
| 562 | * instructions as read instructions no data races are reported |
| 563 | * between conflicting atomic operations nor between atomic |
| 564 | * operations and non-atomic reads. Conflicts between atomic |
| 565 | * operations and non-atomic write operations are still reported |
| 566 | * however. |
| 567 | */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 568 | Int dataSize; |
| 569 | IRCAS* cas = st->Ist.CAS.details; |
| 570 | tl_assert(cas->addr != NULL); |
| 571 | tl_assert(cas->dataLo != NULL); |
| 572 | dataSize = sizeofIRType(typeOfIRExpr(bb->tyenv, cas->dataLo)); |
| 573 | if (cas->dataHi != NULL) |
| 574 | dataSize *= 2; /* since it's a doubleword-CAS */ |
| 575 | instrument_load(bb, cas->addr, dataSize); |
| 576 | } |
| 577 | addStmtToIRSB(bb, st); |
| 578 | break; |
| 579 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 580 | default: |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 581 | addStmtToIRSB(bb, st); |
| 582 | break; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 583 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 584 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 585 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 586 | return bb; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 587 | } |
| 588 | |