-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
@@ -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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
kernel/debugmalloc.c
Outdated
** | | | ||
** X Y | ||
** | ||
** Note: both X and Y are on the alignment boundary |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@jxyang: Thank you for your review. I committed changes to the comments to clarify that the footer is aligned. Also, I am on the fence about whether to remove the V1 function. For now, I left it as a cross check in the unlikely event that V1 and V2 don't produce the same result. :) |
This patch fixes a bug in musl libc
memalign
when using debug-malloc. The functionmyst_debug_posix_memalign()
was calling into musl'smemalign
function, which was calling back intomyst_debug_malloc()
, which would have resulted in a double debug-malloc header and trailer. Consequently,free()
crashed when releasing memory obtained withmemalign()
.