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