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