1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 * Until 2011-08-17 libcurl's Memory Tracking feature also performed
40 * automatic malloc and free filling operations using 0xA5 and 0x13
41 * values. Our own preinitialization of dynamically allocated memory
42 * might be useful when not using third party memory debuggers, but
43 * on the other hand this would fool memory debuggers into thinking
44 * that all dynamically allocated memory is properly initialized.
46 * As a default setting, libcurl's Memory Tracking feature no longer
47 * performs preinitialization of dynamically allocated memory on its
48 * own. If you know what you are doing, and really want to retain old
49 * behavior, you can achieve this compiling with preprocessor symbols
50 * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
54 #ifdef CURL_MT_MALLOC_FILL
55 # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
56 # error "invalid CURL_MT_MALLOC_FILL or out of range"
60 #ifdef CURL_MT_FREE_FILL
61 # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
62 # error "invalid CURL_MT_FREE_FILL or out of range"
66 #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
67 # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
68 # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
72 #ifdef CURL_MT_MALLOC_FILL
73 # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
75 # define mt_malloc_fill(buf,len) Curl_nop_stmt
78 #ifdef CURL_MT_FREE_FILL
79 # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
81 # define mt_free_fill(buf,len) Curl_nop_stmt
91 /* I'm hoping this is the thing with the strictest alignment
92 * requirements. That also means we waste some space :-( */
96 * Note that these debug functions are very simple and they are meant to
97 * remain so. For advanced analysis, record a log file and write perl scripts
100 * Don't use these with multithreaded test programs!
103 #define logfile curl_debuglogfile
104 FILE *curl_debuglogfile = NULL;
105 static bool memlimit = FALSE; /* enable memory limit */
106 static long memsize = 0; /* set number of mallocs allowed */
108 /* this sets the log file name */
109 void curl_memdebug(const char *logname)
112 if(logname && *logname)
113 logfile = fopen(logname, FOPEN_WRITETEXT);
116 #ifdef MEMDEBUG_LOG_SYNC
117 /* Flush the log file after every line so the log isn't lost in a crash */
119 setbuf(logfile, (char *)NULL);
124 /* This function sets the number of malloc() calls that should return
126 void curl_memlimit(long limit)
134 /* returns TRUE if this isn't allowed! */
135 static bool countcheck(const char *func, int line, const char *source)
137 /* if source is NULL, then the call is made internally and this check
138 should not be made */
139 if(memlimit && source) {
143 curl_memlog("LIMIT %s:%d %s reached memlimit\n",
145 /* log to stderr also */
146 fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
148 fflush(logfile); /* because it might crash now */
151 return TRUE; /* RETURN ERROR! */
154 memsize--; /* countdown */
159 return FALSE; /* allow this */
162 void *curl_domalloc(size_t wantedsize, int line, const char *source)
164 struct memdebug *mem;
167 DEBUGASSERT(wantedsize != 0);
169 if(countcheck("malloc", line, source))
172 /* alloc at least 64 bytes */
173 size = sizeof(struct memdebug) + wantedsize;
175 mem = (Curl_cmalloc)(size);
177 /* fill memory with junk */
178 mt_malloc_fill(mem->mem, wantedsize);
179 mem->size = wantedsize;
183 curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
184 source, line, wantedsize,
185 mem ? (void *)mem->mem : (void *)0);
187 return (mem ? mem->mem : NULL);
190 void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
191 int line, const char *source)
193 struct memdebug *mem;
194 size_t size, user_size;
196 DEBUGASSERT(wanted_elements != 0);
197 DEBUGASSERT(wanted_size != 0);
199 if(countcheck("calloc", line, source))
202 /* alloc at least 64 bytes */
203 user_size = wanted_size * wanted_elements;
204 size = sizeof(struct memdebug) + user_size;
206 mem = (Curl_ccalloc)(1, size);
208 mem->size = user_size;
211 curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
212 source, line, wanted_elements, wanted_size,
213 mem ? (void *)mem->mem : (void *)0);
215 return (mem ? mem->mem : NULL);
218 char *curl_dostrdup(const char *str, int line, const char *source)
223 DEBUGASSERT(str != NULL);
225 if(countcheck("strdup", line, source))
228 len = strlen(str) + 1;
230 mem = curl_domalloc(len, 0, NULL); /* NULL prevents logging */
232 memcpy(mem, str, len);
235 curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
236 source, line, (const void *)str, len, (const void *)mem);
241 #if defined(WIN32) && defined(UNICODE)
242 wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
247 DEBUGASSERT(str != NULL);
249 if(countcheck("wcsdup", line, source))
252 wsiz = wcslen(str) + 1;
253 bsiz = wsiz * sizeof(wchar_t);
255 mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
257 memcpy(mem, str, bsiz);
260 curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
261 source, line, (void *)str, bsiz, (void *)mem);
267 /* We provide a realloc() that accepts a NULL as pointer, which then
268 performs a malloc(). In order to work with ares. */
269 void *curl_dorealloc(void *ptr, size_t wantedsize,
270 int line, const char *source)
272 struct memdebug *mem = NULL;
274 size_t size = sizeof(struct memdebug) + wantedsize;
276 DEBUGASSERT(wantedsize != 0);
278 if(countcheck("realloc", line, source))
281 #ifdef __INTEL_COMPILER
282 # pragma warning(push)
283 # pragma warning(disable:1684)
284 /* 1684: conversion from pointer to same-sized integral type */
288 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
290 #ifdef __INTEL_COMPILER
291 # pragma warning(pop)
294 mem = (Curl_crealloc)(mem, size);
296 curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
297 source, line, (void *)ptr, wantedsize,
298 mem ? (void *)mem->mem : (void *)0);
301 mem->size = wantedsize;
308 void curl_dofree(void *ptr, int line, const char *source)
310 struct memdebug *mem;
314 #ifdef __INTEL_COMPILER
315 # pragma warning(push)
316 # pragma warning(disable:1684)
317 /* 1684: conversion from pointer to same-sized integral type */
320 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
322 #ifdef __INTEL_COMPILER
323 # pragma warning(pop)
327 mt_free_fill(mem->mem, mem->size);
334 curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
337 curl_socket_t curl_socket(int domain, int type, int protocol,
338 int line, const char *source)
340 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
341 "FD %s:%d socket() = %d\n" :
342 (sizeof(curl_socket_t) == sizeof(long)) ?
343 "FD %s:%d socket() = %ld\n" :
344 "FD %s:%d socket() = %zd\n";
346 curl_socket_t sockfd;
348 if(countcheck("socket", line, source))
349 return CURL_SOCKET_BAD;
351 sockfd = socket(domain, type, protocol);
353 if(source && (sockfd != CURL_SOCKET_BAD))
354 curl_memlog(fmt, source, line, sockfd);
359 SEND_TYPE_RETV curl_dosend(SEND_TYPE_ARG1 sockfd,
360 SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
361 SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
365 if(countcheck("send", line, source))
367 rc = send(sockfd, buf, len, flags);
369 curl_memlog("SEND %s:%d send(%lu) = %ld\n",
370 source, line, (unsigned long)len, (long)rc);
374 RECV_TYPE_RETV curl_dorecv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
375 RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
379 if(countcheck("recv", line, source))
381 rc = recv(sockfd, buf, len, flags);
383 curl_memlog("RECV %s:%d recv(%lu) = %ld\n",
384 source, line, (unsigned long)len, (long)rc);
388 #ifdef HAVE_SOCKETPAIR
389 int curl_socketpair(int domain, int type, int protocol,
390 curl_socket_t socket_vector[2],
391 int line, const char *source)
393 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
394 "FD %s:%d socketpair() = %d %d\n" :
395 (sizeof(curl_socket_t) == sizeof(long)) ?
396 "FD %s:%d socketpair() = %ld %ld\n" :
397 "FD %s:%d socketpair() = %zd %zd\n";
399 int res = socketpair(domain, type, protocol, socket_vector);
401 if(source && (0 == res))
402 curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
408 curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
409 int line, const char *source)
411 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
412 "FD %s:%d accept() = %d\n" :
413 (sizeof(curl_socket_t) == sizeof(long)) ?
414 "FD %s:%d accept() = %ld\n" :
415 "FD %s:%d accept() = %zd\n";
417 struct sockaddr *addr = (struct sockaddr *)saddr;
418 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
420 curl_socket_t sockfd = accept(s, addr, addrlen);
422 if(source && (sockfd != CURL_SOCKET_BAD))
423 curl_memlog(fmt, source, line, sockfd);
428 /* separate function to allow libcurl to mark a "faked" close */
429 void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
431 const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
432 "FD %s:%d sclose(%d)\n":
433 (sizeof(curl_socket_t) == sizeof(long)) ?
434 "FD %s:%d sclose(%ld)\n":
435 "FD %s:%d sclose(%zd)\n";
438 curl_memlog(fmt, source, line, sockfd);
441 /* this is our own defined way to close sockets on *ALL* platforms */
442 int curl_sclose(curl_socket_t sockfd, int line, const char *source)
444 int res = sclose(sockfd);
445 curl_mark_sclose(sockfd, line, source);
449 FILE *curl_fopen(const char *file, const char *mode,
450 int line, const char *source)
452 FILE *res = fopen(file, mode);
455 curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
456 source, line, file, mode, (void *)res);
462 FILE *curl_fdopen(int filedes, const char *mode,
463 int line, const char *source)
465 FILE *res = fdopen(filedes, mode);
468 curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
469 source, line, filedes, mode, (void *)res);
475 int curl_fclose(FILE *file, int line, const char *source)
479 DEBUGASSERT(file != NULL);
484 curl_memlog("FILE %s:%d fclose(%p)\n",
485 source, line, (void *)file);
490 #define LOGLINE_BUFSIZE 1024
492 /* this does the writing to the memory tracking log file */
493 void curl_memlog(const char *format, ...)
502 buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
506 va_start(ap, format);
507 nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
510 if(nchars > LOGLINE_BUFSIZE - 1)
511 nchars = LOGLINE_BUFSIZE - 1;
514 fwrite(buf, 1, (size_t)nchars, logfile);
519 #endif /* CURLDEBUG */