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