blob: 51ea7c41d77334bc6c0261cbca11294aa3dbef4b [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker6e339b52013-07-03 13:37:05 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6e339b52013-07-03 13:37:05 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
32 is dependent upon MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Rich Evansd08a6052015-02-12 12:17:10 +000035
Paul Bakker6e339b52013-07-03 13:37:05 +020036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020039#include <execinfo.h>
40#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020044#endif
45
Paul Bakker6e339b52013-07-03 13:37:05 +020046#define MAGIC1 0xFF00AA55
47#define MAGIC2 0xEE119966
48#define MAX_BT 20
49
50typedef struct _memory_header memory_header;
51struct _memory_header
52{
53 size_t magic1;
54 size_t size;
55 size_t alloc;
56 memory_header *prev;
57 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020058 memory_header *prev_free;
59 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020061 char **trace;
62 size_t trace_count;
63#endif
64 size_t magic2;
65};
66
67typedef struct
68{
69 unsigned char *buf;
70 size_t len;
71 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020072 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020073 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020075 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +020076 size_t free_count;
77 size_t total_used;
78 size_t maximum_used;
79 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010080 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020081#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_THREADING_C)
83 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +020084#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020085}
86buffer_alloc_ctx;
87
88static buffer_alloc_ctx heap;
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +020091static void debug_header( memory_header *hdr )
92{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020094 size_t i;
95#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +020098 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +010099 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
100 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100102 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_MEMORY_BACKTRACE)
105 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200106 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
108 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200109#endif
110}
111
Joris Aertse75b88d2016-11-04 23:05:56 +0100112static void debug_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200113{
114 memory_header *cur = heap.first;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200117 while( cur != NULL )
118 {
119 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200120 cur = cur->next;
121 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200124 cur = heap.first_free;
125
126 while( cur != NULL )
127 {
128 debug_header( cur );
129 cur = cur->next_free;
130 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200133
134static int verify_header( memory_header *hdr )
135{
136 if( hdr->magic1 != MAGIC1 )
137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_MEMORY_DEBUG)
139 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200140#endif
141 return( 1 );
142 }
143
144 if( hdr->magic2 != MAGIC2 )
145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_MEMORY_DEBUG)
147 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200148#endif
149 return( 1 );
150 }
151
152 if( hdr->alloc > 1 )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_MEMORY_DEBUG)
155 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200156#endif
157 return( 1 );
158 }
159
Paul Bakker1ef120f2013-07-03 17:20:39 +0200160 if( hdr->prev != NULL && hdr->prev == hdr->next )
161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_MEMORY_DEBUG)
163 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200164#endif
165 return( 1 );
166 }
167
168 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_MEMORY_DEBUG)
171 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200172#endif
173 return( 1 );
174 }
175
Paul Bakker6e339b52013-07-03 13:37:05 +0200176 return( 0 );
177}
178
Joris Aertse75b88d2016-11-04 23:05:56 +0100179static int verify_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200180{
Andres AG9cf1f962017-01-30 14:34:25 +0000181 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200182
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100183 if( prv == NULL || verify_header( prv ) != 0 )
Paul Bakker6e339b52013-07-03 13:37:05 +0200184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_MEMORY_DEBUG)
186 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100187 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200188#endif
189 return( 1 );
190 }
191
192 if( heap.first->prev != NULL )
193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_MEMORY_DEBUG)
195 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100196 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200197#endif
198 return( 1 );
199 }
200
Andres AG9cf1f962017-01-30 14:34:25 +0000201 cur = heap.first->next;
202
Paul Bakker6e339b52013-07-03 13:37:05 +0200203 while( cur != NULL )
204 {
205 if( verify_header( cur ) != 0 )
206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_MEMORY_DEBUG)
208 mbedtls_fprintf( stderr, "FATAL: verification of header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100209 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200210#endif
211 return( 1 );
212 }
213
214 if( cur->prev != prv )
215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_MEMORY_DEBUG)
217 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100218 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200219#endif
220 return( 1 );
221 }
222
223 prv = cur;
224 cur = cur->next;
225 }
226
227 return( 0 );
228}
229
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200230static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200231{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200232 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200233 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200234 void *ret;
235 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200237 void *trace_buffer[MAX_BT];
238 size_t trace_cnt;
239#endif
240
241 if( heap.buf == NULL || heap.first == NULL )
242 return( NULL );
243
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200244 original_len = len = n * size;
245
Andres AG9cf1f962017-01-30 14:34:25 +0000246 if( n == 0 || size == 0 || len / n != size )
247 return( NULL );
248 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200249 return( NULL );
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
254 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200255 }
256
257 // Find block that fits
258 //
259 while( cur != NULL )
260 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200261 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200262 break;
263
Paul Bakker1ef120f2013-07-03 17:20:39 +0200264 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200265 }
266
267 if( cur == NULL )
268 return( NULL );
269
Paul Bakker1ef120f2013-07-03 17:20:39 +0200270 if( cur->alloc != 0 )
271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_MEMORY_DEBUG)
273 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100274 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200280 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200281#endif
282
Paul Bakker6e339b52013-07-03 13:37:05 +0200283 // Found location, split block if > memory_header + 4 room left
284 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200285 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 {
288 cur->alloc = 1;
289
Paul Bakker1ef120f2013-07-03 17:20:39 +0200290 // Remove from free_list
291 //
292 if( cur->prev_free != NULL )
293 cur->prev_free->next_free = cur->next_free;
294 else
295 heap.first_free = cur->next_free;
296
297 if( cur->next_free != NULL )
298 cur->next_free->prev_free = cur->prev_free;
299
300 cur->prev_free = NULL;
301 cur->next_free = NULL;
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200304 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200305 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200306 heap.maximum_used = heap.total_used;
307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200309 trace_cnt = backtrace( trace_buffer, MAX_BT );
310 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
311 cur->trace_count = trace_cnt;
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
315 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200316
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200317 ret = (unsigned char *) cur + sizeof( memory_header );
318 memset( ret, 0, original_len );
319
320 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200321 }
322
323 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
324 new = (memory_header *) p;
325
326 new->size = cur->size - len - sizeof(memory_header);
327 new->alloc = 0;
328 new->prev = cur;
329 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200331 new->trace = NULL;
332 new->trace_count = 0;
333#endif
334 new->magic1 = MAGIC1;
335 new->magic2 = MAGIC2;
336
337 if( new->next != NULL )
338 new->next->prev = new;
339
Paul Bakker1ef120f2013-07-03 17:20:39 +0200340 // Replace cur with new in free_list
341 //
342 new->prev_free = cur->prev_free;
343 new->next_free = cur->next_free;
344 if( new->prev_free != NULL )
345 new->prev_free->next_free = new;
346 else
347 heap.first_free = new;
348
349 if( new->next_free != NULL )
350 new->next_free->prev_free = new;
351
Paul Bakker6e339b52013-07-03 13:37:05 +0200352 cur->alloc = 1;
353 cur->size = len;
354 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200355 cur->prev_free = NULL;
356 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200359 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100360 if( heap.header_count > heap.maximum_header_count )
361 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200362 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200363 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200364 heap.maximum_used = heap.total_used;
365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200367 trace_cnt = backtrace( trace_buffer, MAX_BT );
368 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
369 cur->trace_count = trace_cnt;
370#endif
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
373 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200374
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200375 ret = (unsigned char *) cur + sizeof( memory_header );
376 memset( ret, 0, original_len );
377
378 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200379}
380
381static void buffer_alloc_free( void *ptr )
382{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200383 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200384 unsigned char *p = (unsigned char *) ptr;
385
Paul Bakker6e339b52013-07-03 13:37:05 +0200386 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
387 return;
388
Andres AG9cf1f962017-01-30 14:34:25 +0000389 if( p < heap.buf || p >= heap.buf + heap.len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_MEMORY_DEBUG)
392 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100393 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200396 }
397
398 p -= sizeof(memory_header);
399 hdr = (memory_header *) p;
400
401 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200403
404 if( hdr->alloc != 1 )
405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_MEMORY_DEBUG)
407 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100408 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200411 }
412
413 hdr->alloc = 0;
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200416 heap.free_count++;
417 heap.total_used -= hdr->size;
418#endif
419
SimonB42256112016-05-02 01:05:22 +0100420#if defined(MBEDTLS_MEMORY_BACKTRACE)
421 free( hdr->trace );
422 hdr->trace = NULL;
423 hdr->trace_count = 0;
424#endif
425
Paul Bakker6e339b52013-07-03 13:37:05 +0200426 // Regroup with block before
427 //
428 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200431 heap.header_count--;
432#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200433 hdr->prev->size += sizeof(memory_header) + hdr->size;
434 hdr->prev->next = hdr->next;
435 old = hdr;
436 hdr = hdr->prev;
437
438 if( hdr->next != NULL )
439 hdr->next->prev = hdr;
440
Paul Bakker6e339b52013-07-03 13:37:05 +0200441 memset( old, 0, sizeof(memory_header) );
442 }
443
444 // Regroup with block after
445 //
446 if( hdr->next != NULL && hdr->next->alloc == 0 )
447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200449 heap.header_count--;
450#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200451 hdr->size += sizeof(memory_header) + hdr->next->size;
452 old = hdr->next;
453 hdr->next = hdr->next->next;
454
Paul Bakker1ef120f2013-07-03 17:20:39 +0200455 if( hdr->prev_free != NULL || hdr->next_free != NULL )
456 {
457 if( hdr->prev_free != NULL )
458 hdr->prev_free->next_free = hdr->next_free;
459 else
460 heap.first_free = hdr->next_free;
461
462 if( hdr->next_free != NULL )
463 hdr->next_free->prev_free = hdr->prev_free;
464 }
465
466 hdr->prev_free = old->prev_free;
467 hdr->next_free = old->next_free;
468
469 if( hdr->prev_free != NULL )
470 hdr->prev_free->next_free = hdr;
471 else
472 heap.first_free = hdr;
473
474 if( hdr->next_free != NULL )
475 hdr->next_free->prev_free = hdr;
476
Paul Bakker6e339b52013-07-03 13:37:05 +0200477 if( hdr->next != NULL )
478 hdr->next->prev = hdr;
479
Paul Bakker6e339b52013-07-03 13:37:05 +0200480 memset( old, 0, sizeof(memory_header) );
481 }
482
Paul Bakker1ef120f2013-07-03 17:20:39 +0200483 // Prepend to free_list if we have not merged
484 // (Does not have to stay in same order as prev / next list)
485 //
486 if( old == NULL )
487 {
488 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100489 if( heap.first_free != NULL )
490 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200491 heap.first_free = hdr;
492 }
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
495 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200496}
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200499{
500 heap.verify = verify;
501}
502
Joris Aertse75b88d2016-11-04 23:05:56 +0100503int mbedtls_memory_buffer_alloc_verify( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200504{
505 return verify_chain();
506}
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_MEMORY_DEBUG)
Joris Aertse75b88d2016-11-04 23:05:56 +0100509void mbedtls_memory_buffer_alloc_status( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200510{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200512 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200513 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100514 heap.header_count, heap.total_used,
515 heap.maximum_header_count, heap.maximum_used,
516 heap.maximum_header_count * sizeof( memory_header )
517 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200518 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200519
Paul Bakker6e339b52013-07-03 13:37:05 +0200520 if( heap.first->next == NULL )
Darryl Greenb11de302017-11-27 17:12:14 +0000521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Darryl Greenb11de302017-11-27 17:12:14 +0000523 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200524 else
525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200527 debug_chain();
528 }
529}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100532{
533 *max_used = heap.maximum_used;
534 *max_blocks = heap.maximum_header_count;
535}
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100538{
539 heap.maximum_used = 0;
540 heap.maximum_header_count = 0;
541}
542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100544{
545 *cur_used = heap.total_used;
546 *cur_blocks = heap.header_count;
547}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200551static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200552{
553 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200554 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
555 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200556 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200557 if( mbedtls_mutex_unlock( &heap.mutex ) )
558 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200559 return( buf );
560}
561
562static void buffer_alloc_free_mutexed( void *ptr )
563{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200564 /* We have to good option here, but corrupting the heap seems
565 * worse than loosing memory. */
566 if( mbedtls_mutex_lock( &heap.mutex ) )
567 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200568 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200569 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200572
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200573void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200574{
Andres AG9cf1f962017-01-30 14:34:25 +0000575 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_THREADING_C)
578 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200579 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100580 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200581#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200582 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200583#endif
584
Andres AG9cf1f962017-01-30 14:34:25 +0000585 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
586 return;
587 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200588 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100589 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000591 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000593 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200594 }
595
Andres AG9cf1f962017-01-30 14:34:25 +0000596 memset( buf, 0, len );
597
Paul Bakker6e339b52013-07-03 13:37:05 +0200598 heap.buf = buf;
599 heap.len = len;
600
Andres AG9cf1f962017-01-30 14:34:25 +0000601 heap.first = (memory_header *)buf;
602 heap.first->size = len - sizeof( memory_header );
Paul Bakker6e339b52013-07-03 13:37:05 +0200603 heap.first->magic1 = MAGIC1;
604 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200605 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200606}
607
Joris Aertse75b88d2016-11-04 23:05:56 +0100608void mbedtls_memory_buffer_alloc_free( void )
Paul Bakker1337aff2013-09-29 14:45:34 +0200609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_THREADING_C)
611 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200612#endif
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500613 mbedtls_platform_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200614}
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100617static int check_pointer( void *p )
618{
619 if( p == NULL )
620 return( -1 );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100623 return( -1 );
624
625 return( 0 );
626}
627
Joris Aertse75b88d2016-11-04 23:05:56 +0100628static int check_all_free( void )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100629{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100630 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100632 heap.total_used != 0 ||
633#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100634 heap.first != heap.first_free ||
635 (void *) heap.first != (void *) heap.buf )
636 {
637 return( -1 );
638 }
639
640 return( 0 );
641}
642
643#define TEST_ASSERT( condition ) \
644 if( ! (condition) ) \
645 { \
646 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100648 \
649 ret = 1; \
650 goto cleanup; \
651 }
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100654{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100655 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100656 unsigned char *p, *q, *r, *end;
657 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100658
659 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200664 p = mbedtls_calloc( 1, 1 );
665 q = mbedtls_calloc( 1, 128 );
666 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100667
668 TEST_ASSERT( check_pointer( p ) == 0 &&
669 check_pointer( q ) == 0 &&
670 check_pointer( r ) == 0 );
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_free( r );
673 mbedtls_free( q );
674 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100675
676 TEST_ASSERT( check_all_free( ) == 0 );
677
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100678 /* Memorize end to compare with the next test */
679 end = heap.buf + heap.len;
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100682
683 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100685
686 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100690
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100691 TEST_ASSERT( heap.buf + heap.len == end );
692
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200693 p = mbedtls_calloc( 1, 1 );
694 q = mbedtls_calloc( 1, 128 );
695 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100696
697 TEST_ASSERT( check_pointer( p ) == 0 &&
698 check_pointer( q ) == 0 &&
699 check_pointer( r ) == 0 );
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_free( r );
702 mbedtls_free( q );
703 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100704
705 TEST_ASSERT( check_all_free( ) == 0 );
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100708
709 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100711
712 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_printf( " MBA test #3 (full): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100716
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200717 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100718
719 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200720 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100723
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200724 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
725 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100726
727 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200728 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100731
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200732 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100735
736 TEST_ASSERT( check_all_free( ) == 0 );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100739
740 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100742
743cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100745
746 return( ret );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */