blob: 915ec3ae9d447bd45dd4114c26601bacb3b5aee9 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
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 * **********
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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050059#include "mbedtls/platform_util.h"
Rich Evansd08a6052015-02-12 12:17:10 +000060
Paul Bakker6e339b52013-07-03 13:37:05 +020061#include <string.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020064#include <execinfo.h>
65#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020069#endif
70
Paul Bakker6e339b52013-07-03 13:37:05 +020071#define MAGIC1 0xFF00AA55
72#define MAGIC2 0xEE119966
73#define MAX_BT 20
74
75typedef struct _memory_header memory_header;
76struct _memory_header
77{
78 size_t magic1;
79 size_t size;
80 size_t alloc;
81 memory_header *prev;
82 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020083 memory_header *prev_free;
84 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020086 char **trace;
87 size_t trace_count;
88#endif
89 size_t magic2;
90};
91
92typedef struct
93{
94 unsigned char *buf;
95 size_t len;
96 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020097 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020098 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200100 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200101 size_t free_count;
102 size_t total_used;
103 size_t maximum_used;
104 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100105 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +0200109#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200110}
111buffer_alloc_ctx;
112
113static buffer_alloc_ctx heap;
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +0200116static void debug_header( memory_header *hdr )
117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200119 size_t i;
120#endif
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200123 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100124 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
125 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100127 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_MEMORY_BACKTRACE)
130 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200131 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
133 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200134#endif
135}
136
Joris Aertse75b88d2016-11-04 23:05:56 +0100137static void debug_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200138{
139 memory_header *cur = heap.first;
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200142 while( cur != NULL )
143 {
144 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200145 cur = cur->next;
146 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200149 cur = heap.first_free;
150
151 while( cur != NULL )
152 {
153 debug_header( cur );
154 cur = cur->next_free;
155 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200158
159static int verify_header( memory_header *hdr )
160{
161 if( hdr->magic1 != MAGIC1 )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_MEMORY_DEBUG)
164 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200165#endif
166 return( 1 );
167 }
168
169 if( hdr->magic2 != MAGIC2 )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_MEMORY_DEBUG)
172 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200173#endif
174 return( 1 );
175 }
176
177 if( hdr->alloc > 1 )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_MEMORY_DEBUG)
180 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200181#endif
182 return( 1 );
183 }
184
Paul Bakker1ef120f2013-07-03 17:20:39 +0200185 if( hdr->prev != NULL && hdr->prev == hdr->next )
186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_MEMORY_DEBUG)
188 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200189#endif
190 return( 1 );
191 }
192
193 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_MEMORY_DEBUG)
196 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200197#endif
198 return( 1 );
199 }
200
Paul Bakker6e339b52013-07-03 13:37:05 +0200201 return( 0 );
202}
203
Joris Aertse75b88d2016-11-04 23:05:56 +0100204static int verify_chain( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200205{
Andres AG9cf1f962017-01-30 14:34:25 +0000206 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200207
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100208 if( prv == NULL || verify_header( prv ) != 0 )
Paul Bakker6e339b52013-07-03 13:37:05 +0200209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_MEMORY_DEBUG)
211 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100212 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200213#endif
214 return( 1 );
215 }
216
217 if( heap.first->prev != NULL )
218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_MEMORY_DEBUG)
220 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100221 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200222#endif
223 return( 1 );
224 }
225
Andres AG9cf1f962017-01-30 14:34:25 +0000226 cur = heap.first->next;
227
Paul Bakker6e339b52013-07-03 13:37:05 +0200228 while( cur != NULL )
229 {
230 if( verify_header( cur ) != 0 )
231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_MEMORY_DEBUG)
233 mbedtls_fprintf( stderr, "FATAL: verification of header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100234 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200235#endif
236 return( 1 );
237 }
238
239 if( cur->prev != prv )
240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_MEMORY_DEBUG)
242 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100243 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200244#endif
245 return( 1 );
246 }
247
248 prv = cur;
249 cur = cur->next;
250 }
251
252 return( 0 );
253}
254
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200255static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200256{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200257 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200258 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200259 void *ret;
260 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200262 void *trace_buffer[MAX_BT];
263 size_t trace_cnt;
264#endif
265
266 if( heap.buf == NULL || heap.first == NULL )
267 return( NULL );
268
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200269 original_len = len = n * size;
270
Andres AG9cf1f962017-01-30 14:34:25 +0000271 if( n == 0 || size == 0 || len / n != size )
272 return( NULL );
273 else if( len > (size_t)-MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200274 return( NULL );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
279 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200280 }
281
282 // Find block that fits
283 //
284 while( cur != NULL )
285 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200286 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 break;
288
Paul Bakker1ef120f2013-07-03 17:20:39 +0200289 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200290 }
291
292 if( cur == NULL )
293 return( NULL );
294
Paul Bakker1ef120f2013-07-03 17:20:39 +0200295 if( cur->alloc != 0 )
296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_MEMORY_DEBUG)
298 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100299 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200302 }
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200305 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200306#endif
307
Paul Bakker6e339b52013-07-03 13:37:05 +0200308 // Found location, split block if > memory_header + 4 room left
309 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200310 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200312 {
313 cur->alloc = 1;
314
Paul Bakker1ef120f2013-07-03 17:20:39 +0200315 // Remove from free_list
316 //
317 if( cur->prev_free != NULL )
318 cur->prev_free->next_free = cur->next_free;
319 else
320 heap.first_free = cur->next_free;
321
322 if( cur->next_free != NULL )
323 cur->next_free->prev_free = cur->prev_free;
324
325 cur->prev_free = NULL;
326 cur->next_free = NULL;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200329 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200330 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200331 heap.maximum_used = heap.total_used;
332#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200334 trace_cnt = backtrace( trace_buffer, MAX_BT );
335 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
336 cur->trace_count = trace_cnt;
337#endif
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
340 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200341
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200342 ret = (unsigned char *) cur + sizeof( memory_header );
343 memset( ret, 0, original_len );
344
345 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200346 }
347
348 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
349 new = (memory_header *) p;
350
351 new->size = cur->size - len - sizeof(memory_header);
352 new->alloc = 0;
353 new->prev = cur;
354 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200356 new->trace = NULL;
357 new->trace_count = 0;
358#endif
359 new->magic1 = MAGIC1;
360 new->magic2 = MAGIC2;
361
362 if( new->next != NULL )
363 new->next->prev = new;
364
Paul Bakker1ef120f2013-07-03 17:20:39 +0200365 // Replace cur with new in free_list
366 //
367 new->prev_free = cur->prev_free;
368 new->next_free = cur->next_free;
369 if( new->prev_free != NULL )
370 new->prev_free->next_free = new;
371 else
372 heap.first_free = new;
373
374 if( new->next_free != NULL )
375 new->next_free->prev_free = new;
376
Paul Bakker6e339b52013-07-03 13:37:05 +0200377 cur->alloc = 1;
378 cur->size = len;
379 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200380 cur->prev_free = NULL;
381 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200384 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100385 if( heap.header_count > heap.maximum_header_count )
386 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200387 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200388 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200389 heap.maximum_used = heap.total_used;
390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200392 trace_cnt = backtrace( trace_buffer, MAX_BT );
393 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
394 cur->trace_count = trace_cnt;
395#endif
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
398 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200399
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200400 ret = (unsigned char *) cur + sizeof( memory_header );
401 memset( ret, 0, original_len );
402
403 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200404}
405
406static void buffer_alloc_free( void *ptr )
407{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200408 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 unsigned char *p = (unsigned char *) ptr;
410
Paul Bakker6e339b52013-07-03 13:37:05 +0200411 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
412 return;
413
Andres AG9cf1f962017-01-30 14:34:25 +0000414 if( p < heap.buf || p >= heap.buf + heap.len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#if defined(MBEDTLS_MEMORY_DEBUG)
417 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100418 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200421 }
422
423 p -= sizeof(memory_header);
424 hdr = (memory_header *) p;
425
426 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200428
429 if( hdr->alloc != 1 )
430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_MEMORY_DEBUG)
432 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100433 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200436 }
437
438 hdr->alloc = 0;
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200441 heap.free_count++;
442 heap.total_used -= hdr->size;
443#endif
444
SimonB42256112016-05-02 01:05:22 +0100445#if defined(MBEDTLS_MEMORY_BACKTRACE)
446 free( hdr->trace );
447 hdr->trace = NULL;
448 hdr->trace_count = 0;
449#endif
450
Paul Bakker6e339b52013-07-03 13:37:05 +0200451 // Regroup with block before
452 //
453 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200456 heap.header_count--;
457#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200458 hdr->prev->size += sizeof(memory_header) + hdr->size;
459 hdr->prev->next = hdr->next;
460 old = hdr;
461 hdr = hdr->prev;
462
463 if( hdr->next != NULL )
464 hdr->next->prev = hdr;
465
Paul Bakker6e339b52013-07-03 13:37:05 +0200466 memset( old, 0, sizeof(memory_header) );
467 }
468
469 // Regroup with block after
470 //
471 if( hdr->next != NULL && hdr->next->alloc == 0 )
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200474 heap.header_count--;
475#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200476 hdr->size += sizeof(memory_header) + hdr->next->size;
477 old = hdr->next;
478 hdr->next = hdr->next->next;
479
Paul Bakker1ef120f2013-07-03 17:20:39 +0200480 if( hdr->prev_free != NULL || hdr->next_free != NULL )
481 {
482 if( hdr->prev_free != NULL )
483 hdr->prev_free->next_free = hdr->next_free;
484 else
485 heap.first_free = hdr->next_free;
486
487 if( hdr->next_free != NULL )
488 hdr->next_free->prev_free = hdr->prev_free;
489 }
490
491 hdr->prev_free = old->prev_free;
492 hdr->next_free = old->next_free;
493
494 if( hdr->prev_free != NULL )
495 hdr->prev_free->next_free = hdr;
496 else
497 heap.first_free = hdr;
498
499 if( hdr->next_free != NULL )
500 hdr->next_free->prev_free = hdr;
501
Paul Bakker6e339b52013-07-03 13:37:05 +0200502 if( hdr->next != NULL )
503 hdr->next->prev = hdr;
504
Paul Bakker6e339b52013-07-03 13:37:05 +0200505 memset( old, 0, sizeof(memory_header) );
506 }
507
Paul Bakker1ef120f2013-07-03 17:20:39 +0200508 // Prepend to free_list if we have not merged
509 // (Does not have to stay in same order as prev / next list)
510 //
511 if( old == NULL )
512 {
513 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100514 if( heap.first_free != NULL )
515 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200516 heap.first_free = hdr;
517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
520 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200521}
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200524{
525 heap.verify = verify;
526}
527
Joris Aertse75b88d2016-11-04 23:05:56 +0100528int mbedtls_memory_buffer_alloc_verify( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200529{
530 return verify_chain();
531}
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_MEMORY_DEBUG)
Joris Aertse75b88d2016-11-04 23:05:56 +0100534void mbedtls_memory_buffer_alloc_status( void )
Paul Bakker6e339b52013-07-03 13:37:05 +0200535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200537 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200538 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100539 heap.header_count, heap.total_used,
540 heap.maximum_header_count, heap.maximum_used,
541 heap.maximum_header_count * sizeof( memory_header )
542 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200543 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200544
Paul Bakker6e339b52013-07-03 13:37:05 +0200545 if( heap.first->next == NULL )
Darryl Greenb11de302017-11-27 17:12:14 +0000546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Darryl Greenb11de302017-11-27 17:12:14 +0000548 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200549 else
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200552 debug_chain();
553 }
554}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100557{
558 *max_used = heap.maximum_used;
559 *max_blocks = heap.maximum_header_count;
560}
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100563{
564 heap.maximum_used = 0;
565 heap.maximum_header_count = 0;
566}
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100569{
570 *cur_used = heap.total_used;
571 *cur_blocks = heap.header_count;
572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200576static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200577{
578 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200579 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
580 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200581 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200582 if( mbedtls_mutex_unlock( &heap.mutex ) )
583 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200584 return( buf );
585}
586
587static void buffer_alloc_free_mutexed( void *ptr )
588{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200589 /* We have to good option here, but corrupting the heap seems
590 * worse than loosing memory. */
591 if( mbedtls_mutex_lock( &heap.mutex ) )
592 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200593 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200594 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200597
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200598void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200599{
Andres AG9cf1f962017-01-30 14:34:25 +0000600 memset( &heap, 0, sizeof( buffer_alloc_ctx ) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_THREADING_C)
603 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200604 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100605 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200606#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200607 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200608#endif
609
Andres AG9cf1f962017-01-30 14:34:25 +0000610 if( len < sizeof( memory_header ) + MBEDTLS_MEMORY_ALIGN_MULTIPLE )
611 return;
612 else if( (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200613 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100614 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000616 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Andres AG9cf1f962017-01-30 14:34:25 +0000618 - (size_t)buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200619 }
620
Andres AG9cf1f962017-01-30 14:34:25 +0000621 memset( buf, 0, len );
622
Paul Bakker6e339b52013-07-03 13:37:05 +0200623 heap.buf = buf;
624 heap.len = len;
625
Andres AG9cf1f962017-01-30 14:34:25 +0000626 heap.first = (memory_header *)buf;
627 heap.first->size = len - sizeof( memory_header );
Paul Bakker6e339b52013-07-03 13:37:05 +0200628 heap.first->magic1 = MAGIC1;
629 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200630 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200631}
632
Joris Aertse75b88d2016-11-04 23:05:56 +0100633void mbedtls_memory_buffer_alloc_free( void )
Paul Bakker1337aff2013-09-29 14:45:34 +0200634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_THREADING_C)
636 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200637#endif
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500638 mbedtls_platform_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200639}
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100642static int check_pointer( void *p )
643{
644 if( p == NULL )
645 return( -1 );
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100648 return( -1 );
649
650 return( 0 );
651}
652
Joris Aertse75b88d2016-11-04 23:05:56 +0100653static int check_all_free( void )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100654{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100655 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100657 heap.total_used != 0 ||
658#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100659 heap.first != heap.first_free ||
660 (void *) heap.first != (void *) heap.buf )
661 {
662 return( -1 );
663 }
664
665 return( 0 );
666}
667
668#define TEST_ASSERT( condition ) \
669 if( ! (condition) ) \
670 { \
671 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100673 \
674 ret = 1; \
675 goto cleanup; \
676 }
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100679{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100680 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100681 unsigned char *p, *q, *r, *end;
682 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100683
684 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100688
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200689 p = mbedtls_calloc( 1, 1 );
690 q = mbedtls_calloc( 1, 128 );
691 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100692
693 TEST_ASSERT( check_pointer( p ) == 0 &&
694 check_pointer( q ) == 0 &&
695 check_pointer( r ) == 0 );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_free( r );
698 mbedtls_free( q );
699 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100700
701 TEST_ASSERT( check_all_free( ) == 0 );
702
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100703 /* Memorize end to compare with the next test */
704 end = heap.buf + heap.len;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100707
708 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_printf( "passed\n" );
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( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100715
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100716 TEST_ASSERT( heap.buf + heap.len == end );
717
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200718 p = mbedtls_calloc( 1, 1 );
719 q = mbedtls_calloc( 1, 128 );
720 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100721
722 TEST_ASSERT( check_pointer( p ) == 0 &&
723 check_pointer( q ) == 0 &&
724 check_pointer( r ) == 0 );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_free( r );
727 mbedtls_free( q );
728 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100729
730 TEST_ASSERT( check_all_free( ) == 0 );
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100733
734 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100736
737 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_printf( " MBA test #3 (full): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100741
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200742 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100743
744 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200745 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100748
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200749 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
750 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100751
752 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200753 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100756
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200757 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100760
761 TEST_ASSERT( check_all_free( ) == 0 );
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100764
765 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100767
768cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100770
771 return( ret );
772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */