fix(c++): fix misaligned address access errors detected by UBSan in buffer.h#3479
Open
BaldDemian wants to merge 2 commits intoapache:mainfrom
Open
fix(c++): fix misaligned address access errors detected by UBSan in buffer.h#3479BaldDemian wants to merge 2 commits intoapache:mainfrom
BaldDemian wants to merge 2 commits intoapache:mainfrom
Conversation
Collaborator
|
Does thos fix affect the performance? Could you run benchmarks/cpp And compare the performance |
Author
|
after_README.md (in |
Author
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.


Why?
buffer.hreads and writes multi-byte integers (16/32/64-bit) directly into a rawuint8_t*buffer. The original code cast the byte pointer to a typed pointer and dereferenced it:Read:
return reinterpret_cast<const T*>(data_ + offset)[0];Write:
*reinterpret_cast<T*>(data_ + offset) = value;Dereferencing a pointer that is not aligned to
alignof(T)is considered UB in C++. UBSan correctly flagged these as misaligned address runtime errors, becausedata_ + offsetcan be at any byte boundary.What does this PR do?
Two helper templates were added to the
buffer.h:All
reinterpret_castcalls that may lead to UB in the file were replaced with calls to these helpers.No UB were detected when running
bazel test --cache_test_results=no --config=x86_64 --config=ubsan $(bazel query //...)after applying this patch.Details can be found in ubsan_report.txt.
Only a few
unused-but-set-parameterwarnings were detected by UBSan.Related issues
Fix #3459
AI Contribution Checklist
yes/noyes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.Does this PR introduce any user-facing change?
Benchmark
I believe replacing
reinterpret_castwithmemcpywon't incur much runtime burden, sincememcpycan be optimized by both GCC and Clang effectively.