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

[NativeAOT/ARM] NativePrimitiveDecoder generates unaligned accesses with VLDR instruction #97912

Closed
Tracked by #97729
filipnavara opened this issue Feb 3, 2024 · 2 comments · Fixed by #97917
Closed
Tracked by #97729

Comments

@filipnavara
Copy link
Member

filipnavara commented Feb 3, 2024

Code:

public static byte ReadUInt8(ref byte* stream)
{
byte result = *(stream); // Assumes little endian and unaligned access
stream++;
return result;
}
public static ushort ReadUInt16(ref byte* stream)
{
ushort result = *(ushort*)(stream); // Assumes little endian and unaligned access
stream += 2;
return result;
}
public static uint ReadUInt32(ref byte* stream)
{
uint result = *(uint*)(stream); // Assumes little endian and unaligned access
stream += 4;
return result;
}
public static ulong ReadUInt64(ref byte* stream)
{
ulong result = *(ulong*)(stream); // Assumes little endian and unaligned access
stream += 8;
return result;
}

Crash at runtime:

* thread #1, name = 'System.Runtime.', stop reason = signal SIGBUS
  * frame #0: 0x0204ba16 System.Runtime.Tests`Internal.Metadata.NativeFormat.MetadataReader__GetConstantSingleValue(this=0xf49a9454, handle=(_value = 403231906)) at NativeFormatReaderGen.cs:10446
    frame #1: 0x0203cf36 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__ParseConstantNumericValue(handle=<unavailable>, reader=<unavailable>) at MetadataReaderExtensions.NativeFormat.cs:278
    frame #2: 0x0203d050 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantValue(handle=<unavailable>, reader=<unavailable>, value=0xf50eb2f0) at MetadataReaderExtensions.NativeFormat.cs:303
    frame #3: 0x0203d4e6 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantArray(handle=<unavailable>, reader=0xf49a9454, exception=0xf03fe26c) at MetadataReaderExtensions.NativeFormat.cs:405
    frame #4: 0x0203d0da System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantValue(handle=<unavailable>, reader=<unavailable>, value=0xf03fe2c8) at MetadataReaderExtensions.NativeFormat.cs:327
    frame #5: 0x02041922 System.Runtime.Tests`System.Reflection.Runtime.CustomAttributes.NativeFormat.NativeFormatCustomAttributeData__GetConstructorArguments(this=0xf50eb1b0, throwIfMissingMetadata=true) at NativeFormatCustomAttributeData.cs:132
    frame #6: 0x020405a4 System.Runtime.Tests`System.Reflection.Runtime.CustomAttributes.RuntimeCustomAttributeData__get_ConstructorArguments(this=<unavailable>) at RuntimeCustomAttributeData.cs:24
    ...

  0x204ba16 <+53>: vldr   s8, [r3]

        r3 = 0x00a88312  System.Runtime.Tests`__embedded_metadata + 578722

VLDR is not handled by the kernel unalignment traps (ref: raspberrypi/linux#3099) and it doesn't natively support unaligned access.

@ghost
Copy link

ghost commented Feb 3, 2024

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Code:

public static byte ReadUInt8(ref byte* stream)
{
byte result = *(stream); // Assumes little endian and unaligned access
stream++;
return result;
}
public static ushort ReadUInt16(ref byte* stream)
{
ushort result = *(ushort*)(stream); // Assumes little endian and unaligned access
stream += 2;
return result;
}
public static uint ReadUInt32(ref byte* stream)
{
uint result = *(uint*)(stream); // Assumes little endian and unaligned access
stream += 4;
return result;
}
public static ulong ReadUInt64(ref byte* stream)
{
ulong result = *(ulong*)(stream); // Assumes little endian and unaligned access
stream += 8;
return result;
}

Crash at runtime:

* thread #1, name = 'System.Runtime.', stop reason = signal SIGBUS
  * frame #0: 0x0204ba16 System.Runtime.Tests`Internal.Metadata.NativeFormat.MetadataReader__GetConstantSingleValue(this=0xf49a9454, handle=(_value = 403231906)) at NativeFormatReaderGen.cs:10446
    frame #1: 0x0203cf36 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__ParseConstantNumericValue(handle=<unavailable>, reader=<unavailable>) at MetadataReaderExtensions.NativeFormat.cs:278
    frame #2: 0x0203d050 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantValue(handle=<unavailable>, reader=<unavailable>, value=0xf50eb2f0) at MetadataReaderExtensions.NativeFormat.cs:303
    frame #3: 0x0203d4e6 System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantArray(handle=<unavailable>, reader=0xf49a9454, exception=0xf03fe26c) at MetadataReaderExtensions.NativeFormat.cs:405
    frame #4: 0x0203d0da System.Runtime.Tests`System.Reflection.Runtime.General.NativeFormatMetadataReaderExtensions__TryParseConstantValue(handle=<unavailable>, reader=<unavailable>, value=0xf03fe2c8) at MetadataReaderExtensions.NativeFormat.cs:327
    frame #5: 0x02041922 System.Runtime.Tests`System.Reflection.Runtime.CustomAttributes.NativeFormat.NativeFormatCustomAttributeData__GetConstructorArguments(this=0xf50eb1b0, throwIfMissingMetadata=true) at NativeFormatCustomAttributeData.cs:132
    frame #6: 0x020405a4 System.Runtime.Tests`System.Reflection.Runtime.CustomAttributes.RuntimeCustomAttributeData__get_ConstructorArguments(this=<unavailable>) at RuntimeCustomAttributeData.cs:24
    ...

  0x204ba16 <+53>: vldr   s8, [r3]

        r3 = 0x00a88312  System.Runtime.Tests`__embedded_metadata + 578722

VLDR is not handled by the kernel unalignment traps (ref: raspberrypi/linux#3099).

Author: filipnavara
Assignees: -
Labels:

arch-arm32, area-NativeAOT-coreclr

Milestone: -

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 3, 2024
@filipnavara
Copy link
Member Author

filipnavara commented Feb 3, 2024

This actually looks like a RyuJIT issue. There are code paths in the JIT that handle this for field access (1, 2), so it feels like the JIT should also not try to optimize this.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 3, 2024
@ghost ghost removed in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner labels Feb 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant