mem_dbg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/thread_once.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/buffer.h>
  16. #include "internal/bio.h"
  17. #include <openssl/lhash.h>
  18. #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  19. # include <execinfo.h>
  20. #endif
  21. /*
  22. * The state changes to CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE when
  23. * the application asks for it (usually after library initialisation for
  24. * which no book-keeping is desired). State CRYPTO_MEM_CHECK_ON exists only
  25. * temporarily when the library thinks that certain allocations should not be
  26. * checked (e.g. the data structures used for memory checking). It is not
  27. * suitable as an initial state: the library will unexpectedly enable memory
  28. * checking when it executes one of those sections that want to disable
  29. * checking temporarily. State CRYPTO_MEM_CHECK_ENABLE without ..._ON makes
  30. * no sense whatsoever.
  31. */
  32. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  33. static int mh_mode = CRYPTO_MEM_CHECK_OFF;
  34. #endif
  35. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  36. static unsigned long order = 0; /* number of memory requests */
  37. static CRYPTO_ONCE memdbg_init = CRYPTO_ONCE_STATIC_INIT;
  38. CRYPTO_RWLOCK *memdbg_lock;
  39. static CRYPTO_RWLOCK *long_memdbg_lock;
  40. /* memory-block description */
  41. struct mem_st {
  42. void *addr;
  43. int num;
  44. const char *file;
  45. int line;
  46. CRYPTO_THREAD_ID threadid;
  47. unsigned long order;
  48. time_t time;
  49. #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  50. void *array[30];
  51. size_t array_siz;
  52. #endif
  53. };
  54. /*
  55. * hash-table of memory requests (address as * key); access requires
  56. * long_memdbg_lock lock
  57. */
  58. static LHASH_OF(MEM) *mh = NULL;
  59. /* num_disable > 0 iff mh_mode == CRYPTO_MEM_CHECK_ON (w/o ..._ENABLE) */
  60. static unsigned int num_disable = 0;
  61. /*
  62. * Valid iff num_disable > 0. long_memdbg_lock is locked exactly in this
  63. * case (by the thread named in disabling_thread).
  64. */
  65. static CRYPTO_THREAD_ID disabling_threadid;
  66. DEFINE_RUN_ONCE_STATIC(do_memdbg_init)
  67. {
  68. memdbg_lock = CRYPTO_THREAD_lock_new();
  69. long_memdbg_lock = CRYPTO_THREAD_lock_new();
  70. if (memdbg_lock == NULL || long_memdbg_lock == NULL) {
  71. CRYPTO_THREAD_lock_free(memdbg_lock);
  72. memdbg_lock = NULL;
  73. CRYPTO_THREAD_lock_free(long_memdbg_lock);
  74. long_memdbg_lock = NULL;
  75. return 0;
  76. }
  77. return 1;
  78. }
  79. #endif
  80. int CRYPTO_mem_ctrl(int mode)
  81. {
  82. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  83. return mode - mode;
  84. #else
  85. int ret = mh_mode;
  86. if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
  87. return -1;
  88. CRYPTO_THREAD_write_lock(memdbg_lock);
  89. switch (mode) {
  90. default:
  91. break;
  92. case CRYPTO_MEM_CHECK_ON:
  93. mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
  94. num_disable = 0;
  95. break;
  96. case CRYPTO_MEM_CHECK_OFF:
  97. mh_mode = 0;
  98. num_disable = 0;
  99. break;
  100. /* switch off temporarily (for library-internal use): */
  101. case CRYPTO_MEM_CHECK_DISABLE:
  102. if (mh_mode & CRYPTO_MEM_CHECK_ON) {
  103. CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
  104. /* see if we don't have long_memdbg_lock already */
  105. if (!num_disable
  106. || !CRYPTO_THREAD_compare_id(disabling_threadid, cur)) {
  107. /*
  108. * Long-time lock long_memdbg_lock must not be claimed
  109. * while we're holding memdbg_lock, or we'll deadlock
  110. * if somebody else holds long_memdbg_lock (and cannot
  111. * release it because we block entry to this function). Give
  112. * them a chance, first, and then claim the locks in
  113. * appropriate order (long-time lock first).
  114. */
  115. CRYPTO_THREAD_unlock(memdbg_lock);
  116. /*
  117. * Note that after we have waited for long_memdbg_lock and
  118. * memdbg_lock, we'll still be in the right "case" and
  119. * "if" branch because MemCheck_start and MemCheck_stop may
  120. * never be used while there are multiple OpenSSL threads.
  121. */
  122. CRYPTO_THREAD_write_lock(long_memdbg_lock);
  123. CRYPTO_THREAD_write_lock(memdbg_lock);
  124. mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
  125. disabling_threadid = cur;
  126. }
  127. num_disable++;
  128. }
  129. break;
  130. case CRYPTO_MEM_CHECK_ENABLE:
  131. if (mh_mode & CRYPTO_MEM_CHECK_ON) {
  132. if (num_disable) { /* always true, or something is going wrong */
  133. num_disable--;
  134. if (num_disable == 0) {
  135. mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
  136. CRYPTO_THREAD_unlock(long_memdbg_lock);
  137. }
  138. }
  139. }
  140. break;
  141. }
  142. CRYPTO_THREAD_unlock(memdbg_lock);
  143. return ret;
  144. #endif
  145. }
  146. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  147. static int mem_check_on(void)
  148. {
  149. int ret = 0;
  150. CRYPTO_THREAD_ID cur;
  151. if (mh_mode & CRYPTO_MEM_CHECK_ON) {
  152. if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
  153. return 0;
  154. cur = CRYPTO_THREAD_get_current_id();
  155. CRYPTO_THREAD_read_lock(memdbg_lock);
  156. ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
  157. || !CRYPTO_THREAD_compare_id(disabling_threadid, cur);
  158. CRYPTO_THREAD_unlock(memdbg_lock);
  159. }
  160. return ret;
  161. }
  162. static int mem_cmp(const MEM *a, const MEM *b)
  163. {
  164. #ifdef _WIN64
  165. const char *ap = (const char *)a->addr, *bp = (const char *)b->addr;
  166. if (ap == bp)
  167. return 0;
  168. else if (ap > bp)
  169. return 1;
  170. else
  171. return -1;
  172. #else
  173. return (const char *)a->addr - (const char *)b->addr;
  174. #endif
  175. }
  176. static unsigned long mem_hash(const MEM *a)
  177. {
  178. size_t ret;
  179. ret = (size_t)a->addr;
  180. ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
  181. return ret;
  182. }
  183. #if !OPENSSL_API_3
  184. int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
  185. {
  186. return 0;
  187. }
  188. int CRYPTO_mem_debug_pop(void)
  189. {
  190. return 0;
  191. }
  192. #endif
  193. static unsigned long break_order_num = 0;
  194. void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
  195. const char *file, int line)
  196. {
  197. MEM *m, *mm;
  198. switch (before_p & 127) {
  199. case 0:
  200. break;
  201. case 1:
  202. if (addr == NULL)
  203. break;
  204. if (mem_check_on()) {
  205. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  206. if (!RUN_ONCE(&memdbg_init, do_memdbg_init)
  207. || (m = OPENSSL_malloc(sizeof(*m))) == NULL) {
  208. OPENSSL_free(addr);
  209. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  210. return;
  211. }
  212. if (mh == NULL) {
  213. if ((mh = lh_MEM_new(mem_hash, mem_cmp)) == NULL) {
  214. OPENSSL_free(addr);
  215. OPENSSL_free(m);
  216. addr = NULL;
  217. goto err;
  218. }
  219. }
  220. m->addr = addr;
  221. m->file = file;
  222. m->line = line;
  223. m->num = num;
  224. m->threadid = CRYPTO_THREAD_get_current_id();
  225. if (order == break_order_num) {
  226. /* BREAK HERE */
  227. m->order = order;
  228. }
  229. m->order = order++;
  230. # ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  231. m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
  232. # endif
  233. m->time = time(NULL);
  234. if ((mm = lh_MEM_insert(mh, m)) != NULL)
  235. OPENSSL_free(mm);
  236. err:
  237. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  238. }
  239. break;
  240. }
  241. return;
  242. }
  243. void CRYPTO_mem_debug_free(void *addr, int before_p,
  244. const char *file, int line)
  245. {
  246. MEM m, *mp;
  247. switch (before_p) {
  248. case 0:
  249. if (addr == NULL)
  250. break;
  251. if (mem_check_on() && (mh != NULL)) {
  252. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  253. m.addr = addr;
  254. mp = lh_MEM_delete(mh, &m);
  255. OPENSSL_free(mp);
  256. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  257. }
  258. break;
  259. case 1:
  260. break;
  261. }
  262. }
  263. void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
  264. int before_p, const char *file, int line)
  265. {
  266. MEM m, *mp;
  267. switch (before_p) {
  268. case 0:
  269. break;
  270. case 1:
  271. if (addr2 == NULL)
  272. break;
  273. if (addr1 == NULL) {
  274. CRYPTO_mem_debug_malloc(addr2, num, 128 | before_p, file, line);
  275. break;
  276. }
  277. if (mem_check_on()) {
  278. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  279. m.addr = addr1;
  280. mp = lh_MEM_delete(mh, &m);
  281. if (mp != NULL) {
  282. mp->addr = addr2;
  283. mp->num = num;
  284. #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  285. mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
  286. #endif
  287. (void)lh_MEM_insert(mh, mp);
  288. }
  289. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  290. }
  291. break;
  292. }
  293. return;
  294. }
  295. typedef struct mem_leak_st {
  296. int (*print_cb) (const char *str, size_t len, void *u);
  297. void *print_cb_arg;
  298. int chunks;
  299. long bytes;
  300. } MEM_LEAK;
  301. static void print_leak(const MEM *m, MEM_LEAK *l)
  302. {
  303. char buf[1024];
  304. char *bufp = buf, *hex;
  305. size_t len = sizeof(buf);
  306. int n;
  307. struct tm *lcl = NULL;
  308. lcl = localtime(&m->time);
  309. n = BIO_snprintf(bufp, len, "[%02d:%02d:%02d] ",
  310. lcl->tm_hour, lcl->tm_min, lcl->tm_sec);
  311. if (n <= 0) {
  312. bufp[0] = '\0';
  313. return;
  314. }
  315. bufp += n;
  316. len -= n;
  317. n = BIO_snprintf(bufp, len, "%5lu file=%s, line=%d, ",
  318. m->order, m->file, m->line);
  319. if (n <= 0)
  320. return;
  321. bufp += n;
  322. len -= n;
  323. hex = OPENSSL_buf2hexstr((const unsigned char *)&m->threadid,
  324. sizeof(m->threadid));
  325. n = BIO_snprintf(bufp, len, "thread=%s, number=%d, address=%p\n", hex,
  326. m->num, m->addr);
  327. OPENSSL_free(hex);
  328. if (n <= 0)
  329. return;
  330. bufp += n;
  331. len -= n;
  332. l->print_cb(buf, (size_t)(bufp - buf), l->print_cb_arg);
  333. l->chunks++;
  334. l->bytes += m->num;
  335. #ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
  336. {
  337. size_t i;
  338. char **strings = backtrace_symbols(m->array, m->array_siz);
  339. for (i = 0; i < m->array_siz; i++)
  340. fprintf(stderr, "##> %s\n", strings[i]);
  341. free(strings);
  342. }
  343. #endif
  344. }
  345. IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM, MEM_LEAK);
  346. int CRYPTO_mem_leaks_cb(int (*cb) (const char *str, size_t len, void *u),
  347. void *u)
  348. {
  349. MEM_LEAK ml;
  350. /* Ensure all resources are released */
  351. OPENSSL_cleanup();
  352. if (!RUN_ONCE(&memdbg_init, do_memdbg_init))
  353. return -1;
  354. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  355. ml.print_cb = cb;
  356. ml.print_cb_arg = u;
  357. ml.bytes = 0;
  358. ml.chunks = 0;
  359. if (mh != NULL)
  360. lh_MEM_doall_MEM_LEAK(mh, print_leak, &ml);
  361. if (ml.chunks != 0) {
  362. char buf[256];
  363. BIO_snprintf(buf, sizeof(buf), "%ld bytes leaked in %d chunks\n",
  364. ml.bytes, ml.chunks);
  365. cb(buf, strlen(buf), u);
  366. } else {
  367. /*
  368. * Make sure that, if we found no leaks, memory-leak debugging itself
  369. * does not introduce memory leaks (which might irritate external
  370. * debugging tools). (When someone enables leak checking, but does not
  371. * call this function, we declare it to be their fault.)
  372. */
  373. int old_mh_mode;
  374. CRYPTO_THREAD_write_lock(memdbg_lock);
  375. /*
  376. * avoid deadlock when lh_free() uses CRYPTO_mem_debug_free(), which uses
  377. * mem_check_on
  378. */
  379. old_mh_mode = mh_mode;
  380. mh_mode = CRYPTO_MEM_CHECK_OFF;
  381. lh_MEM_free(mh);
  382. mh = NULL;
  383. mh_mode = old_mh_mode;
  384. CRYPTO_THREAD_unlock(memdbg_lock);
  385. }
  386. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF);
  387. /* Clean up locks etc */
  388. CRYPTO_THREAD_lock_free(memdbg_lock);
  389. CRYPTO_THREAD_lock_free(long_memdbg_lock);
  390. memdbg_lock = NULL;
  391. long_memdbg_lock = NULL;
  392. return ml.chunks == 0 ? 1 : 0;
  393. }
  394. static int print_bio(const char *str, size_t len, void *b)
  395. {
  396. return BIO_write((BIO *)b, str, len);
  397. }
  398. int CRYPTO_mem_leaks(BIO *b)
  399. {
  400. /*
  401. * OPENSSL_cleanup() will free the ex_data locks so we can't have any
  402. * ex_data hanging around
  403. */
  404. bio_free_ex_data(b);
  405. return CRYPTO_mem_leaks_cb(print_bio, b);
  406. }
  407. # ifndef OPENSSL_NO_STDIO
  408. int CRYPTO_mem_leaks_fp(FILE *fp)
  409. {
  410. BIO *b;
  411. int ret;
  412. /*
  413. * Need to turn off memory checking when allocated BIOs ... especially as
  414. * we're creating them at a time when we're trying to check we've not
  415. * left anything un-free()'d!!
  416. */
  417. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  418. b = BIO_new(BIO_s_file());
  419. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  420. if (b == NULL)
  421. return -1;
  422. BIO_set_fp(b, fp, BIO_NOCLOSE);
  423. ret = CRYPTO_mem_leaks_cb(print_bio, b);
  424. BIO_free(b);
  425. return ret;
  426. }
  427. # endif
  428. #endif