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