~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to memcheck/mc_main.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrés Roldán
  • Date: 2008-06-13 02:31:40 UTC
  • mto: (1.4.1 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20080613023140-iwk33rz9rhvfkr96
Import upstream version 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
   This file is part of MemCheck, a heavyweight Valgrind tool for
10
10
   detecting memory errors.
11
11
 
12
 
   Copyright (C) 2000-2006 Julian Seward 
 
12
   Copyright (C) 2000-2007 Julian Seward 
13
13
      jseward@acm.org
14
14
 
15
15
   This program is free software; you can redistribute it and/or
43
43
#include "pub_tool_replacemalloc.h"
44
44
#include "pub_tool_tooliface.h"
45
45
#include "pub_tool_threadstate.h"
 
46
#include "pub_tool_oset.h"
46
47
 
47
48
#include "mc_include.h"
48
49
#include "memcheck.h"   /* for client requests */
305
306
static Int   max_defined_SMs   = 0;
306
307
static Int   max_non_DSM_SMs   = 0;
307
308
 
308
 
static ULong n_auxmap_searches  = 0;
309
 
static ULong n_auxmap_cmps      = 0;
 
309
/* # searches initiated in auxmap_L1, and # base cmps required */
 
310
static ULong n_auxmap_L1_searches  = 0;
 
311
static ULong n_auxmap_L1_cmps      = 0;
 
312
/* # of searches that missed in auxmap_L1 and therefore had to
 
313
   be handed to auxmap_L2. And the number of nodes inserted. */
 
314
static ULong n_auxmap_L2_searches  = 0;
 
315
static ULong n_auxmap_L2_nodes     = 0;
 
316
 
310
317
static Int   n_sanity_cheap     = 0;
311
318
static Int   n_sanity_expensive = 0;
312
319
 
345
352
/* An entry in the auxiliary primary map.  base must be a 64k-aligned
346
353
   value, and sm points at the relevant secondary map.  As with the
347
354
   main primary map, the secondary may be either a real secondary, or
348
 
   one of the three distinguished secondaries.
 
355
   one of the three distinguished secondaries.  DO NOT CHANGE THIS
 
356
   LAYOUT: the first word has to be the key for OSet fast lookups.
349
357
*/
350
358
typedef
351
359
   struct { 
354
362
   }
355
363
   AuxMapEnt;
356
364
 
357
 
/* An expanding array of AuxMapEnts. */
358
 
#define N_AUXMAPS 20000 /* HACK */
359
 
static AuxMapEnt  hacky_auxmaps[N_AUXMAPS];
360
 
static Int        auxmap_size = N_AUXMAPS;
361
 
static Int        auxmap_used = 0;
362
 
static AuxMapEnt* auxmap      = &hacky_auxmaps[0];
363
 
 
364
 
 
365
 
/* Find an entry in the auxiliary map.  If an entry is found, move it
366
 
   one step closer to the front of the array, then return its address.
367
 
   If an entry is not found, return NULL.  Note carefully that
368
 
   because a each call potentially rearranges the entries, each call
369
 
   to this function invalidates ALL AuxMapEnt*s previously obtained by
370
 
   calling this fn.  
371
 
*/
372
 
static AuxMapEnt* maybe_find_in_auxmap ( Addr a )
373
 
{
374
 
   UWord i;
 
365
/* Tunable parameter: How big is the L1 queue? */
 
366
#define N_AUXMAP_L1 24
 
367
 
 
368
/* Tunable parameter: How far along the L1 queue to insert
 
369
   entries resulting from L2 lookups? */
 
370
#define AUXMAP_L1_INSERT_IX 12
 
371
 
 
372
static struct {
 
373
          Addr       base;
 
374
          AuxMapEnt* ent; // pointer to the matching auxmap_L2 node
 
375
       } 
 
376
       auxmap_L1[N_AUXMAP_L1];
 
377
 
 
378
static OSet* auxmap_L2 = NULL;
 
379
 
 
380
static void init_auxmap_L1_L2 ( void )
 
381
{
 
382
   Int i;
 
383
   for (i = 0; i < N_AUXMAP_L1; i++) {
 
384
      auxmap_L1[i].base = 0;
 
385
      auxmap_L1[i].ent  = NULL;
 
386
   }
 
387
 
 
388
   tl_assert(0 == offsetof(AuxMapEnt,base));
 
389
   tl_assert(sizeof(Addr) == sizeof(void*));
 
390
   auxmap_L2 = VG_(OSetGen_Create)( /*keyOff*/  offsetof(AuxMapEnt,base),
 
391
                                    /*fastCmp*/ NULL,
 
392
                                    VG_(malloc), VG_(free) );
 
393
}
 
394
 
 
395
/* Check representation invariants; if OK return NULL; else a
 
396
   descriptive bit of text.  Also return the number of
 
397
   non-distinguished secondary maps referred to from the auxiliary
 
398
   primary maps. */
 
399
 
 
400
static HChar* check_auxmap_L1_L2_sanity ( Word* n_secmaps_found )
 
401
{
 
402
   Word i, j;
 
403
   /* On a 32-bit platform, the L2 and L1 tables should
 
404
      both remain empty forever.
 
405
 
 
406
      On a 64-bit platform:
 
407
      In the L2 table:
 
408
       all .base & 0xFFFF == 0
 
409
       all .base > MAX_PRIMARY_ADDRESS
 
410
      In the L1 table:
 
411
       all .base & 0xFFFF == 0
 
412
       all (.base > MAX_PRIMARY_ADDRESS
 
413
            .base & 0xFFFF == 0
 
414
            and .ent points to an AuxMapEnt with the same .base)
 
415
           or
 
416
           (.base == 0 and .ent == NULL)
 
417
   */
 
418
   *n_secmaps_found = 0;
 
419
   if (sizeof(void*) == 4) {
 
420
      /* 32-bit platform */
 
421
      if (VG_(OSetGen_Size)(auxmap_L2) != 0)
 
422
         return "32-bit: auxmap_L2 is non-empty";
 
423
      for (i = 0; i < N_AUXMAP_L1; i++) 
 
424
        if (auxmap_L1[i].base != 0 || auxmap_L1[i].ent != NULL)
 
425
      return "32-bit: auxmap_L1 is non-empty";
 
426
   } else {
 
427
      /* 64-bit platform */
 
428
      UWord elems_seen = 0;
 
429
      AuxMapEnt *elem, *res;
 
430
      AuxMapEnt key;
 
431
      /* L2 table */
 
432
      VG_(OSetGen_ResetIter)(auxmap_L2);
 
433
      while ( (elem = VG_(OSetGen_Next)(auxmap_L2)) ) {
 
434
         elems_seen++;
 
435
         if (0 != (elem->base & (Addr)0xFFFF))
 
436
            return "64-bit: nonzero .base & 0xFFFF in auxmap_L2";
 
437
         if (elem->base <= MAX_PRIMARY_ADDRESS)
 
438
            return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L2";
 
439
         if (elem->sm == NULL)
 
440
            return "64-bit: .sm in _L2 is NULL";
 
441
         if (!is_distinguished_sm(elem->sm))
 
442
            (*n_secmaps_found)++;
 
443
      }
 
444
      if (elems_seen != n_auxmap_L2_nodes)
 
445
         return "64-bit: disagreement on number of elems in _L2";
 
446
      /* Check L1-L2 correspondence */
 
447
      for (i = 0; i < N_AUXMAP_L1; i++) {
 
448
         if (auxmap_L1[i].base == 0 && auxmap_L1[i].ent == NULL)
 
449
            continue;
 
450
         if (0 != (auxmap_L1[i].base & (Addr)0xFFFF))
 
451
            return "64-bit: nonzero .base & 0xFFFF in auxmap_L1";
 
452
         if (auxmap_L1[i].base <= MAX_PRIMARY_ADDRESS)
 
453
            return "64-bit: .base <= MAX_PRIMARY_ADDRESS in auxmap_L1";
 
454
         if (auxmap_L1[i].ent == NULL)
 
455
            return "64-bit: .ent is NULL in auxmap_L1";
 
456
         if (auxmap_L1[i].ent->base != auxmap_L1[i].base)
 
457
            return "64-bit: _L1 and _L2 bases are inconsistent";
 
458
         /* Look it up in auxmap_L2. */
 
459
         key.base = auxmap_L1[i].base;
 
460
         key.sm   = 0;
 
461
         res = VG_(OSetGen_Lookup)(auxmap_L2, &key);
 
462
         if (res == NULL)
 
463
            return "64-bit: _L1 .base not found in _L2";
 
464
         if (res != auxmap_L1[i].ent)
 
465
            return "64-bit: _L1 .ent disagrees with _L2 entry";
 
466
      }
 
467
      /* Check L1 contains no duplicates */
 
468
      for (i = 0; i < N_AUXMAP_L1; i++) {
 
469
         if (auxmap_L1[i].base == 0)
 
470
            continue;
 
471
         for (j = i+1; j < N_AUXMAP_L1; j++) {
 
472
            if (auxmap_L1[j].base == 0)
 
473
               continue;
 
474
            if (auxmap_L1[j].base == auxmap_L1[i].base)
 
475
               return "64-bit: duplicate _L1 .base entries";
 
476
         }
 
477
      }
 
478
   }
 
479
   return NULL; /* ok */
 
480
}
 
481
 
 
482
static void insert_into_auxmap_L1_at ( Word rank, AuxMapEnt* ent )
 
483
{
 
484
   Word i;
 
485
   tl_assert(ent);
 
486
   tl_assert(rank >= 0 && rank < N_AUXMAP_L1);
 
487
   for (i = N_AUXMAP_L1-1; i > rank; i--)
 
488
      auxmap_L1[i] = auxmap_L1[i-1];
 
489
   auxmap_L1[rank].base = ent->base;
 
490
   auxmap_L1[rank].ent  = ent;
 
491
}
 
492
 
 
493
static INLINE AuxMapEnt* maybe_find_in_auxmap ( Addr a )
 
494
{
 
495
   AuxMapEnt  key;
 
496
   AuxMapEnt* res;
 
497
   Word       i;
 
498
 
375
499
   tl_assert(a > MAX_PRIMARY_ADDRESS);
376
 
 
377
500
   a &= ~(Addr)0xFFFF;
378
501
 
379
 
   /* Search .. */
380
 
   n_auxmap_searches++;
381
 
   for (i = 0; i < auxmap_used; i++) {
382
 
      if (auxmap[i].base == a)
 
502
   /* First search the front-cache, which is a self-organising
 
503
      list containing the most popular entries. */
 
504
 
 
505
   if (EXPECTED_TAKEN(auxmap_L1[0].base == a))
 
506
      return auxmap_L1[0].ent;
 
507
   if (EXPECTED_TAKEN(auxmap_L1[1].base == a)) {
 
508
      Addr       t_base = auxmap_L1[0].base;
 
509
      AuxMapEnt* t_ent  = auxmap_L1[0].ent;
 
510
      auxmap_L1[0].base = auxmap_L1[1].base;
 
511
      auxmap_L1[0].ent  = auxmap_L1[1].ent;
 
512
      auxmap_L1[1].base = t_base;
 
513
      auxmap_L1[1].ent  = t_ent;
 
514
      return auxmap_L1[0].ent;
 
515
   }
 
516
 
 
517
   n_auxmap_L1_searches++;
 
518
 
 
519
   for (i = 0; i < N_AUXMAP_L1; i++) {
 
520
      if (auxmap_L1[i].base == a) {
383
521
         break;
 
522
      }
384
523
   }
385
 
   n_auxmap_cmps += (ULong)(i+1);
386
 
 
387
 
   if (i < auxmap_used) {
388
 
      /* Found it.  Nudge it a bit closer to the front. */
 
524
   tl_assert(i >= 0 && i <= N_AUXMAP_L1);
 
525
 
 
526
   n_auxmap_L1_cmps += (ULong)(i+1);
 
527
 
 
528
   if (i < N_AUXMAP_L1) {
389
529
      if (i > 0) {
390
 
         AuxMapEnt tmp = auxmap[i-1];
391
 
         auxmap[i-1] = auxmap[i];
392
 
         auxmap[i] = tmp;
 
530
         Addr       t_base = auxmap_L1[i-1].base;
 
531
         AuxMapEnt* t_ent  = auxmap_L1[i-1].ent;
 
532
         auxmap_L1[i-1].base = auxmap_L1[i-0].base;
 
533
         auxmap_L1[i-1].ent  = auxmap_L1[i-0].ent;
 
534
         auxmap_L1[i-0].base = t_base;
 
535
         auxmap_L1[i-0].ent  = t_ent;
393
536
         i--;
394
537
      }
395
 
      return &auxmap[i];
 
538
      return auxmap_L1[i].ent;
396
539
   }
397
540
 
398
 
   return NULL;
 
541
   n_auxmap_L2_searches++;
 
542
 
 
543
   /* First see if we already have it. */
 
544
   key.base = a;
 
545
   key.sm   = 0;
 
546
 
 
547
   res = VG_(OSetGen_Lookup)(auxmap_L2, &key);
 
548
   if (res)
 
549
      insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, res );
 
550
   return res;
399
551
}
400
552
 
401
 
 
402
 
/* Find an entry in the auxiliary map.  If an entry is found, move it
403
 
   one step closer to the front of the array, then return its address.
404
 
   If an entry is not found, allocate one.  Note carefully that
405
 
   because a each call potentially rearranges the entries, each call
406
 
   to this function invalidates ALL AuxMapEnt*s previously obtained by
407
 
   calling this fn.  
408
 
*/
409
553
static AuxMapEnt* find_or_alloc_in_auxmap ( Addr a )
410
554
{
411
 
   AuxMapEnt* am = maybe_find_in_auxmap(a);
412
 
   if (am)
413
 
      return am;
414
 
 
415
 
   /* We didn't find it.  Hmm.  This is a new piece of address space.
416
 
      We'll need to allocate a new AuxMap entry for it. */
417
 
   if (auxmap_used >= auxmap_size) {
418
 
      tl_assert(auxmap_used == auxmap_size);
419
 
      /* Out of auxmap entries. */
420
 
      tl_assert2(0, "failed to expand the auxmap table");
421
 
   }
422
 
 
423
 
   tl_assert(auxmap_used < auxmap_size);
424
 
 
425
 
   auxmap[auxmap_used].base = a & ~(Addr)0xFFFF;
426
 
   auxmap[auxmap_used].sm   = &sm_distinguished[SM_DIST_NOACCESS];
427
 
 
428
 
   if (0)
429
 
      VG_(printf)("new auxmap, base = 0x%llx\n", 
430
 
                  (ULong)auxmap[auxmap_used].base );
431
 
 
432
 
   auxmap_used++;
433
 
   return &auxmap[auxmap_used-1];
 
555
   AuxMapEnt *nyu, *res;
 
556
 
 
557
   /* First see if we already have it. */
 
558
   res = maybe_find_in_auxmap( a );
 
559
   if (EXPECTED_TAKEN(res))
 
560
      return res;
 
561
 
 
562
   /* Ok, there's no entry in the secondary map, so we'll have
 
563
      to allocate one. */
 
564
   a &= ~(Addr)0xFFFF;
 
565
 
 
566
   nyu = (AuxMapEnt*) VG_(OSetGen_AllocNode)( auxmap_L2, sizeof(AuxMapEnt) );
 
567
   tl_assert(nyu);
 
568
   nyu->base = a;
 
569
   nyu->sm   = &sm_distinguished[SM_DIST_NOACCESS];
 
570
   VG_(OSetGen_Insert)( auxmap_L2, nyu );
 
571
   insert_into_auxmap_L1_at( AUXMAP_L1_INSERT_IX, nyu );
 
572
   n_auxmap_L2_nodes++;
 
573
   return nyu;
434
574
}
435
575
 
436
576
/* --------------- SecMap fundamentals --------------- */
491
631
   secmap may be a distinguished one as the caller will only want to
492
632
   be able to read it. 
493
633
*/
494
 
static SecMap* get_secmap_for_reading ( Addr a )
 
634
static INLINE SecMap* get_secmap_for_reading ( Addr a )
495
635
{
496
636
   return ( a <= MAX_PRIMARY_ADDRESS
497
637
          ? get_secmap_for_reading_low (a)
739
879
 
740
880
static OSet* createSecVBitTable(void)
741
881
{
742
 
   return VG_(OSet_Create)( offsetof(SecVBitNode, a), 
743
 
                            NULL, // use fast comparisons
744
 
                            VG_(malloc), VG_(free) );
 
882
   return VG_(OSetGen_Create)( offsetof(SecVBitNode, a), 
 
883
                               NULL, // use fast comparisons
 
884
                               VG_(malloc), VG_(free) );
745
885
}
746
886
 
747
887
static void gcSecVBitTable(void)
756
896
   secVBitTable2 = createSecVBitTable();
757
897
 
758
898
   // Traverse the table, moving fresh nodes into the new table.
759
 
   VG_(OSet_ResetIter)(secVBitTable);
760
 
   while ( (n = VG_(OSet_Next)(secVBitTable)) ) {
 
899
   VG_(OSetGen_ResetIter)(secVBitTable);
 
900
   while ( (n = VG_(OSetGen_Next)(secVBitTable)) ) {
761
901
      Bool keep = False;
762
902
      if ( (GCs_done - n->last_touched) <= MAX_STALE_AGE ) {
763
903
         // Keep node if it's been touched recently enough (regardless of
778
918
      if ( keep ) {
779
919
         // Insert a copy of the node into the new table.
780
920
         SecVBitNode* n2 = 
781
 
            VG_(OSet_AllocNode)(secVBitTable2, sizeof(SecVBitNode));
 
921
            VG_(OSetGen_AllocNode)(secVBitTable2, sizeof(SecVBitNode));
782
922
         *n2 = *n;
783
 
         VG_(OSet_Insert)(secVBitTable2, n2);
 
923
         VG_(OSetGen_Insert)(secVBitTable2, n2);
784
924
      }
785
925
   }
786
926
 
787
927
   // Get the before and after sizes.
788
 
   n_nodes     = VG_(OSet_Size)(secVBitTable);
789
 
   n_survivors = VG_(OSet_Size)(secVBitTable2);
 
928
   n_nodes     = VG_(OSetGen_Size)(secVBitTable);
 
929
   n_survivors = VG_(OSetGen_Size)(secVBitTable2);
790
930
 
791
931
   // Destroy the old table, and put the new one in its place.
792
 
   VG_(OSet_Destroy)(secVBitTable, NULL);
 
932
   VG_(OSetGen_Destroy)(secVBitTable);
793
933
   secVBitTable = secVBitTable2;
794
934
 
795
935
   if (VG_(clo_verbosity) > 1) {
812
952
{
813
953
   Addr         aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE);
814
954
   Int          amod     = a % BYTES_PER_SEC_VBIT_NODE;
815
 
   SecVBitNode* n        = VG_(OSet_Lookup)(secVBitTable, &aAligned);
 
955
   SecVBitNode* n        = VG_(OSetGen_Lookup)(secVBitTable, &aAligned);
816
956
   UChar        vbits8;
817
957
   tl_assert2(n, "get_sec_vbits8: no node for address %p (%p)\n", aAligned, a);
818
958
   // Shouldn't be fully defined or fully undefined -- those cases shouldn't
826
966
{
827
967
   Addr         aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE);
828
968
   Int          i, amod  = a % BYTES_PER_SEC_VBIT_NODE;
829
 
   SecVBitNode* n        = VG_(OSet_Lookup)(secVBitTable, &aAligned);
 
969
   SecVBitNode* n        = VG_(OSetGen_Lookup)(secVBitTable, &aAligned);
830
970
   // Shouldn't be fully defined or fully undefined -- those cases shouldn't
831
971
   // make it to the secondary V bits table.
832
972
   tl_assert(V_BITS8_DEFINED != vbits8 && V_BITS8_UNDEFINED != vbits8);
837
977
   } else {
838
978
      // New node:  assign the specific byte, make the rest invalid (they
839
979
      // should never be read as-is, but be cautious).
840
 
      n = VG_(OSet_AllocNode)(secVBitTable, sizeof(SecVBitNode));
 
980
      n = VG_(OSetGen_AllocNode)(secVBitTable, sizeof(SecVBitNode));
841
981
      n->a            = aAligned;
842
982
      for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) {
843
983
         n->vbits8[i] = V_BITS8_UNDEFINED;
847
987
 
848
988
      // Do a table GC if necessary.  Nb: do this before inserting the new
849
989
      // node, to avoid erroneously GC'ing the new node.
850
 
      if (secVBitLimit == VG_(OSet_Size)(secVBitTable)) {
 
990
      if (secVBitLimit == VG_(OSetGen_Size)(secVBitTable)) {
851
991
         gcSecVBitTable();
852
992
      }
853
993
 
854
994
      // Insert the new node.
855
 
      VG_(OSet_Insert)(secVBitTable, n);
 
995
      VG_(OSetGen_Insert)(secVBitTable, n);
856
996
      sec_vbits_new_nodes++;
857
997
 
858
 
      n_secVBit_nodes = VG_(OSet_Size)(secVBitTable);
 
998
      n_secVBit_nodes = VG_(OSetGen_Size)(secVBitTable);
859
999
      if (n_secVBit_nodes > max_secVBit_nodes)
860
1000
         max_secVBit_nodes = n_secVBit_nodes;
861
1001
   }
870
1010
   return bigendian ? (wordszB-1-byteno) : byteno;
871
1011
}
872
1012
 
 
1013
 
 
1014
/* --------------- Ignored address ranges --------------- */
 
1015
 
 
1016
#define M_IGNORE_RANGES 4
 
1017
 
 
1018
typedef
 
1019
   struct {
 
1020
      Int  used;
 
1021
      Addr start[M_IGNORE_RANGES];
 
1022
      Addr end[M_IGNORE_RANGES];
 
1023
   }
 
1024
   IgnoreRanges;
 
1025
 
 
1026
static IgnoreRanges ignoreRanges;
 
1027
 
 
1028
static INLINE Bool in_ignored_range ( Addr a )
 
1029
{
 
1030
   Int i;
 
1031
   if (EXPECTED_TAKEN(ignoreRanges.used == 0))
 
1032
      return False;
 
1033
   for (i = 0; i < ignoreRanges.used; i++) {
 
1034
      if (a >= ignoreRanges.start[i] && a < ignoreRanges.end[i])
 
1035
         return True;
 
1036
   }
 
1037
   return False;
 
1038
}
 
1039
 
 
1040
 
 
1041
/* Parse a 32- or 64-bit hex number, including leading 0x, from string
 
1042
   starting at *ppc, putting result in *result, and return True.  Or
 
1043
   fail, in which case *ppc and *result are undefined, and return
 
1044
   False. */
 
1045
 
 
1046
static Bool isHex ( UChar c )
 
1047
{
 
1048
  return ((c >= '0' && c <= '9')
 
1049
          || (c >= 'a' && c <= 'f')
 
1050
          || (c >= 'A' && c <= 'F'));
 
1051
}
 
1052
 
 
1053
static UInt fromHex ( UChar c )
 
1054
{
 
1055
   if (c >= '0' && c <= '9')
 
1056
      return (UInt)c - (UInt)'0';
 
1057
   if (c >= 'a' && c <= 'f')
 
1058
      return 10 +  (UInt)c - (UInt)'a';
 
1059
   if (c >= 'A' && c <= 'F')
 
1060
      return 10 +  (UInt)c - (UInt)'A';
 
1061
   /*NOTREACHED*/
 
1062
   tl_assert(0);
 
1063
   return 0;
 
1064
}
 
1065
 
 
1066
static Bool parse_Addr ( UChar** ppc, Addr* result )
 
1067
{
 
1068
   Int used, limit = 2 * sizeof(Addr);
 
1069
   if (**ppc != '0')
 
1070
      return False;
 
1071
   (*ppc)++;
 
1072
   if (**ppc != 'x')
 
1073
      return False;
 
1074
   (*ppc)++;
 
1075
   *result = 0;
 
1076
   used = 0;
 
1077
   while (isHex(**ppc)) {
 
1078
      UInt d = fromHex(**ppc);
 
1079
      tl_assert(d < 16);
 
1080
      *result = ((*result) << 4) | fromHex(**ppc);
 
1081
      (*ppc)++;
 
1082
      used++;
 
1083
      if (used > limit) return False;
 
1084
   }
 
1085
   if (used == 0)
 
1086
      return False;
 
1087
   return True;
 
1088
}
 
1089
 
 
1090
/* Parse two such numbers separated by a dash, or fail. */
 
1091
 
 
1092
static Bool parse_range ( UChar** ppc, Addr* result1, Addr* result2 )
 
1093
{
 
1094
   Bool ok = parse_Addr(ppc, result1);
 
1095
   if (!ok)
 
1096
      return False;
 
1097
   if (**ppc != '-')
 
1098
      return False;
 
1099
   (*ppc)++;
 
1100
   ok = parse_Addr(ppc, result2);
 
1101
   if (!ok)
 
1102
      return False;
 
1103
   return True;
 
1104
}
 
1105
 
 
1106
/* Parse a set of ranges separated by commas into 'ignoreRanges', or
 
1107
   fail. */
 
1108
 
 
1109
static Bool parse_ignore_ranges ( UChar* str0 )
 
1110
{
 
1111
   Addr start, end;
 
1112
   Bool ok;
 
1113
   UChar*  str = str0;
 
1114
   UChar** ppc = &str;
 
1115
   ignoreRanges.used = 0;
 
1116
   while (1) {
 
1117
      ok = parse_range(ppc, &start, &end);
 
1118
      if (!ok)
 
1119
         return False;
 
1120
      if (ignoreRanges.used >= M_IGNORE_RANGES)
 
1121
         return False;
 
1122
      ignoreRanges.start[ignoreRanges.used] = start;
 
1123
      ignoreRanges.end[ignoreRanges.used] = end;
 
1124
      ignoreRanges.used++;
 
1125
      if (**ppc == 0)
 
1126
         return True;
 
1127
      if (**ppc != ',')
 
1128
         return False;
 
1129
      (*ppc)++;
 
1130
   }
 
1131
   /*NOTREACHED*/
 
1132
   return False;
 
1133
}
 
1134
 
 
1135
 
873
1136
/* --------------- Load/store slow cases. --------------- */
874
1137
 
875
1138
// Forward declarations
876
1139
static void mc_record_address_error  ( ThreadId tid, Addr a,
877
1140
                                       Int size, Bool isWrite );
878
 
static void mc_record_core_mem_error ( ThreadId tid, Bool isUnaddr, Char* s );
879
 
static void mc_record_param_error    ( ThreadId tid, Addr a, Bool isReg,
880
 
                                       Bool isUnaddr, Char* msg );
 
1141
static void mc_record_core_mem_error ( ThreadId tid, Bool isAddrErr, Char* s );
 
1142
static void mc_record_regparam_error ( ThreadId tid, Char* msg );
 
1143
static void mc_record_memparam_error ( ThreadId tid, Addr a,
 
1144
                                       Bool isAddrErr, Char* msg );
881
1145
static void mc_record_jump_error     ( ThreadId tid, Addr a );
882
1146
 
883
1147
static
900
1164
   Bool  ok;
901
1165
 
902
1166
   PROF_EVENT(30, "mc_LOADVn_slow");
 
1167
 
 
1168
   /* ------------ BEGIN semi-fast cases ------------ */
 
1169
   /* These deal quickly-ish with the common auxiliary primary map
 
1170
      cases on 64-bit platforms.  Are merely a speedup hack; can be
 
1171
      omitted without loss of correctness/functionality.  Note that in
 
1172
      both cases the "sizeof(void*) == 8" causes these cases to be
 
1173
      folded out by compilers on 32-bit platforms.  These are derived
 
1174
      from LOADV64 and LOADV32.
 
1175
   */
 
1176
   if (EXPECTED_TAKEN(sizeof(void*) == 8 
 
1177
                      && nBits == 64 && VG_IS_8_ALIGNED(a))) {
 
1178
      SecMap* sm       = get_secmap_for_reading(a);
 
1179
      UWord   sm_off16 = SM_OFF_16(a);
 
1180
      UWord   vabits16 = ((UShort*)(sm->vabits8))[sm_off16];
 
1181
      if (EXPECTED_TAKEN(vabits16 == VA_BITS16_DEFINED))
 
1182
         return V_BITS64_DEFINED;
 
1183
      if (EXPECTED_TAKEN(vabits16 == VA_BITS16_UNDEFINED))
 
1184
         return V_BITS64_UNDEFINED;
 
1185
      /* else fall into the slow case */
 
1186
   }
 
1187
   if (EXPECTED_TAKEN(sizeof(void*) == 8 
 
1188
                      && nBits == 32 && VG_IS_4_ALIGNED(a))) {
 
1189
      SecMap* sm = get_secmap_for_reading(a);
 
1190
      UWord sm_off = SM_OFF(a);
 
1191
      UWord vabits8 = sm->vabits8[sm_off];
 
1192
      if (EXPECTED_TAKEN(vabits8 == VA_BITS8_DEFINED))
 
1193
         return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_DEFINED);
 
1194
      if (EXPECTED_TAKEN(vabits8 == VA_BITS8_UNDEFINED))
 
1195
         return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_UNDEFINED);
 
1196
      /* else fall into slow case */
 
1197
   }
 
1198
   /* ------------ END semi-fast cases ------------ */
 
1199
 
903
1200
   tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8);
904
1201
 
905
1202
   for (i = szB-1; i >= 0; i--) {
949
1246
   Bool  ok;
950
1247
 
951
1248
   PROF_EVENT(35, "mc_STOREVn_slow");
 
1249
 
 
1250
   /* ------------ BEGIN semi-fast cases ------------ */
 
1251
   /* These deal quickly-ish with the common auxiliary primary map
 
1252
      cases on 64-bit platforms.  Are merely a speedup hack; can be
 
1253
      omitted without loss of correctness/functionality.  Note that in
 
1254
      both cases the "sizeof(void*) == 8" causes these cases to be
 
1255
      folded out by compilers on 32-bit platforms.  These are derived
 
1256
      from STOREV64 and STOREV32.
 
1257
   */
 
1258
   if (EXPECTED_TAKEN(sizeof(void*) == 8 
 
1259
                      && nBits == 64 && VG_IS_8_ALIGNED(a))) {
 
1260
      SecMap* sm       = get_secmap_for_reading(a);
 
1261
      UWord   sm_off16 = SM_OFF_16(a);
 
1262
      UWord   vabits16 = ((UShort*)(sm->vabits8))[sm_off16];
 
1263
      if (EXPECTED_TAKEN( !is_distinguished_sm(sm) && 
 
1264
                          (VA_BITS16_DEFINED   == vabits16 ||
 
1265
                           VA_BITS16_UNDEFINED == vabits16) )) {
 
1266
         /* Handle common case quickly: a is suitably aligned, */
 
1267
         /* is mapped, and is addressible. */
 
1268
         // Convert full V-bits in register to compact 2-bit form.
 
1269
         if (EXPECTED_TAKEN(V_BITS64_DEFINED == vbytes)) {
 
1270
            ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_DEFINED;
 
1271
            return;
 
1272
         } else if (V_BITS64_UNDEFINED == vbytes) {
 
1273
            ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_UNDEFINED;
 
1274
            return;
 
1275
         }
 
1276
         /* else fall into the slow case */
 
1277
      }
 
1278
      /* else fall into the slow case */
 
1279
   }
 
1280
   if (EXPECTED_TAKEN(sizeof(void*) == 8
 
1281
                      && nBits == 32 && VG_IS_4_ALIGNED(a))) {
 
1282
      SecMap* sm      = get_secmap_for_reading(a);
 
1283
      UWord   sm_off  = SM_OFF(a);
 
1284
      UWord   vabits8 = sm->vabits8[sm_off];
 
1285
      if (EXPECTED_TAKEN( !is_distinguished_sm(sm) && 
 
1286
                          (VA_BITS8_DEFINED   == vabits8 ||
 
1287
                           VA_BITS8_UNDEFINED == vabits8) )) {
 
1288
         /* Handle common case quickly: a is suitably aligned, */
 
1289
         /* is mapped, and is addressible. */
 
1290
         // Convert full V-bits in register to compact 2-bit form.
 
1291
         if (EXPECTED_TAKEN(V_BITS32_DEFINED == (vbytes & 0xFFFFFFFF))) {
 
1292
            sm->vabits8[sm_off] = VA_BITS8_DEFINED;
 
1293
            return;
 
1294
         } else if (V_BITS32_UNDEFINED == (vbytes & 0xFFFFFFFF)) {
 
1295
            sm->vabits8[sm_off] = VA_BITS8_UNDEFINED;
 
1296
            return;
 
1297
         }
 
1298
         /* else fall into the slow case */
 
1299
      }
 
1300
      /* else fall into the slow case */
 
1301
   }
 
1302
   /* ------------ END semi-fast cases ------------ */
 
1303
 
952
1304
   tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8);
953
1305
 
954
1306
   /* Dump vbytes in memory, iterating from least to most significant
955
 
      byte.  At the same time establish addressibility of the
956
 
      location. */
 
1307
      byte.  At the same time establish addressibility of the location. */
957
1308
   for (i = 0; i < szB; i++) {
958
1309
      PROF_EVENT(36, "mc_STOREVn_slow(loop)");
959
1310
      ai     = a + byte_offset_w(szB, bigendian, i);
1433
1784
static void VG_REGPARM(1) mc_new_mem_stack_4(Addr new_SP)
1434
1785
{
1435
1786
   PROF_EVENT(110, "new_mem_stack_4");
1436
 
   if (VG_IS_4_ALIGNED(new_SP)) {
 
1787
   if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1437
1788
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP );
1438
1789
   } else {
1439
1790
      MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 4 );
1443
1794
static void VG_REGPARM(1) mc_die_mem_stack_4(Addr new_SP)
1444
1795
{
1445
1796
   PROF_EVENT(120, "die_mem_stack_4");
1446
 
   if (VG_IS_4_ALIGNED(new_SP)) {
 
1797
   if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1447
1798
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 );
1448
1799
   } else {
1449
1800
      MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-4, 4 );
1453
1804
static void VG_REGPARM(1) mc_new_mem_stack_8(Addr new_SP)
1454
1805
{
1455
1806
   PROF_EVENT(111, "new_mem_stack_8");
1456
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1807
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1457
1808
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP );
1458
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1809
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1459
1810
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP   );
1460
1811
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 );
1461
1812
   } else {
1466
1817
static void VG_REGPARM(1) mc_die_mem_stack_8(Addr new_SP)
1467
1818
{
1468
1819
   PROF_EVENT(121, "die_mem_stack_8");
1469
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1820
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1470
1821
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 );
1471
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1822
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1472
1823
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 );
1473
1824
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 );
1474
1825
   } else {
1479
1830
static void VG_REGPARM(1) mc_new_mem_stack_12(Addr new_SP)
1480
1831
{
1481
1832
   PROF_EVENT(112, "new_mem_stack_12");
1482
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1833
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1483
1834
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP   );
1484
1835
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 );
1485
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1836
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1837
      /* from previous test we don't have 8-alignment at offset +0,
 
1838
         hence must have 8 alignment at offsets +4/-4.  Hence safe to
 
1839
         do 4 at +0 and then 8 at +4/. */
