memory_buffer_alloc.c 19 KB
Newer Older
1 2 3
/*
 *  Buffer-based memory allocator
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
29
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
30
#include "mbedtls/memory_buffer_alloc.h"
31

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
32 33
/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
   is dependent upon MBEDTLS_PLATFORM_C */
34
#include "mbedtls/platform.h"
35

36 37
#include <string.h>

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
38
#if defined(MBEDTLS_MEMORY_BACKTRACE)
39 40 41
#include <execinfo.h>
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
42
#if defined(MBEDTLS_THREADING_C)
43
#include "mbedtls/threading.h"
44 45
#endif

46
/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
47
static void mbedtls_zeroize( void *v, size_t n ) {
48 49 50
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

51 52 53 54 55 56 57 58 59 60 61 62
#define MAGIC1       0xFF00AA55
#define MAGIC2       0xEE119966
#define MAX_BT 20

typedef struct _memory_header memory_header;
struct _memory_header
{
    size_t          magic1;
    size_t          size;
    size_t          alloc;
    memory_header   *prev;
    memory_header   *next;
63 64
    memory_header   *prev_free;
    memory_header   *next_free;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
65
#if defined(MBEDTLS_MEMORY_BACKTRACE)
66 67 68 69 70 71 72 73 74 75 76
    char            **trace;
    size_t          trace_count;
#endif
    size_t          magic2;
};

typedef struct
{
    unsigned char   *buf;
    size_t          len;
    memory_header   *first;
77
    memory_header   *first_free;
78
    int             verify;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
79
#if defined(MBEDTLS_MEMORY_DEBUG)
80
    size_t          alloc_count;
81 82 83 84
    size_t          free_count;
    size_t          total_used;
    size_t          maximum_used;
    size_t          header_count;
85
    size_t          maximum_header_count;
86
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
87 88
#if defined(MBEDTLS_THREADING_C)
    mbedtls_threading_mutex_t   mutex;
89
#endif
90 91 92 93 94
}
buffer_alloc_ctx;

static buffer_alloc_ctx heap;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
95
#if defined(MBEDTLS_MEMORY_DEBUG)
96 97
static void debug_header( memory_header *hdr )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
98
#if defined(MBEDTLS_MEMORY_BACKTRACE)
99 100 101
    size_t i;
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
102
    mbedtls_fprintf( stderr, "HDR:  PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
103
                              "ALLOC(%zu), SIZE(%10zu)\n",
104 105
                      (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
                      hdr->alloc, hdr->size );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
106
    mbedtls_fprintf( stderr, "      FPREV(%10zu), FNEXT(%10zu)\n",
107
                      (size_t) hdr->prev_free, (size_t) hdr->next_free );
108

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
109 110
#if defined(MBEDTLS_MEMORY_BACKTRACE)
    mbedtls_fprintf( stderr, "TRACE: \n" );
111
    for( i = 0; i < hdr->trace_count; i++ )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
112 113
        mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
    mbedtls_fprintf( stderr, "\n" );
114 115 116 117 118 119 120
#endif
}

static void debug_chain()
{
    memory_header *cur = heap.first;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
121
    mbedtls_fprintf( stderr, "\nBlock list\n" );
122 123 124 125 126
    while( cur != NULL )
    {
        debug_header( cur );
        cur = cur->next;
    }
127

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
128
    mbedtls_fprintf( stderr, "Free list\n" );
129 130 131 132 133 134 135
    cur = heap.first_free;

    while( cur != NULL )
    {
        debug_header( cur );
        cur = cur->next_free;
    }
136
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
137
#endif /* MBEDTLS_MEMORY_DEBUG */
138 139 140 141 142

static int verify_header( memory_header *hdr )
{
    if( hdr->magic1 != MAGIC1 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
143 144
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
145 146 147 148 149 150
#endif
        return( 1 );
    }

    if( hdr->magic2 != MAGIC2 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
151 152
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
153 154 155 156 157 158
#endif
        return( 1 );
    }

    if( hdr->alloc > 1 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
159 160
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
161 162 163 164
#endif
        return( 1 );
    }

165 166
    if( hdr->prev != NULL && hdr->prev == hdr->next )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
167 168
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
169 170 171 172 173 174
#endif
        return( 1 );
    }

    if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
175 176
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
177 178 179 180
#endif
        return( 1 );
    }

181 182 183 184 185 186 187 188 189
    return( 0 );
}

