summaryrefslogtreecommitdiff
path: root/dmake/dbug/malloc/mlc_chk.c
blob: 40690580c0a08a71a2472f1963366d8d53985690 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * (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 "malloc.h"
#include "debug.h"

#ifndef lint
static
char rcs_hdr[] = "$Id: mlc_chk.c,v 1.2 2006-07-25 10:09:34 rt Exp $";
#endif

extern struct mlist	  malloc_start;
extern struct mlist	* malloc_end;
extern char		* malloc_data_start;
extern char		* malloc_data_end;

/*
 * Function:	malloc_in_arena()
 *
 * Purpose:	to verify address is within malloc arena.
 *
 * Arguments:	ptr	- pointer to verify
 *
 * Returns:	TRUE	- if pointer is within malloc area
 *		FALSE	- otherwise
 *
 * Narrative:
 *   IF pointer is >= malloc area start AND <= malloc area end
 *      return TRUE
 *   ELSE
 *      return FALSE
 *
 * Mod History:	
 *   90/01/24	cpcahil		Initial revision.
 */
int
malloc_in_arena(ptr)
    char	* ptr;
{
    extern char	* malloc_data_start;
    extern char	* malloc_data_end;
    int		  rtn = 0;

    if( ptr >= malloc_data_start && ptr <= malloc_data_end )
    {
        rtn = 1;
    }
    
    return(rtn);
}

/*
 * Function:	malloc_check_str()
 *
 * Arguments:	func	- name of function calling this routine
 *		str	- pointer to area to check
 *
 * Purpose:	to verify that if str is within the malloc arena, the data 
 *		it points to does not extend beyond the applicable region.
 *
 * Returns:	Nothing of any use (function is void).
 *
 * Narrative:
 *   IF pointer is within malloc arena
 *      determin length of string
 *      call malloc_verify() to verify data is withing applicable region
 *   return 
 *
 * Mod History:	
 *   90/01/24	cpcahil		Initial revision.
 *   90/01/29	cpcahil		Added code to ignore recursive calls.
 */
void
malloc_check_str(func,str)
    char		* func;
    char		* str;
{
    static int	  layers;
    register char	* s;

    if( (layers++ == 0) &&  malloc_in_arena(str) )
    {
        for( s=str; *s; s++)
        {
        }
        
        malloc_verify(func,str,s-str+1);
    }

    layers--;
}

/*
 * Function:	malloc_check_strn()
 *
 * Arguments:	func	- name of function calling this routine
 *		str	- pointer to area to check
 * 		len     - max length of string
 *
 * Purpose:	to verify that if str is within the malloc arena, the data 
 *		it points to does not extend beyond the applicable region.
 *
 * Returns:	Nothing of any use (function is void).
 *
 * Narrative:
 *   IF pointer is within malloc arena
 *      determin length of string
 *      call malloc_verify() to verify data is withing applicable region
 *   return 
 *
 * Mod History:	
 *   90/01/24	cpcahil		Initial revision.
 *   90/01/29	cpcahil		Added code to ignore recursive calls.
 *   90/08/29	cpcahil		added length (for strn* functions)
 */
void
malloc_check_strn(func,str,len)
    char		* func;
    char		* str;
    int		  len;
{
    register int	  i;
    static int	  layers;
    register char	* s;

    if( (layers++ == 0) &&  malloc_in_arena(str) )
    {
        for( s=str,i=0; (i < len) && *s; s++)
        {
        }
        
        malloc_verify(func,str,s-str+1);
    }

    layers--;
}

/*
 * Function:	malloc_check_data()
 *
 * Arguments:	func	- name of function calling this routine
 *		ptr	- pointer to area to check
 *		len 	- length to verify
 *
 * Purpose:	to verify that if ptr is within the malloc arena, the data 
 *		it points to does not extend beyond the applicable region.
 *
 * Returns:	Nothing of any use (function is void).
 *
 * Narrative:
 *   IF pointer is within malloc arena
 *      call malloc_verify() to verify data is withing applicable region
 *   return 
 *
 * Mod History:	
 *   90/01/24	cpcahil		Initial revision.
 *   90/01/29	cpcahil		Added code to ignore recursive calls.
 */
void
malloc_check_data(func,ptr,len)
    char		* func;
    char		* ptr;
    int		  len;
{
    static int	  layers;

    if( layers++ == 0 )
    {
        DEBUG3(40,"malloc_check_data(%s,0x%x,%d) called...",
            func,ptr,len);
        if( malloc_in_arena(ptr) )
        {
            DEBUG0(10,"pointer in malloc arena, verifying...");
            malloc_verify(func,ptr,len);
        }
    }

    layers--;
}

/*
 * Function:	malloc_verify()
 *
 * Arguments:	func	- name of function calling the malloc check routines
 *		ptr	- pointer to area to check
 *		len 	- length to verify
 *
 * Purpose:	to verify that the data ptr points to does not extend beyond
 *		the applicable malloc region.  This function is only called 
 *		if it has been determined that ptr points into the malloc arena.
 *
 * Returns:	Nothing of any use (function is void).
 *
 * Narrative:
 *
 * Mod History:	
 *   90/01/24	cpcahil		Initial revision.
 */
void
malloc_verify(func,ptr,len)
    char		* func;
    char		* ptr;
    int		  len;
{
    extern struct mlist	* malloc_end;
    extern int		  malloc_errno;
    extern struct mlist 	  malloc_start;
    struct mlist		* mptr;
    
    DEBUG3(40,"malloc_verify(%s,0x%x,%d) called...", func,ptr,len);
    /*
     * Find the malloc block that includes this pointer
     */
    mptr = &malloc_start;
    while( mptr && 
        ! (((char *)mptr < ptr) && ((mptr->data+mptr->s.size) > ptr) ) )
    {
        mptr = mptr->next;
    }

    /*
     * if ptr was not in a malloc block, it must be part of
     *    some direct sbrk() stuff, so just return.
     */
    if( ! mptr )
    {
        DEBUG1(10,"ptr (0x%x) not found in malloc search", ptr);
        return;
    }
    
    /*
      * Now we have a valid malloc block that contains the indicated
     * pointer.  We must verify that it is withing the requested block
     * size (as opposed to the real block size which is rounded up to
     * allow for correct alignment).
     */

    DEBUG4(60,"Checking  0x%x-0x%x, 0x%x-0x%x",
            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
    
    if( (ptr < mptr->data) || ((ptr+len) > (mptr->data+mptr->r_size)) )
    {
        DEBUG4(0,"pointer not within region 0x%x-0x%x, 0x%x-0x%x",
            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);

        malloc_errno = M_CODE_OUTOF_BOUNDS;
        malloc_warning(func);
    }

    return;
}