mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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