memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. /* The last 3 #include files should be in this order */
  29. #include "curl_printf.h"
  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. (fclose)(curl_dbg_logfile);
  63. }
  64. curl_dbg_logfile = NULL;
  65. }
  66. /* this sets the log filename */
  67. void curl_dbg_memdebug(const char *logname)
  68. {
  69. if(!curl_dbg_logfile) {
  70. if(logname && *logname)
  71. #ifdef CURL_FOPEN
  72. curl_dbg_logfile = CURL_FOPEN(logname, FOPEN_WRITETEXT);
  73. #else
  74. curl_dbg_logfile = (fopen)(logname, FOPEN_WRITETEXT);
  75. #endif
  76. else
  77. curl_dbg_logfile = stderr;
  78. #ifdef MEMDEBUG_LOG_SYNC
  79. /* Flush the log file after every line so the log is not lost in a crash */
  80. if(curl_dbg_logfile)
  81. setbuf(curl_dbg_logfile, (char *)NULL);
  82. #endif
  83. }
  84. if(!registered_cleanup)
  85. registered_cleanup = !atexit(curl_dbg_cleanup);
  86. }
  87. /* This function sets the number of malloc() calls that should return
  88. successfully! */
  89. void curl_dbg_memlimit(long limit)
  90. {
  91. if(!memlimit) {
  92. memlimit = TRUE;
  93. memsize = limit;
  94. }
  95. }
  96. /* returns TRUE if this is not allowed! */
  97. static bool countcheck(const char *func, int line, const char *source)
  98. {
  99. /* if source is NULL, then the call is made internally and this check
  100. should not be made */
  101. if(memlimit && source) {
  102. if(!memsize) {
  103. /* log to file */
  104. curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
  105. source, line, func);
  106. /* log to stderr also */
  107. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  108. source, line, func);
  109. fflush(curl_dbg_logfile); /* because it might crash now */
  110. /* !checksrc! disable ERRNOVAR 1 */
  111. CURL_SETERRNO(ENOMEM);
  112. return TRUE; /* RETURN ERROR! */
  113. }
  114. else
  115. memsize--; /* countdown */
  116. }
  117. return FALSE; /* allow this */
  118. }
  119. ALLOC_FUNC
  120. void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
  121. {
  122. struct memdebug *mem;
  123. size_t size;
  124. DEBUGASSERT(wantedsize != 0);
  125. if(countcheck("malloc", line, source))
  126. return NULL;
  127. /* alloc at least 64 bytes */
  128. size = sizeof(struct memdebug) + wantedsize;
  129. mem = (Curl_cmalloc)(size);
  130. if(mem) {
  131. mem->size = wantedsize;
  132. }
  133. if(source)
  134. curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
  135. source, line, wantedsize,
  136. mem ? (void *)mem->mem : (void *)0);
  137. return mem ? mem->mem : NULL;
  138. }
  139. ALLOC_FUNC
  140. void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
  141. int line, const char *source)
  142. {
  143. struct memdebug *mem;
  144. size_t size, user_size;
  145. DEBUGASSERT(wanted_elements != 0);
  146. DEBUGASSERT(wanted_size != 0);
  147. if(countcheck("calloc", line, source))
  148. return NULL;
  149. /* alloc at least 64 bytes */
  150. user_size = wanted_size * wanted_elements;
  151. size = sizeof(struct memdebug) + user_size;
  152. mem = (Curl_ccalloc)(1, size);
  153. if(mem)
  154. mem->size = user_size;
  155. if(source)
  156. curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
  157. source, line, wanted_elements, wanted_size,
  158. mem ? (void *)mem->mem : (void *)0);
  159. return mem ? mem->mem : NULL;
  160. }
  161. ALLOC_FUNC
  162. char *curl_dbg_strdup(const char *str, int line, const char *source)
  163. {
  164. char *mem;
  165. size_t len;
  166. DEBUGASSERT(str != NULL);
  167. if(countcheck("strdup", line, source))
  168. return NULL;
  169. len = strlen(str) + 1;
  170. mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
  171. if(mem)
  172. memcpy(mem, str, len);
  173. if(source)
  174. curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
  175. source, line, (const void *)str, len, (const void *)mem);
  176. return mem;
  177. }
  178. #if defined(_WIN32) && defined(UNICODE)
  179. ALLOC_FUNC
  180. wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source)
  181. {
  182. wchar_t *mem;
  183. size_t wsiz, bsiz;
  184. DEBUGASSERT(str != NULL);
  185. if(countcheck("wcsdup", line, source))
  186. return NULL;
  187. wsiz = wcslen(str) + 1;
  188. bsiz = wsiz * sizeof(wchar_t);
  189. mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
  190. if(mem)
  191. memcpy(mem, str, bsiz);
  192. if(source)
  193. curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  194. source, line, (const void *)str, bsiz, (void *)mem);
  195. return mem;
  196. }
  197. #endif
  198. /* We provide a realloc() that accepts a NULL as pointer, which then
  199. performs a malloc(). In order to work with ares. */
  200. void *curl_dbg_realloc(void *ptr, size_t wantedsize,
  201. int line, const char *source)
  202. {
  203. struct memdebug *mem = NULL;
  204. size_t size = sizeof(struct memdebug) + wantedsize;
  205. DEBUGASSERT(wantedsize != 0);
  206. if(countcheck("realloc", line, source))
  207. return NULL;
  208. #ifdef __INTEL_COMPILER
  209. # pragma warning(push)
  210. # pragma warning(disable:1684)
  211. /* 1684: conversion from pointer to same-sized integral type */
  212. #endif
  213. if(ptr)
  214. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  215. #ifdef __INTEL_COMPILER
  216. # pragma warning(pop)
  217. #endif
  218. mem = (Curl_crealloc)(mem, size);
  219. if(source)
  220. curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
  221. source, line, (void *)ptr, wantedsize,
  222. mem ? (void *)mem->mem : (void *)0);
  223. if(mem) {
  224. mem->size = wantedsize;
  225. return mem->mem;
  226. }
  227. return NULL;
  228. }
  229. void curl_dbg_free(void *ptr, int line, const char *source)
  230. {
  231. if(ptr) {
  232. struct memdebug *mem;
  233. #ifdef __INTEL_COMPILER
  234. # pragma warning(push)
  235. # pragma warning(disable:1684)
  236. /* 1684: conversion from pointer to same-sized integral type */
  237. #endif
  238. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  239. #ifdef __INTEL_COMPILER
  240. # pragma warning(pop)
  241. #endif
  242. /* free for real */
  243. (Curl_cfree)(mem);
  244. }
  245. if(source && ptr)
  246. curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  247. }
  248. curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
  249. int line, const char *source)
  250. {
  251. curl_socket_t sockfd;
  252. if(countcheck("socket", line, source))
  253. return CURL_SOCKET_BAD;
  254. sockfd = (socket)(domain, type, protocol);
  255. if(source && (sockfd != CURL_SOCKET_BAD))
  256. curl_dbg_log("FD %s:%d socket() = %" FMT_SOCKET_T "\n",
  257. source, line, sockfd);
  258. return sockfd;
  259. }
  260. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  261. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  262. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  263. const char *source)
  264. {
  265. SEND_TYPE_RETV rc;
  266. if(countcheck("send", line, source))
  267. return -1;
  268. rc = (send)(sockfd, buf, len, flags);
  269. if(source)
  270. curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
  271. source, line, (unsigned long)len, (long)rc);
  272. return rc;
  273. }
  274. RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  275. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  276. const char *source)
  277. {
  278. RECV_TYPE_RETV rc;
  279. if(countcheck("recv", line, source))
  280. return -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. int res = (socketpair)(domain, type, protocol, socket_vector);
  293. if(source && (0 == res))
  294. curl_dbg_log("FD %s:%d socketpair() = "
  295. "%" FMT_SOCKET_T " %" FMT_SOCKET_T "\n",
  296. source, line, socket_vector[0], socket_vector[1]);
  297. return res;
  298. }
  299. #endif
  300. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  301. int line, const char *source)
  302. {
  303. struct sockaddr *addr = (struct sockaddr *)saddr;
  304. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  305. curl_socket_t sockfd = (accept)(s, addr, addrlen);
  306. if(source && (sockfd != CURL_SOCKET_BAD))
  307. curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
  308. source, line, sockfd);
  309. return sockfd;
  310. }
  311. #ifdef HAVE_ACCEPT4
  312. curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr, void *saddrlen,
  313. int flags,
  314. int line, const char *source)
  315. {
  316. struct sockaddr *addr = (struct sockaddr *)saddr;
  317. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  318. curl_socket_t sockfd = (accept4)(s, addr, addrlen, flags);
  319. if(source && (sockfd != CURL_SOCKET_BAD))
  320. curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
  321. source, line, sockfd);
  322. return sockfd;
  323. }
  324. #endif
  325. /* separate function to allow libcurl to mark a "faked" close */
  326. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  327. {
  328. if(source)
  329. curl_dbg_log("FD %s:%d sclose(%" FMT_SOCKET_T ")\n",
  330. source, line, sockfd);
  331. }
  332. /* this is our own defined way to close sockets on *ALL* platforms */
  333. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  334. {
  335. int res = CURL_SCLOSE(sockfd);
  336. curl_dbg_mark_sclose(sockfd, line, source);
  337. return res;
  338. }
  339. ALLOC_FUNC
  340. FILE *curl_dbg_fopen(const char *file, const char *mode,
  341. int line, const char *source)
  342. {
  343. FILE *res;
  344. #ifdef CURL_FOPEN
  345. res = CURL_FOPEN(file, mode);
  346. #else
  347. res = (fopen)(file, mode);
  348. #endif
  349. if(source)
  350. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  351. source, line, file, mode, (void *)res);
  352. return res;
  353. }
  354. ALLOC_FUNC
  355. FILE *curl_dbg_fdopen(int filedes, const char *mode,
  356. int line, const char *source)
  357. {
  358. FILE *res = (fdopen)(filedes, mode);
  359. if(source)
  360. curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  361. source, line, filedes, mode, (void *)res);
  362. return res;
  363. }
  364. int curl_dbg_fclose(FILE *file, int line, const char *source)
  365. {
  366. int res;
  367. DEBUGASSERT(file != NULL);
  368. if(source)
  369. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  370. source, line, (void *)file);
  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 = 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 */