blob: 19ad31fa1737c0b931849487c03f57e94321881a [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
Paul Bakker6e339b52013-07-03 13:37:05 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6e339b52013-07-03 13:37:05 +02007 *
Paul Bakker6e339b52013-07-03 13:37:05 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
33 is dependent upon MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/platform.h"
Rich Evansd08a6052015-02-12 12:17:10 +000035
Paul Bakker6e339b52013-07-03 13:37:05 +020036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020039#include <execinfo.h>
40#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020044#endif
45
Paul Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020048 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Paul Bakker6e339b52013-07-03 13:37:05 +020051#define MAGIC1 0xFF00AA55
52#define MAGIC2 0xEE119966
53#define MAX_BT 20
54
55typedef struct _memory_header memory_header;
56struct _memory_header
57{
58 size_t magic1;
59 size_t size;
60 size_t alloc;
61 memory_header *prev;
62 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020063 memory_header *prev_free;
64 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020066 char **trace;
67 size_t trace_count;
68#endif
69 size_t magic2;
70};
71
72typedef struct
73{
74 unsigned char *buf;
75 size_t len;
76 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020077 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020078 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020080 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +020081 size_t free_count;
82 size_t total_used;
83 size_t maximum_used;
84 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010085 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_THREADING_C)
88 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +020089#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020090}
91buffer_alloc_ctx;
92
93static buffer_alloc_ctx heap;
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker6e339b52013-07-03 13:37:05 +020096static void debug_header( memory_header *hdr )
97{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020099 size_t i;
100#endif
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200103 "ALLOC(%zu), SIZE(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100104 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
105 hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100107 (size_t) hdr->prev_free, (size_t) hdr->next_free );
Paul Bakker6e339b52013-07-03 13:37:05 +0200108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_MEMORY_BACKTRACE)
110 mbedtls_fprintf( stderr, "TRACE: \n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200111 for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
113 mbedtls_fprintf( stderr, "\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200114#endif
115}
116
117static void debug_chain()
118{
119 memory_header *cur = heap.first;
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_fprintf( stderr, "\nBlock list\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200122 while( cur != NULL )
123 {
124 debug_header( cur );
Paul Bakker6e339b52013-07-03 13:37:05 +0200125 cur = cur->next;
126 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_fprintf( stderr, "Free list\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200129 cur = heap.first_free;
130
131 while( cur != NULL )
132 {
133 debug_header( cur );
134 cur = cur->next_free;
135 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200138
139static int verify_header( memory_header *hdr )
140{
141 if( hdr->magic1 != MAGIC1 )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_MEMORY_DEBUG)
144 mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200145#endif
146 return( 1 );
147 }
148
149 if( hdr->magic2 != MAGIC2 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_MEMORY_DEBUG)
152 mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200153#endif
154 return( 1 );
155 }
156
157 if( hdr->alloc > 1 )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_MEMORY_DEBUG)
160 mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200161#endif
162 return( 1 );
163 }
164
Paul Bakker1ef120f2013-07-03 17:20:39 +0200165 if( hdr->prev != NULL && hdr->prev == hdr->next )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_MEMORY_DEBUG)
168 mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200169#endif
170 return( 1 );
171 }
172
173 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_MEMORY_DEBUG)
176 mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200177#endif
178 return( 1 );
179 }
180
Paul Bakker6e339b52013-07-03 13:37:05 +0200181 return( 0 );
182}
183
184static int verify_chain()
185{
186 memory_header *prv = heap.first, *cur = heap.first->next;
187
188 if( verify_header( heap.first ) != 0 )
189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_MEMORY_DEBUG)
191 mbedtls_fprintf( stderr, "FATAL: verification of first header "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100192 "failed\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200193#endif
194 return( 1 );
195 }
196
197 if( heap.first->prev != NULL )
198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_MEMORY_DEBUG)
200 mbedtls_fprintf( stderr, "FATAL: verification failed: "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100201 "first->prev != NULL\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200202#endif
203 return( 1 );
204 }
205
206 while( cur != NULL )
207 {
208 if( verify_header( cur ) != 0 )
209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_MEMORY_DEBUG)
211 mbedtls_fprintf( stderr, "FATAL: verification of 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( cur->prev != prv )
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 "cur->prev != prv\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200222#endif
223 return( 1 );
224 }
225
226 prv = cur;
227 cur = cur->next;
228 }
229
230 return( 0 );
231}
232
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200233static void *buffer_alloc_calloc( size_t n, size_t size )
Paul Bakker6e339b52013-07-03 13:37:05 +0200234{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200235 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200236 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200237 void *ret;
238 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200240 void *trace_buffer[MAX_BT];
241 size_t trace_cnt;
242#endif
243
244 if( heap.buf == NULL || heap.first == NULL )
245 return( NULL );
246
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200247 original_len = len = n * size;
248
249 if( n != 0 && len / n != size )
250 return( NULL );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
255 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200256 }
257
258 // Find block that fits
259 //
260 while( cur != NULL )
261 {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200262 if( cur->size >= len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200263 break;
264
Paul Bakker1ef120f2013-07-03 17:20:39 +0200265 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200266 }
267
268 if( cur == NULL )
269 return( NULL );
270
Paul Bakker1ef120f2013-07-03 17:20:39 +0200271 if( cur->alloc != 0 )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_MEMORY_DEBUG)
274 mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100275 "data\n" );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_exit( 1 );
Paul Bakker1ef120f2013-07-03 17:20:39 +0200278 }
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200281 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200282#endif
283
Paul Bakker6e339b52013-07-03 13:37:05 +0200284 // Found location, split block if > memory_header + 4 room left
285 //
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200286 if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Paul Bakker6e339b52013-07-03 13:37:05 +0200288 {
289 cur->alloc = 1;
290
Paul Bakker1ef120f2013-07-03 17:20:39 +0200291 // Remove from free_list
292 //
293 if( cur->prev_free != NULL )
294 cur->prev_free->next_free = cur->next_free;
295 else
296 heap.first_free = cur->next_free;
297
298 if( cur->next_free != NULL )
299 cur->next_free->prev_free = cur->prev_free;
300
301 cur->prev_free = NULL;
302 cur->next_free = NULL;
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200305 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200306 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200307 heap.maximum_used = heap.total_used;
308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200310 trace_cnt = backtrace( trace_buffer, MAX_BT );
311 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
312 cur->trace_count = trace_cnt;
313#endif
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
316 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200317
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200318 ret = (unsigned char *) cur + sizeof( memory_header );
319 memset( ret, 0, original_len );
320
321 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200322 }
323
324 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
325 new = (memory_header *) p;
326
327 new->size = cur->size - len - sizeof(memory_header);
328 new->alloc = 0;
329 new->prev = cur;
330 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200332 new->trace = NULL;
333 new->trace_count = 0;
334#endif
335 new->magic1 = MAGIC1;
336 new->magic2 = MAGIC2;
337
338 if( new->next != NULL )
339 new->next->prev = new;
340
Paul Bakker1ef120f2013-07-03 17:20:39 +0200341 // Replace cur with new in free_list
342 //
343 new->prev_free = cur->prev_free;
344 new->next_free = cur->next_free;
345 if( new->prev_free != NULL )
346 new->prev_free->next_free = new;
347 else
348 heap.first_free = new;
349
350 if( new->next_free != NULL )
351 new->next_free->prev_free = new;
352
Paul Bakker6e339b52013-07-03 13:37:05 +0200353 cur->alloc = 1;
354 cur->size = len;
355 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200356 cur->prev_free = NULL;
357 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200360 heap.header_count++;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100361 if( heap.header_count > heap.maximum_header_count )
362 heap.maximum_header_count = heap.header_count;
Paul Bakker891998e2013-07-03 14:45:05 +0200363 heap.total_used += cur->size;
Paul Bakker66d5d072014-06-17 16:39:18 +0200364 if( heap.total_used > heap.maximum_used )
Paul Bakker891998e2013-07-03 14:45:05 +0200365 heap.maximum_used = heap.total_used;
366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200368 trace_cnt = backtrace( trace_buffer, MAX_BT );
369 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
370 cur->trace_count = trace_cnt;
371#endif
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
374 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200375
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200376 ret = (unsigned char *) cur + sizeof( memory_header );
377 memset( ret, 0, original_len );
378
379 return( ret );
Paul Bakker6e339b52013-07-03 13:37:05 +0200380}
381
382static void buffer_alloc_free( void *ptr )
383{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200384 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200385 unsigned char *p = (unsigned char *) ptr;
386
Paul Bakker6e339b52013-07-03 13:37:05 +0200387 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
388 return;
389
390 if( p < heap.buf || p > heap.buf + heap.len )
391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_MEMORY_DEBUG)
393 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100394 "space\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200397 }
398
399 p -= sizeof(memory_header);
400 hdr = (memory_header *) p;
401
402 if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200404
405 if( hdr->alloc != 1 )
406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_MEMORY_DEBUG)
408 mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
Paul Bakker7dc4c442014-02-01 22:50:26 +0100409 "data\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200412 }
413
414 hdr->alloc = 0;
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200417 heap.free_count++;
418 heap.total_used -= hdr->size;
419#endif
420
Paul Bakker6e339b52013-07-03 13:37:05 +0200421 // Regroup with block before
422 //
423 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200426 heap.header_count--;
427#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200428 hdr->prev->size += sizeof(memory_header) + hdr->size;
429 hdr->prev->next = hdr->next;
430 old = hdr;
431 hdr = hdr->prev;
432
433 if( hdr->next != NULL )
434 hdr->next->prev = hdr;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200437 free( old->trace );
438#endif
439 memset( old, 0, sizeof(memory_header) );
440 }
441
442 // Regroup with block after
443 //
444 if( hdr->next != NULL && hdr->next->alloc == 0 )
445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200447 heap.header_count--;
448#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200449 hdr->size += sizeof(memory_header) + hdr->next->size;
450 old = hdr->next;
451 hdr->next = hdr->next->next;
452
Paul Bakker1ef120f2013-07-03 17:20:39 +0200453 if( hdr->prev_free != NULL || hdr->next_free != NULL )
454 {
455 if( hdr->prev_free != NULL )
456 hdr->prev_free->next_free = hdr->next_free;
457 else
458 heap.first_free = hdr->next_free;
459
460 if( hdr->next_free != NULL )
461 hdr->next_free->prev_free = hdr->prev_free;
462 }
463
464 hdr->prev_free = old->prev_free;
465 hdr->next_free = old->next_free;
466
467 if( hdr->prev_free != NULL )
468 hdr->prev_free->next_free = hdr;
469 else
470 heap.first_free = hdr;
471
472 if( hdr->next_free != NULL )
473 hdr->next_free->prev_free = hdr;
474
Paul Bakker6e339b52013-07-03 13:37:05 +0200475 if( hdr->next != NULL )
476 hdr->next->prev = hdr;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200479 free( old->trace );
480#endif
481 memset( old, 0, sizeof(memory_header) );
482 }
483
Paul Bakker1ef120f2013-07-03 17:20:39 +0200484 // Prepend to free_list if we have not merged
485 // (Does not have to stay in same order as prev / next list)
486 //
487 if( old == NULL )
488 {
489 hdr->next_free = heap.first_free;
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100490 if( heap.first_free != NULL )
491 heap.first_free->prev_free = hdr;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200492 heap.first_free = hdr;
493 }
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200496 hdr->trace = NULL;
497 hdr->trace_count = 0;
498#endif
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
501 mbedtls_exit( 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +0200502}
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504void mbedtls_memory_buffer_set_verify( int verify )
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200505{
506 heap.verify = verify;
507}
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_memory_buffer_alloc_verify()
Paul Bakker6e339b52013-07-03 13:37:05 +0200510{
511 return verify_chain();
512}
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_MEMORY_DEBUG)
515void mbedtls_memory_buffer_alloc_status()
Paul Bakker6e339b52013-07-03 13:37:05 +0200516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_fprintf( stderr,
Manuel Pégourié-Gonnard97884a32014-07-12 02:27:35 +0200518 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200519 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
Paul Bakker7dc4c442014-02-01 22:50:26 +0100520 heap.header_count, heap.total_used,
521 heap.maximum_header_count, heap.maximum_used,
522 heap.maximum_header_count * sizeof( memory_header )
523 + heap.maximum_used,
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200524 heap.alloc_count, heap.free_count );
Paul Bakker891998e2013-07-03 14:45:05 +0200525
Paul Bakker6e339b52013-07-03 13:37:05 +0200526 if( heap.first->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200528 else
529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
Paul Bakker6e339b52013-07-03 13:37:05 +0200531 debug_chain();
532 }
533}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100536{
537 *max_used = heap.maximum_used;
538 *max_blocks = heap.maximum_header_count;
539}
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541void mbedtls_memory_buffer_alloc_max_reset( void )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100542{
543 heap.maximum_used = 0;
544 heap.maximum_header_count = 0;
545}
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100548{
549 *cur_used = heap.total_used;
550 *cur_blocks = heap.header_count;
551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200555static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
Paul Bakker1337aff2013-09-29 14:45:34 +0200556{
557 void *buf;
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200558 if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
559 return( NULL );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200560 buf = buffer_alloc_calloc( n, size );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200561 if( mbedtls_mutex_unlock( &heap.mutex ) )
562 return( NULL );
Paul Bakker1337aff2013-09-29 14:45:34 +0200563 return( buf );
564}
565
566static void buffer_alloc_free_mutexed( void *ptr )
567{
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200568 /* We have to good option here, but corrupting the heap seems
569 * worse than loosing memory. */
570 if( mbedtls_mutex_lock( &heap.mutex ) )
571 return;
Paul Bakker1337aff2013-09-29 14:45:34 +0200572 buffer_alloc_free( ptr );
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200573 (void) mbedtls_mutex_unlock( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200576
Manuel Pégourié-Gonnard69a69cc2015-04-29 01:05:19 +0200577void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
Paul Bakker6e339b52013-07-03 13:37:05 +0200578{
Paul Bakker6e339b52013-07-03 13:37:05 +0200579 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
580 memset( buf, 0, len );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_THREADING_C)
583 mbedtls_mutex_init( &heap.mutex );
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200584 mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100585 buffer_alloc_free_mutexed );
Paul Bakker1337aff2013-09-29 14:45:34 +0200586#else
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200587 mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
Paul Bakker1337aff2013-09-29 14:45:34 +0200588#endif
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200591 {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100592 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
594 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
595 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
596 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200597 }
598
Paul Bakker6e339b52013-07-03 13:37:05 +0200599 heap.buf = buf;
600 heap.len = len;
601
602 heap.first = (memory_header *) buf;
603 heap.first->size = len - sizeof(memory_header);
604 heap.first->magic1 = MAGIC1;
605 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200606 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200607}
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609void mbedtls_memory_buffer_alloc_free()
Paul Bakker1337aff2013-09-29 14:45:34 +0200610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_THREADING_C)
612 mbedtls_mutex_free( &heap.mutex );
Paul Bakker1337aff2013-09-29 14:45:34 +0200613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
Paul Bakker1337aff2013-09-29 14:45:34 +0200615}
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100618static int check_pointer( void *p )
619{
620 if( p == NULL )
621 return( -1 );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100624 return( -1 );
625
626 return( 0 );
627}
628
629static int check_all_free( )
630{
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100631 if(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100633 heap.total_used != 0 ||
634#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100635 heap.first != heap.first_free ||
636 (void *) heap.first != (void *) heap.buf )
637 {
638 return( -1 );
639 }
640
641 return( 0 );
642}
643
644#define TEST_ASSERT( condition ) \
645 if( ! (condition) ) \
646 { \
647 if( verbose != 0 ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_printf( "failed\n" ); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100649 \
650 ret = 1; \
651 goto cleanup; \
652 }
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_memory_buffer_alloc_self_test( int verbose )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100655{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100656 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100657 unsigned char *p, *q, *r, *end;
658 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100659
660 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100664
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200665 p = mbedtls_calloc( 1, 1 );
666 q = mbedtls_calloc( 1, 128 );
667 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100668
669 TEST_ASSERT( check_pointer( p ) == 0 &&
670 check_pointer( q ) == 0 &&
671 check_pointer( r ) == 0 );
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_free( r );
674 mbedtls_free( q );
675 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100676
677 TEST_ASSERT( check_all_free( ) == 0 );
678
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100679 /* Memorize end to compare with the next test */
680 end = heap.buf + heap.len;
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_memory_buffer_alloc_free( );
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( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100686
687 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_printf( " MBA test #2 (buf not aligned): " );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100691
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100692 TEST_ASSERT( heap.buf + heap.len == end );
693
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200694 p = mbedtls_calloc( 1, 1 );
695 q = mbedtls_calloc( 1, 128 );
696 r = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100697
698 TEST_ASSERT( check_pointer( p ) == 0 &&
699 check_pointer( q ) == 0 &&
700 check_pointer( r ) == 0 );
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_free( r );
703 mbedtls_free( q );
704 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100705
706 TEST_ASSERT( check_all_free( ) == 0 );
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 #3 (full): " );
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, sizeof( buf ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100717
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200718 p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100719
720 TEST_ASSERT( check_pointer( p ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200721 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100724
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200725 p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
726 q = mbedtls_calloc( 1, 16 );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100727
728 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200729 TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_free( q );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100732
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200733 TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_free( p );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100736
737 TEST_ASSERT( check_all_free( ) == 0 );
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_memory_buffer_alloc_free( );
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( "passed\n" );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100743
744cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_memory_buffer_alloc_free( );
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100746
747 return( ret );
748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */