memdebug.c 12 KB

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