home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / AUDIT.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  3KB  |  125 lines

  1. /* Routines for auditing mbuf consistency. Not used for some time, may
  2.  * not be up to date.
  3.  * Copyright 1991 Phil Karn, KA9Q
  4.  */
  5. #include "global.h"
  6. #include "mbuf.h"
  7.   
  8. extern char _Uend;
  9. extern int _STKRED;
  10.   
  11. union header {
  12.     struct {
  13.         union header *ptr;
  14.         unsigned size;
  15.     } s;
  16.     long l;
  17. };
  18.   
  19. void audit __ARGS((struct mbuf *bp,char *file,int line));
  20. static void audit_mbuf __ARGS((struct mbuf *bp,char *file,int line));
  21. static void dumpbuf __ARGS((struct mbuf *bp));
  22.   
  23. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  24.  * nonzero otherwise
  25.  */
  26. void
  27. audit(bp,file,line)
  28. struct mbuf *bp;
  29. char *file;
  30. int line;
  31. {
  32.     register struct mbuf *bp1;
  33.   
  34.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  35.         audit_mbuf(bp1,file,line);
  36. }
  37.   
  38. static void
  39. audit_mbuf(bp,file,line)
  40. register struct mbuf *bp;
  41. char *file;
  42. int line;
  43. {
  44.     union header *blk;
  45.     char *bufstart,*bufend;
  46.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  47.     int16 datasize;
  48.     int errors = 0;
  49.     char *heapbot,*heaptop;
  50.   
  51.     if(bp == NULLBUF)
  52.         return;
  53.   
  54.     heapbot = &_Uend;
  55.     heaptop = (char *) -_STKRED;
  56.   
  57.     /* Does buffer appear to be a valid malloc'ed block? */
  58.     blk = ((union header *)bp) - 1;
  59.     if(blk->s.ptr != blk){
  60.         printf("Garbage bp %lx\n",(long)bp);
  61.         errors++;
  62.     }
  63.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  64.         /* mbuf has data area associated with it, verify that
  65.          * pointers are within it
  66.          */
  67.         bufstart = (char *)(bp + 1);
  68.         bufend = (char *)bufstart + datasize;
  69.         if(bp->data < bufstart){
  70.             printf("Data pointer before buffer\n");
  71.             errors++;
  72.         }
  73.         if(bp->data + bp->cnt > bufend){
  74.             printf("Data pointer + count past bounds\n");
  75.             errors++;
  76.         }
  77.     } else {
  78.         /* Dup'ed mbuf, at least check that pointers are within
  79.          * heap area
  80.         */
  81.   
  82.         if(bp->data < heapbot
  83.         || bp->data + bp->cnt > heaptop){
  84.             printf("Data outside heap\n");
  85.             errors++;
  86.         }
  87.     }
  88.     /* Now check link list pointers */
  89.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  90.     || bp->next > (struct mbuf *)heaptop)){
  91.         printf("next pointer out of limits\n");
  92.         errors++;
  93.     }
  94.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  95.     || bp->anext > (struct mbuf *)heaptop)){
  96.         printf("anext pointer out of limits\n");
  97.         errors++;
  98.     }
  99.     if(errors != 0){
  100.         dumpbuf(bp);
  101.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  102.         fflush(stdout);
  103.         for(;;)
  104.             ;
  105.     }
  106.     return;
  107. }
  108.   
  109. static void
  110. dumpbuf(bp)
  111. struct mbuf *bp;
  112. {
  113.     union header *blk;
  114.     if(bp == NULLBUF){
  115.         printf("NULL BUFFER\n");
  116.         return;
  117.     }
  118.     blk = ((union header *)bp) - 1;
  119.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  120.     (long)bp,blk->s.size * sizeof(union header),
  121.     (long)bp->data,bp->cnt,
  122.     (long)bp->next,(long)bp->anext);
  123. }
  124.   
  125.