1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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>
31 #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
33 /* The last 3 #include files should be in this order */
34 #include "curl_printf.h"
35 #include "curl_memory.h"
39 # define assert(x) Curl_nop_stmt
43 * Until 2011-08-17 libcurl's Memory Tracking feature also performed
44 * automatic malloc and free filling operations using 0xA5 and 0x13
45 * values. Our own preinitialization of dynamically allocated memory
46 * might be useful when not using third party memory debuggers, but
47 * on the other hand this would fool memory debuggers into thinking
48 * that all dynamically allocated memory is properly initialized.
50 * As a default setting, libcurl's Memory Tracking feature no longer
51 * performs preinitialization of dynamically allocated memory on its
52 * own. If you know what you are doing, and really want to retain old
53 * behavior, you can achieve this compiling with preprocessor symbols
54 * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
58 #ifdef CURL_MT_MALLOC_FILL
59 # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
60 # error "invalid CURL_MT_MALLOC_FILL or out of range"
64 #ifdef CURL_MT_FREE_FILL
65 # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
66 # error "invalid CURL_MT_FREE_FILL or out of range"
70 #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
71 # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
72 # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
76 #ifdef CURL_MT_MALLOC_FILL
77 # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
79 # define mt_malloc_fill(buf,len) Curl_nop_stmt
82 #ifdef CURL_MT_FREE_FILL
83 # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
85 # define mt_free_fill(buf,len) Curl_nop_stmt
95 /* I'm hoping this is the thing with the strictest alignment
96 * requirements. That also means we waste some space :-( */
100 * Note that these debug functions are very simple and they are meant to
101 * remain so. For advanced analysis, record a log file and write perl scripts
104 * Don't use these with multithreaded test programs!
107 #define logfile curl_debuglogfile
108 FILE *curl_debuglogfile = NULL;
109 static bool memlimit = FALSE; /* enable memory limit */
110 static long memsize = 0; /* set number of mallocs allowed */
112 /* this sets the log file name */
113 void curl_memdebug(const char *logname)
116 if(logname && *logname)
117 logfile = fopen(logname, FOPEN_WRITETEXT);
120 #ifdef MEMDEBUG_LOG_SYNC
121 /* Flush the log file after every line so the log isn't lost in a crash */
122 setbuf(logfile, (char *)NULL);
127 /* This function sets the number of malloc() calls that should return
129 void curl_memlimit(long limit)
137 /* returns TRUE if this isn't allowed! */
138 static bool countcheck(const char *func, int line, const char *source)
140 /* if source is NULL, then the call is made internally and this check
141 should not be made */
142 if(memlimit && source) {
146 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
148 /* log to stderr also */
149 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
151 fflush(logfile); /* because it might crash now */
154 return TRUE; /* RETURN ERROR! */
157 memsize--; /* countdown */
162 return FALSE; /* allow this */
165 void *curl_domalloc(size_t wantedsize, int line, const char *source)
167 struct memdebug *mem;
170 assert(wantedsize != 0);
172 if(countcheck("malloc", line, source))
175 /* alloc at least 64 bytes */
176 size = sizeof(struct memdebug)+wantedsize;
178 mem = (Curl_cmalloc)(size);
180 /* fill memory with junk */
181 mt_malloc_fill(mem->mem, wantedsize);
182 mem->size = wantedsize;
186 curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
187 source, line, wantedsize,
188 mem ? (void *)mem->mem : (void *)0);
190 return (mem ? mem->mem : NULL);
193 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
194 int line, const char *source)
196 struct memdebug *mem;
197 size_t size, user_size;
199 assert(wanted_elements != 0);
200 assert(wanted_size != 0);
202 if(countcheck("calloc", line, source))
205 /* alloc at least 64 bytes */
206 user_size = wanted_size * wanted_elements;
207 size = sizeof(struct memdebug) + user_size;
209 mem = (Curl_ccalloc)(1, size);
211 mem->size = user_size;
214 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
215 source, line, wanted_elements, wanted_size,
216 mem ? (void *)mem->mem : (void *)0);
218 return (mem ? mem->mem : NULL);
221 char *curl_dostrdup(const char *str, int line, const char *source)
228 if(countcheck("strdup", line, source))
233 mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
235 memcpy(mem, str, len);
238 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
239 source, line, (void *)str, len, (void *)mem);
244 #if defined(WIN32) && defined(UNICODE)
245 wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
252 if(countcheck("wcsdup", line, source))
255 wsiz = wcslen(str) + 1;
256 bsiz = wsiz * sizeof(wchar_t);
258 mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
260 memcpy(mem, str, bsiz);
263 curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
264 source, line, (void *)str, bsiz, (void *)mem);
270 /* We provide a realloc() that accepts a NULL as pointer, which then
271 performs a malloc(). In order to work with ares. */
272 void *curl_dorealloc(void *ptr, size_t wantedsize,
273 int line, const char *source)
275 struct memdebug *mem=NULL;
277 size_t size = sizeof(struct memdebug)+wantedsize;
279 assert(wantedsize != 0);
281 if(countcheck("realloc", line, source))
284 #ifdef __INTEL_COMPILER
285 # pragma warning(push)
286 # pragma warning(disable:1684)
287 /* 1684: conversion from pointer to same-sized integral type */
291 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
293 #ifdef __INTEL_COMPILER
294 # pragma warning(pop)
297 mem = (Curl_crealloc)(mem, size);
299 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
300 source, line, (void *)ptr, wantedsize,
301 mem ? (void *)mem->mem : (void *)0);
304 mem->size = wantedsize;
311 void curl_dofree(void *ptr, int line, const char *source)
313 struct memdebug *mem;
317 #ifdef __INTEL_COMPILER
318 # pragma warning(push)
319 # pragma warning(disable:1684)
320 /* 1684: conversion from pointer to same-sized integral type */
323 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
325 #ifdef __INTEL_COMPILER
326 # pragma warning(pop)
330 mt_free_fill(mem->mem, mem->size);
337 curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
340 curl_socket_t curl_socket(int domain, int type, int protocol,
341 int line, const char *source)
343 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
344 "FD %s:%d socket() = %d\n" :
345 (sizeof(curl_socket_t) == sizeof(long)) ?
346 "FD %s:%d socket() = %ld\n" :
347 "FD %s:%d socket() = %zd\n";
349 curl_socket_t sockfd = socket(domain, type, protocol);
351 if(source && (sockfd != CURL_SOCKET_BAD))
352 curl_memlog(fmt, source, line, sockfd);
357 #ifdef HAVE_SOCKETPAIR
358 int curl_socketpair(int domain, int type, int protocol,
359 curl_socket_t socket_vector[2],
360 int line, const char *source)
362 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
363 "FD %s:%d socketpair() = %d %d\n" :
364 (sizeof(curl_socket_t) == sizeof(long)) ?
365 "FD %s:%d socketpair() = %ld %ld\n" :
366 "FD %s:%d socketpair() = %zd %zd\n";
368 int res = socketpair(domain, type, protocol, socket_vector);
370 if(source && (0 == res))
371 curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
377 curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
378 int line, const char *source)
380 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
381 "FD %s:%d accept() = %d\n" :
382 (sizeof(curl_socket_t) == sizeof(long)) ?
383 "FD %s:%d accept() = %ld\n" :
384 "FD %s:%d accept() = %zd\n";
386 struct sockaddr *addr = (struct sockaddr *)saddr;
387 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
389 curl_socket_t sockfd = accept(s, addr, addrlen);
391 if(source && (sockfd != CURL_SOCKET_BAD))
392 curl_memlog(fmt, source, line, sockfd);
397 /* separate function to allow libcurl to mark a "faked" close */
398 void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
400 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
401 "FD %s:%d sclose(%d)\n":
402 (sizeof(curl_socket_t) == sizeof(long)) ?
403 "FD %s:%d sclose(%ld)\n":
404 "FD %s:%d sclose(%zd)\n";
407 curl_memlog(fmt, source, line, sockfd);
410 /* this is our own defined way to close sockets on *ALL* platforms */
411 int curl_sclose(curl_socket_t sockfd, int line, const char *source)
413 int res=sclose(sockfd);
414 curl_mark_sclose(sockfd, line, source);
418 FILE *curl_fopen(const char *file, const char *mode,
419 int line, const char *source)
421 FILE *res=fopen(file, mode);
424 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
425 source, line, file, mode, (void *)res);
431 FILE *curl_fdopen(int filedes, const char *mode,
432 int line, const char *source)
434 FILE *res=fdopen(filedes, mode);
437 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
438 source, line, filedes, mode, (void *)res);
444 int curl_fclose(FILE *file, int line, const char *source)
448 assert(file != NULL);
453 curl_memlog("FILE %s:%d fclose(%p)\n",
454 source, line, (void *)file);
459 #define LOGLINE_BUFSIZE 1024
461 /* this does the writting to the memory tracking log file */
462 void curl_memlog(const char *format, ...)
471 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
475 va_start(ap, format);
476 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
479 if(nchars > LOGLINE_BUFSIZE - 1)
480 nchars = LOGLINE_BUFSIZE - 1;
483 fwrite(buf, 1, nchars, logfile);
488 #endif /* CURLDEBUG */