summaryrefslogtreecommitdiff
path: root/dmake/dbug/malloc/mlc_chn.c
blob: a25d906b8d7c70a2613bc8547e86d47d32a91ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
 * You may copy, distribute, and use this software as long as this
 * copyright statement is not removed.
 */
#include <stdio.h>
#include <fcntl.h>
#include "malloc.h"

/*
 * Function:	malloc_chain_check()
 *
 * Purpose:	to verify malloc chain is intact
 *
 * Arguments:	todo	- 0 - just check and return status
 *			  1 - call malloc_warn if error detected
 *
 * Returns:	0	- malloc chain intact & no overflows
 *		other	- problems detected in malloc chain
 *
 * Narrative:
 *
 * Notes:	If todo is non-zero the malloc_warn function, when called
 *		may not return (i.e. it may exit)
 *
 */
#ifndef lint
static
char rcs_hdr[] = "$Id: mlc_chn.c,v 1.1.1.1 2000-09-22 15:33:26 hr Exp $";
#endif


int
malloc_chain_check(todo)
    int		  todo;
{
    char			* func = "malloc_chain_check";
    int			  i;
    extern char		* malloc_data_start;
    extern char		* malloc_data_end;
    extern struct mlist 	* malloc_end;
    extern int		  malloc_errno;
    extern struct mlist	  malloc_start;
    struct mlist		* oldptr;
    struct mlist		* ptr;
    int			  rtn = 0;

    oldptr = &malloc_start;
    for(ptr = malloc_start.next; ; ptr = ptr->next)
    {
        /*
         * Since the malloc chain is a forward only chain, any
         * pointer that we get should always be positioned in 
         * memory following the previous pointer.  If this is not
         * so, we must have a corrupted chain.
         */
        if( ptr )
        {
            if(ptr < oldptr )
            {
                malloc_errno = M_CODE_CHAIN_BROKE;
                if( todo )
                {
                    malloc_fatal(func);
                }
                rtn++;
                break;
            }
            oldptr = ptr;
        }
        else
        {
            if( oldptr != malloc_end )
            {
                /*
                 * This should never happen.  If it does, then
                 * we got a real problem.
                 */
                malloc_errno = M_CODE_NO_END;
                if( todo )
                {
                    malloc_fatal(func);
                }
                rtn++;
            }
            break;
        }
        
        /*
         * verify that ptr is within the malloc region...
         * since we started within the malloc chain this should never
         * happen.
         */

        if( ((char *)ptr < malloc_data_start) ||
            ((char *)ptr > malloc_data_end) )
        {
            malloc_errno = M_CODE_BAD_PTR;
            if( todo )
            {
                malloc_fatal(func);
            }
            rtn++;
            break;
        }

        /* 
         * verify magic flag is set
         */
        
        if( (ptr->flag&M_MAGIC) != M_MAGIC )
        {
            malloc_errno = M_CODE_BAD_MAGIC;
            if( todo )
            {
                malloc_warning(func);
            }
            rtn++;
            continue;
        }

        /* 
         * verify segments are correctly linked together
         */
        
        if( (ptr->prev && (ptr->prev->next != ptr) ) ||
            (ptr->next && (ptr->next->prev != ptr) ) ||
            ((ptr->next == NULL) && (ptr->prev == NULL)) )
        {
            malloc_errno = M_CODE_BAD_CONNECT;
            if( todo )
            {
                malloc_warning(func);
            }
            rtn++;
            continue;
        }

        /*
         * If this segment is allocated
         */

        if( (ptr->flag & M_INUSE) != 0 )
        {
            /*
             * verify no overflow of data area
             */

            for(i=ptr->r_size; i < ptr->s.size; i++)
            {
                if( ptr->data[i] != M_FILL )
                {
                    malloc_errno = M_CODE_OVERRUN;
                    if( todo )
                    {
                        malloc_warning(func);
                    }
                    rtn++;
                    break;
                }
            }
        }
        else /* it's not allocated so */
        {
            /*
             * verify no reuse of freed data blocks
             */

            for(i=0; i < ptr->s.size; i++)
            {
                if( ptr->data[i] != M_FREE_FILL )
                {
                    malloc_errno = M_CODE_REUSE;
                    if( todo )
                    {
                        malloc_warning(func);
                    }
                    rtn++;
                    break;
                }
            }
        }

    } /* for(... */

    return(rtn);

} /* malloc_chain_check(... */