1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #include <curl/curl.h>
29 #include "curl_printf.h"
32 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
33 #include "curl_memory.h"
37 # define assert(x) Curl_nop_stmt
41 * Until 2011-08-17 libcurl's Memory Tracking feature also performed
42 * automatic malloc and free filling operations using 0xA5 and 0x13
43 * values. Our own preinitialization of dynamically allocated memory
44 * might be useful when not using third party memory debuggers, but
45 * on the other hand this would fool memory debuggers into thinking
46 * that all dynamically allocated memory is properly initialized.
48 * As a default setting, libcurl's Memory Tracking feature no longer
49 * performs preinitialization of dynamically allocated memory on its
50 * own. If you know what you are doing, and really want to retain old
51 * behavior, you can achieve this compiling with preprocessor symbols
52 * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
56 #ifdef CURL_MT_MALLOC_FILL
57 # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
58 # error "invalid CURL_MT_MALLOC_FILL or out of range"
62 #ifdef CURL_MT_FREE_FILL
63 # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
64 # error "invalid CURL_MT_FREE_FILL or out of range"
68 #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
69 # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
70 # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
74 #ifdef CURL_MT_MALLOC_FILL
75 # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
77 # define mt_malloc_fill(buf,len) Curl_nop_stmt
80 #ifdef CURL_MT_FREE_FILL
81 # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
83 # define mt_free_fill(buf,len) Curl_nop_stmt
93 /* I'm hoping this is the thing with the strictest alignment
94 * requirements. That also means we waste some space :-( */
98 * Note that these debug functions are very simple and they are meant to
99 * remain so. For advanced analysis, record a log file and write perl scripts
102 * Don't use these with multithreaded test programs!
105 #define logfile curl_debuglogfile
106 FILE *curl_debuglogfile = NULL;
107 static bool memlimit = FALSE; /* enable memory limit */
108 static long memsize = 0; /* set number of mallocs allowed */
110 /* this sets the log file name */
111 void curl_memdebug(const char *logname)
114 if(logname && *logname)
115 logfile = fopen(logname, FOPEN_WRITETEXT);
118 #ifdef MEMDEBUG_LOG_SYNC
119 /* Flush the log file after every line so the log isn't lost in a crash */
120 setvbuf(logfile, (char *)NULL, _IOLBF, 0);
125 /* This function sets the number of malloc() calls that should return
127 void curl_memlimit(long limit)
135 /* returns TRUE if this isn't allowed! */
136 static bool countcheck(const char *func, int line, const char *source)
138 /* if source is NULL, then the call is made internally and this check
139 should not be made */
140 if(memlimit && source) {
144 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
146 /* log to stderr also */
147 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
151 return TRUE; /* RETURN ERROR! */
154 memsize--; /* countdown */
156 /* log the countdown */
158 curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
159 source, line, memsize);
163 return FALSE; /* allow this */
166 void *curl_domalloc(size_t wantedsize, int line, const char *source)
168 struct memdebug *mem;
171 assert(wantedsize != 0);
173 if(countcheck("malloc", line, source))
176 /* alloc at least 64 bytes */
177 size = sizeof(struct memdebug)+wantedsize;
179 mem = (Curl_cmalloc)(size);
181 /* fill memory with junk */
182 mt_malloc_fill(mem->mem, wantedsize);
183 mem->size = wantedsize;
187 curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
188 source, line, wantedsize,
189 mem ? (void *)mem->mem : (void *)0);
191 return (mem ? mem->mem : NULL);
194 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
195 int line, const char *source)
197 struct memdebug *mem;
198 size_t size, user_size;
200 assert(wanted_elements != 0);
201 assert(wanted_size != 0);
203 if(countcheck("calloc", line, source))
206 /* alloc at least 64 bytes */
207 user_size = wanted_size * wanted_elements;
208 size = sizeof(struct memdebug) + user_size;
210 mem = (Curl_ccalloc)(1, size);
212 mem->size = user_size;
215 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
216 source, line, wanted_elements, wanted_size,
217 mem ? (void *)mem->mem : (void *)0);
219 return (mem ? mem->mem : NULL);
222 char *curl_dostrdup(const char *str, int line, const char *source)
229 if(countcheck("strdup", line, source))
234 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
236 memcpy(mem, str, len);
239 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
240 source, line, (void *)str, len, (void *)mem);
245 #if defined(WIN32) && defined(UNICODE)
246 wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
253 if(countcheck("wcsdup", line, source))
256 wsiz = wcslen(str) + 1;
257 bsiz = wsiz * sizeof(wchar_t);
259 mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
261 memcpy(mem, str, bsiz);
264 curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
265 source, line, (void *)str, bsiz, (void *)mem);
271 /* We provide a realloc() that accepts a NULL as pointer, which then
272 performs a malloc(). In order to work with ares. */
273 void *curl_dorealloc(void *ptr, size_t wantedsize,
274 int line, const char *source)
276 struct memdebug *mem=NULL;
278 size_t size = sizeof(struct memdebug)+wantedsize;
280 assert(wantedsize != 0);
282 if(countcheck("realloc", line, source))
285 #ifdef __INTEL_COMPILER
286 # pragma warning(push)
287 # pragma warning(disable:1684)
288 /* 1684: conversion from pointer to same-sized integral type */
292 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
294 #ifdef __INTEL_COMPILER
295 # pragma warning(pop)
298 mem = (Curl_crealloc)(mem, size);
300 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
301 source, line, (void *)ptr, wantedsize,
302 mem ? (void *)mem->mem : (void *)0);
305 mem->size = wantedsize;
312 void curl_dofree(void *ptr, int line, const char *source)
314 struct memdebug *mem;
318 #ifdef __INTEL_COMPILER
319 # pragma warning(push)
320 # pragma warning(disable:1684)
321 /* 1684: conversion from pointer to same-sized integral type */
324 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
326 #ifdef __INTEL_COMPILER
327 # pragma warning(pop)
331 mt_free_fill(mem->mem, mem->size);
338 curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
341 curl_socket_t curl_socket(int domain, int type, int protocol,
342 int line, const char *source)
344 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
345 "FD %s:%d socket() = %d\n" :
346 (sizeof(curl_socket_t) == sizeof(long)) ?
347 "FD %s:%d socket() = %ld\n" :
348 "FD %s:%d socket() = %zd\n";
350 curl_socket_t sockfd = socket(domain, type, protocol);
352 if(source && (sockfd != CURL_SOCKET_BAD))
353 curl_memlog(fmt, source, line, sockfd);
358 #ifdef HAVE_SOCKETPAIR
359 int curl_socketpair(int domain, int type, int protocol,
360 curl_socket_t socket_vector[2],
361 int line, const char *source)
363 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
364 "FD %s:%d socketpair() = %d %d\n" :
365 (sizeof(curl_socket_t) == sizeof(long)) ?
366 "FD %s:%d socketpair() = %ld %ld\n" :
367 "FD %s:%d socketpair() = %zd %zd\n";
369 int res = socketpair(domain, type, protocol, socket_vector);
371 if(source && (0 == res))
372 curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
378 curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
379 int line, const char *source)
381 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
382 "FD %s:%d accept() = %d\n" :
383 (sizeof(curl_socket_t) == sizeof(long)) ?
384 "FD %s:%d accept() = %ld\n" :
385 "FD %s:%d accept() = %zd\n";
387 struct sockaddr *addr = (struct sockaddr *)saddr;
388 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
390 curl_socket_t sockfd = accept(s, addr, addrlen);
392 if(source && (sockfd != CURL_SOCKET_BAD))
393 curl_memlog(fmt, source, line, sockfd);
398 /* separate function to allow libcurl to mark a "faked" close */
399 void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
401 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
402 "FD %s:%d sclose(%d)\n":
403 (sizeof(curl_socket_t) == sizeof(long)) ?
404 "FD %s:%d sclose(%ld)\n":
405 "FD %s:%d sclose(%zd)\n";
408 curl_memlog(fmt, source, line, sockfd);
411 /* this is our own defined way to close sockets on *ALL* platforms */
412 int curl_sclose(curl_socket_t sockfd, int line, const char *source)
414 int res=sclose(sockfd);
415 curl_mark_sclose(sockfd, line, source);
419 FILE *curl_fopen(const char *file, const char *mode,
420 int line, const char *source)
422 FILE *res=fopen(file, mode);
425 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
426 source, line, file, mode, (void *)res);
432 FILE *curl_fdopen(int filedes, const char *mode,
433 int line, const char *source)
435 FILE *res=fdopen(filedes, mode);
438 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
439 source, line, filedes, mode, (void *)res);
445 int curl_fclose(FILE *file, int line, const char *source)
449 assert(file != NULL);
454 curl_memlog("FILE %s:%d fclose(%p)\n",
455 source, line, (void *)file);
460 #define LOGLINE_BUFSIZE 1024
462 /* this does the writting to the memory tracking log file */
463 void curl_memlog(const char *format, ...)
472 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
476 va_start(ap, format);
477 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
480 if(nchars > LOGLINE_BUFSIZE - 1)
481 nchars = LOGLINE_BUFSIZE - 1;
484 fwrite(buf, 1, nchars, logfile);
489 #endif /* CURLDEBUG */