memdebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #ifdef __clang__
  257. #pragma clang diagnostic push
  258. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  259. #endif
  260. curl_dbg_log(fmt, source, line, sockfd);
  261. #ifdef __clang__
  262. #pragma clang diagnostic pop
  263. #endif
  264. }
  265. return sockfd;
  266. }
  267. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  268. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  269. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  270. const char *source)
  271. {
  272. SEND_TYPE_RETV rc;
  273. if(countcheck("send", line, source))
  274. return -1;
  275. rc = send(sockfd, buf, len, flags);
  276. if(source)
  277. curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
  278. source, line, (unsigned long)len, (long)rc);
  279. return rc;
  280. }
  281. RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  282. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  283. const char *source)
  284. {
  285. RECV_TYPE_RETV rc;
  286. if(countcheck("recv", line, source))
  287. return -1;
  288. rc = recv(sockfd, buf, len, flags);
  289. if(source)
  290. curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
  291. source, line, (unsigned long)len, (long)rc);
  292. return rc;
  293. }
  294. #ifdef HAVE_SOCKETPAIR
  295. int curl_dbg_socketpair(int domain, int type, int protocol,
  296. curl_socket_t socket_vector[2],
  297. int line, const char *source)
  298. {
  299. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  300. "FD %s:%d socketpair() = %d %d\n" :
  301. (sizeof(curl_socket_t) == sizeof(long)) ?
  302. "FD %s:%d socketpair() = %ld %ld\n" :
  303. "FD %s:%d socketpair() = %zd %zd\n";
  304. int res = socketpair(domain, type, protocol, socket_vector);
  305. if(source && (0 == res)) {
  306. #ifdef __clang__
  307. #pragma clang diagnostic push
  308. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  309. #endif
  310. curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
  311. #ifdef __clang__
  312. #pragma clang diagnostic pop
  313. #endif
  314. }
  315. return res;
  316. }
  317. #endif
  318. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  319. int line, const char *source)
  320. {
  321. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  322. "FD %s:%d accept() = %d\n" :
  323. (sizeof(curl_socket_t) == sizeof(long)) ?
  324. "FD %s:%d accept() = %ld\n" :
  325. "FD %s:%d accept() = %zd\n";
  326. struct sockaddr *addr = (struct sockaddr *)saddr;
  327. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  328. curl_socket_t sockfd = accept(s, addr, addrlen);
  329. if(source && (sockfd != CURL_SOCKET_BAD)) {
  330. #ifdef __clang__
  331. #pragma clang diagnostic push
  332. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  333. #endif
  334. curl_dbg_log(fmt, source, line, sockfd);
  335. #ifdef __clang__
  336. #pragma clang diagnostic pop
  337. #endif
  338. }
  339. return sockfd;
  340. }
  341. /* separate function to allow libcurl to mark a "faked" close */
  342. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  343. {
  344. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  345. "FD %s:%d sclose(%d)\n":
  346. (sizeof(curl_socket_t) == sizeof(long)) ?
  347. "FD %s:%d sclose(%ld)\n":
  348. "FD %s:%d sclose(%zd)\n";
  349. if(source) {
  350. #ifdef __clang__
  351. #pragma clang diagnostic push
  352. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  353. #endif
  354. curl_dbg_log(fmt, source, line, sockfd);
  355. #ifdef __clang__
  356. #pragma clang diagnostic pop
  357. #endif
  358. }
  359. }
  360. /* this is our own defined way to close sockets on *ALL* platforms */
  361. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  362. {
  363. int res = sclose(sockfd);
  364. curl_dbg_mark_sclose(sockfd, line, source);
  365. return res;
  366. }
  367. ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
  368. int line, const char *source)
  369. {
  370. FILE *res = fopen(file, mode);
  371. if(source)
  372. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  373. source, line, file, mode, (void *)res);
  374. return res;
  375. }
  376. ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
  377. int line, const char *source)
  378. {
  379. FILE *res = fdopen(filedes, mode);
  380. if(source)
  381. curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  382. source, line, filedes, mode, (void *)res);
  383. return res;
  384. }
  385. int curl_dbg_fclose(FILE *file, int line, const char *source)
  386. {
  387. int res;
  388. DEBUGASSERT(file != NULL);
  389. if(source)
  390. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  391. source, line, (void *)file);
  392. res = fclose(file);
  393. return res;
  394. }
  395. #define LOGLINE_BUFSIZE 1024
  396. /* this does the writing to the memory tracking log file */
  397. void curl_dbg_log(const char *format, ...)
  398. {
  399. char *buf;
  400. int nchars;
  401. va_list ap;
  402. if(!curl_dbg_logfile)
  403. return;
  404. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  405. if(!buf)
  406. return;
  407. va_start(ap, format);
  408. #ifdef __clang__
  409. #pragma clang diagnostic push
  410. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  411. #endif
  412. nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  413. #ifdef __clang__
  414. #pragma clang diagnostic pop
  415. #endif
  416. va_end(ap);
  417. if(nchars > LOGLINE_BUFSIZE - 1)
  418. nchars = LOGLINE_BUFSIZE - 1;
  419. if(nchars > 0)
  420. fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
  421. (Curl_cfree)(buf);
  422. }
  423. #endif /* CURLDEBUG */