1486
1840
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP   );
1487
1841
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 );
1488
1842
   } else {
1494
1848
{
1495
1849
   PROF_EVENT(122, "die_mem_stack_12");
1496
1850
   /* Note the -12 in the test */
1497
 
   if (VG_IS_8_ALIGNED(new_SP-12)) {
 
1851
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP-12 )) {
 
1852
      /* We have 8-alignment at -12, hence ok to do 8 at -12 and 4 at
 
1853
         -4. */
1498
1854
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 );
1499
1855
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4  );
1500
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1856
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1857
      /* We have 4-alignment at +0, but we don't have 8-alignment at
 
1858
         -12.  So we must have 8-alignment at -8.  Hence do 4 at -12
 
1859
         and then 8 at -8. */
1501
1860
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 );
1502
1861
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8  );
1503
1862
   } else {
1508
1867
static void VG_REGPARM(1) mc_new_mem_stack_16(Addr new_SP)
1509
1868
{
1510
1869
   PROF_EVENT(113, "new_mem_stack_16");
1511
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1870
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1871
      /* Have 8-alignment at +0, hence do 8 at +0 and 8 at +8. */
1512
1872
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP   );
1513
1873
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 );
1514
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1874
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1875
      /* Have 4 alignment at +0 but not 8; hence 8 must be at +4.
 
1876
         Hence do 4 at +0, 8 at +4, 4 at +12. */
