bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 1 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 2 | This file is part of drd, a thread error detector. |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 3 | |
bart | 922304f | 2011-03-13 12:02:44 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>. |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU General Public License as |
| 8 | published by the Free Software Foundation; either version 2 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307, USA. |
| 20 | |
| 21 | The GNU General Public License is contained in the file COPYING. |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "drd_bitmap.h" |
| 26 | #include "drd_thread_bitmap.h" |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 27 | #include "drd_vc.h" /* DRD_(vc_snprint)() */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 28 | |
| 29 | /* Include several source files here in order to allow the compiler to */ |
| 30 | /* do more inlining. */ |
| 31 | #include "drd_bitmap.c" |
| 32 | #include "drd_load_store.h" |
| 33 | #include "drd_segment.c" |
| 34 | #include "drd_thread.c" |
| 35 | #include "drd_vc.c" |
| 36 | #include "libvex_guest_offsets.h" |
| 37 | |
| 38 | |
| 39 | /* STACK_POINTER_OFFSET: VEX register offset for the stack pointer register. */ |
| 40 | #if defined(VGA_x86) |
| 41 | #define STACK_POINTER_OFFSET OFFSET_x86_ESP |
| 42 | #elif defined(VGA_amd64) |
| 43 | #define STACK_POINTER_OFFSET OFFSET_amd64_RSP |
| 44 | #elif defined(VGA_ppc32) |
sewardj | 4cb6bf7 | 2010-01-01 18:31:41 +0000 | [diff] [blame] | 45 | #define STACK_POINTER_OFFSET OFFSET_ppc32_GPR1 |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 46 | #elif defined(VGA_ppc64) |
sewardj | 4cb6bf7 | 2010-01-01 18:31:41 +0000 | [diff] [blame] | 47 | #define STACK_POINTER_OFFSET OFFSET_ppc64_GPR1 |
| 48 | #elif defined(VGA_arm) |
| 49 | #define STACK_POINTER_OFFSET OFFSET_arm_R13 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 50 | #elif defined(VGA_s390x) |
| 51 | #define STACK_POINTER_OFFSET OFFSET_s390x_r15 |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 52 | #else |
| 53 | #error Unknown architecture. |
| 54 | #endif |
| 55 | |
| 56 | |
| 57 | /* Local variables. */ |
| 58 | |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 59 | static Bool s_check_stack_accesses = False; |
| 60 | static Bool s_first_race_only = False; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | /* Function definitions. */ |
| 64 | |
| 65 | Bool DRD_(get_check_stack_accesses)() |
| 66 | { |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 67 | return s_check_stack_accesses; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void DRD_(set_check_stack_accesses)(const Bool c) |
| 71 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 72 | tl_assert(c == False || c == True); |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 73 | s_check_stack_accesses = c; |
| 74 | } |
| 75 | |
| 76 | Bool DRD_(get_first_race_only)() |
| 77 | { |
| 78 | return s_first_race_only; |
| 79 | } |
| 80 | |
| 81 | void DRD_(set_first_race_only)(const Bool fro) |
| 82 | { |
| 83 | tl_assert(fro == False || fro == True); |
| 84 | s_first_race_only = fro; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 87 | void DRD_(trace_mem_access)(const Addr addr, const SizeT size, |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 88 | const BmAccessTypeT access_type, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 89 | const HWord stored_value_hi, |
| 90 | const HWord stored_value_lo) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 91 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 92 | if (DRD_(is_any_traced)(addr, addr + size)) |
| 93 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 94 | char* vc; |
| 95 | |
| 96 | vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(DRD_(thread_get_running_tid)())); |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 97 | if (access_type == eStore && size <= sizeof(HWord)) { |
bart | 5cda1b5 | 2011-12-12 19:37:10 +0000 | [diff] [blame] | 98 | DRD_(trace_msg_w_bt)("store 0x%lx size %ld val %ld/0x%lx (thread %d /" |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 99 | " vc %s)", addr, size, stored_value_lo, |
| 100 | stored_value_lo, DRD_(thread_get_running_tid)(), |
| 101 | vc); |
| 102 | } else if (access_type == eStore && size > sizeof(HWord)) { |
| 103 | ULong sv; |
| 104 | |
| 105 | tl_assert(sizeof(HWord) == 4); |
| 106 | sv = ((ULong)stored_value_hi << 32) | stored_value_lo; |
| 107 | DRD_(trace_msg_w_bt)("store 0x%lx size %ld val %lld/0x%llx (thread %d" |
| 108 | " / vc %s)", addr, size, sv, sv, |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 109 | DRD_(thread_get_running_tid)(), vc); |
| 110 | } else { |
| 111 | DRD_(trace_msg_w_bt)("%s 0x%lx size %ld (thread %d / vc %s)", |
| 112 | access_type == eLoad ? "load " |
| 113 | : access_type == eStore ? "store" |
| 114 | : access_type == eStart ? "start" |
| 115 | : access_type == eEnd ? "end " : "????", |
| 116 | addr, size, DRD_(thread_get_running_tid)(), vc); |
| 117 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 118 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 119 | tl_assert(DRD_(DrdThreadIdToVgThreadId)(DRD_(thread_get_running_tid)()) |
| 120 | == VG_(get_running_tid)()); |
| 121 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 122 | } |
| 123 | |
bart | 0759503 | 2010-08-29 09:51:06 +0000 | [diff] [blame] | 124 | static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 125 | { |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 126 | return DRD_(trace_mem_access)(addr, size, eLoad, 0, 0); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 129 | static VG_REGPARM(3) void drd_trace_mem_store(const Addr addr,const SizeT size, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 130 | const HWord stored_value_hi, |
| 131 | const HWord stored_value_lo) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 132 | { |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 133 | return DRD_(trace_mem_access)(addr, size, eStore, stored_value_hi, |
| 134 | stored_value_lo); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void drd_report_race(const Addr addr, const SizeT size, |
| 138 | const BmAccessTypeT access_type) |
| 139 | { |
bart | 7a9db0f | 2012-04-01 14:40:16 +0000 | [diff] [blame] | 140 | ThreadId vg_tid; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 141 | |
bart | 7a9db0f | 2012-04-01 14:40:16 +0000 | [diff] [blame] | 142 | vg_tid = VG_(get_running_tid)(); |
bart | 7886dd3 | 2012-04-01 15:06:57 +0000 | [diff] [blame^] | 143 | if (!DRD_(get_check_stack_accesses)() |
| 144 | && DRD_(thread_address_on_any_stack)(addr)) { |
bart | 7a9db0f | 2012-04-01 14:40:16 +0000 | [diff] [blame] | 145 | #if 0 |
| 146 | GenericErrInfo GEI = { |
| 147 | .tid = DRD_(thread_get_running_tid)(), |
| 148 | .addr = addr, |
| 149 | }; |
| 150 | VG_(maybe_record_error)(vg_tid, GenericErr, VG_(get_IP)(vg_tid), |
| 151 | "--check-stack-var=no skips checking stack" |
| 152 | " variables shared over threads", |
| 153 | &GEI); |
| 154 | #endif |
| 155 | } else { |
| 156 | DataRaceErrInfo drei = { |
| 157 | .tid = DRD_(thread_get_running_tid)(), |
| 158 | .addr = addr, |
| 159 | .size = size, |
| 160 | .access_type = access_type, |
| 161 | }; |
| 162 | VG_(maybe_record_error)(vg_tid, DataRaceErr, VG_(get_IP)(vg_tid), |
| 163 | "Conflicting access", &drei); |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 164 | |
bart | 7a9db0f | 2012-04-01 14:40:16 +0000 | [diff] [blame] | 165 | if (s_first_race_only) |
| 166 | DRD_(start_suppression)(addr, addr + size, "first race only"); |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 167 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 170 | VG_REGPARM(2) void DRD_(trace_load)(Addr addr, SizeT size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 171 | { |
| 172 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 173 | /* The assert below has been commented out because of performance reasons.*/ |
bart | d5bbc61 | 2010-09-02 14:44:17 +0000 | [diff] [blame] | 174 | tl_assert(DRD_(thread_get_running_tid)() |
| 175 | == DRD_(VgThreadIdToDrdThreadId)(VG_(get_running_tid()))); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 178 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 179 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 180 | || ! DRD_(thread_address_on_stack)(addr)) |
| 181 | && bm_access_load_triggers_conflict(addr, addr + size) |
| 182 | && ! DRD_(is_suppressed)(addr, addr + size)) |
| 183 | { |
| 184 | drd_report_race(addr, size, eLoad); |
| 185 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static VG_REGPARM(1) void drd_trace_load_1(Addr addr) |
| 189 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 190 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 191 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 192 | || ! DRD_(thread_address_on_stack)(addr)) |
| 193 | && bm_access_load_1_triggers_conflict(addr) |
| 194 | && ! DRD_(is_suppressed)(addr, addr + 1)) |
| 195 | { |
| 196 | drd_report_race(addr, 1, eLoad); |
| 197 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static VG_REGPARM(1) void drd_trace_load_2(Addr addr) |
| 201 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 202 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 203 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 204 | || ! DRD_(thread_address_on_stack)(addr)) |
| 205 | && bm_access_load_2_triggers_conflict(addr) |
| 206 | && ! DRD_(is_suppressed)(addr, addr + 2)) |
| 207 | { |
| 208 | drd_report_race(addr, 2, eLoad); |
| 209 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static VG_REGPARM(1) void drd_trace_load_4(Addr addr) |
| 213 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 214 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 215 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 216 | || ! DRD_(thread_address_on_stack)(addr)) |
| 217 | && bm_access_load_4_triggers_conflict(addr) |
| 218 | && ! DRD_(is_suppressed)(addr, addr + 4)) |
| 219 | { |
| 220 | drd_report_race(addr, 4, eLoad); |
| 221 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static VG_REGPARM(1) void drd_trace_load_8(Addr addr) |
| 225 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 226 | if (DRD_(running_thread_is_recording_loads)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 227 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 228 | || ! DRD_(thread_address_on_stack)(addr)) |
| 229 | && bm_access_load_8_triggers_conflict(addr) |
| 230 | && ! DRD_(is_suppressed)(addr, addr + 8)) |
| 231 | { |
| 232 | drd_report_race(addr, 8, eLoad); |
| 233 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 236 | VG_REGPARM(2) void DRD_(trace_store)(Addr addr, SizeT size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 237 | { |
| 238 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 239 | /* The assert below has been commented out because of performance reasons.*/ |
bart | d5bbc61 | 2010-09-02 14:44:17 +0000 | [diff] [blame] | 240 | tl_assert(DRD_(thread_get_running_tid)() |
| 241 | == DRD_(VgThreadIdToDrdThreadId)(VG_(get_running_tid()))); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 242 | #endif |
| 243 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 244 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 245 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 246 | || ! DRD_(thread_address_on_stack)(addr)) |
| 247 | && bm_access_store_triggers_conflict(addr, addr + size) |
| 248 | && ! DRD_(is_suppressed)(addr, addr + size)) |
| 249 | { |
| 250 | drd_report_race(addr, size, eStore); |
| 251 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static VG_REGPARM(1) void drd_trace_store_1(Addr addr) |
| 255 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 256 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 257 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 258 | || ! DRD_(thread_address_on_stack)(addr)) |
| 259 | && bm_access_store_1_triggers_conflict(addr) |
| 260 | && ! DRD_(is_suppressed)(addr, addr + 1)) |
| 261 | { |
| 262 | drd_report_race(addr, 1, eStore); |
| 263 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static VG_REGPARM(1) void drd_trace_store_2(Addr addr) |
| 267 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 268 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 269 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 270 | || ! DRD_(thread_address_on_stack)(addr)) |
| 271 | && bm_access_store_2_triggers_conflict(addr) |
| 272 | && ! DRD_(is_suppressed)(addr, addr + 2)) |
| 273 | { |
| 274 | drd_report_race(addr, 2, eStore); |
| 275 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static VG_REGPARM(1) void drd_trace_store_4(Addr addr) |
| 279 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 280 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 281 | && (s_check_stack_accesses |
bart | 71ce132 | 2011-12-11 17:54:17 +0000 | [diff] [blame] | 282 | || !DRD_(thread_address_on_stack)(addr)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 283 | && bm_access_store_4_triggers_conflict(addr) |
bart | 71ce132 | 2011-12-11 17:54:17 +0000 | [diff] [blame] | 284 | && !DRD_(is_suppressed)(addr, addr + 4)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 285 | { |
| 286 | drd_report_race(addr, 4, eStore); |
| 287 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static VG_REGPARM(1) void drd_trace_store_8(Addr addr) |
| 291 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 292 | if (DRD_(running_thread_is_recording_stores)() |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 293 | && (s_check_stack_accesses |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 294 | || ! DRD_(thread_address_on_stack)(addr)) |
| 295 | && bm_access_store_8_triggers_conflict(addr) |
| 296 | && ! DRD_(is_suppressed)(addr, addr + 8)) |
| 297 | { |
| 298 | drd_report_race(addr, 8, eStore); |
| 299 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Return true if and only if addr_expr matches the pattern (SP) or |
| 304 | * <offset>(SP). |
| 305 | */ |
| 306 | static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr) |
| 307 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 308 | Bool result = False; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 309 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 310 | if (addr_expr->tag == Iex_RdTmp) |
| 311 | { |
| 312 | int i; |
| 313 | for (i = 0; i < bb->stmts_size; i++) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 314 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 315 | if (bb->stmts[i] |
| 316 | && bb->stmts[i]->tag == Ist_WrTmp |
| 317 | && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp) |
| 318 | { |
| 319 | IRExpr* e = bb->stmts[i]->Ist.WrTmp.data; |
| 320 | if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET) |
| 321 | { |
| 322 | result = True; |
| 323 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 324 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 325 | //ppIRExpr(e); |
| 326 | //VG_(printf)(" (%s)\n", result ? "True" : "False"); |
| 327 | break; |
| 328 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 329 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 330 | } |
| 331 | return result; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 332 | } |
| 333 | |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 334 | static const IROp u_widen_irop[5][9] = { |
bart | 9ad8d80 | 2011-12-12 19:54:32 +0000 | [diff] [blame] | 335 | [Ity_I1 - Ity_I1] = { [4] = Iop_1Uto32, [8] = Iop_1Uto64 }, |
| 336 | [Ity_I8 - Ity_I1] = { [4] = Iop_8Uto32, [8] = Iop_8Uto64 }, |
| 337 | [Ity_I16 - Ity_I1] = { [4] = Iop_16Uto32, [8] = Iop_16Uto64 }, |
| 338 | [Ity_I32 - Ity_I1] = { [8] = Iop_32Uto64 }, |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 341 | /** |
| 342 | * Instrument the client code to trace a memory load (--trace-addr). |
| 343 | */ |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 344 | static IRExpr* instr_trace_mem_load(IRSB* const bb, IRExpr* addr_expr, |
| 345 | const HWord size) |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 346 | { |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 347 | IRTemp tmp; |
| 348 | |
| 349 | tmp = newIRTemp(bb->tyenv, typeOfIRExpr(bb->tyenv, addr_expr)); |
| 350 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, addr_expr)); |
| 351 | addr_expr = IRExpr_RdTmp(tmp); |
| 352 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 353 | addStmtToIRSB(bb, |
| 354 | IRStmt_Dirty( |
| 355 | unsafeIRDirty_0_N(/*regparms*/2, |
| 356 | "drd_trace_mem_load", |
| 357 | VG_(fnptr_to_fnentry) |
| 358 | (drd_trace_mem_load), |
| 359 | mkIRExprVec_2(addr_expr, mkIRExpr_HWord(size))))); |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 360 | |
| 361 | return addr_expr; |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Instrument the client code to trace a memory store (--trace-addr). |
| 366 | */ |
| 367 | static void instr_trace_mem_store(IRSB* const bb, IRExpr* const addr_expr, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 368 | IRExpr* data_expr_hi, IRExpr* data_expr_lo) |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 369 | { |
| 370 | IRType ty_data_expr; |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 371 | HWord size; |
| 372 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 373 | tl_assert(sizeof(HWord) == 4 || sizeof(HWord) == 8); |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 374 | tl_assert(!data_expr_hi || typeOfIRExpr(bb->tyenv, data_expr_hi) == Ity_I32); |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 375 | |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 376 | ty_data_expr = typeOfIRExpr(bb->tyenv, data_expr_lo); |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 377 | size = sizeofIRType(ty_data_expr); |
| 378 | |
bart | 7ca75ed | 2011-12-13 08:53:23 +0000 | [diff] [blame] | 379 | #if 0 |
| 380 | // Test code |
| 381 | if (ty_data_expr == Ity_I32) { |
| 382 | IRTemp tmp = newIRTemp(bb->tyenv, Ity_F32); |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 383 | data_expr_lo = IRExpr_Unop(Iop_ReinterpI32asF32, data_expr_lo); |
| 384 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, data_expr_lo)); |
| 385 | data_expr_lo = IRExpr_RdTmp(tmp); |
bart | 7ca75ed | 2011-12-13 08:53:23 +0000 | [diff] [blame] | 386 | ty_data_expr = Ity_F32; |
| 387 | } else if (ty_data_expr == Ity_I64) { |
| 388 | IRTemp tmp = newIRTemp(bb->tyenv, Ity_F64); |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 389 | data_expr_lo = IRExpr_Unop(Iop_ReinterpI64asF64, data_expr_lo); |
| 390 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, data_expr_lo)); |
| 391 | data_expr_lo = IRExpr_RdTmp(tmp); |
bart | 7ca75ed | 2011-12-13 08:53:23 +0000 | [diff] [blame] | 392 | ty_data_expr = Ity_F64; |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | if (ty_data_expr == Ity_F32) { |
| 397 | IRTemp tmp = newIRTemp(bb->tyenv, Ity_I32); |
| 398 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_ReinterpF32asI32, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 399 | data_expr_lo))); |
| 400 | data_expr_lo = IRExpr_RdTmp(tmp); |
bart | 7ca75ed | 2011-12-13 08:53:23 +0000 | [diff] [blame] | 401 | ty_data_expr = Ity_I32; |
| 402 | } else if (ty_data_expr == Ity_F64) { |
| 403 | IRTemp tmp = newIRTemp(bb->tyenv, Ity_I64); |
| 404 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, IRExpr_Unop(Iop_ReinterpF64asI64, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 405 | data_expr_lo))); |
| 406 | data_expr_lo = IRExpr_RdTmp(tmp); |
bart | 7ca75ed | 2011-12-13 08:53:23 +0000 | [diff] [blame] | 407 | ty_data_expr = Ity_I64; |
| 408 | } |
| 409 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 410 | if (size == sizeof(HWord) |
| 411 | && (ty_data_expr == Ity_I32 || ty_data_expr == Ity_I64)) |
| 412 | { |
| 413 | /* No conversion necessary */ |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 414 | } else { |
| 415 | IROp widen_op; |
| 416 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 417 | if (Ity_I1 <= ty_data_expr |
| 418 | && ty_data_expr |
| 419 | < Ity_I1 + sizeof(u_widen_irop)/sizeof(u_widen_irop[0])) |
| 420 | { |
| 421 | widen_op = u_widen_irop[ty_data_expr - Ity_I1][sizeof(HWord)]; |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 422 | if (!widen_op) |
| 423 | widen_op = Iop_INVALID; |
| 424 | } else { |
| 425 | widen_op = Iop_INVALID; |
| 426 | } |
| 427 | if (widen_op != Iop_INVALID) { |
| 428 | IRTemp tmp; |
| 429 | |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 430 | /* Widen the integer expression to a HWord */ |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 431 | tmp = newIRTemp(bb->tyenv, sizeof(HWord) == 4 ? Ity_I32 : Ity_I64); |
| 432 | addStmtToIRSB(bb, |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 433 | IRStmt_WrTmp(tmp, IRExpr_Unop(widen_op, data_expr_lo))); |
| 434 | data_expr_lo = IRExpr_RdTmp(tmp); |
| 435 | } else if (size > sizeof(HWord) && !data_expr_hi |
| 436 | && ty_data_expr == Ity_I64) { |
| 437 | IRTemp tmp; |
| 438 | |
| 439 | tl_assert(sizeof(HWord) == 4); |
| 440 | tl_assert(size == 8); |
| 441 | tmp = newIRTemp(bb->tyenv, Ity_I32); |
| 442 | addStmtToIRSB(bb, |
| 443 | IRStmt_WrTmp(tmp, |
| 444 | IRExpr_Unop(Iop_64HIto32, data_expr_lo))); |
| 445 | data_expr_hi = IRExpr_RdTmp(tmp); |
| 446 | tmp = newIRTemp(bb->tyenv, Ity_I32); |
| 447 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, |
| 448 | IRExpr_Unop(Iop_64to32, data_expr_lo))); |
| 449 | data_expr_lo = IRExpr_RdTmp(tmp); |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 450 | } else { |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 451 | data_expr_lo = mkIRExpr_HWord(0); |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | addStmtToIRSB(bb, |
| 455 | IRStmt_Dirty( |
| 456 | unsafeIRDirty_0_N(/*regparms*/3, |
| 457 | "drd_trace_mem_store", |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 458 | VG_(fnptr_to_fnentry)(drd_trace_mem_store), |
| 459 | mkIRExprVec_4(addr_expr, mkIRExpr_HWord(size), |
| 460 | data_expr_hi ? data_expr_hi |
| 461 | : mkIRExpr_HWord(0), data_expr_lo)))); |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void instrument_load(IRSB* const bb, IRExpr* const addr_expr, |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 465 | const HWord size) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 466 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 467 | IRExpr* size_expr; |
| 468 | IRExpr** argv; |
| 469 | IRDirty* di; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 470 | |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 471 | if (!s_check_stack_accesses && is_stack_access(bb, addr_expr)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 472 | return; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 473 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 474 | switch (size) |
| 475 | { |
| 476 | case 1: |
| 477 | argv = mkIRExprVec_1(addr_expr); |
| 478 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 479 | "drd_trace_load_1", |
| 480 | VG_(fnptr_to_fnentry)(drd_trace_load_1), |
| 481 | argv); |
| 482 | break; |
| 483 | case 2: |
| 484 | argv = mkIRExprVec_1(addr_expr); |
| 485 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 486 | "drd_trace_load_2", |
| 487 | VG_(fnptr_to_fnentry)(drd_trace_load_2), |
| 488 | argv); |
| 489 | break; |
| 490 | case 4: |
| 491 | argv = mkIRExprVec_1(addr_expr); |
| 492 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 493 | "drd_trace_load_4", |
| 494 | VG_(fnptr_to_fnentry)(drd_trace_load_4), |
| 495 | argv); |
| 496 | break; |
| 497 | case 8: |
| 498 | argv = mkIRExprVec_1(addr_expr); |
| 499 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 500 | "drd_trace_load_8", |
| 501 | VG_(fnptr_to_fnentry)(drd_trace_load_8), |
| 502 | argv); |
| 503 | break; |
| 504 | default: |
| 505 | size_expr = mkIRExpr_HWord(size); |
| 506 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 507 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 508 | "drd_trace_load", |
| 509 | VG_(fnptr_to_fnentry)(DRD_(trace_load)), |
| 510 | argv); |
| 511 | break; |
| 512 | } |
| 513 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 514 | } |
| 515 | |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 516 | static void instrument_store(IRSB* const bb, IRExpr* addr_expr, |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 517 | IRExpr* const data_expr) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 518 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 519 | IRExpr* size_expr; |
| 520 | IRExpr** argv; |
| 521 | IRDirty* di; |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 522 | HWord size; |
| 523 | |
| 524 | size = sizeofIRType(typeOfIRExpr(bb->tyenv, data_expr)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 525 | |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 526 | if (UNLIKELY(DRD_(any_address_is_traced)())) { |
| 527 | IRTemp tmp = newIRTemp(bb->tyenv, typeOfIRExpr(bb->tyenv, addr_expr)); |
| 528 | addStmtToIRSB(bb, IRStmt_WrTmp(tmp, addr_expr)); |
| 529 | addr_expr = IRExpr_RdTmp(tmp); |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 530 | instr_trace_mem_store(bb, addr_expr, NULL, data_expr); |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 531 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 532 | |
bart | 71ce132 | 2011-12-11 17:54:17 +0000 | [diff] [blame] | 533 | if (!s_check_stack_accesses && is_stack_access(bb, addr_expr)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 534 | return; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 535 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 536 | switch (size) |
| 537 | { |
| 538 | case 1: |
| 539 | argv = mkIRExprVec_1(addr_expr); |
| 540 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 541 | "drd_trace_store_1", |
| 542 | VG_(fnptr_to_fnentry)(drd_trace_store_1), |
| 543 | argv); |
| 544 | break; |
| 545 | case 2: |
| 546 | argv = mkIRExprVec_1(addr_expr); |
| 547 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 548 | "drd_trace_store_2", |
| 549 | VG_(fnptr_to_fnentry)(drd_trace_store_2), |
| 550 | argv); |
| 551 | break; |
| 552 | case 4: |
| 553 | argv = mkIRExprVec_1(addr_expr); |
| 554 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 555 | "drd_trace_store_4", |
| 556 | VG_(fnptr_to_fnentry)(drd_trace_store_4), |
| 557 | argv); |
| 558 | break; |
| 559 | case 8: |
| 560 | argv = mkIRExprVec_1(addr_expr); |
| 561 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 562 | "drd_trace_store_8", |
| 563 | VG_(fnptr_to_fnentry)(drd_trace_store_8), |
| 564 | argv); |
| 565 | break; |
| 566 | default: |
| 567 | size_expr = mkIRExpr_HWord(size); |
| 568 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 569 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 570 | "drd_trace_store", |
| 571 | VG_(fnptr_to_fnentry)(DRD_(trace_store)), |
| 572 | argv); |
| 573 | break; |
| 574 | } |
| 575 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 576 | } |
| 577 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 578 | IRSB* DRD_(instrument)(VgCallbackClosure* const closure, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 579 | IRSB* const bb_in, |
| 580 | VexGuestLayout* const layout, |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 581 | VexGuestExtents* const vge, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 582 | IRType const gWordTy, |
| 583 | IRType const hWordTy) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 584 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 585 | IRDirty* di; |
| 586 | Int i; |
| 587 | IRSB* bb; |
| 588 | IRExpr** argv; |
| 589 | Bool instrument = True; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 590 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 591 | /* Set up BB */ |
| 592 | bb = emptyIRSB(); |
| 593 | bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv); |
| 594 | bb->next = deepCopyIRExpr(bb_in->next); |
| 595 | bb->jumpkind = bb_in->jumpkind; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 596 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 597 | for (i = 0; i < bb_in->stmts_used; i++) |
| 598 | { |
| 599 | IRStmt* const st = bb_in->stmts[i]; |
| 600 | tl_assert(st); |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 601 | tl_assert(isFlatIRStmt(st)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 602 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 603 | switch (st->tag) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 604 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 605 | /* Note: the code for not instrumenting the code in .plt */ |
| 606 | /* sections is only necessary on CentOS 3.0 x86 (kernel 2.4.21 */ |
| 607 | /* + glibc 2.3.2 + NPTL 0.60 + binutils 2.14.90.0.4). */ |
| 608 | /* This is because on this platform dynamic library symbols are */ |
| 609 | /* relocated in another way than by later binutils versions. The */ |
| 610 | /* linker e.g. does not generate .got.plt sections on CentOS 3.0. */ |
| 611 | case Ist_IMark: |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 612 | instrument = VG_(DebugInfo_sect_kind)(NULL, 0, st->Ist.IMark.addr) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 613 | != Vg_SectPLT; |
| 614 | addStmtToIRSB(bb, st); |
| 615 | break; |
| 616 | |
| 617 | case Ist_MBE: |
| 618 | switch (st->Ist.MBE.event) |
| 619 | { |
| 620 | case Imbe_Fence: |
| 621 | break; /* not interesting */ |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 622 | default: |
| 623 | tl_assert(0); |
| 624 | } |
| 625 | addStmtToIRSB(bb, st); |
| 626 | break; |
| 627 | |
| 628 | case Ist_Store: |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 629 | if (instrument) |
bart | 7826acb | 2011-12-11 18:49:39 +0000 | [diff] [blame] | 630 | instrument_store(bb, st->Ist.Store.addr, st->Ist.Store.data); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 631 | addStmtToIRSB(bb, st); |
| 632 | break; |
| 633 | |
| 634 | case Ist_WrTmp: |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 635 | if (instrument) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 636 | const IRExpr* const data = st->Ist.WrTmp.data; |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 637 | IRExpr* addr_expr = data->Iex.Load.addr; |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 638 | if (data->tag == Iex_Load) { |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 639 | if (UNLIKELY(DRD_(any_address_is_traced)())) { |
| 640 | addr_expr = instr_trace_mem_load(bb, addr_expr, |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 641 | sizeofIRType(data->Iex.Load.ty)); |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 642 | } |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 643 | instrument_load(bb, data->Iex.Load.addr, |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 644 | sizeofIRType(data->Iex.Load.ty)); |
| 645 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 646 | } |
| 647 | addStmtToIRSB(bb, st); |
| 648 | break; |
| 649 | |
| 650 | case Ist_Dirty: |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 651 | if (instrument) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 652 | IRDirty* d = st->Ist.Dirty.details; |
| 653 | IREffect const mFx = d->mFx; |
| 654 | switch (mFx) { |
| 655 | case Ifx_None: |
| 656 | break; |
| 657 | case Ifx_Read: |
| 658 | case Ifx_Write: |
| 659 | case Ifx_Modify: |
| 660 | tl_assert(d->mAddr); |
| 661 | tl_assert(d->mSize > 0); |
| 662 | argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize)); |
| 663 | if (mFx == Ifx_Read || mFx == Ifx_Modify) { |
| 664 | di = unsafeIRDirty_0_N( |
| 665 | /*regparms*/2, |
| 666 | "drd_trace_load", |
| 667 | VG_(fnptr_to_fnentry)(DRD_(trace_load)), |
| 668 | argv); |
| 669 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 670 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 671 | if (mFx == Ifx_Write || mFx == Ifx_Modify) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 672 | { |
| 673 | di = unsafeIRDirty_0_N( |
| 674 | /*regparms*/2, |
| 675 | "drd_trace_store", |
| 676 | VG_(fnptr_to_fnentry)(DRD_(trace_store)), |
| 677 | argv); |
| 678 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 679 | } |
| 680 | break; |
| 681 | default: |
| 682 | tl_assert(0); |
| 683 | } |
| 684 | } |
| 685 | addStmtToIRSB(bb, st); |
| 686 | break; |
| 687 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 688 | case Ist_CAS: |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 689 | if (instrument) { |
bart | a14e328 | 2009-07-11 14:35:59 +0000 | [diff] [blame] | 690 | /* |
| 691 | * Treat compare-and-swap as a read. By handling atomic |
| 692 | * instructions as read instructions no data races are reported |
| 693 | * between conflicting atomic operations nor between atomic |
| 694 | * operations and non-atomic reads. Conflicts between atomic |
| 695 | * operations and non-atomic write operations are still reported |
| 696 | * however. |
| 697 | */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 698 | Int dataSize; |
| 699 | IRCAS* cas = st->Ist.CAS.details; |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 700 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 701 | tl_assert(cas->addr != NULL); |
| 702 | tl_assert(cas->dataLo != NULL); |
| 703 | dataSize = sizeofIRType(typeOfIRExpr(bb->tyenv, cas->dataLo)); |
| 704 | if (cas->dataHi != NULL) |
| 705 | dataSize *= 2; /* since it's a doubleword-CAS */ |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 706 | |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 707 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
| 708 | instr_trace_mem_store(bb, cas->addr, cas->dataHi, cas->dataLo); |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 709 | |
| 710 | instrument_load(bb, cas->addr, dataSize); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 711 | } |
| 712 | addStmtToIRSB(bb, st); |
| 713 | break; |
| 714 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 715 | case Ist_LLSC: { |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 716 | /* |
| 717 | * Ignore store-conditionals (except for tracing), and handle |
| 718 | * load-linked's exactly like normal loads. |
| 719 | */ |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 720 | IRType dataTy; |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 721 | |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 722 | if (st->Ist.LLSC.storedata == NULL) { |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 723 | /* LL */ |
| 724 | dataTy = typeOfIRTemp(bb_in->tyenv, st->Ist.LLSC.result); |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 725 | if (instrument) { |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 726 | IRExpr* addr_expr = st->Ist.LLSC.addr; |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 727 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 728 | addr_expr = instr_trace_mem_load(bb, addr_expr, |
| 729 | sizeofIRType(dataTy)); |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 730 | |
bart | 25026ef | 2011-12-17 12:59:45 +0000 | [diff] [blame] | 731 | instrument_load(bb, addr_expr, sizeofIRType(dataTy)); |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 732 | } |
bart | ea69215 | 2011-12-11 20:17:57 +0000 | [diff] [blame] | 733 | } else { |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 734 | /* SC */ |
bart | 42f3263 | 2011-12-13 11:12:05 +0000 | [diff] [blame] | 735 | instr_trace_mem_store(bb, st->Ist.LLSC.addr, NULL, |
bart | b63dc78 | 2011-12-12 19:18:26 +0000 | [diff] [blame] | 736 | st->Ist.LLSC.storedata); |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 737 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 738 | addStmtToIRSB(bb, st); |
| 739 | break; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 740 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 741 | |
| 742 | case Ist_NoOp: |
| 743 | case Ist_AbiHint: |
| 744 | case Ist_Put: |
| 745 | case Ist_PutI: |
| 746 | case Ist_Exit: |
| 747 | /* None of these can contain any memory references. */ |
| 748 | addStmtToIRSB(bb, st); |
| 749 | break; |
| 750 | |
| 751 | default: |
| 752 | ppIRStmt(st); |
| 753 | tl_assert(0); |
| 754 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 755 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 756 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 757 | return bb; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 758 | } |
| 759 | |