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