blob: e479e6acb99de6b0a852569269104a57edee90b9 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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 "pub_tool_basics.h" // Addr, SizeT
27#include "pub_tool_debuginfo.h" // VG_(get_objname)()
28#include "pub_tool_libcassert.h" // tl_assert()
29#include "pub_tool_libcbase.h" // VG_(memset)
30#include "pub_tool_libcprint.h" // VG_(printf)
31#include "pub_tool_machine.h" // VG_(get_IP)()
32#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
33#include "pub_drd_bitmap.h"
34#include "drd_bitmap.h"
35#include "drd_error.h"
36#include "drd_suppression.h"
37
38
39// Local constants.
40
41static ULong s_bitmap_creation_count;
42
43
44// Local function declarations.
45
46static void bm2_merge(struct bitmap2* const bm2l,
47 const struct bitmap2* const bm2r);
48
49
50// Function definitions.
51
52struct bitmap* bm_new()
53{
54 struct bitmap* bm;
55
56 // If this assert fails, fix the definition of BITS_PER_BITS_PER_UWORD
57 // in drd_bitmap.h.
58 tl_assert((1 << BITS_PER_BITS_PER_UWORD) == BITS_PER_UWORD);
59
60 bm = VG_(malloc)(sizeof(*bm));
61 tl_assert(bm);
62 bm->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
63
64 s_bitmap_creation_count++;
65
66 return bm;
67}
68
69void bm_delete(struct bitmap* const bm)
70{
71 tl_assert(bm);
72 VG_(OSetGen_Destroy)(bm->oset);
73 VG_(free)(bm);
74}
75
76/**
77 * Record an access of type access_type at addresses a in bitmap bm.
78 */
79static
80__inline__
81void bm_access_1(struct bitmap* const bm,
82 const Addr a,
83 const BmAccessTypeT access_type)
84{
85 struct bitmap2* p2;
86 struct bitmap1* p1;
87 UWord* p0;
88 SPLIT_ADDRESS(a);
89
90 tl_assert(bm);
91
92 p2 = bm2_lookup_or_insert(bm, a1);
93 p1 = &p2->bm1;
94 p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w;
95 bm0_set(p0, a0);
96}
97
98static
99void bm_access_4_nonaligned(struct bitmap* const bm,
100 const Addr a,
101 const BmAccessTypeT access_type)
102{
103 bm_access_1(bm, a + 0, access_type);
104 bm_access_1(bm, a + 1, access_type);
105 bm_access_1(bm, a + 2, access_type);
106 bm_access_1(bm, a + 3, access_type);
107}
108
109static
110__inline__
111void bm_access_4_aligned(struct bitmap* const bm,
112 const Addr a,
113 const BmAccessTypeT access_type)
114{
115 struct bitmap2* p2;
116 struct bitmap1* p1;
117 UWord* p0;
118 SPLIT_ADDRESS(a);
119
120 tl_assert(bm);
121
122 p2 = bm2_lookup_or_insert(bm, a1);
123 p1 = &p2->bm1;
124 p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w;
125 bm0_set(p0, a0+0);
126 bm0_set(p0, a0+1);
127 bm0_set(p0, a0+2);
128 bm0_set(p0, a0+3);
129}
130
131/**
132 * Record an access of type access_type at addresses a .. a + 3 in bitmap bm.
133 */
134void bm_access_4(struct bitmap* const bm,
135 const Addr a,
136 const BmAccessTypeT access_type)
137{
138 tl_assert(bm);
139 if ((a & 3) != 0)
140 {
141 bm_access_4_nonaligned(bm, a, access_type);
142 }
143 else
144 {
145 bm_access_4_aligned(bm, a, access_type);
146 }
147}
148
149/**
150 * Record an access of type access_type at addresses a .. a + size - 1 in
151 * bitmap bm.
152 */
153void bm_access_range(struct bitmap* const bm,
154 const Addr a,
155 const SizeT size,
156 const BmAccessTypeT access_type)
157{
158 tl_assert(bm);
159 tl_assert(size > 0);
160
161 if (size == 4)
162 bm_access_4(bm, a, access_type);
163 else if (size == 1)
164 bm_access_1(bm, a, access_type);
165 else
166 {
167 Addr b;
168 for (b = a; b != a + size; b++)
169 {
170 bm_access_1(bm, b, access_type);
171 }
172 }
173}
174
175Bool bm_has(const struct bitmap* const bm,
176 const Addr a1,
177 const Addr a2,
178 const BmAccessTypeT access_type)
179{
180 Addr b;
181 for (b = a1; b < a2; b++)
182 {
183 if (! bm_has_1(bm, b, access_type))
184 {
185 return False;
186 }
187 }
188 return True;
189}
190
191Bool bm_has_any(const struct bitmap* const bm,
192 const Addr a1,
193 const Addr a2,
194 const BmAccessTypeT access_type)
195{
196 Addr b;
197
198 tl_assert(bm);
199
200 for (b = a1; b < a2; b++)
201 {
202 if (bm_has_1(bm, b, access_type))
203 {
204 return True;
205 }
206 }
207 return False;
208}
209
210/* Return a non-zero value if there is a read access, write access or both */
211/* to any of the addresses in the range [ a1, a2 [ in bitmap bm. */
212UWord bm_has_any_access(const struct bitmap* const bm,
213 const Addr a1,
214 const Addr a2)
215{
216 Addr b, b_next;
217
218 tl_assert(bm);
219
220 for (b = a1; b < a2; b = b_next)
221 {
222 struct bitmap2* bm2 = bm_lookup(bm, b);
223
224 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
225 if (b_next > a2)
226 {
227 b_next = a2;
228 }
229
230 if (bm2)
231 {
232 Addr b_start;
233 Addr b_end;
234 UWord b0;
235
236 if ((bm2->addr << ADDR0_BITS) < a1)
237 b_start = a1;
238 else
239 if ((bm2->addr << ADDR0_BITS) < a2)
240 b_start = (bm2->addr << ADDR0_BITS);
241 else
242 break;
243 tl_assert(a1 <= b_start && b_start <= a2);
244
245 if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2)
246 b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT;
247 else
248 b_end = a2;
249#if 0
250 VG_(message)(Vg_DebugMsg,
251 "in 0x%lx 0x%lx / cur 0x%lx 0x%lx / out 0x%lx 0x%lx",
252 a1, a2,
253 (bm2->addr << ADDR0_BITS),
254 (bm2->addr << ADDR0_BITS) + ADDR0_COUNT,
255 b_start, b_end);
256#endif
257 tl_assert(a1 <= b_end && b_end <= a2);
258 tl_assert(b_start < b_end);
259 tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK));
260
261 for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++)
262 {
263 const struct bitmap1* const p1 = &bm2->bm1;
264 const UWord mask
265 = bm0_is_set(p1->bm0_r, b0) | bm0_is_set(p1->bm0_w, b0);
266 if (mask)
267 {
268 return mask;
269 }
270 }
271 }
272 }
273 return 0;
274}
275
276/**
277 * Report whether an access of type access_type at address a is recorded in
278 * bitmap bm.
279 * @return != 0 means true, and == 0 means false
280 */
281UWord bm_has_1(const struct bitmap* const bm,
282 const Addr a,
283 const BmAccessTypeT access_type)
284{
285 struct bitmap2* p2;
286 struct bitmap1* p1;
287 UWord* p0;
288 const UWord a0 = a & ADDR0_MASK;
289
290 tl_assert(bm);
291
292 p2 = bm_lookup(bm, a);
293 if (p2)
294 {
295 p1 = &p2->bm1;
296 p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w;
297 return bm0_is_set(p0, a0);
298 }
299 return 0;
300}
301
302static __inline__
303void bm1_clear(struct bitmap1* const bm1, const Addr a1, const Addr a2)
304{
305 UWord idx;
306 UWord mask;
307
308#if 0
309 // Commented out the assert statements below because of performance reasons.
310 tl_assert(a1);
311 tl_assert(a1 <= a2);
312 tl_assert(UWORD_MSB(a1) == UWORD_MSB(a2)
313 || UWORD_MSB(a1) == UWORD_MSB(a2 - 1));
314#endif
315
316 idx = (a1 & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD;
317 /* mask: a contiguous series of one bits. The first bit set is bit */
318 /* UWORD_LSB(a2-1), and the last bit set is UWORD_LSB(a1). */
319 mask = UWORD_LSB(a2) ? bm0_mask(a2) - bm0_mask(a1) : - bm0_mask(a1);
320 bm1->bm0_r[idx] &= ~mask;
321 bm1->bm0_w[idx] &= ~mask;
322}
323
324void bm_clear_all(const struct bitmap* const bm)
325{
326 struct bitmap2* bm2;
327
328 VG_(OSetGen_ResetIter)(bm->oset);
329
330 for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; )
331 {
332 struct bitmap1* const bm1 = &bm2->bm1;
333 tl_assert(bm1);
334 VG_(memset)(&bm1->bm0_r[0], 0, sizeof(bm1->bm0_r));
335 VG_(memset)(&bm1->bm0_w[0], 0, sizeof(bm1->bm0_w));
336 }
337}
338
sewardjaf44c822007-11-25 14:01:38 +0000339// New and fast implementation.
340void bm_clear(const struct bitmap* const bm,
341 const Addr a1,
342 const Addr a2)
343{
344 Addr b, b_next;
345
346 tl_assert(bm);
347 tl_assert(a1);
348 tl_assert(a1 <= a2);
349
350 for (b = a1; b < a2; b = b_next)
351 {
352 struct bitmap2* const p2 = bm_lookup(bm, b);
353
354 b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT;
355 if (b_next > a2)
356 {
357 b_next = a2;
358 }
359
360 if (p2)
361 {
362 Addr c = b;
363 if (UWORD_LSB(c))
364 {
365 Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD;
366 if (c_next > b_next)
367 c_next = b_next;
368 bm1_clear(&p2->bm1, c, c_next);
369 c = c_next;
370 }
371 if (UWORD_LSB(c) == 0)
372 {
373 const Addr c_next = UWORD_MSB(b_next);
374 tl_assert(UWORD_LSB(c) == 0);
375 tl_assert(UWORD_LSB(c_next) == 0);
376 tl_assert(c_next <= b_next);
377 tl_assert(c <= c_next);
378 if (c_next > c)
379 {
380 UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD;
381 VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8);
382 VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8);
383 c = c_next;
384 }
385 }
386 if (c != b_next)
387 {
388 bm1_clear(&p2->bm1, c, b_next);
389 }
390 }
391 }
392}
sewardjaf44c822007-11-25 14:01:38 +0000393
394static
395__inline__
396UWord bm_has_conflict_with_1(const struct bitmap* const bm,
397 const Addr a,
398 const BmAccessTypeT access_type)
399{
400 struct bitmap2* p2;
401 const UWord a0 = a & ADDR0_MASK;
402
403 tl_assert(bm);
404
405 p2 = bm_lookup(bm, a);
406 if (p2)
407 {
408 if (access_type == eLoad)
409 {
410 return bm0_is_set(p2->bm1.bm0_w, a0);
411 }
412 else
413 {
414 tl_assert(access_type == eStore);
415 return (bm0_is_set(p2->bm1.bm0_r, a0)
416 | bm0_is_set(p2->bm1.bm0_w, a0));
417 }
418 }
419 return False;
420}
421
422/**
423 * Return true if the access to [a,a+size[ of type access_type conflicts with
424 * any access stored in bitmap bm.
425 */
426Bool bm_has_conflict_with(const struct bitmap* const bm,
427 const Addr a1,
428 const Addr a2,
429 const BmAccessTypeT access_type)
430{
431 Addr b;
432 for (b = a1; b != a2; b++)
433 {
434 if (bm_has_conflict_with_1(bm, b, access_type))
435 {
436 return True;
437 }
438 }
439 return False;
440}
441
442void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2)
443{
444 OSet* const tmp = bm1->oset;
445 bm1->oset = bm2->oset;
446 bm2->oset = tmp;
447}
448
449void bm_merge2(struct bitmap* const lhs,
450 const struct bitmap* const rhs)
451{
452 struct bitmap2* bm2l;
453 const struct bitmap2* bm2r;
454
455 // First step: allocate any missing bitmaps in *lhs.
456 VG_(OSetGen_ResetIter)(rhs->oset);
457 for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; )
458 {
459 bm2_lookup_or_insert(lhs, bm2r->addr);
460 }
461
462 VG_(OSetGen_ResetIter)(lhs->oset);
463 VG_(OSetGen_ResetIter)(rhs->oset);
464
465 for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; )
466 {
467 do
468 {
469 bm2l = VG_(OSetGen_Next)(lhs->oset);
470 //VG_(message)(Vg_DebugMsg, "0x%x 0x%x", bm2l->addr, bm2r->addr);
471 } while (bm2l->addr < bm2r->addr);
472
473 tl_assert(bm2l->addr == bm2r->addr);
474
475 bm2_merge(bm2l, bm2r);
476 }
477}
478
479/**
480 * Report whether there are any RW / WR / WW patterns in lhs and rhs.
481 * @param lhs First bitmap.
482 * @param rhs Bitmap to be compared with lhs.
483 * @return !=0 if there are data races, == 0 if there are none.
484 */
485int bm_has_races(const struct bitmap* const lhs,
486 const struct bitmap* const rhs)
487{
488 VG_(OSetGen_ResetIter)(lhs->oset);
489 VG_(OSetGen_ResetIter)(rhs->oset);
490
491 for (;;)
492 {
493 const struct bitmap2* bm2l = VG_(OSetGen_Next)(lhs->oset);
494 const struct bitmap2* bm2r = VG_(OSetGen_Next)(rhs->oset);
495 const struct bitmap1* bm1l;
496 const struct bitmap1* bm1r;
497 unsigned k;
498
499 while (bm2l && bm2r && bm2l->addr != bm2r->addr)
500 {
501 if (bm2l->addr < bm2r->addr)
502 bm2l = VG_(OSetGen_Next)(lhs->oset);
503 else
504 bm2r = VG_(OSetGen_Next)(rhs->oset);
505 }
506 if (bm2l == 0 || bm2r == 0)
507 break;
508
509 bm1l = &bm2l->bm1;
510 bm1r = &bm2r->bm1;
511
512 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
513 {
514 unsigned b;
515 for (b = 0; b < BITS_PER_UWORD; b++)
516 {
517 UWord const access
518 = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0)
519 | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0)
520 | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0)
521 | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0);
522 Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b);
523 if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1))
524 {
525 return 1;
526 }
527 }
528 }
529 }
530 return 0;
531}
532
sewardjaf44c822007-11-25 14:01:38 +0000533void bm_print(const struct bitmap* const bm)
534{
535 struct bitmap2* bm2;
536
537 VG_(OSetGen_ResetIter)(bm->oset);
538
539 for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; )
540 {
541 const struct bitmap1* const bm1 = &bm2->bm1;
542 unsigned k;
543 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
544 {
545 unsigned b;
546 for (b = 0; b < BITS_PER_UWORD; b++)
547 {
548 int const r = bm1->bm0_r[k] & bm0_mask(b);
549 int const w = bm1->bm0_w[k] & bm0_mask(b);
550 Addr const a = MAKE_ADDRESS(bm2->addr, k * BITS_PER_UWORD | b);
551 if (r || w)
552 {
553 VG_(printf)("0x%08lx %c %c\n",
554 (Addr)(a),
555 w ? 'W' : ' ', r ? 'R' : ' ');
556 }
557 }
558 }
559 }
560}
561
562ULong bm_get_bitmap_creation_count(void)
563{
564 return s_bitmap_creation_count;
565}
566
567ULong bm_get_bitmap2_creation_count(void)
568{
569 return s_bitmap2_creation_count;
570}
571
572static void bm2_merge(struct bitmap2* const bm2l,
573 const struct bitmap2* const bm2r)
574{
575 unsigned k;
576
577 tl_assert(bm2l->addr == bm2r->addr);
578
579 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
580 {
581 bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k];
582 }
583 for (k = 0; k < BITMAP1_UWORD_COUNT; k++)
584 {
585 bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k];
586 }
587}
588
589#if 0
590
591/* Unit test */
592static
593struct { Addr address; SizeT size; BmAccessTypeT access_type; }
594 s_args[] = {
595 { 0, 1, eLoad },
596 { 666, 4, eLoad },
597 { 667, 2, eStore },
598 { 1024, 1, eStore },
599 { 0x0000ffff, 1, eLoad },
600 { 0x0001ffff, 1, eLoad },
601 { 0x00ffffff, 1, eLoad },
602 { 0xffffffff, 1, eStore },
603 };
604
605void bm_test(void)
606{
607 struct bitmap* bm;
608 struct bitmap* bm2;
609 int i, j;
610
611 VG_(printf)("Start of DRD BM unit test.\n");
612
613 bm = bm_new();
614
615 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
616 {
617 bm_access_range(bm, s_args[i].address,
618 s_args[i].size, s_args[i].access_type);
619 }
620
621 VG_(printf)("Map contents -- should contain 10 addresses:\n");
622 bm_print(bm);
623
624 for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++)
625 {
626 for (j = 0; j < s_args[i].size; j++)
627 {
628 tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type));
629 }
630 }
631
632 VG_(printf)("Merge result:\n");
633 bm2 = bm_merge(bm, bm);
634 bm_print(bm);
635
636 bm_delete(bm);
637 bm_delete(bm2);
638
639 VG_(printf)("End of DRD BM unit test.\n");
640}
641#endif
642
643
644/*
645 * Local variables:
646 * c-basic-offset: 3
647 * End:
648 */