memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 https://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 "urldata.h"
  26. #define MEMDEBUG_NODEFINES /* don't redefine the standard functions */
  27. /* The last 3 #include files should be in this order */
  28. #include "curl_printf.h"
  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, FOPEN_WRITETEXT);
  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. setbuf(logfile, (char *)NULL);
  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. fflush(logfile); /* because it might crash now */
  133. }
  134. SET_ERRNO(ENOMEM);
  135. return TRUE; /* RETURN ERROR! */
  136. }
  137. else
  138. memsize--; /* countdown */
  139. }
  140. return FALSE; /* allow this */
  141. }
  142. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  143. {
  144. struct memdebug *mem;
  145. size_t size;
  146. assert(wantedsize != 0);
  147. if(countcheck("malloc", line, source))
  148. return NULL;
  149. /* alloc at least 64 bytes */
  150. size = sizeof(struct memdebug)+wantedsize;
  151. mem = (Curl_cmalloc)(size);
  152. if(mem) {
  153. /* fill memory with junk */
  154. mt_malloc_fill(mem->mem, wantedsize);
  155. mem->size = wantedsize;
  156. }
  157. if(source)
  158. curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
  159. source, line, wantedsize,
  160. mem ? (void *)mem->mem : (void *)0);
  161. return (mem ? mem->mem : NULL);
  162. }
  163. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  164. int line, const char *source)
  165. {
  166. struct memdebug *mem;
  167. size_t size, user_size;
  168. assert(wanted_elements != 0);
  169. assert(wanted_size != 0);
  170. if(countcheck("calloc", line, source))
  171. return NULL;
  172. /* alloc at least 64 bytes */
  173. user_size = wanted_size * wanted_elements;
  174. size = sizeof(struct memdebug) + user_size;
  175. mem = (Curl_ccalloc)(1, size);
  176. if(mem)
  177. mem->size = user_size;
  178. if(source)
  179. curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
  180. source, line, wanted_elements, wanted_size,
  181. mem ? (void *)mem->mem : (void *)0);
  182. return (mem ? mem->mem : NULL);
  183. }
  184. char *curl_dostrdup(const char *str, int line, const char *source)
  185. {
  186. char *mem;
  187. size_t len;
  188. assert(str != NULL);
  189. if(countcheck("strdup", line, source))
  190. return NULL;
  191. len=strlen(str)+1;
  192. mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  193. if(mem)
  194. memcpy(mem, str, len);
  195. if(source)
  196. curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
  197. source, line, (void *)str, len, (void *)mem);
  198. return mem;
  199. }
  200. #if defined(WIN32) && defined(UNICODE)
  201. wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
  202. {
  203. wchar_t *mem;
  204. size_t wsiz, bsiz;
  205. assert(str != NULL);
  206. if(countcheck("wcsdup", line, source))
  207. return NULL;
  208. wsiz = wcslen(str) + 1;
  209. bsiz = wsiz * sizeof(wchar_t);
  210. mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
  211. if(mem)
  212. memcpy(mem, str, bsiz);
  213. if(source)
  214. curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  215. source, line, (void *)str, bsiz, (void *)mem);
  216. return mem;
  217. }
  218. #endif
  219. /* We provide a realloc() that accepts a NULL as pointer, which then
  220. performs a malloc(). In order to work with ares. */
  221. void *curl_dorealloc(void *ptr, size_t wantedsize,
  222. int line, const char *source)
  223. {
  224. struct memdebug *mem=NULL;
  225. size_t size = sizeof(struct memdebug)+wantedsize;
  226. assert(wantedsize != 0);
  227. if(countcheck("realloc", line, source))
  228. return NULL;
  229. #ifdef __INTEL_COMPILER
  230. # pragma warning(push)
  231. # pragma warning(disable:1684)
  232. /* 1684: conversion from pointer to same-sized integral type */
  233. #endif
  234. if(ptr)
  235. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  236. #ifdef __INTEL_COMPILER
  237. # pragma warning(pop)
  238. #endif
  239. mem = (Curl_crealloc)(mem, size);
  240. if(source)
  241. curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
  242. source, line, (void *)ptr, wantedsize,
  243. mem ? (void *)mem->mem : (void *)0);
  244. if(mem) {
  245. mem->size = wantedsize;
  246. return mem->mem;
  247. }
  248. return NULL;
  249. }
  250. void curl_dofree(void *ptr, int line, const char *source)
  251. {
  252. struct memdebug *mem;
  253. if(ptr) {
  254. #ifdef __INTEL_COMPILER
  255. # pragma warning(push)
  256. # pragma warning(disable:1684)
  257. /* 1684: conversion from pointer to same-sized integral type */
  258. #endif
  259. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  260. #ifdef __INTEL_COMPILER
  261. # pragma warning(pop)
  262. #endif
  263. /* destroy */
  264. mt_free_fill(mem->mem, mem->size);
  265. /* free for real */
  266. (Curl_cfree)(mem);
  267. }
  268. if(source)
  269. curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  270. }
  271. curl_socket_t curl_socket(int domain, int type, int protocol,
  272. int line, const char *source)
  273. {
  274. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  275. "FD %s:%d socket() = %d\n" :
  276. (sizeof(curl_socket_t) == sizeof(long)) ?
  277. "FD %s:%d socket() = %ld\n" :
  278. "FD %s:%d socket() = %zd\n";
  279. curl_socket_t sockfd = socket(domain, type, protocol);
  280. if(source && (sockfd != CURL_SOCKET_BAD))
  281. curl_memlog(fmt, source, line, sockfd);
  282. return sockfd;
  283. }
  284. #ifdef HAVE_SOCKETPAIR
  285. int curl_socketpair(int domain, int type, int protocol,
  286. curl_socket_t socket_vector[2],
  287. int line, const char *source)
  288. {
  289. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  290. "FD %s:%d socketpair() = %d %d\n" :
  291. (sizeof(curl_socket_t) == sizeof(long)) ?
  292. "FD %s:%d socketpair() = %ld %ld\n" :
  293. "FD %s:%d socketpair() = %zd %zd\n";
  294. int res = socketpair(domain, type, protocol, socket_vector);
  295. if(source && (0 == res))
  296. curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
  297. return res;
  298. }
  299. #endif
  300. curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
  301. int line, const char *source)
  302. {
  303. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  304. "FD %s:%d accept() = %d\n" :
  305. (sizeof(curl_socket_t) == sizeof(long)) ?
  306. "FD %s:%d accept() = %ld\n" :
  307. "FD %s:%d accept() = %zd\n";
  308. struct sockaddr *addr = (struct sockaddr *)saddr;
  309. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  310. curl_socket_t sockfd = accept(s, addr, addrlen);
  311. if(source && (sockfd != CURL_SOCKET_BAD))
  312. curl_memlog(fmt, source, line, sockfd);
  313. return sockfd;
  314. }
  315. /* separate function to allow libcurl to mark a "faked" close */
  316. void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  317. {
  318. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  319. "FD %s:%d sclose(%d)\n":
  320. (sizeof(curl_socket_t) == sizeof(long)) ?
  321. "FD %s:%d sclose(%ld)\n":
  322. "FD %s:%d sclose(%zd)\n";
  323. if(source)
  324. curl_memlog(fmt, source, line, sockfd);
  325. }
  326. /* this is our own defined way to close sockets on *ALL* platforms */
  327. int curl_sclose(curl_socket_t sockfd, int line, const char *source)
  328. {
  329. int res=sclose(sockfd);
  330. curl_mark_sclose(sockfd, line, source);
  331. return res;
  332. }
  333. FILE *curl_fopen(const char *file, const char *mode,
  334. int line, const char *source)
  335. {
  336. FILE *res=fopen(file, mode);
  337. if(source)
  338. curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  339. source, line, file, mode, (void *)res);
  340. return res;
  341. }
  342. #ifdef HAVE_FDOPEN
  343. FILE *curl_fdopen(int filedes, const char *mode,
  344. int line, const char *source)
  345. {
  346. FILE *res=fdopen(filedes, mode);
  347. if(source)
  348. curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  349. source, line, filedes, mode, (void *)res);
  350. return res;
  351. }
  352. #endif
  353. int curl_fclose(FILE *file, int line, const char *source)
  354. {
  355. int res;
  356. assert(file != NULL);
  357. res=fclose(file);
  358. if(source)
  359. curl_memlog("FILE %s:%d fclose(%p)\n",
  360. source, line, (void *)file);
  361. return res;
  362. }
  363. #define LOGLINE_BUFSIZE 1024
  364. /* this does the writting to the memory tracking log file */
  365. void curl_memlog(const char *format, ...)
  366. {
  367. char *buf;
  368. int nchars;
  369. va_list ap;
  370. if(!logfile)
  371. return;
  372. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  373. if(!buf)
  374. return;
  375. va_start(ap, format);
  376. nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  377. va_end(ap);
  378. if(nchars > LOGLINE_BUFSIZE - 1)
  379. nchars = LOGLINE_BUFSIZE - 1;
  380. if(nchars > 0)
  381. fwrite(buf, 1, nchars, logfile);
  382. (Curl_cfree)(buf);
  383. }
  384. #endif /* CURLDEBUG */