1515
1877
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1516
1878
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4  );
1517
1879
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 );
1523
1885
static void VG_REGPARM(1) mc_die_mem_stack_16(Addr new_SP)
1524
1886
{
1525
1887
   PROF_EVENT(123, "die_mem_stack_16");
1526
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1888
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1889
      /* Have 8-alignment at +0, hence do 8 at -16 and 8 at -8. */
1527
1890
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 );
1528
1891
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8  );
1529
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1892
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1893
      /* 8 alignment must be at -12.  Do 4 at -16, 8 at -12, 4 at -4. */
1530
1894
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 );
1531
1895
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 );
1532
1896
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4  );
1538
1902
static void VG_REGPARM(1) mc_new_mem_stack_32(Addr new_SP)
1539
1903
{
1540
1904
   PROF_EVENT(114, "new_mem_stack_32");
1541
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1905
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1906
      /* Straightforward */
1542
1907
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1543
1908
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8  );
1544
1909
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 );
1545
1910
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 );
1546
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1911
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1912
      /* 8 alignment must be at +4.  Hence do 8 at +4,+12,+20 and 4 at
 
1913
         +0,+28. */
1547
1914
      make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1548
1915
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4  );
1549
1916
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 );
1557
1924
static void VG_REGPARM(1) mc_die_mem_stack_32(Addr new_SP)
1558
1925
{
1559
1926
   PROF_EVENT(124, "die_mem_stack_32");
1560
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1927
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1928
      /* Straightforward */
1561
1929
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 );
1562
1930
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 );
1563
1931
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 );
1564
1932
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 );
1565
 
   } else if (VG_IS_4_ALIGNED(new_SP)) {
 
1933
   } else if (VG_IS_4_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
 
1934
      /* 8 alignment must be at -4 etc.  Hence do 8 at -12,-20,-28 and
 
1935
         4 at -32,-4. */
1566
1936
      make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 );
1567
1937
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-28 );
1568
1938
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-20 );
1576
1946
static void VG_REGPARM(1) mc_new_mem_stack_112(Addr new_SP)
1577
1947
{
1578
1948
   PROF_EVENT(115, "new_mem_stack_112");
1579
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1949
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1580
1950
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1581
1951
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8  );
1582
1952
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 );
1599
1969
static void VG_REGPARM(1) mc_die_mem_stack_112(Addr new_SP)
1600
1970
{
1601
1971
   PROF_EVENT(125, "die_mem_stack_112");
1602
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1972
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1603
1973
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112);
1604
1974
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104);
1605
1975
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 );
1622
1992
static void VG_REGPARM(1) mc_new_mem_stack_128(Addr new_SP)
1623
1993
{
1624
1994
   PROF_EVENT(116, "new_mem_stack_128");
1625
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
1995
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1626
1996
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1627
1997
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8  );
1628
1998
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 );
1647
2017
static void VG_REGPARM(1) mc_die_mem_stack_128(Addr new_SP)
1648
2018
{
1649
2019
   PROF_EVENT(126, "die_mem_stack_128");
1650
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
2020
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1651
2021
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128);
1652
2022
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120);
1653
2023
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112);
1672
2042
static void VG_REGPARM(1) mc_new_mem_stack_144(Addr new_SP)
1673
2043
{
1674
2044
   PROF_EVENT(117, "new_mem_stack_144");
1675
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
2045
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1676
2046
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1677
2047
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8  );
1678
2048
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 );
1699
2069
static void VG_REGPARM(1) mc_die_mem_stack_144(Addr new_SP)
1700
2070
{
1701
2071
   PROF_EVENT(127, "die_mem_stack_144");
1702
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
2072
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1703
2073
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144);
1704
2074
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-136);
1705
2075
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128);
1726
2096
static void VG_REGPARM(1) mc_new_mem_stack_160(Addr new_SP)
1727
2097
{
1728
2098
   PROF_EVENT(118, "new_mem_stack_160");
1729
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
2099
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1730
2100
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP    );
1731
2101
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8  );
1732
2102
      make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 );
1755
2125
static void VG_REGPARM(1) mc_die_mem_stack_160(Addr new_SP)
1756
2126
{
1757
2127
   PROF_EVENT(128, "die_mem_stack_160");
1758
 
   if (VG_IS_8_ALIGNED(new_SP)) {
 
2128
   if (VG_IS_8_ALIGNED( -VG_STACK_REDZONE_SZB + new_SP )) {
1759
2129
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-160);
1760
2130
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-152);
1761
2131
      make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144);
