2
0

memdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 /* don't 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'm 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 very simple and they are meant to
  45. * remain so. For advanced analysis, record a log file and write perl scripts
  46. * to analyze them!
  47. *
  48. * Don't 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 file name */
  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 isn't 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 isn't 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. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  247. "FD %s:%d socket() = %d\n" :
  248. (sizeof(curl_socket_t) == sizeof(long)) ?
  249. "FD %s:%d socket() = %ld\n" :
  250. "FD %s:%d socket() = %zd\n";
  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(fmt, source, line, sockfd);
  257. return sockfd;
  258. }
  259. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  260. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  261. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  262. const char *source)
  263. {
  264. SEND_TYPE_RETV rc;
  265. if(countcheck("send", line, source))
  266. return -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, int line,
  275. const char *source)
  276. {
  277. RECV_TYPE_RETV rc;
  278. if(countcheck("recv", line, source))
  279. return -1;
  280. rc = recv(sockfd, buf, len, flags);
  281. if(source)
  282. curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
  283. source, line, (unsigned long)len, (long)rc);
  284. return rc;
  285. }
  286. #ifdef HAVE_SOCKETPAIR
  287. int curl_dbg_socketpair(int domain, int type, int protocol,
  288. curl_socket_t socket_vector[2],
  289. int line, const char *source)
  290. {
  291. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  292. "FD %s:%d socketpair() = %d %d\n" :
  293. (sizeof(curl_socket_t) == sizeof(long)) ?
  294. "FD %s:%d socketpair() = %ld %ld\n" :
  295. "FD %s:%d socketpair() = %zd %zd\n";
  296. int res = socketpair(domain, type, protocol, socket_vector);
  297. if(source && (0 == res))
  298. curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
  299. return res;
  300. }
  301. #endif
  302. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  303. int line, const char *source)
  304. {
  305. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  306. "FD %s:%d accept() = %d\n" :
  307. (sizeof(curl_socket_t) == sizeof(long)) ?
  308. "FD %s:%d accept() = %ld\n" :
  309. "FD %s:%d accept() = %zd\n";
  310. struct sockaddr *addr = (struct sockaddr *)saddr;
  311. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  312. curl_socket_t sockfd = accept(s, addr, addrlen);
  313. if(source && (sockfd != CURL_SOCKET_BAD))
  314. curl_dbg_log(fmt, source, line, sockfd);
  315. return sockfd;
  316. }
  317. /* separate function to allow libcurl to mark a "faked" close */
  318. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  319. {
  320. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  321. "FD %s:%d sclose(%d)\n":
  322. (sizeof(curl_socket_t) == sizeof(long)) ?
  323. "FD %s:%d sclose(%ld)\n":
  324. "FD %s:%d sclose(%zd)\n";
  325. if(source)
  326. curl_dbg_log(fmt, source, line, sockfd);
  327. }
  328. /* this is our own defined way to close sockets on *ALL* platforms */
  329. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  330. {
  331. int res = sclose(sockfd);
  332. curl_dbg_mark_sclose(sockfd, line, source);
  333. return res;
  334. }
  335. ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
  336. int line, const char *source)
  337. {
  338. FILE *res = fopen(file, mode);
  339. if(source)
  340. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  341. source, line, file, mode, (void *)res);
  342. return res;
  343. }
  344. ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
  345. int line, const char *source)
  346. {
  347. FILE *res = fdopen(filedes, mode);
  348. if(source)
  349. curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  350. source, line, filedes, mode, (void *)res);
  351. return res;
  352. }
  353. int curl_dbg_fclose(FILE *file, int line, const char *source)
  354. {
  355. int res;
  356. DEBUGASSERT(file != NULL);
  357. if(source)
  358. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  359. source, line, (void *)file);
  360. res = fclose(file);
  361. return res;
  362. }
  363. #define LOGLINE_BUFSIZE 1024
  364. /* this does the writing to the memory tracking log file */
  365. void curl_dbg_log(const char *format, ...)
  366. {
  367. char *buf;
  368. int nchars;
  369. va_list ap;
  370. if(!curl_dbg_logfile)
  371. return;
  372. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  373. if(!buf)
  374. return;
  375. va_start(ap, format);
  376. nchars = mvsnprintf(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, (size_t)nchars, curl_dbg_logfile);
  382. (Curl_cfree)(buf);
  383. }
  384. #endif /* CURLDEBUG */