memdebug.c 13 KB

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