static int verify_chain()
{
    memory_header *prv = heap.first, *cur = heap.first->next;

    if( verify_header( heap.first ) != 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
190 191
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: verification of first header "
192
                                  "failed\n" );
193 194 195 196 197 198
#endif
        return( 1 );
    }

    if( heap.first->prev != NULL )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
199 200
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: verification failed: "
201
                                  "first->prev != NULL\n" );
202 203 204 205 206 207 208 209
#endif
        return( 1 );
    }

    while( cur != NULL )
    {
        if( verify_header( cur ) != 0 )
        {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
210 211
#if defined(MBEDTLS_MEMORY_DEBUG)
            mbedtls_fprintf( stderr, "FATAL: verification of header "
212
                                      "failed\n" );
213 214 215 216 217 218
#endif
            return( 1 );
        }

        if( cur->prev != prv )
        {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
219 220
#if defined(MBEDTLS_MEMORY_DEBUG)
            mbedtls_fprintf( stderr, "FATAL: verification failed: "
221
                                      "cur->prev != prv\n" );
222 223 224 225 226 227 228 229 230 231 232
#endif
            return( 1 );
        }

        prv = cur;
        cur = cur->next;
    }

    return( 0 );
}

233
static void *buffer_alloc_calloc( size_t n, size_t size )
234
{
235
    memory_header *new, *cur = heap.first_free;
236
    unsigned char *p;
237 238
    void *ret;
    size_t original_len, len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
239
#if defined(MBEDTLS_MEMORY_BACKTRACE)
240 241 242 243 244 245 246
    void *trace_buffer[MAX_BT];
    size_t trace_cnt;
#endif

    if( heap.buf == NULL || heap.first == NULL )
        return( NULL );

247 248 249 250 251
    original_len = len = n * size;

    if( n != 0 && len / n != size )
        return( NULL );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
252
    if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
253
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
254 255
        len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
        len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
256 257 258 259 260 261
    }

    // Find block that fits
    //
    while( cur != NULL )
    {
262
        if( cur->size >= len )
263 264
            break;

265
        cur = cur->next_free;
266 267 268 269 270
    }

    if( cur == NULL )
        return( NULL );

271 272
    if( cur->alloc != 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
273 274
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
275
                                  "data\n" );
276
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
277
        mbedtls_exit( 1 );
278 279
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
280
#if defined(MBEDTLS_MEMORY_DEBUG)
281
    heap.alloc_count++;
282 283
#endif

284 285
    // Found location, split block if > memory_header + 4 room left
    //
286
    if( cur->size - len < sizeof(memory_header) +
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
287
                          MBEDTLS_MEMORY_ALIGN_MULTIPLE )
288 289 290
    {
        cur->alloc = 1;

291 292 293 294 295 296 297 298 299 300 301 302 303
        // Remove from free_list
        //
        if( cur->prev_free != NULL )
            cur->prev_free->next_free = cur->next_free;
        else
            heap.first_free = cur->next_free;

        if( cur->next_free != NULL )
            cur->next_free->prev_free = cur->prev_free;

        cur->prev_free = NULL;
        cur->next_free = NULL;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
304
#if defined(MBEDTLS_MEMORY_DEBUG)
305
        heap.total_used += cur->size;
306
        if( heap.total_used > heap.maximum_used )
307 308
            heap.maximum_used = heap.total_used;
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
309
#if defined(MBEDTLS_MEMORY_BACKTRACE)
310 311 312 313 314
        trace_cnt = backtrace( trace_buffer, MAX_BT );
        cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
        cur->trace_count = trace_cnt;
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
315 316
        if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
            mbedtls_exit( 1 );
317

318 319 320 321
        ret = (unsigned char *) cur + sizeof( memory_header );
        memset( ret, 0, original_len );

        return( ret );
322 323 324 325 326 327 328 329 330
    }

    p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
    new = (memory_header *) p;

    new->size = cur->size - len - sizeof(memory_header);
    new->alloc = 0;
    new->prev = cur;
    new->next = cur->next;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
