memory_buffer_alloc.c 19 KB

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