Initial import from FreeBSD RELENG_4:
[games.git] / contrib / libpam / libpam / pam_malloc.c
1 /*
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  *
5  * $Log: pam_malloc.c,v $
6  * Revision 1.2  1996/12/01 03:14:13  morgan
7  * use _pam_macros.h
8  *
9  * Revision 1.1  1996/11/10 21:26:11  morgan
10  * Initial revision
11  *
12  */
13
14 /*
15  * This pair of files helps to locate memory leaks. It is a wrapper for
16  * the malloc family of calls. (Actutally, it currently only deals
17  * with calloc, malloc, realloc, free and exit)
18  *
19  * To use these functions the header "pam_malloc.h" must be included
20  * in all parts of the code (that use the malloc functions) and this
21  * file must be linked with the result. The pam_malloc_flags can be
22  * set from another function and determine the level of logging.
23  *
24  * The output is via the macros defined in _pam_macros.h
25  *
26  * It is a debugging tool and should be turned off in released code.
27  *
28  * This suite was written by Andrew Morgan <morgan@parc.power.net> for
29  * Linux-PAM.
30  */
31
32 #ifndef DEBUG
33 #define DEBUG
34 #endif
35
36 #include "pam_private.h"
37
38 #include <security/pam_malloc.h>
39 #include <security/_pam_macros.h>
40
41 /* this must be done to stop infinite recursion! */
42 #undef malloc
43 #undef calloc
44 #undef free
45 #undef realloc
46 #undef exit
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 /*
53  * default debugging level
54  */
55
56 int pam_malloc_flags = PAM_MALLOC_DEFAULT;
57 int pam_malloc_delay_length = 4;
58
59 #define on(x) ((pam_malloc_flags&(x))==(x))
60
61 /*
62  * the implementation
63  */
64
65 static const char *last_fn=NULL;
66 static const char *last_file=NULL;
67 static const char *last_call=NULL;
68 static int last_line = 1;
69
70 #define err(x) { _pam_output_xdebug_info(); _pam_output_debug x ; }
71
72 static void set_last_(const char *x, const char *f
73                       , const char *fn, const int l)
74 {
75      last_fn   = x  ? x : "error-in-pam_malloc..";
76      last_file = f  ? f : "*bad-file*";
77      last_call = fn ? fn: "*bad-fn*";
78      last_line = l;
79 }
80
81 static void _pam_output_xdebug_info(void)
82 {
83     FILE *logfile;
84     int must_close = 1;
85     
86     if (!(logfile = fopen(_PAM_LOGFILE,"a"))) {
87         logfile = stderr;
88         must_close = 0;
89     }
90     fprintf(logfile, "[%s:%s(%d)->%s()] ",
91            last_file, last_call, last_line, last_fn);
92     if (must_close) {
93         fflush(logfile);
94         fclose(logfile);
95     }
96 }
97
98 static void hinder(void)
99 {
100      if (on(PAM_MALLOC_PAUSE)) {
101           if (on(0)) err(("pause requested"));
102           sleep(pam_malloc_delay_length);
103      }
104
105      if (on(PAM_MALLOC_STOP)) {
106           if (on(0)) err(("stop requested"));
107           exit(1);
108      }
109 }
110
111 /*
112  * here are the memory pointer registering functions.. these actually
113  * use malloc(!) but that's ok! ;^)
114  */
115
116 struct reference {
117      void *ptr;          /* pointer */
118      int nelements;      /* number of elements */
119      int size;           /* - each of this size */
120      char *file;         /* where it was requested - filename */
121      char *function;     /*                        - function */
122      int line;           /*                        - line number */
123 /*
124  * linking info
125  */
126      struct reference *next;
127 };
128
129 static void _dump(const char *say, const struct reference *ref)
130 {
131     _pam_output_debug(" <%s: %p (#%d of %d) req. by %s(); %s line %d>\n"
132                       , say
133                       , ref->ptr,ref->nelements,ref->size
134                       , ref->function,ref->file,ref->line);
135 }
136
137 static struct reference *root=NULL;
138
139 static char *_strdup(const char *x)
140 {
141      char *s;
142
143      s = (char *)malloc(strlen(x)+1);
144      if (s == NULL) {
145           if (on(0)) err(("_strdup failed"));
146           exit(1);
147      }
148
149      strcpy(s,x);
150      return s;
151 }
152
153 static void add_new_ref(void *new, int n, int size)
154 {
155      struct reference *ref=NULL;
156
157      ref = (struct reference *) malloc( sizeof(struct reference) );
158      if (new == NULL || ref == NULL) {
159           if (on(0)) err(("internal error {add_new_ref}"));
160           exit(1);
161      }
162
163      ref->ptr = new;
164      ref->nelements = n;
165      ref->size = size;
166
167      ref->file = _strdup(last_file);
168      ref->function = _strdup(last_call);
169      ref->line = last_line;
170
171      ref->next = root;
172
173      if (on(PAM_MALLOC_REQUEST)) {
174           _dump("new_ptr", ref);
175      }
176
177      root = ref;
178 }
179
180 static void del_old_ref(void *old)
181 {
182      struct reference *this,*last;
183
184      if (old == NULL) {
185           if (on(0)) err(("internal error {del_old_ref}"));
186           exit(1);
187      }
188
189      /* locate old pointer */
190
191      last = NULL;
192      this = root;
193      while (this) {
194           if (this->ptr == old)
195                break;
196           last = this;
197           this = this->next;
198      }
199
200      /* Did we find a reference ? */
201
202      if (this) {
203           if (on(PAM_MALLOC_FREE)) {
204                _dump("free old_ptr", this);
205           }
206           if (last == NULL) {
207                root = this->next;
208           } else {
209                last->next = this->next;
210           }
211           free(this->file);
212           free(this->function);
213           free(this);
214      } else {
215           if (on(0)) err(("ERROR!: bad memory"));
216           hinder();
217      }
218 }
219
220 static void verify_old_ref(void *old)
221 {
222      struct reference *this;
223
224      if (old == NULL) {
225           if (on(0)) err(("internal error {verify_old_ref}"));
226           exit(1);
227      }
228
229      /* locate old pointer */
230
231      this = root;
232      while (this) {
233           if (this->ptr == old)
234                break;
235           this = this->next;
236      }
237
238      /* Did we find a reference ? */
239
240      if (this) {
241           if (on(PAM_MALLOC_VERIFY)) {
242                _dump("verify_ptr", this);
243           }
244      } else {
245           if (on(0)) err(("ERROR!: bad request"));
246           hinder();
247      }
248 }
249
250 static void dump_memory_list(const char *dump)
251 {
252      struct reference *this;
253
254      this = root;
255      if (this) {
256           if (on(0)) err(("un-free()'d memory"));
257           while (this) {
258                _dump(dump, this);
259                this = this->next;
260           }
261      } else {
262           if (on(0)) err(("no memory allocated"));
263      }
264 }
265
266 /* now for the wrappers */
267
268 #define _fn(x)  set_last_(x,file,fn,line)
269
270 void *pam_malloc(size_t size, const char *file, const char *fn, const int line)
271 {
272      void *new;
273
274      _fn("malloc");
275
276      if (on(PAM_MALLOC_FUNC)) err(("request for %d", size));
277
278      new = malloc(size);
279      if (new == NULL) {
280           if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
281      } else {
282           if (on(PAM_MALLOC_REQUEST)) err(("request new"));
283           add_new_ref(new, 1, size);
284      }
285
286      return new;
287 }
288
289 void *pam_calloc(size_t nelm, size_t size
290                 , const char *file, const char *fn, const int line)
291 {
292      void *new;
293
294      _fn("calloc");
295
296      if (on(PAM_MALLOC_FUNC)) err(("request for %d of %d", nelm, size));
297
298      new = calloc(nelm,size);
299      if (new == NULL) {
300           if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
301      } else {
302           if (on(PAM_MALLOC_REQUEST)) err(("request new"));
303           add_new_ref(new, nelm, size);
304      }
305
306      return new;
307 }
308
309 void  pam_free(void *ptr
310               , const char *file, const char *fn, const int line)
311 {
312      _fn("free");
313
314      if (on(PAM_MALLOC_FUNC)) err(("request to free %p", ptr));
315
316      if (ptr == NULL) {
317           if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
318      } else {
319           if (on(PAM_MALLOC_FREE)) err(("deleted old"));
320           del_old_ref(ptr);
321           free(ptr);
322      }
323 }
324
325 void *pam_memalign(size_t ali, size_t size
326                   , const char *file, const char *fn, const int line)
327 {
328      _fn("memalign");
329      if (on(0)) err(("not implemented currently (Sorry)"));
330      exit(1);
331 }
332
333 void *pam_realloc(void *ptr, size_t size
334                 , const char *file, const char *fn, const int line)
335 {
336      void *new;
337
338      _fn("realloc");
339
340      if (on(PAM_MALLOC_FUNC)) err(("resize %p to %d", ptr, size));
341
342      if (ptr == NULL) {
343           if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
344      } else {
345           verify_old_ref(ptr);
346      }
347
348      new = realloc(ptr, size);
349      if (new == NULL) {
350           if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
351      } else {
352           if (ptr) {
353                if (on(PAM_MALLOC_FREE)) err(("deleted old"));
354                del_old_ref(ptr);
355           } else {
356                if (on(PAM_MALLOC_NULL)) err(("old is NULL"));
357           }
358           if (on(PAM_MALLOC_REQUEST)) err(("request new"));
359           add_new_ref(new, 1, size);
360      }
361
362      return new;
363 }
364
365 void *pam_valloc(size_t size
366                 , const char *file, const char *fn, const int line)
367 {
368      _fn("valloc");
369      if (on(0)) err(("not implemented currently (Sorry)"));
370      exit(1);
371 }
372
373 #include <alloca.h>
374
375 void *pam_alloca(size_t size
376                 , const char *file, const char *fn, const int line)
377 {
378      _fn("alloca");
379      if (on(0)) err(("not implemented currently (Sorry)"));
380      exit(1);
381 }
382
383 void pam_exit(int i
384               , const char *file, const char *fn, const int line)
385 {
386      _fn("exit");
387
388      if (on(0)) err(("passed (%d)", i));
389      if (on(PAM_MALLOC_LEAKED)) {
390           dump_memory_list("leaked");
391      }
392      exit(i);
393 }
394
395 /* end of file */