2 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
3 * You may copy, distribute, and use this software as long as this
4 * copyright statement is not removed.
13 char rcs_hdr
[] = "$Id: mlc_chk.c,v 1.2 2006-07-25 10:09:34 rt Exp $";
16 extern struct mlist malloc_start
;
17 extern struct mlist
* malloc_end
;
18 extern char * malloc_data_start
;
19 extern char * malloc_data_end
;
22 * Function: malloc_in_arena()
24 * Purpose: to verify address is within malloc arena.
26 * Arguments: ptr - pointer to verify
28 * Returns: TRUE - if pointer is within malloc area
32 * IF pointer is >= malloc area start AND <= malloc area end
38 * 90/01/24 cpcahil Initial revision.
44 extern char * malloc_data_start
;
45 extern char * malloc_data_end
;
48 if( ptr
>= malloc_data_start
&& ptr
<= malloc_data_end
)
57 * Function: malloc_check_str()
59 * Arguments: func - name of function calling this routine
60 * str - pointer to area to check
62 * Purpose: to verify that if str is within the malloc arena, the data
63 * it points to does not extend beyond the applicable region.
65 * Returns: Nothing of any use (function is void).
68 * IF pointer is within malloc arena
69 * determin length of string
70 * call malloc_verify() to verify data is withing applicable region
74 * 90/01/24 cpcahil Initial revision.
75 * 90/01/29 cpcahil Added code to ignore recursive calls.
78 malloc_check_str(func
,str
)
85 if( (layers
++ == 0) && malloc_in_arena(str
) )
91 malloc_verify(func
,str
,s
-str
+1);
98 * Function: malloc_check_strn()
100 * Arguments: func - name of function calling this routine
101 * str - pointer to area to check
102 * len - max length of string
104 * Purpose: to verify that if str is within the malloc arena, the data
105 * it points to does not extend beyond the applicable region.
107 * Returns: Nothing of any use (function is void).
110 * IF pointer is within malloc arena
111 * determin length of string
112 * call malloc_verify() to verify data is withing applicable region
116 * 90/01/24 cpcahil Initial revision.
117 * 90/01/29 cpcahil Added code to ignore recursive calls.
118 * 90/08/29 cpcahil added length (for strn* functions)
121 malloc_check_strn(func
,str
,len
)
130 if( (layers
++ == 0) && malloc_in_arena(str
) )
132 for( s
=str
,i
=0; (i
< len
) && *s
; s
++)
136 malloc_verify(func
,str
,s
-str
+1);
143 * Function: malloc_check_data()
145 * Arguments: func - name of function calling this routine
146 * ptr - pointer to area to check
147 * len - length to verify
149 * Purpose: to verify that if ptr is within the malloc arena, the data
150 * it points to does not extend beyond the applicable region.
152 * Returns: Nothing of any use (function is void).
155 * IF pointer is within malloc arena
156 * call malloc_verify() to verify data is withing applicable region
160 * 90/01/24 cpcahil Initial revision.
161 * 90/01/29 cpcahil Added code to ignore recursive calls.
164 malloc_check_data(func
,ptr
,len
)
173 DEBUG3(40,"malloc_check_data(%s,0x%x,%d) called...",
175 if( malloc_in_arena(ptr
) )
177 DEBUG0(10,"pointer in malloc arena, verifying...");
178 malloc_verify(func
,ptr
,len
);
186 * Function: malloc_verify()
188 * Arguments: func - name of function calling the malloc check routines
189 * ptr - pointer to area to check
190 * len - length to verify
192 * Purpose: to verify that the data ptr points to does not extend beyond
193 * the applicable malloc region. This function is only called
194 * if it has been determined that ptr points into the malloc arena.
196 * Returns: Nothing of any use (function is void).
201 * 90/01/24 cpcahil Initial revision.
204 malloc_verify(func
,ptr
,len
)
209 extern struct mlist
* malloc_end
;
210 extern int malloc_errno
;
211 extern struct mlist malloc_start
;
214 DEBUG3(40,"malloc_verify(%s,0x%x,%d) called...", func
,ptr
,len
);
216 * Find the malloc block that includes this pointer
218 mptr
= &malloc_start
;
220 ! (((char *)mptr
< ptr
) && ((mptr
->data
+mptr
->s
.size
) > ptr
) ) )
226 * if ptr was not in a malloc block, it must be part of
227 * some direct sbrk() stuff, so just return.
231 DEBUG1(10,"ptr (0x%x) not found in malloc search", ptr
);
236 * Now we have a valid malloc block that contains the indicated
237 * pointer. We must verify that it is withing the requested block
238 * size (as opposed to the real block size which is rounded up to
239 * allow for correct alignment).
242 DEBUG4(60,"Checking 0x%x-0x%x, 0x%x-0x%x",
243 ptr
, ptr
+len
, mptr
->data
, mptr
->data
+mptr
->r_size
);
245 if( (ptr
< mptr
->data
) || ((ptr
+len
) > (mptr
->data
+mptr
->r_size
)) )
247 DEBUG4(0,"pointer not within region 0x%x-0x%x, 0x%x-0x%x",
248 ptr
, ptr
+len
, mptr
->data
, mptr
->data
+mptr
->r_size
);
250 malloc_errno
= M_CODE_OUTOF_BOUNDS
;
251 malloc_warning(func
);