1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: memdebug.c,v 1.60 2008-10-23 08:05:40 danf Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #ifdef CURLDEBUG
27 #include <curl/curl.h>
28 
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32 
33 #define _MPRINTF_REPLACE
34 #include <curl/mprintf.h>
35 #include "urldata.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 
44 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
45 #include "memory.h"
46 #include "memdebug.h"
47 
48 struct memdebug {
49   size_t size;
50   union {
51     double d;
52     void * p;
53   } mem[1];
54   /* I'm hoping this is the thing with the strictest alignment
55    * requirements.  That also means we waste some space :-( */
56 };
57 
58 /*
59  * Note that these debug functions are very simple and they are meant to
60  * remain so. For advanced analysis, record a log file and write perl scripts
61  * to analyze them!
62  *
63  * Don't use these with multithreaded test programs!
64  */
65 
66 #define logfile curl_debuglogfile
67 FILE *curl_debuglogfile = NULL;
68 static bool memlimit = FALSE; /* enable memory limit */
69 static long memsize = 0;  /* set number of mallocs allowed */
70 
71 /* this sets the log file name */
curl_memdebug(const char * logname)72 void curl_memdebug(const char *logname)
73 {
74   if(!logfile) {
75     if(logname)
76       logfile = fopen(logname, "w");
77     else
78       logfile = stderr;
79 #ifdef MEMDEBUG_LOG_SYNC
80     /* Flush the log file after every line so the log isn't lost in a crash */
81     setvbuf(logfile, (char *)NULL, _IOLBF, 0);
82 #endif
83   }
84 }
85 
86 /* This function sets the number of malloc() calls that should return
87    successfully! */
curl_memlimit(long limit)88 void curl_memlimit(long limit)
89 {
90   if(!memlimit) {
91     memlimit = TRUE;
92     memsize = limit;
93   }
94 }
95 
96 /* returns TRUE if this isn't allowed! */
countcheck(const char * func,int line,const char * source)97 static bool countcheck(const char *func, int line, const char *source)
98 {
99   /* if source is NULL, then the call is made internally and this check
100      should not be made */
101   if(memlimit && source) {
102     if(!memsize) {
103       if(logfile && source)
104         fprintf(logfile, "LIMIT %s:%d %s reached memlimit\n",
105                 source, line, func);
106       if(source)
107         fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
108                 source, line, func);
109       SET_ERRNO(ENOMEM);
110       return TRUE; /* RETURN ERROR! */
111     }
112     else
113       memsize--; /* countdown */
114 
115     /* log the countdown */
116     if(logfile && source)
117       fprintf(logfile, "LIMIT %s:%d %ld ALLOCS left\n",
118               source, line, memsize);
119 
120   }
121 
122   return FALSE; /* allow this */
123 }
124 
curl_domalloc(size_t wantedsize,int line,const char * source)125 void *curl_domalloc(size_t wantedsize, int line, const char *source)
126 {
127   struct memdebug *mem;
128   size_t size;
129 
130   if(countcheck("malloc", line, source))
131     return NULL;
132 
133   /* alloc at least 64 bytes */
134   size = sizeof(struct memdebug)+wantedsize;
135 
136   mem=(struct memdebug *)(Curl_cmalloc)(size);
137   if(mem) {
138     /* fill memory with junk */
139     memset(mem->mem, 0xA5, wantedsize);
140     mem->size = wantedsize;
141   }
142 
143   if(logfile && source)
144     fprintf(logfile, "MEM %s:%d malloc(%zd) = %p\n",
145             source, line, wantedsize, mem ? mem->mem : 0);
146   return (mem ? mem->mem : NULL);
147 }
148 
curl_docalloc(size_t wanted_elements,size_t wanted_size,int line,const char * source)149 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
150                     int line, const char *source)
151 {
152   struct memdebug *mem;
153   size_t size, user_size;
154 
155   if(countcheck("calloc", line, source))
156     return NULL;
157 
158   /* alloc at least 64 bytes */
159   user_size = wanted_size * wanted_elements;
160   size = sizeof(struct memdebug) + user_size;
161 
162   mem = (struct memdebug *)(Curl_cmalloc)(size);
163   if(mem) {
164     /* fill memory with zeroes */
165     memset(mem->mem, 0, user_size);
166     mem->size = user_size;
167   }
168 
169   if(logfile && source)
170     fprintf(logfile, "MEM %s:%d calloc(%zu,%zu) = %p\n",
171             source, line, wanted_elements, wanted_size, mem ? mem->mem : 0);
172   return (mem ? mem->mem : NULL);
173 }
174 
curl_dostrdup(const char * str,int line,const char * source)175 char *curl_dostrdup(const char *str, int line, const char *source)
176 {
177   char *mem;
178   size_t len;
179 
180   DEBUGASSERT(str != NULL);
181 
182   if(countcheck("strdup", line, source))
183     return NULL;
184 
185   len=strlen(str)+1;
186 
187   mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
188   if(mem)
189     memcpy(mem, str, len);
190 
191   if(logfile)
192     fprintf(logfile, "MEM %s:%d strdup(%p) (%zu) = %p\n",
193             source, line, str, len, mem);
194 
195   return mem;
196 }
197 
198 /* We provide a realloc() that accepts a NULL as pointer, which then
199    performs a malloc(). In order to work with ares. */
curl_dorealloc(void * ptr,size_t wantedsize,int line,const char * source)200 void *curl_dorealloc(void *ptr, size_t wantedsize,
201                      int line, const char *source)
202 {
203   struct memdebug *mem=NULL;
204 
205   size_t size = sizeof(struct memdebug)+wantedsize;
206 
207   if(countcheck("realloc", line, source))
208     return NULL;
209 
210   if(ptr)
211     mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
212 
213   mem=(struct memdebug *)(Curl_crealloc)(mem, size);
214   if(logfile)
215     fprintf(logfile, "MEM %s:%d realloc(%p, %zu) = %p\n",
216             source, line, ptr, wantedsize, mem?mem->mem:NULL);
217 
218   if(mem) {
219     mem->size = wantedsize;
220     return mem->mem;
221   }
222 
223   return NULL;
224 }
225 
curl_dofree(void * ptr,int line,const char * source)226 void curl_dofree(void *ptr, int line, const char *source)
227 {
228   struct memdebug *mem;
229 
230   DEBUGASSERT(ptr != NULL);
231 
232   mem = (struct memdebug *)((char *)ptr - offsetof(struct memdebug, mem));
233 
234   /* destroy  */
235   memset(mem->mem, 0x13, mem->size);
236 
237   /* free for real */
238   (Curl_cfree)(mem);
239 
240   if(logfile)
241     fprintf(logfile, "MEM %s:%d free(%p)\n", source, line, ptr);
242 }
243 
curl_socket(int domain,int type,int protocol,int line,const char * source)244 int curl_socket(int domain, int type, int protocol, int line,
245                 const char *source)
246 {
247   int sockfd=socket(domain, type, protocol);
248   if(logfile && (sockfd!=-1))
249     fprintf(logfile, "FD %s:%d socket() = %d\n",
250             source, line, sockfd);
251   return sockfd;
252 }
253 
curl_accept(int s,void * saddr,void * saddrlen,int line,const char * source)254 int curl_accept(int s, void *saddr, void *saddrlen,
255                 int line, const char *source)
256 {
257   struct sockaddr *addr = (struct sockaddr *)saddr;
258   socklen_t *addrlen = (socklen_t *)saddrlen;
259   int sockfd=accept(s, addr, addrlen);
260   if(logfile)
261     fprintf(logfile, "FD %s:%d accept() = %d\n",
262             source, line, sockfd);
263   return sockfd;
264 }
265 
266 /* this is our own defined way to close sockets on *ALL* platforms */
curl_sclose(int sockfd,int line,const char * source)267 int curl_sclose(int sockfd, int line, const char *source)
268 {
269   int res=sclose(sockfd);
270   if(logfile)
271     fprintf(logfile, "FD %s:%d sclose(%d)\n",
272             source, line, sockfd);
273   return res;
274 }
275 
curl_fopen(const char * file,const char * mode,int line,const char * source)276 FILE *curl_fopen(const char *file, const char *mode,
277                  int line, const char *source)
278 {
279   FILE *res=fopen(file, mode);
280   if(logfile)
281     fprintf(logfile, "FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
282             source, line, file, mode, res);
283   return res;
284 }
285 
286 #ifdef HAVE_FDOPEN
curl_fdopen(int filedes,const char * mode,int line,const char * source)287 FILE *curl_fdopen(int filedes, const char *mode,
288                   int line, const char *source)
289 {
290   FILE *res=fdopen(filedes, mode);
291   if(logfile)
292     fprintf(logfile, "FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
293             source, line, filedes, mode, res);
294   return res;
295 }
296 #endif
297 
curl_fclose(FILE * file,int line,const char * source)298 int curl_fclose(FILE *file, int line, const char *source)
299 {
300   int res;
301 
302   DEBUGASSERT(file != NULL);
303 
304   res=fclose(file);
305   if(logfile)
306     fprintf(logfile, "FILE %s:%d fclose(%p)\n",
307             source, line, file);
308   return res;
309 }
310 #endif /* CURLDEBUG */
311