memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef CURLDEBUG
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #define MEMDEBUG_NODEFINES /* do not redefine the standard functions */
  29. /* The last 3 #include files should be in this order */
  30. #include "curl_printf.h"
  31. #include "curl_memory.h"
  32. #include "memdebug.h"
  33. struct memdebug {
  34. size_t size;
  35. union {
  36. curl_off_t o;
  37. double d;
  38. void *p;
  39. } mem[1];
  40. /* I am hoping this is the thing with the strictest alignment
  41. * requirements. That also means we waste some space :-( */
  42. };
  43. /*
  44. * Note that these debug functions are simple and they are meant to remain so.
  45. * For advanced analysis, record a log file and write perl scripts to analyze
  46. * them!
  47. *
  48. * Do not use these with multithreaded test programs!
  49. */
  50. FILE *curl_dbg_logfile = NULL;
  51. static bool registered_cleanup = FALSE; /* atexit registered cleanup */
  52. static bool memlimit = FALSE; /* enable memory limit */
  53. static long memsize = 0; /* set number of mallocs allowed */
  54. /* LeakSantizier (LSAN) calls _exit() instead of exit() when a leak is detected
  55. on exit so the logfile must be closed explicitly or data could be lost.
  56. Though _exit() does not call atexit handlers such as this, LSAN's call to
  57. _exit() comes after the atexit handlers are called. curl/curl#6620 */
  58. static void curl_dbg_cleanup(void)
  59. {
  60. if(curl_dbg_logfile &&
  61. curl_dbg_logfile != stderr &&
  62. curl_dbg_logfile != stdout) {
  63. fclose(curl_dbg_logfile);
  64. }
  65. curl_dbg_logfile = NULL;
  66. }
  67. /* this sets the log filename */
  68. void curl_dbg_memdebug(const char *logname)
  69. {
  70. if(!curl_dbg_logfile) {
  71. if(logname && *logname)
  72. curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
  73. else
  74. curl_dbg_logfile = stderr;
  75. #ifdef MEMDEBUG_LOG_SYNC
  76. /* Flush the log file after every line so the log is not lost in a crash */
  77. if(curl_dbg_logfile)
  78. setbuf(curl_dbg_logfile, (char *)NULL);
  79. #endif
  80. }
  81. if(!registered_cleanup)
  82. registered_cleanup = !atexit(curl_dbg_cleanup);
  83. }
  84. /* This function sets the number of malloc() calls that should return
  85. successfully! */
  86. void curl_dbg_memlimit(long limit)
  87. {
  88. if(!memlimit) {
  89. memlimit = TRUE;
  90. memsize = limit;
  91. }
  92. }
  93. /* returns TRUE if this is not allowed! */
  94. static bool countcheck(const char *func, int line, const char *source)
  95. {
  96. /* if source is NULL, then the call is made internally and this check
  97. should not be made */
  98. if(memlimit && source) {
  99. if(!memsize) {
  100. /* log to file */
  101. curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
  102. source, line, func);
  103. /* log to stderr also */
  104. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  105. source, line, func);
  106. fflush(curl_dbg_logfile); /* because it might crash now */
  107. errno = ENOMEM;
  108. return TRUE; /* RETURN ERROR! */
  109. }
  110. else
  111. memsize--; /* countdown */
  112. }
  113. return FALSE; /* allow this */
  114. }
  115. ALLOC_FUNC void *curl_dbg_malloc(size_t wantedsize,
  116. int line, const char *source)
  117. {
  118. struct memdebug *mem;
  119. size_t size;
  120. DEBUGASSERT(wantedsize != 0);
  121. if(countcheck("malloc", line, source))
  122. return NULL;
  123. /* alloc at least 64 bytes */
  124. size = sizeof(struct memdebug) + wantedsize;
  125. mem = (Curl_cmalloc)(size);
  126. if(mem) {
  127. mem->size = wantedsize;
  128. }
  129. if(source)
  130. curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
  131. source, line, wantedsize,
  132. mem ? (void *)mem->mem : (void *)0);
  133. return (mem ? mem->mem : NULL);
  134. }
  135. ALLOC_FUNC void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
  136. int line, const char *source)
  137. {
  138. struct memdebug *mem;
  139. size_t size, user_size;
  140. DEBUGASSERT(wanted_elements != 0);
  141. DEBUGASSERT(wanted_size != 0);
  142. if(countcheck("calloc", line, source))
  143. return NULL;
  144. /* alloc at least 64 bytes */
  145. user_size = wanted_size * wanted_elements;
  146. size = sizeof(struct memdebug) + user_size;
  147. mem = (Curl_ccalloc)(1, size);
  148. if(mem)
  149. mem->size = user_size;
  150. if(source)
  151. curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
  152. source, line, wanted_elements, wanted_size,
  153. mem ? (void *)mem->mem : (void *)0);
  154. return (mem ? mem->mem : NULL);
  155. }
  156. ALLOC_FUNC char *curl_dbg_strdup(const char *str,
  157. int line, const char *source)
  158. {
  159. char *mem;
  160. size_t len;
  161. DEBUGASSERT(str != NULL);
  162. if(countcheck("strdup", line, source))
  163. return NULL;
  164. len = strlen(str) + 1;
  165. mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
  166. if(mem)
  167. memcpy(mem, str, len);
  168. if(source)
  169. curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
  170. source, line, (const void *)str, len, (const void *)mem);
  171. return mem;
  172. }
  173. #if defined(_WIN32) && defined(UNICODE)
  174. ALLOC_FUNC wchar_t *curl_dbg_wcsdup(const wchar_t *str,
  175. int line, const char *source)
  176. {
  177. wchar_t *mem;
  178. size_t wsiz, bsiz;
  179. DEBUGASSERT(str != NULL);
  180. if(countcheck("wcsdup", line, source))
  181. return NULL;
  182. wsiz = wcslen(str) + 1;
  183. bsiz = wsiz * sizeof(wchar_t);
  184. mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
  185. if(mem)
  186. memcpy(mem, str, bsiz);
  187. if(source)
  188. curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  189. source, line, (void *)str, bsiz, (void *)mem);
  190. return mem;
  191. }
  192. #endif
  193. /* We provide a realloc() that accepts a NULL as pointer, which then
  194. performs a malloc(). In order to work with ares. */
  195. void *curl_dbg_realloc(void *ptr, size_t wantedsize,
  196. int line, const char *source)
  197. {
  198. struct memdebug *mem = NULL;
  199. size_t size = sizeof(struct memdebug) + wantedsize;
  200. DEBUGASSERT(wantedsize != 0);
  201. if(countcheck("realloc", line, source))
  202. return NULL;
  203. #ifdef __INTEL_COMPILER
  204. # pragma warning(push)
  205. # pragma warning(disable:1684)
  206. /* 1684: conversion from pointer to same-sized integral type */
  207. #endif
  208. if(ptr)
  209. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  210. #ifdef __INTEL_COMPILER
  211. # pragma warning(pop)
  212. #endif
  213. mem = (Curl_crealloc)(mem, size);
  214. if(source)
  215. curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
  216. source, line, (void *)ptr, wantedsize,
  217. mem ? (void *)mem->mem : (void *)0);
  218. if(mem) {
  219. mem->size = wantedsize;
  220. return mem->mem;
  221. }
  222. return NULL;
  223. }
  224. void curl_dbg_free(void *ptr, int line, const char *source)
  225. {
  226. if(ptr) {
  227. struct memdebug *mem;
  228. #ifdef __INTEL_COMPILER
  229. # pragma warning(push)
  230. # pragma warning(disable:1684)
  231. /* 1684: conversion from pointer to same-sized integral type */
  232. #endif
  233. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  234. #ifdef __INTEL_COMPILER
  235. # pragma warning(pop)
  236. #endif
  237. /* free for real */
  238. (Curl_cfree)(mem);
  239. }
  240. if(source && ptr)
  241. curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  242. }
  243. curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
  244. int line, const char *source)
  245. {
  246. curl_socket_t sockfd;
  247. if(countcheck("socket", line, source))
  248. return CURL_SOCKET_BAD;
  249. sockfd = socket(domain, type, protocol);
  250. if(source && (sockfd != CURL_SOCKET_BAD))
  251. curl_dbg_log("FD %s:%d socket() = %" FMT_SOCKET_T "\n",
  252. source, line, sockfd);
  253. return sockfd;
  254. }
  255. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  256. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  257. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  258. const char *source)
  259. {
  260. SEND_TYPE_RETV rc;
  261. if(countcheck("send", line, source))
  262. return -1;
  263. rc = send(sockfd, buf, len, flags);
  264. if(source)
  265. curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
  266. source, line, (unsigned long)len, (long)rc);
  267. return rc;
  268. }
  269. RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  270. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  271. const char *source)
  272. {
  273. RECV_TYPE_RETV rc;
  274. if(countcheck("recv", line, source))
  275. return -1;
  276. rc = recv(sockfd, buf, len, flags);
  277. if(source)
  278. curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
  279. source, line, (unsigned long)len, (long)rc);
  280. return rc;
  281. }
  282. #ifdef HAVE_SOCKETPAIR
  283. int curl_dbg_socketpair(int domain, int type, int protocol,
  284. curl_socket_t socket_vector[2],
  285. int line, const char *source)
  286. {
  287. int res = socketpair(domain, type, protocol, socket_vector);
  288. if(source && (0 == res))
  289. curl_dbg_log("FD %s:%d socketpair() = "
  290. "%" FMT_SOCKET_T " %" FMT_SOCKET_T "\n",
  291. source, line, socket_vector[0], socket_vector[1]);
  292. return res;
  293. }
  294. #endif
  295. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  296. int line, const char *source)
  297. {
  298. struct sockaddr *addr = (struct sockaddr *)saddr;
  299. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  300. curl_socket_t sockfd = accept(s, addr, addrlen);
  301. if(source && (sockfd != CURL_SOCKET_BAD))
  302. curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
  303. source, line, sockfd);
  304. return sockfd;
  305. }
  306. /* separate function to allow libcurl to mark a "faked" close */
  307. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  308. {
  309. if(source)
  310. curl_dbg_log("FD %s:%d sclose(%" FMT_SOCKET_T ")\n",
  311. source, line, sockfd);
  312. }
  313. /* this is our own defined way to close sockets on *ALL* platforms */
  314. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  315. {
  316. int res = sclose(sockfd);
  317. curl_dbg_mark_sclose(sockfd, line, source);
  318. return res;
  319. }
  320. ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
  321. int line, const char *source)
  322. {
  323. FILE *res = fopen(file, mode);
  324. if(source)
  325. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  326. source, line, file, mode, (void *)res);
  327. return res;
  328. }
  329. ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
  330. int line, const char *source)
  331. {
  332. FILE *res = fdopen(filedes, mode);
  333. if(source)
  334. curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  335. source, line, filedes, mode, (void *)res);
  336. return res;
  337. }
  338. int curl_dbg_fclose(FILE *file, int line, const char *source)
  339. {
  340. int res;
  341. DEBUGASSERT(file != NULL);
  342. if(source)
  343. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  344. source, line, (void *)file);
  345. res = fclose(file);
  346. return res;
  347. }
  348. #define LOGLINE_BUFSIZE 1024
  349. /* this does the writing to the memory tracking log file */
  350. void curl_dbg_log(const char *format, ...)
  351. {
  352. char *buf;
  353. int nchars;
  354. va_list ap;
  355. if(!curl_dbg_logfile)
  356. return;
  357. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  358. if(!buf)
  359. return;
  360. va_start(ap, format);
  361. nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  362. va_end(ap);
  363. if(nchars > LOGLINE_BUFSIZE - 1)
  364. nchars = LOGLINE_BUFSIZE - 1;
  365. if(nchars > 0)
  366. fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
  367. (Curl_cfree)(buf);
  368. }
  369. #endif /* CURLDEBUG */