P/Invoke regression in PR #2470 on OSX
PR #2470 introduced a regression on OSX when calling Marshal.AllocCoTaskMem directly from user code. It worked before, but now winds up throwing an exception with the following message:
api-ms-win-core-com-l1-1-0.dll
Stacktrace:
thread #1: tid = 0x1453d9, 0x00000001001244b2 demoapp`RhpThrowEx + 5 at ExceptionHandling.S:487, queue = 'com.apple.main-thread', stop reason = breakpoint 2.3
* frame #0: 0x00000001001244b2 demoapp`RhpThrowEx + 5 at ExceptionHandling.S:487
frame #1: 0x000000010015732b demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__FixupModuleCell + 179
frame #2: 0x0000000100140db4 demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__ResolvePInvokeSlow + 40
frame #3: 0x000000010013cc62 demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__ResolvePInvoke + 30
frame #4: 0x0000000100183ee2 demoapp`System_Private_Interop_System_Runtime_InteropServices_ExternalInterop__CoTaskMemAlloc + 34
frame #5: 0x0000000100167718 demoapp`System_Private_Interop_System_Runtime_InteropServices_Marshal__AllocCoTaskMem + 16
frame #6: 0x0000000100157501 demoapp`src_CFString__FetchString + 81
@yizhang82 Could you please take a look? Fixing this is part of https://github.com/dotnet/corert/issues/669
@jkotas is correct.
@christianscheuer Thanks for reporting the issue. Actually Marshal.AllocCoTaskMem works in OSX by accident. It is pointing to the wrong DLL. Now it is just getting exposed by the correct lazy resolution support.
#if TARGET_CORE_API_SET
internal const string CORE_COM = "api-ms-win-core-com-l1-1-0.dll";
#else
internal const string CORE_COM = "ole32.dll";
#endif
[DllImport(Libraries.CORE_COM)]
[McgGeneratedNativeCallCodeAttribute]
public static extern unsafe void* CoTaskMemAlloc(IntPtr size);
Right now we don't build System.Private.Interop for unix properly. This is the bigger problem.
I think for the most part we could avoid having a shim for S.P.Interop - we can move the x-plat stuff to S.P.CoreLib, and keep the COM/WinRT stuff in S.P.Interop.
@yizhang82 @jkotas I was able to work around the specific issue by calling malloc directly. But I'm hitting this in other places as well. Can you point me in the right direction if I may be able to help out fixing this?
By the way, I'm getting a process crash when the following RhpThrow gets caught in a catch handler that then calls Exception.ToString() to get the stack trace. Should I file that as an issue, or is it expected that RhpThrow's from bad PInvoke resolution should produce frames that will crash Exception.ToString()?
frame #0: 0x0000000100124baa demoapp`RhpThrowEx + 5 at ExceptionHandling.S:96
frame #1: 0x00000001001bbc47 demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__FixupModuleCell + 311
frame #2: 0x0000000100190bf0 demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__ResolvePInvokeSlow + 104
frame #3: 0x000000010016cc11 demoapp`System_Private_CoreLib_Internal_Runtime_CompilerHelpers_InteropHelpers__ResolvePInvoke + 97
frame #4: 0x000000010060df8a demoapp`System_Private_Interop_System_Runtime_InteropServices_ExternalInterop__MultiByteToWideChar + 86
frame #5: 0x00000001005d3a1c demoapp`System_Private_Interop_System_Runtime_InteropServices_ExternalInterop__GetCharCount + 48
frame #6: 0x0000000100593a10 demoapp`System_Private_Interop_System_Runtime_InteropServices_Marshal__ConvertToUnicode + 232
frame #7: 0x00000001006f467d demoapp`System_Private_Interop_System_Runtime_InteropServices_Marshal__PtrToStringAnsi_0 + 177
frame #8: 0x00000001006df495 demoapp`System_IO_FileSystem_Interop_Sys__GetDirectoryEntryName + 69
frame #9: 0x00000001006dd160 demoapp`System_IO_FileSystem_Interop_Sys__ReadDir + 264
frame #10: 0x00000001006d35f3 demoapp`System_IO_FileSystem_System_IO_UnixFileSystem_FileSystemEnumerable_1__Enumerate_d__11<System_Private_CoreLib_System_String>__System_IO_FileSystem_System_IO_UnixFileSystem_FileSystemEnumerable_1__Enumerate_d__11__MoveNext<> + 1187
frame #11: 0x0000000100692818 demoapp`System_IO_FileSystem_System_Collections_Generic_LargeArrayBuilder_1<System_Private_CoreLib_System_String>__System_IO_FileSystem_System_Collections_Generic_LargeArrayBuilder_1__AddRange<> + 260
frame #12: 0x0000000100657e10 demoapp`System_IO_FileSystem_System_Collections_Generic_EnumerableHelpers__System_IO_FileSystem_System_Collections_Generic_EnumerableHelpers__ToArray<System_Private_CoreLib_System_String> + 224
frame #13: 0x000000010064bff7 demoapp`System_IO_FileSystem_System_IO_Directory__InternalGetFileDirectoryNames + 551
frame #14: 0x00000001005ff28c demoapp`System_IO_FileSystem_System_IO_Directory__InternalGetFiles + 56
frame #15: 0x00000001005e4534 demoapp`System_IO_FileSystem_System_IO_Directory__GetFiles_0 + 140
is it expected that RhpThrow's from bad PInvoke resolution should produce frames that will crash Exception.ToString()?
It is not expected. File an issue on it please
@shrah Could you please look at this? It is related to the changes that you are working on.
I looked into it. The first issue with invalid P/Invoke MultiByteToWideChar, I will port those changes as part of https://github.com/dotnet/corert/issues/669.
But the second issue regarding ToString crash, I can certainly repro it in OSX and Linux. I briefly looked into it. It seems like while computing the function pointers in ComputeLdftnReverseLookupLookup it crashes. However, I couldn't set any breakpoint in any managed code using gdb on Linux or lldb on Mac. The function names show up in the frames, so I'm assuming symbols are there, but for some reason breakpoints are not hit.(https://github.com/dotnet/corert/issues/2703) So I couldn't really figure out what is going on. I talked with @tijoytom and he thinks the debugger issue may be a regression.
Fixed with https://github.com/dotnet/corert/commit/15e3e38fd80919aa759d31588ec0d40e409005ad