dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
http://dot.net
MIT License
2.91k stars 508 forks source link

[CppCodeGen] Delegate marshalling doesn't work #6415

Open kbaladurin opened 6 years ago

kbaladurin commented 6 years ago

Following example crashes in cppgen mode:

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Hello
{
    internal class Program
    {
        delegate void Callback();

        [DllImport("libTest.so")]
        static extern int Func(Callback c);

        public static void M()
        {
            System.Console.WriteLine("M");
        }

        private static void Main(string[] args)
        {
            Callback callback = new Callback(M);

            Func(callback);
        }
    }
}
Thread 1 "Hello" received signal SIGABRT, Aborted.
0x00007ffff62a3428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x00007ffff62a3428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff62a502a in __GI_abort () at abort.c:89
#2  0x0000000000f44a0c in Assert (expr=0xf87f94 "ASSERT_UNCONDITIONALLY", file=0xf89a8e "/media/kbaladurin/data/dotnet/forked/corert_1/corert/src/Native/Runtime/portable.cpp", line_num=445, 
    message=0xf89ae3 "NYI") at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/Native/Runtime/rhassert.cpp:25
#3  0x0000000000e6de63 in RhGetCommonStubAddress () at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/Native/Runtime/portable.cpp:445
#4  0x00000000009437e9 in System_Private_CoreLib::System::Runtime::RuntimeImports::GetInteropCommonStubAddress ()
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs:383
#5  0x0000000000933558 in System_Private_CoreLib::System::Runtime::InteropServices::PInvokeMarshal::AllocateThunk (del=0x7fffd99e6630)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs:307
#6  0x0000000000933809 in System_Private_CoreLib::System::Runtime::CompilerServices::ConditionalWeakTable_2_CreateValueCallback_A__System_Private_CoreLib_System_Delegate___System_Private_CoreLib_System_Runtime_InteropServices_PInvokeMarshal_PInvokeDelegateThunk_V_::InvokeOpenStaticThunk (_a0=0x7fffd99e67b8, _a1=0x7fffd99e6630)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs:339
#7  0x0000000000943faf in System_Private_CoreLib::System::Runtime::CompilerServices::ConditionalWeakTable_2_A__System_Private_CoreLib_System_Delegate___System_Private_CoreLib_System_Runtime_InteropServices_PInvokeMarshal_PInvokeDelegateThunk_V_::GetValueLocked (___this=0x7fffd99e6660, key=0x7fffd99e6630, createValueCallback=0x7fffd99e67b8)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs:200
#8  0x0000000000933958 in System_Private_CoreLib::System::Runtime::CompilerServices::ConditionalWeakTable_2_A__System_Private_CoreLib_System_Delegate___System_Private_CoreLib_System_Runtime_InteropServices_PInvokeMarshal_PInvokeDelegateThunk_V_::GetValue (___this=0x7fffd99e6660, key=0x7fffd99e6630, createValueCallback=0x7fffd99e67b8)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs:192
#9  0x00000000008dfe5a in System_Private_CoreLib::System::Runtime::InteropServices::PInvokeMarshal::GetFunctionPointerForDelegate (del=0x7fffd99e6630)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs:210
#10 0x00000000008d4168 in System_Private_CoreLib::Internal::Runtime::CompilerHelpers::InteropHelpers::GetFunctionPointerForDelegate (del=0x7fffd99e6630)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/InteropHelpers.cs:385
#11 0x0000000000896176 in Hello::Hello::Program::Func (c=0x7fffd99e6630) at /media/kbaladurin/data/dotnet/forked/corert_1/corert/tests/src/Simple/Hello/Hello.cs:47
#12 0x000000000088f2c5 in Hello::Hello::Program::Main (args=0x7fffd99e6618) at /media/kbaladurin/data/dotnet/forked/corert_1/corert/tests/src/Simple/Hello/Hello.cs:29
#13 0x0000000000873a50 in Hello::_Module_::MainMethodWrapper (_a0=0x7fffd99e6618) at /media/kbaladurin/data/dotnet/forked/corert_1/corert/tests/src/Simple/Hello/obj/Debug/x64/native/Hello.cpp:16707566
#14 0x0000000000873642 in Hello::_Module_::StartupCodeMain (_a0=1, _a1=140737488345784)
    at /media/kbaladurin/data/dotnet/forked/corert_1/corert/tests/src/Simple/Hello/obj/Debug/x64/native/Hello.cpp:16707566
#15 0x0000000000873abb in __managed__Main (_a0=1, _a1=140737488345784) at /media/kbaladurin/data/dotnet/forked/corert_1/corert/tests/src/Simple/Hello/obj/Debug/x64/native/Hello.cpp:16707569
#16 0x0000000000e607af in main (argc=1, argv=0x7fffffffdab8) at /media/kbaladurin/data/dotnet/forked/corert_1/corert/src/Native/Bootstrap/main.cpp:376

Generated code for Func:

int32_t Hello::Hello::Program::Func(::Hello::Hello::Program_Callback* c)
{
        int32_t _l0 = 0;
        int32_t _l1 = 0;
        intptr_t _l2 = 0;

_bb0: {

        void* _1 = c;
        intptr_t _2 = ::System_Private_CoreLib::Internal::Runtime::CompilerHelpers::InteropHelpers::GetFunctionPointerForDelegate((::System_Private_CoreLib::System::Delegate*)_1);
        _l2 = _2;
        intptr_t _3 = _l2;
        PInvokeTransitionFrame __piframe;
        __pinvoke(&__piframe);
        int32_t _4 = ::Hello::Hello::Program::_A_rawpinvoke_V_Func(_3);
        __pinvoke_return(&__piframe);
        _l1 = _4;
        int32_t _5 = _l1;
        _l0 = _5;
        void* _6 = c;
        ::System_Private_CoreLib::System::GC::KeepAlive((::System_Private_CoreLib::System::Object*)_6);
        int32_t _7 = _l0;
        return _7;
}
}

As far as I understand we shouldn't call GetFunctionPointerForDelegate here because it implements general delegate marshalling (for both instance and static methods) and we don't want to support instance ones (https://github.com/dotnet/corert/issues/3294#issuecomment-330319017).

kbaladurin commented 6 years ago

@iarischenko @alpencolt @sergign60

cshung commented 5 years ago

The repro code no longer compiles:

SHOVON-X1CT:lab andrewau$ dotnet publish /p:NativeCodeGen=cpp -r osx-x64
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 42.19 ms for /Users/andrewau/lab/lab.csproj.
  lab -> /Users/andrewau/lab/bin/Debug/netcoreapp2.1/osx-x64/lab.dll
  /usr/bin/clang
obj/Debug/netcoreapp2.1/osx-x64/native/lab.cpp(25561,15): error GB553787D: redefinition of '__slot__Invoke' [/Users/andrewau/lab/lab.csproj]
  typedef void(*__slot__Invoke)(::lab::Hello::Program_Callback*);
                ^
  obj/Debug/netcoreapp2.1/osx-x64/native/lab.cpp:25543:16: note: previous definition is here
          typedef void(*__slot__Invoke)(::lab::Hello::Program_Callback*);
                        ^
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1171,36): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _43 = __load_string_literal("\X1B]0;%p1%s\X07");return (::System_Private_CoreLib::System::String*)_43;}_bb335: {
                                     ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1171,48): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _43 = __load_string_literal("\X1B]0;%p1%s\X07");return (::System_Private_CoreLib::System::String*)_43;}_bb335: {
                                                 ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1173,36): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _33 = __load_string_literal("\X1B];%p1%s\X07");return (::System_Private_CoreLib::System::String*)_33;}_bb341: {
                                     ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1173,47): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _33 = __load_string_literal("\X1B];%p1%s\X07");return (::System_Private_CoreLib::System::String*)_33;}_bb341: {
                                                ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1175,36): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _39 = __load_string_literal("\X1B]30;%p1%s\X07");return (::System_Private_CoreLib::System::String*)_39;}_bb347: {
                                     ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1175,49): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _39 = __load_string_literal("\X1B]30;%p1%s\X07");return (::System_Private_CoreLib::System::String*)_39;}_bb347: {
                                                  ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1177,36): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _29 = __load_string_literal("\X1Bk%p1%s\X1B");return (::System_Private_CoreLib::System::String*)_29;}_bb353: {
                                     ^~
/_/src/System.Console/src/System/ConsolePal.Unix.cs(1177,46): warning GCAD25DD9: unknown escape sequence '\X' [-Wunknown-escape-sequence] [/Users/andrewau/lab/lab.csproj]
  void* _29 = __load_string_literal("\X1Bk%p1%s\X1B");return (::System_Private_CoreLib::System::String*)_29;}_bb353: {
                                               ^~
  8 warnings and 1 error generated.
/Users/andrewau/git/corert/bin/OSX.x64.Debug/build/Microsoft.NETCore.Native.targets(268,5): error MSB3073: The command "clang "obj/Debug/netcoreapp2.1/osx-x64/native/lab.cpp" -o "obj/Debug/netcoreapp2.1/osx-x64/native/lab.o" -I /Users/andrewau/git/corert/bin/OSX.x64.Debug//inc -g -O0 -c -Wno-invalid-offsetof" exited with code 1. [/Users/andrewau/lab/lab.csproj]
SHOVON-X1CT:lab andrewau$
cshung commented 5 years ago

The build error is fixed with #7622, the warnings related to \X is fixed in #7635.