blob: e5f2cc4645ac2f85c2fbdc520e1c5fcaa5e6c871 [file] [log] [blame]
bart09dc13f2009-02-14 15:13:31 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
bart09dc13f2009-02-14 15:13:31 +00003
bart922304f2011-03-13 12:02:44 +00004 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
bart09dc13f2009-02-14 15:13:31 +00005
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"
bart41b226c2009-02-14 16:55:19 +000027#include "drd_vc.h" /* DRD_(vc_snprint)() */
bart09dc13f2009-02-14 15:13:31 +000028
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)
sewardj4cb6bf72010-01-01 18:31:41 +000045#define STACK_POINTER_OFFSET OFFSET_ppc32_GPR1
bart09dc13f2009-02-14 15:13:31 +000046#elif defined(VGA_ppc64)
sewardj4cb6bf72010-01-01 18:31:41 +000047#define STACK_POINTER_OFFSET OFFSET_ppc64_GPR1
48#elif defined(VGA_arm)
49#define STACK_POINTER_OFFSET OFFSET_arm_R13
sewardjb5b87402011-03-07 16:05:35 +000050#elif defined(VGA_s390x)
51#define STACK_POINTER_OFFSET OFFSET_s390x_r15
bart09dc13f2009-02-14 15:13:31 +000052#else
53#error Unknown architecture.
54#endif
55
56
57/* Local variables. */
58
bartf98a5692009-05-03 17:17:37 +000059static Bool s_check_stack_accesses = False;
60static Bool s_first_race_only = False;
bart09dc13f2009-02-14 15:13:31 +000061
62
63/* Function definitions. */
64
65Bool DRD_(get_check_stack_accesses)()
66{
bartf98a5692009-05-03 17:17:37 +000067 return s_check_stack_accesses;
bart09dc13f2009-02-14 15:13:31 +000068}
69
70void DRD_(set_check_stack_accesses)(const Bool c)
71{
bartbedfd232009-03-26 19:07:15 +000072 tl_assert(c == False || c == True);
bartf98a5692009-05-03 17:17:37 +000073 s_check_stack_accesses = c;
74}
75
76Bool DRD_(get_first_race_only)()
77{
78 return s_first_race_only;
79}
80
81void DRD_(set_first_race_only)(const Bool fro)
82{
83 tl_assert(fro == False || fro == True);
84 s_first_race_only = fro;
bart09dc13f2009-02-14 15:13:31 +000085}
86
bart1335ecc2009-02-14 16:10:53 +000087void DRD_(trace_mem_access)(const Addr addr, const SizeT size,
bart7826acb2011-12-11 18:49:39 +000088 const BmAccessTypeT access_type,
89 const HWord stored_value)
bart09dc13f2009-02-14 15:13:31 +000090{
bartbedfd232009-03-26 19:07:15 +000091 if (DRD_(is_any_traced)(addr, addr + size))
92 {
bart8f822af2009-06-08 18:20:42 +000093 char* vc;
94
95 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(DRD_(thread_get_running_tid)()));
bart7826acb2011-12-11 18:49:39 +000096 if (access_type == eStore && size <= sizeof(HWord)) {
97 DRD_(trace_msg_w_bt)("store 0x%lx size %ld val 0x%lx (thread %d /"
98 " vc %s)", addr, size, stored_value,
99 DRD_(thread_get_running_tid)(), vc);
100 } else {
101 DRD_(trace_msg_w_bt)("%s 0x%lx size %ld (thread %d / vc %s)",
102 access_type == eLoad ? "load "
103 : access_type == eStore ? "store"
104 : access_type == eStart ? "start"
105 : access_type == eEnd ? "end " : "????",
106 addr, size, DRD_(thread_get_running_tid)(), vc);
107 }
bart8f822af2009-06-08 18:20:42 +0000108 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000109 tl_assert(DRD_(DrdThreadIdToVgThreadId)(DRD_(thread_get_running_tid)())
110 == VG_(get_running_tid)());
111 }
bart09dc13f2009-02-14 15:13:31 +0000112}
113
bart07595032010-08-29 09:51:06 +0000114static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size)
bart09dc13f2009-02-14 15:13:31 +0000115{
bart7826acb2011-12-11 18:49:39 +0000116 return DRD_(trace_mem_access)(addr, size, eLoad, 0);
bart09dc13f2009-02-14 15:13:31 +0000117}
118
bart7826acb2011-12-11 18:49:39 +0000119static VG_REGPARM(3) void drd_trace_mem_store(const Addr addr,const SizeT size,
120 const HWord stored_value)
bart09dc13f2009-02-14 15:13:31 +0000121{
bart7826acb2011-12-11 18:49:39 +0000122 return DRD_(trace_mem_access)(addr, size, eStore, stored_value);
bart09dc13f2009-02-14 15:13:31 +0000123}
124
125static void drd_report_race(const Addr addr, const SizeT size,
126 const BmAccessTypeT access_type)
127{
bartbedfd232009-03-26 19:07:15 +0000128 DataRaceErrInfo drei;
bart09dc13f2009-02-14 15:13:31 +0000129
bartbedfd232009-03-26 19:07:15 +0000130 drei.tid = DRD_(thread_get_running_tid)();
131 drei.addr = addr;
132 drei.size = size;
133 drei.access_type = access_type;
134 VG_(maybe_record_error)(VG_(get_running_tid)(),
135 DataRaceErr,
136 VG_(get_IP)(VG_(get_running_tid)()),
bart74b2d972011-10-08 08:54:57 +0000137 "Conflicting access",
bartbedfd232009-03-26 19:07:15 +0000138 &drei);
bartf98a5692009-05-03 17:17:37 +0000139
140 if (s_first_race_only)
141 {
142 DRD_(start_suppression)(addr, addr + size, "first race only");
143 }
bart09dc13f2009-02-14 15:13:31 +0000144}
145
bart99edb292009-02-15 15:59:20 +0000146VG_REGPARM(2) void DRD_(trace_load)(Addr addr, SizeT size)
bart09dc13f2009-02-14 15:13:31 +0000147{
148#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000149 /* The assert below has been commented out because of performance reasons.*/
bartd5bbc612010-09-02 14:44:17 +0000150 tl_assert(DRD_(thread_get_running_tid)()
151 == DRD_(VgThreadIdToDrdThreadId)(VG_(get_running_tid())));
bart09dc13f2009-02-14 15:13:31 +0000152#endif
153
bartd45d9952009-05-31 18:53:54 +0000154 if (DRD_(running_thread_is_recording_loads)()
bartf98a5692009-05-03 17:17:37 +0000155 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000156 || ! DRD_(thread_address_on_stack)(addr))
157 && bm_access_load_triggers_conflict(addr, addr + size)
158 && ! DRD_(is_suppressed)(addr, addr + size))
159 {
160 drd_report_race(addr, size, eLoad);
161 }
bart09dc13f2009-02-14 15:13:31 +0000162}
163
164static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
165{
bartd45d9952009-05-31 18:53:54 +0000166 if (DRD_(running_thread_is_recording_loads)()
bartf98a5692009-05-03 17:17:37 +0000167 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000168 || ! DRD_(thread_address_on_stack)(addr))
169 && bm_access_load_1_triggers_conflict(addr)
170 && ! DRD_(is_suppressed)(addr, addr + 1))
171 {
172 drd_report_race(addr, 1, eLoad);
173 }
bart09dc13f2009-02-14 15:13:31 +0000174}
175
176static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
177{
bartd45d9952009-05-31 18:53:54 +0000178 if (DRD_(running_thread_is_recording_loads)()
bartf98a5692009-05-03 17:17:37 +0000179 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000180 || ! DRD_(thread_address_on_stack)(addr))
181 && bm_access_load_2_triggers_conflict(addr)
182 && ! DRD_(is_suppressed)(addr, addr + 2))
183 {
184 drd_report_race(addr, 2, eLoad);
185 }
bart09dc13f2009-02-14 15:13:31 +0000186}
187
188static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
189{
bartd45d9952009-05-31 18:53:54 +0000190 if (DRD_(running_thread_is_recording_loads)()
bartf98a5692009-05-03 17:17:37 +0000191 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000192 || ! DRD_(thread_address_on_stack)(addr))
193 && bm_access_load_4_triggers_conflict(addr)
194 && ! DRD_(is_suppressed)(addr, addr + 4))
195 {
196 drd_report_race(addr, 4, eLoad);
197 }
bart09dc13f2009-02-14 15:13:31 +0000198}
199
200static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
201{
bartd45d9952009-05-31 18:53:54 +0000202 if (DRD_(running_thread_is_recording_loads)()
bartf98a5692009-05-03 17:17:37 +0000203 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000204 || ! DRD_(thread_address_on_stack)(addr))
205 && bm_access_load_8_triggers_conflict(addr)
206 && ! DRD_(is_suppressed)(addr, addr + 8))
207 {
208 drd_report_race(addr, 8, eLoad);
209 }
bart09dc13f2009-02-14 15:13:31 +0000210}
211
bart99edb292009-02-15 15:59:20 +0000212VG_REGPARM(2) void DRD_(trace_store)(Addr addr, SizeT size)
bart09dc13f2009-02-14 15:13:31 +0000213{
214#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000215 /* The assert below has been commented out because of performance reasons.*/
bartd5bbc612010-09-02 14:44:17 +0000216 tl_assert(DRD_(thread_get_running_tid)()
217 == DRD_(VgThreadIdToDrdThreadId)(VG_(get_running_tid())));
bart09dc13f2009-02-14 15:13:31 +0000218#endif
219
bartd45d9952009-05-31 18:53:54 +0000220 if (DRD_(running_thread_is_recording_stores)()
bartf98a5692009-05-03 17:17:37 +0000221 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000222 || ! DRD_(thread_address_on_stack)(addr))
223 && bm_access_store_triggers_conflict(addr, addr + size)
224 && ! DRD_(is_suppressed)(addr, addr + size))
225 {
226 drd_report_race(addr, size, eStore);
227 }
bart09dc13f2009-02-14 15:13:31 +0000228}
229
230static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
231{
bartd45d9952009-05-31 18:53:54 +0000232 if (DRD_(running_thread_is_recording_stores)()
bartf98a5692009-05-03 17:17:37 +0000233 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000234 || ! DRD_(thread_address_on_stack)(addr))
235 && bm_access_store_1_triggers_conflict(addr)
236 && ! DRD_(is_suppressed)(addr, addr + 1))
237 {
238 drd_report_race(addr, 1, eStore);
239 }
bart09dc13f2009-02-14 15:13:31 +0000240}
241
242static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
243{
bartd45d9952009-05-31 18:53:54 +0000244 if (DRD_(running_thread_is_recording_stores)()
bartf98a5692009-05-03 17:17:37 +0000245 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000246 || ! DRD_(thread_address_on_stack)(addr))
247 && bm_access_store_2_triggers_conflict(addr)
248 && ! DRD_(is_suppressed)(addr, addr + 2))
249 {
250 drd_report_race(addr, 2, eStore);
251 }
bart09dc13f2009-02-14 15:13:31 +0000252}
253
254static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
255{
bartd45d9952009-05-31 18:53:54 +0000256 if (DRD_(running_thread_is_recording_stores)()
bartf98a5692009-05-03 17:17:37 +0000257 && (s_check_stack_accesses
bart71ce1322011-12-11 17:54:17 +0000258 || !DRD_(thread_address_on_stack)(addr))
bartbedfd232009-03-26 19:07:15 +0000259 && bm_access_store_4_triggers_conflict(addr)
bart71ce1322011-12-11 17:54:17 +0000260 && !DRD_(is_suppressed)(addr, addr + 4))
bartbedfd232009-03-26 19:07:15 +0000261 {
262 drd_report_race(addr, 4, eStore);
263 }
bart09dc13f2009-02-14 15:13:31 +0000264}
265
266static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
267{
bartd45d9952009-05-31 18:53:54 +0000268 if (DRD_(running_thread_is_recording_stores)()
bartf98a5692009-05-03 17:17:37 +0000269 && (s_check_stack_accesses
bartbedfd232009-03-26 19:07:15 +0000270 || ! DRD_(thread_address_on_stack)(addr))
271 && bm_access_store_8_triggers_conflict(addr)
272 && ! DRD_(is_suppressed)(addr, addr + 8))
273 {
274 drd_report_race(addr, 8, eStore);
275 }
bart09dc13f2009-02-14 15:13:31 +0000276}
277
278/**
279 * Return true if and only if addr_expr matches the pattern (SP) or
280 * <offset>(SP).
281 */
282static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr)
283{
bartbedfd232009-03-26 19:07:15 +0000284 Bool result = False;
bart09dc13f2009-02-14 15:13:31 +0000285
bartbedfd232009-03-26 19:07:15 +0000286 if (addr_expr->tag == Iex_RdTmp)
287 {
288 int i;
289 for (i = 0; i < bb->stmts_size; i++)
bart09dc13f2009-02-14 15:13:31 +0000290 {
bartbedfd232009-03-26 19:07:15 +0000291 if (bb->stmts[i]
292 && bb->stmts[i]->tag == Ist_WrTmp
293 && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp)
294 {
295 IRExpr* e = bb->stmts[i]->Ist.WrTmp.data;
296 if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET)
297 {
298 result = True;
299 }
bart09dc13f2009-02-14 15:13:31 +0000300
bartbedfd232009-03-26 19:07:15 +0000301 //ppIRExpr(e);
302 //VG_(printf)(" (%s)\n", result ? "True" : "False");
303 break;
304 }
bart09dc13f2009-02-14 15:13:31 +0000305 }
bartbedfd232009-03-26 19:07:15 +0000306 }
307 return result;
bart09dc13f2009-02-14 15:13:31 +0000308}
309
bartea692152011-12-11 20:17:57 +0000310static const IROp u_widen_irop[5][9] = {
bartb63dc782011-12-12 19:18:26 +0000311 [Ity_I1 - Ity_I1][4] = Iop_1Uto32,
312 [Ity_I1 - Ity_I1][8] = Iop_1Uto64,
313 [Ity_I8 - Ity_I1][4] = Iop_8Uto32,
314 [Ity_I8 - Ity_I1][8] = Iop_8Uto64,
315 [Ity_I16 - Ity_I1][4] = Iop_16Uto32,
316 [Ity_I16 - Ity_I1][8] = Iop_16Uto64,
317 [Ity_I32 - Ity_I1][8] = Iop_32Uto64,
bartea692152011-12-11 20:17:57 +0000318};
319
bartb63dc782011-12-12 19:18:26 +0000320/**
321 * Instrument the client code to trace a memory load (--trace-addr).
322 */
323static void instr_trace_mem_load(IRSB* const bb, IRExpr* const addr_expr,
324 const HWord size)
bartea692152011-12-11 20:17:57 +0000325{
bartb63dc782011-12-12 19:18:26 +0000326 addStmtToIRSB(bb,
327 IRStmt_Dirty(
328 unsafeIRDirty_0_N(/*regparms*/2,
329 "drd_trace_mem_load",
330 VG_(fnptr_to_fnentry)
331 (drd_trace_mem_load),
332 mkIRExprVec_2(addr_expr, mkIRExpr_HWord(size)))));
333}
334
335/**
336 * Instrument the client code to trace a memory store (--trace-addr).
337 */
338static void instr_trace_mem_store(IRSB* const bb, IRExpr* const addr_expr,
339 IRExpr* const data_expr)
340{
341 IRType ty_data_expr;
bartea692152011-12-11 20:17:57 +0000342 IRExpr *hword_data_expr;
343 HWord size;
344
bartb63dc782011-12-12 19:18:26 +0000345 tl_assert(sizeof(HWord) == 4 || sizeof(HWord) == 8);
bartea692152011-12-11 20:17:57 +0000346
bartb63dc782011-12-12 19:18:26 +0000347 ty_data_expr = typeOfIRExpr(bb->tyenv, data_expr);
348 size = sizeofIRType(ty_data_expr);
349
350 if (size == sizeof(HWord)
351 && (ty_data_expr == Ity_I32 || ty_data_expr == Ity_I64))
352 {
353 /* No conversion necessary */
bartea692152011-12-11 20:17:57 +0000354 hword_data_expr = data_expr;
355 } else {
356 IROp widen_op;
357
bartb63dc782011-12-12 19:18:26 +0000358 if (Ity_I1 <= ty_data_expr
359 && ty_data_expr
360 < Ity_I1 + sizeof(u_widen_irop)/sizeof(u_widen_irop[0]))
361 {
362 widen_op = u_widen_irop[ty_data_expr - Ity_I1][sizeof(HWord)];
bartea692152011-12-11 20:17:57 +0000363 if (!widen_op)
364 widen_op = Iop_INVALID;
365 } else {
366 widen_op = Iop_INVALID;
367 }
368 if (widen_op != Iop_INVALID) {
369 IRTemp tmp;
370
bartb63dc782011-12-12 19:18:26 +0000371 /* Widen the integer expression to a HWord */
bartea692152011-12-11 20:17:57 +0000372 tmp = newIRTemp(bb->tyenv, sizeof(HWord) == 4 ? Ity_I32 : Ity_I64);
373 addStmtToIRSB(bb,
374 IRStmt_WrTmp(tmp, IRExpr_Unop(widen_op, data_expr)));
375 hword_data_expr = IRExpr_RdTmp(tmp);
376 } else {
bartb63dc782011-12-12 19:18:26 +0000377 /*
378 * Replace anything wider than a HWord and also Ity_F32, Ity_F64,
379 * Ity_F128 and Ity_V128 by zero.
380 */
bartea692152011-12-11 20:17:57 +0000381 hword_data_expr = mkIRExpr_HWord(0);
382 }
383 }
384 addStmtToIRSB(bb,
385 IRStmt_Dirty(
386 unsafeIRDirty_0_N(/*regparms*/3,
387 "drd_trace_mem_store",
388 VG_(fnptr_to_fnentry)
389 (drd_trace_mem_store),
390 mkIRExprVec_3(addr_expr, mkIRExpr_HWord(size),
391 hword_data_expr))));
392}
393
394static void instrument_load(IRSB* const bb, IRExpr* const addr_expr,
bartb63dc782011-12-12 19:18:26 +0000395 const HWord size)
bart09dc13f2009-02-14 15:13:31 +0000396{
bartbedfd232009-03-26 19:07:15 +0000397 IRExpr* size_expr;
398 IRExpr** argv;
399 IRDirty* di;
bart09dc13f2009-02-14 15:13:31 +0000400
bartea692152011-12-11 20:17:57 +0000401 if (!s_check_stack_accesses && is_stack_access(bb, addr_expr))
bartbedfd232009-03-26 19:07:15 +0000402 return;
bart09dc13f2009-02-14 15:13:31 +0000403
bartbedfd232009-03-26 19:07:15 +0000404 switch (size)
405 {
406 case 1:
407 argv = mkIRExprVec_1(addr_expr);
408 di = unsafeIRDirty_0_N(/*regparms*/1,
409 "drd_trace_load_1",
410 VG_(fnptr_to_fnentry)(drd_trace_load_1),
411 argv);
412 break;
413 case 2:
414 argv = mkIRExprVec_1(addr_expr);
415 di = unsafeIRDirty_0_N(/*regparms*/1,
416 "drd_trace_load_2",
417 VG_(fnptr_to_fnentry)(drd_trace_load_2),
418 argv);
419 break;
420 case 4:
421 argv = mkIRExprVec_1(addr_expr);
422 di = unsafeIRDirty_0_N(/*regparms*/1,
423 "drd_trace_load_4",
424 VG_(fnptr_to_fnentry)(drd_trace_load_4),
425 argv);
426 break;
427 case 8:
428 argv = mkIRExprVec_1(addr_expr);
429 di = unsafeIRDirty_0_N(/*regparms*/1,
430 "drd_trace_load_8",
431 VG_(fnptr_to_fnentry)(drd_trace_load_8),
432 argv);
433 break;
434 default:
435 size_expr = mkIRExpr_HWord(size);
436 argv = mkIRExprVec_2(addr_expr, size_expr);
437 di = unsafeIRDirty_0_N(/*regparms*/2,
438 "drd_trace_load",
439 VG_(fnptr_to_fnentry)(DRD_(trace_load)),
440 argv);
441 break;
442 }
443 addStmtToIRSB(bb, IRStmt_Dirty(di));
bart09dc13f2009-02-14 15:13:31 +0000444}
445
bart7826acb2011-12-11 18:49:39 +0000446static void instrument_store(IRSB* const bb, IRExpr* const addr_expr,
447 IRExpr* const data_expr)
bart09dc13f2009-02-14 15:13:31 +0000448{
bartbedfd232009-03-26 19:07:15 +0000449 IRExpr* size_expr;
450 IRExpr** argv;
451 IRDirty* di;
bart7826acb2011-12-11 18:49:39 +0000452 HWord size;
453
454 size = sizeofIRType(typeOfIRExpr(bb->tyenv, data_expr));
bart09dc13f2009-02-14 15:13:31 +0000455
bartbedfd232009-03-26 19:07:15 +0000456 if (UNLIKELY(DRD_(any_address_is_traced)()))
bartb63dc782011-12-12 19:18:26 +0000457 instr_trace_mem_store(bb, addr_expr, data_expr);
bart09dc13f2009-02-14 15:13:31 +0000458
bart71ce1322011-12-11 17:54:17 +0000459 if (!s_check_stack_accesses && is_stack_access(bb, addr_expr))
bartbedfd232009-03-26 19:07:15 +0000460 return;
bart09dc13f2009-02-14 15:13:31 +0000461
bartbedfd232009-03-26 19:07:15 +0000462 switch (size)
463 {
464 case 1:
465 argv = mkIRExprVec_1(addr_expr);
466 di = unsafeIRDirty_0_N(/*regparms*/1,
467 "drd_trace_store_1",
468 VG_(fnptr_to_fnentry)(drd_trace_store_1),
469 argv);
470 break;
471 case 2:
472 argv = mkIRExprVec_1(addr_expr);
473 di = unsafeIRDirty_0_N(/*regparms*/1,
474 "drd_trace_store_2",
475 VG_(fnptr_to_fnentry)(drd_trace_store_2),
476 argv);
477 break;
478 case 4:
479 argv = mkIRExprVec_1(addr_expr);
480 di = unsafeIRDirty_0_N(/*regparms*/1,
481 "drd_trace_store_4",
482 VG_(fnptr_to_fnentry)(drd_trace_store_4),
483 argv);
484 break;
485 case 8:
486 argv = mkIRExprVec_1(addr_expr);
487 di = unsafeIRDirty_0_N(/*regparms*/1,
488 "drd_trace_store_8",
489 VG_(fnptr_to_fnentry)(drd_trace_store_8),
490 argv);
491 break;
492 default:
493 size_expr = mkIRExpr_HWord(size);
494 argv = mkIRExprVec_2(addr_expr, size_expr);
495 di = unsafeIRDirty_0_N(/*regparms*/2,
496 "drd_trace_store",
497 VG_(fnptr_to_fnentry)(DRD_(trace_store)),
498 argv);
499 break;
500 }
501 addStmtToIRSB(bb, IRStmt_Dirty(di));
bart09dc13f2009-02-14 15:13:31 +0000502}
503
bart1335ecc2009-02-14 16:10:53 +0000504IRSB* DRD_(instrument)(VgCallbackClosure* const closure,
bartbedfd232009-03-26 19:07:15 +0000505 IRSB* const bb_in,
506 VexGuestLayout* const layout,
bart31b983d2010-02-21 14:52:59 +0000507 VexGuestExtents* const vge,
bartbedfd232009-03-26 19:07:15 +0000508 IRType const gWordTy,
509 IRType const hWordTy)
bart09dc13f2009-02-14 15:13:31 +0000510{
bartbedfd232009-03-26 19:07:15 +0000511 IRDirty* di;
512 Int i;
513 IRSB* bb;
514 IRExpr** argv;
515 Bool instrument = True;
bart09dc13f2009-02-14 15:13:31 +0000516
bartbedfd232009-03-26 19:07:15 +0000517 /* Set up BB */
518 bb = emptyIRSB();
519 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
520 bb->next = deepCopyIRExpr(bb_in->next);
521 bb->jumpkind = bb_in->jumpkind;
bart09dc13f2009-02-14 15:13:31 +0000522
bartbedfd232009-03-26 19:07:15 +0000523 for (i = 0; i < bb_in->stmts_used; i++)
524 {
525 IRStmt* const st = bb_in->stmts[i];
526 tl_assert(st);
sewardjdb5907d2009-11-26 17:20:21 +0000527 tl_assert(isFlatIRStmt(st));
bart09dc13f2009-02-14 15:13:31 +0000528
bartbedfd232009-03-26 19:07:15 +0000529 switch (st->tag)
bart09dc13f2009-02-14 15:13:31 +0000530 {
bartbedfd232009-03-26 19:07:15 +0000531 /* Note: the code for not instrumenting the code in .plt */
532 /* sections is only necessary on CentOS 3.0 x86 (kernel 2.4.21 */
533 /* + glibc 2.3.2 + NPTL 0.60 + binutils 2.14.90.0.4). */
534 /* This is because on this platform dynamic library symbols are */
535 /* relocated in another way than by later binutils versions. The */
536 /* linker e.g. does not generate .got.plt sections on CentOS 3.0. */
537 case Ist_IMark:
sewardje3f1e592009-07-31 09:41:29 +0000538 instrument = VG_(DebugInfo_sect_kind)(NULL, 0, st->Ist.IMark.addr)
bartbedfd232009-03-26 19:07:15 +0000539 != Vg_SectPLT;
540 addStmtToIRSB(bb, st);
541 break;
542
543 case Ist_MBE:
544 switch (st->Ist.MBE.event)
545 {
546 case Imbe_Fence:
547 break; /* not interesting */
bartbedfd232009-03-26 19:07:15 +0000548 default:
549 tl_assert(0);
550 }
551 addStmtToIRSB(bb, st);
552 break;
553
554 case Ist_Store:
sewardjdb5907d2009-11-26 17:20:21 +0000555 if (instrument)
bart7826acb2011-12-11 18:49:39 +0000556 instrument_store(bb, st->Ist.Store.addr, st->Ist.Store.data);
bartbedfd232009-03-26 19:07:15 +0000557 addStmtToIRSB(bb, st);
558 break;
559
560 case Ist_WrTmp:
bartea692152011-12-11 20:17:57 +0000561 if (instrument) {
bartbedfd232009-03-26 19:07:15 +0000562 const IRExpr* const data = st->Ist.WrTmp.data;
bartb63dc782011-12-12 19:18:26 +0000563 if (data->tag == Iex_Load) {
564 if (UNLIKELY(DRD_(any_address_is_traced)()))
565 instr_trace_mem_load(bb, data->Iex.Load.addr,
566 sizeofIRType(data->Iex.Load.ty));
567
bartea692152011-12-11 20:17:57 +0000568 instrument_load(bb, data->Iex.Load.addr,
bartb63dc782011-12-12 19:18:26 +0000569 sizeofIRType(data->Iex.Load.ty));
570 }
bartbedfd232009-03-26 19:07:15 +0000571 }
572 addStmtToIRSB(bb, st);
573 break;
574
575 case Ist_Dirty:
bartb63dc782011-12-12 19:18:26 +0000576 if (instrument) {
bartbedfd232009-03-26 19:07:15 +0000577 IRDirty* d = st->Ist.Dirty.details;
578 IREffect const mFx = d->mFx;
579 switch (mFx) {
580 case Ifx_None:
581 break;
582 case Ifx_Read:
583 case Ifx_Write:
584 case Ifx_Modify:
585 tl_assert(d->mAddr);
586 tl_assert(d->mSize > 0);
587 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
588 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
589 di = unsafeIRDirty_0_N(
590 /*regparms*/2,
591 "drd_trace_load",
592 VG_(fnptr_to_fnentry)(DRD_(trace_load)),
593 argv);
594 addStmtToIRSB(bb, IRStmt_Dirty(di));
595 }
sewardj1c0ce7a2009-07-01 08:10:49 +0000596 if (mFx == Ifx_Write || mFx == Ifx_Modify)
bartbedfd232009-03-26 19:07:15 +0000597 {
598 di = unsafeIRDirty_0_N(
599 /*regparms*/2,
600 "drd_trace_store",
601 VG_(fnptr_to_fnentry)(DRD_(trace_store)),
602 argv);
603 addStmtToIRSB(bb, IRStmt_Dirty(di));
604 }
605 break;
606 default:
607 tl_assert(0);
608 }
609 }
610 addStmtToIRSB(bb, st);
611 break;
612
sewardj1c0ce7a2009-07-01 08:10:49 +0000613 case Ist_CAS:
bartb63dc782011-12-12 19:18:26 +0000614 if (instrument) {
barta14e3282009-07-11 14:35:59 +0000615 /*
616 * Treat compare-and-swap as a read. By handling atomic
617 * instructions as read instructions no data races are reported
618 * between conflicting atomic operations nor between atomic
619 * operations and non-atomic reads. Conflicts between atomic
620 * operations and non-atomic write operations are still reported
621 * however.
622 */
sewardj1c0ce7a2009-07-01 08:10:49 +0000623 Int dataSize;
624 IRCAS* cas = st->Ist.CAS.details;
bartb63dc782011-12-12 19:18:26 +0000625
sewardj1c0ce7a2009-07-01 08:10:49 +0000626 tl_assert(cas->addr != NULL);
627 tl_assert(cas->dataLo != NULL);
628 dataSize = sizeofIRType(typeOfIRExpr(bb->tyenv, cas->dataLo));
629 if (cas->dataHi != NULL)
630 dataSize *= 2; /* since it's a doubleword-CAS */
bartb63dc782011-12-12 19:18:26 +0000631
632 if (UNLIKELY(DRD_(any_address_is_traced)())) {
633 if (cas->dataHi) {
634 IRExpr* data_expr;
635
636 tl_assert(typeOfIRExpr(bb->tyenv, cas->dataLo) == Ity_I32);
637 data_expr
638 = IRExpr_Binop(
639 Iop_Or64,
640 IRExpr_Binop(
641 Iop_Shl64,
642 IRExpr_Unop(Iop_32Uto64, cas->dataHi),
643 mkIRExpr_HWord(32)),
644 IRExpr_Unop(Iop_32Uto64, cas->dataLo));
645 instr_trace_mem_store(bb, cas->addr, data_expr);
646 } else {
647 instr_trace_mem_store(bb, cas->addr, cas->dataLo);
648 }
649 }
650
651 instrument_load(bb, cas->addr, dataSize);
sewardj1c0ce7a2009-07-01 08:10:49 +0000652 }
653 addStmtToIRSB(bb, st);
654 break;
655
sewardjdb5907d2009-11-26 17:20:21 +0000656 case Ist_LLSC: {
bartea692152011-12-11 20:17:57 +0000657 /*
658 * Ignore store-conditionals (except for tracing), and handle
659 * load-linked's exactly like normal loads.
660 */
sewardjdb5907d2009-11-26 17:20:21 +0000661 IRType dataTy;
bartb63dc782011-12-12 19:18:26 +0000662
bartea692152011-12-11 20:17:57 +0000663 if (st->Ist.LLSC.storedata == NULL) {
sewardjdb5907d2009-11-26 17:20:21 +0000664 /* LL */
665 dataTy = typeOfIRTemp(bb_in->tyenv, st->Ist.LLSC.result);
bartb63dc782011-12-12 19:18:26 +0000666 if (instrument) {
667 if (UNLIKELY(DRD_(any_address_is_traced)()))
668 instr_trace_mem_load(bb, st->Ist.LLSC.addr,
669 sizeofIRType(dataTy));
670
671 instrument_load(bb, st->Ist.LLSC.addr, sizeofIRType(dataTy));
672 }
bartea692152011-12-11 20:17:57 +0000673 } else {
sewardjdb5907d2009-11-26 17:20:21 +0000674 /* SC */
bartb63dc782011-12-12 19:18:26 +0000675 instr_trace_mem_store(bb, st->Ist.LLSC.addr,
676 st->Ist.LLSC.storedata);
sewardjdb5907d2009-11-26 17:20:21 +0000677 }
bartbedfd232009-03-26 19:07:15 +0000678 addStmtToIRSB(bb, st);
679 break;
bart09dc13f2009-02-14 15:13:31 +0000680 }
sewardjdb5907d2009-11-26 17:20:21 +0000681
682 case Ist_NoOp:
683 case Ist_AbiHint:
684 case Ist_Put:
685 case Ist_PutI:
686 case Ist_Exit:
687 /* None of these can contain any memory references. */
688 addStmtToIRSB(bb, st);
689 break;
690
691 default:
692 ppIRStmt(st);
693 tl_assert(0);
694 }
bartbedfd232009-03-26 19:07:15 +0000695 }
bart09dc13f2009-02-14 15:13:31 +0000696
bartbedfd232009-03-26 19:07:15 +0000697 return bb;
bart09dc13f2009-02-14 15:13:31 +0000698}
699