331
#if defined(MBEDTLS_MEMORY_BACKTRACE)
332 333 334 335 336 337 338 339 340
    new->trace = NULL;
    new->trace_count = 0;
#endif
    new->magic1 = MAGIC1;
    new->magic2 = MAGIC2;

    if( new->next != NULL )
        new->next->prev = new;

341 342 343 344 345 346 347 348 349 350 351 352
    // Replace cur with new in free_list
    //
    new->prev_free = cur->prev_free;
    new->next_free = cur->next_free;
    if( new->prev_free != NULL )
        new->prev_free->next_free = new;
    else
        heap.first_free = new;

    if( new->next_free != NULL )
        new->next_free->prev_free = new;

353 354 355
    cur->alloc = 1;
    cur->size = len;
    cur->next = new;
356 357
    cur->prev_free = NULL;
    cur->next_free = NULL;
358

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
359
#if defined(MBEDTLS_MEMORY_DEBUG)
360
    heap.header_count++;
361 362
    if( heap.header_count > heap.maximum_header_count )
        heap.maximum_header_count = heap.header_count;
363
    heap.total_used += cur->size;
364
    if( heap.total_used > heap.maximum_used )
365 366
        heap.maximum_used = heap.total_used;
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
367
#if defined(MBEDTLS_MEMORY_BACKTRACE)
368 369 370 371 372
    trace_cnt = backtrace( trace_buffer, MAX_BT );
    cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
    cur->trace_count = trace_cnt;
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
373 374
    if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
        mbedtls_exit( 1 );
375

376 377 378 379
    ret = (unsigned char *) cur + sizeof( memory_header );
    memset( ret, 0, original_len );

    return( ret );
380 381 382 383
}

static void buffer_alloc_free( void *ptr )
{
384
    memory_header *hdr, *old = NULL;
385 386 387 388 389 390 391
    unsigned char *p = (unsigned char *) ptr;

    if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
        return;

    if( p < heap.buf || p > heap.buf + heap.len )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
392 393
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
394
                                  "space\n" );
395
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
396
        mbedtls_exit( 1 );
397 398 399 400 401 402
    }

    p -= sizeof(memory_header);
    hdr = (memory_header *) p;

    if( verify_header( hdr ) != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
403
        mbedtls_exit( 1 );
404 405 406

    if( hdr->alloc != 1 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
407 408
#if defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
409
                                  "data\n" );
410
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
411
        mbedtls_exit( 1 );
412 413 414 415
    }

    hdr->alloc = 0;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
416
#if defined(MBEDTLS_MEMORY_DEBUG)
417 418 419 420
    heap.free_count++;
    heap.total_used -= hdr->size;
#endif

421 422 423 424
    // Regroup with block before
    //
    if( hdr->prev != NULL && hdr->prev->alloc == 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
425
#if defined(MBEDTLS_MEMORY_DEBUG)
426 427
        heap.header_count--;
#endif
428 429 430 431 432 433 434 435
        hdr->prev->size += sizeof(memory_header) + hdr->size;
        hdr->prev->next = hdr->next;
        old = hdr;
        hdr = hdr->prev;

        if( hdr->next != NULL )
            hdr->next->prev = hdr;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
436
#if defined(MBEDTLS_MEMORY_BACKTRACE)
437 438 439 440 441 442 443 444 445
        free( old->trace );
#endif
        memset( old, 0, sizeof(memory_header) );
    }

    // Regroup with block after
    //
    if( hdr->next != NULL && hdr->next->alloc == 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
446
#if defined(MBEDTLS_MEMORY_DEBUG)
447 448
        heap.header_count--;
#endif
449 450 451 452
        hdr->size += sizeof(memory_header) + hdr->next->size;
        old = hdr->next;
        hdr->next = hdr->next->next;

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        if( hdr->prev_free != NULL || hdr->next_free != NULL )
        {
            if( hdr->prev_free != NULL )
                hdr->prev_free->next_free = hdr->next_free;
            else
                heap.first_free = hdr->next_free;

            if( hdr->next_free != NULL )
                hdr->next_free->prev_free = hdr->prev_free;
        }

        hdr->prev_free = old->prev_free;
        hdr->next_free = old->next_free;

        if( hdr->prev_free != NULL )
            hdr->prev_free->next_free = hdr;
        else
            heap.first_free = hdr;

        if( hdr->next_free != NULL )
            hdr->next_free->prev_free = hdr;

475 476 477
        if( hdr->next != NULL )
            hdr->next->prev = hdr;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
478
#if defined(MBEDTLS_MEMORY_BACKTRACE)
479 480 481 482 483
        free( old->trace );
#endif
        memset( old, 0, sizeof(memory_header) );
    }

