Search Unity

Bug IL2CPP completely ignores P/Invoke metadata

Discussion in 'Editor & General Support' started by feisuzhu, Jul 24, 2022.

  1. feisuzhu

    feisuzhu

    Joined:
    May 18, 2015
    Posts:
    9
    1.
    Code (CSharp):
    1.         [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
    2.         [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
    3.         internal static extern unsafe string git_reference_name(git_reference* reference);
    Code snippet from LibGit2Sharp.
    MarshalAs here is completely ignored, and runtime will try to free returned string, causing crash.

    2.
    Code (CSharp):
    1.         [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
    2.         internal static extern int git_repository_discover(
    3.             GitBuf buf,
    4.             [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath start_path,
    5.             [MarshalAs(UnmanagedType.Bool)] bool across_fs,
    6.             [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath ceiling_dirs);
    Code snippet from LibGit2Sharp.

    Custom marshallers are all ignored, compiled app will throw exception complaining 'dont know how to marshal FilePath'

    3.
    Code (CSharp):
    1.         [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    2.         private delegate void Py_SetPath(string home);
    3.        
    4.         public void SetPath(string home) {
    5.             var pFunc = loader.GetFunction(hLibPython, "Py_SetPath");
    6.             var func = Marshal.GetDelegateForFunctionPointer<Py_SetPath>(pFunc);
    7.             func(home);
    8.         }
    IL2CPP will just marshal the string in UTF8.


    All the three places are working normally in Mono runtime.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
  3. feisuzhu

    feisuzhu

    Joined:
    May 18, 2015
    Posts:
    9
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Isn't P/Invoke and any metadata functionality fairly closely associated with the Process class?

    Well, if it's not helpful, carry on!
     
  5. feisuzhu

    feisuzhu

    Joined:
    May 18, 2015
    Posts:
    9
    Well, no.... No processes are involved in my case:(
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Can you file a bug on this? I can't recall if we ever implemented support for CustomMarshaler.

    And yeah, P/Invoke has nothing to do with the Process class.
     
    Kurt-Dekker likes this.