memdebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. /*
  32. * Until 2011-08-17 libcurl's Memory Tracking feature also performed
  33. * automatic malloc and free filling operations using 0xA5 and 0x13
  34. * values. Our own preinitialization of dynamically allocated memory
  35. * might be useful when not using third party memory debuggers, but
  36. * on the other hand this would fool memory debuggers into thinking
  37. * that all dynamically allocated memory is properly initialized.
  38. *
  39. * As a default setting, libcurl's Memory Tracking feature no longer
  40. * performs preinitialization of dynamically allocated memory on its
  41. * own. If you know what you are doing, and really want to retain old
  42. * behavior, you can achieve this compiling with preprocessor symbols
  43. * CURL_MT_MALLOC_FILL and CURL_MT_FREE_FILL defined with appropriate
  44. * values.
  45. */
  46. #ifdef CURL_MT_MALLOC_FILL
  47. # if (CURL_MT_MALLOC_FILL < 0) || (CURL_MT_MALLOC_FILL > 0xff)
  48. # error "invalid CURL_MT_MALLOC_FILL or out of range"
  49. # endif
  50. #endif
  51. #ifdef CURL_MT_FREE_FILL
  52. # if (CURL_MT_FREE_FILL < 0) || (CURL_MT_FREE_FILL > 0xff)
  53. # error "invalid CURL_MT_FREE_FILL or out of range"
  54. # endif
  55. #endif
  56. #if defined(CURL_MT_MALLOC_FILL) && defined(CURL_MT_FREE_FILL)
  57. # if (CURL_MT_MALLOC_FILL == CURL_MT_FREE_FILL)
  58. # error "CURL_MT_MALLOC_FILL same as CURL_MT_FREE_FILL"
  59. # endif
  60. #endif
  61. #ifdef CURL_MT_MALLOC_FILL
  62. # define mt_malloc_fill(buf,len) memset((buf), CURL_MT_MALLOC_FILL, (len))
  63. #else
  64. # define mt_malloc_fill(buf,len) Curl_nop_stmt
  65. #endif
  66. #ifdef CURL_MT_FREE_FILL
  67. # define mt_free_fill(buf,len) memset((buf), CURL_MT_FREE_FILL, (len))
  68. #else
  69. # define mt_free_fill(buf,len) Curl_nop_stmt
  70. #endif
  71. struct memdebug {
  72. size_t size;
  73. union {
  74. curl_off_t o;
  75. double d;
  76. void *p;
  77. } mem[1];
  78. /* I'm hoping this is the thing with the strictest alignment
  79. * requirements. That also means we waste some space :-( */
  80. };
  81. /*
  82. * Note that these debug functions are very simple and they are meant to
  83. * remain so. For advanced analysis, record a log file and write perl scripts
  84. * to analyze them!
  85. *
  86. * Don't use these with multithreaded test programs!
  87. */
  88. FILE *curl_dbg_logfile = NULL;
  89. static bool memlimit = FALSE; /* enable memory limit */
  90. static long memsize = 0; /* set number of mallocs allowed */
  91. /* this sets the log file name */
  92. void curl_dbg_memdebug(const char *logname)
  93. {
  94. if(!curl_dbg_logfile) {
  95. if(logname && *logname)
  96. curl_dbg_logfile = fopen(logname, FOPEN_WRITETEXT);
  97. else
  98. curl_dbg_logfile = stderr;
  99. #ifdef MEMDEBUG_LOG_SYNC
  100. /* Flush the log file after every line so the log isn't lost in a crash */
  101. if(curl_dbg_logfile)
  102. setbuf(curl_dbg_logfile, (char *)NULL);
  103. #endif
  104. }
  105. }
  106. /* This function sets the number of malloc() calls that should return
  107. successfully! */
  108. void curl_dbg_memlimit(long limit)
  109. {
  110. if(!memlimit) {
  111. memlimit = TRUE;
  112. memsize = limit;
  113. }
  114. }
  115. /* returns TRUE if this isn't allowed! */
  116. static bool countcheck(const char *func, int line, const char *source)
  117. {
  118. /* if source is NULL, then the call is made internally and this check
  119. should not be made */
  120. if(memlimit && source) {
  121. if(!memsize) {
  122. if(source) {
  123. /* log to file */
  124. curl_dbg_log("LIMIT %s:%d %s reached memlimit\n",
  125. source, line, func);
  126. /* log to stderr also */
  127. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  128. source, line, func);
  129. fflush(curl_dbg_logfile); /* because it might crash now */
  130. }
  131. errno = ENOMEM;
  132. return TRUE; /* RETURN ERROR! */
  133. }
  134. else
  135. memsize--; /* countdown */
  136. }
  137. return FALSE; /* allow this */
  138. }
  139. void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
  140. {
  141. struct memdebug *mem;
  142. size_t size;
  143. DEBUGASSERT(wantedsize != 0);
  144. if(countcheck("malloc", line, source))
  145. return NULL;
  146. /* alloc at least 64 bytes */
  147. size = sizeof(struct memdebug) + wantedsize;
  148. mem = (Curl_cmalloc)(size);
  149. if(mem) {
  150. /* fill memory with junk */
  151. mt_malloc_fill(mem->mem, wantedsize);
  152. mem->size = wantedsize;
  153. }
  154. if(source)
  155. curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
  156. source, line, wantedsize,
  157. mem ? (void *)mem->mem : (void *)0);
  158. return (mem ? mem->mem : NULL);
  159. }
  160. void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
  161. int line, const char *source)
  162. {
  163. struct memdebug *mem;
  164. size_t size, user_size;
  165. DEBUGASSERT(wanted_elements != 0);
  166. DEBUGASSERT(wanted_size != 0);
  167. if(countcheck("calloc", line, source))
  168. return NULL;
  169. /* alloc at least 64 bytes */
  170. user_size = wanted_size * wanted_elements;
  171. size = sizeof(struct memdebug) + user_size;
  172. mem = (Curl_ccalloc)(1, size);
  173. if(mem)
  174. mem->size = user_size;
  175. if(source)
  176. curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
  177. source, line, wanted_elements, wanted_size,
  178. mem ? (void *)mem->mem : (void *)0);
  179. return (mem ? mem->mem : NULL);
  180. }
  181. char *curl_dbg_strdup(const char *str, int line, const char *source)
  182. {
  183. char *mem;
  184. size_t len;
  185. DEBUGASSERT(str != NULL);
  186. if(countcheck("strdup", line, source))
  187. return NULL;
  188. len = strlen(str) + 1;
  189. mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
  190. if(mem)
  191. memcpy(mem, str, len);
  192. if(source)
  193. curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
  194. source, line, (const void *)str, len, (const void *)mem);
  195. return mem;
  196. }
  197. #if defined(WIN32) && defined(UNICODE)
  198. wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source)
  199. {
  200. wchar_t *mem;
  201. size_t wsiz, bsiz;
  202. DEBUGASSERT(str != NULL);
  203. if(countcheck("wcsdup", line, source))
  204. return NULL;
  205. wsiz = wcslen(str) + 1;
  206. bsiz = wsiz * sizeof(wchar_t);
  207. mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
  208. if(mem)
  209. memcpy(mem, str, bsiz);
  210. if(source)
  211. curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  212. source, line, (void *)str, bsiz, (void *)mem);
  213. return mem;
  214. }
  215. #endif
  216. /* We provide a realloc() that accepts a NULL as pointer, which then
  217. performs a malloc(). In order to work with ares. */
  218. void *curl_dbg_realloc(void *ptr, size_t wantedsize,
  219. int line, const char *source)
  220. {
  221. struct memdebug *mem = NULL;
  222. size_t size = sizeof(struct memdebug) + wantedsize;
  223. DEBUGASSERT(wantedsize != 0);
  224. if(countcheck("realloc", line, source))
  225. return NULL;
  226. #ifdef __INTEL_COMPILER
  227. # pragma warning(push)
  228. # pragma warning(disable:1684)
  229. /* 1684: conversion from pointer to same-sized integral type */
  230. #endif
  231. if(ptr)
  232. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  233. #ifdef __INTEL_COMPILER
  234. # pragma warning(pop)
  235. #endif
  236. mem = (Curl_crealloc)(mem, size);
  237. if(source)
  238. curl_dbg_log("MEM %s:%d realloc(%p, %zu) = %p\n",
  239. source, line, (void *)ptr, wantedsize,
  240. mem ? (void *)mem->mem : (void *)0);
  241. if(mem) {
  242. mem->size = wantedsize;
  243. return mem->mem;
  244. }
  245. return NULL;
  246. }
  247. void curl_dbg_free(void *ptr, int line, const char *source)
  248. {
  249. if(ptr) {
  250. struct memdebug *mem;
  251. #ifdef __INTEL_COMPILER
  252. # pragma warning(push)
  253. # pragma warning(disable:1684)
  254. /* 1684: conversion from pointer to same-sized integral type */
  255. #endif
  256. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  257. #ifdef __INTEL_COMPILER
  258. # pragma warning(pop)
  259. #endif
  260. /* destroy */
  261. mt_free_fill(mem->mem, mem->size);
  262. /* free for real */
  263. (Curl_cfree)(mem);
  264. }
  265. if(source && ptr)
  266. curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  267. }
  268. curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
  269. int line, const char *source)
  270. {
  271. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  272. "FD %s:%d socket() = %d\n" :
  273. (sizeof(curl_socket_t) == sizeof(long)) ?
  274. "FD %s:%d socket() = %ld\n" :
  275. "FD %s:%d socket() = %zd\n";
  276. curl_socket_t sockfd;
  277. if(countcheck("socket", line, source))
  278. return CURL_SOCKET_BAD;
  279. sockfd = socket(domain, type, protocol);
  280. if(source && (sockfd != CURL_SOCKET_BAD))
  281. curl_dbg_log(fmt, source, line, sockfd);
  282. return sockfd;
  283. }
  284. SEND_TYPE_RETV curl_dbg_send(SEND_TYPE_ARG1 sockfd,
  285. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  286. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  287. const char *source)
  288. {
  289. SEND_TYPE_RETV rc;
  290. if(countcheck("send", line, source))
  291. return -1;
  292. rc = send(sockfd, buf, len, flags);
  293. if(source)
  294. curl_dbg_log("SEND %s:%d send(%lu) = %ld\n",
  295. source, line, (unsigned long)len, (long)rc);
  296. return rc;
  297. }
  298. RECV_TYPE_RETV curl_dbg_recv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  299. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  300. const char *source)
  301. {
  302. RECV_TYPE_RETV rc;
  303. if(countcheck("recv", line, source))
  304. return -1;
  305. rc = recv(sockfd, buf, len, flags);
  306. if(source)
  307. curl_dbg_log("RECV %s:%d recv(%lu) = %ld\n",
  308. source, line, (unsigned long)len, (long)rc);
  309. return rc;
  310. }
  311. #ifdef HAVE_SOCKETPAIR
  312. int curl_dbg_socketpair(int domain, int type, int protocol,
  313. curl_socket_t socket_vector[2],
  314. int line, const char *source)
  315. {
  316. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  317. "FD %s:%d socketpair() = %d %d\n" :
  318. (sizeof(curl_socket_t) == sizeof(long)) ?
  319. "FD %s:%d socketpair() = %ld %ld\n" :
  320. "FD %s:%d socketpair() = %zd %zd\n";
  321. int res = socketpair(domain, type, protocol, socket_vector);
  322. if(source && (0 == res))
  323. curl_dbg_log(fmt, source, line, socket_vector[0], socket_vector[1]);
  324. return res;
  325. }
  326. #endif
  327. curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
  328. int line, const char *source)
  329. {
  330. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  331. "FD %s:%d accept() = %d\n" :
  332. (sizeof(curl_socket_t) == sizeof(long)) ?
  333. "FD %s:%d accept() = %ld\n" :
  334. "FD %s:%d accept() = %zd\n";
  335. struct sockaddr *addr = (struct sockaddr *)saddr;
  336. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  337. curl_socket_t sockfd = accept(s, addr, addrlen);
  338. if(source && (sockfd != CURL_SOCKET_BAD))
  339. curl_dbg_log(fmt, source, line, sockfd);
  340. return sockfd;
  341. }
  342. /* separate function to allow libcurl to mark a "faked" close */
  343. void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  344. {
  345. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  346. "FD %s:%d sclose(%d)\n":
  347. (sizeof(curl_socket_t) == sizeof(long)) ?
  348. "FD %s:%d sclose(%ld)\n":
  349. "FD %s:%d sclose(%zd)\n";
  350. if(source)
  351. curl_dbg_log(fmt, source, line, sockfd);
  352. }
  353. /* this is our own defined way to close sockets on *ALL* platforms */
  354. int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
  355. {
  356. int res = sclose(sockfd);
  357. curl_dbg_mark_sclose(sockfd, line, source);
  358. return res;
  359. }
  360. FILE *curl_dbg_fopen(const char *file, const char *mode,
  361. int line, const char *source)
  362. {
  363. FILE *res = fopen(file, mode);
  364. if(source)
  365. curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  366. source, line, file, mode, (void *)res);
  367. return res;
  368. }
  369. int curl_dbg_fclose(FILE *file, int line, const char *source)
  370. {
  371. int res;
  372. DEBUGASSERT(file != NULL);
  373. if(source)
  374. curl_dbg_log("FILE %s:%d fclose(%p)\n",
  375. source, line, (void *)file);
  376. res = fclose(file);
  377. return res;
  378. }
  379. #define LOGLINE_BUFSIZE 1024
  380. /* this does the writing to the memory tracking log file */
  381. void curl_dbg_log(const char *format, ...)
  382. {
  383. char *buf;
  384. int nchars;
  385. va_list ap;
  386. if(!curl_dbg_logfile)
  387. return;
  388. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  389. if(!buf)
  390. return;
  391. va_start(ap, format);
  392. nchars = mvsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  393. va_end(ap);
  394. if(nchars > LOGLINE_BUFSIZE - 1)
  395. nchars = LOGLINE_BUFSIZE - 1;
  396. if(nchars > 0)
  397. fwrite(buf, 1, (size_t)nchars, curl_dbg_logfile);
  398. (Curl_cfree)(buf);
  399. }
  400. #endif /* CURLDEBUG */