1
0

memdebug.c 12 KB

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