memdebug.c 12 KB

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