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