memory_buffer_alloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Buffer-based memory allocator
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  29. #include "mbedtls/memory_buffer_alloc.h"
  30. /* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
  31. is dependent upon MBEDTLS_PLATFORM_C */
  32. #include "mbedtls/platform.h"
  33. #include <string.h>
  34. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  35. #include <execinfo.h>
  36. #endif
  37. #if defined(MBEDTLS_THREADING_C)
  38. #include "mbedtls/threading.h"
  39. #endif
  40. /* Implementation that should never be optimized out by the compiler */
  41. static void mbedtls_zeroize( void *v, size_t n ) {
  42. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  43. }
  44. #define MAGIC1 0xFF00AA55
  45. #define MAGIC2 0xEE119966
  46. #define MAX_BT 20
  47. typedef struct _memory_header memory_header;
  48. struct _memory_header
  49. {
  50. size_t magic1;
  51. size_t size;
  52. size_t alloc;
  53. memory_header *prev;
  54. memory_header *next;
  55. memory_header *prev_free;
  56. memory_header *next_free;
  57. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  58. char **trace;
  59. size_t trace_count;
  60. #endif
  61. size_t magic2;
  62. };
  63. typedef struct
  64. {
  65. unsigned char *buf;
  66. size_t len;
  67. memory_header *first;
  68. memory_header *first_free;
  69. int verify;
  70. #if defined(MBEDTLS_MEMORY_DEBUG)
  71. size_t alloc_count;
  72. size_t free_count;
  73. size_t total_used;
  74. size_t maximum_used;
  75. size_t header_count;
  76. size_t maximum_header_count;
  77. #endif
  78. #if defined(MBEDTLS_THREADING_C)
  79. mbedtls_threading_mutex_t mutex;
  80. #endif
  81. }
  82. buffer_alloc_ctx;
  83. static buffer_alloc_ctx heap;
  84. #if defined(MBEDTLS_MEMORY_DEBUG)
  85. static void debug_header( memory_header *hdr )
  86. {
  87. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  88. size_t i;
  89. #endif
  90. mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
  91. "ALLOC(%zu), SIZE(%10zu)\n",
  92. (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
  93. hdr->alloc, hdr->size );
  94. mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
  95. (size_t) hdr->prev_free, (size_t) hdr->next_free );
  96. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  97. mbedtls_fprintf( stderr, "TRACE: \n" );
  98. for( i = 0; i < hdr->trace_count; i++ )
  99. mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
  100. mbedtls_fprintf( stderr, "\n" );
  101. #endif
  102. }
  103. static void debug_chain()
  104. {
  105. memory_header *cur = heap.first;
  106. mbedtls_fprintf( stderr, "\nBlock list\n" );
  107. while( cur != NULL )
  108. {
  109. debug_header( cur );
  110. cur = cur->next;
  111. }
  112. mbedtls_fprintf( stderr, "Free list\n" );
  113. cur = heap.first_free;
  114. while( cur != NULL )
  115. {
  116. debug_header( cur );
  117. cur = cur->next_free;
  118. }
  119. }
  120. #endif /* MBEDTLS_MEMORY_DEBUG */
  121. static int verify_header( memory_header *hdr )
  122. {
  123. if( hdr->magic1 != MAGIC1 )
  124. {
  125. #if defined(MBEDTLS_MEMORY_DEBUG)
  126. mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
  127. #endif
  128. return( 1 );
  129. }
  130. if( hdr->magic2 != MAGIC2 )
  131. {
  132. #if defined(MBEDTLS_MEMORY_DEBUG)
  133. mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
  134. #endif
  135. return( 1 );
  136. }
  137. if( hdr->alloc > 1 )
  138. {
  139. #if defined(MBEDTLS_MEMORY_DEBUG)
  140. mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
  141. #endif
  142. return( 1 );
  143. }
  144. if( hdr->prev != NULL && hdr->prev == hdr->next )
  145. {
  146. #if defined(MBEDTLS_MEMORY_DEBUG)
  147. mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
  148. #endif
  149. return( 1 );
  150. }
  151. if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
  152. {
  153. #if defined(MBEDTLS_MEMORY_DEBUG)
  154. mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
  155. #endif
  156. return( 1 );
  157. }
  158. return( 0 );
  159. }
  160. static int verify_chain()
  161. {
  162. memory_header *prv = heap.first, *cur = heap.first->next;
  163. if( verify_header( heap.first ) != 0 )
  164. {
  165. #if defined(MBEDTLS_MEMORY_DEBUG)
  166. mbedtls_fprintf( stderr, "FATAL: verification of first header "
  167. "failed\n" );
  168. #endif
  169. return( 1 );
  170. }
  171. if( heap.first->prev != NULL )
  172. {
  173. #if defined(MBEDTLS_MEMORY_DEBUG)
  174. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  175. "first->prev != NULL\n" );
  176. #endif
  177. return( 1 );
  178. }
  179. while( cur != NULL )
  180. {
  181. if( verify_header( cur ) != 0 )
  182. {
  183. #if defined(MBEDTLS_MEMORY_DEBUG)
  184. mbedtls_fprintf( stderr, "FATAL: verification of header "
  185. "failed\n" );
  186. #endif
  187. return( 1 );
  188. }
  189. if( cur->prev != prv )
  190. {
  191. #if defined(MBEDTLS_MEMORY_DEBUG)
  192. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  193. "cur->prev != prv\n" );
  194. #endif
  195. return( 1 );
  196. }
  197. prv = cur;
  198. cur = cur->next;
  199. }
  200. return( 0 );
  201. }
  202. static void *buffer_alloc_calloc( size_t n, size_t size )
  203. {
  204. memory_header *new, *cur = heap.first_free;
  205. unsigned char *p;
  206. void *ret;
  207. size_t original_len, len;
  208. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  209. void *trace_buffer[MAX_BT];
  210. size_t trace_cnt;
  211. #endif
  212. if( heap.buf == NULL || heap.first == NULL )
  213. return( NULL );
  214. original_len = len = n * size;
  215. if( n != 0 && len / n != size )
  216. return( NULL );
  217. if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  218. {
  219. len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  220. len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  221. }
  222. // Find block that fits
  223. //
  224. while( cur != NULL )
  225. {
  226. if( cur->size >= len )
  227. break;
  228. cur = cur->next_free;
  229. }
  230. if( cur == NULL )
  231. return( NULL );
  232. if( cur->alloc != 0 )
  233. {
  234. #if defined(MBEDTLS_MEMORY_DEBUG)
  235. mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
  236. "data\n" );
  237. #endif
  238. mbedtls_exit( 1 );
  239. }
  240. #if defined(MBEDTLS_MEMORY_DEBUG)
  241. heap.alloc_count++;
  242. #endif
  243. // Found location, split block if > memory_header + 4 room left
  244. //
  245. if( cur->size - len < sizeof(memory_header) +
  246. MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  247. {
  248. cur->alloc = 1;
  249. // Remove from free_list
  250. //
  251. if( cur->prev_free != NULL )
  252. cur->prev_free->next_free = cur->next_free;
  253. else
  254. heap.first_free = cur->next_free;
  255. if( cur->next_free != NULL )
  256. cur->next_free->prev_free = cur->prev_free;
  257. cur->prev_free = NULL;
  258. cur->next_free = NULL;
  259. #if defined(MBEDTLS_MEMORY_DEBUG)
  260. heap.total_used += cur->size;
  261. if( heap.total_used > heap.maximum_used )
  262. heap.maximum_used = heap.total_used;
  263. #endif
  264. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  265. trace_cnt = backtrace( trace_buffer, MAX_BT );
  266. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  267. cur->trace_count = trace_cnt;
  268. #endif
  269. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
  270. mbedtls_exit( 1 );
  271. ret = (unsigned char *) cur + sizeof( memory_header );
  272. memset( ret, 0, original_len );
  273. return( ret );
  274. }
  275. p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
  276. new = (memory_header *) p;
  277. new->size = cur->size - len - sizeof(memory_header);
  278. new->alloc = 0;
  279. new->prev = cur;
  280. new->next = cur->next;
  281. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  282. new->trace = NULL;
  283. new->trace_count = 0;
  284. #endif
  285. new->magic1 = MAGIC1;
  286. new->magic2 = MAGIC2;
  287. if( new->next != NULL )
  288. new->next->prev = new;
  289. // Replace cur with new in free_list
  290. //
  291. new->prev_free = cur->prev_free;
  292. new->next_free = cur->next_free;
  293. if( new->prev_free != NULL )
  294. new->prev_free->next_free = new;
  295. else
  296. heap.first_free = new;
  297. if( new->next_free != NULL )
  298. new->next_free->prev_free = new;
  299. cur->alloc = 1;
  300. cur->size = len;
  301. cur->next = new;
  302. cur->prev_free = NULL;
  303. cur->next_free = NULL;
  304. #if defined(MBEDTLS_MEMORY_DEBUG)
  305. heap.header_count++;
  306. if( heap.header_count > heap.maximum_header_count )
  307. heap.maximum_header_count = heap.header_count;
  308. heap.total_used += cur->size;
  309. if( heap.total_used > heap.maximum_used )
  310. heap.maximum_used = heap.total_used;
  311. #endif
  312. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  313. trace_cnt = backtrace( trace_buffer, MAX_BT );
  314. cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
  315. cur->trace_count = trace_cnt;
  316. #endif
  317. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
  318. mbedtls_exit( 1 );
  319. ret = (unsigned char *) cur + sizeof( memory_header );
  320. memset( ret, 0, original_len );
  321. return( ret );
  322. }
  323. static void buffer_alloc_free( void *ptr )
  324. {
  325. memory_header *hdr, *old = NULL;
  326. unsigned char *p = (unsigned char *) ptr;
  327. if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
  328. return;
  329. if( p < heap.buf || p > heap.buf + heap.len )
  330. {
  331. #if defined(MBEDTLS_MEMORY_DEBUG)
  332. mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
  333. "space\n" );
  334. #endif
  335. mbedtls_exit( 1 );
  336. }
  337. p -= sizeof(memory_header);
  338. hdr = (memory_header *) p;
  339. if( verify_header( hdr ) != 0 )
  340. mbedtls_exit( 1 );
  341. if( hdr->alloc != 1 )
  342. {
  343. #if defined(MBEDTLS_MEMORY_DEBUG)
  344. mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
  345. "data\n" );
  346. #endif
  347. mbedtls_exit( 1 );
  348. }
  349. hdr->alloc = 0;
  350. #if defined(MBEDTLS_MEMORY_DEBUG)
  351. heap.free_count++;
  352. heap.total_used -= hdr->size;
  353. #endif
  354. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  355. free( hdr->trace );
  356. hdr->trace = NULL;
  357. hdr->trace_count = 0;
  358. #endif
  359. // Regroup with block before
  360. //
  361. if( hdr->prev != NULL && hdr->prev->alloc == 0 )
  362. {
  363. #if defined(MBEDTLS_MEMORY_DEBUG)
  364. heap.header_count--;
  365. #endif
  366. hdr->prev->size += sizeof(memory_header) + hdr->size;
  367. hdr->prev->next = hdr->next;
  368. old = hdr;
  369. hdr = hdr->prev;
  370. if( hdr->next != NULL )
  371. hdr->next->prev = hdr;
  372. memset( old, 0, sizeof(memory_header) );
  373. }
  374. // Regroup with block after
  375. //
  376. if( hdr->next != NULL && hdr->next->alloc == 0 )
  377. {
  378. #if defined(MBEDTLS_MEMORY_DEBUG)
  379. heap.header_count--;
  380. #endif
  381. hdr->size += sizeof(memory_header) + hdr->next->size;
  382. old = hdr->next;
  383. hdr->next = hdr->next->next;
  384. if( hdr->prev_free != NULL || hdr->next_free != NULL )
  385. {
  386. if( hdr->prev_free != NULL )
  387. hdr->prev_free->next_free = hdr->next_free;
  388. else
  389. heap.first_free = hdr->next_free;
  390. if( hdr->next_free != NULL )
  391. hdr->next_free->prev_free = hdr->prev_free;
  392. }
  393. hdr->prev_free = old->prev_free;
  394. hdr->next_free = old->next_free;
  395. if( hdr->prev_free != NULL )
  396. hdr->prev_free->next_free = hdr;
  397. else
  398. heap.first_free = hdr;
  399. if( hdr->next_free != NULL )
  400. hdr->next_free->prev_free = hdr;
  401. if( hdr->next != NULL )
  402. hdr->next->prev = hdr;
  403. memset( old, 0, sizeof(memory_header) );
  404. }
  405. // Prepend to free_list if we have not merged
  406. // (Does not have to stay in same order as prev / next list)
  407. //
  408. if( old == NULL )
  409. {
  410. hdr->next_free = heap.first_free;
  411. if( heap.first_free != NULL )
  412. heap.first_free->prev_free = hdr;
  413. heap.first_free = hdr;
  414. }
  415. if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
  416. mbedtls_exit( 1 );
  417. }
  418. void mbedtls_memory_buffer_set_verify( int verify )
  419. {
  420. heap.verify = verify;
  421. }
  422. int mbedtls_memory_buffer_alloc_verify()
  423. {
  424. return verify_chain();
  425. }
  426. #if defined(MBEDTLS_MEMORY_DEBUG)
  427. void mbedtls_memory_buffer_alloc_status()
  428. {
  429. mbedtls_fprintf( stderr,
  430. "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
  431. "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
  432. heap.header_count, heap.total_used,
  433. heap.maximum_header_count, heap.maximum_used,
  434. heap.maximum_header_count * sizeof( memory_header )
  435. + heap.maximum_used,
  436. heap.alloc_count, heap.free_count );
  437. if( heap.first->next == NULL )
  438. mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
  439. else
  440. {
  441. mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
  442. debug_chain();
  443. }
  444. }
  445. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
  446. {
  447. *max_used = heap.maximum_used;
  448. *max_blocks = heap.maximum_header_count;
  449. }
  450. void mbedtls_memory_buffer_alloc_max_reset( void )
  451. {
  452. heap.maximum_used = 0;
  453. heap.maximum_header_count = 0;
  454. }
  455. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
  456. {
  457. *cur_used = heap.total_used;
  458. *cur_blocks = heap.header_count;
  459. }
  460. #endif /* MBEDTLS_MEMORY_DEBUG */
  461. #if defined(MBEDTLS_THREADING_C)
  462. static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
  463. {
  464. void *buf;
  465. if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
  466. return( NULL );
  467. buf = buffer_alloc_calloc( n, size );
  468. if( mbedtls_mutex_unlock( &heap.mutex ) )
  469. return( NULL );
  470. return( buf );
  471. }
  472. static void buffer_alloc_free_mutexed( void *ptr )
  473. {
  474. /* We have to good option here, but corrupting the heap seems
  475. * worse than loosing memory. */
  476. if( mbedtls_mutex_lock( &heap.mutex ) )
  477. return;
  478. buffer_alloc_free( ptr );
  479. (void) mbedtls_mutex_unlock( &heap.mutex );
  480. }
  481. #endif /* MBEDTLS_THREADING_C */
  482. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
  483. {
  484. memset( &heap, 0, sizeof(buffer_alloc_ctx) );
  485. memset( buf, 0, len );
  486. #if defined(MBEDTLS_THREADING_C)
  487. mbedtls_mutex_init( &heap.mutex );
  488. mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
  489. buffer_alloc_free_mutexed );
  490. #else
  491. mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
  492. #endif
  493. if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  494. {
  495. /* Adjust len first since buf is used in the computation */
  496. len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
  497. - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  498. buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
  499. - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  500. }
  501. heap.buf = buf;
  502. heap.len = len;
  503. heap.first = (memory_header *) buf;
  504. heap.first->size = len - sizeof(memory_header);
  505. heap.first->magic1 = MAGIC1;
  506. heap.first->magic2 = MAGIC2;
  507. heap.first_free = heap.first;
  508. }
  509. void mbedtls_memory_buffer_alloc_free()
  510. {
  511. #if defined(MBEDTLS_THREADING_C)
  512. mbedtls_mutex_free( &heap.mutex );
  513. #endif
  514. mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
  515. }
  516. #if defined(MBEDTLS_SELF_TEST)
  517. static int check_pointer( void *p )
  518. {
  519. if( p == NULL )
  520. return( -1 );
  521. if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
  522. return( -1 );
  523. return( 0 );
  524. }
  525. static int check_all_free( )
  526. {
  527. if(
  528. #if defined(MBEDTLS_MEMORY_DEBUG)
  529. heap.total_used != 0 ||
  530. #endif
  531. heap.first != heap.first_free ||
  532. (void *) heap.first != (void *) heap.buf )
  533. {
  534. return( -1 );
  535. }
  536. return( 0 );
  537. }
  538. #define TEST_ASSERT( condition ) \
  539. if( ! (condition) ) \
  540. { \
  541. if( verbose != 0 ) \
  542. mbedtls_printf( "failed\n" ); \
  543. \
  544. ret = 1; \
  545. goto cleanup; \
  546. }
  547. int mbedtls_memory_buffer_alloc_self_test( int verbose )
  548. {
  549. unsigned char buf[1024];
  550. unsigned char *p, *q, *r, *end;
  551. int ret = 0;
  552. if( verbose != 0 )
  553. mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
  554. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  555. p = mbedtls_calloc( 1, 1 );
  556. q = mbedtls_calloc( 1, 128 );
  557. r = mbedtls_calloc( 1, 16 );
  558. TEST_ASSERT( check_pointer( p ) == 0 &&
  559. check_pointer( q ) == 0 &&
  560. check_pointer( r ) == 0 );
  561. mbedtls_free( r );
  562. mbedtls_free( q );
  563. mbedtls_free( p );
  564. TEST_ASSERT( check_all_free( ) == 0 );
  565. /* Memorize end to compare with the next test */
  566. end = heap.buf + heap.len;
  567. mbedtls_memory_buffer_alloc_free( );
  568. if( verbose != 0 )
  569. mbedtls_printf( "passed\n" );
  570. if( verbose != 0 )
  571. mbedtls_printf( " MBA test #2 (buf not aligned): " );
  572. mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
  573. TEST_ASSERT( heap.buf + heap.len == end );
  574. p = mbedtls_calloc( 1, 1 );
  575. q = mbedtls_calloc( 1, 128 );
  576. r = mbedtls_calloc( 1, 16 );
  577. TEST_ASSERT( check_pointer( p ) == 0 &&
  578. check_pointer( q ) == 0 &&
  579. check_pointer( r ) == 0 );
  580. mbedtls_free( r );
  581. mbedtls_free( q );
  582. mbedtls_free( p );
  583. TEST_ASSERT( check_all_free( ) == 0 );
  584. mbedtls_memory_buffer_alloc_free( );
  585. if( verbose != 0 )
  586. mbedtls_printf( "passed\n" );
  587. if( verbose != 0 )
  588. mbedtls_printf( " MBA test #3 (full): " );
  589. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  590. p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
  591. TEST_ASSERT( check_pointer( p ) == 0 );
  592. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  593. mbedtls_free( p );
  594. p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
  595. q = mbedtls_calloc( 1, 16 );
  596. TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
  597. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  598. mbedtls_free( q );
  599. TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
  600. mbedtls_free( p );
  601. TEST_ASSERT( check_all_free( ) == 0 );
  602. mbedtls_memory_buffer_alloc_free( );
  603. if( verbose != 0 )
  604. mbedtls_printf( "passed\n" );
  605. cleanup:
  606. mbedtls_memory_buffer_alloc_free( );
  607. return( ret );
  608. }
  609. #endif /* MBEDTLS_SELF_TEST */
  610. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */