statfs.f_bsize => statfs.f_iosize; statfs.f_fsize => statfs.f_bsize (for SunOS)
[unix-history] / usr / src / sys / kern / kern_malloc.c
index 72d0a65..4f04615 100644 (file)
@@ -1,10 +1,10 @@
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
+ * Copyright (c) 1987, 1991 The Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  *
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  *
- *     @(#)kern_malloc.c       7.24 (Berkeley) %G%
+ *     @(#)kern_malloc.c       7.28 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -26,6 +26,40 @@ long malloc_reentered;
                        else malloc_reentered = 1;}
 #define OUT (malloc_reentered = 0)
 
                        else malloc_reentered = 1;}
 #define OUT (malloc_reentered = 0)
 
+#ifdef DIAGNOSTIC
+/*
+ * This structure serves two purposes.
+ * The first is to provide a set of masks to catch unaligned frees.
+ * The second is to provide known text to copy into free objects so
+ * that modifications after frees can be detected.
+ */
+#define WEIRD_ADDR 0xdeadbeef
+long addrmask[] = { WEIRD_ADDR,
+       0x00000001, 0x00000003, 0x00000007, 0x0000000f,
+       0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
+       0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
+       0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
+};
+
+/*
+ * Normally the first word of the structure is used to hold the list
+ * pointer for free objects. However, when running with diagnostics,
+ * we use the third and fourth fields, so as to catch modifications
+ * in the most commonly trashed first two words.
+ */
+struct freelist {
+       long    spare0;
+       long    spare1;
+       short   type;
+       short   spare2;
+       caddr_t next;
+};
+#else /* !DIAGNOSTIC */
+struct freelist {
+       caddr_t next;
+};
+#endif /* DIAGNOSTIC */
+
 /*
  * Allocate a block of memory
  */
 /*
  * Allocate a block of memory
  */
@@ -36,14 +70,18 @@ malloc(size, type, flags)
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
+       register struct freelist *freep;
        long indx, npg, alloc, allocsize;
        int s;
        caddr_t va, cp, rp;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 
        long indx, npg, alloc, allocsize;
        int s;
        caddr_t va, cp, rp;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 
+#ifdef DIAGNOSTIC
        if (((unsigned long)type) > M_LAST)
                panic("malloc - bogus type");
        if (((unsigned long)type) > M_LAST)
                panic("malloc - bogus type");
+       if (type == M_NAMEI)
+               curproc->p_spare[0]++;
 
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
 
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
@@ -62,6 +100,9 @@ malloc(size, type, flags)
                tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
                IN;
        }
                tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
                IN;
        }
+#endif
+#ifdef DIAGNOSTIC
+       copysize = 1 << indx < sizeof addrmask ? 1 << indx : sizeof addrmask;
 #endif
        if (kbp->kb_next == NULL) {
                if (size > MAXALLOCSAVE)
 #endif
        if (kbp->kb_next == NULL) {
                if (size > MAXALLOCSAVE)
@@ -149,15 +190,6 @@ long addrmask[] = { 0x00000000,
 };
 #endif /* DIAGNOSTIC */
 
 };
 #endif /* DIAGNOSTIC */
 
-#ifdef DIAGNOSTIC
-long addrmask[] = { 0x00000000,
-       0x00000001, 0x00000003, 0x00000007, 0x0000000f,
-       0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
-       0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
-       0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
-};
-#endif /* DIAGNOSTIC */
-
 /*
  * Free a block of memory allocated by malloc.
  */
 /*
  * Free a block of memory allocated by malloc.
  */
@@ -168,8 +200,13 @@ free(addr, type)
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
-       long alloc, size;
+       register struct freelist *freep;
+       long size;
        int s;
        int s;
+#ifdef DIAGNOSTIC
+       caddr_t cp;
+       long alloc, copysize;
+#endif
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
@@ -188,7 +225,15 @@ free(addr, type)
        }
 #endif /* DIAGNOSTIC */
        size = 1 << kup->ku_indx;
        }
 #endif /* DIAGNOSTIC */
        size = 1 << kup->ku_indx;
+       kbp = &bucket[kup->ku_indx];
+       s = splimp();
 #ifdef DIAGNOSTIC
 #ifdef DIAGNOSTIC
+       /*
+        * Check for returns of data that do not point to the
+        * beginning of the allocation.
+        */
+       if (type == M_NAMEI)
+               curproc->p_spare[0]--;
        if (size > NBPG * CLSIZE)
                alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
        else
        if (size > NBPG * CLSIZE)
                alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
        else
@@ -199,8 +244,6 @@ free(addr, type)
                panic("free: unaligned addr");
        }
 #endif /* DIAGNOSTIC */
                panic("free: unaligned addr");
        }
 #endif /* DIAGNOSTIC */
-       kbp = &bucket[kup->ku_indx];
-       s = splimp();
        IN;
        if (size > MAXALLOCSAVE) {
                kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
        IN;
        if (size > MAXALLOCSAVE) {
                kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
@@ -218,6 +261,35 @@ free(addr, type)
                splx(s);
                return;
        }
                splx(s);
                return;
        }
+       freep = (struct freelist *)addr;
+#ifdef DIAGNOSTIC
+       /*
+        * Check for multiple frees. Use a quick check to see if
+        * it looks free before laboriously searching the freelist.
+        */
+       copysize = size < sizeof addrmask ? size : sizeof addrmask;
+       if (freep->spare0 == WEIRD_ADDR) {
+               freep->type = ((struct freelist *)addrmask)->type;
+               freep->next = ((struct freelist *)addrmask)->next;
+               if (!bcmp(addrmask, addr, copysize)) {
+                       for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
+                               if (addr == cp) {
+                                       printf("multiply freed item 0x%x\n",
+                                           addr);
+                                       panic("free: duplicated free");
+                               }
+                       }
+               }
+       }
+       /*
+        * Copy in known text to detect modification after freeing
+        * and to make it look free. Also, save the type being freed
+        * so we can list likely culprit if modification is detected
+        * when the object is reallocated.
+        */
+       bcopy(addrmask, addr, copysize);
+       freep->type = type;
+#endif /* DIAGNOSTIC */
        if (size == 128) {
                long *lp = (long *)addr;
                lp[0] = lp[1] = lp[3] = lp[4] = -1;
        if (size == 128) {
                long *lp = (long *)addr;
                lp[0] = lp[1] = lp[3] = lp[4] = -1;