Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug IL2CPP doesn't support ReadOnlyMemory<T> or ReadOnlySpan<byte> method parameters

Discussion in 'Scripting' started by Gekctek, Apr 7, 2023.

  1. Gekctek

    Gekctek

    Joined:
    Dec 14, 2022
    Posts:
    5
    I have a C# .netstandard2.1 project https://github.com/edjCase/ICP.NET/ that game developers are trying to use with their Unity games but there is a problem when compiling to WebGL/IL2CPP. Any instance where a method is called that has ReadOnlyMemory<T> (readonly struct) or ReadOnlySpan<byte> (readonly ref struct) it gives the following message:
    ```
    Building Library\Bee\artifacts\WebGL\GameAssembly\release_WebGL_wasm\8hlh_.ICP.Agent.o failed with output:
    C:\Git\icp.net_demo\ICP_demo_unity\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\EdjCase.ICP.Agent.cpp:10541:3: error: no matching function for call to 'CborReader__ctor_m5191B60B1242ACF099DE8349ED037D0A17E36DD4'
    CborReader__ctor_m5191B60B1242ACF099DE8349ED037D0A17E36DD4(L_15, L_14, 1, (bool)0, NULL);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    C:\Git\icp.net_demo\ICP_demo_unity\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\EdjCase.ICP.Agent.cpp:3562:41: note: candidate function not viable: no known conversion from 'ReadOnlyMemory_1_t63F301BF893B0AB689953D86A641168CA66D2399' to 'ReadOnlyMemory_1_t43203E8B19A7F4A97A9A49363F67C206E8B058FE' for 2nd argument
    IL2CPP_EXTERN_C IL2CPP_METHOD_ATTR void CborReader__ctor_m5191B60B1242ACF099DE8349ED037D0A17E36DD4 (CborReader_tD16D04772AB3E74EA1866DBEC07FFFD66AC95CE5* __this, ReadOnlyMemory_1_t43203E8B19A7F4A97A9A49363F67C206E8B058FE ___0_data, int32_t ___1_conformanceMode, bool ___2_allowMultipleRootLevelValues, const RuntimeMethod* method) ;


    ```
    Where the method signature is `(ReadOnlyMemory<byte> data, CborConformanceMode conformanceMode = CborConformanceMode.Strict, bool allowMultipleRootLevelValues = false)`

    There are a few other dead forum posts that have similar issues:
    https://forum.unity.com/threads/il2...call-linking-to-addressable-function.1113328/
    https://forum.unity.com/threads/no-...l-with-il2cpp-in-unity-2019-4-8f1-lts.963761/

    Any thoughts on how to resolve this or and potential fixes that can happen.

    Using mono instead of IL2CPP has no issues
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Does your ICP.NET project include its own implementation of System.Memory.dll (maybe to support .NET Standard 2.0)? If so, Unity developers need to remove that, since Unity 2021 and later supports these types in its base class library implementation.

    The error message here suggests to me that there are duplicates of those types, which often happens when a System.Memory.dll is included in the project.
     
    Gekctek likes this.
  3. Gekctek

    Gekctek

    Joined:
    Dec 14, 2022
    Posts:
    5
    I tried that with no luck. It has the same issue.
    From what I understand this a compile time conversion issue between IL and CPP using IL2CPP. It doesnt seem to like readonly structs or something
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Yes, this does sound like it is a bug on the IL2CPP side then. Can you submit a bug report for this issue? We would love to investigate it more.

    https://unity.com/releases/editor/qa/bug-reporting
     
    Gekctek likes this.
  5. Gekctek

    Gekctek

    Joined:
    Dec 14, 2022
    Posts:
    5
    Done. Thank you
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Can you let me know the bug number? Then I can track it down in our system.
     
  7. Gekctek

    Gekctek

    Joined:
    Dec 14, 2022
    Posts:
    5
    CASE IN-37985
     
    JoshPeterson likes this.
  8. admoraguilar

    admoraguilar

    Joined:
    Sep 13, 2019
    Posts:
    9
    Hello, seems like my issue is the same. I've been trying to use ImageSharp in Unity 2022.2.14. https://github.com/SixLabors/ImageSharp/tree/release/1.0.x

    All is good in the editor but it always fails on WebGL:

    Screenshot 2023-04-15 184406.png

    Eventually managed to track it down this line after days of debugging.

    Screenshot 2023-04-15 183849.png

    So signature eventually becomes `this.EncodePixelRow<Rgb32>(ReadonlySpan<Rgb32>, IndexedImageFrame<Rgb32>, int)`.

    Seems like it was fixed for ReadOnlySpan<> fields.
    https://forum.unity.com/threads/il2cpp-readonlyspan-feilds-crash.1338875/

    Do we have a timeline for a fix if this has been already reproduced? Thanks!
     
  9. admoraguilar

    admoraguilar

    Joined:
    Sep 13, 2019
    Posts:
    9
    Just now I've done a test and I've replaced the ReadOnlySpan<TPixel> method parameters to TPixel[] and it worked. Anytime I'll hit over a Span<> or ReadOnlySpan<> method parameter I'm having "null function or function signature mismatch". So I think we can add this note to your investigation.
     
  10. Gekctek

    Gekctek

    Joined:
    Dec 14, 2022
    Posts:
    5
    I have a bug report in with Unity but it has not been reproduced at this point. Hopefully today we'll see something
     
  11. atomikm

    atomikm

    Joined:
    Feb 8, 2020
    Posts:
    20
    @JoshPeterson is there any update on the status of this fix?

    We're currently blocked from doing a major game release due to this bug.
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    It looks like our QA team is still working to reproduce it. I've just pinged them to try to expedite the process.