Skip to content

Unity & Messagepack v2.x Error on some Android devices. #748

Closed

Description

@odysoftware

Bug description

Just built a Android version with the new message pack v2.x on an Android device (iOS is working fine - both using IL2CPP). As soon as I call this line

MessagePackSerializer.Deserialize<CharInfo>(_bytes);

It crashes with the following error:

Plugins: Couldn't open advapi32, error: dlopen failed: library "advapi32" not found

Repro steps

Tested on an Android 6.0 and 8.0 device with different API Levels 23 and 27 crashed, 26 was fine - so it is also not related to any specific Android API Level or Android Version...

Expected behavior

Should not crash.

Actual behavior

Program crashes.

  • Version used: Unity 2019.2.16f1 with MessagePack v2.0.323 (unity package)
  • Runtime: Android + IL2CPP (API compatibility level .NET Stadard 2.0)

Activity

  1. AArnott commented on Dec 29, 2019

    @AArnott
    Collaborator

    Can you please include the full callstack from the exception?

  2. odysoftware commented on Dec 29, 2019

    @odysoftware
    Author

    That is all information I can get when debugging with Android device connected to unity. This is a warning message from unity and right after that message the android device is saying "App is closed" - no more information given. I guess I need to check if I can get more info via adb log from android device itself - ill check that.

  3. AArnott commented on Dec 29, 2019

    @AArnott
    Collaborator

    Ya, anything else you can give would be great. Messagepack has no direct reference to advapi32 so it's probably something else that's failing.

  4. odysoftware commented on Dec 29, 2019

    @odysoftware
    Author

    So with Android and symbols the best I can get is:

    Native StackTrace:
    
    Thread 0 (crashed)
    0   libunity.so                        0x00000000c78b4b5a CrashReporting::Android::SignalHandler(int, siginfo*, void*) 
    1   libunity.so                        0x00000000c718e60e NativeRuntimeException::signal_handler(int, siginfo*, void*) 
    2   libc.so                            0x00000000e7472af4 <system symbols missing> 
    3   libil2cpp.so                       0x00000000c0fa2a90 Unsafe_ReadUnaligned_TisInt64_t7A386C2FF7B0280A0F516992401DDFCF0FF7B436_mCF953FD610E15812D1063A3C6501EB7BC720AE2B_gshared 
    4   libil2cpp.so                       0x00000000c38275d4 SequenceReaderExtensions_TryRead_TisInt64_t7A386C2FF7B0280A0F516992401DDFCF0FF7B436_mE7C293597ACEE090A9913E99B2EABBC0507CAE61_gshared_inline 
    5   libil2cpp.so                       0x00000000c3822764 SequenceReaderExtensions_TryReadReverseEndianness_m86440CA009B364528CBFBE9C1E9831D7E9D43DC0 
    6   libil2cpp.so                       0x00000000c38220c0 SequenceReaderExtensions_TryReadBigEndian_m84CF325B0F9ED1C4BC4D63D22C7E56F9E035E94E 
    7   libil2cpp.so                       0x00000000bff1f970 MessagePackReader_ReadDateTime_m555B60F62286B8A8E326471FDA31B585E954F0ED 
    8   libil2cpp.so                       0x00000000bff1e020 MessagePackReader_ReadDateTime_m06C1CE1BC72B6C5845E1321506DE1C50ED322CD9 
    9   libil2cpp.so                       0x00000000c0ce4cdc DateTimeFormatter_Deserialize_m435870F49A04B1E7E28EF07B64A4126408470E8C
    

    It is deserialising stuff, but it appears to be the date time ! Interesting is that when looking for advapi32 errors in google, I can find an recent issue with unity:

    https://issuetracker.unity3d.com/issues/unable-to-load-dll-advapi32-dot-dll-when-calling-datetime-dot-now

    I have to check but I am using datetime.now too and there I don't have problems... very strange...

  5. odysoftware commented on Dec 29, 2019

    @odysoftware
    Author

    I can confirm using DateTime.Now does not cause any problems here... - very strange... so it must be related to how message pack is using it somehow... -> Unsafe_ReadUnaligned_TisInt64 ?

  6. changed the title [-]Unity & Messagepack v2.x Error on Android devices.[/-] [+]Unity & Messagepack v2.x Error on some Android devices.[/+] on Dec 29, 2019
  7. AArnott commented on Dec 30, 2019

    @AArnott
    Collaborator

    Thanks. This is helpful. Where did you get your Unsafe.dll? I wonder if you're shipping one with your Android app that is compiled for Windows.

  8. neuecc commented on Dec 30, 2019

    @neuecc
    Member

    Stack trace is mentioned Unsafe.ReadUnaligned<long> was crashed.
    I think Unsafe.dll is managed dll, it is no dependent platform.

  9. AArnott commented on Dec 30, 2019

    @AArnott
    Collaborator

    @neuecc Are you saying that advapi32.dll is unrelated?

  10. neuecc commented on Dec 30, 2019

    @neuecc
    Member

    @AArnott
    I guess it's a Unity IL2CPP VM issue.
    In that case, the details are a black box,
    so you have to create a reproduction procedure and report a bug to Unity.

  11. neuecc commented on Dec 30, 2019

    @neuecc
    Member

    Unsafe.dll is not usually used in Unity, so it's no wonder bugs exist in the IL2CPP VM.
    However, if this always happens, Details and workarounds must be sought.

    However, I and several companies have already tried v2 on Android,
    I have not received a similar report.
    Therefore, we cannot assert that there is a problem.

  12. odysoftware commented on Dec 30, 2019

    @odysoftware
    Author

    Well unsafe is checked in unity for Android and since it’s working on a few devices and on iOS it seems to be fine overal. Maybe you can reproduce it with some datetime in messagepack classes because that is where it crashes but it definitely a combination from messagepack and unity. The crash happens in deserialize and i have now moved back to v1.8 - all fine again- because many had this crash with several different Android device new and old. 😥

  13. odysoftware commented on Dec 30, 2019

    @odysoftware
    Author

    I can try to look what exactly in my class using message pack is causing this, that should help for reproduction.

    Maybe it’s related to this same unity issue https://issuetracker.unity3d.com/issues/unable-to-load-dll-advapi32-dot-dll-when-calling-datetime-dot-now

  14. odysoftware commented on Dec 30, 2019

    @odysoftware
    Author

    What I can see is that it is only happening for the datetime type...

    e.g.

    [Key(0)]
    DateTime lastVisited {get; private set;}
    
    public Test(DateTime _lastVisit)
                    {
                        LastVisit = _lastVisit;
                    }
    

    So as soon as message pack is trying to deserialise the datetime in unity via this unsafe.dll - that is the point where it is fatal crashing on some devices. Removing this DateTime prevents it from crashing.

    Maybe this helps in reproduction...?

    From MessagePackGenerated.cs (that where it seems to crash...)

     __lastVisit__ = formatterResolver.GetFormatterWithVerify<global::System.DateTime>().Deserialize(ref reader, options);
    
  15. neuecc commented on Dec 31, 2019

    @neuecc
    Member

    Since DateTime.Now is not used, I don't think it has a direct effect, but I think it is a similar problem.
    Unity says Fixed in 2019.3, and you're trying in 2019.2,
    Could you check in 2019.3?

  16. 14 remaining items

  17. Durnev commented on Jan 27, 2020

    @Durnev

    I found out that crash appears if we call TryRead on SequenceReader to read byte with MP code.

    const double startValue = 42;
    var span = new Span<byte>(new byte[9])
    {
    	[0] = MessagePackCode.Float64
    };
    MessagePackWriter.WriteBigEndian(startValue, span.Slice(1));
    var bytes = span.ToArray();
    
    var reader = new SequenceReader<byte>(bytes);
    reader.TryRead(out _); // HERE 
    reader.TryReadBigEndian(out double result);
    

    I replaced double with long and it also crashed. I think problem is in SequenceReader.

    It can be memory alignment issue, but my experience is not enough to continue research.

    Also, i have a problems with debugging code in SequenceReader.
    image

  18. odysoftware commented on Jan 27, 2020

    @odysoftware
    Author

    Wow great research here :) and finally we can hope for a fix sooner or later in this regard, so people can use 2.0 with unity. I still wonder why there are not a lot more people having issues with messaepack 2.0 and unity - because ARMv7 is still heavily used and this seems to be a general problems with datetime and doubles...

    Anyway I hope someone can catch it now!

  19. neuecc commented on Jan 27, 2020

    @neuecc
    Member

    Currently supporting 64bit architecture is must on upload to Google Play Store.
    Most of me and my customers have already not support ARMv7.
    Therefore, the priority was not high.
    The solution is to replace this with managed code.

  20. Durnev commented on Jan 28, 2020

    @Durnev

    @neuecc We develop the game with 700к daily active users on Android. I prepare statistic about CPU architectures distribution in it:
    image
    17-18% of ARMv7 it's also quite a lot.

    Also, i sent repro project to Unity. I'll keep you informed about it.

  21. odysoftware commented on Jan 28, 2020

    @odysoftware
    Author

    I totally agree ARMv7, even if it outdated, is still in heavy use, as mentioned by Durnev. Yes you need 64bit support, but if you want higher reach there is no way around to also support ARMv7.

    In my eyes at the current stage, Message Pack 2.0 is absolutely not usable with unity, due to this issue. Well its no problem for me to stay on old version... but still I think this needs to get fixed or otherwise people need to be aware that Messagepack 2.0 is not working on ARMv7 devices with unity.

  22. neuecc commented on Jan 29, 2020

    @neuecc
    Member

    thanks for the details.
    I've added PR, I don't confirm yet but maybe fixed.

  23. AArnott commented on Jan 29, 2020

    @AArnott
    Collaborator

    @odysoftware can you confirm with the build in #795 PR that it fixes your issue?

    Also, can you link to a bug tracking this in Unity if you have one?

  24. odysoftware commented on Jan 31, 2020

    @odysoftware
    Author

    Hey there, that sounds great (!), I did not have time to try it out yet - have to do migration again. Maybe I can do it later today, then I can give you feedback if it is working.

  25. odysoftware commented on Jan 31, 2020

    @odysoftware
    Author

    So far everything seems fine, now starting tests on Android !

    There was a mistake though in SequenceReaderExtensions.cs when switching unity to Android ->

    #if UNITY_ANDROID
        private static unsafe bool TryReadMultisegment(ref SequenceReader<byte> reader, out long value)
            {
                Debug.Assert(reader.UnreadSpan.Length < sizeof(T), "reader.UnreadSpan.Length < sizeof(T)");
    .......
    

    This gives an error, you need to change it to:

    Debug.Assert(reader.UnreadSpan.Length < sizeof(long), "reader.UnreadSpan.Length < sizeof(T)");
    

    then it works.

  26. odysoftware commented on Jan 31, 2020

    @odysoftware
    Author

    It didn't help for all problems yet because the datetime is not calling new method so I still have this crash - it may only work for doubles:

    Thread 0 (crashed)
    0   libil2cpp.so                       0x00000000a06074b0 SequenceReaderExtensions_BitConverterToInt64_m388EBB0EE5EB8A4CBC5A72606CCFBBF07A6D81A2 (MessagePack3.cpp:35063)
    1   libil2cpp.so                       0x00000000a0607dfc SequenceReaderExtensions_TryReadReverseEndianness_m86440CA009B364528CBFBE9C1E9831D7E9D43DC0 (MessagePack3.cpp:36934)
    2   libil2cpp.so                       0x00000000a0d41f6c MessagePackReader_ReadDateTime_m555B60F62286B8A8E326471FDA31B585E954F0ED (MessagePack2.cpp:23866)
    3   libil2cpp.so                       0x00000000a0d41db8 MessagePackReader_ReadDateTime_m06C1CE1BC72B6C5845E1321506DE1C50ED322CD9 (MessagePack2.cpp:23773)
    4   libil2cpp.so                       0x00000000a07a82b0 DateTimeFormatter_Deserialize_m435870F49A04B1E7E28EF07B64A4126408470E8C (MessagePack.cpp:18200) 
    

    So we need this also for the other types?

  27. Durnev commented on Feb 3, 2020

    @Durnev

    Hello.
    I have answer from Unity support team about our issue.

    ARMv7 requires for all reads to be aligned.

    In SequenceReaderExtensions.cs​ on line 43, there is this call made Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));​. It gets there from TryReadBigEndian() -> TryReadReverseEndianness() -> TryRead()​.

    IL2CPP does the right thing here as it stops the memory being accessed from an unaligned read, while when using mono, that is ignored and the program returns garbage values.

    I haven't found a ReadAligned method which would allow the user to read in an aligned way from the memory. The best solution would be implementing ReadAligned by yourself.

    So rewrite this code was right solution by @neuecc. We can wait until ARMv7 will be deprecated, but i think it will be not near future.

  28. neuecc commented on Mar 18, 2020

    @neuecc
    Member

    solved by #798

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions