memdebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. #define logfile curl_debuglogfile
  89. FILE *curl_debuglogfile = NULL;
  90. static bool memlimit = FALSE; /* enable memory limit */
  91. static long memsize = 0; /* set number of mallocs allowed */
  92. /* this sets the log file name */
  93. void curl_memdebug(const char *logname)
  94. {
  95. if(!logfile) {
  96. if(logname && *logname)
  97. logfile = fopen(logname, FOPEN_WRITETEXT);
  98. else
  99. logfile = stderr;
  100. #ifdef MEMDEBUG_LOG_SYNC
  101. /* Flush the log file after every line so the log isn't lost in a crash */
  102. if(logfile)
  103. setbuf(logfile, (char *)NULL);
  104. #endif
  105. }
  106. }
  107. /* This function sets the number of malloc() calls that should return
  108. successfully! */
  109. void curl_memlimit(long limit)
  110. {
  111. if(!memlimit) {
  112. memlimit = TRUE;
  113. memsize = limit;
  114. }
  115. }
  116. /* returns TRUE if this isn't allowed! */
  117. static bool countcheck(const char *func, int line, const char *source)
  118. {
  119. /* if source is NULL, then the call is made internally and this check
  120. should not be made */
  121. if(memlimit && source) {
  122. if(!memsize) {
  123. if(source) {
  124. /* log to file */
  125. curl_memlog("LIMIT %s:%d %s reached memlimit\n",
  126. source, line, func);
  127. /* log to stderr also */
  128. fprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
  129. source, line, func);
  130. fflush(logfile); /* because it might crash now */
  131. }
  132. errno = ENOMEM;
  133. return TRUE; /* RETURN ERROR! */
  134. }
  135. else
  136. memsize--; /* countdown */
  137. }
  138. return FALSE; /* allow this */
  139. }
  140. void *curl_domalloc(size_t wantedsize, int line, const char *source)
  141. {
  142. struct memdebug *mem;
  143. size_t size;
  144. DEBUGASSERT(wantedsize != 0);
  145. if(countcheck("malloc", line, source))
  146. return NULL;
  147. /* alloc at least 64 bytes */
  148. size = sizeof(struct memdebug) + wantedsize;
  149. mem = (Curl_cmalloc)(size);
  150. if(mem) {
  151. /* fill memory with junk */
  152. mt_malloc_fill(mem->mem, wantedsize);
  153. mem->size = wantedsize;
  154. }
  155. if(source)
  156. curl_memlog("MEM %s:%d malloc(%zu) = %p\n",
  157. source, line, wantedsize,
  158. mem ? (void *)mem->mem : (void *)0);
  159. return (mem ? mem->mem : NULL);
  160. }
  161. void *curl_docalloc(size_t wanted_elements, size_t wanted_size,
  162. int line, const char *source)
  163. {
  164. struct memdebug *mem;
  165. size_t size, user_size;
  166. DEBUGASSERT(wanted_elements != 0);
  167. DEBUGASSERT(wanted_size != 0);
  168. if(countcheck("calloc", line, source))
  169. return NULL;
  170. /* alloc at least 64 bytes */
  171. user_size = wanted_size * wanted_elements;
  172. size = sizeof(struct memdebug) + user_size;
  173. mem = (Curl_ccalloc)(1, size);
  174. if(mem)
  175. mem->size = user_size;
  176. if(source)
  177. curl_memlog("MEM %s:%d calloc(%zu,%zu) = %p\n",
  178. source, line, wanted_elements, wanted_size,
  179. mem ? (void *)mem->mem : (void *)0);
  180. return (mem ? mem->mem : NULL);
  181. }
  182. char *curl_dostrdup(const char *str, int line, const char *source)
  183. {
  184. char *mem;
  185. size_t len;
  186. DEBUGASSERT(str != NULL);
  187. if(countcheck("strdup", line, source))
  188. return NULL;
  189. len = strlen(str) + 1;
  190. mem = curl_domalloc(len, 0, NULL); /* NULL prevents logging */
  191. if(mem)
  192. memcpy(mem, str, len);
  193. if(source)
  194. curl_memlog("MEM %s:%d strdup(%p) (%zu) = %p\n",
  195. source, line, (const void *)str, len, (const void *)mem);
  196. return mem;
  197. }
  198. #if defined(WIN32) && defined(UNICODE)
  199. wchar_t *curl_dowcsdup(const wchar_t *str, int line, const char *source)
  200. {
  201. wchar_t *mem;
  202. size_t wsiz, bsiz;
  203. DEBUGASSERT(str != NULL);
  204. if(countcheck("wcsdup", line, source))
  205. return NULL;
  206. wsiz = wcslen(str) + 1;
  207. bsiz = wsiz * sizeof(wchar_t);
  208. mem = curl_domalloc(bsiz, 0, NULL); /* NULL prevents logging */
  209. if(mem)
  210. memcpy(mem, str, bsiz);
  211. if(source)
  212. curl_memlog("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
  213. source, line, (void *)str, bsiz, (void *)mem);
  214. return mem;
  215. }
  216. #endif
  217. /* We provide a realloc() that accepts a NULL as pointer, which then
  218. performs a malloc(). In order to work with ares. */
  219. void *curl_dorealloc(void *ptr, size_t wantedsize,
  220. int line, const char *source)
  221. {
  222. struct memdebug *mem = NULL;
  223. size_t size = sizeof(struct memdebug) + wantedsize;
  224. DEBUGASSERT(wantedsize != 0);
  225. if(countcheck("realloc", line, source))
  226. return NULL;
  227. #ifdef __INTEL_COMPILER
  228. # pragma warning(push)
  229. # pragma warning(disable:1684)
  230. /* 1684: conversion from pointer to same-sized integral type */
  231. #endif
  232. if(ptr)
  233. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  234. #ifdef __INTEL_COMPILER
  235. # pragma warning(pop)
  236. #endif
  237. mem = (Curl_crealloc)(mem, size);
  238. if(source)
  239. curl_memlog("MEM %s:%d realloc(%p, %zu) = %p\n",
  240. source, line, (void *)ptr, wantedsize,
  241. mem ? (void *)mem->mem : (void *)0);
  242. if(mem) {
  243. mem->size = wantedsize;
  244. return mem->mem;
  245. }
  246. return NULL;
  247. }
  248. void curl_dofree(void *ptr, int line, const char *source)
  249. {
  250. struct memdebug *mem;
  251. if(ptr) {
  252. #ifdef __INTEL_COMPILER
  253. # pragma warning(push)
  254. # pragma warning(disable:1684)
  255. /* 1684: conversion from pointer to same-sized integral type */
  256. #endif
  257. mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
  258. #ifdef __INTEL_COMPILER
  259. # pragma warning(pop)
  260. #endif
  261. /* destroy */
  262. mt_free_fill(mem->mem, mem->size);
  263. /* free for real */
  264. (Curl_cfree)(mem);
  265. }
  266. if(source)
  267. curl_memlog("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
  268. }
  269. curl_socket_t curl_socket(int domain, int type, int protocol,
  270. int line, const char *source)
  271. {
  272. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  273. "FD %s:%d socket() = %d\n" :
  274. (sizeof(curl_socket_t) == sizeof(long)) ?
  275. "FD %s:%d socket() = %ld\n" :
  276. "FD %s:%d socket() = %zd\n";
  277. curl_socket_t sockfd;
  278. if(countcheck("socket", line, source))
  279. return CURL_SOCKET_BAD;
  280. sockfd = socket(domain, type, protocol);
  281. if(source && (sockfd != CURL_SOCKET_BAD))
  282. curl_memlog(fmt, source, line, sockfd);
  283. return sockfd;
  284. }
  285. SEND_TYPE_RETV curl_dosend(SEND_TYPE_ARG1 sockfd,
  286. SEND_QUAL_ARG2 SEND_TYPE_ARG2 buf,
  287. SEND_TYPE_ARG3 len, SEND_TYPE_ARG4 flags, int line,
  288. const char *source)
  289. {
  290. SEND_TYPE_RETV rc;
  291. if(countcheck("send", line, source))
  292. return -1;
  293. rc = send(sockfd, buf, len, flags);
  294. if(source)
  295. curl_memlog("SEND %s:%d send(%lu) = %ld\n",
  296. source, line, (unsigned long)len, (long)rc);
  297. return rc;
  298. }
  299. RECV_TYPE_RETV curl_dorecv(RECV_TYPE_ARG1 sockfd, RECV_TYPE_ARG2 buf,
  300. RECV_TYPE_ARG3 len, RECV_TYPE_ARG4 flags, int line,
  301. const char *source)
  302. {
  303. RECV_TYPE_RETV rc;
  304. if(countcheck("recv", line, source))
  305. return -1;
  306. rc = recv(sockfd, buf, len, flags);
  307. if(source)
  308. curl_memlog("RECV %s:%d recv(%lu) = %ld\n",
  309. source, line, (unsigned long)len, (long)rc);
  310. return rc;
  311. }
  312. #ifdef HAVE_SOCKETPAIR
  313. int curl_socketpair(int domain, int type, int protocol,
  314. curl_socket_t socket_vector[2],
  315. int line, const char *source)
  316. {
  317. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  318. "FD %s:%d socketpair() = %d %d\n" :
  319. (sizeof(curl_socket_t) == sizeof(long)) ?
  320. "FD %s:%d socketpair() = %ld %ld\n" :
  321. "FD %s:%d socketpair() = %zd %zd\n";
  322. int res = socketpair(domain, type, protocol, socket_vector);
  323. if(source && (0 == res))
  324. curl_memlog(fmt, source, line, socket_vector[0], socket_vector[1]);
  325. return res;
  326. }
  327. #endif
  328. curl_socket_t curl_accept(curl_socket_t s, void *saddr, void *saddrlen,
  329. int line, const char *source)
  330. {
  331. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  332. "FD %s:%d accept() = %d\n" :
  333. (sizeof(curl_socket_t) == sizeof(long)) ?
  334. "FD %s:%d accept() = %ld\n" :
  335. "FD %s:%d accept() = %zd\n";
  336. struct sockaddr *addr = (struct sockaddr *)saddr;
  337. curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
  338. curl_socket_t sockfd = accept(s, addr, addrlen);
  339. if(source && (sockfd != CURL_SOCKET_BAD))
  340. curl_memlog(fmt, source, line, sockfd);
  341. return sockfd;
  342. }
  343. /* separate function to allow libcurl to mark a "faked" close */
  344. void curl_mark_sclose(curl_socket_t sockfd, int line, const char *source)
  345. {
  346. const char *fmt = (sizeof(curl_socket_t) == sizeof(int)) ?
  347. "FD %s:%d sclose(%d)\n":
  348. (sizeof(curl_socket_t) == sizeof(long)) ?
  349. "FD %s:%d sclose(%ld)\n":
  350. "FD %s:%d sclose(%zd)\n";
  351. if(source)
  352. curl_memlog(fmt, source, line, sockfd);
  353. }
  354. /* this is our own defined way to close sockets on *ALL* platforms */
  355. int curl_sclose(curl_socket_t sockfd, int line, const char *source)
  356. {
  357. int res = sclose(sockfd);
  358. curl_mark_sclose(sockfd, line, source);
  359. return res;
  360. }
  361. FILE *curl_fopen(const char *file, const char *mode,
  362. int line, const char *source)
  363. {
  364. FILE *res = fopen(file, mode);
  365. if(source)
  366. curl_memlog("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
  367. source, line, file, mode, (void *)res);
  368. return res;
  369. }
  370. #ifdef HAVE_FDOPEN
  371. FILE *curl_fdopen(int filedes, const char *mode,
  372. int line, const char *source)
  373. {
  374. FILE *res = fdopen(filedes, mode);
  375. if(source)
  376. curl_memlog("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
  377. source, line, filedes, mode, (void *)res);
  378. return res;
  379. }
  380. #endif
  381. int curl_fclose(FILE *file, int line, const char *source)
  382. {
  383. int res;
  384. DEBUGASSERT(file != NULL);
  385. res = fclose(file);
  386. if(source)
  387. curl_memlog("FILE %s:%d fclose(%p)\n",
  388. source, line, (void *)file);
  389. return res;
  390. }
  391. #define LOGLINE_BUFSIZE 1024
  392. /* this does the writing to the memory tracking log file */
  393. void curl_memlog(const char *format, ...)
  394. {
  395. char *buf;
  396. int nchars;
  397. va_list ap;
  398. if(!logfile)
  399. return;
  400. buf = (Curl_cmalloc)(LOGLINE_BUFSIZE);
  401. if(!buf)
  402. return;
  403. va_start(ap, format);
  404. nchars = vsnprintf(buf, LOGLINE_BUFSIZE, format, ap);
  405. va_end(ap);
  406. if(nchars > LOGLINE_BUFSIZE - 1)
  407. nchars = LOGLINE_BUFSIZE - 1;
  408. if(nchars > 0)
  409. fwrite(buf, 1, (size_t)nchars, logfile);
  410. (Curl_cfree)(buf);
  411. }
  412. #endif /* CURLDEBUG */