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