mem_sec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * This file is in two halves. The first half implements the public API
  12. * to be used by external consumers, and to be used by OpenSSL to store
  13. * data in a "secure arena." The second half implements the secure arena.
  14. * For details on that implementation, see below (look for uppercase
  15. * "SECURE HEAP IMPLEMENTATION").
  16. */
  17. #include "e_os.h"
  18. #include <openssl/crypto.h>
  19. #include <string.h>
  20. /* e_os.h includes unistd.h, which defines _POSIX_VERSION */
  21. #if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
  22. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  23. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  24. || defined(__osf__) )
  25. # define IMPLEMENTED
  26. # include <stdlib.h>
  27. # include <assert.h>
  28. # include <unistd.h>
  29. # include <sys/types.h>
  30. # include <sys/mman.h>
  31. # if defined(OPENSSL_SYS_LINUX)
  32. # include <sys/syscall.h>
  33. # if defined(SYS_mlock2)
  34. # include <linux/mman.h>
  35. # include <errno.h>
  36. # endif
  37. # endif
  38. # include <sys/param.h>
  39. # include <sys/stat.h>
  40. # include <fcntl.h>
  41. #endif
  42. #define CLEAR(p, s) OPENSSL_cleanse(p, s)
  43. #ifndef PAGE_SIZE
  44. # define PAGE_SIZE 4096
  45. #endif
  46. #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
  47. # define MAP_ANON MAP_ANONYMOUS
  48. #endif
  49. #ifdef IMPLEMENTED
  50. static size_t secure_mem_used;
  51. static int secure_mem_initialized;
  52. static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
  53. /*
  54. * These are the functions that must be implemented by a secure heap (sh).
  55. */
  56. static int sh_init(size_t size, int minsize);
  57. static void *sh_malloc(size_t size);
  58. static void sh_free(void *ptr);
  59. static void sh_done(void);
  60. static size_t sh_actual_size(char *ptr);
  61. static int sh_allocated(const char *ptr);
  62. #endif
  63. int CRYPTO_secure_malloc_init(size_t size, int minsize)
  64. {
  65. #ifdef IMPLEMENTED
  66. int ret = 0;
  67. if (!secure_mem_initialized) {
  68. sec_malloc_lock = CRYPTO_THREAD_lock_new();
  69. if (sec_malloc_lock == NULL)
  70. return 0;
  71. if ((ret = sh_init(size, minsize)) != 0) {
  72. secure_mem_initialized = 1;
  73. } else {
  74. CRYPTO_THREAD_lock_free(sec_malloc_lock);
  75. sec_malloc_lock = NULL;
  76. }
  77. }
  78. return ret;
  79. #else
  80. return 0;
  81. #endif /* IMPLEMENTED */
  82. }
  83. int CRYPTO_secure_malloc_done()
  84. {
  85. #ifdef IMPLEMENTED
  86. if (secure_mem_used == 0) {
  87. sh_done();
  88. secure_mem_initialized = 0;
  89. CRYPTO_THREAD_lock_free(sec_malloc_lock);
  90. sec_malloc_lock = NULL;
  91. return 1;
  92. }
  93. #endif /* IMPLEMENTED */
  94. return 0;
  95. }
  96. int CRYPTO_secure_malloc_initialized()
  97. {
  98. #ifdef IMPLEMENTED
  99. return secure_mem_initialized;
  100. #else
  101. return 0;
  102. #endif /* IMPLEMENTED */
  103. }
  104. void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
  105. {
  106. #ifdef IMPLEMENTED
  107. void *ret;
  108. size_t actual_size;
  109. if (!secure_mem_initialized) {
  110. return CRYPTO_malloc(num, file, line);
  111. }
  112. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  113. ret = sh_malloc(num);
  114. actual_size = ret ? sh_actual_size(ret) : 0;
  115. secure_mem_used += actual_size;
  116. CRYPTO_THREAD_unlock(sec_malloc_lock);
  117. return ret;
  118. #else
  119. return CRYPTO_malloc(num, file, line);
  120. #endif /* IMPLEMENTED */
  121. }
  122. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
  123. {
  124. void *ret = CRYPTO_secure_malloc(num, file, line);
  125. if (ret != NULL)
  126. memset(ret, 0, num);
  127. return ret;
  128. }
  129. void CRYPTO_secure_free(void *ptr, const char *file, int line)
  130. {
  131. #ifdef IMPLEMENTED
  132. size_t actual_size;
  133. if (ptr == NULL)
  134. return;
  135. if (!CRYPTO_secure_allocated(ptr)) {
  136. CRYPTO_free(ptr, file, line);
  137. return;
  138. }
  139. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  140. actual_size = sh_actual_size(ptr);
  141. CLEAR(ptr, actual_size);
  142. secure_mem_used -= actual_size;
  143. sh_free(ptr);
  144. CRYPTO_THREAD_unlock(sec_malloc_lock);
  145. #else
  146. CRYPTO_free(ptr, file, line);
  147. #endif /* IMPLEMENTED */
  148. }
  149. void CRYPTO_secure_clear_free(void *ptr, size_t num,
  150. const char *file, int line)
  151. {
  152. #ifdef IMPLEMENTED
  153. size_t actual_size;
  154. if (ptr == NULL)
  155. return;
  156. if (!CRYPTO_secure_allocated(ptr)) {
  157. OPENSSL_cleanse(ptr, num);
  158. CRYPTO_free(ptr, file, line);
  159. return;
  160. }
  161. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  162. actual_size = sh_actual_size(ptr);
  163. CLEAR(ptr, actual_size);
  164. secure_mem_used -= actual_size;
  165. sh_free(ptr);
  166. CRYPTO_THREAD_unlock(sec_malloc_lock);
  167. #else
  168. if (ptr == NULL)
  169. return;
  170. OPENSSL_cleanse(ptr, num);
  171. CRYPTO_free(ptr, file, line);
  172. #endif /* IMPLEMENTED */
  173. }
  174. int CRYPTO_secure_allocated(const void *ptr)
  175. {
  176. #ifdef IMPLEMENTED
  177. int ret;
  178. if (!secure_mem_initialized)
  179. return 0;
  180. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  181. ret = sh_allocated(ptr);
  182. CRYPTO_THREAD_unlock(sec_malloc_lock);
  183. return ret;
  184. #else
  185. return 0;
  186. #endif /* IMPLEMENTED */
  187. }
  188. size_t CRYPTO_secure_used()
  189. {
  190. #ifdef IMPLEMENTED
  191. return secure_mem_used;
  192. #else
  193. return 0;
  194. #endif /* IMPLEMENTED */
  195. }
  196. size_t CRYPTO_secure_actual_size(void *ptr)
  197. {
  198. #ifdef IMPLEMENTED
  199. size_t actual_size;
  200. CRYPTO_THREAD_write_lock(sec_malloc_lock);
  201. actual_size = sh_actual_size(ptr);
  202. CRYPTO_THREAD_unlock(sec_malloc_lock);
  203. return actual_size;
  204. #else
  205. return 0;
  206. #endif
  207. }
  208. /* END OF PAGE ...
  209. ... START OF PAGE */
  210. /*
  211. * SECURE HEAP IMPLEMENTATION
  212. */
  213. #ifdef IMPLEMENTED
  214. /*
  215. * The implementation provided here uses a fixed-sized mmap() heap,
  216. * which is locked into memory, not written to core files, and protected
  217. * on either side by an unmapped page, which will catch pointer overruns
  218. * (or underruns) and an attempt to read data out of the secure heap.
  219. * Free'd memory is zero'd or otherwise cleansed.
  220. *
  221. * This is a pretty standard buddy allocator. We keep areas in a multiple
  222. * of "sh.minsize" units. The freelist and bitmaps are kept separately,
  223. * so all (and only) data is kept in the mmap'd heap.
  224. *
  225. * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
  226. * place.
  227. */
  228. #define ONE ((size_t)1)
  229. # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
  230. # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
  231. # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
  232. #define WITHIN_ARENA(p) \
  233. ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
  234. #define WITHIN_FREELIST(p) \
  235. ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
  236. typedef struct sh_list_st
  237. {
  238. struct sh_list_st *next;
  239. struct sh_list_st **p_next;
  240. } SH_LIST;
  241. typedef struct sh_st
  242. {
  243. char* map_result;
  244. size_t map_size;
  245. char *arena;
  246. size_t arena_size;
  247. char **freelist;
  248. ossl_ssize_t freelist_size;
  249. size_t minsize;
  250. unsigned char *bittable;
  251. unsigned char *bitmalloc;
  252. size_t bittable_size; /* size in bits */
  253. } SH;
  254. static SH sh;
  255. static size_t sh_getlist(char *ptr)
  256. {
  257. ossl_ssize_t list = sh.freelist_size - 1;
  258. size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
  259. for (; bit; bit >>= 1, list--) {
  260. if (TESTBIT(sh.bittable, bit))
  261. break;
  262. OPENSSL_assert((bit & 1) == 0);
  263. }
  264. return list;
  265. }
  266. static int sh_testbit(char *ptr, int list, unsigned char *table)
  267. {
  268. size_t bit;
  269. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  270. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  271. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  272. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  273. return TESTBIT(table, bit);
  274. }
  275. static void sh_clearbit(char *ptr, int list, unsigned char *table)
  276. {
  277. size_t bit;
  278. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  279. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  280. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  281. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  282. OPENSSL_assert(TESTBIT(table, bit));
  283. CLEARBIT(table, bit);
  284. }
  285. static void sh_setbit(char *ptr, int list, unsigned char *table)
  286. {
  287. size_t bit;
  288. OPENSSL_assert(list >= 0 && list < sh.freelist_size);
  289. OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
  290. bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
  291. OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
  292. OPENSSL_assert(!TESTBIT(table, bit));
  293. SETBIT(table, bit);
  294. }
  295. static void sh_add_to_list(char **list, char *ptr)
  296. {
  297. SH_LIST *temp;
  298. OPENSSL_assert(WITHIN_FREELIST(list));
  299. OPENSSL_assert(WITHIN_ARENA(ptr));
  300. temp = (SH_LIST *)ptr;
  301. temp->next = *(SH_LIST **)list;
  302. OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
  303. temp->p_next = (SH_LIST **)list;
  304. if (temp->next != NULL) {
  305. OPENSSL_assert((char **)temp->next->p_next == list);
  306. temp->next->p_next = &(temp->next);
  307. }
  308. *list = ptr;
  309. }
  310. static void sh_remove_from_list(char *ptr)
  311. {
  312. SH_LIST *temp, *temp2;
  313. temp = (SH_LIST *)ptr;
  314. if (temp->next != NULL)
  315. temp->next->p_next = temp->p_next;
  316. *temp->p_next = temp->next;
  317. if (temp->next == NULL)
  318. return;
  319. temp2 = temp->next;
  320. OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
  321. }
  322. static int sh_init(size_t size, int minsize)
  323. {
  324. int ret;
  325. size_t i;
  326. size_t pgsize;
  327. size_t aligned;
  328. memset(&sh, 0, sizeof(sh));
  329. /* make sure size and minsize are powers of 2 */
  330. OPENSSL_assert(size > 0);
  331. OPENSSL_assert((size & (size - 1)) == 0);
  332. OPENSSL_assert(minsize > 0);
  333. OPENSSL_assert((minsize & (minsize - 1)) == 0);
  334. if (size <= 0 || (size & (size - 1)) != 0)
  335. goto err;
  336. if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
  337. goto err;
  338. while (minsize < (int)sizeof(SH_LIST))
  339. minsize *= 2;
  340. sh.arena_size = size;
  341. sh.minsize = minsize;
  342. sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
  343. /* Prevent allocations of size 0 later on */
  344. if (sh.bittable_size >> 3 == 0)
  345. goto err;
  346. sh.freelist_size = -1;
  347. for (i = sh.bittable_size; i; i >>= 1)
  348. sh.freelist_size++;
  349. sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
  350. OPENSSL_assert(sh.freelist != NULL);
  351. if (sh.freelist == NULL)
  352. goto err;
  353. sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
  354. OPENSSL_assert(sh.bittable != NULL);
  355. if (sh.bittable == NULL)
  356. goto err;
  357. sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
  358. OPENSSL_assert(sh.bitmalloc != NULL);
  359. if (sh.bitmalloc == NULL)
  360. goto err;
  361. /* Allocate space for heap, and two extra pages as guards */
  362. #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
  363. {
  364. # if defined(_SC_PAGE_SIZE)
  365. long tmppgsize = sysconf(_SC_PAGE_SIZE);
  366. # else
  367. long tmppgsize = sysconf(_SC_PAGESIZE);
  368. # endif
  369. if (tmppgsize < 1)
  370. pgsize = PAGE_SIZE;
  371. else
  372. pgsize = (size_t)tmppgsize;
  373. }
  374. #else
  375. pgsize = PAGE_SIZE;
  376. #endif
  377. sh.map_size = pgsize + sh.arena_size + pgsize;
  378. if (1) {
  379. #ifdef MAP_ANON
  380. sh.map_result = mmap(NULL, sh.map_size,
  381. PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
  382. } else {
  383. #endif
  384. int fd;
  385. sh.map_result = MAP_FAILED;
  386. if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
  387. sh.map_result = mmap(NULL, sh.map_size,
  388. PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  389. close(fd);
  390. }
  391. }
  392. if (sh.map_result == MAP_FAILED)
  393. goto err;
  394. sh.arena = (char *)(sh.map_result + pgsize);
  395. sh_setbit(sh.arena, 0, sh.bittable);
  396. sh_add_to_list(&sh.freelist[0], sh.arena);
  397. /* Now try to add guard pages and lock into memory. */
  398. ret = 1;
  399. /* Starting guard is already aligned from mmap. */
  400. if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
  401. ret = 2;
  402. /* Ending guard page - need to round up to page boundary */
  403. aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
  404. if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
  405. ret = 2;
  406. #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
  407. if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
  408. if (errno == ENOSYS) {
  409. if (mlock(sh.arena, sh.arena_size) < 0)
  410. ret = 2;
  411. } else {
  412. ret = 2;
  413. }
  414. }
  415. #else
  416. if (mlock(sh.arena, sh.arena_size) < 0)
  417. ret = 2;
  418. #endif
  419. #ifdef MADV_DONTDUMP
  420. if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
  421. ret = 2;
  422. #endif
  423. return ret;
  424. err:
  425. sh_done();
  426. return 0;
  427. }
  428. static void sh_done()
  429. {
  430. OPENSSL_free(sh.freelist);
  431. OPENSSL_free(sh.bittable);
  432. OPENSSL_free(sh.bitmalloc);
  433. if (sh.map_result != NULL && sh.map_size)
  434. munmap(sh.map_result, sh.map_size);
  435. memset(&sh, 0, sizeof(sh));
  436. }
  437. static int sh_allocated(const char *ptr)
  438. {
  439. return WITHIN_ARENA(ptr) ? 1 : 0;
  440. }
  441. static char *sh_find_my_buddy(char *ptr, int list)
  442. {
  443. size_t bit;
  444. char *chunk = NULL;
  445. bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
  446. bit ^= 1;
  447. if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
  448. chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
  449. return chunk;
  450. }
  451. static void *sh_malloc(size_t size)
  452. {
  453. ossl_ssize_t list, slist;
  454. size_t i;
  455. char *chunk;
  456. if (size > sh.arena_size)
  457. return NULL;
  458. list = sh.freelist_size - 1;
  459. for (i = sh.minsize; i < size; i <<= 1)
  460. list--;
  461. if (list < 0)
  462. return NULL;
  463. /* try to find a larger entry to split */
  464. for (slist = list; slist >= 0; slist--)
  465. if (sh.freelist[slist] != NULL)
  466. break;
  467. if (slist < 0)
  468. return NULL;
  469. /* split larger entry */
  470. while (slist != list) {
  471. char *temp = sh.freelist[slist];
  472. /* remove from bigger list */
  473. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  474. sh_clearbit(temp, slist, sh.bittable);
  475. sh_remove_from_list(temp);
  476. OPENSSL_assert(temp != sh.freelist[slist]);
  477. /* done with bigger list */
  478. slist++;
  479. /* add to smaller list */
  480. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  481. sh_setbit(temp, slist, sh.bittable);
  482. sh_add_to_list(&sh.freelist[slist], temp);
  483. OPENSSL_assert(sh.freelist[slist] == temp);
  484. /* split in 2 */
  485. temp += sh.arena_size >> slist;
  486. OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
  487. sh_setbit(temp, slist, sh.bittable);
  488. sh_add_to_list(&sh.freelist[slist], temp);
  489. OPENSSL_assert(sh.freelist[slist] == temp);
  490. OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
  491. }
  492. /* peel off memory to hand back */
  493. chunk = sh.freelist[list];
  494. OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
  495. sh_setbit(chunk, list, sh.bitmalloc);
  496. sh_remove_from_list(chunk);
  497. OPENSSL_assert(WITHIN_ARENA(chunk));
  498. return chunk;
  499. }
  500. static void sh_free(void *ptr)
  501. {
  502. size_t list;
  503. void *buddy;
  504. if (ptr == NULL)
  505. return;
  506. OPENSSL_assert(WITHIN_ARENA(ptr));
  507. if (!WITHIN_ARENA(ptr))
  508. return;
  509. list = sh_getlist(ptr);
  510. OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
  511. sh_clearbit(ptr, list, sh.bitmalloc);
  512. sh_add_to_list(&sh.freelist[list], ptr);
  513. /* Try to coalesce two adjacent free areas. */
  514. while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
  515. OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
  516. OPENSSL_assert(ptr != NULL);
  517. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  518. sh_clearbit(ptr, list, sh.bittable);
  519. sh_remove_from_list(ptr);
  520. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  521. sh_clearbit(buddy, list, sh.bittable);
  522. sh_remove_from_list(buddy);
  523. list--;
  524. if (ptr > buddy)
  525. ptr = buddy;
  526. OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
  527. sh_setbit(ptr, list, sh.bittable);
  528. sh_add_to_list(&sh.freelist[list], ptr);
  529. OPENSSL_assert(sh.freelist[list] == ptr);
  530. }
  531. }
  532. static size_t sh_actual_size(char *ptr)
  533. {
  534. int list;
  535. OPENSSL_assert(WITHIN_ARENA(ptr));
  536. if (!WITHIN_ARENA(ptr))
  537. return 0;
  538. list = sh_getlist(ptr);
  539. OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
  540. return sh.arena_size / (ONE << list);
  541. }
  542. #endif /* IMPLEMENTED */