2 * $Id: pam_malloc.c,v 1.2 1996/12/01 03:14:13 morgan Exp $
3 * $FreeBSD: src/contrib/libpam/libpam/pam_malloc.c,v 1.1.1.1.6.2 2001/06/11 15:28:12 markm Exp $
4 * $DragonFly: src/contrib/libpam/libpam/Attic/pam_malloc.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
6 * $Log: pam_malloc.c,v $
7 * Revision 1.2 1996/12/01 03:14:13 morgan
10 * Revision 1.1 1996/11/10 21:26:11 morgan
16 * This pair of files helps to locate memory leaks. It is a wrapper for
17 * the malloc family of calls. (Actutally, it currently only deals
18 * with calloc, malloc, realloc, free and exit)
20 * To use these functions the header "pam_malloc.h" must be included
21 * in all parts of the code (that use the malloc functions) and this
22 * file must be linked with the result. The pam_malloc_flags can be
23 * set from another function and determine the level of logging.
25 * The output is via the macros defined in _pam_macros.h
27 * It is a debugging tool and should be turned off in released code.
29 * This suite was written by Andrew Morgan <morgan@parc.power.net> for
37 #include "pam_private.h"
39 #include <security/pam_malloc.h>
40 #include <security/_pam_macros.h>
42 /* this must be done to stop infinite recursion! */
54 * default debugging level
57 int pam_malloc_flags = PAM_MALLOC_DEFAULT;
58 int pam_malloc_delay_length = 4;
60 #define on(x) ((pam_malloc_flags&(x))==(x))
66 static const char *last_fn=NULL;
67 static const char *last_file=NULL;
68 static const char *last_call=NULL;
69 static int last_line = 1;
71 #define err(x) { _pam_output_xdebug_info(); _pam_output_debug x ; }
73 static void set_last_(const char *x, const char *f
74 , const char *fn, const int l)
76 last_fn = x ? x : "error-in-pam_malloc..";
77 last_file = f ? f : "*bad-file*";
78 last_call = fn ? fn: "*bad-fn*";
82 static void _pam_output_xdebug_info(void)
87 if (!(logfile = fopen(_PAM_LOGFILE,"a"))) {
91 fprintf(logfile, "[%s:%s(%d)->%s()] ",
92 last_file, last_call, last_line, last_fn);
99 static void hinder(void)
101 if (on(PAM_MALLOC_PAUSE)) {
102 if (on(0)) err(("pause requested"));
103 sleep(pam_malloc_delay_length);
106 if (on(PAM_MALLOC_STOP)) {
107 if (on(0)) err(("stop requested"));
113 * here are the memory pointer registering functions.. these actually
114 * use malloc(!) but that's ok! ;^)
118 void *ptr; /* pointer */
119 int nelements; /* number of elements */
120 int size; /* - each of this size */
121 char *file; /* where it was requested - filename */
122 char *function; /* - function */
123 int line; /* - line number */
127 struct reference *next;
130 static void _dump(const char *say, const struct reference *ref)
132 _pam_output_debug(" <%s: %p (#%d of %d) req. by %s(); %s line %d>\n"
134 , ref->ptr,ref->nelements,ref->size
135 , ref->function,ref->file,ref->line);
138 static struct reference *root=NULL;
140 static char *_strdup(const char *x)
144 s = (char *)malloc(strlen(x)+1);
146 if (on(0)) err(("_strdup failed"));
154 static void add_new_ref(void *new, int n, int size)
156 struct reference *ref=NULL;
158 ref = (struct reference *) malloc( sizeof(struct reference) );
159 if (new == NULL || ref == NULL) {
160 if (on(0)) err(("internal error {add_new_ref}"));
168 ref->file = _strdup(last_file);
169 ref->function = _strdup(last_call);
170 ref->line = last_line;
174 if (on(PAM_MALLOC_REQUEST)) {
175 _dump("new_ptr", ref);
181 static void del_old_ref(void *old)
183 struct reference *this,*last;
186 if (on(0)) err(("internal error {del_old_ref}"));
190 /* locate old pointer */
195 if (this->ptr == old)
201 /* Did we find a reference ? */
204 if (on(PAM_MALLOC_FREE)) {
205 _dump("free old_ptr", this);
210 last->next = this->next;
213 free(this->function);
216 if (on(0)) err(("ERROR!: bad memory"));
221 static void verify_old_ref(void *old)
223 struct reference *this;
226 if (on(0)) err(("internal error {verify_old_ref}"));
230 /* locate old pointer */
234 if (this->ptr == old)
239 /* Did we find a reference ? */
242 if (on(PAM_MALLOC_VERIFY)) {
243 _dump("verify_ptr", this);
246 if (on(0)) err(("ERROR!: bad request"));
251 static void dump_memory_list(const char *dump)
253 struct reference *this;
257 if (on(0)) err(("un-free()'d memory"));
263 if (on(0)) err(("no memory allocated"));
267 /* now for the wrappers */
269 #define _fn(x) set_last_(x,file,fn,line)
271 void *pam_malloc(size_t size, const char *file, const char *fn, const int line)
277 if (on(PAM_MALLOC_FUNC)) err(("request for %d", size));
281 if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
283 if (on(PAM_MALLOC_REQUEST)) err(("request new"));
284 add_new_ref(new, 1, size);
290 void *pam_calloc(size_t nelm, size_t size
291 , const char *file, const char *fn, const int line)
297 if (on(PAM_MALLOC_FUNC)) err(("request for %d of %d", nelm, size));
299 new = calloc(nelm,size);
301 if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
303 if (on(PAM_MALLOC_REQUEST)) err(("request new"));
304 add_new_ref(new, nelm, size);
310 void pam_free(void *ptr
311 , const char *file, const char *fn, const int line)
315 if (on(PAM_MALLOC_FUNC)) err(("request to free %p", ptr));
318 if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
320 if (on(PAM_MALLOC_FREE)) err(("deleted old"));
326 void *pam_memalign(size_t ali, size_t size
327 , const char *file, const char *fn, const int line)
330 if (on(0)) err(("not implemented currently (Sorry)"));
334 void *pam_realloc(void *ptr, size_t size
335 , const char *file, const char *fn, const int line)
341 if (on(PAM_MALLOC_FUNC)) err(("resize %p to %d", ptr, size));
344 if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
349 new = realloc(ptr, size);
351 if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
354 if (on(PAM_MALLOC_FREE)) err(("deleted old"));
357 if (on(PAM_MALLOC_NULL)) err(("old is NULL"));
359 if (on(PAM_MALLOC_REQUEST)) err(("request new"));
360 add_new_ref(new, 1, size);
366 void *pam_valloc(size_t size
367 , const char *file, const char *fn, const int line)
370 if (on(0)) err(("not implemented currently (Sorry)"));
376 void *pam_alloca(size_t size
377 , const char *file, const char *fn, const int line)
380 if (on(0)) err(("not implemented currently (Sorry)"));
385 , const char *file, const char *fn, const int line)
389 if (on(0)) err(("passed (%d)", i));
390 if (on(PAM_MALLOC_LEAKED)) {
391 dump_memory_list("leaked");