Skip to content

Code generation failure when using UnmanagedType.LPUTF8Str in the interop definition inside System.Private.CoreLib. #51189

@tarekgh

Description

@tarekgh

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions