Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memalign error in debug-malloc #1448

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions kernel/debugmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include <myst/backtrace.h>
#include <myst/printf.h>
#include <myst/debugmalloc.h>
#include <myst/defs.h>
#include <myst/malloc.h>
Expand All @@ -20,6 +22,8 @@
#define ENABLE_MALLOC_MEMSET
#define ENABLE_FREE_MEMSET

// #define DEBUG_MEMALIGN

/*
**==============================================================================
**
Expand Down Expand Up @@ -173,6 +177,12 @@ MYST_INLINE void* _get_block_address(void* ptr)
return (uint8_t*)ptr - sizeof(header_t) - padding_size;
}

MYST_INLINE void* _get_block_address_v2(void* ptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need _get_block_address v1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could eliminate v1, but I left it as a cross check in case the two functions fail to produce the same quantity. The v2 version is just intended as a sanity check (so if v1 != v2 then we return EINVAL).

{
header_t* header = _get_header(ptr);
return (uint8_t*)header - header->padding;
}

MYST_INLINE size_t _calculate_block_size(size_t alignment, size_t size)
{
size_t r = 0;
Expand Down Expand Up @@ -330,6 +340,17 @@ void myst_debug_free(void* ptr)

void* block = _get_block_address(ptr);

/* sanity cross check for block address */
if (block != _get_block_address_v2(ptr))
{
assert("_get_block_address_v2() failed");
}

#ifdef DEBUG_MEMALIGN
printf("free: block=%p header=%p delta=%zu\n",
block, header, (const uint8_t*)header - (const uint8_t*)block);
#endif

/* fill block with 0xdd (deallocated) bytes */
#ifdef ENABLE_FREE_MEMSET
memset(block, 0xDD, _get_block_size(ptr));
Expand Down Expand Up @@ -393,10 +414,39 @@ int myst_debug_posix_memalign(void** memptr, size_t alignment, size_t size)
const size_t block_size = _calculate_block_size(alignment, size);
void* block = NULL;
header_t* header = NULL;
size_t rsize = _round_up_to_multiple(size, sizeof(uint64_t));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to align the data size to 8 bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data is padded at the end up to an 8-byte boundary. This forces the footer to be aligned on an 8-byte boundary.


if (memptr)
*memptr = NULL;

if (!memptr)
return EINVAL;

/*
** [padding][header][block][footer]
** ^ ^
** | |
** X Y
**
** Note: both X and Y are on the alignment boundary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who do we need to align the header, but not the footer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! We align the footer on an 8-byte-boundary but my picture did not reflect that. I updated the picture and added some more explanation.

*/

#ifdef DEBUG_MEMALIGN
printf("memalign: padding=%zu header=%zu block=%zu footer=%zu\n",
padding_size, sizeof(header_t), size, sizeof(footer_t));
#endif

/* the sum of the parts should add up to total block size */
if (padding_size + sizeof(header_t) + rsize + sizeof(footer_t)
!= block_size)
{
return EINVAL;
}

/* the data should be aligned on the given boundary */
if ((padding_size + sizeof(header_t)) % alignment)
return EINVAL;

if (!_is_ptrsize_multiple(alignment) || !_is_pow2(alignment))
return EINVAL;

Expand All @@ -421,6 +471,7 @@ void* myst_debug_memalign(size_t alignment, size_t size)
alignment = _round_up_to_multiple(alignment, sizeof(void*));

myst_debug_posix_memalign(&ptr, alignment, size);

return ptr;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/debugmalloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ ifdef PERF
OPTS += --perf
endif

OPTS += --thread-stack-size=1048576 --crt-memcheck
OPTS += --thread-stack-size=1048576
OPTS += --crt-memcheck
OPTS += --nobrk
#OPTS += --memory-size=1g

tests: all
$(RUNTEST) $(MYST_EXEC) rootfs /bin/debugmalloc $(OPTS)
Expand Down
105 changes: 105 additions & 0 deletions tests/debugmalloc/debugmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define _GNU_SOURCE
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -44,8 +45,112 @@ void stop()
{
}

void test_malloc()
{
void** ptrs;
size_t* sizes;
const size_t n = 1024*10;

if (!(ptrs = calloc(n, sizeof(void*))))
{
fprintf(stderr, "calloc() failed\n");
exit(1);
}

if (!(sizes = calloc(n, sizeof(size_t))))
{
fprintf(stderr, "calloc() failed\n");
exit(1);
}

/* malloc */
for (size_t i = 0; i < n; i++)
{
size_t size = rand() % 64 + 1;

if (!(ptrs[i] = memalign(128, size)))
{
fprintf(stderr, "memalign() failed\n");
exit(1);
}

memset(ptrs[i], '\0', size);
sizes[i] = size;
}

/* realloc */
for (size_t i = 0; i < n; i++)
{
size_t size = rand() % 128 + 1;

memset(ptrs[i], '\0', sizes[i]);

if (!(ptrs[i] = realloc(ptrs[i], size)))
{
fprintf(stderr, "malloc() failed\n");
exit(1);
}

memset(ptrs[i], '\0', size);
sizes[i] = size;
}

/* clear */
for (size_t i = 0; i < n; i++)
{
memset(ptrs[i], '\0', sizes[i]);
}

/* free every other and allocate new */
for (size_t i = 0; i < n; i += 2)
{
size_t size = rand() % 128 + 1;

memset(ptrs[i], '\0', sizes[i]);
free(ptrs[i]);
ptrs[i] = NULL;
sizes[i] = 0;

if (!(ptrs[i] = malloc(size)))
{
fprintf(stderr, "malloc() failed\n");
exit(1);
}

memset(ptrs[i], '\0', size);
sizes[i] = size;
}

/* free */
for (size_t i = 0; i < n; i++)
{
memset(ptrs[i], '\0', sizes[i]);
free(ptrs[i]);
sizes[i] = 0;
ptrs[i] = NULL;
}

free(ptrs);
free(sizes);
}

void test_malloc2()
{
void* ptr;

if (!(ptr = memalign(256, 16)))
{
fprintf(stderr, "memalign() failed\n");
exit(1);
}
free(ptr);
}

int main(int argc, const char* argv[])
{
test_malloc();
debug_malloc_check(true);

size_t count;
a();

Expand Down
20 changes: 17 additions & 3 deletions third_party/musl/crt/patch.diff
Original file line number Diff line number Diff line change
Expand Up @@ -963,19 +963,33 @@ index 672b518a..0f9a8057 100644
+ return libc_malloc_usable_size(p);
+}
diff --git a/src/malloc/memalign.c b/src/malloc/memalign.c
index cf9dfbda..1aba670c 100644
index cf9dfbda..ac1fca21 100644
--- a/src/malloc/memalign.c
+++ b/src/malloc/memalign.c
@@ -3,7 +3,7 @@
@@ -3,7 +3,9 @@
#include <errno.h>
#include "malloc_impl.h"

-void *__memalign(size_t align, size_t len)
+void *libc_malloc(size_t n);
+
+void *libc_memalign(size_t align, size_t len)
{
unsigned char *mem, *new;

@@ -51,4 +51,14 @@ void *__memalign(size_t align, size_t len)
@@ -18,9 +20,9 @@ void *__memalign(size_t align, size_t len)
}

if (align <= SIZE_ALIGN)
- return malloc(len);
+ return libc_malloc(len);

- if (!(mem = malloc(len + align-1)))
+ if (!(mem = libc_malloc(len + align-1)))
return 0;

new = (void *)((uintptr_t)mem + align-1 & -align);
@@ -51,4 +53,14 @@ void *__memalign(size_t align, size_t len)
return new;
}

Expand Down