Skip to content

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

Closed

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)

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Apr 13, 2021
  2. tarekgh commented on Apr 13, 2021

    @tarekgh
    MemberAuthor
  3. removed
    untriagedNew issue has not been triaged by the area owner
    on Apr 15, 2021
  4. added this to the 6.0.0 milestone on Apr 15, 2021
  5. mangod9 commented on Apr 15, 2021

    @mangod9
    Member

    cc @trylek @davidwrighton. @tarekgh is there a standalone repro for this?

  6. tarekgh commented on Apr 15, 2021

    @tarekgh
    MemberAuthor

    is there a standalone repro for this?

    Try to change the line

    internal static extern unsafe int WindowsIdToIanaId(string windowsId, [MarshalAs(UnmanagedType.LPStr)] string? region, char* ianaId, int ianaIdLength);
    , replaceLPStr with LPUTF8Str and then recompile and you should get the failure.

  7. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 14, 2021
  8. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 15, 2021
  9. mangod9 commented on Jul 15, 2021

    @mangod9
    Member

    @tarekgh the bug is fixed, but looks like the pinvoke call you list above doesnt have MarshalAs attribute anymore. Was that done due to this bug?

  10. tarekgh commented on Jul 15, 2021

    @tarekgh
    MemberAuthor

    the bug is fixed, but looks like the pinvoke call you list above doesnt have MarshalAs attribute anymore. Was that done due to this bug?

    No. we have just changed it for some other reason.

  11. mangod9 commented on Jul 15, 2021

    @mangod9
    Member

    Does it still need to change back to using MarshalAs, just want to ensure the fix works for your case.

  12. tarekgh commented on Jul 16, 2021

    @tarekgh
    MemberAuthor

    @mangod9

    Does it still need to change back to using MarshalAs, just want to ensure the fix works for your case.

    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.

  13. mangod9 commented on Jul 16, 2021

    @mangod9
    Member

    sounds good. Thx

  14. ghost locked as resolved and limited conversation to collaborators on Aug 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions