Skip to content

Unsafe.ReadUnaligned / WriteUnaligned cause Unhandled fault: alignment exception on armhf #80068

Open

Description

@kekekeks

Description

It seems that ReadUnaligned and WriteUnaligned no longer support unaligned pointer access on ARM architecture.

Reproduction Steps

https://godbolt.org/z/nGh9vP6KK

using System;
using System.Runtime.CompilerServices;

unsafe class Repro
{
    public struct Test
    {
        public int X, Y, Z;
    }

    public static Test Test1(void*ptr)
    {
        return Unsafe.ReadUnaligned<Test>(ptr);
    }

    public static Test Test2(void*ptr)
    {
        return *(Test*)ptr;
    }

    public static void WTest1(void*ptr, Test t)
    {
        Unsafe.WriteUnaligned<Test>(ptr, t);
    }

    public static void WTest2(void*ptr, Test t)
    {
        *(Test*)ptr = t;
    }
}

.NET 6.0: Only ReadUnaligned generates invalid code

Repro:WTest2(int,Repro+Test):
            push    {r1,r2,r3}
            push    {lr}
            ldr     r3, [sp+0x04]
            str     r3, [r0]
            ldr     r3, [sp+0x08]
            str     r3, [r0+4]
            ldr     r3, [sp+0x0c]
            str     r3, [r0+8]
            pop     lr
            add     sp, 12
            bx      lr

Repro:WTest1(int,Repro+Test):
            push    {r1,r2,r3}
            push    {lr}
            sub     sp, 16
            ldr     r3, [sp+0x14]
            str     r3, [sp+0x04]	// [V04 tmp2]
            ldr     r3, [sp+0x18]	// [V01 arg1+0x04]
            str     r3, [sp+0x08]	// [V05 tmp3]
            ldr     r3, [sp+0x1c]	// [V01 arg1+0x08]
            str     r3, [sp+0x0c]	// [V06 tmp4]
            ldr     r3, [sp+0x04]	// [V03 tmp1]
            str     r3, [r0]
            ldr     r3, [sp+0x08]	// [V03 tmp1+0x04]
            str     r3, [r0+4]
            ldr     r3, [sp+0x0c]	// [V03 tmp1+0x08]
            str     r3, [r0+8]
            add     sp, 16
            pop     lr
            add     sp, 12
            bx      lr

Repro:Test2(int):Repro+Test:
            push    {r3,lr}
            ldr     r3, [r1]
            str     r3, [r0]
            ldr     r3, [r1+4]
            str     r3, [r0+4]
            ldr     r3, [r1+8]
            str     r3, [r0+8]
            pop     {r3,pc}

Repro:Test1(int):Repro+Test:
            push    {r3,lr}
            ldr     r3, [r1]
            str     r3, [r0]
            ldr     r3, [r1+4]
            str     r3, [r0+4]
            ldr     r3, [r1+8]
            str     r3, [r0+8]
            pop     {r3,pc}

.NET 7.0: Both WriteUnaligned and ReadUnaligned generate direct pointer access

Repro:Test1(uint):Test:
            push    {r3,lr}
            ldr     r3, [r1]
            str     r3, [r0]
            ldr     r3, [r1+0x04]
            str     r3, [r0+0x04]
            ldr     r3, [r1+0x08]
            str     r3, [r0+0x08]
            pop     {r3,pc}

Repro:Test2(uint):Test:
            push    {r3,lr}
            ldr     r3, [r1]
            str     r3, [r0]
            ldr     r3, [r1+0x04]
            str     r3, [r0+0x04]
            ldr     r3, [r1+0x08]
            str     r3, [r0+0x08]
            pop     {r3,pc}

Repro:WTest1(uint,Test):
            push    {r1,r2,r3}
            push    {lr}
            ldr     r3, [sp+0x04]
            str     r3, [r0]
            ldr     r3, [sp+0x08]
            str     r3, [r0+0x04]
            ldr     r3, [sp+0x0C]
            str     r3, [r0+0x08]
            pop     lr
            add     sp, 12
            bx      lr

Repro:WTest2(uint,Test):
            push    {r1,r2,r3}
            push    {lr}
            ldr     r3, [sp+0x04]
            str     r3, [r0]
            ldr     r3, [sp+0x08]
            str     r3, [r0+0x04]
            ldr     r3, [sp+0x0C]
            str     r3, [r0+0x08]
            pop     lr
            add     sp, 12
            bx      lr

Expected behavior

Generated code properly handles misaligned pointers

Actual behavior

Unhandled fault: alignment exception

Regression?

No response

Known Workarounds

Buffer.MemoryCopy

Configuration

Raspberry Pi 3, Debian 11 32 bit

Other information

No response

Activity

  1. ghost added
    untriaged
    on Dec 30, 2022
  2. changed the title [-]Unsafe.ReadUnaligned / WriteUnaligned causes Unhandled fault: alignment exception on armhf[/-] [+]Unsafe.ReadUnaligned / WriteUnaligned cause Unhandled fault: alignment exception on armhf[/+] on Dec 30, 2022
  3. added
    area-CodeGen-coreclr
    on Dec 30, 2022
  4. kekekeks commented on Dec 30, 2022

    @kekekeks
    Author

    Unsafe.CopyBlockUnaligned doesn't seem to respect memory alignment restrictions either when used with Unsafe.SizeOf<T>

  5. added
    needs-further-triage
    on Jan 9, 2023
  6. JulieLeeMSFT commented on Jan 9, 2023

    @JulieLeeMSFT
    Member
  7. EgorBo commented on Jan 9, 2023

    @EgorBo
    Member

    I'm not 100% sure I know how we detect "this platform doesn't support unaligned reads" so it can be a bug in that logic that detects it?

    Another issue is what we assume in R2R? Do we throw R2R unconditionally if current platform doesn't support them or R2R itself assumes unaligned reads are not allowed on given platform?

  8. jkotas commented on Jan 9, 2023

    @jkotas
    Member

    The Linux distros that we test on have the alignment fixups enabled by default. There are likely number of other issues that would popup with alignment fixups disabled.

    Does your distro have the alignment fixups explicitly disabled? Can you enable them?

    Or have you encountered this issue when accessing device memory (similar to #66082)?

  9. jakobbotsch commented on Jan 9, 2023

    @jakobbotsch
    Member

    .NET 6.0: Only ReadUnaligned generates invalid code

    I'm confused, I see the exact same stores/loads from the pointer (in r0) in all your examples, only there are some extra unnecessary copies between stack slots that we are now getting rid of in .NET 7.
    I do believe that the JIT always assumes that unaligned integer loads/stores are supported by the target, so unaligned loads/stores are only treated specially for floats.

  10. kekekeks commented on Jan 9, 2023

    @kekekeks
    Author

    @jkotas the issue was reported on 32-bit Raspbian / Raspberry Pi Zero 2. The memory was allocated via Marshal.AllocHGlobal. Replacing ReadUnaligned with manual byte copy loop fixed the issue.

  11. kekekeks commented on Jan 9, 2023

    @kekekeks
    Author

    @jakobbotsch we do read double, float and float-based System.Numerics structures via ReadUnaligned

  12. removed
    untriaged
    needs-further-triage
    on Feb 13, 2023
  13. added this to the 8.0.0 milestone on Feb 13, 2023
  14. modified the milestones: 8.0.0, Future on Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-arm32area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions