mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * Buffer-based memory allocator
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22
ansond 0:137634ff4186 23 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 24 #include "polarssl/config.h"
ansond 0:137634ff4186 25 #else
ansond 0:137634ff4186 26 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 27 #endif
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
ansond 0:137634ff4186 30 #include "polarssl/memory_buffer_alloc.h"
ansond 0:137634ff4186 31
ansond 0:137634ff4186 32 /* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
ansond 0:137634ff4186 33 is dependent upon POLARSSL_PLATFORM_C */
ansond 0:137634ff4186 34 #include "polarssl/platform.h"
ansond 0:137634ff4186 35
ansond 0:137634ff4186 36 #include <string.h>
ansond 0:137634ff4186 37
ansond 0:137634ff4186 38 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 39 #include <execinfo.h>
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 43 #include "polarssl/threading.h"
ansond 0:137634ff4186 44 #endif
ansond 0:137634ff4186 45
ansond 0:137634ff4186 46 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 47 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 49 }
ansond 0:137634ff4186 50
ansond 0:137634ff4186 51 #define MAGIC1 0xFF00AA55
ansond 0:137634ff4186 52 #define MAGIC2 0xEE119966
ansond 0:137634ff4186 53 #define MAX_BT 20
ansond 0:137634ff4186 54
ansond 0:137634ff4186 55 typedef struct _memory_header memory_header;
ansond 0:137634ff4186 56 struct _memory_header
ansond 0:137634ff4186 57 {
ansond 0:137634ff4186 58 size_t magic1;
ansond 0:137634ff4186 59 size_t size;
ansond 0:137634ff4186 60 size_t alloc;
ansond 0:137634ff4186 61 memory_header *prev;
ansond 0:137634ff4186 62 memory_header *next;
ansond 0:137634ff4186 63 memory_header *prev_free;
ansond 0:137634ff4186 64 memory_header *next_free;
ansond 0:137634ff4186 65 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 66 char **trace;
ansond 0:137634ff4186 67 size_t trace_count;
ansond 0:137634ff4186 68 #endif
ansond 0:137634ff4186 69 size_t magic2;
ansond 0:137634ff4186 70 };
ansond 0:137634ff4186 71
ansond 0:137634ff4186 72 typedef struct
ansond 0:137634ff4186 73 {
ansond 0:137634ff4186 74 unsigned char *buf;
ansond 0:137634ff4186 75 size_t len;
ansond 0:137634ff4186 76 memory_header *first;
ansond 0:137634ff4186 77 memory_header *first_free;
ansond 0:137634ff4186 78 int verify;
ansond 0:137634ff4186 79 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 80 size_t malloc_count;
ansond 0:137634ff4186 81 size_t free_count;
ansond 0:137634ff4186 82 size_t total_used;
ansond 0:137634ff4186 83 size_t maximum_used;
ansond 0:137634ff4186 84 size_t header_count;
ansond 0:137634ff4186 85 size_t maximum_header_count;
ansond 0:137634ff4186 86 #endif
ansond 0:137634ff4186 87 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 88 threading_mutex_t mutex;
ansond 0:137634ff4186 89 #endif
ansond 0:137634ff4186 90 }
ansond 0:137634ff4186 91 buffer_alloc_ctx;
ansond 0:137634ff4186 92
ansond 0:137634ff4186 93 static buffer_alloc_ctx heap;
ansond 0:137634ff4186 94
ansond 0:137634ff4186 95 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 96 static void debug_header( memory_header *hdr )
ansond 0:137634ff4186 97 {
ansond 0:137634ff4186 98 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 99 size_t i;
ansond 0:137634ff4186 100 #endif
ansond 0:137634ff4186 101
ansond 0:137634ff4186 102 polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
ansond 0:137634ff4186 103 "ALLOC(%zu), SIZE(%10zu)\n",
ansond 0:137634ff4186 104 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
ansond 0:137634ff4186 105 hdr->alloc, hdr->size );
ansond 0:137634ff4186 106 polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
ansond 0:137634ff4186 107 (size_t) hdr->prev_free, (size_t) hdr->next_free );
ansond 0:137634ff4186 108
ansond 0:137634ff4186 109 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 110 polarssl_fprintf( stderr, "TRACE: \n" );
ansond 0:137634ff4186 111 for( i = 0; i < hdr->trace_count; i++ )
ansond 0:137634ff4186 112 polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
ansond 0:137634ff4186 113 polarssl_fprintf( stderr, "\n" );
ansond 0:137634ff4186 114 #endif
ansond 0:137634ff4186 115 }
ansond 0:137634ff4186 116
ansond 0:137634ff4186 117 static void debug_chain()
ansond 0:137634ff4186 118 {
ansond 0:137634ff4186 119 memory_header *cur = heap.first;
ansond 0:137634ff4186 120
ansond 0:137634ff4186 121 polarssl_fprintf( stderr, "\nBlock list\n" );
ansond 0:137634ff4186 122 while( cur != NULL )
ansond 0:137634ff4186 123 {
ansond 0:137634ff4186 124 debug_header( cur );
ansond 0:137634ff4186 125 cur = cur->next;
ansond 0:137634ff4186 126 }
ansond 0:137634ff4186 127
ansond 0:137634ff4186 128 polarssl_fprintf( stderr, "Free list\n" );
ansond 0:137634ff4186 129 cur = heap.first_free;
ansond 0:137634ff4186 130
ansond 0:137634ff4186 131 while( cur != NULL )
ansond 0:137634ff4186 132 {
ansond 0:137634ff4186 133 debug_header( cur );
ansond 0:137634ff4186 134 cur = cur->next_free;
ansond 0:137634ff4186 135 }
ansond 0:137634ff4186 136 }
ansond 0:137634ff4186 137 #endif /* POLARSSL_MEMORY_DEBUG */
ansond 0:137634ff4186 138
ansond 0:137634ff4186 139 static int verify_header( memory_header *hdr )
ansond 0:137634ff4186 140 {
ansond 0:137634ff4186 141 if( hdr->magic1 != MAGIC1 )
ansond 0:137634ff4186 142 {
ansond 0:137634ff4186 143 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 144 polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
ansond 0:137634ff4186 145 #endif
ansond 0:137634ff4186 146 return( 1 );
ansond 0:137634ff4186 147 }
ansond 0:137634ff4186 148
ansond 0:137634ff4186 149 if( hdr->magic2 != MAGIC2 )
ansond 0:137634ff4186 150 {
ansond 0:137634ff4186 151 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 152 polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
ansond 0:137634ff4186 153 #endif
ansond 0:137634ff4186 154 return( 1 );
ansond 0:137634ff4186 155 }
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 if( hdr->alloc > 1 )
ansond 0:137634ff4186 158 {
ansond 0:137634ff4186 159 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 160 polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
ansond 0:137634ff4186 161 #endif
ansond 0:137634ff4186 162 return( 1 );
ansond 0:137634ff4186 163 }
ansond 0:137634ff4186 164
ansond 0:137634ff4186 165 if( hdr->prev != NULL && hdr->prev == hdr->next )
ansond 0:137634ff4186 166 {
ansond 0:137634ff4186 167 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 168 polarssl_fprintf( stderr, "FATAL: prev == next\n" );
ansond 0:137634ff4186 169 #endif
ansond 0:137634ff4186 170 return( 1 );
ansond 0:137634ff4186 171 }
ansond 0:137634ff4186 172
ansond 0:137634ff4186 173 if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
ansond 0:137634ff4186 174 {
ansond 0:137634ff4186 175 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 176 polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
ansond 0:137634ff4186 177 #endif
ansond 0:137634ff4186 178 return( 1 );
ansond 0:137634ff4186 179 }
ansond 0:137634ff4186 180
ansond 0:137634ff4186 181 return( 0 );
ansond 0:137634ff4186 182 }
ansond 0:137634ff4186 183
ansond 0:137634ff4186 184 static int verify_chain()
ansond 0:137634ff4186 185 {
ansond 0:137634ff4186 186 memory_header *prv = heap.first, *cur = heap.first->next;
ansond 0:137634ff4186 187
ansond 0:137634ff4186 188 if( verify_header( heap.first ) != 0 )
ansond 0:137634ff4186 189 {
ansond 0:137634ff4186 190 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 191 polarssl_fprintf( stderr, "FATAL: verification of first header "
ansond 0:137634ff4186 192 "failed\n" );
ansond 0:137634ff4186 193 #endif
ansond 0:137634ff4186 194 return( 1 );
ansond 0:137634ff4186 195 }
ansond 0:137634ff4186 196
ansond 0:137634ff4186 197 if( heap.first->prev != NULL )
ansond 0:137634ff4186 198 {
ansond 0:137634ff4186 199 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 200 polarssl_fprintf( stderr, "FATAL: verification failed: "
ansond 0:137634ff4186 201 "first->prev != NULL\n" );
ansond 0:137634ff4186 202 #endif
ansond 0:137634ff4186 203 return( 1 );
ansond 0:137634ff4186 204 }
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 while( cur != NULL )
ansond 0:137634ff4186 207 {
ansond 0:137634ff4186 208 if( verify_header( cur ) != 0 )
ansond 0:137634ff4186 209 {
ansond 0:137634ff4186 210 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 211 polarssl_fprintf( stderr, "FATAL: verification of header "
ansond 0:137634ff4186 212 "failed\n" );
ansond 0:137634ff4186 213 #endif
ansond 0:137634ff4186 214 return( 1 );
ansond 0:137634ff4186 215 }
ansond 0:137634ff4186 216
ansond 0:137634ff4186 217 if( cur->prev != prv )
ansond 0:137634ff4186 218 {
ansond 0:137634ff4186 219 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 220 polarssl_fprintf( stderr, "FATAL: verification failed: "
ansond 0:137634ff4186 221 "cur->prev != prv\n" );
ansond 0:137634ff4186 222 #endif
ansond 0:137634ff4186 223 return( 1 );
ansond 0:137634ff4186 224 }
ansond 0:137634ff4186 225
ansond 0:137634ff4186 226 prv = cur;
ansond 0:137634ff4186 227 cur = cur->next;
ansond 0:137634ff4186 228 }
ansond 0:137634ff4186 229
ansond 0:137634ff4186 230 return( 0 );
ansond 0:137634ff4186 231 }
ansond 0:137634ff4186 232
ansond 0:137634ff4186 233 static void *buffer_alloc_malloc( size_t len )
ansond 0:137634ff4186 234 {
ansond 0:137634ff4186 235 memory_header *new, *cur = heap.first_free;
ansond 0:137634ff4186 236 unsigned char *p;
ansond 0:137634ff4186 237 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 238 void *trace_buffer[MAX_BT];
ansond 0:137634ff4186 239 size_t trace_cnt;
ansond 0:137634ff4186 240 #endif
ansond 0:137634ff4186 241
ansond 0:137634ff4186 242 if( heap.buf == NULL || heap.first == NULL )
ansond 0:137634ff4186 243 return( NULL );
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
ansond 0:137634ff4186 246 {
ansond 0:137634ff4186 247 len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
ansond 0:137634ff4186 248 len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
ansond 0:137634ff4186 249 }
ansond 0:137634ff4186 250
ansond 0:137634ff4186 251 // Find block that fits
ansond 0:137634ff4186 252 //
ansond 0:137634ff4186 253 while( cur != NULL )
ansond 0:137634ff4186 254 {
ansond 0:137634ff4186 255 if( cur->size >= len )
ansond 0:137634ff4186 256 break;
ansond 0:137634ff4186 257
ansond 0:137634ff4186 258 cur = cur->next_free;
ansond 0:137634ff4186 259 }
ansond 0:137634ff4186 260
ansond 0:137634ff4186 261 if( cur == NULL )
ansond 0:137634ff4186 262 return( NULL );
ansond 0:137634ff4186 263
ansond 0:137634ff4186 264 if( cur->alloc != 0 )
ansond 0:137634ff4186 265 {
ansond 0:137634ff4186 266 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 267 polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
ansond 0:137634ff4186 268 "data\n" );
ansond 0:137634ff4186 269 #endif
ansond 0:137634ff4186 270 polarssl_exit( 1 );
ansond 0:137634ff4186 271 }
ansond 0:137634ff4186 272
ansond 0:137634ff4186 273 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 274 heap.malloc_count++;
ansond 0:137634ff4186 275 #endif
ansond 0:137634ff4186 276
ansond 0:137634ff4186 277 // Found location, split block if > memory_header + 4 room left
ansond 0:137634ff4186 278 //
ansond 0:137634ff4186 279 if( cur->size - len < sizeof(memory_header) +
ansond 0:137634ff4186 280 POLARSSL_MEMORY_ALIGN_MULTIPLE )
ansond 0:137634ff4186 281 {
ansond 0:137634ff4186 282 cur->alloc = 1;
ansond 0:137634ff4186 283
ansond 0:137634ff4186 284 // Remove from free_list
ansond 0:137634ff4186 285 //
ansond 0:137634ff4186 286 if( cur->prev_free != NULL )
ansond 0:137634ff4186 287 cur->prev_free->next_free = cur->next_free;
ansond 0:137634ff4186 288 else
ansond 0:137634ff4186 289 heap.first_free = cur->next_free;
ansond 0:137634ff4186 290
ansond 0:137634ff4186 291 if( cur->next_free != NULL )
ansond 0:137634ff4186 292 cur->next_free->prev_free = cur->prev_free;
ansond 0:137634ff4186 293
ansond 0:137634ff4186 294 cur->prev_free = NULL;
ansond 0:137634ff4186 295 cur->next_free = NULL;
ansond 0:137634ff4186 296
ansond 0:137634ff4186 297 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 298 heap.total_used += cur->size;
ansond 0:137634ff4186 299 if( heap.total_used > heap.maximum_used )
ansond 0:137634ff4186 300 heap.maximum_used = heap.total_used;
ansond 0:137634ff4186 301 #endif
ansond 0:137634ff4186 302 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 303 trace_cnt = backtrace( trace_buffer, MAX_BT );
ansond 0:137634ff4186 304 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
ansond 0:137634ff4186 305 cur->trace_count = trace_cnt;
ansond 0:137634ff4186 306 #endif
ansond 0:137634ff4186 307
ansond 0:137634ff4186 308 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
ansond 0:137634ff4186 309 polarssl_exit( 1 );
ansond 0:137634ff4186 310
ansond 0:137634ff4186 311 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
ansond 0:137634ff4186 312 }
ansond 0:137634ff4186 313
ansond 0:137634ff4186 314 p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
ansond 0:137634ff4186 315 new = (memory_header *) p;
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 new->size = cur->size - len - sizeof(memory_header);
ansond 0:137634ff4186 318 new->alloc = 0;
ansond 0:137634ff4186 319 new->prev = cur;
ansond 0:137634ff4186 320 new->next = cur->next;
ansond 0:137634ff4186 321 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 322 new->trace = NULL;
ansond 0:137634ff4186 323 new->trace_count = 0;
ansond 0:137634ff4186 324 #endif
ansond 0:137634ff4186 325 new->magic1 = MAGIC1;
ansond 0:137634ff4186 326 new->magic2 = MAGIC2;
ansond 0:137634ff4186 327
ansond 0:137634ff4186 328 if( new->next != NULL )
ansond 0:137634ff4186 329 new->next->prev = new;
ansond 0:137634ff4186 330
ansond 0:137634ff4186 331 // Replace cur with new in free_list
ansond 0:137634ff4186 332 //
ansond 0:137634ff4186 333 new->prev_free = cur->prev_free;
ansond 0:137634ff4186 334 new->next_free = cur->next_free;
ansond 0:137634ff4186 335 if( new->prev_free != NULL )
ansond 0:137634ff4186 336 new->prev_free->next_free = new;
ansond 0:137634ff4186 337 else
ansond 0:137634ff4186 338 heap.first_free = new;
ansond 0:137634ff4186 339
ansond 0:137634ff4186 340 if( new->next_free != NULL )
ansond 0:137634ff4186 341 new->next_free->prev_free = new;
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 cur->alloc = 1;
ansond 0:137634ff4186 344 cur->size = len;
ansond 0:137634ff4186 345 cur->next = new;
ansond 0:137634ff4186 346 cur->prev_free = NULL;
ansond 0:137634ff4186 347 cur->next_free = NULL;
ansond 0:137634ff4186 348
ansond 0:137634ff4186 349 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 350 heap.header_count++;
ansond 0:137634ff4186 351 if( heap.header_count > heap.maximum_header_count )
ansond 0:137634ff4186 352 heap.maximum_header_count = heap.header_count;
ansond 0:137634ff4186 353 heap.total_used += cur->size;
ansond 0:137634ff4186 354 if( heap.total_used > heap.maximum_used )
ansond 0:137634ff4186 355 heap.maximum_used = heap.total_used;
ansond 0:137634ff4186 356 #endif
ansond 0:137634ff4186 357 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 358 trace_cnt = backtrace( trace_buffer, MAX_BT );
ansond 0:137634ff4186 359 cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
ansond 0:137634ff4186 360 cur->trace_count = trace_cnt;
ansond 0:137634ff4186 361 #endif
ansond 0:137634ff4186 362
ansond 0:137634ff4186 363 if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
ansond 0:137634ff4186 364 polarssl_exit( 1 );
ansond 0:137634ff4186 365
ansond 0:137634ff4186 366 return( ( (unsigned char *) cur ) + sizeof(memory_header) );
ansond 0:137634ff4186 367 }
ansond 0:137634ff4186 368
ansond 0:137634ff4186 369 static void buffer_alloc_free( void *ptr )
ansond 0:137634ff4186 370 {
ansond 0:137634ff4186 371 memory_header *hdr, *old = NULL;
ansond 0:137634ff4186 372 unsigned char *p = (unsigned char *) ptr;
ansond 0:137634ff4186 373
ansond 0:137634ff4186 374 if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
ansond 0:137634ff4186 375 return;
ansond 0:137634ff4186 376
ansond 0:137634ff4186 377 if( p < heap.buf || p > heap.buf + heap.len )
ansond 0:137634ff4186 378 {
ansond 0:137634ff4186 379 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 380 polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
ansond 0:137634ff4186 381 "space\n" );
ansond 0:137634ff4186 382 #endif
ansond 0:137634ff4186 383 polarssl_exit( 1 );
ansond 0:137634ff4186 384 }
ansond 0:137634ff4186 385
ansond 0:137634ff4186 386 p -= sizeof(memory_header);
ansond 0:137634ff4186 387 hdr = (memory_header *) p;
ansond 0:137634ff4186 388
ansond 0:137634ff4186 389 if( verify_header( hdr ) != 0 )
ansond 0:137634ff4186 390 polarssl_exit( 1 );
ansond 0:137634ff4186 391
ansond 0:137634ff4186 392 if( hdr->alloc != 1 )
ansond 0:137634ff4186 393 {
ansond 0:137634ff4186 394 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 395 polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
ansond 0:137634ff4186 396 "data\n" );
ansond 0:137634ff4186 397 #endif
ansond 0:137634ff4186 398 polarssl_exit( 1 );
ansond 0:137634ff4186 399 }
ansond 0:137634ff4186 400
ansond 0:137634ff4186 401 hdr->alloc = 0;
ansond 0:137634ff4186 402
ansond 0:137634ff4186 403 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 404 heap.free_count++;
ansond 0:137634ff4186 405 heap.total_used -= hdr->size;
ansond 0:137634ff4186 406 #endif
ansond 0:137634ff4186 407
ansond 0:137634ff4186 408 // Regroup with block before
ansond 0:137634ff4186 409 //
ansond 0:137634ff4186 410 if( hdr->prev != NULL && hdr->prev->alloc == 0 )
ansond 0:137634ff4186 411 {
ansond 0:137634ff4186 412 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 413 heap.header_count--;
ansond 0:137634ff4186 414 #endif
ansond 0:137634ff4186 415 hdr->prev->size += sizeof(memory_header) + hdr->size;
ansond 0:137634ff4186 416 hdr->prev->next = hdr->next;
ansond 0:137634ff4186 417 old = hdr;
ansond 0:137634ff4186 418 hdr = hdr->prev;
ansond 0:137634ff4186 419
ansond 0:137634ff4186 420 if( hdr->next != NULL )
ansond 0:137634ff4186 421 hdr->next->prev = hdr;
ansond 0:137634ff4186 422
ansond 0:137634ff4186 423 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 424 free( old->trace );
ansond 0:137634ff4186 425 #endif
ansond 0:137634ff4186 426 memset( old, 0, sizeof(memory_header) );
ansond 0:137634ff4186 427 }
ansond 0:137634ff4186 428
ansond 0:137634ff4186 429 // Regroup with block after
ansond 0:137634ff4186 430 //
ansond 0:137634ff4186 431 if( hdr->next != NULL && hdr->next->alloc == 0 )
ansond 0:137634ff4186 432 {
ansond 0:137634ff4186 433 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 434 heap.header_count--;
ansond 0:137634ff4186 435 #endif
ansond 0:137634ff4186 436 hdr->size += sizeof(memory_header) + hdr->next->size;
ansond 0:137634ff4186 437 old = hdr->next;
ansond 0:137634ff4186 438 hdr->next = hdr->next->next;
ansond 0:137634ff4186 439
ansond 0:137634ff4186 440 if( hdr->prev_free != NULL || hdr->next_free != NULL )
ansond 0:137634ff4186 441 {
ansond 0:137634ff4186 442 if( hdr->prev_free != NULL )
ansond 0:137634ff4186 443 hdr->prev_free->next_free = hdr->next_free;
ansond 0:137634ff4186 444 else
ansond 0:137634ff4186 445 heap.first_free = hdr->next_free;
ansond 0:137634ff4186 446
ansond 0:137634ff4186 447 if( hdr->next_free != NULL )
ansond 0:137634ff4186 448 hdr->next_free->prev_free = hdr->prev_free;
ansond 0:137634ff4186 449 }
ansond 0:137634ff4186 450
ansond 0:137634ff4186 451 hdr->prev_free = old->prev_free;
ansond 0:137634ff4186 452 hdr->next_free = old->next_free;
ansond 0:137634ff4186 453
ansond 0:137634ff4186 454 if( hdr->prev_free != NULL )
ansond 0:137634ff4186 455 hdr->prev_free->next_free = hdr;
ansond 0:137634ff4186 456 else
ansond 0:137634ff4186 457 heap.first_free = hdr;
ansond 0:137634ff4186 458
ansond 0:137634ff4186 459 if( hdr->next_free != NULL )
ansond 0:137634ff4186 460 hdr->next_free->prev_free = hdr;
ansond 0:137634ff4186 461
ansond 0:137634ff4186 462 if( hdr->next != NULL )
ansond 0:137634ff4186 463 hdr->next->prev = hdr;
ansond 0:137634ff4186 464
ansond 0:137634ff4186 465 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 466 free( old->trace );
ansond 0:137634ff4186 467 #endif
ansond 0:137634ff4186 468 memset( old, 0, sizeof(memory_header) );
ansond 0:137634ff4186 469 }
ansond 0:137634ff4186 470
ansond 0:137634ff4186 471 // Prepend to free_list if we have not merged
ansond 0:137634ff4186 472 // (Does not have to stay in same order as prev / next list)
ansond 0:137634ff4186 473 //
ansond 0:137634ff4186 474 if( old == NULL )
ansond 0:137634ff4186 475 {
ansond 0:137634ff4186 476 hdr->next_free = heap.first_free;
ansond 0:137634ff4186 477 if( heap.first_free != NULL )
ansond 0:137634ff4186 478 heap.first_free->prev_free = hdr;
ansond 0:137634ff4186 479 heap.first_free = hdr;
ansond 0:137634ff4186 480 }
ansond 0:137634ff4186 481
ansond 0:137634ff4186 482 #if defined(POLARSSL_MEMORY_BACKTRACE)
ansond 0:137634ff4186 483 hdr->trace = NULL;
ansond 0:137634ff4186 484 hdr->trace_count = 0;
ansond 0:137634ff4186 485 #endif
ansond 0:137634ff4186 486
ansond 0:137634ff4186 487 if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
ansond 0:137634ff4186 488 polarssl_exit( 1 );
ansond 0:137634ff4186 489 }
ansond 0:137634ff4186 490
ansond 0:137634ff4186 491 void memory_buffer_set_verify( int verify )
ansond 0:137634ff4186 492 {
ansond 0:137634ff4186 493 heap.verify = verify;
ansond 0:137634ff4186 494 }
ansond 0:137634ff4186 495
ansond 0:137634ff4186 496 int memory_buffer_alloc_verify()
ansond 0:137634ff4186 497 {
ansond 0:137634ff4186 498 return verify_chain();
ansond 0:137634ff4186 499 }
ansond 0:137634ff4186 500
ansond 0:137634ff4186 501 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 502 void memory_buffer_alloc_status()
ansond 0:137634ff4186 503 {
ansond 0:137634ff4186 504 polarssl_fprintf( stderr,
ansond 0:137634ff4186 505 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
ansond 0:137634ff4186 506 "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
ansond 0:137634ff4186 507 heap.header_count, heap.total_used,
ansond 0:137634ff4186 508 heap.maximum_header_count, heap.maximum_used,
ansond 0:137634ff4186 509 heap.maximum_header_count * sizeof( memory_header )
ansond 0:137634ff4186 510 + heap.maximum_used,
ansond 0:137634ff4186 511 heap.malloc_count, heap.free_count );
ansond 0:137634ff4186 512
ansond 0:137634ff4186 513 if( heap.first->next == NULL )
ansond 0:137634ff4186 514 polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
ansond 0:137634ff4186 515 else
ansond 0:137634ff4186 516 {
ansond 0:137634ff4186 517 polarssl_fprintf( stderr, "Memory currently allocated:\n" );
ansond 0:137634ff4186 518 debug_chain();
ansond 0:137634ff4186 519 }
ansond 0:137634ff4186 520 }
ansond 0:137634ff4186 521
ansond 0:137634ff4186 522 void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
ansond 0:137634ff4186 523 {
ansond 0:137634ff4186 524 *max_used = heap.maximum_used;
ansond 0:137634ff4186 525 *max_blocks = heap.maximum_header_count;
ansond 0:137634ff4186 526 }
ansond 0:137634ff4186 527
ansond 0:137634ff4186 528 void memory_buffer_alloc_max_reset( void )
ansond 0:137634ff4186 529 {
ansond 0:137634ff4186 530 heap.maximum_used = 0;
ansond 0:137634ff4186 531 heap.maximum_header_count = 0;
ansond 0:137634ff4186 532 }
ansond 0:137634ff4186 533
ansond 0:137634ff4186 534 void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
ansond 0:137634ff4186 535 {
ansond 0:137634ff4186 536 *cur_used = heap.total_used;
ansond 0:137634ff4186 537 *cur_blocks = heap.header_count;
ansond 0:137634ff4186 538 }
ansond 0:137634ff4186 539 #endif /* POLARSSL_MEMORY_DEBUG */
ansond 0:137634ff4186 540
ansond 0:137634ff4186 541 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 542 static void *buffer_alloc_malloc_mutexed( size_t len )
ansond 0:137634ff4186 543 {
ansond 0:137634ff4186 544 void *buf;
ansond 0:137634ff4186 545 polarssl_mutex_lock( &heap.mutex );
ansond 0:137634ff4186 546 buf = buffer_alloc_malloc( len );
ansond 0:137634ff4186 547 polarssl_mutex_unlock( &heap.mutex );
ansond 0:137634ff4186 548 return( buf );
ansond 0:137634ff4186 549 }
ansond 0:137634ff4186 550
ansond 0:137634ff4186 551 static void buffer_alloc_free_mutexed( void *ptr )
ansond 0:137634ff4186 552 {
ansond 0:137634ff4186 553 polarssl_mutex_lock( &heap.mutex );
ansond 0:137634ff4186 554 buffer_alloc_free( ptr );
ansond 0:137634ff4186 555 polarssl_mutex_unlock( &heap.mutex );
ansond 0:137634ff4186 556 }
ansond 0:137634ff4186 557 #endif /* POLARSSL_THREADING_C */
ansond 0:137634ff4186 558
ansond 0:137634ff4186 559 int memory_buffer_alloc_init( unsigned char *buf, size_t len )
ansond 0:137634ff4186 560 {
ansond 0:137634ff4186 561 memset( &heap, 0, sizeof(buffer_alloc_ctx) );
ansond 0:137634ff4186 562 memset( buf, 0, len );
ansond 0:137634ff4186 563
ansond 0:137634ff4186 564 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 565 polarssl_mutex_init( &heap.mutex );
ansond 0:137634ff4186 566 platform_set_malloc_free( buffer_alloc_malloc_mutexed,
ansond 0:137634ff4186 567 buffer_alloc_free_mutexed );
ansond 0:137634ff4186 568 #else
ansond 0:137634ff4186 569 platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
ansond 0:137634ff4186 570 #endif
ansond 0:137634ff4186 571
ansond 0:137634ff4186 572 if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
ansond 0:137634ff4186 573 {
ansond 0:137634ff4186 574 /* Adjust len first since buf is used in the computation */
ansond 0:137634ff4186 575 len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
ansond 0:137634ff4186 576 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
ansond 0:137634ff4186 577 buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
ansond 0:137634ff4186 578 - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
ansond 0:137634ff4186 579 }
ansond 0:137634ff4186 580
ansond 0:137634ff4186 581 heap.buf = buf;
ansond 0:137634ff4186 582 heap.len = len;
ansond 0:137634ff4186 583
ansond 0:137634ff4186 584 heap.first = (memory_header *) buf;
ansond 0:137634ff4186 585 heap.first->size = len - sizeof(memory_header);
ansond 0:137634ff4186 586 heap.first->magic1 = MAGIC1;
ansond 0:137634ff4186 587 heap.first->magic2 = MAGIC2;
ansond 0:137634ff4186 588 heap.first_free = heap.first;
ansond 0:137634ff4186 589 return( 0 );
ansond 0:137634ff4186 590 }
ansond 0:137634ff4186 591
ansond 0:137634ff4186 592 void memory_buffer_alloc_free()
ansond 0:137634ff4186 593 {
ansond 0:137634ff4186 594 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 595 polarssl_mutex_free( &heap.mutex );
ansond 0:137634ff4186 596 #endif
ansond 0:137634ff4186 597 polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
ansond 0:137634ff4186 598 }
ansond 0:137634ff4186 599
ansond 0:137634ff4186 600 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 601 static int check_pointer( void *p )
ansond 0:137634ff4186 602 {
ansond 0:137634ff4186 603 if( p == NULL )
ansond 0:137634ff4186 604 return( -1 );
ansond 0:137634ff4186 605
ansond 0:137634ff4186 606 if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
ansond 0:137634ff4186 607 return( -1 );
ansond 0:137634ff4186 608
ansond 0:137634ff4186 609 return( 0 );
ansond 0:137634ff4186 610 }
ansond 0:137634ff4186 611
ansond 0:137634ff4186 612 static int check_all_free( )
ansond 0:137634ff4186 613 {
ansond 0:137634ff4186 614 if(
ansond 0:137634ff4186 615 #if defined(POLARSSL_MEMORY_DEBUG)
ansond 0:137634ff4186 616 heap.total_used != 0 ||
ansond 0:137634ff4186 617 #endif
ansond 0:137634ff4186 618 heap.first != heap.first_free ||
ansond 0:137634ff4186 619 (void *) heap.first != (void *) heap.buf )
ansond 0:137634ff4186 620 {
ansond 0:137634ff4186 621 return( -1 );
ansond 0:137634ff4186 622 }
ansond 0:137634ff4186 623
ansond 0:137634ff4186 624 return( 0 );
ansond 0:137634ff4186 625 }
ansond 0:137634ff4186 626
ansond 0:137634ff4186 627 #define TEST_ASSERT( condition ) \
ansond 0:137634ff4186 628 if( ! (condition) ) \
ansond 0:137634ff4186 629 { \
ansond 0:137634ff4186 630 if( verbose != 0 ) \
ansond 0:137634ff4186 631 polarssl_printf( "failed\n" ); \
ansond 0:137634ff4186 632 \
ansond 0:137634ff4186 633 ret = 1; \
ansond 0:137634ff4186 634 goto cleanup; \
ansond 0:137634ff4186 635 }
ansond 0:137634ff4186 636
ansond 0:137634ff4186 637 int memory_buffer_alloc_self_test( int verbose )
ansond 0:137634ff4186 638 {
ansond 0:137634ff4186 639 unsigned char buf[1024];
ansond 0:137634ff4186 640 unsigned char *p, *q, *r, *end;
ansond 0:137634ff4186 641 int ret = 0;
ansond 0:137634ff4186 642
ansond 0:137634ff4186 643 if( verbose != 0 )
ansond 0:137634ff4186 644 polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
ansond 0:137634ff4186 645
ansond 0:137634ff4186 646 memory_buffer_alloc_init( buf, sizeof( buf ) );
ansond 0:137634ff4186 647
ansond 0:137634ff4186 648 p = polarssl_malloc( 1 );
ansond 0:137634ff4186 649 q = polarssl_malloc( 128 );
ansond 0:137634ff4186 650 r = polarssl_malloc( 16 );
ansond 0:137634ff4186 651
ansond 0:137634ff4186 652 TEST_ASSERT( check_pointer( p ) == 0 &&
ansond 0:137634ff4186 653 check_pointer( q ) == 0 &&
ansond 0:137634ff4186 654 check_pointer( r ) == 0 );
ansond 0:137634ff4186 655
ansond 0:137634ff4186 656 polarssl_free( r );
ansond 0:137634ff4186 657 polarssl_free( q );
ansond 0:137634ff4186 658 polarssl_free( p );
ansond 0:137634ff4186 659
ansond 0:137634ff4186 660 TEST_ASSERT( check_all_free( ) == 0 );
ansond 0:137634ff4186 661
ansond 0:137634ff4186 662 /* Memorize end to compare with the next test */
ansond 0:137634ff4186 663 end = heap.buf + heap.len;
ansond 0:137634ff4186 664
ansond 0:137634ff4186 665 memory_buffer_alloc_free( );
ansond 0:137634ff4186 666
ansond 0:137634ff4186 667 if( verbose != 0 )
ansond 0:137634ff4186 668 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 669
ansond 0:137634ff4186 670 if( verbose != 0 )
ansond 0:137634ff4186 671 polarssl_printf( " MBA test #2 (buf not aligned): " );
ansond 0:137634ff4186 672
ansond 0:137634ff4186 673 memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
ansond 0:137634ff4186 674
ansond 0:137634ff4186 675 TEST_ASSERT( heap.buf + heap.len == end );
ansond 0:137634ff4186 676
ansond 0:137634ff4186 677 p = polarssl_malloc( 1 );
ansond 0:137634ff4186 678 q = polarssl_malloc( 128 );
ansond 0:137634ff4186 679 r = polarssl_malloc( 16 );
ansond 0:137634ff4186 680
ansond 0:137634ff4186 681 TEST_ASSERT( check_pointer( p ) == 0 &&
ansond 0:137634ff4186 682 check_pointer( q ) == 0 &&
ansond 0:137634ff4186 683 check_pointer( r ) == 0 );
ansond 0:137634ff4186 684
ansond 0:137634ff4186 685 polarssl_free( r );
ansond 0:137634ff4186 686 polarssl_free( q );
ansond 0:137634ff4186 687 polarssl_free( p );
ansond 0:137634ff4186 688
ansond 0:137634ff4186 689 TEST_ASSERT( check_all_free( ) == 0 );
ansond 0:137634ff4186 690
ansond 0:137634ff4186 691 memory_buffer_alloc_free( );
ansond 0:137634ff4186 692
ansond 0:137634ff4186 693 if( verbose != 0 )
ansond 0:137634ff4186 694 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 695
ansond 0:137634ff4186 696 if( verbose != 0 )
ansond 0:137634ff4186 697 polarssl_printf( " MBA test #3 (full): " );
ansond 0:137634ff4186 698
ansond 0:137634ff4186 699 memory_buffer_alloc_init( buf, sizeof( buf ) );
ansond 0:137634ff4186 700
ansond 0:137634ff4186 701 p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
ansond 0:137634ff4186 702
ansond 0:137634ff4186 703 TEST_ASSERT( check_pointer( p ) == 0 );
ansond 0:137634ff4186 704 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
ansond 0:137634ff4186 705
ansond 0:137634ff4186 706 polarssl_free( p );
ansond 0:137634ff4186 707
ansond 0:137634ff4186 708 p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
ansond 0:137634ff4186 709 q = polarssl_malloc( 16 );
ansond 0:137634ff4186 710
ansond 0:137634ff4186 711 TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
ansond 0:137634ff4186 712 TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
ansond 0:137634ff4186 713
ansond 0:137634ff4186 714 polarssl_free( q );
ansond 0:137634ff4186 715
ansond 0:137634ff4186 716 TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
ansond 0:137634ff4186 717
ansond 0:137634ff4186 718 polarssl_free( p );
ansond 0:137634ff4186 719
ansond 0:137634ff4186 720 TEST_ASSERT( check_all_free( ) == 0 );
ansond 0:137634ff4186 721
ansond 0:137634ff4186 722 memory_buffer_alloc_free( );
ansond 0:137634ff4186 723
ansond 0:137634ff4186 724 if( verbose != 0 )
ansond 0:137634ff4186 725 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 726
ansond 0:137634ff4186 727 cleanup:
ansond 0:137634ff4186 728 memory_buffer_alloc_free( );
ansond 0:137634ff4186 729
ansond 0:137634ff4186 730 return( ret );
ansond 0:137634ff4186 731 }
ansond 0:137634ff4186 732 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 733
ansond 0:137634ff4186 734 #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
ansond 0:137634ff4186 735