Repository navigation
Align storage for the memory_map singleton - #298
Merged
Merged
Conversation
`memory_map::get_instance()` constructs a `memory_map` in a function-local `static char` array. That declaration does not guarantee the alignment required by `memory_map`, so placement construction can use under-aligned storage and cause undefined behavior. Add `alignas(memory_map)` to the buffer, matching the profiler singleton fix in acd0081.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
memory_map::get_instance()constructs amemory_mapin a function-localstatic chararray. The declaration does not guaranteealignof(memory_map)(eight bytes in the tested AArch64 build), so placement construction can use
under-aligned storage and cause undefined behavior.
Add
alignas(memory_map)to the buffer, matching the profiler singleton fixin #288 (acd0081).
Reproducer and observed failure
Tested on Linux AArch64, Ubuntu 24.04, Clang 18.1.3, on commit
256e3c389424d6a1691ee5d18b5ecb5a6c1001ec.Save this as
memory-map-poc.cppin the Coz checkout. It uses the realmemory_mapdefinition frominspect.hand copies the original getter.The preceding one-byte global deliberately perturbs the storage layout to
expose the missing alignment requirement.
Compile and run:
UBSan crash:
After this patch, which changes the buffer declaration to
alignas(memory_map) static char buf[sizeof(memory_map)];the same testexit with status 0 and no sanitizer crash. The failure depends on specific compiler
layout: GCC 13.3 happened to align the buffer in the earlier test.
Verification
Tested on Clang 18.1.3
RelWithDebInfobuild, C++11.The integration tests were run with:
ctest --test-dir build-clang --output-on-failure \ -R '^(path_filter|dwarf_scope_filter)$'path_filterdwarf_scope_filterwith DWARF 5