1826
2196
{
1827
2197
   tl_assert(sizeof(UWord) == sizeof(SizeT));
1828
2198
   if (0)
1829
 
      VG_(printf)("helperc_MAKE_STACK_UNINIT %p %d\n", base, len );
 
2199
      VG_(printf)("helperc_MAKE_STACK_UNINIT %p %lu\n", base, len );
1830
2200
 
1831
2201
#  if 0
1832
2202
   /* Really slow version */
2094
2464
   if (!ok) {
2095
2465
      switch (part) {
2096
2466
      case Vg_CoreSysCall:
2097
 
         mc_record_param_error ( tid, bad_addr, /*isReg*/False,
2098
 
                                    /*isUnaddr*/True, s );
 
2467
         mc_record_memparam_error ( tid, bad_addr, /*isAddrErr*/True, s );
2099
2468
         break;
2100
2469
 
2101
 
      case Vg_CorePThread:
2102
2470
      case Vg_CoreSignal:
2103
 
         mc_record_core_mem_error( tid, /*isUnaddr*/True, s );
 
2471
         mc_record_core_mem_error( tid, /*isAddrErr*/True, s );
2104
2472
         break;
2105
2473
 
2106
2474
      default:
2117
2485
   MC_ReadResult res = is_mem_defined ( base, size, &bad_addr );
2118
2486
 
2119
2487
   if (MC_Ok != res) {
2120
 
      Bool isUnaddr = ( MC_AddrErr == res ? True : False );
 
2488
      Bool isAddrErr = ( MC_AddrErr == res ? True : False );
2121
2489
 
2122
2490
      switch (part) {
2123
2491
      case Vg_CoreSysCall:
2124
 
         mc_record_param_error ( tid, bad_addr, /*isReg*/False,
2125
 
                                 isUnaddr, s );
 
2492
         mc_record_memparam_error ( tid, bad_addr, isAddrErr, s );
2126
2493
         break;
2127
2494
      
2128
 
      case Vg_CoreClientReq: // Kludge: make this a CoreMemErr
2129
 
      case Vg_CorePThread:
2130
 
         mc_record_core_mem_error( tid, isUnaddr, s );
2131
 
         break;
2132
 
 
2133
2495
      /* If we're being asked to jump to a silly address, record an error 
2134
2496
         message before potentially crashing the entire system. */
2135
2497
      case Vg_CoreTranslate:
2152
2514
   tl_assert(part == Vg_CoreSysCall);
2153
2515
   res = mc_is_defined_asciiz ( (Addr)str, &bad_addr );
2154
2516
   if (MC_Ok != res) {
2155
 
      Bool isUnaddr = ( MC_AddrErr == res ? True : False );
2156
 
      mc_record_param_error ( tid, bad_addr, /*isReg*/False, isUnaddr, s );
 
2517
      Bool isAddrErr = ( MC_AddrErr == res ? True : False );
 
2518
      mc_record_memparam_error ( tid, bad_addr, isAddrErr, s );
2157
2519
   }
2158
2520
}
2159
2521
 
2161
2523
void mc_new_mem_startup( Addr a, SizeT len, Bool rr, Bool ww, Bool xx )
2162
2524
{
2163
2525
   /* Ignore the permissions, just make it defined.  Seems to work... */
 
2526
   // Because code is defined, initialised variables get put in the data
 
2527
   // segment and are defined, and uninitialised variables get put in the
 
2528
   // bss segment and are auto-zeroed (and so defined).  
 
2529
   //
 
2530
   // It's possible that there will be padding between global variables.
 
2531
   // This will also be auto-zeroed, and marked as defined by Memcheck.  If
 
2532
   // a program uses it, Memcheck will not complain.  This is arguably a
 
2533
   // false negative, but it's a grey area -- the behaviour is defined (the
 
2534
   // padding is zeroed) but it's probably not what the user intended.  And
 
2535
   // we can't avoid it.
2164
2536
   DEBUG("mc_new_mem_startup(%p, %llu, rr=%u, ww=%u, xx=%u)\n",
2165
2537
         a, (ULong)len, rr, ww, xx);
2166
2538
   MC_(make_mem_defined)(a, len);
2191
2563
static void mc_post_reg_write ( CorePart part, ThreadId tid, 
2192
2564
                                OffT offset, SizeT size)
2193
2565
{
2194
 
#  define MAX_REG_WRITE_SIZE 1392
 
2566
#  define MAX_REG_WRITE_SIZE 1408
2195
2567
   UChar area[MAX_REG_WRITE_SIZE];
2196
2568
   tl_assert(size <= MAX_REG_WRITE_SIZE);
2197
2569
   VG_(memset)(area, V_BITS8_DEFINED, size);
2231
2603
   }
2232
2604
 
2233
2605
   if (bad)
2234
 
      mc_record_param_error ( tid, 0, /*isReg*/True, /*isUnaddr*/False, s );
 
2606
      mc_record_regparam_error ( tid, s );
2235
2607
}
2236
2608
 
2237
2609
 
2238
2610
/*------------------------------------------------------------*/
2239
 
/*--- Error and suppression types                          ---*/
 
2611
/*--- Error types                                          ---*/
2240
2612
/*------------------------------------------------------------*/
2241
2613
 
 
2614
// Different kinds of blocks.
 
2615
typedef enum {
 
2616
   Block_Mallocd = 111,
 
2617
   Block_Freed,
 
2618
   Block_Mempool,
 
2619
   Block_MempoolChunk,
 
2620
   Block_UserG
 
2621
} BlockKind;
 
2622
 
 
2623
/* ------------------ Addresses -------------------- */
 
2624
 
2242
2625
/* The classification of a faulting address. */
2243
2626
typedef 
2244
2627
   enum { 
2245
 
      Undescribed,   // as-yet unclassified
2246
 
      Stack, 
2247
 
      Unknown,       // classification yielded nothing useful
2248
 
      Freed, Mallocd, 
2249
 
      UserG,         // in a user-defined block
2250
 
      Mempool,       // in a mempool
2251
 
      Register,      // in a register;  for Param errors only
 
2628
      Addr_Undescribed,   // as-yet unclassified
 
2629
      Addr_Unknown,       // classification yielded nothing useful
 
2630
      Addr_Stack,          
 
2631
      Addr_Block,
2252
2632
   }
2253
 
   AddrKind;
 
2633
   AddrTag;
2254
2634
 
2255
 
/* Records info about a faulting address. */
2256
2635
typedef
2257
 
   struct {                   // Used by:
2258
 
      AddrKind akind;         //   ALL
2259
 
      SizeT blksize;          //   Freed, Mallocd
2260
 
      OffT rwoffset;          //   Freed, Mallocd
2261
 
      ExeContext* lastchange; //   Freed, Mallocd
2262
 
      ThreadId stack_tid;     //   Stack
2263
 
      const Char *desc;       //   UserG
2264
 
      Bool maybe_gcc;         // True if just below %esp -- could be a gcc bug.
2265
 
   }
 
2636
   struct _AddrInfo
2266
2637
   AddrInfo;
2267
2638
 
2268
 
typedef 
2269
 
   enum { 
2270
 
      ParamSupp,     // Bad syscall params
2271
 
      CoreMemSupp,   // Memory errors in core (pthread ops, signal handling)
2272
 
 
2273
 
      // Use of invalid values of given size (MemCheck only)
2274
 
      Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp, Value16Supp,
2275
 
 
2276
 
      // Invalid read/write attempt at given size
2277
 
      Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, Addr16Supp,
2278
 
 
2279
 
      FreeSupp,      // Invalid or mismatching free
2280
 
      OverlapSupp,   // Overlapping blocks in memcpy(), strcpy(), etc
2281
 
      LeakSupp,      // Something to be suppressed in a leak check.
2282
 
      MempoolSupp,   // Memory pool suppression.
2283
 
   } 
2284
 
   MC_SuppKind;
 
2639
struct _AddrInfo {
 
2640
   AddrTag tag;
 
2641
   union {
 
2642
      // As-yet unclassified.
 
2643
      struct { } Undescribed;
 
2644
 
 
2645
      // On a stack.
 
2646
      struct {
 
2647
         ThreadId tid;        // Which thread's stack?
 
2648
      } Stack;
 
2649
 
 
2650
      // This covers heap blocks (normal and from mempools) and user-defined
 
2651
      // blocks.
 
2652
      struct {
 
2653
         BlockKind   block_kind;
 
2654
         Char*       block_desc;    // "block", "mempool" or user-defined
 
2655
         SizeT       block_szB;
 
2656
         OffT        rwoffset;
 
2657
         ExeContext* lastchange;
 
2658
      } Block;
 
2659
 
 
2660
      // Classification yielded nothing useful.
 
2661
      struct { } Unknown;
 
2662
 
 
2663
   } Addr;
 
2664
};
 
2665
 
 
2666
/* ------------------ Errors ----------------------- */
2285
2667
 
2286
2668
/* What kind of error it is. */
2287
2669
typedef 
2288
 
   enum { ValueErr,
2289
 
          CoreMemErr,   // Error in core op (pthread, signals) or client req
2290
 
          AddrErr, 
2291
 
          ParamErr, UserErr,  /* behaves like an anonymous ParamErr */
2292
 
          FreeErr, FreeMismatchErr,
2293
 
          OverlapErr,
2294
 
          LeakErr,
2295
 
          IllegalMempoolErr,
2296
 
   }
2297
 
   MC_ErrorKind;
2298
 
 
2299
 
/* What kind of memory access is involved in the error? */
2300
 
typedef
2301
 
   enum { ReadAxs, WriteAxs, ExecAxs }
2302
 
   AxsKind;
2303
 
 
2304
 
/* Extra context for memory errors */
2305
 
typedef
2306
 
   struct {                // Used by:
2307
 
      AxsKind axskind;     //   AddrErr
2308
 
      Int size;            //   AddrErr, ValueErr
2309
 
      AddrInfo addrinfo;   //   {Addr,Free,FreeMismatch,Param,User}Err
2310
 
      Bool isUnaddr;       //   {CoreMem,Param,User}Err
2311
 
   }
2312
 
   MC_Error;
 
2670
   enum { 
 
2671
      Err_Value,
 
2672
      Err_Cond,
 
2673
      Err_CoreMem,
 
2674
      Err_Addr, 
 
2675
      Err_Jump, 
 
2676
      Err_RegParam,
 
2677
      Err_MemParam,
 
2678
      Err_User,
 
2679
      Err_Free,
 
2680
      Err_FreeMismatch,
 
2681
      Err_Overlap,
 
2682
      Err_Leak,
 
2683
      Err_IllegalMempool,
 
2684
   }
 
2685
   MC_ErrorTag;
 
2686
 
 
2687
 
 
2688
typedef struct _MC_Error MC_Error;
 
2689
 
 
2690
struct _MC_Error {
 
2691
   // Nb: we don't need the tag here, as it's stored in the Error type! Yuk.
 
2692
   //MC_ErrorTag tag;
 
2693
 
 
2694
   union {
 
2695
      // Use of an undefined value:
 
2696
      // - as a pointer in a load or store
 
2697
      // - as a jump target
 
2698
      struct {
 
2699
         SizeT szB;     // size of value in bytes
 
2700
      } Value;
 
2701
 
 
2702
      // Use of an undefined value in a conditional branch or move.
 
2703
      struct {
 
2704
      } Cond;
 
2705
 
 
2706
      // Addressability error in core (signal-handling) operation.
 
2707
      // It would be good to get rid of this error kind, merge it with
 
2708
      // another one somehow.
 
2709
      struct {
 
2710
      } CoreMem;
 
2711
 
 
2712
      // Use of an unaddressable memory location in a load or store.
 
2713
      struct {
 
2714
         Bool     isWrite;    // read or write?
 
2715
         SizeT    szB;        // not used for exec (jump) errors
 
2716
         Bool     maybe_gcc;  // True if just below %esp -- could be a gcc bug
 
2717
         AddrInfo ai;
 
2718
      } Addr;
 
2719
 
 
2720
      // Jump to an unaddressable memory location.
 
2721
      struct {
 
2722
         AddrInfo ai;
 
2723
      } Jump;
 
2724
 
 
2725
      // System call register input contains undefined bytes.
 
2726
      struct {
 
2727
      } RegParam;
 
2728
 
 
2729
      // System call memory input contains undefined/unaddressable bytes
 
2730
      struct {
 
2731
         Bool     isAddrErr;  // Addressability or definedness error?
 
2732
         AddrInfo ai;
 
2733
      } MemParam;
 
2734
 
 
2735
      // Problem found from a client request like CHECK_MEM_IS_ADDRESSABLE.
 
2736
      struct {
 
2737
         Bool     isAddrErr;  // Addressability or definedness error?
 
2738
         AddrInfo ai;
 
2739
      } User;
 
2740
 
 
2741
      // Program tried to free() something that's not a heap block (this
 
2742
      // covers double-frees). */
 
2743
      struct {
 
2744
         AddrInfo ai;
 
2745
      } Free;
 
2746
 
 
2747
      // Program allocates heap block with one function
 
2748
      // (malloc/new/new[]/custom) and deallocates with not the matching one.
 
2749
      struct {
 
2750
         AddrInfo ai;
 
2751
      } FreeMismatch;
 
2752
 
 
2753
      // Call to strcpy, memcpy, etc, with overlapping blocks.
 
2754
      struct {
 
2755
         Addr src;   // Source block
 
2756
         Addr dst;   // Destination block
 
2757
         Int  szB;   // Size in bytes;  0 if unused.
 
2758
      } Overlap;
 
2759
 
 
2760
      // A memory leak.
 
2761
      struct {
 
2762
         UInt        n_this_record;
 
2763
         UInt        n_total_records;
 
2764
         LossRecord* lossRecord;
 
2765
      } Leak;
 
2766
 
 
2767
      // A memory pool error.
 
2768
      struct {
 
2769
         AddrInfo ai;
 
2770
      } IllegalMempool;
 
2771
 
 
2772
   } Err;
 
2773
};
 
2774
 
2313
2775
 
2314
2776
/*------------------------------------------------------------*/
2315
2777
/*--- Printing errors                                      ---*/
2316
2778
/*------------------------------------------------------------*/
2317
2779
 
2318
 
static void mc_pp_AddrInfo ( Addr a, AddrInfo* ai )
 
2780
static void mc_pp_AddrInfo ( Addr a, AddrInfo* ai, Bool maybe_gcc )
2319
2781
{
2320
2782
   HChar* xpre  = VG_(clo_xml) ? "  <auxwhat>" : " ";
2321
2783
   HChar* xpost = VG_(clo_xml) ? "</auxwhat>"  : "";
2322
2784
 
2323
 
   switch (ai->akind) {
2324
 
      case Stack: 
2325
 
         VG_(message)(Vg_UserMsg, 
2326
 
                      "%sAddress 0x%llx is on thread %d's stack%s", 
2327
 
                      xpre, (ULong)a, ai->stack_tid, xpost);
2328
 
         break;
2329
 
      case Unknown:
2330
 
         if (ai->maybe_gcc) {
 
2785
   switch (ai->tag) {
 
2786
      case Addr_Unknown:
 
2787
         if (maybe_gcc) {
2331
2788
            VG_(message)(Vg_UserMsg, 
2332
2789
               "%sAddress 0x%llx is just below the stack ptr.  "
2333
2790
               "To suppress, use: --workaround-gcc296-bugs=yes%s",
2340
2797
               xpre, (ULong)a, xpost);
2341
2798
         }
2342
2799
         break;
2343
 
      case Freed: case Mallocd: case UserG: case Mempool: {
 
2800
 
 
2801
      case Addr_Stack: 
 
2802
         VG_(message)(Vg_UserMsg, 
 
2803
                      "%sAddress 0x%llx is on thread %d's stack%s", 
 
2804
                      xpre, (ULong)a, ai->Addr.Stack.tid, xpost);
 
2805
         break;
 
2806
 
 
2807
      case Addr_Block: {
 
2808
         SizeT block_szB  = ai->Addr.Block.block_szB;
 
2809
         OffT  rwoffset   = ai->Addr.Block.rwoffset;
2344
2810
         SizeT delta;
2345
2811
         const Char* relative;
2346
 
         const Char* kind;
2347
 
         if (ai->akind == Mempool) {
2348
 
            kind = "mempool";
2349
 
         } else {
2350
 
            kind = "block";
2351
 
         }
2352
 
         if (ai->desc != NULL)
2353
 
            kind = ai->desc;
2354
2812
 
2355
 
         if (ai->rwoffset < 0) {
2356
 
            delta    = (SizeT)(- ai->rwoffset);
 
2813
         if (rwoffset < 0) {
 
2814
            delta    = (SizeT)(-rwoffset);
2357
2815
            relative = "before";
2358
 
         } else if (ai->rwoffset >= ai->blksize) {
2359
 
            delta    = ai->rwoffset - ai->blksize;
 
2816
         } else if (rwoffset >= block_szB) {
 
2817
            delta    = rwoffset - block_szB;
2360
2818
            relative = "after";
2361
2819
         } else {
2362
 
            delta    = ai->rwoffset;
 
2820
            delta    = rwoffset;
2363
2821
            relative = "inside";
2364
2822
         }
2365
2823
         VG_(message)(Vg_UserMsg, 
2366
2824
            "%sAddress 0x%lx is %,lu bytes %s a %s of size %,lu %s%s",
2367
2825
            xpre,
2368
 
            a, delta, relative, kind,
2369
 
            ai->blksize,
2370
 
            ai->akind==Mallocd ? "alloc'd" 
2371
 
               : ai->akind==Freed ? "free'd" 
2372
 
                                  : "client-defined",
 
2826
            a, delta, relative, ai->Addr.Block.block_desc,
 
2827
            block_szB,
 
2828
            ai->Addr.Block.block_kind==Block_Mallocd ? "alloc'd" 
 
2829
            : ai->Addr.Block.block_kind==Block_Freed ? "free'd" 
 
2830
                                                     : "client-defined",
2373
2831
            xpost);
2374
 
         VG_(pp_ExeContext)(ai->lastchange);
 
2832
         VG_(pp_ExeContext)(ai->Addr.Block.lastchange);
2375
2833
         break;
2376
2834
      }
2377
 
      case Register:
2378
 
         // print nothing
2379
 
         tl_assert(0 == a);
2380
 
         break;
 
2835
 
2381
2836
      default:
2382
2837
         VG_(tool_panic)("mc_pp_AddrInfo");
2383
2838
   }
2384
2839
}
2385
2840
 
2386
 
static void mc_pp_Error ( Error* err )
2387
 
{
2388
 
   MC_Error* err_extra = VG_(get_error_extra)(err);
2389
 
 
 
2841
static const HChar* str_leak_lossmode ( Reachedness lossmode )
 
2842
{
 
2843
   const HChar *loss = "?";
 
2844
   switch (lossmode) {
 
2845
      case Unreached:    loss = "definitely lost"; break;
 
2846
      case IndirectLeak: loss = "indirectly lost"; break;
 
2847
      case Interior:     loss = "possibly lost"; break;
 
2848
      case Proper:       loss = "still reachable"; break;
 
2849
   }
 
2850
   return loss;
 
2851
}
 
2852
 
 
2853
static const HChar* xml_leak_kind ( Reachedness lossmode )
 
2854
{
 
2855
   const HChar *loss = "?";
 
2856
   switch (lossmode) {
 
2857
      case Unreached:    loss = "Leak_DefinitelyLost"; break;
 
2858
      case IndirectLeak: loss = "Leak_IndirectlyLost"; break;
 
2859
      case Interior:     loss = "Leak_PossiblyLost"; break;
 
2860
      case Proper:       loss = "Leak_StillReachable"; break;
 
2861
   }
 
2862
   return loss;
 
2863
}
 
2864
 
 
2865
static void mc_pp_msg( Char* xml_name, Error* err, const HChar* format, ... )
 
2866
{
2390
2867
   HChar* xpre  = VG_(clo_xml) ? "  <what>" : "";
2391
2868
   HChar* xpost = VG_(clo_xml) ? "</what>"  : "";
 
2869
   Char buf[256];
 
2870
   va_list vargs;
 
2871
 
 
2872
   if (VG_(clo_xml))
 
2873
      VG_(message)(Vg_UserMsg, "  <kind>%s</kind>", xml_name);
 
2874
   // Stick xpre and xpost on the front and back of the format string.
 
2875
   VG_(snprintf)(buf, 256, "%s%s%s", xpre, format, xpost);
 
2876
   va_start(vargs, format);
 
2877
   VG_(vmessage) ( Vg_UserMsg, buf, vargs );
 
2878
   va_end(vargs);
 
2879
   VG_(pp_ExeContext)( VG_(get_error_where)(err) );
 
2880
}
 
2881
 
 
2882
static void mc_pp_Error ( Error* err )
 
2883
{
 
2884
   MC_Error* extra = VG_(get_error_extra)(err);
2392
2885
 
2393
2886
   switch (VG_(get_error_kind)(err)) {
2394
 
      case CoreMemErr: {
2395
 
         Char* s = ( err_extra->isUnaddr ? "unaddressable" : "uninitialised" );
2396
 
         if (VG_(clo_xml))
2397
 
            VG_(message)(Vg_UserMsg, "  <kind>CoreMemError</kind>");
2398
 
            /* What the hell *is* a CoreMemError? jrs 2005-May-18 */
2399
 
         VG_(message)(Vg_UserMsg, "%s%s contains %s byte(s)%s", 
2400
 
                      xpre, VG_(get_error_string)(err), s, xpost);
2401
 
 
2402
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
 
2887
      case Err_CoreMem: {
 
2888
         /* What the hell *is* a CoreMemError? jrs 2005-May-18 */
 
2889
         /* As of 2006-Dec-14, it's caused by unaddressable bytes in a
 
2890
            signal handler frame.  --njn */
 
2891
         mc_pp_msg("CoreMemError", err,
 
2892
                   "%s contains unaddressable byte(s)", 
 
2893
                   VG_(get_error_string)(err));
2403
2894
         break;
2404
 
      
2405
2895
      } 
2406
2896
      
2407
 
      case ValueErr:
2408
 
         if (err_extra->size == 0) {
2409
 
            if (VG_(clo_xml))
2410
 
               VG_(message)(Vg_UserMsg, "  <kind>UninitCondition</kind>");
2411
 
            VG_(message)(Vg_UserMsg, "%sConditional jump or move depends"
2412
 
                                     " on uninitialised value(s)%s", 
2413
 
                                     xpre, xpost);
 
2897
      case Err_Value:
 
2898
         mc_pp_msg("UninitValue", err,
 
2899
                   "Use of uninitialised value of size %d",
 
2900
                   extra->Err.Value.szB);
 
2901
         break;
 
2902
 
 
2903
      case Err_Cond:
 
2904
         mc_pp_msg("UninitCondition", err,
 
2905
                   "Conditional jump or move depends"
 
2906
                   " on uninitialised value(s)");
 
2907
         break;
 
2908
 
 
2909
      case Err_RegParam:
 
2910
         mc_pp_msg("SyscallParam", err,
 
2911
                   "Syscall param %s contains uninitialised byte(s)",
 
2912
                   VG_(get_error_string)(err));
 
2913
         break;
 
2914
 
 
2915
      case Err_MemParam:
 
2916
         mc_pp_msg("SyscallParam", err,
 
2917
                   "Syscall param %s points to %s byte(s)",
 
2918
                   VG_(get_error_string)(err),
 
2919
                   ( extra->Err.MemParam.isAddrErr 
 
2920
                     ? "unaddressable" : "uninitialised" ));
 
2921
         mc_pp_AddrInfo(VG_(get_error_address)(err),
 
2922
                        &extra->Err.MemParam.ai, False);
 
2923
         break;
 
2924
 
 
2925
      case Err_User:
 
2926
         mc_pp_msg("ClientCheck", err,
 
2927
                   "%s byte(s) found during client check request", 
 
2928
                   ( extra->Err.User.isAddrErr
 
2929
                     ? "Unaddressable" : "Uninitialised" ));
 
2930
         mc_pp_AddrInfo(VG_(get_error_address)(err), &extra->Err.User.ai,
 
2931
                        False);
 
2932
         break;
 
2933
 
 
2934
      case Err_Free:
 
2935
         mc_pp_msg("InvalidFree", err,
 
2936
                   "Invalid free() / delete / delete[]");
 
2937
         mc_pp_AddrInfo(VG_(get_error_address)(err),
 
2938
                        &extra->Err.Free.ai, False);
 
2939
         break;
 
2940
 
 
2941
      case Err_FreeMismatch:
 
2942
         mc_pp_msg("MismatchedFree", err,
 
2943
                   "Mismatched free() / delete / delete []");
 
2944
         mc_pp_AddrInfo(VG_(get_error_address)(err),
 
2945
                        &extra->Err.FreeMismatch.ai, False);
 
2946
         break;
 
2947
 
 
2948
      case Err_Addr:
 
2949
         if (extra->Err.Addr.isWrite) {
 
2950
            mc_pp_msg("InvalidWrite", err,
 
2951
                      "Invalid write of size %d", 
 
2952
                      extra->Err.Addr.szB); 
2414
2953
         } else {
2415
 
            if (VG_(clo_xml))
2416
 
               VG_(message)(Vg_UserMsg, "  <kind>UninitValue</kind>");
2417
 
            VG_(message)(Vg_UserMsg,
2418
 
                         "%sUse of uninitialised value of size %d%s",
2419
 
                         xpre, err_extra->size, xpost);
2420
 
         }
2421
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2422
 
         break;
2423
 
 
2424
 
      case ParamErr: {
2425
 
         Bool isReg = ( Register == err_extra->addrinfo.akind );
2426
 
         Char* s1 = ( isReg ? "contains" : "points to" );
2427
 
         Char* s2 = ( err_extra->isUnaddr ? "unaddressable" : "uninitialised" );
2428
 
         if (isReg) tl_assert(!err_extra->isUnaddr);
2429
 
 
2430
 
         if (VG_(clo_xml))
2431
 
            VG_(message)(Vg_UserMsg, "  <kind>SyscallParam</kind>");
2432
 
         VG_(message)(Vg_UserMsg, "%sSyscall param %s %s %s byte(s)%s",
2433
 
                      xpre, VG_(get_error_string)(err), s1, s2, xpost);
2434
 
 
2435
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2436
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2437
 
         break;
2438
 
      }
2439
 
      case UserErr: {
2440
 
         Char* s = ( err_extra->isUnaddr ? "Unaddressable" : "Uninitialised" );
2441
 
 
2442
 
         if (VG_(clo_xml))
2443
 
            VG_(message)(Vg_UserMsg, "  <kind>ClientCheck</kind>");
2444
 
         VG_(message)(Vg_UserMsg, 
2445
 
            "%s%s byte(s) found during client check request%s", 
2446
 
            xpre, s, xpost);
2447
 
 
2448
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2449
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2450
 
         break;
2451
 
      }
2452
 
      case FreeErr:
2453
 
         if (VG_(clo_xml))
2454
 
            VG_(message)(Vg_UserMsg, "  <kind>InvalidFree</kind>");
2455
 
         VG_(message)(Vg_UserMsg, 
2456
 
                      "%sInvalid free() / delete / delete[]%s",
2457
 
                      xpre, xpost);
2458
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2459
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2460
 
         break;
2461
 
 
2462
 
      case FreeMismatchErr:
2463
 
         if (VG_(clo_xml))
2464
 
            VG_(message)(Vg_UserMsg, "  <kind>MismatchedFree</kind>");
2465
 
         VG_(message)(Vg_UserMsg, 
2466
 
                      "%sMismatched free() / delete / delete []%s",
2467
 
                      xpre, xpost);
2468
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2469
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2470
 
         break;
2471
 
 
2472
 
      case AddrErr:
2473
 
         switch (err_extra->axskind) {
2474
 
            case ReadAxs:
2475
 
               if (VG_(clo_xml))
2476
 
                  VG_(message)(Vg_UserMsg, "  <kind>InvalidRead</kind>");
2477
 
               VG_(message)(Vg_UserMsg,
2478
 
                            "%sInvalid read of size %d%s", 
2479
 
                            xpre, err_extra->size, xpost ); 
2480
 
               break;
2481
 
            case WriteAxs:
2482
 
               if (VG_(clo_xml))
2483
 
                  VG_(message)(Vg_UserMsg, "  <kind>InvalidWrite</kind>");
2484
 
               VG_(message)(Vg_UserMsg, 
2485
 
                           "%sInvalid write of size %d%s", 
2486
 
                           xpre, err_extra->size, xpost ); 
2487
 
               break;
2488
 
            case ExecAxs:
2489
 
               if (VG_(clo_xml))
2490
 
                  VG_(message)(Vg_UserMsg, "  <kind>InvalidJump</kind>");
2491
 
               VG_(message)(Vg_UserMsg, 
2492
 
                            "%sJump to the invalid address "
2493
 
                            "stated on the next line%s",
2494
 
                            xpre, xpost);
2495
 
               break;
2496
 
            default: 
2497
 
               VG_(tool_panic)("mc_pp_Error(axskind)");
2498
 
         }
2499
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2500
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2501
 
         break;
2502
 
 
2503
 
      case OverlapErr: {
2504
 
         OverlapExtra* ov_extra = (OverlapExtra*)VG_(get_error_extra)(err);
2505
 
         if (VG_(clo_xml))
2506
 
            VG_(message)(Vg_UserMsg, "  <kind>Overlap</kind>");
2507
 
         if (ov_extra->len == -1)
2508
 
            VG_(message)(Vg_UserMsg,
2509
 
                         "%sSource and destination overlap in %s(%p, %p)%s",
2510
 
                         xpre,
2511
 
                         VG_(get_error_string)(err),
2512
 
                         ov_extra->dst, ov_extra->src,
2513
 
                         xpost);
 
2954
            mc_pp_msg("InvalidRead", err,
 
2955
                      "Invalid read of size %d", 
 
2956
                      extra->Err.Addr.szB); 
 
2957
         }
 
2958
         mc_pp_AddrInfo(VG_(get_error_address)(err), &extra->Err.Addr.ai,
 
2959
                        extra->Err.Addr.maybe_gcc);
 
2960
         break;
 
2961
 
 
2962
      case Err_Jump:
 
2963
         mc_pp_msg("InvalidJump", err,
 
2964
                   "Jump to the invalid address stated on the next line");
 
2965
         mc_pp_AddrInfo(VG_(get_error_address)(err), &extra->Err.Jump.ai,
 
2966
                        False);
 
2967
         break;
 
2968
 
 
2969
      case Err_Overlap:
 
2970
         if (extra->Err.Overlap.szB == 0)
 
2971
            mc_pp_msg("Overlap", err,
 
2972
                      "Source and destination overlap in %s(%p, %p)",
 
2973
                      VG_(get_error_string)(err),
 
2974
                      extra->Err.Overlap.dst, extra->Err.Overlap.src);
2514
2975
         else
2515
 
            VG_(message)(Vg_UserMsg,
2516
 
                         "%sSource and destination overlap in %s(%p, %p, %d)%s",
2517
 
                         xpre,
2518
 
                         VG_(get_error_string)(err),
2519
 
                         ov_extra->dst, ov_extra->src, ov_extra->len,
2520
 
                         xpost);
2521
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2522
 
         break;
2523
 
      }
2524
 
      case LeakErr: {
2525
 
         MC_(pp_LeakError)(err_extra);
2526
 
         break;
2527
 
      }
2528
 
 
2529
 
      case IllegalMempoolErr:
2530
 
         if (VG_(clo_xml))
2531
 
            VG_(message)(Vg_UserMsg, "  <kind>InvalidMemPool</kind>");
2532
 
         VG_(message)(Vg_UserMsg, "%sIllegal memory pool address%s",
2533
 
                                  xpre, xpost);
2534
 
         VG_(pp_ExeContext)( VG_(get_error_where)(err) );
2535
 
         mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo);
2536
 
         break;
 
2976
            mc_pp_msg("Overlap", err,
 
2977
                      "Source and destination overlap in %s(%p, %p, %d)",
 
2978
                      VG_(get_error_string)(err),
 
2979
                      extra->Err.Overlap.dst, extra->Err.Overlap.src,
 
2980
                      extra->Err.Overlap.szB);
 
2981
         break;
 
2982
 
 
2983
      case Err_IllegalMempool:
 
2984
         mc_pp_msg("InvalidMemPool", err,
 
2985
                   "Illegal memory pool address");
 
2986
         mc_pp_AddrInfo(VG_(get_error_address)(err),
 
2987
                        &extra->Err.IllegalMempool.ai, False);
 
2988
         break;
 
2989
 
 
2990
      case Err_Leak: {
 
2991
         HChar*      xpre  = VG_(clo_xml) ? "  <what>" : "";
 
2992
         HChar*      xpost = VG_(clo_xml) ? "</what>"  : "";
 
2993
         UInt        n_this_record   = extra->Err.Leak.n_this_record;
 
2994
         UInt        n_total_records = extra->Err.Leak.n_total_records;
 
2995
         LossRecord* l               = extra->Err.Leak.lossRecord;
 
2996
 
 
2997
         if (VG_(clo_xml)) {
 
2998
            VG_(message)(Vg_UserMsg, "  <kind>%t</kind>",
 
2999
                         xml_leak_kind(l->loss_mode));
 
3000
         } else {
 
3001
            VG_(message)(Vg_UserMsg, "");
 
3002
         }
 
3003
 
 
3004
         if (l->indirect_bytes) {
 
3005
            VG_(message)(Vg_UserMsg, 
 
3006
               "%s%,lu (%,lu direct, %,lu indirect) bytes in %,u blocks"
 
3007
               " are %s in loss record %,u of %,u%s",
 
3008
               xpre,
 
3009
               l->total_bytes + l->indirect_bytes, 
 
3010
               l->total_bytes, l->indirect_bytes, l->num_blocks,
 
3011
               str_leak_lossmode(l->loss_mode), n_this_record, n_total_records,
 
3012
               xpost
 
3013
            );
 
3014
            if (VG_(clo_xml)) {
 
3015
               // Nb: don't put commas in these XML numbers 
 
3016
               VG_(message)(Vg_UserMsg, "  <leakedbytes>%lu</leakedbytes>", 
 
3017
                                        l->total_bytes + l->indirect_bytes);
 
3018
               VG_(message)(Vg_UserMsg, "  <leakedblocks>%u</leakedblocks>", 
 
3019
                                        l->num_blocks);
 
3020
            }
 
3021
         } else {
 
3022
            VG_(message)(
 
3023
               Vg_UserMsg, 
 
3024
               "%s%,lu bytes in %,u blocks are %s in loss record %,u of %,u%s",
 
3025
               xpre,
 
3026
               l->total_bytes, l->num_blocks,
 
3027
               str_leak_lossmode(l->loss_mode), n_this_record, n_total_records,
 
3028
               xpost
 
3029
            );
 
3030
            if (VG_(clo_xml)) {
 
3031
               VG_(message)(Vg_UserMsg, "  <leakedbytes>%d</leakedbytes>", 
 
3032
                                        l->total_bytes);
 
3033
               VG_(message)(Vg_UserMsg, "  <leakedblocks>%d</leakedblocks>", 
 
3034
                                        l->num_blocks);
 
3035
            }
 
3036
         }
 
3037
         VG_(pp_ExeContext)(l->allocated_at);
 
3038
         break;
 
3039
      }
2537
3040
 
2538
3041
      default: 
2539
3042
         VG_(printf)("Error:\n  unknown Memcheck error code %d\n",
2560
3063
      return False;
2561
3064
}
2562
3065
 
2563
 
static void mc_clear_MC_Error ( MC_Error* err_extra )
2564
 
{
2565
 
   err_extra->axskind             = ReadAxs;
2566
 
   err_extra->size                = 0;
2567
 
   err_extra->isUnaddr            = True;
2568
 
   err_extra->addrinfo.akind      = Unknown;
2569
 
   err_extra->addrinfo.blksize    = 0;
2570
 
   err_extra->addrinfo.rwoffset   = 0;
2571
 
   err_extra->addrinfo.lastchange = NULL;
2572
 
   err_extra->addrinfo.stack_tid  = VG_INVALID_THREADID;
2573
 
   err_extra->addrinfo.maybe_gcc  = False;
2574
 
   err_extra->addrinfo.desc       = NULL;
2575
 
}
 
3066
/* --- Called from generated and non-generated code --- */
2576
3067
 
2577
 
/* This one called from generated code and non-generated code. */
2578
 
static void mc_record_address_error ( ThreadId tid, Addr a, Int size,
 
3068
static void mc_record_address_error ( ThreadId tid, Addr a, Int szB,
2579
3069
                                      Bool isWrite )
2580
3070
{
2581
 
   MC_Error err_extra;
2582
 
   Bool      just_below_esp;
 
3071
   MC_Error extra;
 
3072
   Bool     just_below_esp;
 
3073
 
 
3074
   if (in_ignored_range(a)) 
 
3075
      return;
 
3076
 
 
3077
#  if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5)
 
3078
   /* AIX zero-page handling.  On AIX, reads from page zero are,
 
3079
      bizarrely enough, legitimate.  Writes to page zero aren't,
 
3080
      though.  Since memcheck can't distinguish reads from writes, the
 
3081
      best we can do is to 'act normal' and mark the A bits in the
 
3082
      normal way as noaccess, but then hide any reads from that page
 
3083
      that get reported here. */
 
3084
   if ((!isWrite) && a >= 0 && a < 4096 && a+szB <= 4096) 
 
3085
      return;
 
3086
 
 
3087
   /* Appalling AIX hack.  It suppresses reads done by glink
 
3088
      fragments.  Getting rid of this would require figuring out
 
3089
      somehow where the referenced data areas are (and their
 
3090
      sizes). */
 
3091
   if ((!isWrite) && szB == sizeof(Word)) { 
 
3092
      UInt i1, i2;
 
3093
      UInt* pc = (UInt*)VG_(get_IP)(tid);
 
3094
      if (sizeof(Word) == 4) {
 
3095
         i1 = 0x800c0000; /* lwz r0,0(r12) */
 
3096
         i2 = 0x804c0004; /* lwz r2,4(r12) */
 
3097
      } else {
 
3098
         i1 = 0xe80c0000; /* ld  r0,0(r12) */
 
3099
         i2 = 0xe84c0008; /* ld  r2,8(r12) */
 
3100
      }
 
3101
      if (pc[0] == i1 && pc[1] == i2) return;
 
3102
      if (pc[0] == i2 && pc[-1] == i1) return;
 
3103
   }
 
3104
#  endif
2583
3105
 
2584
3106
   just_below_esp = is_just_below_ESP( VG_(get_SP)(tid), a );
2585
3107
 
2588
3110
   if (MC_(clo_workaround_gcc296_bugs) && just_below_esp)
2589
3111
      return;
2590
3112
 
2591
 
   mc_clear_MC_Error( &err_extra );
2592
 
   err_extra.axskind = isWrite ? WriteAxs : ReadAxs;
2593
 
   err_extra.size    = size;
2594
 
   err_extra.addrinfo.akind     = Undescribed;
2595
 
   err_extra.addrinfo.maybe_gcc = just_below_esp;
2596
 
   VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra );
2597
 
}
2598
 
 
2599
 
/* These ones are called from non-generated code */
 
3113
   extra.Err.Addr.isWrite   = isWrite;
 
3114
   extra.Err.Addr.szB       = szB;
 
3115
   extra.Err.Addr.maybe_gcc = just_below_esp;
 
3116
   extra.Err.Addr.ai.tag    = Addr_Undescribed;
 
3117
   VG_(maybe_record_error)( tid, Err_Addr, a, /*s*/NULL, &extra );
 
3118
}
 
3119
 
 
3120
static void mc_record_value_error ( ThreadId tid, Int szB )
 
3121
{
 
3122
   MC_Error extra;
 
3123
   tl_assert(MC_(clo_undef_value_errors));
 
3124
   extra.Err.Value.szB = szB;
 
3125
   VG_(maybe_record_error)( tid, Err_Value, /*addr*/0, /*s*/NULL, &extra );
 
3126
}
 
3127
 
 
3128
static void mc_record_cond_error ( ThreadId tid )
 
3129
{
 
3130
   tl_assert(MC_(clo_undef_value_errors));
 
3131
   VG_(maybe_record_error)( tid, Err_Cond, /*addr*/0, /*s*/NULL, /*extra*/NULL);
 
3132
}
 
3133
 
 
3134
/* --- Called from non-generated code --- */
2600
3135
 
2601
3136
/* This is for memory errors in pthread functions, as opposed to pthread API
2602
3137
   errors which are found by the core. */
2603
 
static void mc_record_core_mem_error ( ThreadId tid, Bool isUnaddr, Char* msg )
2604
 
{
2605
 
   MC_Error err_extra;
2606
 
 
2607
 
   mc_clear_MC_Error( &err_extra );
2608
 
   err_extra.isUnaddr = isUnaddr;
2609
 
   VG_(maybe_record_error)( tid, CoreMemErr, /*addr*/0, msg, &err_extra );
2610
 
}
2611
 
 
2612
 
// Three kinds of param errors:
2613
 
// - register arg contains undefined bytes
2614
 
// - memory arg is unaddressable
2615
 
// - memory arg contains undefined bytes
2616
 
// 'isReg' and 'isUnaddr' dictate which of these it is.
2617
 
static void mc_record_param_error ( ThreadId tid, Addr a, Bool isReg,
2618
 
                                    Bool isUnaddr, Char* msg )
2619
 
{
2620
 
   MC_Error err_extra;
2621
 
 
2622
 
   if (!isUnaddr) tl_assert(MC_(clo_undef_value_errors));
2623
 
   tl_assert(VG_INVALID_THREADID != tid);
2624
 
   if (isUnaddr) tl_assert(!isReg);    // unaddressable register is impossible
2625
 
   mc_clear_MC_Error( &err_extra );
2626
 
   err_extra.addrinfo.akind = ( isReg ? Register : Undescribed );
2627
 
   err_extra.isUnaddr = isUnaddr;
2628
 
   VG_(maybe_record_error)( tid, ParamErr, a, msg, &err_extra );
 
3138
static void mc_record_core_mem_error ( ThreadId tid, Bool isAddrErr, Char* msg )
 
3139
{
 
3140
   VG_(maybe_record_error)( tid, Err_CoreMem, /*addr*/0, msg, /*extra*/NULL );
 
3141
}
 
3142
 
 
3143
static void mc_record_regparam_error ( ThreadId tid, Char* msg )
 
3144
{
 
3145
   tl_assert(VG_INVALID_THREADID != tid);
 
3146
   VG_(maybe_record_error)( tid, Err_RegParam, /*addr*/0, msg, /*extra*/NULL );
 
3147
}
 
3148
 
 
3149
static void mc_record_memparam_error ( ThreadId tid, Addr a, 
 
3150
                                       Bool isAddrErr, Char* msg )
 
3151
{
 
3152
   MC_Error extra;
 
3153
   tl_assert(VG_INVALID_THREADID != tid);
 
3154
   if (!isAddrErr) 
 
3155
      tl_assert(MC_(clo_undef_value_errors));
 
3156
   extra.Err.MemParam.isAddrErr = isAddrErr;
 
3157
   extra.Err.MemParam.ai.tag    = Addr_Undescribed;
 
3158
   VG_(maybe_record_error)( tid, Err_MemParam, a, msg, &extra );
2629
3159
}
2630
3160
 
2631
3161
static void mc_record_jump_error ( ThreadId tid, Addr a )
2632
3162
{
2633
 
   MC_Error err_extra;
2634
 
 
 
3163
   MC_Error extra;
2635
3164
   tl_assert(VG_INVALID_THREADID != tid);
2636
 
   mc_clear_MC_Error( &err_extra );
2637
 
   err_extra.axskind = ExecAxs;
2638
 
   err_extra.size    = 1;     // size only used for suppressions
2639
 
   err_extra.addrinfo.akind = Undescribed;
2640
 
   VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra );
 
3165
   extra.Err.Jump.ai.tag = Addr_Undescribed;
 
3166
   VG_(maybe_record_error)( tid, Err_Jump, a, /*s*/NULL, &extra );
2641
3167
}
2642
3168
 
2643
3169
void MC_(record_free_error) ( ThreadId tid, Addr a ) 
2644
3170
{
2645
 
   MC_Error err_extra;
 
3171
   MC_Error extra;
 
3172
   tl_assert(VG_INVALID_THREADID != tid);
 
3173
   extra.Err.Free.ai.tag = Addr_Undescribed;
 
3174
   VG_(maybe_record_error)( tid, Err_Free, a, /*s*/NULL, &extra );
 
3175
}
2646
3176
 
 
3177
void MC_(record_freemismatch_error) ( ThreadId tid, MC_Chunk* mc )
 
3178
{
 
3179
   MC_Error extra;
 
3180
   AddrInfo* ai = &extra.Err.FreeMismatch.ai;
2647
3181
   tl_assert(VG_INVALID_THREADID != tid);
2648
 
   mc_clear_MC_Error( &err_extra );
2649
 
   err_extra.addrinfo.akind = Undescribed;
2650
 
   VG_(maybe_record_error)( tid, FreeErr, a, /*s*/NULL, &err_extra );
 
3182
   ai->tag = Addr_Block;
 
3183
   ai->Addr.Block.block_kind = Block_Mallocd;  // Nb: Not 'Block_Freed'
 
3184
   ai->Addr.Block.block_desc = "block";
 
3185
   ai->Addr.Block.block_szB  = mc->szB;
 
3186
   ai->Addr.Block.rwoffset   = 0;
 
3187
   ai->Addr.Block.lastchange = mc->where;
 
3188
   VG_(maybe_record_error)( tid, Err_FreeMismatch, mc->data, /*s*/NULL,
 
3189
                            &extra );
2651
3190
}
2652
3191
 
2653
3192
void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a ) 
2654
3193
{
2655
 
   MC_Error err_extra;
2656
 
 
2657
 
   tl_assert(VG_INVALID_THREADID != tid);
2658
 
   mc_clear_MC_Error( &err_extra );
2659
 
   err_extra.addrinfo.akind = Undescribed;
2660
 
   VG_(maybe_record_error)( tid, IllegalMempoolErr, a, /*s*/NULL, &err_extra );
2661
 
}
2662
 
 
2663
 
void MC_(record_freemismatch_error) ( ThreadId tid, Addr a, MC_Chunk* mc )
2664
 
{
2665
 
   MC_Error err_extra;
2666
 
   AddrInfo* ai;
2667
 
 
2668
 
   tl_assert(VG_INVALID_THREADID != tid);
2669
 
   mc_clear_MC_Error( &err_extra );
2670
 
   ai = &err_extra.addrinfo;
2671
 
   ai->akind      = Mallocd;     // Nb: not 'Freed'
2672
 
   ai->blksize    = mc->size;
2673
 
   ai->rwoffset   = (Int)a - (Int)mc->data;
2674
 
   ai->lastchange = mc->where;
2675
 
   VG_(maybe_record_error)( tid, FreeMismatchErr, a, /*s*/NULL, &err_extra );
2676
 
}
2677
 
 
2678
 
static void mc_record_overlap_error ( ThreadId tid, 
2679
 
                                      Char* function, OverlapExtra* ov_extra )
2680
 
{
 
3194
   MC_Error extra;
 
3195
   tl_assert(VG_INVALID_THREADID != tid);
 
3196
   extra.Err.IllegalMempool.ai.tag = Addr_Undescribed;
 
3197
   VG_(maybe_record_error)( tid, Err_IllegalMempool, a, /*s*/NULL, &extra );
 
3198
}
 
3199
 
 
3200
static void mc_record_overlap_error ( ThreadId tid, Char* function,
 
3201
                                      Addr src, Addr dst, SizeT szB )
 
3202
{
 
3203
   MC_Error extra;
 
3204
   tl_assert(VG_INVALID_THREADID != tid);
 
3205
   extra.Err.Overlap.src = src;
 
3206
   extra.Err.Overlap.dst = dst;
 
3207
   extra.Err.Overlap.szB = szB;
2681
3208
   VG_(maybe_record_error)( 
2682
 
      tid, OverlapErr, /*addr*/0, /*s*/function, ov_extra );
 
3209
      tid, Err_Overlap, /*addr*/0, /*s*/function, &extra );
2683
3210
}
2684
3211
 
2685
 
Bool MC_(record_leak_error) ( ThreadId tid, /*LeakExtra*/void* leak_extra,
2686
 
                              ExeContext* where, Bool print_record )
 
3212
Bool MC_(record_leak_error) ( ThreadId tid, UInt n_this_record,
 
3213
                              UInt n_total_records, LossRecord* lossRecord,
 
3214
                              Bool print_record )
2687
3215
{
 
3216
   MC_Error extra;
 
3217
   extra.Err.Leak.n_this_record   = n_this_record;
 
3218
   extra.Err.Leak.n_total_records = n_total_records;
 
3219
   extra.Err.Leak.lossRecord      = lossRecord;
2688
3220
   return
2689
 
   VG_(unique_error) ( tid, LeakErr, /*Addr*/0, /*s*/NULL,
2690
 
                       /*extra*/leak_extra, where, print_record,
 
3221
   VG_(unique_error) ( tid, Err_Leak, /*Addr*/0, /*s*/NULL, &extra,
 
3222
                       lossRecord->allocated_at, print_record,
2691
3223
                       /*allow_GDB_attach*/False, /*count_error*/False );
2692
3224
}
2693
3225
 
2694
 
 
2695
 
/* Creates a copy of the 'extra' part, updates the copy with address info if
2696
 
   necessary, and returns the copy. */
2697
 
/* This one called from generated code and non-generated code. */
2698
 
static void mc_record_value_error ( ThreadId tid, Int size )
2699
 
{
2700
 
   MC_Error err_extra;
2701
 
 
2702
 
   tl_assert(MC_(clo_undef_value_errors));
2703
 
   mc_clear_MC_Error( &err_extra );
2704
 
   err_extra.size     = size;
2705
 
   err_extra.isUnaddr = False;
2706
 
   VG_(maybe_record_error)( tid, ValueErr, /*addr*/0, /*s*/NULL, &err_extra );
2707
 
}
2708
 
 
2709
 
/* This called from non-generated code */
2710
 
 
2711
 
static void mc_record_user_error ( ThreadId tid, Addr a, Bool isWrite,
2712
 
                                   Bool isUnaddr )
2713
 
{
2714
 
   MC_Error err_extra;
 
3226
static void mc_record_user_error ( ThreadId tid, Addr a, Bool isAddrErr )
 
3227
{
 
3228
   MC_Error extra;
2715
3229
 
2716
3230
   tl_assert(VG_INVALID_THREADID != tid);
2717
 
   mc_clear_MC_Error( &err_extra );
2718
 
   err_extra.addrinfo.akind = Undescribed;
2719
 
   err_extra.isUnaddr       = isUnaddr;
2720
 
   VG_(maybe_record_error)( tid, UserErr, a, /*s*/NULL, &err_extra );
 
3231
   extra.Err.User.isAddrErr = isAddrErr;
 
3232
   extra.Err.User.ai.tag    = Addr_Undescribed;
 
3233
   VG_(maybe_record_error)( tid, Err_User, a, /*s*/NULL, &extra );
2721
3234
}
2722
3235
 
2723
 
__attribute__ ((unused))
2724
 
static Bool eq_AddrInfo ( VgRes res, AddrInfo* ai1, AddrInfo* ai2 )
2725
 
{
2726
 
   if (ai1->akind != Undescribed 
2727
 
       && ai2->akind != Undescribed
2728
 
       && ai1->akind != ai2->akind) 
2729
 
      return False;
2730
 
   if (ai1->akind == Freed || ai1->akind == Mallocd) {
2731
 
      if (ai1->blksize != ai2->blksize)
2732
 
         return False;
2733
 
      if (!VG_(eq_ExeContext)(res, ai1->lastchange, ai2->lastchange))
2734
 
         return False;
2735
 
   }
2736
 
   return True;
2737
 
}
 
3236
/*------------------------------------------------------------*/
 
3237
/*--- Other error operations                               ---*/
 
3238
/*------------------------------------------------------------*/
2738
3239
 
2739
3240
/* Compare error contexts, to detect duplicates.  Note that if they
2740
3241
   are otherwise the same, the faulting addrs and associated rwoffsets
2741
3242
   are allowed to be different.  */
2742
3243
static Bool mc_eq_Error ( VgRes res, Error* e1, Error* e2 )
2743
3244
{
2744
 
   MC_Error* e1_extra = VG_(get_error_extra)(e1);
2745
 
   MC_Error* e2_extra = VG_(get_error_extra)(e2);
 
3245
   MC_Error* extra1 = VG_(get_error_extra)(e1);
 
3246
   MC_Error* extra2 = VG_(get_error_extra)(e2);
2746
3247
 
2747
3248
   /* Guaranteed by calling function */
2748
3249
   tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2));
2749
3250
   
2750
3251
   switch (VG_(get_error_kind)(e1)) {
2751
 
      case CoreMemErr: {
 
3252
      case Err_CoreMem: {
2752
3253
         Char *e1s, *e2s;
2753
 
         if (e1_extra->isUnaddr != e2_extra->isUnaddr) return False;
2754
3254
         e1s = VG_(get_error_string)(e1);
2755
3255
         e2s = VG_(get_error_string)(e2);
2756
 
         if (e1s == e2s)                               return True;
2757
 
         if (0 == VG_(strcmp)(e1s, e2s))               return True;
 
3256
         if (e1s == e2s)                   return True;
 
3257
         if (VG_STREQ(e1s, e2s))           return True;
2758
3258
         return False;
2759
3259
      }
2760
3260
 
 
3261
      case Err_RegParam:
 
3262
         return VG_STREQ(VG_(get_error_string)(e1), VG_(get_error_string)(e2));
 
3263
 
2761
3264
      // Perhaps we should also check the addrinfo.akinds for equality.
2762
3265
      // That would result in more error reports, but only in cases where
2763
3266
      // a register contains uninitialised bytes and points to memory
2764
3267
      // containing uninitialised bytes.  Currently, the 2nd of those to be
2765
3268
      // detected won't be reported.  That is (nearly?) always the memory
2766
3269
      // error, which is good.
2767
 
      case ParamErr:
2768
 
         if (0 != VG_(strcmp)(VG_(get_error_string)(e1),
2769
 
                              VG_(get_error_string)(e2)))   return False;
 
3270
      case Err_MemParam:
 
3271
         if (!VG_STREQ(VG_(get_error_string)(e1),
 
3272
                       VG_(get_error_string)(e2))) return False;
2770
3273
         // fall through
2771
 
      case UserErr:
2772
 
         if (e1_extra->isUnaddr != e2_extra->isUnaddr)      return False;
2773
 
         return True;
2774
 
 
2775
 
      case FreeErr:
2776
 
      case FreeMismatchErr:
2777
 
         /* JRS 2002-Aug-26: comparing addrs seems overkill and can
2778
 
            cause excessive duplication of errors.  Not even AddrErr
2779
 
            below does that.  So don't compare either the .addr field
2780
 
            or the .addrinfo fields. */
2781
 
         /* if (e1->addr != e2->addr) return False; */
2782
 
         /* if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) 
2783
 
               return False;
2784
 
         */
2785
 
         return True;
2786
 
 
2787
 
      case AddrErr:
2788
 
         /* if (e1_extra->axskind != e2_extra->axskind) return False; */
2789
 
         if (e1_extra->size != e2_extra->size) return False;
2790
 
         /*
2791
 
         if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) 
2792
 
            return False;
2793
 
         */
2794
 
         return True;
2795
 
 
2796
 
      case ValueErr:
2797
 
         if (e1_extra->size != e2_extra->size) return False;
2798
 
         return True;
2799
 
 
2800
 
      case OverlapErr:
2801
 
         return True;
2802
 
 
2803
 
      case LeakErr:
2804
 
         VG_(tool_panic)("Shouldn't get LeakErr in mc_eq_Error,\n"
 
3274
      case Err_User:
 
3275
         return ( extra1->Err.User.isAddrErr == extra2->Err.User.isAddrErr
 
3276
                ? True : False );
 
3277
 
 
3278
      case Err_Free:
 
3279
      case Err_FreeMismatch:
 
3280
      case Err_Jump:
 
3281
      case Err_IllegalMempool:
 
3282
      case Err_Overlap:
 
3283
      case Err_Cond:
 
3284
         return True;
 
3285
 
 
3286
      case Err_Addr:
 
3287
         return ( extra1->Err.Addr.szB == extra2->Err.Addr.szB
 
3288
                ? True : False );
 
3289
 
 
3290
      case Err_Value:
 
3291
         return ( extra1->Err.Value.szB == extra2->Err.Value.szB
 
3292
                ? True : False );
 
3293
 
 
3294
      case Err_Leak:
 
3295
         VG_(tool_panic)("Shouldn't get Err_Leak in mc_eq_Error,\n"
2805
3296
                         "since it's handled with VG_(unique_error)()!");
2806
3297
 
2807
 
      case IllegalMempoolErr:
2808
 
         return True;
2809
 
 
2810
3298
      default: 
2811
3299
         VG_(printf)("Error:\n  unknown error code %d\n",
2812
3300
                     VG_(get_error_kind)(e1));
2823
3311
   // in some cases this could result in an incorrect description (eg.
2824
3312
   // saying "12 bytes after block A" when really it's within block B.
2825
3313
   // Fixing would require adding redzone size to MC_Chunks, though.
2826
 
   return VG_(addr_is_in_block)( a, mc->data, mc->size,
 
3314
   return VG_(addr_is_in_block)( a, mc->data, mc->szB,
2827
3315
                                 MC_MALLOC_REDZONE_SZB );
2828
3316
}
2829
3317
 
2830
3318
// Forward declaration
2831
3319
static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai );
2832
3320
 
 
3321
 
2833
3322
/* Describe an address as best you can, for error messages,
2834
3323
   putting the result in ai. */
2835
3324
static void describe_addr ( Addr a, AddrInfo* ai )
2836
3325
{
2837
3326
   MC_Chunk* mc;
2838
 
   ThreadId   tid;
2839
 
   Addr       stack_min, stack_max;
 
3327
   ThreadId  tid;
 
3328
   Addr      stack_min, stack_max;
 
3329
 
 
3330
   tl_assert(Addr_Undescribed == ai->tag);
2840
3331
 
2841
3332
   /* Perhaps it's a user-def'd block? */
2842
3333
   if (client_perm_maybe_describe( a, ai ))
2846
3337
   VG_(thread_stack_reset_iter)();
2847
3338
   while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) {
2848
3339
      if (stack_min <= a && a <= stack_max) {
2849
 
         ai->akind     = Stack;
2850
 
         ai->stack_tid = tid;
 
3340
         ai->tag            = Addr_Stack;
 
3341
         ai->Addr.Stack.tid = tid;
2851
3342
         return;
2852
3343
      }
2853
3344
   }
2855
3346
   mc = MC_(get_freed_list_head)();
2856
3347
   while (mc) {
2857
3348
      if (addr_is_in_MC_Chunk(mc, a)) {
2858
 
         ai->akind      = Freed;
2859
 
         ai->blksize    = mc->size;
2860
 
         ai->rwoffset   = (Int)a - (Int)mc->data;
2861
 
         ai->lastchange = mc->where;
 
3349
         ai->tag = Addr_Block;
 
3350
         ai->Addr.Block.block_kind = Block_Freed;
 
3351
         ai->Addr.Block.block_desc = "block";
 
3352
         ai->Addr.Block.block_szB  = mc->szB;
 
3353
         ai->Addr.Block.rwoffset   = (Int)a - (Int)mc->data;
 
3354
         ai->Addr.Block.lastchange = mc->where;
2862
3355
         return;
2863
3356
      }
2864
3357
      mc = mc->next; 
2867
3360
   VG_(HT_ResetIter)(MC_(malloc_list));
2868
3361
   while ( (mc = VG_(HT_Next)(MC_(malloc_list))) ) {
2869
3362
      if (addr_is_in_MC_Chunk(mc, a)) {
2870
 
         ai->akind      = Mallocd;
2871
 
         ai->blksize    = mc->size;
2872
 
         ai->rwoffset   = (Int)(a) - (Int)mc->data;
2873
 
         ai->lastchange = mc->where;
 
3363
         ai->tag = Addr_Block;
 
3364
         ai->Addr.Block.block_kind = Block_Mallocd;
 
3365
         ai->Addr.Block.block_desc = "block";
 
3366
         ai->Addr.Block.block_szB  = mc->szB;
 
3367
         ai->Addr.Block.rwoffset   = (Int)a - (Int)mc->data;
 
3368
         ai->Addr.Block.lastchange = mc->where;
2874
3369
         return;
2875
3370
      }
2876
3371
   }
2877
3372
   /* Clueless ... */
2878
 
   ai->akind = Unknown;
 
3373
   ai->tag = Addr_Unknown;
2879
3374
   return;
2880
3375
}
2881
3376
 
2882
3377
/* Updates the copy with address info if necessary (but not for all errors). */
2883
3378
static UInt mc_update_extra( Error* err )
2884
3379
{
 
3380
   MC_Error* extra = VG_(get_error_extra)(err);
 
3381
 
2885
3382
   switch (VG_(get_error_kind)(err)) {
2886
 
   // These two don't have addresses associated with them, and so don't
 
3383
   // These ones don't have addresses associated with them, and so don't
2887
3384
   // need any updating.
2888
 
   case CoreMemErr:
2889
 
   case ValueErr: {
2890
 
      MC_Error* extra = VG_(get_error_extra)(err);
2891
 
      tl_assert(Unknown == extra->addrinfo.akind);
2892
 
      return sizeof(MC_Error);
2893
 
   }
2894
 
 
2895
 
   // ParamErrs sometimes involve a memory address; call describe_addr() in
2896
 
   // this case.
2897
 
   case ParamErr: {
2898
 
      MC_Error* extra = VG_(get_error_extra)(err);
2899
 
      tl_assert(Undescribed == extra->addrinfo.akind ||
2900
 
                Register    == extra->addrinfo.akind);
2901
 
      if (Undescribed == extra->addrinfo.akind)
2902
 
         describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) );
2903
 
      return sizeof(MC_Error);
2904
 
   }
2905
 
 
2906
 
   // These four always involve a memory address.
2907
 
   case AddrErr: 
2908
 
   case UserErr:
2909
 
   case FreeErr:
2910
 
   case IllegalMempoolErr: {
2911
 
      MC_Error* extra = VG_(get_error_extra)(err);
2912
 
      tl_assert(Undescribed == extra->addrinfo.akind);
2913
 
      describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) );
2914
 
      return sizeof(MC_Error);
2915
 
   }
2916
 
 
2917
 
   // FreeMismatchErrs have already had their address described;  this is
 
3385
   case Err_CoreMem:
 
3386
   case Err_Value:
 
3387
   case Err_Cond:
 
3388
   case Err_Overlap:
 
3389
   case Err_RegParam:
 
3390
   // For Err_Leaks the returned size does not matter -- they are always
 
3391
   // shown with VG_(unique_error)() so they 'extra' not copied.  But we make it
 
3392
   // consistent with the others.
 
3393
   case Err_Leak:
 
3394
      return sizeof(MC_Error);
 
3395
 
 
3396
   // These ones always involve a memory address.
 
3397
   case Err_Addr:
 
3398
      describe_addr ( VG_(get_error_address)(err), &extra->Err.Addr.ai );
 
3399
      return sizeof(MC_Error);
 
3400
   case Err_MemParam:
 
3401
      describe_addr ( VG_(get_error_address)(err), &extra->Err.MemParam.ai );
 
3402
      return sizeof(MC_Error);
 
3403
   case Err_Jump:
 
3404
      describe_addr ( VG_(get_error_address)(err), &extra->Err.Jump.ai );
 
3405
      return sizeof(MC_Error);
 
3406
   case Err_User:
 
3407
      describe_addr ( VG_(get_error_address)(err), &extra->Err.User.ai );
 
3408
      return sizeof(MC_Error);
 
3409
   case Err_Free:
 
3410
      describe_addr ( VG_(get_error_address)(err), &extra->Err.Free.ai );
 
3411
      return sizeof(MC_Error);
 
3412
   case Err_IllegalMempool:
 
3413
      describe_addr ( VG_(get_error_address)(err),
 
3414
                      &extra->Err.IllegalMempool.ai );
 
3415
      return sizeof(MC_Error);
 
3416
 
 
3417
   // Err_FreeMismatches have already had their address described;  this is
2918
3418
   // possible because we have the MC_Chunk on hand when the error is
2919
3419
   // detected.  However, the address may be part of a user block, and if so
2920
3420
   // we override the pre-determined description with a user block one.
2921
 
   case FreeMismatchErr: {
2922
 
      MC_Error* extra = VG_(get_error_extra)(err);
2923
 
      tl_assert(extra && Mallocd == extra->addrinfo.akind);
 
3421
   case Err_FreeMismatch: {
 
3422
      tl_assert(extra && Block_Mallocd ==
 
3423
                extra->Err.FreeMismatch.ai.Addr.Block.block_kind);
2924
3424
      (void)client_perm_maybe_describe( VG_(get_error_address)(err), 
2925
 
                                        &(extra->addrinfo) );
 
3425
                                        &extra->Err.FreeMismatch.ai );
2926
3426
      return sizeof(MC_Error);
2927
3427
   }
2928
3428
 
2929
 
   // No memory address involved with these ones.  Nb:  for LeakErrs the
2930
 
   // returned size does not matter -- LeakErrs are always shown with
2931
 
   // VG_(unique_error)() so they're not copied.
2932
 
   case LeakErr:     return 0;
2933
 
   case OverlapErr:  return sizeof(OverlapExtra);
2934
 
 
2935
3429
   default: VG_(tool_panic)("mc_update_extra: bad errkind");
2936
3430
   }
2937
3431
}
2940
3434
/*--- Suppressions                                         ---*/
2941
3435
/*------------------------------------------------------------*/
2942
3436
 
 
3437
typedef 
 
3438
   enum { 
 
3439
      ParamSupp,     // Bad syscall params
 
3440
      UserSupp,      // Errors arising from client-request checks
 
3441
      CoreMemSupp,   // Memory errors in core (pthread ops, signal handling)
 
3442
 
 
3443
      // Undefined value errors of given size
 
3444
      Value1Supp, Value2Supp, Value4Supp, Value8Supp, Value16Supp,
 
3445
 
 
3446
      // Undefined value error in conditional.
 
3447
      CondSupp,
 
3448
 
 
3449
      // Unaddressable read/write attempt at given size
 
3450
      Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, Addr16Supp,
 
3451
 
 
3452
      JumpSupp,      // Jump to unaddressable target
 
3453
      FreeSupp,      // Invalid or mismatching free
 
3454
      OverlapSupp,   // Overlapping blocks in memcpy(), strcpy(), etc
 
3455
      LeakSupp,      // Something to be suppressed in a leak check.
 
3456
      MempoolSupp,   // Memory pool suppression.
 
3457
   } 
 
3458
   MC_SuppKind;
 
3459
 
2943
3460
static Bool mc_recognised_suppression ( Char* name, Supp* su )
2944
3461
{
2945
3462
   SuppKind skind;
2946
3463
 
2947
3464
   if      (VG_STREQ(name, "Param"))   skind = ParamSupp;
 
3465
   else if (VG_STREQ(name, "User"))    skind = UserSupp;
2948
3466
   else if (VG_STREQ(name, "CoreMem")) skind = CoreMemSupp;
2949
3467
   else if (VG_STREQ(name, "Addr1"))   skind = Addr1Supp;
2950
3468
   else if (VG_STREQ(name, "Addr2"))   skind = Addr2Supp;
2951
3469
   else if (VG_STREQ(name, "Addr4"))   skind = Addr4Supp;
2952
3470
   else if (VG_STREQ(name, "Addr8"))   skind = Addr8Supp;
2953
3471
   else if (VG_STREQ(name, "Addr16"))  skind = Addr16Supp;
 
3472
   else if (VG_STREQ(name, "Jump"))    skind = JumpSupp;
2954
3473
   else if (VG_STREQ(name, "Free"))    skind = FreeSupp;
2955
3474
   else if (VG_STREQ(name, "Leak"))    skind = LeakSupp;
2956
3475
   else if (VG_STREQ(name, "Overlap")) skind = OverlapSupp;
2957
3476
   else if (VG_STREQ(name, "Mempool")) skind = MempoolSupp;
2958
 
   else if (VG_STREQ(name, "Cond"))    skind = Value0Supp;
2959
 
   else if (VG_STREQ(name, "Value0"))  skind = Value0Supp;/* backwards compat */
 
3477
   else if (VG_STREQ(name, "Cond"))    skind = CondSupp;
 
3478
   else if (VG_STREQ(name, "Value0"))  skind = CondSupp; /* backwards compat */
2960
3479
   else if (VG_STREQ(name, "Value1"))  skind = Value1Supp;
2961
3480
   else if (VG_STREQ(name, "Value2"))  skind = Value2Supp;
2962
3481
   else if (VG_STREQ(name, "Value4"))  skind = Value4Supp;
2984
3503
 
2985
3504
static Bool mc_error_matches_suppression(Error* err, Supp* su)
2986
3505
{
2987
 
   Int        su_size;
2988
 
   MC_Error* err_extra = VG_(get_error_extra)(err);
2989
 
   ErrorKind  ekind     = VG_(get_error_kind )(err);
 
3506
   Int       su_szB;
 
3507
   MC_Error* extra = VG_(get_error_extra)(err);
 
3508
   ErrorKind ekind = VG_(get_error_kind )(err);
2990
3509
 
2991
3510
   switch (VG_(get_supp_kind)(su)) {
2992
3511
      case ParamSupp:
2993
 
         return (ekind == ParamErr 
 
3512
         return ((ekind == Err_RegParam || ekind == Err_MemParam)
2994
3513
              && VG_STREQ(VG_(get_error_string)(err), 
2995
3514
                          VG_(get_supp_string)(su)));
2996
3515
 
 
3516
      case UserSupp:
 
3517
         return (ekind == Err_User);
 
3518
 
2997
3519
      case CoreMemSupp:
2998
 
         return (ekind == CoreMemErr
 
3520
         return (ekind == Err_CoreMem
2999
3521
              && VG_STREQ(VG_(get_error_string)(err),
3000
3522
                          VG_(get_supp_string)(su)));
3001
3523
 
3002
 
      case Value0Supp: su_size = 0; goto value_case;
3003
 
      case Value1Supp: su_size = 1; goto value_case;
3004
 
      case Value2Supp: su_size = 2; goto value_case;
3005
 
      case Value4Supp: su_size = 4; goto value_case;
3006
 
      case Value8Supp: su_size = 8; goto value_case;
3007
 
      case Value16Supp:su_size =16; goto value_case;
 
3524
      case Value1Supp: su_szB = 1; goto value_case;
 
3525
      case Value2Supp: su_szB = 2; goto value_case;
 
3526
      case Value4Supp: su_szB = 4; goto value_case;
 
3527
      case Value8Supp: su_szB = 8; goto value_case;
 
3528
      case Value16Supp:su_szB =16; goto value_case;
3008
3529
      value_case:
3009
 
         return (ekind == ValueErr && err_extra->size == su_size);
3010
 
 
3011
 
      case Addr1Supp: su_size = 1; goto addr_case;
3012
 
      case Addr2Supp: su_size = 2; goto addr_case;
3013
 
      case Addr4Supp: su_size = 4; goto addr_case;
3014
 
      case Addr8Supp: su_size = 8; goto addr_case;
3015
 
      case Addr16Supp:su_size =16; goto addr_case;
 
3530
         return (ekind == Err_Value && extra->Err.Value.szB == su_szB);
 
3531
 
 
3532
      case CondSupp:
 
3533
         return (ekind == Err_Cond);
 
3534
 
 
3535
      case Addr1Supp: su_szB = 1; goto addr_case;
 
3536
      case Addr2Supp: su_szB = 2; goto addr_case;
 
3537
      case Addr4Supp: su_szB = 4; goto addr_case;
 
3538
      case Addr8Supp: su_szB = 8; goto addr_case;
 
3539
      case Addr16Supp:su_szB =16; goto addr_case;
3016
3540
      addr_case:
3017
 
         return (ekind == AddrErr && err_extra->size == su_size);
 
3541
         return (ekind == Err_Addr && extra->Err.Addr.szB == su_szB);
 
3542
 
 
3543
      case JumpSupp:
 
3544
         return (ekind == Err_Jump);
3018
3545
 
3019
3546
      case FreeSupp:
3020
 
         return (ekind == FreeErr || ekind == FreeMismatchErr);
 
3547
         return (ekind == Err_Free || ekind == Err_FreeMismatch);
3021
3548
 
3022
3549
      case OverlapSupp:
3023
 
         return (ekind = OverlapErr);
 
3550
         return (ekind == Err_Overlap);
3024
3551
 
3025
3552
      case LeakSupp:
3026
 
         return (ekind == LeakErr);
 
3553
         return (ekind == Err_Leak);
3027
3554
 
3028
3555
      case MempoolSupp:
3029
 
         return (ekind == IllegalMempoolErr);
 
3556
         return (ekind == Err_IllegalMempool);
3030
3557
 
3031
3558
      default:
3032
3559
         VG_(printf)("Error:\n"
3039
3566
 
3040
3567
static Char* mc_get_error_name ( Error* err )
3041
3568
{
3042
 
   Char* s;
3043
3569
   switch (VG_(get_error_kind)(err)) {
3044
 
   case ParamErr:           return "Param";
3045
 
   case UserErr:            return NULL;  /* Can't suppress User errors */
3046
 
   case FreeMismatchErr:    return "Free";
3047
 
   case IllegalMempoolErr:  return "Mempool";
3048
 
   case FreeErr:            return "Free";
3049
 
   case AddrErr:            
3050
 
      switch ( ((MC_Error*)VG_(get_error_extra)(err))->size ) {
 
3570
   case Err_RegParam:       return "Param";
 
3571
   case Err_MemParam:       return "Param";
 
3572
   case Err_User:           return "User";
 
3573
   case Err_FreeMismatch:   return "Free";
 
3574
   case Err_IllegalMempool: return "Mempool";
 
3575
   case Err_Free:           return "Free";
 
3576
   case Err_Jump:           return "Jump";
 
3577
   case Err_CoreMem:        return "CoreMem";
 
3578
   case Err_Overlap:        return "Overlap";
 
3579
   case Err_Leak:           return "Leak";
 
3580
   case Err_Cond:           return "Cond";
 
3581
   case Err_Addr: {
 
3582
      MC_Error* extra = VG_(get_error_extra)(err);
 
3583
      switch ( extra->Err.Addr.szB ) {
3051
3584
      case 1:               return "Addr1";
3052
3585
      case 2:               return "Addr2";
3053
3586
      case 4:               return "Addr4";
3055
3588
      case 16:              return "Addr16";
3056
3589
      default:              VG_(tool_panic)("unexpected size for Addr");
3057
3590
      }
3058
 
     
3059
 
   case ValueErr:
3060
 
      switch ( ((MC_Error*)VG_(get_error_extra)(err))->size ) {
3061
 
      case 0:               return "Cond";
 
3591
   }
 
3592
   case Err_Value: {
 
3593
      MC_Error* extra = VG_(get_error_extra)(err);
 
3594
      switch ( extra->Err.Value.szB ) {
3062
3595
      case 1:               return "Value1";
3063
3596
      case 2:               return "Value2";
3064
3597
      case 4:               return "Value4";
3066
3599
      case 16:              return "Value16";
3067
3600
      default:              VG_(tool_panic)("unexpected size for Value");
3068
3601
      }
3069
 
   case CoreMemErr:         return "CoreMem";
3070
 
   case OverlapErr:         return "Overlap";
3071
 
   case LeakErr:            return "Leak";
 
3602
   }
3072
3603
   default:                 VG_(tool_panic)("get_error_name: unexpected type");
3073
3604
   }
3074
 
   VG_(printf)(s);
3075
3605
}
3076
3606
 
3077
3607
static void mc_print_extra_suppression_info ( Error* err )
3078
3608
{
3079
 
   if (ParamErr == VG_(get_error_kind)(err)) {
 
3609
   ErrorKind ekind = VG_(get_error_kind )(err);
 
3610
   if (Err_RegParam == ekind || Err_MemParam == ekind) {
3080
3611
      VG_(printf)("   %s\n", VG_(get_error_string)(err));
3081
3612
   }
3082
3613
}
3370
3901
   // Handle common case quickly: a is suitably aligned, is mapped, and is
3371
3902
   // addressible.
3372
3903
   // Convert V bits from compact memory form to expanded register form
3373
 
   // XXX: set the high 16/48 bits of retval to 1 for 64-bit paranoia?
3374
3904
   if      (vabits8 == VA_BITS8_DEFINED  ) { return V_BITS16_DEFINED;   }
3375
3905
   else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS16_UNDEFINED; }
3376
3906
   else {
3479
4009
   // Convert V bits from compact memory form to expanded register form
3480
4010
   // Handle common case quickly: a is mapped, and the entire
3481
4011
   // word32 it lives in is addressible.
3482
 
   // XXX: set the high 24/56 bits of retval to 1 for 64-bit paranoia?
3483
4012
   if      (vabits8 == VA_BITS8_DEFINED  ) { return V_BITS8_DEFINED;   }
3484
4013
   else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS8_UNDEFINED; }
3485
4014
   else {
3556
4085
 
3557
4086
void MC_(helperc_value_check0_fail) ( void )
3558
4087
{
3559
 
   mc_record_value_error ( VG_(get_running_tid)(), 0 );
 
4088
   mc_record_cond_error ( VG_(get_running_tid)() );
3560
4089
}
3561
4090
 
3562
4091
void MC_(helperc_value_check1_fail) ( void )
3590
4119
 
3591
4120
/* Copy Vbits from/to address 'a'. Returns: 1 == OK, 2 == alignment
3592
4121
   error [no longer used], 3 == addressing error. */
 
4122
/* Nb: We used to issue various definedness/addressability errors from here,
 
4123
   but we took them out because they ranged from not-very-helpful to
 
4124
   downright annoying, and they complicated the error data structures. */
3593
4125
static Int mc_get_or_set_vbits_for_client ( 
3594
4126
   ThreadId tid,
3595
4127
   Addr a, 
3604
4136
 
3605
4137
   /* Check that arrays are addressible before doing any getting/setting. */
3606
4138
   for (i = 0; i < szB; i++) {
3607
 
      if (VA_BITS2_NOACCESS == get_vabits2(a + i)) {
3608
 
         mc_record_address_error( tid, a + i,     1, setting ? True : False );
3609
 
         return 3;
3610
 
      }
3611
 
      if (VA_BITS2_NOACCESS == get_vabits2(vbits + i)) {
3612
 
         mc_record_address_error( tid, vbits + i, 1, setting ? False : True );
 
4139
      if (VA_BITS2_NOACCESS == get_vabits2(a + i) ||
 
4140
          VA_BITS2_NOACCESS == get_vabits2(vbits + i)) {
3613
4141
         return 3;
3614
4142
      }
3615
4143
   }
3616
4144
 
3617
4145
   /* Do the copy */
3618
4146
   if (setting) {
3619
 
 
3620
 
      // It's actually a tool ClientReq, but Vg_CoreClientReq is the closest
3621
 
      // thing we have.
3622
 
      check_mem_is_defined(Vg_CoreClientReq, tid, "SET_VBITS(vbits)",
3623
 
                       vbits, szB);
3624
 
      
3625
4147
      /* setting */
3626
4148
      for (i = 0; i < szB; i++) {
3627
4149
         ok = set_vbits8(a + i, ((UChar*)vbits)[i]);
3632
4154
      for (i = 0; i < szB; i++) {
3633
4155
         ok = get_vbits8(a + i, &vbits8);
3634
4156
         tl_assert(ok);
3635
 
// XXX: used to do this, but it's a pain
3636
 
//         if (V_BITS8_DEFINED != vbits8)
3637
 
//            mc_record_value_error(tid, 1);
3638
4157
         ((UChar*)vbits)[i] = vbits8;
3639
4158
      }
3640
4159
      // The bytes in vbits[] have now been set, so mark them as such.
3657
4176
Bool mc_is_within_valid_secondary ( Addr a )
3658
4177
{
3659
4178
   SecMap* sm = maybe_get_secmap_for ( a );
3660
 
   if (sm == NULL || sm == &sm_distinguished[SM_DIST_NOACCESS]) {
 
4179
   if (sm == NULL || sm == &sm_distinguished[SM_DIST_NOACCESS]
 
4180
       || in_ignored_range(a)) {
3661
4181
      /* Definitely not in use. */
3662
4182
      return False;
3663
4183
   } else {
3677
4197
   } else {
3678
4198
      tl_assert(VG_IS_8_ALIGNED(a));
3679
4199
   }
3680
 
   if (is_mem_defined( a, sizeof(UWord), NULL ) == MC_Ok) {
 
4200
   if (is_mem_defined( a, sizeof(UWord), NULL ) == MC_Ok
 
4201
       && !in_ignored_range(a)) {
3681
4202
      return True;
3682
4203
   } else {
3683
4204
      return False;
3729
4250
   for (i = 0; i < N_PRIMARY_MAP; i++)
3730
4251
      primary_map[i] = &sm_distinguished[SM_DIST_NOACCESS];
3731
4252
 
 
4253
   /* Auxiliary primary maps */
 
4254
   init_auxmap_L1_L2();
 
4255
 
3732
4256
   /* auxmap_size = auxmap_used = 0; 
3733
4257
      no ... these are statically initialised */
3734
4258
 
3751
4275
 
3752
4276
static Bool mc_expensive_sanity_check ( void )
3753
4277
{
3754
 
   Int     i, n_secmaps_found;
 
4278
   Int     i;
 
4279
   Word    n_secmaps_found;
3755
4280
   SecMap* sm;
 
4281
   HChar*  errmsg;
3756
4282
   Bool    bad = False;
3757
4283
 
 
4284
   if (0) VG_(printf)("expensive sanity check\n");
 
4285
   if (0) return True;
 
4286
 
3758
4287
   n_sanity_expensive++;
3759
4288
   PROF_EVENT(491, "expensive_sanity_check");
3760
4289
 
3787
4316
   /* If we're not checking for undefined value errors, the secondary V bit
3788
4317
    * table should be empty. */
3789
4318
   if (!MC_(clo_undef_value_errors)) {
3790
 
      if (0 != VG_(OSet_Size)(secVBitTable))
 
4319
      if (0 != VG_(OSetGen_Size)(secVBitTable))
3791
4320
         return False;
3792
4321
   }
3793
4322
 
3794
 
   /* check nonsensical auxmap sizing */
3795
 
   if (auxmap_used > auxmap_size)
3796
 
       bad = True;
3797
 
 
3798
 
   if (bad) {
3799
 
      VG_(printf)("memcheck expensive sanity: "
3800
 
                  "nonsensical auxmap sizing\n");
3801
 
      return False;
3802
 
   }
3803
 
 
3804
 
   /* check that the number of secmaps issued matches the number that
3805
 
      are reachable (iow, no secmap leaks) */
 
4323
   /* check the auxiliary maps, very thoroughly */
3806
4324
   n_secmaps_found = 0;
 
4325
   errmsg = check_auxmap_L1_L2_sanity( &n_secmaps_found );
 
4326
   if (errmsg) {
 
4327
      VG_(printf)("memcheck expensive sanity, auxmaps:\n\t%s", errmsg);
 
4328
      return False;
 
4329
   }
 
4330
 
 
4331
   /* n_secmaps_found is now the number referred to by the auxiliary
 
4332
      primary map.  Now add on the ones referred to by the main
 
4333
      primary map. */
3807
4334
   for (i = 0; i < N_PRIMARY_MAP; i++) {
3808
 
     if (primary_map[i] == NULL) {
3809
 
       bad = True;
3810
 
     } else {
3811
 
     if (!is_distinguished_sm(primary_map[i]))
3812
 
       n_secmaps_found++;
3813
 
     }
3814
 
   }
3815
 
 
3816
 
   for (i = 0; i < auxmap_used; i++) {
3817
 
      if (auxmap[i].sm == NULL) {
 
4335
      if (primary_map[i] == NULL) {
3818
4336
         bad = True;
3819
4337
      } else {
3820
 
         if (!is_distinguished_sm(auxmap[i].sm))
 
4338
         if (!is_distinguished_sm(primary_map[i]))
3821
4339
            n_secmaps_found++;
3822
4340
      }
3823
4341
   }
3824
4342
 
 
4343
   /* check that the number of secmaps issued matches the number that
 
4344
      are reachable (iow, no secmap leaks) */
3825
4345
   if (n_secmaps_found != (n_issued_SMs - n_deissued_SMs))
3826
4346
      bad = True;
3827
4347
 
3831
4351
      return False;
3832
4352
   }
3833
4353
 
3834
 
   /* check that auxmap only covers address space that the primary doesn't */
3835
 
   
3836
 
   for (i = 0; i < auxmap_used; i++)
3837
 
      if (auxmap[i].base <= MAX_PRIMARY_ADDRESS)
3838
 
         bad = True;
3839
 
 
3840
4354
   if (bad) {
3841
4355
      VG_(printf)("memcheck expensive sanity: "
3842
4356
                  "auxmap covers wrong address space\n");
3853
4367
/*------------------------------------------------------------*/
3854
4368
 
3855
4369
Bool          MC_(clo_partial_loads_ok)       = False;
3856
 
Int           MC_(clo_freelist_vol)           = 5000000;
 
4370
Long          MC_(clo_freelist_vol)           = 10*1000*1000LL;
3857
4371
LeakCheckMode MC_(clo_leak_check)             = LC_Summary;
3858
4372
VgRes         MC_(clo_leak_resolution)        = Vg_LowRes;
3859
4373
Bool          MC_(clo_show_reachable)         = False;
3860
4374
Bool          MC_(clo_workaround_gcc296_bugs) = False;
3861
4375
Bool          MC_(clo_undef_value_errors)     = True;
 
4376
Int           MC_(clo_malloc_fill)            = -1;
 
4377
Int           MC_(clo_free_fill)              = -1;
3862
4378
 
3863
4379
static Bool mc_process_cmd_line_options(Char* arg)
3864
4380
{
3868
4384
 
3869
4385
   else VG_BOOL_CLO(arg, "--undef-value-errors",    MC_(clo_undef_value_errors))
3870
4386
   
3871
 
   else VG_BNUM_CLO(arg, "--freelist-vol",  MC_(clo_freelist_vol), 0, 1000000000)
 
4387
   else VG_BNUM_CLO(arg, "--freelist-vol",  MC_(clo_freelist_vol), 
 
4388
                                            0, 10*1000*1000*1000LL)
3872
4389
   
3873
4390
   else if (VG_CLO_STREQ(arg, "--leak-check=no"))
3874
4391
      MC_(clo_leak_check) = LC_Off;
3885
4402
   else if (VG_CLO_STREQ(arg, "--leak-resolution=high"))
3886
4403
      MC_(clo_leak_resolution) = Vg_HighRes;
3887
4404
 
 
4405
   else if (VG_CLO_STREQN(16,arg,"--ignore-ranges=")) {
 
4406
      Int    i;
 
4407
      UChar* txt = (UChar*)(arg+16);
 
4408
      Bool   ok  = parse_ignore_ranges(txt);
 
4409
      if (!ok)
 
4410
        return False;
 
4411
      tl_assert(ignoreRanges.used >= 0);
 
4412
      tl_assert(ignoreRanges.used < M_IGNORE_RANGES);
 
4413
      for (i = 0; i < ignoreRanges.used; i++) {
 
4414
         Addr s = ignoreRanges.start[i];
 
4415
         Addr e = ignoreRanges.end[i];
 
4416
         Addr limit = 0x4000000; /* 64M - entirely arbitrary limit */
 
4417
         if (e <= s) {
 
4418
            VG_(message)(Vg_DebugMsg, 
 
4419
               "ERROR: --ignore-ranges: end <= start in range:");
 
4420
            VG_(message)(Vg_DebugMsg, 
 
4421
               "       0x%lx-0x%lx", s, e);
 
4422
            return False;
 
4423
         }
 
4424
         if (e - s > limit) {
 
4425
            VG_(message)(Vg_DebugMsg, 
 
4426
               "ERROR: --ignore-ranges: suspiciously large range:");
 
4427
            VG_(message)(Vg_DebugMsg, 
 
4428
               "       0x%lx-0x%lx (size %ld)", s, e, (UWord)(e-s));
 
4429
            return False;
 
4430
         }
 
4431
      }
 
4432
   }
 
4433
 
 
4434
   else VG_BHEX_CLO(arg, "--malloc-fill", MC_(clo_malloc_fill), 0x00, 0xFF)
 
4435
   else VG_BHEX_CLO(arg, "--free-fill",   MC_(clo_free_fill), 0x00, 0xFF)
 
4436
 
3888
4437
   else
3889
4438
      return VG_(replacement_malloc_process_cmd_line_option)(arg);
3890
4439
 
3899
4448
"    --show-reachable=no|yes          show reachable blocks in leak check? [no]\n"
3900
4449
"    --undef-value-errors=no|yes      check for undefined value errors [yes]\n"
3901
4450
"    --partial-loads-ok=no|yes        too hard to explain here; see manual [no]\n"
3902
 
"    --freelist-vol=<number>          volume of freed blocks queue [5000000]\n"
 
4451
"    --freelist-vol=<number>          volume of freed blocks queue [10000000]\n"
3903
4452
"    --workaround-gcc296-bugs=no|yes  self explanatory [no]\n"
 
4453
"    --ignore-ranges=0xPP-0xQQ[,0xRR-0xSS]   assume given addresses are OK\n"
 
4454
"    --malloc-fill=<hexnumber>        fill malloc'd areas with given value\n"
 
4455
"    --free-fill=<hexnumber>          fill free'd areas with given value\n"
3904
4456
   );
3905
4457
   VG_(replacement_malloc_print_usage)();
3906
4458
}
3999
4551
static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai )
4000
4552
{
4001
4553
   UInt i;
4002
 
   /* VG_(printf)("try to identify %d\n", a); */
4003
4554
 
4004
4555
   /* Perhaps it's a general block ? */
4005
4556
   for (i = 0; i < cgb_used; i++) {
4016
4567
               VG_(HT_ResetIter)(mp->chunks);
4017
4568
               while ( (mc = VG_(HT_Next)(mp->chunks)) ) {
4018
4569
                  if (addr_is_in_MC_Chunk(mc, a)) {
4019
 
                     ai->akind      = UserG;
4020
 
                     ai->blksize    = mc->size;
4021
 
                     ai->rwoffset   = (Int)(a) - (Int)mc->data;
4022
 
                     ai->lastchange = mc->where;
 
4570
                     ai->tag = Addr_Block;
 
4571
                     ai->Addr.Block.block_kind = Block_MempoolChunk;
 
4572
                     ai->Addr.Block.block_desc = "block";
 
4573
                     ai->Addr.Block.block_szB  = mc->szB;
 
4574
                     ai->Addr.Block.rwoffset   = (Int)a - (Int)mc->data;
 
4575
                     ai->Addr.Block.lastchange = mc->where;
4023
4576
                     return True;
4024
4577
                  }
4025
4578
               }
4026
4579
            }
4027
 
            ai->akind      = Mempool;
4028
 
            ai->blksize    = cgbs[i].size;
4029
 
            ai->rwoffset   = (Int)(a) - (Int)(cgbs[i].start);
4030
 
            ai->lastchange = cgbs[i].where;
 
4580
            ai->tag = Addr_Block;
 
4581
            ai->Addr.Block.block_kind = Block_Mempool;
 
4582
            ai->Addr.Block.block_desc = "mempool";
 
4583
            ai->Addr.Block.block_szB  = cgbs[i].size;
 
4584
            ai->Addr.Block.rwoffset   = (Int)(a) - (Int)(cgbs[i].start);
 
4585
            ai->Addr.Block.lastchange = cgbs[i].where;
4031
4586
            return True;
4032
4587
         }
4033
 
         ai->akind      = UserG;
4034
 
         ai->blksize    = cgbs[i].size;
4035
 
         ai->rwoffset   = (Int)(a) - (Int)(cgbs[i].start);
4036
 
         ai->lastchange = cgbs[i].where;
4037
 
         ai->desc       = cgbs[i].desc;
 
4588
         ai->tag = Addr_Block;
 
4589
         ai->Addr.Block.block_kind = Block_UserG;
 
4590
         ai->Addr.Block.block_desc = cgbs[i].desc;
 
4591
         ai->Addr.Block.block_szB  = cgbs[i].size;
 
4592
         ai->Addr.Block.rwoffset   = (Int)(a) - (Int)(cgbs[i].start);
 
4593
         ai->Addr.Block.lastchange = cgbs[i].where;
4038
4594
         return True;
4039
4595
      }
4040
4596
   }
4053
4609
    && VG_USERREQ__CREATE_MEMPOOL   != arg[0]
4054
4610
    && VG_USERREQ__DESTROY_MEMPOOL  != arg[0]
4055
4611
    && VG_USERREQ__MEMPOOL_ALLOC    != arg[0]
4056
 
    && VG_USERREQ__MEMPOOL_FREE     != arg[0])
 
4612
    && VG_USERREQ__MEMPOOL_FREE     != arg[0]
 
4613
    && VG_USERREQ__MEMPOOL_TRIM     != arg[0]
 
4614
    && VG_USERREQ__MOVE_MEMPOOL     != arg[0]
 
4615
    && VG_USERREQ__MEMPOOL_CHANGE   != arg[0]
 
4616
    && VG_USERREQ__MEMPOOL_EXISTS   != arg[0])
4057
4617
      return False;
4058
4618
 
4059
4619
   switch (arg[0]) {
4060
4620
      case VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE:
4061
4621
         ok = is_mem_addressable ( arg[1], arg[2], &bad_addr );
4062
4622
         if (!ok)
4063
 
            mc_record_user_error ( tid, bad_addr, /*isWrite*/True,
4064
 
                                   /*isUnaddr*/True );
 
4623
            mc_record_user_error ( tid, bad_addr, /*isAddrErr*/True );
4065
4624
         *ret = ok ? (UWord)NULL : bad_addr;
4066
4625
         break;
4067
4626
 
4069
4628
         MC_ReadResult res;
4070
4629
         res = is_mem_defined ( arg[1], arg[2], &bad_addr );
4071
4630
         if (MC_AddrErr == res)
4072
 
            mc_record_user_error ( tid, bad_addr, /*isWrite*/False,
4073
 
                                   /*isUnaddr*/True );
 
4631
            mc_record_user_error ( tid, bad_addr, /*isAddrErr*/True );
4074
4632
         else if (MC_ValueErr == res)
4075
 
            mc_record_user_error ( tid, bad_addr, /*isWrite*/False,
4076
 
                                   /*isUnaddr*/False );
 
4633
            mc_record_user_error ( tid, bad_addr, /*isAddrErr*/False );
4077
4634
         *ret = ( res==MC_Ok ? (UWord)NULL : bad_addr );
4078
4635
         break;
4079
4636
      }
4110
4667
            cgbs[i].start = arg[1];
4111
4668
            cgbs[i].size  = arg[2];
4112
4669
            cgbs[i].desc  = VG_(strdup)((Char *)arg[3]);
4113
 
            cgbs[i].where = VG_(record_ExeContext) ( tid );
 
4670
            cgbs[i].where = VG_(record_ExeContext) ( tid, 0/*first_ip_delta*/ );
4114
4671
 
4115
4672
            *ret = i;
4116
4673
         } else
4132
4689
         break;
4133
4690
 
4134
4691
      case VG_USERREQ__GET_VBITS:
4135
 
         /* Returns: 1 == OK, 2 == alignment error, 3 == addressing
4136
 
            error. */
4137
 
         /* VG_(printf)("get_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */
4138
4692
         *ret = mc_get_or_set_vbits_for_client
4139
4693
                   ( tid, arg[1], arg[2], arg[3], False /* get them */ );
4140
4694
         break;
4141
4695
 
4142
4696
      case VG_USERREQ__SET_VBITS:
4143
 
         /* Returns: 1 == OK, 2 == alignment error, 3 == addressing
4144
 
            error. */
4145
 
         /* VG_(printf)("set_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */
4146
4697
         *ret = mc_get_or_set_vbits_for_client
4147
4698
                   ( tid, arg[1], arg[2], arg[3], True /* set them */ );
4148
4699
         break;
4180
4731
      }
4181
4732
 
4182
4733
      case _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR: {
4183
 
         Char*         s     = (Char*)        arg[1];
4184
 
         OverlapExtra* extra = (OverlapExtra*)arg[2];
4185
 
         mc_record_overlap_error(tid, s, extra);
 
4734
         Char* s   = (Char*)arg[1];
 
4735
         Addr  dst = (Addr) arg[2];
 
4736
         Addr  src = (Addr) arg[3];
 
4737
         SizeT len = (SizeT)arg[4];
 
4738
         mc_record_overlap_error(tid, s, src, dst, len);
4186
4739
         return True;
4187
4740
      }
4188
4741
 
4219
4772
         return True;
4220
4773
      }
4221
4774
 
 
4775
      case VG_USERREQ__MEMPOOL_TRIM: {
 
4776
         Addr pool      = (Addr)arg[1];
 
4777
         Addr addr      = (Addr)arg[2];
 
4778
         UInt size      =       arg[3];
 
4779
 
 
4780
         MC_(mempool_trim) ( pool, addr, size );
 
4781
         return True;
 
4782
      }
 
4783
 
 
4784
      case VG_USERREQ__MOVE_MEMPOOL: {
 
4785
         Addr poolA     = (Addr)arg[1];
 
4786
         Addr poolB     = (Addr)arg[2];
 
4787
 
 
4788
         MC_(move_mempool) ( poolA, poolB );
 
4789
         return True;
 
4790
      }
 
4791
 
 
4792
      case VG_USERREQ__MEMPOOL_CHANGE: {
 
4793
         Addr pool      = (Addr)arg[1];
 
4794
         Addr addrA     = (Addr)arg[2];
 
4795
         Addr addrB     = (Addr)arg[3];
 
4796
         UInt size      =       arg[4];
 
4797
 
 
4798
         MC_(mempool_change) ( pool, addrA, addrB, size );
 
4799
         return True;
 
4800
      }
 
4801
 
 
4802
      case VG_USERREQ__MEMPOOL_EXISTS: {
 
4803
         Addr pool      = (Addr)arg[1];
 
4804
 
 
4805
         *ret = (UWord) MC_(mempool_exists) ( pool );
 
4806
         return True;
 
4807
      }
 
4808
 
 
4809
 
4222
4810
      default:
4223
4811
         VG_(message)(Vg_UserMsg, 
4224
4812
                      "Warning: unknown memcheck client request code %llx",
4325
4913
         n_sanity_cheap, n_sanity_expensive );
4326
4914
      VG_(message)(Vg_DebugMsg,
4327
4915
         " memcheck: auxmaps: %d auxmap entries (%dk, %dM) in use",
4328
 
         auxmap_used, 
4329
 
         auxmap_used * 64, 
4330
 
         auxmap_used / 16 );
4331
 
      VG_(message)(Vg_DebugMsg,
4332
 
         " memcheck: auxmaps: %lld searches, %lld comparisons",
4333
 
         n_auxmap_searches, n_auxmap_cmps );   
 
4916
         n_auxmap_L2_nodes, 
 
4917
         n_auxmap_L2_nodes * 64, 
 
4918
         n_auxmap_L2_nodes / 16 );
 
4919
      VG_(message)(Vg_DebugMsg,
 
4920
         " memcheck: auxmaps_L1: %lld searches, %lld cmps, ratio %lld:10",
 
4921
         n_auxmap_L1_searches, n_auxmap_L1_cmps,
 
4922
         (10ULL * n_auxmap_L1_cmps) 
 
4923
            / (n_auxmap_L1_searches ? n_auxmap_L1_searches : 1) 
 
4924
      );   
 
4925
      VG_(message)(Vg_DebugMsg,
 
4926
         " memcheck: auxmaps_L2: %lld searches, %lld nodes",
 
4927
         n_auxmap_L2_searches, n_auxmap_L2_nodes
 
4928
      );   
4334
4929
 
4335
4930
      print_SM_info("n_issued     ", n_issued_SMs);
4336
4931
      print_SM_info("n_deissued   ", n_deissued_SMs);
4374
4969
   VG_(details_version)         (NULL);
4375
4970
   VG_(details_description)     ("a memory error detector");
4376
4971
   VG_(details_copyright_author)(
4377
 
      "Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.");
 
4972
      "Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.");
4378
4973
   VG_(details_bug_reports_to)  (VG_BUGS_TO);
4379
 
   VG_(details_avg_translation_sizeB) ( 370 );
 
4974
   VG_(details_avg_translation_sizeB) ( 556 );
4380
4975
 
4381
4976
   VG_(basic_tool_funcs)          (mc_post_clo_init,
4382
4977
                                   MC_(instrument),
4383
4978
                                   mc_fini);
4384
4979
 
 
4980
   VG_(needs_final_IR_tidy_pass)  ( MC_(final_tidy) );
 
4981
 
 
4982
 
4385
4983
   VG_(needs_core_errors)         ();
4386
4984
   VG_(needs_tool_errors)         (mc_eq_Error,
4387
4985
                                   mc_pp_Error,
 
4986
                                   True,/*show TIDs for errors*/
4388
4987
                                   mc_update_extra,
4389
4988
                                   mc_recognised_suppression,
4390
4989
                                   mc_read_extra_suppression_info,
4472
5071
   VG_(track_post_reg_write_clientcall_return)( mc_post_reg_write_clientcall );
4473
5072
 
4474
5073
   init_shadow_memory();
4475
 
   MC_(malloc_list)  = VG_(HT_construct)( 80021 );   // prime, big
4476
 
   MC_(mempool_list) = VG_(HT_construct)( 1009  );   // prime, not so big
 
5074
   MC_(malloc_list)  = VG_(HT_construct)( "MC_(malloc_list)" );
 
5075
   MC_(mempool_list) = VG_(HT_construct)( "MC_(mempool_list)" );
4477
5076
   init_prof_mem();
4478
5077
 
4479
5078
   tl_assert( mc_expensive_sanity_check() );
4480
5079
 
4481
5080
   // {LOADV,STOREV}[8421] will all fail horribly if this isn't true.
4482
5081
   tl_assert(sizeof(UWord) == sizeof(Addr));
 
5082
   // Call me paranoid.  I don't care.
 
5083
   tl_assert(sizeof(void*) == sizeof(Addr));
4483
5084
 
4484
5085
   // BYTES_PER_SEC_VBIT_NODE must be a power of two.
4485
5086
   tl_assert(-1 != VG_(log2)(BYTES_PER_SEC_VBIT_NODE));