blob: 821ae2c708b4b73f18d84a2711f7bbf15f01515b [file] [log] [blame] [raw]
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"
Rich Evansd08a6052015-02-12 12:17:10 +000034
Paul Bakker6e339b52013-07-03 13:37:05 +020035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020038#include <execinfo.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020043#endif
44
Paul Bakker34617722014-06-13 17:20:13 +020045/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020047 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
48}
49
Paul Bakker6e339b52013-07-03 13:37:05 +020050#define MAGIC1 0xFF00AA55
51#define MAGIC2 0xEE119966
52#define MAX_BT 20
53
54typedef struct _memory_header memory_header;
55struct _memory_header
56{
57 size_t magic1;
58 size_t size;
59 size_t alloc;
60 memory_header *prev;
61 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020062 memory_header *prev_free;
63 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020065 char **trace;
66 size_t trace_count;
67#endif
68 size_t magic2;
69};
70
71typedef struct
72{
73 unsigned char *buf;
74 size_t len;
75 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020076 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020077 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020079 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +020080 size_t free_count;
81 size_t total_used;
82 size_t maximum_used;
83 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010084 size_t maximum_header_count;
Paul Bakker1337aff2013-09-29 14:45:34 +020085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_THREADING_C)
87 mbedtls_threading_mutex_t mutex;
Paul Bakker891998e2013-07-03 14:45:05 +020088#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020089}
90buffer_alloc_ctx;
91
92static buffer_alloc_ctx heap;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +020095static void debug_header( memory_header *hdr )
96{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020098 size_t i;
99#endif
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200102 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100103 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
104 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100106 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_MEMORY_BACKTRACE)
109 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200110 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
112 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200113#endif
114}
115
Joris Aertse75b88d2016-11-04 23:05:56 +0100116static void debug_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200117{
118 memory_header *cur = heap.first;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200121 while( cur != NULL )
122 {
123 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200124 cur = cur->next;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200125 }
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200128 cur = heap.first_free;
129
130 while( cur != NULL )
131 {
132 debug_header( cur );
133 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200134 }
135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200137
138static int verify_header( memory_header *hdr )
139{
140 if( hdr->magic1 != MAGIC1 )
141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_MEMORY_DEBUG)
143 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200144#endif
145 return( 1 );
146 }
147
148 if( hdr->magic2 != MAGIC2 )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_MEMORY_DEBUG)
151 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200152#endif
153 return( 1 );
154 }
155
156 if( hdr->alloc > 1 )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_MEMORY_DEBUG)
159 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200160#endif
161 return( 1 );
162 }
163
164 if( hdr->prev != NULL && hdr->prev == hdr->next )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_MEMORY_DEBUG)
167 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200168#endif
169 return( 1 );
170 }
171
172 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_MEMORY_DEBUG)
175 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200176#endif
177 return( 1 );
178 }
179
180 return( 0 );
181}
182
Joris Aertse75b88d2016-11-04 23:05:56 +0100183static int verify_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200184{
Andres AG9cf1f962017-01-30 14:34:25 +0000185 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200186
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100187 if( prv == NULL || verify_header( prv ) != 0 )
Paul Bakker6e339b52013-07-03 13:37:05 +0200188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_MEMORY_DEBUG)
190 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100191 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200192#endif
193 return( 1 );
194 }
195
196 if( heap.first->prev != NULL )
197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_MEMORY_DEBUG)
199 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100200 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200201#endif
202 return( 1 );
203 }
Andres AG9cf1f962017-01-30 14:34:25 +0000204
205 cur = heap.first->next;
Paul Bakker6e339b52013-07-03 13:37:05 +0200206
207 while( cur != NULL )
208 {
209 if( verify_header( cur ) != 0 )
210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_MEMORY_DEBUG)
212 mbedtls_fprintf( stderr, "FATAL: verification of header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100213 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200214#endif
215 return( 1 );
216 }
217
218 if( cur->prev != prv )
219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_MEMORY_DEBUG)
221 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100222 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200223#endif
224 return( 1 );
225 }
226
227 prv = cur;
228 cur = cur->next;
229 }
230
231 return( 0 );
232}
233
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200234static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200235{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200236 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200237 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200238 void *ret;
239 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200241 void *trace_buffer[MAX_BT];
242 size_t trace_cnt;
243#endif
244
245 if( heap.buf == NULL || heap.first == NULL )
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200246 return( NULL );
247
248 original_len = len = n * size;
249
Andres AG9cf1f962017-01-30 14:34:25 +0000250 if( n == 0 || size == 0 || len / n != size )
251 return( NULL );
252 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200253 return( NULL );
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
258 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200259 }
260
261 // Find block that fits
262 //
263 while( cur != NULL )
264 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 break;
267
Paul Bakker1ef120f2013-07-03 17:20:39 +0200268 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200269 }
270
271 if( cur == NULL )
272 return( NULL );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200273
274 if( cur->alloc != 0 )
275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_MEMORY_DEBUG)
277 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100278 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200281 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200284 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200285#endif
286
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 // Found location, split block if > memory_header + 4 room left
288 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200289 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200291 {
292 cur->alloc = 1;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200293
294 // Remove from free_list
295 //
296 if( cur->prev_free != NULL )
297 cur->prev_free->next_free = cur->next_free;
298 else
299 heap.first_free = cur->next_free;
300
301 if( cur->next_free != NULL )
302 cur->next_free->prev_free = cur->prev_free;
303
304 cur->prev_free = NULL;
305 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200308 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200309 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200310 heap.maximum_used = heap.total_used;
311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200313 trace_cnt = backtrace( trace_buffer, MAX_BT );
314 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
315 cur->trace_count = trace_cnt;
316#endif
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
319 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200320
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200321 ret = (unsigned char *) cur + sizeof( memory_header );
322 memset( ret, 0, original_len );
323
324 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200325 }
326
327 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
328 new = (memory_header *) p;
329
330 new->size = cur->size - len - sizeof(memory_header);
331 new->alloc = 0;
332 new->prev = cur;
333 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200335 new->trace = NULL;
336 new->trace_count = 0;
337#endif
338 new->magic1 = MAGIC1;
339 new->magic2 = MAGIC2;
340
341 if( new->next != NULL )
342 new->next->prev = new;
343
Paul Bakker1ef120f2013-07-03 17:20:39 +0200344 // Replace cur with new in free_list
345 //
346 new->prev_free = cur->prev_free;
347 new->next_free = cur->next_free;
348 if( new->prev_free != NULL )
349 new->prev_free->next_free = new;
350 else
351 heap.first_free = new;
352
353 if( new->next_free != NULL )
354 new->next_free->prev_free = new;
355
Paul Bakker6e339b52013-07-03 13:37:05 +0200356 cur->alloc = 1;
357 cur->size = len;
358 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200359 cur->prev_free = NULL;
360 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200363 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100364 if( heap.header_count > heap.maximum_header_count )
365 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200366 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200367 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200368 heap.maximum_used = heap.total_used;
369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200371 trace_cnt = backtrace( trace_buffer, MAX_BT );
372 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
373 cur->trace_count = trace_cnt;
374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
377 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200378
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200379 ret = (unsigned char *) cur + sizeof( memory_header );
380 memset( ret, 0, original_len );
381
382 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200383}
384
385static void buffer_alloc_free( void *ptr )
386{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200387 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200388 unsigned char *p = (unsigned char *) ptr;
389
Paul Bakker6e339b52013-07-03 13:37:05 +0200390 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
391 return;
392
Andres AG9cf1f962017-01-30 14:34:25 +0000393 if( p < heap.buf || p >= heap.buf + heap.len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_MEMORY_DEBUG)
396 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100397 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200400 }
401
402 p -= sizeof(memory_header);
403 hdr = (memory_header *) p;
404
405 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200407
408 if( hdr->alloc != 1 )
409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_MEMORY_DEBUG)
411 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100412 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200415 }
416
417 hdr->alloc = 0;
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200420 heap.free_count++;
421 heap.total_used -= hdr->size;
422#endif
423
SimonB42256112016-05-02 01:05:22 +0100424#if defined(MBEDTLS_MEMORY_BACKTRACE)
425 free( hdr->trace );
426 hdr->trace = NULL;
427 hdr->trace_count = 0;
428#endif
429
Paul Bakker6e339b52013-07-03 13:37:05 +0200430 // Regroup with block before
431 //
432 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200435 heap.header_count--;
436#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200437 hdr->prev->size += sizeof(memory_header) + hdr->size;
438 hdr->prev->next = hdr->next;
439 old = hdr;
440 hdr = hdr->prev;
441
442 if( hdr->next != NULL )
443 hdr->next->prev = hdr;
444
Paul Bakker6e339b52013-07-03 13:37:05 +0200445 memset( old, 0, sizeof(memory_header) );
446 }
447
448 // Regroup with block after
449 //
450 if( hdr->next != NULL && hdr->next->alloc == 0 )
451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200453 heap.header_count--;
454#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200455 hdr->size += sizeof(memory_header) + hdr->next->size;
456 old = hdr->next;
457 hdr->next = hdr->next->next;
458
Paul Bakker1ef120f2013-07-03 17:20:39 +0200459 if( hdr->prev_free != NULL || hdr->next_free != NULL )
460 {
461 if( hdr->prev_free != NULL )
462 hdr->prev_free->next_free = hdr->next_free;
463 else
464 heap.first_free = hdr->next_free;
465
466 if( hdr->next_free != NULL )
467 hdr->next_free->prev_free = hdr->prev_free;
468 }
469
470 hdr->prev_free = old->prev_free;
471 hdr->next_free = old->next_free;
472
473 if( hdr->prev_free != NULL )
474 hdr->prev_free->next_free = hdr;
475 else
476 heap.first_free = hdr;
477
478 if( hdr->next_free != NULL )
479 hdr->next_free->prev_free = hdr;
480
Paul Bakker6e339b52013-07-03 13:37:05 +0200481 if( hdr->next != NULL )
482 hdr->next->prev = hdr;
483
Paul Bakker6e339b52013-07-03 13:37:05 +0200484 memset( old, 0, sizeof(memory_header) );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200485 }
486
487 // Prepend to free_list if we have not merged
488 // (Does not have to stay in same order as prev / next list)
489 //
490 if( old == NULL )
491 {
492 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100493 if( heap.first_free != NULL )
494 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200495 heap.first_free = hdr;
Paul Bakker6e339b52013-07-03 13:37:05 +0200496 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
499 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200500}
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200503{
504 heap.verify = verify;
505}
506
Joris Aertse75b88d2016-11-04 23:05:56 +0100507int mbedtls_memory_buffer_alloc_verify( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200508{
509 return verify_chain();
510}
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_MEMORY_DEBUG)
Joris Aertse75b88d2016-11-04 23:05:56 +0100513void mbedtls_memory_buffer_alloc_status( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200516 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200517 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100518 heap.header_count, heap.total_used,
519 heap.maximum_header_count, heap.maximum_used,
520 heap.maximum_header_count * sizeof( memory_header )
521 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200522 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200523
Paul Bakker6e339b52013-07-03 13:37:05 +0200524 if( heap.first->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200526 else
527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200529 debug_chain();
530 }
531}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100534{
535 *max_used = heap.maximum_used;
536 *max_blocks = heap.maximum_header_count;
537}
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100540{
541 heap.maximum_used = 0;
542 heap.maximum_header_count = 0;
543}
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100546{
547 *cur_used = heap.total_used;
548 *cur_blocks = heap.header_count;
549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200553static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200554{
555 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200556 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
557 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200558 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200559 if( mbedtls_mutex_unlock( &heap.mutex ) )
560 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200561 return( buf );
562}
563
564static void buffer_alloc_free_mutexed( void *ptr )
565{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200566 /* We have to good option here, but corrupting the heap seems
567 * worse than loosing memory. */
568 if( mbedtls_mutex_lock( &heap.mutex ) )
569 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200570 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200571 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200574
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200575void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200576{
Andres AG9cf1f962017-01-30 14:34:25 +0000577 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_THREADING_C)
580 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200581 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100582 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200583#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200584 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200585#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200586
Andres AG9cf1f962017-01-30 14:34:25 +0000587 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
588 return;
589 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200590 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100591 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000593 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000595 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200596 }
Andres AG9cf1f962017-01-30 14:34:25 +0000597
598 memset( buf, 0, len );
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200599
Paul Bakker6e339b52013-07-03 13:37:05 +0200600 heap.buf = buf;
601 heap.len = len;
602
Andres AG9cf1f962017-01-30 14:34:25 +0000603 heap.first = (memory_header *)buf;
604 heap.first->size = len - sizeof( memory_header );
Paul Bakker6e339b52013-07-03 13:37:05 +0200605 heap.first->magic1 = MAGIC1;
606 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200607 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200608}
609
Joris Aertse75b88d2016-11-04 23:05:56 +0100610void mbedtls_memory_buffer_alloc_free( void )
Paul Bakker1337aff2013-09-29 14:45:34 +0200611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#if defined(MBEDTLS_THREADING_C)
613 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100619static int check_pointer( void *p )
620{
621 if( p == NULL )
622 return( -1 );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100625 return( -1 );
626
627 return( 0 );
628}
629
Joris Aertse75b88d2016-11-04 23:05:56 +0100630static int check_all_free( void )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100631{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100632 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100634 heap.total_used != 0 ||
635#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100636 heap.first != heap.first_free ||
637 (void *) heap.first != (void *) heap.buf )
638 {
639 return( -1 );
640 }
641
642 return( 0 );
643}
644
645#define TEST_ASSERT( condition ) \
646 if( ! (condition) ) \
647 { \
648 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100650 \
651 ret = 1; \
652 goto cleanup; \
653 }
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100656{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100657 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100658 unsigned char *p, *q, *r, *end;
659 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100660
661 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100665
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200666 p = mbedtls_calloc( 1, 1 );
667 q = mbedtls_calloc( 1, 128 );
668 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100669
670 TEST_ASSERT( check_pointer( p ) == 0 &&
671 check_pointer( q ) == 0 &&
672 check_pointer( r ) == 0 );
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_free( r );
675 mbedtls_free( q );
676 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100677
678 TEST_ASSERT( check_all_free( ) == 0 );
679
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100680 /* Memorize end to compare with the next test */
681 end = heap.buf + heap.len;
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100684
685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100687
688 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100692
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100693 TEST_ASSERT( heap.buf + heap.len == end );
694
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200695 p = mbedtls_calloc( 1, 1 );
696 q = mbedtls_calloc( 1, 128 );
697 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100698
699 TEST_ASSERT( check_pointer( p ) == 0 &&
700 check_pointer( q ) == 0 &&
701 check_pointer( r ) == 0 );
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_free( r );
704 mbedtls_free( q );
705 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100706
707 TEST_ASSERT( check_all_free( ) == 0 );
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100710
711 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100713
714 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_printf( " MBA test #3 (full): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100718
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200719 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100720
721 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200722 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100725
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200726 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
727 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100728
729 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200730 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100733
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200734 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100737
738 TEST_ASSERT( check_all_free( ) == 0 );
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100741
742 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100744
745cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100747
748 return( ret );
749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */