Skip to content

il2cpp - MemoryMarshal.Cast can cause SIGBUS on ARMv7 #931

Closed

Description

@gsuuon

Bug description

2020-05-16 05:02:50.859 22635-22658/? E/AndroidRuntime: FATAL EXCEPTION: UnityMain
    Process: com.DefaultCompany.UnalignedMemoryARMv7, PID: 22635
    java.lang.Error: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    Version '2019.3.3f1 (7ceaae5f7503)', Build type 'Release', Scripting Backend 'il2cpp', CPU 'armeabi-v7a'
    Build fingerprint: 'google/walleye/walleye:10/QQ1A.200105.002/6031801:user/release-keys'
    Revision: 'MP1'
    ABI: 'arm'
    Timestamp: 2020-05-16 05:02:50-0500
    pid: 22635, tid: 22658, name: UnityMain  >>> com.DefaultCompany.UnalignedMemoryARMv7 <<<
    uid: 10433
    signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xdd395d82
        r0  dd395d82  r1  00000000  r2  c480e0b0  r3  c84336a4
        r4  cad6acb8  r5  c86fc31b  r6  0000000e  r7  c83c9914
        r8  00000002  r9  e32d8a40  r10 c47f3190  r11 cad6acb0
        ip  cad6ac50  sp  cad6ac80  lr  c854f0b0  pc  c854f0b0

The top stack in the backtrace is this line. MemoryMarshall can cause problems on platforms that don't support unaligned memory according to docs.

Repro steps

I was able to repro a SIGBUS with this, built with il2cpp in release config for armv7

public class UnalignedMemory : MonoBehaviour
{
    void Start()
    {
        AccessMemoryOffset();
    }

    void AccessMemoryOffset()
    {
        byte[] someBytes = new byte[10];

        ReadOnlySpan<byte> bytesSpan = new ReadOnlySpan<byte>(someBytes);
        ReadOnlySpan<byte> bytesOffset = bytesSpan.Slice(2);

        ulong marshaled = CastBytesToUlong(ref bytesOffset);
    }

    ulong CastBytesToUlong(ref ReadOnlySpan<byte> span)
    {
        return MemoryMarshal.Cast<byte, ulong>(span)[0];
    }
}

Expected behavior

No SIGBUS

Actual behavior

Crashed

  • Version used:
  • Runtime: Unity, il2cpp/release

Activity

  1. AArnott commented on May 17, 2020

    @AArnott
    Collaborator

    This seems really familiar. I think we already dealt with this. What version of the library are you using?

  2. gsuuon commented on May 17, 2020

    @gsuuon
    ContributorAuthor

    I was able to reproduce this with the latest .unitypackage with a type like this:

    [MessagePackObject]
    public class MyStringKeyClass
    {
        [Key("LongKeyOver8Chars")]
        public int MyProperty1 { get; set; }
    
        [Key("Two")]
        public int MyProperty2 { get; set; }
    }

    If I replace this MemoryMarshal.Cast call with BitConverterToInt64 it seems to work. I ran a really basic perf test and the bitwise code also seems to be a good bit faster than MemoryMarshal.Cast.

  3. AArnott commented on May 17, 2020

    @AArnott
    Collaborator

    #748 is the issue I was thinking of that sounds very similar. It was fixed by changing BitConverterToInt64 so that it avoids the bug in the Android runtime, so your fix makes sense--we just didn't update all callers to use the new method.

  4. added this to the v2.1 milestone on May 17, 2020
  5. self-assigned this
    on May 17, 2020
  6. added 2 commits that reference this issue on May 17, 2020
    347089a
    4d76ac8
  7. CodeMasterYi commented on Oct 31, 2022

    @CodeMasterYi

    Crash also happens in MessagePack v2.2.85~
    Android 12/arm64/il2cpp/Unity2021.3.11f1

  8. CodeMasterYi commented on Oct 31, 2022

    @CodeMasterYi

    Tombstone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions