Repository navigation
Code generation failure when using UnmanagedType.LPUTF8Str in the interop definition inside System.Private.CoreLib. #51189
Copy link
Copy link
Closed
Labels
Milestone
Description
Trying to define pinvoke like the following inside System.Private.CoreLib
[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_WindowsIdToIanaId")]
internal static extern unsafe int WindowsIdToIanaId(string windowsId, [MarshalAs(UnmanagedType.LPUTF8Str)] string? region, char* ianaId, int ianaIdLength);
I am getting the following error. Looks because the usage of UnmanagedType.LPUTF8Str. Changing to UnmanagedType.LPStr will make it work but could be wrong if we expect UTF8 string.
EXEC : error : One or more errors occurred. (Code generation failed for method '[S.P.CoreLib]System.TimeZoneInfo.TryConvertWindowsIdToIanaId(string,string,bool,string&)') [C:\oss\runtime\src\coreclr\crossgen-corelib.proj]
System.AggregateException: One or more errors occurred. (Code generation failed for method '[S.P.CoreLib]System.TimeZoneInfo.TryConvertWindowsIdToIanaId(string,string,bool,string&)')
---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[S.P.CoreLib]System.TimeZoneInfo.TryConvertWindowsIdToIanaId(string,string,bool,string&)'
---> System.InvalidOperationException: Expected type 'Internal.Runtime.CompilerHelpers.InteropHelpers' not found in module 'System.Private.CoreLib'
at Internal.IL.HelperExtensions.GetKnownType(ModuleDesc module, String namespace, String name)
at Internal.IL.HelperExtensions.GetHelperType(TypeSystemContext context, String name)
at Internal.IL.HelperExtensions.GetHelperEntryPoint(TypeSystemContext context, String typeName, String methodName)
at Internal.TypeSystem.Interop.UTF8StringMarshaller.TransformManagedToNative(ILCodeStream codeStream)
at Internal.TypeSystem.Interop.Marshaller.AllocAndTransformManagedToNative(ILCodeStream codeStream)
at Internal.TypeSystem.Interop.Marshaller.EmitMarshalArgumentManagedToNative()
at Internal.TypeSystem.Interop.Marshaller.EmitForwardArgumentMarshallingIL()
at Internal.TypeSystem.Interop.Marshaller.EmitArgumentMarshallingIL()
at Internal.TypeSystem.Interop.Marshaller.EmitMarshallingIL(PInvokeILCodeStreams pInvokeILCodeStreams)
at Internal.IL.Stubs.PInvokeILEmitter.EmitIL()
at Internal.IL.Stubs.PInvokeILEmitter.EmitIL(MethodDesc method)
at ILCompiler.Compilation.ILCache.CreateValueFromKey(MethodDesc key)
at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key)
at ILCompiler.Compilation.GetMethodIL(MethodDesc method)
at Internal.JitInterface.CorInfoImpl.pInvokeMarshalingRequired(CORINFO_METHOD_STRUCT_* handle, CORINFO_SIG_INFO* callSiteSig)
at Internal.JitInterface.CorInfoImpl._pInvokeMarshalingRequired(IntPtr thisHandle, IntPtr* ppException, CORINFO_METHOD_STRUCT_* method, CORINFO_SIG_INFO* callSiteSig)
--- End of inner exception stack trace ---
at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL)
at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger)
at ILCompiler.ReadyToRunCodegenCompilation.<ComputeDependencyNodeDependencies>b__36_0(DependencyNodeCore`1 dependency)
at System.Threading.Tasks.Parallel.<>c__DisplayClass33_0`2.<ForEachWorker>b__0(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
at System.Threading.Tasks.TaskReplicator.Replica`1.ExecuteAction(Boolean& yieldedBeforeCompletion)
at System.Threading.Tasks.TaskReplicator.Replica.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure)
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException)
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IList`1 list, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
at ILCompiler.ReadyToRunCodegenCompilation.ComputeDependencyNodeDependencies(List`1 obj)
at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes()
at ILCompiler.ReadyToRunCodegenCompilation.Compile(String outputFile)
at ILCompiler.Program.Run(String[] args)
at ILCompiler.Program.Main(String[] args)
Reactions are currently unavailable
Activity
tarekgh commentedon Apr 13, 2021
CC @jkoritzinsky
mangod9 commentedon Apr 15, 2021
cc @trylek @davidwrighton. @tarekgh is there a standalone repro for this?
tarekgh commentedon Apr 15, 2021
Try to change the line
runtime/src/libraries/Common/src/Interop/Interop.TimeZoneInfo.cs
Line 19 in c2f2a1c
LPStrwithLPUTF8Strand then recompile and you should get the failure.mangod9 commentedon Jul 15, 2021
@tarekgh the bug is fixed, but looks like the pinvoke call you list above doesnt have
MarshalAsattribute anymore. Was that done due to this bug?tarekgh commentedon Jul 15, 2021
No. we have just changed it for some other reason.
mangod9 commentedon Jul 15, 2021
Does it still need to change back to using MarshalAs, just want to ensure the fix works for your case.
tarekgh commentedon Jul 16, 2021
@mangod9
We don't change anything back. The current way is what we need to keep. I'll let you know if something changes in the future. For now, we should be good.
mangod9 commentedon Jul 16, 2021
sounds good. Thx