memory_buffer_alloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 <string.h>
  32. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  33. #include <execinfo.h>
  34. #endif
  35. #if defined(MBEDTLS_THREADING_C)
  36. #include "mbedtls/threading.h"
  37. #endif
  38. /* Implementation that should never be optimized out by the compiler */
  39. static void mbedtls_zeroize( void *v, size_t n ) {
  40. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  41. }
  42. #define MAGIC1 0xFF00AA55
  43. #define MAGIC2 0xEE119966
  44. #define MAX_BT 20
  45. typedef struct _memory_header memory_header;
  46. struct _memory_header
  47. {
  48. size_t magic1;
  49. size_t size;
  50. size_t alloc;
  51. memory_header *prev;
  52. memory_header *next;
  53. memory_header *prev_free;
  54. memory_header *next_free;
  55. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  56. char **trace;
  57. size_t trace_count;
  58. #endif
  59. size_t magic2;
  60. };
  61. typedef struct
  62. {
  63. unsigned char *buf;
  64. size_t len;
  65. memory_header *first;
  66. memory_header *first_free;
  67. int verify;
  68. #if defined(MBEDTLS_MEMORY_DEBUG)
  69. size_t alloc_count;
  70. size_t free_count;
  71. size_t total_used;
  72. size_t maximum_used;
  73. size_t header_count;
  74. size_t maximum_header_count;
  75. #endif
  76. #if defined(MBEDTLS_THREADING_C)
  77. mbedtls_threading_mutex_t mutex;
  78. #endif
  79. }
  80. buffer_alloc_ctx;
  81. static buffer_alloc_ctx heap;
  82. #if defined(MBEDTLS_MEMORY_DEBUG)
  83. static void debug_header( memory_header *hdr )
  84. {
  85. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  86. size_t i;
  87. #endif
  88. mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
  89. "ALLOC(%zu), SIZE(%10zu)\n",
  90. (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
  91. hdr->alloc, hdr->size );
  92. mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
  93. (size_t) hdr->prev_free, (size_t) hdr->next_free );
  94. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  95. mbedtls_fprintf( stderr, "TRACE: \n" );
  96. for( i = 0; i < hdr->trace_count; i++ )
  97. mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
  98. mbedtls_fprintf( stderr, "\n" );
  99. #endif
  100. }
  101. static void debug_chain()
  102. {
  103. memory_header *cur = heap.first;
  104. mbedtls_fprintf( stderr, "\nBlock list\n" );
  105. while( cur != NULL )
  106. {
  107. debug_header( cur );
  108. cur = cur->next;
  109. }
  110. mbedtls_fprintf( stderr, "Free list\n" );
  111. cur = heap.first_free;
  112. while( cur != NULL )
  113. {
  114. debug_header( cur );
  115. cur = cur->next_free;
  116. }
  117. }
  118. #endif /* MBEDTLS_MEMORY_DEBUG */
  119. static int verify_header( memory_header *hdr )
  120. {
  121. if( hdr->magic1 != MAGIC1 )
  122. {
  123. #if defined(MBEDTLS_MEMORY_DEBUG)
  124. mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
  125. #endif
  126. return( 1 );
  127. }
  128. if( hdr->magic2 != MAGIC2 )
  129. {
  130. #if defined(MBEDTLS_MEMORY_DEBUG)
  131. mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
  132. #endif
  133. return( 1 );
  134. }
  135. if( hdr->alloc > 1 )
  136. {
  137. #if defined(MBEDTLS_MEMORY_DEBUG)
  138. mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
  139. #endif
  140. return( 1 );
  141. }
  142. if( hdr->prev != NULL && hdr->prev == hdr->next )
  143. {
  144. #if defined(MBEDTLS_MEMORY_DEBUG)
  145. mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
  146. #endif
  147. return( 1 );
  148. }
  149. if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
  150. {
  151. #if defined(MBEDTLS_MEMORY_DEBUG)
  152. mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
  153. #endif
  154. return( 1 );
  155. }
  156. return( 0 );
  157. }
  158. static int verify_chain()
  159. {
  160. memory_header *prv = heap.first, *cur = heap.first->next;
  161. if( verify_header( heap.first ) != 0 )
  162. {
  163. #if defined(MBEDTLS_MEMORY_DEBUG)
  164. mbedtls_fprintf( stderr, "FATAL: verification of first header "
  165. "failed\n" );
  166. #endif
  167. return( 1 );
  168. }
  169. if( heap.first->prev != NULL )
  170. {
  171. #if defined(MBEDTLS_MEMORY_DEBUG)
  172. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  173. "first->prev != NULL\n" );
  174. #endif
  175. return( 1 );
  176. }
  177. while( cur != NULL )
  178. {
  179. if( verify_header( cur ) != 0 )
  180. {
  181. #if defined(MBEDTLS_MEMORY_DEBUG)
  182. mbedtls_fprintf( stderr, "FATAL: verification of header "
  183. "failed\n" );
  184. #endif
  185. return( 1 );
  186. }
  187. if( cur->prev != prv )
  188. {
  189. #if defined(MBEDTLS_MEMORY_DEBUG)
  190. mbedtls_fprintf( stderr, "FATAL: verification failed: "
  191. "cur->prev != prv\n" );
  192. #endif
  193. return( 1 );
  194. }
  195. prv = cur;
  196. cur = cur->next;
  197. }
  198. return( 0 );
  199. }
  200. static void *buffer_alloc_calloc( size_t n, size_t size )
  201. {
  202. memory_header *new, *cur = heap.first_free;
  203. unsigned char *p;
  204. void *ret;
  205. size_t original_len, len;
  206. #if defined(MBEDTLS_MEMORY_BACKTRACE)
  207. void *trace_buffer[MAX_BT];
  208. size_t trace_cnt;
  209. #endif
  210. if( heap.buf == NULL || heap.first == NULL )
  211. return( NULL );
  212. original_len = len = n * size;
  213. if( n != 0 && len / n != size )
  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()
  421. {
  422. return verify_chain();
  423. }
  424. #if defined(MBEDTLS_MEMORY_DEBUG)
  425. void mbedtls_memory_buffer_alloc_status()
  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. mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
  437. else
  438. {
  439. mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
  440. debug_chain();
  441. }
  442. }
  443. void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
  444. {
  445. *max_used = heap.maximum_used;
  446. *max_blocks = heap.maximum_header_count;
  447. }
  448. void mbedtls_memory_buffer_alloc_max_reset( void )
  449. {
  450. heap.maximum_used = 0;
  451. heap.maximum_header_count = 0;
  452. }
  453. void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
  454. {
  455. *cur_used = heap.total_used;
  456. *cur_blocks = heap.header_count;
  457. }
  458. #endif /* MBEDTLS_MEMORY_DEBUG */
  459. #if defined(MBEDTLS_THREADING_C)
  460. static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
  461. {
  462. void *buf;
  463. if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
  464. return( NULL );
  465. buf = buffer_alloc_calloc( n, size );
  466. if( mbedtls_mutex_unlock( &heap.mutex ) )
  467. return( NULL );
  468. return( buf );
  469. }
  470. static void buffer_alloc_free_mutexed( void *ptr )
  471. {
  472. /* We have to good option here, but corrupting the heap seems
  473. * worse than loosing memory. */
  474. if( mbedtls_mutex_lock( &heap.mutex ) )
  475. return;
  476. buffer_alloc_free( ptr );
  477. (void) mbedtls_mutex_unlock( &heap.mutex );
  478. }
  479. #endif /* MBEDTLS_THREADING_C */
  480. void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
  481. {
  482. memset( &heap, 0, sizeof(buffer_alloc_ctx) );
  483. memset( buf, 0, len );
  484. #if defined(MBEDTLS_THREADING_C)
  485. mbedtls_mutex_init( &heap.mutex );
  486. mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
  487. buffer_alloc_free_mutexed );
  488. #else
  489. mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
  490. #endif
  491. if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
  492. {
  493. /* Adjust len first since buf is used in the computation */
  494. len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
  495. - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  496. buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
  497. - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
  498. }
  499. heap.buf = buf;
  500. heap.len = len;
  501. heap.first = (memory_header *) buf;
  502. heap.first->size = len - sizeof(memory_header);
  503. heap.first->magic1 = MAGIC1;
  504. heap.first->magic2 = MAGIC2;
  505. heap.first_free = heap.first;
  506. }
  507. void mbedtls_memory_buffer_alloc_free()
  508. {
  509. #if defined(MBEDTLS_THREADING_C)
  510. mbedtls_mutex_free( &heap.mutex );
  511. #endif
  512. mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
  513. }
  514. #if defined(MBEDTLS_SELF_TEST)
  515. static int check_pointer( void *p )
  516. {
  517. if( p == NULL )
  518. return( -1 );
  519. if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
  520. return( -1 );
  521. return( 0 );
  522. }
  523. static int check_all_free( )
  524. {
  525. if(
  526. #if defined(MBEDTLS_MEMORY_DEBUG)
  527. heap.total_used != 0 ||
  528. #endif
  529. heap.first != heap.first_free ||
  530. (void *) heap.first != (void *) heap.buf )
  531. {
  532. return( -1 );
  533. }
  534. return( 0 );
  535. }
  536. #define TEST_ASSERT( condition ) \
  537. if( ! (condition) ) \
  538. { \
  539. if( verbose != 0 ) \
  540. mbedtls_printf( "failed\n" ); \
  541. \
  542. ret = 1; \
  543. goto cleanup; \
  544. }
  545. int mbedtls_memory_buffer_alloc_self_test( int verbose )
  546. {
  547. unsigned char buf[1024];
  548. unsigned char *p, *q, *r, *end;
  549. int ret = 0;
  550. if( verbose != 0 )
  551. mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
  552. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  553. p = mbedtls_calloc( 1, 1 );
  554. q = mbedtls_calloc( 1, 128 );
  555. r = mbedtls_calloc( 1, 16 );
  556. TEST_ASSERT( check_pointer( p ) == 0 &&
  557. check_pointer( q ) == 0 &&
  558. check_pointer( r ) == 0 );
  559. mbedtls_free( r );
  560. mbedtls_free( q );
  561. mbedtls_free( p );
  562. TEST_ASSERT( check_all_free( ) == 0 );
  563. /* Memorize end to compare with the next test */
  564. end = heap.buf + heap.len;
  565. mbedtls_memory_buffer_alloc_free( );
  566. if( verbose != 0 )
  567. mbedtls_printf( "passed\n" );
  568. if( verbose != 0 )
  569. mbedtls_printf( " MBA test #2 (buf not aligned): " );
  570. mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
  571. TEST_ASSERT( heap.buf + heap.len == end );
  572. p = mbedtls_calloc( 1, 1 );
  573. q = mbedtls_calloc( 1, 128 );
  574. r = mbedtls_calloc( 1, 16 );
  575. TEST_ASSERT( check_pointer( p ) == 0 &&
  576. check_pointer( q ) == 0 &&
  577. check_pointer( r ) == 0 );
  578. mbedtls_free( r );
  579. mbedtls_free( q );
  580. mbedtls_free( p );
  581. TEST_ASSERT( check_all_free( ) == 0 );
  582. mbedtls_memory_buffer_alloc_free( );
  583. if( verbose != 0 )
  584. mbedtls_printf( "passed\n" );
  585. if( verbose != 0 )
  586. mbedtls_printf( " MBA test #3 (full): " );
  587. mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
  588. p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
  589. TEST_ASSERT( check_pointer( p ) == 0 );
  590. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  591. mbedtls_free( p );
  592. p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
  593. q = mbedtls_calloc( 1, 16 );
  594. TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
  595. TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
  596. mbedtls_free( q );
  597. TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
  598. mbedtls_free( p );
  599. TEST_ASSERT( check_all_free( ) == 0 );
  600. mbedtls_memory_buffer_alloc_free( );
  601. if( verbose != 0 )
  602. mbedtls_printf( "passed\n" );
  603. cleanup:
  604. mbedtls_memory_buffer_alloc_free( );
  605. return( ret );
  606. }
  607. #endif /* MBEDTLS_SELF_TEST */
  608. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */