-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Closed
Copy link
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone
Description
Hi!
I faced with System.AccessViolationException when reading 5-byte structure in .NET 6 / .NET 7 RC1 / Arm64 .NET Framework 4.8.1 app on Windows ARM64, here is demo app:
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Explicit, Size = 5)]
public struct Data
{
[FieldOffset(0)]
public byte Byte;
[FieldOffset(1)]
public int Int;
}
static unsafe void Main(string[] args)
{
var mappedFile = MemoryMappedFile.CreateNew(null, 0x1000);
var viewAccessor = mappedFile.CreateViewAccessor();
byte* viewPtr = null;
viewAccessor.SafeMemoryMappedViewHandle.AcquirePointer(ref viewPtr);
Unsafe.InitBlock(viewPtr, 0x01, (uint)viewAccessor.Capacity);
IntPtr viewStart = new IntPtr(viewPtr);
IntPtr viewEnd = viewStart + (int)viewAccessor.Capacity;
IntPtr last = viewEnd - Unsafe.SizeOf<Data>();
Console.WriteLine($"view {viewStart:X16} - {viewEnd:X16} last {last:X16}");
Data x = Unsafe.ReadUnaligned<Data>(last.ToPointer());
Console.WriteLine($"{x.Byte:X} {x.Int:X}");
}
}Output:
view 0000023537440000 - 0000023537441000 last 0000023537440FFB
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.CompilerServices.Unsafe.ReadUnaligned[[Program+Data, Program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]](Void*)
at Program.Main(System.String[])
...\bin\Debug\net6.0\Program.exe (process 13224) exited with code -1073741819.
Looks like it tries to load 8 bytes from x0:
System.Runtime.CompilerServices.Unsafe.dll!System.Runtime.CompilerServices.Unsafe.ReadUnaligned<T>(void*):
00007FF7F8F0CBF0 stp fp,lr,[sp,#-0x10]!
00007FF7F8F0CBF4 mov fp,sp
00007FF7F8F0CBF8 ldr x0,[x0] <-- here, x0 is 0x0000023537440FFB
00007FF7F8F0CBFC ldp fp,lr,[sp],#0x10
00007FF7F8F0CC00 ret
.NET 6.0.401
.NET 7.0.100-rc.1.22431.12
.NET Framework 4.8.1 ARM64 4.8.9093.0
Windows 11 21H2 22000.978
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI