blob: e854eea8ee7fd0f59cc12a6ecd4afbe63780c06a [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
Bence Szépkútif744bd72020-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útif744bd72020-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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6e339b52013-07-03 13:37:05 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
59 is dependent upon MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Rich Evansd08a6052015-02-12 12:17:10 +000062
Paul Bakker6e339b52013-07-03 13:37:05 +020063#include <string.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020066#include <execinfo.h>
67#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020071#endif
72
Paul Bakker6e339b52013-07-03 13:37:05 +020073#define MAGIC1 0xFF00AA55
74#define MAGIC2 0xEE119966
75#define MAX_BT 20
76
77typedef struct _memory_header memory_header;
78struct _memory_header
79{
80 size_t magic1;
81 size_t size;
82 size_t alloc;
83 memory_header *prev;
84 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020085 memory_header *prev_free;
86 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020088 char **trace;
89 size_t trace_count;
90#endif
91 size_t magic2;
92};
93
94typedef struct
95{
96 unsigned char *buf;
97 size_t len;
98 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020099 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200100 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200102 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200103 size_t free_count;
104 size_t total_used;
105 size_t maximum_used;
106 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100107 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_THREADING_C)
110 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +0200111#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200112}
113buffer_alloc_ctx;
114
115static buffer_alloc_ctx heap;
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +0200118static void debug_header( memory_header *hdr )
119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200121 size_t i;
122#endif
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200125 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100126 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
127 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100129 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_MEMORY_BACKTRACE)
132 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200133 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
135 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200136#endif
137}
138
Joris Aertse75b88d2016-11-04 23:05:56 +0100139static void debug_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200140{
141 memory_header *cur = heap.first;
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200144 while( cur != NULL )
145 {
146 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200147 cur = cur->next;
148 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200151 cur = heap.first_free;
152
153 while( cur != NULL )
154 {
155 debug_header( cur );
156 cur = cur->next_free;
157 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200160
161static int verify_header( memory_header *hdr )
162{
163 if( hdr->magic1 != MAGIC1 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_MEMORY_DEBUG)
166 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200167#endif
168 return( 1 );
169 }
170
171 if( hdr->magic2 != MAGIC2 )
172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_MEMORY_DEBUG)
174 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200175#endif
176 return( 1 );
177 }
178
179 if( hdr->alloc > 1 )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_MEMORY_DEBUG)
182 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200183#endif
184 return( 1 );
185 }
186
Paul Bakker1ef120f2013-07-03 17:20:39 +0200187 if( hdr->prev != NULL && hdr->prev == hdr->next )
188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_MEMORY_DEBUG)
190 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200191#endif
192 return( 1 );
193 }
194
195 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_MEMORY_DEBUG)
198 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200199#endif
200 return( 1 );
201 }
202
Paul Bakker6e339b52013-07-03 13:37:05 +0200203 return( 0 );
204}
205
Joris Aertse75b88d2016-11-04 23:05:56 +0100206static int verify_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200207{
Andres AG9cf1f962017-01-30 14:34:25 +0000208 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200209
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100210 if( prv == NULL || verify_header( prv ) != 0 )
Paul Bakker6e339b52013-07-03 13:37:05 +0200211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_MEMORY_DEBUG)
213 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100214 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200215#endif
216 return( 1 );
217 }
218
219 if( heap.first->prev != NULL )
220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_MEMORY_DEBUG)
222 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100223 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200224#endif
225 return( 1 );
226 }
227
Andres AG9cf1f962017-01-30 14:34:25 +0000228 cur = heap.first->next;
229
Paul Bakker6e339b52013-07-03 13:37:05 +0200230 while( cur != NULL )
231 {
232 if( verify_header( cur ) != 0 )
233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_MEMORY_DEBUG)
235 mbedtls_fprintf( stderr, "FATAL: verification of header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100236 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200237#endif
238 return( 1 );
239 }
240
241 if( cur->prev != prv )
242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_MEMORY_DEBUG)
244 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100245 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200246#endif
247 return( 1 );
248 }
249
250 prv = cur;
251 cur = cur->next;
252 }
253
254 return( 0 );
255}
256
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200257static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200258{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200259 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200260 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200261 void *ret;
262 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200264 void *trace_buffer[MAX_BT];
265 size_t trace_cnt;
266#endif
267
268 if( heap.buf == NULL || heap.first == NULL )
269 return( NULL );
270
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200271 original_len = len = n * size;
272
Andres AG9cf1f962017-01-30 14:34:25 +0000273 if( n == 0 || size == 0 || len / n != size )
274 return( NULL );
275 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200276 return( NULL );
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
281 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200282 }
283
284 // Find block that fits
285 //
286 while( cur != NULL )
287 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200288 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200289 break;
290
Paul Bakker1ef120f2013-07-03 17:20:39 +0200291 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200292 }
293
294 if( cur == NULL )
295 return( NULL );
296
Paul Bakker1ef120f2013-07-03 17:20:39 +0200297 if( cur->alloc != 0 )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_MEMORY_DEBUG)
300 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100301 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200307 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200308#endif
309
Paul Bakker6e339b52013-07-03 13:37:05 +0200310 // Found location, split block if > memory_header + 4 room left
311 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200312 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200314 {
315 cur->alloc = 1;
316
Paul Bakker1ef120f2013-07-03 17:20:39 +0200317 // Remove from free_list
318 //
319 if( cur->prev_free != NULL )
320 cur->prev_free->next_free = cur->next_free;
321 else
322 heap.first_free = cur->next_free;
323
324 if( cur->next_free != NULL )
325 cur->next_free->prev_free = cur->prev_free;
326
327 cur->prev_free = NULL;
328 cur->next_free = NULL;
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200331 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200332 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200333 heap.maximum_used = heap.total_used;
334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200336 trace_cnt = backtrace( trace_buffer, MAX_BT );
337 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
338 cur->trace_count = trace_cnt;
339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
342 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200343
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200344 ret = (unsigned char *) cur + sizeof( memory_header );
345 memset( ret, 0, original_len );
346
347 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200348 }
349
350 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
351 new = (memory_header *) p;
352
353 new->size = cur->size - len - sizeof(memory_header);
354 new->alloc = 0;
355 new->prev = cur;
356 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200358 new->trace = NULL;
359 new->trace_count = 0;
360#endif
361 new->magic1 = MAGIC1;
362 new->magic2 = MAGIC2;
363
364 if( new->next != NULL )
365 new->next->prev = new;
366
Paul Bakker1ef120f2013-07-03 17:20:39 +0200367 // Replace cur with new in free_list
368 //
369 new->prev_free = cur->prev_free;
370 new->next_free = cur->next_free;
371 if( new->prev_free != NULL )
372 new->prev_free->next_free = new;
373 else
374 heap.first_free = new;
375
376 if( new->next_free != NULL )
377 new->next_free->prev_free = new;
378
Paul Bakker6e339b52013-07-03 13:37:05 +0200379 cur->alloc = 1;
380 cur->size = len;
381 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200382 cur->prev_free = NULL;
383 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200386 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100387 if( heap.header_count > heap.maximum_header_count )
388 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200389 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200390 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200391 heap.maximum_used = heap.total_used;
392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200394 trace_cnt = backtrace( trace_buffer, MAX_BT );
395 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
396 cur->trace_count = trace_cnt;
397#endif
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
400 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200401
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200402 ret = (unsigned char *) cur + sizeof( memory_header );
403 memset( ret, 0, original_len );
404
405 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200406}
407
408static void buffer_alloc_free( void *ptr )
409{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200410 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200411 unsigned char *p = (unsigned char *) ptr;
412
Paul Bakker6e339b52013-07-03 13:37:05 +0200413 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
414 return;
415
Andres AG9cf1f962017-01-30 14:34:25 +0000416 if( p < heap.buf || p >= heap.buf + heap.len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_MEMORY_DEBUG)
419 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100420 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200423 }
424
425 p -= sizeof(memory_header);
426 hdr = (memory_header *) p;
427
428 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200430
431 if( hdr->alloc != 1 )
432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_MEMORY_DEBUG)
434 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100435 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200438 }
439
440 hdr->alloc = 0;
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200443 heap.free_count++;
444 heap.total_used -= hdr->size;
445#endif
446
SimonB42256112016-05-02 01:05:22 +0100447#if defined(MBEDTLS_MEMORY_BACKTRACE)
448 free( hdr->trace );
449 hdr->trace = NULL;
450 hdr->trace_count = 0;
451#endif
452
Paul Bakker6e339b52013-07-03 13:37:05 +0200453 // Regroup with block before
454 //
455 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200458 heap.header_count--;
459#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200460 hdr->prev->size += sizeof(memory_header) + hdr->size;
461 hdr->prev->next = hdr->next;
462 old = hdr;
463 hdr = hdr->prev;
464
465 if( hdr->next != NULL )
466 hdr->next->prev = hdr;
467
Paul Bakker6e339b52013-07-03 13:37:05 +0200468 memset( old, 0, sizeof(memory_header) );
469 }
470
471 // Regroup with block after
472 //
473 if( hdr->next != NULL && hdr->next->alloc == 0 )
474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200476 heap.header_count--;
477#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200478 hdr->size += sizeof(memory_header) + hdr->next->size;
479 old = hdr->next;
480 hdr->next = hdr->next->next;
481
Paul Bakker1ef120f2013-07-03 17:20:39 +0200482 if( hdr->prev_free != NULL || hdr->next_free != NULL )
483 {
484 if( hdr->prev_free != NULL )
485 hdr->prev_free->next_free = hdr->next_free;
486 else
487 heap.first_free = hdr->next_free;
488
489 if( hdr->next_free != NULL )
490 hdr->next_free->prev_free = hdr->prev_free;
491 }
492
493 hdr->prev_free = old->prev_free;
494 hdr->next_free = old->next_free;
495
496 if( hdr->prev_free != NULL )
497 hdr->prev_free->next_free = hdr;
498 else
499 heap.first_free = hdr;
500
501 if( hdr->next_free != NULL )
502 hdr->next_free->prev_free = hdr;
503
Paul Bakker6e339b52013-07-03 13:37:05 +0200504 if( hdr->next != NULL )
505 hdr->next->prev = hdr;
506
Paul Bakker6e339b52013-07-03 13:37:05 +0200507 memset( old, 0, sizeof(memory_header) );
508 }
509
Paul Bakker1ef120f2013-07-03 17:20:39 +0200510 // Prepend to free_list if we have not merged
511 // (Does not have to stay in same order as prev / next list)
512 //
513 if( old == NULL )
514 {
515 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100516 if( heap.first_free != NULL )
517 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200518 heap.first_free = hdr;
519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
522 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200523}
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200526{
527 heap.verify = verify;
528}
529
Joris Aertse75b88d2016-11-04 23:05:56 +0100530int mbedtls_memory_buffer_alloc_verify( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200531{
532 return verify_chain();
533}
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_MEMORY_DEBUG)
Joris Aertse75b88d2016-11-04 23:05:56 +0100536void mbedtls_memory_buffer_alloc_status( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200539 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200540 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100541 heap.header_count, heap.total_used,
542 heap.maximum_header_count, heap.maximum_used,
543 heap.maximum_header_count * sizeof( memory_header )
544 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200545 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200546
Paul Bakker6e339b52013-07-03 13:37:05 +0200547 if( heap.first->next == NULL )
Darryl Greenb11de302017-11-27 17:12:14 +0000548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Darryl Greenb11de302017-11-27 17:12:14 +0000550 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200551 else
552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200554 debug_chain();
555 }
556}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100559{
560 *max_used = heap.maximum_used;
561 *max_blocks = heap.maximum_header_count;
562}
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100565{
566 heap.maximum_used = 0;
567 heap.maximum_header_count = 0;
568}
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100571{
572 *cur_used = heap.total_used;
573 *cur_blocks = heap.header_count;
574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200578static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200579{
580 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200581 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
582 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200583 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200584 if( mbedtls_mutex_unlock( &heap.mutex ) )
585 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200586 return( buf );
587}
588
589static void buffer_alloc_free_mutexed( void *ptr )
590{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200591 /* We have to good option here, but corrupting the heap seems
592 * worse than loosing memory. */
593 if( mbedtls_mutex_lock( &heap.mutex ) )
594 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200595 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200596 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200599
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200600void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200601{
Andres AG9cf1f962017-01-30 14:34:25 +0000602 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if defined(MBEDTLS_THREADING_C)
605 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200606 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100607 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200608#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200609 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200610#endif
611
Andres AG9cf1f962017-01-30 14:34:25 +0000612 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
613 return;
614 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200615 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100616 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000618 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000620 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200621 }
622
Andres AG9cf1f962017-01-30 14:34:25 +0000623 memset( buf, 0, len );
624
Paul Bakker6e339b52013-07-03 13:37:05 +0200625 heap.buf = buf;
626 heap.len = len;
627
Andres AG9cf1f962017-01-30 14:34:25 +0000628 heap.first = (memory_header *)buf;
629 heap.first->size = len - sizeof( memory_header );
Paul Bakker6e339b52013-07-03 13:37:05 +0200630 heap.first->magic1 = MAGIC1;
631 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200632 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200633}
634
Joris Aertse75b88d2016-11-04 23:05:56 +0100635void mbedtls_memory_buffer_alloc_free( void )
Paul Bakker1337aff2013-09-29 14:45:34 +0200636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_THREADING_C)
638 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200639#endif
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500640 mbedtls_platform_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200641}
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100644static int check_pointer( void *p )
645{
646 if( p == NULL )
647 return( -1 );
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100650 return( -1 );
651
652 return( 0 );
653}
654
Joris Aertse75b88d2016-11-04 23:05:56 +0100655static int check_all_free( void )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100656{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100657 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100659 heap.total_used != 0 ||
660#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100661 heap.first != heap.first_free ||
662 (void *) heap.first != (void *) heap.buf )
663 {
664 return( -1 );
665 }
666
667 return( 0 );
668}
669
670#define TEST_ASSERT( condition ) \
671 if( ! (condition) ) \
672 { \
673 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100675 \
676 ret = 1; \
677 goto cleanup; \
678 }
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100681{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100682 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100683 unsigned char *p, *q, *r, *end;
684 int ret = 0;
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 #1 (basic alloc-free cycle): " );
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, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100690
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200691 p = mbedtls_calloc( 1, 1 );
692 q = mbedtls_calloc( 1, 128 );
693 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100694
695 TEST_ASSERT( check_pointer( p ) == 0 &&
696 check_pointer( q ) == 0 &&
697 check_pointer( r ) == 0 );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_free( r );
700 mbedtls_free( q );
701 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100702
703 TEST_ASSERT( check_all_free( ) == 0 );
704
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100705 /* Memorize end to compare with the next test */
706 end = heap.buf + heap.len;
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100709
710 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100712
713 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100717
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100718 TEST_ASSERT( heap.buf + heap.len == end );
719
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200720 p = mbedtls_calloc( 1, 1 );
721 q = mbedtls_calloc( 1, 128 );
722 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100723
724 TEST_ASSERT( check_pointer( p ) == 0 &&
725 check_pointer( q ) == 0 &&
726 check_pointer( r ) == 0 );
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_free( r );
729 mbedtls_free( q );
730 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100731
732 TEST_ASSERT( check_all_free( ) == 0 );
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100735
736 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100738
739 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_printf( " MBA test #3 (full): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100743
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200744 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100745
746 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200747 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100750
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200751 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
752 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100753
754 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200755 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100758
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200759 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100762
763 TEST_ASSERT( check_all_free( ) == 0 );
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100766
767 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100769
770cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100772
773 return( ret );
774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */