blob: e5052ce5ac383c3c9922291e65e52e3901e81960 [file] [log] [blame]
Paul Bakker6e339b52013-07-03 13:37:05 +02001/*
2 * Buffer-based memory allocator
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker6e339b52013-07-03 13:37:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
26 is dependent upon MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050028#include "mbedtls/platform_util.h"
Rich Evansd08a6052015-02-12 12:17:10 +000029
Paul Bakker6e339b52013-07-03 13:37:05 +020030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020033#include <execinfo.h>
34#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/threading.h"
Paul Bakker1337aff2013-09-29 14:45:34 +020038#endif
39
Paul Bakker6e339b52013-07-03 13:37:05 +020040#define MAGIC1 0xFF00AA55
41#define MAGIC2 0xEE119966
42#define MAX_BT 20
43
44typedef struct _memory_header memory_header;
Gilles Peskine449bd832023-01-11 14:50:10 +010045struct _memory_header {
Paul Bakker6e339b52013-07-03 13:37:05 +020046 size_t magic1;
47 size_t size;
48 size_t alloc;
49 memory_header *prev;
50 memory_header *next;
Paul Bakker1ef120f2013-07-03 17:20:39 +020051 memory_header *prev_free;
52 memory_header *next_free;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020054 char **trace;
55 size_t trace_count;
56#endif
57 size_t magic2;
58};
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060typedef struct {
Paul Bakker6e339b52013-07-03 13:37:05 +020061 unsigned char *buf;
62 size_t len;
63 memory_header *first;
Paul Bakker1ef120f2013-07-03 17:20:39 +020064 memory_header *first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +020065 int verify;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +020067 size_t alloc_count;
Paul Bakker891998e2013-07-03 14:45:05 +020068 size_t free_count;
69 size_t total_used;
70 size_t maximum_used;
71 size_t header_count;
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +010072 size_t maximum_header_count;
Paul Bakker891998e2013-07-03 14:45:05 +020073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_THREADING_C)
75 mbedtls_threading_mutex_t mutex;
Paul Bakker1337aff2013-09-29 14:45:34 +020076#endif
Paul Bakker6e339b52013-07-03 13:37:05 +020077}
78buffer_alloc_ctx;
79
80static buffer_alloc_ctx heap;
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +010083static void debug_header(memory_header *hdr)
Paul Bakker6e339b52013-07-03 13:37:05 +020084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +020086 size_t i;
87#endif
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_fprintf(stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
90 "ALLOC(%zu), SIZE(%10zu)\n",
91 (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
92 hdr->alloc, hdr->size);
93 mbedtls_fprintf(stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
94 (size_t) hdr->prev_free, (size_t) hdr->next_free);
Paul Bakker6e339b52013-07-03 13:37:05 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_MEMORY_BACKTRACE)
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_fprintf(stderr, "TRACE: \n");
98 for (i = 0; i < hdr->trace_count; i++) {
99 mbedtls_fprintf(stderr, "%s\n", hdr->trace[i]);
100 }
101 mbedtls_fprintf(stderr, "\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200102#endif
103}
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static void debug_chain(void)
Paul Bakker6e339b52013-07-03 13:37:05 +0200106{
107 memory_header *cur = heap.first;
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_fprintf(stderr, "\nBlock list\n");
110 while (cur != NULL) {
111 debug_header(cur);
Paul Bakker6e339b52013-07-03 13:37:05 +0200112 cur = cur->next;
113 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_fprintf(stderr, "Free list\n");
Paul Bakker1ef120f2013-07-03 17:20:39 +0200116 cur = heap.first_free;
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 while (cur != NULL) {
119 debug_header(cur);
Paul Bakker1ef120f2013-07-03 17:20:39 +0200120 cur = cur->next_free;
121 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int verify_header(memory_header *hdr)
Paul Bakker6e339b52013-07-03 13:37:05 +0200126{
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (hdr->magic1 != MAGIC1) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_fprintf(stderr, "FATAL: MAGIC1 mismatch\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200130#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 }
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (hdr->magic2 != MAGIC2) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_fprintf(stderr, "FATAL: MAGIC2 mismatch\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200137#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200139 }
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (hdr->alloc > 1) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_fprintf(stderr, "FATAL: alloc has illegal value\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200144#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200146 }
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (hdr->prev != NULL && hdr->prev == hdr->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_fprintf(stderr, "FATAL: prev == next\n");
Paul Bakker1ef120f2013-07-03 17:20:39 +0200151#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return 1;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200153 }
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (hdr->prev_free != NULL && hdr->prev_free == hdr->next_free) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_fprintf(stderr, "FATAL: prev_free == next_free\n");
Paul Bakker1ef120f2013-07-03 17:20:39 +0200158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return 1;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return 0;
Paul Bakker6e339b52013-07-03 13:37:05 +0200163}
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static int verify_chain(void)
Paul Bakker6e339b52013-07-03 13:37:05 +0200166{
Andres AG9cf1f962017-01-30 14:34:25 +0000167 memory_header *prv = heap.first, *cur;
Paul Bakker6e339b52013-07-03 13:37:05 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (prv == NULL || verify_header(prv) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_fprintf(stderr, "FATAL: verification of first header "
172 "failed\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (heap.first->prev != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_fprintf(stderr, "FATAL: verification failed: "
180 "first->prev != NULL\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200181#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200183 }
184
Andres AG9cf1f962017-01-30 14:34:25 +0000185 cur = heap.first->next;
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 while (cur != NULL) {
188 if (verify_header(cur) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_fprintf(stderr, "FATAL: verification of header "
191 "failed\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200192#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (cur->prev != prv) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_fprintf(stderr, "FATAL: verification failed: "
199 "cur->prev != prv\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200200#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return 1;
Paul Bakker6e339b52013-07-03 13:37:05 +0200202 }
203
204 prv = cur;
205 cur = cur->next;
206 }
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Paul Bakker6e339b52013-07-03 13:37:05 +0200209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211static void *buffer_alloc_calloc(size_t n, size_t size)
Paul Bakker6e339b52013-07-03 13:37:05 +0200212{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200213 memory_header *new, *cur = heap.first_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200214 unsigned char *p;
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200215 void *ret;
216 size_t original_len, len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200218 void *trace_buffer[MAX_BT];
219 size_t trace_cnt;
220#endif
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (heap.buf == NULL || heap.first == NULL) {
223 return NULL;
224 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200225
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200226 original_len = len = n * size;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (n == 0 || size == 0 || len / n != size) {
229 return NULL;
230 } else if (len > (size_t) -MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
231 return NULL;
232 }
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (len % MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
236 len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Paul Bakker6e339b52013-07-03 13:37:05 +0200237 }
238
239 // Find block that fits
240 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 while (cur != NULL) {
242 if (cur->size >= len) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200243 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200245
Paul Bakker1ef120f2013-07-03 17:20:39 +0200246 cur = cur->next_free;
Paul Bakker6e339b52013-07-03 13:37:05 +0200247 }
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (cur == NULL) {
250 return NULL;
251 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (cur->alloc != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_fprintf(stderr, "FATAL: block in free_list but allocated "
256 "data\n");
Paul Bakker1ef120f2013-07-03 17:20:39 +0200257#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_exit(1);
Paul Bakker1ef120f2013-07-03 17:20:39 +0200259 }
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard6c967b92015-05-27 20:18:39 +0200262 heap.alloc_count++;
Paul Bakker891998e2013-07-03 14:45:05 +0200263#endif
264
Paul Bakker6e339b52013-07-03 13:37:05 +0200265 // Found location, split block if > memory_header + 4 room left
266 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (cur->size - len < sizeof(memory_header) +
268 MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200269 cur->alloc = 1;
270
Paul Bakker1ef120f2013-07-03 17:20:39 +0200271 // Remove from free_list
272 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (cur->prev_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200274 cur->prev_free->next_free = cur->next_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 } else {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200276 heap.first_free = cur->next_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (cur->next_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200280 cur->next_free->prev_free = cur->prev_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200282
283 cur->prev_free = NULL;
284 cur->next_free = NULL;
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200287 heap.total_used += cur->size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (heap.total_used > heap.maximum_used) {
Paul Bakker891998e2013-07-03 14:45:05 +0200289 heap.maximum_used = heap.total_used;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Paul Bakker891998e2013-07-03 14:45:05 +0200291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_MEMORY_BACKTRACE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 trace_cnt = backtrace(trace_buffer, MAX_BT);
294 cur->trace = backtrace_symbols(trace_buffer, trace_cnt);
Paul Bakker6e339b52013-07-03 13:37:05 +0200295 cur->trace_count = trace_cnt;
296#endif
297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if ((heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC) && verify_chain() != 0) {
299 mbedtls_exit(1);
300 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 ret = (unsigned char *) cur + sizeof(memory_header);
303 memset(ret, 0, original_len);
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return ret;
Paul Bakker6e339b52013-07-03 13:37:05 +0200306 }
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 p = ((unsigned char *) cur) + sizeof(memory_header) + len;
Paul Bakker6e339b52013-07-03 13:37:05 +0200309 new = (memory_header *) p;
310
311 new->size = cur->size - len - sizeof(memory_header);
312 new->alloc = 0;
313 new->prev = cur;
314 new->next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_MEMORY_BACKTRACE)
Paul Bakker6e339b52013-07-03 13:37:05 +0200316 new->trace = NULL;
317 new->trace_count = 0;
318#endif
319 new->magic1 = MAGIC1;
320 new->magic2 = MAGIC2;
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (new->next != NULL) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200323 new->next->prev = new;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200325
Paul Bakker1ef120f2013-07-03 17:20:39 +0200326 // Replace cur with new in free_list
327 //
328 new->prev_free = cur->prev_free;
329 new->next_free = cur->next_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (new->prev_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200331 new->prev_free->next_free = new;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 } else {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200333 heap.first_free = new;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (new->next_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200337 new->next_free->prev_free = new;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200339
Paul Bakker6e339b52013-07-03 13:37:05 +0200340 cur->alloc = 1;
341 cur->size = len;
342 cur->next = new;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200343 cur->prev_free = NULL;
344 cur->next_free = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200347 heap.header_count++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (heap.header_count > heap.maximum_header_count) {
Manuel Pégourié-Gonnard70896a02013-12-30 18:06:41 +0100349 heap.maximum_header_count = heap.header_count;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 }
Paul Bakker891998e2013-07-03 14:45:05 +0200351 heap.total_used += cur->size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (heap.total_used > heap.maximum_used) {
Paul Bakker891998e2013-07-03 14:45:05 +0200353 heap.maximum_used = heap.total_used;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
Paul Bakker891998e2013-07-03 14:45:05 +0200355#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_MEMORY_BACKTRACE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 trace_cnt = backtrace(trace_buffer, MAX_BT);
358 cur->trace = backtrace_symbols(trace_buffer, trace_cnt);
Paul Bakker6e339b52013-07-03 13:37:05 +0200359 cur->trace_count = trace_cnt;
360#endif
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if ((heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC) && verify_chain() != 0) {
363 mbedtls_exit(1);
364 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 ret = (unsigned char *) cur + sizeof(memory_header);
367 memset(ret, 0, original_len);
Manuel Pégourié-Gonnard200e7312015-05-26 17:42:13 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return ret;
Paul Bakker6e339b52013-07-03 13:37:05 +0200370}
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static void buffer_alloc_free(void *ptr)
Paul Bakker6e339b52013-07-03 13:37:05 +0200373{
Paul Bakker1ef120f2013-07-03 17:20:39 +0200374 memory_header *hdr, *old = NULL;
Paul Bakker6e339b52013-07-03 13:37:05 +0200375 unsigned char *p = (unsigned char *) ptr;
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (ptr == NULL || heap.buf == NULL || heap.first == NULL) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200378 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (p < heap.buf || p >= heap.buf + heap.len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_fprintf(stderr, "FATAL: mbedtls_free() outside of managed "
384 "space\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200385#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_exit(1);
Paul Bakker6e339b52013-07-03 13:37:05 +0200387 }
388
389 p -= sizeof(memory_header);
390 hdr = (memory_header *) p;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (verify_header(hdr) != 0) {
393 mbedtls_exit(1);
394 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (hdr->alloc != 1) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_fprintf(stderr, "FATAL: mbedtls_free() on unallocated "
399 "data\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_exit(1);
Paul Bakker6e339b52013-07-03 13:37:05 +0200402 }
403
404 hdr->alloc = 0;
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200407 heap.free_count++;
408 heap.total_used -= hdr->size;
409#endif
410
SimonB42256112016-05-02 01:05:22 +0100411#if defined(MBEDTLS_MEMORY_BACKTRACE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 free(hdr->trace);
SimonB42256112016-05-02 01:05:22 +0100413 hdr->trace = NULL;
414 hdr->trace_count = 0;
415#endif
416
Paul Bakker6e339b52013-07-03 13:37:05 +0200417 // Regroup with block before
418 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (hdr->prev != NULL && hdr->prev->alloc == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200421 heap.header_count--;
422#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200423 hdr->prev->size += sizeof(memory_header) + hdr->size;
424 hdr->prev->next = hdr->next;
425 old = hdr;
426 hdr = hdr->prev;
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (hdr->next != NULL) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200429 hdr->next->prev = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 memset(old, 0, sizeof(memory_header));
Paul Bakker6e339b52013-07-03 13:37:05 +0200433 }
434
435 // Regroup with block after
436 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (hdr->next != NULL && hdr->next->alloc == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_MEMORY_DEBUG)
Paul Bakker891998e2013-07-03 14:45:05 +0200439 heap.header_count--;
440#endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200441 hdr->size += sizeof(memory_header) + hdr->next->size;
442 old = hdr->next;
443 hdr->next = hdr->next->next;
444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (hdr->prev_free != NULL || hdr->next_free != NULL) {
446 if (hdr->prev_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200447 hdr->prev_free->next_free = hdr->next_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 } else {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200449 heap.first_free = hdr->next_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (hdr->next_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200453 hdr->next_free->prev_free = hdr->prev_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200455 }
456
457 hdr->prev_free = old->prev_free;
458 hdr->next_free = old->next_free;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (hdr->prev_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200461 hdr->prev_free->next_free = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200463 heap.first_free = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (hdr->next_free != NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200467 hdr->next_free->prev_free = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (hdr->next != NULL) {
Paul Bakker6e339b52013-07-03 13:37:05 +0200471 hdr->next->prev = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 memset(old, 0, sizeof(memory_header));
Paul Bakker6e339b52013-07-03 13:37:05 +0200475 }
476
Paul Bakker1ef120f2013-07-03 17:20:39 +0200477 // Prepend to free_list if we have not merged
478 // (Does not have to stay in same order as prev / next list)
479 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (old == NULL) {
Paul Bakker1ef120f2013-07-03 17:20:39 +0200481 hdr->next_free = heap.first_free;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (heap.first_free != NULL) {
Manuel Pégourié-Gonnard547ff662014-11-26 15:42:16 +0100483 heap.first_free->prev_free = hdr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 }
Paul Bakker1ef120f2013-07-03 17:20:39 +0200485 heap.first_free = hdr;
486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if ((heap.verify & MBEDTLS_MEMORY_VERIFY_FREE) && verify_chain() != 0) {
489 mbedtls_exit(1);
490 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200491}
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493void mbedtls_memory_buffer_set_verify(int verify)
Paul Bakkerbf796ac2013-09-28 11:06:38 +0200494{
495 heap.verify = verify;
496}
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498int mbedtls_memory_buffer_alloc_verify(void)
Paul Bakker6e339b52013-07-03 13:37:05 +0200499{
500 return verify_chain();
501}
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100504void mbedtls_memory_buffer_alloc_status(void)
Paul Bakker6e339b52013-07-03 13:37:05 +0200505{
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 mbedtls_fprintf(stderr,
507 "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
508 "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
509 heap.header_count, heap.total_used,
510 heap.maximum_header_count, heap.maximum_used,
511 heap.maximum_header_count * sizeof(memory_header)
512 + heap.maximum_used,
513 heap.alloc_count, heap.free_count);
Paul Bakker891998e2013-07-03 14:45:05 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (heap.first->next == NULL) {
516 mbedtls_fprintf(stderr, "All memory de-allocated in stack buffer\n");
517 } else {
518 mbedtls_fprintf(stderr, "Memory currently allocated:\n");
Paul Bakker6e339b52013-07-03 13:37:05 +0200519 debug_chain();
520 }
521}
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523void mbedtls_memory_buffer_alloc_count_get(size_t *alloc_count, size_t *free_count)
Manuel Pégourié-Gonnard35415a02022-01-04 10:23:34 +0100524{
525 *alloc_count = heap.alloc_count;
526 *free_count = heap.free_count;
527}
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529void mbedtls_memory_buffer_alloc_max_get(size_t *max_used, size_t *max_blocks)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100530{
531 *max_used = heap.maximum_used;
532 *max_blocks = heap.maximum_header_count;
533}
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535void mbedtls_memory_buffer_alloc_max_reset(void)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100536{
537 heap.maximum_used = 0;
538 heap.maximum_header_count = 0;
539}
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541void mbedtls_memory_buffer_alloc_cur_get(size_t *cur_used, size_t *cur_blocks)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100542{
543 *cur_used = heap.total_used;
544 *cur_blocks = heap.header_count;
545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_MEMORY_DEBUG */
Paul Bakker6e339b52013-07-03 13:37:05 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100549static void *buffer_alloc_calloc_mutexed(size_t n, size_t size)
Paul Bakker1337aff2013-09-29 14:45:34 +0200550{
551 void *buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (mbedtls_mutex_lock(&heap.mutex) != 0) {
553 return NULL;
554 }
555 buf = buffer_alloc_calloc(n, size);
556 if (mbedtls_mutex_unlock(&heap.mutex)) {
557 return NULL;
558 }
559 return buf;
Paul Bakker1337aff2013-09-29 14:45:34 +0200560}
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562static void buffer_alloc_free_mutexed(void *ptr)
Paul Bakker1337aff2013-09-29 14:45:34 +0200563{
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800564 /* We have no good option here, but corrupting the heap seems
565 * worse than losing memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (mbedtls_mutex_lock(&heap.mutex)) {
Manuel Pégourié-Gonnardbdd78282015-04-24 14:42:53 +0200567 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 }
569 buffer_alloc_free(ptr);
570 (void) mbedtls_mutex_unlock(&heap.mutex);
Paul Bakker1337aff2013-09-29 14:45:34 +0200571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_THREADING_C */
Paul Bakker1337aff2013-09-29 14:45:34 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574void mbedtls_memory_buffer_alloc_init(unsigned char *buf, size_t len)
Paul Bakker6e339b52013-07-03 13:37:05 +0200575{
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 memset(&heap, 0, sizeof(buffer_alloc_ctx));
Paul Bakker6e339b52013-07-03 13:37:05 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_mutex_init(&heap.mutex);
580 mbedtls_platform_set_calloc_free(buffer_alloc_calloc_mutexed,
581 buffer_alloc_free_mutexed);
Paul Bakker1337aff2013-09-29 14:45:34 +0200582#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_platform_set_calloc_free(buffer_alloc_calloc, buffer_alloc_free);
Paul Bakker1337aff2013-09-29 14:45:34 +0200584#endif
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (len < sizeof(memory_header) + MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
Andres AG9cf1f962017-01-30 14:34:25 +0000587 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else if ((size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100589 /* Adjust len first since buf is used in the computation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
Manuel Pégourié-Gonnard82a5de72014-05-05 14:05:24 +0200594 }
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 memset(buf, 0, len);
Andres AG9cf1f962017-01-30 14:34:25 +0000597
Paul Bakker6e339b52013-07-03 13:37:05 +0200598 heap.buf = buf;
599 heap.len = len;
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 heap.first = (memory_header *) buf;
602 heap.first->size = len - sizeof(memory_header);
Paul Bakker6e339b52013-07-03 13:37:05 +0200603 heap.first->magic1 = MAGIC1;
604 heap.first->magic2 = MAGIC2;
Paul Bakker1ef120f2013-07-03 17:20:39 +0200605 heap.first_free = heap.first;
Paul Bakker6e339b52013-07-03 13:37:05 +0200606}
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608void mbedtls_memory_buffer_alloc_free(void)
Paul Bakker1337aff2013-09-29 14:45:34 +0200609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_mutex_free(&heap.mutex);
Paul Bakker1337aff2013-09-29 14:45:34 +0200612#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_platform_zeroize(&heap, sizeof(buffer_alloc_ctx));
Paul Bakker1337aff2013-09-29 14:45:34 +0200614}
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine449bd832023-01-11 14:50:10 +0100617static int check_pointer(void *p)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100618{
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (p == NULL) {
620 return -1;
621 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if ((size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0) {
624 return -1;
625 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630static int check_all_free(void)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100631{
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard491a3fe2015-02-05 12:08:47 +0100634 heap.total_used != 0 ||
635#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100636 heap.first != heap.first_free ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 (void *) heap.first != (void *) heap.buf) {
638 return -1;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100639 }
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100642}
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644#define TEST_ASSERT(condition) \
645 if (!(condition)) \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100646 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (verbose != 0) \
648 mbedtls_printf("failed\n"); \
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100649 \
650 ret = 1; \
651 goto cleanup; \
652 }
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654int mbedtls_memory_buffer_alloc_self_test(int verbose)
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100655{
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100656 unsigned char buf[1024];
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100657 unsigned char *p, *q, *r, *end;
658 int ret = 0;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (verbose != 0) {
661 mbedtls_printf(" MBA test #1 (basic alloc-free cycle): ");
662 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 p = mbedtls_calloc(1, 1);
667 q = mbedtls_calloc(1, 128);
668 r = mbedtls_calloc(1, 16);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 TEST_ASSERT(check_pointer(p) == 0 &&
671 check_pointer(q) == 0 &&
672 check_pointer(r) == 0);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_free(r);
675 mbedtls_free(q);
676 mbedtls_free(p);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 TEST_ASSERT(check_all_free() == 0);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100679
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100680 /* Memorize end to compare with the next test */
681 end = heap.buf + heap.len;
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (verbose != 0) {
686 mbedtls_printf("passed\n");
687 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (verbose != 0) {
690 mbedtls_printf(" MBA test #2 (buf not aligned): ");
691 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 mbedtls_memory_buffer_alloc_init(buf + 1, sizeof(buf) - 1);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 TEST_ASSERT(heap.buf + heap.len == end);
Manuel Pégourié-Gonnard5dd28ea2014-11-27 13:57:42 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 p = mbedtls_calloc(1, 1);
698 q = mbedtls_calloc(1, 128);
699 r = mbedtls_calloc(1, 16);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 TEST_ASSERT(check_pointer(p) == 0 &&
702 check_pointer(q) == 0 &&
703 check_pointer(r) == 0);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 mbedtls_free(r);
706 mbedtls_free(q);
707 mbedtls_free(p);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 TEST_ASSERT(check_all_free() == 0);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (verbose != 0) {
714 mbedtls_printf("passed\n");
715 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (verbose != 0) {
718 mbedtls_printf(" MBA test #3 (full): ");
719 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 p = mbedtls_calloc(1, sizeof(buf) - sizeof(memory_header));
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 TEST_ASSERT(check_pointer(p) == 0);
726 TEST_ASSERT(mbedtls_calloc(1, 1) == NULL);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_free(p);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 p = mbedtls_calloc(1, sizeof(buf) - 2 * sizeof(memory_header) - 16);
731 q = mbedtls_calloc(1, 16);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_ASSERT(check_pointer(p) == 0 && check_pointer(q) == 0);
734 TEST_ASSERT(mbedtls_calloc(1, 1) == NULL);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_free(q);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 TEST_ASSERT(mbedtls_calloc(1, 17) == NULL);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 mbedtls_free(p);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 TEST_ASSERT(check_all_free() == 0);
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (verbose != 0) {
747 mbedtls_printf("passed\n");
748 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100749
750cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return ret;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */