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

Conversation

mikbras
Copy link
Contributor

@mikbras mikbras commented Jan 19, 2023

This patch fixes a bug in musl libc memalign when using debug-malloc. The function myst_debug_posix_memalign() was calling into musl's memalign function, which was calling back into myst_debug_malloc(), which would have resulted in a double debug-malloc header and trailer. Consequently, free() crashed when releasing memory obtained with memalign().

@@ -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).

@@ -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.

** | |
** 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.

@mikbras
Copy link
Contributor Author

mikbras commented Jan 20, 2023

@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. :)

@mikbras mikbras merged commit a2010ab into deislabs:main Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants