Fix unaligned memory access errors in TraceEvent - #1104
Conversation
|
The JIT output seems very different (reference): public unsafe double M1(IntPtr pointer, int offset) {
return *((double*)((byte*)pointer.ToPointer() + offset));
}public unsafe double M2(IntPtr pointer, int offset) {
var val = Marshal.ReadInt64(pointer, offset);
return *(double*)&val;
} |
|
This option seems better: public unsafe double M3(IntPtr pointer, int offset) {
return Unsafe.ReadUnaligned<double>(IntPtr.Add(pointer, offset).ToPointer());
} |
sharwell
left a comment
There was a problem hiding this comment.
We should use Unsafe.ReadUnaligned<T> to avoid the overhead of Marshal.Read*.
|
@sharwell Thanks for the review and introducing a good tool.
|
|
|
||
| <!-- *** SourceLink Support *** --> | ||
| <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="All" /> | ||
| <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" /> |
There was a problem hiding this comment.
@brianrob @adamsitnik Please review this added dependency. IMO it is a clear win.
- System.Runtime.CompilerServices.Unsafe is added as a new dependency.
|
@swift-kim, thanks for contributing this. This looks OK to me - just want to confirm that you've confirmed that the issue is fixed with the latest version of the PR, since you switched APIs? |
|
@brianrob Yes, I've confirmed dotnet-counters.dll now works in our production system. I'll report if I ever find any other issue. Thank you. |
|
Thanks @swift-kim! |
This fixes unaligned read errors (SIGBUS) caused by unmanaged pointer operations on ARM platforms. The
Marshal.Read*()operations are portable and have little performance impacts.