-
Couldn't load subscription status.
- Fork 5.2k
Fix misaligned memory writes in i386 and amd64 dynamic helper code generation #118680
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
Conversation
Co-authored-by: jkotas <[email protected]>
|
@copilot Could you please apply similar fixes in cgenamd64.cpp as well? |
Co-authored-by: jkotas <[email protected]>
Applied similar fixes to cgenamd64.cpp in commit dd5087a. Replaced 58 instances of misaligned memory writes with appropriate SET_UNALIGNED_* macros, following the same pattern as the i386 fixes. CoreCLR builds successfully with the changes. |
|
When I did a regex ( |
Majority of hits found by this pattern are false positives. |
Co-authored-by: jkotas <[email protected]>
Co-authored-by: jkotas <[email protected]>
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.
Pull Request Overview
This PR fixes undefined behavior in CoreCLR's dynamic helper code generation by replacing misaligned memory writes with safe unaligned access macros. The changes address potential crashes and undefined behavior that can be detected by sanitizers when the memory pointer is not properly aligned for the write operation size.
Key changes:
- Replace direct pointer dereferences with
SET_UNALIGNED_*macros in i386 and amd64 code generation - Ensure safe memory access regardless of alignment for 16-bit, 32-bit, and 64-bit writes
- Preserve all existing functionality while eliminating undefined behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/coreclr/vm/i386/cgenx86.cpp | Replace 30 instances of misaligned memory writes with SET_UNALIGNED_16 and SET_UNALIGNED_32 macros |
| src/coreclr/vm/amd64/cgenamd64.cpp | Replace 61 instances of misaligned memory writes with SET_UNALIGNED_16, SET_UNALIGNED_32, and SET_UNALIGNED_64 macros |
|
Tagging subscribers to this area: @mangod9 |
This PR fixes undefined behavior caused by misaligned memory writes in the CoreCLR dynamic helper code generation for both i386 and amd64 architectures.
Problem
The code in
src/coreclr/vm/i386/cgenx86.cppandsrc/coreclr/vm/amd64/cgenamd64.cppwas performing direct 32-bit, 16-bit, and 64-bit memory writes using pointer dereferences like*(INT32 *)p = value,*(UINT16 *)p = value, and*(TADDR *)p = value. When the pointerpis not aligned to the appropriate byte boundaries (4-byte for INT32, 2-byte for UINT16, 8-byte for TADDR on AMD64), this causes undefined behavior that can be detected by clang's undefined behavior sanitizer (-fsanitize=undefined).Solution
Replaced all misaligned memory access patterns with the appropriate
SET_UNALIGNED_*macros that are specifically designed to handle potentially unaligned memory access safely:i386 (cgenx86.cpp):
*(INT32 *)p = value→SET_UNALIGNED_32(p, value)*(UINT16 *)p = value→SET_UNALIGNED_16(p, value)CreateDictionaryLookupHelper:*(UINT16 *)p = value→SET_UNALIGNED_16(p, value)*(UINT32 *)p = value→SET_UNALIGNED_32(p, value)amd64 (cgenamd64.cpp):
*(UINT16 *)p = value→SET_UNALIGNED_16(p, value)*(UINT32 *)p = value→SET_UNALIGNED_32(p, value)*(INT32 *)p = value→SET_UNALIGNED_32(p, value)*(TADDR *)p = value→SET_UNALIGNED_64(p, value)*(UINT64 UNALIGNED *)p = value→SET_UNALIGNED_64(p, value)These macros are defined in the CoreCLR PAL headers and use the
UNALIGNEDattribute to ensure safe memory access regardless of alignment.Testing
Fixes #118602.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.