2018-02-08 18:04:43 +00:00
|
|
|
/*
|
|
|
|
* Buffer-based memory allocator
|
|
|
|
*
|
2020-09-05 10:53:20 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-01-30 13:09:13 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-02-08 18:04:43 +00:00
|
|
|
*/
|
|
|
|
|
2021-12-21 11:54:05 +00:00
|
|
|
#include "common.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
|
|
|
|
|
|
/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
|
|
|
|
is dependent upon MBEDTLS_PLATFORM_C */
|
|
|
|
#include "mbedtls/platform.h"
|
2018-06-07 14:25:01 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
#include "mbedtls/threading.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAGIC1 0xFF00AA55
|
|
|
|
#define MAGIC2 0xEE119966
|
|
|
|
#define MAX_BT 20
|
|
|
|
|
|
|
|
typedef struct _memory_header memory_header;
|
2023-04-18 08:38:24 +00:00
|
|
|
struct _memory_header {
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t magic1;
|
|
|
|
size_t size;
|
|
|
|
size_t alloc;
|
|
|
|
memory_header *prev;
|
|
|
|
memory_header *next;
|
|
|
|
memory_header *prev_free;
|
|
|
|
memory_header *next_free;
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
|
|
|
char **trace;
|
|
|
|
size_t trace_count;
|
|
|
|
#endif
|
|
|
|
size_t magic2;
|
|
|
|
};
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
typedef struct {
|
2018-02-08 18:04:43 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t len;
|
|
|
|
memory_header *first;
|
|
|
|
memory_header *first_free;
|
|
|
|
int verify;
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
size_t alloc_count;
|
|
|
|
size_t free_count;
|
|
|
|
size_t total_used;
|
|
|
|
size_t maximum_used;
|
|
|
|
size_t header_count;
|
|
|
|
size_t maximum_header_count;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
buffer_alloc_ctx;
|
|
|
|
|
|
|
|
static buffer_alloc_ctx heap;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
static void debug_header(memory_header *hdr)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
|
|
|
size_t i;
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
|
|
|
|
"ALLOC(%zu), SIZE(%10zu)\n",
|
|
|
|
(size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
|
|
|
|
hdr->alloc, hdr->size);
|
|
|
|
mbedtls_fprintf(stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
|
|
|
|
(size_t) hdr->prev_free, (size_t) hdr->next_free);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "TRACE: \n");
|
|
|
|
for (i = 0; i < hdr->trace_count; i++) {
|
|
|
|
mbedtls_fprintf(stderr, "%s\n", hdr->trace[i]);
|
|
|
|
}
|
|
|
|
mbedtls_fprintf(stderr, "\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static void debug_chain(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
memory_header *cur = heap.first;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "\nBlock list\n");
|
|
|
|
while (cur != NULL) {
|
|
|
|
debug_header(cur);
|
2018-02-08 18:04:43 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "Free list\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
cur = heap.first_free;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
while (cur != NULL) {
|
|
|
|
debug_header(cur);
|
2018-02-08 18:04:43 +00:00
|
|
|
cur = cur->next_free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_MEMORY_DEBUG */
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static int verify_header(memory_header *hdr)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->magic1 != MAGIC1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: MAGIC1 mismatch\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->magic2 != MAGIC2) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: MAGIC2 mismatch\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->alloc > 1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: alloc has illegal value\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->prev != NULL && hdr->prev == hdr->next) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: prev == next\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->prev_free != NULL && hdr->prev_free == hdr->next_free) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: prev_free == next_free\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static int verify_chain(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2018-03-28 15:26:33 +00:00
|
|
|
memory_header *prv = heap.first, *cur;
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (prv == NULL || verify_header(prv) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: verification of first header "
|
|
|
|
"failed\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.first->prev != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: verification failed: "
|
|
|
|
"first->prev != NULL\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 15:26:33 +00:00
|
|
|
cur = heap.first->next;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
while (cur != NULL) {
|
|
|
|
if (verify_header(cur) != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: verification of header "
|
|
|
|
"failed\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->prev != prv) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: verification failed: "
|
|
|
|
"cur->prev != prv\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
return 1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prv = cur;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static void *buffer_alloc_calloc(size_t n, size_t size)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
memory_header *new, *cur = heap.first_free;
|
|
|
|
unsigned char *p;
|
|
|
|
void *ret;
|
|
|
|
size_t original_len, len;
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
|
|
|
void *trace_buffer[MAX_BT];
|
|
|
|
size_t trace_cnt;
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.buf == NULL || heap.first == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
original_len = len = n * size;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (n == 0 || size == 0 || len / n != size) {
|
|
|
|
return NULL;
|
|
|
|
} else if (len > (size_t) -MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (len % MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
|
2018-02-08 18:04:43 +00:00
|
|
|
len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
|
|
|
len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find block that fits
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
while (cur != NULL) {
|
|
|
|
if (cur->size >= len) {
|
2018-02-08 18:04:43 +00:00
|
|
|
break;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
cur = cur->next_free;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->alloc != 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: block in free_list but allocated "
|
|
|
|
"data\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_exit(1);
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.alloc_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Found location, split block if > memory_header + 4 room left
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->size - len < sizeof(memory_header) +
|
|
|
|
MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
|
2018-02-08 18:04:43 +00:00
|
|
|
cur->alloc = 1;
|
|
|
|
|
|
|
|
// Remove from free_list
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->prev_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
cur->prev_free->next_free = cur->next_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
} else {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free = cur->next_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->next_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
cur->next_free->prev_free = cur->prev_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
cur->prev_free = NULL;
|
|
|
|
cur->next_free = NULL;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.total_used += cur->size;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.total_used > heap.maximum_used) {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.maximum_used = heap.total_used;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
2023-04-18 08:38:24 +00:00
|
|
|
trace_cnt = backtrace(trace_buffer, MAX_BT);
|
|
|
|
cur->trace = backtrace_symbols(trace_buffer, trace_cnt);
|
2018-02-08 18:04:43 +00:00
|
|
|
cur->trace_count = trace_cnt;
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC) && verify_chain() != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
ret = (unsigned char *) cur + sizeof(memory_header);
|
|
|
|
memset(ret, 0, original_len);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
p = ((unsigned char *) cur) + sizeof(memory_header) + len;
|
2018-02-08 18:04:43 +00:00
|
|
|
new = (memory_header *) p;
|
|
|
|
|
|
|
|
new->size = cur->size - len - sizeof(memory_header);
|
|
|
|
new->alloc = 0;
|
|
|
|
new->prev = cur;
|
|
|
|
new->next = cur->next;
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
|
|
|
new->trace = NULL;
|
|
|
|
new->trace_count = 0;
|
|
|
|
#endif
|
|
|
|
new->magic1 = MAGIC1;
|
|
|
|
new->magic2 = MAGIC2;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (new->next != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
new->next->prev = new;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
// Replace cur with new in free_list
|
|
|
|
//
|
|
|
|
new->prev_free = cur->prev_free;
|
|
|
|
new->next_free = cur->next_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (new->prev_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
new->prev_free->next_free = new;
|
2023-04-18 08:38:24 +00:00
|
|
|
} else {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free = new;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (new->next_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
new->next_free->prev_free = new;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
cur->alloc = 1;
|
|
|
|
cur->size = len;
|
|
|
|
cur->next = new;
|
|
|
|
cur->prev_free = NULL;
|
|
|
|
cur->next_free = NULL;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.header_count++;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.header_count > heap.maximum_header_count) {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.maximum_header_count = heap.header_count;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.total_used += cur->size;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.total_used > heap.maximum_used) {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.maximum_used = heap.total_used;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
2023-04-18 08:38:24 +00:00
|
|
|
trace_cnt = backtrace(trace_buffer, MAX_BT);
|
|
|
|
cur->trace = backtrace_symbols(trace_buffer, trace_cnt);
|
2018-02-08 18:04:43 +00:00
|
|
|
cur->trace_count = trace_cnt;
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC) && verify_chain() != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
ret = (unsigned char *) cur + sizeof(memory_header);
|
|
|
|
memset(ret, 0, original_len);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static void buffer_alloc_free(void *ptr)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
memory_header *hdr, *old = NULL;
|
|
|
|
unsigned char *p = (unsigned char *) ptr;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (ptr == NULL || heap.buf == NULL || heap.first == NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
return;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (p < heap.buf || p >= heap.buf + heap.len) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: mbedtls_free() outside of managed "
|
|
|
|
"space\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_exit(1);
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p -= sizeof(memory_header);
|
|
|
|
hdr = (memory_header *) p;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verify_header(hdr) != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->alloc != 1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr, "FATAL: mbedtls_free() on unallocated "
|
|
|
|
"data\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_exit(1);
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr->alloc = 0;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.free_count++;
|
|
|
|
heap.total_used -= hdr->size;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
2023-04-18 08:38:24 +00:00
|
|
|
free(hdr->trace);
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->trace = NULL;
|
|
|
|
hdr->trace_count = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Regroup with block before
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->prev != NULL && hdr->prev->alloc == 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.header_count--;
|
|
|
|
#endif
|
|
|
|
hdr->prev->size += sizeof(memory_header) + hdr->size;
|
|
|
|
hdr->prev->next = hdr->next;
|
|
|
|
old = hdr;
|
|
|
|
hdr = hdr->prev;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->next != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->next->prev = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
memset(old, 0, sizeof(memory_header));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regroup with block after
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->next != NULL && hdr->next->alloc == 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.header_count--;
|
|
|
|
#endif
|
|
|
|
hdr->size += sizeof(memory_header) + hdr->next->size;
|
|
|
|
old = hdr->next;
|
|
|
|
hdr->next = hdr->next->next;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->prev_free != NULL || hdr->next_free != NULL) {
|
|
|
|
if (hdr->prev_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->prev_free->next_free = hdr->next_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
} else {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free = hdr->next_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->next_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->next_free->prev_free = hdr->prev_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr->prev_free = old->prev_free;
|
|
|
|
hdr->next_free = old->next_free;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->prev_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->prev_free->next_free = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
} else {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->next_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->next_free->prev_free = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hdr->next != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->next->prev = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
memset(old, 0, sizeof(memory_header));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend to free_list if we have not merged
|
|
|
|
// (Does not have to stay in same order as prev / next list)
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
if (old == NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
hdr->next_free = heap.first_free;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (heap.first_free != NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free->prev_free = hdr;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first_free = hdr;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((heap.verify & MBEDTLS_MEMORY_VERIFY_FREE) && verify_chain() != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_set_verify(int verify)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
heap.verify = verify;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_memory_buffer_alloc_verify(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
return verify_chain();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_status(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"Current use: %zu blocks / %zu bytes, max: %zu blocks / "
|
|
|
|
"%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
|
|
|
|
heap.header_count, heap.total_used,
|
|
|
|
heap.maximum_header_count, heap.maximum_used,
|
|
|
|
heap.maximum_header_count * sizeof(memory_header)
|
|
|
|
+ heap.maximum_used,
|
|
|
|
heap.alloc_count, heap.free_count);
|
|
|
|
|
|
|
|
if (heap.first->next == NULL) {
|
|
|
|
mbedtls_fprintf(stderr, "All memory de-allocated in stack buffer\n");
|
|
|
|
} else {
|
|
|
|
mbedtls_fprintf(stderr, "Memory currently allocated:\n");
|
2018-02-08 18:04:43 +00:00
|
|
|
debug_chain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 03:04:06 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_count_get(size_t *alloc_count, size_t *free_count)
|
|
|
|
{
|
|
|
|
*alloc_count = heap.alloc_count;
|
|
|
|
*free_count = heap.free_count;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_max_get(size_t *max_used, size_t *max_blocks)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
*max_used = heap.maximum_used;
|
|
|
|
*max_blocks = heap.maximum_header_count;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_max_reset(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
heap.maximum_used = 0;
|
|
|
|
heap.maximum_header_count = 0;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_cur_get(size_t *cur_used, size_t *cur_blocks)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
*cur_used = heap.total_used;
|
|
|
|
*cur_blocks = heap.header_count;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_MEMORY_DEBUG */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-04-18 08:38:24 +00:00
|
|
|
static void *buffer_alloc_calloc_mutexed(size_t n, size_t size)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
void *buf;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mutex_lock(&heap.mutex) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = buffer_alloc_calloc(n, size);
|
|
|
|
if (mbedtls_mutex_unlock(&heap.mutex)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return buf;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static void buffer_alloc_free_mutexed(void *ptr)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2022-07-18 12:48:00 +00:00
|
|
|
/* We have no good option here, but corrupting the heap seems
|
|
|
|
* worse than losing memory. */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_mutex_lock(&heap.mutex)) {
|
2018-02-08 18:04:43 +00:00
|
|
|
return;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
|
|
|
buffer_alloc_free(ptr);
|
|
|
|
(void) mbedtls_mutex_unlock(&heap.mutex);
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_THREADING_C */
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_init(unsigned char *buf, size_t len)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
memset(&heap, 0, sizeof(buffer_alloc_ctx));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mutex_init(&heap.mutex);
|
|
|
|
mbedtls_platform_set_calloc_free(buffer_alloc_calloc_mutexed,
|
|
|
|
buffer_alloc_free_mutexed);
|
2018-02-08 18:04:43 +00:00
|
|
|
#else
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_platform_set_calloc_free(buffer_alloc_calloc, buffer_alloc_free);
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (len < sizeof(memory_header) + MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
|
2018-03-28 15:26:33 +00:00
|
|
|
return;
|
2023-04-18 08:38:24 +00:00
|
|
|
} else if ((size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE) {
|
2018-02-08 18:04:43 +00:00
|
|
|
/* Adjust len first since buf is used in the computation */
|
|
|
|
len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
|
2023-04-18 08:38:24 +00:00
|
|
|
- (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
2018-02-08 18:04:43 +00:00
|
|
|
buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
|
2023-04-18 08:38:24 +00:00
|
|
|
- (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
memset(buf, 0, len);
|
2018-03-28 15:26:33 +00:00
|
|
|
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.buf = buf;
|
|
|
|
heap.len = len;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
heap.first = (memory_header *) buf;
|
|
|
|
heap.first->size = len - sizeof(memory_header);
|
2018-02-08 18:04:43 +00:00
|
|
|
heap.first->magic1 = MAGIC1;
|
|
|
|
heap.first->magic2 = MAGIC2;
|
|
|
|
heap.first_free = heap.first;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_memory_buffer_alloc_free(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_mutex_free(&heap.mutex);
|
2018-02-08 18:04:43 +00:00
|
|
|
#endif
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_platform_zeroize(&heap, sizeof(buffer_alloc_ctx));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2023-04-18 08:38:24 +00:00
|
|
|
static int check_pointer(void *p)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static int check_all_free(void)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
if (
|
2018-02-08 18:04:43 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
heap.total_used != 0 ||
|
|
|
|
#endif
|
|
|
|
heap.first != heap.first_free ||
|
2023-04-18 08:38:24 +00:00
|
|
|
(void *) heap.first != (void *) heap.buf) {
|
|
|
|
return -1;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
#define TEST_ASSERT(condition) \
|
|
|
|
if (!(condition)) \
|
2018-02-08 18:04:43 +00:00
|
|
|
{ \
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) \
|
|
|
|
mbedtls_printf("failed\n"); \
|
2018-02-08 18:04:43 +00:00
|
|
|
\
|
|
|
|
ret = 1; \
|
|
|
|
goto cleanup; \
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_memory_buffer_alloc_self_test(int verbose)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
unsigned char buf[1024];
|
|
|
|
unsigned char *p, *q, *r, *end;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MBA test #1 (basic alloc-free cycle): ");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
p = mbedtls_calloc(1, 1);
|
|
|
|
q = mbedtls_calloc(1, 128);
|
|
|
|
r = mbedtls_calloc(1, 16);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_pointer(p) == 0 &&
|
|
|
|
check_pointer(q) == 0 &&
|
|
|
|
check_pointer(r) == 0);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_free(r);
|
|
|
|
mbedtls_free(q);
|
|
|
|
mbedtls_free(p);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_all_free() == 0);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Memorize end to compare with the next test */
|
|
|
|
end = heap.buf + heap.len;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MBA test #2 (buf not aligned): ");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_init(buf + 1, sizeof(buf) - 1);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(heap.buf + heap.len == end);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
p = mbedtls_calloc(1, 1);
|
|
|
|
q = mbedtls_calloc(1, 128);
|
|
|
|
r = mbedtls_calloc(1, 16);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_pointer(p) == 0 &&
|
|
|
|
check_pointer(q) == 0 &&
|
|
|
|
check_pointer(r) == 0);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_free(r);
|
|
|
|
mbedtls_free(q);
|
|
|
|
mbedtls_free(p);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_all_free() == 0);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" MBA test #3 (full): ");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
p = mbedtls_calloc(1, sizeof(buf) - sizeof(memory_header));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_pointer(p) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_calloc(1, 1) == NULL);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_free(p);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
p = mbedtls_calloc(1, sizeof(buf) - 2 * sizeof(memory_header) - 16);
|
|
|
|
q = mbedtls_calloc(1, 16);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_pointer(p) == 0 && check_pointer(q) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_calloc(1, 1) == NULL);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_free(q);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(mbedtls_calloc(1, 17) == NULL);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_free(p);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
TEST_ASSERT(check_all_free() == 0);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
|