memdebug.c 13 KB

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