484 485 486 487 488 489
    // Prepend to free_list if we have not merged
    // (Does not have to stay in same order as prev / next list)
    //
    if( old == NULL )
    {
        hdr->next_free = heap.first_free;
490 491
        if( heap.first_free != NULL )
            heap.first_free->prev_free = hdr;
492 493 494
        heap.first_free = hdr;
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
495
#if defined(MBEDTLS_MEMORY_BACKTRACE)
496 497 498 499
    hdr->trace = NULL;
    hdr->trace_count = 0;
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
500 501
    if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
        mbedtls_exit( 1 );
502 503
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
504
void mbedtls_memory_buffer_set_verify( int verify )
505 506 507 508
{
    heap.verify = verify;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
509
int mbedtls_memory_buffer_alloc_verify()
510 511 512 513
{
    return verify_chain();
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
514 515
#if defined(MBEDTLS_MEMORY_DEBUG)
void mbedtls_memory_buffer_alloc_status()
516
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
517
    mbedtls_fprintf( stderr,
518
                      "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
519
                      "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
520 521 522 523
                      heap.header_count, heap.total_used,
                      heap.maximum_header_count, heap.maximum_used,
                      heap.maximum_header_count * sizeof( memory_header )
                      + heap.maximum_used,
524
                      heap.alloc_count, heap.free_count );
525

526
    if( heap.first->next == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
527
        mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
528 529
    else
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
530
        mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
531 532 533
        debug_chain();
    }
}
534

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
535
void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
536 537 538 539 540
{
    *max_used   = heap.maximum_used;
    *max_blocks = heap.maximum_header_count;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
541
void mbedtls_memory_buffer_alloc_max_reset( void )
542 543 544 545 546
{
    heap.maximum_used = 0;
    heap.maximum_header_count = 0;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
547
void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
548 549 550 551
{
    *cur_used   = heap.total_used;
    *cur_blocks = heap.header_count;
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
552
#endif /* MBEDTLS_MEMORY_DEBUG */
553

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
554
#if defined(MBEDTLS_THREADING_C)
555
static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
556
{
557
    void *buf;
558 559
    if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
        return( NULL );
560
    buf = buffer_alloc_calloc( n, size );
561 562
    if( mbedtls_mutex_unlock( &heap.mutex ) )
        return( NULL );
563 564 565 566 567
    return( buf );
}

static void buffer_alloc_free_mutexed( void *ptr )
{
568 569 570 571
    /* We have to good option here, but corrupting the heap seems
     * worse than loosing memory. */
    if( mbedtls_mutex_lock( &heap.mutex ) )
        return;
572
    buffer_alloc_free( ptr );
573
    (void) mbedtls_mutex_unlock( &heap.mutex );
574
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
575
#endif /* MBEDTLS_THREADING_C */
576

577
void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
578
{
579 580 581
    memset( &heap, 0, sizeof(buffer_alloc_ctx) );
    memset( buf, 0, len );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
582 583
#if defined(MBEDTLS_THREADING_C)
    mbedtls_mutex_init( &heap.mutex );
584
    mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
585
                              buffer_alloc_free_mutexed );
586
#else
587
    mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
588 589
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
590
    if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
591
    {
592
        /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
593 594 595 596
        len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
             - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
        buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
             - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
597 598
    }

599 600 601 602 603 604 605
    heap.buf = buf;
    heap.len = len;

    heap.first = (memory_header *) buf;
    heap.first->size = len - sizeof(memory_header);
    heap.first->magic1 = MAGIC1;
    heap.first->magic2 = MAGIC2;
606
    heap.first_free = heap.first;
607 608
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
609
void mbedtls_memory_buffer_alloc_free()
610
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
611 612
#if defined(MBEDTLS_THREADING_C)
    mbedtls_mutex_free( &heap.mutex );
613
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
614
    mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
615 616
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
617
#if defined(MBEDTLS_SELF_TEST)
618 619 620 621 622
static int check_pointer( void *p )
{
    if( p == NULL )
        return( -1 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
623
    if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
624 625 626 627 628 629 630
        return( -1 );

    return( 0 );
}

static int check_all_free( )
{
631
    if(
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
632
#if defined(MBEDTLS_MEMORY_DEBUG)
633 634
        heap.total_used != 0 ||
#endif
635 636 637 638 639 640 641 642 643 644 645 646 647
        heap.first != heap.first_free ||
        (void *) heap.first != (void *) heap.buf )
    {
        return( -1 );
    }

    return( 0 );
}

#define TEST_ASSERT( condition )            \
    if( ! (condition) )                     \
    {                                       \
        if( verbose != 0 )                  \
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
648
            mbedtls_printf( "failed\n" );  \
649 650 651 652 653
                                            \
        ret = 1;                            \
        goto cleanup;                       \
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
654
int mbedtls_memory_buffer_alloc_self_test( int verbose )
655 656
{
    unsigned char buf[1024];
657 658
    unsigned char *p, *q, *r, *end;
    int ret = 0;
659 660

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
661
        mbedtls_printf( "  MBA test #1 (basic alloc-free cycle): " );
662

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
663
    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
664

665 666 667
    p = mbedtls_calloc( 1, 1 );
    q = mbedtls_calloc( 1, 128 );
    r = mbedtls_calloc( 1, 16 );
668 669 670 671 672

    TEST_ASSERT( check_pointer( p ) == 0 &&
                 check_pointer( q ) == 0 &&
                 check_pointer( r ) == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
673 674 675
    mbedtls_free( r );
    mbedtls_free( q );
    mbedtls_free( p );
676 677 678

    TEST_ASSERT( check_all_free( ) == 0 );

679 680 681
    /* Memorize end to compare with the next test */
    end = heap.buf + heap.len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
682
    mbedtls_memory_buffer_alloc_free( );
683 684

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
685
        mbedtls_printf( "passed\n" );
686 687

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
688
        mbedtls_printf( "  MBA test #2 (buf not aligned): " );
689

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
690
    mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
691

692 693
    TEST_ASSERT( heap.buf + heap.len == end );

694 695 696
    p = mbedtls_calloc( 1, 1 );
    q = mbedtls_calloc( 1, 128 );
    r = mbedtls_calloc( 1, 16 );
697 698 699 700 701

    TEST_ASSERT( check_pointer( p ) == 0 &&
                 check_pointer( q ) == 0 &&
                 check_pointer( r ) == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
702 703 704
    mbedtls_free( r );
    mbedtls_free( q );
    mbedtls_free( p );
705 706 707

    TEST_ASSERT( check_all_free( ) == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
708
    mbedtls_memory_buffer_alloc_free( );
709 710

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
711
        mbedtls_printf( "passed\n" );
712 713

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
714
        mbedtls_printf( "  MBA test #3 (full): " );
715

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
716
    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
717

718
    p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
719 720

    TEST_ASSERT( check_pointer( p ) == 0 );
721
    TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
722

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
723
    mbedtls_free( p );
724

725 726
    p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
    q = mbedtls_calloc( 1, 16 );
727 728

    TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
729
    TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
730

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
731
    mbedtls_free( q );
732

733
    TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
734

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
735
    mbedtls_free( p );
736 737 738

    TEST_ASSERT( check_all_free( ) == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
739
    mbedtls_memory_buffer_alloc_free( );
740 741

    if( verbose != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
742
        mbedtls_printf( "passed\n" );
743 744

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
745
    mbedtls_memory_buffer_alloc_free( );
746 747 748

    return( ret );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
749
#endif /* MBEDTLS_SELF_TEST */
750

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
751
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */