memory_buffer_alloc.c 19 KB

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