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

Buffer.Memmove has unaligned casts #83709

Closed
xtqqczze opened this issue Mar 21, 2023 · 6 comments · Fixed by #98812
Closed

Buffer.Memmove has unaligned casts #83709

xtqqczze opened this issue Mar 21, 2023 · 6 comments · Fixed by #98812
Labels
area-System.Runtime bug in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@xtqqczze
Copy link
Contributor

xtqqczze commented Mar 21, 2023

#26654 (comment):

It is invalid to reinterpret cast a ref byte to a ref ushort/uint/ulong without first checking the alignment of the input buffer. This could cause problems on future architectures or IOT devices where unaligned reads incur faults. This code should pin and check alignment or should use Unsafe.WriteUnaligned instead.

cc:@GrabYourPitchforks

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 21, 2023
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Mar 21, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

#26654 (comment):

It is invalid to reinterpret cast a ref byte to a ref ushort/uint/ulong without first checking the alignment of the input buffer. This could cause problems on future architectures or IOT devices where unaligned reads incur faults. This code should pin and check alignment or should use Unsafe.WriteUnaligned instead.

cc:@GrabYourPitchforks

Author: xtqqczze
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Mar 21, 2023

For such integer reads/writes we assume that OS will be able to fix them for us via alignment fixups. On OSes without such fixups we have plenty of potential problems as is, Unsafe.Read/WriteUnaligned won't help (the way they're implemented now in JIT). See #80068 (comment)

@tannergooding
Copy link
Member

We likely should be using Unsafe.Read/WriteUnaligned anyways as it improves the overall portability of the code (e.g. on other Runtimes) and has no real negative impact on architectures with fast unaligned reads/writes (e.g. Arm64 and x86/x64).

@xtqqczze
Copy link
Contributor Author

We likely should be using Unsafe.Read/WriteUnaligned anyways as it improves the overall portability of the code (e.g. on other Runtimes) and has no real negative impact on architectures with fast unaligned reads/writes (e.g. Arm64 and x86/x64).

@tannergooding We would regress when using ReadUnaligned and WriteUnaligned with HAS_CUSTOM_BLOCKS structs, as the copy would then go through the stack. Could we just simplify the implementation and use CopyBlockUnaligned with bytes counts of 2, 4, 8, 16, 64, resulting in the same codegen? (Compiler Explorer)

internal static void CopyBlock64(ref byte dest, ref byte src)
{
  Unsafe.As<byte, Block64>(ref dest) = Unsafe.As<byte, Block64>(ref src);
}
internal static void CopyBlock64Unaligned(ref byte dest, ref byte src)
{
  Unsafe.WriteUnaligned<Block64>(ref dest, Unsafe.ReadUnaligned<Block64>(ref src));
}
internal static void CopyBlockUnaligned(ref byte dest, ref byte src)
{
  Unsafe.CopyBlockUnaligned(ref dest, ref src, 64);
}

@tannergooding
Copy link
Member

We would regress when using ReadUnaligned and WriteUnaligned with HAS_CUSTOM_BLOCKS structs, as the copy would then go through the stack

Block16, Block32, and Block64 aren't integer reads. They are opaque sized structs that go through what is effectively a "block copy". Since they have no fields, the packing/natural alignment of them is 1 and they aren't subject to the issue.

@tannergooding tannergooding added bug and removed untriaged New issue has not been triaged by the area owner labels Mar 23, 2023
@ericstj ericstj added the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Aug 14, 2023
@ericstj ericstj added this to the Future milestone Aug 14, 2023
@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Feb 22, 2024
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 22, 2024
xtqqczze added a commit to xtqqczze/dotnet-runtime that referenced this issue Feb 22, 2024
Fix dotnet#83709
IL size is reduced by ~140 bytes.
xtqqczze added a commit to xtqqczze/dotnet-runtime that referenced this issue Feb 28, 2024
Fix dotnet#83709
IL size is reduced by ~140 bytes.
xtqqczze added a commit to xtqqczze/dotnet-runtime that referenced this issue Feb 28, 2024
Fix dotnet#83709
IL size is reduced by ~140 bytes.
xtqqczze added a commit to xtqqczze/dotnet-runtime that referenced this issue Mar 2, 2024
Fix dotnet#83709
IL size is reduced by ~140 bytes.
@jkotas jkotas closed this as completed in 5aee4f4 Apr 15, 2024
matouskozak pushed a commit to matouskozak/runtime that referenced this issue Apr 30, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 16, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this issue May 30, 2024
@tannergooding tannergooding removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime bug in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@EgorBo @huoyaoyuan @ericstj @tannergooding @xtqqczze and others