blob: dc8b4bf09f551fb11a3f15dc068f5a0a2b693cb0 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker6e339b52013-07-03 13:37:05 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker6e339b52013-07-03 13:37:05 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
57 is dependent upon MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Rich Evansd08a6052015-02-12 12:17:10 +000059
Paul Bakker6e339b52013-07-03 13:37:05 +020060#include <string.h>
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020063#include <execinfo.h>
64#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020068#endif
69
Paul Bakker34617722014-06-13 17:20:13 +020070/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020072 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
73}
74
Paul Bakker6e339b52013-07-03 13:37:05 +020075#define MAGIC1 0xFF00AA55
76#define MAGIC2 0xEE119966
77#define MAX_BT 20
78
79typedef struct _memory_header memory_header;
80struct _memory_header
81{
82 size_t magic1;
83 size_t size;
84 size_t alloc;
85 memory_header *prev;
86 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020087 memory_header *prev_free;
88 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020090 char **trace;
91 size_t trace_count;
92#endif
93 size_t magic2;
94};
95
96typedef struct
97{
98 unsigned char *buf;
99 size_t len;
100 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200101 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200102 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200104 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200105 size_t free_count;
106 size_t total_used;
107 size_t maximum_used;
108 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100109 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_THREADING_C)
112 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +0200113#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200114}
115buffer_alloc_ctx;
116
117static buffer_alloc_ctx heap;
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +0200120static void debug_header( memory_header *hdr )
121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200123 size_t i;
124#endif
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200127 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100128 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
129 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100131 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_MEMORY_BACKTRACE)
134 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200135 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
137 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200138#endif
139}
140
141static void debug_chain()
142{
143 memory_header *cur = heap.first;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200146 while( cur != NULL )
147 {
148 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200149 cur = cur->next;
150 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200153 cur = heap.first_free;
154
155 while( cur != NULL )
156 {
157 debug_header( cur );
158 cur = cur->next_free;
159 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200162
163static int verify_header( memory_header *hdr )
164{
165 if( hdr->magic1 != MAGIC1 )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_MEMORY_DEBUG)
168 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200169#endif
170 return( 1 );
171 }
172
173 if( hdr->magic2 != MAGIC2 )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_MEMORY_DEBUG)
176 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200177#endif
178 return( 1 );
179 }
180
181 if( hdr->alloc > 1 )
182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_MEMORY_DEBUG)
184 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200185#endif
186 return( 1 );
187 }
188
Paul Bakker1ef120f2013-07-03 17:20:39 +0200189 if( hdr->prev != NULL && hdr->prev == hdr->next )
190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_MEMORY_DEBUG)
192 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200193#endif
194 return( 1 );
195 }
196
197 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_MEMORY_DEBUG)
200 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200201#endif
202 return( 1 );
203 }
204
Paul Bakker6e339b52013-07-03 13:37:05 +0200205 return( 0 );
206}
207
208static int verify_chain()
209{
Andres AG9cf1f962017-01-30 14:34:25 +0000210 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200211
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100212 if( prv == NULL || verify_header( prv ) != 0 )
Paul Bakker6e339b52013-07-03 13:37:05 +0200213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_MEMORY_DEBUG)
215 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100216 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200217#endif
218 return( 1 );
219 }
220
221 if( heap.first->prev != NULL )
222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_MEMORY_DEBUG)
224 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100225 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200226#endif
227 return( 1 );
228 }
229
Andres AG9cf1f962017-01-30 14:34:25 +0000230 cur = heap.first->next;
231
Paul Bakker6e339b52013-07-03 13:37:05 +0200232 while( cur != NULL )
233 {
234 if( verify_header( cur ) != 0 )
235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_MEMORY_DEBUG)
237 mbedtls_fprintf( stderr, "FATAL: verification of header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100238 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200239#endif
240 return( 1 );
241 }
242
243 if( cur->prev != prv )
244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_MEMORY_DEBUG)
246 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100247 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200248#endif
249 return( 1 );
250 }
251
252 prv = cur;
253 cur = cur->next;
254 }
255
256 return( 0 );
257}
258
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200259static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200260{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200261 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200262 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200263 void *ret;
264 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 void *trace_buffer[MAX_BT];
267 size_t trace_cnt;
268#endif
269
270 if( heap.buf == NULL || heap.first == NULL )
271 return( NULL );
272
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200273 original_len = len = n * size;
274
Andres AG9cf1f962017-01-30 14:34:25 +0000275 if( n == 0 || size == 0 || len / n != size )
276 return( NULL );
277 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200278 return( NULL );
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
283 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200284 }
285
286 // Find block that fits
287 //
288 while( cur != NULL )
289 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200290 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200291 break;
292
Paul Bakker1ef120f2013-07-03 17:20:39 +0200293 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200294 }
295
296 if( cur == NULL )
297 return( NULL );
298
Paul Bakker1ef120f2013-07-03 17:20:39 +0200299 if( cur->alloc != 0 )
300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_MEMORY_DEBUG)
302 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100303 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200306 }
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200309 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200310#endif
311
Paul Bakker6e339b52013-07-03 13:37:05 +0200312 // Found location, split block if > memory_header + 4 room left
313 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200314 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200316 {
317 cur->alloc = 1;
318
Paul Bakker1ef120f2013-07-03 17:20:39 +0200319 // Remove from free_list
320 //
321 if( cur->prev_free != NULL )
322 cur->prev_free->next_free = cur->next_free;
323 else
324 heap.first_free = cur->next_free;
325
326 if( cur->next_free != NULL )
327 cur->next_free->prev_free = cur->prev_free;
328
329 cur->prev_free = NULL;
330 cur->next_free = NULL;
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200333 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200334 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200335 heap.maximum_used = heap.total_used;
336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200338 trace_cnt = backtrace( trace_buffer, MAX_BT );
339 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
340 cur->trace_count = trace_cnt;
341#endif
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
344 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200345
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200346 ret = (unsigned char *) cur + sizeof( memory_header );
347 memset( ret, 0, original_len );
348
349 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200350 }
351
352 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
353 new = (memory_header *) p;
354
355 new->size = cur->size - len - sizeof(memory_header);
356 new->alloc = 0;
357 new->prev = cur;
358 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200360 new->trace = NULL;
361 new->trace_count = 0;
362#endif
363 new->magic1 = MAGIC1;
364 new->magic2 = MAGIC2;
365
366 if( new->next != NULL )
367 new->next->prev = new;
368
Paul Bakker1ef120f2013-07-03 17:20:39 +0200369 // Replace cur with new in free_list
370 //
371 new->prev_free = cur->prev_free;
372 new->next_free = cur->next_free;
373 if( new->prev_free != NULL )
374 new->prev_free->next_free = new;
375 else
376 heap.first_free = new;
377
378 if( new->next_free != NULL )
379 new->next_free->prev_free = new;
380
Paul Bakker6e339b52013-07-03 13:37:05 +0200381 cur->alloc = 1;
382 cur->size = len;
383 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200384 cur->prev_free = NULL;
385 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200388 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100389 if( heap.header_count > heap.maximum_header_count )
390 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200391 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200392 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200393 heap.maximum_used = heap.total_used;
394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200396 trace_cnt = backtrace( trace_buffer, MAX_BT );
397 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
398 cur->trace_count = trace_cnt;
399#endif
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
402 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200403
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200404 ret = (unsigned char *) cur + sizeof( memory_header );
405 memset( ret, 0, original_len );
406
407 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200408}
409
410static void buffer_alloc_free( void *ptr )
411{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200412 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200413 unsigned char *p = (unsigned char *) ptr;
414
Paul Bakker6e339b52013-07-03 13:37:05 +0200415 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
416 return;
417
Andres AG9cf1f962017-01-30 14:34:25 +0000418 if( p < heap.buf || p >= heap.buf + heap.len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_MEMORY_DEBUG)
421 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100422 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200425 }
426
427 p -= sizeof(memory_header);
428 hdr = (memory_header *) p;
429
430 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200432
433 if( hdr->alloc != 1 )
434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_MEMORY_DEBUG)
436 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100437 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200440 }
441
442 hdr->alloc = 0;
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200445 heap.free_count++;
446 heap.total_used -= hdr->size;
447#endif
448
SimonB42256112016-05-02 01:05:22 +0100449#if defined(MBEDTLS_MEMORY_BACKTRACE)
450 free( hdr->trace );
451 hdr->trace = NULL;
452 hdr->trace_count = 0;
453#endif
454
Paul Bakker6e339b52013-07-03 13:37:05 +0200455 // Regroup with block before
456 //
457 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200460 heap.header_count--;
461#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200462 hdr->prev->size += sizeof(memory_header) + hdr->size;
463 hdr->prev->next = hdr->next;
464 old = hdr;
465 hdr = hdr->prev;
466
467 if( hdr->next != NULL )
468 hdr->next->prev = hdr;
469
Paul Bakker6e339b52013-07-03 13:37:05 +0200470 memset( old, 0, sizeof(memory_header) );
471 }
472
473 // Regroup with block after
474 //
475 if( hdr->next != NULL && hdr->next->alloc == 0 )
476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200478 heap.header_count--;
479#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200480 hdr->size += sizeof(memory_header) + hdr->next->size;
481 old = hdr->next;
482 hdr->next = hdr->next->next;
483
Paul Bakker1ef120f2013-07-03 17:20:39 +0200484 if( hdr->prev_free != NULL || hdr->next_free != NULL )
485 {
486 if( hdr->prev_free != NULL )
487 hdr->prev_free->next_free = hdr->next_free;
488 else
489 heap.first_free = hdr->next_free;
490
491 if( hdr->next_free != NULL )
492 hdr->next_free->prev_free = hdr->prev_free;
493 }
494
495 hdr->prev_free = old->prev_free;
496 hdr->next_free = old->next_free;
497
498 if( hdr->prev_free != NULL )
499 hdr->prev_free->next_free = hdr;
500 else
501 heap.first_free = hdr;
502
503 if( hdr->next_free != NULL )
504 hdr->next_free->prev_free = hdr;
505
Paul Bakker6e339b52013-07-03 13:37:05 +0200506 if( hdr->next != NULL )
507 hdr->next->prev = hdr;
508
Paul Bakker6e339b52013-07-03 13:37:05 +0200509 memset( old, 0, sizeof(memory_header) );
510 }
511
Paul Bakker1ef120f2013-07-03 17:20:39 +0200512 // Prepend to free_list if we have not merged
513 // (Does not have to stay in same order as prev / next list)
514 //
515 if( old == NULL )
516 {
517 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100518 if( heap.first_free != NULL )
519 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200520 heap.first_free = hdr;
521 }
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
524 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200525}
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200528{
529 heap.verify = verify;
530}
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_memory_buffer_alloc_verify()
Paul Bakker6e339b52013-07-03 13:37:05 +0200533{
534 return verify_chain();
535}
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_MEMORY_DEBUG)
538void mbedtls_memory_buffer_alloc_status()
Paul Bakker6e339b52013-07-03 13:37:05 +0200539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200541 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200542 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100543 heap.header_count, heap.total_used,
544 heap.maximum_header_count, heap.maximum_used,
545 heap.maximum_header_count * sizeof( memory_header )
546 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200547 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200548
Paul Bakker6e339b52013-07-03 13:37:05 +0200549 if( heap.first->next == NULL )
Darryl Green68207f82017-11-27 17:12:14 +0000550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Darryl Green68207f82017-11-27 17:12:14 +0000552 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200553 else
554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200556 debug_chain();
557 }
558}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100561{
562 *max_used = heap.maximum_used;
563 *max_blocks = heap.maximum_header_count;
564}
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100567{
568 heap.maximum_used = 0;
569 heap.maximum_header_count = 0;
570}
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100573{
574 *cur_used = heap.total_used;
575 *cur_blocks = heap.header_count;
576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200580static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200581{
582 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200583 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
584 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200585 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200586 if( mbedtls_mutex_unlock( &heap.mutex ) )
587 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200588 return( buf );
589}
590
591static void buffer_alloc_free_mutexed( void *ptr )
592{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200593 /* We have to good option here, but corrupting the heap seems
594 * worse than loosing memory. */
595 if( mbedtls_mutex_lock( &heap.mutex ) )
596 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200597 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200598 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200601
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200602void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200603{
Andres AG9cf1f962017-01-30 14:34:25 +0000604 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_THREADING_C)
607 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200608 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100609 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200610#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200611 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200612#endif
613
Andres AG9cf1f962017-01-30 14:34:25 +0000614 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
615 return;
616 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200617 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100618 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000620 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000622 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200623 }
624
Andres AG9cf1f962017-01-30 14:34:25 +0000625 memset( buf, 0, len );
626
Paul Bakker6e339b52013-07-03 13:37:05 +0200627 heap.buf = buf;
628 heap.len = len;
629
Andres AG9cf1f962017-01-30 14:34:25 +0000630 heap.first = (memory_header *)buf;
631 heap.first->size = len - sizeof( memory_header );
Paul Bakker6e339b52013-07-03 13:37:05 +0200632 heap.first->magic1 = MAGIC1;
633 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200634 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200635}
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637void mbedtls_memory_buffer_alloc_free()
Paul Bakker1337aff2013-09-29 14:45:34 +0200638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_THREADING_C)
640 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200641#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200643}
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100646static int check_pointer( void *p )
647{
648 if( p == NULL )
649 return( -1 );
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100652 return( -1 );
653
654 return( 0 );
655}
656
657static int check_all_free( )
658{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100659 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100661 heap.total_used != 0 ||
662#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663 heap.first != heap.first_free ||
664 (void *) heap.first != (void *) heap.buf )
665 {
666 return( -1 );
667 }
668
669 return( 0 );
670}
671
672#define TEST_ASSERT( condition ) \
673 if( ! (condition) ) \
674 { \
675 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100677 \
678 ret = 1; \
679 goto cleanup; \
680 }
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100683{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100684 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100685 unsigned char *p, *q, *r, *end;
686 int ret = 0;
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 #1 (basic alloc-free cycle): " );
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, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100692
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é-Gonnard5dd28ea2014-11-27 13:57:42 +0100707 /* Memorize end to compare with the next test */
708 end = heap.buf + heap.len;
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_memory_buffer_alloc_free( );
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( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100714
715 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100719
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100720 TEST_ASSERT( heap.buf + heap.len == end );
721
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200722 p = mbedtls_calloc( 1, 1 );
723 q = mbedtls_calloc( 1, 128 );
724 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100725
726 TEST_ASSERT( check_pointer( p ) == 0 &&
727 check_pointer( q ) == 0 &&
728 check_pointer( r ) == 0 );
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_free( r );
731 mbedtls_free( q );
732 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100733
734 TEST_ASSERT( check_all_free( ) == 0 );
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100737
738 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100740
741 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_printf( " MBA test #3 (full): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100745
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200746 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100747
748 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200749 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100752
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200753 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
754 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100755
756 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200757 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100760
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200761 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100764
765 TEST_ASSERT( check_all_free( ) == 0 );
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100768
769 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100771
772cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100774
775 return( ret );
776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */