memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. ***************************************************************************/
  22. #include "setup.h"
  23. #ifdef CURLDEBUG
  24. #include <curl/curl.h>
  25. #ifdef HAVE_SYS_SOCKET_H
  26. #include <sys/socket.h>
  27. #endif
  28. #define _MPRINTF_REPLACE
  29. #include <curl/mprintf.h>
  30. #include "urldata.h"
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. #ifndef HAVE_ASSERT_H
  38. # define assert(x) Curl_nop_stmt
  39. #endif
  40. /*
  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.
  47. *
  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
  53. * values.
  54. */
  55. #ifdef CURL_MT_MALLOC_FILL
  56. # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
  57. # error "invalid CURL_MT_MALLOC_FILL or out of range"
  58. # endif
  59. #endif
  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"
  63. # endif
  64. #endif
  65. #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
  66. # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
  67. # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
  68. # endif
  69. #endif
  70. #ifdef CURL_MT_MALLOC_FILL
  71. # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
  72. #else
  73. # define mt_malloc_fill(buf,len) Curl_nop_stmt
  74. #endif
  75. #ifdef CURL_MT_FREE_FILL
  76. # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
  77. #else
  78. # define mt_free_fill(buf,len) Curl_nop_stmt
  79. #endif
  80. struct memdebug {
  81. size_t size;
  82. union {
  83. curl_off_t o;
  84. double d;
  85. void * p;
  86. } mem[1];
  87. /* I'm hoping this is the thing with the strictest alignment
  88. * requirements. That also means we waste some space :-( */
  89. };
  90. /*
  91. * Note that these debug functions are very simple and they are meant to
  92. * remain so. For advanced analysis, record a log file and write perl scripts
  93. * to analyze them!
  94. *
  95. * Don't use these with multithreaded test programs!
  96. */
  97. #define logfile curl_debuglogfile
  98. FILE *curl_debuglogfile = NULL;
  99. static bool memlimit = FALSE; /* enable memory limit */
  100. static long memsize = 0; /* set number of mallocs allowed */
  101. /* this sets the log file name */
  102. void curl_memdebug(const char *logname)
  103. {
  104. if(!logfile) {
  105. if(logname && *logname)
  106. logfile = fopen(logname, "w");
  107. else
  108. logfile = stderr;
  109. #ifdef MEMDEBUG_LOG_SYNC
  110. /* Flush the log file after every line so the log isn't lost in a crash */
  111. setvbuf(logfile, (char *)NULL, _IOLBF, 0);
  112. #endif
  113. }
  114. }
  115. /* This function sets the number of malloc() calls that should return
  116. successfully! */
  117. void curl_memlimit(long limit)
  118. {
  119. if(!memlimit) {
  120. memlimit = TRUE;
  121. memsize = limit;
  122. }
  123. }
  124. /* returns TRUE if this isn't allowed! */
  125. static bool countcheck(const char *func, int line, const char *source)
  126. {
  127. /* if source is NULL, then the call is made internally and this check
  128. should not be made */
  129. if(memlimit && source) {
  130. if(!memsize) {
  131. if(source) {
  132. /* log to file */
  133. curl_memlog("LIMIT %s:%d %s reached memlimit\n",
  134. source, line, func);
  135. /* log to stderr also */
  136. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  137. source, line, func);
  138. }
  139. SET_ERRNO(ENOMEM);
  140. return TRUE; /* RETURN ERROR! */
  141. }
  142. else
  143. memsize--; /* countdown */
  144. /* log the countdown */
  145. if(source)
  146. curl_memlog("LIMIT %s:%d %ld ALLOCS left\n",
  147. source, line, memsize);
  148. }
  149. return FALSE; /* allow this */
  150. }
  151. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  152. {
  153. struct memdebug *mem;
  154. size_t size;
  155. assert(wantedsize != 0);
  156. if(countcheck("malloc", line, source))
  157. return NULL;
  158. /* alloc at least 64 bytes */
  159. size = sizeof(struct memdebug)+wantedsize;
  160. mem = (Curl_cmalloc)(size);
  161. if(mem) {
  162. /* fill memory with junk */
  163. mt_malloc_fill(mem->mem, wantedsize);
  164. mem->size = wantedsize;
  165. }
  166. if(source)
  167. curl_memlog("MEM %s:%d malloc(%zd) = %p\n",
  168. source, line, wantedsize, mem ? mem->mem : 0);
  169. return (mem ? mem->mem : NULL);
  170. }
  171. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  172. int line, const char *source)
  173. {
  174. struct memdebug *mem;
  175. size_t size, user_size;
  176. assert(wanted_elements != 0);
  177. assert(wanted_size != 0);
  178. if(countcheck("calloc", line, source))
  179. return NULL;
  180. /* alloc at least 64 bytes */
  181. user_size = wanted_size * wanted_elements;
  182. size = sizeof(struct memdebug) + user_size;
  183. mem = (Curl_ccalloc)(1, size);
  184. if(mem)
  185. mem->size = user_size;
  186. if(source)
  187. curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
  188. source, line, wanted_elements, wanted_size, mem?mem->mem:0);
  189. return (mem ? mem->mem : NULL);
  190. }
  191. char *curl_dostrdup(const char *str, int line, const char *source)
  192. {
  193. char *mem;
  194. size_t len;
  195. assert(str != NULL);
  196. if(countcheck("strdup", line, source))
  197. return NULL;
  198. len=strlen(str)+1;
  199. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  200. if(mem)
  201. memcpy(mem, str, len);
  202. if(source)
  203. curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
  204. source, line, str, len, mem);
  205. return mem;
  206. }
  207. /* We provide a realloc() that accepts a NULL as pointer, which then
  208. performs a malloc(). In order to work with ares. */
  209. void *curl_dorealloc(void *ptr, size_t wantedsize,
  210. int line, const char *source)
  211. {
  212. struct memdebug *mem=NULL;
  213. size_t size = sizeof(struct memdebug)+wantedsize;
  214. assert(wantedsize != 0);
  215. if(countcheck("realloc", line, source))
  216. return NULL;
  217. #ifdef __INTEL_COMPILER
  218. # pragma warning(push)
  219. # pragma warning(disable:1684)
  220. /* 1684: conversion from pointer to same-sized integral type */
  221. #endif
  222. if(ptr)
  223. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  224. #ifdef __INTEL_COMPILER
  225. # pragma warning(pop)
  226. #endif
  227. mem = (Curl_crealloc)(mem, size);
  228. if(source)
  229. curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
  230. source, line, ptr, wantedsize, mem?mem->mem:NULL);
  231. if(mem) {
  232. mem->size = wantedsize;
  233. return mem->mem;
  234. }
  235. return NULL;
  236. }
  237. void curl_dofree(void *ptr, int line, const char *source)
  238. {
  239. struct memdebug *mem;
  240. assert(ptr != NULL);
  241. #ifdef __INTEL_COMPILER
  242. # pragma warning(push)
  243. # pragma warning(disable:1684)
  244. /* 1684: conversion from pointer to same-sized integral type */
  245. #endif
  246. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  247. #ifdef __INTEL_COMPILER
  248. # pragma warning(pop)
  249. #endif
  250. /* destroy */
  251. mt_free_fill(mem->mem, mem->size);
  252. /* free for real */
  253. (Curl_cfree)(mem);
  254. if(source)
  255. curl_memlog("MEM %s:%d free(%p)\n", source, line, ptr);
  256. }
  257. curl_socket_t curl_socket(int domain, int type, int protocol,
  258. int line, const char *source)
  259. {
  260. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  261. "FD %s:%d socket() = %d\n" :
  262. (sizeof(curl_socket_t) == sizeof(long)) ?
  263. "FD %s:%d socket() = %ld\n" :
  264. "FD %s:%d socket() = %zd\n" ;
  265. curl_socket_t sockfd = socket(domain, type, protocol);
  266. if(source && (sockfd != CURL_SOCKET_BAD))
  267. curl_memlog(fmt, source, line, sockfd);
  268. return sockfd;
  269. }
  270. #ifdef HAVE_SOCKETPAIR
  271. int curl_socketpair(int domain, int type, int protocol,
  272. curl_socket_t socket_vector[2],
  273. int line, const char *source)
  274. {
  275. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  276. "FD %s:%d socketpair() = %d %d\n" :
  277. (sizeof(curl_socket_t) == sizeof(long)) ?
  278. "FD %s:%d socketpair() = %ld %ld\n" :
  279. "FD %s:%d socketpair() = %zd %zd\n" ;
  280. int res = socketpair(domain, type, protocol, socket_vector);
  281. if(source && (0 == res))
  282. curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
  283. return res;
  284. }
  285. #endif
  286. curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
  287. int line, const char *source)
  288. {
  289. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  290. "FD %s:%d accept() = %d\n" :
  291. (sizeof(curl_socket_t) == sizeof(long)) ?
  292. "FD %s:%d accept() = %ld\n" :
  293. "FD %s:%d accept() = %zd\n" ;
  294. struct sockaddr *addr = (struct sockaddr *)saddr;
  295. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  296. curl_socket_t sockfd = accept(s, addr, addrlen);
  297. if(source && (sockfd != CURL_SOCKET_BAD))
  298. curl_memlog(fmt, source, line, sockfd);
  299. return sockfd;
  300. }
  301. /* separate function to allow libcurl to mark a "faked" close */
  302. void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  303. {
  304. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  305. "FD %s:%d sclose(%d)\n" :
  306. (sizeof(curl_socket_t) == sizeof(long)) ?
  307. "FD %s:%d sclose(%ld)\n" :
  308. "FD %s:%d sclose(%zd)\n" ;
  309. if(source)
  310. curl_memlog(fmt, source, line, sockfd);
  311. }
  312. /* this is our own defined way to close sockets on *ALL* platforms */
  313. int curl_sclose(curl_socket_t sockfd, int line, const char *source)
  314. {
  315. int res=sclose(sockfd);
  316. curl_mark_sclose(sockfd, line, source);
  317. return res;
  318. }
  319. FILE *curl_fopen(const char *file, const char *mode,
  320. int line, const char *source)
  321. {
  322. FILE *res=fopen(file, mode);
  323. if(source)
  324. curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  325. source, line, file, mode, res);
  326. return res;
  327. }
  328. #ifdef HAVE_FDOPEN
  329. FILE *curl_fdopen(int filedes, const char *mode,
  330. int line, const char *source)
  331. {
  332. FILE *res=fdopen(filedes, mode);
  333. if(source)
  334. curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  335. source, line, filedes, mode, res);
  336. return res;
  337. }
  338. #endif
  339. int curl_fclose(FILE *file, int line, const char *source)
  340. {
  341. int res;
  342. assert(file != NULL);
  343. res=fclose(file);
  344. if(source)
  345. curl_memlog("FILE %s:%d fclose(%p)\n",
  346. source, line, file);
  347. return res;
  348. }
  349. #define LOGLINE_BUFSIZE 1024
  350. /* this does the writting to the memory tracking log file */
  351. void curl_memlog(const char *format, ...)
  352. {
  353. char *buf;
  354. int nchars;
  355. va_list ap;
  356. if(!logfile)
  357. return;
  358. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  359. if(!buf)
  360. return;
  361. va_start(ap, format);
  362. nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  363. va_end(ap);
  364. if(nchars > LOGLINE_BUFSIZE - 1)
  365. nchars = LOGLINE_BUFSIZE - 1;
  366. if(nchars > 0)
  367. fwrite(buf, 1, nchars, logfile);
  368. (Curl_cfree)(buf);
  369. }
  370. #endif /* CURLDEBUG */